~larsu/indicator-messages/add-title

355.1.1 by Pete Woods
Re-do merge
1
/*
2
An indicator to show information that is in messaging applications
3
that the user is using.
4
5
Copyright 2012 Canonical Ltd.
6
7
This program is free software: you can redistribute it and/or modify it 
8
under the terms of the GNU General Public License version 3, as published 
9
by the Free Software Foundation.
10
11
This program is distributed in the hope that it will be useful, but 
12
WITHOUT ANY WARRANTY; without even the implied warranties of 
13
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
14
PURPOSE.  See the GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License along 
17
with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
19
20
#include <glib.h>
21
#include <gio/gio.h>
22
#include <gtest/gtest.h>
23
24
extern "C" {
25
#include "gactionmuxer.h"
26
}
27
28
static gboolean
29
strv_contains (gchar **str_array,
30
	       const gchar *str)
31
{
32
	gchar **it;
33
34
	for (it = str_array; *it; it++) {
35
		if (!g_strcmp0 (*it, str))
36
			return TRUE;
37
	}
38
39
	return FALSE;
40
}
41
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
42
TEST(GActionMuxerTest, Sanity) {
43
	GActionMuxer *muxer;
44
355.1.1 by Pete Woods
Re-do merge
45
#if G_ENCODE_VERSION(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION) <= GLIB_VERSION_2_34
46
	g_type_init ();
47
#endif
48
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
49
	g_action_muxer_insert (NULL, NULL, NULL);
50
	g_action_muxer_remove (NULL, NULL);
51
52
	muxer = g_action_muxer_new ();
53
54
	g_action_muxer_insert (muxer, NULL, NULL);
55
	g_action_muxer_remove (muxer, NULL);
56
	EXPECT_FALSE (g_action_group_has_action (G_ACTION_GROUP (muxer), NULL));
57
	EXPECT_FALSE (g_action_group_get_action_enabled (G_ACTION_GROUP (muxer), NULL));
58
	EXPECT_FALSE (g_action_group_query_action (G_ACTION_GROUP (muxer), NULL, NULL, NULL, NULL, NULL, NULL));
59
	g_action_group_activate_action (G_ACTION_GROUP (muxer), NULL, NULL);
60
61
	g_object_unref (muxer);
62
}
63
272.2.40 by Lars Uebernickel
gactionmuxer: don't crash when no global actions are given
64
TEST(GActionMuxerTest, Empty) {
65
	GActionMuxer *muxer;
66
	gchar **actions;
67
355.1.1 by Pete Woods
Re-do merge
68
#if G_ENCODE_VERSION(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION) <= GLIB_VERSION_2_34
69
	g_type_init ();
70
#endif
71
272.2.40 by Lars Uebernickel
gactionmuxer: don't crash when no global actions are given
72
	muxer = g_action_muxer_new ();
73
74
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
75
	EXPECT_EQ (0, g_strv_length (actions));
76
77
	g_strfreev (actions);
78
	g_object_unref (muxer);
79
}
80
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
81
TEST(GActionMuxerTest, AddAndRemove) {
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
82
	const GActionEntry entries1[] = { { "one" }, { "two" }, { "three" } };
83
	const GActionEntry entries2[] = { { "gb" }, { "es" }, { "fr" } };
84
	const GActionEntry entries3[] = { { "foo" }, { "bar" } };
85
	GSimpleActionGroup *group1;
86
	GSimpleActionGroup *group2;
87
	GSimpleActionGroup *group3;
88
	GActionMuxer *muxer;
89
	gchar **actions;
90
355.1.1 by Pete Woods
Re-do merge
91
#if G_ENCODE_VERSION(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION) <= GLIB_VERSION_2_34
92
	g_type_init ();
93
#endif
94
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
95
	group1 = g_simple_action_group_new ();
358.2.1 by Lars Uebernickel
Don't use deprecated g_simple_action_group_{lookup,insert}
96
	g_action_map_add_action_entries (G_ACTION_MAP (group1),
97
					 entries1,
98
					 G_N_ELEMENTS (entries1),
99
					 NULL);
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
100
101
	group2 = g_simple_action_group_new ();
358.2.1 by Lars Uebernickel
Don't use deprecated g_simple_action_group_{lookup,insert}
102
	g_action_map_add_action_entries (G_ACTION_MAP (group2),
103
					 entries2,
104
					 G_N_ELEMENTS (entries2),
105
					 NULL);
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
106
107
	group3 = g_simple_action_group_new ();
358.2.1 by Lars Uebernickel
Don't use deprecated g_simple_action_group_{lookup,insert}
108
	g_action_map_add_action_entries (G_ACTION_MAP (group3),
109
					 entries3,
110
					 G_N_ELEMENTS (entries3),
111
					 NULL);
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
112
113
	muxer = g_action_muxer_new ();
114
	g_action_muxer_insert (muxer, "first", G_ACTION_GROUP (group1));
115
	g_action_muxer_insert (muxer, "second", G_ACTION_GROUP (group2));
116
	g_action_muxer_insert (muxer, NULL, G_ACTION_GROUP (group3));
117
118
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
119
	EXPECT_TRUE (g_action_group_has_action (G_ACTION_GROUP (muxer), "first.one"));
120
	EXPECT_FALSE (g_action_group_has_action (G_ACTION_GROUP (muxer), "one"));
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
121
	EXPECT_EQ (8, g_strv_length (actions));
122
	EXPECT_TRUE (strv_contains (actions, "first.one"));
123
	EXPECT_TRUE (strv_contains (actions, "first.two"));
124
	EXPECT_TRUE (strv_contains (actions, "first.three"));
125
	EXPECT_TRUE (strv_contains (actions, "second.gb"));
126
	EXPECT_TRUE (strv_contains (actions, "second.es"));
127
	EXPECT_TRUE (strv_contains (actions, "second.fr"));
128
	EXPECT_TRUE (strv_contains (actions, "foo"));
129
	EXPECT_TRUE (strv_contains (actions, "bar"));
130
	g_strfreev (actions);
131
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
132
	g_action_muxer_remove (muxer, NULL);
133
	EXPECT_FALSE (g_action_group_has_action (G_ACTION_GROUP (muxer), "foo"));
134
	EXPECT_TRUE (g_action_group_has_action (G_ACTION_GROUP (muxer), "first.one"));
135
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
136
	EXPECT_EQ (6, g_strv_length (actions));
137
	EXPECT_FALSE (strv_contains (actions, "foo"));
138
	EXPECT_TRUE (strv_contains (actions, "first.one"));
139
	g_strfreev (actions);
140
141
	g_action_muxer_remove (muxer, "first");
142
	EXPECT_FALSE (g_action_group_has_action (G_ACTION_GROUP (muxer), "first.two"));
143
	EXPECT_TRUE (g_action_group_has_action (G_ACTION_GROUP (muxer), "second.es"));
144
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
145
	EXPECT_EQ (3, g_strv_length (actions));
146
	EXPECT_FALSE (strv_contains (actions, "first.two"));
147
	EXPECT_TRUE (strv_contains (actions, "second.es"));
148
	g_strfreev (actions);
149
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
150
	g_action_muxer_insert (muxer, "second", G_ACTION_GROUP (group2));
151
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
152
	EXPECT_EQ (3, g_strv_length (actions));
153
	g_strfreev (actions);
154
155
	g_action_muxer_insert (muxer, NULL, G_ACTION_GROUP (group3));
156
	actions = g_action_group_list_actions (G_ACTION_GROUP (muxer));
157
	EXPECT_EQ (5, g_strv_length (actions));
158
	g_strfreev (actions);
159
272.2.36 by Lars Uebernickel
Add a first gactionmuxer test
160
	g_object_unref (muxer);
161
	g_object_unref (group1);
162
	g_object_unref (group2);
163
	g_object_unref (group3);
164
}
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
165
166
static gboolean
167
g_variant_equal0 (gconstpointer one,
168
		  gconstpointer two)
169
{
170
	if (one == NULL)
171
		return two == NULL;
172
	else
173
		return g_variant_equal (one, two);
174
}
175
176
TEST(GActionMuxerTest, ActionAttributes) {
177
	GSimpleActionGroup *group;
178
	GSimpleAction *action;
179
	GActionMuxer *muxer;
180
	gboolean enabled[2];
181
	const GVariantType *param_type[2];
182
	const GVariantType *state_type[2];
183
	GVariant *state_hint[2];
184
	GVariant *state[2];
185
355.1.1 by Pete Woods
Re-do merge
186
#if G_ENCODE_VERSION(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION) <= GLIB_VERSION_2_34
187
	g_type_init ();
188
#endif
189
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
190
	group = g_simple_action_group_new ();
191
	action = g_simple_action_new ("one", G_VARIANT_TYPE_STRING);
357.1.1 by Ted Gould
Swap g_simple_action_group_insert() for g_action_map_add_action()
192
	g_action_map_add_action (G_ACTION_MAP(group), G_ACTION (action));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
193
194
	muxer = g_action_muxer_new ();
195
	g_action_muxer_insert (muxer, "first", G_ACTION_GROUP (group));
196
197
	/* test two of the convenience functions */
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
198
	EXPECT_TRUE (g_action_group_get_action_enabled (G_ACTION_GROUP (muxer), "first.one"));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
