2 * Copyright (C) 2009 Jan Michael Alonzo
3 * Copyright (C) 2009 Gustavo Noronha Silva
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.
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.
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.
21 #include "test_utils.h"
24 #include <glib/gstdio.h>
25 #include <libsoup/soup.h>
27 #include <webkit/webkit.h>
30 #if GTK_CHECK_VERSION(2, 14, 0)
36 /* For real request testing */
38 server_callback(SoupServer *server, SoupMessage *msg,
39 const char *path, GHashTable *query,
40 SoupClientContext *context, gpointer data)
42 if (msg->method != SOUP_METHOD_GET) {
43 soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
47 soup_message_set_status(msg, SOUP_STATUS_OK);
50 if (g_str_equal(path, "/pdf")) {
55 g_file_get_contents("test.pdf", &contents, &length, &error);
58 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
59 } else if (g_str_equal(path, "/html")) {
64 g_file_get_contents("test.html", &contents, &length, &error);
67 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
68 } else if (g_str_equal(path, "/text")) {
73 soup_message_headers_append(msg->response_headers, "Content-Disposition", "attachment; filename=test.txt");
75 g_file_get_contents("test.txt", &contents, &length, &error);
78 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
79 } else if (g_str_equal(path, "/ogg")) {
84 g_file_get_contents("test.ogg", &contents, &length, &error);
87 soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
90 soup_message_body_complete(msg->response_body);
93 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
95 if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED ||
96 webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FAILED)
97 g_main_loop_quit(loop);
100 static gboolean mime_type_policy_decision_requested_cb(WebKitWebView* view, WebKitWebFrame* frame,
101 WebKitNetworkRequest* request, const char* mime_type,
102 WebKitWebPolicyDecision* decision, gpointer data)
104 char* type = (char*)data;
106 if (g_str_equal(type, "pdf")) {
107 g_assert_cmpstr(mime_type, ==, "application/pdf");
108 g_assert(!webkit_web_view_can_show_mime_type(view, mime_type));
109 } else if (g_str_equal(type, "html")) {
110 g_assert_cmpstr(mime_type, ==, "text/html");
111 g_assert(webkit_web_view_can_show_mime_type(view, mime_type));
112 } else if (g_str_equal(type, "text")) {
113 WebKitNetworkResponse* response = webkit_web_frame_get_network_response(frame);
114 SoupMessage* message = webkit_network_response_get_message(response);
118 soup_message_headers_get_content_disposition(message->response_headers,
120 g_object_unref(response);
122 g_assert_cmpstr(disposition, ==, "attachment");
125 g_assert_cmpstr(mime_type, ==, "text/plain");
126 g_assert(webkit_web_view_can_show_mime_type(view, mime_type));
127 } else if (g_str_equal(type, "ogg")) {
128 g_assert_cmpstr(mime_type, ==, "audio/x-vorbis+ogg");
129 g_assert(webkit_web_view_can_show_mime_type(view, mime_type));
137 static void testRemoteMimeType(const void* data)
139 const char* name = (const char*) data;
140 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
141 g_object_ref_sink(G_OBJECT(view));
143 loop = g_main_loop_new(NULL, TRUE);
145 g_object_connect(G_OBJECT(view),
146 "signal::notify::load-status", idle_quit_loop_cb, NULL,
147 "signal::mime-type-policy-decision-requested", mime_type_policy_decision_requested_cb, g_strdup(name),
150 char* effective_uri = g_strdup_printf("%s%s", base_uri, name);
151 webkit_web_view_load_uri(view, effective_uri);
152 g_free(effective_uri);
154 g_main_loop_run(loop);
156 g_object_unref(view);
159 static void testLocalMimeType(const void* data)
161 const char* typeName = (const char*) data;
162 WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
163 g_object_ref_sink(G_OBJECT(view));
165 loop = g_main_loop_new(NULL, TRUE);
167 g_object_connect(G_OBJECT(view),
168 "signal::notify::load-status", idle_quit_loop_cb, NULL,
169 "signal::mime-type-policy-decision-requested", mime_type_policy_decision_requested_cb, g_strdup(typeName),
172 gchar* filename = g_strdup_printf("test.%s", typeName);
173 GFile* file = g_file_new_for_path(filename);
176 gchar* fileURI = g_file_get_uri(file);
177 g_object_unref(file);
179 webkit_web_view_load_uri(view, fileURI);
182 g_main_loop_run(loop);
183 g_object_unref(view);
186 int main(int argc, char** argv)
192 gtk_test_init(&argc, &argv, NULL);
194 /* Hopefully make test independent of the path it's called from. */
195 testutils_relative_chdir("Source/WebKit/gtk/tests/resources/test.html", argv[0]);
197 server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
198 soup_server_run_async(server);
200 soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
202 soup_uri = soup_uri_new("http://127.0.0.1/");
203 soup_uri_set_port(soup_uri, soup_server_get_port(server));
205 base_uri = soup_uri_to_string(soup_uri, FALSE);
206 soup_uri_free(soup_uri);
208 g_test_bug_base("https://bugs.webkit.org/");
209 g_test_add_data_func("/webkit/mime/remote-PDF", "pdf", testRemoteMimeType);
210 g_test_add_data_func("/webkit/mime/remote-HTML", "html", testRemoteMimeType);
211 g_test_add_data_func("/webkit/mime/remote-TEXT", "text", testRemoteMimeType);
212 g_test_add_data_func("/webkit/mime/remote-OGG", "ogg", testRemoteMimeType);
213 g_test_add_data_func("/webkit/mime/local-PDF", "pdf", testLocalMimeType);
214 g_test_add_data_func("/webkit/mime/local-HTML", "html", testLocalMimeType);
215 g_test_add_data_func("/webkit/mime/local-TEXT", "text", testLocalMimeType);
216 g_test_add_data_func("/webkit/mime/local-OGG", "ogg", testLocalMimeType);
222 int main(int argc, char** argv)
224 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");