Commit 54bae1f6 authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

Create window in unittests with Scene API

Change-Id: I4996afbbc8b56b59e979a5f2e2c5a136f281bf6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2403361
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Auto-Submit: Olivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806106}
parent a9415461
...@@ -35,6 +35,28 @@ static base::TestSuite* g_test_suite = NULL; ...@@ -35,6 +35,28 @@ static base::TestSuite* g_test_suite = NULL;
static int g_argc; static int g_argc;
static char** g_argv; static char** g_argv;
namespace {
void PopulateUIWindow(UIWindow* window) {
[window setBackgroundColor:[UIColor whiteColor]];
[window makeKeyAndVisible];
CGRect bounds = [[UIScreen mainScreen] bounds];
// Add a label with the app name.
UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease];
label.text = [[NSProcessInfo processInfo] processName];
label.textAlignment = NSTextAlignmentCenter;
[window addSubview:label];
// An NSInternalInconsistencyException is thrown if the app doesn't have a
// root view controller. Set an empty one here.
[window setRootViewController:[[[UIViewController alloc] init] autorelease]];
}
bool IsSceneStartupEnabled() {
return [[NSBundle mainBundle].infoDictionary
objectForKey:@"UIApplicationSceneManifest"];
}
}
@interface UIApplication (Testing) @interface UIApplication (Testing)
- (void)_terminateWithStatus:(int)status; - (void)_terminateWithStatus:(int)status;
@end @end
...@@ -54,10 +76,10 @@ static char** g_argv; ...@@ -54,10 +76,10 @@ static char** g_argv;
// the application delegate, so they need to be separate objects (the same // the application delegate, so they need to be separate objects (the same
// object can't be both the app and scene delegate, since new scene delegates // object can't be both the app and scene delegate, since new scene delegates
// are created for each scene). // are created for each scene).
@interface ChromeUnitTestSceneDelegate : NSObject <UISceneDelegate> @interface ChromeUnitTestSceneDelegate : NSObject <UIWindowSceneDelegate> {
@end base::scoped_nsobject<UIWindow> _window;
}
@implementation ChromeUnitTestSceneDelegate
@end @end
@interface ChromeUnitTestDelegate : NSObject <GoogleTestRunnerDelegate> { @interface ChromeUnitTestDelegate : NSObject <GoogleTestRunnerDelegate> {
...@@ -66,6 +88,26 @@ static char** g_argv; ...@@ -66,6 +88,26 @@ static char** g_argv;
- (void)runTests; - (void)runTests;
@end @end
@implementation ChromeUnitTestSceneDelegate
- (void)scene:(UIScene*)scene
willConnectToSession:(UISceneSession*)session
options:(UISceneConnectionOptions*)connectionOptions
API_AVAILABLE(ios(13)) {
// Unittests do not support multiple scenes.
DCHECK(![[UIApplication sharedApplication] supportsMultipleScenes]);
// Yes, this is leaked, it's just to make what's running visible.
_window.reset([[UIWindow alloc]
initWithWindowScene:static_cast<UIWindowScene*>(scene)]);
PopulateUIWindow(_window);
}
- (void)sceneDidDisconnect:(UIScene*)scene API_AVAILABLE(ios(13)) {
_window.reset();
}
@end
@implementation ChromeUnitTestDelegate @implementation ChromeUnitTestDelegate
- (BOOL)application:(UIApplication *)application - (BOOL)application:(UIApplication *)application
...@@ -80,22 +122,13 @@ static char** g_argv; ...@@ -80,22 +122,13 @@ static char** g_argv;
[[UIKeyboardImpl sharedInstance] setSoftwareKeyboardShownByTouch:YES]; [[UIKeyboardImpl sharedInstance] setSoftwareKeyboardShownByTouch:YES];
#endif // TARGET_IPHONE_SIMULATOR #endif // TARGET_IPHONE_SIMULATOR
CGRect bounds = [[UIScreen mainScreen] bounds]; if (!IsSceneStartupEnabled()) {
CGRect bounds = [[UIScreen mainScreen] bounds];
// Yes, this is leaked, it's just to make what's running visible. // Yes, this is leaked, it's just to make what's running visible.
_window.reset([[UIWindow alloc] initWithFrame:bounds]); _window.reset([[UIWindow alloc] initWithFrame:bounds]);
[_window setBackgroundColor:[UIColor whiteColor]]; PopulateUIWindow(_window);
[_window makeKeyAndVisible]; }
// Add a label with the app name.
UILabel* label = [[[UILabel alloc] initWithFrame:bounds] autorelease];
label.text = [[NSProcessInfo processInfo] processName];
label.textAlignment = NSTextAlignmentCenter;
[_window addSubview:label];
// An NSInternalInconsistencyException is thrown if the app doesn't have a
// root view controller. Set an empty one here.
[_window setRootViewController:[[[UIViewController alloc] init] autorelease]];
if ([self shouldRedirectOutputToFile]) if ([self shouldRedirectOutputToFile])
[self redirectOutput]; [self redirectOutput];
......
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