unit: Add Manager API binding
[framework/connectivity/connman.git] / unit / utils.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27
28 #include "gdbus/gdbus.h"
29
30 #include "test-connman.h"
31
32 #define ENABLE_WRAPPER 1
33
34 gboolean util_quit_loop(gpointer data)
35 {
36         struct test_fix *fix = data;
37
38         g_main_loop_quit(fix->main_loop);
39
40         return FALSE;
41 }
42
43 guint util_idle_call(struct test_fix *fix, GSourceFunc func,
44                         GDestroyNotify notify)
45 {
46         GSource *source;
47         guint id;
48
49         source = g_idle_source_new();
50         g_source_set_callback(source, func, fix, notify);
51         id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
52         g_source_unref(source);
53
54         return id;
55 }
56
57 guint util_call(struct test_fix *fix, GSourceFunc func,
58                 GDestroyNotify notify)
59 {
60         GSource *source;
61         guint id;
62
63         source = g_timeout_source_new(0);
64         g_source_set_callback(source, func, fix, notify);
65         id = g_source_attach(source, g_main_loop_get_context(fix->main_loop));
66         g_source_unref(source);
67
68         return id;
69 }
70
71 void util_setup(struct test_fix *fix, gconstpointer data)
72 {
73         fix->main_loop = g_main_loop_new(NULL, FALSE);
74         fix->main_connection = g_dbus_setup_private(DBUS_BUS_SYSTEM,
75                                                         NULL, NULL);
76 }
77
78 void util_teardown(struct test_fix *fix, gconstpointer data)
79 {
80         dbus_connection_close(fix->main_connection);
81         dbus_connection_unref(fix->main_connection);
82
83         g_main_loop_unref(fix->main_loop);
84 }
85
86 static void util_wrapper(struct test_fix *fix, gconstpointer data)
87 {
88         GSourceFunc func = data;
89 #if ENABLE_WRAPPER
90         if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
91                 util_call(fix, func, NULL);
92                 g_main_loop_run(fix->main_loop);
93                 exit(0);
94         }
95
96         g_test_trap_assert_passed();
97 #else
98         util_call(fix, func, NULL);
99         g_main_loop_run(fix->main_loop);
100 #endif
101 }
102
103 void util_test_add(const char *test_name, GSourceFunc test_func,
104                         util_test_setup_cb setup_cb,
105                         util_test_teardown_cb teardown_cb)
106 {
107         g_test_add(test_name, struct test_fix, test_func,
108                 setup_cb, util_wrapper, teardown_cb);
109 }