FreeCAD

Форк
0
/
test_uninstaller_gui.py 
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

24
import functools
25
import unittest
26

27
from PySide import QtCore, QtWidgets
28

29
import FreeCAD
30

31
from AddonManagerTest.gui.gui_mocks import (
32
    DialogWatcher,
33
    FakeWorker,
34
    MockThread,
35
)
36
from AddonManagerTest.app.mocks import MockAddon
37

38
from addonmanager_uninstaller_gui import AddonUninstallerGUI
39

40
translate = FreeCAD.Qt.translate
41

42

43
class TestUninstallerGUI(unittest.TestCase):
44

45
    MODULE = "test_uninstaller_gui"  # file name without extension
46

47
    def setUp(self):
48
        self.addon_to_remove = MockAddon()
49
        self.uninstaller_gui = AddonUninstallerGUI(self.addon_to_remove)
50
        self.finalized_thread = False
51
        self.signals_caught = []
52

53
    def tearDown(self):
54
        pass
55

56
    def catch_signal(self, signal_name, *_):
57
        self.signals_caught.append(signal_name)
58

59
    def test_confirmation_dialog_yes(self):
60
        dialog_watcher = DialogWatcher(
61
            translate("AddonsInstaller", "Confirm remove"),
62
            QtWidgets.QDialogButtonBox.Yes,
63
        )
64
        answer = self.uninstaller_gui._confirm_uninstallation()
65
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
66
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
67
        self.assertTrue(answer, "Expected a 'Yes' click to return True, but got False")
68

69
    def test_confirmation_dialog_cancel(self):
70
        dialog_watcher = DialogWatcher(
71
            translate("AddonsInstaller", "Confirm remove"),
72
            QtWidgets.QDialogButtonBox.Cancel,
73
        )
74
        answer = self.uninstaller_gui._confirm_uninstallation()
75
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
76
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
77
        self.assertFalse(answer, "Expected a 'Cancel' click to return False, but got True")
78

79
    def test_progress_dialog(self):
80
        dialog_watcher = DialogWatcher(
81
            translate("AddonsInstaller", "Removing Addon"),
82
            QtWidgets.QDialogButtonBox.Cancel,
83
        )
84
        self.uninstaller_gui._show_progress_dialog()
85
        # That call isn't modal, so spin our own event loop:
86
        while self.uninstaller_gui.progress_dialog.isVisible():
87
            QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 100)
88
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
89
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
90

91
    def test_timer_launches_progress_dialog(self):
92
        worker = FakeWorker()
93
        dialog_watcher = DialogWatcher(
94
            translate("AddonsInstaller", "Removing Addon"),
95
            QtWidgets.QDialogButtonBox.Cancel,
96
        )
97
        QtCore.QTimer.singleShot(1000, worker.stop)  # If the test fails, this kills the "worker"
98
        self.uninstaller_gui._confirm_uninstallation = lambda: True
99
        self.uninstaller_gui._run_uninstaller = worker.work
100
        self.uninstaller_gui._finalize = lambda: None
101
        self.uninstaller_gui.dialog_timer.setInterval(1)  # To speed up the test, only wait 1ms
102
        self.uninstaller_gui.run()  # Blocks once it hits the fake worker
103
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
104
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
105
        worker.stop()
106

107
    def test_success_dialog(self):
108
        dialog_watcher = DialogWatcher(
109
            translate("AddonsInstaller", "Uninstall complete"),
110
            QtWidgets.QDialogButtonBox.Ok,
111
        )
112
        self.uninstaller_gui._succeeded(self.addon_to_remove)
113
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
114
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
115

116
    def test_failure_dialog(self):
117
        dialog_watcher = DialogWatcher(
118
            translate("AddonsInstaller", "Uninstall failed"),
119
            QtWidgets.QDialogButtonBox.Ok,
120
        )
121
        self.uninstaller_gui._failed(
122
            self.addon_to_remove, "Some failure message\nAnother failure message"
123
        )
124
        self.assertTrue(dialog_watcher.dialog_found, "Failed to find the expected dialog box")
125
        self.assertTrue(dialog_watcher.button_found, "Failed to find the expected button")
126

127
    def test_finalize(self):
128
        self.uninstaller_gui.finished.connect(functools.partial(self.catch_signal, "finished"))
129
        self.uninstaller_gui.worker_thread = MockThread()
130
        self.uninstaller_gui._finalize()
131
        self.assertIn("finished", self.signals_caught)
132

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.