diff --git a/tests/.gitignore b/tests/.gitignore index 26bf83db..ffc8b9d2 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -5,3 +5,4 @@ test-text-client wayland-test-client-protocol.h wayland-test-protocol.c wayland-test-server-protocol.h +keyboard-test diff --git a/tests/Makefile.am b/tests/Makefile.am index 7ef61a24..268e4cab 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,7 +1,12 @@ -TESTS = surface-test.la client-test.la \ +TESTS = $(module_tests) $(weston_tests) + +module_tests = surface-test.la client-test.la \ event-test.la text-test.la \ surface-global-test.la \ - button-test.la keyboard-test.la + button-test.la + +weston_tests = \ + keyboard-test TESTS_ENVIRONMENT = $(SHELL) $(top_srcdir)/tests/weston-tests-env @@ -15,11 +20,12 @@ noinst_PROGRAMS = \ matrix-test check_LTLIBRARIES = \ - $(TESTS) + $(module_tests) check_PROGRAMS = \ test-client \ - test-text-client + test-text-client \ + $(weston_tests) AM_CFLAGS = $(GCC_CFLAGS) AM_CPPFLAGS = -I$(top_srcdir)/src -DUNIT_TEST $(COMPOSITOR_CFLAGS) @@ -33,7 +39,6 @@ client_test_la_SOURCES = client-test.c $(test_runner_src) event_test_la_SOURCES = event-test.c $(test_runner_src) text_test_la_SOURCES = text-test.c $(test_runner_src) button_test_la_SOURCES = button-test.c $(test_runner_src) -keyboard_test_la_SOURCES = keyboard-test.c $(test_runner_src) test_client_SOURCES = test-client.c test_client_LDADD = $(SIMPLE_CLIENT_LIBS) @@ -50,6 +55,22 @@ weston_test_la_SOURCES = \ wayland-test-protocol.c \ wayland-test-server-protocol.h +weston_test_runner_src = \ + weston-test-runner.c \ + weston-test-runner.h +weston_test_client_src = \ + weston-test-client-helper.c \ + weston-test-client-helper.h \ + wayland-test-protocol.c \ + wayland-test-client-protocol.h \ + $(weston_test_runner_src) +weston_test_client_libs = \ + $(SIMPLE_CLIENT_LIBS) \ + ../shared/libshared.la + +keyboard_test_SOURCES = keyboard-test.c $(weston_test_client_src) +keyboard_test_LDADD = $(weston_test_client_libs) + matrix_test_SOURCES = \ matrix-test.c \ $(top_srcdir)/shared/matrix.c \ diff --git a/tests/keyboard-test.c b/tests/keyboard-test.c index ef450b50..353dde2e 100644 --- a/tests/keyboard-test.c +++ b/tests/keyboard-test.c @@ -20,105 +20,48 @@ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include -#include -#include -#include +#include "weston-test-client-helper.h" -#include "test-runner.h" - -struct context { - struct weston_seat *seat; - struct weston_surface *surface; - uint32_t expect_key; - uint32_t expect_key_state; - int expect_focus; -}; - -static void -handle_keyboard_state(struct test_client *client) +TEST(simple_keyboard_test) { - struct context *context = client->data; - uint32_t key, key_state; - int focus; + struct client *client; + struct surface *expect_focus = NULL; + struct keyboard *keyboard; + uint32_t expect_key = 0; + uint32_t expect_state = 0; - assert(sscanf(client->buf, "%u %u %d", &key, &key_state, &focus)); - - assert(key == context->expect_key); - assert(key_state == context->expect_key_state); - assert(focus == context->expect_focus); + client = client_create(10, 10, 1, 1); + assert(client); - if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED) { - context->expect_key_state = WL_KEYBOARD_KEY_STATE_RELEASED; - notify_key(context->seat, 100, context->expect_key, - context->expect_key_state, - STATE_UPDATE_AUTOMATIC); - } else if (focus) { - context->expect_focus = 0; - notify_keyboard_focus_out(context->seat); - } else if (context->expect_key < 10) { - context->expect_key++; - context->expect_focus = 1; - context->expect_key_state = WL_KEYBOARD_KEY_STATE_PRESSED; - notify_keyboard_focus_in(context->seat, - &context->seat->keyboard.keys, - STATE_UPDATE_AUTOMATIC); - notify_key(context->seat, 100, context->expect_key, - context->expect_key_state, - STATE_UPDATE_AUTOMATIC); - } else { - test_client_send(client, "bye\n"); - return; + keyboard = client->input->keyboard; + + while(1) { + assert(keyboard->key == expect_key); + assert(keyboard->state == expect_state); + assert(keyboard->focus == expect_focus); + + if (keyboard->state == WL_KEYBOARD_KEY_STATE_PRESSED) { + expect_state = WL_KEYBOARD_KEY_STATE_RELEASED; + wl_test_send_key(client->test->wl_test, expect_key, + expect_state); + yield(client); + } else if (keyboard->focus) { + expect_focus = NULL; + wl_test_activate_surface(client->test->wl_test, + NULL); + yield(client); + } else if (expect_key < 10) { + expect_key++; + expect_focus = client->surface; + expect_state = WL_KEYBOARD_KEY_STATE_PRESSED; + wl_test_activate_surface(client->test->wl_test, + expect_focus->wl_surface); + yield(client); + wl_test_send_key(client->test->wl_test, expect_key, + expect_state); + yield(client); + } else { + break; + } } - - test_client_send(client, "send-keyboard-state\n"); -} - -static void -handle_surface(struct test_client *client) -{ - uint32_t id; - struct context *context = client->data; - struct wl_resource *resource; - struct wl_list *seat_list; - - assert(sscanf(client->buf, "surface %u", &id) == 1); - fprintf(stderr, "server: got surface id %u\n", id); - resource = wl_client_get_object(client->client, id); - assert(resource); - assert(strcmp(resource->object.interface->name, "wl_surface") == 0); - - context->surface = (struct weston_surface *) resource; - weston_surface_set_color(context->surface, 0.0, 0.0, 0.0, 1.0); - weston_surface_configure(context->surface, 100, 100, 100, 100); - weston_surface_update_transform(context->surface); - weston_surface_damage(context->surface); - - seat_list = &client->compositor->seat_list; - assert(wl_list_length(seat_list) == 1); - context->seat = container_of(seat_list->next, struct weston_seat, link); - - context->seat->keyboard.focus = context->surface; - notify_keyboard_focus_out(context->seat); - - test_client_send(client, "send-keyboard-state\n"); - client->handle = handle_keyboard_state; -} - -TEST(keyboard_test) -{ - struct context *context; - struct test_client *client; - - client = test_client_launch(compositor, "test-client"); - client->terminate = 1; - - test_client_send(client, "create-surface\n"); - client->handle = handle_surface; - - context = calloc(1, sizeof *context); - assert(context); - client->data = context; } diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c new file mode 100644 index 00000000..1859356a --- /dev/null +++ b/tests/weston-test-client-helper.c @@ -0,0 +1,488 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "../shared/os-compatibility.h" +#include "weston-test-client-helper.h" + +int +surface_contains(struct surface *surface, int x, int y) +{ + /* test whether a global x,y point is contained in the surface */ + int sx = surface->x; + int sy = surface->y; + int sw = surface->width; + int sh = surface->height; + return x >= sx && y >= sy && x < sx + sw && y < sy + sh; +} + +void +yield(struct client *client) +{ + /* + * FIXME: ugh! how do we ensure all events have finished + * propagating to the client. The calls to usleep seem to do a + * pretty reasonable job... and without them, tests can fail + * intermittently. + */ + usleep(0.02 * 1e6); + wl_display_flush(client->wl_display); + wl_display_roundtrip(client->wl_display); + usleep(0.02 * 1e6); +} + +void +move_pointer(struct client *client, int x, int y) +{ + wl_test_move_pointer(client->test->wl_test, x, y); + + yield(client); +} + +void +move_client(struct client *client, int x, int y) +{ + struct surface *surface = client->surface; + + client->surface->x = x; + client->surface->y = y; + wl_test_move_surface(client->test->wl_test, surface->wl_surface, + surface->x, surface->y); + wl_surface_damage(surface->wl_surface, 0, 0, surface->width, + surface->height); + wl_surface_commit(surface->wl_surface); + + yield(client); + yield(client); +} + +static void +pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *wl_surface, + wl_fixed_t x, wl_fixed_t y) +{ + struct pointer *pointer = data; + + pointer->focus = wl_surface_get_user_data(wl_surface); + pointer->x = wl_fixed_to_int(x); + pointer->y = wl_fixed_to_int(y); + + fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n", + pointer->x, pointer->y, pointer->focus); +} + +static void +pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *wl_surface) +{ + struct pointer *pointer = data; + + pointer->focus = NULL; + + fprintf(stderr, "test-client: got pointer leave, surface %p\n", + wl_surface_get_user_data(wl_surface)); +} + +static void +pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t x, wl_fixed_t y) +{ + struct pointer *pointer = data; + + pointer->x = wl_fixed_to_int(x); + pointer->y = wl_fixed_to_int(y); + + fprintf(stderr, "test-client: got pointer motion %d %d\n", + pointer->x, pointer->y); +} + +static void +pointer_handle_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, + uint32_t state) +{ + struct pointer *pointer = data; + + pointer->button = button; + pointer->state = state; + + fprintf(stderr, "test-client: got pointer button %u %u\n", + button, state); +} + +static void +pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) +{ + fprintf(stderr, "test-client: got pointer axis %u %d\n", axis, value); +} + +static const struct wl_pointer_listener pointer_listener = { + pointer_handle_enter, + pointer_handle_leave, + pointer_handle_motion, + pointer_handle_button, + pointer_handle_axis, +}; + +static void +keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, + uint32_t format, int fd, uint32_t size) +{ + close(fd); + + fprintf(stderr, "test-client: got keyboard keymap\n"); +} + +static void +keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *wl_surface, + struct wl_array *keys) +{ + struct keyboard *keyboard = data; + + keyboard->focus = wl_surface_get_user_data(wl_surface); + + fprintf(stderr, "test-client: got keyboard enter, surface %p\n", + keyboard->focus); +} + +static void +keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *wl_surface) +{ + struct keyboard *keyboard = data; + + keyboard->focus = NULL; + + fprintf(stderr, "test-client: got keyboard leave, surface %p\n", + wl_surface_get_user_data(wl_surface)); +} + +static void +keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t time, uint32_t key, + uint32_t state) +{ + struct keyboard *keyboard = data; + + keyboard->key = key; + keyboard->state = state; + + fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state); +} + +static void +keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t mods_depressed, + uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) +{ + struct keyboard *keyboard = data; + + keyboard->mods_depressed = mods_depressed; + keyboard->mods_latched = mods_latched; + keyboard->mods_locked = mods_locked; + keyboard->group = group; + + fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n", + mods_depressed, mods_latched, mods_locked, group); +} + +static const struct wl_keyboard_listener keyboard_listener = { + keyboard_handle_keymap, + keyboard_handle_enter, + keyboard_handle_leave, + keyboard_handle_key, + keyboard_handle_modifiers, +}; + +static void +surface_enter(void *data, + struct wl_surface *wl_surface, struct wl_output *output) +{ + struct surface *surface = data; + + surface->output = wl_output_get_user_data(output); + + fprintf(stderr, "test-client: got surface enter output %p\n", + surface->output); +} + +static void +surface_leave(void *data, + struct wl_surface *wl_surface, struct wl_output *output) +{ + struct surface *surface = data; + + surface->output = NULL; + + fprintf(stderr, "test-client: got surface leave output %p\n", + wl_output_get_user_data(output)); +} + +static const struct wl_surface_listener surface_listener = { + surface_enter, + surface_leave +}; + +static void +create_shm_buffer(struct client *client) +{ + struct surface *surface = client->surface; + struct wl_shm *shm = client->wl_shm; + struct wl_shm_pool *pool; + int fd, size, stride; + + stride = surface->width * 4; + size = stride * surface->height; + + fd = os_create_anonymous_file(size); + assert(fd >= 0); + + surface->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, + fd, 0); + if (surface->data == MAP_FAILED) { + close(fd); + assert(surface->data != MAP_FAILED); + } + + pool = wl_shm_create_pool(shm, fd, size); + surface->wl_buffer = + wl_shm_pool_create_buffer(pool, 0, surface->width, + surface->height, stride, + WL_SHM_FORMAT_ARGB8888); + wl_shm_pool_destroy(pool); + + close(fd); +} + +static void +shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) +{ + struct client *client = data; + + if (format == WL_SHM_FORMAT_ARGB8888) + client->has_argb = 1; +} + +struct wl_shm_listener shm_listener = { + shm_format +}; + +static void +test_handle_pointer_position(void *data, struct wl_test *wl_test, + wl_fixed_t x, wl_fixed_t y) +{ + struct test *test = data; + test->pointer_x = wl_fixed_to_int(x); + test->pointer_y = wl_fixed_to_int(y); + + fprintf(stderr, "test-client: got global pointer %d %d\n", + test->pointer_x, test->pointer_y); +} + +static const struct wl_test_listener test_listener = { + test_handle_pointer_position +}; + +static void +seat_handle_capabilities(void *data, struct wl_seat *seat, + enum wl_seat_capability caps) +{ + struct input *input = data; + struct pointer *pointer; + struct keyboard *keyboard; + + if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { + pointer = calloc(1, sizeof *pointer); + pointer->wl_pointer = wl_seat_get_pointer(seat); + wl_pointer_set_user_data(pointer->wl_pointer, pointer); + wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, + pointer); + input->pointer = pointer; + } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { + wl_pointer_destroy(input->pointer->wl_pointer); + free(input->pointer); + input->pointer = NULL; + } + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { + keyboard = calloc(1, sizeof *keyboard); + keyboard->wl_keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); + wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, + keyboard); + input->keyboard = keyboard; + } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { + wl_keyboard_destroy(input->keyboard->wl_keyboard); + free(input->keyboard); + input->keyboard = NULL; + } +} + +static const struct wl_seat_listener seat_listener = { + seat_handle_capabilities, +}; + +static void +output_handle_geometry(void *data, + struct wl_output *wl_output, + int x, int y, + int physical_width, + int physical_height, + int subpixel, + const char *make, + const char *model, + int32_t transform) +{ + struct output *output = data; + + output->x = x; + output->y = y; +} + +static void +output_handle_mode(void *data, + struct wl_output *wl_output, + uint32_t flags, + int width, + int height, + int refresh) +{ + struct output *output = data; + + if (flags & WL_OUTPUT_MODE_CURRENT) { + output->width = width; + output->height = height; + } +} + +static const struct wl_output_listener output_listener = { + output_handle_geometry, + output_handle_mode +}; + +static void +handle_global(void *data, struct wl_registry *registry, + uint32_t id, const char *interface, uint32_t version) +{ + struct client *client = data; + struct input *input; + struct output *output; + struct test *test; + + if (strcmp(interface, "wl_compositor") == 0) { + client->wl_compositor = + wl_registry_bind(registry, id, + &wl_compositor_interface, 1); + } else if (strcmp(interface, "wl_seat") == 0) { + input = calloc(1, sizeof *input); + input->wl_seat = + wl_registry_bind(registry, id, + &wl_seat_interface, 1); + wl_seat_add_listener(input->wl_seat, &seat_listener, input); + client->input = input; + } else if (strcmp(interface, "wl_shm") == 0) { + client->wl_shm = + wl_registry_bind(registry, id, + &wl_shm_interface, 1); + wl_shm_add_listener(client->wl_shm, &shm_listener, client); + } else if (strcmp(interface, "wl_output") == 0) { + output = malloc(sizeof *output); + output->wl_output = + wl_registry_bind(registry, id, + &wl_output_interface, 1); + wl_output_add_listener(output->wl_output, + &output_listener, output); + client->output = output; + } else if (strcmp(interface, "wl_test") == 0) { + test = calloc(1, sizeof *test); + test->wl_test = + wl_registry_bind(registry, id, + &wl_test_interface, 1); + wl_test_add_listener(test->wl_test, &test_listener, test); + client->test = test; + } +} + +static const struct wl_registry_listener registry_listener = { + handle_global +}; + +struct client * +client_create(int x, int y, int width, int height) +{ + struct client *client; + struct surface *surface; + + /* connect to display */ + client = calloc(1, sizeof *client); + client->wl_display = wl_display_connect(NULL); + assert(client->wl_display); + + /* setup registry so we can bind to interfaces */ + client->wl_registry = wl_display_get_registry(client->wl_display); + wl_registry_add_listener(client->wl_registry, ®istry_listener, client); + + /* trigger global listener */ + wl_display_dispatch(client->wl_display); + wl_display_roundtrip(client->wl_display); + + /* must have WL_SHM_FORMAT_ARGB32 */ + assert(client->has_argb); + + /* must have wl_test interface */ + assert(client->test); + + /* must have an output */ + assert(client->output); + + /* initialize the client surface */ + surface = calloc(1, sizeof *surface); + + surface->wl_surface = + wl_compositor_create_surface(client->wl_compositor); + assert(surface->wl_surface); + + wl_surface_add_listener(surface->wl_surface, &surface_listener, + surface); + + client->surface = surface; + wl_surface_set_user_data(surface->wl_surface, surface); + + surface->width = width; + surface->height = height; + create_shm_buffer(client); + + memset(surface->data, 64, width * height * 4); + wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0); + + move_client(client, x, y); + + return client; +} diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h new file mode 100644 index 00000000..582572d5 --- /dev/null +++ b/tests/weston-test-client-helper.h @@ -0,0 +1,109 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _WESTON_TEST_CLIENT_HELPER_H_ +#define _WESTON_TEST_CLIENT_HELPER_H_ + +#include +#include "weston-test-runner.h" +#include "wayland-test-client-protocol.h" + +struct client { + struct wl_display *wl_display; + struct wl_registry *wl_registry; + struct wl_compositor *wl_compositor; + struct wl_shm *wl_shm; + struct test *test; + struct input *input; + struct output *output; + struct surface *surface; + int has_argb; +}; + +struct test { + struct wl_test *wl_test; + int pointer_x; + int pointer_y; +}; + +struct input { + struct wl_seat *wl_seat; + struct pointer *pointer; + struct keyboard *keyboard; +}; + +struct pointer { + struct wl_pointer *wl_pointer; + struct surface *focus; + int x; + int y; + uint32_t button; + uint32_t state; +}; + +struct keyboard { + struct wl_keyboard *wl_keyboard; + struct surface *focus; + uint32_t key; + uint32_t state; + uint32_t mods_depressed; + uint32_t mods_latched; + uint32_t mods_locked; + uint32_t group; +}; + +struct output { + struct wl_output *wl_output; + int x; + int y; + int width; + int height; +}; + +struct surface { + struct wl_surface *wl_surface; + struct wl_buffer *wl_buffer; + struct output *output; + int x; + int y; + int width; + int height; + void *data; +}; + +struct client * +client_create(int x, int y, int width, int height); + +int +surface_contains(struct surface *surface, int x, int y); + +void +yield(struct client *client); + +void +move_pointer(struct client *client, int x, int y); + +void +move_client(struct client *client, int x, int y); + + +#endif diff --git a/tests/weston-test-runner.c b/tests/weston-test-runner.c new file mode 100644 index 00000000..e60d4d21 --- /dev/null +++ b/tests/weston-test-runner.c @@ -0,0 +1,114 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "weston-test-runner.h" + +extern const struct weston_test __start_test_section, __stop_test_section; + +static const struct weston_test * +find_test(const char *name) +{ + const struct weston_test *t; + + for (t = &__start_test_section; t < &__stop_test_section; t++) + if (strcmp(t->name, name) == 0) + return t; + + return NULL; +} + +static void +run_test(const struct weston_test *t) +{ + t->run(); + exit(EXIT_SUCCESS); +} + +int main(int argc, char *argv[]) +{ + const struct weston_test *t; + pid_t pid; + int total, pass; + siginfo_t info; + + if (argc == 2) { + t = find_test(argv[1]); + if (t == NULL) { + fprintf(stderr, "unknown test: \"%s\"\n", argv[1]); + exit(EXIT_FAILURE); + } + + run_test(t); + } + + pass = 0; + for (t = &__start_test_section; t < &__stop_test_section; t++) { + int success = 0; + + pid = fork(); + assert(pid >= 0); + + if (pid == 0) + run_test(t); /* never returns */ + + if (waitid(P_ALL, 0, &info, WEXITED)) { + fprintf(stderr, "waitid failed: %m\n"); + abort(); + } + + fprintf(stderr, "test \"%s\":\t", t->name); + switch (info.si_code) { + case CLD_EXITED: + fprintf(stderr, "exit status %d", info.si_status); + if (info.si_status == EXIT_SUCCESS) + success = 1; + break; + case CLD_KILLED: + case CLD_DUMPED: + fprintf(stderr, "signal %d", info.si_status); + break; + } + + if (t->must_fail) + success = !success; + + if (success) { + pass++; + fprintf(stderr, ", pass.\n"); + } else + fprintf(stderr, ", fail.\n"); + } + + total = &__stop_test_section - &__start_test_section; + fprintf(stderr, "%d tests, %d pass, %d fail\n", + total, pass, total - pass); + + return pass == total ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/tests/weston-test-runner.h b/tests/weston-test-runner.h new file mode 100644 index 00000000..41df3868 --- /dev/null +++ b/tests/weston-test-runner.h @@ -0,0 +1,56 @@ +/* + * Copyright © 2012 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _WESTON_TEST_RUNNER_H_ +#define _WESTON_TEST_RUNNER_H_ + +#ifdef NDEBUG +#error "Tests must not be built with NDEBUG defined, they rely on assert()." +#endif + +struct weston_test { + const char *name; + void (*run)(void); + int must_fail; +} __attribute__ ((aligned (16))); + +#define TEST(name) \ + static void name(void); \ + \ + const struct weston_test test##name \ + __attribute__ ((section ("test_section"))) = { \ + #name, name, 0 \ + }; \ + \ + static void name(void) + +#define FAIL_TEST(name) \ + static void name(void); \ + \ + const struct weston_test test##name \ + __attribute__ ((section ("test_section"))) = { \ + #name, name, 1 \ + }; \ + \ + static void name(void) + +#endif