summaryrefslogtreecommitdiff
path: root/tests/cocoa/test_cursor.m
blob: aaca815d4948500c40ecc99fd9e179a1fb64d017 (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
#import <AppKit/AppKit.h>
#include <yasl/native.h>
#include <assert.h>

static void assert_transparent(NSCursor *cursor)
{
    assert(cursor);
    NSBitmapImageRep *bitmap = [NSBitmapImageRep imageRepWithData:cursor.image.TIFFRepresentation];
    assert(bitmap && bitmap.hasAlpha);
    for (NSInteger y = 0; y < bitmap.pixelsHigh; y++)
        for (NSInteger x = 0; x < bitmap.pixelsWide; x++)
            assert([bitmap colorAtX:x y:y].alphaComponent == 0);
}

int main(void)
{
    @autoreleasepool {
        struct yasl_display *display = yasl_display_open(NULL, NULL);
        assert(display);
        struct yasl_window_spec spec = {.title = "Cursor test", .size = {320, 240}};
        struct yasl_surface *surface = yasl_surface_create_toplevel(display, &spec);
        assert(surface);
        NSWindow *window = (NSWindow *)yasl_surface_get_native_handle(surface);
        NSView *view = window.contentView;
        NSEvent *cursor = [NSEvent enterExitEventWithType:NSEventTypeCursorUpdate
            location:NSMakePoint(100, 120) modifierFlags:0 timestamp:0
            windowNumber:window.windowNumber context:nil eventNumber:0 trackingNumber:0 userData:NULL];
        assert(cursor);
        yasl_surface_set_cursor(surface, YASL_CURSOR_HIDDEN);
        [view cursorUpdate:cursor];
        NSCursor *hidden = [NSCursor currentCursor];
        assert_transparent(hidden);

        NSEvent *motion = [NSEvent mouseEventWithType:NSEventTypeMouseMoved
            location:NSMakePoint(100, 120) modifierFlags:0 timestamp:0
            windowNumber:window.windowNumber context:nil eventNumber:0 clickCount:0 pressure:0];
        [view mouseMoved:motion];
        [view cursorUpdate:cursor];
        assert([NSCursor currentCursor] == hidden);
        struct yasl_input_event event;
        bool moved = false;
        while (yasl_surface_input_poll(surface, &event))
            if (event.kind == YASL_INPUT_MOUSE && event.mouse.kind == YASL_PTR_MOTION)
                moved = true;
        assert(moved);

        [[NSCursor arrowCursor] set]; /* Simulate leaving and re-entering. */
        [view cursorUpdate:cursor];
        assert([NSCursor currentCursor] == hidden);
        yasl_surface_set_cursor(surface, YASL_CURSOR_ARROW);
        [view cursorUpdate:cursor];
        assert([NSCursor currentCursor] == [NSCursor arrowCursor]);
        yasl_surface_destroy(surface);
        yasl_display_close(display);
    }
    return 0;
}