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
|
#include <gio/gio.h>
#include <gtest/gtest.h>
extern "C" {
#include "bus-watch-namespace.h"
}
class NameWatchTest : public ::testing::Test
{
private:
GTestDBus * testbus = NULL;
protected:
virtual void SetUp() {
testbus = g_test_dbus_new(G_TEST_DBUS_NONE);
g_test_dbus_up(testbus);
}
virtual void TearDown() {
g_test_dbus_down(testbus);
g_clear_object(&testbus);
}
};
typedef struct {
guint appeared;
guint vanished;
} callback_count_t;
static void
appeared_simple_cb (GDBusConnection * bus, const gchar * name, const gchar * owner, gpointer user_data)
{
callback_count_t * callback_count = static_cast<callback_count_t *>(user_data);
callback_count->appeared++;
}
static void
vanished_simple_cb (GDBusConnection * bus, const gchar * name, gpointer user_data)
{
callback_count_t * callback_count = static_cast<callback_count_t *>(user_data);
callback_count->vanished++;
}
TEST_F(NameWatchTest, BaseWatch)
{
callback_count_t callback_count = {0};
guint ns_watch = bus_watch_namespace(G_BUS_TYPE_SESSION,
"com.foo",
appeared_simple_cb,
vanished_simple_cb,
&callback_count,
NULL);
guint name1 = g_bus_own_name(G_BUS_TYPE_SESSION,
"com.foo.bar",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL, NULL, NULL);
guint name2 = g_bus_own_name(G_BUS_TYPE_SESSION,
"com.foo.bar_too",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL, NULL, NULL);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.appeared, 2);
g_bus_unown_name(name1);
g_bus_unown_name(name2);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.vanished, 2);
bus_unwatch_namespace(ns_watch);
}
TEST_F(NameWatchTest, NonMatches)
{
callback_count_t callback_count = {0};
guint ns_watch = bus_watch_namespace(G_BUS_TYPE_SESSION,
"com.foo",
appeared_simple_cb,
vanished_simple_cb,
&callback_count,
NULL);
guint name1 = g_bus_own_name(G_BUS_TYPE_SESSION,
"com.foobar.bar",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL, NULL, NULL);
guint name2 = g_bus_own_name(G_BUS_TYPE_SESSION,
"com.bar.com.foo",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL, NULL, NULL);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.appeared, 0);
g_bus_unown_name(name1);
g_bus_unown_name(name2);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.vanished, 0);
bus_unwatch_namespace(ns_watch);
}
TEST_F(NameWatchTest, StartupNames)
{
guint name1 = g_bus_own_name(G_BUS_TYPE_SESSION,
"com.foo.bar",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL, NULL, NULL);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
callback_count_t callback_count = {0};
guint ns_watch = bus_watch_namespace(G_BUS_TYPE_SESSION,
"com.foo",
appeared_simple_cb,
vanished_simple_cb,
&callback_count,
NULL);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.appeared, 1);
g_bus_unown_name(name1);
g_usleep(100000);
while (g_main_pending())
g_main_iteration(TRUE);
ASSERT_EQ(callback_count.vanished, 1);
bus_unwatch_namespace(ns_watch);
}
|