1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/public/cpp/test/shell_test_api.h"
6 #include "base/memory/raw_ptr.h"
10 #include "ash/accelerators/accelerator_commands.h"
11 #include "ash/accelerators/accelerator_controller_impl.h"
12 #include "ash/accelerometer/accelerometer_reader.h"
13 #include "ash/hud_display/hud_display.h"
14 #include "ash/keyboard/keyboard_controller_impl.h"
15 #include "ash/public/cpp/autotest_private_api_utils.h"
16 #include "ash/public/cpp/tablet_mode_observer.h"
17 #include "ash/root_window_controller.h"
18 #include "ash/shell.h"
19 #include "ash/system/message_center/session_state_notification_blocker.h"
20 #include "ash/system/power/backlights_forced_off_setter.h"
21 #include "ash/system/power/power_button_controller.h"
22 #include "ash/wm/overview/overview_animation_state_waiter.h"
23 #include "ash/wm/overview/overview_controller.h"
24 #include "ash/wm/splitview/split_view_controller.h"
25 #include "ash/wm/tablet_mode/tablet_mode_controller.h"
26 #include "ash/wm/workspace_controller.h"
27 #include "base/functional/bind.h"
28 #include "base/run_loop.h"
29 #include "components/prefs/testing_pref_service.h"
30 #include "ui/compositor/compositor.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/compositor/layer_animation_observer.h"
33 #include "ui/compositor/layer_animator.h"
34 #include "ui/display/manager/display_manager.h"
35 #include "ui/events/devices/device_data_manager_test_api.h"
36 #include "ui/events/gesture_detection/gesture_configuration.h"
41 class WindowAnimationWaiter : public ui::LayerAnimationObserver {
43 explicit WindowAnimationWaiter(aura::Window* window)
44 : animator_(window->layer()->GetAnimator()) {
45 animator_->AddObserver(this);
47 ~WindowAnimationWaiter() override = default;
49 WindowAnimationWaiter(const WindowAnimationWaiter& other) = delete;
50 WindowAnimationWaiter& operator=(const WindowAnimationWaiter& rhs) = delete;
52 // ui::LayerAnimationObserver:
53 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
54 if (!animator_->is_animating()) {
55 animator_->RemoveObserver(this);
59 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
60 void OnLayerAnimationScheduled(
61 ui::LayerAnimationSequence* sequence) override {}
63 void Wait() { run_loop_.Run(); }
66 raw_ptr<ui::LayerAnimator, DanglingUntriaged | ExperimentalAsh> animator_;
67 base::RunLoop run_loop_;
72 ShellTestApi::ShellTestApi() : shell_(Shell::Get()) {}
73 ShellTestApi::~ShellTestApi() = default;
76 void ShellTestApi::SetTabletControllerUseScreenshotForTest(
77 bool use_screenshot) {
78 TabletModeController::SetUseScreenshotForTest(use_screenshot);
82 void ShellTestApi::SetUseLoginNotificationDelayForTest(bool use_delay) {
83 SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(
87 MessageCenterController* ShellTestApi::message_center_controller() {
88 return shell_->message_center_controller_.get();
91 WorkspaceController* ShellTestApi::workspace_controller() {
92 // TODO(afakhry): Split this into two, one for root, and one for context.
93 return GetActiveWorkspaceController(shell_->GetPrimaryRootWindow());
96 ScreenPositionController* ShellTestApi::screen_position_controller() {
97 return shell_->screen_position_controller_.get();
100 NativeCursorManagerAsh* ShellTestApi::native_cursor_manager_ash() {
101 return shell_->native_cursor_manager_;
104 DragDropController* ShellTestApi::drag_drop_controller() {
105 return shell_->drag_drop_controller_.get();
108 PowerPrefs* ShellTestApi::power_prefs() {
109 return shell_->power_prefs_.get();
112 display::DisplayManager* ShellTestApi::display_manager() {
113 return shell_->display_manager();
116 void ShellTestApi::ResetPowerButtonControllerForTest() {
117 shell_->backlights_forced_off_setter_->ResetForTest();
118 shell_->power_button_controller_.reset();
119 shell_->power_button_controller_ = std::make_unique<PowerButtonController>(
120 shell_->backlights_forced_off_setter_.get());
123 void ShellTestApi::SimulateModalWindowOpenForTest(bool modal_window_open) {
124 shell_->simulate_modal_window_open_for_test_ = modal_window_open;
127 bool ShellTestApi::IsSystemModalWindowOpen() {
128 return Shell::IsSystemModalWindowOpen();
131 void ShellTestApi::SetTabletModeEnabledForTest(bool enable) {
132 // Detach mouse devices, so we can enter tablet mode.
133 // Calling RunUntilIdle() here is necessary before setting the mouse devices
134 // to prevent the callback from evdev thread from overwriting whatever we set
135 // here below. See `InputDeviceFactoryEvdevProxy::OnStartupScanComplete()`.
136 base::RunLoop().RunUntilIdle();
137 ui::DeviceDataManagerTestApi().OnDeviceListsComplete();
138 ui::DeviceDataManagerTestApi().SetMouseDevices({});
140 TabletMode::Waiter waiter(enable);
141 shell_->tablet_mode_controller()->SetEnabledForTest(enable);
145 void ShellTestApi::EnableVirtualKeyboard() {
146 shell_->keyboard_controller()->SetEnableFlag(
147 keyboard::KeyboardEnableFlag::kCommandLineEnabled);
150 void ShellTestApi::ToggleFullscreen() {
151 accelerators::ToggleFullscreen();
154 void ShellTestApi::AddRemoveDisplay() {
155 shell_->display_manager()->AddRemoveDisplay();
158 void ShellTestApi::WaitForOverviewAnimationState(OverviewAnimationState state) {
159 auto* overview_controller = shell_->overview_controller();
160 if (state == OverviewAnimationState::kEnterAnimationComplete &&
161 overview_controller->InOverviewSession() &&
162 !overview_controller->IsInStartAnimation()) {
163 // If there is no animation applied, call the callback immediately.
166 if (state == OverviewAnimationState::kExitAnimationComplete &&
167 !overview_controller->InOverviewSession() &&
168 !overview_controller->IsCompletingShutdownAnimations()) {
169 // If there is no animation applied, call the callback immediately.
172 base::RunLoop run_loop;
173 new OverviewAnimationStateWaiter(
174 state, base::BindOnce([](base::RunLoop* run_loop,
175 bool finished) { run_loop->QuitWhenIdle(); },
176 base::Unretained(&run_loop)));
180 void ShellTestApi::WaitForWindowFinishAnimating(aura::Window* window) {
181 WindowAnimationWaiter waiter(window);
185 bool ShellTestApi::IsContextMenuShown() const {
186 return Shell::GetPrimaryRootWindowController()->IsContextMenuShown();
189 bool ShellTestApi::IsActionForAcceleratorEnabled(
190 const ui::Accelerator& accelerator) const {
191 auto* controller = Shell::Get()->accelerator_controller();
192 return AcceleratorControllerImpl::TestApi(controller)
193 .IsActionForAcceleratorEnabled(accelerator);
196 bool ShellTestApi::PressAccelerator(const ui::Accelerator& accelerator) {
197 return Shell::Get()->accelerator_controller()->AcceleratorPressed(
201 bool ShellTestApi::IsHUDShown() {
202 return hud_display::HUDDisplayView::IsShown();