~mial/ubuntu/oneiric/unity/bug-791810

« back to all changes in this revision

Viewing changes to tests/unit/TestPanelService.cpp

Import the work done so far with Compiz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2010 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as published
6
 
 * by the  Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * version 3 along with this program.  If not, see
15
 
 * <http://www.gnu.org/licenses/>
16
 
 *
17
 
 * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
18
 
 *
19
 
 */
20
 
 
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include "panel-service.h"
25
 
#include <libindicator/indicator-object.h>
26
 
 
27
 
//----------------------- TESTING INDICATOR STUFF -----------------------------
28
 
 
29
 
#define TEST_TYPE_OBJECT (test_object_get_type ())
30
 
 
31
 
#define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),\
32
 
  TEST_TYPE_OBJECT, TestObject))
33
 
 
34
 
#define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),\
35
 
  TEST_TYPE_OBJECT, TestObjectClass))
36
 
 
37
 
#define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),\
38
 
  TEST_TYPE_OBJECT))
39
 
 
40
 
#define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),\
41
 
  TEST_TYPE_OBJECT))
42
 
 
43
 
#define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),\
44
 
  TEST_TYPE_OBJECT, TestObjectClass))
45
 
 
46
 
typedef struct _TestObject        TestObject;
47
 
typedef struct _TestObjectClass   TestObjectClass;
48
 
 
49
 
struct _TestObject
50
 
{
51
 
  IndicatorObject parent;
52
 
 
53
 
  GList* entries;
54
 
};
55
 
 
56
 
struct _TestObjectClass
57
 
{
58
 
  IndicatorObjectClass parent_class;
59
 
};
60
 
 
61
 
GType             test_object_get_type(void) G_GNUC_CONST;
62
 
 
63
 
IndicatorObject*       test_object_new();
64
 
 
65
 
IndicatorObjectEntry* test_object_add_entry(TestObject*  self,
66
 
                                            const gchar* label,
67
 
                                            const gchar* icon_name);
68
 
 
69
 
//--- .c
70
 
 
71
 
G_DEFINE_TYPE(TestObject, test_object, INDICATOR_OBJECT_TYPE);
72
 
 
73
 
void
74
 
test_object_dispose(GObject* object)
75
 
{
76
 
  TestObject* self = TEST_OBJECT(object);
77
 
 
78
 
  GList* e;
79
 
  for (e = self->entries; e; e = e->next)
80
 
  {
81
 
    g_slice_free(IndicatorObjectEntry, e->data);
82
 
  }
83
 
  g_list_free(self->entries);
84
 
  self->entries = NULL;
85
 
 
86
 
  G_OBJECT_CLASS(test_object_parent_class)->dispose(object);
87
 
}
88
 
 
89
 
GList*
90
 
test_object_get_entries(IndicatorObject* io)
91
 
{
92
 
  g_return_val_if_fail(TEST_IS_OBJECT(io), NULL);
93
 
 
94
 
  return g_list_copy(TEST_OBJECT(io)->entries);
95
 
}
96
 
 
97
 
guint
98
 
test_object_get_location(IndicatorObject*      io,
99
 
                         IndicatorObjectEntry* entry)
100
 
{
101
 
  g_return_val_if_fail(TEST_IS_OBJECT(io), 0);
102
 
 
103
 
  return g_list_index(TEST_OBJECT(io)->entries, entry);
104
 
}
105
 
 
106
 
void
107
 
test_object_entry_activate(IndicatorObject*      io,
108
 
                           IndicatorObjectEntry* entry,
109
 
                           guint                 timestamp)
110
 
{
111
 
 
112
 
}
113
 
 
114
 
void
115
 
test_object_class_init(TestObjectClass* klass)
116
 
{
117
 
  GObjectClass*         obj_class = G_OBJECT_CLASS(klass);
118
 
  IndicatorObjectClass* ind_class = INDICATOR_OBJECT_CLASS(klass);
119
 
 
120
 
  obj_class->dispose = test_object_dispose;
121
 
 
122
 
  ind_class->get_entries    = test_object_get_entries;
123
 
  ind_class->get_location   = test_object_get_location;
124
 
  ind_class->entry_activate = test_object_entry_activate;
125
 
}
126
 
 
127
 
void
128
 
test_object_init(TestObject* self)
129
 
{
130
 
 
131
 
}
132
 
 
133
 
IndicatorObject*
134
 
test_object_new()
135
 
{
136
 
  return (IndicatorObject*)g_object_new(TEST_TYPE_OBJECT, NULL);
137
 
}
138
 
 
139
 
IndicatorObjectEntry*
140
 
test_object_add_entry(TestObject*  self,
141
 
                      const gchar* label,
142
 
                      const gchar* icon_name)
143
 
