OSDN Git Service

Merge "Crash fix, don't delete WebUrlLoaderClient before WebRequest is finished."
[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 <JavaScriptCore/JSRetainPtr.h>
32 #include <WebKit2/WKBundleFrame.h>
33 #include <WebKit2/WKRetainPtr.h>
34 #include <WebKit2/WKStringCF.h>
35 #include <WebKit2/WebKit2.h>
36
37 namespace WTR {
38
39 PassRefPtr<LayoutTestController> LayoutTestController::create(const std::string& testPathOrURL)
40 {
41     return adoptRef(new LayoutTestController(testPathOrURL));
42 }
43
44 LayoutTestController::LayoutTestController(const std::string& testPathOrURL)
45     : m_dumpAsText(false)
46     , m_dumpStatusCallbacks(false)
47     , m_waitToDump(false)
48     , m_testRepaint(false)
49     , m_testRepaintSweepHorizontally(false)
50     , m_testPathOrURL(testPathOrURL)
51 {
52 }
53
54 LayoutTestController::~LayoutTestController()
55 {
56 }
57
58 JSClassRef LayoutTestController::wrapperClass()
59 {
60     return JSLayoutTestController::layoutTestControllerClass();
61 }
62
63 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
64 // Eventually it should be changed to match.
65 static const CFTimeInterval waitToDumpWatchdogInterval = 6.0;
66
67 void LayoutTestController::display()
68 {
69     // FIXME: actually implement, once we want pixel tests
70 }
71
72 void LayoutTestController::invalidateWaitToDumpWatchdog()
73 {
74     if (m_waitToDumpWatchdog) {
75         CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
76         m_waitToDumpWatchdog = 0;
77     }
78 }
79
80 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
81 {
82     InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
83 }
84
85 void LayoutTestController::waitUntilDone()
86 {
87     m_waitToDump = true;
88     if (!m_waitToDumpWatchdog) {
89         m_waitToDumpWatchdog.adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval, 
90                                                       0, 0, 0, waitUntilDoneWatchdogFired, NULL));
91         CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_waitToDumpWatchdog.get(), kCFRunLoopCommonModes);
92     }
93 }
94
95 void LayoutTestController::waitToDumpWatchdogTimerFired()
96 {
97     invalidateWaitToDumpWatchdog();
98     const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
99     InjectedBundle::shared().os() << message << "\n";
100     InjectedBundle::shared().done();
101 }
102
103 void LayoutTestController::notifyDone()
104 {
105     if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
106         InjectedBundle::shared().page()->dump();
107     m_waitToDump = false;
108 }
109
110 unsigned LayoutTestController::numberOfActiveAnimations() const
111 {
112     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
113     return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
114 }
115
116 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
117 {
118     RetainPtr<CFStringRef> idCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, elementId));
119     WKRetainPtr<WKStringRef> idWK(AdoptWK, WKStringCreateWithCFString(idCF.get()));
120     RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, animationName));
121     WKRetainPtr<WKStringRef> nameWK(AdoptWK, WKStringCreateWithCFString(nameCF.get()));
122
123     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
124     return WKBundleFramePauseAnimationOnElementWithId(mainFrame, nameWK.get(), idWK.get(), time);
125 }
126
127 // Object Creation
128
129 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
130 {
131     JSRetainPtr<JSStringRef> layoutTestContollerStr(Adopt, JSStringCreateWithUTF8CString("layoutTestController"));
132     JSObjectSetProperty(context, windowObject, layoutTestContollerStr.get(), JSWrapper::wrap(context, this), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
133 }
134
135 } // namespace WTR