5
#include <SDL3/SDL_test.h>
6
#include "testautomation_suites.h"
10
static int clipboard_update_count;
12
static SDL_bool ClipboardEventWatch(void *userdata, SDL_Event *event)
14
if (event->type == SDL_EVENT_CLIPBOARD_UPDATE) {
15
++clipboard_update_count;
23
TEST_MIME_TYPE_CUSTOM_TEXT,
27
static const char *test_mime_types[] = {
28
"text/plain;charset=utf-8",
32
SDL_COMPILE_TIME_ASSERT(test_mime_types, SDL_arraysize(test_mime_types) == NUM_TEST_MIME_TYPES);
40
static int clipboard_callback_count;
42
static const void *ClipboardDataCallback(void *userdata, const char *mime_type, size_t *length)
44
TestClipboardData *test_data = (TestClipboardData *)userdata;
46
++clipboard_callback_count;
48
if (SDL_strcmp(mime_type, test_mime_types[TEST_MIME_TYPE_TEXT]) == 0) {
50
static const char *test_text = "XXX TEST XXX";
54
if (SDL_strcmp(mime_type, test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]) == 0) {
56
static const char *custom_text = "XXX CUSTOM XXX";
58
return custom_text + 4;
60
if (SDL_strcmp(mime_type, test_mime_types[TEST_MIME_TYPE_DATA]) == 0) {
61
*length = test_data->data_size;
62
return test_data->data;
67
static int clipboard_cleanup_count;
69
static void ClipboardCleanupCallback(void *userdata)
71
++clipboard_cleanup_count;
82
static int clipboard_testClipboardDataFunctions(void *arg)
86
int last_clipboard_update_count;
87
int last_clipboard_callback_count;
88
int last_clipboard_cleanup_count;
92
const char *expected_text;
94
TestClipboardData test_data1 = {
98
TestClipboardData test_data2 = {
99
&last_clipboard_callback_count,
100
sizeof(last_clipboard_callback_count)
103
SDL_AddEventWatch(ClipboardEventWatch, NULL);
106
result = SDL_ClearClipboardData();
109
"Validate SDL_ClearClipboardData result, expected 0, got %i",
113
last_clipboard_update_count = clipboard_update_count;
114
result = SDL_ClearClipboardData();
117
"Validate SDL_ClearClipboardData result, expected 0, got %i",
120
clipboard_update_count == last_clipboard_update_count,
121
"Verify clipboard update unchanged, got %d",
122
clipboard_update_count - last_clipboard_update_count);
125
last_clipboard_update_count = clipboard_update_count;
126
result = SDL_SetClipboardData(NULL, NULL, NULL, test_mime_types, SDL_arraysize(test_mime_types));
129
"Validate SDL_SetClipboardData(invalid) result, expected -1, got %i",
132
clipboard_update_count == last_clipboard_update_count,
133
"Verify clipboard update count unchanged, got %d",
134
clipboard_update_count - last_clipboard_update_count);
136
last_clipboard_update_count = clipboard_update_count;
137
result = SDL_SetClipboardData(ClipboardDataCallback, ClipboardCleanupCallback, NULL, NULL, 0);
140
"Validate SDL_SetClipboardData(invalid) result, expected -1, got %i",
143
clipboard_update_count == last_clipboard_update_count,
144
"Verify clipboard update count unchanged, got %d",
145
clipboard_update_count - last_clipboard_update_count);
148
last_clipboard_update_count = clipboard_update_count;
149
last_clipboard_callback_count = clipboard_callback_count;
150
last_clipboard_cleanup_count = clipboard_cleanup_count;
151
result = SDL_SetClipboardData(ClipboardDataCallback, ClipboardCleanupCallback, &test_data1, test_mime_types, SDL_arraysize(test_mime_types));
154
"Validate SDL_SetClipboardData(test_data1) result, expected 0, got %i",
157
clipboard_update_count == last_clipboard_update_count + 1,
158
"Verify clipboard update count incremented by 1, got %d",
159
clipboard_update_count - last_clipboard_update_count);
161
clipboard_cleanup_count == last_clipboard_cleanup_count,
162
"Verify clipboard cleanup count unchanged, got %d",
163
clipboard_cleanup_count - last_clipboard_cleanup_count);
165
expected_text = "TEST";
166
text = SDL_GetClipboardText();
168
text && SDL_strcmp(text, expected_text) == 0,
169
"Verify clipboard text, expected \"%s\", got \"%s\"",
170
expected_text, text);
173
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
176
"Verify has test text data, expected SDL_TRUE, got SDL_FALSE");
177
text = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT], &size);
180
"Verify has test text data, expected valid result, got NULL");
184
"Verify test text data, expected null termination, got %c",
187
SDL_strcmp(text, expected_text) == 0,
188
"Verify test text data, expected \"%s\", got \"%s\"",
189
expected_text, text);
192
size == SDL_strlen(expected_text),
193
"Verify test text size, expected %d, got %d",
194
(int)SDL_strlen(expected_text), (int)size);
197
expected_text = "CUSTOM";
198
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
201
"Verify has test text data, expected SDL_TRUE, got SDL_FALSE");
202
text = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT], &size);
205
"Verify has test text data, expected valid result, got NULL");
209
"Verify test text data, expected null termination, got %c",
212
SDL_strcmp(text, expected_text) == 0,
213
"Verify test text data, expected \"%s\", got \"%s\"",
214
expected_text, text);
217
size == SDL_strlen(expected_text),
218
"Verify test text size, expected %d, got %d",
219
(int)SDL_strlen(expected_text), (int)size);
222
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_DATA]);
225
"Verify has test text data, expected SDL_TRUE, got SDL_FALSE");
226
data = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_DATA], &size);
228
data && SDL_memcmp(data, test_data1.data, test_data1.data_size) == 0,
231
size == test_data1.data_size,
232
"Verify test data size, expected %d, got %d",
233
(int)test_data1.data_size, (int)size);
236
boolResult = SDL_HasClipboardData("test/invalid");
239
"Verify has test text data, expected SDL_FALSE, got SDL_TRUE");
240
data = SDL_GetClipboardData("test/invalid", &size);
243
"Verify invalid data, expected NULL, got %p",
247
"Verify invalid data size, expected 0, got %d",
253
(clipboard_callback_count == last_clipboard_callback_count + 3) ||
254
(clipboard_callback_count == last_clipboard_callback_count + 4),
255
"Verify clipboard callback count incremented by 3 or 4, got %d",
256
clipboard_callback_count - last_clipboard_callback_count);
260
last_clipboard_update_count = clipboard_update_count;
261
last_clipboard_callback_count = clipboard_callback_count;
262
last_clipboard_cleanup_count = clipboard_cleanup_count;
263
result = SDL_SetClipboardData(ClipboardDataCallback, ClipboardCleanupCallback, &test_data2, test_mime_types, SDL_arraysize(test_mime_types));
266
"Validate SDL_SetClipboardData(test_data2) result, expected 0, got %i",
269
clipboard_update_count == last_clipboard_update_count + 1,
270
"Verify clipboard update count incremented by 1, got %d",
271
clipboard_update_count - last_clipboard_update_count);
273
clipboard_cleanup_count == last_clipboard_cleanup_count + 1,
274
"Verify clipboard cleanup count incremented by 1, got %d",
275
clipboard_cleanup_count - last_clipboard_cleanup_count);
277
expected_text = "TEST";
278
text = SDL_GetClipboardText();
280
text && SDL_strcmp(text, expected_text) == 0,
281
"Verify clipboard text, expected \"%s\", got \"%s\"",
282
expected_text, text);
285
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
288
"Verify has test text data, expected SDL_TRUE, got SDL_FALSE");
289
text = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT], &size);
292
"Verify has test text data, expected valid result, got NULL");
296
"Verify test text data, expected null termination, got %c",
299
SDL_strcmp(text, expected_text) == 0,
300
"Verify test text data, expected \"%s\", got \"%s\"",
301
expected_text, text);
304
size == SDL_strlen(expected_text),
305
"Verify test text size, expected %d, got %d",
306
(int)SDL_strlen(expected_text), (int)size);
309
expected_text = "CUSTOM";
310
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
313
"Verify has test text data, expected SDL_TRUE, got SDL_FALSE");
314
text = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT], &size);
317
"Verify has test text data, expected valid result, got NULL");
321
"Verify test text data, expected null termination, got %c",
324
SDL_strcmp(text, expected_text) == 0,
325
"Verify test text data, expected \"%s\", got \"%s\"",
326
expected_text, text);
329
size == SDL_strlen(expected_text),
330
"Verify test text size, expected %d, got %d",
331
(int)SDL_strlen(expected_text), (int)size);
334
data = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_DATA], &size);
336
data && SDL_memcmp(data, test_data2.data, test_data2.data_size) == 0,
339
size == test_data2.data_size,
340
"Verify test data size, expected %d, got %d",
341
(int)test_data2.data_size, (int)size);
344
data = SDL_GetClipboardData("test/invalid", &size);
347
"Verify invalid data, expected NULL, got %p",
351
"Verify invalid data size, expected 0, got %d",
357
(clipboard_callback_count == last_clipboard_callback_count + 3) ||
358
(clipboard_callback_count == last_clipboard_callback_count + 4),
359
"Verify clipboard callback count incremented by 3 or 4, got %d",
360
clipboard_callback_count - last_clipboard_callback_count);
364
last_clipboard_update_count = clipboard_update_count;
365
last_clipboard_cleanup_count = clipboard_cleanup_count;
366
result = SDL_ClearClipboardData();
369
"Validate SDL_ClearClipboardData result, expected 0, got %i",
372
clipboard_update_count == last_clipboard_update_count + 1,
373
"Verify clipboard update count incremented by 1, got %d",
374
clipboard_update_count - last_clipboard_update_count);
376
clipboard_cleanup_count == last_clipboard_cleanup_count + 1,
377
"Verify clipboard cleanup count incremented by 1, got %d",
378
clipboard_cleanup_count - last_clipboard_cleanup_count);
379
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
382
"Verify has test text data, expected SDL_FALSE, got SDL_TRUE");
383
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_DATA]);
386
"Verify has test text data, expected SDL_FALSE, got SDL_TRUE");
387
boolResult = SDL_HasClipboardData("test/invalid");
390
"Verify has test text data, expected SDL_FALSE, got SDL_TRUE");
392
SDL_DelEventWatch(ClipboardEventWatch, NULL);
394
return TEST_COMPLETED;
403
static int clipboard_testClipboardTextFunctions(void *arg)
405
char *textRef = SDLTest_RandomAsciiString();
406
char *text = SDL_strdup(textRef);
410
int last_clipboard_update_count;
412
SDL_AddEventWatch(ClipboardEventWatch, NULL);
415
last_clipboard_update_count = clipboard_update_count;
416
intResult = SDL_SetClipboardText(NULL);
419
"Verify result from SDL_SetClipboardText(NULL), expected 0, got %i",
421
charResult = SDL_GetClipboardText();
423
charResult && SDL_strcmp(charResult, "") == 0,
424
"Verify SDL_GetClipboardText returned \"\", got %s",
426
SDL_free(charResult);
427
boolResult = SDL_HasClipboardText();
429
boolResult == SDL_FALSE,
430
"Verify SDL_HasClipboardText returned SDL_FALSE, got %s",
431
(boolResult) ? "SDL_TRUE" : "SDL_FALSE");
433
clipboard_update_count == last_clipboard_update_count,
434
"Verify clipboard update unchanged, got %d",
435
clipboard_update_count - last_clipboard_update_count);
439
last_clipboard_update_count = clipboard_update_count;
440
intResult = SDL_SetClipboardText(text);
443
"Verify result from SDL_SetClipboardText(%s), expected 0, got %i", text,
446
SDL_strcmp(textRef, text) == 0,
447
"Verify SDL_SetClipboardText did not modify input string, expected '%s', got '%s'",
449
boolResult = SDL_HasClipboardText();
451
boolResult == SDL_TRUE,
452
"Verify SDL_HasClipboardText returned SDL_TRUE, got %s",
453
(boolResult) ? "SDL_TRUE" : "SDL_FALSE");
454
charResult = SDL_GetClipboardText();
456
charResult && SDL_strcmp(textRef, charResult) == 0,
457
"Verify SDL_GetClipboardText returned correct string, expected '%s', got '%s'",
458
textRef, charResult);
459
SDL_free(charResult);
461
clipboard_update_count == last_clipboard_update_count + 1,
462
"Verify clipboard update count incremented by 1, got %d",
463
clipboard_update_count - last_clipboard_update_count);
466
intResult = SDL_SetClipboardText(NULL);
469
"Verify result from SDL_SetClipboardText(NULL), expected 0, got %i",
476
SDL_DelEventWatch(ClipboardEventWatch, NULL);
478
return TEST_COMPLETED;
487
static int clipboard_testPrimarySelectionTextFunctions(void *arg)
489
char *textRef = SDLTest_RandomAsciiString();
490
char *text = SDL_strdup(textRef);
494
int last_clipboard_update_count;
496
SDL_AddEventWatch(ClipboardEventWatch, NULL);
499
last_clipboard_update_count = clipboard_update_count;
500
intResult = SDL_SetPrimarySelectionText(NULL);
503
"Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
505
charResult = SDL_GetPrimarySelectionText();
507
charResult && SDL_strcmp(charResult, "") == 0,
508
"Verify SDL_GetPrimarySelectionText returned \"\", got %s",
510
SDL_free(charResult);
511
boolResult = SDL_HasPrimarySelectionText();
513
boolResult == SDL_FALSE,
514
"Verify SDL_HasPrimarySelectionText returned SDL_FALSE, got %s",
515
(boolResult) ? "SDL_TRUE" : "SDL_FALSE");
517
clipboard_update_count == last_clipboard_update_count + 1,
518
"Verify clipboard update count incremented by 1, got %d",
519
clipboard_update_count - last_clipboard_update_count);
522
last_clipboard_update_count = clipboard_update_count;
523
intResult = SDL_SetPrimarySelectionText(text);
526
"Verify result from SDL_SetPrimarySelectionText(%s), expected 0, got %i", text,
529
SDL_strcmp(textRef, text) == 0,
530
"Verify SDL_SetPrimarySelectionText did not modify input string, expected '%s', got '%s'",
532
boolResult = SDL_HasPrimarySelectionText();
534
boolResult == SDL_TRUE,
535
"Verify SDL_HasPrimarySelectionText returned SDL_TRUE, got %s",
536
(boolResult) ? "SDL_TRUE" : "SDL_FALSE");
537
charResult = SDL_GetPrimarySelectionText();
539
charResult && SDL_strcmp(textRef, charResult) == 0,
540
"Verify SDL_GetPrimarySelectionText returned correct string, expected '%s', got '%s'",
541
textRef, charResult);
542
SDL_free(charResult);
544
clipboard_update_count == last_clipboard_update_count + 1,
545
"Verify clipboard update count incremented by 1, got %d",
546
clipboard_update_count - last_clipboard_update_count);
549
intResult = SDL_SetPrimarySelectionText(NULL);
552
"Verify result from SDL_SetPrimarySelectionText(NULL), expected 0, got %i",
559
SDL_DelEventWatch(ClipboardEventWatch, NULL);
561
return TEST_COMPLETED;
566
static const SDLTest_TestCaseReference clipboardTest1 = {
567
(SDLTest_TestCaseFp)clipboard_testClipboardDataFunctions, "clipboard_testClipboardDataFunctions", "End-to-end test of SDL_xyzClipboardData functions", TEST_ENABLED
570
static const SDLTest_TestCaseReference clipboardTest2 = {
571
(SDLTest_TestCaseFp)clipboard_testClipboardTextFunctions, "clipboard_testClipboardTextFunctions", "End-to-end test of SDL_xyzClipboardText functions", TEST_ENABLED
574
static const SDLTest_TestCaseReference clipboardTest3 = {
575
(SDLTest_TestCaseFp)clipboard_testPrimarySelectionTextFunctions, "clipboard_testPrimarySelectionTextFunctions", "End-to-end test of SDL_xyzPrimarySelectionText functions", TEST_ENABLED
579
static const SDLTest_TestCaseReference *clipboardTests[] = {
580
&clipboardTest1, &clipboardTest2, &clipboardTest3, NULL
584
SDLTest_TestSuiteReference clipboardTestSuite = {