~unity-team/unity/trunk

« back to all changes in this revision

Viewing changes to tests/test_service_panel.c

  • Committer: Tarmac
  • Author(s): Albert Astals
  • Date: 2012-10-09 09:48:07 UTC
  • mfrom: (2807.2.30 unity)
  • Revision ID: tarmac-20121009094807-r47h33bftvl90pnw
Do not reuse the menu entries if their order changes

Since the indicators api only have "add" and "remove" signals, if we reuse an entry that is not in the correct order it will case the menus or whatever this indicator represents to be in the wrong order. Fixes: https://bugs.launchpad.net/bugs/1062283. Approved by Marco Trevisan (Treviño).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "test_service_panel.h"
 
2
#include <unity.h>
 
3
#include <gio/gio.h>
 
4
 
 
5
static const char * panel_interface = 
 
6
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 
7
"<node name=\"/\">\n"
 
8
"        <interface name=\"com.canonical.Unity.Panel.Service\">\n"
 
9
"\n"
 
10
"<!-- Begin of real methods/signals -->\n"
 
11
"    <method name='Sync'>"
 
12
"      <arg type='a(ssssbbusbbi)' name='state' direction='out'/>"
 
13
"    </method>"
 
14
"\n"
 
15
"    <signal name='ReSync'>"
 
16
"     <arg type='s' name='indicator_id' />"
 
17
"    </signal>"
 
18
"\n"
 
19
"<!-- Begin of test only methods/signals -->\n"
 
20
"\n"
 
21
"    <method name='TriggerResync1' />"
 
22
"\n"
 
23
"    <method name='TriggerResync1Sent'>"
 
24
"      <arg type='b' name='sent' direction='out'/>"
 
25
"    </method>"
 
26
"\n"
 
27
"        </interface>\n"
 
28
"</node>\n"
 
29
;
 
30
static void bus_got_cb          (GObject *object, GAsyncResult * res, gpointer user_data);
 
31
static void bus_method          (GDBusConnection *connection,
 
32
                                const gchar *sender,
 
33
                                const gchar *object_path,
 
34
                                const gchar *interface_name,
 
35
                                const gchar *method_name,
 
36
                                GVariant *parameters,
 
37
                                GDBusMethodInvocation *invocation,
 
38
                                gpointer user_data);
 
39
 
 
40
G_DEFINE_TYPE(ServicePanel, service_panel, G_TYPE_OBJECT);
 
41
static GDBusNodeInfo * node_info = NULL;
 
42
static GDBusInterfaceInfo * iface_info = NULL;
 
43
static GDBusInterfaceVTable bus_vtable = {
 
44
  method_call: bus_method,
 
45
  get_property: NULL,
 
46
  set_property: NULL,
 
47
};
 
48
 
 
49
struct _ServicePanelPrivate
 
50
{
 
51
  GDBusConnection * bus;
 
52
  GCancellable * bus_lookup;
 
53
  guint bus_registration;
 
54
  guint sig_emission_handle;
 
55
};
 
56
 
 
57
static void
 
58
service_panel_dispose(GObject* object)
 
59
{
 
60
  ServicePanel* self = SERVICE_PANEL(object);
 
61
  if (self->priv->bus_lookup != NULL) {
 
62
    g_cancellable_cancel(self->priv->bus_lookup);
 
63
    g_object_unref(self->priv->bus_lookup);
 
64
    self->priv->bus_lookup = NULL;
 
65
  }
 
66
 
 
67
  if (self->priv->bus_registration != 0) {
 
68
    g_dbus_connection_unregister_object(self->priv->bus, self->priv->bus_registration);
 
69
    self->priv->bus_registration = 0;
 
70
  }
 
71
 
 
72
  if (self->priv->bus != NULL) {
 
73
    g_object_unref(self->priv->bus);
 
74
    self->priv->bus = NULL;
 
75
  }
 
76
 
 
77
  if (self->priv->sig_emission_handle) {
 
78
    g_source_remove(self->priv->sig_emission_handle);
 
79
    self->priv->sig_emission_handle = 0;
 
80
  }
 
81
 
 
82
}
 
83
 
 
84
static void
 
85
service_panel_class_init(ServicePanelClass* klass)
 
86
{
 
87
  G_OBJECT_CLASS(klass)->dispose = service_panel_dispose;
 
88
  g_type_class_add_private (klass, sizeof (ServicePanelPrivate));
 
89
 
 
90
  if (node_info == NULL) 
 
91
  {
 
92
    GError * error = NULL;
 
93
 
 
94
    node_info = g_dbus_node_info_new_for_xml(panel_interface, &error);
 
95
    if (error != NULL) 
 
96
    {
 
97
        g_error("Unable to parse Panel interface: %s", error->message);
 
98
        g_error_free(error);
 
99
    }
 
100
  }
 
101
 
 
102
  if (node_info != NULL && iface_info == NULL) 
 
103
  {
 
104
    iface_info = g_dbus_node_info_lookup_interface(node_info,"com.canonical.Unity.Panel.Service");
 
105
    if (iface_info == NULL) 
 
106
    {
 
107
      g_error("Unable to find interface 'com.canonical.Unity.Panel.Service'");
 
108
    }
 
109
  }
 
110
 
 
111
}
 
112
 
 
113
static void
 
114
service_panel_init(ServicePanel* self)
 
115
{
 
116
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self, SERVICE_TYPE_PANEL, ServicePanelPrivate);
 
117
  self->priv->bus = NULL;
 
118
  self->priv->bus_lookup = NULL;
 
119
  self->priv->bus_registration = 0;
 
120
  
 
121
  self->priv->bus_lookup = g_cancellable_new();
 
