screenshot.c (3863B)
1/* See COPYING.txt for the full license governing this code. */ 2/** 3 * \file screenshot.c 4 * 5 * Source file for the screenshot API. 6 */ 7 8#include "SDL_visualtest_mischelper.h" 9#include <SDL_test.h> 10 11int 12SDLVisualTest_VerifyScreenshots(char* args, char* test_dir, char* verify_dir) 13{ 14 int i, verify_len, return_code, test_len; 15 char hash[33]; 16 char* verify_path; /* path to the bmp file used for verification */ 17 char* test_path; /* path to the bmp file to be verified */ 18 SDL_RWops* rw; 19 SDL_Surface* verifybmp; 20 21 return_code = 1; 22 23 if(!args) 24 { 25 SDLTest_LogError("args argument cannot be NULL"); 26 return_code = -1; 27 goto verifyscreenshots_cleanup_generic; 28 } 29 if(!test_dir) 30 { 31 SDLTest_LogError("test_dir argument cannot be NULL"); 32 return_code = -1; 33 goto verifyscreenshots_cleanup_generic; 34 } 35 if(!verify_dir) 36 { 37 SDLTest_LogError("verify_dir argument cannot be NULL"); 38 return_code = -1; 39 goto verifyscreenshots_cleanup_generic; 40 } 41 42 /* generate the MD5 hash */ 43 SDLVisualTest_HashString(args, hash); 44 45 /* find the verification image */ 46 /* path_len + hash_len + some number of extra characters */ 47 verify_len = SDL_strlen(verify_dir) + 32 + 10; 48 verify_path = (char*)SDL_malloc(verify_len * sizeof(char)); 49 if(!verify_path) 50 { 51 SDLTest_LogError("malloc() failed"); 52 return_code = -1; 53 goto verifyscreenshots_cleanup_generic; 54 } 55 SDL_snprintf(verify_path, verify_len - 1, 56 "%s/%s.bmp", verify_dir, hash); 57 rw = SDL_RWFromFile(verify_path, "rb"); 58 if(!rw) 59 { 60 SDLTest_Log("Verification image does not exist." 61 " Please manually verify that the SUT is working correctly."); 62 return_code = 2; 63 goto verifyscreenshots_cleanup_verifypath; 64 } 65 66 /* load the verification image */ 67 verifybmp = SDL_LoadBMP_RW(rw, 1); 68 if(!verifybmp) 69 { 70 SDLTest_LogError("SDL_LoadBMP_RW() failed"); 71 return_code = -1; 72 goto verifyscreenshots_cleanup_verifypath; 73 } 74 75 /* load the test images and compare with the verification image */ 76 /* path_len + hash_len + some number of extra characters */ 77 test_len = SDL_strlen(test_dir) + 32 + 10; 78 test_path = (char*)SDL_malloc(test_len * sizeof(char)); 79 if(!test_path) 80 { 81 SDLTest_LogError("malloc() failed"); 82 return_code = -1; 83 goto verifyscreenshots_cleanup_verifybmp; 84 } 85 86 for(i = 1; ; i++) 87 { 88 SDL_RWops* testrw; 89 SDL_Surface* testbmp; 90 91 if(i == 1) 92 SDL_snprintf(test_path, test_len - 1, "%s/%s.bmp", test_dir, hash); 93 else 94 SDL_snprintf(test_path, test_len - 1, "%s/%s_%d.bmp", test_dir, hash, i); 95 testrw = SDL_RWFromFile(test_path, "rb"); 96 97 /* we keep going until we've iterated through the screenshots each 98 SUT window */ 99 if(!testrw) 100 break; 101 102 /* load the test screenshot */ 103 testbmp = SDL_LoadBMP_RW(testrw, 1); 104 if(!testbmp) 105 { 106 SDLTest_LogError("SDL_LoadBMP_RW() failed"); 107 return_code = -1; 108 goto verifyscreenshots_cleanup_verifybmp; 109 } 110 111 /* compare with the verification image */ 112 if(SDLTest_CompareSurfaces(testbmp, verifybmp, 0) != 0) 113 { 114 return_code = 0; 115 SDL_FreeSurface(testbmp); 116 goto verifyscreenshots_cleanup_verifybmp; 117 } 118 119 SDL_FreeSurface(testbmp); 120 } 121 122 if(i == 1) 123 { 124 SDLTest_LogError("No verification images found"); 125 return_code = -1; 126 } 127 128verifyscreenshots_cleanup_verifybmp: 129 SDL_FreeSurface(verifybmp); 130 131verifyscreenshots_cleanup_verifypath: 132 SDL_free(verify_path); 133 134verifyscreenshots_cleanup_generic: 135 return return_code; 136}