1 /*
2 * Copyright (C) 2009 Christian Dywan <christian@twotoasts.de>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2,1 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 <errno.h>
21 #include <unistd.h>
22 #include <glib/gstdio.h>
23 #include <webkit/webkit.h>
24
25 #if GTK_CHECK_VERSION(2, 14, 0)
26
27 GMainLoop* loop;
28 char* temporaryFilename = NULL;
29 WebKitDownload* theDownload = NULL;
30
31 static void
test_webkit_download_create(void)32 test_webkit_download_create(void)
33 {
34 WebKitNetworkRequest* request;
35 WebKitDownload* download;
36 const gchar* uri = "http://example.com";
37 gchar* tmpDir;
38
39 request = webkit_network_request_new(uri);
40 download = webkit_download_new(request);
41 g_object_unref(request);
42 g_assert_cmpstr(webkit_download_get_uri(download), ==, uri);
43 g_assert(webkit_download_get_network_request(download) == request);
44 g_assert(g_strrstr(uri, webkit_download_get_suggested_filename(download)));
45 g_assert(webkit_download_get_status(download) == WEBKIT_DOWNLOAD_STATUS_CREATED);
46 g_assert(!webkit_download_get_total_size(download));
47 g_assert(!webkit_download_get_current_size(download));
48 g_assert(!webkit_download_get_progress(download));
49 g_assert(!webkit_download_get_elapsed_time(download));
50 tmpDir = g_filename_to_uri(g_get_tmp_dir(), NULL, NULL);
51 webkit_download_set_destination_uri(download, tmpDir);
52 g_assert_cmpstr(tmpDir, ==, webkit_download_get_destination_uri(download));;
53 g_free(tmpDir);
54 g_object_unref(download);
55 }
56
57 static gboolean
navigation_policy_decision_requested_cb(WebKitWebView * web_view,WebKitWebFrame * web_frame,WebKitNetworkRequest * request,WebKitWebNavigationAction * action,WebKitWebPolicyDecision * decision,gpointer data)58 navigation_policy_decision_requested_cb(WebKitWebView* web_view,
59 WebKitWebFrame* web_frame,
60 WebKitNetworkRequest* request,
61 WebKitWebNavigationAction* action,
62 WebKitWebPolicyDecision* decision,
63 gpointer data)
64 {
65 webkit_web_policy_decision_download(decision);
66 return TRUE;
67 }
68
69 static void
notify_status_cb(GObject * object,GParamSpec * pspec,gpointer data)70 notify_status_cb(GObject* object, GParamSpec* pspec, gpointer data)
71 {
72 WebKitDownload* download = WEBKIT_DOWNLOAD(object);
73 switch (webkit_download_get_status(download)) {
74 case WEBKIT_DOWNLOAD_STATUS_FINISHED:
75 case WEBKIT_DOWNLOAD_STATUS_ERROR:
76 g_main_loop_quit(loop);
77 break;
78 case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
79 g_assert_not_reached();
80 break;
81 default:
82 break;
83 }
84 }
85
86 static gboolean
set_filename(gchar * filename)87 set_filename(gchar* filename)
88 {
89 gchar *uri = g_filename_to_uri(filename, NULL, NULL);
90
91 webkit_download_set_destination_uri(theDownload, uri);
92 g_free(uri);
93
94 webkit_download_start(theDownload);
95 return FALSE;
96 }
97
98 static void
handle_download_requested_cb(WebKitDownload * download,gboolean * beenThere,gboolean asynch)99 handle_download_requested_cb(WebKitDownload* download,
100 gboolean* beenThere,
101 gboolean asynch)
102 {
103 theDownload = download;
104 *beenThere = TRUE;
105
106 if (temporaryFilename) {
107 if (asynch) {
108 g_idle_add((GSourceFunc)set_filename, temporaryFilename);
109 } else {
110 gchar *uri = g_filename_to_uri(temporaryFilename, NULL, NULL);
111 if (uri)
112 webkit_download_set_destination_uri(download, uri);
113 g_free(uri);
114 }
115 }
116
117 g_signal_connect(download, "notify::status",
118 G_CALLBACK(notify_status_cb), NULL);
119 }
120
121 static gboolean
download_requested_cb(WebKitWebView * web_view,WebKitDownload * download,gboolean * beenThere)122 download_requested_cb(WebKitWebView* web_view,
123 WebKitDownload* download,
124 gboolean* beenThere)
125 {
126 handle_download_requested_cb(download, beenThere, FALSE);
127 return TRUE;
128 }
129
130 static gboolean
download_requested_asynch_cb(WebKitWebView * web_view,WebKitDownload * download,gboolean * beenThere)131 download_requested_asynch_cb(WebKitWebView* web_view,
132 WebKitDownload* download,
133 gboolean* beenThere)
134 {
135 handle_download_requested_cb(download, beenThere, TRUE);
136 return TRUE;
137 }
138
139 static void
test_webkit_download_perform(gboolean asynch)140 test_webkit_download_perform(gboolean asynch)
141 {
142 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
143 GCallback downloadRequestCallback = NULL;
144
145 g_object_ref_sink(G_OBJECT(webView));
146
147 g_signal_connect(webView, "navigation-policy-decision-requested",
148 G_CALLBACK(navigation_policy_decision_requested_cb),
149 NULL);
150
151 if (asynch)
152 downloadRequestCallback = G_CALLBACK(download_requested_asynch_cb);
153 else
154 downloadRequestCallback = G_CALLBACK(download_requested_cb);
155
156 gboolean beenThere = FALSE;
157 g_signal_connect(webView, "download-requested",
158 downloadRequestCallback, &beenThere);
159
160 /* Preparation; FIXME: we should move this code to a test
161 * utilities file, because we have a very similar one in
162 * testwebframe.c */
163 GError *error = NULL;
164 gchar* filename;
165 int fd = g_file_open_tmp("webkit-testwebdownload-XXXXXX", &filename, &error);
166 close(fd);
167
168 if (error)
169 g_critical("Failed to open a temporary file for writing: %s.", error->message);
170
171 if (g_unlink(filename) == -1)
172 g_critical("Failed to delete the temporary file: %s.", g_strerror(errno));
173
174 theDownload = NULL;
175 temporaryFilename = filename;
176
177 loop = g_main_loop_new(NULL, TRUE);
178 webkit_web_view_load_uri(webView, "http://gnome.org/");
179 g_main_loop_run(loop);
180
181 g_assert_cmpint(beenThere, ==, TRUE);
182
183 g_assert_cmpint(g_file_test(temporaryFilename, G_FILE_TEST_IS_REGULAR), ==, TRUE);
184
185 g_unlink(temporaryFilename);
186 g_free(temporaryFilename);
187 temporaryFilename = NULL;
188
189 g_main_loop_unref(loop);
190 g_object_unref(webView);
191 }
192
193 static void
test_webkit_download_synch(void)194 test_webkit_download_synch(void)
195 {
196 test_webkit_download_perform(FALSE);
197 }
198
199 static void
test_webkit_download_asynch(void)200 test_webkit_download_asynch(void)
201 {
202 test_webkit_download_perform(TRUE);
203 }
204
mime_type_policy_decision_requested_cb(WebKitWebView * view,WebKitWebFrame * frame,WebKitNetworkRequest * request,const char * mime_type,WebKitWebPolicyDecision * decision,gpointer data)205 static gboolean mime_type_policy_decision_requested_cb(WebKitWebView* view, WebKitWebFrame* frame,
206 WebKitNetworkRequest* request, const char* mime_type,
207 WebKitWebPolicyDecision* decision, gpointer data)
208 {
209 webkit_web_policy_decision_download(decision);
210 return TRUE;
211 }
212
idle_quit_loop_cb(WebKitWebView * web_view,GParamSpec * pspec,gpointer data)213 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
214 {
215 if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED ||
216 webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED)
217 g_main_loop_quit(loop);
218 }
219
220 static void
test_webkit_download_data(void)221 test_webkit_download_data(void)
222 {
223 gboolean beenThere = FALSE;
224 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
225 g_object_ref_sink(webView);
226
227 g_signal_connect(webView, "download-requested",
228 G_CALLBACK(download_requested_cb),
229 &beenThere);
230
231 g_signal_connect(webView, "notify::load-status",
232 G_CALLBACK(idle_quit_loop_cb),
233 NULL);
234
235 g_signal_connect(webView, "mime-type-policy-decision-requested",
236 G_CALLBACK(mime_type_policy_decision_requested_cb),
237 NULL);
238
239 loop = g_main_loop_new(NULL, TRUE);
240
241 /* We're testing for a crash, so just not crashing is a pass */
242 webkit_web_view_load_uri(webView, "data:application/octect-stream,");
243 g_main_loop_run(loop);
244
245 g_assert_cmpint(beenThere, ==, TRUE);
246
247 g_main_loop_unref(loop);
248 g_object_unref(webView);
249 }
250
main(int argc,char ** argv)251 int main(int argc, char** argv)
252 {
253 g_thread_init(NULL);
254 gtk_test_init(&argc, &argv, NULL);
255
256 g_test_bug_base("https://bugs.webkit.org/");
257 g_test_add_func("/webkit/download/create", test_webkit_download_create);
258 g_test_add_func("/webkit/download/synch", test_webkit_download_synch);
259 g_test_add_func("/webkit/download/asynch", test_webkit_download_asynch);
260 g_test_add_func("/webkit/download/data", test_webkit_download_data);
261 return g_test_run ();
262 }
263
264 #else
265
main(int argc,char ** argv)266 int main(int argc, char** argv)
267 {
268 g_critical("You will need at least GTK+ 2.14.0 to run the unit tests.");
269 return 0;
270 }
271
272 #endif
273