summaryrefslogtreecommitdiff
path: root/src/tests/eldbus/eldbus_test_eldbus_model.c
blob: dcd9fe4ac78fe5f77a1560e5e61bea0d2fb0cc10 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

#include <Eina.h>
#include <Ecore.h>
#include <Eldbus.h>
#include <Eldbus_Model.h>

#include "eldbus_test_eldbus_model.h"
#include "eldbus_suite.h"

static void
_promise_then_quit_cb(void **data , void **value )
{
   *data = *value;
   ecore_main_loop_quit();
}

static void
_promise_then_quit_u_cb(unsigned *data , unsigned *value)
{
   *data = *value;
   ecore_main_loop_quit();
}

static void
_promise_then_cp(Eina_Value *data , void *value)
{
   eina_value_copy(value, data);
   ecore_main_loop_quit();
}


static void
_promise_check_err(void *data EINA_UNUSED, void *value EINA_UNUSED)
{
   ck_assert_msg(0, "Promise Expected Error:\n");
   ecore_main_loop_quit();
}

static void
_error_then_ok(void* data, Eina_Error const* error)
{
   Eina_Error const* expected_error = (Eina_Error*)data;
   if (data != NULL)
   {
     ck_assert_int_eq(*error, *expected_error);
   }

   ecore_main_loop_quit();
}

static void
_error_then_cb(void* data EINA_UNUSED, Eina_Error const* error)
{
   ck_assert_msg(0,"Promise ERROR: %s\n", eina_error_msg_get(*error));
   ecore_main_loop_quit();
}

static Eina_Bool
_eo_event_quit_cb(void *data EINA_UNUSED, const Eo_Event *event EINA_UNUSED)
{
   ecore_main_loop_quit();
   return EINA_FALSE;
}

void *
efl_model_promise_then(Eina_Promise *promise)
{
   void *data = NULL;
   eina_promise_then(promise, (Eina_Promise_Cb)&_promise_then_quit_cb, &_error_then_cb, &data);
   ecore_main_loop_begin();
   return data;
}

void
check_efl_model_promise_error(Eina_Promise *promise, Eina_Error *err)
{
   eina_promise_then(promise, &_promise_check_err, &_error_then_ok, err);
   ecore_main_loop_begin();
}

int
efl_model_promise_then_u(Eina_Promise *promise)
{
   unsigned i = 0;
   eina_promise_then(promise, (Eina_Promise_Cb)&_promise_then_quit_u_cb, &_error_then_cb, &i);
   ecore_main_loop_begin();
   return i;
}

void
efl_model_wait_for_event(Eo *obj, const Eo_Event_Description* event)
{
   eo_event_callback_add(obj, event, _eo_event_quit_cb, NULL);
   ecore_main_loop_begin();
   eo_event_callback_del(obj, event, _eo_event_quit_cb, NULL);
}

Efl_Model_Base *
efl_model_nth_child_get(Efl_Model_Base *efl_model, unsigned int n)
{
   Eina_Accessor *accessor;
   Eina_Promise *promise;
   efl_model_children_slice_get(efl_model, n, 1, &promise);
   eina_promise_ref(promise);
   ck_assert_ptr_ne(NULL, promise);

   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);
   Eo *child = NULL;

   Eina_Bool ret = eina_accessor_data_get(accessor, 0, (void**)&child);
   eina_promise_unref(promise);
   ck_assert(ret);
   ck_assert_ptr_ne(NULL, child);
   return child;
}

Efl_Model_Base *
efl_model_first_child_get(Efl_Model_Base *efl_model)
{
   return efl_model_nth_child_get(efl_model, 1);
}

void
check_init(void)
{
   ecore_init();
   int ret = eldbus_init();
   ck_assert_int_ge(ret, 1);
}

void
check_shutdown(void)
{
   ecore_shutdown();
   int ret = eldbus_shutdown();
   ck_assert_int_eq(ret, 0);
}

void
check_property(Eo *object, const char *property_name, const char *expected_value)
{
   Eina_Promise *promise;
   efl_model_property_get(object, property_name, &promise);
   ck_assert_ptr_ne(NULL, promise);
   Eina_Value  *value = efl_model_promise_then(promise);
   char *actual_value;
   eina_value_get(value, &actual_value);
   if (!actual_value)
     ck_assert_ptr_eq(expected_value, actual_value);
   else
     {
        bool is_property_equal = strcmp(expected_value, actual_value) == 0;
        ck_assert_msg(is_property_equal, "'%s' != '%s'", expected_value, actual_value);
        free(actual_value);
     }
}

