summaryrefslogtreecommitdiff
path: root/src/tests/eldbus/eldbus_test_eldbus_model.c
blob: adba9f58022bac6b8da80e5eaaca002e31c41c9a (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
#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 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_wait_for_event(Eo *obj, const Eo_Event_Description* event)
{
   eo_do(obj, eo_event_callback_add(event, _eo_event_quit_cb, NULL));
   ecore_main_loop_begin();
   eo_do(obj, eo_event_callback_del(event, _eo_event_quit_cb, NULL));
}

static Eina_Bool
_event_load_status_quit_cb(void *data, const Eo_Event *event)
{
   printf("_event_load_status_quit_cb\n");
   Efl_Model_Load_Status expected_status = (Efl_Model_Load_Status)data;
   Efl_Model_Load *actual_load = (Efl_Model_Load*)event->event_info;

   if (expected_status == actual_load->status)
     {
        ecore_main_loop_quit();
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

void
efl_model_wait_for_load_status(Efl_Model_Base *efl_model, Efl_Model_Load_Status expected_status)
{
   Efl_Model_Load_Status actual_status;
   eo_do(efl_model, actual_status = efl_model_load_status_get());
   if (expected_status == actual_status)
     return;

   eo_do(efl_model, eo_event_callback_add(EFL_MODEL_BASE_EVENT_LOAD_STATUS, _event_load_status_quit_cb, (void*)expected_status));
   ecore_main_loop_begin();
   eo_do(efl_model, eo_event_callback_del(EFL_MODEL_BASE_EVENT_LOAD_STATUS, _event_load_status_quit_cb, (void*)expected_status));
}

Efl_Model_Base *
efl_model_nth_child_get(Efl_Model_Base *efl_model, unsigned int n)
{
   Eina_Accessor *accessor;
   Efl_Model_Load_Status status;
   eo_do(efl_model, status = efl_model_children_slice_get(n, 1, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   ck_assert_ptr_ne(NULL, accessor);
   Eo *child = NULL;
   Eina_Bool ret = eina_accessor_data_get(accessor, 0, (void**)&child);
   eina_accessor_free(accessor);
   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
efl_model_load_and_wait_for_load_status(Eo *obj, Efl_Model_Load_Status expected_status)
{
   eo_do(obj, efl_model_load());
   efl_model_wait_for_load_status(obj, expected_status);
}

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

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

void
check_property(Eo *object, const char *property_name, const char *expected_value)
{
   Eina_Value const* property_value;
   Efl_Model_Load_Status status;
   eo_do(object, status = efl_model_property_get(property_name, &property_value));
   ck_assert_msg(EFL_MODEL_LOAD_STATUS_ERROR != status, "Nonexistent property: %s", property_name);
   ck_assert_ptr_ne(NULL, property_value);
   char *actual_value = eina_value_to_string(property_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(ELDBUS_CONNECTION_TYPE_SESSION,
                                         NULL,
                                         EINA_FALSE));
   ck_assert_ptr_ne(NULL, connection);
   return connection;
}

Eo *
create_and_load_connection(void)
{
   Eo *connection = create_connection();
   efl_model_load_and_wait_for_load_status(connection, EFL_MODEL_LOAD_STATUS_LOADED);
   return connection;
}

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

Eo *
create_and_load_object(void)
{
   Eo *object = create_object();
   efl_model_load_and_wait_for_load_status(object, EFL_MODEL_LOAD_STATUS_LOADED);
   return object;
}

void
check_efl_model_load_status_get(Efl_Model_Base *efl_model, Efl_Model_Load_Status expected_load_status)
{
   Efl_Model_Load_Status actual_load_status;
   eo_do(efl_model, actual_load_status = efl_model_load_status_get());
   ck_assert_int_eq(expected_load_status, actual_load_status);
}

void
check_efl_model_children_count_eq(Efl_Model_Base *efl_model, unsigned int expected_children_count)
{
   unsigned int actual_children_count = 0;
   eo_do(efl_model, efl_model_children_count_get(&actual_children_count));
   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 = 0;
   Efl_Model_Load_Status status;
   eo_do(efl_model, status = efl_model_children_count_get(&actual_children_count));
   // A minimum count only exists if model have EFL_MODEL_LOAD_STATUS_LOADED_CHILDREN
   ck_assert((EFL_MODEL_LOAD_STATUS_LOADED_CHILDREN & status) == EFL_MODEL_LOAD_STATUS_LOADED_CHILDREN);

   ck_assert_int_ge(actual_children_count, minimum_children_count);
}

void
check_efl_model_children_slice_get(Efl_Model_Base *efl_model)
{
   unsigned int count = 0;
   Efl_Model_Load_Status status;
   eo_do(efl_model, status = efl_model_children_count_get(&count));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   ck_assert_msg(count, "There must be at least 1 child to test");

   // Test slice all
   Eina_Accessor *accessor;
   eo_do(efl_model, status = efl_model_children_slice_get(0, 0, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   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_accessor_free(accessor);

   // Test slice first child
   Eo *child = NULL;
   eo_do(efl_model, status = efl_model_children_slice_get(1, 1, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   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_accessor_free(accessor);

   // Test slice last child
   eo_do(efl_model, status = efl_model_children_slice_get(count, 1, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   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_accessor_free(accessor);

   // Test slice nonexistent element
   eo_do(efl_model, status = efl_model_children_slice_get(count + 1, 1, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   ck_assert_ptr_eq(NULL, accessor);
}

START_TEST(smoke)
{
   check_init();

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

   check_shutdown();
}
END_TEST

START_TEST(object)
{
   check_init();

   Eo *root = create_object();

   efl_model_load_and_wait_for_load_status(root, EFL_MODEL_LOAD_STATUS_LOADED);

   eo_unref(root);

   check_shutdown();
}
END_TEST

START_TEST(proxy)
{
   check_init();

   Eo *root = create_object();

   efl_model_load_and_wait_for_load_status(root, EFL_MODEL_LOAD_STATUS_LOADED);

   Eina_Accessor *accessor = NULL;
   eo_do(root, efl_model_children_slice_get(0, 0, &accessor));
   ck_assert_ptr_ne(NULL, accessor);

   unsigned int i;
   Eo *proxy;
   EINA_ACCESSOR_FOREACH(accessor, i, proxy)
     {
       efl_model_load_and_wait_for_load_status(proxy, EFL_MODEL_LOAD_STATUS_LOADED);
     }
   eina_accessor_free(accessor);

   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;
   Efl_Model_Load_Status status;
   eo_do(object, status = efl_model_children_slice_get(0, 0, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   ck_assert_ptr_ne(NULL, accessor);

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

end:
   eina_accessor_free(accessor);
   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;
   Efl_Model_Load_Status status;
   eo_do(proxy, status = efl_model_children_slice_get(0, 0, &accessor));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   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;
        eo_do(child, name = eldbus_model_arguments_name_get());
        ck_assert_ptr_ne(NULL, name);
        if (strcmp(name, method_name) == 0)
          goto end;
     }
   child = NULL;

end:
   eina_accessor_free(accessor);
   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 const* property_value;
   Efl_Model_Load_Status status;
   eo_do(efl_model, status = efl_model_property_get(property, &property_value));
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
   ck_assert_ptr_ne(NULL, property_value);

   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);
}

void
check_efl_model_property_int_set(Efl_Model_Base *efl_model, const char *property, int value)
{
   Eina_Value eina_value;
   eina_value_setup(&eina_value, EINA_VALUE_TYPE_INT);
   eina_value_set(&eina_value, value);
   Efl_Model_Load_Status status;
   eo_do(efl_model, status = efl_model_property_set(property, &eina_value));
   eina_value_flush(&eina_value);
   ck_assert_int_eq(EFL_MODEL_LOAD_STATUS_LOADED, status);
}