| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2011 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 "config.h" | |
| 27 #include "PlatformUtilities.h" | |
| 28 #include "PlatformWebView.h" | |
| 29 #include <CoreFoundation/CoreFoundation.h> | |
| 30 #include <WebKit2/WKURLCF.h> | |
| 31 #include <WebKit2/WKContextPrivate.h> | |
| 32 #include <wtf/RetainPtr.h> | |
| 33 | |
| 34 namespace TestWebKitAPI { | |
| 35 | |
| 36 static bool didFinishLoad; | |
| 37 static bool didReceiveMessage; | |
| 38 | |
| 39 static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messag
eName, WKTypeRef body, const void*) | |
| 40 { | |
| 41 didReceiveMessage = true; | |
| 42 | |
| 43 EXPECT_WK_STREQ("DidGetWebArchive", messageName); | |
| 44 EXPECT_TRUE(body); | |
| 45 EXPECT_EQ(WKDataGetTypeID(), WKGetTypeID(body)); | |
| 46 WKDataRef receivedData = static_cast<WKDataRef>(body); | |
| 47 | |
| 48 // Do basic sanity checks on the returned webarchive. We have more thorough
checks in LayoutTests. | |
| 49 size_t size = WKDataGetSize(receivedData); | |
| 50 const unsigned char* bytes = WKDataGetBytes(receivedData); | |
| 51 RetainPtr<CFDataRef> data(AdoptCF, CFDataCreate(0, bytes, size)); | |
| 52 RetainPtr<CFPropertyListRef> propertyList(AdoptCF, CFPropertyListCreateWithD
ata(0, data.get(), kCFPropertyListImmutable, 0, 0)); | |
| 53 EXPECT_TRUE(propertyList); | |
| 54 | |
| 55 // It should be a dictionary. | |
| 56 EXPECT_EQ(CFDictionaryGetTypeID(), CFGetTypeID(propertyList.get())); | |
| 57 CFDictionaryRef dictionary = (CFDictionaryRef)propertyList.get(); | |
| 58 | |
| 59 // It should have a main resource. | |
| 60 CFTypeRef mainResource = CFDictionaryGetValue(dictionary, CFSTR("WebMainReso
urce")); | |
| 61 EXPECT_TRUE(mainResource); | |
| 62 EXPECT_EQ(CFDictionaryGetTypeID(), CFGetTypeID(mainResource)); | |
| 63 CFDictionaryRef mainResourceDictionary = (CFDictionaryRef)mainResource; | |
| 64 | |
| 65 // Main resource should have a non-empty url and mime type. | |
| 66 CFTypeRef url = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResou
rceURL")); | |
| 67 EXPECT_TRUE(url); | |
| 68 EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(url)); | |
| 69 EXPECT_NE(CFStringGetLength((CFStringRef)url), 0); | |
| 70 | |
| 71 CFTypeRef mimeType = CFDictionaryGetValue(mainResourceDictionary, CFSTR("Web
ResourceMIMEType")); | |
| 72 EXPECT_TRUE(mimeType); | |
| 73 EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(mimeType)); | |
| 74 EXPECT_NE(CFStringGetLength((CFStringRef)mimeType), 0); | |
| 75 | |
| 76 // Main resource dictionary should have a "WebResourceData" key. | |
| 77 CFTypeRef resourceData = CFDictionaryGetValue(mainResourceDictionary, CFSTR(
"WebResourceData")); | |
| 78 EXPECT_TRUE(resourceData); | |
| 79 EXPECT_EQ(CFDataGetTypeID(), CFGetTypeID(resourceData)); | |
| 80 | |
| 81 RetainPtr<CFStringRef> stringData(AdoptCF, CFStringCreateFromExternalReprese
ntation(0, (CFDataRef)resourceData, kCFStringEncodingUTF8)); | |
| 82 EXPECT_TRUE(stringData); | |
| 83 | |
| 84 // It should contain the string "Simple HTML file." in it. | |
| 85 bool foundString = CFStringFind(stringData.get(), CFSTR("Simple HTML file.")
, 0).location != kCFNotFound; | |
| 86 EXPECT_TRUE(foundString); | |
| 87 } | |
| 88 | |
| 89 static void setInjectedBundleClient(WKContextRef context) | |
| 90 { | |
| 91 WKContextInjectedBundleClient injectedBundleClient; | |
| 92 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient)); | |
| 93 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessage
FromInjectedBundle; | |
| 94 | |
| 95 WKContextSetInjectedBundleClient(context, &injectedBundleClient); | |
| 96 } | |
| 97 | |
| 98 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef, WKTypeRef, const v
oid*) | |
| 99 { | |
| 100 didFinishLoad = true; | |
| 101 } | |
| 102 | |
| 103 TEST(WebKit2, WebArchive) | |
| 104 { | |
| 105 WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBu
ndleTest("WebArchiveTest")); | |
| 106 setInjectedBundleClient(context.get()); | |
| 107 | |
| 108 PlatformWebView webView(context.get()); | |
| 109 | |
| 110 WKPageLoaderClient loaderClient; | |
| 111 memset(&loaderClient, 0, sizeof(loaderClient)); | |
| 112 | |
| 113 loaderClient.version = kWKPageLoaderClientCurrentVersion; | |
| 114 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame; | |
| 115 WKPageSetPageLoaderClient(webView.page(), &loaderClient); | |
| 116 | |
| 117 WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple", "
html")).get()); | |
| 118 | |
| 119 // Wait till the load finishes before getting the web archive. | |
| 120 Util::run(&didFinishLoad); | |
| 121 WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("GetWebArchiv
e").get(), webView.page()); | |
| 122 | |
| 123 // Wait till we have received the web archive from the injected bundle. | |
| 124 Util::run(&didReceiveMessage); | |
| 125 } | |
| 126 | |
| 127 } // namespace TestWebKitAPI | |
| OLD | NEW |