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 "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Const for an http server that returns a blank document. | 142 // Const for an http server that returns a blank document. |
| 143 const char* kJavaScriptTestURL = "http://jsalerts"; | 143 const char* kJavaScriptTestURL = "http://jsalerts"; |
| 144 const char* kJavaScriptTestResponse = | 144 const char* kJavaScriptTestResponse = |
| 145 "<!DOCTYPE html><html><body></body></html>"; | 145 "<!DOCTYPE html><html><body></body></html>"; |
| 146 | 146 |
| 147 // Waits until |string| is displayed on the web view. | 147 // Waits until |string| is displayed on the web view. |
| 148 void WaitForWebDisplay(const std::string& string) { | 148 void WaitForWebDisplay(const std::string& string) { |
| 149 id<GREYMatcher> response1Matcher = | 149 id<GREYMatcher> response1Matcher = |
| 150 chrome_test_util::webViewContainingText(string); | 150 chrome_test_util::WebViewContainingText(string); |
| 151 [[EarlGrey selectElementWithMatcher:response1Matcher] | 151 [[EarlGrey selectElementWithMatcher:response1Matcher] |
| 152 assertWithMatcher:grey_notNil()]; | 152 assertWithMatcher:grey_notNil()]; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Display the javascript alert. | 155 // Display the javascript alert. |
| 156 void DisplayJavaScriptAlert(JavaScriptAlertType type) { | 156 void DisplayJavaScriptAlert(JavaScriptAlertType type) { |
| 157 // Get the WebController. | 157 // Get the WebController. |
| 158 web::WebState* webState = chrome_test_util::GetCurrentWebState(); | 158 web::WebState* webState = chrome_test_util::GetCurrentWebState(); |
| 159 | 159 |
| 160 // Evaluate JavaScript. | 160 // Evaluate JavaScript. |
| 161 NSString* script = GetScriptForAlertWithType(type); | 161 NSString* script = GetScriptForAlertWithType(type); |
| 162 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); | 162 webState->ExecuteJavaScript(base::SysNSStringToUTF16(script)); |
| 163 } | 163 } |
| 164 | 164 |
| 165 // Assert that the javascript alert has been presented. | 165 // Assert that the javascript alert has been presented. |
| 166 void WaitForAlertToBeShown(NSString* alert_label) { | 166 void WaitForAlertToBeShown(NSString* alert_label) { |
| 167 // Wait for the alert to be shown by trying to get the alert title. | 167 // Wait for the alert to be shown by trying to get the alert title. |
| 168 ConditionBlock condition = ^{ | 168 ConditionBlock condition = ^{ |
| 169 NSError* error = nil; | 169 NSError* error = nil; |
| 170 id<GREYMatcher> titleLabel = | 170 id<GREYMatcher> titleLabel = |
| 171 chrome_test_util::staticTextWithAccessibilityLabel(alert_label); | 171 chrome_test_util::StaticTextWithAccessibilityLabel(alert_label); |
| 172 [[EarlGrey selectElementWithMatcher:titleLabel] | 172 [[EarlGrey selectElementWithMatcher:titleLabel] |
| 173 assertWithMatcher:grey_notNil() | 173 assertWithMatcher:grey_notNil() |
| 174 error:&error]; | 174 error:&error]; |
| 175 return !error; | 175 return !error; |
| 176 }; | 176 }; |
| 177 GREYAssert(testing::WaitUntilConditionOrTimeout( | 177 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 178 testing::kWaitForJSCompletionTimeout, condition), | 178 testing::kWaitForJSCompletionTimeout, condition), |
| 179 @"Alert with title was not present: %@", alert_label); | 179 @"Alert with title was not present: %@", alert_label); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void WaitForJavaScripDialogToBeShown() { | 182 void WaitForJavaScripDialogToBeShown() { |
| 183 NSString* alert_label = [DialogPresenter | 183 NSString* alert_label = [DialogPresenter |
| 184 localizedTitleForJavaScriptAlertFromPage:web::test::HttpServer::MakeUrl( | 184 localizedTitleForJavaScriptAlertFromPage:web::test::HttpServer::MakeUrl( |
| 185 kJavaScriptTestURL)]; | 185 kJavaScriptTestURL)]; |
| 186 WaitForAlertToBeShown(alert_label); | 186 WaitForAlertToBeShown(alert_label); |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Injects JavaScript to show a dialog with |type|, verifying that it was | 189 // Injects JavaScript to show a dialog with |type|, verifying that it was |
| 190 // properly displayed. | 190 // properly displayed. |
| 191 void ShowJavaScriptDialog(JavaScriptAlertType type) { | 191 void ShowJavaScriptDialog(JavaScriptAlertType type) { |
| 192 DisplayJavaScriptAlert(type); | 192 DisplayJavaScriptAlert(type); |
| 193 | 193 |
| 194 WaitForJavaScripDialogToBeShown(); | 194 WaitForJavaScripDialogToBeShown(); |
| 195 | 195 |
| 196 // Check the message of the alert. | 196 // Check the message of the alert. |
| 197 id<GREYMatcher> messageLabel = | 197 id<GREYMatcher> messageLabel = |
| 198 chrome_test_util::staticTextWithAccessibilityLabel( | 198 chrome_test_util::StaticTextWithAccessibilityLabel( |
| 199 GetMessageForAlertWithType(type)); | 199 GetMessageForAlertWithType(type)); |
| 200 [[EarlGrey selectElementWithMatcher:messageLabel] | 200 [[EarlGrey selectElementWithMatcher:messageLabel] |
| 201 assertWithMatcher:grey_notNil()]; | 201 assertWithMatcher:grey_notNil()]; |
| 202 } | 202 } |
| 203 | 203 |
| 204 // Assert no javascript alert is visible. | 204 // Assert no javascript alert is visible. |
| 205 void AssertJavaScriptAlertNotPresent() { | 205 void AssertJavaScriptAlertNotPresent() { |
| 206 ConditionBlock condition = ^{ | 206 ConditionBlock condition = ^{ |
| 207 NSError* error = nil; | 207 NSError* error = nil; |
| 208 NSString* alertLabel = [DialogPresenter | 208 NSString* alertLabel = [DialogPresenter |
| 209 localizedTitleForJavaScriptAlertFromPage:web::test::HttpServer::MakeUrl( | 209 localizedTitleForJavaScriptAlertFromPage:web::test::HttpServer::MakeUrl( |
| 210 kJavaScriptTestURL)]; | 210 kJavaScriptTestURL)]; |
| 211 id<GREYMatcher> titleLabel = | 211 id<GREYMatcher> titleLabel = |
| 212 chrome_test_util::staticTextWithAccessibilityLabel(alertLabel); | 212 chrome_test_util::StaticTextWithAccessibilityLabel(alertLabel); |
| 213 [[EarlGrey selectElementWithMatcher:titleLabel] assertWithMatcher:grey_nil() | 213 [[EarlGrey selectElementWithMatcher:titleLabel] assertWithMatcher:grey_nil() |
| 214 error:&error]; | 214 error:&error]; |
| 215 return !error; | 215 return !error; |
| 216 }; | 216 }; |
| 217 | 217 |
| 218 GREYAssert(testing::WaitUntilConditionOrTimeout( | 218 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 219 testing::kWaitForJSCompletionTimeout, condition), | 219 testing::kWaitForJSCompletionTimeout, condition), |
| 220 @"Javascript alert title was still present"); | 220 @"Javascript alert title was still present"); |
| 221 } | 221 } |
| 222 | 222 |
| 223 // Types |input| in the prompt. | 223 // Types |input| in the prompt. |
| 224 void TypeInPrompt(NSString* input) { | 224 void TypeInPrompt(NSString* input) { |
| 225 [[[EarlGrey selectElementWithMatcher: | 225 [[[EarlGrey selectElementWithMatcher: |
| 226 grey_accessibilityID( | 226 grey_accessibilityID( |
| 227 kJavaScriptDialogTextFieldAccessibiltyIdentifier)] | 227 kJavaScriptDialogTextFieldAccessibiltyIdentifier)] |
| 228 assertWithMatcher:grey_sufficientlyVisible()] performAction:grey_tap()]; | 228 assertWithMatcher:grey_sufficientlyVisible()] performAction:grey_tap()]; |
| 229 [[EarlGrey selectElementWithMatcher: | 229 [[EarlGrey selectElementWithMatcher: |
| 230 grey_accessibilityID( | 230 grey_accessibilityID( |
| 231 kJavaScriptDialogTextFieldAccessibiltyIdentifier)] | 231 kJavaScriptDialogTextFieldAccessibiltyIdentifier)] |
| 232 performAction:grey_typeText(input)]; | 232 performAction:grey_typeText(input)]; |
| 233 } | 233 } |
| 234 | 234 |
| 235 void TapOK() { | 235 void TapOK() { |
| 236 id<GREYMatcher> ok_button = | 236 id<GREYMatcher> ok_button = |
| 237 chrome_test_util::buttonWithAccessibilityLabelId(IDS_OK); | 237 chrome_test_util::ButtonWithAccessibilityLabelId(IDS_OK); |
| 238 [[EarlGrey selectElementWithMatcher:ok_button] performAction:grey_tap()]; | 238 [[EarlGrey selectElementWithMatcher:ok_button] performAction:grey_tap()]; |
| 239 } | 239 } |
| 240 | 240 |
| 241 void TapCancel() { | 241 void TapCancel() { |
| 242 [[EarlGrey selectElementWithMatcher:chrome_test_util::cancelButton()] | 242 [[EarlGrey selectElementWithMatcher:chrome_test_util::CancelButton()] |
| 243 performAction:grey_tap()]; | 243 performAction:grey_tap()]; |
| 244 } | 244 } |
| 245 | 245 |
| 246 void TapSuppressDialogsButton() { | 246 void TapSuppressDialogsButton() { |
| 247 id<GREYMatcher> suppress_dialogs_button = | 247 id<GREYMatcher> suppress_dialogs_button = |
| 248 chrome_test_util::buttonWithAccessibilityLabelId( | 248 chrome_test_util::ButtonWithAccessibilityLabelId( |
| 249 IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); | 249 IDS_IOS_JAVA_SCRIPT_DIALOG_BLOCKING_BUTTON_TEXT); |
| 250 [[EarlGrey selectElementWithMatcher:suppress_dialogs_button] | 250 [[EarlGrey selectElementWithMatcher:suppress_dialogs_button] |
| 251 performAction:grey_tap()]; | 251 performAction:grey_tap()]; |
| 252 } | 252 } |
| 253 | 253 |
| 254 } // namespace | 254 } // namespace |
| 255 | 255 |
| 256 @interface JavaScriptDialogTestCase : ChromeTestCase { | 256 @interface JavaScriptDialogTestCase : ChromeTestCase { |
| 257 GURL _URL; | 257 GURL _URL; |
| 258 } | 258 } |
| 259 | 259 |
| 260 @end | 260 @end |
| 261 | 261 |
| 262 @implementation JavaScriptDialogTestCase | 262 @implementation JavaScriptDialogTestCase |
| 263 | 263 |
| 264 - (void)setUp { | 264 - (void)setUp { |
| 265 [super setUp]; | 265 [super setUp]; |
| 266 _URL = web::test::HttpServer::MakeUrl(kJavaScriptTestURL); | 266 _URL = web::test::HttpServer::MakeUrl(kJavaScriptTestURL); |
| 267 std::map<GURL, std::string> responses; | 267 std::map<GURL, std::string> responses; |
| 268 responses[web::test::HttpServer::MakeUrl(kJavaScriptTestURL)] = | 268 responses[web::test::HttpServer::MakeUrl(kJavaScriptTestURL)] = |
| 269 kJavaScriptTestResponse; | 269 kJavaScriptTestResponse; |
| 270 web::test::SetUpSimpleHttpServer(responses); | 270 web::test::SetUpSimpleHttpServer(responses); |
| 271 | 271 |
| 272 [ChromeEarlGrey loadURL:_URL]; | 272 [ChromeEarlGrey loadURL:_URL]; |
| 273 id<GREYMatcher> response2Matcher = | 273 id<GREYMatcher> response2Matcher = |
| 274 chrome_test_util::webViewContainingText(std::string("")); | 274 chrome_test_util::WebViewContainingText(std::string("")); |
|
Eugene But (OOO till 7-30)
2017/01/23 19:39:03
nit: std::string()
baxley
2017/01/24 22:18:49
Done.
| |
| 275 [[EarlGrey selectElementWithMatcher:response2Matcher] | 275 [[EarlGrey selectElementWithMatcher:response2Matcher] |
| 276 assertWithMatcher:grey_notNil()]; | 276 assertWithMatcher:grey_notNil()]; |
| 277 } | 277 } |
| 278 | 278 |
| 279 - (void)tearDown { | 279 - (void)tearDown { |
| 280 NSError* errorOK = nil; | 280 NSError* errorOK = nil; |
| 281 NSError* errorCancel = nil; | 281 NSError* errorCancel = nil; |
| 282 | 282 |
| 283 // Dismiss JavaScript alert by tapping Cancel. | 283 // Dismiss JavaScript alert by tapping Cancel. |
| 284 [[EarlGrey selectElementWithMatcher:chrome_test_util::cancelButton()] | 284 [[EarlGrey selectElementWithMatcher:chrome_test_util::CancelButton()] |
| 285 performAction:grey_tap() | 285 performAction:grey_tap() |
| 286 error:&errorCancel]; | 286 error:&errorCancel]; |
| 287 // Dismiss JavaScript alert by tapping OK. | 287 // Dismiss JavaScript alert by tapping OK. |
| 288 id<GREYMatcher> OKButton = | 288 id<GREYMatcher> OKButton = |
| 289 chrome_test_util::buttonWithAccessibilityLabelId(IDS_OK); | 289 chrome_test_util::ButtonWithAccessibilityLabelId(IDS_OK); |
| 290 [[EarlGrey selectElementWithMatcher:OKButton] performAction:grey_tap() | 290 [[EarlGrey selectElementWithMatcher:OKButton] performAction:grey_tap() |
| 291 error:&errorOK]; | 291 error:&errorOK]; |
| 292 | 292 |
| 293 if (!errorOK || !errorCancel) { | 293 if (!errorOK || !errorCancel) { |
| 294 GREYFail(@"There are still alerts"); | 294 GREYFail(@"There are still alerts"); |
| 295 } | 295 } |
| 296 [super tearDown]; | 296 [super tearDown]; |
| 297 } | 297 } |
| 298 | 298 |
| 299 #pragma mark - Tests | 299 #pragma mark - Tests |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 @"alerts would prevent app alerts to present " | 440 @"alerts would prevent app alerts to present " |
| 441 @"correctly."); | 441 @"correctly."); |
| 442 #endif | 442 #endif |
| 443 | 443 |
| 444 // Show settings. | 444 // Show settings. |
| 445 [ChromeEarlGreyUI openToolsMenu]; | 445 [ChromeEarlGreyUI openToolsMenu]; |
| 446 [[EarlGrey | 446 [[EarlGrey |
| 447 selectElementWithMatcher:grey_accessibilityID(kToolsMenuSettingsId)] | 447 selectElementWithMatcher:grey_accessibilityID(kToolsMenuSettingsId)] |
| 448 performAction:grey_tap()]; | 448 performAction:grey_tap()]; |
| 449 [[EarlGrey selectElementWithMatcher:chrome_test_util:: | 449 [[EarlGrey selectElementWithMatcher:chrome_test_util:: |
| 450 staticTextWithAccessibilityLabelId( | 450 StaticTextWithAccessibilityLabelId( |
| 451 IDS_IOS_SETTINGS_TITLE)] | 451 IDS_IOS_SETTINGS_TITLE)] |
| 452 assertWithMatcher:grey_sufficientlyVisible()]; | 452 assertWithMatcher:grey_sufficientlyVisible()]; |
| 453 | 453 |
| 454 // Show an alert. | 454 // Show an alert. |
| 455 DisplayJavaScriptAlert(JavaScriptAlertType::ALERT); | 455 DisplayJavaScriptAlert(JavaScriptAlertType::ALERT); |
| 456 | 456 |
| 457 // Make sure the alert is not present. | 457 // Make sure the alert is not present. |
| 458 AssertJavaScriptAlertNotPresent(); | 458 AssertJavaScriptAlertNotPresent(); |
| 459 | 459 |
| 460 // Close the settings. | 460 // Close the settings. |
| 461 [[EarlGrey | 461 [[EarlGrey |
| 462 selectElementWithMatcher:chrome_test_util::buttonWithAccessibilityLabelId( | 462 selectElementWithMatcher:chrome_test_util::ButtonWithAccessibilityLabelId( |
| 463 IDS_IOS_NAVIGATION_BAR_DONE_BUTTON)] | 463 IDS_IOS_NAVIGATION_BAR_DONE_BUTTON)] |
| 464 performAction:grey_tap()]; | 464 performAction:grey_tap()]; |
| 465 | 465 |
| 466 // Make sure the alert is present. | 466 // Make sure the alert is present. |
| 467 WaitForJavaScripDialogToBeShown(); | 467 WaitForJavaScripDialogToBeShown(); |
| 468 | 468 |
| 469 // Tap the OK button. | 469 // Tap the OK button. |
| 470 TapOK(); | 470 TapOK(); |
| 471 | 471 |
| 472 // Wait for the html body to be reset to the correct value. | 472 // Wait for the html body to be reset to the correct value. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 494 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); | 494 ShowJavaScriptDialog(JavaScriptAlertType::ALERT); |
| 495 | 495 |
| 496 // Tap the OK button. | 496 // Tap the OK button. |
| 497 TapOK(); | 497 TapOK(); |
| 498 | 498 |
| 499 // Wait for the html body to be reset to the correct value. | 499 // Wait for the html body to be reset to the correct value. |
| 500 WaitForWebDisplay(kAlertResultBody); | 500 WaitForWebDisplay(kAlertResultBody); |
| 501 } | 501 } |
| 502 | 502 |
| 503 @end | 503 @end |
| OLD | NEW |