WKConnection.cpp 4.5 KB

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