screenshot.c 3.8 KB

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