FreeCAD
131 строка · 6.2 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24import functools25import unittest26
27from PySide import QtCore, QtWidgets28
29import FreeCAD30
31from AddonManagerTest.gui.gui_mocks import (32DialogWatcher,33FakeWorker,34MockThread,35)
36from AddonManagerTest.app.mocks import MockAddon37
38from addonmanager_uninstaller_gui import AddonUninstallerGUI39
40translate = FreeCAD.Qt.translate41
42
43class TestUninstallerGUI(unittest.TestCase):44
45MODULE = "test_uninstaller_gui" # file name without extension46
47def setUp(self):48self.addon_to_remove = MockAddon()49self.uninstaller_gui = AddonUninstallerGUI(self.addon_to_remove)50self.finalized_thread = False51self.signals_caught = []52
53def tearDown(self):54pass55
56def catch_signal(self, signal_name, *_):57self.signals_caught.append(signal_name)58
59def test_confirmation_dialog_yes(self):60dialog_watcher = DialogWatcher(61translate("AddonsInstaller", "Confirm remove"),62QtWidgets.QDialogButtonBox.Yes,63)64answer = self.uninstaller_gui._confirm_uninstallation()65self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")66self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")67self.assertTrue(answer, "Expected a 'Yes' click to return True, but got False")68
69def test_confirmation_dialog_cancel(self):70dialog_watcher = DialogWatcher(71translate("AddonsInstaller", "Confirm remove"),72QtWidgets.QDialogButtonBox.Cancel,73)74answer = self.uninstaller_gui._confirm_uninstallation()75self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")76self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")77self.assertFalse(answer, "Expected a 'Cancel' click to return False, but got True")78
79def test_progress_dialog(self):80dialog_watcher = DialogWatcher(81translate("AddonsInstaller", "Removing Addon"),82QtWidgets.QDialogButtonBox.Cancel,83)84self.uninstaller_gui._show_progress_dialog()85# That call isn't modal, so spin our own event loop:86while self.uninstaller_gui.progress_dialog.isVisible():87QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 100)88self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")89self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")90
91def test_timer_launches_progress_dialog(self):92worker = FakeWorker()93dialog_watcher = DialogWatcher(94translate("AddonsInstaller", "Removing Addon"),95QtWidgets.QDialogButtonBox.Cancel,96)97QtCore.QTimer.singleShot(1000, worker.stop) # If the test fails, this kills the "worker"98self.uninstaller_gui._confirm_uninstallation = lambda: True99self.uninstaller_gui._run_uninstaller = worker.work100self.uninstaller_gui._finalize = lambda: None101self.uninstaller_gui.dialog_timer.setInterval(1) # To speed up the test, only wait 1ms102self.uninstaller_gui.run() # Blocks once it hits the fake worker103self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")104self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")105worker.stop()106
107def test_success_dialog(self):108dialog_watcher = DialogWatcher(109translate("AddonsInstaller", "Uninstall complete"),110QtWidgets.QDialogButtonBox.Ok,111)112self.uninstaller_gui._succeeded(self.addon_to_remove)113self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")114self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")115
116def test_failure_dialog(self):117dialog_watcher = DialogWatcher(118translate("AddonsInstaller", "Uninstall failed"),119QtWidgets.QDialogButtonBox.Ok,120)121self.uninstaller_gui._failed(122self.addon_to_remove, "Some failure message\nAnother failure message"123)124self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")125self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")126
127def test_finalize(self):128self.uninstaller_gui.finished.connect(functools.partial(self.catch_signal, "finished"))129self.uninstaller_gui.worker_thread = MockThread()130self.uninstaller_gui._finalize()131self.assertIn("finished", self.signals_caught)132