1 | import sys |
---|
2 | import unittest |
---|
3 | from unittest.mock import MagicMock |
---|
4 | |
---|
5 | from PyQt5 import QtGui, QtWidgets |
---|
6 | |
---|
7 | # set up import paths |
---|
8 | import path_prepare |
---|
9 | |
---|
10 | # Local |
---|
11 | from sas.qtgui.Plotting.PlotProperties import PlotProperties |
---|
12 | |
---|
13 | if not QtWidgets.QApplication.instance(): |
---|
14 | app = QtWidgets.QApplication(sys.argv) |
---|
15 | |
---|
16 | class PlotPropertiesTest(unittest.TestCase): |
---|
17 | '''Test the PlotProperties''' |
---|
18 | def setUp(self): |
---|
19 | '''Create the PlotProperties''' |
---|
20 | |
---|
21 | self.widget = PlotProperties(None, |
---|
22 | color=1, |
---|
23 | marker=3, |
---|
24 | marker_size=10, |
---|
25 | legend="LL") |
---|
26 | |
---|
27 | def tearDown(self): |
---|
28 | '''Destroy the GUI''' |
---|
29 | self.widget.close() |
---|
30 | self.widget = None |
---|
31 | |
---|
32 | def testDefaults(self): |
---|
33 | '''Test the GUI in its default state''' |
---|
34 | self.assertIsInstance(self.widget, QtWidgets.QDialog) |
---|
35 | self.assertEqual(self.widget.windowTitle(), "Modify Plot Properties") |
---|
36 | |
---|
37 | # Check the combo boxes |
---|
38 | self.assertEqual(self.widget.cbColor.currentText(), "Green") |
---|
39 | self.assertEqual(self.widget.cbColor.count(), 7) |
---|
40 | self.assertEqual(self.widget.cbShape.currentText(), "Triangle Down") |
---|
41 | self.assertEqual(self.widget.txtLegend.text(), "LL") |
---|
42 | self.assertEqual(self.widget.sbSize.value(), 10) |
---|
43 | |
---|
44 | def testDefaultsWithCustomColor(self): |
---|
45 | '''Test the GUI when called with custom color''' |
---|
46 | widget = PlotProperties(None, |
---|
47 | color="#FF00FF", |
---|
48 | marker=7, |
---|
49 | marker_size=10, |
---|
50 | legend="LL") |
---|
51 | |
---|
52 | self.assertEqual(widget.cbColor.currentText(), "Custom") |
---|
53 | self.assertEqual(widget.cbColor.count(), 8) |
---|
54 | |
---|
55 | def testOnColorChange(self): |
---|
56 | '''Test the response to color change event''' |
---|
57 | # Accept the new color |
---|
58 | QtWidgets.QColorDialog.getColor = MagicMock(return_value=QtGui.QColor(255, 0, 255)) |
---|
59 | |
---|
60 | self.widget.onColorChange() |
---|
61 | |
---|
62 | self.assertEqual(self.widget.color(), "#ff00ff") |
---|
63 | self.assertTrue(self.widget.custom_color) |
---|
64 | self.assertEqual(self.widget.cbColor.currentIndex(), 7) |
---|
65 | self.assertEqual(self.widget.cbColor.currentText(), "Custom") |
---|
66 | |
---|
67 | # Reset the color - this will remove "Custom" from the combobox |
---|
68 | # and set its index to "Green" |
---|
69 | self.widget.cbColor.setCurrentIndex(1) |
---|
70 | self.assertEqual(self.widget.cbColor.currentText(), "Green") |
---|
71 | |
---|
72 | # Cancel the dialog now |
---|
73 | bad_color = QtGui.QColor() # constructs an invalid color |
---|
74 | QtWidgets.QColorDialog.getColor = MagicMock(return_value=bad_color) |
---|
75 | self.widget.onColorChange() |
---|
76 | |
---|
77 | self.assertEqual(self.widget.color(), 1) |
---|
78 | self.assertFalse(self.widget.custom_color) |
---|
79 | self.assertEqual(self.widget.cbColor.currentIndex(), 1) |
---|
80 | self.assertEqual(self.widget.cbColor.currentText(), "Green") |
---|
81 | |
---|
82 | |
---|
83 | def testOnColorIndexChange(self): |
---|
84 | '''Test the response to color index change event''' |
---|
85 | # Intitial population of the color combo box |
---|
86 | self.widget.onColorIndexChange() |
---|
87 | self.assertEqual(self.widget.cbColor.count(), 7) |
---|
88 | # Block the callback so we can update the cb |
---|
89 | self.widget.cbColor.blockSignals(True) |
---|
90 | # Add the Custom entry |
---|
91 | self.widget.cbColor.addItems(["Custom"]) |
---|
92 | # Unblock the callback |
---|
93 | self.widget.cbColor.blockSignals(False) |
---|
94 | # Assert the new CB |
---|
95 | self.assertEqual(self.widget.cbColor.count(), 8) |
---|
96 | # Call the method |
---|
97 | self.widget.onColorIndexChange() |
---|
98 | # see that the Custom entry disappeared |
---|
99 | self.assertEqual(self.widget.cbColor.count(), 7) |
---|
100 | self.assertEqual(self.widget.cbColor.findText("Custom"), -1) |
---|
101 | |
---|
102 | if __name__ == "__main__": |
---|
103 | unittest.main() |
---|