~ubuntu-branches/ubuntu/trusty/hud/trusty-updates

« back to all changes in this revision

Viewing changes to tests/test-source.c

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-20 19:43:59 UTC
  • mfrom: (1.1.26)
  • Revision ID: package-import@ubuntu.com-20140120194359-jxxxqtd4ql9elvpf
Tags: 13.10.1+14.04.20140120-0ubuntu1
* New rebuild forced
* Automatic snapshot from revision 362

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 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
6
 
 * published 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 along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 */
16
 
 
17
 
#define G_LOG_DOMAIN "test-source"
18
 
 
19
 
#include "settings.h"
20
 
#include "query.h"
21
 
#include "token.h"
22
 
#include "string-list.h"
23
 
#include "source.h"
24
 
#include "source-list.h"
25
 
#include "manual-source.h"
26
 
#include "dbusmenu-collector.h"
27
 
#include "hud-query-iface.h"
28
 
#include "test-utils.h"
29
 
#include "app-list-dummy.h"
30
 
#include "query-columns.h"
31
 
 
32
 
#include <glib-object.h>
33
 
#include <dee.h>
34
 
#include <libdbustest/dbus-test.h>
35
 
 
36
 
/* Define the global default timeout for hud_test_utils_process_mainloop */
37
 
#ifndef TEST_DEFAULT_TIMEOUT
38
 
#define TEST_DEFAULT_TIMEOUT 1000
39
 
#endif
40
 
 
41
 
/* hardcode some parameters for reasons of determinism.
42
 
 */
43
 
HudSettings hud_settings = {
44
 
  .indicator_penalty = 50,
45
 
  .add_penalty = 10,
46
 
  .drop_penalty = 10,
47
 
  .end_drop_penalty = 1,
48
 
  .swap_penalty = 15,
49
 
  .max_distance = 30
50
 
};
51
 
 
52
 
typedef struct
53
 
{
54
 
  GDBusConnection *session;
55
 
  const gchar *object_path;
56
 
  const gchar *query;
57
 
} TestSourceThreadData;
58
 
 
59
 
static HudQueryIfaceComCanonicalHudQuery*
60
 
test_source_create_proxy (TestSourceThreadData* thread_data)
61
 
{
62
 
  GError *error = NULL;
63
 
  GDBusConnection *session = G_DBUS_CONNECTION(thread_data->session);
64
 
  const gchar* name = g_dbus_connection_get_unique_name (session);
65
 
 
66
 
  HudQueryIfaceComCanonicalHudQuery *proxy =
67
 
      hud_query_iface_com_canonical_hud_query_proxy_new_sync (session,
68
 
          G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, name, thread_data->object_path, NULL,
69
 
          &error);
70
 
  g_dbus_proxy_set_default_timeout(G_DBUS_PROXY(proxy), 30 * 1000);
71
 
  if (error != NULL )
72
 
  {
73
 
    g_warning("%s %s\n", "The New Proxy request failed:", error->message);
74
 
    g_error_free (error);
75
 
    return NULL ;
76
 
  }
77
 
  else
78
 
  {
79
 
    g_debug("Created Proxy to the query: %s", thread_data->object_path);
80
 
  }
81
 
 
82
 
  /* Make sure we have a connection through the life
83
 
     of this thread. */
84
 
  g_object_set_data_full(G_OBJECT(proxy), "test-source-bus-ensure", g_object_ref(session), g_object_unref);
85
 
 
86
 
  return proxy;
87
 
}
88
 
 
89
 
static gpointer
90
 
test_source_call_close_query (gpointer user_data)
91
 
{
92
 
  HudQueryIfaceComCanonicalHudQuery *proxy = test_source_create_proxy (
93
 
      (TestSourceThreadData*) user_data);
94
 
 
95
 
  GError *error = NULL;
96
 
  if (!hud_query_iface_com_canonical_hud_query_call_close_query_sync (proxy,
97
 
      NULL, &error))
98
 
  {
99
 
    g_warning("%s %s\n", "The Close Query request failed:", error->message);
100
 
    g_error_free (error);
101
 
  }
102
 
 
103
 
  g_object_unref (proxy);
104
 
 
105
 
  return FALSE;
106
 
}
107
 
 
108
 
