[87cc73a] | 1 | import sys |
---|
| 2 | import unittest |
---|
[db5cd8d] | 3 | from mock import MagicMock |
---|
[87cc73a] | 4 | |
---|
| 5 | from PyQt4 import QtGui |
---|
| 6 | |
---|
| 7 | # set up import paths |
---|
| 8 | import path_prepare |
---|
| 9 | |
---|
| 10 | # Local |
---|
[83eb5208] | 11 | from sas.qtgui.Plotting.PlotProperties import PlotProperties |
---|
[87cc73a] | 12 | |
---|
[464cd07] | 13 | if not QtGui.QApplication.instance(): |
---|
| 14 | app = QtGui.QApplication(sys.argv) |
---|
[87cc73a] | 15 | |
---|
| 16 | class 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''' |
---|
| 34 | self.assertIsInstance(self.widget, QtGui.QDialog) |
---|
| 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 |
---|
| 58 | QtGui.QColorDialog.getColor = MagicMock(return_value=QtGui.QColor(255, 0, 255)) |
---|
| 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 |
---|
| 74 | QtGui.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 | |
---|
| 102 | if __name__ == "__main__": |
---|
| 103 | unittest.main() |
---|