ya2 · news · projects · code · about

9ac9b29e56b76e08138695c07c741ae0439f7efb
[pmachines.git] / tests / functional_test.py
1 '''Create ref:
2 * M-x fla-set-fun-test
3 * rm options.ini
4 * python main.py --functional-test --functional-ref & python -m ya2.tools.functional_test.py 1
5 * python main.py --functional-test --functional-ref & python -m ya2.tools.functional_test.py 2
6 * M-x fla-unset-fun-test'''
7 from panda3d.core import load_prc_file_data
8 load_prc_file_data('', 'window-type none')
9 import datetime
10 from time import sleep
11 from os import getcwd, system
12 from xmlrpc.client import ServerProxy
13 from logging import debug, info
14 from pathlib import Path
15 from shutil import rmtree
16 from os import makedirs
17 from os.path import join, exists
18 from glob import glob
19 from sys import exit, argv
20 from panda3d.core import Filename
21 from direct.showbase.ShowBase import ShowBase
22 from direct.gui.OnscreenText import OnscreenText
23 from ya2.patterns.gameobject import GameObject
24 from ya2.build.build import _branch
25
26
27 class FunctionalTest(GameObject):
28
29 screenshot_time = 1.2
30 evt_time = 1.0
31 drag_time = 1.0
32 start_time = 2
33
34 def __init__(self, idx, offset):
35 super().__init__()
36 info('test idx: %s' % idx)
37 self._offset = offset
38 sleep(5)
39 self._proxy = ServerProxy('http://localhost:6000')
40 self._curr_time = 0
41 self._tasks = []
42 self._prev_time = 0
43 taskMgr.add(self.on_frame_unpausable, 'on-frame-unpausable')
44 self._proxy.set_idx(idx)
45 self._do_screenshots(idx)
46
47 def _do_screenshot(self, name):
48 time = datetime.datetime.now().strftime('%y%m%d%H%M%S')
49 name = name + '.png'
50 self._proxy.screenshot(name)
51 info('screenshot %s' % name)
52
53 def _screenshot(self, time, name):
54 self._tasks += [(
55 self._curr_time + time,
56 lambda: self._do_screenshot(name),
57 'screenshot: %s' % name)]
58 self._curr_time += time
59
60 def __mouse_click(self, pos, btn):
61 offset_x = int((1920 - 1360) / 2) #+ 1 # xfce decorations
62 offset_y = int((1080 - 768) / 2) #+ 24 + self._offset # xfce decorations
63 btn = 3 if btn == 'right' else 1
64 system('xdotool mousemove %s %s' % (offset_x + pos[0], offset_y + pos[1]))
65 def click(task):
66 system('xdotool click %s' % btn)
67 taskMgr.do_method_later(.28, click, 'click')
68
69 def __mouse_drag(self, start, end, btn):
70 offset_x = int((1920 - 1360) / 2) #+ 1 # xfce decorations
71 offset_y = int((1080 - 768) / 2) #+ 24 + self._offset # xfce decorations
72 btn = 3 if btn == 'right' else 1
73 system('xdotool mousemove %s %s' % (offset_x + start[0], offset_y + start[1]))
74 def mousedown(task):
75 system('xdotool mousedown %s' % btn)
76 def mousemove(task):
77 system('xdotool mousemove %s %s' % (offset_x + end[0], offset_y + end[1]))
78 def mouseup(task):
79 system('xdotool mouseup %s' % btn)
80 taskMgr.do_method_later(.28, mouseup, 'mouseup')
81 taskMgr.do_method_later(.28, mousemove, 'mousemove')
82 taskMgr.do_method_later(.28, mousedown, 'mousedown')
83
84 def _event(self, time, evt, mouse_args=None):
85 if evt == 'mouseclick':
86 cback = lambda: self.__mouse_click(*mouse_args)
87 elif evt == 'mousedrag':
88 cback = lambda: self.__mouse_drag(*mouse_args)
89 self._tasks += [(
90 self._curr_time + time,
91 cback,
92 'event: %s' % evt)]
93 self._curr_time += time
94
95 def _enforce_res(self, time, res):
96 def cback():
97 self._proxy.enforce_res(res)
98 info('enforce_res %s' % res)
99 self._tasks += [(
100 self._curr_time + time,
101 cback,
102 'enforce res: %s' % res)]
103 self._curr_time += time
104
105 def _enforce_resolution(self, time, res):
106 def cback():
107 self._proxy.enforce_resolution(res)
108 info('enforce_resolution %s (send)' % res)
109 self._tasks += [(
110 self._curr_time + time,
111 cback,
112 'enforce resolution: %s' % res)]
113 self._curr_time += time
114
115 def _verify(self):
116 def __verify():
117 self._proxy.verify()
118 info('verify')
119 self._tasks += [(
120 self._curr_time + 3,
121 lambda: __verify(),
122 'verify')]
123 self._curr_time += 3
124
125 def _exit(self):
126 def do_exit():
127 self._proxy.close()
128 exit()
129 self._tasks += [(
130 self._curr_time + 3,
131 lambda: do_exit(),
132 'exit')]
133
134 def on_frame_unpausable(self, task):
135 for tsk in self._tasks:
136 #if self._prev_time <= tsk[0] < self.eng.event.unpaused_time:
137 if self._prev_time <= tsk[0] < globalClock.getFrameTime():
138 debug('%s %s' % (tsk[0], tsk[2]))
139 tsk[1]()
140 self._prev_time = globalClock.getFrameTime() # self.eng.event.unpaused_time
141 return task.cont
142
143 def _do_screenshots_1(self):
144 info('_do_screenshots_1')
145 self._screenshot(FunctionalTest.start_time, 'main_menu')
146 #self._do_screenshots_credits()
147 #self._do_screenshots_options()
148 self._do_screenshots_exit()
149
150 def _do_screenshots_credits(self):
151 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left'])
152 self._screenshot(FunctionalTest.screenshot_time, 'credits_menu')
153 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 680), 'left'])
154 self._screenshot(FunctionalTest.screenshot_time, 'main_menu_back_from_credits')
155
156 def _do_screenshots_options(self):
157 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 300), 'left'])
158 self._screenshot(FunctionalTest.screenshot_time, 'options_menu')
159 # languages
160 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 60), 'left'])
161 self._screenshot(FunctionalTest.screenshot_time, 'open_languages')
162 self._event(FunctionalTest.evt_time, 'mouseclick', [(980, 120), 'left'])
163 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_italian')
164 # volume
165 self._event(FunctionalTest.evt_time, 'mouseclick', [(740, 163), 'left'])
166 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_drag_1')
167 # antialiasing
168 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 440), 'left'])
169 self._screenshot(FunctionalTest.screenshot_time, 'antialiasing_no')
170 # shadows
171 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 540), 'left'])
172 self._screenshot(FunctionalTest.screenshot_time, 'shadows_no')
173 # test aa and shadows
174 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 680), 'left']) # back
175 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 140), 'left']) # play
176 self._event(FunctionalTest.evt_time, 'mouseclick', [(230, 160), 'left']) # domino
177 self._event(FunctionalTest.evt_time, 'mouseclick', [(900, 490), 'left']) # close instructions
178 self._screenshot(FunctionalTest.screenshot_time, 'aa_no_shadows_no')
179 self._event(FunctionalTest.evt_time, 'mouseclick', [(25, 740), 'left']) # home
180
181 def _do_screenshots_restore_options(self):
182 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 300), 'left'])
183 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_restored')
184 # languages
185 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 60), 'left'])
186 self._screenshot(FunctionalTest.screenshot_time, 'open_languages_restored')
187 self._event(FunctionalTest.evt_time, 'mouseclick', [(980, 20), 'left'])
188 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_english')
189 # volume
190 self._event(FunctionalTest.evt_time, 'mouseclick', [(719, 163), 'left'])
191 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_drag_2')
192 # fullscreen
193 # the first one is because of the windowed mode in test
194 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 250), 'left'])
195 self._screenshot(FunctionalTest.screenshot_time, 'fullscreen')
196 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 250), 'left'])
197 self._screenshot(FunctionalTest.screenshot_time, 'fullscreen')
198 #self._event(8 + FunctionalTest.evt_time, 'mouseclick', [(440, 120), 'left'])
199 self._event(8 + FunctionalTest.evt_time, 'mouseclick', [(680, 250), 'left'])
200 self._screenshot(8 + FunctionalTest.screenshot_time, 'back_from_fullscreen')
201 # resolution
202 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 340), 'left'])
203 self._screenshot(FunctionalTest.screenshot_time, 'resolutions')
204 self._enforce_resolution(FunctionalTest.evt_time, '1440x900')
205 self._event(FunctionalTest.evt_time, 'mouseclick', [(1000, 440), 'left'])
206 self._screenshot(FunctionalTest.screenshot_time, '1440x900')
207 self._event(FunctionalTest.evt_time, 'mouseclick', [(740, 400), 'left'])
208 self._screenshot(FunctionalTest.screenshot_time, 'resolutions_2')
209 self._enforce_resolution(FunctionalTest.evt_time, '1360x768')
210 self._event(FunctionalTest.evt_time, 'mouseclick', [(1110, 80), 'left'])
211 self._screenshot(FunctionalTest.screenshot_time, '1360x768')
212 # antialiasing
213 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 440), 'left'])
214 self._screenshot(FunctionalTest.screenshot_time, 'antialiasing_yes')
215 # shadows
216 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 540), 'left'])
217 self._screenshot(FunctionalTest.screenshot_time, 'shadows_yes')
218 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 680), 'left']) # back
219
220 def _do_screenshots_play(self):
221 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 140), 'left']) # play
222 self._screenshot(FunctionalTest.screenshot_time, 'play_menu')
223 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 680), 'left']) # back
224 self._screenshot(FunctionalTest.screenshot_time, 'back_from_play')
225 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 140), 'left']) # play
226 self._event(FunctionalTest.evt_time, 'mouseclick', [(230, 160), 'left']) # domino scene
227 self._screenshot(FunctionalTest.screenshot_time, 'scene_domino_instructions')
228 self._event(FunctionalTest.evt_time, 'mouseclick', [(850, 490), 'left']) # close instructions
229 self._screenshot(FunctionalTest.screenshot_time, 'scene_domino')
230 self._event(FunctionalTest.evt_time, 'mouseclick', [(25, 740), 'left']) # home
231 self._screenshot(FunctionalTest.screenshot_time, 'home_back_from_scene')
232 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 140), 'left']) # play
233 self._event(FunctionalTest.evt_time, 'mouseclick', [(230, 160), 'left']) # domino
234 self._event(FunctionalTest.evt_time, 'mouseclick', [(850, 490), 'left']) # close instructions
235 self._event(FunctionalTest.evt_time, 'mouseclick', [(70, 740), 'left']) # info
236 self._screenshot(FunctionalTest.screenshot_time, 'info')
237 self._event(FunctionalTest.evt_time, 'mouseclick', [(850, 490), 'left']) # close instructions
238 # self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (430, 280), 'left']) # drag a piece
239 # self._screenshot(FunctionalTest.screenshot_time, 'domino_dragged')
240 # self._event(FunctionalTest.evt_time, 'mouseclick', [(1220, 740), 'left']) # rewind
241 # self._screenshot(FunctionalTest.screenshot_time, 'rewind')
242 self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (550, 380), 'left']) # drag a piece
243 # self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (715, 380), 'left']) # drag a piece
244 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
245 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_domino')
246 self._event(FunctionalTest.evt_time, 'mouseclick', [(630, 450), 'left']) # home
247 self._screenshot(FunctionalTest.screenshot_time, 'home_back_from_fail')
248 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 140), 'left']) # play
249 self._event(FunctionalTest.evt_time, 'mouseclick', [(230, 160), 'left']) # domino
250 self._event(FunctionalTest.evt_time, 'mouseclick', [(850, 490), 'left']) # close instructions
251 self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (550, 380), 'left']) # drag a piece
252 self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (715, 380), 'left']) # drag a piece
253 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
254 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_domino_2')
255 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
256 self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (570, 380), 'left']) # drag a piece
257 self._event(FunctionalTest.drag_time, 'mousedrag', [(570, 355), (605, 355), 'right']) # rotate the piece
258 self._event(FunctionalTest.drag_time, 'mousedrag', [(35, 60), (715, 380), 'left']) # drag a piece
259 self._enforce_res(FunctionalTest.evt_time, 'win')
260 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
261 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_domino')
262 self._enforce_res(FunctionalTest.evt_time, '')
263 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
264 self._screenshot(FunctionalTest.screenshot_time, 'scene_box')
265 # scene 2
266 self._event(FunctionalTest.evt_time, 'mouseclick', [(880, 490), 'left']) # close instructions
267 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (710, 620), 'left']) # drag a box
268 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (710, 540), 'left']) # drag a box
269 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
270 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_box')
271 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
272 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (710, 620), 'left']) # drag a box
273 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (710, 540), 'left']) # drag a box
274 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (705, 460), 'left']) # drag a box
275 self._enforce_res(FunctionalTest.evt_time, 'win')
276 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
277 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_box')
278 self._enforce_res(FunctionalTest.evt_time, '')
279 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
280 self._screenshot(FunctionalTest.screenshot_time, 'scene_box_domino')
281 # scene 3
282 self._event(FunctionalTest.evt_time, 'mouseclick', [(930, 485), 'left']) # close instructions
283 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (910, 440), 'left']) # drag a box
284 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (910, 360), 'left']) # drag a box
285 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
286 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_box_domino')
287 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
288 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (910, 440), 'left']) # drag a box
289 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (835, 250), 'left']) # drag a box
290 self._enforce_res(FunctionalTest.evt_time, 'win')
291 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
292 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_box_domino')
293 self._enforce_res(FunctionalTest.evt_time, '')
294 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
295 self._screenshot(FunctionalTest.screenshot_time, 'scene_basketball')
296 # scene 4
297 self._event(FunctionalTest.evt_time, 'mouseclick', [(870, 490), 'left']) # close instructions
298 self._event(FunctionalTest.drag_time, 'mousedrag', [(55, 50), (650, 310), 'left']) # drag a ball
299 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
300 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_basketball')
301 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
302 self._event(FunctionalTest.drag_time, 'mousedrag', [(55, 50), (380, 50), 'left']) # drag a ball
303 self._enforce_res(FunctionalTest.evt_time, 'win')
304 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
305 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_basketball')
306 self._enforce_res(FunctionalTest.evt_time, '')
307 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
308 self._screenshot(FunctionalTest.screenshot_time, 'scene_domino_box_basketball')
309 # scene 5
310 self._event(FunctionalTest.evt_time, 'mouseclick', [(865, 490), 'left']) # close instructions
311 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (580, 440), 'left']) # drag a box
312 self._event(FunctionalTest.drag_time, 'mousedrag', [(30, 60), (590, 370), 'left']) # drag a piece
313 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
314 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_domino_box_basketball')
315 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
316 self._event(FunctionalTest.drag_time, 'mousedrag', [(65, 60), (580, 440), 'left']) # drag a box
317 self._event(FunctionalTest.drag_time, 'mousedrag', [(30, 60), (660, 440), 'left']) # drag a piece
318 self._event(FunctionalTest.drag_time, 'mousedrag', [(660, 425), (625, 425), 'right']) # rotate a piece
319 self._event(FunctionalTest.drag_time, 'mousedrag', [(660, 435), (650, 445), 'left']) # drag a piece
320 self._enforce_res(FunctionalTest.evt_time, 'win')
321 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
322 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_domino_box_basketball')
323 self._enforce_res(FunctionalTest.evt_time, '')
324 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
325 self._screenshot(FunctionalTest.screenshot_time, 'scene_teeter_tooter')
326 # scene 6
327 self._event(FunctionalTest.evt_time, 'mouseclick', [(870, 485), 'left']) # close instructions
328 self._event(FunctionalTest.drag_time, 'mousedrag', [(60, 60), (490, 300), 'left']) # drag a box
329 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
330 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_teeter_tooter')
331 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
332 self._event(FunctionalTest.drag_time, 'mousedrag', [(60, 60), (490, 150), 'left']) # drag a box
333 self._event(FunctionalTest.drag_time, 'mousedrag', [(515, 115), (515, 122), 'right']) # rotate a box
334 self._enforce_res(FunctionalTest.evt_time, 'win')
335 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
336 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_teeter_tooter')
337 self._enforce_res(FunctionalTest.evt_time, '')
338 self._event(FunctionalTest.evt_time, 'mouseclick', [(735, 450), 'left']) # next
339 self._screenshot(FunctionalTest.screenshot_time, 'scene_teeter_domino_box_basketball')
340 # scene 7
341 self._event(FunctionalTest.evt_time, 'mouseclick', [(930, 485), 'left']) # close instructions
342 self._event(FunctionalTest.drag_time, 'mousedrag', [(60, 60), (155, 180), 'left']) # drag a box
343 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
344 self._screenshot(16 + FunctionalTest.screenshot_time, 'fail_teeter_domino_box_basketball')
345 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 450), 'left']) # replay
346 self._event(FunctionalTest.drag_time, 'mousedrag', [(60, 60), (170, 80), 'left']) # drag a box
347 self._event(FunctionalTest.drag_time, 'mousedrag', [(195, 50), (195, 80), 'right']) # rotate a box
348 self._enforce_res(FunctionalTest.evt_time, 'win')
349 self._event(FunctionalTest.evt_time, 'mouseclick', [(1340, 740), 'left']) # play
350 self._screenshot(16 + FunctionalTest.screenshot_time, 'win_teeter_domino_box_basketball')
351 self._enforce_res(FunctionalTest.evt_time, '')
352 self._event(FunctionalTest.evt_time, 'mouseclick', [(630, 450), 'left']) # home
353 self._screenshot(FunctionalTest.screenshot_time, 'home_from_play')
354
355 def _exit(self):
356 self._tasks += [(
357 self._curr_time + 3,
358 lambda: exit(),
359 'exit')]
360
361 def _do_screenshots_exit(self):
362 self._verify()
363 self._event(FunctionalTest.evt_time, 'mouseclick', [(680, 600), 'left'])
364 self._exit()
365
366 def _do_screenshots_2(self):
367 info('_do_screenshots_2')
368 self._screenshot(FunctionalTest.start_time, 'main_menu_2')
369 self._do_screenshots_restore_options()
370 self._do_screenshots_play()
371 self._do_screenshots_exit()
372
373 def _do_screenshots(self, idx):
374 [self._do_screenshots_1, self._do_screenshots_2][int(idx) - 1]()
375
376
377 class TestApp(ShowBase):
378
379 def __init__(self):
380 ShowBase.__init__(self)
381 offset = int(argv[2]) if len(argv) >= 3 else 0
382 fun_test = FunctionalTest(int(argv[1]), offset)
383
384
385 TestApp().run()