2 * Copyright (C) 2009 Jan Michael Alonzo
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.
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.
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.
22 #include <webkit/webkit.h>
24 #if GTK_CHECK_VERSION(2, 14, 0)
27 char* webkitWebSettingsUserAgentForURI(WebKitWebSettings *settings, const char *uri);
29 static void test_webkit_web_settings_copy(void)
31 WebKitWebSettings *settings = webkit_web_settings_new();
33 // Set some non-default settings to verify that settings are properly copied.
34 g_object_set(settings,
36 "enable-fullscreen", TRUE,
37 "auto-load-images", FALSE,
38 "default-encoding", "utf-8", NULL);
40 WebKitWebSettings *copy = webkit_web_settings_copy(settings);
42 gboolean enableWebGL = FALSE;
43 gboolean enableFullscreen = FALSE;
44 gboolean autoLoadImages = FALSE;
45 char *defaultEncoding = 0;
47 "enable-fullscreen", &enableFullscreen,
48 "enable-webgl", &enableWebGL,
49 "auto-load-images", &autoLoadImages,
50 "default-encoding", &defaultEncoding, NULL);
52 g_assert(enableWebGL);
53 g_assert(enableFullscreen);
54 g_assert(!autoLoadImages);
55 g_assert_cmpstr(defaultEncoding, ==, "utf-8");
56 g_free(defaultEncoding);
59 static void test_non_quirky_user_agents(WebKitWebSettings *settings, const char *defaultUserAgent)
63 // test a custom UA string
65 g_object_set(settings, "user-agent", "testwebsettings/0.1", NULL);
66 g_object_get(settings,"user-agent", &userAgent, NULL);
67 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
70 // setting it to NULL or an empty value should give us the default UA string
72 g_object_set(settings, "user-agent", 0, NULL);
73 g_object_get(settings,"user-agent", &userAgent, NULL);
74 g_assert_cmpstr(userAgent, ==, defaultUserAgent);
78 g_object_set(settings, "user-agent", "", NULL);
79 g_object_get(settings,"user-agent", &userAgent, NULL);
80 g_assert_cmpstr(userAgent, ==, defaultUserAgent);
84 static void test_webkit_web_settings_user_agent(void)
86 WebKitWebSettings *settings;
88 char *defaultUserAgent;
92 webView = webkit_web_view_new();
93 g_object_ref_sink(webView);
95 settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webView));
96 defaultUserAgent = g_strdup(webkit_web_settings_get_user_agent(settings));
98 test_non_quirky_user_agents(settings, defaultUserAgent);
100 /* Test quirky google domains */
101 g_object_set(settings, "user-agent", "testwebsettings/0.1", NULL);
103 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://www.google.com/");
104 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
107 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://gmail.com/");
108 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
111 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://www.google.com.br/");
112 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
115 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://calendar.google.com/");
116 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
119 /* Now enable quirks handling */
120 g_object_set(settings, "enable-site-specific-quirks", TRUE, NULL);
122 test_non_quirky_user_agents(settings, defaultUserAgent);
124 g_object_set(settings, "user-agent", "testwebsettings/0.1", NULL);
126 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://www.google.com/");
127 g_assert_cmpstr(userAgent, ==, defaultUserAgent);
130 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://gmail.com/");
131 g_assert_cmpstr(userAgent, ==, defaultUserAgent);
134 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://www.google.com.br/");
135 g_assert_cmpstr(userAgent, ==, defaultUserAgent);
138 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://www.google.uk.not.com.br/");
139 g_assert_cmpstr(userAgent, ==, "testwebsettings/0.1");
142 userAgent = webkitWebSettingsUserAgentForURI(settings, "http://calendar.google.com/");
143 g_assert(g_str_has_prefix(userAgent, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en_US) AppleWebKit/"));
146 g_free(defaultUserAgent);
147 g_object_unref(webView);
150 int main(int argc, char **argv)
153 gtk_test_init(&argc, &argv, NULL);
155 g_test_bug_base("https://bugs.webkit.org/");
156 g_test_add_func("/webkit/websettings/user_agent", test_webkit_web_settings_user_agent);
157 g_test_add_func("/webkit/websettings/copy", test_webkit_web_settings_copy);
158 return g_test_run ();
162 int main(int argc, char **argv)
164 g_critical("You will need gtk-2.14.0 to run the unit tests. Doing nothing now.");