1 | """ |
---|
2 | test gui for sld calculator |
---|
3 | """ |
---|
4 | |
---|
5 | import unittest |
---|
6 | import wx |
---|
7 | |
---|
8 | H2O_DENSITY = 1.0 |
---|
9 | WAVELENGTH = 6.0 |
---|
10 | |
---|
11 | class testTextCtrl(unittest.TestCase): |
---|
12 | """ |
---|
13 | txtctrl should have a pink background when the result are incorrect |
---|
14 | and white when reset or correct value |
---|
15 | """ |
---|
16 | def setUp(self): |
---|
17 | """ |
---|
18 | Create an Sld calculator |
---|
19 | """ |
---|
20 | self.app = wx.App() |
---|
21 | from sans.perspectives.calculator.sld_panel import SldWindow |
---|
22 | self.sld_frame = SldWindow() |
---|
23 | |
---|
24 | def testCompoundTextCtrl(self): |
---|
25 | """ |
---|
26 | Test Compund textCtrl |
---|
27 | """ |
---|
28 | #add invalid value for compound |
---|
29 | self.sld_frame.panel.compound_ctl.SetValue("String not in table") |
---|
30 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
31 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
32 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
33 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
34 | bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
35 | self.assert_(bkg.GetAsString() == 'pink') |
---|
36 | #compute invariant without entering a value for compound |
---|
37 | self.sld_frame.panel.compound_ctl.SetValue("") |
---|
38 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
39 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
40 | bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
41 | self.assert_(bkg.GetAsString() == 'pink') |
---|
42 | #compute invariant without entering a value for compound |
---|
43 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
44 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
45 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
46 | bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
47 | self.assert_(bkg.GetAsString() == 'white') |
---|
48 | |
---|
49 | def testDensityTextCtrl(self): |
---|
50 | """ |
---|
51 | Test Density textCtrl |
---|
52 | |
---|
53 | """ |
---|
54 | #add invalid value for density |
---|
55 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
56 | self.sld_frame.panel.density_ctl.SetValue("invalid density") |
---|
57 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
58 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
59 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
60 | bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
61 | self.assert_(bkg.GetAsString() == 'pink') |
---|
62 | #compute invariant without entering a value for density |
---|
63 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
64 | self.sld_frame.panel.density_ctl.SetValue("") |
---|
65 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
66 | bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
67 | self.assert_(bkg.GetAsString() == 'pink') |
---|
68 | #compute invariant without entering a value for density |
---|
69 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
70 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
71 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
72 | bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
73 | self.assert_(bkg.GetAsString() == 'white') |
---|
74 | |
---|
75 | def testWavelengthTextCtrl(self): |
---|
76 | """ |
---|
77 | Test wavelength textCtrl |
---|
78 | |
---|
79 | """ |
---|
80 | #add invalid value for wavelength |
---|
81 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
82 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
83 | self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength") |
---|
84 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
85 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
86 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
87 | bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
88 | self.assert_(bkg.GetAsString() == 'pink') |
---|
89 | #compute invariant without entering a value for wavelegnth |
---|
90 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
91 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
92 | self.sld_frame.panel.wavelength_ctl.SetValue("") |
---|
93 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
94 | cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
95 | self.assert_(cp_bkg.GetAsString() == 'white') |
---|
96 | ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
97 | self.assert_(ds_bkg.GetAsString() == 'white') |
---|
98 | wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
99 | value = self.sld_frame.panel.wavelength_ctl.GetValue() |
---|
100 | self.assert_(wv_bkg.GetAsString() == 'white') |
---|
101 | self.assert_(float(value) == WAVELENGTH) |
---|
102 | sld_real = self.sld_frame.panel.neutron_sld_real_ctl.GetValue() |
---|
103 | sld_im = self.sld_frame.panel.neutron_sld_im_ctl.GetValue() |
---|
104 | mo_real = self.sld_frame.panel.mo_ka_sld_real_ctl.GetValue() |
---|
105 | mo_im = self.sld_frame.panel.mo_ka_sld_im_ctl.GetValue() |
---|
106 | cu_real = self.sld_frame.panel.cu_ka_sld_real_ctl.GetValue() |
---|
107 | cu_im = self.sld_frame.panel.cu_ka_sld_im_ctl.GetValue() |
---|
108 | abs = self.sld_frame.panel.neutron_abs_ctl.GetValue() |
---|
109 | incoh = self.sld_frame.panel.neutron_inc_ctl.GetValue() |
---|
110 | length = self.sld_frame.panel.neutron_length_ctl.GetValue() |
---|
111 | |
---|
112 | self.assertAlmostEquals(float(sld_real), 1.04e-6, 1) |
---|
113 | self.assertAlmostEquals(float(sld_im), -1.5e-7, 1) |
---|
114 | #test absorption value |
---|
115 | self.assertAlmostEquals(float(abs) , 0.0741, 2) |
---|
116 | self.assertAlmostEquals(float(incoh), 5.62, 2) |
---|
117 | #Test length |
---|
118 | self.assertAlmostEquals(float(length), 0.1755, 2) |
---|
119 | #test Cu sld |
---|
120 | self.assertAlmostEquals(float(cu_real), 9.46e-6, 1) |
---|
121 | self.assertAlmostEquals(float(cu_im), 3.01e-8) |
---|
122 | # test Mo sld |
---|
123 | self.assertAlmostEquals(float(mo_real), 9.43e-6) |
---|
124 | self.assertAlmostEquals(float(mo_im), 5.65e-7, 1) |
---|
125 | #compute invariant with all correct inputs value |
---|
126 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
127 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
128 | self.sld_frame.panel.wavelength_ctl.SetValue(str(WAVELENGTH/2)) |
---|
129 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
130 | bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
131 | value = self.sld_frame.panel.wavelength_ctl.GetValue() |
---|
132 | self.assert_(bkg.GetAsString() == 'white') |
---|
133 | self.assert_(float(value) == WAVELENGTH/2) |
---|
134 | |
---|
135 | def testSomeCombination(self): |
---|
136 | """ |
---|
137 | Test other error |
---|
138 | """ |
---|
139 | #only wavelength is invalid |
---|
140 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
141 | self.sld_frame.panel.density_ctl.SetValue(str(H2O_DENSITY)) |
---|
142 | self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength") |
---|
143 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
144 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
145 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
146 | cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
147 | self.assert_(cp_bkg.GetAsString() == 'white') |
---|
148 | ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
149 | self.assert_(ds_bkg.GetAsString() == 'white') |
---|
150 | wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
151 | self.assert_(wv_bkg.GetAsString() == 'pink') |
---|
152 | #density, wavelength is invalid |
---|
153 | self.sld_frame.panel.compound_ctl.SetValue("H2O") |
---|
154 | self.sld_frame.panel.density_ctl.SetValue("invalid density") |
---|
155 | self.sld_frame.panel.wavelength_ctl.SetValue("invalid wavelength") |
---|
156 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
157 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
158 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
159 | cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
160 | self.assert_(cp_bkg.GetAsString() == 'white') |
---|
161 | ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
162 | self.assert_(ds_bkg.GetAsString() == 'pink') |
---|
163 | wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
164 | self.assert_(wv_bkg.GetAsString() == 'pink') |
---|
165 | #density, wavelength is invalid |
---|
166 | self.sld_frame.panel.compound_ctl.SetValue("invalid compound") |
---|
167 | self.sld_frame.panel.density_ctl.SetValue("invalid density") |
---|
168 | self.sld_frame.panel.wavelength_ctl.SetValue("") |
---|
169 | id = self.sld_frame.panel.button_calculate.GetId() |
---|
170 | clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, id) |
---|
171 | self.sld_frame.panel.ProcessEvent(clickEvent) |
---|
172 | cp_bkg = self.sld_frame.panel.compound_ctl.GetBackgroundColour() |
---|
173 | self.assert_(cp_bkg.GetAsString() == 'pink') |
---|
174 | ds_bkg = self.sld_frame.panel.density_ctl.GetBackgroundColour() |
---|
175 | self.assert_(ds_bkg.GetAsString() == 'pink') |
---|
176 | wv_bkg = self.sld_frame.panel.wavelength_ctl.GetBackgroundColour() |
---|
177 | self.assert_(wv_bkg.GetAsString() == 'white') |
---|
178 | value = self.sld_frame.panel.wavelength_ctl.GetValue() |
---|
179 | self.assert_(float(value) == WAVELENGTH) |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | def tearDown(self): |
---|
184 | """ |
---|
185 | Destroy the sld calculator frame |
---|
186 | """ |
---|
187 | self.sld_frame.Close() |
---|
188 | self.app.MainLoop() |
---|
189 | |
---|
190 | if __name__ == "__main__": |
---|
191 | unittest.main() |
---|