static gpointer
109
 
test_source_call_update_query (gpointer user_data)
110
 
{
111
 
  TestSourceThreadData* thread_data = (TestSourceThreadData*) user_data;
112
 
  HudQueryIfaceComCanonicalHudQuery *proxy = test_source_create_proxy (
113
 
      thread_data);
114
 
 
115
 
  gint model_revision;
116
 
  GError *error = NULL;
117
 
  if (!hud_query_iface_com_canonical_hud_query_call_update_query_sync (proxy,
118
 
      thread_data->query, &model_revision, NULL, &error))
119
 
  {
120
 
    g_warning("%s %s\n", "The Update Query request failed:", error->message);
121
 
    g_error_free (error);
122
 
  }
123
 
 
124
 
  g_object_unref (proxy);
125
 
 
126
 
  return FALSE;
127
 
}
128
 
 
129
 
static gpointer
130
 
test_source_call_update_app (gpointer user_data)
131
 
{
132
 
  TestSourceThreadData* thread_data = (TestSourceThreadData*) user_data;
133
 
  HudQueryIfaceComCanonicalHudQuery *proxy = test_source_create_proxy (
134
 
      thread_data);
135
 
 
136
 
  gint model_revision;
137
 
  GError *error = NULL;
138
 
  if (!hud_query_iface_com_canonical_hud_query_call_update_app_sync (proxy,
139
 
      thread_data->query, &model_revision, NULL, &error))
140
 
  {
141
 
    g_warning("%s %s\n", "The Update App request failed:", error->message);
142
 
    g_error_free (error);
143
 
  }
144
 
 
145
 
  g_object_unref (proxy);
146
 
 
147
 
  return FALSE;
148
 
}
149
 
 
150
 
static void
151
 
test_source_make_assertions (HudQuery* query, const gchar *appstack,
152
 
    const gchar *path, const gchar *name, const gchar **expected_rows,
153
 
    const guint32 *expected_distances, const gint32 *expected_starts,
154
 
        const gint32 *expected_stops, const guint expected_count)
155
 
{
156
 
  guint row;
157
 
 
158
 
  g_assert_cmpstr(hud_query_get_appstack_name(query), ==, appstack);
159
 
  g_assert_cmpstr(hud_query_get_path(query), ==, path);
160
 
  g_assert_cmpstr(hud_query_get_results_name(query), ==, name);
161
 
 
162
 
  DeeModel* model = hud_query_get_results_model (query);
163
 
  g_assert_cmpint(dee_model_get_n_rows(model), ==, expected_count);
164
 
 
165
 
  for (row = 0; row < expected_count; row++)
166
 
  {
167
 
    DeeModelIter* iter = dee_model_get_iter_at_row (model, row);
168
 
 
169
 
    g_debug("Result: %s", dee_model_get_string(model, iter, HUD_QUERY_RESULTS_COMMAND_NAME));
170
 
    g_debug("Expected: %s", expected_rows[row]);
171
 
    g_debug("Distance: %d", dee_model_get_uint32(model, iter, HUD_QUERY_RESULTS_DISTANCE));
172
 
    g_debug("Exp Distance: %d", expected_distances[row]);
173
 
 
174
 
    GVariant * highlights = dee_model_get_value(model, iter, HUD_QUERY_RESULTS_COMMAND_HIGHLIGHTS);
175
 
    g_assert(highlights != NULL);
176
 
 
177
 
    g_debug("Highlight Count: %d", (guint)g_variant_n_children(highlights));
178
 
    g_debug("Exp Highlight Cnt: %d", expected_starts[row] == -1 ? 0 : 1);
179
 
 
180
 
    if (expected_starts[row] != -1)
181
 
    {
182
 
      g_debug("Highlights: %s", g_variant_print(highlights, FALSE));
183
 
      g_debug("Exp Highlights: (%d, %d)", expected_starts[row], expected_stops[row]);
184
 
    }
185
 
 
186
 
    g_assert_cmpstr(dee_model_get_string(model, iter, HUD_QUERY_RESULTS_COMMAND_NAME), ==, expected_rows[row]);
187
 
    g_assert_cmpint(dee_model_get_uint32(model, iter, HUD_QUERY_RESULTS_DISTANCE), ==, expected_distances[row]);
188
 
        g_assert_cmpint(g_variant_n_children(highlights), ==, (expected_starts[row] == -1 ? 0 : 1));
189
 
 
190
 
    if (expected_starts[row] != -1)
191
 
    {
192
 
      GVariant * highlight = g_variant_get_child_value(highlights, 0);
193
 
      GVariant * vstart = g_variant_get_child_value(highlight, 0);
194
 
      GVariant * vstop = g_variant_get_child_value(highlight, 1);
195
 
 
196
 
      g_assert_cmpint(g_variant_get_int32(vstart), ==, expected_starts[row]);
197
 
      g_assert_cmpint(g_variant_get_int32(vstop), ==, expected_stops[row]);
198
 
 
199
 
      g_variant_unref(vstop);
200
 
      g_variant_unref(vstart);
201
 
      g_variant_unref(highlight);
202
 
    }
203
 
 
204
 
    g_variant_unref(highlights);
205
 
  }
206
 
}
207
 
 
208
 
