WebKitTools/WebKitTestRunner/InjectedBundle/LayoutTestController.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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_shouldDumpAllFrameScrollPositions(false)
       
    47     , m_dumpStatusCallbacks(false)
       
    48     , m_waitToDump(false)
       
    49     , m_testRepaint(false)
       
    50     , m_testRepaintSweepHorizontally(false)
       
    51     , m_testPathOrURL(testPathOrURL)
       
    52 {
       
    53 }
       
    54 
       
    55 LayoutTestController::~LayoutTestController()
       
    56 {
       
    57 }
       
    58 
       
    59 JSClassRef LayoutTestController::wrapperClass()
       
    60 {
       
    61     return JSLayoutTestController::layoutTestControllerClass();
       
    62 }
       
    63 
       
    64 // This is lower than DumpRenderTree's timeout, to make it easier to work through the failures
       
    65 // Eventually it should be changed to match.
       
    66 static const CFTimeInterval waitToDumpWatchdogInterval = 6.0;
       
    67 
       
    68 void LayoutTestController::display()
       
    69 {
       
    70     // FIXME: actually implement, once we want pixel tests
       
    71 }
       
    72 
       
    73 void LayoutTestController::invalidateWaitToDumpWatchdog()
       
    74 {
       
    75     if (m_waitToDumpWatchdog) {
       
    76         CFRunLoopTimerInvalidate(m_waitToDumpWatchdog.get());
       
    77         m_waitToDumpWatchdog = 0;
       
    78     }
       
    79 }
       
    80 
       
    81 static void waitUntilDoneWatchdogFired(CFRunLoopTimerRef timer, void* info)
       
    82 {
       
    83     InjectedBundle::shared().layoutTestController()->waitToDumpWatchdogTimerFired();
       
    84 }
       
    85 
       
    86 void LayoutTestController::waitUntilDone()
       
    87 {
       
    88     m_waitToDump = true;
       
    89     if (!m_waitToDumpWatchdog) {
       
    90         m_waitToDumpWatchdog.adoptCF(CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + waitToDumpWatchdogInterval, 
       
    91                                                       0, 0, 0, waitUntilDoneWatchdogFired, NULL));
       
    92         CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_waitToDumpWatchdog.get(), kCFRunLoopCommonModes);
       
    93     }
       
    94 }
       
    95 
       
    96 void LayoutTestController::waitToDumpWatchdogTimerFired()
       
    97 {
       
    98     invalidateWaitToDumpWatchdog();
       
    99     const char* message = "FAIL: Timed out waiting for notifyDone to be called\n";
       
   100     InjectedBundle::shared().os() << message << "\n";
       
   101     InjectedBundle::shared().done();
       
   102 }
       
   103 
       
   104 void LayoutTestController::notifyDone()
       
   105 {
       
   106     if (m_waitToDump && !InjectedBundle::shared().page()->isLoading())
       
   107         InjectedBundle::shared().page()->dump();
       
   108     m_waitToDump = false;
       
   109 }
       
   110 
       
   111 unsigned LayoutTestController::numberOfActiveAnimations() const
       
   112 {
       
   113     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
       
   114     return WKBundleFrameGetNumberOfActiveAnimations(mainFrame);
       
   115 }
       
   116 
       
   117 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
       
   118 {
       
   119     RetainPtr<CFStringRef> idCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, elementId));
       
   120     WKRetainPtr<WKStringRef> idWK(AdoptWK, WKStringCreateWithCFString(idCF.get()));
       
   121     RetainPtr<CFStringRef> nameCF(AdoptCF, JSStringCopyCFString(kCFAllocatorDefault, animationName));
       
   122     WKRetainPtr<WKStringRef> nameWK(AdoptWK, WKStringCreateWithCFString(nameCF.get()));
       
   123 
       
   124     WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page());
       
   125     return WKBundleFramePauseAnimationOnElementWithId(mainFrame, nameWK.get(), idWK.get(), time);
       
   126 }
       
   127 
       
   128 bool LayoutTestController::shouldDumpDOMAsWebArchive() const
       
   129 {
       
   130     return false;
       
   131 }
       
   132 
       
   133 bool LayoutTestController::shouldDumpSourceAsWebArchive() const
       
   134 {
       
   135     return false;
       
   136 }
       
   137 
       
   138 bool LayoutTestController::shouldDumpMainFrameScrollPosition() const
       
   139 {
       
   140     return !shouldDumpAsText() && !shouldDumpDOMAsWebArchive() && !shouldDumpSourceAsWebArchive();
       
   141 }
       
   142 
       
   143 // Object Creation
       
   144 
       
   145 void LayoutTestController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception)
       
   146 {
       
   147     JSRetainPtr<JSStringRef> layoutTestContollerStr(Adopt, JSStringCreateWithUTF8CString("layoutTestController"));
       
   148     JSObjectSetProperty(context, windowObject, layoutTestContollerStr.get(), JSWrapper::wrap(context, this), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
       
   149 }
       
   150 
       
   151 } // namespace WTR