WillLoad.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2013 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 WebKit2WillLoadTest : public ::testing::Test {
  33. public:
  34. WebKit2WillLoadTest()
  35. : didReceiveMessage(false)
  36. {
  37. }
  38. WKRetainPtr<WKContextRef> context;
  39. OwnPtr<PlatformWebView> webView;
  40. WKRetainPtr<WKStringRef> messageName;
  41. WKRetainPtr<WKTypeRef> messageBody;
  42. bool didReceiveMessage;
  43. static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
  44. {
  45. ((WebKit2WillLoadTest*)clientInfo)->messageName = messageName;
  46. ((WebKit2WillLoadTest*)clientInfo)->messageBody = messageBody;
  47. ((WebKit2WillLoadTest*)clientInfo)->didReceiveMessage = true;
  48. }
  49. static void setInjectedBundleClient(WKContextRef context, const void* clientInfo)
  50. {
  51. WKContextInjectedBundleClient injectedBundleClient;
  52. memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
  53. injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion;
  54. injectedBundleClient.clientInfo = clientInfo;
  55. injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
  56. WKContextSetInjectedBundleClient(context, &injectedBundleClient);
  57. }
  58. virtual void SetUp()
  59. {
  60. context = adoptWK(Util::createContextForInjectedBundleTest("WillLoadTest"));
  61. setInjectedBundleClient(context.get(), this);
  62. webView = adoptPtr(new PlatformWebView(context.get()));
  63. didReceiveMessage = false;
  64. }
  65. void testWillLoadURLRequestReturnValues(WKURLRef expectedURL, WKStringRef expectedUserDataString)
  66. {
  67. didReceiveMessage = false;
  68. Util::run(&didReceiveMessage);
  69. EXPECT_WK_STREQ("WillLoadURLRequestReturn", messageName.get());
  70. EXPECT_EQ(WKDictionaryGetTypeID(), WKGetTypeID(messageBody.get()));
  71. WKDictionaryRef dictionary = static_cast<WKDictionaryRef>(messageBody.get());
  72. if (expectedUserDataString) {
  73. WKStringRef userDataReturnValue = static_cast<WKStringRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("UserDataReturn").get()));
  74. EXPECT_WK_STREQ(expectedUserDataString, userDataReturnValue);
  75. } else
  76. EXPECT_NULL(WKDictionaryGetItemForKey(dictionary, Util::toWK("UserDataReturn").get()));
  77. WKURLRequestRef urlRequestReturnValue = static_cast<WKURLRequestRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("URLRequestReturn").get()));
  78. WKRetainPtr<WKURLRef> urlReturnValue = adoptWK(WKURLRequestCopyURL(urlRequestReturnValue));
  79. EXPECT_TRUE(WKURLIsEqual(expectedURL, urlReturnValue.get()));
  80. }
  81. void testWillLoadDataRequestReturnValues(WKURLRef expectedURL, WKStringRef expectedMIMEType, WKStringRef expectedEncodingName, WKURLRef expectedUnreachableURL, WKStringRef expectedUserDataString)
  82. {
  83. didReceiveMessage = false;
  84. Util::run(&didReceiveMessage);
  85. EXPECT_WK_STREQ("WillLoadDataRequestReturn", messageName.get());
  86. EXPECT_EQ(WKDictionaryGetTypeID(), WKGetTypeID(messageBody.get()));
  87. WKDictionaryRef dictionary = static_cast<WKDictionaryRef>(messageBody.get());
  88. if (expectedUserDataString) {
  89. WKStringRef userDataReturnValue = static_cast<WKStringRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("UserDataReturn").get()));
  90. EXPECT_WK_STREQ(expectedUserDataString, userDataReturnValue);
  91. } else
  92. EXPECT_NULL(WKDictionaryGetItemForKey(dictionary, Util::toWK("UserDataReturn").get()));
  93. WKURLRequestRef urlRequestReturnValue = static_cast<WKURLRequestRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("URLRequestReturn").get()));
  94. WKRetainPtr<WKURLRef> urlReturnValue = adoptWK(WKURLRequestCopyURL(urlRequestReturnValue));
  95. EXPECT_TRUE(WKURLIsEqual(expectedURL, urlReturnValue.get()));
  96. WKStringRef MIMEType = static_cast<WKStringRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("MIMETypeReturn").get()));
  97. EXPECT_WK_STREQ(expectedMIMEType, MIMEType);
  98. WKStringRef encodingName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("EncodingNameReturn").get()));
  99. EXPECT_WK_STREQ(expectedEncodingName, encodingName);
  100. if (expectedUnreachableURL) {
  101. WKURLRef unreachableURL = static_cast<WKURLRef>(WKDictionaryGetItemForKey(dictionary, Util::toWK("UnreachableURLReturn").get()));
  102. EXPECT_TRUE(WKURLIsEqual(expectedUnreachableURL, unreachableURL));
  103. } else
  104. EXPECT_NULL(WKDictionaryGetItemForKey(dictionary, Util::toWK("UnreachableURLReturn").get()));
  105. }
  106. };
  107. // URL Request tests
  108. TEST_F(WebKit2WillLoadTest, WKPageLoadURLWithUserData)
  109. {
  110. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  111. WKRetainPtr<WKStringRef> userData = Util::toWK("WKPageLoadURLWithUserData UserData");
  112. WKPageLoadURLWithUserData(webView->page(), url.get(), userData.get());
  113. testWillLoadURLRequestReturnValues(url.get(), userData.get());
  114. }
  115. TEST_F(WebKit2WillLoadTest, WKPageLoadURL)
  116. {
  117. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  118. WKPageLoadURL(webView->page(), url.get());
  119. testWillLoadURLRequestReturnValues(url.get(), 0);
  120. }
  121. TEST_F(WebKit2WillLoadTest, WKPageLoadURLRequestWithUserData)
  122. {
  123. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  124. WKRetainPtr<WKURLRequestRef> urlRequest = adoptWK(WKURLRequestCreateWithWKURL(url.get()));
  125. WKRetainPtr<WKStringRef> userData = Util::toWK("WKPageLoadURLRequestWithUserData UserData");
  126. WKPageLoadURLRequestWithUserData(webView->page(), urlRequest.get(), userData.get());
  127. testWillLoadURLRequestReturnValues(url.get(), userData.get());
  128. }
  129. TEST_F(WebKit2WillLoadTest, WKPageLoadURLRequest)
  130. {
  131. WKRetainPtr<WKURLRef> url = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  132. WKRetainPtr<WKURLRequestRef> urlRequest = adoptWK(WKURLRequestCreateWithWKURL(url.get()));
  133. WKPageLoadURLRequest(webView->page(), urlRequest.get());
  134. testWillLoadURLRequestReturnValues(url.get(), 0);
  135. }
  136. // Data Request tests
  137. TEST_F(WebKit2WillLoadTest, WKPageLoadHTMLStringWithUserData)
  138. {
  139. WKRetainPtr<WKURLRef> baseURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  140. WKRetainPtr<WKStringRef> userData = Util::toWK("WKPageLoadHTMLStringWithUserData UserData");
  141. WKRetainPtr<WKStringRef> htmlString = Util::toWK("<body>Hello, World</body>");
  142. WKPageLoadHTMLStringWithUserData(webView->page(), htmlString.get(), baseURL.get(), userData.get());
  143. testWillLoadDataRequestReturnValues(baseURL.get(), Util::toWK("text/html").get(), Util::toWK("utf-16").get(), 0, userData.get());
  144. }
  145. TEST_F(WebKit2WillLoadTest, WKPageLoadHTMLString)
  146. {
  147. WKRetainPtr<WKURLRef> baseURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  148. WKRetainPtr<WKStringRef> htmlString = Util::toWK("<body>Hello, World</body>");
  149. WKPageLoadHTMLString(webView->page(), htmlString.get(), baseURL.get());
  150. testWillLoadDataRequestReturnValues(baseURL.get(), Util::toWK("text/html").get(), Util::toWK("utf-16").get(), 0, 0);
  151. }
  152. TEST_F(WebKit2WillLoadTest, WKPageLoadAlternateHTMLStringWithUserData)
  153. {
  154. WKRetainPtr<WKStringRef> htmlString = Util::toWK("<body>Hello, World</body>");
  155. WKRetainPtr<WKURLRef> baseURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  156. WKRetainPtr<WKURLRef> unreachableURL = adoptWK(WKURLCreateWithUTF8CString("about:other"));
  157. WKRetainPtr<WKStringRef> userData = Util::toWK("WKPageLoadAlternateHTMLStringWithUserData UserData");
  158. WKPageLoadAlternateHTMLStringWithUserData(webView->page(), htmlString.get(), baseURL.get(), unreachableURL.get(), userData.get());
  159. testWillLoadDataRequestReturnValues(baseURL.get(), Util::toWK("text/html").get(), Util::toWK("utf-16").get(), unreachableURL.get(), userData.get());
  160. }
  161. TEST_F(WebKit2WillLoadTest, WKPageLoadAlternateHTMLString)
  162. {
  163. WKRetainPtr<WKStringRef> htmlString = Util::toWK("<body>Hello, World</body>");
  164. WKRetainPtr<WKURLRef> baseURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  165. WKRetainPtr<WKURLRef> unreachableURL = adoptWK(WKURLCreateWithUTF8CString("about:other"));
  166. WKPageLoadAlternateHTMLString(webView->page(), htmlString.get(), baseURL.get(), unreachableURL.get());
  167. testWillLoadDataRequestReturnValues(baseURL.get(), Util::toWK("text/html").get(), Util::toWK("utf-16").get(), unreachableURL.get(), 0);
  168. }
  169. TEST_F(WebKit2WillLoadTest, WKPageLoadPlainTextStringWithUserData)
  170. {
  171. WKRetainPtr<WKStringRef> plaintTextString = Util::toWK("Hello, World");
  172. WKRetainPtr<WKStringRef> userData = Util::toWK("WKPageLoadPlainTextStringWithUserData UserData");
  173. WKPageLoadPlainTextStringWithUserData(webView->page(), plaintTextString.get(), userData.get());
  174. WKRetainPtr<WKURLRef> blankURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  175. testWillLoadDataRequestReturnValues(blankURL.get(), Util::toWK("text/plain").get(), Util::toWK("utf-16").get(), 0, userData.get());
  176. }
  177. TEST_F(WebKit2WillLoadTest, WKPageLoadPlainTextString)
  178. {
  179. WKRetainPtr<WKStringRef> plaintTextString = Util::toWK("Hello, World");
  180. WKPageLoadPlainTextString(webView->page(), plaintTextString.get());
  181. WKRetainPtr<WKURLRef> blankURL = adoptWK(WKURLCreateWithUTF8CString("about:blank"));
  182. testWillLoadDataRequestReturnValues(blankURL.get(), Util::toWK("text/plain").get(), Util::toWK("utf-16").get(), 0, 0);
  183. }
  184. } // namespace TestWebKitAPI