UserMessage.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "config.h"
  26. #include "Test.h"
  27. #include "PlatformUtilities.h"
  28. #include "PlatformWebView.h"
  29. #include <wtf/OwnPtr.h>
  30. #include <wtf/PassOwnPtr.h>
  31. namespace TestWebKitAPI {
  32. class WebKit2UserMessageRoundTripTest : public ::testing::Test {
  33. public:
  34. WebKit2UserMessageRoundTripTest()
  35. : didFinishLoad(false)
  36. , didReceiveMessage(false)
  37. {
  38. }
  39. WKRetainPtr<WKContextRef> context;
  40. OwnPtr<PlatformWebView> webView;
  41. WKRetainPtr<WKTypeRef> recievedBody;
  42. bool didFinishLoad;
  43. bool didReceiveMessage;
  44. static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
  45. {
  46. if (!WKStringIsEqualToUTF8CString(messageName, "RoundTripReturn"))
  47. return;
  48. ((WebKit2UserMessageRoundTripTest*)clientInfo)->recievedBody = messageBody;
  49. ((WebKit2UserMessageRoundTripTest*)clientInfo)->didReceiveMessage = true;
  50. }
  51. static void didFinishLoadForFrame(WKPageRef, WKFrameRef, WKTypeRef, const void* clientInfo)
  52. {
  53. ((WebKit2UserMessageRoundTripTest*)clientInfo)->didFinishLoad = true;
  54. }
  55. static void setInjectedBundleClient(WKContextRef context, const void* clientInfo)
  56. {
  57. WKContextInjectedBundleClient injectedBundleClient;
  58. memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
  59. injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion;
  60. injectedBundleClient.clientInfo = clientInfo;
  61. injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
  62. WKContextSetInjectedBundleClient(context, &injectedBundleClient);
  63. }
  64. static void setPageLoaderClient(WKPageRef page, const void* clientInfo)
  65. {
  66. WKPageLoaderClient loaderClient;
  67. memset(&loaderClient, 0, sizeof(loaderClient));
  68. loaderClient.version = kWKPageLoaderClientCurrentVersion;
  69. loaderClient.clientInfo = clientInfo;
  70. loaderClient.didFinishLoadForFrame = didFinishLoadForFrame;
  71. WKPageSetPageLoaderClient(page, &loaderClient);
  72. }
  73. virtual void SetUp()
  74. {
  75. context = adoptWK(Util::createContextForInjectedBundleTest("UserMessageTest"));
  76. setInjectedBundleClient(context.get(), this);
  77. webView = adoptPtr(new PlatformWebView(context.get()));
  78. setPageLoaderClient(webView->page(), this);
  79. didFinishLoad = false;
  80. didReceiveMessage = false;
  81. // Force the creation of the
  82. WKPageLoadURL(webView->page(), adoptWK(Util::createURLForResource("simple", "html")).get());
  83. Util::run(&didFinishLoad);
  84. }
  85. // Used to test sending a WKType round trip to the WebProcess and back.
  86. // Result is stored into the recievedBody member variable.
  87. void roundTrip(WKTypeRef object)
  88. {
  89. WKTypeID storedTypeID = WKGetTypeID(object);
  90. recievedBody.clear();
  91. didReceiveMessage = false;
  92. WKContextPostMessageToInjectedBundle(context.get(), Util::toWK("RoundTrip").get(), object);
  93. Util::run(&didReceiveMessage);
  94. EXPECT_NOT_NULL(recievedBody);
  95. EXPECT_EQ(storedTypeID, WKGetTypeID(recievedBody.get()));
  96. }
  97. };
  98. TEST_F(WebKit2UserMessageRoundTripTest, WKURLRequestRef)
  99. {
  100. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webkit.org/"));
  101. WKRetainPtr<WKURLRequestRef> request = adoptWK(WKURLRequestCreateWithWKURL(url.get()));
  102. roundTrip(request.get());
  103. WKTypeRef roundTrippedTypeRef = recievedBody.get();
  104. WKRetainPtr<WKURLRequestRef> roundTrippedRequest = static_cast<WKURLRequestRef>(roundTrippedTypeRef);
  105. WKRetainPtr<WKURLRef> roundTrippedURL = adoptWK(WKURLRequestCopyURL(roundTrippedRequest.get()));
  106. EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get()));
  107. }
  108. TEST_F(WebKit2UserMessageRoundTripTest, WKURL)
  109. {
  110. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("http://webkit.org/"));
  111. roundTrip(url.get());
  112. WKTypeRef roundTrippedTypeRef = recievedBody.get();
  113. WKRetainPtr<WKURLRef> roundTrippedURL = static_cast<WKURLRef>(roundTrippedTypeRef);
  114. EXPECT_TRUE(WKURLIsEqual(roundTrippedURL.get(), url.get()));
  115. }
  116. TEST_F(WebKit2UserMessageRoundTripTest, WKString)
  117. {
  118. WKRetainPtr<WKStringRef> string = adoptWK(WKStringCreateWithUTF8CString("An important string"));
  119. roundTrip(string.get());
  120. WKTypeRef roundTrippedTypeRef = recievedBody.get();
  121. WKRetainPtr<WKStringRef> roundTrippedString = static_cast<WKStringRef>(roundTrippedTypeRef);
  122. EXPECT_TRUE(WKStringIsEqual(roundTrippedString.get(), string.get()));
  123. }
  124. } // namespace TestWebKitAPI