summaryrefslogtreecommitdiff
path: root/src/tests/elementary/suite_helpers.c
blob: 53c473a9fa1f5db2224032a693aad0c57427f4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif
#include <Elementary.h>
#include <Efl_Ui.h>
#include <check.h>
#include "eo_internal.h"
#include "../efl_check.h"
#include "elm_widget.h"
#include "ecore_private.h"
#include "ecore_evas_private.h"
#include "suite_helpers.h"


static int main_pid = -1;
static Eina_Bool did_shutdown;
static Evas_Object *global_win;
static Eina_Bool buffer = EINA_FALSE;
static Eina_Bool legacy_mode = EINA_FALSE;
static int log_abort;
static int log_abort_level;

Eina_Bool abort_on_warnings = EINA_FALSE;

void elm_test_init(TCase *tc);

static char *args[] = { "exe" };

SUITE_INIT(elm)
{
   ck_assert_int_eq(elm_init(1, args), 1);
}

void
_elm2_suite_init(void)
{
   if (is_forked())
     {
        if (abort_on_warnings)
          fail_on_errors_setup();
        ecore_fork_reset();
        return;
     }
   if (!did_shutdown)
     {
        /* if running un-forked then remove cached init */
        ck_assert_int_eq(elm_shutdown(), 0);
        did_shutdown = 1;
     }
   ck_assert_int_eq(elm_init(1, args), 1);
   if (abort_on_warnings)
     fail_on_errors_setup();
}

void
_elm_suite_shutdown(void)
{
   ck_assert_int_eq(elm_shutdown(), 0);
   /* verify that ecore was de-initialized completely */
   ck_assert_int_eq(ecore_init(), 1);
   /* avoid slowdowns in fork mode */
   if (is_forked()) return;
   ck_assert_int_eq(ecore_shutdown(), 0);
}


static const Efl_Test_Case etc_init[] = {
  { "init", elm_test_init },
  { NULL, NULL }
};


EAPI_MAIN void
efl_main(void *data EINA_UNUSED,
         const Efl_Event *ev)
{
   Efl_Loop_Arguments *arge = ev->info;


   fail_if(!arge->initialization);
   fprintf(stderr, "ARGC %d\n", eina_array_count(arge->argv));
   fail_if(eina_array_count(arge->argv) != 2);
   fail_if(!eina_streq(eina_array_data_get(arge->argv, 1), "test"));

   efl_loop_quit(ev->object, eina_value_string_init("success"));
}

EFL_START_TEST(efl_ui_test_init)
{
   /* EFL_MAIN */
   Eina_Value *ret__;
   int real__;

   int argc = 2;
   char *argv[] = { "efl_ui_suite", "test" };
   _efl_startup_time = ecore_time_unix_get();
   _EFL_APP_VERSION_SET();
   fail_if(!ecore_init());
   efl_event_callback_add(efl_app_main_get(), EFL_LOOP_EVENT_ARGUMENTS, efl_main, NULL);
   fail_if(!ecore_init_ex(argc, argv));
   __EFL_MAIN_CONSTRUCTOR;
   ret__ = efl_loop_begin(efl_app_main_get());
   real__ = efl_loop_exit_code_process(ret__);
   fail_if(real__ != 0);
   __EFL_MAIN_DESTRUCTOR;
   ecore_shutdown_ex();
   ecore_shutdown();
}
EFL_END_TEST

void efl_ui_init(TCase *tc)
{
   tcase_add_test(tc, efl_ui_test_init);
}


static const Efl_Test_Case ui_init[] = {
  { "init", efl_ui_init },
  { NULL, NULL }
};

#undef ecore_timer_add
#define BUFFER_RENDER_INTERVAL 0.005
#define LOOP_INCREMENT 0.1
#define TIMER_SCALE ((1.0 / BUFFER_RENDER_INTERVAL) * LOOP_INCREMENT)

void
loop_timer_interval_set(Eo *obj, double in)
{
   efl_loop_timer_interval_set(obj, in * TIMER_SCALE);
}

