| 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 "Test.h" | |
| 30 | |
| 31 namespace TestWebKitAPI { | |
| 32 | |
| 33 // State for part 1 - setting up the connection. | |
| 34 static bool connectionEstablished; | |
| 35 static WKConnectionRef connectionToBundle; | |
| 36 | |
| 37 // State for part 2 - send/recieving messages. | |
| 38 static bool messageReceived; | |
| 39 | |
| 40 // State for part 3 - tearing down the connection. | |
| 41 static bool connectionTornDown; | |
| 42 | |
| 43 | |
| 44 /* WKContextConnectionClient */ | |
| 45 static void didCreateConnection(WKContextRef context, WKConnectionRef connection
, const void* clientInfo) | |
| 46 { | |
| 47 connectionEstablished = true; | |
| 48 | |
| 49 // Store off the conneciton to use. | |
| 50 connectionToBundle = (WKConnectionRef)WKRetain(connection); | |
| 51 } | |
| 52 | |
| 53 /* WKConnectionClient */ | |
| 54 static void connectionDidReceiveMessage(WKConnectionRef connection, WKStringRef
messageName, WKTypeRef messageBody, const void *clientInfo) | |
| 55 { | |
| 56 // We only expect to get the "Pong" message. | |
| 57 EXPECT_WK_STREQ(messageName, "PongMessageName"); | |
| 58 EXPECT_WK_STREQ((WKStringRef)messageBody, "PongMessageBody"); | |
| 59 | |
| 60 messageReceived = true; | |
| 61 } | |
| 62 | |
| 63 static void connectionDidClose(WKConnectionRef connection, const void* clientInf
o) | |
| 64 { | |
| 65 connectionTornDown = true; | |
| 66 } | |
| 67 | |
| 68 TEST(WebKit2, WKConnectionTest) | |
| 69 { | |
| 70 WKRetainPtr<WKContextRef> context(AdoptWK, Util::createContextForInjectedBun
dleTest("WKConnectionTest")); | |
| 71 | |
| 72 // Set up the context's connection client so that we can access the connecti
on when | |
| 73 // it is created. | |
| 74 WKContextConnectionClient contextConnectionClient; | |
| 75 memset(&contextConnectionClient, 0, sizeof(contextConnectionClient)); | |
| 76 contextConnectionClient.version = kWKContextConnectionClientCurrentVersion; | |
| 77 contextConnectionClient.clientInfo = 0; | |
| 78 contextConnectionClient.didCreateConnection = didCreateConnection; | |
| 79 WKContextSetConnectionClient(context.get(), &contextConnectionClient); | |
| 80 | |
| 81 // Load a simple page to start the WebProcess and establish a connection. | |
| 82 PlatformWebView webView(context.get()); | |
| 83 WKRetainPtr<WKURLRef> url(AdoptWK, Util::createURLForResource("simple", "htm
l")); | |
| 84 WKPageLoadURL(webView.page(), url.get()); | |
| 85 | |
| 86 // Wait until the connection is established. | |
| 87 Util::run(&connectionEstablished); | |
| 88 ASSERT_NOT_NULL(connectionToBundle); | |
| 89 | |
| 90 // Setup a client on the connection so we can listen for messages and | |
| 91 // tear down notifications. | |
| 92 WKConnectionClient connectionClient; | |
| 93 memset(&connectionClient, 0, sizeof(connectionClient)); | |
| 94 connectionClient.version = WKConnectionClientCurrentVersion; | |
| 95 connectionClient.clientInfo = 0; | |
| 96 connectionClient.didReceiveMessage = connectionDidReceiveMessage; | |
| 97 connectionClient.didClose = connectionDidClose; | |
| 98 WKConnectionSetConnectionClient(connectionToBundle, &connectionClient); | |
| 99 | |
| 100 // Post a simple message to the bundle via the connection. | |
| 101 WKConnectionPostMessage(connectionToBundle, Util::toWK("PingMessageName").ge
t(), Util::toWK("PingMessageBody").get()); | |
| 102 | |
| 103 // Wait for the reply. | |
| 104 Util::run(&messageReceived); | |
| 105 | |
| 106 // Terminate the page to force the connection closed. | |
| 107 WKPageTerminate(webView.page()); | |
| 108 | |
| 109 // Wait for the connection to close. | |
| 110 Util::run(&connectionTornDown); | |
| 111 | |
| 112 // This release is to balance the retain in didCreateConnection. | |
| 113 WKRelease(connectionToBundle); | |
| 114 } | |
| 115 | |
| 116 } // namespace TestWebKitAPI | |
| OLD | NEW |