aboutsummaryrefslogtreecommitdiff
path: root/tests/test-libappindicator.c
blob: 02733984412da7ede0b0133b16dbfbb4582cee13 (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
Tests for the libappindicator library.

Copyright 2009 Canonical Ltd.
Copyright 2023 Robert Tari

Authors:
    Ted Gould <ted@canonical.com>
    Robert Tari <robert@tari.in>

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <glib.h>
#include <glib-object.h>

#include <app-indicator.h>

#include <libdbusmenu-glib/menuitem.h>
#include <libdbusmenu-glib/server.h>

static gboolean
allow_warnings (const gchar *log_domain, GLogLevelFlags log_level,
                const gchar *message, gpointer user_data)
{
    // By default, gtest will fail a test on even a warning message.
    // But since some of our sub-libraries are noisy (especially at-spi2),
    // only fail on critical or worse.
    return ((log_level & G_LOG_LEVEL_MASK) <= G_LOG_LEVEL_CRITICAL);
}

void
test_libappindicator_prop_signals_status_helper (AppIndicator * ci, gchar * status, gboolean * signalactivated)
{
    *signalactivated = TRUE;
    return;
}

void
test_libappindicator_prop_signals_helper (AppIndicator * ci, gboolean * signalactivated)
{
    *signalactivated = TRUE;
    return;
}

void
test_libappindicator_prop_signals (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

        AppIndicator * ci = app_indicator_new ("test-app-indicator",
                                               "indicator-messages",
                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);

    gboolean signaled = FALSE;
    gulong handlerid;

    handlerid = g_signal_connect(G_OBJECT(ci), "new-icon", G_CALLBACK(test_libappindicator_prop_signals_helper), &signaled);
    g_assert(handlerid != 0);

    handlerid = g_signal_connect(G_OBJECT(ci), "new-attention-icon", G_CALLBACK(test_libappindicator_prop_signals_helper), &signaled);
    g_assert(handlerid != 0);

    handlerid = g_signal_connect(G_OBJECT(ci), "new-status", G_CALLBACK(test_libappindicator_prop_signals_status_helper), &signaled);
    g_assert(handlerid != 0);


    signaled = FALSE;
    app_indicator_set_icon_full (ci, "bob", NULL);
    g_assert(signaled);

    signaled = FALSE;
    app_indicator_set_icon_full (ci, "bob", NULL);
    g_assert(!signaled);

    signaled = FALSE;
    app_indicator_set_icon_full (ci, "al", NULL);
    g_assert(signaled);


    signaled = FALSE;
    app_indicator_set_attention_icon_full (ci, "bob", NULL);
    g_assert(signaled);

    signaled = FALSE;
    app_indicator_set_attention_icon_full (ci, "bob", NULL);
    g_assert(!signaled);

    signaled = FALSE;
    app_indicator_set_attention_icon_full (ci, "al", NULL);
    g_assert(signaled);


    signaled = FALSE;
    app_indicator_set_status(ci, APP_INDICATOR_STATUS_PASSIVE);
    g_assert(!signaled);

    signaled = FALSE;
    app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
    g_assert(signaled);

    signaled = FALSE;
    app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
    g_assert(!signaled);

    signaled = FALSE;
    app_indicator_set_status(ci, APP_INDICATOR_STATUS_ATTENTION);
    g_assert(signaled);

    return;
}

void
test_libappindicator_init_set_props (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

        AppIndicator * ci = app_indicator_new ("my-id",
                                               "my-name",
                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);

    app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_attention_icon_full (ci, "my-attention-name", NULL);
    app_indicator_set_title(ci, "My Title");

    g_assert(!g_strcmp0("my-id", app_indicator_get_id(ci)));
    g_assert(!g_strcmp0("my-name", app_indicator_get_icon(ci)));
    g_assert(!g_strcmp0("my-attention-name", app_indicator_get_attention_icon(ci)));
    g_assert(!g_strcmp0("My Title", app_indicator_get_title(ci)));
    g_assert(app_indicator_get_status(ci) == APP_INDICATOR_STATUS_ACTIVE);
    g_assert(app_indicator_get_category(ci) == APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_init_with_props (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

        AppIndicator * ci = app_indicator_new ("my-id",
                                               "my-name",
                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

        app_indicator_set_status (ci, APP_INDICATOR_STATUS_ACTIVE);
        app_indicator_set_attention_icon_full (ci, "my-attention-name", NULL);

    g_assert(ci != NULL);

    g_assert(!g_strcmp0("my-id", app_indicator_get_id(ci)));
    g_assert(!g_strcmp0("my-name", app_indicator_get_icon(ci)));
    g_assert(!g_strcmp0("my-attention-name", app_indicator_get_attention_icon(ci)));
    g_assert(app_indicator_get_status(ci) == APP_INDICATOR_STATUS_ACTIVE);
    g_assert(app_indicator_get_category(ci) == APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_init (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

        AppIndicator * ci = app_indicator_new ("my-id", "my-name", APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
    g_assert(ci != NULL);
    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_set_label (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

    AppIndicator * ci = app_indicator_new ("my-id",
                                           "my-name",
                                           APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);
    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    /* First check all the clearing modes, this is important as
       we're going to use them later, we need them to work. */
    app_indicator_set_label(ci, NULL, NULL);

    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, "", NULL);

    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, NULL, "");

    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, "", "");

    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, "label", "");

    g_assert(g_strcmp0(app_indicator_get_label(ci), "label") == 0);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, NULL, NULL);

    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_set_label(ci, "label", "guide");

    g_assert(g_strcmp0(app_indicator_get_label(ci), "label") == 0);
    g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "guide") == 0);

    app_indicator_set_label(ci, "label2", "guide");

    g_assert(g_strcmp0(app_indicator_get_label(ci), "label2") == 0);
    g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "guide") == 0);

    app_indicator_set_label(ci, "trick-label", "trick-guide");

    g_assert(g_strcmp0(app_indicator_get_label(ci), "trick-label") == 0);
    g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "trick-guide") == 0);

    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_set_menu (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

    AppIndicator * ci = app_indicator_new ("my-id",
                                           "my-name",
                                           APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);

    GtkMenu * menu = GTK_MENU(gtk_menu_new());

    GtkMenuItem * item = GTK_MENU_ITEM(gtk_menu_item_new_with_label("Test Label"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), GTK_WIDGET(item));
    gtk_widget_show(GTK_WIDGET(item));

    app_indicator_set_menu(ci, menu);

    g_assert(app_indicator_get_menu(ci) != NULL);

    GValue serverval = {0};
    g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
    g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);

    DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
    g_assert(server != NULL);

    GValue rootval = {0};
    g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
    g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
    DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
    g_assert(root != NULL);

    GList * children = dbusmenu_menuitem_get_children(root);
    g_assert(children != NULL);
    g_assert(g_list_length(children) == 1);

    const gchar * label = dbusmenu_menuitem_property_get(DBUSMENU_MENUITEM(children->data), DBUSMENU_MENUITEM_PROP_LABEL);
    g_assert(label != NULL);
    g_assert(g_strcmp0(label, "Test Label") == 0);

    /* Interesting, eh?  We need this because we send out events on the bus
       but they don't come back until the idle is run.  So we need those
       events to clear before removing the object */
    while (g_main_context_pending(NULL)) {
        g_main_context_iteration(NULL, TRUE);
    }

    g_object_unref(G_OBJECT(ci));
    return;
}

