src/test/SDL_test_harness.c
changeset 6718 918ba414168b
parent 6717 2acd95060548
child 6721 53b71f45a53a
     1.1 --- a/src/test/SDL_test_harness.c	Fri Nov 30 23:25:34 2012 -0800
     1.2 +++ b/src/test/SDL_test_harness.c	Sat Dec 01 14:48:30 2012 -0800
     1.3 @@ -23,7 +23,10 @@
     1.4  
     1.5  #include "SDL_test.h"
     1.6  
     1.7 -// TODO: port over harness
     1.8 +#include <stdio.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +// TODO: port over remaining harness
    1.12  
    1.13  /**
    1.14   * Generates a random run seed string for the harness. The generated seed
    1.15 @@ -140,3 +143,48 @@
    1.16  
    1.17  	return keys[0];
    1.18  }
    1.19 +
    1.20 +/**
    1.21 + * \brief Set timeout handler for test.
    1.22 + *
    1.23 + * Note: SDL_Init(SDL_INIT_TIMER) will be called if it wasn't done so before.
    1.24 + *
    1.25 + * \param timeout Timeout interval in seconds.
    1.26 + * \param callback Function that will be called after timeout has elapsed.
    1.27 + * 
    1.28 + * \return Timer id or -1 on failure.
    1.29 + */
    1.30 +SDL_TimerID
    1.31 +SetTestTimeout(int timeout, void (*callback)())
    1.32 +{
    1.33 +	Uint32 timeoutInMilliseconds;
    1.34 +	SDL_TimerID timerID;
    1.35 +
    1.36 +	if (callback == NULL) {
    1.37 +		SDLTest_LogError("Timeout callback can't be NULL");
    1.38 +		return -1;
    1.39 +	}
    1.40 +
    1.41 +	if (timeout < 0) {
    1.42 +		SDLTest_LogError("Timeout value must be bigger than zero.");
    1.43 +		return -1;
    1.44 +	}
    1.45 +
    1.46 +	/* Init SDL timer if not initialized before */
    1.47 +	if (SDL_WasInit(SDL_INIT_TIMER) == 0) {
    1.48 +		if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
    1.49 +			SDLTest_LogError("Failed to init timer subsystem: %s", SDL_GetError());
    1.50 +			return -1;
    1.51 +		}
    1.52 +	}
    1.53 +
    1.54 +	/* Set timer */
    1.55 +	timeoutInMilliseconds = timeout * 1000;
    1.56 +	timerID = SDL_AddTimer(timeoutInMilliseconds, (SDL_TimerCallback)callback, 0x0);
    1.57 +	if (timerID == 0) {
    1.58 +		SDLTest_LogError("Creation of SDL timer failed: %s", SDL_GetError());
    1.59 +		return -1;
    1.60 +	}
    1.61 +
    1.62 +	return timerID;
    1.63 +}