Chromium Code Reviews| Index: ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm | 
| diff --git a/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm b/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm | 
| index 3e58bb7d9b84094b92d448203f1679e82c076d67..512287baf4851e1b25f0e31b2c7906ce1049dfdc 100644 | 
| --- a/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm | 
| +++ b/ios/chrome/browser/ui/dialogs/javascript_dialog_egtest.mm | 
| @@ -21,6 +21,7 @@ | 
| #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 
| #import "ios/testing/earl_grey/matchers.h" | 
| #import "ios/testing/wait_util.h" | 
| +#import "ios/web/public/test/earl_grey/web_view_matchers.h" | 
| #import "ios/web/public/test/http_server.h" | 
| #import "ios/web/public/test/http_server_util.h" | 
| #include "ios/web/public/test/url_test_util.h" | 
| @@ -47,85 +48,84 @@ enum class JavaScriptAlertType : NSUInteger { ALERT, CONFIRMATION, PROMPT }; | 
| // Script to inject that will show an alert. The document's body will be reset | 
| // to |kAlertResultBody| after the dialog is dismissed. | 
| -const char kAlertMessage[] = "This is a JavaScript alert."; | 
| -const char kAlertResultBody[] = "JAVASCRIPT ALERT WAS DISMISSED"; | 
| -const char kJavaScriptAlertTestScriptFormat[] = | 
| - "(function(){ " | 
| - " alert(\"%@\");" | 
| - " document.body.innerHTML = \"%@\";" | 
| - "})();"; | 
| +NSString* const kAlertMessage = @"This is a JavaScript alert."; | 
| +NSString* const kAlertResultBody = @"JAVASCRIPT ALERT WAS DISMISSED"; | 
| +NSString* const kJavaScriptAlertTestScriptFormat = | 
| + @"(function(){ " | 
| + " alert(\"%@\");" | 
| + " document.body.innerHTML = \"%@\";" | 
| + "})();"; | 
| NSString* GetJavaScriptAlertTestScript() { | 
| - return [NSString stringWithFormat:@(kJavaScriptAlertTestScriptFormat), | 
| - @(kAlertMessage), @(kAlertResultBody)]; | 
| + return [NSString stringWithFormat:kJavaScriptAlertTestScriptFormat, | 
| + kAlertMessage, kAlertResultBody]; | 
| } | 
| // Script to inject that will show a confirmation dialog. The document's body | 
| // will be reset to |kConfirmationResultBodyOK| or | 
| // |kConfirmationResultBodyCancelled| depending on whether the OK or Cancel | 
| // button was tapped. | 
| -const char kConfirmationMessage[] = "This is a JavaScript confirmation."; | 
| -const char kConfirmationResultBodyOK[] = "Okay"; | 
| -const char kConfirmationResultBodyCancelled[] = "Cancelled"; | 
| -const char kJavaScriptConfirmationScriptFormat[] = | 
| - "(function(){ " | 
| - " if (confirm(\"%@\") == true) {" | 
| - " document.body.innerHTML = \"%@\";" | 
| - " } else {" | 
| - " document.body.innerHTML = \"%@\";" | 
| - " }" | 
| - "})();"; | 
| +NSString* const kConfirmationMessage = @"This is a JavaScript confirmation."; | 
| +NSString* const kConfirmationResultBodyOK = @"Okay"; | 
| +NSString* const kConfirmationResultBodyCancelled = @"Cancelled"; | 
| +NSString* const kJavaScriptConfirmationScriptFormat = | 
| + @"(function(){ " | 
| + " if (confirm(\"%@\") == true) {" | 
| + " document.body.innerHTML = \"%@\";" | 
| + " } else {" | 
| + " document.body.innerHTML = \"%@\";" | 
| + " }" | 
| + "})();"; | 
| NSString* GetJavaScriptConfirmationTestScript() { | 
| - return [NSString stringWithFormat:@(kJavaScriptConfirmationScriptFormat), | 
| - @(kConfirmationMessage), | 
| - @(kConfirmationResultBodyOK), | 
| - @(kConfirmationResultBodyCancelled)]; | 
| + return [NSString stringWithFormat:kJavaScriptConfirmationScriptFormat, | 
| + kConfirmationMessage, | 
| + kConfirmationResultBodyOK, | 
| + kConfirmationResultBodyCancelled]; | 
| } | 
| // Script to inject that will show a prompt dialog. The document's body will be | 
| // reset to |kPromptResultBodyCancelled| or |kPromptTestUserInput| depending on | 
| // whether the OK or Cancel button was tapped. | 
| -const char kPromptMessage[] = "This is a JavaScript prompt."; | 
| -const char kPromptResultBodyCancelled[] = "Cancelled"; | 
| -const char kPromptTestUserInput[] = "test"; | 
| -const char kJavaScriptPromptTestScriptFormat[] = | 
| - "(function(){ " | 
| - " var input = prompt(\"%@\");" | 
| - " if (input != null) {" | 
| - " document.body.innerHTML = input;" | 
| - " } else {" | 
| - " document.body.innerHTML = \"%@\";" | 
| - " }" | 
| - "})();"; | 
| +NSString* const kPromptMessage = @"This is a JavaScript prompt."; | 
| +NSString* const kPromptResultBodyCancelled = @"Cancelled"; | 
| +NSString* const kPromptTestUserInput = @"test"; | 
| +NSString* const kJavaScriptPromptTestScriptFormat = | 
| + @"(function(){ " | 
| + " var input = prompt(\"%@\");" | 
| + " if (input != null) {" | 
| + " document.body.innerHTML = input;" | 
| + " } else {" | 
| + " document.body.innerHTML = \"%@\";" | 
| + " }" | 
| + "})();"; | 
| NSString* GetJavaScriptPromptTestScript() { | 
| - return [NSString stringWithFormat:@(kJavaScriptPromptTestScriptFormat), | 
| - @(kPromptMessage), | 
| - @(kPromptResultBodyCancelled)]; | 
| + return [NSString stringWithFormat:kJavaScriptPromptTestScriptFormat, | 
| + kPromptMessage, kPromptResultBodyCancelled]; | 
| } | 
| // Script to inject that will show a JavaScript alert in a loop 20 times, then | 
| // reset the document's HTML to |kAlertLoopFinishedText|. | 
| -const char kAlertLoopFinishedText[] = "Loop Finished"; | 
| -const char kJavaScriptAlertLoopScriptFormat[] = | 
| - "(function(){ " | 
| - " for (i = 0; i < 20; ++i) {" | 
| - " alert(\"ALERT TEXT\");" | 
| - " }" | 
| - " document.body.innerHTML = \"%@\";" | 
| - "})();"; | 
| +NSString* const kAlertLoopFinishedText = @"Loop Finished"; | 
| +NSString* const kJavaScriptAlertLoopScriptFormat = | 
| + @"(function(){ " | 
| + " for (i = 0; i < 20; ++i) {" | 
| + " alert(\"ALERT TEXT\");" | 
| + " }" | 
| + " document.body.innerHTML = \"%@\";" | 
| + "})();"; | 
| NSString* GetJavaScriptAlertLoopScript() { | 
| - return [NSString stringWithFormat:@(kJavaScriptAlertLoopScriptFormat), | 
| - @(kAlertLoopFinishedText)]; | 
| + return [NSString stringWithFormat:kJavaScriptAlertLoopScriptFormat, | 
| + kAlertLoopFinishedText]; | 
| } | 
| // Returns the message for a JavaScript alert with |type|. | 
| NSString* GetMessageForAlertWithType(JavaScriptAlertType type) { | 
| switch (type) { | 
| case JavaScriptAlertType::ALERT: | 
| - return @(kAlertMessage); | 
| + return kAlertMessage; | 
| case JavaScriptAlertType::CONFIRMATION: | 
| - return @(kConfirmationMessage); | 
| + return kConfirmationMessage; | 
| case JavaScriptAlertType::PROMPT: | 
| - return @(kPromptMessage); | 
| + return kPromptMessage; | 
| } | 
| GREYFail(@"JavascriptAlertType not recognized."); | 
| return nil; | 
| @@ -148,34 +148,27 @@ NSString* GetScriptForAlertWithType(JavaScriptAlertType type) { | 
| // HTTP server constants. | 
| // URL and response for a blank document. | 
| -const char* kJavaScriptTestURL = "http://jsalerts"; | 
| -const char* kJavaScriptTestResponse = | 
| +const char kJavaScriptTestURL[] = "http://jsalerts"; | 
| +const char kJavaScriptTestResponse[] = | 
| "<!DOCTYPE html><html><body></body></html>"; | 
| // URL and response for a page with an onload alert. | 
| -const char* kOnLoadAlertURL = "http://onloadalert"; | 
| -const char* kOnLoadAlertResponse = | 
| +const char kOnLoadAlertURL[] = "http://onloadalert"; | 
| +const char kOnLoadAlertResponse[] = | 
| "<!DOCTYPE html><html><body onload=\"alert('alert')\"></body></html>"; | 
| // URL and response for a page with a link to |kOnLoadAlertURL|. | 
| -const char* kPageWithLinkURL = "http://pagewithlink"; | 
| -const char* kPageWithLinkResponseFormat = | 
| - "<!DOCTYPE html><html><body><a id=\"%s\" href=\"%s\">%s</a></body></html>"; | 
| -const char* kPageWithLinkText = "LINK TO ONLOAD ALERT PAGE"; | 
| -const char* kLinkID = "link-id"; | 
| +const char kPageWithLinkURL[] = "http://pagewithlink"; | 
| +NSString* const kPageWithLinkResponseFormat = | 
| + @"<!DOCTYPE html><html><body><a id=\"%@\" href=\"%@\">%@</a></body></html>"; | 
| +NSString* const kPageWithLinkText = @"LINK TO ONLOAD ALERT PAGE"; | 
| +const char kLinkID[] = "link-id"; | 
| std::string GetPageWithLinkResponse() { | 
| - return base::SysNSStringToUTF8([NSString | 
| - stringWithFormat:@(kPageWithLinkResponseFormat), kLinkID, | 
| - HttpServer::MakeUrl(kOnLoadAlertURL).spec().c_str(), | 
| - kPageWithLinkText]); | 
| -} | 
| - | 
| -// Waits until |string| is displayed on the web view. | 
| -void WaitForWebDisplay(const std::string& string) { | 
| - id<GREYMatcher> response1Matcher = | 
| - chrome_test_util::WebViewContainingText(string); | 
| - [[EarlGrey selectElementWithMatcher:response1Matcher] | 
| - assertWithMatcher:grey_notNil()]; | 
| + NSString* URL = base::SysUTF8ToNSString( | 
| 
 
Eugene But (OOO till 7-30)
2017/04/21 18:49:21
nit: s/URL/spec
 
 | 
| + HttpServer::MakeUrl(kOnLoadAlertURL).spec().c_str()); | 
| + return base::SysNSStringToUTF8( | 
| + [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.
 
 | 
| + kPageWithLinkText]); | 
| } | 
| // Display the javascript alert. | 
| @@ -329,12 +322,12 @@ void TapSuppressDialogsButton() { | 
| - (void)loadBlankTestPage { | 
| [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kJavaScriptTestURL)]; | 
| - WaitForWebDisplay(std::string()); | 
| + [ChromeEarlGrey waitForWebViewContainingText:@""]; | 
| } | 
| - (void)loadPageWithLink { | 
| [ChromeEarlGrey loadURL:HttpServer::MakeUrl(kPageWithLinkURL)]; | 
| - WaitForWebDisplay(kPageWithLinkText); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kPageWithLinkText]; | 
| } | 
| #pragma mark - Tests | 
| @@ -356,7 +349,7 @@ void TapSuppressDialogsButton() { | 
| TapOK(); | 
| // Wait for the html body to be reset to the correct value. | 
| - WaitForWebDisplay(kAlertResultBody); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; | 
| } | 
| // Tests that a confirmation dialog is shown, and that the completion block is | 
| @@ -377,7 +370,7 @@ void TapSuppressDialogsButton() { | 
| TapOK(); | 
| // Wait for the html body to be reset to the correct value. | 
| - WaitForWebDisplay(kConfirmationResultBodyOK); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kConfirmationResultBodyOK]; | 
| } | 
| // Tests that a confirmation dialog is shown, and that the completion block is | 
| @@ -398,7 +391,8 @@ void TapSuppressDialogsButton() { | 
| TapCancel(); | 
| // Wait for the html body to be reset to the correct value. | 
| - WaitForWebDisplay(kConfirmationResultBodyCancelled); | 
| + [ChromeEarlGrey | 
| + waitForWebViewContainingText:kConfirmationResultBodyCancelled]; | 
| } | 
| // Tests that a prompt dialog is shown, and that the completion block is called | 
| @@ -416,13 +410,13 @@ void TapSuppressDialogsButton() { | 
| ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); | 
| // Enter text into text field. | 
| - TypeInPrompt(@(kPromptTestUserInput)); | 
| + TypeInPrompt(kPromptTestUserInput); | 
| // Tap the OK button. | 
| TapOK(); | 
| // Wait for the html body to be reset to the input text. | 
| - WaitForWebDisplay(kPromptTestUserInput); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kPromptTestUserInput]; | 
| } | 
| // Tests that a prompt dialog is shown, and that the completion block is called | 
| @@ -440,13 +434,13 @@ void TapSuppressDialogsButton() { | 
| ShowJavaScriptDialog(JavaScriptAlertType::PROMPT); | 
| // Enter text into text field. | 
| - TypeInPrompt(@(kPromptTestUserInput)); | 
| + TypeInPrompt(kPromptTestUserInput); | 
| // Tap the Cancel button. | 
| TapCancel(); | 
| // Wait for the html body to be reset to the cancel text. | 
| - WaitForWebDisplay(kPromptResultBodyCancelled); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kPromptResultBodyCancelled]; | 
| } | 
| // Tests that JavaScript alerts that are shown in a loop can be suppressed. | 
| @@ -474,7 +468,7 @@ void TapSuppressDialogsButton() { | 
| TapSuppressDialogsButton(); | 
| // Wait for the html body to be reset to the loop finished text. | 
| - WaitForWebDisplay(kAlertLoopFinishedText); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kAlertLoopFinishedText]; | 
| } | 
| // Tests to ensure crbug.com/658260 does not regress. | 
| @@ -518,7 +512,7 @@ void TapSuppressDialogsButton() { | 
| TapOK(); | 
| // Wait for the html body to be reset to the correct value. | 
| - WaitForWebDisplay(kAlertResultBody); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; | 
| } | 
| // Tests that an alert is presented after displaying the share menu. | 
| @@ -548,7 +542,7 @@ void TapSuppressDialogsButton() { | 
| TapOK(); | 
| // Wait for the html body to be reset to the correct value. | 
| - WaitForWebDisplay(kAlertResultBody); | 
| + [ChromeEarlGrey waitForWebViewContainingText:kAlertResultBody]; | 
| } | 
| // Tests that an alert is presented after a new tab animation is finished. | 
| @@ -562,8 +556,13 @@ void TapSuppressDialogsButton() { | 
| // Load the test page with a link to kOnLoadAlertURL and long tap on the link. | 
| [self loadPageWithLink]; | 
| + | 
| + // TODO(crbug.com/712358): Use method LongPressElementAndTapOnButton once | 
| + // it is moved out of context_menu_egtests.mm and into a shared location. | 
| + [ChromeEarlGrey waitForWebViewContainingText:kPageWithLinkText]; | 
| id<GREYMatcher> webViewMatcher = | 
| - chrome_test_util::WebViewContainingText(std::string(kPageWithLinkText)); | 
| + web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); | 
| + | 
| [[EarlGrey selectElementWithMatcher:webViewMatcher] | 
| performAction:chrome_test_util::LongPressElementForContextMenu( | 
| kLinkID, true /* menu should appear */)]; |