client: Fix segmentation fault in the case weston-nested
[platform/upstream/weston.git] / tests / ivi_layout-internal-test.c
1 /*
2  * Copyright © 2013 DENSO CORPORATION
3  * Copyright © 2015 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28
29 #include <unistd.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34
35 #include "compositor.h"
36 #include "compositor/weston.h"
37 #include "ivi-shell/ivi-layout-export.h"
38 #include "ivi-shell/ivi-layout-private.h"
39 #include "ivi-test.h"
40 #include "shared/helpers.h"
41
42 struct test_context {
43         struct weston_compositor *compositor;
44         const struct ivi_layout_interface *layout_interface;
45         uint32_t user_flags;
46
47         struct wl_listener layer_property_changed;
48         struct wl_listener layer_created;
49         struct wl_listener layer_removed;
50 };
51
52 static void
53 iassert_fail(const char *cond, const char *file, int line,
54              const char *func, struct test_context *ctx)
55 {
56         weston_log("Assert failure in %s:%d, %s: '%s'\n",
57                    file, line, func, cond);
58         weston_compositor_exit_with_code(ctx->compositor, EXIT_FAILURE);
59 }
60
61 #define iassert(cond) ({                                                \
62         bool b_ = (cond);                                               \
63         if (!b_)                                                        \
64                 iassert_fail(#cond, __FILE__, __LINE__, __func__, ctx); \
65         b_;                                                             \
66 })
67
68 /************************ tests begin ******************************/
69
70 /*
71  * These are all internal ivi_layout API tests that do not require
72  * any client objects.
73  */
74 static void
75 test_surface_bad_visibility(struct test_context *ctx)
76 {
77         const struct ivi_layout_interface *lyt = ctx->layout_interface;
78
79         iassert(lyt->surface_set_visibility(NULL, true) == IVI_FAILED);
80
81         lyt->commit_changes();
82 }
83
84 static void
85 test_surface_bad_destination_rectangle(struct test_context *ctx)
86 {
87         const struct ivi_layout_interface *lyt = ctx->layout_interface;
88
89         iassert(lyt->surface_set_destination_rectangle(NULL, 20, 30, 200, 300) == IVI_FAILED);
90 }
91
92 static void
93 test_surface_bad_source_rectangle(struct test_context *ctx)
94 {
95         const struct ivi_layout_interface *lyt = ctx->layout_interface;
96
97         iassert(lyt->surface_set_source_rectangle(NULL, 20, 30, 200, 300) == IVI_FAILED);
98 }
99
100 static void
101 test_surface_bad_properties(struct test_context *ctx)
102 {
103         const struct ivi_layout_interface *lyt = ctx->layout_interface;
104
105         iassert(lyt->get_properties_of_surface(NULL) == NULL);
106 }
107
108 static void
109 test_layer_create(struct test_context *ctx)
110 {
111         const struct ivi_layout_interface *lyt = ctx->layout_interface;
112         uint32_t id1;
113         uint32_t id2;
114         struct ivi_layout_layer *ivilayer;
115         struct ivi_layout_layer *new_ivilayer;
116
117         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
118         iassert(ivilayer != NULL);
119
120         iassert(IVI_TEST_LAYER_ID(0) == lyt->get_id_of_layer(ivilayer));
121
122         new_ivilayer = lyt->get_layer_from_id(IVI_TEST_LAYER_ID(0));
123         iassert(ivilayer == new_ivilayer);
124
125         id1 = lyt->get_id_of_layer(ivilayer);
126         id2 = lyt->get_id_of_layer(new_ivilayer);
127         iassert(id1 == id2);
128
129         lyt->layer_destroy(ivilayer);
130         iassert(lyt->get_layer_from_id(IVI_TEST_LAYER_ID(0)) == NULL);
131 }
132
133 static void
134 test_layer_visibility(struct test_context *ctx)
135 {
136         const struct ivi_layout_interface *lyt = ctx->layout_interface;
137         struct ivi_layout_layer *ivilayer;
138         const struct ivi_layout_layer_properties *prop;
139
140         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
141         iassert(ivilayer != NULL);
142
143         prop = lyt->get_properties_of_layer(ivilayer);
144
145         iassert(prop->visibility == false);
146
147         iassert(lyt->layer_set_visibility(ivilayer, true) == IVI_SUCCEEDED);
148
149         iassert(prop->visibility == false);
150
151         lyt->commit_changes();
152
153         iassert(prop->visibility == true);
154
155         lyt->layer_destroy(ivilayer);
156 }
157
158 static void
159 test_layer_opacity(struct test_context *ctx)
160 {
161         const struct ivi_layout_interface *lyt = ctx->layout_interface;
162         struct ivi_layout_layer *ivilayer;
163         const struct ivi_layout_layer_properties *prop;
164
165         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
166         iassert(ivilayer != NULL);
167
168         prop = lyt->get_properties_of_layer(ivilayer);
169         iassert(prop->opacity == wl_fixed_from_double(1.0));
170
171         iassert(lyt->layer_set_opacity(
172                 ivilayer, wl_fixed_from_double(0.5)) == IVI_SUCCEEDED);
173
174         iassert(prop->opacity == wl_fixed_from_double(1.0));
175
176         lyt->commit_changes();
177
178         iassert(prop->opacity == wl_fixed_from_double(0.5));
179
180         lyt->layer_destroy(ivilayer);
181 }
182
183 static void
184 test_layer_dimension(struct test_context *ctx)
185 {
186         const struct ivi_layout_interface *lyt = ctx->layout_interface;
187         struct ivi_layout_layer *ivilayer;
188         const struct ivi_layout_layer_properties *prop;
189
190         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
191         iassert(ivilayer != NULL);
192
193         prop = lyt->get_properties_of_layer(ivilayer);
194         iassert(prop->dest_width == 200);
195         iassert(prop->dest_height == 300);
196
197         iassert(lyt->layer_set_destination_rectangle(ivilayer, prop->dest_x, prop->dest_y,
198                         400, 600) == IVI_SUCCEEDED);
199
200         iassert(prop->dest_width == 200);
201         iassert(prop->dest_height == 300);
202
203         lyt->commit_changes();
204
205         iassert(prop->dest_width == 400);
206         iassert(prop->dest_height == 600);
207
208         lyt->layer_destroy(ivilayer);
209 }
210
211 static void
212 test_layer_position(struct test_context *ctx)
213 {
214         const struct ivi_layout_interface *lyt = ctx->layout_interface;
215         struct ivi_layout_layer *ivilayer;
216         const struct ivi_layout_layer_properties *prop;
217
218         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
219         iassert(ivilayer != NULL);
220
221         prop = lyt->get_properties_of_layer(ivilayer);
222         iassert(prop->dest_x == 0);
223         iassert(prop->dest_y == 0);
224
225         iassert(lyt->layer_set_destination_rectangle(ivilayer, 20, 30,
226                         prop->dest_width, prop->dest_height) == IVI_SUCCEEDED);
227
228         iassert(prop->dest_x == 0);
229         iassert(prop->dest_y == 0);
230
231         lyt->commit_changes();
232
233         prop = lyt->get_properties_of_layer(ivilayer);
234         iassert(prop->dest_x == 20);
235         iassert(prop->dest_y == 30);
236
237         lyt->layer_destroy(ivilayer);
238 }
239
240 static void
241 test_layer_destination_rectangle(struct test_context *ctx)
242 {
243         const struct ivi_layout_interface *lyt = ctx->layout_interface;
244         struct ivi_layout_layer *ivilayer;
245         const struct ivi_layout_layer_properties *prop;
246
247         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
248         iassert(ivilayer != NULL);
249
250         prop = lyt->get_properties_of_layer(ivilayer);
251         iassert(prop->dest_width == 200);
252         iassert(prop->dest_height == 300);
253         iassert(prop->dest_x == 0);
254         iassert(prop->dest_y == 0);
255
256         iassert(lyt->layer_set_destination_rectangle(
257                 ivilayer, 20, 30, 400, 600) == IVI_SUCCEEDED);
258
259         prop = lyt->get_properties_of_layer(ivilayer);
260         iassert(prop->dest_width == 200);
261         iassert(prop->dest_height == 300);
262         iassert(prop->dest_x == 0);
263         iassert(prop->dest_y == 0);
264
265         lyt->commit_changes();
266
267         iassert(prop->dest_width == 400);
268         iassert(prop->dest_height == 600);
269         iassert(prop->dest_x == 20);
270         iassert(prop->dest_y == 30);
271
272         lyt->layer_destroy(ivilayer);
273 }
274
275 static void
276 test_layer_source_rectangle(struct test_context *ctx)
277 {
278         const struct ivi_layout_interface *lyt = ctx->layout_interface;
279         struct ivi_layout_layer *ivilayer;
280         const struct ivi_layout_layer_properties *prop;
281
282         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
283         iassert(ivilayer != NULL);
284
285         prop = lyt->get_properties_of_layer(ivilayer);
286         iassert(prop->source_width == 200);
287         iassert(prop->source_height == 300);
288         iassert(prop->source_x == 0);
289         iassert(prop->source_y == 0);
290
291         iassert(lyt->layer_set_source_rectangle(
292                 ivilayer, 20, 30, 400, 600) == IVI_SUCCEEDED);
293
294         prop = lyt->get_properties_of_layer(ivilayer);
295         iassert(prop->source_width == 200);
296         iassert(prop->source_height == 300);
297         iassert(prop->source_x == 0);
298         iassert(prop->source_y == 0);
299
300         lyt->commit_changes();
301
302         prop = lyt->get_properties_of_layer(ivilayer);
303         iassert(prop->source_width == 400);
304         iassert(prop->source_height == 600);
305         iassert(prop->source_x == 20);
306         iassert(prop->source_y == 30);
307
308         lyt->layer_destroy(ivilayer);
309 }
310
311 static void
312 test_layer_bad_remove(struct test_context *ctx)
313 {
314         const struct ivi_layout_interface *lyt = ctx->layout_interface;
315         lyt->layer_destroy(NULL);
316 }
317
318 static void
319 test_layer_bad_visibility(struct test_context *ctx)
320 {
321         const struct ivi_layout_interface *lyt = ctx->layout_interface;
322
323         iassert(lyt->layer_set_visibility(NULL, true) == IVI_FAILED);
324
325         lyt->commit_changes();
326 }
327
328 static void
329 test_layer_bad_opacity(struct test_context *ctx)
330 {
331         const struct ivi_layout_interface *lyt = ctx->layout_interface;
332         struct ivi_layout_layer *ivilayer;
333         const struct ivi_layout_layer_properties *prop;
334
335         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
336         iassert(ivilayer != NULL);
337
338         iassert(lyt->layer_set_opacity(
339                 NULL, wl_fixed_from_double(0.3)) == IVI_FAILED);
340
341         iassert(lyt->layer_set_opacity(
342                 ivilayer, wl_fixed_from_double(0.3)) == IVI_SUCCEEDED);
343
344         iassert(lyt->layer_set_opacity(
345                 ivilayer, wl_fixed_from_double(-1)) == IVI_FAILED);
346
347         lyt->commit_changes();
348
349         prop = lyt->get_properties_of_layer(ivilayer);
350         iassert(prop->opacity == wl_fixed_from_double(0.3));
351
352         iassert(lyt->layer_set_opacity(
353                 ivilayer, wl_fixed_from_double(1.1)) == IVI_FAILED);
354
355         lyt->commit_changes();
356
357         iassert(prop->opacity == wl_fixed_from_double(0.3));
358
359         iassert(lyt->layer_set_opacity(
360                 NULL, wl_fixed_from_double(0.5)) == IVI_FAILED);
361
362         lyt->commit_changes();
363
364         lyt->layer_destroy(ivilayer);
365 }
366
367 static void
368 test_layer_bad_destination_rectangle(struct test_context *ctx)
369 {
370         const struct ivi_layout_interface *lyt = ctx->layout_interface;
371
372         iassert(lyt->layer_set_destination_rectangle(
373                 NULL, 20, 30, 200, 300) == IVI_FAILED);
374 }
375
376 static void
377 test_layer_bad_source_rectangle(struct test_context *ctx)
378 {
379         const struct ivi_layout_interface *lyt = ctx->layout_interface;
380
381         iassert(lyt->layer_set_source_rectangle(
382                 NULL, 20, 30, 200, 300) == IVI_FAILED);
383 }
384
385 static void
386 test_layer_bad_properties(struct test_context *ctx)
387 {
388         const struct ivi_layout_interface *lyt = ctx->layout_interface;
389
390         iassert(lyt->get_properties_of_layer(NULL) == NULL);
391 }
392
393 static void
394 test_commit_changes_after_visibility_set_layer_destroy(struct test_context *ctx)
395 {
396         const struct ivi_layout_interface *lyt = ctx->layout_interface;
397         struct ivi_layout_layer *ivilayer;
398
399         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
400         iassert(ivilayer != NULL);
401
402         iassert(lyt->layer_set_visibility(ivilayer, true) == IVI_SUCCEEDED);
403         lyt->layer_destroy(ivilayer);
404         lyt->commit_changes();
405 }
406
407 static void
408 test_commit_changes_after_opacity_set_layer_destroy(struct test_context *ctx)
409 {
410         const struct ivi_layout_interface *lyt = ctx->layout_interface;
411         struct ivi_layout_layer *ivilayer;
412
413         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
414         iassert(ivilayer != NULL);
415
416         iassert(lyt->layer_set_opacity(
417                     ivilayer, wl_fixed_from_double(0.5)) == IVI_SUCCEEDED);
418         lyt->layer_destroy(ivilayer);
419         lyt->commit_changes();
420 }
421
422 static void
423 test_commit_changes_after_source_rectangle_set_layer_destroy(struct test_context *ctx)
424 {
425         const struct ivi_layout_interface *lyt = ctx->layout_interface;
426         struct ivi_layout_layer *ivilayer;
427
428         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
429         iassert(ivilayer != NULL);
430
431         iassert(lyt->layer_set_source_rectangle(
432                     ivilayer, 20, 30, 200, 300) == IVI_SUCCEEDED);
433         lyt->layer_destroy(ivilayer);
434         lyt->commit_changes();
435 }
436
437 static void
438 test_commit_changes_after_destination_rectangle_set_layer_destroy(struct test_context *ctx)
439 {
440         const struct ivi_layout_interface *lyt = ctx->layout_interface;
441         struct ivi_layout_layer *ivilayer;
442
443         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
444         iassert(ivilayer != NULL);
445
446         iassert(lyt->layer_set_destination_rectangle(
447                     ivilayer, 20, 30, 200, 300) == IVI_SUCCEEDED);
448         lyt->layer_destroy(ivilayer);
449         lyt->commit_changes();
450 }
451
452 static void
453 test_layer_create_duplicate(struct test_context *ctx)
454 {
455         const struct ivi_layout_interface *lyt = ctx->layout_interface;
456         struct ivi_layout_layer *ivilayer;
457         struct ivi_layout_layer *duplicatelayer;
458
459         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
460         iassert(ivilayer != NULL);
461
462         if (ivilayer != NULL)
463                 iassert(ivilayer->ref_count == 1);
464
465         duplicatelayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
466         iassert(ivilayer == duplicatelayer);
467
468         if (ivilayer != NULL)
469                 iassert(ivilayer->ref_count == 2);
470
471         lyt->layer_destroy(ivilayer);
472
473         if (ivilayer != NULL)
474                 iassert(ivilayer->ref_count == 1);
475
476         lyt->layer_destroy(ivilayer);
477 }
478
479 static void
480 test_get_layer_after_destory_layer(struct test_context *ctx)
481 {
482         const struct ivi_layout_interface *lyt = ctx->layout_interface;
483         struct ivi_layout_layer *ivilayer;
484
485         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
486         iassert(ivilayer != NULL);
487
488         lyt->layer_destroy(ivilayer);
489
490         ivilayer = lyt->get_layer_from_id(IVI_TEST_LAYER_ID(0));
491         iassert(ivilayer == NULL);
492 }
493
494 static void
495 test_screen_render_order(struct test_context *ctx)
496 {
497 #define LAYER_NUM (3)
498         const struct ivi_layout_interface *lyt = ctx->layout_interface;
499         struct weston_output *output;
500         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
501         struct ivi_layout_layer **array;
502         int32_t length = 0;
503         uint32_t i;
504
505         if (!iassert(!wl_list_empty(&ctx->compositor->output_list)))
506                 return;
507
508         output = wl_container_of(ctx->compositor->output_list.next, output, link);
509
510         for (i = 0; i < LAYER_NUM; i++)
511                 ivilayers[i] = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(i), 200, 300);
512
513         iassert(lyt->screen_set_render_order(output, ivilayers, LAYER_NUM) == IVI_SUCCEEDED);
514
515         lyt->commit_changes();
516
517         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
518         iassert(length == LAYER_NUM);
519         for (i = 0; i < LAYER_NUM; i++)
520                 iassert(array[i] == ivilayers[i]);
521
522         if (length > 0)
523                 free(array);
524
525         array = NULL;
526
527         iassert(lyt->screen_set_render_order(output, NULL, 0) == IVI_SUCCEEDED);
528
529         lyt->commit_changes();
530
531         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
532         iassert(length == 0 && array == NULL);
533
534         for (i = 0; i < LAYER_NUM; i++)
535                 lyt->layer_destroy(ivilayers[i]);
536
537 #undef LAYER_NUM
538 }
539
540 static void
541 test_screen_bad_render_order(struct test_context *ctx)
542 {
543 #define LAYER_NUM (3)
544         const struct ivi_layout_interface *lyt = ctx->layout_interface;
545         struct weston_output *output;
546         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
547         struct ivi_layout_layer **array;
548         int32_t length = 0;
549         uint32_t i;
550
551         if (!iassert(!wl_list_empty(&ctx->compositor->output_list)))
552                 return;
553
554         output = wl_container_of(ctx->compositor->output_list.next, output, link);
555
556         for (i = 0; i < LAYER_NUM; i++)
557                 ivilayers[i] = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(i), 200, 300);
558
559         iassert(lyt->screen_set_render_order(NULL, ivilayers, LAYER_NUM) == IVI_FAILED);
560
561         lyt->commit_changes();
562
563         iassert(lyt->get_layers_on_screen(NULL, &length, &array) == IVI_FAILED);
564         iassert(lyt->get_layers_on_screen(output, NULL, &array) == IVI_FAILED);
565         iassert(lyt->get_layers_on_screen(output, &length, NULL) == IVI_FAILED);
566
567         for (i = 0; i < LAYER_NUM; i++)
568                 lyt->layer_destroy(ivilayers[i]);
569
570 #undef LAYER_NUM
571 }
572
573 static void
574 test_screen_add_layers(struct test_context *ctx)
575 {
576 #define LAYER_NUM (3)
577         const struct ivi_layout_interface *lyt = ctx->layout_interface;
578         struct weston_output *output;
579         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
580         struct ivi_layout_layer **array;
581         int32_t length = 0;
582         uint32_t i;
583
584         if (!iassert(!wl_list_empty(&ctx->compositor->output_list)))
585                 return;
586
587         output = wl_container_of(ctx->compositor->output_list.next, output, link);
588
589         for (i = 0; i < LAYER_NUM; i++) {
590                 ivilayers[i] = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(i), 200, 300);
591                 iassert(lyt->screen_add_layer(output, ivilayers[i]) == IVI_SUCCEEDED);
592         }
593
594         lyt->commit_changes();
595
596         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
597         iassert(length == LAYER_NUM);
598         for (i = 0; i < (uint32_t)length; i++)
599                 iassert(array[i] == ivilayers[i]);
600
601         if (length > 0)
602                 free(array);
603
604         array = NULL;
605
606         iassert(lyt->screen_set_render_order(output, NULL, 0) == IVI_SUCCEEDED);
607         for (i = LAYER_NUM; i-- > 0;)
608                 iassert(lyt->screen_add_layer(output, ivilayers[i]) == IVI_SUCCEEDED);
609
610         lyt->commit_changes();
611
612         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
613         iassert(length == LAYER_NUM);
614         for (i = 0; i < (uint32_t)length; i++)
615                 iassert(array[i] == ivilayers[LAYER_NUM - (i + 1)]);
616
617         if (length > 0)
618                 free(array);
619
620         for (i = 0; i < LAYER_NUM; i++)
621                 lyt->layer_destroy(ivilayers[i]);
622
623 #undef LAYER_NUM
624 }
625
626 static void
627 test_screen_remove_layer(struct test_context *ctx)
628 {
629         const struct ivi_layout_interface *lyt = ctx->layout_interface;
630         struct ivi_layout_layer *ivilayer;
631         struct weston_output *output;
632         struct ivi_layout_layer **array;
633         int32_t length = 0;
634
635         if (wl_list_empty(&ctx->compositor->output_list))
636                 return;
637
638         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
639         iassert(ivilayer != NULL);
640
641         output = wl_container_of(ctx->compositor->output_list.next, output, link);
642
643         iassert(lyt->screen_add_layer(output, ivilayer) == IVI_SUCCEEDED);
644         lyt->commit_changes();
645
646         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
647         iassert(length == 1);
648         iassert(array[0] == ivilayer);
649
650         iassert(lyt->screen_remove_layer(output, ivilayer) == IVI_SUCCEEDED);
651         lyt->commit_changes();
652
653         if (length > 0)
654                 free(array);
655
656         array = NULL;
657
658         iassert(lyt->get_layers_on_screen(output, &length, &array) == IVI_SUCCEEDED);
659         iassert(length == 0);
660         iassert(array == NULL);
661
662         lyt->layer_destroy(ivilayer);
663 }
664
665 static void
666 test_screen_bad_remove_layer(struct test_context *ctx)
667 {
668         const struct ivi_layout_interface *lyt = ctx->layout_interface;
669         struct ivi_layout_layer *ivilayer;
670         struct weston_output *output;
671
672         if (wl_list_empty(&ctx->compositor->output_list))
673                 return;
674
675         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
676         iassert(ivilayer != NULL);
677
678         output = wl_container_of(ctx->compositor->output_list.next, output, link);
679
680         iassert(lyt->screen_remove_layer(NULL, ivilayer) == IVI_FAILED);
681         lyt->commit_changes();
682
683         iassert(lyt->screen_remove_layer(output, NULL) == IVI_FAILED);
684         lyt->commit_changes();
685
686         iassert(lyt->screen_remove_layer(NULL, NULL) == IVI_FAILED);
687         lyt->commit_changes();
688
689         lyt->layer_destroy(ivilayer);
690 }
691
692
693 static void
694 test_commit_changes_after_render_order_set_layer_destroy(
695         struct test_context *ctx)
696 {
697 #define LAYER_NUM (3)
698         const struct ivi_layout_interface *lyt = ctx->layout_interface;
699         struct weston_output *output;
700         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
701         uint32_t i;
702
703         if (!iassert(!wl_list_empty(&ctx->compositor->output_list)))
704                 return;
705
706         output = wl_container_of(ctx->compositor->output_list.next, output, link);
707
708         for (i = 0; i < LAYER_NUM; i++)
709                 ivilayers[i] = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(i), 200, 300);
710
711         iassert(lyt->screen_set_render_order(output, ivilayers, LAYER_NUM) == IVI_SUCCEEDED);
712
713         lyt->layer_destroy(ivilayers[1]);
714
715         lyt->commit_changes();
716
717         lyt->layer_destroy(ivilayers[0]);
718         lyt->layer_destroy(ivilayers[2]);
719 #undef LAYER_NUM
720 }
721
722 static void
723 test_layer_properties_changed_notification_callback(struct wl_listener *listener, void *data)
724 {
725         struct test_context *ctx =
726                         container_of(listener, struct test_context,
727                                         layer_property_changed);
728         const struct ivi_layout_interface *lyt = ctx->layout_interface;
729         struct ivi_layout_layer *ivilayer = data;
730         const struct ivi_layout_layer_properties *prop = lyt->get_properties_of_layer(ivilayer);
731
732         iassert(lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0));
733         iassert(prop->source_width == 200);
734         iassert(prop->source_height == 300);
735
736         if (lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0) &&
737             prop->source_width == 200 && prop->source_height == 300)
738                 ctx->user_flags = 1;
739 }
740
741 static void
742 test_layer_properties_changed_notification(struct test_context *ctx)
743 {
744         const struct ivi_layout_interface *lyt = ctx->layout_interface;
745         struct ivi_layout_layer *ivilayer;
746
747         ctx->user_flags = 0;
748
749         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
750
751         ctx->layer_property_changed.notify = test_layer_properties_changed_notification_callback;
752
753         iassert(lyt->layer_add_listener(ivilayer, &ctx->layer_property_changed) == IVI_SUCCEEDED);
754
755         lyt->commit_changes();
756
757         iassert(ctx->user_flags == 0);
758
759         iassert(lyt->layer_set_destination_rectangle(
760                 ivilayer, 20, 30, 200, 300) == IVI_SUCCEEDED);
761
762         lyt->commit_changes();
763
764         iassert(ctx->user_flags == 1);
765
766         ctx->user_flags = 0;
767         iassert(lyt->layer_set_destination_rectangle(
768                 ivilayer, 20, 30, 200, 300) == IVI_SUCCEEDED);
769
770         lyt->commit_changes();
771
772         iassert(ctx->user_flags == 0);
773
774         // remove layer property changed listener.
775         wl_list_remove(&ctx->layer_property_changed.link);
776
777         ctx->user_flags = 0;
778         lyt->commit_changes();
779
780         iassert(ctx->user_flags == 0);
781
782         lyt->layer_destroy(ivilayer);
783 }
784
785 static void
786 test_layer_create_notification_callback(struct wl_listener *listener, void *data)
787 {
788         struct test_context *ctx =
789                         container_of(listener, struct test_context,
790                                         layer_created);
791         const struct ivi_layout_interface *lyt = ctx->layout_interface;
792         struct ivi_layout_layer *ivilayer = data;
793         const struct ivi_layout_layer_properties *prop = lyt->get_properties_of_layer(ivilayer);
794
795         iassert(lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0));
796         iassert(prop->source_width == 200);
797         iassert(prop->source_height == 300);
798
799         if (lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0) &&
800             prop->source_width == 200 && prop->source_height == 300)
801                 ctx->user_flags = 1;
802 }
803
804 static void
805 test_layer_create_notification(struct test_context *ctx)
806 {
807 #define LAYER_NUM (2)
808         const struct ivi_layout_interface *lyt = ctx->layout_interface;
809         static const uint32_t layers[LAYER_NUM] = {IVI_TEST_LAYER_ID(0), IVI_TEST_LAYER_ID(1)};
810         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
811
812         ctx->user_flags = 0;
813         ctx->layer_created.notify = test_layer_create_notification_callback;
814
815         iassert(lyt->add_listener_create_layer(&ctx->layer_created) == IVI_SUCCEEDED);
816         ivilayers[0] = lyt->layer_create_with_dimension(layers[0], 200, 300);
817
818         iassert(ctx->user_flags == 1);
819
820         ctx->user_flags = 0;
821         // remove layer created listener.
822         wl_list_remove(&ctx->layer_created.link);
823
824         ivilayers[1] = lyt->layer_create_with_dimension(layers[1], 400, 500);
825
826         iassert(ctx->user_flags == 0);
827
828         lyt->layer_destroy(ivilayers[0]);
829         lyt->layer_destroy(ivilayers[1]);
830 #undef LAYER_NUM
831 }
832
833 static void
834 test_layer_remove_notification_callback(struct wl_listener *listener, void *data)
835 {
836         struct test_context *ctx =
837                         container_of(listener, struct test_context,
838                                         layer_removed);
839         const struct ivi_layout_interface *lyt = ctx->layout_interface;
840         struct ivi_layout_layer *ivilayer = data;
841         const struct ivi_layout_layer_properties *prop =
842                 lyt->get_properties_of_layer(ivilayer);
843
844         iassert(lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0));
845         iassert(prop->source_width == 200);
846         iassert(prop->source_height == 300);
847
848         if (lyt->get_id_of_layer(ivilayer) == IVI_TEST_LAYER_ID(0) &&
849             prop->source_width == 200 && prop->source_height == 300)
850                 ctx->user_flags = 1;
851 }
852
853 static void
854 test_layer_remove_notification(struct test_context *ctx)
855 {
856 #define LAYER_NUM (2)
857         const struct ivi_layout_interface *lyt = ctx->layout_interface;
858         static const uint32_t layers[LAYER_NUM] = {IVI_TEST_LAYER_ID(0), IVI_TEST_LAYER_ID(1)};
859         struct ivi_layout_layer *ivilayers[LAYER_NUM] = {};
860
861         ctx->user_flags = 0;
862         ctx->layer_removed.notify = test_layer_remove_notification_callback;
863
864         ivilayers[0] = lyt->layer_create_with_dimension(layers[0], 200, 300);
865         iassert(lyt->add_listener_remove_layer(&ctx->layer_removed) == IVI_SUCCEEDED);
866         lyt->layer_destroy(ivilayers[0]);
867
868         iassert(ctx->user_flags == 1);
869
870         ctx->user_flags = 0;
871         ivilayers[1] = lyt->layer_create_with_dimension(layers[1], 250, 350);
872
873         // remove layer property changed listener.
874         wl_list_remove(&ctx->layer_removed.link);
875         lyt->layer_destroy(ivilayers[1]);
876
877         iassert(ctx->user_flags == 0);
878 #undef LAYER_NUM
879 }
880
881 static void
882 test_layer_bad_properties_changed_notification_callback(struct wl_listener *listener, void *data)
883 {
884 }
885
886 static void
887 test_layer_bad_properties_changed_notification(struct test_context *ctx)
888 {
889         const struct ivi_layout_interface *lyt = ctx->layout_interface;
890         struct ivi_layout_layer *ivilayer;
891
892         ivilayer = lyt->layer_create_with_dimension(IVI_TEST_LAYER_ID(0), 200, 300);
893
894         ctx->layer_property_changed.notify = test_layer_bad_properties_changed_notification_callback;
895
896         iassert(lyt->layer_add_listener(NULL, &ctx->layer_property_changed) == IVI_FAILED);
897         iassert(lyt->layer_add_listener(ivilayer, NULL) == IVI_FAILED);
898
899         lyt->layer_destroy(ivilayer);
900 }
901
902 static void
903 test_surface_bad_configure_notification(struct test_context *ctx)
904 {
905         const struct ivi_layout_interface *lyt = ctx->layout_interface;
906
907         iassert(lyt->add_listener_configure_surface(NULL) == IVI_FAILED);
908 }
909
910 static void
911 test_layer_bad_create_notification(struct test_context *ctx)
912 {
913         const struct ivi_layout_interface *lyt = ctx->layout_interface;
914
915         iassert(lyt->add_listener_create_layer(NULL) == IVI_FAILED);
916 }
917
918 static void
919 test_surface_bad_create_notification(struct test_context *ctx)
920 {
921         const struct ivi_layout_interface *lyt = ctx->layout_interface;
922
923         iassert(lyt->add_listener_create_surface(NULL) == IVI_FAILED);
924 }
925
926 static void
927 test_layer_bad_remove_notification(struct test_context *ctx)
928 {
929         const struct ivi_layout_interface *lyt = ctx->layout_interface;
930
931         iassert(lyt->add_listener_remove_layer(NULL) == IVI_FAILED);
932 }
933
934 static void
935 test_surface_bad_remove_notification(struct test_context *ctx)
936 {
937         const struct ivi_layout_interface *lyt = ctx->layout_interface;
938
939         iassert(lyt->add_listener_remove_surface(NULL) == IVI_FAILED);
940 }
941
942 /************************ tests end ********************************/
943
944 static void
945 run_internal_tests(void *data)
946 {
947         struct test_context *ctx = data;
948
949         test_surface_bad_visibility(ctx);
950         test_surface_bad_destination_rectangle(ctx);
951         test_surface_bad_source_rectangle(ctx);
952         test_surface_bad_properties(ctx);
953
954         test_layer_create(ctx);
955         test_layer_visibility(ctx);
956         test_layer_opacity(ctx);
957         test_layer_dimension(ctx);
958         test_layer_position(ctx);
959         test_layer_destination_rectangle(ctx);
960         test_layer_source_rectangle(ctx);
961         test_layer_bad_remove(ctx);
962         test_layer_bad_visibility(ctx);
963         test_layer_bad_opacity(ctx);
964         test_layer_bad_destination_rectangle(ctx);
965         test_layer_bad_source_rectangle(ctx);
966         test_layer_bad_properties(ctx);
967         test_commit_changes_after_visibility_set_layer_destroy(ctx);
968         test_commit_changes_after_opacity_set_layer_destroy(ctx);
969         test_commit_changes_after_source_rectangle_set_layer_destroy(ctx);
970         test_commit_changes_after_destination_rectangle_set_layer_destroy(ctx);
971         test_layer_create_duplicate(ctx);
972         test_get_layer_after_destory_layer(ctx);
973
974         test_screen_render_order(ctx);
975         test_screen_bad_render_order(ctx);
976         test_screen_add_layers(ctx);
977         test_screen_remove_layer(ctx);
978         test_screen_bad_remove_layer(ctx);
979         test_commit_changes_after_render_order_set_layer_destroy(ctx);
980
981         test_layer_properties_changed_notification(ctx);
982         test_layer_create_notification(ctx);
983         test_layer_remove_notification(ctx);
984         test_layer_bad_properties_changed_notification(ctx);
985         test_surface_bad_configure_notification(ctx);
986         test_layer_bad_create_notification(ctx);
987         test_surface_bad_create_notification(ctx);
988         test_layer_bad_remove_notification(ctx);
989         test_surface_bad_remove_notification(ctx);
990
991         weston_compositor_exit_with_code(ctx->compositor, EXIT_SUCCESS);
992         free(ctx);
993 }
994
995 WL_EXPORT int
996 wet_module_init(struct weston_compositor *compositor,
997                        int *argc, char *argv[])
998 {
999         struct wl_event_loop *loop;
1000         struct test_context *ctx;
1001         const struct ivi_layout_interface *iface;
1002
1003         iface = ivi_layout_get_api(compositor);
1004
1005         if (!iface) {
1006                 weston_log("fatal: cannot use ivi_layout_interface.\n");
1007                 return -1;
1008         }
1009
1010         ctx = zalloc(sizeof(*ctx));
1011         if (!ctx)
1012                 return -1;
1013
1014         ctx->compositor = compositor;
1015         ctx->layout_interface = iface;
1016
1017         loop = wl_display_get_event_loop(compositor->wl_display);
1018         wl_event_loop_add_idle(loop, run_internal_tests, ctx);
1019
1020         return 0;
1021 }