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

ESS_GUI
Last change on this file was 53c771e, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Converted unit tests

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[87cc73a]1import sys
2import unittest
[7fb471d]3from unittest.mock import MagicMock
[87cc73a]4
[53c771e]5from PyQt5 import QtGui, QtWidgets
[87cc73a]6
7# set up import paths
8import path_prepare
9
10# Local
[83eb5208]11from sas.qtgui.Plotting.PlotProperties import PlotProperties
[87cc73a]12
[53c771e]13if not QtWidgets.QApplication.instance():
14    app = QtWidgets.QApplication(sys.argv)
[87cc73a]15
16class PlotPropertiesTest(unittest.TestCase):
17    '''Test the PlotProperties'''
18    def setUp(self):
19        '''Create the PlotProperties'''
20
[239214f]21        self.widget = PlotProperties(None,
[db5cd8d]22                         color=1,
23                         marker=3,
24                         marker_size=10,
25                         legend="LL")
[87cc73a]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'''
[53c771e]34        self.assertIsInstance(self.widget, QtWidgets.QDialog)
[87cc73a]35        self.assertEqual(self.widget.windowTitle(), "Modify Plot Properties")
[db5cd8d]36
37        # Check the combo boxes
38        self.assertEqual(self.widget.cbColor.currentText(), "Green")
[239214f]39        self.assertEqual(self.widget.cbColor.count(), 7)
[db5cd8d]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)
[239214f]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)
[87cc73a]54       
55    def testOnColorChange(self):
56        '''Test the response to color change event'''
[db5cd8d]57        # Accept the new color
[53c771e]58        QtWidgets.QColorDialog.getColor = MagicMock(return_value=QtGui.QColor(255, 0, 255))
[db5cd8d]59
[0f3c22d]60        self.widget.onColorChange()
[db5cd8d]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
[53c771e]74        QtWidgets.QColorDialog.getColor = MagicMock(return_value=bad_color)
[0f3c22d]75        self.widget.onColorChange()
[db5cd8d]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
[87cc73a]82
83    def testOnColorIndexChange(self):
84        '''Test the response to color index change event'''
[db5cd8d]85        # Intitial population of the color combo box
[0f3c22d]86        self.widget.onColorIndexChange()
[db5cd8d]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
[0f3c22d]97        self.widget.onColorIndexChange()
[db5cd8d]98        # see that the Custom entry disappeared
99        self.assertEqual(self.widget.cbColor.count(), 7)
100        self.assertEqual(self.widget.cbColor.findText("Custom"), -1)
[87cc73a]101
102if __name__ == "__main__":
103    unittest.main()
Note: See TracBrowser for help on using the repository browser.