| 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 #import "base/strings/sys_string_conversions.h" | 9 #import "base/strings/sys_string_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 [[EarlGrey selectElementWithMatcher:titleLabel] | 193 [[EarlGrey selectElementWithMatcher:titleLabel] |
| 194 assertWithMatcher:grey_notNil() | 194 assertWithMatcher:grey_notNil() |
| 195 error:&error]; | 195 error:&error]; |
| 196 return !error; | 196 return !error; |
| 197 }; | 197 }; |
| 198 GREYAssert(testing::WaitUntilConditionOrTimeout( | 198 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 199 testing::kWaitForUIElementTimeout, condition), | 199 testing::kWaitForUIElementTimeout, condition), |
| 200 @"Alert with title was not present: %@", alert_label); | 200 @"Alert with title was not present: %@", alert_label); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void WaitForJavaScripDialogToBeShown() { | 203 void WaitForJavaScriptDialogToBeShown() { |
| 204 NSString* alert_label = [DialogPresenter | 204 GURL javaScriptURL = HttpServer::MakeUrl(kJavaScriptTestURL); |
| 205 localizedTitleForJavaScriptAlertFromPage:HttpServer::MakeUrl( | 205 |
| 206 kJavaScriptTestURL)]; | 206 NSString* hostname = base::SysUTF8ToNSString(javaScriptURL.host()); |
| 207 WaitForAlertToBeShown(alert_label); | 207 NSString* expectedTitle = l10n_util::GetNSStringF( |
| 208 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); |
| 209 |
| 210 WaitForAlertToBeShown(expectedTitle); |
| 208 } | 211 } |
| 209 | 212 |
| 210 // Injects JavaScript to show a dialog with |type|, verifying that it was | 213 // Injects JavaScript to show a dialog with |type|, verifying that it was |
| 211 // properly displayed. | 214 // properly displayed. |
| 212 void ShowJavaScriptDialog(JavaScriptAlertType type) { | 215 void ShowJavaScriptDialog(JavaScriptAlertType type) { |
| 213 DisplayJavaScriptAlert(type); | 216 DisplayJavaScriptAlert(type); |
| 214 | 217 |
| 215 WaitForJavaScripDialogToBeShown(); | 218 WaitForJavaScriptDialogToBeShown(); |
| 216 | 219 |
| 217 // Check the message of the alert. | 220 // Check the message of the alert. |
| 218 id<GREYMatcher> messageLabel = | 221 id<GREYMatcher> messageLabel = |
| 219 chrome_test_util::StaticTextWithAccessibilityLabel( | 222 chrome_test_util::StaticTextWithAccessibilityLabel( |
| 220 GetMessageForAlertWithType(type)); | 223 GetMessageForAlertWithType(type)); |
| 221 [[EarlGrey selectElementWithMatcher:messageLabel] | 224 [[EarlGrey selectElementWithMatcher:messageLabel] |
| 222 assertWithMatcher:grey_notNil()]; | 225 assertWithMatcher:grey_notNil()]; |
| 223 } | 226 } |
| 224 | 227 |
| 225 // Assert no javascript alert is visible. | 228 // Assert no javascript alert is visible. |
| 226 void AssertJavaScriptAlertNotPresent() { | 229 void AssertJavaScriptAlertNotPresent() { |
| 227 ConditionBlock condition = ^{ | 230 ConditionBlock condition = ^{ |
| 228 NSError* error = nil; | 231 NSError* error = nil; |
| 229 NSString* alertLabel = [DialogPresenter | 232 GURL javaScriptURL = HttpServer::MakeUrl(kJavaScriptTestURL); |
| 230 localizedTitleForJavaScriptAlertFromPage:HttpServer::MakeUrl( | 233 NSString* hostname = base::SysUTF8ToNSString(javaScriptURL.host()); |
| 231 kJavaScriptTestURL)]; | 234 NSString* expectedTitle = l10n_util::GetNSStringF( |
| 235 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); |
| 236 |
| 232 id<GREYMatcher> titleLabel = | 237 id<GREYMatcher> titleLabel = |
| 233 chrome_test_util::StaticTextWithAccessibilityLabel(alertLabel); | 238 chrome_test_util::StaticTextWithAccessibilityLabel(expectedTitle); |
| 234 [[EarlGrey selectElementWithMatcher:titleLabel] assertWithMatcher:grey_nil() | 239 [[EarlGrey selectElementWithMatcher:titleLabel] assertWithMatcher:grey_nil() |
| 235 error:&error]; | 240 error:&error]; |
| 236 return !error; | 241 return !error; |
| 237 }; | 242 }; |
| 238 | 243 |
| 239 GREYAssert(testing::WaitUntilConditionOrTimeout( | 244 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 240 testing::kWaitForJSCompletionTimeout, condition), | 245 testing::kWaitForJSCompletionTimeout, condition), |
| 241 @"Javascript alert title was still present"); | 246 @"Javascript alert title was still present"); |
| 242 } | 247 } |
| 243 | 248 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 [ChromeEarlGrey waitForWebViewContainingText:kPromptResultBodyCancelled]; | 441 [ChromeEarlGrey waitForWebViewContainingText:kPromptResultBodyCancelled]; |
| 437 } | 442 } |
| 438 | 443 |
| 439 // Tests that JavaScript alerts that are shown in a loop can be suppressed. | 444 // Tests that JavaScript alerts that are shown in a loop can be suppressed. |
| 440 - (void)testShowJavaScriptAlertLoop { | 445 - (void)testShowJavaScriptAlertLoop { |
| 441 // Load the blank test page and show alerts in a loop. | 446 // Load the blank test page and show alerts in a loop. |
| 442 [self loadBlankTestPage]; | 447 [self loadBlankTestPage]; |
| 443 web::WebState* webState = chrome_test_util::GetCurrentWebState(); | 448 web::WebState* webState = chrome_test_util::GetCurrentWebState(); |
| 444 NSString* script = GetJavaScriptAlertLoopScript(); | 449 NSString* script = GetJavaScriptAlertLoopScript(); |
| 445 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); | 450 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); |
| 446 WaitForJavaScripDialogToBeShown(); | 451 WaitForJavaScriptDialogToBeShown(); |
| 447 | 452 |
| 448 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; | 453 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; |
| 449 WaitForJavaScripDialogToBeShown(); | 454 WaitForJavaScriptDialogToBeShown(); |
| 450 | 455 |
| 451 // Tap the suppress dialogs button. | 456 // Tap the suppress dialogs button. |
| 452 TapSuppressDialogsButton(); | 457 TapSuppressDialogsButton(); |
| 453 | 458 |
| 454 // Wait for confirmation action sheet to be shown. | 459 // Wait for confirmation action sheet to be shown. |
| 455 NSString* alertLabel = | 460 NSString* alertLabel = |
| 456 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | 461 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
| 457 WaitForAlertToBeShown(alertLabel); | 462 WaitForAlertToBeShown(alertLabel); |
| 458 | 463 |
| 459 // Tap the suppress dialogs confirmation button. | 464 // Tap the suppress dialogs confirmation button. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 488 DisplayJavaScriptAlert(JavaScriptAlertType::ALERT); | 493 DisplayJavaScriptAlert(JavaScriptAlertType::ALERT); |
| 489 | 494 |
| 490 // Make sure the alert is not present. | 495 // Make sure the alert is not present. |
| 491 AssertJavaScriptAlertNotPresent(); | 496 AssertJavaScriptAlertNotPresent(); |
| 492 | 497 |
| 493 // Close the settings. | 498 // Close the settings. |
| 494 [[EarlGrey selectElementWithMatcher:NavigationBarDoneButton()] | 499 [[EarlGrey selectElementWithMatcher:NavigationBarDoneButton()] |
| 495 performAction:grey_tap()]; | 500 performAction:grey_tap()]; |
| 496 | 501 |
| 497 // Make sure the alert is present. | 502 // Make sure the alert is present. |
| 498 WaitForJavaScripDialogToBeShown(); | 503 WaitForJavaScriptDialogToBeShown(); |
| 499 | 504 |
| 500 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; | 505 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; |
| 501 | 506 |
| 502 // Wait for the html body to be reset to the correct value. | 507 // Wait for the html body to be reset to the correct value. |
| 503 [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; | 508 [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; |
| 504 } | 509 } |
| 505 | 510 |
| 506 // Tests that an alert is presented after displaying the share menu. | 511 // Tests that an alert is presented after displaying the share menu. |
| 507 - (void)testShowJavaScriptAfterShareMenu { | 512 - (void)testShowJavaScriptAfterShareMenu { |
| 508 // TODO(crbug.com/663026): Reenable the test for devices. | 513 // TODO(crbug.com/663026): Reenable the test for devices. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 // continues to animate until the dialog is closed. Disabling EarlGrey | 572 // continues to animate until the dialog is closed. Disabling EarlGrey |
| 568 // synchronization code for iPad allows the test to detect and dismiss the | 573 // synchronization code for iPad allows the test to detect and dismiss the |
| 569 // dialog while this animation is occurring. | 574 // dialog while this animation is occurring. |
| 570 if (IsIPadIdiom()) { | 575 if (IsIPadIdiom()) { |
| 571 [[GREYConfiguration sharedInstance] | 576 [[GREYConfiguration sharedInstance] |
| 572 setValue:@(NO) | 577 setValue:@(NO) |
| 573 forConfigKey:kGREYConfigKeySynchronizationEnabled]; | 578 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 574 } | 579 } |
| 575 | 580 |
| 576 // Wait for the alert to be shown. | 581 // Wait for the alert to be shown. |
| 577 NSString* alertLabel = [DialogPresenter | 582 GURL javaScriptURL = HttpServer::MakeUrl(kJavaScriptTestURL); |
| 578 localizedTitleForJavaScriptAlertFromPage:HttpServer::MakeUrl( | 583 NSString* hostname = base::SysUTF8ToNSString(javaScriptURL.host()); |
| 579 kJavaScriptTestURL)]; | 584 NSString* expectedTitle = l10n_util::GetNSStringF( |
| 580 WaitForAlertToBeShown(alertLabel); | 585 IDS_JAVASCRIPT_MESSAGEBOX_TITLE, base::SysNSStringToUTF16(hostname)); |
| 586 |
| 587 WaitForAlertToBeShown(expectedTitle); |
| 581 | 588 |
| 582 // Verify that the omnibox shows the correct URL when the dialog is visible. | 589 // Verify that the omnibox shows the correct URL when the dialog is visible. |
| 583 GURL onloadURL = HttpServer::MakeUrl(kOnLoadAlertURL); | 590 GURL onloadURL = HttpServer::MakeUrl(kOnLoadAlertURL); |
| 584 std::string title = base::UTF16ToUTF8(web::GetDisplayTitleForUrl(onloadURL)); | 591 std::string title = base::UTF16ToUTF8(web::GetDisplayTitleForUrl(onloadURL)); |
| 585 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(title)] | 592 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText(title)] |
| 586 assertWithMatcher:grey_notNil()]; | 593 assertWithMatcher:grey_notNil()]; |
| 587 | 594 |
| 588 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; | 595 [[EarlGrey selectElementWithMatcher:OKButton()] performAction:grey_tap()]; |
| 589 | 596 |
| 590 // Reenable synchronization on iPads now that the dialog has been dismissed. | 597 // Reenable synchronization on iPads now that the dialog has been dismissed. |
| 591 if (IsIPadIdiom()) { | 598 if (IsIPadIdiom()) { |
| 592 [[GREYConfiguration sharedInstance] | 599 [[GREYConfiguration sharedInstance] |
| 593 setValue:@(YES) | 600 setValue:@(YES) |
| 594 forConfigKey:kGREYConfigKeySynchronizationEnabled]; | 601 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 595 } | 602 } |
| 596 } | 603 } |
| 597 | 604 |
| 598 @end | 605 @end |
| OLD | NEW |