static void
209
 
test_source_make_assertions_ext (HudQuery* query, const gchar *appstack,
210
 
    const gchar **expected_appstack_ids, const gchar **expected_appstack_icons, const guint expected_appstack_count,
211
 
    const gchar *path, const gchar *name, const gchar **expected_rows,
212
 
    const guint32 *expected_distances, const gint32 *expected_starts,
213
 
        const gint32 *expected_stops, const guint expected_count)
214
 
{
215
 
  guint row;
216
 
 
217
 
  test_source_make_assertions(query, appstack, path, name, expected_rows, expected_distances, expected_starts, expected_stops, expected_count);
218
 
 
219
 
  DeeModel* model = hud_query_get_appstack_model (query);
220
 
  g_assert_cmpint(dee_model_get_n_rows(model), ==, expected_appstack_count);
221
 
 
222
 
  for (row = 0; row < expected_appstack_count; row++)
223
 
  {
224
 
    DeeModelIter* iter = dee_model_get_iter_at_row (model, row);
225
 
 
226
 
    g_debug("Id: %s", dee_model_get_string(model, iter, HUD_QUERY_APPSTACK_APPLICATION_ID));
227
 
    g_debug("Expected Id: %s", expected_appstack_ids[row]);
228
 
    g_debug("Icon: %s", dee_model_get_string(model, iter, HUD_QUERY_APPSTACK_ICON_NAME));
229
 
    g_debug("Exp Icon: %s", expected_appstack_icons[row]);
230
 
 
231
 
    g_assert_cmpstr(dee_model_get_string(model, iter, HUD_QUERY_APPSTACK_APPLICATION_ID), ==, expected_appstack_ids[row]);
232
 
    g_assert_cmpstr(dee_model_get_string(model, iter, HUD_QUERY_APPSTACK_ICON_NAME), ==, expected_appstack_icons[row]);
233
 
  }
234
 
}
235
 
 
236
 
static HudQuery*
237
 
test_source_create_query (GDBusConnection *session, HudSource *source, HudApplicationList * list, const gchar *search, const guint query_count)
238
 
{
239
 
  g_debug ("query: [%s], on [%s]", search, g_dbus_connection_get_unique_name(session));
240
 
 
241
 
  HudQuery * query = hud_query_new (source, NULL, list, search, 1u << 30, session, NULL, query_count);
242
 
 
243
 
  return query;
244
 
}
245
 
 
246
 
static void
247
 
test_hud_query_sequence ()
248
 