199
	g_simple_action_set_enabled (action, FALSE);
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
200
	EXPECT_FALSE (g_action_group_get_action_enabled (G_ACTION_GROUP (muxer), "first.one"));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
201
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
202
	EXPECT_STREQ ((gchar *) g_action_group_get_action_parameter_type (G_ACTION_GROUP (muxer), "first.one"),
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
203
		      (gchar *) G_VARIANT_TYPE_STRING);
204
205
	/* query_action */
206
	g_action_group_query_action (G_ACTION_GROUP (group), "one",
207
				     &enabled[0], &param_type[0], &state_type[0], &state_hint[0], &state[0]);
208
	g_action_group_query_action (G_ACTION_GROUP (muxer), "first.one",
209
				     &enabled[1], &param_type[1], &state_type[1], &state_hint[1], &state[1]);
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
210
	EXPECT_EQ (enabled[0], enabled[1]);
211
	EXPECT_STREQ ((gchar *) param_type[0], (gchar *) param_type[1]);
212
	EXPECT_STREQ ((gchar *) state_type[0], (gchar *) state_type[1]);
213
	EXPECT_TRUE (g_variant_equal0 ((gconstpointer) state_hint[0], (gconstpointer) state_hint[1]));
214
	EXPECT_TRUE (g_variant_equal0 ((gconstpointer) state[0], (gconstpointer) state[1]));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
