initial import
[vuplus_webkit] / 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 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef id)
106 {
107     gchar* idGChar = JSStringCopyUTF8CString(id);
108     CString counterValueGChar = DumpRenderTreeSupportGtk::counterValueForElementById(mainFrame, idGChar);
109     g_free(idGChar);
110     if (counterValueGChar.isNull())
111         return 0;
112     JSRetainPtr<JSStringRef> counterValue(Adopt, JSStringCreateWithUTF8CString(counterValueGChar.data()));
113     return counterValue;
114 }
115
116 void LayoutTestController::keepWebHistory()
117 {
118     // FIXME: implement
119 }
120
121 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSContextRef context, JSValueRef value)
122 {
123     // FIXME: Implement this.
124     return JSValueMakeUndefined(context);
125 }
126
127 JSValueRef LayoutTestController::nodesFromRect(JSContextRef context, JSValueRef value, int x, int y, unsigned top, unsigned right, unsigned bottom, unsigned left, bool ignoreClipping)
128 {
129     return DumpRenderTreeSupportGtk::nodesFromRect(context, value, x, y, top, right, bottom, left, ignoreClipping);
130 }
131
132 JSRetainPtr<JSStringRef> LayoutTestController::layerTreeAsText() const
133 {
134     // FIXME: implement
135     JSRetainPtr<JSStringRef> string(Adopt, JSStringCreateWithUTF8CString(""));
136     return string;
137 }
138
139 int LayoutTestController::pageNumberForElementById(JSStringRef id, float pageWidth, float pageHeight)
140 {
141     gchar* idGChar = JSStringCopyUTF8CString(id);
142     int pageNumber = DumpRenderTreeSupportGtk::pageNumberForElementById(mainFrame, idGChar, pageWidth, pageHeight);
143     g_free(idGChar);
144     return pageNumber;
145 }
146
147 int LayoutTestController::numberOfPages(float pageWidth, float pageHeight)
148 {
149     return DumpRenderTreeSupportGtk::numberOfPagesForFrame(mainFrame, pageWidth, pageHeight);
150 }
151
152 JSRetainPtr<JSStringRef> LayoutTestController::pageProperty(const char* propertyName, int pageNumber) const
153 {
154     JSRetainPtr<JSStringRef> propertyValue(Adopt, JSStringCreateWithUTF8CString(DumpRenderTreeSupportGtk::pageProperty(mainFrame, propertyName, pageNumber).data()));
155     return propertyValue;
156 }
157
158 bool LayoutTestController::isPageBoxVisible(int pageNumber) const
159 {
160     return DumpRenderTreeSupportGtk::isPageBoxVisible(mainFrame, pageNumber);
161 }
162
163 JSRetainPtr<JSStringRef> LayoutTestController::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft) const
164 {
165     JSRetainPtr<JSStringRef> propertyValue(Adopt, JSStringCreateWithUTF8CString(DumpRenderTreeSupportGtk::pageSizeAndMarginsInPixels(mainFrame, pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft).data()));
166     return propertyValue;
167 }
168
169 size_t LayoutTestController::webHistoryItemCount()
170 {
171     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
172     WebKitWebBackForwardList* list = webkit_web_view_get_back_forward_list(webView);
173
174     if (!list)
175         return -1;
176
177     // We do not add the current page to the total count as it's not
178     // considered in DRT tests
179     return webkit_web_back_forward_list_get_back_length(list) +
180             webkit_web_back_forward_list_get_forward_length(list);
181 }
182
183 unsigned LayoutTestController::workerThreadCount() const
184 {
185     return DumpRenderTreeSupportGtk::workerThreadCount();
186 }
187
188 JSRetainPtr<JSStringRef> LayoutTestController::platformName() const
189 {
190     JSRetainPtr<JSStringRef> platformName(Adopt, JSStringCreateWithUTF8CString("gtk"));
191     return platformName;
192 }
193
194 void LayoutTestController::notifyDone()
195 {
196     if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
197         dump();
198     m_waitToDump = false;
199     waitForPolicy = false;
200 }
201
202 JSStringRef LayoutTestController::pathToLocalResource(JSContextRef context, JSStringRef url)
203 {
204     // Function introduced in r28690. This may need special-casing on Windows.
205     return JSStringRetain(url); // Do nothing on Unix.
206 }
207
208 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
209 {
210     gchar* relativeURL = JSStringCopyUTF8CString(url);
211     SoupURI* baseURI = soup_uri_new(webkit_web_frame_get_uri(mainFrame));
212
213     SoupURI* absoluteURI = soup_uri_new_with_base(baseURI, relativeURL);
214     soup_uri_free(baseURI);
215     g_free(relativeURL);
216
217     gchar* absoluteCString;
218     if (absoluteURI) {
219         absoluteCString = soup_uri_to_string(absoluteURI, FALSE);
220         soup_uri_free(absoluteURI);
221     } else
222         absoluteCString = JSStringCopyUTF8CString(url);
223
224     JSRetainPtr<JSStringRef> absoluteURL(Adopt, JSStringCreateWithUTF8CString(absoluteCString));
225     g_free(absoluteCString);
226
227     WorkQueue::shared()->queue(new LoadItem(absoluteURL.get(), target));
228 }
229
230 void LayoutTestController::setAcceptsEditing(bool acceptsEditing)
231 {
232     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
233     webkit_web_view_set_editable(webView, acceptsEditing);
234 }
235
236 void LayoutTestController::setAlwaysAcceptCookies(bool alwaysAcceptCookies)
237 {
238     SoupSession* session = webkit_get_default_session();
239     SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
240
241     /* If the jar was not created - we create it on demand, i.e, just
242        in case we have HTTP requests - then we must create it here in
243        order to set the proper accept policy */
244     if (!jar) {
245         jar = soup_cookie_jar_new();
246         soup_session_add_feature(session, SOUP_SESSION_FEATURE(jar));
247         g_object_unref(jar);
248     }
249
250     SoupCookieJarAcceptPolicy policy;
251
252     if (alwaysAcceptCookies)
253         policy = SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
254     else
255         policy = SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
256
257     g_object_set(G_OBJECT(jar), SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
258 }
259
260 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate, bool permissive)
261 {
262     // FIXME: implement
263 }
264
265 void LayoutTestController::waitForPolicyDelegate()
266 {
267     waitForPolicy = true;
268     setWaitToDump(true);
269 }
270
271 void LayoutTestController::setScrollbarPolicy(JSStringRef orientation, JSStringRef policy)
272 {
273     // FIXME: implement
274 }
275
276 void LayoutTestController::addOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef protocol, JSStringRef host, bool includeSubdomains)
277 {
278     gchar* sourceOriginGChar = JSStringCopyUTF8CString(sourceOrigin);
279     gchar* protocolGChar = JSStringCopyUTF8CString(protocol);
280     gchar* hostGChar = JSStringCopyUTF8CString(host);
281     DumpRenderTreeSupportGtk::whiteListAccessFromOrigin(sourceOriginGChar, protocolGChar, hostGChar, includeSubdomains);
282     g_free(sourceOriginGChar);
283     g_free(protocolGChar);
284     g_free(hostGChar);
285 }
286
287 void LayoutTestController::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef protocol, JSStringRef host, bool includeSubdomains)
288 {
289     // FIXME: implement
290 }
291
292 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
293 {
294     // FIXME: implement
295 }
296
297 void LayoutTestController::setTabKeyCyclesThroughElements(bool cycles)
298 {
299     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
300     WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
301     g_object_set(G_OBJECT(settings), "tab-key-cycles-through-elements", cycles, NULL);
302 }
303
304 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
305 {
306     // FIXME: implement
307 }
308
309 static gchar* userStyleSheet = NULL;
310 static gboolean userStyleSheetEnabled = TRUE;
311
312 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
313 {
314     userStyleSheetEnabled = flag;
315
316     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
317     WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
318     if (flag && userStyleSheet)
319         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", userStyleSheet, NULL);
320     else
321         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", "", NULL);
322 }
323
324 void LayoutTestController::setUserStyleSheetLocation(JSStringRef path)
325 {
326     g_free(userStyleSheet);
327     userStyleSheet = JSStringCopyUTF8CString(path);
328     if (userStyleSheetEnabled)
329         setUserStyleSheetEnabled(true);
330 }
331
332 void LayoutTestController::setValueForUser(JSContextRef context, JSValueRef nodeObject, JSStringRef value)
333 {
334     DumpRenderTreeSupportGtk::setValueForUser(context, nodeObject, value);
335 }
336
337 void LayoutTestController::setViewModeMediaFeature(JSStringRef mode)
338 {
339     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
340     ASSERT(view);
341
342     char* viewMode = JSStringCopyUTF8CString(mode);
343
344     if (!g_strcmp0(viewMode, "windowed"))
345         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_WINDOWED);
346     else if (!g_strcmp0(viewMode, "floating"))
347         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_FLOATING);
348     else if (!g_strcmp0(viewMode, "fullscreen"))
349         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_FULLSCREEN);
350     else if (!g_strcmp0(viewMode, "maximized"))
351         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_MAXIMIZED);
352     else if (!g_strcmp0(viewMode, "minimized"))
353         webkit_web_view_set_view_mode(view, WEBKIT_WEB_VIEW_VIEW_MODE_MINIMIZED);
354
355     g_free(viewMode);
356 }
357
358 void LayoutTestController::setWindowIsKey(bool windowIsKey)
359 {
360     // FIXME: implement
361 }
362
363 void LayoutTestController::setSmartInsertDeleteEnabled(bool flag)
364 {
365     DumpRenderTreeSupportGtk::setSmartInsertDeleteEnabled(webkit_web_frame_get_web_view(mainFrame), flag);
366 }
367
368 static gboolean waitToDumpWatchdogFired(void*)
369 {
370     waitToDumpWatchdog = 0;
371     gLayoutTestController->waitToDumpWatchdogTimerFired();
372     return FALSE;
373 }
374
375 void LayoutTestController::setWaitToDump(bool waitUntilDone)
376 {
377     static const int timeoutSeconds = 30;
378
379     m_waitToDump = waitUntilDone;
380     if (m_waitToDump && !waitToDumpWatchdog)
381         waitToDumpWatchdog = g_timeout_add_seconds(timeoutSeconds, waitToDumpWatchdogFired, 0);
382 }
383
384 int LayoutTestController::windowCount()
385 {
386     // +1 -> including the main view
387     return g_slist_length(webViewList) + 1;
388 }
389
390 void LayoutTestController::setPrivateBrowsingEnabled(bool flag)
391 {
392     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
393     ASSERT(view);
394
395     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
396     g_object_set(G_OBJECT(settings), "enable-private-browsing", flag, NULL);
397 }
398
399 void LayoutTestController::setJavaScriptCanAccessClipboard(bool flag)
400 {
401     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
402     ASSERT(view);
403
404     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
405     g_object_set(G_OBJECT(settings), "javascript-can-access-clipboard", flag, NULL);
406 }
407
408 void LayoutTestController::setXSSAuditorEnabled(bool flag)
409 {
410     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
411     ASSERT(view);
412
413     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
414     g_object_set(G_OBJECT(settings), "enable-xss-auditor", flag, NULL);
415 }
416
417 void LayoutTestController::setFrameFlatteningEnabled(bool flag)
418 {
419     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
420     ASSERT(view);
421
422     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
423     g_object_set(G_OBJECT(settings), "enable-frame-flattening", flag, NULL);
424 }
425
426 void LayoutTestController::setSpatialNavigationEnabled(bool flag)
427 {
428     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
429     ASSERT(view);
430
431     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
432     g_object_set(G_OBJECT(settings), "enable-spatial-navigation", flag, NULL);
433 }
434
435 void LayoutTestController::setAllowUniversalAccessFromFileURLs(bool flag)
436 {
437     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
438     ASSERT(view);
439
440     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
441     g_object_set(G_OBJECT(settings), "enable-universal-access-from-file-uris", flag, NULL);
442 }
443
444 void LayoutTestController::setAllowFileAccessFromFileURLs(bool flag)
445 {
446     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
447     ASSERT(view);
448
449     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
450     g_object_set(G_OBJECT(settings), "enable-file-access-from-file-uris", flag, NULL);
451 }
452
453 void LayoutTestController::setAuthorAndUserStylesEnabled(bool flag)
454 {
455     // FIXME: implement
456 }
457
458 void LayoutTestController::setAutofilled(JSContextRef context, JSValueRef nodeObject, bool isAutofilled)
459 {
460     DumpRenderTreeSupportGtk::setAutofilled(context, nodeObject, isAutofilled);
461 }
462
463 void LayoutTestController::disableImageLoading()
464 {
465     // FIXME: Implement for testing fix for https://bugs.webkit.org/show_bug.cgi?id=27896
466     // Also need to make sure image loading is re-enabled for each new test.
467 }
468
469 void LayoutTestController::setMockDeviceOrientation(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
470 {
471     // FIXME: Implement for DeviceOrientation layout tests.
472     // See https://bugs.webkit.org/show_bug.cgi?id=30335.
473 }
474
475 void LayoutTestController::setMockGeolocationPosition(double latitude, double longitude, double accuracy)
476 {
477     // FIXME: Implement for Geolocation layout tests.
478     // See https://bugs.webkit.org/show_bug.cgi?id=28264.
479 }
480
481 void LayoutTestController::setMockGeolocationError(int code, JSStringRef message)
482 {
483     // FIXME: Implement for Geolocation layout tests.
484     // See https://bugs.webkit.org/show_bug.cgi?id=28264.
485 }
486
487 void LayoutTestController::setGeolocationPermission(bool allow)
488 {
489     // FIXME: Implement for Geolocation layout tests.
490     setGeolocationPermissionCommon(allow);
491 }
492
493 int LayoutTestController::numberOfPendingGeolocationPermissionRequests()
494 {
495     // FIXME: Implement for Geolocation layout tests.
496     return -1;
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::startSpeechInput(JSContextRef inputElement)
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::setIconDatabaseEnabled(bool enabled)
512 {
513     WebKitIconDatabase* database = webkit_get_icon_database();
514     if (enabled) {
515         GOwnPtr<gchar> iconDatabasePath(g_build_filename(g_get_tmp_dir(), "DumpRenderTree", "icondatabase", NULL));
516         webkit_icon_database_set_path(database, iconDatabasePath.get());
517     } else
518         webkit_icon_database_set_path(database, 0);
519 }
520
521 void LayoutTestController::setJavaScriptProfilingEnabled(bool flag)
522 {
523     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
524     ASSERT(view);
525
526     setDeveloperExtrasEnabled(flag);
527
528     WebKitWebInspector* inspector = webkit_web_view_get_inspector(view);
529     g_object_set(G_OBJECT(inspector), "javascript-profiling-enabled", flag, NULL);
530 }
531
532 void LayoutTestController::setSelectTrailingWhitespaceEnabled(bool flag)
533 {
534     DumpRenderTreeSupportGtk::setSelectTrailingWhitespaceEnabled(flag);
535 }
536
537 void LayoutTestController::setPopupBlockingEnabled(bool flag)
538 {
539     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
540     ASSERT(view);
541
542     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
543     g_object_set(G_OBJECT(settings), "javascript-can-open-windows-automatically", !flag, NULL);
544
545 }
546
547 void LayoutTestController::setPluginsEnabled(bool flag)
548 {
549     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
550     ASSERT(view);
551
552     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
553     g_object_set(G_OBJECT(settings), "enable-plugins", flag, NULL);
554 }
555
556 bool LayoutTestController::elementDoesAutoCompleteForElementWithId(JSStringRef id) 
557 {
558     // FIXME: implement
559     return false;
560 }
561
562 void LayoutTestController::execCommand(JSStringRef name, JSStringRef value)
563 {
564     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
565     ASSERT(view);
566
567     gchar* cName = JSStringCopyUTF8CString(name);
568     gchar* cValue = JSStringCopyUTF8CString(value);
569     DumpRenderTreeSupportGtk::executeCoreCommandByName(view, cName, cValue);
570     g_free(cName);
571     g_free(cValue);
572 }
573
574 bool LayoutTestController::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray)
575 {
576     WebKitFindOptions findOptions = 0;
577     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
578     ASSERT(webView);
579
580     JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length"));
581     JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); 
582     if (!JSValueIsNumber(context, lengthValue))
583         return false;
584
585     GOwnPtr<gchar> targetString(JSStringCopyUTF8CString(target));
586
587     size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0));
588     for (size_t i = 0; i < length; ++i) {
589         JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); 
590         if (!JSValueIsString(context, value))
591             continue;
592     
593         JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0));
594
595         if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive"))
596             findOptions |= WebKit::WebFindOptionsCaseInsensitive;
597         else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts"))
598             findOptions |= WebKit::WebFindOptionsAtWordStarts;
599         else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart"))
600             findOptions |= WebKit::WebFindOptionsTreatMedialCapitalAsWordStart;
601         else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards"))
602             findOptions |= WebKit::WebFindOptionsBackwards;
603         else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround"))
604             findOptions |= WebKit::WebFindOptionsWrapAround;
605         else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection"))
606             findOptions |= WebKit::WebFindOptionsStartInSelection;
607     }   
608
609     return DumpRenderTreeSupportGtk::findString(webView, targetString.get(), findOptions); 
610 }
611
612 bool LayoutTestController::isCommandEnabled(JSStringRef name)
613 {
614     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
615     ASSERT(view);
616
617     gchar* cName = JSStringCopyUTF8CString(name);
618     bool result = DumpRenderTreeSupportGtk::isCommandEnabled(view, cName);
619     g_free(cName);
620     return result;
621 }
622
623 void LayoutTestController::setCacheModel(int cacheModel)
624 {
625     // These constants are derived from the Mac cache model enum in Source/WebKit/mac/WebView/WebPreferences.h.
626     switch (cacheModel) {
627     case 0:
628         webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
629         break;
630     case 1:
631         webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER);
632         break;
633     case 3:
634         webkit_set_cache_model(WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER);
635         break;
636     default:
637         ASSERT_NOT_REACHED();
638     }
639 }
640
641 void LayoutTestController::setPersistentUserStyleSheetLocation(JSStringRef jsURL)
642 {
643     // FIXME: implement
644 }
645
646 void LayoutTestController::clearPersistentUserStyleSheet()
647 {
648     // FIXME: implement
649 }
650
651 void LayoutTestController::clearAllApplicationCaches()
652 {
653     // FIXME: Implement to support application cache quotas.
654 }
655
656 void LayoutTestController::setApplicationCacheOriginQuota(unsigned long long quota)
657 {
658     // FIXME: Implement to support application cache quotas.
659 }
660
661 void LayoutTestController::clearApplicationCacheForOrigin(OpaqueJSString*)
662 {
663     // FIXME: Implement to support deleting all application caches for an origin.
664 }
665
666 long long LayoutTestController::localStorageDiskUsageForOrigin(JSStringRef originIdentifier)
667 {
668     // FIXME: Implement to support getting disk usage in bytes for an origin.
669     return 0;
670 }
671
672 JSValueRef LayoutTestController::originsWithApplicationCache(JSContextRef context)
673 {
674     // FIXME: Implement to get origins that contain application caches.
675     return JSValueMakeUndefined(context);
676 }
677
678 long long LayoutTestController::applicationCacheDiskUsageForOrigin(JSStringRef name)
679 {
680     // FIXME: implement
681     return 0;
682 }
683
684 void LayoutTestController::clearAllDatabases()
685 {
686     webkit_remove_all_web_databases();
687 }
688  
689 void LayoutTestController::setDatabaseQuota(unsigned long long quota)
690 {
691     WebKitSecurityOrigin* origin = webkit_web_frame_get_security_origin(mainFrame);
692     webkit_security_origin_set_web_database_quota(origin, quota);
693 }
694
695 JSValueRef LayoutTestController::originsWithLocalStorage(JSContextRef context)
696 {
697     // FIXME: implement
698     return JSValueMakeUndefined(context);
699 }
700
701 void LayoutTestController::deleteAllLocalStorage()
702 {
703         // FIXME: implement
704 }
705
706 void LayoutTestController::deleteLocalStorageForOrigin(JSStringRef originIdentifier)
707 {
708         // FIXME: implement
709 }
710
711 void LayoutTestController::observeStorageTrackerNotifications(unsigned number)
712 {
713         // FIXME: implement
714 }
715
716 void LayoutTestController::syncLocalStorage()
717 {
718     // FIXME: implement
719 }
720
721 void LayoutTestController::setDomainRelaxationForbiddenForURLScheme(bool, JSStringRef)
722 {
723     // FIXME: implement
724 }
725
726 void LayoutTestController::goBack()
727 {
728     // FIXME: implement to enable loader/navigation-while-deferring-loads.html
729 }
730
731 void LayoutTestController::setDefersLoading(bool)
732 {
733     // FIXME: implement to enable loader/navigation-while-deferring-loads.html
734 }
735
736 void LayoutTestController::setAppCacheMaximumSize(unsigned long long size)
737 {
738     webkit_application_cache_set_maximum_size(size);
739 }
740
741 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
742 {    
743     gchar* name = JSStringCopyUTF8CString(animationName);
744     gchar* element = JSStringCopyUTF8CString(elementId);
745     bool returnValue = DumpRenderTreeSupportGtk::pauseAnimation(mainFrame, name, time, element);
746     g_free(name);
747     g_free(element);
748     return returnValue;
749 }
750
751 bool LayoutTestController::pauseTransitionAtTimeOnElementWithId(JSStringRef propertyName, double time, JSStringRef elementId)
752 {    
753     gchar* name = JSStringCopyUTF8CString(propertyName);
754     gchar* element = JSStringCopyUTF8CString(elementId);
755     bool returnValue = DumpRenderTreeSupportGtk::pauseTransition(mainFrame, name, time, element);
756     g_free(name);
757     g_free(element);
758     return returnValue;
759 }
760
761 bool LayoutTestController::sampleSVGAnimationForElementAtTime(JSStringRef animationId, double time, JSStringRef elementId)
762 {    
763     gchar* name = JSStringCopyUTF8CString(animationId);
764     gchar* element = JSStringCopyUTF8CString(elementId);
765     bool returnValue = DumpRenderTreeSupportGtk::pauseSVGAnimation(mainFrame, name, time, element);
766     g_free(name);
767     g_free(element);
768     return returnValue;
769 }
770
771 unsigned LayoutTestController::numberOfActiveAnimations() const
772 {
773     return DumpRenderTreeSupportGtk::numberOfActiveAnimations(mainFrame);
774 }
775
776 void LayoutTestController::suspendAnimations() const
777 {
778     DumpRenderTreeSupportGtk::suspendAnimations(mainFrame);
779 }
780
781 void LayoutTestController::resumeAnimations() const
782 {
783     DumpRenderTreeSupportGtk::resumeAnimations(mainFrame);
784 }
785
786 void LayoutTestController::overridePreference(JSStringRef key, JSStringRef value)
787 {
788     GOwnPtr<gchar> originalName(JSStringCopyUTF8CString(key));
789     GOwnPtr<gchar> valueAsString(JSStringCopyUTF8CString(value));
790
791     WebKitWebView* view = webkit_web_frame_get_web_view(mainFrame);
792     ASSERT(view);
793
794     // This transformation could be handled by a hash table (and it once was), but
795     // having it prominent, makes it easier for people from other ports to keep the
796     // list up to date.
797     const gchar* propertyName = 0;
798     if (g_str_equal(originalName.get(), "WebKitJavaScriptEnabled"))
799         propertyName = "enable-scripts";
800     else if (g_str_equal(originalName.get(), "WebKitDefaultFontSize"))
801         propertyName = "default-font-size";
802     else if (g_str_equal(originalName.get(), "WebKitEnableCaretBrowsing"))
803         propertyName = "enable-caret-browsing";
804     else if (g_str_equal(originalName.get(), "WebKitUsesPageCachePreferenceKey"))
805         propertyName = "enable-page-cache";
806     else if (g_str_equal(originalName.get(), "WebKitPluginsEnabled"))
807         propertyName = "enable-plugins";
808     else if (g_str_equal(originalName.get(), "WebKitHyperlinkAuditingEnabled"))
809         propertyName = "enable-hyperlink-auditing";
810     else if (g_str_equal(originalName.get(), "WebKitWebGLEnabled"))
811         propertyName = "enable-webgl";
812     else if (g_str_equal(originalName.get(), "WebKitTabToLinksPreferenceKey")) {
813         DumpRenderTreeSupportGtk::setLinksIncludedInFocusChain(!g_ascii_strcasecmp(valueAsString.get(), "true") || !g_ascii_strcasecmp(valueAsString.get(), "1"));
814         return;
815     } else {
816         fprintf(stderr, "LayoutTestController::overridePreference tried to override "
817                 "unknown preference '%s'.\n", originalName.get());
818         return;
819     }
820
821     WebKitWebSettings* settings = webkit_web_view_get_settings(view);
822     GParamSpec* pspec = g_object_class_find_property(G_OBJECT_CLASS(
823         WEBKIT_WEB_SETTINGS_GET_CLASS(settings)), propertyName);
824     GValue currentPropertyValue = { 0, { { 0 } } };
825     g_value_init(&currentPropertyValue, pspec->value_type);
826
827     if (G_VALUE_HOLDS_STRING(&currentPropertyValue))
828         g_object_set(settings, propertyName, valueAsString.get(), NULL);
829     else if (G_VALUE_HOLDS_BOOLEAN(&currentPropertyValue))
830         g_object_set(G_OBJECT(settings), propertyName, !g_ascii_strcasecmp(valueAsString.get(), "true")
831                         || !g_ascii_strcasecmp(valueAsString.get(), "1"), NULL);
832     else if (G_VALUE_HOLDS_INT(&currentPropertyValue))
833         g_object_set(G_OBJECT(settings), propertyName, atoi(valueAsString.get()), NULL);
834     else if (G_VALUE_HOLDS_FLOAT(&currentPropertyValue)) {
835         gfloat newValue = g_ascii_strtod(valueAsString.get(), 0);
836         g_object_set(G_OBJECT(settings), propertyName, newValue, NULL);
837     } else
838         fprintf(stderr, "LayoutTestController::overridePreference failed to override "
839                 "preference '%s'.\n", originalName.get());
840 }
841
842 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
843 {
844     printf("LayoutTestController::addUserScript not implemented.\n");
845 }
846
847 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
848 {
849     GOwnPtr<gchar> sourceCode(JSStringCopyUTF8CString(source));
850     DumpRenderTreeSupportGtk::addUserStyleSheet(mainFrame, sourceCode.get(), allFrames);
851     // FIXME: needs more investigation why userscripts/user-style-top-frame-only.html fails when allFrames is false.
852
853 }
854
855 void LayoutTestController::setDeveloperExtrasEnabled(bool enabled)
856 {
857     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
858     WebKitWebSettings* webSettings = webkit_web_view_get_settings(webView);
859
860     g_object_set(webSettings, "enable-developer-extras", enabled, NULL);
861 }
862
863 void LayoutTestController::setAsynchronousSpellCheckingEnabled(bool)
864 {
865     // FIXME: Implement this.
866 }
867
868 void LayoutTestController::showWebInspector()
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_show(inspector);
874 }
875
876 void LayoutTestController::closeWebInspector()
877 {
878     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
879     WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
880
881     webkit_web_inspector_close(inspector);
882 }
883
884 void LayoutTestController::evaluateInWebInspector(long callId, JSStringRef script)
885 {
886     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
887     WebKitWebInspector* inspector = webkit_web_view_get_inspector(webView);
888     char* scriptString = JSStringCopyUTF8CString(script);
889
890     webkit_web_inspector_execute_script(inspector, callId, scriptString);
891     g_free(scriptString);
892 }
893
894 void LayoutTestController::evaluateScriptInIsolatedWorld(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
895 {
896     // FIXME: Implement this.
897 }
898
899 void LayoutTestController::removeAllVisitedLinks()
900 {
901     // FIXME: Implement this.
902 }
903
904 bool LayoutTestController::callShouldCloseOnWebView()
905 {
906     return DumpRenderTreeSupportGtk::shouldClose(mainFrame);
907 }
908
909 void LayoutTestController::apiTestNewWindowDataLoadBaseURL(JSStringRef utf8Data, JSStringRef baseURL)
910 {
911
912 }
913
914 void LayoutTestController::apiTestGoToCurrentBackForwardItem()
915 {
916
917 }
918
919 void LayoutTestController::setWebViewEditable(bool)
920 {
921 }
922
923 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSContextRef context, JSValueRef nodeObject) const
924 {
925     CString markerTextGChar = DumpRenderTreeSupportGtk::markerTextForListItem(mainFrame, context, nodeObject);
926     if (markerTextGChar.isNull())
927         return 0;
928
929     JSRetainPtr<JSStringRef> markerText(Adopt, JSStringCreateWithUTF8CString(markerTextGChar.data()));
930     return markerText;
931 }
932
933 void LayoutTestController::authenticateSession(JSStringRef, JSStringRef, JSStringRef)
934 {
935 }
936
937 void LayoutTestController::setEditingBehavior(const char* editingBehavior)
938 {
939     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
940     WebKitWebSettings* settings = webkit_web_view_get_settings(webView);
941
942     if (!strcmp(editingBehavior, "win"))
943         g_object_set(G_OBJECT(settings), "editing-behavior", WEBKIT_EDITING_BEHAVIOR_WINDOWS, NULL);
944     else if (!strcmp(editingBehavior, "mac"))
945         g_object_set(G_OBJECT(settings), "editing-behavior", WEBKIT_EDITING_BEHAVIOR_MAC, NULL);
946     else if (!strcmp(editingBehavior, "unix"))
947         g_object_set(G_OBJECT(settings), "editing-behavior", WEBKIT_EDITING_BEHAVIOR_UNIX, NULL);
948 }
949
950 void LayoutTestController::abortModal()
951 {
952 }
953
954 bool LayoutTestController::hasSpellingMarker(int from, int length)
955 {
956     return DumpRenderTreeSupportGtk::webkitWebFrameSelectionHasSpellingMarker(mainFrame, from, length);
957 }
958
959 bool LayoutTestController::hasGrammarMarker(int from, int length)
960 {
961     return false;
962 }
963
964 void LayoutTestController::dumpConfigurationForViewport(int deviceDPI, int deviceWidth, int deviceHeight, int availableWidth, int availableHeight)
965 {
966     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
967     ASSERT(webView);
968     DumpRenderTreeSupportGtk::dumpConfigurationForViewport(webView, deviceDPI, deviceWidth, deviceHeight, availableWidth, availableHeight);
969 }
970
971 void LayoutTestController::setSerializeHTTPLoads(bool)
972 {
973     // FIXME: Implement if needed for https://bugs.webkit.org/show_bug.cgi?id=50758.
974 }
975
976 void LayoutTestController::setMinimumTimerInterval(double minimumTimerInterval)
977 {
978     WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame);
979     DumpRenderTreeSupportGtk::setMinimumTimerInterval(webView, minimumTimerInterval);
980 }
981
982 void LayoutTestController::setTextDirection(JSStringRef direction)
983 {
984     // FIXME: Implement.
985 }
986
987 void LayoutTestController::allowRoundingHacks()
988 {
989 }