void
label_signals_cb (AppIndicator * appindicator, gchar * label, gchar * guide, gpointer user_data)
{
    gint * label_signals_count = (gint *)user_data;
    (*label_signals_count)++;
    return;
}

void
label_signals_check (void)
{
    while (g_main_context_pending(NULL)) {
        g_main_context_iteration(NULL, TRUE);
    }

    return;
}

void
test_libappindicator_label_signals (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

    gint label_signals_count = 0;
    AppIndicator * ci = app_indicator_new ("my-id",
                                           "my-name",
                                           APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);
    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    g_signal_connect(G_OBJECT(ci), APP_INDICATOR_SIGNAL_NEW_LABEL, G_CALLBACK(label_signals_cb), &label_signals_count);

    /* Shouldn't be a signal as it should be stuck in idle */
    app_indicator_set_label(ci, "label", "guide");
    g_assert(label_signals_count == 0);

    /* Should show up after idle processing */
    label_signals_check();
    g_assert(label_signals_count == 1);

    /* Shouldn't signal with no change */
    label_signals_count = 0;
    app_indicator_set_label(ci, "label", "guide");
    label_signals_check();
    g_assert(label_signals_count == 0);

    /* Change one, we should get one signal */
    app_indicator_set_label(ci, "label2", "guide");
    label_signals_check();
    g_assert(label_signals_count == 1);

    /* Change several times, one signal */
    label_signals_count = 0;
    app_indicator_set_label(ci, "label1", "guide0");
    app_indicator_set_label(ci, "label1", "guide1");
    app_indicator_set_label(ci, "label2", "guide2");
    app_indicator_set_label(ci, "label3", "guide3");
    label_signals_check();
    g_assert(label_signals_count == 1);

    /* Clear should signal too */
    label_signals_count = 0;
    app_indicator_set_label(ci, NULL, NULL);
    label_signals_check();
    g_assert(label_signals_count == 1);

    return;
}

