1/*
2 * Copyright (C) 2011 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "LoadTrackingTest.h"
22
23#include <webkit2/webkit2.h>
24
25static void loadChangedCallback(WebKitWebView* webView, WebKitLoadEvent loadEvent, LoadTrackingTest* test)
26{
27    switch (loadEvent) {
28    case WEBKIT_LOAD_STARTED:
29        g_assert(webkit_web_view_is_loading(webView));
30        g_assert_cmpstr(test->m_activeURI.data(), ==, webkit_web_view_get_uri(webView));
31        test->provisionalLoadStarted();
32        break;
33    case WEBKIT_LOAD_REDIRECTED:
34        g_assert(webkit_web_view_is_loading(webView));
35        test->m_activeURI = webkit_web_view_get_uri(webView);
36        if (!test->m_redirectURI.isNull())
37            g_assert_cmpstr(test->m_redirectURI.data(), ==, test->m_activeURI.data());
38        test->provisionalLoadReceivedServerRedirect();
39        break;
40    case WEBKIT_LOAD_COMMITTED: {
41        g_assert(webkit_web_view_is_loading(webView));
42        g_assert_cmpstr(test->m_activeURI.data(), ==, webkit_web_view_get_uri(webView));
43
44        // Check that on committed we always have a main resource with a response.
45        WebKitWebResource* resource = webkit_web_view_get_main_resource(webView);
46        g_assert(resource);
47        g_assert(webkit_web_resource_get_response(resource));
48
49        test->loadCommitted();
50        break;
51    }
52    case WEBKIT_LOAD_FINISHED:
53        g_assert(!webkit_web_view_is_loading(webView));
54        if (!test->m_loadFailed)
55            g_assert_cmpstr(test->m_activeURI.data(), ==, webkit_web_view_get_uri(webView));
56        test->loadFinished();
57        break;
58    default:
59        g_assert_not_reached();
60    }
61}
62
63static void loadFailedCallback(WebKitWebView* webView, WebKitLoadEvent loadEvent, const char* failingURI, GError* error, LoadTrackingTest* test)
64{
65    test->m_loadFailed = true;
66    test->m_error.set(g_error_copy(error));
67
68    switch (loadEvent) {
69    case WEBKIT_LOAD_STARTED:
70        g_assert(!webkit_web_view_is_loading(webView));
71        g_assert_cmpstr(test->m_activeURI.data(), ==, webkit_web_view_get_uri(webView));
72        g_assert(error);
73        test->provisionalLoadFailed(failingURI, error);
74        break;
75    case WEBKIT_LOAD_COMMITTED:
76        g_assert(!webkit_web_view_is_loading(webView));
77        g_assert_cmpstr(test->m_activeURI.data(), ==, webkit_web_view_get_uri(webView));
78        g_assert(error);
79        test->loadFailed(failingURI, error);
80        break;
81    default:
82        g_assert_not_reached();
83    }
84}
85
86static void estimatedProgressChangedCallback(GObject*, GParamSpec*, LoadTrackingTest* test)
87{
88    test->estimatedProgressChanged();
89}
90
91LoadTrackingTest::LoadTrackingTest()
92    : m_runLoadUntilCompletion(false)
93    , m_loadFailed(false)
94{
95    g_signal_connect(m_webView, "load-changed", G_CALLBACK(loadChangedCallback), this);
96    g_signal_connect(m_webView, "load-failed", G_CALLBACK(loadFailedCallback), this);
97    g_signal_connect(m_webView, "notify::estimated-load-progress", G_CALLBACK(estimatedProgressChangedCallback), this);
98
99    g_assert(!webkit_web_view_get_uri(m_webView));
100}
101
102LoadTrackingTest::~LoadTrackingTest()
103{
104    g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
105}
106
107void LoadTrackingTest::waitUntilLoadFinished()
108{
109    m_estimatedProgress = 0;
110    m_runLoadUntilCompletion = true;
111    g_main_loop_run(m_mainLoop);
112}
113
114void LoadTrackingTest::provisionalLoadStarted()
115{
116    m_loadEvents.append(ProvisionalLoadStarted);
117}
118
119void LoadTrackingTest::provisionalLoadReceivedServerRedirect()
120{
121    m_loadEvents.append(ProvisionalLoadReceivedServerRedirect);
122}
123
124void LoadTrackingTest::provisionalLoadFailed(const gchar* failingURI, GError* error)
125{
126    m_loadEvents.append(ProvisionalLoadFailed);
127}
128
129void LoadTrackingTest::loadCommitted()
130{
131    m_loadEvents.append(LoadCommitted);
132}
133
134void LoadTrackingTest::loadFinished()
135{
136    m_loadEvents.append(LoadFinished);
137    if (m_runLoadUntilCompletion)
138        g_main_loop_quit(m_mainLoop);
139}
140
141void LoadTrackingTest::loadFailed(const gchar* failingURI, GError* error)
142{
143    m_loadEvents.append(LoadFailed);
144}
145
146void LoadTrackingTest::estimatedProgressChanged()
147{
148    double progress = webkit_web_view_get_estimated_load_progress(m_webView);
149    g_assert_cmpfloat(m_estimatedProgress, <, progress);
150    m_estimatedProgress = progress;
151}
152
153void LoadTrackingTest::loadURI(const char* uri)
154{
155    m_loadEvents.clear();
156    m_estimatedProgress = 0;
157    m_error.clear();
158    WebViewTest::loadURI(uri);
159}
160
161void LoadTrackingTest::loadHtml(const char* html, const char* baseURI)
162{
163    m_loadEvents.clear();
164    m_estimatedProgress = 0;
165    m_error.clear();
166    WebViewTest::loadHtml(html, baseURI);
167}
168
169void LoadTrackingTest::loadPlainText(const char* plainText)
170{
171    m_loadEvents.clear();
172    m_estimatedProgress = 0;
173    m_error.clear();
174    WebViewTest::loadPlainText(plainText);
175}
176
177void LoadTrackingTest::loadRequest(WebKitURIRequest* request)
178{
179    m_loadEvents.clear();
180    m_estimatedProgress = 0;
181    m_error.clear();
182    WebViewTest::loadRequest(request);
183}
184
185void LoadTrackingTest::reload()
186{
187    m_loadEvents.clear();
188    m_estimatedProgress = 0;
189    m_error.clear();
190    webkit_web_view_reload(m_webView);
191}
192
193void LoadTrackingTest::goBack()
194{
195    m_loadEvents.clear();
196    m_estimatedProgress = 0;
197    m_error.clear();
198    WebViewTest::goBack();
199}
200
201void LoadTrackingTest::goForward()
202{
203    m_loadEvents.clear();
204    m_estimatedProgress = 0;
205    m_error.clear();
206    WebViewTest::goForward();
207}
208