6e5347633ff211011c57b6341f4c6fc572f0f81b
[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     // By default, smooth scrolling is disabled.
247     g_assert(!webkit_settings_get_enable_smooth_scrolling(settings));
248     webkit_settings_set_enable_smooth_scrolling(settings, TRUE);
249     g_assert(webkit_settings_get_enable_smooth_scrolling(settings));
250
251     // By default, accelerated 2D canvas is disabled.
252     g_assert(!webkit_settings_get_enable_accelerated_2d_canvas(settings));
253     webkit_settings_set_enable_accelerated_2d_canvas(settings, TRUE);
254     g_assert(webkit_settings_get_enable_accelerated_2d_canvas(settings));
255
256     // By default, writing of console messages to stdout is disabled.
257     g_assert(!webkit_settings_get_enable_write_console_messages_to_stdout(settings));
258     webkit_settings_set_enable_write_console_messages_to_stdout(settings, TRUE);
259     g_assert(webkit_settings_get_enable_write_console_messages_to_stdout(settings));
260
261     // MediaStream is disabled by default.
262     g_assert(!webkit_settings_get_enable_media_stream(settings));
263     webkit_settings_set_enable_media_stream(settings, TRUE);
264     g_assert(webkit_settings_get_enable_media_stream(settings));
265
266     // By default, SpatialNavigation is disabled
267     g_assert(!webkit_settings_get_enable_spatial_navigation(settings));
268     webkit_settings_set_enable_spatial_navigation(settings, TRUE);
269     g_assert(webkit_settings_get_enable_spatial_navigation(settings));
270
271     g_object_unref(G_OBJECT(settings));
272 }
273
274 void testWebKitSettingsNewWithSettings(Test* test, gconstpointer)
275 {
276     GRefPtr<WebKitSettings> settings = adoptGRef(webkit_settings_new_with_settings("enable-javascript", FALSE,
277                                                                                    "auto-load-images", FALSE,
278                                                                                    "load-icons-ignoring-image-load-setting", TRUE,
279                                                                                    NULL));
280     test->assertObjectIsDeletedWhenTestFinishes(G_OBJECT(settings.get()));
281     g_assert(!webkit_settings_get_enable_javascript(settings.get()));
282     g_assert(!webkit_settings_get_auto_load_images(settings.get()));
283     g_assert(webkit_settings_get_load_icons_ignoring_image_load_setting(settings.get()));
284 }
285
286 static CString convertWebViewMainResourceDataToCString(WebViewTest* test)
287 {
288     size_t mainResourceDataSize = 0;
289     const char* mainResourceData = test->mainResourceData(mainResourceDataSize);
290     return CString(mainResourceData, mainResourceDataSize);
291 }
292
293 static void assertThatUserAgentIsSentInHeaders(WebViewTest* test, const CString& userAgent)
294 {
295     test->loadURI(gServer->getURIForPath("/").data());
296     test->waitUntilLoadFinished();
297     ASSERT_CMP_CSTRING(convertWebViewMainResourceDataToCString(test), ==, userAgent);
298 }
299
300 static void testWebKitSettingsUserAgent(WebViewTest* test, gconstpointer)
301 {
302     GRefPtr<WebKitSettings> settings = adoptGRef(webkit_settings_new());
303     CString defaultUserAgent = webkit_settings_get_user_agent(settings.get());
304     webkit_web_view_set_settings(test->m_webView, settings.get());
305
306     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Safari"));
307     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chromium"));
308     g_assert(g_strstr_len(defaultUserAgent.data(), -1, "Chrome"));
309
310     webkit_settings_set_user_agent(settings.get(), 0);
311     g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
312     assertThatUserAgentIsSentInHeaders(test, defaultUserAgent.data());
313
314     webkit_settings_set_user_agent(settings.get(), "");
315     g_assert_cmpstr(defaultUserAgent.data(), ==, webkit_settings_get_user_agent(settings.get()));
316
317     const char* funkyUserAgent = "Funky!";
318     webkit_settings_set_user_agent(settings.get(), funkyUserAgent);
319     g_assert_cmpstr(funkyUserAgent, ==, webkit_settings_get_user_agent(settings.get()));
320     assertThatUserAgentIsSentInHeaders(test, funkyUserAgent);
321
322     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", 0);
323     const char* userAgentWithNullVersion = webkit_settings_get_user_agent(settings.get());
324     g_assert_cmpstr(g_strstr_len(userAgentWithNullVersion, -1, defaultUserAgent.data()), ==, userAgentWithNullVersion);
325     g_assert(g_strstr_len(userAgentWithNullVersion, -1, "WebKitGTK+"));
326
327     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebKitGTK+", "");
328     g_assert_cmpstr(webkit_settings_get_user_agent(settings.get()), ==, userAgentWithNullVersion);
329
330     webkit_settings_set_user_agent_with_application_details(settings.get(), "WebCatGTK+", "3.4.5");
331     const char* newUserAgent = webkit_settings_get_user_agent(settings.get());
332     g_assert(g_strstr_len(newUserAgent, -1, "3.4.5"));
333     g_assert(g_strstr_len(newUserAgent, -1, "WebCatGTK+"));
334 }
335
336 static void serverCallback(SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer)
337 {
338     if (message->method != SOUP_METHOD_GET) {
339         soup_message_set_status(message, SOUP_STATUS_NOT_IMPLEMENTED);
340         return;
341     }
342
343     if (g_str_equal(path, "/")) {
344         const char* userAgent = soup_message_headers_get_one(message->request_headers, "User-Agent");
345         soup_message_set_status(message, SOUP_STATUS_OK);
346         soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, userAgent, strlen(userAgent));
347         soup_message_body_complete(message->response_body);
348     } else
349         soup_message_set_status(message, SOUP_STATUS_NOT_FOUND);
350 }
351
352 void beforeAll()
353 {
354     gServer = new WebKitTestServer();
355     gServer->run(serverCallback);
356
357     Test::add("WebKitSettings", "webkit-settings", testWebKitSettings);
358     Test::add("WebKitSettings", "new-with-settings", testWebKitSettingsNewWithSettings);
359     WebViewTest::add("WebKitSettings", "user-agent", testWebKitSettingsUserAgent);
360 }
361
362 void afterAll()
363 {
364     delete gServer;
365 }
366