2010-02-23 Arno Renevier <arno@renevier.net>
[WebKit-https.git] / WebKit / gtk / tests / testmimehandling.c
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 "test_utils.h"
22
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 #include <libsoup/soup.h>
26 #include <string.h>
27 #include <webkit/webkit.h>
28 #include <unistd.h>
29
30 #if GLIB_CHECK_VERSION(2, 16, 0) && GTK_CHECK_VERSION(2, 14, 0)
31
32 GMainLoop* loop;
33 SoupSession *session;
34 char* base_uri;
35
36 /* For real request testing */
37 static void
38 server_callback(SoupServer *server, SoupMessage *msg,
39                  const char *path, GHashTable *query,
40                  SoupClientContext *context, gpointer data)
41 {
42     if (msg->method != SOUP_METHOD_GET) {
43         soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
44         return;
45     }
46
47     soup_message_set_status(msg, SOUP_STATUS_OK);
48
49     /* PDF */
50     if (g_str_equal(path, "/pdf")) {
51         char* contents;
52         gsize length;
53         GError* error = NULL;
54
55         g_file_get_contents("test.pdf", &contents, &length, &error);
56         g_assert(!error);
57
58         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
59     } else if (g_str_equal(path, "/html")) {
60         char* contents;
61         gsize length;
62         GError* error = NULL;
63
64         g_file_get_contents("test.html", &contents, &length, &error);
65         g_assert(!error);
66
67         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
68     } else if (g_str_equal(path, "/text")) {
69         char* contents;
70         gsize length;
71         GError* error = NULL;
72
73         soup_message_headers_append(msg->response_headers, "Content-Disposition", "attachment; filename=test.txt");
74
75         g_file_get_contents("test.txt", &contents, &length, &error);
76         g_assert(!error);
77
78         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
79     } else if (g_str_equal(path, "/ogg")) {
80         char* contents;
81         gsize length;
82         GError* error = NULL;
83
84         g_file_get_contents("test.ogg", &contents, &length, &error);
85         g_assert(!error);
86
87         soup_message_body_append(msg->response_body, SOUP_MEMORY_TAKE, contents, length);
88     }
89
90     soup_message_body_complete(msg->response_body);
91 }
92
93 static void idle_quit_loop_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
94 {
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);
98 }
99
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)
103 {
104     char* type = (char*)data;
105
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);
115         char* disposition;
116
117         g_assert(message);
118         soup_message_headers_get_content_disposition(message->response_headers,
119                                                      &disposition, NULL);
120         g_object_unref(response);
121
122         g_assert_cmpstr(disposition, ==, "attachment");
123         g_free(disposition);
124
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/ogg");
129         g_assert(webkit_web_view_can_show_mime_type(view, mime_type));
130     }
131
132     g_free(type);
133
134     return FALSE;
135 }
136
137 static void test_mime_type(const char* name)
138 {
139     WebKitWebView* view = WEBKIT_WEB_VIEW(webkit_web_view_new());
140     g_object_ref_sink(G_OBJECT(view));
141
142     loop = g_main_loop_new(NULL, TRUE);
143
144     g_object_connect(G_OBJECT(view),
145                      "signal::notify::load-status", idle_quit_loop_cb, NULL,
146                      "signal::mime-type-policy-decision-requested", mime_type_policy_decision_requested_cb, g_strdup(name),
147                      NULL);
148
149     char* effective_uri = g_strdup_printf("%s%s", base_uri, name);
150     webkit_web_view_load_uri(view, effective_uri);
151     g_free(effective_uri);
152
153     g_main_loop_run(loop);
154
155     g_object_unref(view);
156 }
157
158 static void test_mime_pdf()
159 {
160     test_mime_type("pdf");
161 }
162
163 static void test_mime_html()
164 {
165     test_mime_type("html");
166 }
167
168 static void test_mime_text()
169 {
170     test_mime_type("text");
171 }
172
173 static void test_mime_ogg()
174 {
175     test_mime_type("pdf");
176 }
177
178 int main(int argc, char** argv)
179 {
180     SoupServer* server;
181     SoupURI* soup_uri;
182
183     g_thread_init(NULL);
184     gtk_test_init(&argc, &argv, NULL);
185
186     /* Hopefully make test independent of the path it's called from. */
187     testutils_relative_chdir("WebKit/gtk/tests/resources/test.html", argv[0]);
188
189     server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
190     soup_server_run_async(server);
191
192     soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
193
194     soup_uri = soup_uri_new("http://127.0.0.1/");
195     soup_uri_set_port(soup_uri, soup_server_get_port(server));
196
197     base_uri = soup_uri_to_string(soup_uri, FALSE);
198     soup_uri_free(soup_uri);
199
200     g_test_bug_base("https://bugs.webkit.org/");
201     g_test_add_func("/webkit/mime/PDF", test_mime_pdf);
202     g_test_add_func("/webkit/mime/HTML", test_mime_html);
203     g_test_add_func("/webkit/mime/TEXT", test_mime_text);
204     g_test_add_func("/webkit/mime/OGG", test_mime_ogg);
205
206     return g_test_run();
207 }
208
209 #else
210 int main(int argc, char** argv)
211 {
212     g_critical("You will need at least glib-2.16.0 and gtk-2.14.0 to run the unit tests. Doing nothing now.");
213     return 0;
214 }
215
216 #endif