test/testautomation_video.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 25 Feb 2013 08:05:26 -0800
changeset 6921 9a29af9de577
parent 6891 177f29ae5279
child 6936 76d9c31e823d
permissions -rw-r--r--
Add tests to video suite
     1 /**
     2  * Video test suite
     3  */
     4 
     5 #include <stdio.h>
     6 
     7 #include "SDL.h"
     8 #include "SDL_test.h"
     9 
    10 /* Private helpers */
    11 
    12 /* 
    13  * Create a test window
    14  */
    15 SDL_Window *_createVideoSuiteTestWindow(const char *title)
    16 {
    17   SDL_Window* window;
    18   int x, y, w, h;
    19   SDL_WindowFlags flags;
    20 
    21   /* Standard window */
    22   x = SDLTest_RandomIntegerInRange(1, 100);
    23   y = SDLTest_RandomIntegerInRange(1, 100);
    24   w = SDLTest_RandomIntegerInRange(320, 1024);
    25   h = SDLTest_RandomIntegerInRange(320, 768);
    26   flags = SDL_WINDOW_SHOWN;
    27   
    28   window = SDL_CreateWindow(title, x, y, w, h, flags);
    29   SDLTest_AssertPass("Call to SDL_CreateWindow('Title',%d,%d,%d,%d,%d)", x, y, w, h, flags);
    30   SDLTest_AssertCheck(window != NULL, "Validate that returned window struct is not NULL");
    31 
    32   return window;
    33 }
    34 
    35 /*
    36  * Destroy test window  
    37  */
    38 void _destroyVideoSuiteTestWindow(SDL_Window *window)
    39 {
    40   if (window != NULL) {  
    41      SDL_DestroyWindow(window);
    42      window = NULL;
    43      SDLTest_AssertPass("Call to SDL_DestroyWindow");
    44   }
    45 }
    46 
    47 /* Test case functions */
    48 
    49 /**
    50  * @brief Enable and disable screensaver while checking state
    51  */
    52 int
    53 video_enableDisableScreensaver(void *arg)
    54 {
    55 	SDL_bool initialResult;
    56 	SDL_bool result;
    57 
    58 	/* Get current state and proceed according to current state */
    59 	initialResult = SDL_IsScreenSaverEnabled();
    60 	SDLTest_AssertPass("Call to SDL_IsScreenSaverEnabled()");	
    61 	if (initialResult == SDL_TRUE) {
    62 	
    63 	  /* Currently enabled: disable first, then enable again */
    64 	  
    65 	  /* Disable screensaver and check */	
    66 	  SDL_DisableScreenSaver();
    67 	  SDLTest_AssertPass("Call to SDL_DisableScreenSaver()");	
    68 	  result = SDL_IsScreenSaverEnabled();
    69 	  SDLTest_AssertPass("Call to SDL_IsScreenSaverEnabled()");
    70 	  SDLTest_AssertCheck(result == SDL_FALSE, "Verify result from SDL_IsScreenSaverEnabled, expected: %i, got: %i", SDL_FALSE, result);
    71 	
    72 	  /* Enable screensaver and check */	
    73 	  SDL_EnableScreenSaver();
    74 	  SDLTest_AssertPass("Call to SDL_EnableScreenSaver()");
    75 	  result = SDL_IsScreenSaverEnabled();
    76 	  SDLTest_AssertPass("Call to SDL_IsScreenSaverEnabled()");
    77 	  SDLTest_AssertCheck(result == SDL_TRUE, "Verify result from SDL_IsScreenSaverEnabled, expected: %i, got: %i", SDL_TRUE, result);
    78 
    79 	} else {
    80 
    81 	  /* Currently disabled: enable first, then disable again */
    82 	  
    83 	  /* Enable screensaver and check */	
    84 	  SDL_EnableScreenSaver();
    85 	  SDLTest_AssertPass("Call to SDL_EnableScreenSaver()");
    86 	  result = SDL_IsScreenSaverEnabled();
    87 	  SDLTest_AssertPass("Call to SDL_IsScreenSaverEnabled()");
    88 	  SDLTest_AssertCheck(result == SDL_TRUE, "Verify result from SDL_IsScreenSaverEnabled, expected: %i, got: %i", SDL_TRUE, result);
    89 
    90 	  /* Disable screensaver and check */	
    91 	  SDL_DisableScreenSaver();
    92 	  SDLTest_AssertPass("Call to SDL_DisableScreenSaver()");	
    93 	  result = SDL_IsScreenSaverEnabled();
    94 	  SDLTest_AssertPass("Call to SDL_IsScreenSaverEnabled()");
    95 	  SDLTest_AssertCheck(result == SDL_FALSE, "Verify result from SDL_IsScreenSaverEnabled, expected: %i, got: %i", SDL_FALSE, result);	
    96 	}	
    97 	
    98 	return TEST_COMPLETED;
    99 }
   100 
   101 /**
   102  * @brief Tests the functionality of the SDL_CreateWindow function using different positions
   103  */
   104 int
   105 video_createWindowVariousPositions(void *arg)
   106 {
   107   SDL_Window* window;
   108   const char* title = "video_createWindowVariousPositions Test Window";
   109   int x, y, w, h;
   110   int xVariation, yVariation;
   111   
   112   for (xVariation = 0; xVariation < 6; xVariation++) {
   113    for (yVariation = 0; yVariation < 6; yVariation++) {
   114     switch(xVariation) {
   115      case 0:
   116       /* Zero X Position */  
   117       x = 0;
   118       break;
   119      case 1:
   120       /* Random X position inside screen */  
   121       x = SDLTest_RandomIntegerInRange(1, 100);
   122       break;
   123      case 2:
   124       /* Random X position outside screen (positive) */  
   125       x = SDLTest_RandomIntegerInRange(10000, 11000);
   126       break;
   127      case 3:
   128       /* Random X position outside screen (negative) */  
   129       x = SDLTest_RandomIntegerInRange(-1000, -100);
   130       break;
   131      case 4:
   132       /* Centered X position */  
   133       x = SDL_WINDOWPOS_CENTERED;
   134       break;
   135      case 5:
   136       /* Undefined X position */  
   137       x = SDL_WINDOWPOS_UNDEFINED;
   138       break;
   139     }
   140 
   141     switch(yVariation) {
   142      case 0:
   143       /* Zero X Position */  
   144       y = 0;
   145       break;
   146      case 1:
   147       /* Random X position inside screen */  
   148       y = SDLTest_RandomIntegerInRange(1, 100);
   149       break;
   150      case 2:
   151       /* Random X position outside screen (positive) */  
   152       y = SDLTest_RandomIntegerInRange(10000, 11000);
   153       break;
   154      case 3:
   155       /* Random Y position outside screen (negative) */  
   156       y = SDLTest_RandomIntegerInRange(-1000, -100);
   157       break;
   158      case 4:
   159       /* Centered Y position */  
   160       y = SDL_WINDOWPOS_CENTERED;
   161       break;
   162      case 5:
   163       /* Undefined Y position */  
   164       y = SDL_WINDOWPOS_UNDEFINED;
   165       break;
   166     }
   167      
   168     w = SDLTest_RandomIntegerInRange(32, 96);
   169     h = SDLTest_RandomIntegerInRange(32, 96);
   170     window = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN);
   171     SDLTest_AssertPass("Call to SDL_CreateWindow('Title',%d,%d,%d,%d,SHOWN)", x, y, w, h);
   172     SDLTest_AssertCheck(window != NULL, "Validate that returned window struct is not NULL");
   173 
   174     /* Clean up */    
   175     _destroyVideoSuiteTestWindow(window);
   176    }
   177   } 
   178 
   179   return TEST_COMPLETED;
   180 }
   181 
   182 /**
   183  * @brief Tests the functionality of the SDL_CreateWindow function using different sizes
   184  */
   185 int
   186 video_createWindowVariousSizes(void *arg)
   187 {
   188   SDL_Window* window;
   189   const char* title = "video_createWindowVariousSizes Test Window";
   190   int x, y, w, h;
   191   int wVariation, hVariation;
   192   
   193   x = SDLTest_RandomIntegerInRange(1, 100);
   194   y = SDLTest_RandomIntegerInRange(1, 100);
   195   for (wVariation = 0; wVariation < 3; wVariation++) {
   196    for (hVariation = 0; hVariation < 3; hVariation++) {
   197     switch(wVariation) {
   198      case 0:
   199       /* Width of 1 */  
   200       w = 1;
   201       break;
   202      case 1:
   203       /* Random "normal" width */  
   204       w = SDLTest_RandomIntegerInRange(320, 1920);
   205       break;
   206      case 2:
   207       /* Random "large" width */  
   208       w = SDLTest_RandomIntegerInRange(2048, 4095);
   209       break;
   210     }
   211 
   212     switch(hVariation) {
   213      case 0:
   214       /* Height of 1 */  
   215       h = 1;
   216       break;
   217      case 1:
   218       /* Random "normal" height */  
   219       h = SDLTest_RandomIntegerInRange(320, 1080);
   220       break;
   221      case 2:
   222       /* Random "large" height */  
   223       h = SDLTest_RandomIntegerInRange(2048, 4095);
   224       break;
   225      }
   226      
   227     window = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN);
   228     SDLTest_AssertPass("Call to SDL_CreateWindow('Title',%d,%d,%d,%d,SHOWN)", x, y, w, h);
   229     SDLTest_AssertCheck(window != NULL, "Validate that returned window struct is not NULL");
   230 
   231     /* Clean up */    
   232     _destroyVideoSuiteTestWindow(window);
   233    }
   234   }  
   235 
   236   return TEST_COMPLETED;
   237 }
   238 
   239 /**
   240  * @brief Tests the functionality of the SDL_CreateWindow function using different flags
   241  */
   242 int
   243 video_createWindowVariousFlags(void *arg)
   244 {
   245   SDL_Window* window;
   246   const char* title = "video_createWindowVariousFlags Test Window";
   247   int x, y, w, h;
   248   int fVariation;
   249   SDL_WindowFlags flags;
   250   
   251   /* Standard window */
   252   x = SDLTest_RandomIntegerInRange(1, 100);
   253   y = SDLTest_RandomIntegerInRange(1, 100);
   254   w = SDLTest_RandomIntegerInRange(320, 1024);
   255   h = SDLTest_RandomIntegerInRange(320, 768);
   256 
   257   for (fVariation = 0; fVariation < 13; fVariation++) {
   258     switch(fVariation) {
   259      case 0:
   260       flags = SDL_WINDOW_FULLSCREEN;
   261       /* Skip - blanks screen; comment out next line to run test */
   262       continue;   
   263       break;
   264      case 1:
   265       flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
   266       /* Skip - blanks screen; comment out next line to run test */
   267       continue;  
   268       break;
   269      case 2:
   270       flags = SDL_WINDOW_OPENGL;
   271       break;  
   272      case 3:
   273       flags = SDL_WINDOW_SHOWN;
   274       break;   
   275      case 4:    
   276       flags = SDL_WINDOW_HIDDEN;
   277       break;     
   278      case 5:
   279       flags = SDL_WINDOW_BORDERLESS;
   280       break;       
   281      case 6:
   282       flags = SDL_WINDOW_RESIZABLE;
   283       break;         
   284      case 7:
   285       flags = SDL_WINDOW_MINIMIZED;
   286       break;           
   287      case 8:
   288       flags = SDL_WINDOW_MAXIMIZED;
   289       break;
   290      case 9: 
   291       flags = SDL_WINDOW_INPUT_GRABBED;
   292       break;
   293      case 10:              
   294       flags = SDL_WINDOW_INPUT_FOCUS;
   295       break;                 
   296      case 11:                      
   297       flags = SDL_WINDOW_MOUSE_FOCUS;
   298       break;
   299      case 12: 
   300       flags = SDL_WINDOW_FOREIGN;
   301       break;
   302     }
   303        
   304     window = SDL_CreateWindow(title, x, y, w, h, flags);
   305     SDLTest_AssertPass("Call to SDL_CreateWindow('Title',%d,%d,%d,%d,%d)", x, y, w, h, flags);
   306     SDLTest_AssertCheck(window != NULL, "Validate that returned window struct is not NULL");
   307 
   308 
   309     /* Clean up */    
   310     _destroyVideoSuiteTestWindow(window);  
   311   }
   312 
   313   return TEST_COMPLETED;
   314 }
   315 
   316 
   317 /**
   318  * @brief Tests the functionality of the SDL_GetWindowFlags function
   319  */
   320 int
   321 video_getWindowFlags(void *arg)
   322 {
   323   SDL_Window* window;
   324   const char* title = "video_getWindowFlags Test Window";
   325   SDL_WindowFlags flags;
   326   Uint32 actualFlags;
   327   
   328   /* Reliable flag set always set in test window */
   329   flags = SDL_WINDOW_SHOWN;
   330   
   331   /* Call against new test window */ 
   332   window = _createVideoSuiteTestWindow(title);
   333   if (window != NULL) {
   334       actualFlags = SDL_GetWindowFlags(window);
   335       SDLTest_AssertPass("Call to SDL_GetWindowFlags");
   336       SDLTest_AssertCheck((flags & actualFlags) == flags, "Verify returned value has flags %d set, got: %d", flags, actualFlags);
   337   }
   338 
   339   /* Clean up */    
   340   _destroyVideoSuiteTestWindow(window);
   341   
   342   return TEST_COMPLETED;
   343 }
   344 
   345 /**
   346  * @brief Tests the functionality of the SDL_GetNumDisplayModes function
   347  */
   348 int
   349 video_getNumDisplayModes(void *arg)
   350 {
   351   int result;
   352   int displayNum;
   353   int i;
   354 
   355   /* Get number of displays */
   356   displayNum = SDL_GetNumVideoDisplays();
   357   SDLTest_AssertPass("Call to SDL_GetNumVideoDisplays");
   358 
   359   /* Make call for each display */  
   360   for (i=0; i<displayNum; i++) {
   361     result = SDL_GetNumDisplayModes(i);
   362     SDLTest_AssertPass("Call to SDL_GetNumDisplayModes(%d)", i);
   363     SDLTest_AssertCheck(result >= 1, "Validate returned value from function; expected: >=1; got: %d", result);
   364   }
   365 
   366   return TEST_COMPLETED;
   367 }
   368 
   369 /**
   370  * @brief Tests negative call to SDL_GetNumDisplayModes function
   371  */
   372 int
   373 video_getNumDisplayModesNegative(void *arg)
   374 {
   375   int result;
   376   int displayNum;
   377   int displayIndex;
   378 
   379   /* Get number of displays */
   380   displayNum = SDL_GetNumVideoDisplays();
   381   SDLTest_AssertPass("Call to SDL_GetNumVideoDisplays");
   382 
   383   /* Invalid boundary values */
   384   displayIndex =  SDLTest_RandomSint32BoundaryValue(0, displayNum, SDL_FALSE);
   385   result = SDL_GetNumDisplayModes(displayIndex);
   386   SDLTest_AssertPass("Call to SDL_GetNumDisplayModes(%d=out-of-bounds/boundary)", displayIndex);
   387   SDLTest_AssertCheck(result < 0, "Validate returned value from function; expected: <0; got: %d", result);  
   388   
   389   /* Large (out-of-bounds) display index */
   390   displayIndex = SDLTest_RandomIntegerInRange(-2000, -1000);
   391   result = SDL_GetNumDisplayModes(displayIndex);
   392   SDLTest_AssertPass("Call to SDL_GetNumDisplayModes(%d=out-of-bounds/large negative)", displayIndex);
   393   SDLTest_AssertCheck(result < 0, "Validate returned value from function; expected: <0; got: %d", result);  
   394 
   395   displayIndex = SDLTest_RandomIntegerInRange(1000, 2000);
   396   result = SDL_GetNumDisplayModes(displayIndex);
   397   SDLTest_AssertPass("Call to SDL_GetNumDisplayModes(%d=out-of-bounds/large positive)", displayIndex);
   398   SDLTest_AssertCheck(result < 0, "Validate returned value from function; expected: <0; got: %d", result);  
   399 
   400   return TEST_COMPLETED;
   401 }
   402 
   403 /**
   404  * @brief Tests the functionality of the SDL_GetClosestDisplayMode function against current resolution
   405  */
   406 int
   407 video_getClosestDisplayModeCurrentResolution(void *arg)
   408 {
   409   int result;
   410   SDL_DisplayMode current; 
   411   SDL_DisplayMode target; 
   412   SDL_DisplayMode closest;
   413   SDL_DisplayMode* dResult;
   414   int displayNum;
   415   int i;
   416   int variation;
   417 
   418   /* Get number of displays */
   419   displayNum = SDL_GetNumVideoDisplays();
   420   SDLTest_AssertPass("Call to SDL_GetNumVideoDisplays");
   421 
   422   /* Make calls for each display */  
   423   for (i=0; i<displayNum; i++) {
   424     SDLTest_Log("Testing against display: %d", i);
   425   
   426     /* Get first display mode to get a sane resolution; this should always work */
   427     result = SDL_GetDisplayMode(i, 0, &current);
   428     SDLTest_AssertPass("Call to SDL_GetDisplayMode");
   429     SDLTest_AssertCheck(result == 0, "Verify return value, expected: 0, got: %d", result);
   430     if (result != 0) {
   431       return TEST_ABORTED;
   432     }
   433        
   434     /* Set the desired resolution equals to current resolution */
   435     target.w = current.w;
   436     target.h = current.h;    
   437     for (variation = 0; variation < 8; variation ++) {
   438       /* Vary constraints on other query parameters */
   439       target.format = (variation & 1) ? current.format : 0;
   440       target.refresh_rate = (variation & 2) ? current.refresh_rate : 0;
   441       target.driverdata = (variation & 4) ? current.driverdata : 0;
   442           
   443       /* Make call */
   444       dResult = SDL_GetClosestDisplayMode(i, &target, &closest);
   445       SDLTest_AssertPass("Call to SDL_GetClosestDisplayMode(target=current/variation%d)", variation);
   446       SDLTest_AssertCheck(dResult != NULL, "Verify returned value is not NULL");
   447     
   448       /* Check that one gets the current resolution back again */
   449       SDLTest_AssertCheck(closest.w == current.w, "Verify returned width matches current width; expected: %d, got: %d", current.w, closest.w);
   450       SDLTest_AssertCheck(closest.h == current.h, "Verify returned height matches current height; expected: %d, got: %d", current.h, closest.h);
   451       SDLTest_AssertCheck(closest.w == dResult->w, "Verify return value matches assigned value; expected: %d, got: %d", closest.w, dResult->w);
   452       SDLTest_AssertCheck(closest.h == dResult->h, "Verify return value matches assigned value; expected: %d, got: %d", closest.h, dResult->h);
   453     }
   454   }
   455 
   456   return TEST_COMPLETED;
   457 }
   458 
   459 /**
   460  * @brief Tests the functionality of the SDL_GetClosestDisplayMode function against random resolution
   461  */
   462 int
   463 video_getClosestDisplayModeRandomResolution(void *arg)
   464 {
   465   SDL_DisplayMode target; 
   466   SDL_DisplayMode closest;
   467   SDL_DisplayMode* dResult;
   468   int displayNum;
   469   int i;
   470   int variation;
   471 
   472   /* Get number of displays */
   473   displayNum = SDL_GetNumVideoDisplays();
   474   SDLTest_AssertPass("Call to SDL_GetNumVideoDisplays");
   475 
   476   /* Make calls for each display */  
   477   for (i=0; i<displayNum; i++) {
   478     SDLTest_Log("Testing against display: %d", i);
   479          
   480     for (variation = 0; variation < 16; variation ++) {
   481     
   482       /* Set random constraints */
   483       target.w = (variation & 1) ? SDLTest_RandomIntegerInRange(1, 4096) : 0;
   484       target.h = (variation & 2) ? SDLTest_RandomIntegerInRange(1, 4096) : 0;    
   485       target.format = (variation & 4) ? SDLTest_RandomIntegerInRange(1, 10) : 0;
   486       target.refresh_rate = (variation & 8) ? SDLTest_RandomIntegerInRange(25, 120) : 0;
   487       target.driverdata = 0;
   488           
   489       /* Make call; may or may not find anything, so don't validate any further */
   490       dResult = SDL_GetClosestDisplayMode(i, &target, &closest);
   491       SDLTest_AssertPass("Call to SDL_GetClosestDisplayMode(target=random/variation%d)", variation);      
   492     }
   493   }
   494 
   495   return TEST_COMPLETED;
   496 }
   497 
   498 /**
   499  * @brief Tests call to SDL_GetWindowBrightness
   500  */
   501 int
   502 video_getWindowBrightness(void *arg)
   503 {
   504   SDL_Window* window;
   505   const char* title = "video_getWindowBrightness Test Window";
   506   float result;
   507 
   508   /* Call against new test window */ 
   509   window = _createVideoSuiteTestWindow(title);
   510   if (window != NULL) {
   511       result = SDL_GetWindowBrightness(window);
   512       SDLTest_AssertPass("Call to SDL_GetWindowBrightness");
   513       SDLTest_AssertCheck(result >= 0.0 && result <= 1.0, "Validate range of result value; expected: [0.0, 1.0], got: %f", result);
   514   }
   515 
   516   /* Clean up */    
   517   _destroyVideoSuiteTestWindow(window);
   518   
   519   return TEST_COMPLETED;
   520 }
   521 
   522 /* ================= Test References ================== */
   523 
   524 /* Video test cases */
   525 static const SDLTest_TestCaseReference videoTest1 =
   526 		{ (SDLTest_TestCaseFp)video_enableDisableScreensaver, "video_enableDisableScreensaver",  "Enable and disable screenaver while checking state", TEST_ENABLED };
   527 
   528 static const SDLTest_TestCaseReference videoTest2 =
   529 		{ (SDLTest_TestCaseFp)video_createWindowVariousPositions, "video_createWindowVariousPositions",  "Create windows at various locations", TEST_ENABLED };
   530 
   531 static const SDLTest_TestCaseReference videoTest3 =
   532 		{ (SDLTest_TestCaseFp)video_createWindowVariousSizes, "video_createWindowVariousSizes",  "Create windows with various sizes", TEST_ENABLED };
   533 
   534 static const SDLTest_TestCaseReference videoTest4 =
   535 		{ (SDLTest_TestCaseFp)video_createWindowVariousFlags, "video_createWindowVariousFlags",  "Create windows using various flags", TEST_ENABLED };
   536 
   537 static const SDLTest_TestCaseReference videoTest5 =
   538 		{ (SDLTest_TestCaseFp)video_getWindowFlags, "video_getWindowFlags",  "Get window flags set during SDL_CreateWindow", TEST_ENABLED };
   539 
   540 static const SDLTest_TestCaseReference videoTest6 =
   541 		{ (SDLTest_TestCaseFp)video_getNumDisplayModes, "video_getNumDisplayModes",  "Use SDL_GetNumDisplayModes function to get number of display modes", TEST_ENABLED };
   542 
   543 static const SDLTest_TestCaseReference videoTest7 =
   544 		{ (SDLTest_TestCaseFp)video_getNumDisplayModesNegative, "video_getNumDisplayModesNegative",  "Negative tests for SDL_GetNumDisplayModes", TEST_ENABLED };
   545 
   546 static const SDLTest_TestCaseReference videoTest8 =
   547 		{ (SDLTest_TestCaseFp)video_getClosestDisplayModeCurrentResolution, "video_getClosestDisplayModeCurrentResolution",  "Use function to get closes match to requested display mode for current resolution", TEST_ENABLED };
   548 
   549 static const SDLTest_TestCaseReference videoTest9 =
   550 		{ (SDLTest_TestCaseFp)video_getClosestDisplayModeRandomResolution, "video_getClosestDisplayModeRandomResolution",  "Use function to get closes match to requested display mode for random resolution", TEST_ENABLED };
   551 
   552 static const SDLTest_TestCaseReference videoTest10 =
   553 		{ (SDLTest_TestCaseFp)video_getWindowBrightness, "video_getWindowBrightness",  "Get window brightness", TEST_ENABLED };
   554 
   555 /* Sequence of Video test cases */
   556 static const SDLTest_TestCaseReference *videoTests[] =  {
   557 	&videoTest1, &videoTest2, &videoTest3, &videoTest4, &videoTest5, &videoTest6, 
   558 	&videoTest7, &videoTest8, &videoTest9, &videoTest10, NULL
   559 };
   560 
   561 /* Video test suite (global) */
   562 SDLTest_TestSuiteReference videoTestSuite = {
   563 	"Video",
   564 	NULL,
   565 	videoTests,
   566 	NULL
   567 };