diff --git a/ios/chrome/test/earl_grey/BUILD.gn b/ios/chrome/test/earl_grey/BUILD.gn index ba1edacdad8c82..568a38de2b4af2 100644 --- a/ios/chrome/test/earl_grey/BUILD.gn +++ b/ios/chrome/test/earl_grey/BUILD.gn @@ -245,6 +245,8 @@ source_set("test_support") { "chrome_matchers_app_interface.mm", "chrome_test_case.h", "chrome_test_case.mm", + "chrome_test_case_app_interface.h", + "chrome_test_case_app_interface.mm", ] deps = [ @@ -332,6 +334,8 @@ source_set("eg_app_support+eg2") { "chrome_earl_grey_app_interface.mm", "chrome_matchers_app_interface.h", "chrome_matchers_app_interface.mm", + "chrome_test_case_app_interface.h", + "chrome_test_case_app_interface.mm", ] deps = [ @@ -392,6 +396,9 @@ source_set("eg_test_support+eg2") { "chrome_matchers.h", "chrome_matchers.mm", "chrome_matchers_app_interface.h", + "chrome_test_case.h", + "chrome_test_case.mm", + "chrome_test_case_app_interface.h", ] deps = [ @@ -406,6 +413,8 @@ source_set("eg_test_support+eg2") { "//ios/testing/earl_grey:eg_test_support+eg2", "//ios/third_party/earl_grey2:test_lib", "//ios/web/public/test:element_selector", + "//ios/web/public/test/http_server", + "//net:test_support", "//ui/base", "//url", ] diff --git a/ios/chrome/test/earl_grey/chrome_test_case.mm b/ios/chrome/test/earl_grey/chrome_test_case.mm index 684e02e4535c3a..0e4cfba0c1c819 100644 --- a/ios/chrome/test/earl_grey/chrome_test_case.mm +++ b/ios/chrome/test/earl_grey/chrome_test_case.mm @@ -3,22 +3,18 @@ // found in the LICENSE file. #import "ios/chrome/test/earl_grey/chrome_test_case.h" -#import "ios/chrome/test/earl_grey/chrome_error_util.h" #import -#import - #include #include "base/command_line.h" -#include "base/mac/scoped_block.h" #include "base/strings/sys_string_conversions.h" -#include "components/signin/core/browser/signin_switches.h" -#import "ios/chrome/test/app/chrome_test_util.h" -#include "ios/chrome/test/app/signin_test_util.h" #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" +#import "ios/chrome/test/earl_grey/chrome_error_util.h" +#import "ios/chrome/test/earl_grey/chrome_test_case_app_interface.h" #import "ios/testing/earl_grey/coverage_utils.h" +#import "ios/testing/earl_grey/earl_grey_test.h" #import "ios/web/public/test/http_server/http_server.h" #if !defined(__has_feature) || !__has_feature(objc_arc) @@ -81,8 +77,36 @@ const CFTimeInterval kDrainTimeout = 5; +void SetUpMockAuthentication() { + [ChromeTestCaseAppInterface setUpMockAuthentication]; +} + +void TearDownMockAuthentication() { + [ChromeTestCaseAppInterface tearDownMockAuthentication]; +} + +void ResetAuthentication() { + [ChromeTestCaseAppInterface resetAuthentication]; +} + +void RemoveInfoBarsAndPresentedState() { + [ChromeTestCaseAppInterface removeInfoBarsAndPresentedState]; +} + +UIDeviceOrientation GetCurrentDeviceOrientation() { +#if defined(CHROME_EARL_GREY_1) + return [[UIDevice currentDevice] orientation]; +#elif defined(CHROME_EARL_GREY_2) + return [[GREY_REMOTE_CLASS_IN_APP(UIDevice) currentDevice] orientation]; +#endif +} + } // namespace +#if defined(CHROME_EARL_GREY_2) +GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(ChromeTestCaseAppInterface) +#endif + @interface ChromeTestCase () { // Block to be executed during object tearDown. ProceduralBlock _tearDownHandler; @@ -165,8 +189,7 @@ + (void)setUp { // ensure the UI is in a clean state. [self removeAnyOpenMenusAndInfoBars]; [self closeAllTabs]; - CHROME_EG_ASSERT_NO_ERROR( - [ChromeEarlGrey setContentSettings:CONTENT_SETTING_DEFAULT]); + [ChromeEarlGrey setContentSettings:CONTENT_SETTING_DEFAULT]; [CoverageUtils configureCoverageReportPath]; } @@ -194,16 +217,13 @@ - (void)setUp { _isHTTPServerStopped = NO; _isMockAuthenticationDisabled = NO; _tearDownHandler = nil; - _originalOrientation = [[UIDevice currentDevice] orientation]; + _originalOrientation = GetCurrentDeviceOrientation(); - chrome_test_util::ResetSigninPromoPreferences(); - chrome_test_util::ResetMockAuthentication(); + ResetAuthentication(); // Reset any remaining sign-in state from previous tests. - GREYAssert(chrome_test_util::SignOutAndClearAccounts(), - @"Failed to clean up the sign-in state before starting the test."); - - CHROME_EG_ASSERT_NO_ERROR([ChromeEarlGrey openNewTab]); + [ChromeEarlGrey signOutAndClearAccounts]; + [ChromeEarlGrey openNewTab]; } // Tear down called once per test, to close all tabs and menus, and clear the @@ -215,7 +235,7 @@ - (void)tearDown { } // Clear any remaining test accounts and signed in users. - chrome_test_util::SignOutAndClearAccounts(); + [ChromeEarlGrey signOutAndClearAccounts]; // Re-start anything that was disabled this test, so it is running when the // next test starts. @@ -233,10 +253,16 @@ - (void)tearDown { [[self class] removeAnyOpenMenusAndInfoBars]; [[self class] closeAllTabs]; - if ([[UIDevice currentDevice] orientation] != _originalOrientation) { + if (GetCurrentDeviceOrientation() != _originalOrientation) { // Rotate the device back to the original orientation, since some tests // attempt to run in other orientations. +#if defined(CHROME_EARL_GREY_1) [EarlGrey rotateDeviceToOrientation:_originalOrientation errorOrNil:nil]; +#elif defined(CHROME_EARL_GREY_2) + [EarlGrey rotateDeviceToOrientation:_originalOrientation error:nil]; +#else +#error Neither CHROME_EARL_GREY_1 nor CHROME_EARL_GREY_2 are defined +#endif } [super tearDown]; } @@ -250,8 +276,7 @@ - (void)setTearDownHandler:(ProceduralBlock)tearDownHandler { } + (void)removeAnyOpenMenusAndInfoBars { - chrome_test_util::RemoveAllInfoBars(); - chrome_test_util::ClearPresentedState(); + RemoveInfoBarsAndPresentedState(); // After programatically removing UI elements, allow Earl Grey's // UI synchronization to become idle, so subsequent steps won't start before // the UI is in a good state. @@ -285,15 +310,13 @@ - (void)stopHTTPServer { + (void)disableMockAuthentication { // Make sure local data is cleared, before disabling mock authentication, // where data may be sent to real servers. - chrome_test_util::SignOutAndClearAccounts(); + [ChromeEarlGrey signOutAndClearAccounts]; [ChromeEarlGrey tearDownFakeSyncServer]; - chrome_test_util::TearDownMockAccountReconcilor(); - chrome_test_util::TearDownMockAuthentication(); + TearDownMockAuthentication(); } + (void)enableMockAuthentication { - chrome_test_util::SetUpMockAuthentication(); - chrome_test_util::SetUpMockAccountReconcilor(); + SetUpMockAuthentication(); [ChromeEarlGrey setUpFakeSyncServer]; } diff --git a/ios/chrome/test/earl_grey/chrome_test_case_app_interface.h b/ios/chrome/test/earl_grey/chrome_test_case_app_interface.h new file mode 100644 index 00000000000000..3bb2d8f538c9e9 --- /dev/null +++ b/ios/chrome/test/earl_grey/chrome_test_case_app_interface.h @@ -0,0 +1,26 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef IOS_CHROME_TEST_EARL_GREY_CHROME_TEST_CASE_APP_INTERFACE_H_ +#define IOS_CHROME_TEST_EARL_GREY_CHROME_TEST_CASE_APP_INTERFACE_H_ + +#import + +@interface ChromeTestCaseAppInterface : NSObject + +// Sets up mock authentication and the mock account reconcilor. ++ (void)setUpMockAuthentication; + +// Tears down mock authentication and the mock account reconcilor. ++ (void)tearDownMockAuthentication; + +// Resets mock authentication and signin promo settings. ++ (void)resetAuthentication; + +// Removes all infobars and clears any presented state. ++ (void)removeInfoBarsAndPresentedState; + +@end + +#endif // IOS_CHROME_TEST_EARL_GREY_CHROME_TEST_CASE_APP_INTERFACE_H_ diff --git a/ios/chrome/test/earl_grey/chrome_test_case_app_interface.mm b/ios/chrome/test/earl_grey/chrome_test_case_app_interface.mm new file mode 100644 index 00000000000000..c89d432e4b9436 --- /dev/null +++ b/ios/chrome/test/earl_grey/chrome_test_case_app_interface.mm @@ -0,0 +1,36 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "ios/chrome/test/earl_grey/chrome_test_case_app_interface.h" + +#import "ios/chrome/test/app/chrome_test_util.h" +#include "ios/chrome/test/app/signin_test_util.h" + +#if !defined(__has_feature) || !__has_feature(objc_arc) +#error "This file requires ARC support." +#endif + +@implementation ChromeTestCaseAppInterface + ++ (void)setUpMockAuthentication { + chrome_test_util::SetUpMockAuthentication(); + chrome_test_util::SetUpMockAccountReconcilor(); +} + ++ (void)tearDownMockAuthentication { + chrome_test_util::TearDownMockAccountReconcilor(); + chrome_test_util::TearDownMockAuthentication(); +} + ++ (void)resetAuthentication { + chrome_test_util::ResetSigninPromoPreferences(); + chrome_test_util::ResetMockAuthentication(); +} + ++ (void)removeInfoBarsAndPresentedState { + chrome_test_util::RemoveAllInfoBars(); + chrome_test_util::ClearPresentedState(); +} + +@end