2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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>
34 namespace TestWebKitAPI {
36 static bool didFinishLoad;
37 static bool didReceiveMessage;
39 static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef body, const void*)
41 didReceiveMessage = true;
43 EXPECT_WK_STREQ("DidGetWebArchive", messageName);
45 EXPECT_EQ(WKDataGetTypeID(), WKGetTypeID(body));
46 WKDataRef receivedData = static_cast<WKDataRef>(body);
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 CFPropertyListFormat format = kCFPropertyListXMLFormat_v1_0 | kCFPropertyListBinaryFormat_v1_0;
53 RetainPtr<CFPropertyListRef> propertyList(AdoptCF, CFPropertyListCreateWithData(0, data.get(), kCFPropertyListImmutable, &format, 0));
54 EXPECT_TRUE(propertyList);
56 // It should be a dictionary.
57 EXPECT_EQ(CFDictionaryGetTypeID(), CFGetTypeID(propertyList.get()));
58 CFDictionaryRef dictionary = (CFDictionaryRef)propertyList.get();
60 // It should have a main resource.
61 CFTypeRef mainResource = CFDictionaryGetValue(dictionary, CFSTR("WebMainResource"));
62 EXPECT_TRUE(mainResource);
63 EXPECT_EQ(CFDictionaryGetTypeID(), CFGetTypeID(mainResource));
64 CFDictionaryRef mainResourceDictionary = (CFDictionaryRef)mainResource;
66 // Main resource should have a non-empty url and mime type.
67 CFTypeRef url = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResourceURL"));
69 EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(url));
70 EXPECT_NE(CFStringGetLength((CFStringRef)url), 0);
72 CFTypeRef mimeType = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResourceMIMEType"));
73 EXPECT_TRUE(mimeType);
74 EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(mimeType));
75 EXPECT_NE(CFStringGetLength((CFStringRef)mimeType), 0);
77 // Main resource dictionary should have a "WebResourceData" key.
78 CFTypeRef resourceData = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResourceData"));
79 EXPECT_TRUE(resourceData);
80 EXPECT_EQ(CFDataGetTypeID(), CFGetTypeID(resourceData));
82 RetainPtr<CFStringRef> stringData(AdoptCF, CFStringCreateFromExternalRepresentation(0, (CFDataRef)resourceData, kCFStringEncodingUTF8));
83 EXPECT_TRUE(stringData);
85 // It should contain the string "Simple HTML file." in it.
86 bool foundString = CFStringFind(stringData.get(), CFSTR("Simple HTML file."), 0).location != kCFNotFound;
87 EXPECT_TRUE(foundString);
90 static void setInjectedBundleClient(WKContextRef context)
92 WKContextInjectedBundleClient injectedBundleClient;
93 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
94 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
96 WKContextSetInjectedBundleClient(context, &injectedBundleClient);
99 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef, WKTypeRef, const void*)
101 didFinishLoad = true;
104 TEST(WebKit2, WebArchive)
106 WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBundleTest("WebArchiveTest"));
107 setInjectedBundleClient(context.get());
109 PlatformWebView webView(context.get());
111 WKPageLoaderClient loaderClient;
112 memset(&loaderClient, 0, sizeof(loaderClient));
114 loaderClient.version = kWKPageLoaderClientCurrentVersion;
115 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
116 WKPageSetPageLoaderClient(webView.page(), &loaderClient);
118 WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple", "html")).get());
120 // Wait till the load finishes before getting the web archive.
121 Util::run(&didFinishLoad);
122 WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("GetWebArchive").get(), webView.page());
124 // Wait till we have received the web archive from the injected bundle.
125 Util::run(&didReceiveMessage);
128 } // namespace TestWebKitAPI