215
216
	g_object_unref (action);
217
	g_object_unref (group);
218
	g_object_unref (muxer);
219
}
220
221
typedef struct {
222
	gboolean signal_ran;
223
	const gchar *name;
224
} TestSignalClosure;
225
226
static void
227
action_added (GActionGroup *group,
228
	      gchar *action_name,
229
	      gpointer user_data)
230
{
231
	TestSignalClosure *c = (TestSignalClosure *)user_data;
232
	EXPECT_STREQ (c->name, action_name);
233
	c->signal_ran = TRUE;
234
}
235
236
static void
237
action_enabled_changed (GActionGroup *group,
238
			gchar *action_name,
239
			gboolean enabled,
240
			gpointer user_data)
241
{
242
	TestSignalClosure *c = (TestSignalClosure *)user_data;
243
	EXPECT_EQ (enabled, FALSE);
244
	c->signal_ran = TRUE;
245
}
246
247
static void
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
248
action_state_changed (GActionGroup *group,
249
		      gchar *action_name,
250
		      GVariant *value,
251
		      gpointer user_data)
252
{
253
	TestSignalClosure *c = (TestSignalClosure *)user_data;
254
	EXPECT_STREQ (g_variant_get_string (value, NULL), "off");
255
	c->signal_ran = TRUE;
256
}
257
258
static void
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
259
action_removed (GActionGroup *group,
260
		gchar *action_name,
261
		gpointer user_data)
