aboutsummaryrefslogtreecommitdiffstats
path: root/tests/ephy-test-utils.c
blob: 95a90ec2a111a1240a4f8f338db9905271f8e9d0 (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
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=2 sts=2 et: */
/*
 *  Copyright © 2013 Igalia S.L.
 *
 *  This program 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, or (at your option)
 *  any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "ephy-test-utils.h"

#include "ephy-embed-shell.h"

#include <glib.h>

static guint web_view_ready_counter = 0;

guint
ephy_test_utils_get_web_view_ready_counter (void)
{
  return web_view_ready_counter;
}

void
ephy_test_utils_check_ephy_web_view_address (EphyWebView *view,
                                             const gchar *address)
{
  g_assert_cmpstr (ephy_web_view_get_address (view), ==, address);
}

static void
load_changed_cb (WebKitWebView *web_view,
#ifdef HAVE_WEBKIT2
                 WebKitLoadEvent status,
#else
                 GParamSpec *pspec,
#endif
                 GMainLoop *loop)
{
#ifndef HAVE_WEBKIT2
  WebKitLoadStatus status = webkit_web_view_get_load_status (web_view);
#endif

  if (status == WEBKIT_LOAD_COMMITTED) {
    web_view_ready_counter--;
    g_signal_handlers_disconnect_by_func (web_view, load_changed_cb, loop);

    if (web_view_ready_counter == 0)
      g_main_loop_quit (loop);
  }

}

static void
wait_until_load_is_committed (WebKitWebView *web_view, GMainLoop *loop)
{
#ifdef HAVE_WEBKIT2
  g_signal_connect (web_view, "load-changed", G_CALLBACK (load_changed_cb), loop);
#else
  g_signal_connect (web_view, "notify::load-status", G_CALLBACK (load_changed_cb), loop);
#endif
}

static void
web_view_created_cb (EphyEmbedShell *shell, EphyWebView *view, GMainLoop *loop)
{
  web_view_ready_counter++;
  wait_until_load_is_committed (WEBKIT_WEB_VIEW (view), loop);
}

GMainLoop*
ephy_test_utils_setup_ensure_web_views_are_loaded (void)
{
  GMainLoop *loop;

  web_view_ready_counter = 0;

  loop = g_main_loop_new (NULL, FALSE);
  g_signal_connect (ephy_embed_shell_get_default (), "web-view-created",
                    G_CALLBACK (web_view_created_cb), loop);

  return loop;
}

void
ephy_test_utils_ensure_web_views_are_loaded (GMainLoop *loop)
{
  if (web_view_ready_counter != 0)
    g_main_loop_run (loop);

  g_signal_handlers_disconnect_by_func (ephy_embed_shell_get_default (), G_CALLBACK (web_view_created_cb), loop);
  g_assert_cmpint (web_view_ready_counter, ==, 0);
  g_main_loop_unref (loop);
}