aboutsummaryrefslogtreecommitdiffstats
path: root/tests/ephy-history-test.c
blob: 63b4a0dd70479c3df88a887922190288d6db8bb1 (plain) (blame)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* vim: set sw=2 ts=2 sts=2 et: */
/*
 * ephy-sqlite-statement.c
 * This file is part of Epiphany
 *
 * Copyright © 2010, 2011, 2012 Igalia S.L.
 *
 * Epiphany is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Epiphany is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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 Epiphany; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "ephy-history-service.h"

#include <glib/gstdio.h>
#include <gtk/gtk.h>

static EphyHistoryService *
ensure_empty_history (const char* filename)
{
  if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
    g_unlink (filename);

  return ephy_history_service_new (filename);
}

static void
test_create_history_service (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);

  g_free (temporary_file);
  g_object_unref (service);
}

static gboolean
destroy_history_service_and_end_main_loop (EphyHistoryService *service)
{
  g_object_unref (service);
  g_assert (TRUE);
  gtk_main_quit ();

  return FALSE;
}

static void
test_create_history_service_and_destroy_later (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  g_free (temporary_file);
  g_timeout_add (100, (GSourceFunc) destroy_history_service_and_end_main_loop, service);

  gtk_main ();
}

static void
page_vist_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  g_object_unref (service);
  g_assert (result_data == NULL);
  g_assert (user_data == NULL);
  g_assert (success);
  gtk_main_quit ();
}

static void
test_create_history_entry (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);

  EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
  ephy_history_service_add_visit (service, visit, NULL, page_vist_created, NULL);
  ephy_history_page_visit_free (visit);
  g_free (temporary_file);

  gtk_main ();
}

static GList *
create_test_page_visit_list ()
{
  GList *visits = NULL;
  int i;
  for (i = 0; i < 100; i++) {
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 3, EPHY_PAGE_VISIT_TYPED));
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 5, EPHY_PAGE_VISIT_TYPED));
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 7, EPHY_PAGE_VISIT_TYPED));
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.cuteoverload.com", 8, EPHY_PAGE_VISIT_TYPED));
  }
  return visits;
}

static void
verify_create_history_entry_cb (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  GList *visits = (GList *) result_data;
  GList *baseline_visits = create_test_page_visit_list ();
  GList *current = visits;
  GList *current_baseline = baseline_visits;

  g_assert (user_data == NULL);
  g_assert (success);
  g_assert (visits != NULL);
  g_assert_cmpint (g_list_length (visits), ==, g_list_length (baseline_visits));

  while (current_baseline) {
    EphyHistoryPageVisit *visit, *baseline_visit;

    g_assert (current);
    visit = (EphyHistoryPageVisit *) current->data;
    baseline_visit = (EphyHistoryPageVisit *) current_baseline->data;

    g_assert_cmpstr (visit->url->url, ==, baseline_visit->url->url);
    g_assert_cmpstr (visit->url->title, ==, baseline_visit->url->title);
    g_assert_cmpint (visit->visit_time, ==, baseline_visit->visit_time);
    g_assert_cmpint (visit->visit_type, ==, baseline_visit->visit_type);

    current = current->next;
    current_baseline = current_baseline->next;
  }

  ephy_history_page_visit_list_free (visits);
  ephy_history_page_visit_list_free (baseline_visits);

  g_object_unref (service);
  gtk_main_quit ();
}

static void
verify_create_history_entry (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  g_assert (result_data == NULL);
  g_assert_cmpint (42, ==, GPOINTER_TO_INT(user_data)); 
  g_assert (success);
  ephy_history_service_find_visits_in_time (service, 0, 8, NULL, verify_create_history_entry_cb, NULL);
}

static void
test_create_history_entries (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);

  GList *visits = create_test_page_visit_list ();

  /* We use 42 here just to verify that user_data is passed properly to the callback */
  ephy_history_service_add_visits (service, visits, NULL, verify_create_history_entry, GINT_TO_POINTER(42));
  ephy_history_page_visit_list_free (visits);
  g_free (temporary_file);

  gtk_main ();
}

static void
get_url (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  EphyHistoryURL *url = (EphyHistoryURL *) result_data;

  g_assert (success == TRUE);
  g_assert (url != NULL);
  g_assert_cmpstr (url->title, ==, "GNOME");

  ephy_history_url_free (url);
  g_object_unref (service);
  gtk_main_quit();
}