{
249
 
  DbusTestService * service = NULL;
250
 
  GDBusConnection * session = NULL;
251
 
 
252
 
  hud_test_utils_start_dbusmenu_mock_app (&service, &session, JSON_SOURCE);
253
 
 
254
 
  HudDbusmenuCollector *collector = hud_dbusmenu_collector_new_for_endpoint (
255
 
      "test-id", "Prefix", "no-icon", 0, /* penalty */
256
 
      HUD_TEST_UTILS_LOADER_NAME, HUD_TEST_UTILS_LOADER_PATH,
257
 
      HUD_SOURCE_ITEM_TYPE_BACKGROUND_APP);
258
 
  g_assert(collector != NULL);
259
 
  g_assert(HUD_IS_DBUSMENU_COLLECTOR(collector));
260
 
 
261
 
  hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
262
 
 
263
 
  HudManualSource *manual_source = hud_manual_source_new("manual_app", "manual_icon");
264
 
 
265
 
  HudApplicationList * applist = HUD_APPLICATION_LIST(app_list_dummy_new(HUD_SOURCE(collector)));
266
 
 
267
 
  HudSourceList *source_list = hud_source_list_new();
268
 
  hud_source_list_add(source_list, HUD_SOURCE(collector));
269
 
  hud_source_list_add(source_list, HUD_SOURCE(manual_source));
270
 
 
271
 
  hud_source_use(HUD_SOURCE(source_list));
272
 
 
273
 
  {
274
 
    gchar *search = "ash";
275
 
    const gchar *expected[5] = { "any rash", "stray slash", "swift sad", "itch step", "mess strand" };
276
 
    const guint32 expected_distances[5] = { 10, 20, 22, 23, 25 };
277
 
    const gint32 expected_starts[5] = { 5, 8, -1, -1, -1 };
278
 
    const gint32 expected_stops[5] = { 8, 11, -1, -1, -1 };
279
 
    const gchar *appstack = "com.canonical.hud.query0.appstack";
280
 
    const gchar *path = "/com/canonical/hud/query0";
281
 
    const gchar *name = "com.canonical.hud.query0.results";
282
 
 
283
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 0);
284
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
285
 
    g_object_unref (query);
286
 
  }
287
 
 
288
 
  {
289
 
    gchar *search = "mess";
290
 
    const gchar *expected[1] = { "mess strand"};
291
 
    const guint32 expected_distances[1] = { 1 };
292
 
    const gint32 expected_starts[1] = { 0 };
293
 
    const gint32 expected_stops[1] = { 4 };
294
 
    const gchar *appstack = "com.canonical.hud.query1.appstack";
295
 
    const gchar *path = "/com/canonical/hud/query1";
296
 
    const gchar *name = "com.canonical.hud.query1.results";
297
 
 
298
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 1);
299
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
300
 
    g_object_unref (query);
301
 
  }
302
 
 
303
 
  {
304
 
    gchar *search = "dare";
305
 
    const gchar *expected[2] = { "mess strand", "bowl"};
306
 
    const guint32 expected_distances[2] = { 2, 30 };
307
 
    const gint32 expected_starts[2] = { -1, -1 };
308
 
    const gint32 expected_stops[2] = { -1, -1 };
309
 
    const gchar *appstack = "com.canonical.hud.query2.appstack";
310
 
    const gchar *path = "/com/canonical/hud/query2";
311
 
    const gchar *name = "com.canonical.hud.query2.results";
312
 
 
313
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 2);
314
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
315
 
    g_object_unref (query);
316
 
  }
317
 
 
318
 
  /* This time closing using dbus call */
319
 
  {
320
 
    gchar *search = "itch step";
321
 
    const gchar *expected[1] = { "itch step"};
322
 
    const guint32 expected_distances[1] = { 0 };
323
 
    const gint32 expected_starts[1] = { 0 };
324
 
    const gint32 expected_stops[1] = { 9 };
325
 
    const gchar *appstack = "com.canonical.hud.query3.appstack";
326
 
    const gchar *path = "/com/canonical/hud/query3";
327
 
    const gchar *name = "com.canonical.hud.query3.results";
328
 
 
329
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 3);
330
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
331
 
 
332
 
    TestSourceThreadData thread_data = {session, path};
333
 
    GThread* thread = g_thread_new ("close_query", test_source_call_close_query, &thread_data);
334
 
    hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
335
 
    g_thread_join(thread);
336
 
  }
337
 
 
338
 
  /* This time updating query using dbus call */
