summaryrefslogtreecommitdiff
path: root/tests/common/test_contracts.c
blob: ee6fe612eab679d4a54dbc6da0a060b2ccb206bc (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "common/backend.h"
#include "common/input.h"
#include "common/timing.h"

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

static void test_timing_clock(void)
{
    struct yasl_frame_clock clock = {.refresh_ns = YASL_REFRESH_DEFAULT_NS};
    yasl_clock_presented(&clock, 1000000000, true);
    assert(clock.last_present_ns == 1000000000);
    uint64_t next = yasl_clock_next_present_ns(&clock);
    assert(next > 1000000000);
    assert(yasl_clock_extend_ms(1000, 1000000000) == 1000000000);
    assert(yasl_clock_frame_timestamp_ns(&clock, next) == next);

    yasl_clock_note_resize(&clock, 500000);
    struct yasl_resize_stats stats;
    yasl_clock_resize_stats(&clock, &stats);
    assert(stats.count == 1);
    assert(stats.max_us == 500.0);
}

static void test_surface_state(void)
{
    /* Reading reported flags does not require a backend query or display. */
    struct yasl_surface surface = {.state = YASL_STATE_FULLSCREEN | YASL_STATE_ACTIVATED};
    assert(!yasl_surface_state(NULL));
    assert(yasl_surface_state(&surface) == (YASL_STATE_FULLSCREEN | YASL_STATE_ACTIVATED));
}

static void test_queue(void)
{
    struct input_queue q = {0};
    struct yasl_surface surface = {0};
    const enum yasl_input_kind kinds[] = {
        YASL_INPUT_TEXT, YASL_INPUT_PASTE, YASL_INPUT_DROP_FILES,
    };
    for (size_t k = 0; k < sizeof(kinds) / sizeof(kinds[0]); k++) {
        char *source = malloc(4);
        assert(source);
        memcpy(source, "a\0b", 4);
        struct yasl_input_event in = {.surface = &surface, .kind = kinds[k]};
        if (in.kind == YASL_INPUT_TEXT) { in.text.text = source; in.text.len = 3; }
        if (in.kind == YASL_INPUT_PASTE) { in.paste.text = source; in.paste.len = 3; }
        if (in.kind == YASL_INPUT_DROP_FILES) { in.drop_files.files = source; in.drop_files.len = 3; }
        assert(input_queue_push(&q, &in));
        memset(source, 'x', 4);
        free(source);
        struct yasl_input_event out;
        assert(input_queue_pop(&q, &out));
        const char *payload = out.kind == YASL_INPUT_TEXT ? out.text.text :
            out.kind == YASL_INPUT_PASTE ? out.paste.text : out.drop_files.files;
        assert(out.surface == &surface && out.kind == kinds[k]);
        assert(memcmp(payload, "a\0b", 4) == 0);
        assert(!input_queue_pop(&q, &out));
        assert(memcmp(payload, "a\0b", 4) == 0);
        /* Growth/wraparound must not invalidate a previously polled payload. */
        for (unsigned i = 0; i < 4096; i++) {
            in = (struct yasl_input_event){.kind = YASL_INPUT_KEY, .key.key = i};
            assert(input_queue_push(&q, &in));
        }
        assert(memcmp(payload, "a\0b", 4) == 0);
        for (unsigned i = 0; i < 2048; i++) {
            assert(input_queue_pop(&q, &out));
            assert(out.key.key == i);
        }
        for (unsigned i = 4096; i < 6144; i++) {
            in.key.key = i;
            assert(input_queue_push(&q, &in));
        }
        for (unsigned i = 2048; i < 6144; i++) {
            assert(input_queue_pop(&q, &out));
            assert(out.key.key == i);
        }
        assert(!input_queue_pop(&q, &out));
    }
    struct yasl_input_event empty = {.surface = &surface, .kind = YASL_INPUT_PASTE};
    struct yasl_input_event out;
    assert(input_queue_push(&q, &empty));
    assert(input_queue_pop(&q, &out));
    assert(out.paste.text && !out.paste.len && out.paste.text[0] == '\0');
    struct yasl_input_event in = {.surface = &surface, .kind = YASL_INPUT_TEXT,
                                 .text = {"owned", 5}};
    assert(input_queue_push(&q, &in));
    assert(input_queue_pop(&q, &out));
    const char *last = out.text.text;
    assert(input_queue_push(&q, &in));
    /* Exercise the allocation-failure reset path without allocating huge RAM. */
    in.text.len = SIZE_MAX;
    assert(!input_queue_push(&q, &in));
    assert(strcmp(last, "owned") == 0);
    in = (struct yasl_input_event){.surface = &surface, .kind = YASL_INPUT_KEY};
    assert(input_queue_push(&q, &in));
    assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_RESET);
    assert(out.surface == &surface);
    assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_KEY);
    assert(!input_queue_pop(&q, &out));
    in = (struct yasl_input_event){.kind = YASL_INPUT_PASTE, .paste = {"pending", 7}};
    assert(input_queue_push(&q, &in));
    input_queue_clear(&q);
    input_queue_clear(&q);
    assert(!q.events && !q.last_text && !q.count && !q.reset_pending);
}

