test/testautomation_mouse.c
author Alex Baines <alex@abaines.me.uk>
Tue, 19 Aug 2014 23:31:50 +0100
changeset 9097 56d712662a82
parent 7191 75360622e65f
child 11275 6dd70238edb7
permissions -rw-r--r--
Add a SDL_IM_INTERNAL_EDITING event to make IMs like iBus render editing text in its own UI instead of sending TEXTEDITING events.
This is useful for applications that handle TEXTINPUT events but not TEXTEDITING events.
aschiffler@6801
     1
/**
aschiffler@6801
     2
 * Mouse test suite
aschiffler@6801
     3
 */
aschiffler@6801
     4
aschiffler@6801
     5
#include <stdio.h>
aschiffler@6801
     6
#include <limits.h>
aschiffler@6801
     7
aschiffler@6801
     8
#include "SDL.h"
aschiffler@6801
     9
#include "SDL_test.h"
aschiffler@6801
    10
aschiffler@6801
    11
/* ================= Test Case Implementation ================== */
aschiffler@6801
    12
aschiffler@6801
    13
/* Test case functions */
aschiffler@6801
    14
aschiffler@6801
    15
/* Helper to evaluate state returned from SDL_GetMouseState */
philipp@7157
    16
int _mouseStateCheck(Uint32 state)
aschiffler@6801
    17
{
slouken@7191
    18
  return (state == 0) ||
slouken@7191
    19
         (state == SDL_BUTTON(SDL_BUTTON_LEFT)) ||
slouken@7191
    20
         (state == SDL_BUTTON(SDL_BUTTON_MIDDLE)) ||
slouken@7191
    21
         (state == SDL_BUTTON(SDL_BUTTON_RIGHT)) ||
slouken@7191
    22
         (state == SDL_BUTTON(SDL_BUTTON_X1)) ||
aschiffler@6822
    23
         (state == SDL_BUTTON(SDL_BUTTON_X2));
aschiffler@6801
    24
}
aschiffler@6801
    25
aschiffler@6801
    26
/**
aschiffler@6801
    27
 * @brief Check call to SDL_GetMouseState
slouken@7191
    28
 *
aschiffler@6801
    29
 */
aschiffler@6801
    30
int
aschiffler@6801
    31
mouse_getMouseState(void *arg)
aschiffler@6801
    32
{
aschiffler@6801
    33
   int x;
aschiffler@6801
    34
   int y;
philipp@7157
    35
   Uint32 state;
aschiffler@6801
    36
aschiffler@6801
    37
   /* Pump some events to update mouse state */
aschiffler@6801
    38
   SDL_PumpEvents();
aschiffler@6801
    39
   SDLTest_AssertPass("Call to SDL_PumpEvents()");
aschiffler@6801
    40
slouken@7191
    41
   /* Case where x, y pointer is NULL */
aschiffler@6801
    42
   state = SDL_GetMouseState(NULL, NULL);
aschiffler@6801
    43
   SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, NULL)");
aschiffler@6801
    44
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
    45
aschiffler@6801
    46
   /* Case where x pointer is not NULL */
aschiffler@6801
    47
   x = INT_MIN;
aschiffler@6801
    48
   state = SDL_GetMouseState(&x, NULL);
aschiffler@6801
    49
   SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)");
aschiffler@6801
    50
   SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
aschiffler@6801
    51
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
    52
aschiffler@6801
    53
   /* Case where y pointer is not NULL */
aschiffler@6801
    54
   y = INT_MIN;
aschiffler@6801
    55
   state = SDL_GetMouseState(NULL, &y);
aschiffler@6801
    56
   SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)");
aschiffler@6801
    57
   SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
aschiffler@6801
    58
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
    59
aschiffler@6801
    60
   /* Case where x and y pointer is not NULL */
aschiffler@6801
    61
   x = INT_MIN;
aschiffler@6801
    62
   y = INT_MIN;
aschiffler@6801
    63
   state = SDL_GetMouseState(&x, &y);
aschiffler@6801
    64
   SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)");
aschiffler@6801
    65
   SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
aschiffler@6801
    66
   SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
aschiffler@6801
    67
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
slouken@7191
    68
aschiffler@6801
    69
   return TEST_COMPLETED;
aschiffler@6801
    70
}
aschiffler@6801
    71
aschiffler@6801
    72
/**
aschiffler@6801
    73
 * @brief Check call to SDL_GetRelativeMouseState
slouken@7191
    74
 *
aschiffler@6801
    75
 */
aschiffler@6801
    76
int
aschiffler@6801
    77
