aboutsummaryrefslogtreecommitdiffstats
path: root/tests/ephy-session-test.c
blob: f93a2766ce017b34fb23a3e2d06c44f5abdf9e4d (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
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=2 sts=2 et: */
/*
 * Copyright © 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-debug.h"
#include "ephy-embed-container.h"
#include "ephy-embed-prefs.h"
#include "ephy-embed-private.h"
#include "ephy-file-helpers.h"
#include "ephy-private.h"
#include "ephy-settings.h"
#include "ephy-shell.h"
#include "ephy-session.h"
#include "ephy-test-utils.h"

#include <glib.h>
#include <glib/gstdio.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <string.h>

const char *session_data = 
"<?xml version=\"1.0\"?>"
"<session>"
     "<window x=\"94\" y=\"48\" width=\"1132\" height=\"684\" active-tab=\"0\" role=\"epiphany-window-67c6e8a5\">"
         "<embed url=\"about:memory\" title=\"Memory usage\"/>"
     "</window>"
"</session>";

static gboolean load_stream_retval;

static void
load_from_stream_cb (GObject *object,
                     GAsyncResult *result,
                     gpointer user_data)
{
  GMainLoop *loop = (GMainLoop *)user_data;

  load_stream_retval = ephy_session_load_from_stream_finish (EPHY_SESSION (object), result, NULL);
  g_main_loop_quit (loop);
}

static gboolean
load_session_from_string (EphySession *session,
                          const char *data)
{
  GMainLoop *loop;
  GInputStream *stream;

  loop = g_main_loop_new (NULL, FALSE);
  stream = g_memory_input_stream_new_from_data (data, -1, NULL);
  ephy_session_load_from_stream (session, stream, 0, NULL, load_from_stream_cb, loop);
  g_main_loop_run (loop);
  g_main_loop_unref (loop);

  return load_stream_retval;
}

static void
enable_delayed_loading (void)
{
  g_settings_set_boolean (EPHY_SETTINGS_MAIN,
                          EPHY_PREFS_RESTORE_SESSION_DELAYING_LOADS,
                          TRUE);
}

static void
disable_delayed_loading (void)
{
  g_settings_set_boolean (EPHY_SETTINGS_MAIN,
                          EPHY_PREFS_RESTORE_SESSION_DELAYING_LOADS,
                          FALSE);
}

static void
test_ephy_session_load (void)
{
    EphySession *session;
    gboolean ret;
    GList *l;
    EphyEmbed *embed;
    EphyWebView *view;
    GMainLoop *loop;

    disable_delayed_loading ();

    session = ephy_shell_get_session (ephy_shell_get_default ());
    g_assert (session);

    loop = ephy_test_utils_setup_ensure_web_views_are_loaded ();

    ret = load_session_from_string (session, session_data);
    g_assert (ret);

    ephy_test_utils_ensure_web_views_are_loaded (loop);

    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, 1);

    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (l->data));
    g_assert (embed);
    view = ephy_embed_get_web_view (embed);
    g_assert (view);
    ephy_test_utils_check_ephy_web_view_address (view, "ephy-about:memory");

    ephy_session_clear (session);

    enable_delayed_loading ();
}

/* FIXME: This #ifdef should be removed once bug #695437 is fixed. */
#ifdef HAVE_WEBKIT2
const char *session_data_many_windows =
"<?xml version=\"1.0\"?>"
"<session>"
     "<window x=\"100\" y=\"26\" width=\"1067\" height=\"740\" active-tab=\"0\" role=\"epiphany-window-7da420dd\">"
       "<embed url=\"about:epiphany\" title=\"Epiphany\"/>"
     "</window>"
     "<window x=\"73\" y=\"26\" width=\"1067\" height=\"740\" active-tab=\"0\" role=\"epiphany-window-1261c786\">"
       "<embed url=\"about:config\" title=\"Epiphany\"/>"
     "</window>"
"</session>";
#else
const char *session_data_many_windows =
"<?xml version=\"1.0\"?>"
"<session>"
     "<window x=\"100\" y=\"26\" width=\"1067\" height=\"740\" active-tab=\"0\" role=\"epiphany-window-7da420dd\">"
       "<embed url=\"about:epiphany\" title=\"Epiphany\"/>"
     "</window>"
     "<window x=\"73\" y=\"26\" width=\"1067\" height=\"740\" active-tab=\"0\" role=\"epiphany-window-1261c786\">"
       "<embed url=\"about:epiphany\" title=\"Epiphany\"/>"
     "</window>"
"</session>";
#endif

static void
test_ephy_session_clear (void)
{
  EphySession *session;
  GList *l;
  GMainLoop *loop;

  disable_delayed_loading ();

  session = EPHY_SESSION (ephy_shell_get_session (ephy_shell_get_default ()));

  loop = ephy_test_utils_setup_ensure_web_views_are_loaded ();

  load_session_from_string (session, session_data_many_windows);

  ephy_test_utils_ensure_web_views_are_loaded (loop);

  l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
  gtk_widget_destroy (GTK_WIDGET (l->data));

  ephy_session_clear (session);

  g_assert (gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ())) == NULL);
  g_assert (ephy_session_get_can_undo_tab_closed (session) == FALSE);
}

