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 27GMainLoop* loop; 28char* temporaryFilename = NULL; 29WebKitDownload* theDownload = NULL; 30 31static void 32test_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 57static gboolean 58navigation_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 69static void 70notify_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 86static gboolean 87download_requested_cb(WebKitWebView* web_view, 88 WebKitDownload* download, 89 gboolean* beenThere) 90{ 91 theDownload = download; 92 *beenThere = TRUE; 93 if (temporaryFilename) { 94 gchar *uri = g_filename_to_uri(temporaryFilename, NULL, NULL); 95 if (uri) 96 webkit_download_set_destination_uri(download, uri); 97 g_free(uri); 98 } 99 100 g_signal_connect(download, "notify::status", 101 G_CALLBACK(notify_status_cb), NULL); 102 103 return TRUE; 104} 105 106static gboolean 107set_filename(gchar* filename) 108{ 109 gchar *uri = g_filename_to_uri(filename, NULL, NULL); 110 webkit_download_set_destination_uri(theDownload, uri); 111 g_free(uri); 112 temporaryFilename = filename; 113 webkit_download_start(theDownload); 114 return FALSE; 115} 116 117static void 118test_webkit_download_perform(gboolean asynch) 119{ 120 WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new()); 121 122 g_object_ref_sink(G_OBJECT(webView)); 123 124 g_signal_connect(webView, "navigation-policy-decision-requested", 125 G_CALLBACK(navigation_policy_decision_requested_cb), 126 NULL); 127 128 gboolean beenThere = FALSE; 129 g_signal_connect(webView, "download-requested", 130 G_CALLBACK(download_requested_cb), 131 &beenThere); 132 133 /* Preparation; FIXME: we should move this code to a test 134 * utilities file, because we have a very similar one in 135 * testwebframe.c */ 136 GError *error = NULL; 137 gchar* filename; 138 int fd = g_file_open_tmp("webkit-testwebdownload-XXXXXX", &filename, &error); 139 close(fd); 140 141 if (error) 142 g_critical("Failed to open a temporary file for writing: %s.", error->message); 143 144 if (g_unlink(filename) == -1) 145 g_critical("Failed to delete the temporary file: %s.", g_strerror(errno)); 146 147 if (asynch) 148 g_idle_add((GSourceFunc)set_filename, filename); 149 else 150 temporaryFilename = filename; 151 152 theDownload = NULL; 153 154 loop = g_main_loop_new(NULL, TRUE); 155 webkit_web_view_load_uri(webView, "http://gnome.org/"); 156 g_main_loop_run(loop); 157 158 g_assert_cmpint(beenThere, ==, TRUE); 159 160 g_assert_cmpint(g_file_test(temporaryFilename, G_FILE_TEST_IS_REGULAR), ==, TRUE); 161 162 g_unlink(temporaryFilename); 163 g_free(temporaryFilename); 164 g_main_loop_unref(loop); 165 g_object_unref(webView); 166} 167 168static void 169test_webkit_download_synch(void) 170{ 171 test_webkit_download_perform(FALSE); 172} 173 174static void 175test_webkit_download_asynch(void) 176{ 177 test_webkit_download_perform(TRUE); 178} 179 180int main(int argc, char** argv) 181{ 182 g_thread_init(NULL); 183 gtk_test_init(&argc, &argv, NULL); 184 185 g_test_bug_base("https://bugs.webkit.org/"); 186 g_test_add_func("/webkit/download/create", test_webkit_download_create); 187 g_test_add_func("/webkit/download/synch", test_webkit_download_synch); 188 g_test_add_func("/webkit/download/asynch", test_webkit_download_asynch); 189 return g_test_run (); 190} 191 192#else 193 194int main(int argc, char** argv) 195{ 196 g_critical("You will need at least GTK+ 2.14.0 to run the unit tests."); 197 return 0; 198} 199 200#endif 201