summaryrefslogtreecommitdiff
path: root/tests/cocoa/test_ownership.m
blob: 5fd5cbfe3dc556859dbf9d695778937201bedb58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "yasl/native.h"
#include "yasl/native/cocoa.h"
#import <AppKit/AppKit.h>
#include "yasl/yasl.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    struct yasl_display *d = yasl_display_open(NULL, NULL);
    assert(d);
    struct yasl_window_spec spec = {.title = "Ownership test", .size = {160, 100}};
    struct yasl_surface *s = yasl_surface_create_toplevel(d, &spec);
    assert(s);
    assert(yasl_display_get_native_handle(d) == (void *)NSApp);
    assert(yasl_cocoa_display_get_application(d) == NSApp);
    NSWindow *window = (NSWindow *)yasl_surface_get_native_handle(s);
    assert(yasl_cocoa_surface_get_window(s) == window);
    assert(yasl_cocoa_surface_get_view(s) == window.contentView);
    assert([window isKindOfClass:[NSWindow class]] && window.contentView);
    struct yasl_event event;
    while (yasl_surface_poll_event(s, &event)) {}
    @autoreleasepool {
        for (int i = 0; i < 600; i++) {
            char text[32];
            snprintf(text, sizeof(text), "paste-%d", i);
            yasl_display_copy(d, YASL_CLIPBOARD, text);
            yasl_display_paste(d, YASL_CLIPBOARD, s);
            memset(text, 'x', sizeof(text));
        }
    }
    /* All NSString/UTF8String temporaries have now expired. */
    for (int i = 0; i < 600; i++) {
        char text[32];
        snprintf(text, sizeof(text), "paste-%d", i);
        assert(yasl_surface_poll_event(s, &event));
        assert(event.type == YASL_EVENT_PASTE && strcmp(event.text.data, text) == 0);
    }
    const char *last = event.text.data;
    assert(!yasl_surface_poll_event(s, &event));
    @autoreleasepool { yasl_display_copy(d, YASL_CLIPBOARD, "new selection"); }
    assert(strcmp(last, "paste-599") == 0);
    @autoreleasepool {
        yasl_display_copy(d, YASL_CLIPBOARD, "");
        yasl_display_paste(d, YASL_CLIPBOARD, s);
    }
    assert(yasl_surface_poll_event(s, &event));
    assert(event.type == YASL_EVENT_PASTE && !event.text.len && !event.text.data[0]);
    yasl_surface_destroy(s);
    yasl_display_close(d);
    return 0;
}