{
144
 
  IndicatorObjectEntry* entry;
145
 
  g_return_val_if_fail(TEST_IS_OBJECT(self), NULL);
146
 
 
147
 
  entry = g_slice_new0(IndicatorObjectEntry);
148
 
  entry->label = (GtkLabel*)gtk_label_new(label);
149
 
  entry->image = icon_name ? (GtkImage*)gtk_image_new_from_icon_name(icon_name,
150
 
                                                                     GTK_ICON_SIZE_MENU) : NULL;
151
 
  entry->menu = NULL;
152
 
 
153
 
  self->entries = g_list_append(self->entries, entry);
154
 
 
155
 
  g_signal_emit(self, INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, entry, TRUE);
156
 
 
157
 
  return entry;
158
 
}
159
 
 
160
 
void
161
 
test_object_show_entry(TestObject*           self,
162
 
                       IndicatorObjectEntry* entry,
163
 
                       guint                 timestamp)
164
 
{
165
 
  g_signal_emit(self, INDICATOR_OBJECT_SIGNAL_MENU_SHOW_ID, 0, entry, timestamp);
166
 
}
167
 
 
168
 
//----------------------- /TESTING INDICATOR STUFF ----------------------------
169
 
 
170
 
//------------------------ USEFUL FUNCTIONS -----------------------------------
171
 
 
172
 
guint
173
 
get_n_indicators_in_result(GVariant* result)
174
 
{
175
 
  guint  ret = 0;
176
 
  gchar* current_object_id = NULL;
177
 
  GVariantIter* iter;
178
 
  gchar*        indicator_id;
179
 
  gchar*        entry_id;
180
 
  gchar*        label;
181
 
  gboolean      label_sensitive;
182
 
  gboolean      label_visible;
183
 
  guint32       image_type;
184
 
  gchar*        image_data;
185
 
  gboolean      image_sensitive;
186
 
  gboolean      image_visible;
187
 
 
188
 
  g_variant_get(result, "(a(sssbbusbb))", &iter);
189
 
  while (g_variant_iter_loop(iter, "(sssbbusbb)",
190
 
                             &indicator_id,
191
 
                             &entry_id,
192
 
                             &label,
193
 
                             &label_sensitive,
194
 
                             &label_visible,
195
 
                             &image_type,
196
 
                             &image_data,
197
 
                             &image_sensitive,
198
 
                             &image_visible))
199
 
  {
200
 
    if (g_strcmp0(current_object_id, indicator_id) != 0)
201
 
    {
202
 
      ret++;
203
 
      g_free(current_object_id);
204
 
      current_object_id = g_strdup(indicator_id);
205
 
    }
206
 
  }
207
 
  g_free(current_object_id);
208
 
 
209
 
  return ret;
210
 
}
211
 
 
212
 
guint
213
 
get_n_entries_in_result(GVariant* result)
214
 
{
215
 
  guint  ret = 0;
216
 
  GVariantIter* iter;
217
 
  gchar*        indicator_id;
218
 
  gchar*        name_hint;
219
 
  gchar*        entry_id;
220
 
  gchar*        label;
221
 
  gboolean      label_sensitive;
222
 
  gboolean      label_visible;
223
 
  guint32       image_type;
224
 
  gchar*        image_data;
225
 
  gboolean      image_sensitive;
226
 
  gboolean      image_visible;
227
 
  gboolean      priority;
228
 
 
229
 
  g_variant_get(result, "(a(ssssbbusbbi))", &iter);
230
 
  while (g_variant_iter_loop(iter, "(ssssbbusbbi)",
231
 
                             &indicator_id,
232
 
                             &name_hint,
233
 
                             &entry_id,
234
 
                             &label,
235
 
                             &label_sensitive,
236
 
                             &label_visible,
237
 
                             &image_type,
238
 
                             &image_data,
239
 
                             &image_sensitive,
240
 
                             &image_visible,
241
 
                             &priority))
242
 
  {
243
 
    if (g_strcmp0(entry_id, "") != 0)
244
 
      ret++;
245
 
  }
246
 
 
247
 
  return ret;
248
 
}
249
 
 
250
 
 
251
 
 
252
 
//------------------------ /USEFUL FUNCTIONS ----------------------------------
253
 
 
254
 
static void TestAllocation(void);
255
 
static void TestIndicatorLoading(void);
256
 
static void TestEmptyIndicatorObject(void);
257
 
static void TestEntryAddition(void);
258
 
static void TestEntryActivateRequest(void);
259
 
 
260
 
void
261
 
TestPanelServiceCreateSuite()
262
 
{
263
 
#define _DOMAIN "/Unit/PanelService"
264
 
 
265
 
  g_test_add_func(_DOMAIN"/Allocation", TestAllocation);
266
 
  g_test_add_func(_DOMAIN"/IndicatorLoading", TestIndicatorLoading);
267
 
  g_test_add_func(_DOMAIN"/EmptyIndicatorObject", TestEmptyIndicatorObject);
268
 
  g_test_add_func(_DOMAIN"/EntryAddition", TestEntryAddition);
269
 
  g_test_add_func(_DOMAIN"/EntryActivateRequest", TestEntryActivateRequest);
270
 
}
271
 
 
272
 
static void
273
 
TestAllocation()
274
 