339
 
  {
340
 
    gchar *search = "ash";
341
 
    const gchar *expected[5] = { "any rash", "stray slash", "swift sad", "itch step", "mess strand" };
342
 
    const guint32 expected_distances[5] = { 10, 20, 22, 23, 25 };
343
 
    const gint32 expected_starts[5] = { 5, 8, -1, -1, -1 };
344
 
    const gint32 expected_stops[5] = { 8, 11, -1, -1, -1 };
345
 
    const gchar *appstack = "com.canonical.hud.query4.appstack";
346
 
    const gchar *path = "/com/canonical/hud/query4";
347
 
    const gchar *name = "com.canonical.hud.query4.results";
348
 
 
349
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 4);
350
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
351
 
 
352
 
    TestSourceThreadData thread_data = {session, path, "dare"};
353
 
    GThread* thread = g_thread_new ("update_query", test_source_call_update_query, &thread_data);
354
 
    hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
355
 
    g_thread_join(thread);
356
 
 
357
 
    const gchar *expected_after[2] = { "mess strand", "bowl"};
358
 
    const guint32 expected_distances_after[2] = { 2, 30 };
359
 
    const gint32 expected_starts_after[2] = { -1, -1 };
360
 
    const gint32 expected_stops_after[2] = { -1, -1 };
361
 
 
362
 
    test_source_make_assertions (query, appstack, path, name, expected_after, expected_distances_after, expected_starts_after, expected_stops_after, G_N_ELEMENTS(expected_after));
363
 
 
364
 
    g_object_unref (query);
365
 
  }
366
 
 
367
 
  /* Adding new data to the manual source */
368
 
  {
369
 
      gchar *search = "dare";
370
 
      const gchar *expected[2] = { "mess strand", "bowl"};
371
 
      const guint32 expected_distances[2] = { 2, 30 };
372
 
      const gint32 expected_starts[2] = { -1, -1 };
373
 
      const gint32 expected_stops[2] = { -1, -1 };
374
 
      const gchar *appstack = "com.canonical.hud.query5.appstack";
375
 
      const gchar *path = "/com/canonical/hud/query5";
376
 
      const gchar *name = "com.canonical.hud.query5.results";
377
 
 
378
 
      HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 5);
379
 
      test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
380
 
 
381
 
      HudStringList *tokens = hud_string_list_add_item("extra", NULL);
382
 
      tokens = hud_string_list_add_item("something dare", tokens);
383
 
      hud_manual_source_add(manual_source, tokens, NULL, "shortcut1", TRUE);
384
 
 
385
 
      HudStringList *tokens2 = hud_string_list_add_item("extra", NULL);
386
 
      tokens2 = hud_string_list_add_item("something else darn", tokens2);
387
 
      hud_manual_source_add(manual_source, tokens2, NULL, "shortcut2", TRUE);
388
 
 
389
 
      app_list_dummy_set_focus(APP_LIST_DUMMY(applist), HUD_SOURCE(manual_source));
390
 
 
391
 
      hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT / 2);
392
 
 
393
 
      const gchar *expected_after[2] = { "something dare", "something else darn"};
394
 
      const guint32 expected_distances_after[2] = { 0, 11 };
395
 
      const gint32 expected_starts_after[2] = { 10, -1 };
396
 
      const gint32 expected_stops_after[2] = { 14, -1 };
397
 
 
398
 
      g_assert(G_N_ELEMENTS(expected_after) == G_N_ELEMENTS(expected_distances_after));
399
 
      test_source_make_assertions (query, appstack, path, name, expected_after, expected_distances_after, expected_starts_after, expected_stops_after, G_N_ELEMENTS(expected_after));
400
 
 
401
 
      g_object_unref (query);
402
 
    }
403
 
 
404
 
  /* Test query currentSource */
