Commit 8e87ef93 authored by Eric Aleshire's avatar Eric Aleshire Committed by Commit Bot

Reland https://chromium-review.googlesource.com/c/chromium/src/+/1635859.

Original CL description:

Implement AppLaunchManager for controlling app-under-test for EG2 tests.

No tests are added to smoke_egtest to test app relaunching, as it descends
from ChromeTestCase, which currently has logic which is required to run
every time the app-under-test is launched. Long-term, it is preferred if
this logic is moved to -setUp and executing before every test method. This
refactor will happen in a later CL as it requires some planning.

Confirmed that smoke_egtest does however only launch the app once with the
change to use AppLaunchManager.

Bug: 922813
Change-Id: I02d7e65cb47d86c35c09d049bc628c0220b4adb8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1641468Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: ericale <ericale@chromium.org>
Cr-Commit-Position: refs/heads/master@{#666024}
parent 4ab01269
......@@ -15,6 +15,8 @@ source_set("earl_grey_support") {
]
sources = [
"app_launch_manager.h",
"app_launch_manager.mm",
"base_earl_grey_test_case.h",
"base_earl_grey_test_case.mm",
"base_eg_test_helper_impl.h",
......@@ -59,6 +61,8 @@ source_set("eg_test_support+eg2") {
testonly = true
sources = [
"app_launch_manager.h",
"app_launch_manager.mm",
"base_earl_grey_test_case.h",
"base_earl_grey_test_case.mm",
"base_eg_test_helper_impl.h",
......
// 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_TESTING_EARL_GREY_APP_LAUNCH_MANAGER_H_
#define IOS_TESTING_EARL_GREY_APP_LAUNCH_MANAGER_H_
#import <Foundation/Foundation.h>
// Provides control of the single application-under-test to EarlGrey 2 tests.
@interface AppLaunchManager : NSObject
// Returns the singleton instance of this class.
+ (AppLaunchManager*)sharedManager;
- (instancetype)init NS_UNAVAILABLE;
// Makes sure the app has been started with the appropriate |arguments|.
// In EG2, will launch the app if any of the following conditions are met:
// * The app is not running
// * The app is currently running with different arguments.
// * |forceRestart| is YES
// Otherwise, the app will be activated instead of (re)launched.
// Will wait until app is activated or launched, and fail the test if it
// fails to do so.
// In EG1, this method is a no-op.
- (void)ensureAppLaunchedWithArgs:(NSArray<NSString*>*)arguments
forceRestart:(BOOL)forceRestart;
@end
#endif // IOS_TESTING_EARL_GREY_APP_LAUNCH_MANAGER_H_
\ No newline at end of file
// 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/testing/earl_grey/app_launch_manager.h"
#import <XCTest/XCTest.h>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#if defined(CHROME_EARL_GREY_2) // avoid unused function warning in EG1
namespace {
// Checks if two pairs of launch arguments are equivalent.
bool LaunchArgumentsAreEqual(NSArray<NSString*>* args1,
NSArray<NSString*>* args2) {
// isEqualToArray will only return true if both arrays are non-nil,
// so first check if both arrays are empty or nil
if (!args1.count && !args2.count) {
return true;
}
return [args1 isEqualToArray:args2];
}
} // namespace
#endif
@interface AppLaunchManager ()
@property(nonatomic) XCUIApplication* runningApplication;
@property(nonatomic) NSArray<NSString*>* currentLaunchArgs;
@end
@implementation AppLaunchManager
+ (AppLaunchManager*)sharedManager {
static AppLaunchManager* instance = nil;
static dispatch_once_t guard;
dispatch_once(&guard, ^{
instance = [[AppLaunchManager alloc] initPrivate];
});
return instance;
}
- (instancetype)initPrivate {
self = [super init];
return self;
}
- (void)ensureAppLaunchedWithArgs:(NSArray<NSString*>*)arguments
forceRestart:(BOOL)forceRestart {
#if defined(CHROME_EARL_GREY_2)
bool appNeedsLaunching =
forceRestart || !self.runningApplication ||
!LaunchArgumentsAreEqual(arguments, self.currentLaunchArgs);
if (!appNeedsLaunching) {
[self.runningApplication activate];
return;
}
XCUIApplication* application = [[XCUIApplication alloc] init];
application.launchArguments = arguments;
[application launch];
self.runningApplication = application;
self.currentLaunchArgs = arguments;
#endif
}
@end
......@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import "ios/testing/earl_grey/app_launch_manager.h"
#import "ios/testing/earl_grey/coverage_utils.h"
#import "ios/testing/earl_grey/earl_grey_test.h"
......@@ -41,11 +42,8 @@
}
- (void)launchAppForTestMethod {
static dispatch_once_t launchAppToken;
dispatch_once(&launchAppToken, ^{
XCUIApplication* application = [[XCUIApplication alloc] init];
[application launch];
});
[[AppLaunchManager sharedManager] ensureAppLaunchedWithArgs:nil
forceRestart:false];
}
// Prevents tests inheriting from this class from putting logic in +setUp.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment