blob: f95fa2c1b3ffffdae4aafacba838c86e7e7025f7 [file] [log] [blame]
// Copyright 2020 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/scoped_key_window.h"
#include "base/check_op.h"
#import "base/ios/ios_util.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
ScopedKeyWindow::ScopedKeyWindow() {
if (base::ios::IsSceneStartupSupported()) {
if (@available(iOS 13, *)) {
NSSet<UIScene*>* scenes =
([[UIApplication sharedApplication] connectedScenes]);
// Only one scene is supported in unittests at the moment.
DCHECK_EQ([scenes count], 1u);
UIScene* scene =
[[[UIApplication sharedApplication] connectedScenes] anyObject];
DCHECK([scene isKindOfClass:[UIWindowScene class]]);
UIWindowScene* windowScene = static_cast<UIWindowScene*>(scene);
for (UIWindow* window in windowScene.windows) {
if ([window isKeyWindow]) {
original_key_window_ = window;
}
}
DCHECK(original_key_window_);
current_key_window_ = [[UIWindow alloc] initWithWindowScene:windowScene];
}
}
if (!current_key_window_) {
current_key_window_ =
[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
original_key_window_ = GetAnyKeyWindow();
}
[current_key_window_ makeKeyAndVisible];
}
ScopedKeyWindow::~ScopedKeyWindow() {
[original_key_window_ makeKeyAndVisible];
}
UIWindow* ScopedKeyWindow::Get() {
return current_key_window_;
}