OSDN Git Service

am 15643db2: am 77ab6dc8: Don\'t force video end event when full screen video playing...
[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 const double LayoutTestController::waitToDumpWatchdogTimerInterval = 6;
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     platformInitialize();
98 }
99
100 LayoutTestController::~LayoutTestController()
101 {
102 }
103
104 JSClassRef LayoutTestController::wrapperClass()
105 {
106     return JSLayoutTestController::layoutTestControllerClass();
107 }
108
109 void LayoutTestController::display()
110 {
111     // FIXME: actually implement, once we want pixel tests
112 }
113
114 void LayoutTestController::waitUntilDone()
115 {
116     m_waitToDump = true;
117     initializeWaitToDumpWatchdogTimerIfNeeded();
118 }
119
120 void LayoutTestController::waitToDumpWatchdogTimerFired()
121 {
122     invalidateWaitToDumpWatchdogTimer();
123     const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
124     InjectedBundle::shared().os() << message << "\n";
125     InjectedBundle::shared().done();
126 }
127
128 void LayoutTestController::notifyDone()
129 {
130     if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
131         InjectedBundle::shared().page()->dump();
132     m_waitToDump = false;
133 }
134
135 unsigned LayoutTestController::numberOfActiveAnimations() const
136 {
137     // FIXME: Is it OK this works only for the main frame?
138     // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
139     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
140     return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
141 }
142
143 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
144 {
145     // FIXME: Is it OK this works only for the main frame?
146     // FIXME: If this is needed only for the main frame, then why is the function on WKBundleFrame instead of WKBundlePage?
147     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
148     return WKBundleFramePauseAnimationOnElementWithId(mainFrame, toWK(animationName).get(), toWK(elementId).get(), time);
149 }
150
151 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
152 {
153     WKRetainPtr<WKStringRef> sourceWK = toWK(source);
154     WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
155
156     WKBundleAddUserScript(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
157         (runAtStart ? kWKInjectAtDocumentStart : kWKInjectAtDocumentEnd),
158         (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
159 }
160
161 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
162 {
163     WKRetainPtr<WKStringRef> sourceWK = toWK(source);
164     WKRetainPtr<WKBundleScriptWorldRef> scriptWorld(AdoptWK, WKBundleScriptWorldCreateWorld());
165
166     WKBundleAddUserStyleSheet(InjectedBundle::shared().bundle(), scriptWorld.get(), sourceWK.get(), 0, 0, 0,
167         (allFrames ? kWKInjectInAllFrames : kWKInjectInTopFrameOnly));
168 }
169
170 void LayoutTestController::keepWebHistory()
171 {
172     WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
173 }
174
175 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
176 {
177     // FIXME: Is it OK this works only for the main frame?
178     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
179     JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
180     if (!JSValueIsObject(context, element))
181         return JSValueMakeUndefined(context);
182     JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element));
183     if (!value)
184         return JSValueMakeUndefined(context);
185     return value;
186 }
187
188 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
189 {
190     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
191     JSObjectRef element = getElementById(mainFrame, elementId);
192     if (!element)
193         return 0;
194     WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
195     return toJS(value);
196 }
197
198 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
199 {
200     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
201     JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
202     if (!element || !JSValueIsObject(context, element))
203         return 0;
204     WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
205     if (WKStringIsEmpty(text.get()))
206         return 0;
207     return toJS(text);
208 }
209
210 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
211 {
212     WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
213 }
214
215 bool LayoutTestController::isCommandEnabled(JSStringRef name)
216 {
217     return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
218 }
219
220 void LayoutTestController::setCanOpenWindows(bool)
221 {
222     // It's not clear if or why any tests require opening windows be forbidden.
223     // For now, just ignore this setting, and if we find later it's needed we can add it.
224 }
225
226 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
227 {
228     WKBundleOverrideXSSAuditorEnabledForTestRunner(InjectedBundle::shared().bundle(), true);
229 }
230
231 unsigned LayoutTestController::windowCount()
232 {
233     return InjectedBundle::shared().pageCount();
234 }
235
236 // Object Creation
237
238 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
239 {
240     setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
241 }
242
243 } // namespace WTR