WebKitTools/WebKitTestRunner/TestInvocation.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 "TestInvocation.h"
       
    27 
       
    28 #include "PlatformWebView.h"
       
    29 #include "TestController.h"
       
    30 #include <WebKit2/WKContextPrivate.h>
       
    31 #include <WebKit2/WKRetainPtr.h>
       
    32 #include <WebKit2/WKStringCF.h>
       
    33 #include <WebKit2/WKURLCF.h>
       
    34 #include <wtf/PassOwnPtr.h>
       
    35 #include <wtf/RetainPtr.h>
       
    36 #include <wtf/Vector.h>
       
    37 
       
    38 using namespace WebKit;
       
    39 
       
    40 namespace WTR {
       
    41 
       
    42 static WKURLRef createWKURL(const char* pathOrURL)
       
    43 {
       
    44     RetainPtr<CFStringRef> pathOrURLCFString(AdoptCF, CFStringCreateWithCString(0, pathOrURL, kCFStringEncodingUTF8));
       
    45     RetainPtr<CFURLRef> cfURL;
       
    46     if (CFStringHasPrefix(pathOrURLCFString.get(), CFSTR("http://")) || CFStringHasPrefix(pathOrURLCFString.get(), CFSTR("https://")))
       
    47         cfURL.adoptCF(CFURLCreateWithString(0, pathOrURLCFString.get(), 0));
       
    48     else
       
    49 #if defined(WIN32) || defined(_WIN32)
       
    50         cfURL.adoptCF(CFURLCreateWithFileSystemPath(0, pathOrURLCFString.get(), kCFURLWindowsPathStyle, false));
       
    51 #else
       
    52         cfURL.adoptCF(CFURLCreateWithFileSystemPath(0, pathOrURLCFString.get(), kCFURLPOSIXPathStyle, false));
       
    53 #endif
       
    54     return WKURLCreateWithCFURL(cfURL.get());
       
    55 }
       
    56 
       
    57 static PassOwnPtr<Vector<char> > WKStringToUTF8(WKStringRef wkStringRef)
       
    58 {
       
    59     RetainPtr<CFStringRef> cfString(AdoptCF, WKStringCopyCFString(0, wkStringRef));
       
    60     CFIndex bufferLength = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfString.get()), kCFStringEncodingUTF8) + 1;
       
    61     OwnPtr<Vector<char> > buffer(new Vector<char>(bufferLength));
       
    62     if (!CFStringGetCString(cfString.get(), buffer->data(), bufferLength, kCFStringEncodingUTF8)) {
       
    63         buffer->shrink(1);
       
    64         (*buffer)[0] = 0;
       
    65     } else
       
    66         buffer->shrink(strlen(buffer->data()) + 1);
       
    67     return buffer.release();
       
    68 }
       
    69 
       
    70 TestInvocation::TestInvocation(const char* pathOrURL)
       
    71     : m_url(AdoptWK, createWKURL(pathOrURL))
       
    72     , m_pathOrURL(fastStrDup(pathOrURL))
       
    73     , m_gotInitialResponse(false)
       
    74     , m_gotFinalMessage(false)
       
    75     , m_error(false)
       
    76 {
       
    77 }
       
    78 
       
    79 TestInvocation::~TestInvocation()
       
    80 {
       
    81     fastFree(m_pathOrURL);
       
    82 }
       
    83 
       
    84 static const unsigned w3cSVGWidth = 480;
       
    85 static const unsigned w3cSVGHeight = 360;
       
    86 static const unsigned normalWidth = 800;
       
    87 static const unsigned normalHeight = 600;
       
    88 
       
    89 static void sizeWebViewForCurrentTest(char* pathOrURL)
       
    90 {
       
    91     bool isSVGW3CTest = strstr(pathOrURL, "svg/W3C-SVG-1.1");
       
    92 
       
    93     if (isSVGW3CTest)
       
    94         TestController::shared().mainWebView()->resizeTo(w3cSVGWidth, w3cSVGHeight);
       
    95     else
       
    96         TestController::shared().mainWebView()->resizeTo(normalWidth, normalHeight);
       
    97 }
       
    98 
       
    99 void TestInvocation::resetPreferencesToConsistentValues()
       
   100 {
       
   101     WKPreferencesRef preferences = WKContextGetPreferences(TestController::shared().context());
       
   102     
       
   103     WKPreferencesSetOfflineWebApplicationCacheEnabled(preferences, true);
       
   104 }
       
   105 
       
   106 void TestInvocation::invoke()
       
   107 {
       
   108     sizeWebViewForCurrentTest(m_pathOrURL);
       
   109     resetPreferencesToConsistentValues();
       
   110 
       
   111     WKRetainPtr<WKStringRef> message(AdoptWK, WKStringCreateWithCFString(CFSTR("BeginTest")));
       
   112     WKContextPostMessageToInjectedBundle(TestController::shared().context(), message.get());
       
   113 
       
   114     runUntil(m_gotInitialResponse);
       
   115     if (m_error) {
       
   116         dump("FAIL\n");
       
   117         return;
       
   118     }
       
   119 
       
   120     WKPageLoadURL(TestController::shared().mainWebView()->page(), m_url.get());
       
   121 
       
   122     runUntil(m_gotFinalMessage);
       
   123     if (m_error) {
       
   124         dump("FAIL\n");
       
   125         return;
       
   126     }
       
   127 }
       
   128 
       
   129 void TestInvocation::dump(const char* stringToDump)
       
   130 {
       
   131     printf("Content-Type: text/plain\n");
       
   132     printf("%s", stringToDump);
       
   133 
       
   134     fputs("#EOF\n", stdout);
       
   135     fputs("#EOF\n", stdout);
       
   136     fputs("#EOF\n", stderr);
       
   137 
       
   138     fflush(stdout);
       
   139     fflush(stderr);
       
   140 }
       
   141 
       
   142 void TestInvocation::didRecieveMessageFromInjectedBundle(WKStringRef message)
       
   143 {
       
   144     RetainPtr<CFStringRef> cfMessage(AdoptCF, WKStringCopyCFString(0, message));
       
   145     
       
   146     if (CFEqual(cfMessage.get(), CFSTR("Error"))) {
       
   147         // Set all states to true to stop spinning the runloop.
       
   148         m_gotInitialResponse = true;
       
   149         m_gotFinalMessage = true;
       
   150         m_error = true;
       
   151         return;
       
   152     }
       
   153 
       
   154     if (CFEqual(cfMessage.get(), CFSTR("BeginTestAck"))) {
       
   155         m_gotInitialResponse = true;
       
   156         return;
       
   157     }
       
   158 
       
   159     OwnPtr<Vector<char> > utf8Message = WKStringToUTF8(message);
       
   160 
       
   161     dump(utf8Message->data());
       
   162     m_gotFinalMessage = true;
       
   163 }
       
   164 
       
   165 } // namespace WTR