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