1/* 2 * Copyright (C) 2009 Jan Michael Alonzo 3 * Copyright (C) 2009 Gustavo Noronha Silva 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 */ 20 21#include "autotoolsconfig.h" 22#include "test_utils.h" 23 24#include <glib.h> 25#include <glib/gstdio.h> 26#include <libsoup/soup.h> 27#include <string.h> 28#include <webkit/webkit.h> 29#include <unistd.h> 30 31GMainLoop* loop; 32SoupSession *session; 33char* base_uri; 34 35/* For real request testing */ 36static void 37server_callback(SoupServer *server, SoupMessage *msg, 38 const char *path, GHashTable *query, 39 SoupClientContext *context, gpointer data) 40{ 41 if (msg->method != SOUP_METHOD_GET) { 42 soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED); 43 return; 44 } 45 46 soup_message_set_status(msg, SOUP_STATUS_OK); 47 48 /* PDF */ 49 if (g_str_equal(path, "/pdf")) { 50 char* contents; 51 gsize length; 52 GError* error = NULL; 53 54 g_file_get_contents("test.pdf", &contents, &length, &error); 55 g_assert(!error); 56 57 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length); 58 } else if (g_str_equal(path, "/html")) { 59 char* contents; 60 gsize length; 61 GError* error = NULL; 62 63 g_file_get_contents("test.html", &contents, &length, &error); 64 g_assert(!error); 65 66 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length); 67 } else if (g_str_equal(path, "/text")) { 68 char* contents; 69 gsize length; 70 GError* error = NULL; 71 72 soup_message_headers_append(msg->response_headers, "Content-Disposition", "attachment; filename=test.txt"); 73 74 g_file_get_contents("test.txt", &contents, &length, &error); 75 g_assert(!error); 76 77 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length); 78 } 79 80 soup_message_body_complete(msg->response_body); 81} 82 83static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data) 84{ 85 if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED || 86 webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED) 87 g_main_loop_quit(loop); 88} 89 90static gboolean mime_type_policy_decision_requested_cb(WebKitWebView* view, WebKitWebFrame* frame, 91 WebKitNetworkRequest* request, const char* mime_type, 92 WebKitWebPolicyDecision* decision, gpointer data) 93{ 94 char* type = (char*)data; 95 96 if (g_str_equal(type, "pdf")) { 97 g_assert_cmpstr(mime_type, ==, "application/pdf"); 98 g_assert(!webkit_web_view_can_show_mime_type(view, mime_type)); 99 } else if (g_str_equal(type, "html")) { 100 g_assert_cmpstr(mime_type, ==, "text/html"); 101 g_assert(webkit_web_view_can_show_mime_type(view, mime_type)); 102 } else if (g_str_equal(type, "text")) { 103 WebKitNetworkResponse* response = webkit_web_frame_get_network_response(frame); 104 SoupMessage* message = webkit_network_response_get_message(response); 105 char* disposition; 106 107 g_assert(message); 108 soup_message_headers_get_content_disposition(message->response_headers, 109 &disposition, NULL); 110 g_object_unref(response); 111 112 g_assert_cmpstr(disposition, ==, "attachment"); 113 g_free(disposition); 114 115 g_assert_cmpstr(mime_type, ==, "text/plain"); 116 g_assert(webkit_web_view_can_show_mime_type(view, mime_type)); 117 } 118 119 g_free(type); 120 121 return FALSE; 122} 123 124static void testRemoteMimeType(const void* data) 125{ 126 const char* name = (const char*) data; 127 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new()); 128 g_object_ref_sink(G_OBJECT(view)); 129 130 loop = g_main_loop_new(NULL, TRUE); 131 132 g_object_connect(G_OBJECT(view), 133 "signal::notify::load-status", idle_quit_loop_cb, NULL, 134 "signal::mime-type-policy-decision-requested", mime_type_policy_decision_requested_cb, g_strdup(name), 135 NULL); 136 137 char* effective_uri = g_strdup_printf("%s%s", base_uri, name); 138 webkit_web_view_load_uri(view, effective_uri); 139 g_free(effective_uri); 140 141 g_main_loop_run(loop); 142 143 g_object_unref(view); 144} 145 146static void testLocalMimeType(const void* data) 147{ 148 const char* typeName = (const char*) data; 149 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new()); 150 g_object_ref_sink(G_OBJECT(view)); 151 152 loop = g_main_loop_new(NULL, TRUE); 153 154 g_object_connect(G_OBJECT(view), 155 "signal::notify::load-status", idle_quit_loop_cb, NULL, 156 "signal::mime-type-policy-decision-requested", mime_type_policy_decision_requested_cb, g_strdup(typeName), 157 NULL); 158 159 gchar* filename = g_strdup_printf("test.%s", typeName); 160 GFile* file = g_file_new_for_path(filename); 161 g_free(filename); 162 163 gchar* fileURI = g_file_get_uri(file); 164 g_object_unref(file); 165 166 webkit_web_view_load_uri(view, fileURI); 167 g_free(fileURI); 168 169 g_main_loop_run(loop); 170 g_object_unref(view); 171} 172 173int main(int argc, char** argv) 174{ 175 SoupServer* server; 176 SoupURI* soup_uri; 177 178 gtk_test_init(&argc, &argv, NULL); 179 180 /* Hopefully make test independent of the path it's called from. */ 181 testutils_relative_chdir("Source/WebKit/gtk/tests/resources/test.html", argv[0]); 182 183 server = soup_server_new(SOUP_SERVER_PORT, 0, NULL); 184 soup_server_run_async(server); 185 186 soup_server_add_handler(server, NULL, server_callback, NULL, NULL); 187 188 soup_uri = soup_uri_new("http://127.0.0.1/"); 189 soup_uri_set_port(soup_uri, soup_server_get_port(server)); 190 191 base_uri = soup_uri_to_string(soup_uri, FALSE); 192 soup_uri_free(soup_uri); 193 194 g_test_bug_base("https://bugs.webkit.org/"); 195 g_test_add_data_func("/webkit/mime/remote-PDF", "pdf", testRemoteMimeType); 196 g_test_add_data_func("/webkit/mime/remote-HTML", "html", testRemoteMimeType); 197 g_test_add_data_func("/webkit/mime/remote-TEXT", "text", testRemoteMimeType); 198 g_test_add_data_func("/webkit/mime/local-PDF", "pdf", testLocalMimeType); 199 g_test_add_data_func("/webkit/mime/local-HTML", "html", testLocalMimeType); 200 g_test_add_data_func("/webkit/mime/local-TEXT", "text", testLocalMimeType); 201 202 return g_test_run(); 203} 204