| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <EarlGrey/EarlGrey.h> | 5 #import <EarlGrey/EarlGrey.h> |
| 6 | 6 |
| 7 #include "components/strings/grit/components_strings.h" | 7 #include "components/strings/grit/components_strings.h" |
| 8 #import "ios/chrome/test/app/chrome_test_util.h" |
| 8 #include "ios/chrome/test/app/web_view_interaction_test_util.h" | 9 #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_earl_grey.h" |
| 10 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 11 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 11 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 12 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 12 #import "ios/web/public/test/http_server.h" | 13 #import "ios/web/public/test/http_server.h" |
| 14 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 13 #include "ios/web/public/test/http_server_util.h" | 15 #include "ios/web/public/test/http_server_util.h" |
| 14 #include "ios/web/public/test/response_providers/data_response_provider.h" | 16 #include "ios/web/public/test/response_providers/data_response_provider.h" |
| 15 #include "ios/web/public/test/response_providers/error_page_response_provider.h" | 17 #include "ios/web/public/test/response_providers/error_page_response_provider.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 18 | 20 |
| 19 using chrome_test_util::omniboxText; | 21 using chrome_test_util::OmniboxText; |
| 20 using chrome_test_util::staticHtmlViewContainingText; | 22 using chrome_test_util::StaticHtmlViewContainingText; |
| 21 using chrome_test_util::TapWebViewElementWithId; | 23 using chrome_test_util::TapWebViewElementWithId; |
| 22 using chrome_test_util::webViewBelongingToWebController; | 24 using chrome_test_util::WebViewContainingText; |
| 23 using chrome_test_util::webViewContainingText; | |
| 24 | 25 |
| 25 using web::test::HttpServer; | 26 using web::test::HttpServer; |
| 26 | 27 |
| 27 // Tests display of error pages for bad URLs. | 28 // Tests display of error pages for bad URLs. |
| 28 @interface ErrorPageTestCase : ChromeTestCase | 29 @interface ErrorPageTestCase : ChromeTestCase |
| 29 | 30 |
| 30 // Checks that the DNS error page is visible. | 31 // Checks that the DNS error page is visible. |
| 31 - (void)checkErrorPageIsVisible; | 32 - (void)checkErrorPageIsVisible; |
| 32 | 33 |
| 33 // Checks that the DNS error page is not visible. | 34 // Checks that the DNS error page is not visible. |
| 34 - (void)checkErrorPageIsNotVisible; | 35 - (void)checkErrorPageIsNotVisible; |
| 35 | 36 |
| 36 @end | 37 @end |
| 37 | 38 |
| 38 @implementation ErrorPageTestCase | 39 @implementation ErrorPageTestCase |
| 39 | 40 |
| 40 #pragma mark - utilities | 41 #pragma mark - utilities |
| 41 | 42 |
| 42 // TODO(crbug.com/638674): Evaluate if this can move to shared code. | 43 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 43 - (void)checkErrorPageIsVisible { | 44 - (void)checkErrorPageIsVisible { |
| 44 // The DNS error page is static HTML content, so it isn't part of the webview | 45 // The DNS error page is static HTML content, so it isn't part of the webview |
| 45 // owned by the webstate. | 46 // owned by the webstate. |
| 46 [[EarlGrey selectElementWithMatcher:webViewBelongingToWebController()] | 47 id<GREYMatcher> webViewMatcher = |
| 48 web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); |
| 49 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 47 assertWithMatcher:grey_nil()]; | 50 assertWithMatcher:grey_nil()]; |
| 48 NSString* const kError = | 51 NSString* const kError = |
| 49 l10n_util::GetNSString(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); | 52 l10n_util::GetNSString(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); |
| 50 [[EarlGrey selectElementWithMatcher:staticHtmlViewContainingText(kError)] | 53 [[EarlGrey selectElementWithMatcher:StaticHtmlViewContainingText(kError)] |
| 51 assertWithMatcher:grey_notNil()]; | 54 assertWithMatcher:grey_notNil()]; |
| 52 } | 55 } |
| 53 | 56 |
| 54 - (void)checkErrorPageIsNotVisible { | 57 - (void)checkErrorPageIsNotVisible { |
| 55 // Check that the webview belongs to the web controller, and that the error | 58 // Check that the webview belongs to the web controller, and that the error |
| 56 // text doesn't appear in the webview. | 59 // text doesn't appear in the webview. |
| 57 [[EarlGrey selectElementWithMatcher:webViewBelongingToWebController()] | 60 id<GREYMatcher> webViewMatcher = |
| 61 web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); |
| 62 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 58 assertWithMatcher:grey_notNil()]; | 63 assertWithMatcher:grey_notNil()]; |
| 59 const std::string kError = | 64 const std::string kError = |
| 60 l10n_util::GetStringUTF8(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); | 65 l10n_util::GetStringUTF8(IDS_ERRORPAGES_HEADING_NOT_AVAILABLE); |
| 61 [[EarlGrey selectElementWithMatcher:webViewContainingText(kError)] | 66 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kError)] |
| 62 assertWithMatcher:grey_nil()]; | 67 assertWithMatcher:grey_nil()]; |
| 63 } | 68 } |
| 64 | 69 |
| 65 #pragma mark - tests | 70 #pragma mark - tests |
| 66 | 71 |
| 67 // Tests whether the error page is displayed for a bad URL. | 72 // Tests whether the error page is displayed for a bad URL. |
| 68 - (void)testErrorPage { | 73 - (void)testErrorPage { |
| 69 std::unique_ptr<web::DataResponseProvider> provider( | 74 std::unique_ptr<web::DataResponseProvider> provider( |
| 70 new ErrorPageResponseProvider()); | 75 new ErrorPageResponseProvider()); |
| 71 web::test::SetUpHttpServer(std::move(provider)); | 76 web::test::SetUpHttpServer(std::move(provider)); |
| 72 | 77 |
| 73 [ChromeEarlGrey loadURL:ErrorPageResponseProvider::GetDnsFailureUrl()]; | 78 [ChromeEarlGrey loadURL:ErrorPageResponseProvider::GetDnsFailureUrl()]; |
| 74 | 79 |
| 75 [self checkErrorPageIsVisible]; | 80 [self checkErrorPageIsVisible]; |
| 76 } | 81 } |
| 77 | 82 |
| 78 // Tests whether the error page is displayed if it is behind a redirect. | 83 // Tests whether the error page is displayed if it is behind a redirect. |
| 79 - (void)testErrorPageRedirect { | 84 - (void)testErrorPageRedirect { |
| 80 std::unique_ptr<web::DataResponseProvider> provider( | 85 std::unique_ptr<web::DataResponseProvider> provider( |
| 81 new ErrorPageResponseProvider()); | 86 new ErrorPageResponseProvider()); |
| 82 web::test::SetUpHttpServer(std::move(provider)); | 87 web::test::SetUpHttpServer(std::move(provider)); |
| 83 | 88 |
| 84 // Load a URL that redirects to the DNS-failing URL. | 89 // Load a URL that redirects to the DNS-failing URL. |
| 85 [ChromeEarlGrey | 90 [ChromeEarlGrey |
| 86 loadURL:ErrorPageResponseProvider::GetRedirectToDnsFailureUrl()]; | 91 loadURL:ErrorPageResponseProvider::GetRedirectToDnsFailureUrl()]; |
| 87 | 92 |
| 88 // Verify that the redirect occurred before checking for the DNS error. | 93 // Verify that the redirect occurred before checking for the DNS error. |
| 89 const std::string& redirectedURL = | 94 const std::string& redirectedURL = |
| 90 ErrorPageResponseProvider::GetDnsFailureUrl().GetContent(); | 95 ErrorPageResponseProvider::GetDnsFailureUrl().GetContent(); |
| 91 [[EarlGrey selectElementWithMatcher:omniboxText(redirectedURL)] | 96 [[EarlGrey selectElementWithMatcher:OmniboxText(redirectedURL)] |
| 92 assertWithMatcher:grey_notNil()]; | 97 assertWithMatcher:grey_notNil()]; |
| 93 | 98 |
| 94 [self checkErrorPageIsVisible]; | 99 [self checkErrorPageIsVisible]; |
| 95 } | 100 } |
| 96 | 101 |
| 97 // Tests that the error page is not displayed if the bad URL is in a <iframe> | 102 // Tests that the error page is not displayed if the bad URL is in a <iframe> |
| 98 // tag. | 103 // tag. |
| 99 - (void)testErrorPageInIFrame { | 104 - (void)testErrorPageInIFrame { |
| 100 std::map<GURL, std::string> responses; | 105 std::map<GURL, std::string> responses; |
| 101 const GURL URL = HttpServer::MakeUrl("http://browsingErrorPageInIFrame"); | 106 const GURL URL = HttpServer::MakeUrl("http://browsingErrorPageInIFrame"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 128 "<script>setTimeout(" + "function() { document.body.innerHTML+='<p>" + | 133 "<script>setTimeout(" + "function() { document.body.innerHTML+='<p>" + |
| 129 kTimerCompleted + "</p>" + "<iframe src=\"" + | 134 kTimerCompleted + "</p>" + "<iframe src=\"" + |
| 130 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + | 135 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + |
| 131 "\"></iframe>'}, 1000);" + "</script>"; | 136 "\"></iframe>'}, 1000);" + "</script>"; |
| 132 std::unique_ptr<web::DataResponseProvider> provider( | 137 std::unique_ptr<web::DataResponseProvider> provider( |
| 133 new ErrorPageResponseProvider(responses)); | 138 new ErrorPageResponseProvider(responses)); |
| 134 web::test::SetUpHttpServer(std::move(provider)); | 139 web::test::SetUpHttpServer(std::move(provider)); |
| 135 | 140 |
| 136 [ChromeEarlGrey loadURL:URL]; | 141 [ChromeEarlGrey loadURL:URL]; |
| 137 // Check that the timer has completed. | 142 // Check that the timer has completed. |
| 138 [[EarlGrey selectElementWithMatcher:webViewContainingText(kTimerCompleted)] | 143 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kTimerCompleted)] |
| 139 assertWithMatcher:grey_notNil()]; | 144 assertWithMatcher:grey_notNil()]; |
| 140 // DNS error page should still not appear. | 145 // DNS error page should still not appear. |
| 141 [self checkErrorPageIsNotVisible]; | 146 [self checkErrorPageIsNotVisible]; |
| 142 } | 147 } |
| 143 | 148 |
| 144 // Tests that the error page is not displayed if the navigation was not | 149 // Tests that the error page is not displayed if the navigation was not |
| 145 // user-initiated. | 150 // user-initiated. |
| 146 - (void)testErrorPageNoUserInteraction { | 151 - (void)testErrorPageNoUserInteraction { |
| 147 // Create map of canned responses and set up the test HTML server. | 152 // Create map of canned responses and set up the test HTML server. |
| 148 std::map<GURL, std::string> responses; | 153 std::map<GURL, std::string> responses; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 165 "</p><iframe src=" + | 170 "</p><iframe src=" + |
| 166 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + | 171 ErrorPageResponseProvider::GetDnsFailureUrl().spec() + |
| 167 ">\"}, " + kTimeoutMs + ");'></form>"; | 172 ">\"}, " + kTimeoutMs + ");'></form>"; |
| 168 std::unique_ptr<web::DataResponseProvider> provider( | 173 std::unique_ptr<web::DataResponseProvider> provider( |
| 169 new ErrorPageResponseProvider(responses)); | 174 new ErrorPageResponseProvider(responses)); |
| 170 web::test::SetUpHttpServer(std::move(provider)); | 175 web::test::SetUpHttpServer(std::move(provider)); |
| 171 | 176 |
| 172 [ChromeEarlGrey loadURL:URL]; | 177 [ChromeEarlGrey loadURL:URL]; |
| 173 TapWebViewElementWithId(kButtonId); | 178 TapWebViewElementWithId(kButtonId); |
| 174 // Check that the timer has completed. | 179 // Check that the timer has completed. |
| 175 [[EarlGrey selectElementWithMatcher:webViewContainingText(kTimerCompleted)] | 180 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kTimerCompleted)] |
| 176 assertWithMatcher:grey_notNil()]; | 181 assertWithMatcher:grey_notNil()]; |
| 177 // DNS error page should still not appear. | 182 // DNS error page should still not appear. |
| 178 [self checkErrorPageIsNotVisible]; | 183 [self checkErrorPageIsNotVisible]; |
| 179 } | 184 } |
| 180 | 185 |
| 181 @end | 186 @end |
| OLD | NEW |