static void
set_url_title (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  gboolean test_result = GPOINTER_TO_INT (user_data);
  g_assert (success == TRUE);

  if (test_result == FALSE) {
    g_object_unref (service);
    gtk_main_quit ();
  } else
    ephy_history_service_get_url (service, "http://www.gnome.org", NULL, get_url, NULL);
}

static void
set_url_title_visit_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", NULL, set_url_title, user_data);
}

static void
test_set_url_title_helper (gboolean test_results)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);

  EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
  ephy_history_service_add_visit (service, visit, NULL, set_url_title_visit_created, GINT_TO_POINTER (test_results));
  ephy_history_page_visit_free (visit);
  g_free (temporary_file);

  gtk_main ();
}

static void
test_set_url_title (void)
{
  test_set_url_title_helper (FALSE);
}

static void
test_set_url_title_is_correct (void)
{
  test_set_url_title_helper (TRUE);
}

static void
set_url_title_url_not_existent (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  g_assert (success == FALSE);
  g_object_unref (service);
  gtk_main_quit ();
}

static void
test_set_url_title_url_not_existent (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  g_free (temporary_file);

  ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", NULL, set_url_title_url_not_existent, NULL);

  gtk_main();
}

static void
test_get_url_done (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  EphyHistoryURL *url;
  gboolean expected_success = GPOINTER_TO_INT (user_data);

  url = (EphyHistoryURL *)result_data;

  g_assert (success == expected_success);

  if (expected_success == TRUE) {
    g_assert (url != NULL);
    g_assert_cmpstr (url->url, ==, "http://www.gnome.org");
    g_assert_cmpint (url->id, !=, -1);
    ephy_history_url_free (url);
  } else
    g_assert (url == NULL);

  g_object_unref (service);
  gtk_main_quit ();
}

static void
test_get_url_visit_added (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
  g_assert (success == TRUE);

  ephy_history_service_get_url (service, "http://www.gnome.org", NULL, test_get_url_done, user_data);
}

static void
test_get_url_helper (gboolean add_entry)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  g_free (temporary_file);

  if (add_entry == TRUE) {
    EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
    ephy_history_service_add_visit (service, visit, NULL, test_get_url_visit_added, GINT_TO_POINTER (add_entry));
    ephy_history_page_visit_free (visit);
  } else
    ephy_history_service_get_url (service, "http://www.gnome.org", NULL, test_get_url_done, GINT_TO_POINTER (add_entry));

  gtk_main();
}

static void
test_get_url (void)
{
  test_get_url_helper (TRUE);
}

static void
test_get_url_not_existent (void)
{
  test_get_url_helper (FALSE);
}

static GList *
create_visits_for_complex_tests (void)
{
  int i;
  GList *visits = NULL;

  for (i = 0; i < 10; i++)
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.gnome.org", 10 * i, EPHY_PAGE_VISIT_TYPED));
  for (i = 0; i < 30; i++)
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.wikipedia.org", 10 * i, EPHY_PAGE_VISIT_TYPED));
  for (i = 0; i < 20; i++)
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.freedesktop.org", 10 * i, EPHY_PAGE_VISIT_TYPED));
  for (i = 0; i < 5; i++)
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.musicbrainz.org", 10 * i, EPHY_PAGE_VISIT_TYPED));
  for (i = 0; i < 2; i++)
    visits = g_list_append (visits, ephy_history_page_visit_new ("http://www.webkitgtk.org", 1000 * i, EPHY_PAGE_VISIT_TYPED));

  return visits;
}

static void
verify_complex_url_query (EphyHistoryService *service,
                          gboolean success,
                          gpointer result_data,
                          gpointer user_data)
{
  EphyHistoryURL *url, *baseline;
  GList *urls = (GList*) result_data;

  /* Only one result expected. */
  g_assert_cmpint (g_list_length (urls), ==, 1);

  url = (EphyHistoryURL *) urls->data;
  baseline = (EphyHistoryURL *) user_data;

  g_assert_cmpstr (url->url, ==, baseline->url);
  g_assert_cmpuint (url->visit_count, ==, baseline->visit_count);

  g_object_unref (service);

  gtk_main_quit();
}

