Fixed swizzle of SDL_FillRect() on 24-bit surface (thanks, "nagydavid91"!).
Fixes Bugzilla #2986.
1 /* See COPYING.txt for the full license governing this code. */
5 * Source file for the test harness.
11 #include <SDL_assert.h>
12 #include "SDL_visualtest_harness_argparser.h"
13 #include "SDL_visualtest_process.h"
14 #include "SDL_visualtest_variators.h"
15 #include "SDL_visualtest_screenshot.h"
16 #include "SDL_visualtest_mischelper.h"
18 #if defined(__WIN32__) && !defined(__CYGWIN__)
20 #elif defined(__WIN32__) && defined(__CYGWIN__)
22 #elif defined(__LINUX__)
24 #include <sys/types.h>
27 #error "Unsupported platform"
30 /** Code for the user event triggered when a new action is to be executed */
31 #define ACTION_TIMER_EVENT 0
32 /** Code for the user event triggered when the maximum timeout is reached */
33 #define KILL_TIMER_EVENT 1
34 /** FPS value used for delays in the action loop */
35 #define ACTION_LOOP_FPS 10
37 /** Value returned by RunSUTAndTest() when the test has passed */
39 /** Value returned by RunSUTAndTest() when the test has failed */
41 /** Value returned by RunSUTAndTest() on a fatal error */
44 static SDL_ProcessInfo pinfo;
45 static SDL_ProcessExitStatus sut_exitstatus;
46 static SDLVisualTest_HarnessState state;
47 static SDLVisualTest_Variator variator;
48 static SDLVisualTest_ActionNode* current; /* the current action being performed */
49 static SDL_TimerID action_timer, kill_timer;
51 /* returns a char* to be passed as the format argument of a printf-style function. */
55 return "Usage: \n%s --sutapp xyz"
56 " [--sutargs abc | --parameter-config xyz.parameters"
57 " [--variator exhaustive|random]"
58 " [--num-variations N] [--no-launch]] [--timeout hh:mm:ss]"
59 " [--action-config xyz.actions]"
60 " [--output-dir /path/to/output]"
61 " [--verify-dir /path/to/verify]"
62 " or --config app.config";
65 /* register Ctrl+C handlers */
66 #if defined(__LINUX__) || defined(__CYGWIN__)
68 CtrlCHandlerCallback(int signum)
71 SDLTest_Log("Ctrl+C received");
72 event.type = SDL_QUIT;
73 SDL_PushEvent(&event);
78 ActionTimerCallback(Uint32 interval, void* param)
81 SDL_UserEvent userevent;
82 Uint32 next_action_time;
84 /* push an event to handle the action */
85 userevent.type = SDL_USEREVENT;
86 userevent.code = ACTION_TIMER_EVENT;
87 userevent.data1 = ¤t->action;
88 userevent.data2 = NULL;
90 event.type = SDL_USEREVENT;
91 event.user = userevent;
92 SDL_PushEvent(&event);
94 /* calculate the new interval and return it */
96 next_action_time = current->next->action.time - current->action.time;
103 current = current->next;
104 return next_action_time;
108 KillTimerCallback(Uint32 interval, void* param)
111 SDL_UserEvent userevent;
113 userevent.type = SDL_USEREVENT;
114 userevent.code = KILL_TIMER_EVENT;
115 userevent.data1 = NULL;
116 userevent.data2 = NULL;
118 event.type = SDL_USEREVENT;
119 event.user = userevent;
120 SDL_PushEvent(&event);
127 ProcessAction(SDLVisualTest_Action* action, int* sut_running, char* args)
129 if(!action || !sut_running)
134 case SDL_ACTION_KILL:
135 SDLTest_Log("Action: Kill SUT");
136 if(SDL_IsProcessRunning(&pinfo) == 1 &&
137 !SDL_KillProcess(&pinfo, &sut_exitstatus))
139 SDLTest_LogError("SDL_KillProcess() failed");
145 case SDL_ACTION_QUIT:
146 SDLTest_Log("Action: Quit SUT");
147 if(SDL_IsProcessRunning(&pinfo) == 1 &&
148 !SDL_QuitProcess(&pinfo, &sut_exitstatus))
150 SDLTest_LogError("SDL_QuitProcess() failed");
156 case SDL_ACTION_LAUNCH:
160 SDL_ProcessInfo action_process;
161 SDL_ProcessExitStatus ps;
163 path = action->extra.process.path;
164 args = action->extra.process.args;
167 SDLTest_Log("Action: Launch process: %s with arguments: %s",
171 SDLTest_Log("Action: Launch process: %s", path);
172 if(!SDL_LaunchProcess(path, args, &action_process))
174 SDLTest_LogError("SDL_LaunchProcess() failed");
178 /* small delay so that the process can do its job */
181 if(SDL_IsProcessRunning(&action_process) > 0)
183 SDLTest_LogError("Process %s took too long too complete."
184 " Force killing...", action->extra);
185 if(!SDL_KillProcess(&action_process, &ps))
187 SDLTest_LogError("SDL_KillProcess() failed");
194 case SDL_ACTION_SCREENSHOT:
196 char path[MAX_PATH_LEN], hash[33];
198 SDLTest_Log("Action: Take screenshot");
199 /* can't take a screenshot if the SUT isn't running */
200 if(SDL_IsProcessRunning(&pinfo) != 1)
202 SDLTest_LogError("SUT has quit.");
207 /* file name for the screenshot image */
208 SDLVisualTest_HashString(args, hash);
209 SDL_snprintf(path, MAX_PATH_LEN, "%s/%s", state.output_dir, hash);
210 if(!SDLVisualTest_ScreenshotProcess(&pinfo, path))
212 SDLTest_LogError("SDLVisualTest_ScreenshotProcess() failed");
218 case SDL_ACTION_VERIFY:
222 SDLTest_Log("Action: Verify screenshot");
223 ret = SDLVisualTest_VerifyScreenshots(args, state.output_dir,
228 SDLTest_LogError("SDLVisualTest_VerifyScreenshots() failed");
233 SDLTest_Log("Verification failed: Images were not equal.");
237 SDLTest_Log("Verification successful.");
240 SDLTest_Log("Verfication skipped.");
247 SDLTest_LogError("Invalid action type");
256 RunSUTAndTest(char* sutargs, int variation_num)
258 int success, sut_running, return_code;
262 return_code = TEST_PASSED;
266 SDLTest_LogError("sutargs argument cannot be NULL");
267 return_code = TEST_ERROR;
268 goto runsutandtest_cleanup_generic;
271 SDLVisualTest_HashString(sutargs, hash);
272 SDLTest_Log("Hash: %s", hash);
274 success = SDL_LaunchProcess(state.sutapp, sutargs, &pinfo);
277 SDLTest_Log("Could not launch SUT.");
278 return_code = TEST_ERROR;
279 goto runsutandtest_cleanup_generic;
281 SDLTest_Log("SUT launch successful.");
282 SDLTest_Log("Process will be killed in %d milliseconds", state.timeout);
285 /* launch the timers */
286 SDLTest_Log("Performing actions..");
287 current = state.action_queue.front;
292 action_timer = SDL_AddTimer(current->action.time, ActionTimerCallback, NULL);
295 SDLTest_LogError("SDL_AddTimer() failed");
296 return_code = TEST_ERROR;
297 goto runsutandtest_cleanup_timer;
300 kill_timer = SDL_AddTimer(state.timeout, KillTimerCallback, NULL);
303 SDLTest_LogError("SDL_AddTimer() failed");
304 return_code = TEST_ERROR;
305 goto runsutandtest_cleanup_timer;
308 /* the timer stops running if the actions queue is empty, and the
309 SUT stops running if it crashes or if we encounter a KILL/QUIT action */
312 /* process the actions by using an event queue */
313 while(SDL_PollEvent(&event))
315 if(event.type == SDL_USEREVENT)
317 if(event.user.code == ACTION_TIMER_EVENT)
319 SDLVisualTest_Action* action;
321 action = (SDLVisualTest_Action*)event.user.data1;
323 switch(ProcessAction(action, &sut_running, sutargs))
329 return_code = TEST_FAILED;
330 goto runsutandtest_cleanup_timer;
334 SDLTest_LogError("ProcessAction() failed");
335 return_code = TEST_ERROR;
336 goto runsutandtest_cleanup_timer;
339 else if(event.user.code == KILL_TIMER_EVENT)
341 SDLTest_LogError("Maximum timeout reached. Force killing..");
342 return_code = TEST_FAILED;
343 goto runsutandtest_cleanup_timer;
346 else if(event.type == SDL_QUIT)
348 SDLTest_LogError("Received QUIT event. Testharness is quitting..");
349 return_code = TEST_ERROR;
350 goto runsutandtest_cleanup_timer;
353 SDL_Delay(1000/ACTION_LOOP_FPS);
356 SDLTest_Log("SUT exit code was: %d", sut_exitstatus.exit_status);
357 if(sut_exitstatus.exit_status == 0)
359 return_code = TEST_PASSED;
360 goto runsutandtest_cleanup_timer;
364 return_code = TEST_FAILED;
365 goto runsutandtest_cleanup_timer;
368 return_code = TEST_ERROR;
369 goto runsutandtest_cleanup_generic;
371 runsutandtest_cleanup_timer:
372 if(action_timer && !SDL_RemoveTimer(action_timer))
374 SDLTest_Log("SDL_RemoveTimer() failed");
375 return_code = TEST_ERROR;
378 if(kill_timer && !SDL_RemoveTimer(kill_timer))
380 SDLTest_Log("SDL_RemoveTimer() failed");
381 return_code = TEST_ERROR;
383 /* runsutandtest_cleanup_process: */
384 if(SDL_IsProcessRunning(&pinfo) && !SDL_KillProcess(&pinfo, &sut_exitstatus))
386 SDLTest_Log("SDL_KillProcess() failed");
387 return_code = TEST_ERROR;
389 runsutandtest_cleanup_generic:
393 /** Entry point for testharness */
395 main(int argc, char* argv[])
397 int i, passed, return_code, failed;
399 /* freeing resources, linux style! */
404 SDLTest_Log(usage(), argv[0]);
405 goto cleanup_generic;
408 #if defined(__LINUX__) || defined(__CYGWIN__)
409 signal(SIGINT, CtrlCHandlerCallback);
412 /* parse arguments */
413 if(!SDLVisualTest_ParseHarnessArgs(argv + 1, &state))
415 SDLTest_Log(usage(), argv[0]);
417 goto cleanup_generic;
419 SDLTest_Log("Parsed harness arguments successfully.");
422 if(SDL_Init(SDL_INIT_TIMER) == -1)
424 SDLTest_LogError("SDL_Init() failed.");
425 SDLVisualTest_FreeHarnessState(&state);
427 goto cleanup_harness_state;
430 /* create an output directory if none exists */
431 #if defined(__LINUX__) || defined(__CYGWIN__)
432 mkdir(state.output_dir, 0777);
433 #elif defined(__WIN32__)
434 _mkdir(state.output_dir);
436 #error "Unsupported platform"
439 /* test with sutargs */
440 if(SDL_strlen(state.sutargs))
442 SDLTest_Log("Running: %s %s", state.sutapp, state.sutargs);
445 switch(RunSUTAndTest(state.sutargs, 0))
448 SDLTest_Log("Status: PASSED");
452 SDLTest_Log("Status: FAILED");
456 SDLTest_LogError("Some error occurred while testing.");
464 if(state.sut_config.num_options > 0)
466 char* variator_name = state.variator_type == SDL_VARIATOR_RANDOM ?
467 "RANDOM" : "EXHAUSTIVE";
468 if(state.num_variations > 0)
469 SDLTest_Log("Testing SUT with variator: %s for %d variations",
470 variator_name, state.num_variations);
472 SDLTest_Log("Testing SUT with variator: %s and ALL variations",
474 /* initialize the variator */
475 if(!SDLVisualTest_InitVariator(&variator, &state.sut_config,
476 state.variator_type, 0))
478 SDLTest_LogError("Could not initialize variator");
483 /* iterate through all the variations */
486 for(i = 0; state.num_variations > 0 ? (i < state.num_variations) : 1; i++)
488 char* args = SDLVisualTest_GetNextVariation(&variator);
491 SDLTest_Log("\nVariation number: %d\nArguments: %s", i + 1, args);
495 switch(RunSUTAndTest(args, i + 1))
498 SDLTest_Log("Status: PASSED");
503 SDLTest_Log("Status: FAILED");
508 SDLTest_LogError("Some error occurred while testing.");
509 goto cleanup_variator;
517 SDLTest_Log("Testing complete.");
518 SDLTest_Log("%d/%d tests passed.", passed, passed + failed);
520 goto cleanup_variator;
526 SDLVisualTest_FreeVariator(&variator);
529 cleanup_harness_state:
530 SDLVisualTest_FreeHarnessState(&state);