Disabled touch adjust feature in case of SDK version.
[framework/web/webkit-efl.git] / Tools / DumpRenderTree / gtk / LayoutTestControllerGtk.cpp
1 /*
2  * Copyright (C) 2007 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4  * Copyright (C) 2008 Nuanti Ltd.
5  * Copyright (C) 2009 Jan Michael Alonzo <jmalonzo@gmail.com>
6  * Copyright (C) 2009,2011 Collabora Ltd.
7  * Copyright (C) 2010 Joone Hur <joone@kldp.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1.  Redistributions of source code must retain the above copyright
14  *     notice, this list of conditions and the following disclaimer.
15  * 2.  Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in the
17  *     documentation and/or other materials provided with the distribution.
18  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
19  *     its contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "config.h"
35 #include "LayoutTestController.h"
36
37 #include "DumpRenderTree.h"
38 #include "WebCoreSupport/DumpRenderTreeSupportGtk.h"
39 #include "WorkQueue.h"
40 #include "WorkQueueItem.h"
41 #include <JavaScriptCore/JSRetainPtr.h>
42 #include <JavaScriptCore/JSStringRef.h>
43 #include <cstring>
44 #include <iostream>
45 #include <sstream>
46 #include <stdio.h>
47 #include <glib.h>
48 #include <libsoup/soup.h>
49 #include <webkit/webkit.h>
50 #include <wtf/gobject/GOwnPtr.h>
51
52 extern "C" {
53 void webkit_web_inspector_execute_script(WebKitWebInspector* inspector, long callId, const gchar* script);
54 }
55
56 LayoutTestController::~LayoutTestController()
57 {
58     // FIXME: implement
59 }
60
61 void LayoutTestController::addDisallowedURL(JSStringRef url)
62 {
63     // FIXME: implement
64 }
65
66 void LayoutTestController::clearBackForwardList()
67 {
68     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
69     WebKitWebBackForwardList* list = webkit_web_view_get_back_forward_list(webView);
70     WebKitWebHistoryItem* item = webkit_web_back_forward_list_get_current_item(list);
71     g_object_ref(item);
72
73     // We clear the history by setting the back/forward list's capacity to 0
74     // then restoring it back and adding back the current item.
75     gint limit = webkit_web_back_forward_list_get_limit(list);
76     webkit_web_back_forward_list_set_limit(list, 0);
77     webkit_web_back_forward_list_set_limit(list, limit);
78     webkit_web_back_forward_list_add_item(list, item);
79     webkit_web_back_forward_list_go_to_item(list, item);
80     g_object_unref(item);
81 }
82
83 JSStringRef LayoutTestController::copyDecodedHostName(JSStringRef name)
84 {
85     // FIXME: implement
86     return 0;
87 }
88
89 JSStringRef LayoutTestController::copyEncodedHostName(JSStringRef name)
90 {
91     // FIXME: implement
92     return 0;
93 }
94
95 void LayoutTestController::dispatchPendingLoadRequests()
96 {
97     // FIXME: Implement for testing fix for 6727495
98 }
99
100 void LayoutTestController::display()
101 {
102     displayWebView();
103 }
104
105 void LayoutTestController::keepWebHistory()
106 {
107     // FIXME: implement
108 }
109
110 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSContextRef context, JSValueRef value)
111 {
112     return DumpRenderTreeSupportGtk::computedStyleIncludingVisitedInfo(context, value);
113 }
114
115 JSRetainPtr<JSStringRef> LayoutTestController::layerTreeAsText() const
116 {
117     // FIXME: implement
118     JSRetainPtr<JSStringRef> string(Adopt, JSStringCreateWithUTF8CString(""));
119     return string;
120 }
121
122 int LayoutTestController::numberOfPages(float pageWidth, float pageHeight)
123 {
124     return DumpRenderTreeSupportGtk::numberOfPagesForFrame(mainFrame, pageWidth, pageHeight);
125 }
126
127 JSRetainPtr<JSStringRef> LayoutTestController::pageProperty(const char* propertyName, int pageNumber) const
128 {
129     JSRetainPtr<JSStringRef> propertyValue(Adopt, JSStringCreateWithUTF8CString(DumpRenderTreeSupportGtk::pageProperty(mainFrame, propertyName, pageNumber).data()));
130     return propertyValue;
131 }
132
133 JSRetainPtr<JSStringRef> LayoutTestController::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft) const
134 {
135     JSRetainPtr<JSStringRef> propertyValue(Adopt, JSStringCreateWithUTF8CString(DumpRenderTreeSupportGtk::pageSizeAndMarginsInPixels(mainFrame, pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft).data()));
136     return propertyValue;
137 }
138
139 size_t LayoutTestController::webHistoryItemCount()
140 {
141     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
142     WebKitWebBackForwardList* list = webkit_web_view_get_back_forward_list(webView);
143
144     if (!list)
145         return -1;
146
147     // We do not add the current page to the total count as it's not
148     // considered in DRT tests
149     return webkit_web_back_forward_list_get_back_length(list) +
150             webkit_web_back_forward_list_get_forward_length(list);
151 }
152
153 unsigned LayoutTestController::workerThreadCount() const
154 {
155     return DumpRenderTreeSupportGtk::workerThreadCount();
156 }
157
158 JSRetainPtr<JSStringRef> LayoutTestController::platformName() const
159 {
160     JSRetainPtr<JSStringRef> platformName(Adopt, JSStringCreateWithUTF8CString("gtk"));
161     return platformName;
162 }
163
164 void LayoutTestController::notifyDone()
165 {
166     if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
167         dump();
168     m_waitToDump = false;
169     waitForPolicy = false;
170 }
171
172 JSStringRef LayoutTestController::pathToLocalResource(JSContextRef context, JSStringRef url)
173 {
174     GOwnPtr<char> urlCString(JSStringCopyUTF8CString(url));
175     if (!g_str_has_prefix(urlCString.get(), "file:///tmp/LayoutTests/"))
176         return JSStringRetain(url);
177
178     const char* layoutTestsSuffix = urlCString.get() + strlen("file:///tmp/");
179     GOwnPtr<char> testPath(g_build_filename(getTopLevelPath().data(), layoutTestsSuffix, NULL));
180     GOwnPtr<char> testURI(g_filename_to_uri(testPath.get(), 0, 0));
181     return JSStringCreateWithUTF8CString(testURI.get());
182 }
183
184 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
185 {
186     gchar* relativeURL = JSStringCopyUTF8CString(url);
187     SoupURI* baseURI = soup_uri_new(webkit_web_frame_get_uri(mainFrame));
188
189     SoupURI* absoluteURI = soup_uri_new_with_base(baseURI, relativeURL);
190     soup_uri_free(baseURI);
191     g_free(relativeURL);
192
193     gchar* absoluteCString;
194     if (absoluteURI) {
195         absoluteCString = soup_uri_to_string(absoluteURI, FALSE);
196         soup_uri_free(absoluteURI);
197     } else
198         absoluteCString = JSStringCopyUTF8CString(url);
199
200     JSRetainPtr<JSStringRef> absoluteURL(Adopt, JSStringCreateWithUTF8CString(absoluteCString));
201     g_free(absoluteCString);
202
203     WorkQueue::shared()->queue(new LoadItem(absoluteURL.get(), target));
204 }
205
206 void LayoutTestController::setAcceptsEditing(bool acceptsEditing)
207 {
208     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
209     webkit_web_view_set_editable(webView, acceptsEditing);
210 }
211
212 void LayoutTestController::setAlwaysAcceptCookies(bool alwaysAcceptCookies)
213 {
214     SoupSession* session = webkit_get_default_session();
215     SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
216
217     /* If the jar was not created - we create it on demand, i.e, just
218        in case we have HTTP requests - then we must create it here in
219        order to set the proper accept policy */
220     if (!jar) {
221         jar = soup_cookie_jar_new();
222         soup_session_add_feature(session, SOUP_SESSION_FEATURE(jar));
223         g_object_unref(jar);
224     }
225
226     SoupCookieJarAcceptPolicy policy;
227
228     if (alwaysAcceptCookies)
229         policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
230     else
231         policy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
232
233     g_object_set(G_OBJECT(jar), SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
234 }
235
236 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate, bool permissive)
237 {
238     // FIXME: implement
239 }
240
241 void LayoutTestController::waitForPolicyDelegate()
242 {
243     waitForPolicy = true;
244     setWaitToDump(true);
245 }
246
247 void LayoutTestController::setScrollbarPolicy(JSStringRef orientation, JSStringRef policy)
248 {
249     // FIXME: implement
250 }
251
252 void LayoutTestController::addOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef protocol, JSStringRef host, bool includeSubdomains)
253 {
254     gchar* sourceOriginGChar = JSStringCopyUTF8CString(sourceOrigin);
255     gchar* protocolGChar = JSStringCopyUTF8CString(protocol);
256     gchar* hostGChar = JSStringCopyUTF8CString(host);
257     DumpRenderTreeSupportGtk::whiteListAccessFromOrigin(sourceOriginGChar, protocolGChar, hostGChar, includeSubdomains);
258     g_free(sourceOriginGChar);
259     g_free(protocolGChar);
260     g_free(hostGChar);
261 }
262
263 void LayoutTestController::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef protocol, JSStringRef host, bool includeSubdomains)
264 {
265     GOwnPtr<gchar> sourceOriginGChar(JSStringCopyUTF8CString(sourceOrigin));
266     GOwnPtr<gchar> protocolGChar(JSStringCopyUTF8CString(protocol));
267     GOwnPtr<gchar> hostGChar(JSStringCopyUTF8CString(host));
268     DumpRenderTreeSupportGtk::removeWhiteListAccessFromOrigin(sourceOriginGChar.get(), protocolGChar.get(), hostGChar.get(), includeSubdomains);
269 }
270
271 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
272 {
273     // FIXME: implement
274 }
275
276 void LayoutTestController::setTabKeyCyclesThroughElements(bool cycles)
277 {
278     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
279     WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
280     g_object_set(G_OBJECT(settings), "tab-key-cycles-through-elements", cycles, NULL);
281 }
282
283 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
284 {
285     // FIXME: implement
286 }
287
288 static gchar* userStyleSheet = NULL;
289 static gboolean userStyleSheetEnabled = TRUE;
290
291 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
292 {
293     userStyleSheetEnabled = flag;
294
295     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
296     WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
297     if (flag && userStyleSheet)
298         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", userStyleSheet, NULL);
299     else
300         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", "", NULL);
301 }
302
303 void LayoutTestController::setUserStyleSheetLocation(JSStringRef path)
304 {
305     g_free(userStyleSheet);
306     userStyleSheet = JSStringCopyUTF8CString(path);
307     if (userStyleSheetEnabled)
308         setUserStyleSheetEnabled(true);
309 }
310
311 void LayoutTestController::setValueForUser(JSContextRef context, JSValueRef nodeObject, JSStringRef value)
312 {
313     DumpRenderTreeSupportGtk::setValueForUser(context, nodeObject, value);
314 }
315
316 void LayoutTestController::setViewModeMediaFeature(JSStringRef mode)
317 {
318     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
319     ASSERT(view);
320
321     char* viewMode = JSStringCopyUTF8CString(mode);
322
323     if (!g_strcmp0(viewMode, "windowed"))
324         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_WINDOWED);
325     else if (!g_strcmp0(viewMode, "floating"))
326         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_FLOATING);
327     else if (!g_strcmp0(viewMode, "fullscreen"))
328         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_FULLSCREEN);
329     else if (!g_strcmp0(viewMode, "maximized"))
330         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_MAXIMIZED);
331     else if (!g_strcmp0(viewMode, "minimized"))
332         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_MINIMIZED);
333
334     g_free(viewMode);
335 }
336
337 void LayoutTestController::setWindowIsKey(bool windowIsKey)
338 {
339     // FIXME: implement
340 }
341
342 void LayoutTestController::setSmartInsertDeleteEnabled(bool flag)
343 {
344     DumpRenderTreeSupportGtk::setSmartInsertDeleteEnabled(webkit_web_frame_get_web_view(mainFrame), flag);
345 }
346
347 static gboolean waitToDumpWatchdogFired(void*)
348 {
349     setWaitToDumpWatchdog(0);
350     gLayoutTestController->waitToDumpWatchdogTimerFired();
351     return FALSE;
352 }
353
354 void LayoutTestController::setWaitToDump(bool waitUntilDone)
355 {
356     static const int timeoutSeconds = 30;
357
358     m_waitToDump = waitUntilDone;
359     if (m_waitToDump && shouldSetWaitToDumpWatchdog())
360         setWaitToDumpWatchdog(g_timeout_add_seconds(timeoutSeconds, waitToDumpWatchdogFired, 0));
361 }
362
363 int LayoutTestController::windowCount()
364 {
365     // +1 -> including the main view
366     return g_slist_length(webViewList) + 1;
367 }
368
369 void LayoutTestController::setPrivateBrowsingEnabled(bool flag)
370 {
371     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
372     ASSERT(view);
373
374     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
375     g_object_set(G_OBJECT(settings), "enable-private-browsing", flag, NULL);
376 }
377
378 void LayoutTestController::setJavaScriptCanAccessClipboard(bool flag)
379 {
380     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
381     ASSERT(view);
382
383     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
384     g_object_set(G_OBJECT(settings), "javascript-can-access-clipboard", flag, NULL);
385 }
386
387 void LayoutTestController::setXSSAuditorEnabled(bool flag)
388 {
389     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
390     ASSERT(view);
391
392     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
393     g_object_set(G_OBJECT(settings), "enable-xss-auditor", flag, NULL);
394 }
395
396 void LayoutTestController::setFrameFlatteningEnabled(bool flag)
397 {
398     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
399     ASSERT(view);
400
401     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
402     g_object_set(G_OBJECT(settings), "enable-frame-flattening", flag, NULL);
403 }
404
405 void LayoutTestController::setSpatialNavigationEnabled(bool flag)
406 {
407     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
408     ASSERT(view);
409
410     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
411     g_object_set(G_OBJECT(settings), "enable-spatial-navigation", flag, NULL);
412 }
413
414 void LayoutTestController::setAllowUniversalAccessFromFileURLs(bool flag)
415 {
416     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
417     ASSERT(view);
418
419     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
420     g_object_set(G_OBJECT(settings), "enable-universal-access-from-file-uris", flag, NULL);
421 }
422
423 void LayoutTestController::setAllowFileAccessFromFileURLs(bool flag)
424 {
425     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
426     ASSERT(view);
427
428     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
429     g_object_set(G_OBJECT(settings), "enable-file-access-from-file-uris", flag, NULL);
430 }
431
432 void LayoutTestController::setAuthorAndUserStylesEnabled(bool flag)
433 {
434     // FIXME: implement
435 }
436
437 void LayoutTestController::setAutofilled(JSContextRef context, JSValueRef nodeObject, bool isAutofilled)
438 {
439     DumpRenderTreeSupportGtk::setAutofilled(context, nodeObject, isAutofilled);
440 }
441
442 void LayoutTestController::disableImageLoading()
443 {
444     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
445     ASSERT(view);
446
447     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
448     g_object_set(G_OBJECT(settings), "auto-load-images", FALSE, NULL);
449 }
450
451 void LayoutTestController::setMockDeviceOrientation(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
452 {
453     // FIXME: Implement for DeviceOrientation layout tests.
454     // See https://bugs.webkit.org/show_bug.cgi?id=30335.
455 }
456
457 void LayoutTestController::setMockGeolocationPosition(double latitude, double longitude, double accuracy)
458 {
459     WebKitWebView* view = WEBKIT_WEB_VIEW(g_slist_nth_data(webViewList, 0));
460     if (!view)
461         view = webkit_web_frame_get_web_view(mainFrame);
462     ASSERT(view);
463
464     DumpRenderTreeSupportGtk::setMockGeolocationPosition(view, latitude, longitude, accuracy);
465 }
466
467 void LayoutTestController::setMockGeolocationError(int code, JSStringRef message)
468 {
469     WebKitWebView* view = WEBKIT_WEB_VIEW(g_slist_nth_data(webViewList, 0));
470     if (!view)
471         view = webkit_web_frame_get_web_view(mainFrame);
472     ASSERT(view);
473
474     GOwnPtr<gchar> cMessage(JSStringCopyUTF8CString(message));
475     DumpRenderTreeSupportGtk::setMockGeolocationError(view, code, cMessage.get());
476 }
477
478 void LayoutTestController::setGeolocationPermission(bool allow)
479 {
480     setGeolocationPermissionCommon(allow);
481     WebKitWebView* view = WEBKIT_WEB_VIEW(g_slist_nth_data(webViewList, 0));
482     if (!view)
483         view = webkit_web_frame_get_web_view(mainFrame);
484     ASSERT(view);
485
486     DumpRenderTreeSupportGtk::setMockGeolocationPermission(view, allow);
487 }
488
489 int LayoutTestController::numberOfPendingGeolocationPermissionRequests()
490 {
491     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
492     if (!view)
493         view = webkit_web_frame_get_web_view(mainFrame);
494     ASSERT(view);
495
496     return DumpRenderTreeSupportGtk::numberOfPendingGeolocationPermissionRequests(view);
497 }
498
499 void LayoutTestController::addMockSpeechInputResult(JSStringRef result, double confidence, JSStringRef language)
500 {
501     // FIXME: Implement for speech input layout tests.
502     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
503 }
504
505 void LayoutTestController::setMockSpeechInputDumpRect(bool flag)
506 {
507     // FIXME: Implement for speech input layout tests.
508     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
509 }
510
511 void LayoutTestController::startSpeechInput(JSContextRef inputElement)
512 {
513     // FIXME: Implement for speech input layout tests.
514     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
515 }
516
517 void LayoutTestController::setIconDatabaseEnabled(bool enabled)
518 {
519     WebKitIconDatabase* database = webkit_get_icon_database();
520     if (enabled) {
521         GOwnPtr<gchar> iconDatabasePath(g_build_filename(g_get_tmp_dir(), "DumpRenderTree", "icondatabase", NULL));
522         webkit_icon_database_set_path(database, iconDatabasePath.get());
523     } else
524         webkit_icon_database_set_path(database, 0);
525 }
526
527 void LayoutTestController::setSelectTrailingWhitespaceEnabled(bool flag)
528 {
529     DumpRenderTreeSupportGtk::setSelectTrailingWhitespaceEnabled(flag);
530 }
531
532 void LayoutTestController::setPopupBlockingEnabled(bool flag)
533 {
534     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
535     ASSERT(view);
536
537     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
538     g_object_set(G_OBJECT(settings), "javascript-can-open-windows-automatically", !flag, NULL);
539
540 }
541
542 void LayoutTestController::setPluginsEnabled(bool flag)
543 {
544     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
545     ASSERT(view);
546
547     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
548     g_object_set(G_OBJECT(settings), "enable-plugins", flag, NULL);
549 }
550
551 bool LayoutTestController::elementDoesAutoCompleteForElementWithId(JSStringRef id) 
552 {
553     return DumpRenderTreeSupportGtk::elementDoesAutoCompleteForElementWithId(mainFrame, id);
554 }
555
556 void LayoutTestController::execCommand(JSStringRef name, JSStringRef value)
557 {
558     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
559     ASSERT(view);
560
561     gchar* cName = JSStringCopyUTF8CString(name);
562     gchar* cValue = JSStringCopyUTF8CString(value);
563     DumpRenderTreeSupportGtk::executeCoreCommandByName(view, cName, cValue);
564     g_free(cName);
565     g_free(cValue);
566 }
567
568 bool LayoutTestController::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray)
569 {
570     WebKitFindOptions findOptions = 0;
571     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
572     ASSERT(webView);
573
574     JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length"));
575     JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); 
576     if (!JSValueIsNumber(context, lengthValue))
577         return false;
578
579     GOwnPtr<gchar> targetString(JSStringCopyUTF8CString(target));
580
581     size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0));
582     for (size_t i = 0; i < length; ++i) {
583         JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); 
584         if (!JSValueIsString(context, value))
585             continue;
586     
587         JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0));
588
589         if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive"))
590             findOptions |= WebKit::WebFindOptionsCaseInsensitive;
591         else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts"))
592             findOptions |= WebKit::WebFindOptionsAtWordStarts;
593         else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart"))
594             findOptions |= WebKit::WebFindOptionsTreatMedialCapitalAsWordStart;
595         else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards"))
596             findOptions |= WebKit::WebFindOptionsBackwards;
597         else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround"))
598             findOptions |= WebKit::WebFindOptionsWrapAround;
599         else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection"))
600             findOptions |= WebKit::WebFindOptionsStartInSelection;
601     }   
602
603     return DumpRenderTreeSupportGtk::findString(webView, targetString.get(), findOptions); 
604 }
605
606 bool LayoutTestController::isCommandEnabled(JSStringRef name)
607 {
608     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
609     ASSERT(view);
610
611     gchar* cName = JSStringCopyUTF8CString(name);
612     bool result = DumpRenderTreeSupportGtk::isCommandEnabled(view, cName);
613     g_free(cName);
614     return result;
615 }
616
617 void LayoutTestController::setCacheModel(int cacheModel)
618 {
619     // These constants are derived from the Mac cache model enum in Source/WebKit/mac/WebView/WebPreferences.h.
620     switch (cacheModel) {
621     case 0:
622         webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
623         break;
624     case 1:
625         webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER);
626         break;
627     case 2:
628         webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
629         break;
630     default:
631         ASSERT_NOT_REACHED();
632     }
633 }
634
635 void LayoutTestController::setPersistentUserStyleSheetLocation(JSStringRef jsURL)
636 {
637     // FIXME: implement
638 }
639
640 void LayoutTestController::clearPersistentUserStyleSheet()
641 {
642     // FIXME: implement
643 }
644
645 void LayoutTestController::clearAllApplicationCaches()
646 {
647     // FIXME: Implement to support application cache quotas.
648 }
649
650 void LayoutTestController::setApplicationCacheOriginQuota(unsigned long long quota)
651 {
652     // FIXME: Implement to support application cache quotas.
653 }
654
655 void LayoutTestController::clearApplicationCacheForOrigin(OpaqueJSString*)
656 {
657     // FIXME: Implement to support deleting all application caches for an origin.
658 }
659
660 long long LayoutTestController::localStorageDiskUsageForOrigin(JSStringRef originIdentifier)
661 {
662     // FIXME: Implement to support getting disk usage in bytes for an origin.
663     return 0;
664 }
665
666 JSValueRef LayoutTestController::originsWithApplicationCache(JSContextRef context)
667 {
668     // FIXME: Implement to get origins that contain application caches.
669     return JSValueMakeUndefined(context);
670 }
671
672 long long LayoutTestController::applicationCacheDiskUsageForOrigin(JSStringRef name)
673 {
674     // FIXME: implement
675     return 0;
676 }
677
678 void LayoutTestController::clearAllDatabases()
679 {
680     webkit_remove_all_web_databases();
681 }
682  
683 void LayoutTestController::setDatabaseQuota(unsigned long long quota)
684 {
685     WebKitSecurityOrigin* origin = webkit_web_frame_get_security_origin(mainFrame);
686     webkit_security_origin_set_web_database_quota(origin, quota);
687 }
688
689 JSValueRef LayoutTestController::originsWithLocalStorage(JSContextRef context)
690 {
691     // FIXME: implement
692     return JSValueMakeUndefined(context);
693 }
694
695 void LayoutTestController::deleteAllLocalStorage()
696 {
697         // FIXME: implement
698 }
699
700 void LayoutTestController::deleteLocalStorageForOrigin(JSStringRef originIdentifier)
701 {
702         // FIXME: implement
703 }
704
705 void LayoutTestController::observeStorageTrackerNotifications(unsigned number)
706 {
707         // FIXME: implement
708 }
709
710 void LayoutTestController::syncLocalStorage()
711 {
712     // FIXME: implement
713 }
714
715 void LayoutTestController::setDomainRelaxationForbiddenForURLScheme(bool forbidden, JSStringRef scheme)
716 {
717     GOwnPtr<gchar> urlScheme(JSStringCopyUTF8CString(scheme));
718     DumpRenderTreeSupportGtk::setDomainRelaxationForbiddenForURLScheme(forbidden, urlScheme.get());
719 }
720
721 void LayoutTestController::goBack()
722 {
723     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
724     webkit_web_view_go_back(webView);
725 }
726
727 void LayoutTestController::setDefersLoading(bool defers)
728 {
729     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
730     DumpRenderTreeSupportGtk::setDefersLoading(webView, defers);
731 }
732
733 void LayoutTestController::setAppCacheMaximumSize(unsigned long long size)
734 {
735     webkit_application_cache_set_maximum_size(size);
736 }
737
738 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
739 {    
740     gchar* name = JSStringCopyUTF8CString(animationName);
741     gchar* element = JSStringCopyUTF8CString(elementId);
742     bool returnValue = DumpRenderTreeSupportGtk::pauseAnimation(mainFrame, name, time, element);
743     g_free(name);
744     g_free(element);
745     return returnValue;
746 }
747
748 bool LayoutTestController::pauseTransitionAtTimeOnElementWithId(JSStringRef propertyName, double time, JSStringRef elementId)
749 {    
750     gchar* name = JSStringCopyUTF8CString(propertyName);
751     gchar* element = JSStringCopyUTF8CString(elementId);
752     bool returnValue = DumpRenderTreeSupportGtk::pauseTransition(mainFrame, name, time, element);
753     g_free(name);
754     g_free(element);
755     return returnValue;
756 }
757
758 unsigned LayoutTestController::numberOfActiveAnimations() const
759 {
760     return DumpRenderTreeSupportGtk::numberOfActiveAnimations(mainFrame);
761 }
762
763 static gboolean booleanFromValue(gchar* value)
764 {
765     return !g_ascii_strcasecmp(value, "true") || !g_ascii_strcasecmp(value, "1");
766 }
767
768 void LayoutTestController::overridePreference(JSStringRef key, JSStringRef value)
769 {
770     GOwnPtr<gchar> originalName(JSStringCopyUTF8CString(key));
771     GOwnPtr<gchar> valueAsString(JSStringCopyUTF8CString(value));
772
773     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
774     ASSERT(view);
775
776     // This transformation could be handled by a hash table (and it once was), but
777     // having it prominent, makes it easier for people from other ports to keep the
778     // list up to date.
779     const gchar* propertyName = 0;
780     if (g_str_equal(originalName.get(), "WebKitJavaScriptEnabled"))
781         propertyName = "enable-scripts";
782     else if (g_str_equal(originalName.get(), "WebKitDefaultFontSize"))
783         propertyName = "default-font-size";
784     else if (g_str_equal(originalName.get(), "WebKitEnableCaretBrowsing"))
785         propertyName = "enable-caret-browsing";
786     else if (g_str_equal(originalName.get(), "WebKitUsesPageCachePreferenceKey"))
787         propertyName = "enable-page-cache";
788     else if (g_str_equal(originalName.get(), "WebKitPluginsEnabled"))
789         propertyName = "enable-plugins";
790     else if (g_str_equal(originalName.get(), "WebKitHyperlinkAuditingEnabled"))
791         propertyName = "enable-hyperlink-auditing";
792     else if (g_str_equal(originalName.get(), "WebKitWebGLEnabled"))
793         propertyName = "enable-webgl";
794     else if (g_str_equal(originalName.get(), "WebKitWebAudioEnabled"))
795         propertyName = "enable-webaudio";
796     else if (g_str_equal(originalName.get(), "WebKitTabToLinksPreferenceKey")) {
797         DumpRenderTreeSupportGtk::setLinksIncludedInFocusChain(booleanFromValue(valueAsString.get()));
798         return;
799     } else if (g_str_equal(originalName.get(), "WebKitPageCacheSupportsPluginsPreferenceKey")) {
800         DumpRenderTreeSupportGtk::setPageCacheSupportsPlugins(webkit_web_frame_get_web_view(mainFrame), booleanFromValue(valueAsString.get()));
801         return;
802     } else if (g_str_equal(originalName.get(), "WebKitCSSGridLayoutEnabled")) {
803         DumpRenderTreeSupportGtk::setCSSGridLayoutEnabled(webkit_web_frame_get_web_view(mainFrame), booleanFromValue(valueAsString.get()));
804         return;
805     } else if (g_str_equal(originalName.get(), "WebKitCSSRegionsEnabled")) {
806         DumpRenderTreeSupportGtk::setCSSRegionsEnabled(webkit_web_frame_get_web_view(mainFrame), booleanFromValue(valueAsString.get()));
807         return;
808     } else {
809         fprintf(stderr, "LayoutTestController::overridePreference tried to override "
810                 "unknown preference '%s'.\n", originalName.get());
811         return;
812     }
813
814     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
815     GParamSpec* pspec = g_object_class_find_property(G_OBJECT_CLASS(
816         WEBKIT_WEB_SETTINGS_GET_CLASS(settings)), propertyName);
817     GValue currentPropertyValue = { 0, { { 0 } } };
818     g_value_init(&currentPropertyValue, pspec->value_type);
819
820     if (G_VALUE_HOLDS_STRING(&currentPropertyValue))
821         g_object_set(settings, propertyName, valueAsString.get(), NULL);
822     else if (G_VALUE_HOLDS_BOOLEAN(&currentPropertyValue))
823         g_object_set(G_OBJECT(settings), propertyName, booleanFromValue(valueAsString.get()), NULL);
824     else if (G_VALUE_HOLDS_INT(&currentPropertyValue))
825         g_object_set(G_OBJECT(settings), propertyName, atoi(valueAsString.get()), NULL);
826     else if (G_VALUE_HOLDS_FLOAT(&currentPropertyValue)) {
827         gfloat newValue = g_ascii_strtod(valueAsString.get(), 0);
828         g_object_set(G_OBJECT(settings), propertyName, newValue, NULL);
829     } else
830         fprintf(stderr, "LayoutTestController::overridePreference failed to override "
831                 "preference '%s'.\n", originalName.get());
832 }
833
834 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
835 {
836     printf("LayoutTestController::addUserScript not implemented.\n");
837 }
838
839 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
840 {
841     GOwnPtr<gchar> sourceCode(JSStringCopyUTF8CString(source));
842     DumpRenderTreeSupportGtk::addUserStyleSheet(mainFrame, sourceCode.get(), allFrames);
843     // FIXME: needs more investigation why userscripts/user-style-top-frame-only.html fails when allFrames is false.
844
845 }
846
847 void LayoutTestController::setDeveloperExtrasEnabled(bool enabled)
848 {
849     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
850     WebKitWebSettings* webSettings = webkit_web_view_get_settings(webView);
851
852     g_object_set(webSettings, "enable-developer-extras", enabled, NULL);
853 }
854
855 void LayoutTestController::setAsynchronousSpellCheckingEnabled(bool)
856 {
857     // FIXME: Implement this.
858 }
859
860 void LayoutTestController::showWebInspector()
861 {
862     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
863     WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
864
865     webkit_web_inspector_show(inspector);
866 }
867
868 void LayoutTestController::closeWebInspector()
869 {
870     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
871     WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
872
873     webkit_web_inspector_close(inspector);
874 }
875
876 void LayoutTestController::evaluateInWebInspector(long callId, JSStringRef script)
877 {
878     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
879     WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
880     char* scriptString = JSStringCopyUTF8CString(script);
881
882     webkit_web_inspector_execute_script(inspector, callId, scriptString);
883     g_free(scriptString);
884 }
885
886 void LayoutTestController::evaluateScriptInIsolatedWorldAndReturnValue(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
887 {
888     // FIXME: Implement this.
889 }
890
891 void LayoutTestController::evaluateScriptInIsolatedWorld(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
892 {
893     // FIXME: Implement this.
894 }
895
896 void LayoutTestController::removeAllVisitedLinks()
897 {
898     // FIXME: Implement this.
899 }
900
901 bool LayoutTestController::callShouldCloseOnWebView()
902 {
903     return DumpRenderTreeSupportGtk::shouldClose(mainFrame);
904 }
905
906 void LayoutTestController::apiTestNewWindowDataLoadBaseURL(JSStringRef utf8Data, JSStringRef baseURL)
907 {
908
909 }
910
911 void LayoutTestController::apiTestGoToCurrentBackForwardItem()
912 {
913
914 }
915
916 void LayoutTestController::setWebViewEditable(bool)
917 {
918 }
919
920 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSContextRef context, JSValueRef nodeObject) const
921 {
922     CString markerTextGChar = DumpRenderTreeSupportGtk::markerTextForListItem(mainFrame, context, nodeObject);
923     if (markerTextGChar.isNull())
924         return 0;
925
926     JSRetainPtr<JSStringRef> markerText(Adopt, JSStringCreateWithUTF8CString(markerTextGChar.data()));
927     return markerText;
928 }
929
930 void LayoutTestController::authenticateSession(JSStringRef, JSStringRef, JSStringRef)
931 {
932 }
933
934 void LayoutTestController::abortModal()
935 {
936 }
937
938 void LayoutTestController::setSerializeHTTPLoads(bool serialize)
939 {
940     DumpRenderTreeSupportGtk::setSerializeHTTPLoads(serialize);
941 }
942
943 void LayoutTestController::setMinimumTimerInterval(double minimumTimerInterval)
944 {
945     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
946     DumpRenderTreeSupportGtk::setMinimumTimerInterval(webView, minimumTimerInterval);
947 }
948
949 void LayoutTestController::setTextDirection(JSStringRef direction)
950 {
951     // FIXME: Implement.
952 }
953
954 void LayoutTestController::addChromeInputField()
955 {
956 }
957
958 void LayoutTestController::removeChromeInputField()
959 {
960 }
961
962 void LayoutTestController::focusWebView()
963 {
964 }
965
966 void LayoutTestController::setBackingScaleFactor(double)
967 {
968 }
969
970 void LayoutTestController::simulateDesktopNotificationClick(JSStringRef title)
971 {
972 }
973
974 void LayoutTestController::resetPageVisibility()
975 {
976     // FIXME: Implement this.
977 }
978
979 void LayoutTestController::setPageVisibility(const char*)
980 {
981     // FIXME: Implement this.
982 }
983
984 void LayoutTestController::setAutomaticLinkDetectionEnabled(bool)
985 {
986     // FIXME: Implement this.
987 }
988
989 void LayoutTestController::sendWebIntentResponse(JSStringRef)
990 {
991     // FIXME: Implement this.
992 }
993
994 void LayoutTestController::deliverWebIntent(JSStringRef, JSStringRef, JSStringRef)
995 {
996     // FIXME: Implement this.
997 }
998
999 void LayoutTestController::setStorageDatabaseIdleInterval(double)
1000 {
1001     // FIXME: Implement this.
1002 }