mouse_getRelativeMouseState(void *arg)
aschiffler@6801
    78
{
aschiffler@6801
    79
   int x;
aschiffler@6801
    80
   int y;
philipp@7157
    81
   Uint32 state;
aschiffler@6801
    82
aschiffler@6801
    83
   /* Pump some events to update mouse state */
aschiffler@6801
    84
   SDL_PumpEvents();
aschiffler@6801
    85
   SDLTest_AssertPass("Call to SDL_PumpEvents()");
aschiffler@6801
    86
slouken@7191
    87
   /* Case where x, y pointer is NULL */
aschiffler@6801
    88
   state = SDL_GetRelativeMouseState(NULL, NULL);
aschiffler@6801
    89
   SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, NULL)");
aschiffler@6801
    90
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
    91
aschiffler@6801
    92
   /* Case where x pointer is not NULL */
aschiffler@6801
    93
   x = INT_MIN;
aschiffler@6801
    94
   state = SDL_GetRelativeMouseState(&x, NULL);
aschiffler@6801
    95
   SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)");
aschiffler@6801
    96
   SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
aschiffler@6801
    97
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
    98
aschiffler@6801
    99
   /* Case where y pointer is not NULL */
aschiffler@6801
   100
   y = INT_MIN;
aschiffler@6801
   101
   state = SDL_GetRelativeMouseState(NULL, &y);
aschiffler@6801
   102
   SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)");
aschiffler@6801
   103
   SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
aschiffler@6801
   104
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
aschiffler@6801
   105
aschiffler@6801
   106
   /* Case where x and y pointer is not NULL */
aschiffler@6801
   107
   x = INT_MIN;
aschiffler@6801
   108
   y = INT_MIN;
aschiffler@6801
   109
   state = SDL_GetRelativeMouseState(&x, &y);
aschiffler@6801
   110
   SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)");
aschiffler@6801
   111
   SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
aschiffler@6801
   112
   SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
aschiffler@6801
   113
   SDLTest_AssertCheck(_mouseStateCheck(state), "Validate state returned from function, got: %i", state);
slouken@7191
   114
aschiffler@6801
   115
   return TEST_COMPLETED;
aschiffler@6801
   116
}
aschiffler@6801
   117
aschiffler@6821
   118
aschiffler@6821
   119
/* XPM definition of mouse Cursor */
aschiffler@6821
   120
static const char *_mouseArrowData[] = {
aschiffler@6821
   121
  /* pixels */
aschiffler@6821
   122
  "X                               ",
aschiffler@6821
   123
  "XX                              ",
aschiffler@6821
   124
  "X.X                             ",
aschiffler@6821
   125
  "X..X                            ",
aschiffler@6821
   126
  "X...X                           ",
aschiffler@6821
   127
  "X....X                          ",
aschiffler@6821
   128
  "X.....X                         ",
aschiffler@6821
   129
  "X......X                        ",
aschiffler@6821
   130
  "X.......X                       ",
aschiffler@6821
   131
  "X........X                      ",
aschiffler@6821
   132
  "X.....XXXXX                     ",
aschiffler@6821
   133
  "X..X..X                         ",
aschiffler@6821
   134
  "X.X X..X                        ",
aschiffler@6821
   135
  "XX  X..X                        ",
aschiffler@6821
   136
  "X    X..X                       ",
aschiffler@6821
   137
  "     X..X                       ",
aschiffler@6821
   138
  "      X..X                      ",
aschiffler@6821
   139
  "      X..X                      ",
aschiffler@6821
   140
  "       XX                       ",
aschiffler@6821
   141
  "                                ",
aschiffler@6821
   142
  "                                ",
aschiffler@6821
   143
  "                                ",
aschiffler@6821
   144
  "                                ",
aschiffler@6821
   145
  "                                ",
aschiffler@6821
   146
  "                                ",
aschiffler@6821
   147
  "                                ",
aschiffler@6821
   148
  "                                ",
aschiffler@6821
   149
  "                                ",
aschiffler@6821
   150
  "                                ",
aschiffler@6821
   151
  "                                ",
aschiffler@6821
   152
  "                                ",
aschiffler@6821
   153
  "                                "
aschiffler@6821
   154
};
aschiffler@6821
   155
aschiffler@6821
   156
/* Helper that creates a new mouse cursor from an XPM */
aschiffler@6821
   157
