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/WKBundlePrivate.h>
36 #include <WebKit2/WKRetainPtr.h>
37 #include <WebKit2/WebKit2.h>
41 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
42 // Eventually it should be changed to match.
43 static const CFTimeInterval waitToDumpWatchdogInterval = 6.0;
45 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
49 JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
51 return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
54 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
56 JSValueRef value = propertyValue(context, object, propertyName);
57 if (!value || !JSValueIsObject(context, value))
59 return const_cast<JSObjectRef>(value);
62 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
64 JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
65 JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
68 JSValueRef getElementById = propertyObject(context, document, "getElementById");
69 if (!getElementById || !JSValueIsObject(context, getElementById))
71 JSValueRef elementIdValue = JSValueMakeString(context, elementId);
73 JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
74 if (!element || !JSValueIsObject(context, element))
76 return const_cast<JSObjectRef>(element);
79 PassRefPtr<LayoutTestController> LayoutTestController::create()
81 return adoptRef(new LayoutTestController);
84 LayoutTestController::LayoutTestController()
85 : m_whatToDump(RenderTree)
86 , m_shouldDumpAllFrameScrollPositions(false)
87 , m_shouldAllowEditing(true)
88 , m_shouldCloseExtraWindows(false)
89 , m_dumpEditingCallbacks(false)
90 , m_dumpStatusCallbacks(false)
91 , m_dumpTitleChanges(false)
93 , m_testRepaint(false)
94 , m_testRepaintSweepHorizontally(false)
98 LayoutTestController::~LayoutTestController()
102 JSClassRef LayoutTestController::wrapperClass()
104 return JSLayoutTestController::layoutTestControllerClass();
107 void LayoutTestController::display()
109 // FIXME: actually implement, once we want pixel tests
112 void LayoutTestController::invalidateWaitToDumpWatchdog()
114 if (m_waitToDumpWatchdog) {
115 CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
116 m_waitToDumpWatchdog = 0;
120 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
122 InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
125 void LayoutTestController::waitUntilDone()
128 if (!m_waitToDumpWatchdog) {
129 m_waitToDumpWatchdog.adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval,
130 0, 0, 0, waitUntilDoneWatchdogFired, NULL));
131 CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_waitToDumpWatchdog.get(), kCFRunLoopCommonModes);
135 void LayoutTestController::waitToDumpWatchdogTimerFired()
137 invalidateWaitToDumpWatchdog();
138 const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
139 InjectedBundle::shared().os() << message << "\n";
140 InjectedBundle::shared().done();
143 void LayoutTestController::notifyDone()
145 if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
146 InjectedBundle::shared().page()->dump();
147 m_waitToDump = false;
150 unsigned LayoutTestController::numberOfActiveAnimations() const
152 // FIXME: Is it OK this works only for the main frame?
153 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
154 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
155 return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
158 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
160 // FIXME: Is it OK this works only for the main frame?
161 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
162 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
163 return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
166 void LayoutTestController::keepWebHistory()
168 WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
171 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
173 // FIXME: Is it OK this works only for the main frame?
174 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
175 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
176 if (!JSValueIsObject(context, element))
177 return JSValueMakeUndefined(context);
178 JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
180 return JSValueMakeUndefined(context);
184 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
186 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
187 JSObjectRef element = getElementById(mainFrame, elementId);
190 WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
194 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
196 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
197 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
198 if (!element || !JSValueIsObject(context, element))
200 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
201 if (WKStringIsEmpty(text.get()))
206 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
208 WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
211 bool LayoutTestController::isCommandEnabled(JSStringRef name)
213 return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
216 void LayoutTestController::setCanOpenWindows(bool)
218 // It's not clear if or why any tests require opening windows be forbidden.
219 // For now, just ignore this setting, and if we find later it's needed we can add it.
222 unsigned LayoutTestController::windowCount()
224 return InjectedBundle::shared().pageCount();
229 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
231 setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);