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