Chromium Code Reviews| 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" |
| 11 #include "components/strings/grit/components_strings.h" | 11 #include "components/strings/grit/components_strings.h" |
| 12 #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" | 12 #import "ios/chrome/browser/ui/dialogs/dialog_presenter.h" |
| 13 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" | 13 #include "ios/chrome/browser/ui/tools_menu/tools_menu_constants.h" |
| 14 #include "ios/chrome/browser/ui/ui_util.h" | 14 #include "ios/chrome/browser/ui/ui_util.h" |
| 15 #include "ios/chrome/grit/ios_strings.h" | 15 #include "ios/chrome/grit/ios_strings.h" |
| 16 #include "ios/chrome/test/app/chrome_test_util.h" | 16 #include "ios/chrome/test/app/chrome_test_util.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_actions.h" | 17 #import "ios/chrome/test/earl_grey/chrome_actions.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" | 19 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 20 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 21 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 21 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 22 #import "ios/testing/earl_grey/matchers.h" | 22 #import "ios/testing/earl_grey/matchers.h" |
| 23 #import "ios/testing/wait_util.h" | 23 #import "ios/testing/wait_util.h" |
| 24 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | |
| 24 #import "ios/web/public/test/http_server.h" | 25 #import "ios/web/public/test/http_server.h" |
| 25 #import "ios/web/public/test/http_server_util.h" | 26 #import "ios/web/public/test/http_server_util.h" |
| 26 #include "ios/web/public/test/url_test_util.h" | 27 #include "ios/web/public/test/url_test_util.h" |
| 27 #include "ios/web/public/web_state/web_state.h" | 28 #include "ios/web/public/web_state/web_state.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/l10n/l10n_util_mac.h" | 30 #include "ui/base/l10n/l10n_util_mac.h" |
| 30 #import "url/gurl.h" | 31 #import "url/gurl.h" |
| 31 | 32 |
| 32 #if !defined(__has_feature) || !__has_feature(objc_arc) | 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 33 #error "This file requires ARC support." | 34 #error "This file requires ARC support." |
| 34 #endif | 35 #endif |
| 35 | 36 |
| 36 using chrome_test_util::NavigationBarDoneButton; | 37 using chrome_test_util::NavigationBarDoneButton; |
| 37 using web::test::HttpServer; | 38 using web::test::HttpServer; |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 // Enum specifying different types of JavaScript alerts: | 42 // Enum specifying different types of JavaScript alerts: |
| 42 // - JavaScriptAlertType::ALERT - Dialog with only one OK button. | 43 // - JavaScriptAlertType::ALERT - Dialog with only one OK button. |
| 43 // - JavaScriptAlertType::CONFIRMATION - Dialog with OK and Cancel button. | 44 // - JavaScriptAlertType::CONFIRMATION - Dialog with OK and Cancel button. |
| 44 // - JavaScriptAlertType::PROMPT - Dialog with OK button, cancel button, and | 45 // - JavaScriptAlertType::PROMPT - Dialog with OK button, cancel button, and |
| 45 // a text field. | 46 // a text field. |
| 46 enum class JavaScriptAlertType : NSUInteger { ALERT, CONFIRMATION, PROMPT }; | 47 enum class JavaScriptAlertType : NSUInteger { ALERT, CONFIRMATION, PROMPT }; |
| 47 | 48 |
| 48 // Script to inject that will show an alert. The document's body will be reset | 49 // Script to inject that will show an alert. The document's body will be reset |
| 49 // to |kAlertResultBody| after the dialog is dismissed. | 50 // to |kAlertResultBody| after the dialog is dismissed. |
| 50 const char kAlertMessage[] = "This is a JavaScript alert."; | 51 NSString* const kAlertMessage = @"This is a JavaScript alert."; |
| 51 const char kAlertResultBody[] = "JAVASCRIPT ALERT WAS DISMISSED"; | 52 NSString* const kAlertResultBody = @"JAVASCRIPT ALERT WAS DISMISSED"; |
| 52 const char kJavaScriptAlertTestScriptFormat[] = | 53 NSString* const kJavaScriptAlertTestScriptFormat = |
| 53 "(function(){ " | 54 @"(function(){ " |
| 54 " alert(\"%@\");" | 55 " alert(\"%@\");" |
| 55 " document.body.innerHTML = \"%@\";" | 56 " document.body.innerHTML = \"%@\";" |
| 56 "})();"; | 57 "})();"; |
| 57 NSString* GetJavaScriptAlertTestScript() { | 58 NSString* GetJavaScriptAlertTestScript() { |
| 58 return [NSString stringWithFormat:@(kJavaScriptAlertTestScriptFormat), | 59 return [NSString stringWithFormat:kJavaScriptAlertTestScriptFormat, |
| 59 @(kAlertMessage), @(kAlertResultBody)]; | 60 kAlertMessage, kAlertResultBody]; |
| 60 } | 61 } |
| 61 | 62 |
| 62 // Script to inject that will show a confirmation dialog. The document's body | 63 // Script to inject that will show a confirmation dialog. The document's body |
| 63 // will be reset to |kConfirmationResultBodyOK| or | 64 // will be reset to |kConfirmationResultBodyOK| or |
| 64 // |kConfirmationResultBodyCancelled| depending on whether the OK or Cancel | 65 // |kConfirmationResultBodyCancelled| depending on whether the OK or Cancel |
| 65 // button was tapped. | 66 // button was tapped. |
| 66 const char kConfirmationMessage[] = "This is a JavaScript confirmation."; | 67 NSString* const kConfirmationMessage = @"This is a JavaScript confirmation."; |
| 67 const char kConfirmationResultBodyOK[] = "Okay"; | 68 NSString* const kConfirmationResultBodyOK = @"Okay"; |
| 68 const char kConfirmationResultBodyCancelled[] = "Cancelled"; | 69 NSString* const kConfirmationResultBodyCancelled = @"Cancelled"; |
| 69 const char kJavaScriptConfirmationScriptFormat[] = | 70 NSString* const kJavaScriptConfirmationScriptFormat = |
| 70 "(function(){ " | 71 @"(function(){ " |
| 71 " if (confirm(\"%@\") == true) {" | 72 " if (confirm(\"%@\") == true) {" |
| 72 " document.body.innerHTML = \"%@\";" | 73 " document.body.innerHTML = \"%@\";" |
| 73 " } else {" | 74 " } else {" |
| 74 " document.body.innerHTML = \"%@\";" | 75 " document.body.innerHTML = \"%@\";" |
| 75 " }" | 76 " }" |
| 76 "})();"; | 77 "})();"; |
| 77 NSString* GetJavaScriptConfirmationTestScript() { | 78 NSString* GetJavaScriptConfirmationTestScript() { |
| 78 return [NSString stringWithFormat:@(kJavaScriptConfirmationScriptFormat), | 79 return [NSString stringWithFormat:kJavaScriptConfirmationScriptFormat, |
| 79 @(kConfirmationMessage), | 80 kConfirmationMessage, |
| 80 @(kConfirmationResultBodyOK), | 81 kConfirmationResultBodyOK, |
| 81 @(kConfirmationResultBodyCancelled)]; | 82 kConfirmationResultBodyCancelled]; |
| 82 } | 83 } |
| 83 | 84 |
| 84 // Script to inject that will show a prompt dialog. The document's body will be | 85 // Script to inject that will show a prompt dialog. The document's body will be |
| 85 // reset to |kPromptResultBodyCancelled| or |kPromptTestUserInput| depending on | 86 // reset to |kPromptResultBodyCancelled| or |kPromptTestUserInput| depending on |
| 86 // whether the OK or Cancel button was tapped. | 87 // whether the OK or Cancel button was tapped. |
| 87 const char kPromptMessage[] = "This is a JavaScript prompt."; | 88 NSString* const kPromptMessage = @"This is a JavaScript prompt."; |
| 88 const char kPromptResultBodyCancelled[] = "Cancelled"; | 89 NSString* const kPromptResultBodyCancelled = @"Cancelled"; |
| 89 const char kPromptTestUserInput[] = "test"; | 90 NSString* const kPromptTestUserInput = @"test"; |
| 90 const char kJavaScriptPromptTestScriptFormat[] = | 91 NSString* const kJavaScriptPromptTestScriptFormat = |
| 91 "(function(){ " | 92 @"(function(){ " |
| 92 " var input = prompt(\"%@\");" | 93 " var input = prompt(\"%@\");" |
| 93 " if (input != null) {" | 94 " if (input != null) {" |
| 94 " document.body.innerHTML = input;" | 95 " document.body.innerHTML = input;" |
| 95 " } else {" | 96 " } else {" |
| 96 " document.body.innerHTML = \"%@\";" | 97 " document.body.innerHTML = \"%@\";" |
| 97 " }" | 98 " }" |
| 98 "})();"; | 99 "})();"; |
| 99 NSString* GetJavaScriptPromptTestScript() { | 100 NSString* GetJavaScriptPromptTestScript() { |
| 100 return [NSString stringWithFormat:@(kJavaScriptPromptTestScriptFormat), | 101 return [NSString stringWithFormat:kJavaScriptPromptTestScriptFormat, |
| 101 @(kPromptMessage), | 102 kPromptMessage, kPromptResultBodyCancelled]; |
| 102 @(kPromptResultBodyCancelled)]; | |
| 103 } | 103 } |
| 104 | 104 |
| 105 // Script to inject that will show a JavaScript alert in a loop 20 times, then | 105 // Script to inject that will show a JavaScript alert in a loop 20 times, then |
| 106 // reset the document's HTML to |kAlertLoopFinishedText|. | 106 // reset the document's HTML to |kAlertLoopFinishedText|. |
| 107 const char kAlertLoopFinishedText[] = "Loop Finished"; | 107 NSString* const kAlertLoopFinishedText = @"Loop Finished"; |
| 108 const char kJavaScriptAlertLoopScriptFormat[] = | 108 NSString* const kJavaScriptAlertLoopScriptFormat = |
| 109 "(function(){ " | 109 @"(function(){ " |
| 110 " for (i = 0; i < 20; ++i) {" | 110 " for (i = 0; i < 20; ++i) {" |
| 111 " alert(\"ALERT TEXT\");" | 111 " alert(\"ALERT TEXT\");" |
| 112 " }" | 112 " }" |
| 113 " document.body.innerHTML = \"%@\";" | 113 " document.body.innerHTML = \"%@\";" |
| 114 "})();"; | 114 "})();"; |
| 115 NSString* GetJavaScriptAlertLoopScript() { | 115 NSString* GetJavaScriptAlertLoopScript() { |
| 116 return [NSString stringWithFormat:@(kJavaScriptAlertLoopScriptFormat), | 116 return [NSString stringWithFormat:kJavaScriptAlertLoopScriptFormat, |
| 117 @(kAlertLoopFinishedText)]; | 117 kAlertLoopFinishedText]; |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Returns the message for a JavaScript alert with |type|. | 120 // Returns the message for a JavaScript alert with |type|. |
| 121 NSString* GetMessageForAlertWithType(JavaScriptAlertType type) { | 121 NSString* GetMessageForAlertWithType(JavaScriptAlertType type) { |
| 122 switch (type) { | 122 switch (type) { |
| 123 case JavaScriptAlertType::ALERT: | 123 case JavaScriptAlertType::ALERT: |
| 124 return @(kAlertMessage); | 124 return kAlertMessage; |
| 125 case JavaScriptAlertType::CONFIRMATION: | 125 case JavaScriptAlertType::CONFIRMATION: |
| 126 return @(kConfirmationMessage); | 126 return kConfirmationMessage; |
| 127 case JavaScriptAlertType::PROMPT: | 127 case JavaScriptAlertType::PROMPT: |
| 128 return @(kPromptMessage); | 128 return kPromptMessage; |
| 129 } | 129 } |
| 130 GREYFail(@"JavascriptAlertType not recognized."); | 130 GREYFail(@"JavascriptAlertType not recognized."); |
| 131 return nil; | 131 return nil; |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Returns the script to show a JavaScript alert with |type|. | 134 // Returns the script to show a JavaScript alert with |type|. |
| 135 NSString* GetScriptForAlertWithType(JavaScriptAlertType type) { | 135 NSString* GetScriptForAlertWithType(JavaScriptAlertType type) { |
| 136 switch (type) { | 136 switch (type) { |
| 137 case JavaScriptAlertType::ALERT: | 137 case JavaScriptAlertType::ALERT: |
| 138 return GetJavaScriptAlertTestScript(); | 138 return GetJavaScriptAlertTestScript(); |
| 139 case JavaScriptAlertType::CONFIRMATION: | 139 case JavaScriptAlertType::CONFIRMATION: |
| 140 return GetJavaScriptConfirmationTestScript(); | 140 return GetJavaScriptConfirmationTestScript(); |
| 141 case JavaScriptAlertType::PROMPT: | 141 case JavaScriptAlertType::PROMPT: |
| 142 return GetJavaScriptPromptTestScript(); | 142 return GetJavaScriptPromptTestScript(); |
| 143 } | 143 } |
| 144 GREYFail(@"JavascriptAlertType not recognized."); | 144 GREYFail(@"JavascriptAlertType not recognized."); |
| 145 return nil; | 145 return nil; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // HTTP server constants. | 148 // HTTP server constants. |
| 149 | 149 |
| 150 // URL and response for a blank document. | 150 // URL and response for a blank document. |
| 151 const char* kJavaScriptTestURL = "http://jsalerts"; | 151 const char kJavaScriptTestURL[] = "http://jsalerts"; |
| 152 const char* kJavaScriptTestResponse = | 152 const char kJavaScriptTestResponse[] = |
| 153 "<!DOCTYPE html><html><body></body></html>"; | 153 "<!DOCTYPE html><html><body></body></html>"; |
| 154 | 154 |
| 155 // URL and response for a page with an onload alert. | 155 // URL and response for a page with an onload alert. |
| 156 const char* kOnLoadAlertURL = "http://onloadalert"; | 156 const char kOnLoadAlertURL[] = "http://onloadalert"; |
| 157 const char* kOnLoadAlertResponse = | 157 const char kOnLoadAlertResponse[] = |
| 158 "<!DOCTYPE html><html><body onload=\"alert('alert')\"></body></html>"; | 158 "<!DOCTYPE html><html><body onload=\"alert('alert')\"></body></html>"; |
| 159 | 159 |
| 160 // URL and response for a page with a link to |kOnLoadAlertURL|. | 160 // URL and response for a page with a link to |kOnLoadAlertURL|. |
| 161 const char* kPageWithLinkURL = "http://pagewithlink"; | 161 const char kPageWithLinkURL[] = "http://pagewithlink"; |
| 162 const char* kPageWithLinkResponseFormat = | 162 NSString* const kPageWithLinkResponseFormat = |
| 163 "<!DOCTYPE html><html><body><a id=\"%s\" href=\"%s\">%s</a></body></html>"; | 163 @"<!DOCTYPE html><html><body><a id=\"%@\" href=\"%@\">%@</a></body></html>"; |
| 164 const char* kPageWithLinkText = "LINK TO ONLOAD ALERT PAGE"; | 164 NSString* const kPageWithLinkText = @"LINK TO ONLOAD ALERT PAGE"; |
| 165 const char* kLinkID = "link-id"; | 165 const char kLinkID[] = "link-id"; |
| 166 std::string GetPageWithLinkResponse() { | 166 std::string GetPageWithLinkResponse() { |
| 167 return base::SysNSStringToUTF8([NSString | 167 NSString* URL = base::SysUTF8ToNSString( |
|
Eugene But (OOO till 7-30)
2017/04/21 18:49:21
nit: s/URL/spec
| |
| 168 stringWithFormat:@(kPageWithLinkResponseFormat), kLinkID, | 168 HttpServer::MakeUrl(kOnLoadAlertURL).spec().c_str()); |
| 169 HttpServer::MakeUrl(kOnLoadAlertURL).spec().c_str(), | 169 return base::SysNSStringToUTF8( |
| 170 kPageWithLinkText]); | 170 [NSString stringWithFormat:kPageWithLinkResponseFormat, @(kLinkID), URL, |
|
Eugene But (OOO till 7-30)
2017/04/21 18:49:21
Is there a reason for converting everything to NSS
Eugene But (OOO till 7-30)
2017/04/21 18:49:21
Isn't @(char[]) just a wrapper for initWithUTF8Str
baxley
2017/04/21 20:09:01
Acknowledged.
| |
| 171 } | 171 kPageWithLinkText]); |
| 172 | |
| 173 // Waits until |string| is displayed on the web view. | |
| 174 void WaitForWebDisplay(const std::string& string) { | |
| 175 id<GREYMatcher> response1Matcher = | |
| 176 chrome_test_util::WebViewContainingText(string); | |
| 177 [[EarlGrey selectElementWithMatcher:response1Matcher] | |
| 178 assertWithMatcher:grey_notNil()]; | |
| 179 } | 172 } |
| 180 | 173 |
| 181 // Display the javascript alert. | 174 // Display the javascript alert. |
| 182 void DisplayJavaScriptAlert(JavaScriptAlertType type) { | 175 void DisplayJavaScriptAlert(JavaScriptAlertType type) { |
| 183 // Get the WebController. | 176 // Get the WebController. |
| 184 web::WebState* webState = chrome_test_util::GetCurrentWebState(); | 177 web::WebState* webState = chrome_test_util::GetCurrentWebState(); |
| 185 | 178 |
| 186 // Evaluate JavaScript. | 179 // Evaluate JavaScript. |
| 187 NSString* script = GetScriptForAlertWithType(type); | 180 NSString* script = GetScriptForAlertWithType(type); |
| 188 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); | 181 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 if (!errorOK || !errorCancel) { | 315 if (!errorOK || !errorCancel) { |
| 323 GREYFail(@"There are still alerts"); | 316 GREYFail(@"There are still alerts"); |
| 324 } | 317 } |
| 325 [super tearDown]; | 318 [super tearDown]; |
| 326 } | 319 } |
| 327 | 320 |
| 328 #pragma mark - Utility | 321 #pragma mark - Utility |
| 329 | 322 |
| 330 - (void)loadBlankTestPage { | 323 - (void)loadBlankTestPage { |
| 331 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kJavaScriptTestURL)]; | 324 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kJavaScriptTestURL)]; |
| 332 WaitForWebDisplay(std::string()); | 325 [ChromeEarlGrey waitForWebViewContainingText:@""]; |
| 333 } | 326 } |
| 334 | 327 |
| 335 - (void)loadPageWithLink { | 328 - (void)loadPageWithLink { |
| 336 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kPageWithLinkURL)]; | 329 [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kPageWithLinkURL)]; |
| 337 WaitForWebDisplay(kPageWithLinkText); | 330 [ChromeEarlGrey waitForWebViewContainingText:kPageWithLinkText]; |
| 338 } | 331 } |
| 339 | 332 |
| 340 #pragma mark - Tests | 333 #pragma mark - Tests |
| 341 | 334 |
| 342 // Tests that an alert is shown, and that the completion block is called. | 335 // Tests that an alert is shown, and that the completion block is called. |
| 343 - (void)testShowJavaScriptAlert { | 336 - (void)testShowJavaScriptAlert { |
| 344 // TODO(crbug.com/663026): Reenable the test for devices. | 337 // TODO(crbug.com/663026): Reenable the test for devices. |
| 345 #if !TARGET_IPHONE_SIMULATOR | 338 #if !TARGET_IPHONE_SIMULATOR |
| 346 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 339 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 347 @"alerts would prevent app alerts to present " | 340 @"alerts would prevent app alerts to present " |
| 348 @"correctly."); | 341 @"correctly."); |
| 349 #endif | 342 #endif |
| 350 | 343 |
| 351 // Load the blank test page and show an alert. | 344 // Load the blank test page and show an alert. |
| 352 [self loadBlankTestPage]; | 345 [self loadBlankTestPage]; |
| 353 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); | 346 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); |
| 354 | 347 |
| 355 // Tap the OK button. | 348 // Tap the OK button. |
| 356 TapOK(); | 349 TapOK(); |
| 357 | 350 |
| 358 // Wait for the html body to be reset to the correct value. | 351 // Wait for the html body to be reset to the correct value. |
| 359 WaitForWebDisplay(kAlertResultBody); | 352 [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; |
| 360 } | 353 } |
| 361 | 354 |
| 362 // Tests that a confirmation dialog is shown, and that the completion block is | 355 // Tests that a confirmation dialog is shown, and that the completion block is |
| 363 // called with the correct value when the OK buton is tapped. | 356 // called with the correct value when the OK buton is tapped. |
| 364 - (void)testShowJavaScriptConfirmationOK { | 357 - (void)testShowJavaScriptConfirmationOK { |
| 365 // TODO(crbug.com/663026): Reenable the test for devices. | 358 // TODO(crbug.com/663026): Reenable the test for devices. |
| 366 #if !TARGET_IPHONE_SIMULATOR | 359 #if !TARGET_IPHONE_SIMULATOR |
| 367 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 360 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 368 @"alerts would prevent app alerts to present " | 361 @"alerts would prevent app alerts to present " |
| 369 @"correctly."); | 362 @"correctly."); |
| 370 #endif | 363 #endif |
| 371 | 364 |
| 372 // Load the blank test page and show a confirmation dialog. | 365 // Load the blank test page and show a confirmation dialog. |
| 373 [self loadBlankTestPage]; | 366 [self loadBlankTestPage]; |
| 374 ShowJavaScriptDialog(JavaScriptAlertType::CONFIRMATION); | 367 ShowJavaScriptDialog(JavaScriptAlertType::CONFIRMATION); |
| 375 | 368 |
| 376 // Tap the OK button. | 369 // Tap the OK button. |
| 377 TapOK(); | 370 TapOK(); |
| 378 | 371 |
| 379 // Wait for the html body to be reset to the correct value. | 372 // Wait for the html body to be reset to the correct value. |
| 380 WaitForWebDisplay(kConfirmationResultBodyOK); | 373 [ChromeEarlGrey waitForWebViewContainingText:kConfirmationResultBodyOK]; |
| 381 } | 374 } |
| 382 | 375 |
| 383 // Tests that a confirmation dialog is shown, and that the completion block is | 376 // Tests that a confirmation dialog is shown, and that the completion block is |
| 384 // called with the correct value when the Cancel buton is tapped. | 377 // called with the correct value when the Cancel buton is tapped. |
| 385 - (void)testShowJavaScriptConfirmationCancelled { | 378 - (void)testShowJavaScriptConfirmationCancelled { |
| 386 // TODO(crbug.com/663026): Reenable the test for devices. | 379 // TODO(crbug.com/663026): Reenable the test for devices. |
| 387 #if !TARGET_IPHONE_SIMULATOR | 380 #if !TARGET_IPHONE_SIMULATOR |
| 388 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 381 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 389 @"alerts would prevent app alerts to present " | 382 @"alerts would prevent app alerts to present " |
| 390 @"correctly."); | 383 @"correctly."); |
| 391 #endif | 384 #endif |
| 392 | 385 |
| 393 // Load the blank test page and show a confirmation dialog. | 386 // Load the blank test page and show a confirmation dialog. |
| 394 [self loadBlankTestPage]; | 387 [self loadBlankTestPage]; |
| 395 ShowJavaScriptDialog(JavaScriptAlertType::CONFIRMATION); | 388 ShowJavaScriptDialog(JavaScriptAlertType::CONFIRMATION); |
| 396 | 389 |
| 397 // Tap the Cancel button. | 390 // Tap the Cancel button. |
| 398 TapCancel(); | 391 TapCancel(); |
| 399 | 392 |
| 400 // Wait for the html body to be reset to the correct value. | 393 // Wait for the html body to be reset to the correct value. |
| 401 WaitForWebDisplay(kConfirmationResultBodyCancelled); | 394 [ChromeEarlGrey |
| 395 waitForWebViewContainingText:kConfirmationResultBodyCancelled]; | |
| 402 } | 396 } |
| 403 | 397 |
| 404 // Tests that a prompt dialog is shown, and that the completion block is called | 398 // Tests that a prompt dialog is shown, and that the completion block is called |
| 405 // with the correct value when the OK buton is tapped. | 399 // with the correct value when the OK buton is tapped. |
| 406 - (void)testShowJavaScriptPromptOK { | 400 - (void)testShowJavaScriptPromptOK { |
| 407 // TODO(crbug.com/663026): Reenable the test for devices. | 401 // TODO(crbug.com/663026): Reenable the test for devices. |
| 408 #if !TARGET_IPHONE_SIMULATOR | 402 #if !TARGET_IPHONE_SIMULATOR |
| 409 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 403 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 410 @"alerts would prevent app alerts to present " | 404 @"alerts would prevent app alerts to present " |
| 411 @"correctly."); | 405 @"correctly."); |
| 412 #endif | 406 #endif |
| 413 | 407 |
| 414 // Load the blank test page and show a prompt dialog. | 408 // Load the blank test page and show a prompt dialog. |
| 415 [self loadBlankTestPage]; | 409 [self loadBlankTestPage]; |
| 416 ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); | 410 ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); |
| 417 | 411 |
| 418 // Enter text into text field. | 412 // Enter text into text field. |
| 419 TypeInPrompt(@(kPromptTestUserInput)); | 413 TypeInPrompt(kPromptTestUserInput); |
| 420 | 414 |
| 421 // Tap the OK button. | 415 // Tap the OK button. |
| 422 TapOK(); | 416 TapOK(); |
| 423 | 417 |
| 424 // Wait for the html body to be reset to the input text. | 418 // Wait for the html body to be reset to the input text. |
| 425 WaitForWebDisplay(kPromptTestUserInput); | 419 [ChromeEarlGrey waitForWebViewContainingText:kPromptTestUserInput]; |
| 426 } | 420 } |
| 427 | 421 |
| 428 // Tests that a prompt dialog is shown, and that the completion block is called | 422 // Tests that a prompt dialog is shown, and that the completion block is called |
| 429 // with the correct value when the Cancel buton is tapped. | 423 // with the correct value when the Cancel buton is tapped. |
| 430 - (void)testShowJavaScriptPromptCancelled { | 424 - (void)testShowJavaScriptPromptCancelled { |
| 431 // TODO(crbug.com/663026): Reenable the test for devices. | 425 // TODO(crbug.com/663026): Reenable the test for devices. |
| 432 #if !TARGET_IPHONE_SIMULATOR | 426 #if !TARGET_IPHONE_SIMULATOR |
| 433 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 427 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 434 @"alerts would prevent app alerts to present " | 428 @"alerts would prevent app alerts to present " |
| 435 @"correctly."); | 429 @"correctly."); |
| 436 #endif | 430 #endif |
| 437 | 431 |
| 438 // Load the blank test page and show a prompt dialog. | 432 // Load the blank test page and show a prompt dialog. |
| 439 [self loadBlankTestPage]; | 433 [self loadBlankTestPage]; |
| 440 ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); | 434 ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); |
| 441 | 435 |
| 442 // Enter text into text field. | 436 // Enter text into text field. |
| 443 TypeInPrompt(@(kPromptTestUserInput)); | 437 TypeInPrompt(kPromptTestUserInput); |
| 444 | 438 |
| 445 // Tap the Cancel button. | 439 // Tap the Cancel button. |
| 446 TapCancel(); | 440 TapCancel(); |
| 447 | 441 |
| 448 // Wait for the html body to be reset to the cancel text. | 442 // Wait for the html body to be reset to the cancel text. |
| 449 WaitForWebDisplay(kPromptResultBodyCancelled); | 443 [ChromeEarlGrey waitForWebViewContainingText:kPromptResultBodyCancelled]; |
| 450 } | 444 } |
| 451 | 445 |
| 452 // Tests that JavaScript alerts that are shown in a loop can be suppressed. | 446 // Tests that JavaScript alerts that are shown in a loop can be suppressed. |
| 453 - (void)testShowJavaScriptAlertLoop { | 447 - (void)testShowJavaScriptAlertLoop { |
| 454 // Load the blank test page and show alerts in a loop. | 448 // Load the blank test page and show alerts in a loop. |
| 455 [self loadBlankTestPage]; | 449 [self loadBlankTestPage]; |
| 456 web::WebState* webState = chrome_test_util::GetCurrentWebState(); | 450 web::WebState* webState = chrome_test_util::GetCurrentWebState(); |
| 457 NSString* script = GetJavaScriptAlertLoopScript(); | 451 NSString* script = GetJavaScriptAlertLoopScript(); |
| 458 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); | 452 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); |
| 459 WaitForJavaScripDialogToBeShown(); | 453 WaitForJavaScripDialogToBeShown(); |
| 460 | 454 |
| 461 // Tap the OK button and wait for another dialog to be shown. | 455 // Tap the OK button and wait for another dialog to be shown. |
| 462 TapOK(); | 456 TapOK(); |
| 463 WaitForJavaScripDialogToBeShown(); | 457 WaitForJavaScripDialogToBeShown(); |
| 464 | 458 |
| 465 // Tap the suppress dialogs button. | 459 // Tap the suppress dialogs button. |
| 466 TapSuppressDialogsButton(); | 460 TapSuppressDialogsButton(); |
| 467 | 461 |
| 468 // Wait for confirmation action sheet to be shown. | 462 // Wait for confirmation action sheet to be shown. |
| 469 NSString* alertLabel = | 463 NSString* alertLabel = |
| 470 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | 464 l10n_util::GetNSString(IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
| 471 WaitForAlertToBeShown(alertLabel); | 465 WaitForAlertToBeShown(alertLabel); |
| 472 | 466 |
| 473 // Tap the suppress dialogs confirmation button. | 467 // Tap the suppress dialogs confirmation button. |
| 474 TapSuppressDialogsButton(); | 468 TapSuppressDialogsButton(); |
| 475 | 469 |
| 476 // Wait for the html body to be reset to the loop finished text. | 470 // Wait for the html body to be reset to the loop finished text. |
| 477 WaitForWebDisplay(kAlertLoopFinishedText); | 471 [ChromeEarlGrey waitForWebViewContainingText:kAlertLoopFinishedText]; |
| 478 } | 472 } |
| 479 | 473 |
| 480 // Tests to ensure crbug.com/658260 does not regress. | 474 // Tests to ensure crbug.com/658260 does not regress. |
| 481 // Tests that if an alert should be called when settings are displays, the alert | 475 // Tests that if an alert should be called when settings are displays, the alert |
| 482 // waits for the dismiss of the settings. | 476 // waits for the dismiss of the settings. |
| 483 - (void)testShowJavaScriptBehindSettings { | 477 - (void)testShowJavaScriptBehindSettings { |
| 484 // TODO(crbug.com/663026): Reenable the test for devices. | 478 // TODO(crbug.com/663026): Reenable the test for devices. |
| 485 #if !TARGET_IPHONE_SIMULATOR | 479 #if !TARGET_IPHONE_SIMULATOR |
| 486 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 480 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 487 @"alerts would prevent app alerts to present " | 481 @"alerts would prevent app alerts to present " |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 511 [[EarlGrey selectElementWithMatcher:NavigationBarDoneButton()] | 505 [[EarlGrey selectElementWithMatcher:NavigationBarDoneButton()] |
| 512 performAction:grey_tap()]; | 506 performAction:grey_tap()]; |
| 513 | 507 |
| 514 // Make sure the alert is present. | 508 // Make sure the alert is present. |
| 515 WaitForJavaScripDialogToBeShown(); | 509 WaitForJavaScripDialogToBeShown(); |
| 516 | 510 |
| 517 // Tap the OK button. | 511 // Tap the OK button. |
| 518 TapOK(); | 512 TapOK(); |
| 519 | 513 |
| 520 // Wait for the html body to be reset to the correct value. | 514 // Wait for the html body to be reset to the correct value. |
| 521 WaitForWebDisplay(kAlertResultBody); | 515 [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; |
| 522 } | 516 } |
| 523 | 517 |
| 524 // Tests that an alert is presented after displaying the share menu. | 518 // Tests that an alert is presented after displaying the share menu. |
| 525 - (void)testShowJavaScriptAfterShareMenu { | 519 - (void)testShowJavaScriptAfterShareMenu { |
| 526 // TODO(crbug.com/663026): Reenable the test for devices. | 520 // TODO(crbug.com/663026): Reenable the test for devices. |
| 527 #if !TARGET_IPHONE_SIMULATOR | 521 #if !TARGET_IPHONE_SIMULATOR |
| 528 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 522 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 529 @"alerts would prevent app alerts to present " | 523 @"alerts would prevent app alerts to present " |
| 530 @"correctly."); | 524 @"correctly."); |
| 531 #endif | 525 #endif |
| 532 | 526 |
| 533 // Load the blank test page. | 527 // Load the blank test page. |
| 534 [self loadBlankTestPage]; | 528 [self loadBlankTestPage]; |
| 535 | 529 |
| 536 [ChromeEarlGreyUI openShareMenu]; | 530 [ChromeEarlGreyUI openShareMenu]; |
| 537 | 531 |
| 538 // Copy URL, dismissing the share menu. | 532 // Copy URL, dismissing the share menu. |
| 539 id<GREYMatcher> printButton = | 533 id<GREYMatcher> printButton = |
| 540 grey_allOf(grey_accessibilityLabel(@"Copy"), | 534 grey_allOf(grey_accessibilityLabel(@"Copy"), |
| 541 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); | 535 grey_accessibilityTrait(UIAccessibilityTraitButton), nil); |
| 542 [[EarlGrey selectElementWithMatcher:printButton] performAction:grey_tap()]; | 536 [[EarlGrey selectElementWithMatcher:printButton] performAction:grey_tap()]; |
| 543 | 537 |
| 544 // Show an alert and assert it is present. | 538 // Show an alert and assert it is present. |
| 545 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); | 539 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); |
| 546 | 540 |
| 547 // Tap the OK button. | 541 // Tap the OK button. |
| 548 TapOK(); | 542 TapOK(); |
| 549 | 543 |
| 550 // Wait for the html body to be reset to the correct value. | 544 // Wait for the html body to be reset to the correct value. |
| 551 WaitForWebDisplay(kAlertResultBody); | 545 [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; |
| 552 } | 546 } |
| 553 | 547 |
| 554 // Tests that an alert is presented after a new tab animation is finished. | 548 // Tests that an alert is presented after a new tab animation is finished. |
| 555 - (void)testShowJavaScriptAfterNewTabAnimation { | 549 - (void)testShowJavaScriptAfterNewTabAnimation { |
| 556 // TODO(crbug.com/663026): Reenable the test for devices. | 550 // TODO(crbug.com/663026): Reenable the test for devices. |
| 557 #if !TARGET_IPHONE_SIMULATOR | 551 #if !TARGET_IPHONE_SIMULATOR |
| 558 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " | 552 EARL_GREY_TEST_DISABLED(@"Disabled for devices because existing system " |
| 559 @"alerts would prevent app alerts to present " | 553 @"alerts would prevent app alerts to present " |
| 560 @"correctly."); | 554 @"correctly."); |
| 561 #endif | 555 #endif |
| 562 | 556 |
| 563 // Load the test page with a link to kOnLoadAlertURL and long tap on the link. | 557 // Load the test page with a link to kOnLoadAlertURL and long tap on the link. |
| 564 [self loadPageWithLink]; | 558 [self loadPageWithLink]; |
| 559 | |
| 560 // TODO(crbug.com/712358): Use method LongPressElementAndTapOnButton once | |
| 561 // it is moved out of context_menu_egtests.mm and into a shared location. | |
| 562 [ChromeEarlGrey waitForWebViewContainingText:kPageWithLinkText]; | |
| 565 id<GREYMatcher> webViewMatcher = | 563 id<GREYMatcher> webViewMatcher = |
| 566 chrome_test_util::WebViewContainingText(std::string(kPageWithLinkText)); | 564 web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); |
| 565 | |
| 567 [[EarlGrey selectElementWithMatcher:webViewMatcher] | 566 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 568 performAction:chrome_test_util::LongPressElementForContextMenu( | 567 performAction:chrome_test_util::LongPressElementForContextMenu( |
| 569 kLinkID, true /* menu should appear */)]; | 568 kLinkID, true /* menu should appear */)]; |
| 570 | 569 |
| 571 // Tap on the "Open In New Tab" button. | 570 // Tap on the "Open In New Tab" button. |
| 572 id<GREYMatcher> newTabMatcher = testing::ContextMenuItemWithText( | 571 id<GREYMatcher> newTabMatcher = testing::ContextMenuItemWithText( |
| 573 l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB)); | 572 l10n_util::GetNSStringWithFixup(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB)); |
| 574 [[EarlGrey selectElementWithMatcher:newTabMatcher] performAction:grey_tap()]; | 573 [[EarlGrey selectElementWithMatcher:newTabMatcher] performAction:grey_tap()]; |
| 575 | 574 |
| 576 // This test case requires that a dialog is presented in the onload event so | 575 // This test case requires that a dialog is presented in the onload event so |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 604 | 603 |
| 605 // Reenable synchronization on iPads now that the dialog has been dismissed. | 604 // Reenable synchronization on iPads now that the dialog has been dismissed. |
| 606 if (IsIPadIdiom()) { | 605 if (IsIPadIdiom()) { |
| 607 [[GREYConfiguration sharedInstance] | 606 [[GREYConfiguration sharedInstance] |
| 608 setValue:@(YES) | 607 setValue:@(YES) |
| 609 forConfigKey:kGREYConfigKeySynchronizationEnabled]; | 608 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 610 } | 609 } |
| 611 } | 610 } |
| 612 | 611 |
| 613 @end | 612 @end |
| OLD | NEW |