[55d89f8] | 1 | import sys |
---|
| 2 | import unittest |
---|
[3bdbfcc] | 3 | import numpy |
---|
[b2a5042] | 4 | import platform |
---|
[55d89f8] | 5 | |
---|
[53c771e] | 6 | from PyQt5 import QtGui, QtWidgets, QtPrintSupport |
---|
| 7 | from PyQt5 import QtCore |
---|
| 8 | from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
---|
[7fb471d] | 9 | from unittest.mock import MagicMock |
---|
[64f1e93] | 10 | from mpl_toolkits.mplot3d import Axes3D |
---|
[55d89f8] | 11 | |
---|
| 12 | ####### TEMP |
---|
[416fa8f] | 13 | import path_prepare |
---|
[55d89f8] | 14 | ####### |
---|
[dc5ef15] | 15 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 16 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[092a3d9] | 17 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
[55d89f8] | 18 | |
---|
| 19 | # Tested module |
---|
[83eb5208] | 20 | import sas.qtgui.Plotting.Plotter2D as Plotter2D |
---|
[55d89f8] | 21 | |
---|
[53c771e] | 22 | if not QtWidgets.QApplication.instance(): |
---|
| 23 | app = QtWidgets.QApplication(sys.argv) |
---|
[55d89f8] | 24 | |
---|
| 25 | class Plotter2DTest(unittest.TestCase): |
---|
| 26 | '''Test the Plotter 2D class''' |
---|
| 27 | def setUp(self): |
---|
| 28 | '''create''' |
---|
[3bdbfcc] | 29 | class dummy_manager(object): |
---|
| 30 | def communicator(self): |
---|
| 31 | return Communicate() |
---|
| 32 | def perspective(self): |
---|
| 33 | return MyPerspective() |
---|
| 34 | def workspace(self): |
---|
| 35 | return None |
---|
| 36 | |
---|
| 37 | self.plotter = Plotter2D.Plotter2D(parent=dummy_manager(), quickplot=True) |
---|
[55d89f8] | 38 | |
---|
| 39 | self.data = Data2D(image=[0.1]*4, |
---|
| 40 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
| 41 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
| 42 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
| 43 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
| 44 | q_data=[1,2,3,4], |
---|
| 45 | xmin=-1.0, xmax=5.0, |
---|
| 46 | ymin=-1.0, ymax=15.0, |
---|
| 47 | zmin=-1.0, zmax=20.0) |
---|
| 48 | |
---|
| 49 | self.data.title="Test data" |
---|
| 50 | self.data.id = 1 |
---|
[b2a5042] | 51 | self.isWindows = platform.system=="Windows" |
---|
[55d89f8] | 52 | |
---|
| 53 | def tearDown(self): |
---|
| 54 | '''destroy''' |
---|
[53c771e] | 55 | self.plotter.figure.clf() |
---|
[55d89f8] | 56 | self.plotter = None |
---|
| 57 | |
---|
| 58 | def testDataProperty(self): |
---|
| 59 | """ Adding data """ |
---|
| 60 | self.plotter.data = self.data |
---|
| 61 | |
---|
| 62 | self.assertEqual(self.plotter.data, self.data) |
---|
| 63 | self.assertEqual(self.plotter._title, self.data.title) |
---|
| 64 | self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$") |
---|
| 65 | self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$") |
---|
| 66 | |
---|
| 67 | def testPlot(self): |
---|
| 68 | """ Look at the plotting """ |
---|
| 69 | self.plotter.data = self.data |
---|
| 70 | self.plotter.show() |
---|
[64f1e93] | 71 | FigureCanvas.draw_idle = MagicMock() |
---|
[55d89f8] | 72 | |
---|
| 73 | self.plotter.plot() |
---|
| 74 | |
---|
[64f1e93] | 75 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
[53c771e] | 76 | self.plotter.figure.clf() |
---|
[64f1e93] | 77 | |
---|
[092a3d9] | 78 | def testCalculateDepth(self): |
---|
| 79 | ''' Test the depth calculator ''' |
---|
[03c372d] | 80 | self.plotter.data = self.data |
---|
| 81 | |
---|
| 82 | # Default, log scale |
---|
| 83 | depth = self.plotter.calculateDepth() |
---|
| 84 | self.assertEqual(depth, (0.1, 1.e20)) |
---|
| 85 | |
---|
| 86 | # Change the scale to linear |
---|
| 87 | self.plotter.scale = 'linear' |
---|
| 88 | depth = self.plotter.calculateDepth() |
---|
| 89 | self.assertEqual(depth[0], -32.) |
---|
| 90 | self.assertAlmostEqual(depth[1], 1.30103, 5) |
---|
[092a3d9] | 91 | |
---|
| 92 | def testOnColorMap(self): |
---|
| 93 | ''' Respond to the color map event ''' |
---|
[03c372d] | 94 | self.plotter.data = self.data |
---|
| 95 | self.plotter.plot() |
---|
| 96 | self.plotter.show() |
---|
| 97 | |
---|
[53c771e] | 98 | QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted) |
---|
[03c372d] | 99 | |
---|
| 100 | # Just this one plot |
---|
| 101 | self.plotter.onColorMap() |
---|
| 102 | |
---|
| 103 | # Check that exec_ got called |
---|
[53c771e] | 104 | self.assertTrue(QtWidgets.QDialog.exec_.called) |
---|
[03c372d] | 105 | |
---|
| 106 | self.assertEqual(self.plotter.cmap, "jet") |
---|
[965fbd8] | 107 | self.assertAlmostEqual(self.plotter.vmin, 0.1, 6) |
---|
| 108 | self.assertAlmostEqual(self.plotter.vmax, 1e+20, 6) |
---|
[53c771e] | 109 | self.plotter.figure.clf() |
---|
[092a3d9] | 110 | |
---|
[64f1e93] | 111 | def testOnToggleScale(self): |
---|
| 112 | """ Respond to the event by replotting """ |
---|
| 113 | self.plotter.data = self.data |
---|
| 114 | self.plotter.show() |
---|
| 115 | FigureCanvas.draw_idle = MagicMock() |
---|
| 116 | |
---|
| 117 | self.plotter.onToggleScale(None) |
---|
| 118 | |
---|
| 119 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
[53c771e] | 120 | self.plotter.figure.clf() |
---|
[55d89f8] | 121 | |
---|
[3bdbfcc] | 122 | def testOnBoxSum(self): |
---|
| 123 | """ Test the box sum display and functionality """ |
---|
| 124 | |
---|
| 125 | # hacky way to make things work in manipulations._sum |
---|
| 126 | self.data.detector = [1] |
---|
| 127 | self.data.err_data = numpy.array([0.0, 0.0, 0.1, 0.0]) |
---|
| 128 | self.plotter.data = self.data |
---|
| 129 | self.plotter.show() |
---|
| 130 | |
---|
| 131 | # Mock the main window |
---|
| 132 | self.plotter.manager.parent = MagicMock() |
---|
| 133 | |
---|
| 134 | # Call the main tested method |
---|
| 135 | self.plotter.onBoxSum() |
---|
| 136 | |
---|
| 137 | # Test various properties |
---|
| 138 | self.assertIsInstance(self.plotter.slicer.model(), QtGui.QStandardItemModel) |
---|
| 139 | self.assertTrue(self.plotter.boxwidget.isVisible()) |
---|
| 140 | self.assertIsInstance(self.plotter.boxwidget.model, QtGui.QStandardItemModel) |
---|
[53c771e] | 141 | self.plotter.figure.clf() |
---|
[3bdbfcc] | 142 | |
---|
[55d89f8] | 143 | def testContextMenuQuickPlot(self): |
---|
| 144 | """ Test the right click menu """ |
---|
| 145 | self.plotter.data = self.data |
---|
[b46f285] | 146 | self.plotter.createContextMenuQuick() |
---|
[55d89f8] | 147 | actions = self.plotter.contextMenu.actions() |
---|
| 148 | self.assertEqual(len(actions), 7) |
---|
| 149 | |
---|
| 150 | # Trigger Print Image and make sure the method is called |
---|
| 151 | self.assertEqual(actions[1].text(), "Print Image") |
---|
[53c771e] | 152 | QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected) |
---|
[55d89f8] | 153 | actions[1].trigger() |
---|
[53c771e] | 154 | self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called) |
---|
[55d89f8] | 155 | |
---|
| 156 | # Trigger Copy to Clipboard and make sure the method is called |
---|
| 157 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
| 158 | |
---|
| 159 | # Trigger Toggle Grid and make sure the method is called |
---|
| 160 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
| 161 | self.plotter.ax.grid = MagicMock() |
---|
| 162 | actions[4].trigger() |
---|
| 163 | self.assertTrue(self.plotter.ax.grid.called) |
---|
| 164 | |
---|
| 165 | # Trigger Change Scale and make sure the method is called |
---|
| 166 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
[64f1e93] | 167 | FigureCanvas.draw_idle = MagicMock() |
---|
[55d89f8] | 168 | actions[6].trigger() |
---|
[64f1e93] | 169 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
| 170 | |
---|
[b2a5042] | 171 | # Spy on cliboard's dataChanged() signal |
---|
| 172 | if not self.isWindows: |
---|
| 173 | return |
---|
| 174 | self.clipboard_called = False |
---|
| 175 | def done(): |
---|
| 176 | self.clipboard_called = True |
---|
[53c771e] | 177 | QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
[b2a5042] | 178 | actions[2].trigger() |
---|
[53c771e] | 179 | QtWidgets.qApp.processEvents() |
---|
[b2a5042] | 180 | # Make sure clipboard got updated. |
---|
| 181 | self.assertTrue(self.clipboard_called) |
---|
[53c771e] | 182 | self.plotter.figure.clf() |
---|
[b2a5042] | 183 | |
---|
[64f1e93] | 184 | def testShowNoPlot(self): |
---|
| 185 | """ Test the plot rendering and generation """ |
---|
| 186 | |
---|
| 187 | FigureCanvas.draw_idle = MagicMock() |
---|
| 188 | FigureCanvas.draw = MagicMock() |
---|
| 189 | |
---|
| 190 | # Test early return on no data |
---|
| 191 | self.plotter.showPlot(data=None, |
---|
| 192 | qx_data=self.data.qx_data, |
---|
| 193 | qy_data=self.data.qy_data, |
---|
| 194 | xmin=self.data.xmin, |
---|
| 195 | xmax=self.data.xmax, |
---|
| 196 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 197 | cmap=None, zmin=None, |
---|
| 198 | zmax=None) |
---|
| 199 | |
---|
| 200 | self.assertFalse(FigureCanvas.draw_idle.called) |
---|
| 201 | self.assertFalse(FigureCanvas.draw.called) |
---|
[53c771e] | 202 | self.plotter.figure.clf() |
---|
[64f1e93] | 203 | |
---|
| 204 | def testShow3DPlot(self): |
---|
| 205 | """ Test the 3Dplot rendering and generation """ |
---|
| 206 | # Test 3D printout |
---|
| 207 | FigureCanvas.draw = MagicMock() |
---|
| 208 | Axes3D.plot_surface = MagicMock() |
---|
| 209 | self.plotter.figure.colorbar = MagicMock() |
---|
| 210 | |
---|
| 211 | self.plotter.dimension = 3 |
---|
| 212 | self.plotter.data = self.data |
---|
| 213 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
| 214 | qx_data=self.data.qx_data, |
---|
| 215 | qy_data=self.data.qy_data, |
---|
| 216 | xmin=self.data.xmin, |
---|
| 217 | xmax=self.data.xmax, |
---|
| 218 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 219 | cmap=None, zmin=None, |
---|
| 220 | zmax=None) |
---|
| 221 | self.assertTrue(Axes3D.plot_surface.called) |
---|
| 222 | self.assertTrue(FigureCanvas.draw.called) |
---|
[53c771e] | 223 | self.plotter.figure.clf() |
---|
[64f1e93] | 224 | |
---|
| 225 | def testShow2DPlot(self): |
---|
| 226 | """ Test the 2Dplot rendering and generation """ |
---|
| 227 | # Test 2D printout |
---|
| 228 | FigureCanvas.draw_idle = MagicMock() |
---|
| 229 | self.plotter.figure.colorbar = MagicMock() |
---|
| 230 | |
---|
| 231 | self.plotter.dimension = 2 |
---|
| 232 | self.plotter.data = self.data |
---|
| 233 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
| 234 | qx_data=self.data.qx_data, |
---|
| 235 | qy_data=self.data.qy_data, |
---|
| 236 | xmin=self.data.xmin, |
---|
| 237 | xmax=self.data.xmax, |
---|
| 238 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 239 | cmap=None, zmin=None, |
---|
| 240 | zmax=None) |
---|
| 241 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
[53c771e] | 242 | self.plotter.figure.clf() |
---|
[55d89f8] | 243 | |
---|
| 244 | |
---|
| 245 | if __name__ == "__main__": |
---|
| 246 | unittest.main() |
---|