forked from bretmckee/sxASCOM.old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameraTestSuite.py
executable file
·252 lines (201 loc) · 7.79 KB
/
cameraTestSuite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sys, time, getopt, random, unittest
import win32com.client
# import the ascom python bindings
from win32com.client import gencache
gencache.EnsureModule('{76618F90-032F-4424-A680-802467A55742}', 0, 1, 0)
from win32com.client import constants
### Global variables
verbose = False
util = None
defaultExposure = 0.100
camera = None
randomExposures=0
### Classes
class ConnectionTestCases(unittest.TestCase):
def setUp(self):
if camera.Connected:
camera.Connected = False
self.assertFalse(camera.Connected)
def tearDown(self):
if camera.Connected:
camera.Connected = False
self.assertFalse(camera.Connected)
def testConnetDisconnect(self):
camera.Connected = True
self.assertTrue(camera.Connected)
camera.Connected = False
self.assertFalse(camera.Connected)
def testUnconnectedDisconnet(self):
self.assertFalse(camera.Connected)
camera.Connected = False
self.assertFalse(camera.Connected)
def testConnectedConnet(self):
camera.Connected = True
self.assertTrue(camera.Connected)
# make sure we can't connect twice
try:
camera.Connected = True
except win32com.client.pywintypes.com_error:
pass
self.assertTrue(camera.Connected)
camera.Connected = False
self.assertFalse(camera.Connected)
def testDoubleDisconnect(self):
camera.Connected = True
self.assertTrue(camera.Connected)
# Now disconnnect
camera.Connected = False
self.assertFalse(camera.Connected)
# And make sure we can't disconnect twice
try:
camera.Connected = False
except win32com.client.pywintypes.com_error:
pass
self.assertFalse(camera.Connected)
class ASCOMCameraTest(unittest.TestCase):
def setUp(self):
camera.Connected = True
def tearDown(self):
camera.Connected = False
def expose(self,
binX=1, binY=1,
startX=0, startY=0,
numX = -1, numY = -1,
exposure = defaultExposure):
try:
camera.BinX = binX
camera.BinY = binY
camera.StartX = startX
camera.StartY = startY
if numX == -1:
camera.NumX = camera.CameraXSize
else:
camera.NumX = numX
if numY == -1:
camera.NumY = camera.CameraYSize
else:
camera.NumY = numY
camera.StartExposure(exposure, False)
time.sleep(exposure)
for retries in xrange(50): # wait up to 5 seconds after the exposure ends
if camera.CameraState == constants.cameraIdle:
break
# try:
# err = camera.LastError
# except win32com.client.pywintypes.com_error:
# pass
# else:
# self.fail("got error {0} during exposure".format(err))
time.sleep(0.1)
self.assertTrue(camera.ImageReady, "image not ready")
except:
print "exception in expose({0}, {1}, {2}, {3}, {4}, {5}, {6})".format(binX, binY, startX, startY, numX, numY, exposure)
raise
return camera.ImageArray
class RegressionTestCases(ASCOMCameraTest):
def testRegression1(self):
image = self.expose(2, 2, 284, 283, 89, 4)
image = self.expose(7, 7, 80, 34, 7, 25)
class RandomTests(ASCOMCameraTest):
def runRandomTests(self):
for i in xrange(randomExposures):
xBin = random.randint(1,camera.MaxBinX)
if camera.CanAsymmetricBin:
yBin = random.randint(1,camera.MaxBinY)
else:
yBin = xBin
binnedXSize = camera.CameraXSize/xBin
binnedYSize = camera.CameraYSize/yBin
startX = random.randint(0, binnedXSize - 2)
startY = random.randint(0, binnedYSize - 2)
numX = random.randint(1, (binnedXSize - 1 -startX))
numY = random.randint(1, (binnedYSize - 1 -startY))
self.expose(xBin, yBin, startX, startY, numX, numY)
def repeatableRandomTests(self):
random.seed(0)
self.runRandomTests()
def randomTests(self):
random.seed()
self.runRandomTests()
class SubImageTestCases(ASCOMCameraTest):
def testFullSubImage(self):
pass
def testOnePixelSubImage(self):
pass
class BinnedTestCases(ASCOMCameraTest):
def testBinnedModes(self):
if camera.CanAsymmetricBin:
pass
else:
self.assertEqual(camera.MaxBinX, camera.MaxBinY)
for i in xrange(camera.MaxBinX):
bin = i + 1
image = self.expose (bin, bin, 0, 0, camera.CameraXSize/bin, camera.CameraYSize/bin)
class BinnedSubImageTestCases(ASCOMCameraTest):
def testBinnedModes(self):
if camera.CanAsymmetricBin:
pass
else:
self.assertEqual(camera.MaxBinX, camera.MaxBinY)
for i in xrange(camera.MaxBinX):
bin = i + 1
for start in xrange(bin):
for height in xrange(1, camera.CameraYSize/bin - start):
image = self.expose(bin, bin, 0, start, bin, height)
def usage():
print >>sys.stderr, "usage: %s [<camera_name>]"
print >>sys.stderr, "where options can be:"
print >>sys.stderr, "\t-h|--help\t\t\t\t-> display this help message"
print >>sys.stderr, "\t-v|--verbose\t\t\t\t-> verbose output"
print >>sys.stderr, "\t-r <iterations>|--random <iterations>\t-> perform <iterations> random tests (default=0)"
def main():
global camera
global randomExposures
cameraName = None
try:
opts, args = getopt.getopt(sys.argv[1:], "hr:v", ["help", "verbose", "random="])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(1)
output = None
global verbose
for o, a in opts:
if o in ("-v", "--verbose"):
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-r", "--random"):
randomExposures = int(a)
else:
assert False, "unhandled option"
if len(args) > 1:
usage()
sys.exit(1)
elif len(args) == 1:
cameraName = args[0]
else: # len(args) == 0
utilChooser = win32com.client.Dispatch("ASCOM.Utilities.Chooser")
utilChooser.DeviceType = "Camera"
cameraName = utilChooser.Choose("")
print "chose ", cameraName
utilProfile = win32com.client.Dispatch("ASCOM.Utilities.Profile")
utilProfile.DeviceType = "Camera"
if not utilProfile.IsRegistered(cameraName):
print "camera {0} is not registered".format(cameraName)
sys.exit()
camera = win32com.client.Dispatch(cameraName)
allTests = unittest.TestSuite()
allTests.addTest(unittest.makeSuite(ConnectionTestCases,'test'))
allTests.addTest(unittest.makeSuite(RegressionTestCases,'test'))
allTests.addTest(RandomTests("repeatableRandomTests"))
allTests.addTest(RandomTests("randomTests"))
#allTests.addTest(unittest.makeSuite(SubImageTestCases,'test'))
#allTests.addTest(unittest.makeSuite(BinnedTestCases,'test'))
#allTests.addTest(unittest.makeSuite(BinnedSubImageTestCases,'test'))
runner = unittest.TextTestRunner()
runner.run(allTests)
if __name__ == "__main__":
main()