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