const char *session_data_empty = 
"";

static void
test_ephy_session_load_empty_session (void)
{
    EphySession *session;
    gboolean ret;
    GList *l;
    EphyEmbed *embed;
    EphyWebView *view;
    GMainLoop *loop;

    disable_delayed_loading ();

    session = ephy_shell_get_session (ephy_shell_get_default ());
    g_assert (session);

    loop = ephy_test_utils_setup_ensure_web_views_are_loaded ();

    ret = load_session_from_string (session, session_data_empty);
    g_assert (ret == FALSE);

    /* Loading the session should have failed, but we should still get
     * the default empty window. Got to spin the mainloop though,
     * since the fallback is done by queueing another session
     * command. */
    ephy_test_utils_ensure_web_views_are_loaded (loop);

    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, 1);

    embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (l->data));
    g_assert (embed);
    view = ephy_embed_get_web_view (embed);
    g_assert (view);
    ephy_test_utils_check_ephy_web_view_address (view, "ephy-about:overview");

    enable_delayed_loading ();
    ephy_session_clear (session);
}

static void
test_ephy_session_load_many_windows (void)
{
    EphySession *session;
    gboolean ret;
    GList *l, *p;
    EphyEmbed *embed;
    EphyWebView *view;
    GMainLoop *loop;

    disable_delayed_loading ();

    session = ephy_shell_get_session (ephy_shell_get_default ());
    g_assert (session);

    loop = ephy_test_utils_setup_ensure_web_views_are_loaded ();

    ret = load_session_from_string (session, session_data_many_windows);
    g_assert (ret);
    g_assert_cmpint (ephy_test_utils_get_web_view_ready_counter (), >=, 0);
    g_assert_cmpint (ephy_test_utils_get_web_view_ready_counter (), <=, 2);

    ephy_test_utils_ensure_web_views_are_loaded (loop);

    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, 2);

    for (p = l; p; p = p->next) {
      embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (p->data));
      g_assert (embed);
      view = ephy_embed_get_web_view (embed);
      g_assert (view);
#ifndef HAVE_WEBKIT2
      /* FIXME: This #ifndef should be removed once bug #695437 is fixed. */
      ephy_test_utils_check_ephy_web_view_address (view, "ephy-about:epiphany");
#endif
    }

    enable_delayed_loading ();
    ephy_session_clear (session);
}

static void
open_uris_after_loading_session (const char** uris, int final_num_windows)
{
    EphySession *session;
    gboolean ret;
    GList *l, *p;
    EphyEmbed *embed;
    EphyWebView *view;
    guint32 user_time;
    GMainLoop *loop;

    disable_delayed_loading ();

    session = ephy_shell_get_session (ephy_shell_get_default ());
    g_assert (session);

    loop = ephy_test_utils_setup_ensure_web_views_are_loaded ();

    user_time = gdk_x11_display_get_user_time (gdk_display_get_default ());

    ret = load_session_from_string (session, session_data_many_windows);
    g_assert (ret);
    g_assert_cmpint (ephy_test_utils_get_web_view_ready_counter (), >=, 0);
    g_assert_cmpint (ephy_test_utils_get_web_view_ready_counter (), <=, 2);

    ephy_test_utils_ensure_web_views_are_loaded (loop);

    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));

    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, 2);

    for (p = l; p; p = p->next) {
      embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (p->data));
      g_assert (embed);
      view = ephy_embed_get_web_view (embed);
      g_assert (view);
#ifndef HAVE_WEBKIT2
      /* FIXME: This #ifndef should be removed once bug #695437 is fixed. */
      ephy_test_utils_check_ephy_web_view_address (view, "ephy-about:epiphany");
#endif
    }

    /* Causing a session load here should not create new windows, since we
     * already have some.
     */
    ephy_session_save (session, "type:session_state");

    ephy_session_resume (session, user_time, NULL, NULL, NULL);

    /* Ensure the queue is processed. */
    while (gtk_events_pending ())
        gtk_main_iteration_do (FALSE);

    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, 2);

    /* We should still have only 2 windows after the session load
     * command - it should bail after noticing there are windows
     * already.
     */
    ephy_shell_open_uris (ephy_shell_get_default (), uris, 0, user_time);

    while (gtk_events_pending ())
        gtk_main_iteration_do (FALSE);

    /* We should still have 2 windows here, since the new URI should be
     * in a new tab of an existing window.
     */
    l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
    g_assert (l);
    g_assert_cmpint (g_list_length (l), ==, final_num_windows);

    enable_delayed_loading ();
    ephy_session_clear (session);
}

