summaryrefslogtreecommitdiff
path: root/tests/cocoa/test_ownership.m
blob: 6d699077dab1781305ad18b03412e4cb2ea7e4c7 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "yasl/native.h"
#include "yasl/native/cocoa.h"
#import <AppKit/AppKit.h>
#include "yasl/yasl.h"
#include <assert.h>
#include <errno.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_display_poll_event(d, &event)) {}

    assert(yasl_surface_set_size(s, (struct yasl_vector2i){180, 110}));
    assert(yasl_surface_set_fullscreen(s, false));
    assert(yasl_surface_set_maximized(s, false));
    assert(!yasl_surface_set_size(s, (struct yasl_vector2i){0, 100}));
    assert(errno == EINVAL);
    struct yasl_popup_spec popup_spec = {
        .size = {80, 40}, .anchor = {10, 10, 2, 2},
    };
    struct yasl_surface *popup = yasl_surface_create_popup(s, &popup_spec);
    assert(popup);
    assert(!yasl_surface_set_size(popup, (struct yasl_vector2i){80, 40}));
    assert(errno == EINVAL);
    assert(!yasl_surface_set_fullscreen(popup, true) && errno == EINVAL);
    assert(!yasl_surface_set_maximized(popup, true) && errno == EINVAL);
    assert(!yasl_surface_minimize(popup) && errno == EINVAL);
    yasl_surface_destroy(popup);
    while (yasl_display_poll_event(d, &event)) {}

    id<NSTextInputClient> text_input = (id<NSTextInputClient>)yasl_cocoa_surface_get_view(s);
    [text_input setMarkedText:@"é漢" selectedRange:NSMakeRange(1, 1)
              replacementRange:NSMakeRange(NSNotFound, 0)];
    assert(strcmp(yasl_surface_ime_preedit(s), "é漢") == 0);
    [text_input unmarkText];
    assert(yasl_surface_ime_preedit(s) == NULL);
    assert(yasl_display_poll_event(d, &event));
    assert(event.type == YASL_EVENT_COMPOSITION && event.composition.active);
    assert(strcmp(event.composition.data, "é漢") == 0);
    assert(event.composition.begin == 2 && event.composition.end == 5);
    assert(yasl_display_poll_event(d, &event));
    assert(event.type == YASL_EVENT_COMPOSITION && !event.composition.active);
    assert(event.composition.begin == -1 && event.composition.end == -1);
    while (yasl_display_poll_event(d, &event)) {}

    uint64_t request_ids[600];
    @autoreleasepool {
        for (int i = 0; i < 600; i++) {
            char text[32];
            snprintf(text, sizeof(text), "paste-%d", i);
            assert(yasl_clipboard_set(d, YASL_CLIPBOARD, text));
            request_ids[i] = yasl_clipboard_request(s, YASL_CLIPBOARD);
            assert(request_ids[i] != 0);
            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_display_poll_event(d, &event));
        assert(event.type == YASL_EVENT_PASTE && event.paste.request_id == request_ids[i]);
        assert(event.paste.status == YASL_TRANSFER_OK && strcmp(event.paste.data, text) == 0);
    }
    const char *last = event.paste.data;
    assert(!yasl_display_poll_event(d, &event));
    @autoreleasepool { yasl_display_copy(d, YASL_CLIPBOARD, "new selection"); }
    assert(strcmp(last, "paste-599") == 0);
    assert(!yasl_clipboard_set(d, YASL_CLIPBOARD, "\xc3"));
    assert(errno == EILSEQ);
    assert(yasl_clipboard_set(d, YASL_CLIPBOARD, ""));
    uint64_t empty_id = yasl_clipboard_request(s, YASL_CLIPBOARD);
    assert(empty_id && yasl_display_poll_event(d, &event));
    assert(event.type == YASL_EVENT_PASTE && event.paste.request_id == empty_id);
    assert(event.paste.status == YASL_TRANSFER_OK && !event.paste.len && !event.paste.data[0]);
    [NSPasteboard.generalPasteboard clearContents];
    uint64_t unavailable_id = yasl_clipboard_request(s, YASL_CLIPBOARD);
    assert(unavailable_id && yasl_display_poll_event(d, &event));
    assert(event.type == YASL_EVENT_PASTE && event.paste.request_id == unavailable_id);
    assert(event.paste.status == YASL_TRANSFER_UNAVAILABLE && !event.paste.len);
    yasl_display_copy(d, YASL_CLIPBOARD, "legacy paste");
    yasl_display_paste(d, YASL_CLIPBOARD, s);
    assert(yasl_display_poll_event(d, &event));
    assert(event.type == YASL_EVENT_PASTE && event.paste.request_id == 0);
    assert(event.paste.status == YASL_TRANSFER_OK && strcmp(event.text.data, "legacy paste") == 0);
    yasl_surface_destroy(s);
    yasl_display_close(d);
    return 0;
}