405
 
  {
406
 
      gchar *search = "dare";
407
 
      const gchar *expected[2] = { "mess strand", "bowl"};
408
 
      const guint32 expected_distances[2] = { 2, 30 };
409
 
      const gint32 expected_starts[2] = { -1, -1 };
410
 
      const gint32 expected_stops[2] = { -1, -1 };
411
 
      const gchar *appstack_expected_ids[2] = { "manual_app", "test-id"};
412
 
      const gchar *appstack_expected_icons[2] = { "manual_icon", "no-icon"};
413
 
      const gchar *appstack = "com.canonical.hud.query6.appstack";
414
 
      const gchar *path = "/com/canonical/hud/query6";
415
 
      const gchar *name = "com.canonical.hud.query6.results";
416
 
 
417
 
      g_assert(G_N_ELEMENTS(appstack_expected_ids) == G_N_ELEMENTS(appstack_expected_icons));
418
 
      g_assert(G_N_ELEMENTS(expected) == G_N_ELEMENTS(expected_distances));
419
 
 
420
 
      AppListDummy * dummy = app_list_dummy_new(HUD_SOURCE(collector));
421
 
      HudQuery *query = hud_query_new (HUD_SOURCE(source_list), NULL, HUD_APPLICATION_LIST(dummy), search, 1u << 30, session, NULL, 6);
422
 
      test_source_make_assertions_ext (query, appstack, appstack_expected_ids, appstack_expected_icons, G_N_ELEMENTS(appstack_expected_ids), path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
423
 
 
424
 
      // Change the app to the manual_source
425
 
          g_debug("Changing to 'manual_app'");
426
 
      TestSourceThreadData thread_data = {session, path, "manual_app"};
427
 
      GThread* thread = g_thread_new ("update_app", test_source_call_update_app, &thread_data);
428
 
      hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
429
 
      g_thread_join(thread);
430
 
 
431
 
      const gchar *expected_after[2] = { "something dare", "something else darn"};
432
 
      const guint32 expected_distances_after[2] = { 0, 11 };
433
 
      const gint32 expected_starts_after[2] = { 10, -1 };
434
 
      const gint32 expected_stops_after[2] = { 14, -1 };
435
 
      test_source_make_assertions_ext (query, appstack, appstack_expected_ids, appstack_expected_icons, G_N_ELEMENTS(appstack_expected_ids), path, name, expected_after, expected_distances_after, expected_starts_after, expected_stops_after, G_N_ELEMENTS(expected_after));
436
 
 
437
 
      g_object_unref (query);
438
 
      g_object_unref(dummy);
439
 
    }
440
 
 
441
 
  hud_source_unuse (HUD_SOURCE(source_list) );
442
 
 
443
 
  g_object_unref (source_list);
444
 
  g_object_unref (collector);
445
 
  g_object_unref (manual_source);
446
 
  g_object_unref (applist);
447
 
 
448
 
  hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
449
 
 
450
 
  g_object_unref (service);
451
 
  hud_test_utils_wait_for_connection_close(session);
452
 
}
453
 
 
454
 
static void
455
 
test_hud_query_sequence_counter_increment ()
456
 
{
457
 
  DbusTestService * service = NULL;
458
 
  GDBusConnection * session = NULL;
459
 
 
460
 
  hud_test_utils_start_dbusmenu_mock_app (&service, &session, JSON_SOURCE);
461
 
 
462
 
  HudDbusmenuCollector *collector = hud_dbusmenu_collector_new_for_endpoint (
463
 
      "test-id", "Prefix", "no-icon", 0, /* penalty */
464
 
      HUD_TEST_UTILS_LOADER_NAME, HUD_TEST_UTILS_LOADER_PATH,
465
 
      HUD_SOURCE_ITEM_TYPE_BACKGROUND_APP);
466
 
  g_assert(collector != NULL);
467
 
  g_assert(HUD_IS_DBUSMENU_COLLECTOR(collector));
468
 
 
469
 
  hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
470
 
 
471
 
  HudManualSource *manual_source = hud_manual_source_new("manual-id", "manual-icon");
472
 
 
473
 
  HudApplicationList * applist = HUD_APPLICATION_LIST(app_list_dummy_new(HUD_SOURCE(collector)));
474
 
 
475
 
  HudSourceList *source_list = hud_source_list_new();
476
 
  hud_source_list_add(source_list, HUD_SOURCE(collector));
477
 
  hud_source_list_add(source_list, HUD_SOURCE(manual_source));
478
 
 
479
 
  hud_source_use(HUD_SOURCE(source_list));
480
 
 
481
 
  {
482
 
    gchar *search = "ash";
483
 
    const gchar *expected[5] = { "any rash", "stray slash", "swift sad", "itch step", "mess strand" };
484
 
    const guint32 expected_distances[5] = { 10, 20, 22, 23, 25 };
485
 
    const gint32 expected_starts[5] = { 5, 8, -1, -1, -1 };
486
 
    const gint32 expected_stops[5] = { 8, 11, -1, -1, -1 };
487
 
    const gchar *appstack = "com.canonical.hud.query6.appstack";
488
 
    const gchar *path = "/com/canonical/hud/query6";
489
 
    const gchar *name = "com.canonical.hud.query6.results";
490
 
 
491
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 6);
492
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
493
 
    g_object_unref (query);
494
 
  }