static void
test_ephy_session_open_uri_after_loading_session (void)
{
    const char* uris[] = { "ephy-about:epiphany", NULL };

    open_uris_after_loading_session (uris, 2);
}

static void
test_ephy_session_open_empty_uri_forces_new_window (void)
{
    const char* uris[] = { "", NULL };

    open_uris_after_loading_session (uris, 3);
}

#ifndef HAVE_WEBKIT2
static void
test_ephy_session_restore_tabs (void)
{
  EphySession *session = EPHY_SESSION (ephy_shell_get_session (ephy_shell_get_default ()));
  const char* uris[] = { "ephy-about:epiphany", "ephy-about:config", NULL };
  guint32 user_time = gdk_x11_display_get_user_time (gdk_display_get_default ());
  gboolean ret;
  GList *l;
  gchar *url;
  int n_windows;
  EphyEmbed *embed;

  disable_delayed_loading ();

  /* Nothing to restore. */
  g_assert (ephy_session_get_can_undo_tab_closed (session) == FALSE);

  ephy_shell_open_uris (ephy_shell_get_default(), uris, 0, user_time);
  while (gtk_events_pending ())
    gtk_main_iteration_do (FALSE);

  /* Nothing to restore, again. */
  g_assert (ephy_session_get_can_undo_tab_closed (session) == FALSE);

  l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (l->data));
  url = g_strdup (ephy_web_view_get_address (ephy_embed_get_web_view (embed)));
  gtk_widget_destroy (GTK_WIDGET (embed));

  /* There should be now at least one tab that can be restored. */
  g_assert (ephy_session_get_can_undo_tab_closed (session) == TRUE);

  ephy_session_undo_close_tab (session);
  while (gtk_events_pending ())
    gtk_main_iteration_do (FALSE);

  /* Nothing to restore, again. */
  g_assert (ephy_session_get_can_undo_tab_closed (session) == FALSE);

  /* The active child should now be pointing to the restored tab,
     whose address is the one we copied previously. */
  embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (l->data));
  ephy_test_utils_check_ephy_web_view_address (ephy_embed_get_web_view (embed), url);
  g_free (url);

  ephy_session_clear (session);

  ret = load_session_from_string (session, session_data_many_windows);
  g_assert (ret);

  l = gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default ()));
  n_windows = g_list_length (l);
  /* We need more than one window for the next test to make sense. */
  g_assert_cmpint (n_windows, >, 1);
  gtk_widget_destroy (GTK_WIDGET (l->data));
  /* One window is gone. */
  g_assert_cmpint (n_windows, ==, g_list_length (gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default()))) + 1);
  g_assert (ephy_session_get_can_undo_tab_closed (session) == TRUE);
  ephy_session_undo_close_tab (session);
  while (gtk_events_pending ())
    gtk_main_iteration_do (FALSE);
  /* We have the same amount of windows than before destroying one. */
  g_assert_cmpint (n_windows, ==, g_list_length (gtk_application_get_windows (GTK_APPLICATION (ephy_shell_get_default()))));

  enable_delayed_loading ();
  ephy_session_clear (session);
}
#endif

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

  setenv ("GSETTINGS_BACKEND", "memory", TRUE);

  gtk_test_init (&argc, &argv);

  ephy_debug_init ();
  ephy_embed_prefs_init ();

  if (!ephy_file_helpers_init (NULL,
                               EPHY_FILE_HELPERS_PRIVATE_PROFILE | EPHY_FILE_HELPERS_ENSURE_EXISTS,
                               NULL)) {
    g_debug ("Something wrong happened with ephy_file_helpers_init()");
    return -1;
  }

  _ephy_shell_create_instance (EPHY_EMBED_SHELL_MODE_TEST);
  g_assert (ephy_shell_get_default ());

  g_application_register (G_APPLICATION (ephy_shell_get_default ()), NULL, NULL);

  g_test_add_func ("/src/ephy-session/load",
                   test_ephy_session_load);

  g_test_add_func ("/src/ephy-session/clear",
                   test_ephy_session_clear);

  g_test_add_func ("/src/ephy-session/load-empty-session",
                   test_ephy_session_load_empty_session);

  g_test_add_func ("/src/ephy-session/load-many-windows",
                   test_ephy_session_load_many_windows);

  g_test_add_func ("/src/ephy-session/open-uri-after-loading_session",
                   test_ephy_session_open_uri_after_loading_session);

  g_test_add_func ("/src/ephy-session/open-empty-uri-forces-new-window",
                   test_ephy_session_open_empty_uri_forces_new_window);

#ifndef HAVE_WEBKIT2
  g_test_add_func("/src/ephy-session/restore-tabs",
                  test_ephy_session_restore_tabs);
#endif

  ret = g_test_run ();

  g_object_unref (ephy_shell_get_default ());
  ephy_file_helpers_shutdown ();

  return ret;
}