diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 21:23:03 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 21:23:03 +0300 |
| commit | dd1775a9927922c14f8679ccbc4d3ca8d5917e20 (patch) | |
| tree | 3acc442bb4a3470364b74aa82ff67cb84d555c45 /tests/common/test_app.c | |
| parent | ecb81aeea4c0c880b3a60a0dc8418c4bf2a7cf85 (diff) | |
public api refactor
Diffstat (limited to 'tests/common/test_app.c')
| -rw-r--r-- | tests/common/test_app.c | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/tests/common/test_app.c b/tests/common/test_app.c index f61a957..214d8f2 100644 --- a/tests/common/test_app.c +++ b/tests/common/test_app.c @@ -2,6 +2,7 @@ #include "yasl/app.h" #include <assert.h> +#include <errno.h> #include <pthread.h> #include <stdio.h> #include <unistd.h> @@ -16,13 +17,16 @@ struct test_state { bool quit_called; int frame_count; yasl_timer_id timer_id; + pthread_t worker; + uint64_t target_id; }; static void *worker_thread(void *arg) { struct test_state *state = arg; usleep(50000); /* 50ms */ - bool ok = yasl_post_event(state->display, state->surface, 42, (void *)0x1234, 0); + bool ok = yasl_display_post(state->display, &(struct yasl_message){ + .target_id = state->target_id, .code = 42, .payload = (void *)0x1234}); assert(ok); return NULL; } @@ -54,13 +58,13 @@ static enum yasl_app_result on_init(void *ud, struct yasl_display *d) yasl_surface_set_pointer_mode(state->surface, YASL_POINTER_RELATIVE); yasl_surface_set_pointer_mode(state->surface, YASL_POINTER_NORMAL); - state->timer_id = yasl_app_add_timer(d, YASL_MS_TO_NS(20), on_timer, state); + state->timer_id = yasl_loop_add_timer(yasl_display_loop(d), YASL_MS_TO_NS(20), on_timer, state); assert(state->timer_id > 0); - pthread_t thread; - int err = pthread_create(&thread, NULL, worker_thread, state); + state->target_id = yasl_surface_id(state->surface); + int err = pthread_create(&state->worker, NULL, worker_thread, state); assert(err == 0); - pthread_detach(thread); + yasl_surface_request_frame(state->surface); return YASL_APP_CONTINUE; @@ -80,7 +84,7 @@ static enum yasl_app_result on_event(void *ud, const struct yasl_event *ev) return YASL_APP_CONTINUE; } -static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context *ctx) +static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame *ctx) { struct test_state *state = ud; assert(s == state->surface); @@ -88,12 +92,12 @@ static void on_frame(void *ud, struct yasl_surface *s, struct yasl_frame_context state->frame_called = true; state->frame_count++; - if (ctx->cpu.pixels) { - size_t count = (size_t)ctx->size.x * ctx->size.y; + if (ctx->target.pixels) { + size_t count = (size_t)ctx->target.size.x * ctx->target.size.y; for (size_t i = 0; i < count; i++) { - ctx->cpu.pixels[i] = 0xff204060; + ctx->target.pixels[i] = 0xff204060; } - bool ok = yasl_frame_present_cpu(ctx, NULL, 0); + bool ok = yasl_frame_present(ctx, NULL, 0); assert(ok); } @@ -106,14 +110,15 @@ static void on_quit(void *ud, struct yasl_display *d) { (void)d; struct test_state *state = ud; + assert(!pthread_join(state->worker, NULL)); state->quit_called = true; } -static void present_once(void *ud, struct yasl_surface *s, struct yasl_frame_context *ctx) +static void present_once(void *ud, struct yasl_surface *s, struct yasl_frame *ctx) { (*(int *)ud)++; - if (ctx->cpu.pixels) - assert(yasl_frame_present_cpu(ctx, NULL, 0)); + if (ctx->target.pixels) + assert(yasl_frame_present(ctx, NULL, 0)); yasl_surface_destroy(s); } @@ -132,6 +137,43 @@ static void test_window_without_init(void) assert(frames == 1); } + +static enum yasl_app_result fail_init(void *ud, struct yasl_display *d) +{ + (void)ud; (void)d; + return YASL_APP_ERROR; +} +static void count_quit(void *ud, struct yasl_display *d) +{ + (void)d; + (*(int *)ud)++; +} +static enum yasl_app_result inspect_initial_window(void *ud, struct yasl_display *d) +{ + struct yasl_surface *s = yasl_app_window(d); + assert(s && yasl_surface_userdata(s) == ud); + errno = 0; + assert(!yasl_surface_set_callbacks(s, NULL, NULL) && errno == EBUSY); + return YASL_APP_CONTINUE; +} +static void fail_frame(void *ud, struct yasl_surface *s, struct yasl_frame *frame) +{ + (void)ud; + assert(frame->surface == s && frame->target.stride_bytes == frame->target.size.x * 4); + yasl_app_exit(yasl_surface_display(s), YASL_APP_ERROR); +} +static void test_error_lifecycle(void) +{ + int quits = 0; + assert(yasl_run(&(struct yasl_app_desc){.userdata = &quits, + .init = fail_init, .quit = count_quit}) < 0); + assert(quits == 1 && errno == EIO); + struct yasl_window_spec window = {.size_logical = {160, 120}, .userdata = &quits}; + assert(yasl_run(&(struct yasl_app_desc){.userdata = &quits, .window = &window, + .init = inspect_initial_window, .frame = fail_frame, .quit = count_quit}) < 0); + assert(quits == 2 && errno == EIO); +} + int main(void) { struct test_state state = {0}; @@ -146,6 +188,9 @@ int main(void) }; int res = yasl_run(&desc); + if (res < 0 && !state.init_called && + (errno == EPERM || errno == EACCES || errno == ENOENT || + errno == ECONNREFUSED || errno == ENODEV)) return 77; assert(res == 0); assert(state.init_called); assert(state.frame_called); @@ -153,6 +198,7 @@ int main(void) assert(state.timer_called); assert(state.quit_called); test_window_without_init(); + test_error_lifecycle(); printf("App runner tests passed!\n"); return 0; } |
