From e30b83a56306745a4c3359a761b5deb0dc93b4ed Mon Sep 17 00:00:00 2001 From: zhaoyangli Date: Tue, 28 Apr 2020 19:45:36 +0000 Subject: [PATCH] [iOS][EG2] Handle iOS upgrade alert before EG2 tests start Handles unknown system dialog by catching the exception. If it's a system update alert, choose later. If not, deny it. Bug: 1072000 Change-Id: Ifc12a37771c247d2f68c7f73ac0169aa337e5dda Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159916 Commit-Queue: Zhaoyang Li Reviewed-by: Eugene But Cr-Commit-Position: refs/heads/master@{#763491} --- .../earl_grey/base_earl_grey_test_case.mm | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/ios/testing/earl_grey/base_earl_grey_test_case.mm b/ios/testing/earl_grey/base_earl_grey_test_case.mm index ca257ea6ca07e6..e4557dad23e544 100644 --- a/ios/testing/earl_grey/base_earl_grey_test_case.mm +++ b/ios/testing/earl_grey/base_earl_grey_test_case.mm @@ -90,10 +90,14 @@ - (void)handleSystemAlertIfVisible { GREYAssertNil(alertGetTextError, @"Error getting alert text.\n%@", alertGetTextError); - // If the system alert is of a known type, accept it. - // Otherwise, reject it, as unknown types include alerts which are not - // desirable to accept, including OS upgrades. - if ([self grey_systemAlertType] != GREYSystemAlertTypeUnknown) { + @try { + // TODO(crbug.com/1073542): Style guide does not allow throwing + // exceptions. This call throws an NSInternalInconsistencyException when + // the system alert is unknown in EG2 framework. The exception will be + // handled in @catch. Otherwise the system alert is of a known type, + // accept it. + [self grey_systemAlertType]; + DLOG(WARNING) << "Accepting iOS system alert: " << base::SysNSStringToUTF8(alertText); @@ -101,16 +105,41 @@ - (void)handleSystemAlertIfVisible { [self grey_acceptSystemDialogWithError:&acceptAlertError]; GREYAssertNil(acceptAlertError, @"Error accepting system alert.\n%@", acceptAlertError); - } else { - DLOG(WARNING) << "Denying iOS system alert of unknown type: " - << base::SysNSStringToUTF8(alertText); - NSError* denyAlertError = nil; - [self grey_denySystemDialogWithError:&denyAlertError]; - GREYAssertNil(denyAlertError, @"Error denying system alert.\n%@", - denyAlertError); + } @catch (NSException* exception) { + GREYAssert( + (exception.name == NSInternalInconsistencyException && + [exception.reason rangeOfString:@"Invalid System Alert."].location != + NSNotFound), + @"Unknown error caught when handling unknown system alert: %@", + exception.reason); + // If the unsupported alert is iOS upgrade alert, choose "Later". + // Otherwise, reject it, as unknown types include alerts which are not + // desirable to accept. + if ([alertText isEqualToString:@"Software Update"]) { + DLOG(WARNING) << "Denying iOS system alert of Software Update!"; + + NSError* dismissingUpgradeAlertError = nil; + [self grey_tapSystemDialogButtonWithText:@"Later" + error:&dismissingUpgradeAlertError]; + GREYAssertNil(dismissingUpgradeAlertError, + @"Error denying Software Update alert.\n%@", + dismissingUpgradeAlertError); + + } else { + DLOG(WARNING) << "Denying iOS system alert of unknown type: " + << base::SysNSStringToUTF8(alertText); + + NSError* denyAlertError = nil; + [self grey_denySystemDialogWithError:&denyAlertError]; + GREYAssertNil(denyAlertError, @"Error denying system alert.\n%@", + denyAlertError); + } } } + // Ensures no visible alert after handling. + [self grey_waitForAlertVisibility:NO + withTimeout:kSystemAlertVisibilityTimeout]; #endif // CHROME_EARL_GREY_2 }