OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #import <EarlGrey/EarlGrey.h> |
| 6 |
| 7 #include "components/strings/grit/components_strings.h" |
| 8 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 9 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 10 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 11 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 12 #import "ios/web/public/test/http_server.h" |
| 13 #include "ios/web/public/test/http_server_util.h" |
| 14 #include "ios/web/public/test/response_providers/data_response_provider.h" |
| 15 #include "ios/web/public/test/response_providers/error_page_response_provider.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/l10n/l10n_util_mac.h" |
| 18 |
| 19 using chrome_test_util::omniboxText; |
| 20 using chrome_test_util::staticHtmlViewContainingText; |
| 21 using chrome_test_util::TapWebViewElementWithId; |
| 22 using chrome_test_util::webViewBelongingToWebController; |
| 23 using chrome_test_util::webViewContainingText; |
| 24 |
| 25 using web::test::HttpServer; |
| 26 |
| 27 // Tests display of error pages for bad URLs. |
| 28 @interface ErrorPageTestCase : ChromeTestCase |
| 29 |
| 30 // Checks that the DNS error page is visible. |
| 31 - (void)checkErrorPageIsVisible; |
| 32 |
| 33 // Checks that the DNS error page is not visible. |
| 34 - (void)checkErrorPageIsNotVisible; |
| 35 |
| 36 @end |
| 37 |
| 38 @implementation ErrorPageTestCase |
| 39 |
| 40 #pragma mark - utilities |
| 41 |
| 42 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 43 - (void)checkErrorPageIsVisible { |
| 44 // The DNS error page is static HTML content, so it isn't part of the webview |
| 45 // owned by the webstate. |
| 46 [[EarlGrey selectElementWithMatcher:webViewBelongingToWebController()] |
| 47 assertWithMatcher:grey_nil()]; |
| 48 NSString* const kError = |
| 49 l10n_util::GetNSString(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); |
| 50 [[EarlGrey selectElementWithMatcher:staticHtmlViewContainingText(kError)] |
| 51 assertWithMatcher:grey_notNil()]; |
| 52 } |
| 53 |
| 54 - (void)checkErrorPageIsNotVisible { |
| 55 // Check that the webview belongs to the web controller, and that the error |
| 56 // text doesn't appear in the webview. |
| 57 [[EarlGrey selectElementWithMatcher:webViewBelongingToWebController()] |
| 58 assertWithMatcher:grey_notNil()]; |
| 59 const std::string kError = |
| 60 l10n_util::GetStringUTF8(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); |
| 61 [[EarlGrey selectElementWithMatcher:webViewContainingText(kError)] |
| 62 assertWithMatcher:grey_nil()]; |
| 63 } |
| 64 |
| 65 #pragma mark - tests |
| 66 |
| 67 // Tests whether the error page is displayed for a bad URL. |
| 68 - (void)testErrorPage { |
| 69 std::unique_ptr<web::DataResponseProvider> provider( |
| 70 new ErrorPageResponseProvider()); |
| 71 web::test::SetUpHttpServer(std::move(provider)); |
| 72 |
| 73 [ChromeEarlGrey loadURL:ErrorPageResponseProvider::GetDnsFailureUrl()]; |
| 74 |
| 75 [self checkErrorPageIsVisible]; |
| 76 } |
| 77 |
| 78 // Tests whether the error page is displayed if it is behind a redirect. |
| 79 - (void)testErrorPageRedirect { |
| 80 std::unique_ptr<web::DataResponseProvider> provider( |
| 81 new ErrorPageResponseProvider()); |
| 82 web::test::SetUpHttpServer(std::move(provider)); |
| 83 |
| 84 // Load a URL that redirects to the DNS-failing URL. |
| 85 [ChromeEarlGrey |
| 86 loadURL:ErrorPageResponseProvider::GetRedirectToDnsFailureUrl()]; |
| 87 |
| 88 // Verify that the redirect occurred before checking for the DNS error. |
| 89 const std::string& redirectedURL = |
| 90 ErrorPageResponseProvider::GetDnsFailureUrl().GetContent(); |
| 91 [[EarlGrey selectElementWithMatcher:omniboxText(redirectedURL)] |
| 92 assertWithMatcher:grey_notNil()]; |
| 93 |
| 94 [self checkErrorPageIsVisible]; |
| 95 } |
| 96 |
| 97 // Tests that the error page is not displayed if the bad URL is in a <iframe> |
| 98 // tag. |
| 99 - (void)testErrorPageInIFrame { |
| 100 std::map<GURL, std::string> responses; |
| 101 const GURL URL = HttpServer::MakeUrl("http://browsingErrorPageInIFrame"); |
| 102 // This page contains an iframe to a bad URL. |
| 103 responses[URL] = std::string("This page contains an iframe.<iframe src='") + |
| 104 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + |
| 105 "'>whatever</iframe>"; |
| 106 std::unique_ptr<web::DataResponseProvider> provider( |
| 107 new ErrorPageResponseProvider(responses)); |
| 108 web::test::SetUpHttpServer(std::move(provider)); |
| 109 |
| 110 [ChromeEarlGrey loadURL:URL]; |
| 111 |
| 112 [self checkErrorPageIsNotVisible]; |
| 113 } |
| 114 |
| 115 // Tests that the error page is not displayed if the bad URL is in a <iframe> |
| 116 // tag that is loaded after the initial page load completes. |
| 117 - (void)testErrorPageInIFrameAfterDelay { |
| 118 // Create map of canned responses and set up the test HTML server. |
| 119 std::map<GURL, std::string> responses; |
| 120 const GURL URL = |
| 121 HttpServer::MakeUrl("http://browsingErrorPageInIFrameAfterDelay"); |
| 122 // This page adds an iframe to a bad URL one second after loading. When the |
| 123 // timer completes, some text is also added to the page so EarlGrey can detect |
| 124 // that the timer has completed. |
| 125 const std::string kTimerCompleted = "Timer completed"; |
| 126 responses[URL] = |
| 127 std::string("This page will have an iframe appended after page load.") + |
| 128 "<script>setTimeout(" + "function() { document.body.innerHTML+='<p>" + |
| 129 kTimerCompleted + "</p>" + "<iframe src=\"" + |
| 130 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + |
| 131 "\"></iframe>'}, 1000);" + "</script>"; |
| 132 std::unique_ptr<web::DataResponseProvider> provider( |
| 133 new ErrorPageResponseProvider(responses)); |
| 134 web::test::SetUpHttpServer(std::move(provider)); |
| 135 |
| 136 [ChromeEarlGrey loadURL:URL]; |
| 137 // Check that the timer has completed. |
| 138 [[EarlGrey selectElementWithMatcher:webViewContainingText(kTimerCompleted)] |
| 139 assertWithMatcher:grey_notNil()]; |
| 140 // DNS error page should still not appear. |
| 141 [self checkErrorPageIsNotVisible]; |
| 142 } |
| 143 |
| 144 // Tests that the error page is not displayed if the navigation was not |
| 145 // user-initiated. |
| 146 - (void)testErrorPageNoUserInteraction { |
| 147 // Create map of canned responses and set up the test HTML server. |
| 148 std::map<GURL, std::string> responses; |
| 149 const GURL URL = |
| 150 HttpServer::MakeUrl("http://browsingErrorPageNoUserInteraction"); |
| 151 // This page contains a button that starts a timer that appends an iframe to a |
| 152 // bad URL. To create a non-user-initiated navigation, the timer takes longer |
| 153 // than the CRWWebController's kMaximumDelayForUserInteractionInSeconds |
| 154 // constant. When the timer completes, some text is also added to the page so |
| 155 // the test can detect that the timer has completed. |
| 156 const std::string kTimerCompleted = "Timer completed"; |
| 157 const std::string kButtonId = "aButton"; |
| 158 // Timeout used for the setTimeout() call the button invokes. This value must |
| 159 // be greater than CRWWebController's kMaximumDelayForUserInteractionInSeconds |
| 160 // and less than testing::kWaitForUIElementTimeout |
| 161 const std::string kTimeoutMs = "3000"; |
| 162 responses[URL] = std::string("<form><input type='button' id='") + kButtonId + |
| 163 "' value='button' onClick='setTimeout(function() { " + |
| 164 "document.body.innerHTML+=\"<p>" + kTimerCompleted + |
| 165 "</p><iframe src=" + |
| 166 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + |
| 167 ">\"}, " + kTimeoutMs + ");'></form>"; |
| 168 std::unique_ptr<web::DataResponseProvider> provider( |
| 169 new ErrorPageResponseProvider(responses)); |
| 170 web::test::SetUpHttpServer(std::move(provider)); |
| 171 |
| 172 [ChromeEarlGrey loadURL:URL]; |
| 173 TapWebViewElementWithId(kButtonId); |
| 174 // Check that the timer has completed. |
| 175 [[EarlGrey selectElementWithMatcher:webViewContainingText(kTimerCompleted)] |
| 176 assertWithMatcher:grey_notNil()]; |
| 177 // DNS error page should still not appear. |
| 178 [self checkErrorPageIsNotVisible]; |
| 179 } |
| 180 |
| 181 @end |
OLD | NEW |