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/WKBundleBackForwardList.h>
33 #include <WebKit2/WKBundleFrame.h>
34 #include <WebKit2/WKBundleFramePrivate.h>
35 #include <WebKit2/WKBundlePagePrivate.h>
36 #include <WebKit2/WKBundleScriptWorld.h>
37 #include <WebKit2/WKBundlePrivate.h>
38 #include <WebKit2/WKRetainPtr.h>
39 #include <WebKit2/WebKit2.h>
43 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
44 // Eventually it should be changed to match.
45 const double LayoutTestController::waitToDumpWatchdogTimerInterval = 6;
47 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
51 JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
53 return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
56 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
58 JSValueRef value = propertyValue(context, object, propertyName);
59 if (!value || !JSValueIsObject(context, value))
61 return const_cast<JSObjectRef>(value);
64 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
66 JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
67 JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
70 JSValueRef getElementById = propertyObject(context, document, "getElementById");
71 if (!getElementById || !JSValueIsObject(context, getElementById))
73 JSValueRef elementIdValue = JSValueMakeString(context, elementId);
75 JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
76 if (!element || !JSValueIsObject(context, element))
78 return const_cast<JSObjectRef>(element);
81 PassRefPtr<LayoutTestController> LayoutTestController::create()
83 return adoptRef(new LayoutTestController);
86 LayoutTestController::LayoutTestController()
87 : m_whatToDump(RenderTree)
88 , m_shouldDumpAllFrameScrollPositions(false)
89 , m_shouldDumpBackForwardListsForAllWindows(false)
90 , m_shouldAllowEditing(true)
91 , m_shouldCloseExtraWindows(false)
92 , m_dumpEditingCallbacks(false)
93 , m_dumpStatusCallbacks(false)
94 , m_dumpTitleChanges(false)
96 , m_testRepaint(false)
97 , m_testRepaintSweepHorizontally(false)
102 LayoutTestController::~LayoutTestController()
106 JSClassRef LayoutTestController::wrapperClass()
108 return JSLayoutTestController::layoutTestControllerClass();
111 void LayoutTestController::display()
113 // FIXME: actually implement, once we want pixel tests
116 void LayoutTestController::waitUntilDone()
119 initializeWaitToDumpWatchdogTimerIfNeeded();
122 void LayoutTestController::waitToDumpWatchdogTimerFired()
124 invalidateWaitToDumpWatchdogTimer();
125 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
126 InjectedBundle::shared().os() << message << "\n";
127 InjectedBundle::shared().done();
130 void LayoutTestController::notifyDone()
132 if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
133 InjectedBundle::shared().page()->dump();
134 m_waitToDump = false;
137 unsigned LayoutTestController::numberOfActiveAnimations() const
139 // FIXME: Is it OK this works only for the main frame?
140 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
141 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
142 return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
145 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
147 // FIXME: Is it OK this works only for the main frame?
148 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
149 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
150 return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
153 void LayoutTestController::suspendAnimations()
155 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
156 WKBundleFrameSuspendAnimations(mainFrame);
159 void LayoutTestController::resumeAnimations()
161 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
162 WKBundleFrameResumeAnimations(mainFrame);
165 JSRetainPtr<JSStringRef> LayoutTestController::layerTreeAsText() const
167 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
168 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyLayerTreeAsText(mainFrame));
172 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
174 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
175 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
177 WKBundleAddUserScript(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
178 (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
179 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
182 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
184 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
185 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
187 WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
188 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
191 void LayoutTestController::keepWebHistory()
193 WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
196 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
198 // FIXME: Is it OK this works only for the main frame?
199 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
200 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
201 if (!JSValueIsObject(context, element))
202 return JSValueMakeUndefined(context);
203 JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
205 return JSValueMakeUndefined(context);
209 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
211 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
212 JSObjectRef element = getElementById(mainFrame, elementId);
215 WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
219 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
221 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
222 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
223 if (!element || !JSValueIsObject(context, element))
225 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
226 if (WKStringIsEmpty(text.get()))
231 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
233 WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
236 bool LayoutTestController::isCommandEnabled(JSStringRef name)
238 return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
241 void LayoutTestController::setCanOpenWindows(bool)
243 // It's not clear if or why any tests require opening windows be forbidden.
244 // For now, just ignore this setting, and if we find later it's needed we can add it.
247 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
249 WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), true);
252 unsigned LayoutTestController::windowCount()
254 return InjectedBundle::shared().pageCount();
257 void LayoutTestController::clearBackForwardList()
259 WKBundleBackForwardListClear(WKBundlePageGetBackForwardList(InjectedBundle::shared().page()->page()));
264 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
266 setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);