summaryrefslogtreecommitdiff
path: root/src/tests/elementary/elm_test_image.c
blob: 888069f2b7a4ad8a1d55ad79691b6f6d432f52d4 (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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define EFL_ACCESS_OBJECT_BETA
#include <Efl_Ui.h>
#include <Elementary.h>
#include "elm_suite.h"

static const char pathfmt[] =  ELM_IMAGE_DATA_DIR"/images/icon_%02d.png";
static const char invalid[] = "thereisnosuchimage.png";
#define MAX_IMAGE_ID 23

typedef struct _Test_Data Test_Data;
struct _Test_Data
{
   int image_id;
   int success;
};

static int
_file_to_memory(const char *filename, char **result)
{
   int size;
   FILE *f;

   f = fopen(filename, "rb");
   if (f == NULL)
     {
        *result = NULL;
        return -1;
     }

   fseek(f, 0, SEEK_END);
   size = ftell(f);
   fseek(f, 0, SEEK_SET);
   *result = (char *)malloc(size + 1);
   if ((size_t)size != fread(*result, sizeof(char), size, f))
     {
        free(*result);
        fclose(f);
        return -1;
     }
   fclose(f);
   (*result)[size] = 0;
   return size;
}

EFL_START_TEST(elm_image_legacy_type_check)
{
   Evas_Object *win, *image;
   const char *type;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);

   type = elm_object_widget_type_get(image);
   ck_assert(type != NULL);
   ck_assert(!strcmp(type, "Elm_Image"));

   type = evas_object_type_get(image);
   ck_assert(type != NULL);
   ck_assert(!strcmp(type, "elm_image"));

}
EFL_END_TEST

EFL_START_TEST(elm_atspi_role_get)
{
   Evas_Object *win, *image;
   Efl_Access_Role role;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);
   role = efl_access_object_role_get(image);

   ck_assert(role == EFL_ACCESS_ROLE_IMAGE);

}
EFL_END_TEST

static void
_async_error_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Test_Data *td = data;
   char path[PATH_MAX];
   sprintf(path, pathfmt, td->image_id);
   elm_image_file_set(obj, path, NULL);

   td->success = 1;
}

static void
_async_opened_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Test_Data *td = data;
   const char *ff, *kk, *r1, *r2;
   char path[PATH_MAX];

   sprintf(path, pathfmt, td->image_id);
   elm_image_file_get(obj, &ff, &kk);
   r1 = strrchr(ff, '/');
   r2 = strrchr(path, '/');
   ck_assert(eina_streq(r1, r2));
   ck_assert(!kk);

   fprintf(stderr, "opened: %s\n", ff);

   if (td->image_id < MAX_IMAGE_ID / 2)
     {
        td->image_id++;
        sprintf(path, pathfmt, td->image_id);
        elm_image_file_set(obj, path, NULL);
        return;
     }
   else if (td->image_id < MAX_IMAGE_ID)
     {
        // mini stress-test
        for (; td->image_id < MAX_IMAGE_ID;)
          {
             sprintf(path, pathfmt, ++td->image_id);
             elm_image_file_set(obj, path, NULL);
          }
        return;
     }

   if (td->success == 1)
     td->success = 2;
}

static void
_async_ready_cb(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Test_Data *td = data;

   const char *ff;
   elm_image_file_get(obj, &ff, 0);
   fprintf(stderr, "ready : %s\n", ff);
   if (td->success == 2)
     {
        td->success = 3;
        ecore_main_loop_quit();
     }
}

static Eina_Bool
_timeout_cb(void *data)
{
   Test_Data *td = data;

   td->success = 0;
   ecore_main_loop_quit();

   return ECORE_CALLBACK_CANCEL;
}

EFL_START_TEST(elm_image_async_path)
{
   Evas_Object *win, *image;
   Ecore_Timer *t;
   Eina_Bool ok;
   Test_Data td;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   td.success = 0;
   td.image_id = 0;

   image = elm_image_add(win);
   elm_image_async_open_set(image, 1);
   evas_object_smart_callback_add(image, "load,error", _async_error_cb, &td);
   evas_object_smart_callback_add(image, "load,open", _async_opened_cb, &td);
   evas_object_smart_callback_add(image, "load,ready", _async_ready_cb, &td);
   evas_object_show(image);
   ok = elm_image_file_set(image, invalid, NULL);
   ck_assert(ok);

   t = ecore_timer_add(10.0, _timeout_cb, &td);

   elm_run();
   ck_assert(td.success == 3);

   ecore_timer_del(t);
}
EFL_END_TEST

EFL_START_TEST(elm_image_async_mmap)
{
   Evas_Object *win, *image;
   Ecore_Timer *t;
   Eina_Bool ok;
   Test_Data td;
   Eina_File *f;
   char path[PATH_MAX];

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   td.success = 1; // skip "error" case
   td.image_id = 1;

   sprintf(path, pathfmt, td.image_id);
   f = eina_file_open(path, 0);
   ck_assert(f);

   image = elm_image_add(win);
   elm_image_async_open_set(image, 1);
   evas_object_smart_callback_add(image, "load,error", _async_error_cb, &td);
   evas_object_smart_callback_add(image, "load,open", _async_opened_cb, &td);
   evas_object_smart_callback_add(image, "load,ready", _async_ready_cb, &td);
   evas_object_show(image);
   ok = elm_image_mmap_set(image, f, NULL);
   ck_assert(ok);

   t = ecore_timer_add(10.0, _timeout_cb, &td);

   elm_run();
   ck_assert(td.success == 3);

   eina_file_close(f);

   ecore_timer_del(t);
}
EFL_END_TEST