Eo *
timer_add(double in, Ecore_Task_Cb cb, void *data)
{
   return ecore_timer_add(in * TIMER_SCALE, cb, data);
}

void *
real_timer_add(double in, Ecore_Task_Cb cb, void *data)
{
   return ecore_timer_add(in, cb, data);
}

static void
_win_manual_render(void *data, const Efl_Event *event EINA_UNUSED)
 {
   double t = ecore_loop_time_get();

   ecore_loop_time_set(t + LOOP_INCREMENT);
   ecore_animator_custom_tick();
   ecore_evas_render_prepare(ecore_evas_ecore_evas_get(evas_object_evas_get(data)));
   evas_norender(evas_object_evas_get(data));
}

static void
_loop_iterate(void *data, const Efl_Event *event EINA_UNUSED)
{
   efl_loop_iterate(data);
}

static void
_win_show(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Eo *timer = evas_object_data_get(obj, "timer");
   efl_event_thaw(timer);
   efl_event_callback_add(efl_loop_get(obj), EFL_LOOP_EVENT_IDLE, _loop_iterate, efl_parent_get(timer));
}

static void
_win_hide(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Eo *timer = evas_object_data_get(obj, "timer");
   efl_event_freeze(timer);
   efl_loop_timer_reset(timer);
   efl_event_callback_del(efl_loop_get(obj), EFL_LOOP_EVENT_IDLE, _loop_iterate, efl_parent_get(timer));
}

static void
_ui_win_visibility_change(void *data EINA_UNUSED, const Efl_Event *ev)
{
   Eina_Bool *visible = ev->info;
   if (*visible)
     _win_show(NULL, NULL, ev->object, NULL);
   else
     {
        _win_hide(NULL, NULL, ev->object, NULL);
        efl_key_data_set(ev->object, "timer", NULL);
     }
}

EFL_CLASS_SIMPLE_CLASS(efl_loop, "Efl.Loop", EFL_LOOP_CLASS)

static Evas_Object *
_elm_suite_win_create(void)
{
   Evas_Object *win;
   Eo *loop, *timer;

   if (legacy_mode)
     win = elm_win_add(NULL, "elm_suite", ELM_WIN_BASIC);
   else
     win = efl_add(EFL_UI_WIN_CLASS, efl_main_loop_get());
   if (!buffer) return win;
   loop = efl_add(efl_loop_realized_class_get(), win);
   timer = efl_add(EFL_LOOP_TIMER_CLASS, loop,
     efl_loop_timer_interval_set(efl_added, BUFFER_RENDER_INTERVAL),
     efl_event_freeze(efl_added),
     efl_event_callback_add(efl_added, EFL_LOOP_TIMER_EVENT_TIMER_TICK, _win_manual_render, win)
     );
   evas_object_data_set(win, "timer", timer);
   ecore_evas_manual_render_set(ecore_evas_ecore_evas_get(evas_object_evas_get(win)), EINA_TRUE);
   edje_frametime_set(BUFFER_RENDER_INTERVAL);
   ecore_animator_source_set(ECORE_ANIMATOR_SOURCE_CUSTOM);
   if (legacy_mode)
     {
        evas_object_event_callback_add(win, EVAS_CALLBACK_SHOW, _win_show, NULL);
        evas_object_event_callback_add(win, EVAS_CALLBACK_HIDE, _win_hide, NULL);
     }
   else
     {
        efl_event_callback_add(win, EFL_GFX_ENTITY_EVENT_VISIBILITY_CHANGED, _ui_win_visibility_change, NULL);
     }
   return win;
}

#define TEST_FONT_DIR TESTS_SRC_DIR "/fonts/"

Evas_Object *
win_add(void)
{
   static Eina_Bool font_path = EINA_FALSE;

   if (!font_path)
     {
        evas_font_path_global_append(TEST_FONT_DIR);
        font_path = EINA_TRUE;
     }
   if (is_forked())
     {
        if (global_win) return global_win;
     }
   return _elm_suite_win_create();
}

