• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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
server_callback(SoupServer * server,SoupMessage * msg,const char * path,GHashTable * query,SoupClientContext * context,gpointer data)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 
idle_quit_loop_cb(WebKitWebView * web_view,GParamSpec * pspec,gpointer data)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 
mime_type_policy_decision_requested_cb(WebKitWebView * view,WebKitWebFrame * frame,WebKitNetworkRequest * request,const char * mime_type,WebKitWebPolicyDecision * decision,gpointer data)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/x-vorbis+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 
testRemoteMimeType(const void * data)137 static void testRemoteMimeType(const void* data)
138 {
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));
142 
143     loop = g_main_loop_new(NULL, TRUE);
144 
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),
148                      NULL);
149 
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);
153 
154     g_main_loop_run(loop);
155 
156     g_object_unref(view);
157 }
158 
testLocalMimeType(const void * data)159 static void testLocalMimeType(const void* data)
160 {
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));
164 
165      loop = g_main_loop_new(NULL, TRUE);
166 
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),
170                       NULL);
171 
172      gchar* filename = g_strdup_printf("test.%s", typeName);
173      GFile* file = g_file_new_for_path(filename);
174      g_free(filename);
175 
176      gchar* fileURI = g_file_get_uri(file);
177      g_object_unref(file);
178 
179      webkit_web_view_load_uri(view, fileURI);
180      g_free(fileURI);
181 
182      g_main_loop_run(loop);
183      g_object_unref(view);
184 }
185 
main(int argc,char ** argv)186 int main(int argc, char** argv)
187 {
188     SoupServer* server;
189     SoupURI* soup_uri;
190 
191     g_thread_init(NULL);
192     gtk_test_init(&argc, &argv, NULL);
193 
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]);
196 
197     server = soup_server_new(SOUP_SERVER_PORT, 0, NULL);
198     soup_server_run_async(server);
199 
200     soup_server_add_handler(server, NULL, server_callback, NULL, NULL);
201 
202     soup_uri = soup_uri_new("http://127.0.0.1/");
203     soup_uri_set_port(soup_uri, soup_server_get_port(server));
204 
205     base_uri = soup_uri_to_string(soup_uri, FALSE);
206     soup_uri_free(soup_uri);
207 
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);
217 
218     return g_test_run();
219 }
220 
221 #else
main(int argc,char ** argv)222 int main(int argc, char** argv)
223 {
224     g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");
225     return 0;
226 }
227 
228 #endif
229