Pekka Paalanen | f5b74f7 | 2015-03-25 12:50:31 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * Copyright © 2013 DENSO CORPORATION |
| 4 | * Copyright © 2015 Collabora, Ltd. |
| 5 | * |
| 6 | * Permission to use, copy, modify, distribute, and sell this software and |
| 7 | * its documentation for any purpose is hereby granted without fee, provided |
| 8 | * that the above copyright notice appear in all copies and that both that |
| 9 | * copyright notice and this permission notice appear in supporting |
| 10 | * documentation, and that the name of the copyright holders not be used in |
| 11 | * advertising or publicity pertaining to distribution of the software |
| 12 | * without specific, written prior permission. The copyright holders make |
| 13 | * no representations about the suitability of this software for any |
| 14 | * purpose. It is provided "as is" without express or implied warranty. |
| 15 | * |
| 16 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 17 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 18 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 20 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 21 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 22 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | |
| 27 | #include <unistd.h> |
| 28 | #include <signal.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "../src/compositor.h" |
| 32 | #include "weston-test-server-protocol.h" |
| 33 | #include "ivi-test.h" |
| 34 | #include "../ivi-shell/ivi-layout-export.h" |
| 35 | |
| 36 | struct test_context; |
| 37 | |
| 38 | struct runner_test { |
| 39 | const char *name; |
| 40 | void (*run)(struct test_context *); |
| 41 | } __attribute__ ((aligned (32))); |
| 42 | |
| 43 | #define RUNNER_TEST(name) \ |
| 44 | static void runner_func_##name(struct test_context *); \ |
| 45 | \ |
| 46 | const struct runner_test runner_test_##name \ |
| 47 | __attribute__ ((section ("test_section"))) = \ |
| 48 | { \ |
| 49 | #name, runner_func_##name \ |
| 50 | }; \ |
| 51 | \ |
| 52 | static void runner_func_##name(struct test_context *ctx) |
| 53 | |
| 54 | extern const struct runner_test __start_test_section; |
| 55 | extern const struct runner_test __stop_test_section; |
| 56 | |
| 57 | static const struct runner_test * |
| 58 | find_runner_test(const char *name) |
| 59 | { |
| 60 | const struct runner_test *t; |
| 61 | |
| 62 | for (t = &__start_test_section; t < &__stop_test_section; t++) { |
| 63 | if (strcmp(t->name, name) == 0) |
| 64 | return t; |
| 65 | } |
| 66 | |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | struct test_launcher { |
| 71 | struct weston_compositor *compositor; |
| 72 | char exe[2048]; |
| 73 | struct weston_process process; |
| 74 | const struct ivi_controller_interface *controller_interface; |
| 75 | }; |
| 76 | |
| 77 | static void |
| 78 | runner_destroy_handler(struct wl_client *client, struct wl_resource *resource) |
| 79 | { |
| 80 | wl_resource_destroy(resource); |
| 81 | } |
| 82 | |
| 83 | struct test_context { |
| 84 | const struct ivi_controller_interface *controller_interface; |
| 85 | struct wl_resource *runner_resource; |
| 86 | }; |
| 87 | |
| 88 | static void |
| 89 | runner_run_handler(struct wl_client *client, struct wl_resource *resource, |
| 90 | const char *test_name) |
| 91 | { |
| 92 | struct test_launcher *launcher; |
| 93 | const struct runner_test *t; |
| 94 | struct test_context ctx; |
| 95 | |
| 96 | launcher = wl_resource_get_user_data(resource); |
| 97 | ctx.controller_interface = launcher->controller_interface; |
| 98 | ctx.runner_resource = resource; |
| 99 | |
| 100 | t = find_runner_test(test_name); |
| 101 | if (!t) { |
| 102 | weston_log("Error: runner test \"%s\" not found.\n", |
| 103 | test_name); |
| 104 | wl_resource_post_error(resource, |
| 105 | WESTON_TEST_RUNNER_ERROR_UNKNOWN_TEST, |
| 106 | "weston_test_runner: unknown: '%s'", |
| 107 | test_name); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | weston_log("weston_test_runner.run(\"%s\")\n", test_name); |
| 112 | |
| 113 | t->run(&ctx); |
| 114 | |
| 115 | weston_test_runner_send_finished(resource); |
| 116 | } |
| 117 | |
| 118 | static const struct weston_test_runner_interface runner_implementation = { |
| 119 | runner_destroy_handler, |
| 120 | runner_run_handler |
| 121 | }; |
| 122 | |
| 123 | static void |
| 124 | bind_runner(struct wl_client *client, void *data, |
| 125 | uint32_t version, uint32_t id) |
| 126 | { |
| 127 | struct test_launcher *launcher = data; |
| 128 | struct wl_resource *resource; |
| 129 | |
| 130 | resource = wl_resource_create(client, &weston_test_runner_interface, |
| 131 | 1, id); |
| 132 | if (!resource) { |
| 133 | wl_client_post_no_memory(client); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | wl_resource_set_implementation(resource, &runner_implementation, |
| 138 | launcher, NULL); |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | test_client_sigchld(struct weston_process *process, int status) |
| 143 | { |
| 144 | struct test_launcher *launcher = |
| 145 | container_of(process, struct test_launcher, process); |
| 146 | struct weston_compositor *c = launcher->compositor; |
| 147 | |
| 148 | /* Chain up from weston-test-runner's exit code so that automake |
| 149 | * knows the exit status and can report e.g. skipped tests. */ |
| 150 | if (WIFEXITED(status)) |
| 151 | weston_compositor_exit_with_code(c, WEXITSTATUS(status)); |
| 152 | else |
| 153 | weston_compositor_exit_with_code(c, EXIT_FAILURE); |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | idle_launch_client(void *data) |
| 158 | { |
| 159 | struct test_launcher *launcher = data; |
| 160 | pid_t pid; |
| 161 | sigset_t allsigs; |
| 162 | |
| 163 | pid = fork(); |
| 164 | if (pid == -1) { |
| 165 | weston_log("fatal: failed to fork '%s': %m\n", launcher->exe); |
| 166 | weston_compositor_exit_with_code(launcher->compositor, |
| 167 | EXIT_FAILURE); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (pid == 0) { |
| 172 | sigfillset(&allsigs); |
| 173 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 174 | execl(launcher->exe, launcher->exe, NULL); |
| 175 | weston_log("compositor: executing '%s' failed: %m\n", |
| 176 | launcher->exe); |
| 177 | _exit(EXIT_FAILURE); |
| 178 | } |
| 179 | |
| 180 | launcher->process.pid = pid; |
| 181 | launcher->process.cleanup = test_client_sigchld; |
| 182 | weston_watch_process(&launcher->process); |
| 183 | } |
| 184 | |
| 185 | int |
| 186 | controller_module_init(struct weston_compositor *compositor, |
| 187 | int *argc, char *argv[], |
| 188 | const struct ivi_controller_interface *iface, |
| 189 | size_t iface_version); |
| 190 | |
| 191 | WL_EXPORT int |
| 192 | controller_module_init(struct weston_compositor *compositor, |
| 193 | int *argc, char *argv[], |
| 194 | const struct ivi_controller_interface *iface, |
| 195 | size_t iface_version) |
| 196 | { |
| 197 | struct wl_event_loop *loop; |
| 198 | struct test_launcher *launcher; |
| 199 | const char *path; |
| 200 | |
| 201 | /* strict check, since this is an internal test module */ |
| 202 | if (iface_version != sizeof(*iface)) { |
| 203 | weston_log("fatal: controller interface mismatch\n"); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | path = getenv("WESTON_BUILD_DIR"); |
| 208 | if (!path) { |
| 209 | weston_log("test setup failure: WESTON_BUILD_DIR not set\n"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | launcher = zalloc(sizeof *launcher); |
| 214 | if (!launcher) |
| 215 | return -1; |
| 216 | |
| 217 | launcher->compositor = compositor; |
| 218 | launcher->controller_interface = iface; |
| 219 | snprintf(launcher->exe, sizeof launcher->exe, |
| 220 | "%s/ivi-layout.ivi", path); |
| 221 | |
| 222 | if (wl_global_create(compositor->wl_display, |
| 223 | &weston_test_runner_interface, 1, |
| 224 | launcher, bind_runner) == NULL) |
| 225 | return -1; |
| 226 | |
| 227 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 228 | wl_event_loop_add_idle(loop, idle_launch_client, launcher); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static void |
| 234 | runner_assert_fail(const char *cond, const char *file, int line, |
| 235 | const char *func, struct test_context *ctx) |
| 236 | { |
| 237 | weston_log("Assert failure in %s:%d, %s: '%s'\n", |
| 238 | file, line, func, cond); |
| 239 | wl_resource_post_error(ctx->runner_resource, |
| 240 | WESTON_TEST_RUNNER_ERROR_TEST_FAILED, |
| 241 | "Assert failure in %s:%d, %s: '%s'\n", |
| 242 | file, line, func, cond); |
| 243 | } |
| 244 | |
| 245 | #define runner_assert(cond) ({ \ |
| 246 | bool b_ = (cond); \ |
| 247 | if (!b_) \ |
| 248 | runner_assert_fail(#cond, __FILE__, __LINE__, \ |
| 249 | __func__, ctx); \ |
| 250 | b_; \ |
| 251 | }) |
| 252 | |
| 253 | #define runner_assert_or_return(cond) do { \ |
| 254 | bool b_ = (cond); \ |
| 255 | if (!b_) { \ |
| 256 | runner_assert_fail(#cond, __FILE__, __LINE__, \ |
| 257 | __func__, ctx); \ |
| 258 | return; \ |
| 259 | } \ |
| 260 | } while (0) |
| 261 | |
| 262 | |
| 263 | /*************************** tests **********************************/ |
| 264 | |
| 265 | /* |
| 266 | * This is a controller module: a plugin to ivi-shell.so, i.e. a sub-plugin. |
| 267 | * This module is specially written to execute tests that target the |
| 268 | * ivi_layout API. |
| 269 | * |
| 270 | * This module is listed in TESTS in Makefile.am. weston-tests-env handles |
| 271 | * this module specially by loading it in ivi-shell. |
| 272 | * |
| 273 | * Once Weston init completes, this module launches one test program: |
| 274 | * ivi-layout.ivi (ivi_layout-test.c). That program uses the weston-test-runner |
| 275 | * framework to fork and exec each TEST() in ivi_layout-test.c with a fresh |
| 276 | * connection to the single compositor instance. |
| 277 | * |
| 278 | * Each TEST() in ivi_layout-test.c will bind to weston_test_runner global |
| 279 | * interface. A TEST() will set up the client state, and issue |
| 280 | * weston_test_runner.run request to execute the compositor-side of the test. |
| 281 | * |
| 282 | * The compositor-side parts of the tests are in this file. They are specified |
| 283 | * by RUNNER_TEST() macro, where the name argument matches the name string |
| 284 | * passed to weston_test_runner.run. |
| 285 | * |
| 286 | * A RUNNER_TEST() function simply returns when it succeeds. If it fails, |
| 287 | * a fatal protocol error is sent to the client from runner_assert() or |
| 288 | * runner_assert_or_return(). This module catches the test program exit |
| 289 | * code and passes it out of Weston to the test harness. |
| 290 | * |
| 291 | * A single TEST() in ivi_layout-test.c may use multiple RUNNER_TEST()s to |
| 292 | * achieve multiple test points over a client action sequence. |
| 293 | */ |
| 294 | |
| 295 | RUNNER_TEST(surface_create_p1) |
| 296 | { |
| 297 | const struct ivi_controller_interface *ctl = ctx->controller_interface; |
| 298 | struct ivi_layout_surface *ivisurf[2]; |
| 299 | uint32_t ivi_id; |
| 300 | |
| 301 | ivisurf[0] = ctl->get_surface_from_id(IVI_TEST_SURFACE_ID(0)); |
| 302 | runner_assert_or_return(ivisurf[0]); |
| 303 | |
| 304 | ivisurf[1] = ctl->get_surface_from_id(IVI_TEST_SURFACE_ID(1)); |
| 305 | runner_assert_or_return(ivisurf[1]); |
| 306 | |
| 307 | ivi_id = ctl->get_id_of_surface(ivisurf[0]); |
| 308 | runner_assert_or_return(ivi_id == IVI_TEST_SURFACE_ID(0)); |
| 309 | |
| 310 | ivi_id = ctl->get_id_of_surface(ivisurf[1]); |
| 311 | runner_assert_or_return(ivi_id == IVI_TEST_SURFACE_ID(1)); |
| 312 | } |
| 313 | |
| 314 | RUNNER_TEST(surface_create_p2) |
| 315 | { |
| 316 | const struct ivi_controller_interface *ctl = ctx->controller_interface; |
| 317 | struct ivi_layout_surface *ivisurf; |
| 318 | |
| 319 | /* the ivi_surface was destroyed by the client */ |
| 320 | ivisurf = ctl->get_surface_from_id(IVI_TEST_SURFACE_ID(0)); |
| 321 | runner_assert_or_return(ivisurf == NULL); |
| 322 | } |
| 323 | |
| 324 | RUNNER_TEST(surface_visibility) |
| 325 | { |
| 326 | const struct ivi_controller_interface *ctl = ctx->controller_interface; |
| 327 | struct ivi_layout_surface *ivisurf; |
| 328 | int32_t ret; |
| 329 | bool visibility; |
| 330 | const struct ivi_layout_surface_properties *prop; |
| 331 | |
| 332 | ivisurf = ctl->get_surface_from_id(IVI_TEST_SURFACE_ID(0)); |
| 333 | runner_assert_or_return(ivisurf); |
| 334 | |
| 335 | ret = ctl->surface_set_visibility(ivisurf, true); |
| 336 | runner_assert_or_return(ret == IVI_SUCCEEDED); |
| 337 | |
| 338 | ctl->commit_changes(); |
| 339 | |
| 340 | visibility = ctl->surface_get_visibility(ivisurf); |
| 341 | runner_assert_or_return(visibility == true); |
| 342 | |
| 343 | prop = ctl->get_properties_of_surface(ivisurf); |
| 344 | runner_assert_or_return(prop->visibility == true); |
| 345 | } |
| 346 | |
| 347 | RUNNER_TEST(surface_opacity) |
| 348 | { |
| 349 | const struct ivi_controller_interface *ctl = ctx->controller_interface; |
| 350 | struct ivi_layout_surface *ivisurf; |
| 351 | int32_t ret; |
| 352 | wl_fixed_t opacity; |
| 353 | const struct ivi_layout_surface_properties *prop; |
| 354 | |
| 355 | ivisurf = ctl->get_surface_from_id(IVI_TEST_SURFACE_ID(0)); |
| 356 | runner_assert_or_return(ivisurf); |
| 357 | |
| 358 | ret = ctl->surface_set_opacity(ivisurf, wl_fixed_from_double(0.5)); |
| 359 | runner_assert_or_return(ret == IVI_SUCCEEDED); |
| 360 | |
| 361 | ctl->commit_changes(); |
| 362 | |
| 363 | opacity = ctl->surface_get_opacity(ivisurf); |
| 364 | runner_assert_or_return(opacity == wl_fixed_from_double(0.5)); |
| 365 | |
| 366 | prop = ctl->get_properties_of_surface(ivisurf); |
| 367 | runner_assert_or_return(prop->opacity == wl_fixed_from_double(0.5)); |
| 368 | } |