source: sasview/src/sas/qtgui/UnitTesting/ColorMapTest.py @ 03c372d

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 03c372d was 03c372d, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Added unit tests to SASVIEW-391

  • Property mode set to 100755
File size: 5.3 KB
Line 
1import sys
2import unittest
3import numpy
4
5from PyQt4 import QtGui
6from mock import MagicMock
7import matplotlib as mpl
8
9# set up import paths
10import path_prepare
11
12from sas.sasgui.guiframe.dataFitting import Data2D
13import sas.qtgui.Plotter2D as Plotter2D
14from UnitTesting.TestUtils import WarningTestNotImplemented
15
16# Local
17from sas.qtgui.ColorMap import ColorMap
18
19app = QtGui.QApplication(sys.argv)
20
21class ColorMapTest(unittest.TestCase):
22    '''Test the ColorMap'''
23    def setUp(self):
24        '''Create the ColorMap'''
25        self.plotter = Plotter2D.Plotter2D(None, quickplot=True)
26
27        self.data = Data2D(image=[0.1]*4,
28                           qx_data=[1.0, 2.0, 3.0, 4.0],
29                           qy_data=[10.0, 11.0, 12.0, 13.0],
30                           dqx_data=[0.1, 0.2, 0.3, 0.4],
31                           dqy_data=[0.1, 0.2, 0.3, 0.4],
32                           q_data=[1,2,3,4],
33                           xmin=-1.0, xmax=5.0,
34                           ymin=-1.0, ymax=15.0,
35                           zmin=-1.0, zmax=20.0)
36
37        self.data.title="Test data"
38        self.data.id = 1
39        self.widget = ColorMap(parent=self.plotter, data=self.data)
40
41    def tearDown(self):
42        '''Destroy the GUI'''
43        self.widget.close()
44        self.widget = None
45
46    def testDefaults(self):
47        '''Test the GUI in its default state'''
48        self.assertIsInstance(self.widget, QtGui.QDialog)
49
50        self.assertEqual(self.widget._cmap_orig, "jet")
51        self.assertEqual(len(self.widget.all_maps), 144)
52        self.assertEqual(len(self.widget.maps), 72)
53        self.assertEqual(len(self.widget.rmaps), 72)
54
55        self.assertEqual(self.widget.lblWidth.text(), "0")
56        self.assertEqual(self.widget.lblHeight.text(), "0")
57        self.assertEqual(self.widget.lblQmax.text(), "15.8")
58        self.assertEqual(self.widget.lblStopRadius.text(), "-1")
59        self.assertFalse(self.widget.chkReverse.isChecked())
60        self.assertEqual(self.widget.cbColorMap.count(), 72)
61        self.assertEqual(self.widget.cbColorMap.currentIndex(), 60)
62
63        # validators
64        self.assertIsInstance(self.widget.txtMinAmplitude.validator(), QtGui.QDoubleValidator)
65        self.assertIsInstance(self.widget.txtMaxAmplitude.validator(), QtGui.QDoubleValidator)
66
67    def testOnReset(self):
68        '''Check the dialog reset function'''
69        # Set some controls to non-default state
70        self.widget.cbColorMap.setCurrentIndex(20)
71        self.widget.chkReverse.setChecked(True)
72        self.widget.txtMinAmplitude.setText("20.0")
73
74        # Reset the widget state
75        self.widget.onReset()
76
77        # Assure things went back to default
78        self.assertEqual(self.widget.cbColorMap.currentIndex(), 20)
79        self.assertFalse(self.widget.chkReverse.isChecked())
80        self.assertEqual(self.widget.txtMinAmplitude.text(), "")
81
82    def testInitMapCombobox(self):
83        '''Test the combo box initializer'''
84        # Set a color map from the direct list
85        self.widget._cmap = "gnuplot"
86        self.widget.initMapCombobox()
87
88        # Check the combobox
89        self.assertEqual(self.widget.cbColorMap.currentIndex(), 55)
90        self.assertFalse(self.widget.chkReverse.isChecked())
91
92        # Set a reversed value
93        self.widget._cmap = "hot_r"
94        self.widget.initMapCombobox()
95        # Check the combobox
96        self.assertEqual(self.widget.cbColorMap.currentIndex(), 56)
97        self.assertTrue(self.widget.chkReverse.isChecked())
98
99
100    def testOnMapIndexChange(self):
101        '''Test the response to the combo box index change'''
102
103        self.widget.canvas.draw = MagicMock()
104        mpl.colorbar.ColorbarBase = MagicMock()
105
106        # simulate index change
107        self.widget.cbColorMap.setCurrentIndex(1)
108
109        # Check that draw() got called
110        self.assertTrue(self.widget.canvas.draw.called)
111        self.assertTrue(mpl.colorbar.ColorbarBase.called)
112
113    def testOnColorMapReversed(self):
114        '''Test reversing the color map functionality'''
115        # Check the defaults
116        self.assertEqual(self.widget._cmap, "jet")
117        self.widget.cbColorMap.addItems = MagicMock()
118
119        # Reverse the choice
120        self.widget.onColorMapReversed(True)
121
122        # check the behaviour
123        self.assertEqual(self.widget._cmap, "jet_r")
124        self.assertTrue(self.widget.cbColorMap.addItems.called)
125
126    def testOnAmplitudeChange(self):
127        '''Check the callback method for responding to changes in textboxes'''
128        self.widget.canvas.draw = MagicMock()
129        mpl.colors.Normalize = MagicMock()
130        mpl.colorbar.ColorbarBase = MagicMock()
131
132        self.widget.vmin = 0.0
133        self.widget.vmax = 100.0
134
135        # good values in fields
136        self.widget.txtMinAmplitude.setText("1.0")
137        self.widget.txtMaxAmplitude.setText("10.0")
138
139        self.widget.onAmplitudeChange()
140
141        # Check the arguments to Normalize
142        mpl.colors.Normalize.assert_called_with(vmin=1.0, vmax=10.0)
143        self.assertTrue(self.widget.canvas.draw.called)
144
145        # Bad values in fields
146        self.widget.txtMinAmplitude.setText("cake")
147        self.widget.txtMaxAmplitude.setText("more cake")
148
149        self.widget.onAmplitudeChange()
150
151        # Check the arguments to Normalize - should be defaults
152        mpl.colors.Normalize.assert_called_with(vmin=0.0, vmax=100.0)
153        self.assertTrue(self.widget.canvas.draw.called)
154
155
156if __name__ == "__main__":
157    unittest.main()
Note: See TracBrowser for help on using the repository browser.