~dbusmenu-team/libdbusmenu/trunk.16.10

« back to all changes in this revision

Viewing changes to tests/test-glib-properties-client.c

Merging in the properties branch to provide some basis to work with.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
A test for libdbusmenu to ensure its quality.
 
3
 
 
4
Copyright 2009 Canonical Ltd.
 
5
 
 
6
Authors:
 
7
    Ted Gould <ted@canonical.com>
 
8
 
 
9
This program is free software: you can redistribute it and/or modify it 
 
10
under the terms of the GNU General Public License version 3, as published 
 
11
by the Free Software Foundation.
 
12
 
 
13
This program is distributed in the hope that it will be useful, but 
 
14
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
15
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
16
PURPOSE.  See the GNU General Public License for more details.
 
17
 
 
18
You should have received a copy of the GNU General Public License along 
 
19
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include <glib.h>
 
23
 
 
24
#include <libdbusmenu-glib/client.h>
 
25
#include <libdbusmenu-glib/menuitem.h>
 
26
 
 
27
#include "test-glib-properties.h"
 
28
 
 
29
static guint layouton = 0;
 
30
static GMainLoop * mainloop = NULL;
 
31
static gboolean passed = TRUE;
 
32
static guint death_timer = 0;
 
33
 
 
34
static gboolean
 
35
verify_props (DbusmenuMenuitem * mi, gchar ** properties)
 
36
{
 
37
        if (properties == NULL) {
 
38
                return TRUE;
 
39
        }
 
40
 
 
41
        /* Verify they're all there and correct */
 
42
        guint i;
 
43
        for (i = 0; properties[i] != NULL; i += 2) {
 
44
                const gchar * value = dbusmenu_menuitem_property_get(mi, properties[i]);
 
45
                if (g_strcmp0(value, properties[i + 1])) {
 
46
                        g_debug("\tFailed as property '%s' should be '%s' and is '%s'", properties[i], properties[i+1], value);
 
47
                        return FALSE;
 
48
                }
 
49
        }
 
50
 
 
51
        /* Verify that we don't have any extras */
 
52
        // GList * props = dbusmenu_menuitem_properties_list(mi);
 
53
 
 
54
        return TRUE;
 
55
}
 
56
 
 
57
static gboolean
 
58
verify_root_to_layout(DbusmenuMenuitem * mi, proplayout_t * layout)
 
59
{
 
60
        g_debug("Verifying ID: %d", layout->id);
 
61
 
 
62
        if (layout->id != dbusmenu_menuitem_get_id(mi)) {
 
63
                g_debug("\tFailed as ID %d is not equal to %d", layout->id, dbusmenu_menuitem_get_id(mi));
 
64
                return FALSE;
 
65
        }
 
66
 
 
67
        if (!verify_props(mi, layout->properties)) {
 
68
                g_debug("\tFailed as unable to verify properties.");
 
69
                return FALSE;
 
70
        }
 
71
 
 
72
        GList * children = dbusmenu_menuitem_get_children(mi);
 
73
 
 
74
        if (children == NULL && layout->submenu == NULL) {
 
75
                g_debug("\tPassed: %d", layout->id);
 
76
                return TRUE;
 
77
        }
 
78
        if (children == NULL || layout->submenu == NULL) {
 
79
                if (children == NULL) {
 
80
                        g_debug("\tFailed as there are no children but we have submenus");
 
81
                } else {
 
82
                        g_debug("\tFailed as we have children but no submenu");
 
83
                }
 
84
                return FALSE;
 
85
        }
 
86
 
 
87
        guint i = 0;
 
88
        for (i = 0; children != NULL && layout->submenu[i].id != 0; children = g_list_next(children), i++) {
 
89
                if (!verify_root_to_layout(DBUSMENU_MENUITEM(children->data), &layout->submenu[i])) {
 
90
                        return FALSE;
 
91
                }
 
92
        }
 
93
 
 
94
        if (children == NULL && layout->submenu[i].id == 0) {
 
95
                g_debug("\tPassed: %d", layout->id);
 
96
                return TRUE;
 
97
        }
 
98
 
 
99
        if (children != NULL) {
 
100
                g_debug("\tFailed as there are still children but no submenus.  (ID: %d)", layout->id);
 
101
        } else {
 
102
                g_debug("\tFailed as there are still submenus but no children.  (ID: %d)", layout->id);
 
103
        }
 
104
        return FALSE;
 
105
}
 
106
 
 
107
static gboolean
 
108
timer_func (gpointer data)
 
109
{
 
110
        g_debug("Death timer.  Oops.  Got to: %d", layouton);
 
111
        passed = FALSE;
 
112
        g_main_loop_quit(mainloop);
 
113
        return FALSE;
 
114
}
 
115
 
 
116
static gboolean layout_verify_timer (gpointer data);
 
117
 
 
118
static void
 
119
layout_updated (DbusmenuClient * client, gpointer data)
 
120
{
 
121
        g_debug("Layout Updated");
 
122
        g_timeout_add (250, layout_verify_timer, client);
 
123
        return;
 
124
}
 
125
 
 
126
static gboolean
 
127
layout_verify_timer (gpointer data)
 
128
{
 
129
        DbusmenuMenuitem * menuroot = dbusmenu_client_get_root(DBUSMENU_CLIENT(data));
 
130
        proplayout_t * layout = &layouts[layouton];
 
131
        
 
132
        if (!verify_root_to_layout(menuroot, layout)) {
 
133
                g_debug("FAILED LAYOUT: %d", layouton);
 
134
                passed = FALSE;
 
135
        } else {
 
136
                /* Extend our death */
 
137
                g_source_remove(death_timer);
 
138
                death_timer = g_timeout_add_seconds(10, timer_func, data);
 
139
        }
 
140
 
 
141
        layouton++;
 
142
        
 
143
        if (layouts[layouton].id == 0) {
 
144
                g_main_loop_quit(mainloop);
 
145
        }
 
146
 
 
147
        return FALSE;
 
148
}
 
149
 
 
150
int
 
151
main (int argc, char ** argv)
 
152
{
 
153
        g_type_init();
 
154
 
 
155
        /* Make sure the server starts up and all that */
 
156
        g_usleep(500000);
 
157
 
 
158
        DbusmenuClient * client = dbusmenu_client_new(":1.0", "/org/test");
 
159
        g_signal_connect(G_OBJECT(client), DBUSMENU_CLIENT_SIGNAL_LAYOUT_UPDATED, G_CALLBACK(layout_updated), NULL);
 
160
 
 
161
        death_timer = g_timeout_add_seconds(10, timer_func, client);
 
162
 
 
163
        mainloop = g_main_loop_new(NULL, FALSE);
 
164
        g_main_loop_run(mainloop);
 
165
 
 
166
        g_object_unref(G_OBJECT(client));
 
167
 
 
168
        if (passed) {
 
169
                g_debug("Quiting");
 
170
                return 0;
 
171
        } else {
 
172
                g_debug("Quiting as we're a failure");
 
173
                return 0;
 
174
        }
 
175
}