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 "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/time/time.h" |
| 10 #include "ios/chrome/browser/ui/ui_util.h" |
| 11 #include "ios/chrome/test/app/navigation_test_util.h" |
| 12 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 13 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 14 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 15 #import "ios/web/public/test/http_server.h" |
| 16 #include "ios/web/public/test/http_server_util.h" |
| 17 #include "ios/web/public/test/response_providers/html_response_provider.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 using chrome_test_util::buttonWithAccessibilityLabelId; |
| 21 using chrome_test_util::webViewContainingText; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Text appearing on the navigation test page. |
| 26 const char kPageText[] = "Navigation testing page"; |
| 27 |
| 28 // Response provider that serves the page which never finishes loading. |
| 29 class InfinitePendingResponseProvider : public HtmlResponseProvider { |
| 30 public: |
| 31 explicit InfinitePendingResponseProvider(const GURL& url) : url_(url) {} |
| 32 ~InfinitePendingResponseProvider() override {} |
| 33 |
| 34 // HtmlResponseProvider overrides: |
| 35 bool CanHandleRequest(const Request& request) override { |
| 36 return request.url == url_ || |
| 37 request.url == GetInfinitePendingResponseUrl(); |
| 38 } |
| 39 void GetResponseHeadersAndBody( |
| 40 const Request& request, |
| 41 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 42 std::string* response_body) override { |
| 43 if (request.url == url_) { |
| 44 *headers = GetDefaultResponseHeaders(); |
| 45 *response_body = |
| 46 base::StringPrintf("<p>%s</p><img src='%s'/>", kPageText, |
| 47 GetInfinitePendingResponseUrl().spec().c_str()); |
| 48 } else if (request.url == GetInfinitePendingResponseUrl()) { |
| 49 base::PlatformThread::Sleep(base::TimeDelta::FromDays(1)); |
| 50 } else { |
| 51 NOTREACHED(); |
| 52 } |
| 53 } |
| 54 |
| 55 private: |
| 56 // Returns a url for which this responce provider will never reply. |
| 57 GURL GetInfinitePendingResponseUrl() const { |
| 58 GURL::Replacements replacements; |
| 59 replacements.SetPathStr("resource"); |
| 60 return url_.GetOrigin().ReplaceComponents(replacements); |
| 61 } |
| 62 |
| 63 // Main page URL that never finish loading. |
| 64 GURL url_; |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 69 // Test case for Stop Loading button. |
| 70 @interface StopLoadingTestCase : ChromeTestCase |
| 71 @end |
| 72 |
| 73 @implementation StopLoadingTestCase |
| 74 |
| 75 - (void)tearDown { |
| 76 // |testStopLoading| Disables synchronization, so make sure that it is enabled |
| 77 // if that test has failed and did not enable it back. |
| 78 [[GREYConfiguration sharedInstance] |
| 79 setValue:@YES |
| 80 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 81 [super tearDown]; |
| 82 } |
| 83 |
| 84 // Tests that tapping "Stop" button stops the loading. |
| 85 - (void)testStopLoading { |
| 86 // Load a page which never finishes loading. |
| 87 GURL infinitePendingURL = web::test::HttpServer::MakeUrl("http://infinite"); |
| 88 web::test::SetUpHttpServer( |
| 89 base::MakeUnique<InfinitePendingResponseProvider>(infinitePendingURL)); |
| 90 |
| 91 // The page being loaded never completes, so call the LoadUrl helper that |
| 92 // does not wait for the page to complete loading. |
| 93 chrome_test_util::LoadUrl(infinitePendingURL); |
| 94 |
| 95 if (IsIPadIdiom()) { |
| 96 // Disable EG synchronization so the framework does not wait until the tab |
| 97 // loading spinner becomes idle (which will not happen until the stop button |
| 98 // is tapped). |
| 99 [[GREYConfiguration sharedInstance] |
| 100 setValue:@NO |
| 101 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 102 } |
| 103 |
| 104 // Wait until the page is half loaded. |
| 105 [[EarlGrey selectElementWithMatcher:webViewContainingText(kPageText)] |
| 106 assertWithMatcher:grey_notNil()]; |
| 107 |
| 108 // On iPhone Stop/Reload button is a part of tools menu, so open it. |
| 109 if (!IsIPadIdiom()) { |
| 110 [ChromeEarlGreyUI openToolsMenu]; |
| 111 } |
| 112 |
| 113 // Verify that stop button is visible and reload button is hidden. |
| 114 [[EarlGrey selectElementWithMatcher:chrome_test_util::stopButton()] |
| 115 assertWithMatcher:grey_sufficientlyVisible()]; |
| 116 [[EarlGrey selectElementWithMatcher:chrome_test_util::reloadButton()] |
| 117 assertWithMatcher:grey_notVisible()]; |
| 118 |
| 119 // Stop the page loading. |
| 120 [[EarlGrey selectElementWithMatcher:chrome_test_util::stopButton()] |
| 121 performAction:grey_tap()]; |
| 122 |
| 123 // Enable synchronization back. The spinner should become idle and test should |
| 124 // wait for it. |
| 125 [[GREYConfiguration sharedInstance] |
| 126 setValue:@YES |
| 127 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 128 |
| 129 // Verify that stop button is hidden and reload button is visible. |
| 130 if (!IsIPadIdiom()) { |
| 131 [ChromeEarlGreyUI openToolsMenu]; |
| 132 } |
| 133 [[EarlGrey selectElementWithMatcher:chrome_test_util::stopButton()] |
| 134 assertWithMatcher:grey_notVisible()]; |
| 135 [[EarlGrey selectElementWithMatcher:chrome_test_util::reloadButton()] |
| 136 assertWithMatcher:grey_sufficientlyVisible()]; |
| 137 } |
| 138 |
| 139 @end |
OLD | NEW |