static void
force_focus_win(Evas_Object *win)
{
   Ecore_Evas *ee;

   ee = ecore_evas_ecore_evas_get(evas_object_evas_get(win));
   ecore_evas_focus_set(ee, EINA_TRUE);
   ecore_evas_callback_focus_in_set(ee, NULL);
   ecore_evas_callback_focus_out_set(ee, NULL);
   Elm_Widget_Smart_Data *pd = efl_data_scope_safe_get(win, EFL_UI_WIDGET_CLASS);
   pd->top_win_focused = EINA_TRUE;
}

Evas_Object *
win_add_focused()
{
   Evas_Object *win;

   if (is_forked())
     {
        if (global_win) return global_win;
     }

   win = _elm_suite_win_create();
   force_focus_win(win);
   return win;
}

Eina_Bool
is_forked(void)
{
   return getpid() != main_pid;
}

Eina_Bool
is_buffer(void)
{
   return buffer;
}

static void (*suite_setup_cb)(Eo*);

void
suite_setup_cb_set(void (*cb)(Eo*))
{
   suite_setup_cb = cb;
}

int
suite_setup(Eina_Bool legacy)
{
   int failed_count;

   legacy_mode = legacy;

#ifdef NEED_RUN_IN_TREE
   putenv("EFL_RUN_IN_TREE=1");
#endif
   if (!getenv("ELM_ENGINE"))
     putenv("ELM_ENGINE=buffer");
   if (eina_streq(getenv("ELM_ENGINE"), "buffer"))
     {
        putenv("TESTS_GL_DISABLED=1");
        buffer = EINA_TRUE;
     }

   main_pid = getpid();
   ecore_app_no_system_modules();
   efreet_cache_disable();

   if (legacy)
     failed_count = _efl_suite_build_and_run(0, NULL,
                                             "Elementary_Init", etc_init, SUITE_INIT_FN(elm), SUITE_SHUTDOWN_FN(elm));
   else
     failed_count = _efl_suite_build_and_run(0, NULL,
                                             "Efl_Ui_Init", ui_init, NULL, NULL);

   failed_count += !elm_init(1, (char*[]){"exe"});
   if (buffer)
     {
        global_win = _elm_suite_win_create();
        force_focus_win(global_win);
        if (suite_setup_cb) suite_setup_cb(global_win);
     }
   EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
   /* preload default theme */
   if (legacy)
     {
        failed_count += !elm_theme_group_path_find(NULL, "elm/button/base/default");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
        failed_count += !elm_theme_group_path_find(NULL, "elm/border/base/default");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
        failed_count += !elm_theme_group_path_find(NULL, "elm/win/base/default");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
     }
   else
     {
        failed_count += !elm_theme_group_path_find(NULL, "efl/button");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
        failed_count += !elm_theme_group_path_find(NULL, "efl/border");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
        failed_count += !elm_theme_group_path_find(NULL, "efl/win");
        EINA_SAFETY_ON_TRUE_RETURN_VAL(failed_count, 255);
     }

   /* none of these will be found in the default theme,
    * but all are "fetched" hundreds of times
    * T6865
    */
   if (legacy)
     {
        elm_theme_group_path_find(NULL, "elm/cursor/top_side/default");
        elm_theme_group_path_find(NULL, "elm/cursor/bottom_side/default");
        elm_theme_group_path_find(NULL, "elm/cursor/top_left_corner/default");
        elm_theme_group_path_find(NULL, "elm/cursor/bottom_left_corner/default");
        elm_theme_group_path_find(NULL, "elm/cursor/top_right_corner/default");
        elm_theme_group_path_find(NULL, "elm/cursor/bottom_right_corner/default");


        elm_theme_group_path_find(NULL, "elm/colorselector/item/color/default");
     }
   else
     {
        elm_theme_group_path_find(NULL, "efl/cursor/top_side");
        elm_theme_group_path_find(NULL, "efl/cursor/bottom_side");
        elm_theme_group_path_find(NULL, "efl/cursor/top_left_corner");
        elm_theme_group_path_find(NULL, "efl/cursor/bottom_left_corner");
        elm_theme_group_path_find(NULL, "efl/cursor/top_right_corner");
        elm_theme_group_path_find(NULL, "efl/cursor/bottom_right_corner");


        elm_theme_group_path_find(NULL, "efl/colorselector/item/color");
     }
   return 0;
}

