OSDN Git Service

am cd011297: am ff93a851: Bug fix 2337042 <base> URL + <img> URL = URL that starts...
[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/WKBundlePrivate.h>
36 #include <WebKit2/WKRetainPtr.h>
37 #include <WebKit2/WebKit2.h>
38
39 namespace WTR {
40
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;
44
45 static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
46 {
47     if (!object)
48         return 0;
49     JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
50     JSValueRef exception;
51     return JSObjectGetProperty(context, object, propertyNameString.get(), &exception);
52 }
53
54 static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName)
55 {
56     JSValueRef value = propertyValue(context, object, propertyName);
57     if (!value || !JSValueIsObject(context, value))
58         return 0;
59     return const_cast<JSObjectRef>(value);
60 }
61
62 static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId)
63 {
64     JSContextRef context = WKBundleFrameGetJavaScriptContext(frame);
65     JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document");
66     if (!document)
67         return 0;
68     JSValueRef getElementById = propertyObject(context, document, "getElementById");
69     if (!getElementById || !JSValueIsObject(context, getElementById))
70         return 0;
71     JSValueRef elementIdValue = JSValueMakeString(context, elementId);
72     JSValueRef exception;
73     JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception);
74     if (!element || !JSValueIsObject(context, element))
75         return 0;
76     return const_cast<JSObjectRef>(element);
77 }
78
79 PassRefPtr<LayoutTestController> LayoutTestController::create()
80 {
81     return adoptRef(new LayoutTestController);
82 }
83
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)
92     , m_waitToDump(false)
93     , m_testRepaint(false)
94     , m_testRepaintSweepHorizontally(false)
95 {
96 }
97
98 LayoutTestController::~LayoutTestController()
99 {
100 }
101
102 JSClassRef LayoutTestController::wrapperClass()
103 {
104     return JSLayoutTestController::layoutTestControllerClass();
105 }
106
107 void LayoutTestController::display()
108 {
109     // FIXME: actually implement, once we want pixel tests
110 }
111
112 void LayoutTestController::invalidateWaitToDumpWatchdog()
113 {
114     if (m_waitToDumpWatchdog) {
115         CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
116         m_waitToDumpWatchdog = 0;
117     }
118 }
119
120 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
121 {
122     InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
123 }
124
125 void LayoutTestController::waitUntilDone()
126 {
127     m_waitToDump = true;
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);
132     }
133 }
134
135 void LayoutTestController::waitToDumpWatchdogTimerFired()
136 {
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();
141 }
142
143 void LayoutTestController::notifyDone()
144 {
145     if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
146         InjectedBundle::shared().page()->dump();
147     m_waitToDump = false;
148 }
149
150 unsigned LayoutTestController::numberOfActiveAnimations() const
151 {
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);
156 }
157
158 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
159 {
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);
164 }
165
166 void LayoutTestController::keepWebHistory()
167 {
168     WKBundleSetShouldTrackVisitedLinks(InjectedBundle::shared().bundle(), true);
169 }
170
171 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element)
172 {
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));
179     if (!value)
180         return JSValueMakeUndefined(context);
181     return value;
182 }
183
184 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef elementId)
185 {
186     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
187     JSObjectRef element = getElementById(mainFrame, elementId);
188     if (!element)
189         return 0;
190     WKRetainPtr<WKStringRef> value(AdoptWK, WKBundleFrameCopyCounterValue(mainFrame, const_cast<JSObjectRef>(element)));
191     return toJS(value);
192 }
193
194 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element)
195 {
196     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
197     JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
198     if (!element || !JSValueIsObject(context, element))
199         return 0;
200     WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element)));
201     if (WKStringIsEmpty(text.get()))
202         return 0;
203     return toJS(text);
204 }
205
206 void LayoutTestController::execCommand(JSStringRef name, JSStringRef argument)
207 {
208     WKBundlePageExecuteEditingCommand(InjectedBundle::shared().page()->page(), toWK(name).get(), toWK(argument).get());
209 }
210
211 bool LayoutTestController::isCommandEnabled(JSStringRef name)
212 {
213     return WKBundlePageIsEditingCommandEnabled(InjectedBundle::shared().page()->page(), toWK(name).get());
214 }
215
216 void LayoutTestController::setCanOpenWindows(bool)
217 {
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.
220 }
221
222 unsigned LayoutTestController::windowCount()
223 {
224     return InjectedBundle::shared().pageCount();
225 }
226
227 // Object Creation
228
229 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
230 {
231     setProperty(context, windowObject, "layoutTestController", this, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
232 }
233
234 } // namespace WTR