{
275
 
  PanelService* service;
276
 
 
277
 
  service = panel_service_get_default();
278
 
  g_assert(PANEL_IS_SERVICE(service));
279
 
 
280
 
  g_object_unref(service);
281
 
  g_assert(PANEL_IS_SERVICE(service) == FALSE);
282
 
}
283
 
 
284
 
static void
285
 
TestIndicatorLoading()
286
 
{
287
 
  PanelService*    service;
288
 
  IndicatorObject* object;
289
 
  GList*           objects = NULL;
290
 
 
291
 
  object = test_object_new();
292
 
  g_assert(INDICATOR_IS_OBJECT(object));
293
 
  objects = g_list_append(objects, object);
294
 
 
295
 
  service = panel_service_get_default_with_indicators(objects);
296
 
  g_assert(PANEL_IS_SERVICE(service));
297
 
 
298
 
  g_assert_cmpint(panel_service_get_n_indicators(service), == , 1);
299
 
 
300
 
  g_list_free(objects);
301
 
  g_object_unref(object);
302
 
  g_object_unref(service);
303
 
}
304
 
 
305
 
static void
306
 
TestEmptyIndicatorObject()
307
 
{
308
 
  PanelService*    service;
309
 
  IndicatorObject* object;
310
 
  GList*           objects = NULL;
311
 
  GVariant*        result;
312
 
 
313
 
  object = test_object_new();
314
 
  g_assert(INDICATOR_IS_OBJECT(object));
315
 
  objects = g_list_append(objects, object);
316
 
 
317
 
  service = panel_service_get_default_with_indicators(objects);
318
 
  g_assert(PANEL_IS_SERVICE(service));
319
 
 
320
 
  g_assert_cmpint(panel_service_get_n_indicators(service), == , 1);
321
 
 
322
 
  result = panel_service_sync(service);
323
 
  g_assert(result != NULL);
324
 
 
325
 
  g_assert_cmpint(get_n_indicators_in_result(result), == , 1);
326
 
 
327
 
  g_variant_unref(result);
328
 
  g_list_free(objects);
329
 
  g_object_unref(object);
330
 
  g_object_unref(service);
331
 
}
332
 
 
333
 
static void
334
 
TestEntryAddition()
335
 
{
336
 
  PanelService* service;
337
 
  TestObject*   object;
338
 
  GList*        objects = NULL;
339
 
  GVariant*     result;
340
 
  int           i;
341
 
 
342
 
  object = (TestObject*)test_object_new();
343
 
  test_object_add_entry(object, "Hello", "gtk-apply");
344
 
  g_assert(INDICATOR_IS_OBJECT(object));
345
 
  objects = g_list_append(objects, object);
346
 
 
347
 
  service = panel_service_get_default_with_indicators(objects);
348
 
  g_assert(PANEL_IS_SERVICE(service));
349
 
 
350
 
  result = panel_service_sync(service);
351
 
  g_assert(result != NULL);
352
 
  g_assert_cmpint(get_n_entries_in_result(result), == , 1);
353
 
 
354
 
  for (i = 2; i < 10; i++)
355
 
  {
356
 
    g_variant_unref(result);
357
 
 
358
 
    test_object_add_entry(object, "Bye", "gtk-forward");
359
 
    result = panel_service_sync(service);
360
 
    g_assert_cmpint(get_n_entries_in_result(result), == , i);
361
 
  }
362
 
 
363
 
  g_variant_unref(result);
364
 
  g_list_free(objects);
365
 
  g_object_unref(object);
366
 
  g_object_unref(service);
367
 
}
368
 
 
369
 
static void
370
 
OnEntryActivateRequest(IndicatorObject* object,
371
 
                       const gchar*     entry_id,
372
 
                       const gchar*     entry_id_should_be)
373
 
{
374
 
  g_assert_cmpstr(entry_id, == , entry_id_should_be);
375
 
}
376
 
 
377
 
static void
378
 
TestEntryActivateRequest()
379
 
{
380
 
  PanelService* service;
381
 
  TestObject*   object;
382
 
  GList*        objects = NULL;
383
 
  IndicatorObjectEntry* entry;
384
 
  gchar*        id;
385
 
 
386
 
  object = (TestObject*)test_object_new();
387
 
  entry = test_object_add_entry(object, "Hello", "gtk-apply");
388
 
  id = g_strdup_printf("%p", entry);
389
 
  g_assert(INDICATOR_IS_OBJECT(object));
390
 
  objects = g_list_append(objects, object);
391
 
 
392
 
  service = panel_service_get_default_with_indicators(objects);
393
 
  g_assert(PANEL_IS_SERVICE(service));
394
 
 
395
 
  g_signal_connect(service, "entry-activate-request",
396
 
                   G_CALLBACK(OnEntryActivateRequest),
397
 
                   id);
398
 
 
399
 
  test_object_show_entry(object, entry, 1234);
400
 
 
401
 
  g_free(id);
402
 
  g_list_free(objects);
403
 
  g_object_unref(object);
404
 
  g_object_unref(service);
405
 
}