void
test_libappindicator_desktop_menu (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

    AppIndicator * ci = app_indicator_new ("my-id-desktop-menu",
                                           "my-name",
                                           APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);
    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_build_menu_from_desktop(ci, SRCDIR "/test-libappindicator.desktop", "Test Program");

    GValue serverval = {0};
    g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
    g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);

    DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
    g_assert(server != NULL);

    GValue rootval = {0};
    g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
    g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
    DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
    g_assert(root != NULL);

    GList * children = dbusmenu_menuitem_get_children(root);
    g_assert(children != NULL);
    g_assert(g_list_length(children) == 3);



    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_desktop_menu_bad (void)
{
    g_test_log_set_fatal_handler (allow_warnings, NULL);

    AppIndicator * ci = app_indicator_new ("my-id-desktop-menu-bad",
                                           "my-name",
                                           APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    g_assert(ci != NULL);
    g_assert(app_indicator_get_label(ci) == NULL);
    g_assert(app_indicator_get_label_guide(ci) == NULL);

    app_indicator_build_menu_from_desktop(ci, SRCDIR "/test-libappindicator.desktop", "Not Test Program");

    GValue serverval = {0};
    g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
    g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);

    DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
    g_assert(server != NULL);

    GValue rootval = {0};
    g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
    g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
    DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
    g_assert(root != NULL);

    GList * children = dbusmenu_menuitem_get_children(root);
    g_assert(g_list_length(children) == 0);

    g_object_unref(G_OBJECT(ci));
    return;
}

void
test_libappindicator_props_suite (void)
{
    g_test_add_func ("/indicator-application/libappindicator/init",            test_libappindicator_init);
    g_test_add_func ("/indicator-application/libappindicator/init_props",      test_libappindicator_init_with_props);
    g_test_add_func ("/indicator-application/libappindicator/init_set_props",  test_libappindicator_init_set_props);
    g_test_add_func ("/indicator-application/libappindicator/prop_signals",    test_libappindicator_prop_signals);
    g_test_add_func ("/indicator-application/libappindicator/set_label",       test_libappindicator_set_label);
    g_test_add_func ("/indicator-application/libappindicator/set_menu",        test_libappindicator_set_menu);
    g_test_add_func ("/indicator-application/libappindicator/label_signals",   test_libappindicator_label_signals);
    g_test_add_func ("/indicator-application/libappindicator/desktop_menu",    test_libappindicator_desktop_menu);
    g_test_add_func ("/indicator-application/libappindicator/desktop_menu_bad",test_libappindicator_desktop_menu_bad);

    return;
}

gint
main (gint argc, gchar * argv[])
{
    gtk_init(&argc, &argv);
    g_test_init(&argc, &argv, NULL);

    /* Test suites */
    test_libappindicator_props_suite();


    return g_test_run ();
}