Eo *
create_connection(void)
{
   Eo *connection = eo_add_ref(ELDBUS_MODEL_CONNECTION_CLASS, NULL, eldbus_model_connection_constructor(eo_self, ELDBUS_CONNECTION_TYPE_SESSION, NULL, EINA_FALSE));
   ck_assert_ptr_ne(NULL, connection);
   return connection;
}

Eo *
create_object(void)
{
   Eo *object = eo_add_ref(ELDBUS_MODEL_OBJECT_CLASS, NULL, eldbus_model_object_constructor(eo_self, ELDBUS_CONNECTION_TYPE_SESSION, NULL, EINA_FALSE, ELDBUS_FDO_BUS, ELDBUS_FDO_PATH));
   ck_assert_ptr_ne(NULL, object);
   return object;
}

void
check_efl_model_children_count_eq(Efl_Model_Base *efl_model, unsigned int expected_children_count)
{
   unsigned int actual_children_count;
   Eina_Promise *promise;
   efl_model_children_count_get(efl_model, &promise);
   actual_children_count = efl_model_promise_then_u(promise);
   ck_assert_int_eq(expected_children_count, actual_children_count);
}

void
check_efl_model_children_count_ge(Efl_Model_Base *efl_model, unsigned int minimum_children_count)
{
   unsigned int actual_children_count;
   Eina_Promise *promise;
   efl_model_children_count_get(efl_model, &promise);
   actual_children_count = efl_model_promise_then_u(promise);
   ck_assert_int_ge(actual_children_count, minimum_children_count);
}

void
check_efl_model_children_slice_get(Efl_Model_Base *efl_model)
{
   unsigned count;
   Eina_Promise *promise;
   Eina_Accessor *accessor;
   efl_model_children_count_get(efl_model, &promise);
   count = efl_model_promise_then_u(promise);
   ck_assert_msg(count, "There must be at least 1 child to test");

   // Test slice all
   efl_model_children_slice_get(efl_model, 0, 0, &promise);
   eina_promise_ref(promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);
   // Get first child
   Eo *first_child = NULL;
   Eina_Bool ret = eina_accessor_data_get(accessor, 0, (void**)&first_child);
   ck_assert(ret);
   ck_assert_ptr_ne(NULL, first_child);
   // get last child
   Eo *last_child = NULL;
   ret = eina_accessor_data_get(accessor, count - 1, (void**)&last_child);
   ck_assert(ret);
   ck_assert_ptr_ne(NULL, last_child);
   // Test nonexistent child
   Eo *nonexistent_child = NULL;
   ret = eina_accessor_data_get(accessor, count, (void**)&nonexistent_child);
   ck_assert(!ret);
   ck_assert_ptr_eq(NULL, nonexistent_child);
   eina_promise_unref(promise);

   // Test slice first child
   Eo *child = NULL;
   efl_model_children_slice_get(efl_model, 1, 1, &promise);
   eina_promise_ref(promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);
   ret = eina_accessor_data_get(accessor, 0, (void**)&child);
   ck_assert(ret);
   ck_assert_ptr_ne(NULL, child);
   ret = eina_accessor_data_get(accessor, 1, (void**)&child);
   ck_assert(!ret);
   ck_assert_ptr_eq(first_child, child);
   eina_promise_unref(promise);

   // Test slice last child
   efl_model_children_slice_get(efl_model, count, 1, &promise);
   eina_promise_ref(promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);
   ret = eina_accessor_data_get(accessor, 0, (void**)&child);
   ck_assert(ret);
   ck_assert_ptr_ne(NULL, child);
   ret = eina_accessor_data_get(accessor, 1, (void**)&child);
   ck_assert(!ret);
   ck_assert_ptr_eq(last_child, child);
   eina_promise_unref(promise);

   // Test slice nonexistent element
   efl_model_children_slice_get(efl_model, count + 1, 1, &promise);
   eina_promise_ref(promise);
   ck_assert_ptr_ne(NULL, promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_eq(NULL, accessor);
   eina_promise_unref(promise);
}

START_TEST(smoke)
{
   check_init();

   Eo *connection = create_connection();
   eo_unref(connection);

   check_shutdown();
}
END_TEST

START_TEST(object)
{
   check_init();

   Eo *root = create_object();

   eo_unref(root);

   check_shutdown();
}
END_TEST

START_TEST(proxy)
{
   check_init();

   Eo *root = create_object();

   Eina_Accessor *accessor = NULL;
   Eina_Promise *promise = NULL;
   efl_model_children_slice_get(root, 0, 0, &promise);
   eina_promise_ref(promise);
   ck_assert_ptr_ne(NULL, promise);

   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);

   unsigned int i;
   Eo *proxy;
   EINA_ACCESSOR_FOREACH(accessor, i, proxy)
     {
        ck_assert_ptr_ne(NULL, proxy);
       //efl_model_load_and_wait_for_load_status(proxy, EFL_MODEL_LOAD_STATUS_LOADED);
     }
   eina_promise_unref(promise);

   eo_unref(root);

   check_shutdown();
}
END_TEST

