[WebKit2] [GTK] Add API for controlling the user agent
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / gtk / tests / TestWebKitSettings.cpp
1 /*
2  * Copyright (c) 2011 Motorola Mobility, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, 
5  * are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, 
8  * this list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice, 
11  * this list of conditions and the following disclaimer in the documentation and/or 
12  * other materials provided with the distribution.
13  *
14  * Neither the name of Motorola Mobility, Inc. nor the names of its contributors may 
15  * be used to endorse or promote products derived from this software without 
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "TestMain.h"
34 #include "WebViewTest.h"
35 #include "WebKitTestServer.h"
36 #include <gtk/gtk.h>
37 #include <webkit2/webkit2.h>
38 #include <wtf/gobject/GRefPtr.h>
39
40 static WebKitTestServer* gServer;
41
42 static void testWebKitSettings(Test*, gconstpointer)
43 {
44     WebKitSettings* settings = webkit_settings_new();
45
46     // JavaScript is enabled by default.
47     g_assert(webkit_settings_get_enable_javascript(settings));
48     webkit_settings_set_enable_javascript(settings, FALSE);
49     g_assert(!webkit_settings_get_enable_javascript(settings));
50
51     // By default auto-load-image is true.
52     g_assert(webkit_settings_get_auto_load_images(settings));
53     webkit_settings_set_auto_load_images(settings, FALSE);
54     g_assert(!webkit_settings_get_auto_load_images(settings));
55
56     // load-icons-ignoring-image-load-setting is false by default.
57     g_assert(!webkit_settings_get_load_icons_ignoring_image_load_setting(settings));
58     webkit_settings_set_load_icons_ignoring_image_load_setting(settings, TRUE);
59     g_assert(webkit_settings_get_load_icons_ignoring_image_load_setting(settings));
60     
61     // Offline application cache is true by default.
62     g_assert(webkit_settings_get_enable_offline_web_application_cache(settings));
63     webkit_settings_set_enable_offline_web_application_cache(settings, FALSE);
64     g_assert(!webkit_settings_get_enable_offline_web_application_cache(settings));
65
66     // Local storage is enable by default.
67     g_assert(webkit_settings_get_enable_html5_local_storage(settings));
68     webkit_settings_set_enable_html5_local_storage(settings, FALSE);
69     g_assert(!webkit_settings_get_enable_html5_local_storage(settings));
70
71     // HTML5 database is enabled by default.
72     g_assert(webkit_settings_get_enable_html5_database(settings));
73     webkit_settings_set_enable_html5_database(settings, FALSE);
74     g_assert(!webkit_settings_get_enable_html5_database(settings));
75
76     // XSS Auditor is enabled by default.
77     g_assert(webkit_settings_get_enable_xss_auditor(settings));
78     webkit_settings_set_enable_xss_auditor(settings, FALSE);
79     g_assert(!webkit_settings_get_enable_xss_auditor(settings));
80
81     // Frame flattening is disabled by default.
82     g_assert(!webkit_settings_get_enable_frame_flattening(settings));
83     webkit_settings_set_enable_frame_flattening(settings, TRUE);
84     g_assert(webkit_settings_get_enable_frame_flattening(settings));
85
86     // Plugins are enabled by default.
87     g_assert(webkit_settings_get_enable_plugins(settings));
88     webkit_settings_set_enable_plugins(settings, FALSE);
89     g_assert(!webkit_settings_get_enable_plugins(settings));
90
91     // Java is enabled by default.
92     g_assert(webkit_settings_get_enable_java(settings));
93     webkit_settings_set_enable_java(settings, FALSE);
94     g_assert(!webkit_settings_get_enable_java(settings));
95
96     // By default, JavaScript can open windows automatically is disabled.
97     g_assert(!webkit_settings_get_javascript_can_open_windows_automatically(settings));
98     webkit_settings_set_javascript_can_open_windows_automatically(settings, TRUE);
99     g_assert(webkit_settings_get_javascript_can_open_windows_automatically(settings));
100
101     // By default hyper link auditing is disabled.
102     g_assert(!webkit_settings_get_enable_hyperlink_auditing(settings));
103     webkit_settings_set_enable_hyperlink_auditing(settings, TRUE);
104     g_assert(webkit_settings_get_enable_hyperlink_auditing(settings));
105
106     // Default font family is "sans-serif".
107     g_assert_cmpstr(webkit_settings_get_default_font_family(settings), ==, "sans-serif");
108     webkit_settings_set_default_font_family(settings, "monospace");
109     g_assert_cmpstr(webkit_settings_get_default_font_family(settings), ==, "monospace");
110
111     // Default monospace font family font family is "monospace".
112     g_assert_cmpstr(webkit_settings_get_monospace_font_family(settings), ==, "monospace");
113     webkit_settings_set_monospace_font_family(settings, "sans-serif");
114     g_assert_cmpstr(webkit_settings_get_monospace_font_family(settings), ==, "sans-serif");
115
116     // Default serif font family is "serif".
117     g_assert_cmpstr(webkit_settings_get_serif_font_family(settings), ==, "serif");
118     webkit_settings_set_serif_font_family(settings, "sans-serif");
119     g_assert_cmpstr(webkit_settings_get_serif_font_family(settings), ==, "sans-serif");
120
121     // Default sans serif font family is "sans-serif".
122     g_assert_cmpstr(webkit_settings_get_sans_serif_font_family(settings), ==, "sans-serif");
123     webkit_settings_set_sans_serif_font_family(settings, "serif");
124     g_assert_cmpstr(webkit_settings_get_sans_serif_font_family(settings), ==, "serif");
125
126     // Default cursive font family "serif".
127     g_assert_cmpstr(webkit_settings_get_cursive_font_family(settings), ==, "serif");
128     webkit_settings_set_cursive_font_family(settings, "sans-serif");
129     g_assert_cmpstr(webkit_settings_get_cursive_font_family(settings), ==, "sans-serif");
130
131     // Default fantasy font family is "serif".
132     g_assert_cmpstr(webkit_settings_get_fantasy_font_family(settings), ==, "serif");
133     webkit_settings_set_fantasy_font_family(settings, "sans-serif");
134     g_assert_cmpstr(webkit_settings_get_fantasy_font_family(settings), ==, "sans-serif");
135
136     // Default pictograph font family is "serif".
137     g_assert_cmpstr(webkit_settings_get_pictograph_font_family(settings), ==, "serif");
138     webkit_settings_set_pictograph_font_family(settings, "sans-serif");
139     g_assert_cmpstr(webkit_settings_get_pictograph_font_family(settings), ==, "sans-serif");
140
141     // Default font size is 16.
142     g_assert_cmpuint(webkit_settings_get_default_font_size(settings), ==, 16);
143     webkit_settings_set_default_font_size(settings, 14);
144     g_assert_cmpuint(webkit_settings_get_default_font_size(settings), ==, 14);
145
146     // Default monospace font size is 13.
147     g_assert_cmpuint(webkit_settings_get_default_monospace_font_size(settings), ==, 13);
148     webkit_settings_set_default_monospace_font_size(settings, 10);
149     g_assert_cmpuint(webkit_settings_get_default_monospace_font_size(settings), ==, 10);
150
151     // Default minimum font size is 0.
152     g_assert_cmpuint(webkit_settings_get_minimum_font_size(settings), ==, 0);
153     webkit_settings_set_minimum_font_size(settings, 7);
154     g_assert_cmpuint(webkit_settings_get_minimum_font_size(settings), ==, 7);
155
156     // Default charset is "iso-8859-1".
157     g_assert_cmpstr(webkit_settings_get_default_charset(settings), ==, "iso-8859-1");
158     webkit_settings_set_default_charset(settings, "utf8");
159     g_assert_cmpstr(webkit_settings_get_default_charset(settings), ==, "utf8");
160
161     g_assert(!webkit_settings_get_enable_private_browsing(settings));
162     webkit_settings_set_enable_private_browsing(settings, TRUE);
163     g_assert(webkit_settings_get_enable_private_browsing(settings));
164
165     g_assert(!webkit_settings_get_enable_developer_extras(settings));
166     webkit_settings_set_enable_developer_extras(settings, TRUE);
167     g_assert(webkit_settings_get_enable_developer_extras(settings));
168
169     g_assert(webkit_settings_get_enable_resizable_text_areas(settings));
170     webkit_settings_set_enable_resizable_text_areas(settings, FALSE);
171     g_assert(!webkit_settings_get_enable_resizable_text_areas(settings));
172
173     g_assert(webkit_settings_get_enable_tabs_to_links(settings));
174     webkit_settings_set_enable_tabs_to_links(settings, FALSE);
175     g_assert(!webkit_settings_get_enable_tabs_to_links(settings));
176
177     g_assert(!webkit_settings_get_enable_dns_prefetching(settings));
178     webkit_settings_set_enable_dns_prefetching(settings, TRUE);
179     g_assert(webkit_settings_get_enable_dns_prefetching(settings));
180
181     // Caret browsing is disabled by default.
182     g_assert(!webkit_settings_get_enable_caret_browsing(settings));
183     webkit_settings_set_enable_caret_browsing(settings, TRUE);
184     g_assert(webkit_settings_get_enable_caret_browsing(settings));
185
186     // Fullscreen JavaScript API is disabled by default.
187     g_assert(!webkit_settings_get_enable_fullscreen(settings));
188     webkit_settings_set_enable_fullscreen(settings, TRUE);
189     g_assert(webkit_settings_get_enable_fullscreen(settings));
190
191     // Print backgrounds is enabled by default
192     g_assert(webkit_settings_get_print_backgrounds(settings));
193     webkit_settings_set_print_backgrounds(settings, FALSE);
194     g_assert(!webkit_settings_get_print_backgrounds(settings));
195
196     // WebAudio is disabled by default.
197     g_assert(!webkit_settings_get_enable_webaudio(settings));
198     webkit_settings_set_enable_webaudio(settings, TRUE);
199     g_assert(webkit_settings_get_enable_webaudio(settings));
200
201     // WebGL is disabled by default.
202     g_assert(!webkit_settings_get_enable_webgl(settings));
203     webkit_settings_set_enable_webgl(settings, TRUE);
204     g_assert(webkit_settings_get_enable_webgl(settings));
205
206     // Allow Modal Dialogs is disabled by default.
207     g_assert(!webkit_settings_get_allow_modal_dialogs(settings));
208     webkit_settings_set_allow_modal_dialogs(settings, TRUE);
209     g_assert(webkit_settings_get_allow_modal_dialogs(settings));
210
211     // Zoom text only is disabled by default.
212     g_assert(!webkit_settings_get_zoom_text_only(settings));
213     webkit_settings_set_zoom_text_only(settings, TRUE);
214     g_assert(webkit_settings_get_zoom_text_only(settings));
215
216     // By default, JavaScript cannot access the clipboard.
217     g_assert(!webkit_settings_get_javascript_can_access_clipboard(settings));
218     webkit_settings_set_javascript_can_access_clipboard(settings, TRUE);
219     g_assert(webkit_settings_get_javascript_can_access_clipboard(settings));
220
221     // By default, media playback doesn't require user gestures.
222     g_assert(!webkit_settings_get_media_playback_requires_user_gesture(settings));
223     webkit_settings_set_media_playback_requires_user_gesture(settings, TRUE);
224     g_assert(webkit_settings_get_media_playback_requires_user_gesture(settings));
225
226     // By default, inline media playback is allowed
227     g_assert(webkit_settings_get_media_playback_allows_inline(settings));
228     webkit_settings_set_media_playback_allows_inline(settings, FALSE);
229     g_assert(!webkit_settings_get_media_playback_allows_inline(settings));
230
231     // By default, debug indicators are disabled.
232     g_assert(!webkit_settings_get_draw_compositing_indicators(settings));
233     webkit_settings_set_draw_compositing_indicators(settings, TRUE);
234     g_assert(webkit_settings_get_draw_compositing_indicators(settings));
235
236     // By default, site specific quirks are disabled.
237     g_assert(!webkit_settings_get_enable_site_specific_quirks(settings));
238     webkit_settings_set_enable_site_specific_quirks(settings, TRUE);
239     g_assert(webkit_settings_get_enable_site_specific_quirks(settings));
240
241     // By default, page cache is enabled.
242     g_assert(webkit_settings_get_enable_page_cache(settings));
243     webkit_settings_set_enable_page_cache(settings, FALSE);
244     g_assert(!webkit_settings_get_enable_page_cache(settings));
245
246     g_object_unref(G_OBJECT(settings));
247 }
248
249 void testWebKitSettingsNewWithSettings(Test* test, gconstpointer)
250 {
251     GRefPtr<WebKitSettings> settings = adoptGRef(webkit_settings_new_with_settings("enable-javascript", FALSE,
252                                                                                    "auto-load-images", FALSE,
253                                                                                    "load-icons-ignoring-image-load-setting", TRUE,
254                                                                                    NULL));
255     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(settings.get()));
256     g_assert(!webkit_settings_get_enable_javascript(settings.get()));
257     g_assert(!webkit_settings_get_auto_load_images(settings.get()));
258     g_assert(webkit_settings_get_load_icons_ignoring_image_load_setting(settings.get()));
259 }
260
261 static CString convertWebViewMainResourceDataToCString(WebViewTest* test)
262 {
263     size_t mainResourceDataSize = 0;
264     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
265     return CString(mainResourceData, mainResourceDataSize);
266 }
267
268 static void assertThatUserAgentIsSentInHeaders(WebViewTest* test, const CString& userAgent)
269 {
270     test->loadURI(gServer->getURIForPath("/").data());
271     test->waitUntilLoadFinished();
272     g_assert_cmpstr(convertWebViewMainResourceDataToCString(test).data(), ==, userAgent.data());
273 }
274
275 static void testWebKitSettingsUserAgent(WebViewTest* test, gconstpointer)
276 {
277     GRefPtr<WebKitSettings> settings = adoptGRef(webkit_settings_new());
278     CString defaultUserAgent = webkit_settings_get_user_agent(settings.get());
279     webkit_web_view_set_settings(test->m_webView, settings.get());
280
281     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Safari"));
282     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chromium"));
283     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chrome"));
284
285     webkit_settings_set_user_agent(settings.get(), 0);
286     g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
287     assertThatUserAgentIsSentInHeaders(test, defaultUserAgent.data());
288
289     webkit_settings_set_user_agent(settings.get(), "");
290     g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
291
292     const char* funkyUserAgent = "Funky!";
293     webkit_settings_set_user_agent(settings.get(), funkyUserAgent);
294     g_assert_cmpstr(funkyUserAgent, ==, webkit_settings_get_user_agent(settings.get()));
295     assertThatUserAgentIsSentInHeaders(test, funkyUserAgent);
296
297     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", 0);
298     CString userAgentWithNullVersion = webkit_settings_get_user_agent(settings.get());
299     g_assert_cmpstr(g_strstr_len(userAgentWithNullVersion.data(), -1, defaultUserAgent.data()), ==, userAgentWithNullVersion.data());
300     g_assert(g_strstr_len(userAgentWithNullVersion.data(), -1, "WebKitGTK+"));
301
302     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", "");
303     g_assert_cmpstr(webkit_settings_get_user_agent(settings.get()), ==, userAgentWithNullVersion.data());
304
305     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebCatGTK+", "3.4.5");
306     const char* newUserAgent = webkit_settings_get_user_agent(settings.get());
307     g_assert(g_strstr_len(newUserAgent, -1, "3.4.5"));
308     g_assert(g_strstr_len(newUserAgent, -1, "WebCatGTK+"));
309 }
310
311 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
312 {
313     if (message->method != SOUP_METHOD_GET) {
314         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
315         return;
316     }
317
318     soup_message_set_status(message, SOUP_STATUS_OK);
319     const char* userAgent = soup_message_headers_get_one(message->request_headers, "User-Agent");
320     soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, userAgent, strlen(userAgent));
321     soup_message_body_complete(message->response_body);
322 }
323
324 void beforeAll()
325 {
326     gServer = new WebKitTestServer();
327     gServer->run(serverCallback);
328
329     Test::add("WebKitSettings", "webkit-settings", testWebKitSettings);
330     Test::add("WebKitSettings", "new-with-settings", testWebKitSettingsNewWithSettings);
331     WebViewTest::add("WebKitSettings", "user-agent", testWebKitSettingsUserAgent);
332 }
333
334 void afterAll()
335 {
336     delete gServer;
337 }
338