initial import
[vuplus_webkit] / Tools / TestWebKitAPI / Tests / WebKit2 / WebArchive.cpp
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 messageName, 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     CFPropertyListFormat format = kCFPropertyListXMLFormat_v1_0 | kCFPropertyListBinaryFormat_v1_0;
53     RetainPtr<CFPropertyListRef> propertyList(AdoptCF, CFPropertyListCreateWithData(0, data.get(), kCFPropertyListImmutable, &format, 0));
54     EXPECT_TRUE(propertyList);
55     
56     // It should be a dictionary.
57     EXPECT_EQ(CFDictionaryGetTypeID(), CFGetTypeID(propertyList.get()));
58     CFDictionaryRef dictionary = (CFDictionaryRef)propertyList.get();
59     
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;
65     
66     // Main resource should have a non-empty url and mime type.
67     CFTypeRef url = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResourceURL"));
68     EXPECT_TRUE(url);
69     EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(url));
70     EXPECT_NE(CFStringGetLength((CFStringRef)url), 0);
71     
72     CFTypeRef mimeType = CFDictionaryGetValue(mainResourceDictionary, CFSTR("WebResourceMIMEType"));
73     EXPECT_TRUE(mimeType);
74     EXPECT_EQ(CFStringGetTypeID(), CFGetTypeID(mimeType));
75     EXPECT_NE(CFStringGetLength((CFStringRef)mimeType), 0);
76     
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));
81     
82     RetainPtr<CFStringRef> stringData(AdoptCF, CFStringCreateFromExternalRepresentation(0, (CFDataRef)resourceData, kCFStringEncodingUTF8));
83     EXPECT_TRUE(stringData);
84     
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);
88 }
89
90 static void setInjectedBundleClient(WKContextRef context)
91 {
92     WKContextInjectedBundleClient injectedBundleClient;
93     memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
94     injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
95
96     WKContextSetInjectedBundleClient(context, &injectedBundleClient);
97 }
98
99 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef, WKTypeRef, const void*)
100 {
101     didFinishLoad = true;
102 }
103
104 TEST(WebKit2, WebArchive)
105 {
106     WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBundleTest("WebArchiveTest"));
107     setInjectedBundleClient(context.get());
108
109     PlatformWebView webView(context.get());
110
111     WKPageLoaderClient loaderClient;
112     memset(&loaderClient, 0, sizeof(loaderClient));
113     
114     loaderClient.version = kWKPageLoaderClientCurrentVersion;
115     loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
116     WKPageSetPageLoaderClient(webView.page(), &loaderClient);
117
118     WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple", "html")).get());
119
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());
123
124     // Wait till we have received the web archive from the injected bundle.
125     Util::run(&didReceiveMessage);
126 }
127
128 } // namespace TestWebKitAPI