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
28#if WK_HAVE_C_SPI
29
30#include "Test.h"
31
32#include "PlatformUtilities.h"
33#include "PlatformWebView.h"
34
35namespace TestWebKitAPI {
36
37class WebKit2UserMessageRoundTripTest : public ::testing::Test {
38public:
39 WebKit2UserMessageRoundTripTest()
40 : didFinishLoad(false)
41 , didReceiveMessage(false)
42 {
43 }
44
45 WKRetainPtr<WKContextRef> context;
46 std::unique_ptr<PlatformWebView> webView;
47
48 WKRetainPtr<WKTypeRef> recievedBody;
49
50 bool didFinishLoad;
51 bool didReceiveMessage;
52
53 static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
54 {
55 if (!WKStringIsEqualToUTF8CString(messageName, "RoundTripReturn"))
56 return;
57
58 ((WebKit2UserMessageRoundTripTest*)clientInfo)->recievedBody = messageBody;
59 ((WebKit2UserMessageRoundTripTest*)clientInfo)->didReceiveMessage = true;
60 }
61
62 static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef, const void* clientInfo)
63 {
64 ((WebKit2UserMessageRoundTripTest*)clientInfo)->didFinishLoad = true;
65 }
66
67 static void setInjectedBundleClient(WKContextRef context, const void* clientInfo)
68 {
69 WKContextInjectedBundleClientV1 injectedBundleClient;
70 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
71
72 injectedBundleClient.base.version = 1;
73 injectedBundleClient.base.clientInfo = clientInfo;
74 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
75
76 WKContextSetInjectedBundleClient(context, &injectedBundleClient.base);
77 }
78
79 static void setPageLoaderClient(WKPageRef page, const void* clientInfo)
80 {
81 WKPageNavigationClientV3 loaderClient;
82 memset(&loaderClient, 0, sizeof(loaderClient));
83
84 loaderClient.base.version = 3;
85 loaderClient.base.clientInfo = clientInfo;
86 loaderClient.didFinishNavigation = didFinishNavigation;
87
88 WKPageSetPageNavigationClient(page, &loaderClient.base);
89 }
90
91 virtual void SetUp()
92 {
93 context = adoptWK(Util::createContextForInjectedBundleTest("UserMessageTest"));
94 setInjectedBundleClient(context.get(), this);
95
96 webView = std::make_unique<PlatformWebView>(context.get());
97 setPageLoaderClient(webView->page(), this);
98
99 didFinishLoad = false;
100 didReceiveMessage = false;
101
102 // Force the creation of the
103 WKPageLoadURL(webView->page(), adoptWK(Util::createURLForResource("simple", "html")).get());
104 Util::run(&didFinishLoad);
105
106 }
107
108 // Used to test sending a WKType round trip to the WebProcess and back.
109 // Result is stored into the recievedBody member variable.
110 void roundTrip(WKTypeRef object)
111 {
112 WKTypeID storedTypeID = WKGetTypeID(object);
113
114 recievedBody.clear();
115 didReceiveMessage = false;
116 WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("RoundTrip").get(), object);
117 Util::run(&didReceiveMessage);
118
119 EXPECT_NOT_NULL(recievedBody);
120 EXPECT_EQ(storedTypeID, WKGetTypeID(recievedBody.get()));
121 }
122};
123
124
125TEST_F(WebKit2UserMessageRoundTripTest, WKURLRequestRef)
126{
127 WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webkit.org/"));
128 WKRetainPtr<WKURLRequestRef> request = adoptWK(WKURLRequestCreateWithWKURL(url.get()));
129
130 roundTrip(request.get());
131 WKTypeRef roundTrippedTypeRef = recievedBody.get();
132
133 WKRetainPtr<WKURLRequestRef> roundTrippedRequest = static_cast<WKURLRequestRef>(roundTrippedTypeRef);
134 WKRetainPtr<WKURLRef> roundTrippedURL = adoptWK(WKURLRequestCopyURL(roundTrippedRequest.get()));
135 EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get()));
136}
137
138TEST_F(WebKit2UserMessageRoundTripTest, WKURL)
139{
140 WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webkit.org/"));
141
142 roundTrip(url.get());
143 WKTypeRef roundTrippedTypeRef = recievedBody.get();
144
145 WKRetainPtr<WKURLRef> roundTrippedURL = static_cast<WKURLRef>(roundTrippedTypeRef);
146 EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get()));
147}
148
149TEST_F(WebKit2UserMessageRoundTripTest, WKString)
150{
151 WKRetainPtr<WKStringRef> string = adoptWK(WKStringCreateWithUTF8CString("An important string"));
152
153 roundTrip(string.get());
154 WKTypeRef roundTrippedTypeRef = recievedBody.get();
155
156 WKRetainPtr<WKStringRef> roundTrippedString = static_cast<WKStringRef>(roundTrippedTypeRef);
157 EXPECT_TRUE(WKStringIsEqual(roundTrippedString.get(), string.get()));
158}
159
160} // namespace TestWebKitAPI
161
162#endif
163