source: sasview/src/sas/qtgui/Plotting/UnitTesting/PlotPropertiesTest.py @ cd4f421

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since cd4f421 was 464cd07, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Use singleton QApplication in unit tests to avoid issues on Ubuntu. SASVIEW-485

  • Property mode set to 100644
File size: 3.7 KB
Line 
1import sys
2import unittest
3from mock import MagicMock
4
5from PyQt4 import QtGui
6
7# set up import paths
8import path_prepare
9
10# Local
11from sas.qtgui.Plotting.PlotProperties import PlotProperties
12
13if not QtGui.QApplication.instance():
14    app = QtGui.QApplication(sys.argv)
15
16class 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, QtGui.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        QtGui.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        QtGui.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
102if __name__ == "__main__":
103    unittest.main()
Note: See TracBrowser for help on using the repository browser.