static int presents, copies, pastes, creates;
static struct yasl_damage_list received;
static bool present(struct yasl_surface *s, const struct yasl_framebuffer *fb,
                    const struct yasl_damage_list *damage)
{
    (void)s; (void)fb;
    presents++;
    received = *damage;
    return true;
}
static uint32_t caps(const struct yasl_display *d) { (void)d; return 0; }
static void copy(struct yasl_display *d, enum yasl_selection sel, const char *text)
{ (void)d; (void)sel; (void)text; copies++; }
static void paste(struct yasl_display *d, enum yasl_selection sel, struct yasl_surface *s)
{ (void)d; (void)sel; (void)s; pastes++; }
static struct yasl_surface *create(struct yasl_display *d, const struct yasl_window_spec *spec, void *graphics_device)
{ (void)d; (void)spec; (void)graphics_device; creates++; return NULL; }

static void *native_device;
static int native_queries;
static struct yasl_surface native_surface;
static uint32_t native_caps(const struct yasl_display *d)
{ (void)d; return YASL_CAP_NATIVE_GRAPHICS; }
static struct yasl_surface *native_create(struct yasl_display *d,
        const struct yasl_window_spec *spec, void *device)
{
    (void)spec;
    native_device = device;
    native_surface = (struct yasl_surface){.display = d, .frame_generation = 1};
    return &native_surface;
}
static bool native_frame(struct yasl_surface *s, const struct yasl_framebuffer *fb,
                         struct yasl_native_frame *out)
{
    (void)s; (void)fb;
    native_queries++;
    *out = (struct yasl_native_frame){.drawable = native_device};
    return true;
}

static void test_native(void)
{
    const struct yasl_backend_ops ops = {.display_caps = native_caps,
        .surface_create_toplevel = native_create, .surface_get_native_frame = native_frame};
    struct yasl_display d = {.ops = &ops};
    struct yasl_window_spec spec = {.size = {100, 80}};
    assert(yasl_surface_create_toplevel(&d, &spec) == &native_surface);
    assert(!native_device);
    assert(yasl_surface_create_native(&d, &spec, &d) == &native_surface);
    assert(native_device == &d);

    struct yasl_framebuffer fb = {.generation = 1, .size = {100, 80}};
    struct yasl_native_frame out = {0};
    assert(!yasl_surface_get_native_frame(NULL, &fb, &out));
    assert(!yasl_surface_get_native_frame(&native_surface, NULL, &out));
    assert(!yasl_surface_get_native_frame(&native_surface, &fb, NULL));
    fb.generation = 0;
    assert(!yasl_surface_get_native_frame(&native_surface, &fb, &out));
    fb.generation = 2;
    assert(!yasl_surface_get_native_frame(&native_surface, &fb, &out));
    assert(!native_queries && !out.drawable);
    fb.generation = 1;
    assert(yasl_surface_get_native_frame(&native_surface, &fb, &out));
    assert(native_queries == 1 && out.drawable == &d);
}

