[GTK] WebKitWebView::load-failed-with-tls-errors should receive the failing URI inste...
[WebKit-https.git] / Tools / TestWebKitAPI / gtk / WebKit2Gtk / LoadTrackingTest.cpp
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
25 static 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
63 static void loadFailedCallback(WebKitWebView* webView, WebKitLoadEvent loadEvent, const char* failingURI, GError* error, LoadTrackingTest* test)
64 {
65     test->m_loadFailed = true;
66     test->m_error.reset(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(), ==, failingURI);
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
86 static gboolean loadFailedWithTLSErrorsCallback(WebKitWebView* webView, const char* failingURI, GTlsCertificate* certificate, GTlsCertificateFlags tlsErrors, LoadTrackingTest* test)
87 {
88     test->m_loadFailed = true;
89     g_assert(!webkit_web_view_is_loading(webView));
90     g_assert_cmpstr(test->m_activeURI.data(), ==, failingURI);
91     g_assert(G_IS_TLS_CERTIFICATE(certificate));
92     g_assert(tlsErrors);
93     return test->loadFailedWithTLSErrors(failingURI, certificate, tlsErrors);
94 }
95
96 static void estimatedProgressChangedCallback(GObject*, GParamSpec*, LoadTrackingTest* test)
97 {
98     test->estimatedProgressChanged();
99 }
100
101 LoadTrackingTest::LoadTrackingTest()
102     : m_runLoadUntilCompletion(false)
103     , m_loadFailed(false)
104 {
105     g_signal_connect(m_webView, "load-changed", G_CALLBACK(loadChangedCallback), this);
106     g_signal_connect(m_webView, "load-failed", G_CALLBACK(loadFailedCallback), this);
107     g_signal_connect(m_webView, "load-failed-with-tls-errors", G_CALLBACK(loadFailedWithTLSErrorsCallback), this);
108     g_signal_connect(m_webView, "notify::estimated-load-progress", G_CALLBACK(estimatedProgressChangedCallback), this);
109
110     g_assert(!webkit_web_view_get_uri(m_webView));
111 }
112
113 LoadTrackingTest::~LoadTrackingTest()
114 {
115     g_signal_handlers_disconnect_matched(m_webView, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, this);
116 }
117
118 void LoadTrackingTest::waitUntilLoadFinished()
119 {
120     m_estimatedProgress = 0;
121     m_runLoadUntilCompletion = true;
122     g_main_loop_run(m_mainLoop);
123 }
124
125 void LoadTrackingTest::provisionalLoadStarted()
126 {
127     m_loadEvents.append(ProvisionalLoadStarted);
128 }
129
130 void LoadTrackingTest::provisionalLoadReceivedServerRedirect()
131 {
132     m_loadEvents.append(ProvisionalLoadReceivedServerRedirect);
133 }
134
135 void LoadTrackingTest::provisionalLoadFailed(const gchar* failingURI, GError* error)
136 {
137     m_loadEvents.append(ProvisionalLoadFailed);
138 }
139
140 void LoadTrackingTest::loadCommitted()
141 {
142     m_loadEvents.append(LoadCommitted);
143 }
144
145 void LoadTrackingTest::loadFinished()
146 {
147     m_loadEvents.append(LoadFinished);
148     if (m_runLoadUntilCompletion)
149         g_main_loop_quit(m_mainLoop);
150 }
151
152 void LoadTrackingTest::loadFailed(const gchar* failingURI, GError* error)
153 {
154     m_loadEvents.append(LoadFailed);
155 }
156
157 bool LoadTrackingTest::loadFailedWithTLSErrors(const gchar* /*failingURI*/, GTlsCertificate*, GTlsCertificateFlags)
158 {
159     m_loadEvents.append(LoadFailedWithTLSErrors);
160     return false;
161 }
162
163 void LoadTrackingTest::estimatedProgressChanged()
164 {
165     double progress = webkit_web_view_get_estimated_load_progress(m_webView);
166     g_assert_cmpfloat(m_estimatedProgress, <, progress);
167     m_estimatedProgress = progress;
168 }
169
170 void LoadTrackingTest::loadURI(const char* uri)
171 {
172     m_loadEvents.clear();
173     m_estimatedProgress = 0;
174     m_error.reset();
175     WebViewTest::loadURI(uri);
176 }
177
178 void LoadTrackingTest::loadHtml(const char* html, const char* baseURI)
179 {
180     m_loadEvents.clear();
181     m_estimatedProgress = 0;
182     m_error.reset();
183     WebViewTest::loadHtml(html, baseURI);
184 }
185
186 void LoadTrackingTest::loadPlainText(const char* plainText)
187 {
188     m_loadEvents.clear();
189     m_estimatedProgress = 0;
190     m_error.reset();
191     WebViewTest::loadPlainText(plainText);
192 }
193
194 void LoadTrackingTest::loadBytes(GBytes* bytes, const char* mimeType, const char* encoding, const char* baseURI)
195 {
196     m_loadEvents.clear();
197     m_estimatedProgress = 0;
198     m_error.reset();
199     WebViewTest::loadBytes(bytes, mimeType, encoding, baseURI);
200 }
201
202 void LoadTrackingTest::loadRequest(WebKitURIRequest* request)
203 {
204     m_loadEvents.clear();
205     m_estimatedProgress = 0;
206     m_error.reset();
207     WebViewTest::loadRequest(request);
208 }
209
210 void LoadTrackingTest::reload()
211 {
212     m_loadEvents.clear();
213     m_estimatedProgress = 0;
214     m_error.reset();
215     webkit_web_view_reload(m_webView);
216 }
217
218 void LoadTrackingTest::goBack()
219 {
220     m_loadEvents.clear();
221     m_estimatedProgress = 0;
222     m_error.reset();
223     WebViewTest::goBack();
224 }
225
226 void LoadTrackingTest::goForward()
227 {
228     m_loadEvents.clear();
229     m_estimatedProgress = 0;
230     m_error.reset();
231     WebViewTest::goForward();
232 }