OSDN Git Service

Merge WebKit at r66666 : Initial merge by git.
[android-x86/external-webkit.git] / WebKitTools / WebKitTestRunner / InjectedBundle / LayoutTestController.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "LayoutTestController.h"
27
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>
39
40 namespace WTR {
41
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;
45
46 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
47 {
48     if (!object)
49         return 0;
50     JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
51     JSValueRef exception;
52     return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
53 }
54
55 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
56 {
57     JSValueRef value = propertyValue(context, object, propertyName);
58     if (!value || !JSValueIsObject(context, value))
59         return 0;
60     return const_cast<JSObjectRef>(value);
61 }
62
63 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
64 {
65     JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
66     JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
67     if (!document)
68         return 0;
69     JSValueRef getElementById = propertyObject(context, document, "getElementById");
70     if (!getElementById || !JSValueIsObject(context, getElementById))
71         return 0;
72     JSValueRef elementIdValue = JSValueMakeString(context, elementId);
73     JSValueRef exception;
74     JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
75     if (!element || !JSValueIsObject(context, element))
76         return 0;
77     return const_cast<JSObjectRef>(element);
78 }
79
80 PassRefPtr<LayoutTestController> LayoutTestController::create()
81 {
82     return adoptRef(new LayoutTestController);
83 }
84
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)
93     , m_waitToDump(false)
94     , m_testRepaint(false)
95     , m_testRepaintSweepHorizontally(false)
96 {
97 }
98
99 LayoutTestController::~LayoutTestController()
100 {
101 }
102
103 JSClassRef LayoutTestController::wrapperClass()
104 {
105     return JSLayoutTestController::layoutTestControllerClass();
106 }
107
108 void LayoutTestController::display()
109 {
110     // FIXME: actually implement, once we want pixel tests
111 }
112
113 void LayoutTestController::invalidateWaitToDumpWatchdog()
114 {
115     if (m_waitToDumpWatchdog) {
116         CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
117         m_waitToDumpWatchdog = 0;
118     }
119 }
120
121 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
122 {
123     InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
124 }
125
126 void LayoutTestController::waitUntilDone()
127 {
128     m_waitToDump = true;
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);
133     }
134 }
135
136 void LayoutTestController::waitToDumpWatchdogTimerFired()
137 {
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();
142 }
143
144 void LayoutTestController::notifyDone()
145 {
146     if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
147         InjectedBundle::shared().page()->dump();
148     m_waitToDump = false;
149 }
150
151 unsigned LayoutTestController::numberOfActiveAnimations() const
152 {
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);
157 }
158
159 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
160 {
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);
165 }
166
167 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
168 {
169     WKRetainPtr<WKStringRef> sourceWK = toWK(source);
170     WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
171
172     WKBundleAddUserScript(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
173         (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
174         (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
175 }
176
177 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
178 {
179     WKRetainPtr<WKStringRef> sourceWK = toWK(source);
180     WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
181
182     WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
183         (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
184 }
185
186 void LayoutTestController::keepWebHistory()
187 {
188     WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
189 }
190
191 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
192 {
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));
199     if (!value)
200         return JSValueMakeUndefined(context);
201     return value;
202 }
203
204 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
205 {
206     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
207     JSObjectRef element = getElementById(mainFrame, elementId);
208     if (!element)
209         return 0;
210     WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
211     return toJS(value);
212 }
213
214 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
215 {
216     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
217     JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
218     if (!element || !JSValueIsObject(context, element))
219         return 0;
220     WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
221     if (WKStringIsEmpty(text.get()))
222         return 0;
223     return toJS(text);
224 }
225
226 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
227 {
228     WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
229 }
230
231 bool LayoutTestController::isCommandEnabled(JSStringRef name)
232 {
233     return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
234 }
235
236 void LayoutTestController::setCanOpenWindows(bool)
237 {
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.
240 }
241
242 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
243 {
244     WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), true);
245 }
246
247 unsigned LayoutTestController::windowCount()
248 {
249     return InjectedBundle::shared().pageCount();
250 }
251
252 // Object Creation
253
254 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
255 {
256     setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
257 }
258
259 } // namespace WTR