client: Fix segmentation fault in the case weston-nested
[platform/upstream/weston.git] / tests / ivi_layout-test-plugin.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2013 DENSO CORPORATION
4  * Copyright © 2015 Collabora, Ltd.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27
28 #include "config.h"
29
30 #include <stdint.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include "compositor.h"
37 #include "compositor/weston.h"
38 #include "weston-test-server-protocol.h"
39 #include "ivi-test.h"
40 #include "ivi-shell/ivi-layout-export.h"
41 #include "shared/helpers.h"
42
43 struct test_context;
44
45 struct runner_test {
46         const char *name;
47         void (*run)(struct test_context *);
48 } __attribute__ ((aligned (32)));
49
50 #define RUNNER_TEST(name)                                       \
51         static void runner_func_##name(struct test_context *);  \
52                                                                 \
53         const struct runner_test runner_test_##name             \
54                 __attribute__ ((section ("test_section"))) =    \
55         {                                                       \
56                 #name, runner_func_##name                       \
57         };                                                      \
58                                                                 \
59         static void runner_func_##name(struct test_context *ctx)
60
61 extern const struct runner_test __start_test_section;
62 extern const struct runner_test __stop_test_section;
63
64 static const struct runner_test *
65 find_runner_test(const char *name)
66 {
67         const struct runner_test *t;
68
69         for (t = &__start_test_section; t < &__stop_test_section; t++) {
70                 if (strcmp(t->name, name) == 0)
71                         return t;
72         }
73
74         return NULL;
75 }
76
77 struct test_launcher {
78         struct weston_compositor *compositor;
79         char exe[2048];
80         struct weston_process process;
81         const struct ivi_layout_interface *layout_interface;
82 };
83
84 struct test_context {
85         const struct ivi_layout_interface *layout_interface;
86         struct wl_resource *runner_resource;
87         uint32_t user_flags;
88
89         struct wl_listener surface_property_changed;
90         struct wl_listener surface_created;
91         struct wl_listener surface_removed;
92         struct wl_listener surface_configured;
93 };
94
95 static struct test_context static_context;
96
97 static void
98 destroy_runner(struct wl_resource *resource)
99 {
100         assert(static_context.runner_resource == NULL ||
101                static_context.runner_resource == resource);
102
103         static_context.layout_interface = NULL;
104         static_context.runner_resource = NULL;
105 }
106
107 static void
108 runner_destroy_handler(struct wl_client *client, struct wl_resource *resource)
109 {
110         wl_resource_destroy(resource);
111 }
112
113 static void
114 runner_run_handler(struct wl_client *client, struct wl_resource *resource,
115                    const char *test_name)
116 {
117         struct test_launcher *launcher;
118         const struct runner_test *t;
119
120         assert(static_context.runner_resource == NULL ||
121                static_context.runner_resource == resource);
122
123         launcher = wl_resource_get_user_data(resource);
124         static_context.layout_interface = launcher->layout_interface;
125         static_context.runner_resource = resource;
126
127         t = find_runner_test(test_name);
128         if (!t) {
129                 weston_log("Error: runner test \"%s\" not found.\n",
130                            test_name);
131                 wl_resource_post_error(resource,
132                                        WESTON_TEST_RUNNER_ERROR_UNKNOWN_TEST,
133                                        "weston_test_runner: unknown: '%s'",
134                                        test_name);
135                 return;
136         }
137
138         weston_log("weston_test_runner.run(\"%s\")\n", test_name);
139
140         t->run(&static_context);
141
142         weston_test_runner_send_finished(resource);
143 }
144
145 static const struct weston_test_runner_interface runner_implementation = {
146         runner_destroy_handler,
147         runner_run_handler
148 };
149
150 static void
151 bind_runner(struct wl_client *client, void *data,
152             uint32_t version, uint32_t id)
153 {
154         struct test_launcher *launcher = data;
155         struct wl_resource *resource;
156
157         resource = wl_resource_create(client, &weston_test_runner_interface,
158                                       1, id);
159         if (!resource) {
160                 wl_client_post_no_memory(client);
161                 return;
162         }
163
164         wl_resource_set_implementation(resource, &runner_implementation,
165                                        launcher, destroy_runner);
166
167         if (static_context.runner_resource != NULL) {
168                 weston_log("test FATAL: "
169                            "attempting to run several tests in parallel.\n");
170                 wl_resource_post_error(resource,
171                                        WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
172                                        "attempt to run parallel tests");
173         }
174 }
175
176 static void
177 test_client_sigchld(struct weston_process *process, int status)
178 {
179         struct test_launcher *launcher =
180                 container_of(process, struct test_launcher, process);
181         struct weston_compositor *c = launcher->compositor;
182
183         /* Chain up from weston-test-runner's exit code so that automake
184          * knows the exit status and can report e.g. skipped tests. */
185         if (WIFEXITED(status))
186                 weston_compositor_exit_with_code(c, WEXITSTATUS(status));
187         else
188                 weston_compositor_exit_with_code(c, EXIT_FAILURE);
189 }
190
191 static void
192 idle_launch_client(void *data)
193 {
194         struct test_launcher *launcher = data;
195         pid_t pid;
196         sigset_t allsigs;
197
198         pid = fork();
199         if (pid == -1) {
200                 weston_log("fatal: failed to fork '%s': %m\n", launcher->exe);
201                 weston_compositor_exit_with_code(launcher->compositor,
202                                                  EXIT_FAILURE);
203                 return;
204         }
205
206         if (pid == 0) {
207                 sigfillset(&allsigs);
208                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
209                 execl(launcher->exe, launcher->exe, NULL);
210                 weston_log("compositor: executing '%s' failed: %m\n",
211                            launcher->exe);
212                 _exit(EXIT_FAILURE);
213         }
214
215         launcher->process.pid = pid;
216         launcher->process.cleanup = test_client_sigchld;
217         weston_watch_process(&launcher->process);
218 }
219
220 WL_EXPORT int
221 wet_module_init(struct weston_compositor *compositor,
222                        int *argc, char *argv[])
223 {
224         struct wl_event_loop *loop;
225         struct test_launcher *launcher;
226         const char *path;
227         const struct ivi_layout_interface *iface;
228
229         iface = ivi_layout_get_api(compositor);
230
231         if (!iface) {
232                 weston_log("fatal: cannot use ivi_layout_interface.\n");
233                 return -1;
234         }
235
236         path = getenv("WESTON_BUILD_DIR");
237         if (!path) {
238                 weston_log("test setup failure: WESTON_BUILD_DIR not set\n");
239                 return -1;
240         }
241
242         launcher = zalloc(sizeof *launcher);
243         if (!launcher)
244                 return -1;
245
246         launcher->compositor = compositor;
247         launcher->layout_interface = iface;
248         snprintf(launcher->exe, sizeof launcher->exe,
249                  "%s/ivi-layout.ivi", path);
250
251         if (wl_global_create(compositor->wl_display,
252                              &weston_test_runner_interface, 1,
253                              launcher, bind_runner) == NULL)
254                 return -1;
255
256         loop = wl_display_get_event_loop(compositor->wl_display);
257         wl_event_loop_add_idle(loop, idle_launch_client, launcher);
258
259         return 0;
260 }
261
262 static void
263 runner_assert_fail(const char *cond, const char *file, int line,
264                    const char *func, struct test_context *ctx)
265 {
266         weston_log("Assert failure in %s:%d, %s: '%s'\n",
267                    file, line, func, cond);
268
269         assert(ctx->runner_resource);
270         wl_resource_post_error(ctx->runner_resource,
271                                WESTON_TEST_RUNNER_ERROR_TEST_FAILED,
272                                "Assert failure in %s:%d, %s: '%s'\n",
273                                file, line, func, cond);
274 }
275
276 #define runner_assert(cond) ({                                  \
277         bool b_ = (cond);                                       \
278         if (!b_)                                                \
279                 runner_assert_fail(#cond, __FILE__, __LINE__,   \
280                                    __func__, ctx);              \
281         b_;                                                     \
282 })
283
284 #define runner_assert_or_return(cond) do {                      \
285         bool b_ = (cond);                                       \
286         if (!b_) {                                              \
287                 runner_assert_fail(#cond, __FILE__, __LINE__,   \
288                                    __func__, ctx);              \
289                 return;                                         \
290         }                                                       \
291 } while (0)
292
293
294 /*************************** tests **********************************/
295
296 /*
297  * This is a controller module: a plugin to ivi-shell.so, i.e. a sub-plugin.
298  * This module is specially written to execute tests that target the
299  * ivi_layout API.
300  *
301  * This module is listed in TESTS in Makefile.am. weston-tests-env handles
302  * this module specially by loading it in ivi-shell.
303  *
304  * Once Weston init completes, this module launches one test program:
305  * ivi-layout.ivi (ivi_layout-test.c). That program uses the weston-test-runner
306  * framework to fork and exec each TEST() in ivi_layout-test.c with a fresh
307  * connection to the single compositor instance.
308  *
309  * Each TEST() in ivi_layout-test.c will bind to weston_test_runner global
310  * interface. A TEST() will set up the client state, and issue
311  * weston_test_runner.run request to execute the compositor-side of the test.
312  *
313  * The compositor-side parts of the tests are in this file. They are specified
314  * by RUNNER_TEST() macro, where the name argument matches the name string
315  * passed to weston_test_runner.run.
316  *
317  * A RUNNER_TEST() function simply returns when it succeeds. If it fails,
318  * a fatal protocol error is sent to the client from runner_assert() or
319  * runner_assert_or_return(). This module catches the test program exit
320  * code and passes it out of Weston to the test harness.
321  *
322  * A single TEST() in ivi_layout-test.c may use multiple RUNNER_TEST()s to
323  * achieve multiple test points over a client action sequence.
324  */
325
326 RUNNER_TEST(surface_create_p1)
327 {
328         const struct ivi_layout_interface *lyt = ctx->layout_interface;
329         struct ivi_layout_surface *ivisurf[2];
330         uint32_t ivi_id;
331
332         ivisurf[0] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
333         runner_assert(ivisurf[0]);
334
335         ivisurf[1] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(1));
336         runner_assert(ivisurf[1]);
337
338         ivi_id = lyt->get_id_of_surface(ivisurf[0]);
339         runner_assert(ivi_id == IVI_TEST_SURFACE_ID(0));
340
341         ivi_id = lyt->get_id_of_surface(ivisurf[1]);
342         runner_assert(ivi_id == IVI_TEST_SURFACE_ID(1));
343 }
344
345 RUNNER_TEST(surface_create_p2)
346 {
347         const struct ivi_layout_interface *lyt = ctx->layout_interface;
348         struct ivi_layout_surface *ivisurf;
349
350         /* the ivi_surface was destroyed by the client */
351         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
352         runner_assert(ivisurf == NULL);
353 }
354
355 RUNNER_TEST(surface_visibility)
356 {
357         const struct ivi_layout_interface *lyt = ctx->layout_interface;
358         struct ivi_layout_surface *ivisurf;
359         int32_t ret;
360         const struct ivi_layout_surface_properties *prop;
361
362         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
363         runner_assert(ivisurf);
364
365         ret = lyt->surface_set_visibility(ivisurf, true);
366         runner_assert(ret == IVI_SUCCEEDED);
367
368         lyt->commit_changes();
369
370         prop = lyt->get_properties_of_surface(ivisurf);
371         runner_assert(prop->visibility == true);
372 }
373
374 RUNNER_TEST(surface_opacity)
375 {
376         const struct ivi_layout_interface *lyt = ctx->layout_interface;
377         struct ivi_layout_surface *ivisurf;
378         int32_t ret;
379         const struct ivi_layout_surface_properties *prop;
380
381         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
382         runner_assert(ivisurf);
383
384         prop = lyt->get_properties_of_surface(ivisurf);
385         runner_assert(prop->opacity == wl_fixed_from_double(1.0));
386
387         ret = lyt->surface_set_opacity(ivisurf, wl_fixed_from_double(0.5));
388         runner_assert(ret == IVI_SUCCEEDED);
389
390         runner_assert(prop->opacity == wl_fixed_from_double(1.0));
391
392         lyt->commit_changes();
393
394         runner_assert(prop->opacity == wl_fixed_from_double(0.5));
395 }
396
397 RUNNER_TEST(surface_dimension)
398 {
399         const struct ivi_layout_interface *lyt = ctx->layout_interface;
400         struct ivi_layout_surface *ivisurf;
401         const struct ivi_layout_surface_properties *prop;
402
403         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
404         runner_assert(ivisurf != NULL);
405
406         prop = lyt->get_properties_of_surface(ivisurf);
407         runner_assert_or_return(prop);
408         runner_assert(prop->dest_width == 1);
409         runner_assert(prop->dest_height == 1);
410
411         runner_assert(IVI_SUCCEEDED ==
412                       lyt->surface_set_destination_rectangle(ivisurf, prop->dest_x,
413                       prop->dest_y, 200, 300));
414
415         runner_assert(prop->dest_width == 1);
416         runner_assert(prop->dest_height == 1);
417
418         lyt->commit_changes();
419
420         prop = lyt->get_properties_of_surface(ivisurf);
421         runner_assert_or_return(prop);
422         runner_assert(prop->dest_width == 200);
423         runner_assert(prop->dest_height == 300);
424 }
425
426 RUNNER_TEST(surface_position)
427 {
428         const struct ivi_layout_interface *lyt = ctx->layout_interface;
429         struct ivi_layout_surface *ivisurf;
430         const struct ivi_layout_surface_properties *prop;
431
432         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
433         runner_assert(ivisurf != NULL);
434
435         prop = lyt->get_properties_of_surface(ivisurf);
436         runner_assert_or_return(prop);
437         runner_assert(prop->dest_x == 0);
438         runner_assert(prop->dest_y == 0);
439
440         runner_assert(lyt->surface_set_destination_rectangle(
441                       ivisurf, 20, 30,
442                       prop->dest_width, prop->dest_height) == IVI_SUCCEEDED);
443
444         runner_assert(prop->dest_x == 0);
445         runner_assert(prop->dest_y == 0);
446
447         lyt->commit_changes();
448
449         prop = lyt->get_properties_of_surface(ivisurf);
450         runner_assert_or_return(prop);
451         runner_assert(prop->dest_x == 20);
452         runner_assert(prop->dest_y == 30);
453 }
454
455 RUNNER_TEST(surface_destination_rectangle)
456 {
457         const struct ivi_layout_interface *lyt = ctx->layout_interface;
458         struct ivi_layout_surface *ivisurf;
459         const struct ivi_layout_surface_properties *prop;
460
461         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
462         runner_assert(ivisurf != NULL);
463
464         prop = lyt->get_properties_of_surface(ivisurf);
465         runner_assert_or_return(prop);
466         runner_assert(prop->dest_width == 1);
467         runner_assert(prop->dest_height == 1);
468         runner_assert(prop->dest_x == 0);
469         runner_assert(prop->dest_y == 0);
470
471         runner_assert(lyt->surface_set_destination_rectangle(
472                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
473
474         prop = lyt->get_properties_of_surface(ivisurf);
475         runner_assert_or_return(prop);
476         runner_assert(prop->dest_width == 1);
477         runner_assert(prop->dest_height == 1);
478         runner_assert(prop->dest_x == 0);
479         runner_assert(prop->dest_y == 0);
480
481         lyt->commit_changes();
482
483         prop = lyt->get_properties_of_surface(ivisurf);
484         runner_assert_or_return(prop);
485         runner_assert(prop->dest_width == 200);
486         runner_assert(prop->dest_height == 300);
487         runner_assert(prop->dest_x == 20);
488         runner_assert(prop->dest_y == 30);
489 }
490
491 RUNNER_TEST(surface_source_rectangle)
492 {
493         const struct ivi_layout_interface *lyt = ctx->layout_interface;
494         struct ivi_layout_surface *ivisurf;
495         const struct ivi_layout_surface_properties *prop;
496
497         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
498         runner_assert(ivisurf != NULL);
499
500         prop = lyt->get_properties_of_surface(ivisurf);
501         runner_assert_or_return(prop);
502         runner_assert(prop->source_width == 0);
503         runner_assert(prop->source_height == 0);
504         runner_assert(prop->source_x == 0);
505         runner_assert(prop->source_y == 0);
506
507         runner_assert(lyt->surface_set_source_rectangle(
508                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
509
510         prop = lyt->get_properties_of_surface(ivisurf);
511         runner_assert_or_return(prop);
512         runner_assert(prop->source_width == 0);
513         runner_assert(prop->source_height == 0);
514         runner_assert(prop->source_x == 0);
515         runner_assert(prop->source_y == 0);
516
517         lyt->commit_changes();
518
519         prop = lyt->get_properties_of_surface(ivisurf);
520         runner_assert_or_return(prop);
521         runner_assert(prop->source_width == 200);
522         runner_assert(prop->source_height == 300);
523         runner_assert(prop->source_x == 20);
524         runner_assert(prop->source_y == 30);
525 }
526
527 RUNNER_TEST(surface_bad_opacity)
528 {
529         const struct ivi_layout_interface *lyt = ctx->layout_interface;
530         struct ivi_layout_surface *ivisurf;
531         const struct ivi_layout_surface_properties *prop;
532
533         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
534         runner_assert(ivisurf != NULL);
535
536         runner_assert(lyt->surface_set_opacity(
537                       NULL, wl_fixed_from_double(0.3)) == IVI_FAILED);
538
539         runner_assert(lyt->surface_set_opacity(
540                       ivisurf, wl_fixed_from_double(0.3)) == IVI_SUCCEEDED);
541
542         runner_assert(lyt->surface_set_opacity(
543                       ivisurf, wl_fixed_from_double(-1)) == IVI_FAILED);
544
545         lyt->commit_changes();
546
547         prop = lyt->get_properties_of_surface(ivisurf);
548         runner_assert(prop->opacity == wl_fixed_from_double(0.3));
549
550         runner_assert(lyt->surface_set_opacity(
551                       ivisurf, wl_fixed_from_double(1.1)) == IVI_FAILED);
552
553         lyt->commit_changes();
554
555         runner_assert(prop->opacity == wl_fixed_from_double(0.3));
556
557         runner_assert(lyt->surface_set_opacity(
558                       NULL, wl_fixed_from_double(0.5)) == IVI_FAILED);
559
560         lyt->commit_changes();
561 }
562
563 RUNNER_TEST(surface_on_many_layer)
564 {
565         const struct ivi_layout_interface *lyt = ctx->layout_interface;
566         struct ivi_layout_surface *ivisurf;
567         struct ivi_layout_layer *ivilayers[IVI_TEST_LAYER_COUNT] = {};
568         struct ivi_layout_layer **array;
569         int32_t length = 0;
570         uint32_t i;
571
572         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
573         runner_assert(ivisurf != NULL);
574
575         for (i = 0; i < IVI_TEST_LAYER_COUNT; i++) {
576                 ivilayers[i] = lyt->layer_create_with_dimension(
577                                 IVI_TEST_LAYER_ID(i), 200, 300);
578                 runner_assert(lyt->layer_add_surface(
579                                 ivilayers[i], ivisurf) == IVI_SUCCEEDED);
580         }
581
582         lyt->commit_changes();
583
584         runner_assert(lyt->get_layers_under_surface(
585                       ivisurf, &length, &array) == IVI_SUCCEEDED);
586         runner_assert(IVI_TEST_LAYER_COUNT == length);
587         for (i = 0; i < IVI_TEST_LAYER_COUNT; i++)
588                 runner_assert(array[i] == ivilayers[i]);
589
590         if (length > 0)
591                 free(array);
592
593         for (i = 0; i < IVI_TEST_LAYER_COUNT; i++)
594                 lyt->layer_remove_surface(ivilayers[i], ivisurf);
595
596         array = NULL;
597
598         lyt->commit_changes();
599
600         runner_assert(lyt->get_layers_under_surface(
601                       ivisurf, &length, &array) == IVI_SUCCEEDED);
602         runner_assert(length == 0 && array == NULL);
603
604         for (i = 0; i < IVI_TEST_LAYER_COUNT; i++)
605                 lyt->layer_destroy(ivilayers[i]);
606 }
607
608 RUNNER_TEST(ivi_layout_commit_changes)
609 {
610         const struct ivi_layout_interface *lyt = ctx->layout_interface;
611
612         lyt->commit_changes();
613 }
614
615 RUNNER_TEST(commit_changes_after_visibility_set_surface_destroy)
616 {
617         const struct ivi_layout_interface *lyt = ctx->layout_interface;
618         struct ivi_layout_surface *ivisurf;
619
620         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
621         runner_assert(ivisurf != NULL);
622         runner_assert(lyt->surface_set_visibility(
623                       ivisurf, true) == IVI_SUCCEEDED);
624 }
625
626 RUNNER_TEST(commit_changes_after_opacity_set_surface_destroy)
627 {
628         const struct ivi_layout_interface *lyt = ctx->layout_interface;
629         struct ivi_layout_surface *ivisurf;
630
631         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
632         runner_assert(ivisurf != NULL);
633         runner_assert(lyt->surface_set_opacity(
634                       ivisurf, wl_fixed_from_double(0.5)) == IVI_SUCCEEDED);
635 }
636
637 RUNNER_TEST(commit_changes_after_source_rectangle_set_surface_destroy)
638 {
639         const struct ivi_layout_interface *lyt = ctx->layout_interface;
640         struct ivi_layout_surface *ivisurf;
641
642         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
643         runner_assert(ivisurf != NULL);
644         runner_assert(lyt->surface_set_source_rectangle(
645                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
646 }
647
648 RUNNER_TEST(commit_changes_after_destination_rectangle_set_surface_destroy)
649 {
650         const struct ivi_layout_interface *lyt = ctx->layout_interface;
651         struct ivi_layout_surface *ivisurf;
652
653         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
654         runner_assert(ivisurf != NULL);
655         runner_assert(lyt->surface_set_destination_rectangle(
656                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
657 }
658
659 RUNNER_TEST(get_surface_after_destroy_surface)
660 {
661         const struct ivi_layout_interface *lyt = ctx->layout_interface;
662         struct ivi_layout_surface *ivisurf;
663
664         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
665         runner_assert(ivisurf == NULL);
666 }
667
668 RUNNER_TEST(layer_render_order)
669 {
670         const struct ivi_layout_interface *lyt = ctx->layout_interface;
671         struct ivi_layout_layer *ivilayer;
672         struct ivi_layout_surface *ivisurfs[IVI_TEST_SURFACE_COUNT] = {};
673         struct ivi_layout_surface **array;
674         int32_t length = 0;
675         uint32_t i;
676
677         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
678
679         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
680                 ivisurfs[i] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(i));
681
682         runner_assert(lyt->layer_set_render_order(
683                       ivilayer, ivisurfs, IVI_TEST_SURFACE_COUNT) == IVI_SUCCEEDED);
684
685         lyt->commit_changes();
686
687         runner_assert(lyt->get_surfaces_on_layer(
688                       ivilayer, &length, &array) == IVI_SUCCEEDED);
689         runner_assert(IVI_TEST_SURFACE_COUNT == length);
690         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
691                 runner_assert(array[i] == ivisurfs[i]);
692
693         if (length > 0)
694                 free(array);
695
696         runner_assert(lyt->layer_set_render_order(
697                       ivilayer, NULL, 0) == IVI_SUCCEEDED);
698
699         array = NULL;
700
701         lyt->commit_changes();
702
703         runner_assert(lyt->get_surfaces_on_layer(
704                       ivilayer, &length, &array) == IVI_SUCCEEDED);
705         runner_assert(length == 0 && array == NULL);
706
707         lyt->layer_destroy(ivilayer);
708 }
709
710 RUNNER_TEST(test_layer_render_order_destroy_one_surface_p1)
711 {
712         const struct ivi_layout_interface *lyt = ctx->layout_interface;
713         struct ivi_layout_layer *ivilayer;
714         struct ivi_layout_surface *ivisurfs[IVI_TEST_SURFACE_COUNT] = {};
715         struct ivi_layout_surface **array;
716         int32_t length = 0;
717         int32_t i;
718
719         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
720
721         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
722                 ivisurfs[i] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(i));
723
724         runner_assert(lyt->layer_set_render_order(
725                       ivilayer, ivisurfs, IVI_TEST_SURFACE_COUNT) == IVI_SUCCEEDED);
726
727         lyt->commit_changes();
728
729         runner_assert(lyt->get_surfaces_on_layer(
730                       ivilayer, &length, &array) == IVI_SUCCEEDED);
731         runner_assert(IVI_TEST_SURFACE_COUNT == length);
732         for (i = 0; i < length; i++)
733                 runner_assert(array[i] == ivisurfs[i]);
734
735         if (length > 0)
736                 free(array);
737 }
738
739 RUNNER_TEST(test_layer_render_order_destroy_one_surface_p2)
740 {
741         const struct ivi_layout_interface *lyt = ctx->layout_interface;
742         struct ivi_layout_layer *ivilayer;
743         struct ivi_layout_surface *ivisurfs[2] = {};
744         struct ivi_layout_surface **array;
745         int32_t length = 0;
746         int32_t i;
747
748         ivilayer = lyt->get_layer_from_id(IVI_TEST_LAYER_ID(0));
749         ivisurfs[0] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
750         ivisurfs[1] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(2));
751
752         runner_assert(lyt->get_surfaces_on_layer(
753                       ivilayer, &length, &array) == IVI_SUCCEEDED);
754         runner_assert(2 == length);
755         for (i = 0; i < length; i++)
756                 runner_assert(array[i] == ivisurfs[i]);
757
758         if (length > 0)
759                 free(array);
760
761         lyt->layer_destroy(ivilayer);
762 }
763
764 RUNNER_TEST(layer_bad_render_order)
765 {
766         const struct ivi_layout_interface *lyt = ctx->layout_interface;
767         struct ivi_layout_layer *ivilayer;
768         struct ivi_layout_surface *ivisurfs[IVI_TEST_SURFACE_COUNT] = {};
769         struct ivi_layout_surface **array = NULL;
770         int32_t length = 0;
771         uint32_t i;
772
773         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
774
775         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
776                 ivisurfs[i] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(i));
777
778         runner_assert(lyt->layer_set_render_order(
779                       NULL, ivisurfs, IVI_TEST_SURFACE_COUNT) == IVI_FAILED);
780
781         lyt->commit_changes();
782
783         runner_assert(lyt->get_surfaces_on_layer(
784                       NULL, &length, &array) == IVI_FAILED);
785         runner_assert(length == 0 && array == NULL);
786
787         runner_assert(lyt->get_surfaces_on_layer(
788                       ivilayer, NULL, &array) == IVI_FAILED);
789         runner_assert(array == NULL);
790
791         runner_assert(lyt->get_surfaces_on_layer(
792                       ivilayer, &length, NULL) == IVI_FAILED);
793         runner_assert(length == 0);
794
795         lyt->layer_destroy(ivilayer);
796 }
797
798 RUNNER_TEST(layer_add_surfaces)
799 {
800         const struct ivi_layout_interface *lyt = ctx->layout_interface;
801         struct ivi_layout_layer *ivilayer;
802         struct ivi_layout_surface *ivisurfs[IVI_TEST_SURFACE_COUNT] = {};
803         struct ivi_layout_surface **array;
804         int32_t length = 0;
805         uint32_t i;
806
807         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
808
809         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++) {
810                 ivisurfs[i] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(i));
811                 runner_assert(lyt->layer_add_surface(
812                                       ivilayer, ivisurfs[i]) == IVI_SUCCEEDED);
813         }
814
815         lyt->commit_changes();
816
817         runner_assert(lyt->get_surfaces_on_layer(
818                       ivilayer, &length, &array) == IVI_SUCCEEDED);
819         runner_assert(IVI_TEST_SURFACE_COUNT == length);
820         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
821                 runner_assert(array[i] == ivisurfs[i]);
822
823         if (length > 0)
824                 free(array);
825
826         runner_assert(lyt->layer_set_render_order(
827                       ivilayer, NULL, 0) == IVI_SUCCEEDED);
828
829         for (i = IVI_TEST_SURFACE_COUNT; i-- > 0;)
830                 runner_assert(lyt->layer_add_surface(
831                                       ivilayer, ivisurfs[i]) == IVI_SUCCEEDED);
832
833         lyt->commit_changes();
834
835         runner_assert(lyt->get_surfaces_on_layer(
836                       ivilayer, &length, &array) == IVI_SUCCEEDED);
837         runner_assert(IVI_TEST_SURFACE_COUNT == length);
838         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
839                 runner_assert(array[i] == ivisurfs[IVI_TEST_SURFACE_COUNT - (i + 1)]);
840
841         if (length > 0)
842                 free(array);
843
844         lyt->layer_destroy(ivilayer);
845 }
846
847 RUNNER_TEST(commit_changes_after_render_order_set_surface_destroy)
848 {
849         const struct ivi_layout_interface *lyt = ctx->layout_interface;
850         struct ivi_layout_layer *ivilayer;
851         struct ivi_layout_surface *ivisurfs[IVI_TEST_SURFACE_COUNT] = {};
852         int i;
853
854         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
855
856         for (i = 0; i < IVI_TEST_SURFACE_COUNT; i++)
857                 ivisurfs[i] = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(i));
858
859         runner_assert(lyt->layer_set_render_order(
860                       ivilayer, ivisurfs, IVI_TEST_SURFACE_COUNT) == IVI_SUCCEEDED);
861 }
862
863 RUNNER_TEST(cleanup_layer)
864 {
865         const struct ivi_layout_interface *lyt = ctx->layout_interface;
866         struct ivi_layout_layer *ivilayer;
867
868         ivilayer = lyt->get_layer_from_id(IVI_TEST_LAYER_ID(0));
869         lyt->layer_destroy(ivilayer);
870 }
871
872 static void
873 test_surface_properties_changed_notification_callback(struct wl_listener *listener, void *data)
874
875 {
876         struct test_context *ctx =
877                         container_of(listener, struct test_context,
878                                         surface_property_changed);
879         const struct ivi_layout_interface *lyt = ctx->layout_interface;
880         struct ivi_layout_surface *ivisurf = data;
881
882         runner_assert_or_return(lyt->get_id_of_surface(ivisurf) == IVI_TEST_SURFACE_ID(0));
883
884         ctx->user_flags = 1;
885 }
886
887 RUNNER_TEST(surface_properties_changed_notification)
888 {
889         const struct ivi_layout_interface *lyt = ctx->layout_interface;
890         const uint32_t id_surface = IVI_TEST_SURFACE_ID(0);
891         struct ivi_layout_surface *ivisurf;
892
893         ctx->user_flags = 0;
894
895         ivisurf = lyt->get_surface_from_id(id_surface);
896         runner_assert(ivisurf != NULL);
897
898         ctx->surface_property_changed.notify = test_surface_properties_changed_notification_callback;
899
900         runner_assert(lyt->surface_add_listener(
901                       ivisurf, &ctx->surface_property_changed) == IVI_SUCCEEDED);
902
903         lyt->commit_changes();
904
905         runner_assert(ctx->user_flags == 0);
906
907         runner_assert(lyt->surface_set_destination_rectangle(
908                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
909
910         lyt->commit_changes();
911
912         runner_assert(ctx->user_flags == 1);
913
914         ctx->user_flags = 0;
915         runner_assert(lyt->surface_set_destination_rectangle(
916                       ivisurf, 20, 30, 200, 300) == IVI_SUCCEEDED);
917
918         lyt->commit_changes();
919
920         runner_assert(ctx->user_flags == 0);
921
922         // remove surface property changed listener.
923         wl_list_remove(&ctx->surface_property_changed.link);
924         ctx->user_flags = 0;
925         runner_assert(lyt->surface_set_destination_rectangle(
926                       ivisurf, 40, 50, 400, 500) == IVI_SUCCEEDED);
927
928         lyt->commit_changes();
929
930         runner_assert(ctx->user_flags == 0);
931 }
932
933 static void
934 test_surface_configure_notification_callback(struct wl_listener *listener, void *data)
935 {
936         struct test_context *ctx =
937                         container_of(listener, struct test_context,
938                                         surface_configured);
939         const struct ivi_layout_interface *lyt = ctx->layout_interface;
940         struct ivi_layout_surface *ivisurf = data;
941
942         runner_assert_or_return(lyt->get_id_of_surface(ivisurf) == IVI_TEST_SURFACE_ID(0));
943
944         ctx->user_flags = 1;
945 }
946
947 RUNNER_TEST(surface_configure_notification_p1)
948 {
949         const struct ivi_layout_interface *lyt = ctx->layout_interface;
950
951         ctx->surface_configured.notify = test_surface_configure_notification_callback;
952         runner_assert(IVI_SUCCEEDED == lyt->add_listener_configure_surface(&ctx->surface_configured));
953         lyt->commit_changes();
954
955         ctx->user_flags = 0;
956 }
957
958 RUNNER_TEST(surface_configure_notification_p2)
959 {
960         runner_assert(ctx->user_flags == 1);
961
962         // remove surface configured listener.
963         wl_list_remove(&ctx->surface_configured.link);
964         ctx->user_flags = 0;
965 }
966
967 RUNNER_TEST(surface_configure_notification_p3)
968 {
969         const struct ivi_layout_interface *lyt = ctx->layout_interface;
970
971         lyt->commit_changes();
972         runner_assert(ctx->user_flags == 0);
973 }
974
975 static void
976 test_surface_create_notification_callback(struct wl_listener *listener, void *data)
977 {
978         struct test_context *ctx =
979                         container_of(listener, struct test_context,
980                                         surface_created);
981         const struct ivi_layout_interface *lyt = ctx->layout_interface;
982         struct ivi_layout_surface *ivisurf = data;
983
984         runner_assert_or_return(lyt->get_id_of_surface(ivisurf) == IVI_TEST_SURFACE_ID(0));
985
986         ctx->user_flags = 1;
987 }
988
989 RUNNER_TEST(surface_create_notification_p1)
990 {
991         const struct ivi_layout_interface *lyt = ctx->layout_interface;
992
993         ctx->surface_created.notify = test_surface_create_notification_callback;
994         runner_assert(lyt->add_listener_create_surface(
995                         &ctx->surface_created) == IVI_SUCCEEDED);
996
997         ctx->user_flags = 0;
998 }
999
1000 RUNNER_TEST(surface_create_notification_p2)
1001 {
1002         runner_assert(ctx->user_flags == 1);
1003
1004         // remove surface created listener.
1005         wl_list_remove(&ctx->surface_created.link);
1006         ctx->user_flags = 0;
1007 }
1008
1009 RUNNER_TEST(surface_create_notification_p3)
1010 {
1011         runner_assert(ctx->user_flags == 0);
1012 }
1013
1014 static void
1015 test_surface_remove_notification_callback(struct wl_listener *listener, void *data)
1016 {
1017         struct test_context *ctx =
1018                         container_of(listener, struct test_context,
1019                                         surface_removed);
1020         const struct ivi_layout_interface *lyt = ctx->layout_interface;
1021         struct ivi_layout_surface *ivisurf = data;
1022
1023         runner_assert_or_return(lyt->get_id_of_surface(ivisurf) == IVI_TEST_SURFACE_ID(0));
1024
1025         ctx->user_flags = 1;
1026 }
1027
1028 RUNNER_TEST(surface_remove_notification_p1)
1029 {
1030         const struct ivi_layout_interface *lyt = ctx->layout_interface;
1031
1032         ctx->surface_removed.notify = test_surface_remove_notification_callback;
1033         runner_assert(lyt->add_listener_remove_surface(&ctx->surface_removed)
1034                         == IVI_SUCCEEDED);
1035
1036         ctx->user_flags = 0;
1037 }
1038
1039 RUNNER_TEST(surface_remove_notification_p2)
1040 {
1041         runner_assert(ctx->user_flags == 1);
1042
1043         // remove surface removed listener.
1044         wl_list_remove(&ctx->surface_removed.link);
1045         ctx->user_flags = 0;
1046 }
1047
1048 RUNNER_TEST(surface_remove_notification_p3)
1049 {
1050         runner_assert(ctx->user_flags == 0);
1051 }
1052
1053 static void
1054 test_surface_bad_properties_changed_notification_callback(struct wl_listener *listener, void *data)
1055 {
1056 }
1057
1058 RUNNER_TEST(surface_bad_properties_changed_notification)
1059 {
1060         const struct ivi_layout_interface *lyt = ctx->layout_interface;
1061         struct ivi_layout_surface *ivisurf;
1062
1063         ivisurf = lyt->get_surface_from_id(IVI_TEST_SURFACE_ID(0));
1064         runner_assert(ivisurf != NULL);
1065
1066         ctx->surface_property_changed.notify = test_surface_bad_properties_changed_notification_callback;
1067
1068         runner_assert(lyt->surface_add_listener(
1069                       NULL, &ctx->surface_property_changed) == IVI_FAILED);
1070         runner_assert(lyt->surface_add_listener(
1071                       ivisurf, NULL) == IVI_FAILED);
1072 }