122
  g_bus_get(G_BUS_TYPE_SESSION, self->priv->bus_lookup, bus_got_cb, self);
 
123
}
 
124
 
 
125
ServicePanel*
 
126
service_panel_new()
 
127
{
 
128
  return g_object_new(SERVICE_TYPE_PANEL, NULL);
 
129
}
 
130
 
 
131
static void
 
132
bus_got_cb (GObject *object, GAsyncResult * res, gpointer user_data)
 
133
{
 
134
  GError * error = NULL;
 
135
  ServicePanel * self = SERVICE_PANEL(user_data);
 
136
  GDBusConnection * bus;
 
137
 
 
138
  bus = g_bus_get_finish(res, &error);
 
139
  if (error != NULL) {
 
140
    g_critical("Unable to get bus: %s", error->message);
 
141
    g_error_free(error);
 
142
    return;
 
143
  }
 
144
 
 
145
  self->priv->bus = bus;
 
146
 
 
147
  /* Register object */
 
148
  self->priv->bus_registration = g_dbus_connection_register_object(bus,
 
149
                                                  /* path */       "/com/canonical/Unity/Panel/Service",
 
150
                                                  /* interface */  iface_info,
 
151
                                                  /* vtable */     &bus_vtable,
 
152
                                                  /* userdata */   self,
 
153
                                                  /* destroy */    NULL,
 
154
                                                  /* error */      &error);
 
155
 
 
156
  if (error != NULL) {
 
157
    g_critical ("Unable to create bus connection object, %s", error->message);
 
158
    g_error_free(error);
 
159
    return;
 
160
  }
 
161
 
 
162
  return;
 
163
}
 
164
 
 
165
static void
 
166
add_entry_id(GVariantBuilder *b)
 
167
{
 
168
  g_variant_builder_add (b, "(ssssbbusbbi)",
 
169
                      "test_indicator_id",
 
170
                      "test_entry_id",
 
171
                      "test_entry_name_hint",
 
172
                      "test_entry_label",
 
173
                      TRUE, /* label sensitive */
 
174
                      TRUE, /* label visible */
 
175
                      0, /* image type */
 
176
                      "", /* image_data */
 
177
                      TRUE, /* image sensitive */
 
178
                      TRUE, /* image visible */
 
179
                      1 /* priority  */);
 
180
}
 
181
 
 
182
static void
 
183
add_entry_id_2(GVariantBuilder *b)
 
184
{
 
185
  g_variant_builder_add (b, "(ssssbbusbbi)",
 
186
                      "test_indicator_id",
 
187
                      "test_entry_id2",
 
188
                      "test_entry_name_hint2",
 
189
                      "test_entry_label2",
 
190
                      TRUE, /* label sensitive */
 
191
                      TRUE, /* label visible */
 
192
                      0, /* image type */
 
193
                      "", /* image_data */
 
194
                      TRUE, /* image sensitive */
 
195
                      TRUE, /* image visible */
 
196
                      1 /* priority  */);
 
197
}
 
198
 
 
199
static int sync_return_mode = 0;
 
200
static int trigger_resync1_sent = FALSE;
 
201
 
 
202
static void
 
203
bus_method (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data)
 
204
{
 
205
  if (g_strcmp0(method_name, "Sync") == 0) 
 
206
  {
 
207
    GVariantBuilder b;
 
208
 
 
209
    g_variant_builder_init (&b, G_VARIANT_TYPE ("(a(ssssbbusbbi))"));
 
210
    g_variant_builder_open (&b, G_VARIANT_TYPE ("a(ssssbbusbbi)"));
 
211
 
 
212
    if (sync_return_mode == 0)
 
213
    {
 
214
      add_entry_id(&b);
 
215
      add_entry_id_2(&b);
 
216
    }
 
217
    else if (sync_return_mode == 1)
 
218
    {
 
219
      add_entry_id_2(&b);
 
220
      add_entry_id(&b);
 
221
    }
 
222
 
 
223
    g_variant_builder_close (&b);
 
224
 
 
225
    g_dbus_method_invocation_return_value(invocation, g_variant_builder_end (&b));
 
226
 
 
227
    if (sync_return_mode == 1)
 
228
    {
 
229
      trigger_resync1_sent = TRUE;
 
230
    }
 
231
  }
 
232
  else if (g_strcmp0(method_name, "TriggerResync1") == 0) 
 
233
  {
 
234
    sync_return_mode = 1;
 
235
    trigger_resync1_sent = FALSE;
 
236
 
 
237
    g_dbus_method_invocation_return_value(invocation, NULL);
 
238
    GVariantBuilder ret_builder;
 
239
    g_variant_builder_init(&ret_builder, G_VARIANT_TYPE_TUPLE);
 
240
    g_variant_builder_add_value(&ret_builder, g_variant_new_string(""));
 
241
    g_dbus_connection_emit_signal (connection, NULL, "/com/canonical/Unity/Panel/Service", "com.canonical.Unity.Panel.Service", "ReSync", g_variant_builder_end(&ret_builder), NULL);
 
242
  }
 
243
  else if (g_strcmp0(method_name, "TriggerResync1Sent") == 0) 
 
244
  {
 
245
    GVariantBuilder ret_builder;
 
246
    g_variant_builder_init(&ret_builder, G_VARIANT_TYPE ("(b)"));
 
247
    g_variant_builder_add_value (&ret_builder, g_variant_new_boolean(trigger_resync1_sent));
 
248
    g_dbus_method_invocation_return_value(invocation, g_variant_builder_end (&ret_builder));
 
249
  }
 
250
 
 
251
  return;
 
252
}
 
253