void
fail_on_errors_teardown(void)
{
   eina_log_abort_on_critical_set(log_abort);
   eina_log_abort_on_critical_level_set(log_abort_level);
}

void
fail_on_errors_setup(void)
{
   log_abort = eina_log_abort_on_critical_get();
   log_abort_level = eina_log_abort_on_critical_level_get();
   eina_log_abort_on_critical_level_set(2);
   eina_log_abort_on_critical_set(1);
}

static void
next_event_job()
{
   ecore_main_loop_quit();
}

static void
events_norendered(void *data EINA_UNUSED, Evas *e, void *event_info EINA_UNUSED)
{
   evas_event_callback_del(e, EVAS_CALLBACK_RENDER_POST, events_norendered);
   ecore_job_add(next_event_job, NULL);
}

void
get_me_to_those_events(Eo *obj)
{
   Evas *e = obj;

   if ((!efl_isa(obj, EFL_CANVAS_SCENE_INTERFACE)) || efl_isa(obj, EFL_UI_WIN_CLASS))
     e = evas_object_evas_get(obj);
   evas_smart_objects_calculate(e);
   evas_event_callback_add(e, EVAS_CALLBACK_RENDER_POST, events_norendered, NULL);
   ecore_main_loop_begin();
}

enum
{
   NONE = 0,
   LEFT = 1 << 0,
   RIGHT = 1 << 1,
   TOP = 1 << 2,
   BOTTOM = 1 << 3,
};

static Eina_Position2D
attempt_to_find_the_right_point_for_mouse_positioning(Eo *obj, int dir)
{
   int x, y;
   Eina_Rect r = efl_gfx_entity_geometry_get(obj);
   if (dir & LEFT)
     x = r.x + (.1 * r.w);
   else if (dir & RIGHT)
     x = r.x + (.9 * r.w);
   else
     x = r.x + r.w / 2;
   if (dir & TOP)
     y = r.y + (.1 * r.h);
   else if (dir & BOTTOM)
     y = r.y + (.9 * r.h);
   else
     y = r.y + r.h / 2;
   return EINA_POSITION2D(x, y);
}

static void
click_object_internal(Eo *obj, int dir, int flags)
{
   Evas *e = evas_object_evas_get(obj);
   Eina_Position2D pos = attempt_to_find_the_right_point_for_mouse_positioning(obj, dir);
   evas_event_feed_mouse_move(e, pos.x, pos.y, 0, NULL);
   evas_event_feed_mouse_down(e, 1, flags, 0, NULL);
   evas_event_feed_mouse_up(e, 1, 0, 0, NULL);
}

void
click_object(Eo *obj)
{
   click_object_internal(obj, NONE, 0);
}

void
click_object_flags(Eo *obj, int flags)
{
   click_object_internal(obj, NONE, flags);
}

void
click_part_flags(Eo *obj, const char *part, int flags)
{
   Efl_Part *part_obj = efl_ref(efl_part(obj, part));
   Eo *content;
   int dir = 0;

   if (efl_canvas_layout_part_type_get(part_obj) == EFL_CANVAS_LAYOUT_PART_TYPE_SWALLOW)
     content = efl_content_get(part_obj);
   else
     {
        content = part_obj;
        if (strstr(part, "left"))
          dir |= LEFT;
        else if (strstr(part, "right"))
          dir |= RIGHT;
        if (strstr(part, "top"))
          dir |= TOP;
        else if (strstr(part, "bottom"))
          dir |= BOTTOM;
     }
   click_object_internal(content, dir, flags);
   if (efl_isa(content, EFL_LAYOUT_SIGNAL_INTERFACE))
     edje_object_message_signal_process(content);
   edje_object_message_signal_process(obj);
   efl_unref(part_obj);
}