static SDL_Cursor *_initArrowCursor(const char *image[])
aschiffler@6821
   158
{
aschiffler@6821
   159
  SDL_Cursor *cursor;
aschiffler@6821
   160
  int i, row, col;
aschiffler@6821
   161
  Uint8 data[4*32];
aschiffler@6821
   162
  Uint8 mask[4*32];
aschiffler@6821
   163
aschiffler@6821
   164
  i = -1;
aschiffler@6821
   165
  for ( row=0; row<32; ++row ) {
aschiffler@6821
   166
    for ( col=0; col<32; ++col ) {
aschiffler@6821
   167
      if ( col % 8 ) {
aschiffler@6821
   168
        data[i] <<= 1;
aschiffler@6821
   169
        mask[i] <<= 1;
aschiffler@6821
   170
      } else {
aschiffler@6821
   171
        ++i;
aschiffler@6821
   172
        data[i] = mask[i] = 0;
aschiffler@6821
   173
      }
aschiffler@6821
   174
      switch (image[row][col]) {
aschiffler@6821
   175
        case 'X':
aschiffler@6821
   176
          data[i] |= 0x01;
aschiffler@6821
   177
          mask[i] |= 0x01;
aschiffler@6821
   178
          break;
aschiffler@6821
   179
        case '.':
aschiffler@6821
   180
          mask[i] |= 0x01;
aschiffler@6821
   181
          break;
aschiffler@6821
   182
        case ' ':
aschiffler@6821
   183
          break;
aschiffler@6821
   184
      }
aschiffler@6821
   185
    }
aschiffler@6821
   186
  }
aschiffler@6821
   187
aschiffler@6821
   188
  cursor = SDL_CreateCursor(data, mask, 32, 32, 0, 0);
aschiffler@6821
   189
  return cursor;
aschiffler@6821
   190
}
aschiffler@6801
   191
aschiffler@6801
   192
/**
aschiffler@6801
   193
 * @brief Check call to SDL_CreateCursor and SDL_FreeCursor
slouken@7191
   194
 *
aschiffler@6801
   195
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_CreateCursor
aschiffler@6801
   196
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_FreeCursor
aschiffler@6801
   197
 */
aschiffler@6801
   198
int
aschiffler@6801
   199
mouse_createFreeCursor(void *arg)
aschiffler@6801
   200
{
slouken@7191
   201
    SDL_Cursor *cursor;
aschiffler@6801
   202
slouken@7191
   203
    /* Create a cursor */
slouken@7191
   204
    cursor = _initArrowCursor(_mouseArrowData);
aschiffler@6821
   205
        SDLTest_AssertPass("Call to SDL_CreateCursor()");
slouken@7191
   206
        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
slouken@7191
   207
    if (cursor == NULL) {
slouken@7191
   208
        return TEST_ABORTED;
slouken@7191
   209
    }
aschiffler@6801
   210
slouken@7191
   211
    /* Free cursor again */
slouken@7191
   212
    SDL_FreeCursor(cursor);
slouken@7191
   213
    SDLTest_AssertPass("Call to SDL_FreeCursor()");
aschiffler@6801
   214
slouken@7191
   215
    return TEST_COMPLETED;
aschiffler@6801
   216
}
aschiffler@6801
   217
aschiffler@6822
   218
/**
aschiffler@6822
   219
 * @brief Check call to SDL_CreateColorCursor and SDL_FreeCursor
slouken@7191
   220
 *
aschiffler@6822
   221
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_CreateColorCursor
aschiffler@6822
   222
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_FreeCursor
aschiffler@6822
   223
 */
aschiffler@6822
   224
int
aschiffler@6822
   225
mouse_createFreeColorCursor(void *arg)
aschiffler@6822
   226
{
slouken@7191
   227
    SDL_Surface *face;
slouken@7191
   228
    SDL_Cursor *cursor;
aschiffler@6822
   229
slouken@7191
   230
    /* Get sample surface */
slouken@7191
   231
    face = SDLTest_ImageFace();
slouken@7191
   232
    SDLTest_AssertCheck(face != NULL, "Validate sample input image is not NULL");
slouken@7191
   233
    if (face == NULL) return TEST_ABORTED;
slouken@7191
   234
slouken@7191
   235
    /* Create a color cursor from surface */
slouken@7191
   236
    cursor = SDL_CreateColorCursor(face, 0, 0);
aschiffler@6822
   237
        SDLTest_AssertPass("Call to SDL_CreateColorCursor()");
slouken@7191
   238
        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateColorCursor() is not NULL");
slouken@7191
   239
    if (cursor == NULL) {
slouken@7191
   240
        SDL_FreeSurface(face);
slouken@7191
   241
        return TEST_ABORTED;
slouken@7191
   242
    }
aschiffler@6822
   243
slouken@7191
   244
    /* Free cursor again */
slouken@7191
   245
    SDL_FreeCursor(cursor);
slouken@7191
   246
    SDLTest_AssertPass("Call to SDL_FreeCursor()");
aschiffler@6822
   247
slouken@7191
   248
    /* Clean up */
slouken@7191
   249
    SDL_FreeSurface(face);
slouken@7191
   250
slouken@7191
   251
    return TEST_COMPLETED;
aschiffler@6822
   252
}
aschiffler@6822
   253