static void test_validation(void)
{
    const struct yasl_backend_ops ops = {.surface_present = present,
        .display_caps = caps, .display_copy = copy, .display_paste = paste,
        .surface_create_toplevel = create};
    struct yasl_display d = {.ops = &ops};
    struct yasl_surface s = {.display = &d, .frame_generation = 1};
    struct yasl_framebuffer fb = {.generation = 1, .size = {100, 80}};
    struct yasl_damage_list damage = {.n = -2};
    assert(!yasl_surface_present(&s, &fb, &damage));
    damage.n = YASL_DAMAGE_MAX + 1;
    assert(!yasl_surface_present(&s, &fb, &damage));
    damage.n = 1;
    damage.rects[0] = (struct yasl_rect2i){0, 0, -1, 1};
    assert(!yasl_surface_present(&s, &fb, &damage));
    assert(!yasl_surface_present(&s, &fb, NULL));
    assert(!presents);
    damage.n = -1;
    fb.generation = 0;
    assert(!yasl_surface_present(&s, &fb, &damage));
    fb.generation = 2;
    assert(!yasl_surface_present(&s, &fb, &damage));
    fb.generation = 1;
    damage.n = 4;
    damage.rects[0] = (struct yasl_rect2i){-10, -10, 30, 30};
    damage.rects[1] = (struct yasl_rect2i){90, 70, INT_MAX, INT_MAX};
    damage.rects[2] = (struct yasl_rect2i){INT_MAX, INT_MAX, INT_MAX, INT_MAX};
    damage.rects[3] = (struct yasl_rect2i){INT_MIN, INT_MIN, INT_MAX, INT_MAX};
    assert(yasl_surface_present(&s, &fb, &damage));
    assert(received.n == 2 && received.rects[0].x == 0 && received.rects[0].width == 20);
    assert(received.rects[1].width == 10 && received.rects[1].height == 10);
    assert(damage.n == 4 && damage.rects[0].x == -10);
    damage.n = -1;
    assert(yasl_surface_present(&s, &fb, &damage) && received.n == -1);
    damage.n = 0;
    assert(yasl_surface_present(&s, &fb, &damage) && received.n == 0);
    yasl_display_copy(&d, YASL_PRIMARY, "no alias");
    yasl_display_copy(&d, (enum yasl_selection)255, "invalid");
    yasl_display_paste(&d, YASL_PRIMARY, &s);
    yasl_display_paste(&d, (enum yasl_selection)255, &s);
    assert(!copies && !pastes);
    yasl_display_copy(&d, YASL_CLIPBOARD, "yes");
    yasl_display_paste(&d, YASL_CLIPBOARD, &s);
    assert(copies == 1 && pastes == 1);
    struct yasl_window_spec spec = {.size = {100, 80}};
    assert(!yasl_surface_create_native(&d, &spec, &d) && errno == ENOTSUP);
    assert(!yasl_surface_create_native(&d, &spec, NULL) && errno == EINVAL);
    spec.size.x = 0;
    assert(!yasl_surface_create_toplevel(&d, &spec) && errno == EINVAL);
    assert(!creates);
}

static void test_scale(void)
{
    assert(yasl_scale_to_device(-10, 120) == -10);
    assert(yasl_scale_to_device(-0.5, 120) == -1);
    assert(yasl_scale_to_device(0.5, 120) == 1);
    assert(yasl_scale_to_device(NAN, 120) == 0);
    assert(yasl_scale_to_device(INFINITY, 120) == 0);
    assert(yasl_scale_to_device(DBL_MAX, 240) == INT_MAX);
    assert(yasl_scale_to_logical(-1, 240) == -1);
    assert(yasl_scale_to_logical(1, 240) == 1);
    assert(yasl_scale_to_logical(INT_MAX, 1) == INT_MAX);
    assert(yasl_scale_to_logical(INT_MIN, 1) == INT_MIN);
    assert(yasl_scale_to_logical(1, 0) == 0);
}

static int popup_calls;
static struct yasl_surface *popup_create(struct yasl_surface *parent,
        const struct yasl_popup_spec *spec)
{
    (void)parent; (void)spec;
    popup_calls++;
    return NULL; /* Backend fails without setting errno. */
}

