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.
27 #include "LayoutTestController.h"
29 #include "InjectedBundle.h"
30 #include "InjectedBundlePage.h"
31 #include "JSLayoutTestController.h"
32 #include "PlatformWebView.h"
33 #include "StringFunctions.h"
34 #include "TestController.h"
35 #include <WebKit2/WKBundleBackForwardList.h>
36 #include <WebKit2/WKBundleFrame.h>
37 #include <WebKit2/WKBundleFramePrivate.h>
38 #include <WebKit2/WKBundleInspector.h>
39 #include <WebKit2/WKBundleNodeHandlePrivate.h>
40 #include <WebKit2/WKBundlePagePrivate.h>
41 #include <WebKit2/WKBundlePrivate.h>
42 #include <WebKit2/WKBundleScriptWorld.h>
43 #include <WebKit2/WKRetainPtr.h>
44 #include <WebKit2/WebKit2.h>
45 #include <wtf/HashMap.h>
49 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
50 // Eventually it should be changed to match.
51 const double LayoutTestController::waitToDumpWatchdogTimerInterval = 6;
53 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
57 JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
59 return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
62 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
64 JSValueRef value = propertyValue(context, object, propertyName);
65 if (!value || !JSValueIsObject(context, value))
67 return const_cast<JSObjectRef>(value);
70 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
72 JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
73 JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
76 JSValueRef getElementById = propertyObject(context, document, "getElementById");
77 if (!getElementById || !JSValueIsObject(context, getElementById))
79 JSValueRef elementIdValue = JSValueMakeString(context, elementId);
81 JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
82 if (!element || !JSValueIsObject(context, element))
84 return const_cast<JSObjectRef>(element);
87 PassRefPtr<LayoutTestController> LayoutTestController::create()
89 return adoptRef(new LayoutTestController);
92 LayoutTestController::LayoutTestController()
93 : m_whatToDump(RenderTree)
94 , m_shouldDumpAllFrameScrollPositions(false)
95 , m_shouldDumpBackForwardListsForAllWindows(false)
96 , m_shouldAllowEditing(true)
97 , m_shouldCloseExtraWindows(false)
98 , m_dumpEditingCallbacks(false)
99 , m_dumpStatusCallbacks(false)
100 , m_dumpTitleChanges(false)
102 , m_waitToDump(false)
103 , m_testRepaint(false)
104 , m_testRepaintSweepHorizontally(false)
105 , m_willSendRequestReturnsNull(false)
107 platformInitialize();
110 LayoutTestController::~LayoutTestController()
114 JSClassRef LayoutTestController::wrapperClass()
116 return JSLayoutTestController::layoutTestControllerClass();
119 void LayoutTestController::display()
121 // FIXME: actually implement, once we want pixel tests
124 void LayoutTestController::dumpAsText()
126 m_whatToDump = MainFrameText;
127 m_dumpPixels = false;
130 void LayoutTestController::waitUntilDone()
133 initializeWaitToDumpWatchdogTimerIfNeeded();
136 void LayoutTestController::waitToDumpWatchdogTimerFired()
138 invalidateWaitToDumpWatchdogTimer();
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 (!InjectedBundle::shared().isTestRunning())
149 if (m_waitToDump && !InjectedBundle::shared().topLoadingFrame())
150 InjectedBundle::shared().page()->dump();
152 m_waitToDump = false;
155 unsigned LayoutTestController::numberOfActiveAnimations() const
157 // FIXME: Is it OK this works only for the main frame?
158 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
159 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
160 return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
163 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
165 // FIXME: Is it OK this works only for the main frame?
166 // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
167 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
168 return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
171 void LayoutTestController::suspendAnimations()
173 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
174 WKBundleFrameSuspendAnimations(mainFrame);
177 void LayoutTestController::resumeAnimations()
179 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
180 WKBundleFrameResumeAnimations(mainFrame);
183 JSRetainPtr<JSStringRef> LayoutTestController::layerTreeAsText() const
185 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
186 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyLayerTreeAsText(mainFrame));
190 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
192 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
193 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
195 WKBundleAddUserScript(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
196 (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
197 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
200 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
202 WKRetainPtr<WKStringRef> sourceWK = toWK(source);
203 WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
205 WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
206 (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
209 void LayoutTestController::keepWebHistory()
211 WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
214 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
216 // FIXME: Is it OK this works only for the main frame?
217 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
218 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
219 if (!JSValueIsObject(context, element))
220 return JSValueMakeUndefined(context);
221 JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
223 return JSValueMakeUndefined(context);
227 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
229 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
230 JSObjectRef element = getElementById(mainFrame, elementId);
233 WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
237 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
239 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
240 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
241 if (!element || !JSValueIsObject(context, element))
243 WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
244 if (WKStringIsEmpty(text.get()))
249 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
251 WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
254 bool LayoutTestController::findString(JSStringRef target, JSValueRef optionsArrayAsValue)
256 WKFindOptions options = 0;
258 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
259 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
260 JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length"));
261 JSObjectRef optionsArray = JSValueToObject(context, optionsArrayAsValue, 0);
262 JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0);
263 if (!JSValueIsNumber(context, lengthValue))
266 size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0));
267 for (size_t i = 0; i < length; ++i) {
268 JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0);
269 if (!JSValueIsString(context, value))
272 JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0));
274 if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive"))
275 options |= kWKFindOptionsCaseInsensitive;
276 else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts"))
277 options |= kWKFindOptionsAtWordStarts;
278 else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart"))
279 options |= kWKFindOptionsTreatMedialCapitalAsWordStart;
280 else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards"))
281 options |= kWKFindOptionsBackwards;
282 else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround"))
283 options |= kWKFindOptionsWrapAround;
284 else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection")) {
285 // FIXME: No kWKFindOptionsStartInSelection.
289 return WKBundlePageFindString(InjectedBundle::shared().page()->page(), toWK(target).get(), options);
292 void LayoutTestController::clearAllDatabases()
294 WKBundleClearAllDatabases(InjectedBundle::shared().bundle());
297 void LayoutTestController::setDatabaseQuota(uint64_t quota)
299 return WKBundleSetDatabaseQuota(InjectedBundle::shared().bundle(), quota);
302 bool LayoutTestController::isCommandEnabled(JSStringRef name)
304 return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
307 void LayoutTestController::setCanOpenWindows(bool)
309 // It's not clear if or why any tests require opening windows be forbidden.
310 // For now, just ignore this setting, and if we find later it's needed we can add it.
313 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
315 WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), true);
318 void LayoutTestController::setAllowUniversalAccessFromFileURLs(bool enabled)
320 WKBundleOverrideAllowUniversalAccessFromFileURLsForTestRunner(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
323 void LayoutTestController::setAllowFileAccessFromFileURLs(bool enabled)
325 WKBundleSetAllowFileAccessFromFileURLs(InjectedBundle::shared().bundle(), InjectedBundle::shared().pageGroup(), enabled);
328 int LayoutTestController::numberOfPages(double pageWidthInPixels, double pageHeightInPixels)
330 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
331 return WKBundleNumberOfPages(InjectedBundle::shared().bundle(), mainFrame, pageWidthInPixels, pageHeightInPixels);
334 int LayoutTestController::pageNumberForElementById(JSStringRef id, double pageWidthInPixels, double pageHeightInPixels)
336 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
337 return WKBundlePageNumberForElementById(InjectedBundle::shared().bundle(), mainFrame, toWK(id).get(), pageWidthInPixels, pageHeightInPixels);
340 unsigned LayoutTestController::windowCount()
342 return InjectedBundle::shared().pageCount();
345 JSValueRef LayoutTestController::shadowRoot(JSValueRef element)
347 WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
348 JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
350 if (!element || !JSValueIsObject(context, element))
351 return JSValueMakeNull(context);
353 WKRetainPtr<WKBundleNodeHandleRef> domElement = adoptWK(WKBundleNodeHandleCreate(context, const_cast<JSObjectRef>(element)));
355 return JSValueMakeNull(context);
357 WKRetainPtr<WKBundleNodeHandleRef> shadowRootDOMElement = adoptWK(WKBundleNodeHandleCopyElementShadowRoot(domElement.get()));
358 if (!shadowRootDOMElement)
359 return JSValueMakeNull(context);
361 return WKBundleFrameGetJavaScriptWrapperForNodeForWorld(mainFrame, shadowRootDOMElement.get(), WKBundleScriptWorldNormalWorld());
364 void LayoutTestController::clearBackForwardList()
366 WKBundleBackForwardListClear(WKBundlePageGetBackForwardList(InjectedBundle::shared().page()->page()));
371 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
373 setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
376 void LayoutTestController::showWebInspector()
378 WKBundleInspectorShow(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
381 void LayoutTestController::closeWebInspector()
383 WKBundleInspectorClose(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()));
386 void LayoutTestController::evaluateInWebInspector(long callID, JSStringRef script)
388 WKRetainPtr<WKStringRef> scriptWK = toWK(script);
389 WKBundleInspectorEvaluateScriptForTest(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()), callID, scriptWK.get());
392 void LayoutTestController::setTimelineProfilingEnabled(bool enabled)
394 WKBundleInspectorSetPageProfilingEnabled(WKBundlePageGetInspector(InjectedBundle::shared().page()->page()), enabled);
397 typedef WTF::HashMap<unsigned, WKRetainPtr<WKBundleScriptWorldRef> > WorldMap;
398 static WorldMap& worldMap()
400 static WorldMap& map = *new WorldMap;
404 unsigned LayoutTestController::worldIDForWorld(WKBundleScriptWorldRef world)
406 WorldMap::const_iterator end = worldMap().end();
407 for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
408 if (it->second == world)
415 void LayoutTestController::evaluateScriptInIsolatedWorld(JSContextRef context, unsigned worldID, JSStringRef script)
417 // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
418 // that is created once and cached forever.
419 WKRetainPtr<WKBundleScriptWorldRef> world;
421 world.adopt(WKBundleScriptWorldCreateWorld());
423 WKRetainPtr<WKBundleScriptWorldRef>& worldSlot = worldMap().add(worldID, 0).first->second;
425 worldSlot.adopt(WKBundleScriptWorldCreateWorld());
429 WKBundleFrameRef frame = WKBundleFrameForJavaScriptContext(context);
431 frame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
433 JSGlobalContextRef jsContext = WKBundleFrameGetJavaScriptContextForWorld(frame, world.get());
434 JSEvaluateScript(jsContext, script, 0, 0, 0, 0);
437 void LayoutTestController::setPOSIXLocale(JSStringRef locale)
440 JSStringGetUTF8CString(locale, localeBuf, sizeof(localeBuf));
441 setlocale(LC_ALL, localeBuf);