| 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 #import <UIKit/UIKit.h> | 6 #import <UIKit/UIKit.h> |
| 7 #import <XCTest/XCTest.h> | 7 #import <XCTest/XCTest.h> |
| 8 | 8 |
| 9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 33 | 33 |
| 34 using chrome_test_util::NavigationBarDoneButton; | 34 using chrome_test_util::NavigationBarDoneButton; |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 // URLs used in the tests. | 38 // URLs used in the tests. |
| 39 const char* kBlockPopupsUrl = "http://blockpopups"; | 39 const char* kBlockPopupsUrl = "http://blockpopups"; |
| 40 const char* kOpenedWindowUrl = "http://openedwindow"; | 40 const char* kOpenedWindowUrl = "http://openedwindow"; |
| 41 | 41 |
| 42 // JavaScript to open a new window after a short delay. | 42 // Page with a button that opens a new window after a short delay. |
| 43 NSString* kBlockPopupsResponseTemplate = | 43 NSString* kBlockPopupsResponseTemplate = |
| 44 @"<input type=\"button\" onclick=\"setTimeout(function() {" | 44 @"<input type=\"button\" onclick=\"setTimeout(function() {" |
| 45 "window.open('%@')}, 1)\" " | 45 "window.open('%@')}, 1)\" " |
| 46 "id=\"openWindow\" " | 46 "id=\"open-window\" " |
| 47 "value=\"openWindow\">"; | 47 "value=\"openWindow\">"; |
| 48 // JavaScript that clicks that button. |
| 49 NSString* kOpenPopupScript = @"document.getElementById('open-window').click()"; |
| 48 const std::string kOpenedWindowResponse = "Opened window"; | 50 const std::string kOpenedWindowResponse = "Opened window"; |
| 49 | 51 |
| 50 // Opens the block popups settings page. Must be called from the NTP. | 52 // Opens the block popups settings page. Must be called from the NTP. |
| 51 void OpenBlockPopupsSettings() { | 53 void OpenBlockPopupsSettings() { |
| 52 const CGFloat scroll_displacement = 50.0; | 54 const CGFloat scroll_displacement = 50.0; |
| 53 id<GREYMatcher> tools_menu_table_view_matcher = | 55 id<GREYMatcher> tools_menu_table_view_matcher = |
| 54 grey_accessibilityID(kToolsMenuTableViewId); | 56 grey_accessibilityID(kToolsMenuTableViewId); |
| 55 id<GREYMatcher> settings_button_matcher = | 57 id<GREYMatcher> settings_button_matcher = |
| 56 grey_accessibilityID(kToolsMenuSettingsId); | 58 grey_accessibilityID(kToolsMenuSettingsId); |
| 57 id<GREYMatcher> content_settings_button_matcher = | 59 id<GREYMatcher> content_settings_button_matcher = |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 base::SysUTF8ToNSString(openedWindowURL.spec()); | 196 base::SysUTF8ToNSString(openedWindowURL.spec()); |
| 195 responses[blockPopupsURL] = base::SysNSStringToUTF8([NSString | 197 responses[blockPopupsURL] = base::SysNSStringToUTF8([NSString |
| 196 stringWithFormat:kBlockPopupsResponseTemplate, openedWindowURLString]); | 198 stringWithFormat:kBlockPopupsResponseTemplate, openedWindowURLString]); |
| 197 responses[openedWindowURL] = kOpenedWindowResponse; | 199 responses[openedWindowURL] = kOpenedWindowResponse; |
| 198 web::test::SetUpSimpleHttpServer(responses); | 200 web::test::SetUpSimpleHttpServer(responses); |
| 199 | 201 |
| 200 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); | 202 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); |
| 201 [ChromeEarlGrey loadURL:blockPopupsURL]; | 203 [ChromeEarlGrey loadURL:blockPopupsURL]; |
| 202 chrome_test_util::AssertMainTabCount(1U); | 204 chrome_test_util::AssertMainTabCount(1U); |
| 203 | 205 |
| 204 // Tap the "Open Window" link and make sure the popup opened in a new tab. | 206 // Request popup and make sure the popup opened in a new tab. |
| 205 chrome_test_util::TapWebViewElementWithId("openWindow"); | 207 NSError* error = nil; |
| 208 chrome_test_util::ExecuteJavaScript(kOpenPopupScript, &error); |
| 209 GREYAssert(!error, @"Error during script execution: %@", error); |
| 206 chrome_test_util::AssertMainTabCount(2U); | 210 chrome_test_util::AssertMainTabCount(2U); |
| 207 | 211 |
| 208 // No infobar should be displayed. | 212 // No infobar should be displayed. |
| 209 [[EarlGrey selectElementWithMatcher:chrome_test_util:: | 213 [[EarlGrey selectElementWithMatcher:chrome_test_util:: |
| 210 StaticTextWithAccessibilityLabel( | 214 StaticTextWithAccessibilityLabel( |
| 211 @"Pop-ups blocked (1)")] | 215 @"Pop-ups blocked (1)")] |
| 212 assertWithMatcher:grey_notVisible()]; | 216 assertWithMatcher:grey_notVisible()]; |
| 213 } | 217 } |
| 214 | 218 |
| 215 // Tests that popups are prevented from opening and an infobar is displayed when | 219 // Tests that popups are prevented from opening and an infobar is displayed when |
| 216 // the preference is set to BLOCK. | 220 // the preference is set to BLOCK. |
| 217 - (void)testPopupsBlocked { | 221 - (void)testPopupsBlocked { |
| 218 std::map<GURL, std::string> responses; | 222 std::map<GURL, std::string> responses; |
| 219 const GURL blockPopupsURL = web::test::HttpServer::MakeUrl(kBlockPopupsUrl); | 223 const GURL blockPopupsURL = web::test::HttpServer::MakeUrl(kBlockPopupsUrl); |
| 220 const GURL openedWindowURL = web::test::HttpServer::MakeUrl(kOpenedWindowUrl); | 224 const GURL openedWindowURL = web::test::HttpServer::MakeUrl(kOpenedWindowUrl); |
| 221 NSString* openedWindowURLString = | 225 NSString* openedWindowURLString = |
| 222 base::SysUTF8ToNSString(openedWindowURL.spec()); | 226 base::SysUTF8ToNSString(openedWindowURL.spec()); |
| 223 responses[blockPopupsURL] = base::SysNSStringToUTF8([NSString | 227 responses[blockPopupsURL] = base::SysNSStringToUTF8([NSString |
| 224 stringWithFormat:kBlockPopupsResponseTemplate, openedWindowURLString]); | 228 stringWithFormat:kBlockPopupsResponseTemplate, openedWindowURLString]); |
| 225 responses[openedWindowURL] = kOpenedWindowResponse; | 229 responses[openedWindowURL] = kOpenedWindowResponse; |
| 226 web::test::SetUpSimpleHttpServer(responses); | 230 web::test::SetUpSimpleHttpServer(responses); |
| 227 | 231 |
| 228 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK); | 232 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_BLOCK); |
| 229 [ChromeEarlGrey loadURL:blockPopupsURL]; | 233 [ChromeEarlGrey loadURL:blockPopupsURL]; |
| 230 chrome_test_util::AssertMainTabCount(1U); | 234 chrome_test_util::AssertMainTabCount(1U); |
| 231 | 235 |
| 232 // Tap the "Open Window" link, then make sure the popup was blocked and an | 236 // Request popup, then make sure it was blocked and an infobar was displayed. |
| 233 // infobar was displayed. The window.open() call is run via async JS, so the | 237 // The window.open() call is run via async JS, so the infobar may not open |
| 234 // infobar may not open immediately. | 238 // immediately. |
| 235 chrome_test_util::TapWebViewElementWithId("openWindow"); | 239 NSError* error = nil; |
| 240 chrome_test_util::ExecuteJavaScript(kOpenPopupScript, &error); |
| 241 GREYAssert(!error, @"Error during script execution: %@", error); |
| 242 |
| 236 [[GREYCondition | 243 [[GREYCondition |
| 237 conditionWithName:@"Wait for blocked popups infobar to show" | 244 conditionWithName:@"Wait for blocked popups infobar to show" |
| 238 block:^BOOL { | 245 block:^BOOL { |
| 239 NSError* error = nil; | 246 NSError* error = nil; |
| 240 [[EarlGrey | 247 [[EarlGrey |
| 241 selectElementWithMatcher: | 248 selectElementWithMatcher: |
| 242 chrome_test_util::StaticTextWithAccessibilityLabel( | 249 chrome_test_util::StaticTextWithAccessibilityLabel( |
| 243 @"Pop-ups blocked (1)")] | 250 @"Pop-ups blocked (1)")] |
| 244 assertWithMatcher:grey_sufficientlyVisible() | 251 assertWithMatcher:grey_sufficientlyVisible() |
| 245 error:&error]; | 252 error:&error]; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 assertWithMatcher:grey_sufficientlyVisible()]; | 296 assertWithMatcher:grey_sufficientlyVisible()]; |
| 290 [[EarlGrey | 297 [[EarlGrey |
| 291 selectElementWithMatcher:grey_accessibilityLabel(l10n_util::GetNSString( | 298 selectElementWithMatcher:grey_accessibilityLabel(l10n_util::GetNSString( |
| 292 IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON))] | 299 IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON))] |
| 293 assertWithMatcher:grey_sufficientlyVisible()]; | 300 assertWithMatcher:grey_sufficientlyVisible()]; |
| 294 | 301 |
| 295 CloseSettings(); | 302 CloseSettings(); |
| 296 } | 303 } |
| 297 | 304 |
| 298 @end | 305 @end |
| OLD | NEW |