static bool popup_reposition(struct yasl_surface *popup,
        const struct yasl_popup_spec *spec)
{
    (void)popup; (void)spec;
    popup_calls++;
    errno = EBUSY;
    return false;
}

static uint32_t popup_caps(const struct yasl_display *d)
{ (void)d; return YASL_CAP_POPUPS; }

static int dispatch_failure(struct yasl_display *d, int timeout)
{ (void)d; (void)timeout; return -1; }

static struct yasl_surface *create_failure(struct yasl_display *d,
        const struct yasl_window_spec *spec, void *device)
{ (void)d; (void)spec; (void)device; return NULL; }

static int drag_starts;
static bool drag_without_listener(struct yasl_display *d, struct yasl_surface *s,
        const struct yasl_drag_image *image, const struct yasl_drag_listener *listener,
        void *ud)
{
    (void)d; (void)s; (void)image; (void)ud;
    /* Backends may require a table even when no completion callback is needed. */
    assert(listener && !listener->end);
    drag_starts++;
    return true;
}

static void test_errors(void)
{
    const struct yasl_backend_ops ops = {.display_caps = popup_caps,
        .surface_create_toplevel = create_failure,
        .surface_create_popup = popup_create,
        .surface_popup_reposition = popup_reposition,
        .display_drag_start = drag_without_listener,
        .display_dispatch_timeout = dispatch_failure};
    struct yasl_display d = {.ops = &ops};
    struct yasl_surface s = {.display = &d};
    struct yasl_popup_spec spec = {.size = {120, 80}, .anchor = {0, 0, 1, 1}};
    assert(!yasl_surface_create_popup(NULL, &spec) && errno == EINVAL);
    assert(!yasl_surface_create_popup(&s, NULL) && errno == EINVAL);
    spec.anchor.width = -1; /* Must not reach Wayland's protocol marshaler. */
    assert(!yasl_surface_create_popup(&s, &spec) && errno == EINVAL);
    assert(!yasl_surface_popup_reposition(&s, &spec) && errno == EINVAL);
    spec.anchor.width = 1;
    spec.content_offset.x = INT_MAX;
    spec.content_size.x = INT_MAX;
    assert(!yasl_surface_create_popup(&s, &spec) && errno == EINVAL);
    spec.content_offset.x = spec.content_size.x = 0;
    spec.size.x = 32768;
    assert(!yasl_surface_create_popup(&s, &spec) && errno == EINVAL);
    assert(!popup_calls);
    spec.size.x = 120;
    errno = ERANGE;
    assert(!yasl_surface_create_popup(&s, &spec) && errno == ENODEV);
    assert(!yasl_surface_popup_reposition(&s, &spec) && errno == EBUSY);
    assert(popup_calls == 2);
    const struct yasl_backend_ops unsupported = {0};
    d.ops = &unsupported;
    assert(!yasl_surface_create_popup(&s, &spec) && errno == ENOTSUP);
    assert(!yasl_surface_popup_reposition(&s, &spec) && errno == ENOTSUP);
    assert(yasl_display_dispatch(&d, 0) == -1 && errno == ENOTSUP);
    assert(yasl_display_dispatch(NULL, 0) == -1 && errno == EINVAL);
    d.ops = &ops;
    errno = ERANGE;
    assert(yasl_display_dispatch(&d, 0) == -1 && errno == EIO);
    struct yasl_window_spec toplevel = {.size = {120, 80}};
    errno = ERANGE;
    assert(!yasl_surface_create_toplevel(&d, &toplevel) && errno == ENODEV);
    assert(yasl_display_drag_start(&d, &s, NULL, NULL, NULL));
    struct yasl_drag_image image = {.size = {1, 1}};
    assert(!yasl_display_drag_start(&d, &s, &image, NULL, NULL));
    uint32_t pixel = 0;
    image.pixels = &pixel;
    image.size.x = INT_MAX;
    assert(!yasl_display_drag_start(&d, &s, &image, NULL, NULL));
    assert(drag_starts == 1);
}

int main(void)
{
    test_timing_clock();
    test_surface_state();
    test_queue();
    test_validation();
    test_native();
    test_scale();
    test_errors();
    return 0;
}