495
 
 
496
 
  {
497
 
    gchar *search = "mess";
498
 
    const gchar *expected[1] = { "mess strand"};
499
 
    const guint32 expected_distances[1] = { 1 };
500
 
    const gint32 expected_starts[1] = { 0 };
501
 
    const gint32 expected_stops[1] = { 4 };
502
 
    const gchar *appstack = "com.canonical.hud.query7.appstack";
503
 
    const gchar *path = "/com/canonical/hud/query7";
504
 
    const gchar *name = "com.canonical.hud.query7.results";
505
 
 
506
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 7);
507
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
508
 
    g_object_unref (query);
509
 
  }
510
 
 
511
 
  {
512
 
    gchar *search = "dare";
513
 
    const gchar *expected[2] = { "mess strand", "bowl"};
514
 
    const guint32 expected_distances[2] = { 2, 30 };
515
 
    const gint32 expected_starts[2] = { -1, -1 };
516
 
    const gint32 expected_stops[2] = { -1, -1 };
517
 
    const gchar *appstack = "com.canonical.hud.query8.appstack";
518
 
    const gchar *path = "/com/canonical/hud/query8";
519
 
    const gchar *name = "com.canonical.hud.query8.results";
520
 
 
521
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 8);
522
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
523
 
    g_object_unref (query);
524
 
  }
525
 
 
526
 
  /* This time closing using dbus call */
527
 
  {
528
 
    gchar *search = "itch step";
529
 
    const gchar *expected[1] = { "itch step"};
530
 
    const guint32 expected_distances[1] = { 0 };
531
 
    const gint32 expected_starts[1] = { 0 };
532
 
    const gint32 expected_stops[1] = { 9 };
533
 
    const gchar *appstack = "com.canonical.hud.query9.appstack";
534
 
    const gchar *path = "/com/canonical/hud/query9";
535
 
    const gchar *name = "com.canonical.hud.query9.results";
536
 
 
537
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 9);
538
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
539
 
 
540
 
    TestSourceThreadData thread_data = {session, path};
541
 
    GThread* thread = g_thread_new ("close_query", test_source_call_close_query, &thread_data);
542
 
    hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
543
 
    g_thread_join(thread);
544
 
  }
545
 
 
546
 
  /* This time updating query using dbus call */
547
 
  {
548
 
    gchar *search = "ash";
549
 
    const gchar *expected[5] = { "any rash", "stray slash", "swift sad", "itch step", "mess strand" };
550
 
    const guint32 expected_distances[5] = { 10, 20, 22, 23, 25 };
551
 
    const gint32 expected_starts[5] = { 5, 8, -1, -1, -1 };
552
 
    const gint32 expected_stops[5] = { 8, 11, -1, -1, -1 };
553
 
    const gchar *appstack = "com.canonical.hud.query10.appstack";
554
 
    const gchar *path = "/com/canonical/hud/query10";
555
 
    const gchar *name = "com.canonical.hud.query10.results";
556
 
 
557
 
    HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 10);
558
 
    test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
559
 
 
560
 
    TestSourceThreadData thread_data = {session, path, "dare"};
561
 
    GThread* thread = g_thread_new ("update_query", test_source_call_update_query, &thread_data);
