-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_gui_utilities.py
150 lines (120 loc) · 6.3 KB
/
test_gui_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# _ __ _ ___ _ ___ _ _
# | |/ /_ _ __ _| |_ ___ __/ __| __ _| |___ _ __ ___| _ \ |_ _ __ _(_)_ _
# | ' <| '_/ _` | _/ _ (_-<__ \/ _` | / _ \ ' \/ -_) _/ | || / _` | | ' \
# |_|\_\_| \__,_|\__\___/__/___/\__,_|_\___/_|_|_\___|_| |_|\_,_\__, |_|_||_|
# |___/
# License: BSD License ; see LICENSE
#
# Main authors: Philipp Bucher (https://github.com/philbucher)
#
# set up testing environment (before anything else)
import initialize_testing_environment
# python imports
import unittest
from pathlib import Path
from sys import executable, platform
from subprocess import Popen, PIPE
import locale
# plugin imports
from kratos_salome_plugin.gui import utilities
# tests imports
from testing_utilities import QtTestCase
# qt imports
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest
"""for currently unknown reasons testing QMessageBox doesn't play nice with
the tests for QMainWindow. Due to this they are currently being executed in a separate
process if run together with other tests
"""
if __name__ == '__main__':
# test is run standalone
class TestCreateInformativeMessageBox(QtTestCase):
"""TODO add a test where the close ( x ) button is pressed => figure out how to trigger"""
def test_wrong_icon(self):
wrong_name = "random_icon_non_existing"
with self.assertRaisesRegex(AttributeError, 'The requested icon "{}" does not exist.\nOnly the following icons are available:'.format(wrong_name)):
utilities.CreateInformativeMessageBox("my_text", wrong_name)
def test_basics(self):
my_text = "some_text that has nothing"
def CheckFct(message_box):
SingleShotOkKlick(message_box)
self.assertEqual(message_box.text(), my_text)
self.assertEqual(message_box.informativeText(), "")
self.assertEqual(message_box.detailedText(), "")
utilities.CreateInformativeMessageBox(my_text, "NoIcon", fct_ptr=CheckFct)
def test_informative_text(self):
my_text = "some_text that has a little bit"
inform_text = "Here some more info is provided!"
def CheckFct(message_box):
SingleShotOkKlick(message_box)
self.assertEqual(message_box.text(), my_text)
self.assertEqual(message_box.informativeText(), inform_text)
self.assertEqual(message_box.detailedText(), "")
utilities.CreateInformativeMessageBox(my_text, "NoIcon", inform_text, fct_ptr=CheckFct)
def test_detailed_text(self):
my_text = "some_text that has a a lot!"
inform_text = "Here some more info is kinda provided!"
det_text = "This is some pretty detailed information\nEven more!!"
def CheckFct(message_box):
SingleShotOkKlick(message_box)
self.assertEqual(message_box.text(), my_text)
self.assertEqual(message_box.informativeText(), inform_text)
self.assertEqual(message_box.detailedText(), det_text)
utilities.CreateInformativeMessageBox(my_text, "NoIcon", inform_text, det_text, fct_ptr=CheckFct)
def test_ok_key(self):
base_args = ["some text", "NoIcon"] # those are always needed
additional_args = [
[],
["inform_Text"],
["inform_Text", "detailed_text"]
]
for args in additional_args:
all_args = base_args+args
with self.subTest(all_args=all_args):
mbx = utilities.CreateInformativeMessageBox(*all_args, fct_ptr=SingleShotOkKlick) # args need to be passed then it blocks!
# only pass detailed but not informative text
with self.subTest(detailed_text=additional_args[2][1]):
mbx = utilities.CreateInformativeMessageBox(*base_args, detailed_text=additional_args[2][1], fct_ptr=SingleShotOkKlick) # args need to be passed then it blocks!
def test_escape(self):
base_args = ["some text", "NoIcon"] # those are always needed
additional_args = [
[],
["inform_Text"],
["inform_Text", "detailed_text"]
]
for args in additional_args:
all_args = base_args+args
with self.subTest(all_args=all_args):
utilities.CreateInformativeMessageBox(*all_args, fct_ptr=SingleShotEscape) # args need to be passed then it blocks!
# only pass detailed but not informative text
with self.subTest(detailed_text=additional_args[2][1]):
mbx = utilities.CreateInformativeMessageBox(*base_args, detailed_text=additional_args[2][1], fct_ptr=SingleShotEscape) # args need to be passed then it blocks!
def SingleShotOkKlick(message_box):
"""aux function to close messagebox"""
close_delay = 0 # [ms]
ok_btn = message_box.button(QMessageBox.Ok)
QTimer.singleShot(close_delay, ok_btn.click)
def SingleShotEscape(message_box):
"""aux function to close messagebox"""
close_delay = 0 # [ms]
QTimer.singleShot(close_delay, lambda: QTest.keyClick(message_box, Qt.Key_Escape))
unittest.main()
else:
# test is run with others
class TestCreateInformativeMessageBoxInSubprocess(unittest.TestCase):
def test_in_subprocess(self):
if "win" in platform:
self.skipTest('This test is currenlty not working in windows (This plugin does not support propagateSizeHints())')
def GetProcessOutput(proc_stdout, proc_stderr):
msg = "\nStdOut:\n"
if proc_stdout:
msg += proc_stdout.decode(locale.getpreferredencoding())
msg += "\nStdErr:\n"
if proc_stderr:
msg += proc_stderr.decode(locale.getpreferredencoding())
return msg
proc = Popen([executable, str(Path(__file__))], stdout=PIPE, stderr=PIPE, cwd=str(Path(__file__).parent))
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0, msg=GetProcessOutput(stdout, stderr))