blob: 6dba8374bad7f21a1aafd4cc64e381e663489634 [file] [log] [blame]
[email protected]09464c72013-04-26 07:31:281// Copyright 2013 The Chromium Authors. All rights reserved.
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/test/ash_test_helper.h"
6
xiyuan317fdbe2017-05-31 15:02:027#include <algorithm>
xiyuan317fdbe2017-05-31 15:02:028
Zentaro Kavanagh1f436492021-05-20 23:29:369#include "ash/accelerators/accelerator_controller_impl.h"
Harvey Yangd8306592020-10-29 05:09:0810#include "ash/accelerometer/accelerometer_reader.h"
wutao92a8f5c2020-11-09 23:25:5411#include "ash/ambient/test/ambient_ash_test_helper.h"
Scott Violet42d6695782018-06-04 16:12:1712#include "ash/app_list/test/app_list_test_helper.h"
David Blackec4cbcd2020-04-15 20:35:2513#include "ash/assistant/assistant_controller_impl.h"
Xiaohui Chene5d3b432019-06-14 17:51:2414#include "ash/assistant/test/test_assistant_service.h"
Henrique Ferreiro7b693762021-02-18 23:01:4615#include "ash/components/audio/cras_audio_handler.h"
Henrique Ferreiro54c89f82021-06-15 15:41:2816#include "ash/constants/ash_switches.h"
James Cook346c8c62017-07-18 19:38:3717#include "ash/display/display_configuration_controller_test_api.h"
James Cookd3cf52c2018-05-24 14:57:3018#include "ash/display/screen_ash.h"
Peter Kasting1618c312020-03-31 00:46:3619#include "ash/host/ash_window_tree_host.h"
Darren Shen7daf3e12019-07-02 07:25:4920#include "ash/keyboard/keyboard_controller_impl.h"
Jun Mukaif2357792018-12-20 19:58:0021#include "ash/keyboard/test_keyboard_ui.h"
Steven Bennetts99f5b692018-11-27 16:31:4022#include "ash/public/cpp/test/test_keyboard_controller_observer.h"
Jun Mukai688f8b422019-05-22 00:54:4823#include "ash/public/cpp/test/test_new_window_delegate.h"
Scott Violet42d6695782018-06-04 16:12:1724#include "ash/session/test_session_controller_client.h"
[email protected]09464c72013-04-26 07:31:2825#include "ash/shell.h"
Peter Kasting16204ab22020-03-12 03:38:4326#include "ash/shell_init_params.h"
Ahmed Mehfooz812c5422021-03-29 22:42:1627#include "ash/system/message_center/session_state_notification_blocker.h"
James Cookee5fc732019-06-07 14:48:5228#include "ash/system/model/system_tray_model.h"
James Cooka35a1e22017-04-08 02:33:0829#include "ash/system/screen_layout_observer.h"
[email protected]93e828f2014-05-14 14:08:1130#include "ash/test/ash_test_views_delegate.h"
Jun Mukai5a7036332020-07-20 21:35:3531#include "ash/test/toplevel_window.h"
James Cook346c8c62017-07-18 19:38:3732#include "ash/test_shell_delegate.h"
Jeffrey Young708db7f2021-09-20 19:38:4633#include "ash/wallpaper/test_wallpaper_controller_client.h"
Mike Wassermana5352da2019-05-30 17:26:0834#include "ash/wallpaper/wallpaper_controller_impl.h"
Sammie Quon8bac8282019-03-30 03:06:4635#include "ash/wm/overview/overview_controller.h"
Sammie Quona071cc592019-06-12 17:16:4536#include "ash/wm/tablet_mode/tablet_mode_controller.h"
[email protected]09464c72013-04-26 07:31:2837#include "base/run_loop.h"
Mitsuru Oshima513902c2019-07-30 23:56:5538#include "base/system/sys_info.h"
Steven Bennettsc5caf70d12019-04-08 22:12:3939#include "chromeos/dbus/audio/cras_audio_client.h"
Steven Bennetts3330b9f2019-03-15 20:24:1340#include "chromeos/dbus/power/power_policy_controller.h"
Mitsuru Oshimaca97b1082021-01-29 21:12:3841#include "chromeos/login/login_state/login_state.h"
jamescooke96488a2017-01-20 15:49:4442#include "device/bluetooth/bluetooth_adapter_factory.h"
43#include "device/bluetooth/dbus/bluez_dbus_manager.h"
Scott Violetac410622018-08-11 18:33:2244#include "ui/aura/test/test_windows.h"
45#include "ui/aura/window.h"
Peter Kasting86d2b5812020-04-01 22:32:1446#include "ui/aura/window_tree_host.h"
Henrique Ferreiro1220b392021-09-16 19:58:0747#include "ui/base/ime/ash/mock_input_method_manager.h"
varkhac8621dd2017-05-31 19:09:0148#include "ui/display/display.h"
wutaob161bf22017-04-26 16:55:0349#include "ui/display/display_switches.h"
skyd06586d2017-02-22 17:46:3650#include "ui/display/manager/display_manager.h"
rjkroege72f8154f2016-10-29 00:49:0251#include "ui/display/test/display_manager_test_api.h"
Peter Kasting86d2b5812020-04-01 22:32:1452#include "ui/events/gesture_detection/gesture_configuration.h"
53#include "ui/gfx/geometry/point.h"
Maksim Sisovf6382c22019-09-03 13:36:3854#include "ui/platform_window/common/platform_window_defaults.h"
Peter Kasting0639dc72020-03-27 16:43:5455#include "ui/views/test/views_test_helper_aura.h"
[email protected]ee3ed10772014-03-11 22:02:0156#include "ui/wm/core/capture_controller.h"
anantaf2b280f2016-04-13 21:49:3557#include "ui/wm/core/cursor_manager.h"
Peter Kasting1618c312020-03-31 00:46:3658#include "ui/wm/core/focus_controller.h"
[email protected]09464c72013-04-26 07:31:2859
60namespace ash {
[email protected]09464c72013-04-26 07:31:2861
Peter Kasting0639dc72020-03-27 16:43:5462namespace {
Peter Kasting37122d02020-04-02 00:08:5563std::unique_ptr<views::TestViewsDelegate> MakeTestViewsDelegate() {
Peter Kasting0639dc72020-03-27 16:43:5464 return std::make_unique<AshTestViewsDelegate>();
65}
66} // namespace
67
Peter Kasting16204ab22020-03-12 03:38:4368AshTestHelper::InitParams::InitParams() = default;
69AshTestHelper::InitParams::InitParams(InitParams&&) = default;
70AshTestHelper::InitParams::~InitParams() = default;
71
Peter Kasting4d66384d2020-03-30 23:28:4972class AshTestHelper::BluezDBusManagerInitializer {
73 public:
74 BluezDBusManagerInitializer() { bluez::BluezDBusManager::InitializeFake(); }
75 ~BluezDBusManagerInitializer() {
76 device::BluetoothAdapterFactory::Shutdown();
77 bluez::BluezDBusManager::Shutdown();
78 }
79};
80
81class AshTestHelper::PowerPolicyControllerInitializer {
82 public:
83 PowerPolicyControllerInitializer() {
84 chromeos::PowerPolicyController::Initialize(
85 chromeos::PowerManagerClient::Get());
86 }
87 ~PowerPolicyControllerInitializer() {
88 chromeos::PowerPolicyController::Shutdown();
89 }
90};
91
James Cook0eda2542021-03-30 23:49:1992AshTestHelper::AshTestHelper(ui::ContextFactory* context_factory)
93 : AuraTestHelper(context_factory) {
Peter Kasting0639dc72020-03-27 16:43:5494 views::ViewsTestHelperAura::SetFallbackTestViewsDelegateFactory(
Peter Kasting37122d02020-04-02 00:08:5595 &MakeTestViewsDelegate);
Peter Kasting9eaedfd92020-03-30 23:24:5096
97 // TODO(jamescook): Can we do this without changing command line?
Peter Kasting4d66384d2020-03-30 23:28:4998 // Use the origin (1,1) so that it doesn't overlap with the native mouse
99 // cursor.
Peter Kasting9eaedfd92020-03-30 23:24:50100 if (!base::SysInfo::IsRunningOnChromeOS() &&
101 !command_line_->GetProcessCommandLine()->HasSwitch(
102 ::switches::kHostWindowBounds)) {
103 // TODO(oshima): Disable native events instead of adding offset.
104 command_line_->GetProcessCommandLine()->AppendSwitchASCII(
105 ::switches::kHostWindowBounds, "10+10-800x600");
106 }
107
James Cook0eda2542021-03-30 23:49:19108 TabletModeController::SetUseScreenshotForTest(false);
Peter Kasting9eaedfd92020-03-30 23:24:50109
James Cook0eda2542021-03-30 23:49:19110 display::ResetDisplayIdForTest();
Peter Kasting9eaedfd92020-03-30 23:24:50111
112 chromeos::CrasAudioClient::InitializeFake();
113 // Create CrasAudioHandler for testing since g_browser_process is not
114 // created in AshTestBase tests.
Henrique Ferreiro3d9d93d2021-02-19 22:17:39115 CrasAudioHandler::InitializeForTesting();
Peter Kasting9eaedfd92020-03-30 23:24:50116
117 // Reset the global state for the cursor manager. This includes the
118 // last cursor visibility state, etc.
Peter Kasting4d66384d2020-03-30 23:28:49119 wm::CursorManager::ResetCursorVisibilityStateForTest();
Peter Kasting86d2b5812020-04-01 22:32:14120
121 // Clears the saved state so that test doesn't use on the wrong
122 // default state.
123 shell::ToplevelWindow::ClearSavedStateForTest();
Peter Kasting0639dc72020-03-27 16:43:54124}
[email protected]09464c72013-04-26 07:31:28125
James Cookd3cf52c2018-05-24 14:57:30126AshTestHelper::~AshTestHelper() {
Peter Kasting3b81c72f2020-03-30 23:28:19127 if (app_list_test_helper_)
128 TearDown();
129
Peter Kastingec1e1cfb2020-03-26 23:11:18130 // Ensure the next test starts with a null display::Screen. This must be done
131 // here instead of in TearDown() since some tests test access to the Screen
132 // after the shell shuts down (which they use TearDown() to trigger).
James Cookd3cf52c2018-05-24 14:57:30133 ScreenAsh::DeleteScreenForShutdown();
Peter Kasting0639dc72020-03-27 16:43:54134
135 // This should never have a meaningful effect, since either there is no
136 // ViewsTestHelperAura instance or the instance is currently in its
137 // destructor.
Peter Kasting37122d02020-04-02 00:08:55138 views::ViewsTestHelperAura::SetFallbackTestViewsDelegateFactory(nullptr);
James Cookd3cf52c2018-05-24 14:57:30139}
[email protected]09464c72013-04-26 07:31:28140
Peter Kasting9eaedfd92020-03-30 23:24:50141void AshTestHelper::SetUp() {
142 SetUp(InitParams());
[email protected]09464c72013-04-26 07:31:28143}
144
145void AshTestHelper::TearDown() {
wutao92a8f5c2020-11-09 23:25:54146 ambient_ash_test_helper_.reset();
147
Peter Kastingec1e1cfb2020-03-26 23:11:18148 // The AppListTestHelper holds a pointer to the AppListController the Shell
149 // owns, so shut the test helper down first.
Jiaquan Hedbfd4512018-03-02 02:16:38150 app_list_test_helper_.reset();
151
Scott Violetc43ea0c62018-07-20 15:13:00152 Shell::DeleteInstance();
starazd8f4f642016-12-10 23:49:20153 // Suspend the tear down until all resources are returned via
danakj2e3aca02017-06-23 22:58:52154 // CompositorFrameSinkClient::ReclaimResources()
Steven Bennettsa1f686d2018-06-05 18:01:19155 base::RunLoop().RunUntilIdle();
benb91330f2015-10-03 22:20:18156
Mitsuru Oshimaca97b1082021-01-29 21:12:38157 chromeos::LoginState::Shutdown();
158
Henrique Ferreiro3d9d93d2021-02-19 22:17:39159 CrasAudioHandler::Shutdown();
Steven Bennetts10ecd9d2019-04-08 20:36:46160 chromeos::CrasAudioClient::Shutdown();
skyd06586d2017-02-22 17:46:36161
Peter Kastingec1e1cfb2020-03-26 23:11:18162 // The PowerPolicyController holds a pointer to the PowerManagementClient, so
163 // shut the controller down first.
Peter Kasting4d66384d2020-03-30 23:28:49164 power_policy_controller_initializer_.reset();
Evan Stade523f7fc2019-03-02 19:20:51165 chromeos::PowerManagerClient::Shutdown();
166
Peter Kastingec1e1cfb2020-03-26 23:11:18167 TabletModeController::SetUseScreenshotForTest(true);
168
169 // Destroy all owned objects to prevent tests from depending on their state
170 // after this returns.
171 test_keyboard_controller_observer_.reset();
Peter Kasting9eaedfd92020-03-30 23:24:50172 session_controller_client_.reset();
Peter Kastingec1e1cfb2020-03-26 23:11:18173 test_views_delegate_.reset();
Hidehiko Abe6e3e57f42021-03-23 11:23:58174 new_window_delegate_provider_.reset();
Peter Kasting4d66384d2020-03-30 23:28:49175 bluez_dbus_manager_initializer_.reset();
Peter Kastingec1e1cfb2020-03-26 23:11:18176 system_tray_client_.reset();
177 assistant_service_.reset();
178 notifier_settings_controller_.reset();
179 prefs_provider_.reset();
180 statistics_provider_.reset();
181 command_line_.reset();
skyd06586d2017-02-22 17:46:36182
Peter Kasting1618c312020-03-31 00:46:36183 AuraTestHelper::TearDown();
Zentaro Kavanagh930de992021-05-10 15:51:07184
185 // Cleanup the global state for InputMethodManager, but only if
186 // it was setup by this test helper. This allows tests to implement
187 // their own override, and in that case we shouldn't call Shutdown
188 // otherwise the global state will be deleted twice.
189 if (input_method_manager_) {
Henrique Ferreiroe08cf1b2021-09-22 19:33:58190 input_method::InputMethodManager::Shutdown();
Zentaro Kavanagh930de992021-05-10 15:51:07191 input_method_manager_ = nullptr;
192 }
Scott Violetac410622018-08-11 18:33:22193}
194
Peter Kasting97defc92020-03-05 18:49:02195aura::Window* AshTestHelper::GetContext() {
sky27344382017-03-08 21:30:32196 aura::Window* root_window = Shell::GetRootWindowForNewWindows();
[email protected]09464c72013-04-26 07:31:28197 if (!root_window)
198 root_window = Shell::GetPrimaryRootWindow();
199 DCHECK(root_window);
200 return root_window;
201}
202
Peter Kasting1618c312020-03-31 00:46:36203aura::WindowTreeHost* AshTestHelper::GetHost() {
204 auto* manager = Shell::Get()->window_tree_host_manager();
205 const int64_t id = manager->GetPrimaryDisplayId();
206 return manager->GetAshWindowTreeHostForDisplayId(id)->AsWindowTreeHost();
207}
208
209aura::TestScreen* AshTestHelper::GetTestScreen() {
210 // If a test needs this, we may need to refactor TestScreen such that its
211 // methods can operate atop some sort of real screen/host/display, and hook
212 // them to the ones provided by the shell. For now, not bothering.
213 NOTIMPLEMENTED();
214 return nullptr;
215}
216
217aura::client::FocusClient* AshTestHelper::GetFocusClient() {
218 return Shell::Get()->focus_controller();
219}
220
221aura::client::CaptureClient* AshTestHelper::GetCaptureClient() {
222 return wm::CaptureController::Get();
223}
224
Peter Kasting9eaedfd92020-03-30 23:24:50225void AshTestHelper::SetUp(InitParams init_params) {
226 // This block of objects are conditionally initialized here rather than in the
227 // constructor to make it easier for test classes to override them.
Zentaro Kavanagh930de992021-05-10 15:51:07228 if (!input_method::InputMethodManager::Get()) {
229 // |input_method_manager_| is not owned and is cleaned up in TearDown()
230 // by calling InputMethodManager::Shutdown().
Henrique Ferreiroe08cf1b2021-09-22 19:33:58231 input_method_manager_ = new input_method::MockInputMethodManager();
Zentaro Kavanagh930de992021-05-10 15:51:07232 input_method::InputMethodManager::Initialize(input_method_manager_);
233 }
234
Peter Kasting9eaedfd92020-03-30 23:24:50235 if (!bluez::BluezDBusManager::IsInitialized()) {
Peter Kasting4d66384d2020-03-30 23:28:49236 bluez_dbus_manager_initializer_ =
237 std::make_unique<BluezDBusManagerInitializer>();
Peter Kasting9eaedfd92020-03-30 23:24:50238 }
239 if (!chromeos::PowerManagerClient::Get())
240 chromeos::PowerManagerClient::InitializeFake();
241 if (!chromeos::PowerPolicyController::IsInitialized()) {
Peter Kasting4d66384d2020-03-30 23:28:49242 power_policy_controller_initializer_ =
243 std::make_unique<PowerPolicyControllerInitializer>();
Peter Kasting9eaedfd92020-03-30 23:24:50244 }
Hidehiko Abe6e3e57f42021-03-23 11:23:58245 if (!NewWindowDelegate::GetInstance()) {
246 new_window_delegate_provider_ =
247 std::make_unique<TestNewWindowDelegateProvider>(
248 std::make_unique<TestNewWindowDelegate>());
249 }
Peter Kasting9eaedfd92020-03-30 23:24:50250 if (!views::ViewsDelegate::GetInstance())
Peter Kasting37122d02020-04-02 00:08:55251 test_views_delegate_ = MakeTestViewsDelegate();
Peter Kasting9eaedfd92020-03-30 23:24:50252
Mitsuru Oshimaca97b1082021-01-29 21:12:38253 chromeos::LoginState::Initialize();
254
wutao92a8f5c2020-11-09 23:25:54255 ambient_ash_test_helper_ = std::make_unique<AmbientAshTestHelper>();
256
Zentaro Kavanagh1f436492021-05-20 23:29:36257 // There is a temporary M92-M94 notification that shows once to users
258 // at startup, but this interferes with many tests that expect a
259 // specific active window, or a certain number of notifications.
260 AcceleratorControllerImpl::SetShouldShowShortcutNotificationForTest(false);
261
Peter Kasting9eaedfd92020-03-30 23:24:50262 ShellInitParams shell_init_params;
263 shell_init_params.delegate = std::move(init_params.delegate);
264 if (!shell_init_params.delegate)
265 shell_init_params.delegate = std::make_unique<TestShellDelegate>();
Peter Kasting1618c312020-03-31 00:46:36266 shell_init_params.context_factory = GetContextFactory();
Peter Kasting9eaedfd92020-03-30 23:24:50267 shell_init_params.local_state = init_params.local_state;
268 shell_init_params.keyboard_ui_factory =
269 std::make_unique<TestKeyboardUIFactory>();
270 Shell::CreateInstance(std::move(shell_init_params));
271 Shell* shell = Shell::Get();
272
Jeffrey Young708db7f2021-09-20 19:38:46273 // Set up a test wallpaper controller client before signing in any users. At
274 // the time a user logs in, Wallpaper controller relies on
275 // WallpaperControllerClient to check if user data should be synced.
276 wallpaper_controller_client_ =
277 std::make_unique<TestWallpaperControllerClient>();
278 shell->wallpaper_controller()->SetClient(wallpaper_controller_client_.get());
279
Ahmed Mehfooz812c5422021-03-29 22:42:16280 // Disable the notification delay timer used to prevent non system
281 // notifications from showing up right after login. This needs to be done
282 // before any user sessions are added since the delay timer starts right
283 // after that.
284 SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(false);
285
Peter Kasting9eaedfd92020-03-30 23:24:50286 // Cursor is visible by default in tests.
287 shell->cursor_manager()->ShowCursor();
288
Xiaohui Chen2af09302020-06-25 04:06:00289 shell->assistant_controller()->SetAssistant(assistant_service_.get());
Peter Kasting9eaedfd92020-03-30 23:24:50290
291 shell->system_tray_model()->SetClient(system_tray_client_.get());
292
Peter Kasting4d66384d2020-03-30 23:28:49293 session_controller_client_ = std::make_unique<TestSessionControllerClient>(
294 shell->session_controller(), prefs_provider_.get());
Peter Kasting9eaedfd92020-03-30 23:24:50295 session_controller_client_->InitializeAndSetClient();
296 if (init_params.start_session)
297 session_controller_client_->CreatePredefinedUserSessions(1);
298
299 // Requires the AppListController the Shell creates.
300 app_list_test_helper_ = std::make_unique<AppListTestHelper>();
301
Peter Kasting86d2b5812020-04-01 22:32:14302 Shell::GetPrimaryRootWindow()->Show();
303 Shell::GetPrimaryRootWindow()->GetHost()->Show();
304
Peter Kasting9eaedfd92020-03-30 23:24:50305 // Don't change the display size due to host size resize.
306 display::test::DisplayManagerTestApi(shell->display_manager())
307 .DisableChangeDisplayUponHostResize();
308
309 // Create the test keyboard controller observer to respond to
310 // OnLoadKeyboardContentsRequested().
311 test_keyboard_controller_observer_ =
312 std::make_unique<TestKeyboardControllerObserver>(
313 shell->keyboard_controller());
314
Peter Kasting9eaedfd92020-03-30 23:24:50315 // Tests that change the display configuration generally don't care about the
316 // notifications and the popup UI can interfere with things like cursors.
317 shell->screen_layout_observer()->set_show_notifications_for_testing(false);
318
319 // Disable display change animations in unit tests.
320 DisplayConfigurationControllerTestApi(
321 shell->display_configuration_controller())
322 .SetDisplayAnimator(false);
323
324 // Remove the app dragging animations delay for testing purposes.
325 shell->overview_controller()->set_delayed_animation_task_delay_for_test(
326 base::TimeDelta());
327
328 // Tests expect empty wallpaper.
329 shell->wallpaper_controller()->CreateEmptyWallpaperForTesting();
Peter Kasting86d2b5812020-04-01 22:32:14330
331 // Move the mouse cursor to far away so that native events don't interfere
332 // with test expectations.
333 Shell::GetPrimaryRootWindow()->MoveCursorTo(gfx::Point(-1000, -1000));
334 Shell::Get()->cursor_manager()->EnableMouseEvents();
335
336 // Changing GestureConfiguration shouldn't make tests fail. These values
337 // prevent unexpected events from being generated during tests. Such as
338 // delayed events which create race conditions on slower tests.
339 ui::GestureConfiguration* gesture_config =
340 ui::GestureConfiguration::GetInstance();
341 gesture_config->set_max_touch_down_duration_for_click_in_ms(800);
342 gesture_config->set_long_press_time_in_ms(1000);
343 gesture_config->set_max_touch_move_in_pixels_for_click(5);
Harvey Yangd8306592020-10-29 05:09:08344
345 // Fake the |ec_lid_angle_driver_status_| in the unittests.
346 AccelerometerReader::GetInstance()->SetECLidAngleDriverStatusForTesting(
347 ECLidAngleDriverStatus::NOT_SUPPORTED);
Peter Kasting9eaedfd92020-03-30 23:24:50348}
349
Andrew Xubd59c162019-08-01 01:35:00350display::Display AshTestHelper::GetSecondaryDisplay() const {
Su Hong Koo777e83d2020-02-07 01:45:33351 return display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
352 .GetSecondaryDisplay();
skyd06586d2017-02-22 17:46:36353}
354
[email protected]09464c72013-04-26 07:31:28355} // namespace ash