void
click_part(Eo *obj, const char *part)
{
   click_part_flags(obj, part, 0);
}

static void
wheel_object_internal(Eo *obj, int dir, Eina_Bool horiz, Eina_Bool down)
{
   Eina_Position2D pos = attempt_to_find_the_right_point_for_mouse_positioning(obj, dir);
   wheel_object_at(obj, pos.x, pos.y, horiz, down);
}

void
wheel_object(Eo *obj, Eina_Bool horiz, Eina_Bool down)
{
   wheel_object_internal(obj, NONE, horiz, down);
}

void
wheel_part(Eo *obj, const char *part, Eina_Bool horiz, Eina_Bool down)
{
   Efl_Part *part_obj = efl_ref(efl_part(obj, part));
   Eo *content;
   int dir = 0;

   if (efl_canvas_layout_part_type_get(part_obj) == EFL_CANVAS_LAYOUT_PART_TYPE_SWALLOW)
     content = efl_content_get(part_obj);
   else
     {
        content = part_obj;
        if (strstr(part, "left"))
          dir |= LEFT;
        else if (strstr(part, "right"))
          dir |= RIGHT;
        if (strstr(part, "top"))
          dir |= TOP;
        else if (strstr(part, "bottom"))
          dir |= BOTTOM;
     }
   wheel_object_internal(content, dir, horiz, down);
   if (efl_isa(content, EFL_LAYOUT_SIGNAL_INTERFACE))
     edje_object_message_signal_process(content);
   edje_object_message_signal_process(obj);
   efl_unref(part_obj);
}

void
event_callback_single_call_int_data(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   int *called = data;

   ck_assert_int_eq(*called, 0);
   *called = 1;
}

void
event_callback_that_increments_an_int_when_called(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   int *called = data;

   *called += 1;
}

void
event_callback_that_quits_the_main_loop_when_called()
{
   ecore_main_loop_quit();
}

void
click_object_at(Eo *obj, int x, int y)
{
   Evas *e = evas_object_evas_get(obj);
   evas_event_feed_mouse_move(e, x, y, 0, NULL);
   evas_event_feed_mouse_down(e, 1, 0, 0, NULL);
   evas_event_feed_mouse_up(e, 1, 0, 0, NULL);
}

void
click_object_at_flags(Eo *obj, int x, int y, int flags)
{
   Evas *e = evas_object_evas_get(obj);
   evas_event_feed_mouse_move(e, x, y, 0, NULL);
   evas_event_feed_mouse_down(e, 1, flags, 0, NULL);
   evas_event_feed_mouse_up(e, 1, 0, 0, NULL);
}

void
wheel_object_at(Eo *obj, int x, int y, Eina_Bool horiz, Eina_Bool down)
{
   Evas *e = evas_object_evas_get(obj);
   evas_event_feed_mouse_move(e, x, y, 0, NULL);
   evas_event_feed_mouse_wheel(e, horiz, down, 0, NULL);
}

void
drag_object(Eo *obj, int x, int y, int dx, int dy, Eina_Bool iterate)
{
   Evas *e = evas_object_evas_get(obj);
   int i;
   evas_event_feed_mouse_move(e, x, y, 0, NULL);
   evas_event_feed_mouse_down(e, 1, 0, 0, NULL);
   if (iterate)
     {
        /* iterate twice to trigger timers */
        ecore_main_loop_iterate();
        ecore_main_loop_iterate();
      }
   /* create DRAG_OBJECT_NUM_MOVES move events distinct from up/down */
   for (i = 0; i < DRAG_OBJECT_NUM_MOVES; i++)
     {
        evas_event_feed_mouse_move(e, x + (i * dx / DRAG_OBJECT_NUM_MOVES), y + (i * dy / DRAG_OBJECT_NUM_MOVES), 0, NULL);
        /* also trigger smart calc if we're iterating just in case that's important */
        evas_smart_objects_calculate(e);
     }
   evas_event_feed_mouse_move(e, x + dx, y + dy, 0, NULL);
   evas_event_feed_mouse_up(e, 1, 0, 0, NULL);
}