Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2009 Collabora Ltd.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include <gtk/gtk.h>
     21 #include <webkit/webkit.h>
     22 
     23 #if GTK_CHECK_VERSION(2, 14, 0)
     24 
     25 static void notify_load_status_cb(WebKitWebView* web_view, GParamSpec* pspec, gpointer data)
     26 {
     27     if (webkit_web_view_get_load_status(web_view) == WEBKIT_LOAD_FINISHED) {
     28         GMainLoop* loop = (GMainLoop*)data;
     29 
     30         g_main_loop_quit(loop);
     31     }
     32 }
     33 
     34 static void test_webkit_window_scrollbar_policy(void)
     35 {
     36     GMainLoop* loop;
     37     GtkWidget* scrolledWindow;
     38     GtkWidget* webView;
     39     WebKitWebFrame* mainFrame;
     40     GtkPolicyType horizontalPolicy;
     41     GtkPolicyType verticalPolicy;
     42 
     43     loop = g_main_loop_new(NULL, TRUE);
     44 
     45     scrolledWindow = gtk_scrolled_window_new(NULL, NULL);
     46     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
     47                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
     48 
     49     webView = webkit_web_view_new();
     50     g_object_ref_sink(webView);
     51 
     52     g_signal_connect(webView, "notify::load-status",
     53                      G_CALLBACK(notify_load_status_cb), loop);
     54 
     55     gtk_container_add(GTK_CONTAINER(scrolledWindow), webView);
     56 
     57     mainFrame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(webView));
     58 
     59     /* Test we correctly apply policy for not having scrollbars; This
     60      * case is special, because we turn the policy from NEVER to
     61      * AUTOMATIC, since we cannot easily represent the same thing
     62      * using GtkScrolledWindow */
     63     webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(webView),
     64                                      "<html><body>WebKit!</body><script>document.getElementsByTagName('body')[0].style.overflow = 'hidden';</script></html>",
     65                                      "file://");
     66 
     67     g_main_loop_run(loop);
     68 
     69     gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
     70                                    &horizontalPolicy, &verticalPolicy);
     71 
     72     g_assert(horizontalPolicy == GTK_POLICY_AUTOMATIC);
     73     g_assert(verticalPolicy == GTK_POLICY_AUTOMATIC);
     74 
     75     g_assert(GTK_POLICY_NEVER == webkit_web_frame_get_horizontal_scrollbar_policy(mainFrame));
     76     g_assert(GTK_POLICY_NEVER == webkit_web_frame_get_vertical_scrollbar_policy(mainFrame));
     77 
     78     /* Test we correctly apply policy for always having scrollbars */
     79     webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(webView),
     80                                      "<html><body>WebKit!</body><script>document.getElementsByTagName('body')[0].style.overflow = 'scroll';</script></html>",
     81                                      "file://");
     82 
     83     g_main_loop_run(loop);
     84 
     85     gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
     86                                    &horizontalPolicy, &verticalPolicy);
     87 
     88     g_assert(horizontalPolicy == GTK_POLICY_ALWAYS);
     89     g_assert(verticalPolicy == GTK_POLICY_ALWAYS);
     90 
     91     g_assert(horizontalPolicy == webkit_web_frame_get_horizontal_scrollbar_policy(mainFrame));
     92     g_assert(verticalPolicy == webkit_web_frame_get_vertical_scrollbar_policy(mainFrame));
     93 
     94     /* Test we correctly apply policy for having scrollbars when needed */
     95     webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(webView),
     96                                      "<html><body>WebKit!</body><script>document.getElementsByTagName('body')[0].style.overflow = 'auto';</script></html>",
     97                                      "file://");
     98 
     99     g_main_loop_run(loop);
    100 
    101     gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(scrolledWindow),
    102                                    &horizontalPolicy, &verticalPolicy);
    103 
    104     g_assert(horizontalPolicy == GTK_POLICY_AUTOMATIC);
    105     g_assert(verticalPolicy == GTK_POLICY_AUTOMATIC);
    106 
    107     g_assert(horizontalPolicy == webkit_web_frame_get_horizontal_scrollbar_policy(mainFrame));
    108     g_assert(verticalPolicy == webkit_web_frame_get_vertical_scrollbar_policy(mainFrame));
    109 
    110     g_object_unref(webView);
    111 }
    112 
    113 int main(int argc, char** argv)
    114 {
    115     g_thread_init(NULL);
    116     gtk_test_init(&argc, &argv, NULL);
    117 
    118     g_test_bug_base("https://bugs.webkit.org/");
    119     g_test_add_func("/webkit/window/scrollbar_policy", test_webkit_window_scrollbar_policy);
    120     return g_test_run ();
    121 }
    122 
    123 #else
    124 int main(int argc, char** argv)
    125 {
    126     g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");
    127     return 0;
    128 }
    129 
    130 #endif
    131