EFL_START_TEST(elm_image_evas_object_color_set)
{
   Evas_Object *win, *image;
   int r = 128, g = 99, b = 3, a = 230;
   int rr = 0, gg = 0, bb = 0, aa = 0;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);
   evas_object_color_set(image, r, g, b, a);
   evas_object_color_get(image, &rr, &gg, &bb, &aa);
   ck_assert(r == rr);
   ck_assert(g == gg);
   ck_assert(b == bb);
   ck_assert(a == aa);
}
EFL_END_TEST

EFL_START_TEST(elm_image_evas_image_get)
{
   Evas_Object *win, *image, *obj;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);
   obj = elm_image_object_get(image);

   ck_assert(obj);
}
EFL_END_TEST

EFL_START_TEST(elm_image_test_memfile_set)
{
   Evas_Object *win, *image;
   char *mem;
   int size;
   const char *file = NULL;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);
   ck_assert(elm_image_file_set(image, ELM_IMAGE_DATA_DIR"/images/icon_01.png", NULL));
   size = _file_to_memory(ELM_IMAGE_DATA_DIR"/images/icon_02.png", &mem);
   ck_assert_int_ge(size, 0);
   ck_assert(elm_image_memfile_set(image, mem, size, "png", NULL));
   elm_image_file_get(image, &file, NULL);
   ck_assert_str_ne(file, ELM_IMAGE_DATA_DIR"/images/icon_01.png");
   ck_assert(elm_image_file_set(image, ELM_IMAGE_DATA_DIR"/images/icon_01.png", NULL));
   elm_image_file_get(image, &file, NULL);
   ck_assert_str_eq(file, ELM_IMAGE_DATA_DIR"/images/icon_01.png");
}
EFL_END_TEST

EFL_START_TEST(elm_image_test_scale_method)
{
   Evas_Object *win, *image;
   int w, h;

   win = win_add(NULL, "image", ELM_WIN_BASIC);
   evas_object_resize(win, 100, 100);

   image = elm_image_add(win);
   ck_assert(elm_image_file_set(image, ELM_IMAGE_DATA_DIR"/images/logo.png", NULL));
   evas_object_size_hint_align_set(image, 0.5, 0.0);
   efl_gfx_image_scale_method_set(image, EFL_GFX_IMAGE_SCALE_METHOD_FIT_WIDTH);
   evas_object_resize(image, 100, 100);
   evas_object_show(image);
   evas_object_show(win);
   get_me_to_those_events(win);
   evas_object_geometry_get(image, NULL, NULL, &w, &h);
   ck_assert_int_eq(w, 100);
   ck_assert_int_eq(h, 100);
   evas_object_geometry_get(elm_image_object_get(image), NULL, NULL, &w, &h);
   ck_assert_int_eq(w, 100);
   ck_assert_int_eq(h, 100);
}
EFL_END_TEST

#ifdef BUILD_LOADER_GIF
static void
_test_render(void *data, Evas *e EINA_UNUSED, void *event_info)
{
   int *pass = data;
   Evas_Event_Render_Post *ev = event_info;
   *pass = eina_list_count(ev->updated_area);
   ecore_main_loop_quit();
}

static void
_test_preload(void *data, Evas *e, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   if (evas_object_image_load_error_get(obj) == EVAS_LOAD_ERROR_NONE)
     evas_event_callback_add(e, EVAS_CALLBACK_RENDER_POST, _test_render, data);
   else
     ecore_main_loop_quit();
}

EFL_START_TEST(elm_image_test_gif)
{
   Evas_Object *win, *image;
   int pass = 0;

   win = win_add(NULL, "image", ELM_WIN_BASIC);

   image = elm_image_add(win);
   evas_object_resize(win, 100, 100);
   evas_object_resize(image, 100, 100);
   evas_object_show(win);
   evas_object_show(image);

   get_me_to_those_events(win);
   ck_assert(elm_image_file_set(image, ELM_IMAGE_DATA_DIR"/images/fire.gif", NULL));
   elm_image_animated_set(image, EINA_TRUE);
   elm_image_animated_play_set(image, EINA_TRUE);
   evas_object_event_callback_add(elm_image_object_get(image), EVAS_CALLBACK_IMAGE_PRELOADED, _test_preload, &pass);
   /* verify that we haven't tried to set a frame with index 0, as this is an error */
   ck_assert_int_gt(evas_object_image_animated_frame_get(elm_image_object_get(image)), 0);
   ecore_main_loop_begin();
   ck_assert_int_gt(pass, 0);
}
EFL_END_TEST

#endif

void elm_test_image(TCase *tc)
{
   tcase_add_test(tc, elm_image_legacy_type_check);
   tcase_add_test(tc, elm_atspi_role_get);
   tcase_add_test(tc, elm_image_async_path);
   tcase_add_test(tc, elm_image_async_mmap);
   tcase_add_test(tc, elm_image_evas_object_color_set);
   tcase_add_test(tc, elm_image_evas_image_get);
   tcase_add_test(tc, elm_image_test_memfile_set);
   tcase_add_test(tc, elm_image_test_scale_method);
#ifdef BUILD_LOADER_GIF
   tcase_add_test(tc, elm_image_test_gif);
#endif
}