summaryrefslogtreecommitdiff
path: root/src/tests/eldbus/eldbus_test_eldbus_signal_handler.c
blob: 5e9bfc5600f14a07d489595db8c1ef258c6f572e (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
#include <Eldbus.h>
#include <Ecore.h>
#include <string.h>
#include <stdio.h>
#include <Eina.h>

#include "eldbus_suite.h"

static const char *bus = "org.freedesktop.DBus";
static const char *path = "/org/freedesktop/DBus";
static const char *interface = "org.freedesktop.DBus";
static const char *signal_name = "NameOwnerChanged";

static int cb_data = 5;

static Eina_Bool is_success_cb = EINA_FALSE;

static Ecore_Timer *timeout = NULL;

/**
* @addtogroup eldbus
* @{
* @defgroup eldbus_signal_handler
*
* @precondition
* @step 1 Initialize eldbus with eldbus_init()
*/

static Eina_Bool
_ecore_loop_close(void *data EINA_UNUSED)
{
   ecore_main_loop_quit();

   return ECORE_CALLBACK_CANCEL;
}

static void
_signal_name_owner_changed(void *data, const Eldbus_Message *msg)
{
   const char *errname, *errmsg;
   int *user_data = data;

   if (timeout != NULL)
     {
        ecore_timer_del(timeout);
        timeout = NULL;
     }

   if ((user_data) && (*user_data == cb_data))
     {
        if (!eldbus_message_error_get(msg, &errname, &errmsg))
          {
             is_success_cb = EINA_TRUE;
          }
     }

   ecore_main_loop_quit();
}

static void
_response_message_cb(void *data EINA_UNUSED, const Eldbus_Message *msg EINA_UNUSED, Eldbus_Pending *pending EINA_UNUSED)
{
}

static void
_signal_handler_free_cb(void *data, const void *deadptr EINA_UNUSED)
{
   int *user_data = data;

   if (timeout != NULL)
     {
        ecore_timer_del(timeout);
        timeout = NULL;
     }

   if ((user_data) && (*user_data == cb_data))
     is_success_cb = EINA_TRUE;

   ecore_main_loop_quit();
}

static Eldbus_Signal_Handler *
_signal_handler_get(Eldbus_Connection *conn)
{
   is_success_cb = EINA_FALSE;

   Eldbus_Signal_Handler *signal_handler = eldbus_signal_handler_add(conn, NULL, path, interface,
                                                                     signal_name, _signal_name_owner_changed, &cb_data);

   if (!signal_handler)
     {
        return NULL;
     }

   timeout = ecore_timer_add(0.1, _ecore_loop_close, NULL);
   if (!timeout)
     {
        eldbus_signal_handler_unref(signal_handler);
        return NULL;
     }

   return signal_handler;
}

/**
 * @addtogroup eldbus_signal_handler
 * @{
 * @defgroup eldbus_signal_handler_add eldbus_signal_handler_add()
 * @{
 * @objective Positive test case checks that function add a signal handler without error.
 *
 * @n Input Data:
 * @li the conn connection object
 * @li the msg message connection object
 * @li callback function to be called
 * @li data passed to callback
 *
 * @procedure
 * @step 1 Get eldbus connection object and check on NULL
 * @step 2 Call eldbus_signal_handler_add with callback function and data
 * to be called when this signal is received.
 * @step 3 Check returned signal handler object on NULL.
 * @step 4 Call eldbus_message_signal_new to create a new signal message.
 * @step 5 Check returned message object on NULL.
 * @step 6 Call eldbus_connection_send to send a signal a dbus object.
 * @step 7 Check returned pending object on NULL.
 * @step 8 Set timer for preschedule termination of main loop if tested callback wasn't executed.
 * @step 9 Start of main loop and wait for tested response in callback executing.
 * @step 10 Check static variables named is_success_cb.
 * If are equals 1, that callback was executed and signal is received without error.
 * In other cases error.
 * @step 11 Call eldbus_message_unref function to delete message object
 * @step 12 Call eldbus_signal_handler_unref function to delete signal handler object
 * @step 13 Call eldbus_connection_unref function to delete connection object
 *
 * @passcondition Variables named is_success_cb must equals 1, and there is no segmentation fault.
 * @}
 * @}
 */
EFL_START_TEST(utc_eldbus_signal_handler_add_p)
{
   is_success_cb = EINA_FALSE;

   Eldbus_Connection *conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   ck_assert_ptr_ne(NULL, conn);

   Eldbus_Signal_Handler *signal_handler = eldbus_signal_handler_add(conn, NULL, path, interface,
                                                                     signal_name, _signal_name_owner_changed, &cb_data);

   ck_assert_ptr_ne(NULL, signal_handler);

   Eldbus_Message *msg = eldbus_message_signal_new(path, interface, signal_name);
   ck_assert_ptr_ne(NULL, signal_handler);

   Eldbus_Pending *pending = eldbus_connection_send(conn, msg, _response_message_cb, NULL, -1);
   ck_assert_ptr_ne(NULL, pending);

   timeout = ecore_timer_add(0.1, _ecore_loop_close, NULL);
   ck_assert_ptr_ne(NULL, timeout);

   ecore_main_loop_begin();

   ck_assert_msg(is_success_cb, "Signal %s is not call", signal_name);

   eldbus_signal_handler_unref(signal_handler);
   eldbus_connection_unref(conn);
}
EFL_END_TEST

/**
 * @addtogroup eldbus_signal_handler
 * @{
 * @defgroup eldbus_signal_handler_del eldbus_signal_handler_del()
 * @{
 * @objective Positive test case checks that function add and delete a signal handler without error.
 *
 * @n Input Data:
 * @li the conn connection object
 * @li the msg message connection object
 * @li callback function to be called
 * @li data passed to callback
 *
 * @procedure
 * @step 1 Get eldbus connection object and check on NULL
 * @step 2 Call eldbus_signal_handler_add with callback function and data
 * to be called when this signal is received.
 * @step 3 Check returned signal handler object on NULL.
 * @step 4 Call eldbus_signal_handler_del the signal handler will stop listening to signals.
 * @step 5 Call eldbus_message_signal_new to create a new signal message.
 * @step 6 Check returned message object on NULL.
 * @step 7 Call eldbus_connection_send to send a signal a dbus object.
 * @step 8 Check returned pending object on NULL.
 * @step 9 Set timer for preschedule termination of main loop if tested callback wasn't executed.
 * @step 10 Start of main loop and wait for tested response in callback executing.
 * @step 11 Check static variables named is_success_cb.
 * If is equal 1, that callback was executed and this is error.
 * @step 12 Call eldbus_message_unref function to delete message object
 * @step 13 Call eldbus_signal_handler_unref function to delete signal handler object
 * @step 14 Call eldbus_connection_unref function to delete connection object
 *
 * @passcondition Variables named is_success_cb must equals 0
 * and eldbus_signal_handler_del must be delete registered callback, and there is no segmentation fault.
 * @}
 * @}
 */
EFL_START_TEST(utc_eldbus_signal_handler_del_p)
{
   is_success_cb = EINA_FALSE;

   Eldbus_Connection *conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   ck_assert_ptr_ne(NULL, conn);

   Eldbus_Signal_Handler *signal_handler = eldbus_signal_handler_add(conn, NULL, path, interface,
                                                                     signal_name, _signal_name_owner_changed, &cb_data);

   ck_assert_ptr_ne(NULL, signal_handler);

   eldbus_signal_handler_del(signal_handler);

   Eldbus_Message *msg = eldbus_message_signal_new(path, interface, signal_name);
   ck_assert_ptr_ne(NULL, msg);

   Eldbus_Pending *pending = eldbus_connection_send(conn, msg, _response_message_cb, NULL, -1);
   ck_assert_ptr_ne(NULL, pending);

   timeout = ecore_timer_add(0.1, _ecore_loop_close, NULL);
   ck_assert_ptr_ne(NULL, timeout);

   ecore_main_loop_begin();

   ck_assert_msg(!is_success_cb, "Signal %s was called", signal_name);

   eldbus_connection_unref(conn);
}
EFL_END_TEST

/**
 * @addtogroup eldbus_signal_handler
 * @{
 * @defgroup eldbus_signal_handler_get
 * @li eldbus_signal_handler_connection_get()
 * @li eldbus_signal_handler_interface_get()
 * @li eldbus_signal_handler_member_get()
 * @li eldbus_signal_handler_path_get()
 * @li eldbus_signal_handler_sender_get()
 * @{
 * @objective Positive test case checks those functions return valid expected value.
 *
 * @n Input Data:
 * @li the conn connection object
 * @li the path to bus interface
 * @li the sender bus name
 * @li the interface bus interface
 * @li the signal_name bus signal name
 *
 * @procedure
 * @step 1 Get eldbus connection object and check on NULL
 * @step 2 Call eldbus_signal_handler_add with callback function and data
 * to be called when this signal is received.
 * @step 3 Check returned signal handler object on NULL.
 * @step 4 Call eldbus_signal_handler_connection_get and check on expected value.
 * @step 5 Call eldbus_signal_handler_path_get and check on expected value.
 * @step 6 Call eldbus_signal_handler_sender_get and check on expected value.
 * @step 7 Call eldbus_signal_handler_interface_get and check on expected value.
 * @step 8 Call eldbus_signal_handler_member_get and check on expected value.
 * @step 9 Call eldbus_connection_unref function to delete signal handler object
 * @step 10 Call eldbus_connection_unref function to delete connection object
 *
 * @passcondition Functions should returns valid expected value and without segmentation fault.
 * @}
 * @}
 */
EFL_START_TEST(utc_eldbus_signal_handler_get_p)
{
   Eldbus_Connection *conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   ck_assert_ptr_ne(NULL, conn);

   Eldbus_Signal_Handler *signal_handler = eldbus_signal_handler_add(conn, bus, path, interface,
                                                                     signal_name, _signal_name_owner_changed, &cb_data);
   ck_assert_ptr_ne(NULL, signal_handler);

   Eldbus_Connection *conn_get = eldbus_signal_handler_connection_get(signal_handler);
   ck_assert_msg((conn_get == conn), "Connection objects are different");

   const char *signal_path = eldbus_signal_handler_path_get(signal_handler);
   ck_assert_msg(NULL != signal_path, "Signal path is null");
   ck_assert_msg((!strcmp(signal_path, path)), "Signal path is different than original");

   const char *signal_sender = eldbus_signal_handler_sender_get(signal_handler);
   ck_assert_msg(NULL != signal_sender, "Signal sender is null");
   ck_assert_msg((!strcmp(signal_sender, bus)), "Signal sender is different than original");

   const char *signal_interface = eldbus_signal_handler_interface_get(signal_handler);
   ck_assert_msg(NULL != signal_interface, "Signal interface is null");
   ck_assert_msg((!strcmp(signal_interface, interface)), "Signal interface is different than original");

   const char *signal_handler_name = eldbus_signal_handler_member_get(signal_handler);
   ck_assert_msg(NULL != signal_handler_name, "Signal name is null");
   ck_assert_msg((!strcmp(signal_handler_name, signal_name)), "Signal name is different than original");

   eldbus_signal_handler_unref(signal_handler);
   eldbus_connection_unref(conn);
}
EFL_END_TEST

/**
 * @addtogroup eldbus_signal_handler
 * @{
 * @defgroup eldbus_signal_handler_ref_unref
 * @li eldbus_signal_handler_ref()
 * @li eldbus_signal_handler_unref()
 * @{
 * @objective Positive test case checks that function increase, Decrease signal handler reference without error.
 *
 * @n Input Data:
 * @li the conn connection object
 * @li the signal_handler signal handler object
 *
 * @procedure
 * @step 1 Get eldbus connection object and check on NULL
 * @step 2 Call eldbus_signal_handler_add with callback function and data
 * to be called when this signal is received.
 * @step 3 Check returned signal handler object on NULL.
 * @step 4 Call eldbus_signal_handler_ref the increase signal handler reference.
 * @step 5 Check returned signal ref handler object with original signal handler object.
 * @step 6 Call eldbus_signal_handler_unref function decrease reference signal handler reference
 * @step 7 Call eldbus_signal_handler_unref function decrease original signal handler reference
 * @step 8 Call eldbus_message_signal_new to create a new signal message.
 * @step 9 Check returned message object on NULL.
 * @step 10 Call eldbus_connection_send to send a signal a dbus object.
 * @step 11 Check returned pending object on NULL.
 * @step 12 Set timer for preschedule termination of main loop if tested callback wasn't executed.
 * @step 13 Start of main loop and wait for tested response in callback executing.
 * @step 14 Check static variables named is_success_cb.
 * If is equal 1, that callback was executed and this is error.
 * @step 15 Call eldbus_message_unref function to delete message object
 * @step 16 Call eldbus_connection_unref function to delete connection object
 *
 * @passcondition Variables named is_success_cb must equals 0
 * and eldbus_signal_handler_unref must be decrease signal handler reference, and there is no segmentation fault.
 * @}
 * @}
 */

EFL_START_TEST(utc_eldbus_signal_handler_ref_unref_p)
{
   is_success_cb = EINA_FALSE;

   Eldbus_Connection *conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   ck_assert_ptr_ne(NULL, conn);

   Eldbus_Signal_Handler *signal_handler = eldbus_signal_handler_add(conn, NULL, path, interface,
                                                                     signal_name, _signal_name_owner_changed, &cb_data);
   ck_assert_ptr_ne(NULL, signal_handler);

   Eldbus_Signal_Handler* signal_ref = eldbus_signal_handler_ref(signal_handler);
   ck_assert_msg((signal_ref == signal_handler), "Signal references are different");

   eldbus_signal_handler_unref(signal_ref);
   eldbus_signal_handler_unref(signal_handler);

   Eldbus_Message *msg = eldbus_message_signal_new(path, interface, signal_name);
   ck_assert_ptr_ne(NULL, msg);

   Eldbus_Pending *pending = eldbus_connection_send(conn, msg, _response_message_cb, NULL, -1);
   ck_assert_ptr_ne(NULL, pending);

   timeout = ecore_timer_add(0.1, _ecore_loop_close, NULL);
   ck_assert_ptr_ne(NULL, timeout);

   ecore_main_loop_begin();

   ck_assert_msg(!is_success_cb, "Signal %s was called", signal_name);

   eldbus_connection_unref(conn);
}
EFL_END_TEST

/**
 * @addtogroup eldbus_signal_handler
 * @{
 * @defgroup eldbus_signal_handler_free_cb
 * @li eldbus_signal_handler_free_cb_add()
 * @li eldbus_signal_handler_free_cb_del()
 * @{
 * @objective Positive test case checks that function add and delete a callback function without error.
 *
 * @n Input Data:
 * @li the conn connection object
 * @li the signal_handler signal handler object
 *
 * @procedure
 * @step 1 Get eldbus connection object and check on NULL
 * @step 2 Get signal handler object and check on NULL.
 * @step 3 Call eldbus_signal_handler_free_cb_add to add a callback function
 * to be called when signal handler will be freed.
 * @step 4 Call eldbus_signal_handler_unref function decrease reference signal handler reference
 * @step 5 Start of main loop and wait for tested response in callback executing.
 * @step 6 Check static variables named is_success_cb.
 * If is equal 1, that callback was executed ignal handler was freed is received without error.

 * @step 7 Get signal handler object and check on NULL.
 * @step 8 Call eldbus_signal_handler_free_cb_add to add a callback function
 * to be called when signal handler will be freed.
 * @step 9 Call eldbus_signal_handler_free_cb_del to remove callback registered
 * in eldbus_signal_handler_free_cb_add().
 * @step 10 Call eldbus_signal_handler_unref function decrease reference signal handler reference
 * @step 11 Check static variables named is_success_cb.
 * If is equal 1, that callback was executed and this is error.
 * @step 12 Call eldbus_connection_unref function to delete connection object
 *
 * @passcondition The function should add and delete registered callback function,
 * and there is no segmentation fault.
 * @}
 * @}
 */

EFL_START_TEST(utc_eldbus_signal_handler_free_cb_add_del_p)
{
   Eldbus_Connection *conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   ck_assert_ptr_ne(NULL, conn);

   Eldbus_Signal_Handler *signal_handler = _signal_handler_get(conn);
   ck_assert_ptr_ne(NULL, signal_handler);

   eldbus_signal_handler_free_cb_add(signal_handler, _signal_handler_free_cb, &cb_data);

   eldbus_signal_handler_unref(signal_handler);

   ecore_main_loop_begin();

   ck_assert_msg(is_success_cb, "Callback is not called");

   signal_handler = _signal_handler_get(conn);
   ck_assert_ptr_ne(NULL, signal_handler);

   eldbus_signal_handler_free_cb_add(signal_handler, _signal_handler_free_cb, &cb_data);
   eldbus_signal_handler_free_cb_del(signal_handler, _signal_handler_free_cb, &cb_data);

   eldbus_signal_handler_unref(signal_handler);

   ecore_main_loop_begin();

   ck_assert_msg(!is_success_cb, "Callback was called");

   eldbus_connection_unref(conn);
}
EFL_END_TEST

/**
 *@}
 */
void
eldbus_test_eldbus_signal_handler(TCase *tc)
{
   tcase_add_test(tc, utc_eldbus_signal_handler_add_p);
   tcase_add_test(tc, utc_eldbus_signal_handler_del_p);
   tcase_add_test(tc, utc_eldbus_signal_handler_get_p);
   tcase_add_test(tc, utc_eldbus_signal_handler_ref_unref_p);
   tcase_add_test(tc, utc_eldbus_signal_handler_free_cb_add_del_p);
}