static void
perform_complex_url_query (EphyHistoryService *service,
                           gboolean success,
                           gpointer result_data,
                           gpointer user_data)
{
  EphyHistoryQuery *query;
  EphyHistoryURL *url;

  g_assert (success == TRUE);

  /* Get the most visited site that contains 'k'. */
  query = ephy_history_query_new ();
  query->substring_list = g_list_prepend (query->substring_list, "k");
  query->limit = 1;
  query->sort_type = EPHY_HISTORY_SORT_MV;

  /* The expected result. */
  url = ephy_history_url_new ("http://www.wikipedia.org",
                              "Wikipedia",
                              30, 30, 0);

  ephy_history_service_query_urls (service, query, NULL, verify_complex_url_query, url);
}

static void
test_complex_url_query (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  GList *visits;

  visits = create_visits_for_complex_tests ();

  ephy_history_service_add_visits (service, visits, NULL, perform_complex_url_query, NULL);

  gtk_main ();
}

static void
perform_complex_url_query_with_time_range (EphyHistoryService *service,
                                           gboolean success,
                                           gpointer result_data,
                                           gpointer user_data)
{
  EphyHistoryQuery *query;
  EphyHistoryURL *url;

  g_assert (success == TRUE);

  /* Get the most visited site that contains 'k' that was visited since timestamp 500. */
  query = ephy_history_query_new ();
  query->substring_list = g_list_prepend (query->substring_list, "k");
  query->limit = 1;
  query->sort_type = EPHY_HISTORY_SORT_MV;
  query->from = 500;

  /* The expected result. */
  url = ephy_history_url_new ("http://www.webkitgtk.org",
                              "WebKitGTK+",
                              2, 2, 0);

  ephy_history_service_query_urls (service, query, NULL, verify_complex_url_query, url);
}

static void
test_complex_url_query_with_time_range (void)
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  GList *visits;

  visits = create_visits_for_complex_tests ();

  ephy_history_service_add_visits (service, visits, NULL, perform_complex_url_query_with_time_range, NULL);

  gtk_main ();
}

static void
verify_query_after_clear (EphyHistoryService *service,
                          gboolean success,
                          gpointer result_data,
                          gpointer user_data)
{
  GList *urls = (GList*)result_data;

  /* No results expected. */
  g_assert_cmpint (g_list_length (urls), ==, 0);

  g_object_unref (service);

  gtk_main_quit();
}

static void
perform_query_after_clear (EphyHistoryService *service,
                           gboolean success,
                           gpointer result_data,
                           gpointer user_data)
{
  EphyHistoryQuery *query;

  g_assert (success == TRUE);

  /* Get 10 random sites, the query should fail. */
  query = ephy_history_query_new ();
  query->substring_list = g_list_prepend (query->substring_list, "gnome");
  query->limit = 10;
  query->sort_type = EPHY_HISTORY_SORT_MV;

  ephy_history_service_query_urls (service, query, NULL, verify_query_after_clear, NULL);
}

static void
test_clear ()
{
  gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
  EphyHistoryService *service = ensure_empty_history (temporary_file);
  GList *visits = create_test_page_visit_list ();

  ephy_history_service_add_visits (service, visits, NULL, NULL, NULL);
  ephy_history_service_clear (service, NULL, perform_query_after_clear, NULL);

  gtk_main ();
}

int
main (int argc, char *argv[])
{
  gtk_test_init (&argc, &argv);

  g_test_add_func ("/embed/history/test_create_history_service", test_create_history_service);
  g_test_add_func ("/embed/history/test_create_history_service_and_destroy_later", test_create_history_service_and_destroy_later);
  g_test_add_func ("/embed/history/test_create_history_entry", test_create_history_entry);
  g_test_add_func ("/embed/history/test_create_history_entries", test_create_history_entries);
  g_test_add_func ("/embed/history/test_set_url_title", test_set_url_title);
  g_test_add_func ("/embed/history/test_set_url_title_is_correct", test_set_url_title_is_correct);
  g_test_add_func ("/embed/history/test_set_url_title_url_not_existent", test_set_url_title_url_not_existent);
  g_test_add_func ("/embed/history/test_get_url", test_get_url);
  g_test_add_func ("/embed/history/test_get_url_not_existent", test_get_url_not_existent);
  g_test_add_func ("/embed/history/test_complex_url_query", test_complex_url_query);
  g_test_add_func ("/embed/history/test_complex_url_query_with_time_range", test_complex_url_query_with_time_range);
  g_test_add_func ("/embed/history/test_clear", test_clear);

  return g_test_run ();
}