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 static const CFTimeInterval waitToDumpWatchdogInterval = 6.0;
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)
99 LayoutTestController::~LayoutTestController()
103 JSClassRef LayoutTestController::wrapperClass()
105 return JSLayoutTestController::layoutTestControllerClass();
108 void LayoutTestController::display()
110 // FIXME: actually implement, once we want pixel tests
113 void LayoutTestController::invalidateWaitToDumpWatchdog()
115 if (m_waitToDumpWatchdog) {
116 CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
117 m_waitToDumpWatchdog = 0;
121 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
123 InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
126 void LayoutTestController::waitUntilDone()
129 if (!m_waitToDumpWatchdog) {
130 m_waitToDumpWatchdog.adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval,
131 0, 0, 0, waitUntilDoneWatchdogFired, NULL));
132 CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_waitToDumpWatchdog.get(), kCFRunLoopCommonModes);
136 void LayoutTestController::waitToDumpWatchdogTimerFired()
138 invalidateWaitToDumpWatchdog();
139 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
140 InjectedBundle::shared().os() << message << "\n";
141 InjectedBundle::shared().done();
144 void LayoutTestController::notifyDone()
146 if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
147 InjectedBundle::shared().page()->dump();
148 m_waitToDump = false;
151 unsigned LayoutTestController::numberOfActiveAnimations() const
153 // FIXME: Is it OK this works only for the main frame?
154 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
155 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
156 return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
159 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
161 // FIXME: Is it OK this works only for the main frame?
162 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
163 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
164 return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
167 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
169 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
170 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
172 WKBundleAddUserScript(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
173 (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
174 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
177 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
179 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
180 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
182 WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
183 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
186 void LayoutTestController::keepWebHistory()
188 WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
191 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
193 // FIXME: Is it OK this works only for the main frame?
194 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
195 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
196 if (!JSValueIsObject(context, element))
197 return JSValueMakeUndefined(context);
198 JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
200 return JSValueMakeUndefined(context);
204 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
206 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
207 JSObjectRef element = getElementById(mainFrame, elementId);
210 WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
214 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
216 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
217 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
218 if (!element || !JSValueIsObject(context, element))
220 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
221 if (WKStringIsEmpty(text.get()))
226 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
228 WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
231 bool LayoutTestController::isCommandEnabled(JSStringRef name)
233 return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
236 void LayoutTestController::setCanOpenWindows(bool)
238 // It's not clear if or why any tests require opening windows be forbidden.
239 // For now, just ignore this setting, and if we find later it's needed we can add it.
242 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
244 WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), true);
247 unsigned LayoutTestController::windowCount()
249 return InjectedBundle::shared().pageCount();
254 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
256 setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);