Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / ash / shell_test_api.cc
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.
4
5 #include "ash/public/cpp/test/shell_test_api.h"
6 #include "base/memory/raw_ptr.h"
7
8 #include <memory>
9
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"
37
38 namespace ash {
39 namespace {
40
41 class WindowAnimationWaiter : public ui::LayerAnimationObserver {
42  public:
43   explicit WindowAnimationWaiter(aura::Window* window)
44       : animator_(window->layer()->GetAnimator()) {
45     animator_->AddObserver(this);
46   }
47   ~WindowAnimationWaiter() override = default;
48
49   WindowAnimationWaiter(const WindowAnimationWaiter& other) = delete;
50   WindowAnimationWaiter& operator=(const WindowAnimationWaiter& rhs) = delete;
51
52   // ui::LayerAnimationObserver:
53   void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
54     if (!animator_->is_animating()) {
55       animator_->RemoveObserver(this);
56       run_loop_.Quit();
57     }
58   }
59   void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
60   void OnLayerAnimationScheduled(
61       ui::LayerAnimationSequence* sequence) override {}
62
63   void Wait() { run_loop_.Run(); }
64
65  private:
66   raw_ptr<ui::LayerAnimator, DanglingUntriaged | ExperimentalAsh> animator_;
67   base::RunLoop run_loop_;
68 };
69
70 }  // namespace
71
72 ShellTestApi::ShellTestApi() : shell_(Shell::Get()) {}
73 ShellTestApi::~ShellTestApi() = default;
74
75 // static
76 void ShellTestApi::SetTabletControllerUseScreenshotForTest(
77     bool use_screenshot) {
78   TabletModeController::SetUseScreenshotForTest(use_screenshot);
79 }
80
81 // static
82 void ShellTestApi::SetUseLoginNotificationDelayForTest(bool use_delay) {
83   SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(
84       use_delay);
85 }
86
87 MessageCenterController* ShellTestApi::message_center_controller() {
88   return shell_->message_center_controller_.get();
89 }
90
91 WorkspaceController* ShellTestApi::workspace_controller() {
92   // TODO(afakhry): Split this into two, one for root, and one for context.
93   return GetActiveWorkspaceController(shell_->GetPrimaryRootWindow());
94 }
95
96 ScreenPositionController* ShellTestApi::screen_position_controller() {
97   return shell_->screen_position_controller_.get();
98 }
99
100 NativeCursorManagerAsh* ShellTestApi::native_cursor_manager_ash() {
101   return shell_->native_cursor_manager_;
102 }
103
104 DragDropController* ShellTestApi::drag_drop_controller() {
105   return shell_->drag_drop_controller_.get();
106 }
107
108 PowerPrefs* ShellTestApi::power_prefs() {
109   return shell_->power_prefs_.get();
110 }
111
112 display::DisplayManager* ShellTestApi::display_manager() {
113   return shell_->display_manager();
114 }
115
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());
121 }
122
123 void ShellTestApi::SimulateModalWindowOpenForTest(bool modal_window_open) {
124   shell_->simulate_modal_window_open_for_test_ = modal_window_open;
125 }
126
127 bool ShellTestApi::IsSystemModalWindowOpen() {
128   return Shell::IsSystemModalWindowOpen();
129 }
130
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({});
139
140   TabletMode::Waiter waiter(enable);
141   shell_->tablet_mode_controller()->SetEnabledForTest(enable);
142   waiter.Wait();
143 }
144
145 void ShellTestApi::EnableVirtualKeyboard() {
146   shell_->keyboard_controller()->SetEnableFlag(
147       keyboard::KeyboardEnableFlag::kCommandLineEnabled);
148 }
149
150 void ShellTestApi::ToggleFullscreen() {
151   accelerators::ToggleFullscreen();
152 }
153
154 void ShellTestApi::AddRemoveDisplay() {
155   shell_->display_manager()->AddRemoveDisplay();
156 }
157
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.
164     return;
165   }
166   if (state == OverviewAnimationState::kExitAnimationComplete &&
167       !overview_controller->InOverviewSession() &&
168       !overview_controller->IsCompletingShutdownAnimations()) {
169     // If there is no animation applied, call the callback immediately.
170     return;
171   }
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)));
177   run_loop.Run();
178 }
179
180 void ShellTestApi::WaitForWindowFinishAnimating(aura::Window* window) {
181   WindowAnimationWaiter waiter(window);
182   waiter.Wait();
183 }
184
185 bool ShellTestApi::IsContextMenuShown() const {
186   return Shell::GetPrimaryRootWindowController()->IsContextMenuShown();
187 }
188
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);
194 }
195
196 bool ShellTestApi::PressAccelerator(const ui::Accelerator& accelerator) {
197   return Shell::Get()->accelerator_controller()->AcceleratorPressed(
198       accelerator);
199 }
200
201 bool ShellTestApi::IsHUDShown() {
202   return hud_display::HUDDisplayView::IsShown();
203 }
204
205 }  // namespace ash