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
|
/*
* Copyright (C) 2009 Christian Dywan <christian@twotoasts.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2,1 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "autotoolsconfig.h"
#include <errno.h>
#include <unistd.h>
#include <glib/gstdio.h>
#include <webkit/webkit.h>
GMainLoop* loop;
char* temporaryFilename = NULL;
WebKitDownload* theDownload = NULL;
static void
test_webkit_download_create(void)
{
WebKitNetworkRequest* request;
WebKitDownload* download;
const gchar* uri = "http://example.com";
gchar* tmpDir;
request = webkit_network_request_new(uri);
download = webkit_download_new(request);
g_object_unref(request);
g_assert_cmpstr(webkit_download_get_uri(download), ==, uri);
g_assert(webkit_download_get_network_request(download) == request);
g_assert(g_strrstr(uri, webkit_download_get_suggested_filename(download)));
g_assert(webkit_download_get_status(download) == WEBKIT_DOWNLOAD_STATUS_CREATED);
g_assert(!webkit_download_get_total_size(download));
g_assert(!webkit_download_get_current_size(download));
g_assert(!webkit_download_get_progress(download));
g_assert(!webkit_download_get_elapsed_time(download));
tmpDir = g_filename_to_uri(g_get_tmp_dir(), NULL, NULL);
webkit_download_set_destination_uri(download, tmpDir);
g_assert_cmpstr(tmpDir, ==, webkit_download_get_destination_uri(download));;
g_free(tmpDir);
g_object_unref(download);
}
static gboolean
navigation_policy_decision_requested_cb(WebKitWebView* web_view,
WebKitWebFrame* web_frame,
WebKitNetworkRequest* request,
WebKitWebNavigationAction* action,
WebKitWebPolicyDecision* decision,
gpointer data)
{
webkit_web_policy_decision_download(decision);
return TRUE;
}
static void
notify_status_cb(GObject* object, GParamSpec* pspec, gpointer data)
{
WebKitDownload* download = WEBKIT_DOWNLOAD(object);
switch (webkit_download_get_status(download)) {
case WEBKIT_DOWNLOAD_STATUS_FINISHED:
case WEBKIT_DOWNLOAD_STATUS_ERROR:
g_main_loop_quit(loop);
break;
case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
g_assert_not_reached();
break;
default:
break;
}
}
static gboolean
set_filename(gchar* filename)
{
gchar *uri = g_filename_to_uri(filename, NULL, NULL);
webkit_download_set_destination_uri(theDownload, uri);
g_free(uri);
webkit_download_start(theDownload);
return FALSE;
}
static void
handle_download_requested_cb(WebKitDownload* download,
gboolean* beenThere,
gboolean asynch)
{
theDownload = download;
*beenThere = TRUE;
if (temporaryFilename) {
if (asynch) {
g_idle_add((GSourceFunc)set_filename, temporaryFilename);
} else {
gchar *uri = g_filename_to_uri(temporaryFilename, NULL, NULL);
if (uri)
webkit_download_set_destination_uri(download, uri);
g_free(uri);
}
}
g_signal_connect(download, "notify::status",
G_CALLBACK(notify_status_cb), NULL);
}
static gboolean
download_requested_cb(WebKitWebView* web_view,
WebKitDownload* download,
gboolean* beenThere)
{
handle_download_requested_cb(download, beenThere, FALSE);
return TRUE;
}
static gboolean
download_requested_asynch_cb(WebKitWebView* web_view,
WebKitDownload* download,
gboolean* beenThere)
{
handle_download_requested_cb(download, beenThere, TRUE);
return TRUE;
}
static void
test_webkit_download_perform(gboolean asynch)
{
WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
GCallback downloadRequestCallback = NULL;
g_object_ref_sink(G_OBJECT(webView));
g_signal_connect(webView, "navigation-policy-decision-requested",
G_CALLBACK(navigation_policy_decision_requested_cb),
NULL);
if (asynch)
downloadRequestCallback = G_CALLBACK(download_requested_asynch_cb);
else
downloadRequestCallback = G_CALLBACK(download_requested_cb);
gboolean beenThere = FALSE;
g_signal_connect(webView, "download-requested",
downloadRequestCallback, &beenThere);
/* Preparation; FIXME: we should move this code to a test
* utilities file, because we have a very similar one in
* testwebframe.c */
GError *error = NULL;
gchar* filename;
int fd = g_file_open_tmp("webkit-testwebdownload-XXXXXX", &filename, &error);
close(fd);
if (error)
g_critical("Failed to open a temporary file for writing: %s.", error->message);
if (g_unlink(filename) == -1)
g_critical("Failed to delete the temporary file: %s.", g_strerror(errno));
theDownload = NULL;
temporaryFilename = filename;
loop = g_main_loop_new(NULL, TRUE);
webkit_web_view_load_uri(webView, "http://gnome.org/");
g_main_loop_run(loop);
g_assert_cmpint(beenThere, ==, TRUE);
g_assert_cmpint(g_file_test(temporaryFilename, G_FILE_TEST_IS_REGULAR), ==, TRUE);
g_unlink(temporaryFilename);
g_free(temporaryFilename);
temporaryFilename = NULL;
g_main_loop_unref(loop);
g_object_unref(webView);
}
static void
test_webkit_download_synch(void)
{
test_webkit_download_perform(FALSE);
}
static void
test_webkit_download_asynch(void)
{
test_webkit_download_perform(TRUE);
}
static gboolean mime_type_policy_decision_requested_cb(WebKitWebView* view, WebKitWebFrame* frame,
WebKitNetworkRequest* request, const char* mime_type,
WebKitWebPolicyDecision* decision, gpointer data)
{
webkit_web_policy_decision_download(decision);
return TRUE;
}
static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
{
if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED ||
webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED)
g_main_loop_quit(loop);
}
static void
test_webkit_download_data(void)
{
gboolean beenThere = FALSE;
WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
g_object_ref_sink(webView);
g_signal_connect(webView, "download-requested",
G_CALLBACK(download_requested_cb),
&beenThere);
g_signal_connect(webView, "notify::load-status",
G_CALLBACK(idle_quit_loop_cb),
NULL);
g_signal_connect(webView, "mime-type-policy-decision-requested",
G_CALLBACK(mime_type_policy_decision_requested_cb),
NULL);
loop = g_main_loop_new(NULL, TRUE);
/* We're testing for a crash, so just not crashing is a pass */
webkit_web_view_load_uri(webView, "data:application/octect-stream,");
g_main_loop_run(loop);
g_assert_cmpint(beenThere, ==, TRUE);
g_main_loop_unref(loop);
g_object_unref(webView);
}
static void notifyDownloadStatusCallback(GObject *object, GParamSpec *pspec, gpointer data)
{
WebKitDownload *download = WEBKIT_DOWNLOAD(object);
WebKitNetworkResponse *response = webkit_download_get_network_response(download);
SoupMessage *message = webkit_network_response_get_message(response);
switch (webkit_download_get_status(download)) {
case WEBKIT_DOWNLOAD_STATUS_ERROR:
g_assert_cmpint(message->status_code, ==, 404);
g_main_loop_quit(loop);
break;
case WEBKIT_DOWNLOAD_STATUS_FINISHED:
case WEBKIT_DOWNLOAD_STATUS_CANCELLED:
g_assert_not_reached();
break;
default:
break;
}
}
static void serverCallback(SoupServer *server, SoupMessage *message, const char *path, GHashTable *query, SoupClientContext *context, gpointer userData)
{
if (message->method != SOUP_METHOD_GET) {
soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
return;
}
soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
soup_message_body_complete(message->response_body);
}
static void test_webkit_download_not_found(void)
{
SoupServer *server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
soup_server_run_async(server);
soup_server_add_handler(server, NULL, serverCallback, NULL, NULL);
SoupURI *baseURI = soup_uri_new("http://127.0.0.1/");
soup_uri_set_port(baseURI, soup_server_get_port(server));
SoupURI *uri = soup_uri_new_with_base(baseURI, "/foo");
char *uriString = soup_uri_to_string(uri, FALSE);
soup_uri_free(uri);
loop = g_main_loop_new(NULL, TRUE);
WebKitNetworkRequest *request = webkit_network_request_new(uriString);
g_free (uriString);
WebKitDownload *download = webkit_download_new(request);
g_object_unref(request);
webkit_download_set_destination_uri(download, "file:///tmp/foo");
g_signal_connect(download, "notify::status", G_CALLBACK(notifyDownloadStatusCallback), NULL);
webkit_download_start(download);
g_main_loop_run(loop);
g_object_unref(download);
g_main_loop_unref(loop);
soup_uri_free(baseURI);
g_object_unref(server);
}
int main(int argc, char** argv)
{
gtk_test_init(&argc, &argv, NULL);
g_test_bug_base("https://bugs.webkit.org/");
g_test_add_func("/webkit/download/create", test_webkit_download_create);
g_test_add_func("/webkit/download/synch", test_webkit_download_synch);
g_test_add_func("/webkit/download/asynch", test_webkit_download_asynch);
g_test_add_func("/webkit/download/data", test_webkit_download_data);
g_test_add_func("/webkit/download/not-found", test_webkit_download_not_found);
return g_test_run ();
}
|