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 #import <UIKit/UIKit.h> |
| 7 #import <XCTest/XCTest.h> |
| 8 |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 13 #import "ios/chrome/test/app/chrome_test_util.h" |
| 14 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 15 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 19 #import "ios/web/public/test/http_server.h" |
| 20 #include "ios/web/public/test/http_server_util.h" |
| 21 #include "url/url_constants.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Timeout to use when waiting for a condition to be true. |
| 26 const CFTimeInterval kConditionTimeout = 4.0; |
| 27 |
| 28 // Returns the URL for the HTML that is used for testing purposes in this file. |
| 29 GURL GetTestUrl() { |
| 30 return web::test::HttpServer::MakeUrl( |
| 31 "http://ios/testing/data/http_server_files/" |
| 32 "browsing_prevent_default_test_page.html"); |
| 33 } |
| 34 |
| 35 // ScopedBlockPopupsPref modifies the block popups preference and resets the |
| 36 // preference to its original value when this object goes out of scope. |
| 37 class ScopedBlockPopupsPref { |
| 38 public: |
| 39 explicit ScopedBlockPopupsPref(ContentSetting setting) { |
| 40 original_setting_ = GetPrefValue(); |
| 41 SetPrefValue(setting); |
| 42 } |
| 43 ~ScopedBlockPopupsPref() { SetPrefValue(original_setting_); } |
| 44 |
| 45 private: |
| 46 // Gets the current value of the preference. |
| 47 ContentSetting GetPrefValue() { |
| 48 ContentSetting popupSetting = |
| 49 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 50 chrome_test_util::GetOriginalBrowserState()) |
| 51 ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, NULL); |
| 52 return popupSetting; |
| 53 } |
| 54 |
| 55 // Sets the preference to the given value. |
| 56 void SetPrefValue(ContentSetting setting) { |
| 57 DCHECK(setting == CONTENT_SETTING_BLOCK || |
| 58 setting == CONTENT_SETTING_ALLOW); |
| 59 ios::ChromeBrowserState* state = |
| 60 chrome_test_util::GetOriginalBrowserState(); |
| 61 ios::HostContentSettingsMapFactory::GetForBrowserState(state) |
| 62 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, setting); |
| 63 } |
| 64 |
| 65 // Saves the original pref setting so that it can be restored when the scoper |
| 66 // is destroyed. |
| 67 ContentSetting original_setting_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(ScopedBlockPopupsPref); |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 // Tests that the javascript preventDefault() function correctly prevents new |
| 75 // tabs from opening or navigation from occurring. |
| 76 @interface BrowsingPreventDefaultTestCase : ChromeTestCase |
| 77 @end |
| 78 |
| 79 @implementation BrowsingPreventDefaultTestCase |
| 80 |
| 81 // Helper function to tap a link and verify that the URL did not change and no |
| 82 // new tabs were opened. |
| 83 - (void)runTestAndVerifyNoNavigationForLinkID:(const std::string&)linkID { |
| 84 // Disable popup blocking, because that will mask failures that try to open |
| 85 // new tabs. |
| 86 ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW); |
| 87 web::test::SetUpFileBasedHttpServer(); |
| 88 |
| 89 const GURL testURL = GetTestUrl(); |
| 90 [ChromeEarlGrey loadURL:testURL]; |
| 91 chrome_test_util::AssertMainTabCount(1U); |
| 92 |
| 93 // Tap on the test link and wait for the page to display "Click done", as an |
| 94 // indicator that the element was tapped. |
| 95 chrome_test_util::TapWebViewElementWithId(linkID); |
| 96 [[GREYCondition |
| 97 conditionWithName:@"Waiting for webview to display 'Click done'." |
| 98 block:^BOOL { |
| 99 id<GREYMatcher> webViewMatcher = |
| 100 chrome_test_util::webViewContainingText("Click done"); |
| 101 NSError* error = nil; |
| 102 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 103 assertWithMatcher:grey_notNil() |
| 104 error:&error]; |
| 105 return error == nil; |
| 106 }] waitWithTimeout:kConditionTimeout]; |
| 107 |
| 108 // Check that no navigation occurred and no new tabs were opened. |
| 109 chrome_test_util::AssertMainTabCount(1U); |
| 110 const GURL& currentURL = |
| 111 chrome_test_util::GetCurrentWebState()->GetVisibleURL(); |
| 112 GREYAssert(currentURL == testURL, |
| 113 [NSString stringWithFormat:@"Page navigated unexpectedly %s", |
| 114 currentURL.spec().c_str()]); |
| 115 } |
| 116 |
| 117 // Taps a link with onclick="event.preventDefault()" and target="_blank" and |
| 118 // verifies that the URL didn't change and no tabs were opened. |
| 119 - (void)testPreventDefaultOverridesTargetBlank { |
| 120 const std::string linkID = |
| 121 "webScenarioBrowsingLinkPreventDefaultOverridesTargetBlank"; |
| 122 [self runTestAndVerifyNoNavigationForLinkID:linkID]; |
| 123 } |
| 124 |
| 125 // Tests clicking a link with target="_blank" and event 'preventDefault()' and |
| 126 // 'stopPropagation()' does not change the current URL nor open a new tab. |
| 127 - (void)testPreventDefaultOverridesStopPropagation { |
| 128 const std::string linkID = |
| 129 "webScenarioBrowsingLinkPreventDefaultOverridesStopPropagation"; |
| 130 [self runTestAndVerifyNoNavigationForLinkID:linkID]; |
| 131 } |
| 132 |
| 133 // Tests clicking a link with event 'preventDefault()' and URL loaded by |
| 134 // JavaScript does not open a new tab, but does navigate to the URL. |
| 135 - (void)testPreventDefaultOverridesWindowOpen { |
| 136 // Disable popup blocking, because that will mask failures that try to open |
| 137 // new tabs. |
| 138 ScopedBlockPopupsPref scoper(CONTENT_SETTING_ALLOW); |
| 139 web::test::SetUpFileBasedHttpServer(); |
| 140 |
| 141 const GURL testURL = GetTestUrl(); |
| 142 [ChromeEarlGrey loadURL:testURL]; |
| 143 chrome_test_util::AssertMainTabCount(1U); |
| 144 |
| 145 // Tap on the test link. |
| 146 const std::string linkID = |
| 147 "webScenarioBrowsingLinkPreventDefaultOverridesWindowOpen"; |
| 148 chrome_test_util::TapWebViewElementWithId(linkID); |
| 149 |
| 150 // Stall a bit to make sure the webview doesn't do anything asynchronously. |
| 151 [[GREYCondition |
| 152 conditionWithName:@"Waiting for webview to display 'Click done'." |
| 153 block:^BOOL { |
| 154 id<GREYMatcher> webViewMatcher = |
| 155 chrome_test_util::webViewContainingText("Click done"); |
| 156 NSError* error = nil; |
| 157 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 158 assertWithMatcher:grey_notNil() |
| 159 error:&error]; |
| 160 return error == nil; |
| 161 }] waitWithTimeout:kConditionTimeout]; |
| 162 |
| 163 // Check that the tab navigated to about:blank and no new tabs were opened. |
| 164 [[GREYCondition |
| 165 conditionWithName:@"Wait for navigation to about:blank" |
| 166 block:^BOOL { |
| 167 const GURL& currentURL = |
| 168 chrome_test_util::GetCurrentWebState()->GetVisibleURL(); |
| 169 return currentURL == GURL(url::kAboutBlankURL); |
| 170 }] waitWithTimeout:kConditionTimeout]; |
| 171 chrome_test_util::AssertMainTabCount(1U); |
| 172 } |
| 173 |
| 174 @end |
OLD | NEW |