562
 
    hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
563
 
    g_thread_join(thread);
564
 
 
565
 
    const gchar *expected_after[2] = { "mess strand", "bowl"};
566
 
    const guint32 expected_distances_after[2] = { 2, 30 };
567
 
    const gint32 expected_starts_after[2] = { -1, -1 };
568
 
    const gint32 expected_stops_after[2] = { -1, -1 };
569
 
 
570
 
    test_source_make_assertions (query, appstack, path, name, expected_after, expected_distances_after, expected_starts_after, expected_stops_after, G_N_ELEMENTS(expected_after));
571
 
 
572
 
    g_object_unref (query);
573
 
  }
574
 
 
575
 
  /* Adding new data to the manual source */
576
 
  {
577
 
      gchar *search = "dare";
578
 
      const gchar *expected[2] = { "mess strand", "bowl"};
579
 
      const guint32 expected_distances[2] = { 2, 30 };
580
 
      const gint32 expected_starts[2] = { -1, -1 };
581
 
      const gint32 expected_stops[2] = { -1, -1 };
582
 
      const gchar *appstack = "com.canonical.hud.query11.appstack";
583
 
      const gchar *path = "/com/canonical/hud/query11";
584
 
      const gchar *name = "com.canonical.hud.query11.results";
585
 
 
586
 
      HudQuery *query = test_source_create_query (session, HUD_SOURCE(source_list), applist, search, 11);
587
 
      test_source_make_assertions (query, appstack, path, name, expected, expected_distances, expected_starts, expected_stops, G_N_ELEMENTS(expected));
588
 
 
589
 
      HudStringList *tokens = hud_string_list_add_item("extra", NULL);
590
 
      tokens = hud_string_list_add_item("something dare", tokens);
591
 
      hud_manual_source_add(manual_source, tokens, NULL, "shortcut1", TRUE);
592
 
 
593
 
      HudStringList *tokens2 = hud_string_list_add_item("extra", NULL);
594
 
      tokens2 = hud_string_list_add_item("something else darn", tokens2);
595
 
      hud_manual_source_add(manual_source, tokens2, NULL, "shortcut2", TRUE);
596
 
 
597
 
      app_list_dummy_set_focus(APP_LIST_DUMMY(applist), HUD_SOURCE(manual_source));
598
 
 
599
 
      hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT / 2);
600
 
 
601
 
      const gchar *expected_after[2] = { "something dare", "something else darn" };
602
 
      const guint32 expected_distances_after[2] = { 0, 11 };
603
 
      const gint32 expected_starts_after[2] = { 10, -1 };
604
 
      const gint32 expected_stops_after[2] = { 14, -1 };
605
 
 
606
 
      g_assert(G_N_ELEMENTS(expected_after) == G_N_ELEMENTS(expected_distances_after));
607
 
      test_source_make_assertions (query, appstack, path, name, expected_after, expected_distances_after, expected_starts_after, expected_stops_after, G_N_ELEMENTS(expected_after));
608
 
 
609
 
      g_object_unref (query);
610
 
    }
611
 
 
612
 
  hud_source_unuse (HUD_SOURCE(source_list) );
613
 
 
614
 
  g_object_unref (source_list);
615
 
  g_object_unref (collector);
616
 
  g_object_unref (manual_source);
617
 
  g_object_unref (applist);
618
 
 
619
 
  hud_test_utils_process_mainloop (TEST_DEFAULT_TIMEOUT);
620
 
 
621
 
  g_object_unref (service);
622
 
  hud_test_utils_wait_for_connection_close(session);
623
 
}
624
 
 
625
 
int
626
 
main (int argc, char **argv)
627
 
{
628
 
#ifndef GLIB_VERSION_2_36
629
 
  g_type_init ();
630
 
#endif
631
 
 
632
 
  g_test_init (&argc, &argv, NULL);
633
 
  g_test_add_func ("/hud/source/query_sequence", test_hud_query_sequence);
634
 
  g_test_add_func ("/hud/source/query_sequence_counter_increment", test_hud_query_sequence_counter_increment);
635
 
 
636
 
  return g_test_run ();
637
 
}