262
{
263
	TestSignalClosure *c = (TestSignalClosure *)user_data;
264
	EXPECT_STREQ (c->name, action_name);
265
	c->signal_ran = TRUE;
266
}
267
268
TEST(GActionMuxerTest, Signals) {
269
	GSimpleActionGroup *group;
270
	GSimpleAction *action;
271
	GActionMuxer *muxer;
272
	TestSignalClosure closure;
273
274
	group = g_simple_action_group_new ();
275
276
	action = g_simple_action_new ("one", G_VARIANT_TYPE_STRING);
357.1.1 by Ted Gould
Swap g_simple_action_group_insert() for g_action_map_add_action()
277
	g_action_map_add_action (G_ACTION_MAP(group), G_ACTION (action));
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
278
	g_object_unref (action);
279
280
	muxer = g_action_muxer_new ();
281
282
	g_signal_connect (muxer, "action-added",
283
			  G_CALLBACK (action_added), (gpointer) &closure);
284
	g_signal_connect (muxer, "action-enabled-changed",
285
			  G_CALLBACK (action_enabled_changed), (gpointer) &closure);
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
286
	g_signal_connect (muxer, "action-state-changed",
287
			  G_CALLBACK (action_state_changed), (gpointer) &closure);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
288
	g_signal_connect (muxer, "action-removed",
289
			  G_CALLBACK (action_removed), (gpointer) &closure);
290
291
	/* add the group with "one" action and check whether the signal is emitted */
292
	closure.signal_ran = FALSE;
293
	closure.name = "first.one";
294
	g_action_muxer_insert (muxer, "first", G_ACTION_GROUP (group));
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
295
	EXPECT_TRUE (closure.signal_ran);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
296
297
	/* add a second action after the group was added to the muxer */
298
	closure.signal_ran = FALSE;
299
	closure.name = "first.two";
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
300
	action = g_simple_action_new_stateful ("two", G_VARIANT_TYPE_STRING,
301
					       g_variant_new_string ("on"));
357.1.1 by Ted Gould
Swap g_simple_action_group_insert() for g_action_map_add_action()
302
	g_action_map_add_action (G_ACTION_MAP(group), G_ACTION (action));
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
303
	EXPECT_TRUE (closure.signal_ran);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
304
305
	/* disable the action */
306
	closure.signal_ran = FALSE;
307
	g_simple_action_set_enabled (action, FALSE);
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
308
	EXPECT_TRUE (closure.signal_ran);
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
309
310
	/* change its state */
311
	closure.signal_ran = FALSE;
312
	g_simple_action_set_state (action, g_variant_new_string ("off"));
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
313
	EXPECT_TRUE (closure.signal_ran);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
314
	g_object_unref (action);
315
316
	/* remove the first action */
317
	closure.signal_ran = FALSE;
318
	closure.name = "first.one";
357.1.2 by Ted Gould
Change g_simple_action_group_remove() to g_action_map_remove_action()
319
	g_action_map_remove_action (G_ACTION_MAP(group), "one");
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
320
	EXPECT_TRUE (closure.signal_ran);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
321
322
	/* remove the whole group, should be notified about "first.two" */
323
	closure.signal_ran = FALSE;
324
	closure.name = "first.two";
325
	g_action_muxer_remove (muxer, "first");
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
326
	EXPECT_TRUE (closure.signal_ran);
272.2.41 by Lars Uebernickel
Test gactionmuxer more extensively
327
328
	g_object_unref (group);
329
	g_object_unref (muxer);
330
}
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
331
332
static void
333
action_activated (GSimpleAction *simple,
334
		  GVariant      *parameter,
335
		  gpointer       user_data)
336
{
337
	gboolean *signal_ran = (gboolean *)user_data;
338
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
339
	EXPECT_STREQ (g_variant_get_string (parameter, NULL), "value");
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
340
	*signal_ran = TRUE;
341
}
342
343
static void
344
action_change_state (GSimpleAction *simple,
345
		     GVariant      *value,
346
		     gpointer       user_data)
347
{
348
	gboolean *signal_ran = (gboolean *)user_data;
349
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
350
	EXPECT_STREQ (g_variant_get_string (value, NULL), "off");
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
351
	*signal_ran = TRUE;
352
}
353
354
TEST(GActionMuxerTest, ActivateAction) {
355
	GSimpleActionGroup *group;
356
	GSimpleAction *action;
357
	GActionMuxer *muxer;
358
	gboolean signal_ran;
359
360
	group = g_simple_action_group_new ();
361
362
	action = g_simple_action_new ("one", G_VARIANT_TYPE_STRING);
357.1.1 by Ted Gould
Swap g_simple_action_group_insert() for g_action_map_add_action()
363
	g_action_map_add_action (G_ACTION_MAP(group), G_ACTION (action));
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
364
	g_signal_connect (action, "activate",
365
			  G_CALLBACK (action_activated), (gpointer) &signal_ran);
366
	g_object_unref (action);
367
368
	action = g_simple_action_new_stateful ("two", NULL,
369
					       g_variant_new_string ("on"));
357.1.1 by Ted Gould
Swap g_simple_action_group_insert() for g_action_map_add_action()
370
	g_action_map_add_action (G_ACTION_MAP(group), G_ACTION (action));
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
371
	g_signal_connect (action, "change-state",
372
			  G_CALLBACK (action_change_state), (gpointer) &signal_ran);
373
	g_object_unref (action);
374
375
	muxer = g_action_muxer_new ();
376
	g_action_muxer_insert (muxer, "first", G_ACTION_GROUP (group));
377
378
	signal_ran = FALSE;
379
	g_action_group_activate_action (G_ACTION_GROUP (muxer), "first.one",
380
					g_variant_new_string  ("value"));
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
381
	EXPECT_TRUE (signal_ran);
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
382
383
	signal_ran = FALSE;
384
	g_action_group_change_action_state (G_ACTION_GROUP (muxer), "first.two",
385
					    g_variant_new_string  ("off"));
272.2.48 by Lars Uebernickel
testing: use EXPECT_* instead of ASSERT_*
386
	EXPECT_TRUE (signal_ran);
272.2.47 by Lars Uebernickel
gactionmuxer: more tests
387
388
	g_object_unref (group);
389
	g_object_unref (muxer);
390
}