1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | from PyQt4 import QtGui |
---|
5 | from PyQt4 import QtCore |
---|
6 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
7 | from mock import MagicMock |
---|
8 | |
---|
9 | ####### TEMP |
---|
10 | import LocalSetup |
---|
11 | ####### |
---|
12 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
13 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
14 | |
---|
15 | # Tested module |
---|
16 | import sas.qtgui.Plotter2D as Plotter2D |
---|
17 | |
---|
18 | app = QtGui.QApplication(sys.argv) |
---|
19 | |
---|
20 | class Plotter2DTest(unittest.TestCase): |
---|
21 | '''Test the Plotter 2D class''' |
---|
22 | def setUp(self): |
---|
23 | '''create''' |
---|
24 | self.plotter = Plotter2D.Plotter2D(None, quickplot=True) |
---|
25 | |
---|
26 | self.data = Data2D(image=[0.1]*4, |
---|
27 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
28 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
29 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
30 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
31 | q_data=[1,2,3,4], |
---|
32 | xmin=-1.0, xmax=5.0, |
---|
33 | ymin=-1.0, ymax=15.0, |
---|
34 | zmin=-1.0, zmax=20.0) |
---|
35 | |
---|
36 | self.data.title="Test data" |
---|
37 | self.data.id = 1 |
---|
38 | |
---|
39 | def tearDown(self): |
---|
40 | '''destroy''' |
---|
41 | self.plotter = None |
---|
42 | |
---|
43 | def testDataProperty(self): |
---|
44 | """ Adding data """ |
---|
45 | self.plotter.data = self.data |
---|
46 | |
---|
47 | self.assertEqual(self.plotter.data, self.data) |
---|
48 | self.assertEqual(self.plotter._title, self.data.title) |
---|
49 | self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$") |
---|
50 | self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$") |
---|
51 | |
---|
52 | def testPlot(self): |
---|
53 | """ Look at the plotting """ |
---|
54 | self.plotter.data = self.data |
---|
55 | self.plotter.show() |
---|
56 | FigureCanvas.draw = MagicMock() |
---|
57 | |
---|
58 | self.plotter.plot() |
---|
59 | |
---|
60 | #self.assertTrue(FigureCanvas.draw.called) |
---|
61 | |
---|
62 | def testContextMenuQuickPlot(self): |
---|
63 | """ Test the right click menu """ |
---|
64 | self.plotter.data = self.data |
---|
65 | actions = self.plotter.contextMenu.actions() |
---|
66 | self.assertEqual(len(actions), 7) |
---|
67 | |
---|
68 | # Trigger Save Image and make sure the method is called |
---|
69 | self.assertEqual(actions[0].text(), "Save Image") |
---|
70 | self.plotter.toolbar.save_figure = MagicMock() |
---|
71 | actions[0].trigger() |
---|
72 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
73 | |
---|
74 | # Trigger Print Image and make sure the method is called |
---|
75 | self.assertEqual(actions[1].text(), "Print Image") |
---|
76 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
77 | actions[1].trigger() |
---|
78 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
79 | |
---|
80 | # Trigger Copy to Clipboard and make sure the method is called |
---|
81 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
82 | |
---|
83 | # Spy on cliboard's dataChanged() signal |
---|
84 | self.clipboard_called = False |
---|
85 | def done(): |
---|
86 | self.clipboard_called = True |
---|
87 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
88 | actions[2].trigger() |
---|
89 | QtGui.qApp.processEvents() |
---|
90 | # Make sure clipboard got updated. |
---|
91 | self.assertTrue(self.clipboard_called) |
---|
92 | |
---|
93 | # Trigger Toggle Grid and make sure the method is called |
---|
94 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
95 | self.plotter.ax.grid = MagicMock() |
---|
96 | actions[4].trigger() |
---|
97 | self.assertTrue(self.plotter.ax.grid.called) |
---|
98 | |
---|
99 | # Trigger Change Scale and make sure the method is called |
---|
100 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
101 | FigureCanvas.draw = MagicMock() |
---|
102 | actions[6].trigger() |
---|
103 | #self.assertTrue(FigureCanvas.draw.called) |
---|
104 | |
---|
105 | |
---|
106 | if __name__ == "__main__": |
---|
107 | unittest.main() |
---|