aschiffler@6801
   254
/* Helper that changes cursor visibility */
aschiffler@6801
   255
void _changeCursorVisibility(int state)
aschiffler@6801
   256
{
slouken@7191
   257
    int oldState;
slouken@7191
   258
    int newState;
slouken@7191
   259
    int result;
aschiffler@6801
   260
aschiffler@6821
   261
        oldState = SDL_ShowCursor(SDL_QUERY);
slouken@7191
   262
    SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
aschiffler@6801
   263
aschiffler@6821
   264
        result = SDL_ShowCursor(state);
slouken@7191
   265
    SDLTest_AssertPass("Call to SDL_ShowCursor(%s)", (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE");
slouken@7191
   266
    SDLTest_AssertCheck(result == oldState, "Validate result from SDL_ShowCursor(%s), expected: %i, got: %i",
slouken@7191
   267
        (state == SDL_ENABLE) ? "SDL_ENABLE" : "SDL_DISABLE", oldState, result);
slouken@7191
   268
slouken@7191
   269
    newState = SDL_ShowCursor(SDL_QUERY);
slouken@7191
   270
    SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
slouken@7191
   271
    SDLTest_AssertCheck(state == newState, "Validate new state, expected: %i, got: %i",
slouken@7191
   272
        state, newState);
aschiffler@6801
   273
}
aschiffler@6801
   274
aschiffler@6801
   275
/**
aschiffler@6801
   276
 * @brief Check call to SDL_ShowCursor
slouken@7191
   277
 *
aschiffler@6801
   278
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_ShowCursor
aschiffler@6801
   279
 */
aschiffler@6801
   280
int
aschiffler@6801
   281
mouse_showCursor(void *arg)
aschiffler@6801
   282
{
slouken@7191
   283
    int currentState;
aschiffler@6801
   284
slouken@7191
   285
    /* Get current state */
slouken@7191
   286
    currentState = SDL_ShowCursor(SDL_QUERY);
slouken@7191
   287
    SDLTest_AssertPass("Call to SDL_ShowCursor(SDL_QUERY)");
slouken@7191
   288
    SDLTest_AssertCheck(currentState == SDL_DISABLE || currentState == SDL_ENABLE,
slouken@7191
   289
        "Validate result is %i or %i, got: %i", SDL_DISABLE, SDL_ENABLE, currentState);
slouken@7191
   290
    if (currentState == SDL_DISABLE) {
slouken@7191
   291
        /* Show the cursor, then hide it again */
slouken@7191
   292
        _changeCursorVisibility(SDL_ENABLE);
slouken@7191
   293
        _changeCursorVisibility(SDL_DISABLE);
slouken@7191
   294
    } else if (currentState == SDL_ENABLE) {
slouken@7191
   295
        /* Hide the cursor, then show it again */
slouken@7191
   296
        _changeCursorVisibility(SDL_DISABLE);
slouken@7191
   297
        _changeCursorVisibility(SDL_ENABLE);
slouken@7191
   298
    } else {
slouken@7191
   299
        return TEST_ABORTED;
slouken@7191
   300
    }
aschiffler@6801
   301
slouken@7191
   302
    return TEST_COMPLETED;
aschiffler@6801
   303
}
aschiffler@6801
   304
aschiffler@6801
   305
/**
aschiffler@6801
   306
 * @brief Check call to SDL_SetCursor
slouken@7191
   307
 *
aschiffler@6801
   308
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetCursor
aschiffler@6801
   309
 */
aschiffler@6801
   310
int
aschiffler@6801
   311
mouse_setCursor(void *arg)
aschiffler@6801
   312
{
slouken@7191
   313
    SDL_Cursor *cursor;
aschiffler@6801
   314
slouken@7191
   315
    /* Create a cursor */
slouken@7191
   316
    cursor = _initArrowCursor(_mouseArrowData);
aschiffler@6821
   317
        SDLTest_AssertPass("Call to SDL_CreateCursor()");
slouken@7191
   318
        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_CreateCursor() is not NULL");
slouken@7191
   319
    if (cursor == NULL) {
slouken@7191
   320
        return TEST_ABORTED;
slouken@7191
   321
    }
aschiffler@6801
   322
slouken@7191
   323
    /* Set the arrow cursor */
slouken@7191
   324
    SDL_SetCursor(cursor);
slouken@7191
   325
    SDLTest_AssertPass("Call to SDL_SetCursor(cursor)");
aschiffler@6801
   326
slouken@7191
   327
    /* Force redraw */
slouken@7191
   328
    SDL_SetCursor(NULL);
slouken@7191
   329
    SDLTest_AssertPass("Call to SDL_SetCursor(NULL)");
aschiffler@6801
   330
slouken@7191
   331
    /* Free cursor again */
slouken@7191
   332
    SDL_FreeCursor(cursor);
slouken@7191
   333
    SDLTest_AssertPass("Call to SDL_FreeCursor()");
aschiffler@6801
   334
slouken@7191
   335
    return TEST_COMPLETED;
aschiffler@6801
   336
}
aschiffler@6801
   337
aschiffler@6822
   338
/**
aschiffler@6822
   339
 * @brief Check call to SDL_GetCursor
slouken@7191
   340
 *
aschiffler@6822
   341
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetCursor
aschiffler@6822
   342
 */
aschiffler@6822
   343
int
aschiffler@6822
   344
mouse_getCursor(void *arg)
aschiffler@6822
   345
{
slouken@7191
   346
    SDL_Cursor *cursor;
aschiffler@6822
   347
slouken@7191
   348
    /* Get current cursor */
slouken@7191
   349
    cursor = SDL_GetCursor();
aschiffler@6822
   350
        SDLTest_AssertPass("Call to SDL_GetCursor()");
slouken@7191
   351
        SDLTest_AssertCheck(cursor != NULL, "Validate result from SDL_GetCursor() is not NULL");
aschiffler@6822
   352
slouken@7191
   353
    return TEST_COMPLETED;
aschiffler@6822
   354
}
aschiffler@6822
   355
aschiffler@6822
   356
/**
aschiffler@6822
   357
 * @brief Check call to SDL_GetRelativeMouseMode and SDL_SetRelativeMouseMode
slouken@7191
   358
 *
aschiffler@6822
   359
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetRelativeMouseMode
aschiffler@6822
   360
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetRelativeMouseMode
aschiffler@6822
   361
 */
aschiffler@6822
   362
int
aschiffler@6822
   363
mouse_getSetRelativeMouseMode(void *arg)
aschiffler@6822
   364
{
slouken@7191
   365
    int result;
aschiffler@6822
   366
        int i;
slouken@7191
   367
    SDL_bool initialState;
slouken@7191
   368
    SDL_bool currentState;
aschiffler@6822
   369
slouken@7191
   370
    /* Capture original state so we can revert back to it later */
slouken@7191
   371
    initialState = SDL_GetRelativeMouseMode();
aschiffler@6822
   372
        SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
aschiffler@6822
   373
aschiffler@6822
   374
        /* Repeat twice to check D->D transition */
aschiffler@6822
   375
        for (i=0; i<2; i++) {
slouken@7191
   376
      /* Disable - should always be supported */
aschiffler@6822
   377
          result = SDL_SetRelativeMouseMode(SDL_FALSE);
aschiffler@6822
   378
          SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(FALSE)");
aschiffler@6822
   379
          SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
slouken@7191
   380
      currentState = SDL_GetRelativeMouseMode();
aschiffler@6822
   381
          SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
aschiffler@6822
   382
          SDLTest_AssertCheck(currentState == SDL_FALSE, "Validate current state is FALSE, got: %i", currentState);
aschiffler@6822
   383
        }
slouken@7191
   384
aschiffler@6822
   385
        /* Repeat twice to check D->E->E transition */
aschiffler@6822
   386
        for (i=0; i<2; i++) {
slouken@7191
   387
      /* Enable - may not be supported */
aschiffler@6822
   388
          result = SDL_SetRelativeMouseMode(SDL_TRUE);
aschiffler@6822
   389
          SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(TRUE)");
aschiffler@6822
   390
          if (result != -1) {
aschiffler@6822
   391
            SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
slouken@7191
   392
        currentState = SDL_GetRelativeMouseMode();
aschiffler@6822
   393
            SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
aschiffler@6822
   394
            SDLTest_AssertCheck(currentState == SDL_TRUE, "Validate current state is TRUE, got: %i", currentState);
aschiffler@6822
   395
          }
aschiffler@6822
   396
        }
aschiffler@6822
   397
slouken@7191
   398
    /* Disable to check E->D transition */
aschiffler@6822
   399
        result = SDL_SetRelativeMouseMode(SDL_FALSE);
aschiffler@6822
   400
        SDLTest_AssertPass("Call to SDL_SetRelativeMouseMode(FALSE)");
aschiffler@6822
   401
        SDLTest_AssertCheck(result == 0, "Validate result value from SDL_SetRelativeMouseMode, expected: 0, got: %i", result);
slouken@7191
   402
    currentState = SDL_GetRelativeMouseMode();
aschiffler@6822
   403
        SDLTest_AssertPass("Call to SDL_GetRelativeMouseMode()");
aschiffler@6822
   404
        SDLTest_AssertCheck(currentState == SDL_FALSE, "Validate current state is FALSE, got: %i", currentState);
slouken@7191
   405
philipp@7144
   406
        /* Revert to original state - ignore result */
aschiffler@6822
   407
        result = SDL_SetRelativeMouseMode(initialState);
aschiffler@6822
   408
slouken@7191
   409
    return TEST_COMPLETED;
aschiffler@6822
   410
}
aschiffler@6822
   411
slouken@7191
   412
#define MOUSE_TESTWINDOW_WIDTH  320
aschiffler@6821
   413
#define MOUSE_TESTWINDOW_HEIGHT 200
aschiffler@6821
   414
aschiffler@6821
   415
/**
aschiffler@6921
   416
 * Creates a test window
aschiffler@6821
   417
 */
aschiffler@6921
   418
SDL_Window *_createMouseSuiteTestWindow()
aschiffler@6821
   419
{
aschiffler@6821
   420
  int posX = 100, posY = 100, width = MOUSE_TESTWINDOW_WIDTH, height = MOUSE_TESTWINDOW_HEIGHT;
aschiffler@6821
   421
  SDL_Window *window;
aschiffler@6921
   422
  window = SDL_CreateWindow("mouse_createMouseSuiteTestWindow", posX, posY, width, height, 0);
aschiffler@6821
   423
  SDLTest_AssertPass("SDL_CreateWindow()");
aschiffler@6821
   424
  SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
aschiffler@6821
   425
  return window;
aschiffler@6821
   426
}
aschiffler@6821
   427
aschiffler@6821
   428
/*
slouken@7191
   429
 * Destroy test window
aschiffler@6821
   430
 */
aschiffler@6921
   431
void _destroyMouseSuiteTestWindow(SDL_Window *window)
aschiffler@6821
   432
{
slouken@7191
   433
  if (window != NULL) {
aschiffler@6821
   434
     SDL_DestroyWindow(window);
aschiffler@6821
   435
     window = NULL;
aschiffler@6921
   436
     SDLTest_AssertPass("SDL_DestroyWindow()");
aschiffler@6821
   437
  }
aschiffler@6821
   438
}
aschiffler@6821
   439
aschiffler@6821
   440
/**
aschiffler@6821
   441
 * @brief Check call to SDL_WarpMouseInWindow
slouken@7191
   442
 *
aschiffler@6821
   443
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_WarpMouseInWindow
aschiffler@6821
   444
 */
aschiffler@6821
   445
int
aschiffler@6821
   446
mouse_warpMouseInWindow(void *arg)
aschiffler@6821
   447
{
slouken@7191
   448
    const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
slouken@7191
   449
    int numPositions = 6;
slouken@7191
   450
    int xPositions[] = {-1, 0, 1, w-1, w, w+1 };
slouken@7191
   451
    int yPositions[] = {-1, 0, 1, h-1, h, h+1 };
slouken@7191
   452
    int x, y, i, j;
slouken@7191
   453
    SDL_Window *window;
aschiffler@6821
   454
slouken@7191
   455
    /* Create test window */
slouken@7191
   456
    window = _createMouseSuiteTestWindow();
slouken@7191
   457
    if (window == NULL) return TEST_ABORTED;
slouken@7191
   458
slouken@7191
   459
    /* Mouse to random position inside window */
slouken@7191
   460
    x = SDLTest_RandomIntegerInRange(1, w-1);
slouken@7191
   461
    y = SDLTest_RandomIntegerInRange(1, h-1);
slouken@7191
   462
    SDL_WarpMouseInWindow(window, x, y);
slouken@7191
   463
    SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
aschiffler@6821
   464
aschiffler@6821
   465
        /* Same position again */
slouken@7191
   466
    SDL_WarpMouseInWindow(window, x, y);
slouken@7191
   467
    SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
aschiffler@6821
   468
slouken@7191
   469
    /* Mouse to various boundary positions */
slouken@7191
   470
    for (i=0; i<numPositions; i++) {
slouken@7191
   471
      for (j=0; j<numPositions; j++) {
slouken@7191
   472
        x = xPositions[i];
slouken@7191
   473
        y = yPositions[j];
slouken@7191
   474
        SDL_WarpMouseInWindow(window, x, y);
slouken@7191
   475
        SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
aschiffler@6821
   476
slouken@7191
   477
        /* TODO: add tracking of events and check that each call generates a mouse motion event */
slouken@7191
   478
        SDL_PumpEvents();
slouken@7191
   479
        SDLTest_AssertPass("SDL_PumpEvents()");
slouken@7191
   480
      }
slouken@7191
   481
    }
aschiffler@6821
   482
slouken@7191
   483
slouken@7191
   484
        /* Clean up test window */
slouken@7191
   485
    _destroyMouseSuiteTestWindow(window);
slouken@7191
   486
slouken@7191
   487
    return TEST_COMPLETED;
aschiffler@6821
   488
}
aschiffler@6821
   489
aschiffler@6821
   490
/**
aschiffler@6821
   491
 * @brief Check call to SDL_GetMouseFocus
slouken@7191
   492
 *
aschiffler@6821
   493
 * @sa http://wiki.libsdl.org/moin.cgi/SDL_GetMouseFocus
aschiffler@6821
   494
 */
aschiffler@6821
   495
int
aschiffler@6821
   496
mouse_getMouseFocus(void *arg)
aschiffler@6821
   497
{
slouken@7191
   498
    const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
slouken@7191
   499
    int x, y;
slouken@7191
   500
    SDL_Window *window;
slouken@7191
   501
    SDL_Window *focusWindow;
aschiffler@6821
   502
slouken@7191
   503
    /* Get focus - focus non-deterministic */
slouken@7191
   504
    focusWindow = SDL_GetMouseFocus();
slouken@7191
   505
    SDLTest_AssertPass("SDL_GetMouseFocus()");
aschiffler@6821
   506
slouken@7191
   507
        /* Create test window */
slouken@7191
   508
    window = _createMouseSuiteTestWindow();
slouken@7191
   509
    if (window == NULL) return TEST_ABORTED;
aschiffler@6821
   510
slouken@7191
   511
    /* Mouse to random position inside window */
slouken@7191
   512
    x = SDLTest_RandomIntegerInRange(1, w-1);
slouken@7191
   513
    y = SDLTest_RandomIntegerInRange(1, h-1);
slouken@7191
   514
    SDL_WarpMouseInWindow(window, x, y);
slouken@7191
   515
    SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
aschiffler@6821
   516
slouken@7191
   517
    /* Pump events to update focus state */
slouken@7191
   518
    SDL_PumpEvents();
slouken@7191
   519
    SDLTest_AssertPass("SDL_PumpEvents()");
aschiffler@6821
   520
slouken@7191
   521
        /* Get focus with explicit window setup - focus deterministic */
slouken@7191
   522
    focusWindow = SDL_GetMouseFocus();
slouken@7191
   523
    SDLTest_AssertPass("SDL_GetMouseFocus()");
slouken@7191
   524
    SDLTest_AssertCheck (focusWindow != NULL, "Check returned window value is not NULL");
slouken@7191
   525
    SDLTest_AssertCheck (focusWindow == window, "Check returned window value is test window");
aschiffler@6821
   526
slouken@7191
   527
    /* Mouse to random position outside window */
slouken@7191
   528
    x = SDLTest_RandomIntegerInRange(-9, -1);
slouken@7191
   529
    y = SDLTest_RandomIntegerInRange(-9, -1);
slouken@7191
   530
    SDL_WarpMouseInWindow(window, x, y);
slouken@7191
   531
    SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
aschiffler@6821
   532
slouken@7191
   533
        /* Clean up test window */
slouken@7191
   534
    _destroyMouseSuiteTestWindow(window);
aschiffler@6821
   535
slouken@7191
   536
    /* Pump events to update focus state */
slouken@7191
   537
    SDL_PumpEvents();
slouken@7191
   538
    SDLTest_AssertPass("SDL_PumpEvents()");
aschiffler@6821
   539
slouken@7191
   540
        /* Get focus for non-existing window */
slouken@7191
   541
    focusWindow = SDL_GetMouseFocus();
slouken@7191
   542
    SDLTest_AssertPass("SDL_GetMouseFocus()");
slouken@7191
   543
    SDLTest_AssertCheck (focusWindow == NULL, "Check returned window value is NULL");
aschiffler@6821
   544
slouken@7191
   545
slouken@7191
   546
    return TEST_COMPLETED;
aschiffler@6821
   547
}
aschiffler@6821
   548
aschiffler@6801
   549
/* ================= Test References ================== */
aschiffler@6801
   550
aschiffler@6801
   551
/* Mouse test cases */
aschiffler@6801
   552
static const SDLTest_TestCaseReference mouseTest1 =
slouken@7191
   553
        { (SDLTest_TestCaseFp)mouse_getMouseState, "mouse_getMouseState", "Check call to SDL_GetMouseState", TEST_ENABLED };
aschiffler@6801
   554
aschiffler@6801
   555
static const SDLTest_TestCaseReference mouseTest2 =
slouken@7191
   556
        { (SDLTest_TestCaseFp)mouse_getRelativeMouseState, "mouse_getRelativeMouseState", "Check call to SDL_GetRelativeMouseState", TEST_ENABLED };
aschiffler@6801
   557
aschiffler@6801
   558
static const SDLTest_TestCaseReference mouseTest3 =
slouken@7191
   559
        { (SDLTest_TestCaseFp)mouse_createFreeCursor, "mouse_createFreeCursor", "Check call to SDL_CreateCursor and SDL_FreeCursor", TEST_ENABLED };
aschiffler@6801
   560
aschiffler@6801
   561
static const SDLTest_TestCaseReference mouseTest4 =
slouken@7191
   562
        { (SDLTest_TestCaseFp)mouse_showCursor, "mouse_showCursor", "Check call to SDL_ShowCursor", TEST_ENABLED };
aschiffler@6801
   563
aschiffler@6801
   564
static const SDLTest_TestCaseReference mouseTest5 =
slouken@7191
   565
        { (SDLTest_TestCaseFp)mouse_setCursor, "mouse_setCursor", "Check call to SDL_SetCursor", TEST_ENABLED };
aschiffler@6801
   566
aschiffler@6821
   567
static const SDLTest_TestCaseReference mouseTest6 =
slouken@7191
   568
        { (SDLTest_TestCaseFp)mouse_getCursor, "mouse_getCursor", "Check call to SDL_GetCursor", TEST_ENABLED };
aschiffler@6822
   569
aschiffler@6822
   570
static const SDLTest_TestCaseReference mouseTest7 =
slouken@7191
   571
        { (SDLTest_TestCaseFp)mouse_warpMouseInWindow, "mouse_warpMouseInWindow", "Check call to SDL_WarpMouseInWindow", TEST_ENABLED };
aschiffler@6821
   572
aschiffler@6822
   573
static const SDLTest_TestCaseReference mouseTest8 =
slouken@7191
   574
        { (SDLTest_TestCaseFp)mouse_getMouseFocus, "mouse_getMouseFocus", "Check call to SDL_getMouseFocus", TEST_ENABLED };
aschiffler@6821
   575
aschiffler@6822
   576
static const SDLTest_TestCaseReference mouseTest9 =
slouken@7191
   577
        { (SDLTest_TestCaseFp)mouse_createFreeColorCursor, "mouse_createFreeColorCursor", "Check call to SDL_CreateColorCursor and SDL_FreeCursor", TEST_ENABLED };
aschiffler@6822
   578
aschiffler@6822
   579
static const SDLTest_TestCaseReference mouseTest10 =
slouken@7191
   580
        { (SDLTest_TestCaseFp)mouse_getSetRelativeMouseMode, "mouse_getSetRelativeMouseMode", "Check call to SDL_GetRelativeMouseMode and SDL_SetRelativeMouseMode", TEST_ENABLED };
aschiffler@6822
   581
aschiffler@6801
   582
/* Sequence of Mouse test cases */
aschiffler@6801
   583
static const SDLTest_TestCaseReference *mouseTests[] =  {
slouken@7191
   584
    &mouseTest1, &mouseTest2, &mouseTest3, &mouseTest4, &mouseTest5, &mouseTest6,
slouken@7191
   585
    &mouseTest7, &mouseTest8, &mouseTest9, &mouseTest10, NULL
aschiffler@6801
   586
};
aschiffler@6801
   587
aschiffler@6801
   588
/* Mouse test suite (global) */
aschiffler@6801
   589
SDLTest_TestSuiteReference mouseTestSuite = {
slouken@7191
   590
    "Mouse",
slouken@7191
   591
    NULL,
slouken@7191
   592
    mouseTests,
slouken@7191
   593
    NULL
aschiffler@6801
   594
};