2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #include "LayoutTestController.h"
28 #include "InjectedBundle.h"
29 #include "InjectedBundlePage.h"
30 #include "JSLayoutTestController.h"
31 #include "StringFunctions.h"
32 #include <WebKit2/WKBundleFrame.h>
33 #include <WebKit2/WKBundleFramePrivate.h>
34 #include <WebKit2/WKBundlePagePrivate.h>
35 #include <WebKit2/WKBundleScriptWorld.h>
36 #include <WebKit2/WKBundlePrivate.h>
37 #include <WebKit2/WKRetainPtr.h>
38 #include <WebKit2/WebKit2.h>
42 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
43 // Eventually it should be changed to match.
44 const double LayoutTestController::waitToDumpWatchdogTimerInterval = 6;
46 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
50 JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
52 return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
55 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
57 JSValueRef value = propertyValue(context, object, propertyName);
58 if (!value || !JSValueIsObject(context, value))
60 return const_cast<JSObjectRef>(value);
63 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
65 JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
66 JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
69 JSValueRef getElementById = propertyObject(context, document, "getElementById");
70 if (!getElementById || !JSValueIsObject(context, getElementById))
72 JSValueRef elementIdValue = JSValueMakeString(context, elementId);
74 JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
75 if (!element || !JSValueIsObject(context, element))
77 return const_cast<JSObjectRef>(element);
80 PassRefPtr<LayoutTestController> LayoutTestController::create()
82 return adoptRef(new LayoutTestController);
85 LayoutTestController::LayoutTestController()
86 : m_whatToDump(RenderTree)
87 , m_shouldDumpAllFrameScrollPositions(false)
88 , m_shouldAllowEditing(true)
89 , m_shouldCloseExtraWindows(false)
90 , m_dumpEditingCallbacks(false)
91 , m_dumpStatusCallbacks(false)
92 , m_dumpTitleChanges(false)
94 , m_testRepaint(false)
95 , m_testRepaintSweepHorizontally(false)
100 LayoutTestController::~LayoutTestController()
104 JSClassRef LayoutTestController::wrapperClass()
106 return JSLayoutTestController::layoutTestControllerClass();
109 void LayoutTestController::display()
111 // FIXME: actually implement, once we want pixel tests
114 void LayoutTestController::waitUntilDone()
117 initializeWaitToDumpWatchdogTimerIfNeeded();
120 void LayoutTestController::waitToDumpWatchdogTimerFired()
122 invalidateWaitToDumpWatchdogTimer();
123 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
124 InjectedBundle::shared().os() << message << "\n";
125 InjectedBundle::shared().done();
128 void LayoutTestController::notifyDone()
130 if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
131 InjectedBundle::shared().page()->dump();
132 m_waitToDump = false;
135 unsigned LayoutTestController::numberOfActiveAnimations() const
137 // FIXME: Is it OK this works only for the main frame?
138 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
139 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
140 return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
143 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
145 // FIXME: Is it OK this works only for the main frame?
146 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
147 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
148 return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
151 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
153 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
154 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
156 WKBundleAddUserScript(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
157 (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
158 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
161 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
163 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
164 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
166 WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
167 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
170 void LayoutTestController::keepWebHistory()
172 WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
175 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
177 // FIXME: Is it OK this works only for the main frame?
178 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
179 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
180 if (!JSValueIsObject(context, element))
181 return JSValueMakeUndefined(context);
182 JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
184 return JSValueMakeUndefined(context);
188 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
190 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
191 JSObjectRef element = getElementById(mainFrame, elementId);
194 WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
198 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
200 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
201 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
202 if (!element || !JSValueIsObject(context, element))
204 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
205 if (WKStringIsEmpty(text.get()))
210 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
212 WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
215 bool LayoutTestController::isCommandEnabled(JSStringRef name)
217 return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
220 void LayoutTestController::setCanOpenWindows(bool)
222 // It's not clear if or why any tests require opening windows be forbidden.
223 // For now, just ignore this setting, and if we find later it's needed we can add it.
226 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
228 WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), true);
231 unsigned LayoutTestController::windowCount()
233 return InjectedBundle::shared().pageCount();
238 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
240 setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);