ya2 · news · projects · code · about

first commit
[pmachines.git] / lib / engine / functional.py
1 '''Create ref:
2 * M-x fla-set-fun-test
3 * rm options.ini
4 * python main.py --functional-test 1 --functional-ref
5 * python main.py --functional-test 2 --functional-ref
6 * M-x fla-unset-fun-test'''
7 from logging import debug, info
8 from pathlib import Path
9 from shutil import rmtree
10 from os import makedirs
11 from os.path import join, exists
12 from glob import glob
13 from sys import exit
14 from panda3d.core import Filename
15 from direct.gui.OnscreenText import OnscreenText
16 from lib.gameobject import GameObject
17 from lib.build.build import _branch
18
19
20 class FunctionalTest(GameObject):
21
22 screenshot_time = 1.2
23 evt_time = 1.0
24 start_time = 5
25
26 def __init__(self, idx, ref):
27 super().__init__()
28 self.txt = OnscreenText('', fg=(1, 0, 0, 1), scale=.16)
29 #self._path = ''
30 #if self.eng.is_appimage:
31 self._path = str(Filename().get_user_appdata_directory())
32 self._path += '/yocto_racer/'
33 self._path += 'tests/functional%s/' % ('_ref' if ref else '')
34 home = '/home/flavio' # we must force this for wine
35 # if self._path.startswith('/c/users/') and exists(str(Path.home()) + '/.local/share/flatpak-wine601/default/'):
36 # self._path = str(Path.home()) + '/.local/share/flatpak-wine601/default/drive_' + self._path[1:]
37 if self._path.startswith('/c/users/') and exists(home + '/.wine/'):
38 self._path = home + '/.wine/drive_' + self._path[1:]
39 if ref:
40 self._path = join(
41 Filename().get_user_appdata_directory(),
42 'yocto_racer/tests/functional_ref_%s/' % _branch())
43 self._curr_time = 0
44 if int(idx) == 1:
45 rmtree(self._path, ignore_errors=True)
46 info('creating dir: %s' % self._path)
47 makedirs(self._path, exist_ok=True)
48 self._fnames = []
49 self._tasks = []
50 self._prev_time = 0
51 self.eng.attach_obs(self.on_frame_unpausable)
52 self._do_screenshots(idx)
53
54 def _screenshot(self, time, name):
55 self._fnames += [self._path + name + '.png']
56 self._tasks += [(
57 self._curr_time + time,
58 lambda: self.eng.gfx.gfx_mgr.screenshot(self._path + name + '.png'),
59 'screenshot: %s' % name)]
60 def txt(show_hide):
61 self.txt['text'] = name
62 (self.txt.show if show_hide else self.txt.hide)()
63 self._tasks += [(
64 self._curr_time + time + .1,
65 lambda: txt(True),
66 'screenshot: %s (show)' % name)]
67 self._tasks += [(
68 self._curr_time + time + FunctionalTest.evt_time - .1,
69 lambda: txt(False),
70 'screenshot: %s (hide)' % name)]
71 self._curr_time += time
72
73 def __keypress(self, key):
74 '''Emulates a keypress'''
75 dev = base.win.getInputDevice(0)
76 dev.buttonDown(key)
77 dev.buttonUp(key)
78
79 def __char_entered(self, char):
80 '''Emulates a character being entered.'''
81 dev = base.win.getInputDevice(0)
82 dev.keystroke(ord(char))
83
84 def _event(self, time, evt, messenger_evt=False, append_up=True):
85 def _append_up(evt_name):
86 return evt + ('' if evt.endswith('-up') or not append_up else '-up')
87 def cback_char(_evt):
88 self.__char_entered(_evt)
89 def cback_keyp(_evt):
90 self.__keypress(_evt)
91 self.__keypress('raw-' + _evt)
92 cback = lambda: (cback_char(evt) if len(evt) == 1 else cback_keyp(evt))
93 if messenger_evt:
94 cback = lambda: messenger.send(_append_up(evt))
95 self._tasks += [(
96 self._curr_time + time,
97 cback,
98 'event: %s' % evt)]
99 def txt(show_hide):
100 self.txt['text'] = evt
101 (self.txt.show if show_hide else self.txt.hide)()
102 self._tasks += [(
103 self._curr_time + time + .2,
104 lambda: txt(True),
105 'event: %s (show)' % evt)]
106 self._tasks += [(
107 self._curr_time + time + .8,
108 lambda: txt(False),
109 'event: %s (hide)' % evt)]
110 self._curr_time += time
111
112 def _verify(self):
113 def __verify():
114 files = glob(self._path + '*')
115 for fname in self._fnames:
116 info('verifying %s' % fname)
117 assert exists(fname)
118 self._tasks += [(
119 self._curr_time + 3,
120 lambda: __verify(),
121 'verify')]
122 self._curr_time += 3
123
124 def _exit(self):
125 self._tasks += [(
126 self._curr_time + 3,
127 lambda: exit(),
128 'exit')]
129
130 def on_frame_unpausable(self):
131 for tsk in self._tasks:
132 if self._prev_time <= tsk[0] < self.eng.event.unpaused_time:
133 debug('%s %s' % (tsk[0], tsk[2]))
134 tsk[1]()
135 self._prev_time = self.eng.event.unpaused_time
136
137 def _do_screenshots_1(self):
138 info('_do_screenshots_1')
139 self._screenshot(FunctionalTest.start_time, 'main_menu')
140 self._do_screenshots_credits()
141 self._do_screenshots_options()
142 self._do_screenshots_exit()
143
144 def _do_screenshots_credits(self):
145 # go to credits
146 self._event(FunctionalTest.evt_time, 'joypad0-dpad_down', True)
147 self._event(FunctionalTest.evt_time, 'arrow_down')
148 self._event(FunctionalTest.evt_time, 'joypad0-dpad_down', True)
149 self._event(FunctionalTest.evt_time, 'arrow_down')
150 self._screenshot(FunctionalTest.screenshot_time, 'main_menu_highlight')
151 self._event(FunctionalTest.evt_time, 'rcontrol')
152 self._screenshot(FunctionalTest.screenshot_time, 'credits_menu')
153 # go to supporters
154 self._event(FunctionalTest.evt_time, 'joypad0-face_a', True)
155 self._screenshot(FunctionalTest.screenshot_time, 'supporters_menu')
156 # back to main
157 self._event(FunctionalTest.evt_time, 'rcontrol')
158 self._event(FunctionalTest.evt_time, 'joypad0-face_b', True)
159 self._event(FunctionalTest.evt_time, 'arrow_up')
160 self._event(FunctionalTest.evt_time, 'arrow_up')
161 self._event(FunctionalTest.evt_time, 'arrow_up')
162 self._event(FunctionalTest.evt_time, 'arrow_up')
163
164 def _do_screenshots_options(self):
165 # go to options
166 self._event(FunctionalTest.evt_time, 'arrow_down')
167 self._event(FunctionalTest.evt_time, 'arrow_down')
168 self._event(FunctionalTest.evt_time, 'rcontrol')
169 self._screenshot(FunctionalTest.screenshot_time, 'options_menu')
170 # language
171 self._event(FunctionalTest.evt_time, 'rcontrol')
172 self._screenshot(FunctionalTest.screenshot_time, 'language_open')
173 self._event(FunctionalTest.evt_time, 'arrow_down')
174 self._screenshot(FunctionalTest.screenshot_time, 'language_highlight')
175 self._event(FunctionalTest.evt_time, 'rcontrol')
176 self._screenshot(FunctionalTest.screenshot_time, 'language_it')
177 # volume
178 self._event(FunctionalTest.evt_time, 'arrow_down')
179 self._event(FunctionalTest.evt_time, 'arrow_right')
180 self._event(FunctionalTest.evt_time, 'arrow_right')
181 self._screenshot(FunctionalTest.screenshot_time, 'volume')
182 # car's number
183 self._event(FunctionalTest.evt_time, 'arrow_down')
184 self._event(FunctionalTest.evt_time, 'rcontrol')
185 self._screenshot(FunctionalTest.screenshot_time, 'cars_open')
186 self._event(FunctionalTest.evt_time, 'rcontrol')
187 self._screenshot(FunctionalTest.screenshot_time, 'cars_changed')
188 # back
189 self._event(FunctionalTest.evt_time, 'arrow_down')
190 self._event(FunctionalTest.evt_time, 'arrow_down')
191 self._event(FunctionalTest.evt_time, 'arrow_down')
192 self._event(FunctionalTest.evt_time, 'rcontrol')
193 self._event(FunctionalTest.evt_time, 'arrow_up')
194 self._event(FunctionalTest.evt_time, 'arrow_up')
195
196 def _do_screenshots_exit(self):
197 self._event(FunctionalTest.evt_time, 'arrow_down')
198 self._event(FunctionalTest.evt_time, 'arrow_down')
199 self._event(FunctionalTest.evt_time, 'arrow_down')
200 self._event(FunctionalTest.evt_time, 'arrow_down')
201 self._event(FunctionalTest.evt_time, 'arrow_down')
202 self._event(FunctionalTest.evt_time, 'rcontrol')
203 self._event(FunctionalTest.evt_time, 'arrow_down')
204 self._verify()
205 self._event(FunctionalTest.evt_time, 'rcontrol')
206 self._exit()
207
208 def _do_screenshots_2(self):
209 info('_do_screenshots_2')
210 self._do_screenshots_restore_options()
211 self._do_screenshots_game()
212 self._do_screenshots_end()
213
214 def _do_screenshots_restore_options(self):
215 # go to options
216 self._event(FunctionalTest.evt_time, 'joypad0-dpad_down', True)
217 self._event(FunctionalTest.evt_time, 'arrow_down')
218 self._event(FunctionalTest.evt_time, 'rcontrol')
219 self._screenshot(FunctionalTest.screenshot_time, 'options_menu_restored')
220 # # language
221 self._event(FunctionalTest.evt_time, 'rcontrol')
222 self._event(FunctionalTest.evt_time, 'arrow_up')
223 self._event(FunctionalTest.evt_time, 'rcontrol')
224 self._screenshot(FunctionalTest.screenshot_time, 'language_en_restored')
225 # # volume
226 self._event(FunctionalTest.evt_time, 'arrow_down')
227 self._event(FunctionalTest.evt_time, 'arrow_left')
228 self._event(FunctionalTest.evt_time, 'arrow_left')
229 self._screenshot(FunctionalTest.screenshot_time, 'volume_restored')
230 # car's number
231 self._event(FunctionalTest.evt_time, 'arrow_down')
232 self._event(FunctionalTest.evt_time, 'rcontrol')
233 self._event(FunctionalTest.evt_time, 'arrow_down')
234 self._event(FunctionalTest.evt_time, 'arrow_down')
235 self._event(FunctionalTest.evt_time, 'arrow_down')
236 self._event(FunctionalTest.evt_time, 'arrow_down')
237 self._event(FunctionalTest.evt_time, 'rcontrol')
238 self._screenshot(FunctionalTest.screenshot_time, 'cars_restored')
239 # graphics settings
240 self._event(FunctionalTest.evt_time, 'arrow_down')
241 self._event(FunctionalTest.evt_time, 'rcontrol')
242 self._screenshot(FunctionalTest.screenshot_time, 'graphics_settings')
243 self._event(FunctionalTest.evt_time, 'arrow_down')
244 self._event(FunctionalTest.evt_time, 'arrow_down')
245 self._event(FunctionalTest.evt_time, 'rcontrol')
246 self._screenshot(FunctionalTest.screenshot_time, 'antialiasing')
247 self._event(FunctionalTest.evt_time, 'rcontrol')
248 self._event(FunctionalTest.evt_time, 'arrow_down')
249 self._event(FunctionalTest.evt_time, 'rcontrol')
250 self._screenshot(FunctionalTest.screenshot_time, 'shadows')
251 self._event(FunctionalTest.evt_time, 'rcontrol')
252 self._event(FunctionalTest.evt_time, 'arrow_down')
253 self._event(FunctionalTest.evt_time, 'rcontrol')
254 self._screenshot(FunctionalTest.screenshot_time, 'fog')
255 self._event(FunctionalTest.evt_time, 'rcontrol')
256 self._event(FunctionalTest.evt_time, 'arrow_down')
257 self._event(FunctionalTest.evt_time, 'rcontrol')
258 self._screenshot(FunctionalTest.screenshot_time, 'normal_mapping')
259 self._event(FunctionalTest.evt_time, 'rcontrol')
260 self._event(FunctionalTest.evt_time, 'arrow_down')
261 self._event(FunctionalTest.evt_time, 'rcontrol')
262 self._screenshot(FunctionalTest.screenshot_time, 'occlusion')
263 self._event(FunctionalTest.evt_time, 'rcontrol')
264 self._event(FunctionalTest.evt_time, 'arrow_down')
265 self._event(FunctionalTest.evt_time, 'rcontrol')
266 # input
267 self._event(FunctionalTest.evt_time, 'arrow_down')
268 self._event(FunctionalTest.evt_time, 'rcontrol')
269 self._screenshot(FunctionalTest.screenshot_time, 'input')
270 self._event(FunctionalTest.evt_time, 'rcontrol')
271 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1')
272 self._event(FunctionalTest.evt_time, 'rcontrol')
273 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1_rec')
274 self._event(FunctionalTest.evt_time, '8', True, False)
275 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1_changed')
276 self._event(FunctionalTest.evt_time, 'rcontrol')
277 self._event(FunctionalTest.evt_time, 'arrow_up', True, False)
278 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1_restored')
279 self._event(FunctionalTest.evt_time, 'rcontrol')
280 self._event(FunctionalTest.evt_time, 'w', True, False)
281 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1_already')
282 self._event(FunctionalTest.evt_time, 'rcontrol')
283 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p1_already_closed')
284 self._event(FunctionalTest.evt_time, 'arrow_down')
285 self._event(FunctionalTest.evt_time, 'arrow_down')
286 self._event(FunctionalTest.evt_time, 'arrow_down')
287 self._event(FunctionalTest.evt_time, 'arrow_down')
288 self._event(FunctionalTest.evt_time, 'rcontrol')
289 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p2')
290 self._event(FunctionalTest.evt_time, 'arrow_down')
291 self._event(FunctionalTest.evt_time, 'arrow_down')
292 self._event(FunctionalTest.evt_time, 'arrow_down')
293 self._event(FunctionalTest.evt_time, 'arrow_down')
294 self._event(FunctionalTest.evt_time, 'rcontrol')
295 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p3')
296 self._event(FunctionalTest.evt_time, 'arrow_down')
297 self._event(FunctionalTest.evt_time, 'arrow_down')
298 self._event(FunctionalTest.evt_time, 'arrow_down')
299 self._event(FunctionalTest.evt_time, 'arrow_down')
300 self._event(FunctionalTest.evt_time, 'rcontrol')
301 self._screenshot(FunctionalTest.screenshot_time, 'keyboard_p4')
302 self._event(FunctionalTest.evt_time, 'arrow_down')
303 self._event(FunctionalTest.evt_time, 'arrow_down')
304 self._event(FunctionalTest.evt_time, 'arrow_down')
305 self._event(FunctionalTest.evt_time, 'arrow_down')
306 self._event(FunctionalTest.evt_time, 'rcontrol')
307 self._event(FunctionalTest.evt_time, 'arrow_down')
308 self._event(FunctionalTest.evt_time, 'rcontrol')
309 self._event(FunctionalTest.evt_time, 'arrow_down')
310 self._event(FunctionalTest.evt_time, 'rcontrol')
311 self._event(FunctionalTest.evt_time, 'arrow_down')
312 self._event(FunctionalTest.evt_time, 'rcontrol')
313 self._event(FunctionalTest.evt_time, 'arrow_down')
314 self._event(FunctionalTest.evt_time, 'rcontrol')
315 self._event(FunctionalTest.evt_time, 'arrow_down')
316 self._event(FunctionalTest.evt_time, 'rcontrol')
317 self._event(FunctionalTest.evt_time, 'arrow_up')
318 self._event(FunctionalTest.evt_time, 'arrow_up')
319
320 def _do_screenshots_game(self):
321 # single player
322 self._event(FunctionalTest.evt_time, 'rcontrol')
323 self._screenshot(FunctionalTest.screenshot_time, 'single_player_menu')
324 self._event(FunctionalTest.evt_time, 'rcontrol')
325 self._screenshot(FunctionalTest.screenshot_time, 'track_page')
326 self._event(FunctionalTest.evt_time, 'rcontrol')
327 self._screenshot(FunctionalTest.screenshot_time, 'car_page_start')
328 self._event(FunctionalTest.evt_time, 'arrow_left')
329 self._screenshot(FunctionalTest.screenshot_time, 'car_page_sel')
330 self._event(FunctionalTest.evt_time, 'rcontrol')
331 self._screenshot(FunctionalTest.screenshot_time, 'driver_page_start')
332 self._event(FunctionalTest.evt_time, 'arrow_down')
333 self._event(FunctionalTest.evt_time, 'arrow_down')
334 self._event(FunctionalTest.evt_time, 'rcontrol')
335 self._event(FunctionalTest.evt_time, 'arrow_down')
336 self._event(FunctionalTest.evt_time, 'arrow_down')
337 self._event(FunctionalTest.evt_time, 'rcontrol')
338 self._event(FunctionalTest.evt_time, 'arrow_down')
339 self._event(FunctionalTest.evt_time, 'rcontrol')
340 self._event(FunctionalTest.evt_time, 'arrow_up')
341 self._event(FunctionalTest.evt_time, 'rcontrol')
342 self._event(FunctionalTest.evt_time, 'rcontrol')
343 self._event(FunctionalTest.evt_time, 'arrow_left')
344 self._event(FunctionalTest.evt_time, 'rcontrol')
345 self._event(FunctionalTest.evt_time, 'rcontrol')
346 self._event(FunctionalTest.evt_time, 'rcontrol')
347 self._event(FunctionalTest.evt_time, 'rcontrol')
348 self._event(FunctionalTest.evt_time, 'rcontrol')
349 self._event(FunctionalTest.evt_time, 'arrow_left')
350 self._event(FunctionalTest.evt_time, 'rcontrol')
351 self._event(FunctionalTest.evt_time, 'arrow_up')
352 self._event(FunctionalTest.evt_time, 'rcontrol')
353 self._screenshot(FunctionalTest.screenshot_time, 'driver_page_entry')
354 self._event(FunctionalTest.evt_time, 'backspace')
355 self._event(FunctionalTest.evt_time, 'backspace')
356 self._event(FunctionalTest.evt_time, 'backspace')
357 self._event(FunctionalTest.evt_time, 'backspace')
358 self._event(FunctionalTest.evt_time, 'backspace')
359 self._event(FunctionalTest.evt_time, 'backspace')
360 self._event(FunctionalTest.evt_time, 'backspace')
361 self._event(FunctionalTest.evt_time, 'backspace')
362 self._event(FunctionalTest.evt_time, 'backspace')
363 self._event(FunctionalTest.evt_time, 'backspace')
364 self._screenshot(FunctionalTest.screenshot_time, 'driver_page_entry_empty')
365 self._event(FunctionalTest.evt_time, 'f')
366 self._event(FunctionalTest.evt_time, 'l')
367 self._event(FunctionalTest.evt_time, 'a')
368 self._event(FunctionalTest.evt_time, 'v')
369 self._event(FunctionalTest.evt_time, 'i')
370 self._event(FunctionalTest.evt_time, 'o')
371 self._event(FunctionalTest.evt_time, 'enter')
372 self._screenshot(FunctionalTest.screenshot_time, 'driver_page_entry_full')
373 self._event(FunctionalTest.evt_time, 'arrow_down')
374 self._event(FunctionalTest.evt_time, 'arrow_right')
375 self._screenshot(FunctionalTest.screenshot_time, 'driver_page_sel')
376 # some ai tests
377 self._event(FunctionalTest.evt_time, 'rcontrol')
378 self._event(40, 'escape-up')
379 self._screenshot(FunctionalTest.screenshot_time, 'ingame_menu')
380 self._event(FunctionalTest.evt_time, 'rcontrol')
381 self._screenshot(FunctionalTest.screenshot_time, 'race_back')
382 self._event(FunctionalTest.evt_time, 'escape-up')
383 self._event(FunctionalTest.evt_time, 'arrow_down')
384 self._screenshot(FunctionalTest.screenshot_time, 'ingame_sel')
385 self._event(FunctionalTest.evt_time, 'rcontrol')
386 self._screenshot(FunctionalTest.screenshot_time, 'main_page_back_race')
387
388 def _do_screenshots_end(self):
389 self._event(FunctionalTest.evt_time, 'arrow_down')
390 self._event(FunctionalTest.evt_time, 'arrow_down')
391 self._event(FunctionalTest.evt_time, 'arrow_down')
392 self._event(FunctionalTest.evt_time, 'arrow_down')
393 self._event(FunctionalTest.evt_time, 'arrow_down')
394 self._event(FunctionalTest.evt_time, 'rcontrol')
395 self._screenshot(FunctionalTest.screenshot_time, 'exit_page')
396 self._event(FunctionalTest.evt_time, 'arrow_down')
397 self._screenshot(FunctionalTest.screenshot_time, 'exit_page_sel')
398 self._verify()
399 self._event(FunctionalTest.evt_time, 'rcontrol')
400 self._exit()
401
402 def _do_screenshots(self, idx):
403 [self._do_screenshots_1, self._do_screenshots_2][int(idx) - 1]()