void
eldbus_test_eldbus_model(TCase *tc)
{
   tcase_add_test(tc, smoke);
   tcase_add_test(tc, object);
   tcase_add_test(tc, proxy);
}

Eldbus_Model_Proxy *
eldbus_model_proxy_from_object_get(Eldbus_Model_Object *object, const char *interface_name)
{
   Eina_Accessor *accessor;
   Eina_Promise *promise = NULL;
   efl_model_children_slice_get(object, 0, 0, &promise);
   ck_assert_ptr_ne(NULL, promise);
   eina_promise_ref(promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);

   Eo *proxy = NULL;
   unsigned int i;
   EINA_ACCESSOR_FOREACH(accessor, i, proxy)
     {
        const char *name;
        name = eldbus_model_proxy_name_get(proxy);
        ck_assert_ptr_ne(NULL, name);
        if (strcmp(name, interface_name) == 0)
          goto end;
     }
   proxy = NULL;

end:
   eina_promise_unref(promise);
   return proxy;
}

static Eldbus_Model_Arguments *
_eldbus_model_arguments_from_proxy_get(Eldbus_Model_Proxy *proxy, const char *method_name, const Eo_Class *klass)
{
   Eina_Accessor *accessor;
   Eina_Promise *promise = NULL;
   efl_model_children_slice_get(proxy, 0, 0, &promise);
   ck_assert_ptr_ne(NULL, promise);
   eina_promise_ref(promise);
   accessor = efl_model_promise_then(promise);
   ck_assert_ptr_ne(NULL, accessor);

   Eo *child = NULL;
   unsigned int i;
   EINA_ACCESSOR_FOREACH(accessor, i, child)
     {
        if (!eo_isa(child, klass))
          continue;

        const char *name;
        name = eldbus_model_arguments_name_get(child);
        ck_assert_ptr_ne(NULL, name);
        if (strcmp(name, method_name) == 0)
          goto end;
     }
   child = NULL;

end:
   eina_promise_unref(promise);
   return child;
}

Eldbus_Model_Method *
eldbus_model_method_from_proxy_get(Eldbus_Model_Proxy *proxy, const char *method_name)
{
   return _eldbus_model_arguments_from_proxy_get(proxy, method_name, ELDBUS_MODEL_METHOD_CLASS);
}

Eldbus_Model_Signal *
eldbus_model_signal_from_proxy_get(Eldbus_Model_Proxy *proxy, const char *signal_name)
{
   return _eldbus_model_arguments_from_proxy_get(proxy, signal_name, ELDBUS_MODEL_SIGNAL_CLASS);
}

void
check_efl_model_property_int_eq(Efl_Model_Base *efl_model, const char *property, int expected_value)
{
   Eina_Value property_value;
   Eina_Promise *promise;
   efl_model_property_get(efl_model, property, &promise);
   ck_assert_ptr_ne(NULL, promise);

   eina_promise_then(promise, (Eina_Promise_Cb)&_promise_then_cp, &_error_then_cb, &property_value);
   ecore_main_loop_begin();

   const Eina_Value_Type *property_type = eina_value_type_get(&property_value);
   ck_assert_ptr_eq(EINA_VALUE_TYPE_INT, property_type);

   int actual_value = 0;
   eina_value_get(&property_value, &actual_value);
   ck_assert_int_eq(expected_value, actual_value);
   eina_value_flush(&property_value);
}

void
check_efl_model_property_int_set(Efl_Model_Base *efl_model, const char *property, int value)
{
   Eina_Value eina_value, value_ret;
   Eina_Promise *promise;

   eina_value_setup(&eina_value, EINA_VALUE_TYPE_INT);
   eina_value_set(&eina_value, value);
   efl_model_property_set(efl_model, property, &eina_value, &promise);

   eina_promise_then(promise, (Eina_Promise_Cb)&_promise_then_cp, &_error_then_cb, &value_ret);
   ecore_main_loop_begin();

   const Eina_Value_Type *property_type = eina_value_type_get(&value_ret);
   ck_assert_ptr_eq(EINA_VALUE_TYPE_INT, property_type);

   int actual_value;
   eina_value_get(&value_ret, &actual_value);
   ck_assert_int_eq(value, actual_value);
   eina_value_flush(&eina_value);
}