OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 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 "Test.h" | |
28 | |
29 #include "PlatformUtilities.h" | |
30 #include "PlatformWebView.h" | |
31 #include <wtf/OwnPtr.h> | |
32 #include <wtf/PassOwnPtr.h> | |
33 | |
34 namespace TestWebKitAPI { | |
35 | |
36 class WebKit2UserMessageRoundTripTest : public ::testing::Test { | |
37 public: | |
38 WebKit2UserMessageRoundTripTest() | |
39 : didFinishLoad(false) | |
40 , didReceiveMessage(false) | |
41 { | |
42 } | |
43 | |
44 WKRetainPtr<WKContextRef> context; | |
45 OwnPtr<PlatformWebView> webView; | |
46 | |
47 WKRetainPtr<WKTypeRef> recievedBody; | |
48 | |
49 bool didFinishLoad; | |
50 bool didReceiveMessage; | |
51 | |
52 static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef me
ssageName, WKTypeRef messageBody, const void* clientInfo) | |
53 { | |
54 if (!WKStringIsEqualToUTF8CString(messageName, "RoundTripReturn")) | |
55 return; | |
56 | |
57 ((WebKit2UserMessageRoundTripTest*)clientInfo)->recievedBody = messageBo
dy; | |
58 ((WebKit2UserMessageRoundTripTest*)clientInfo)->didReceiveMessage = true
; | |
59 } | |
60 | |
61 static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const vo
id* clientInfo) | |
62 { | |
63 ((WebKit2UserMessageRoundTripTest*)clientInfo)->didFinishLoad = true; | |
64 } | |
65 | |
66 static void setInjectedBundleClient(WKContextRef context, const void* client
Info) | |
67 { | |
68 WKContextInjectedBundleClient injectedBundleClient; | |
69 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient)); | |
70 injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVers
ion; | |
71 injectedBundleClient.clientInfo = clientInfo; | |
72 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMes
sageFromInjectedBundle; | |
73 | |
74 WKContextSetInjectedBundleClient(context, &injectedBundleClient); | |
75 } | |
76 | |
77 static void setPageLoaderClient(WKPageRef page, const void* clientInfo) | |
78 { | |
79 WKPageLoaderClient loaderClient; | |
80 memset(&loaderClient, 0, sizeof(loaderClient)); | |
81 loaderClient.version = kWKPageLoaderClientCurrentVersion; | |
82 loaderClient.clientInfo = clientInfo; | |
83 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame; | |
84 | |
85 WKPageSetPageLoaderClient(page, &loaderClient); | |
86 } | |
87 | |
88 virtual void SetUp() | |
89 { | |
90 context = adoptWK(Util::createContextForInjectedBundleTest("UserMessageT
est")); | |
91 setInjectedBundleClient(context.get(), this); | |
92 | |
93 webView = adoptPtr(new PlatformWebView(context.get())); | |
94 setPageLoaderClient(webView->page(), this); | |
95 | |
96 didFinishLoad = false; | |
97 didReceiveMessage = false; | |
98 | |
99 // Force the creation of the | |
100 WKPageLoadURL(webView->page(), adoptWK(Util::createURLForResource("simpl
e", "html")).get()); | |
101 Util::run(&didFinishLoad); | |
102 | |
103 } | |
104 | |
105 // Used to test sending a WKType round trip to the WebProcess and back. | |
106 // Result is stored into the recievedBody member variable. | |
107 void roundTrip(WKTypeRef object) | |
108 { | |
109 WKTypeID storedTypeID = WKGetTypeID(object); | |
110 | |
111 recievedBody.clear(); | |
112 didReceiveMessage = false; | |
113 WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("RoundTri
p").get(), object); | |
114 Util::run(&didReceiveMessage); | |
115 | |
116 EXPECT_NOT_NULL(recievedBody); | |
117 EXPECT_EQ(storedTypeID, WKGetTypeID(recievedBody.get())); | |
118 } | |
119 }; | |
120 | |
121 | |
122 TEST_F(WebKit2UserMessageRoundTripTest, WKURLRequestRef) | |
123 { | |
124 WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webki
t.org/")); | |
125 WKRetainPtr<WKURLRequestRef> request = adoptWK(WKURLRequestCreateWithWKURL(u
rl.get())); | |
126 | |
127 roundTrip(request.get()); | |
128 WKTypeRef roundTrippedTypeRef = recievedBody.get(); | |
129 | |
130 WKRetainPtr<WKURLRequestRef> roundTrippedRequest = static_cast<WKURLRequestR
ef>(roundTrippedTypeRef); | |
131 WKRetainPtr<WKURLRef> roundTrippedURL = adoptWK(WKURLRequestCopyURL(roundTri
ppedRequest.get())); | |
132 EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get())); | |
133 } | |
134 | |
135 TEST_F(WebKit2UserMessageRoundTripTest, WKURL) | |
136 { | |
137 WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webki
t.org/")); | |
138 | |
139 roundTrip(url.get()); | |
140 WKTypeRef roundTrippedTypeRef = recievedBody.get(); | |
141 | |
142 WKRetainPtr<WKURLRef> roundTrippedURL = static_cast<WKURLRef>(roundTrippedTy
peRef); | |
143 EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get())); | |
144 } | |
145 | |
146 TEST_F(WebKit2UserMessageRoundTripTest, WKString) | |
147 { | |
148 WKRetainPtr<WKStringRef> string = adoptWK(WKStringCreateWithUTF8CString("An
important string")); | |
149 | |
150 roundTrip(string.get()); | |
151 WKTypeRef roundTrippedTypeRef = recievedBody.get(); | |
152 | |
153 WKRetainPtr<WKStringRef> roundTrippedString = static_cast<WKStringRef>(round
TrippedTypeRef); | |
154 EXPECT_TRUE(WKStringIsEqual(roundTrippedString.get(), string.get())); | |
155 } | |
156 | |
157 } // namespace TestWebKitAPI | |
OLD | NEW |