ui_delegate_inttest.mm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import <ChromeWebView/ChromeWebView.h>
  5. #import <Foundation/Foundation.h>
  6. #import "base/test/ios/wait_util.h"
  7. #import "ios/web_view/test/web_view_inttest_base.h"
  8. #import "ios/web_view/test/web_view_test_util.h"
  9. #import "net/base/mac/url_conversions.h"
  10. #include "net/test/embedded_test_server/embedded_test_server.h"
  11. #include "testing/gtest_mac.h"
  12. #import "third_party/ocmock/OCMock/OCMock.h"
  13. #include "url/gurl.h"
  14. #if !defined(__has_feature) || !__has_feature(objc_arc)
  15. #error "This file requires ARC support."
  16. #endif
  17. using base::test::ios::kWaitForUIElementTimeout;
  18. namespace ios_web_view {
  19. // Tests CWVUIDelegate.
  20. class UIDelegateTest : public ios_web_view::WebViewInttestBase {
  21. public:
  22. UIDelegateTest() : mock_delegate_(OCMProtocolMock(@protocol(CWVUIDelegate))) {
  23. web_view_.UIDelegate = mock_delegate_;
  24. }
  25. void SetUp() override {
  26. ios_web_view::WebViewInttestBase::SetUp();
  27. ASSERT_TRUE(test_server_->Start());
  28. }
  29. NSURL* GetEchoURL() {
  30. return net::NSURLWithGURL(test_server_->GetURL("/echo"));
  31. }
  32. id<CWVUIDelegate> mock_delegate_;
  33. };
  34. // Tests -webView:createWebViewWithConfiguration:forNavigationAction:
  35. TEST_F(UIDelegateTest, CreateWebView) {
  36. id expected_navigation_action =
  37. [OCMArg checkWithBlock:^(CWVNavigationAction* action) {
  38. return
  39. [action.request.URL.absoluteString isEqual:@"http://example.com/"];
  40. }];
  41. OCMExpect([mock_delegate_ webView:web_view_
  42. createWebViewWithConfiguration:web_view_.configuration
  43. forNavigationAction:expected_navigation_action]);
  44. ASSERT_TRUE(test::LoadUrl(web_view_, GetEchoURL()));
  45. bool success;
  46. EXPECT_NE(nil,
  47. test::EvaluateJavaScript(
  48. web_view_, @"typeof open('http://example.com/') === 'object'",
  49. &success));
  50. EXPECT_TRUE(success);
  51. [(id)mock_delegate_ verify];
  52. }
  53. // Tests -webView:runJavaScriptAlertPanelWithMessage:pageURL:completionHandler:
  54. TEST_F(UIDelegateTest, RunJavaScriptAlertPanel) {
  55. id mock_completion_handler =
  56. [OCMArg checkWithBlock:^(void (^completionHandler)(void)) {
  57. completionHandler();
  58. return YES;
  59. }];
  60. OCMExpect([mock_delegate_ webView:web_view_
  61. runJavaScriptAlertPanelWithMessage:@"message"
  62. pageURL:GetEchoURL()
  63. completionHandler:mock_completion_handler]);
  64. ASSERT_TRUE(test::LoadUrl(web_view_, GetEchoURL()));
  65. bool success;
  66. test::EvaluateJavaScript(web_view_, @"alert('message')", &success);
  67. EXPECT_TRUE(success);
  68. [(id)mock_delegate_ verify];
  69. }
  70. // Tests
  71. // -webView:runJavaScriptConfirmPanelWithMessage:pageURL:completionHandler:
  72. TEST_F(UIDelegateTest, RunJavaScriptConfirmPanel) {
  73. id mock_completion_handler =
  74. [OCMArg checkWithBlock:^(void (^completionHandler)(BOOL)) {
  75. completionHandler(YES);
  76. return YES;
  77. }];
  78. OCMExpect([mock_delegate_ webView:web_view_
  79. runJavaScriptConfirmPanelWithMessage:@"message"
  80. pageURL:GetEchoURL()
  81. completionHandler:mock_completion_handler]);
  82. ASSERT_TRUE(test::LoadUrl(web_view_, GetEchoURL()));
  83. bool success;
  84. EXPECT_NSEQ(@(YES), test::EvaluateJavaScript(web_view_, @"confirm('message')",
  85. &success));
  86. EXPECT_TRUE(success);
  87. [(id)mock_delegate_ verify];
  88. }
  89. // Tests
  90. // -webView:runJavaScriptTextInputPanelWithPrompt:pageURL:completionHandler:
  91. TEST_F(UIDelegateTest, RunJavaScriptTextInputPanel) {
  92. id mock_completion_handler =
  93. [OCMArg checkWithBlock:^(void (^completionHandler)(NSString*)) {
  94. completionHandler(@"input");
  95. return YES;
  96. }];
  97. OCMExpect([mock_delegate_ webView:web_view_
  98. runJavaScriptTextInputPanelWithPrompt:@"prompt"
  99. defaultText:@"default"
  100. pageURL:GetEchoURL()
  101. completionHandler:mock_completion_handler]);
  102. ASSERT_TRUE(test::LoadUrl(web_view_, GetEchoURL()));
  103. bool success;
  104. EXPECT_NSEQ(@"input",
  105. test::EvaluateJavaScript(
  106. web_view_, @"prompt('prompt', 'default')", &success));
  107. EXPECT_TRUE(success);
  108. [(id)mock_delegate_ verify];
  109. }
  110. // Tests -webView:didLoadFavicons:
  111. TEST_F(UIDelegateTest, DidLoadFavicons) {
  112. NSURL* page_url = net::NSURLWithGURL(GetUrlForPageWithHtml(R"(
  113. <!DOCTYPE html>
  114. <html>
  115. <head>
  116. <link rel="icon" href="/testfavicon.png">
  117. </head>
  118. <body></body>
  119. </html>
  120. )"));
  121. // This file does not exist, but it doesn't matter for this test.
  122. NSURL* favicon_url =
  123. net::NSURLWithGURL(test_server_->GetURL("/testfavicon.png"));
  124. __block NSArray<CWVFavicon*>* favicons = nil;
  125. id favicons_arg = [OCMArg checkWithBlock:^(NSArray<CWVFavicon*>* value) {
  126. favicons = value;
  127. return YES;
  128. }];
  129. OCMExpect([mock_delegate_ webView:web_view_ didLoadFavicons:favicons_arg]);
  130. ASSERT_TRUE(test::LoadUrl(web_view_, page_url));
  131. [(id)mock_delegate_ verifyWithDelay:kWaitForUIElementTimeout];
  132. ASSERT_EQ(1u, favicons.count);
  133. EXPECT_EQ(CWVFaviconTypeFavicon, favicons[0].type);
  134. EXPECT_NSEQ(favicon_url, favicons[0].URL);
  135. }
  136. } // namespace ios_web_view