blob: 35138448cd542bbb04855ba6ddfd924da2750b65 [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
sky44a09892016-07-20 17:46:587#include "ash/accelerators/accelerator_controller_delegate_aura.h"
bruthiga2239af2016-08-02 17:07:298#include "ash/common/material_design/material_design_controller.h"
skyaec28902016-08-03 00:14:559#include "ash/common/test/material_design_controller_test_api.h"
sky1593c7bf2016-10-07 21:07:5310#include "ash/common/test/test_session_state_delegate.h"
jamescooka8003052016-10-11 17:02:3411#include "ash/common/test/test_system_tray_delegate.h"
mswbc0a8b482016-06-30 02:21:1412#include "ash/common/wm_shell.h"
[email protected]09464c72013-04-26 07:31:2813#include "ash/shell.h"
[email protected]1b4c7472014-05-15 18:32:2314#include "ash/shell_init_params.h"
skycf094f672016-09-01 19:40:1215#include "ash/test/ash_test_environment.h"
[email protected]93e828f2014-05-14 14:08:1116#include "ash/test/ash_test_views_delegate.h"
[email protected]09464c72013-04-26 07:31:2817#include "ash/test/display_manager_test_api.h"
18#include "ash/test/shell_test_api.h"
[email protected]c72834442013-10-01 03:11:2919#include "ash/test/test_screenshot_delegate.h"
[email protected]09464c72013-04-26 07:31:2820#include "ash/test/test_shell_delegate.h"
[email protected]09464c72013-04-26 07:31:2821#include "base/run_loop.h"
22#include "ui/aura/env.h"
[email protected]ccf28b82013-10-14 17:19:1523#include "ui/aura/input_state_lookup.h"
24#include "ui/aura/test/env_test_helper.h"
[email protected]1f0ece912014-08-11 05:52:2025#include "ui/aura/test/event_generator_delegate_aura.h"
[email protected]749bf6432013-06-12 16:00:0826#include "ui/base/ime/input_method_initializer.h"
varkha0b40cc392016-04-14 01:13:0527#include "ui/base/material_design/material_design_controller.h"
28#include "ui/base/test/material_design_controller_test_api.h"
[email protected]09464c72013-04-26 07:31:2829#include "ui/compositor/scoped_animation_duration_scale_mode.h"
[email protected]f6333942013-10-30 17:32:5530#include "ui/compositor/test/context_factories_for_test.h"
rjkroege259c01882016-08-30 19:29:5031#include "ui/display/manager/managed_display_info.h"
[email protected]09464c72013-04-26 07:31:2832#include "ui/message_center/message_center.h"
[email protected]ee3ed10772014-03-11 22:02:0133#include "ui/wm/core/capture_controller.h"
anantaf2b280f2016-04-13 21:49:3534#include "ui/wm/core/cursor_manager.h"
[email protected]09464c72013-04-26 07:31:2835
[email protected]6d8c8f02013-05-13 17:33:1236#if defined(OS_CHROMEOS)
jamescookf60d6ea2016-10-11 21:37:3737#include "ash/system/chromeos/screen_layout_observer.h"
[email protected]6d8c8f02013-05-13 17:33:1238#include "chromeos/audio/cras_audio_handler.h"
[email protected]f08f39b2013-12-19 06:14:1139#include "chromeos/dbus/dbus_thread_manager.h"
skycf094f672016-09-01 19:40:1240#include "device/bluetooth/bluetooth_adapter_factory.h"
rkc06c92c02015-11-05 01:55:4041#include "device/bluetooth/dbus/bluez_dbus_manager.h"
[email protected]6d8c8f02013-05-13 17:33:1242#endif
43
[email protected]3f4c26e2014-04-09 23:53:0744#if defined(OS_WIN)
45#include "base/win/windows_version.h"
46#endif
47
[email protected]6b9aa602013-05-18 09:08:4048#if defined(USE_X11)
[email protected]e8cf7332014-02-14 18:54:5849#include "ui/aura/window_tree_host_x11.h"
[email protected]6b9aa602013-05-18 09:08:4050#endif
51
[email protected]09464c72013-04-26 07:31:2852namespace ash {
53namespace test {
54
skycf094f672016-09-01 19:40:1255AshTestHelper::AshTestHelper(AshTestEnvironment* ash_test_environment)
56 : ash_test_environment_(ash_test_environment),
ben93072442015-10-05 19:14:3257 test_shell_delegate_(nullptr),
skycf094f672016-09-01 19:40:1258 test_screenshot_delegate_(nullptr) {
tfarinad7f1236f2015-01-16 16:21:2059#if defined(OS_CHROMEOS)
60 dbus_thread_manager_initialized_ = false;
rkc06c92c02015-11-05 01:55:4061 bluez_dbus_manager_initialized_ = false;
tfarinad7f1236f2015-01-16 16:21:2062#endif
[email protected]6b9aa602013-05-18 09:08:4063#if defined(USE_X11)
64 aura::test::SetUseOverrideRedirectWindowByDefault(true);
65#endif
[email protected]1f0ece912014-08-11 05:52:2066 aura::test::InitializeAuraEventGeneratorDelegate();
[email protected]09464c72013-04-26 07:31:2867}
68
jamescookb8dcef522016-06-25 14:42:5569AshTestHelper::~AshTestHelper() {}
[email protected]09464c72013-04-26 07:31:2870
mohsenc66d6872016-07-30 01:46:0871void AshTestHelper::SetUp(bool start_session,
72 MaterialDesignController::Mode material_mode) {
rjkroege259c01882016-08-30 19:29:5073 display::ResetDisplayIdForTest();
skycf094f672016-09-01 19:40:1274 views_delegate_ = ash_test_environment_->CreateViewsDelegate();
[email protected]ef663fa2013-12-20 17:55:0075
[email protected]09464c72013-04-26 07:31:2876 // Disable animations during tests.
77 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
78 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
[email protected]749bf6432013-06-12 16:00:0879 ui::InitializeInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:2880
[email protected]380a8bdf2014-02-20 19:02:5481 bool enable_pixel_output = false;
[email protected]fa69f2b62014-05-22 21:47:5882 ui::ContextFactory* context_factory =
83 ui::InitializeContextFactoryForTests(enable_pixel_output);
[email protected]f6333942013-10-30 17:32:5584
[email protected]09464c72013-04-26 07:31:2885 // Creates Shell and hook with Desktop.
[email protected]b2e2a4a2014-03-15 05:39:2886 if (!test_shell_delegate_)
87 test_shell_delegate_ = new TestShellDelegate;
[email protected]09464c72013-04-26 07:31:2888
[email protected]09464c72013-04-26 07:31:2889 // Creates MessageCenter since g_browser_process is not created in AshTestBase
90 // tests.
91 message_center::MessageCenter::Initialize();
[email protected]6d8c8f02013-05-13 17:33:1292
93#if defined(OS_CHROMEOS)
[email protected]f08f39b2013-12-19 06:14:1194 // Create DBusThreadManager for testing.
95 if (!chromeos::DBusThreadManager::IsInitialized()) {
jamescookdd618382016-09-19 16:42:0896 chromeos::DBusThreadManager::Initialize(
97 chromeos::DBusThreadManager::PROCESS_ASH);
[email protected]f08f39b2013-12-19 06:14:1198 dbus_thread_manager_initialized_ = true;
99 }
rkc06c92c02015-11-05 01:55:40100
101 if (!bluez::BluezDBusManager::IsInitialized()) {
102 bluez::BluezDBusManager::Initialize(
103 chromeos::DBusThreadManager::Get()->GetSystemBus(),
jamescook9be05a42016-09-19 19:09:49104 chromeos::DBusThreadManager::Get()->IsUsingFakes());
rkc06c92c02015-11-05 01:55:40105 bluez_dbus_manager_initialized_ = true;
106 }
107
[email protected]934e3c92013-08-01 00:18:23108 // Create CrasAudioHandler for testing since g_browser_process is not
109 // created in AshTestBase tests.
110 chromeos::CrasAudioHandler::InitializeForTesting();
[email protected]07343082013-08-13 03:46:34111#endif
skycf094f672016-09-01 19:40:12112 ash_test_environment_->SetUp();
anantaf2b280f2016-04-13 21:49:35113 // Reset the global state for the cursor manager. This includes the
114 // last cursor visibility state, etc.
sky984c1892016-04-20 00:00:34115 ::wm::CursorManager::ResetCursorVisibilityStateForTest();
anantaf2b280f2016-04-13 21:49:35116
varkha0b40cc392016-04-14 01:13:05117 // ContentTestSuiteBase might have already initialized
118 // MaterialDesignController in unit_tests suite.
119 ui::test::MaterialDesignControllerTestAPI::Uninitialize();
120 ui::MaterialDesignController::Initialize();
varkhab6a597112016-04-28 00:50:41121 ash::MaterialDesignController::Initialize();
mohsenc66d6872016-07-30 01:46:08122 if (material_mode == MaterialDesignController::Mode::UNINITIALIZED)
123 material_mode = MaterialDesignController::GetMode();
124 material_design_state_.reset(
125 new test::MaterialDesignControllerTestAPI(material_mode));
126
[email protected]1b4c7472014-05-15 18:32:23127 ShellInitParams init_params;
128 init_params.delegate = test_shell_delegate_;
[email protected]fa69f2b62014-05-22 21:47:58129 init_params.context_factory = context_factory;
skycf094f672016-09-01 19:40:12130 init_params.blocking_pool = ash_test_environment_->GetBlockingPool();
mswbc0a8b482016-06-30 02:21:14131 Shell::CreateInstance(init_params);
dchenga94547472016-04-08 08:41:11132 aura::test::EnvTestHelper(aura::Env::GetInstance())
133 .SetInputStateLookup(std::unique_ptr<aura::InputStateLookup>());
[email protected]ccf28b82013-10-14 17:19:15134
[email protected]09464c72013-04-26 07:31:28135 Shell* shell = Shell::GetInstance();
[email protected]cf6fea22013-08-07 14:24:01136 if (start_session) {
afakhry86c88842015-02-27 01:11:16137 GetTestSessionStateDelegate()->SetActiveUserSessionStarted(true);
138 GetTestSessionStateDelegate()->SetHasActiveUser(true);
[email protected]cf6fea22013-08-07 14:24:01139 }
[email protected]ad2f5df2013-07-02 08:21:56140
jamescookf60d6ea2016-10-11 21:37:37141#if defined(OS_CHROMEOS)
142 // Tests that change the display configuration generally don't care about the
143 // notifications and the popup UI can interfere with things like cursors.
144 shell->screen_layout_observer()->set_show_notifications_for_testing(false);
145#endif
146
rjkroege980ccfd2016-10-06 18:00:24147 test::DisplayManagerTestApi(Shell::GetInstance()->display_manager())
148 .DisableChangeDisplayUponHostResize();
stevenjbf402ac52016-01-16 00:30:49149 ShellTestApi(shell).DisableDisplayAnimator();
[email protected]c72834442013-10-01 03:11:29150
151 test_screenshot_delegate_ = new TestScreenshotDelegate();
sky44a09892016-07-20 17:46:58152 shell->accelerator_controller_delegate()->SetScreenshotDelegate(
dchenga94547472016-04-08 08:41:11153 std::unique_ptr<ScreenshotDelegate>(test_screenshot_delegate_));
[email protected]09464c72013-04-26 07:31:28154}
155
156void AshTestHelper::TearDown() {
[email protected]09464c72013-04-26 07:31:28157 // Tear down the shell.
158 Shell::DeleteInstance();
mohsenc66d6872016-07-30 01:46:08159 material_design_state_.reset();
varkhab6a597112016-04-28 00:50:41160 test::MaterialDesignControllerTestAPI::Uninitialize();
skycf094f672016-09-01 19:40:12161 ash_test_environment_->TearDown();
benb91330f2015-10-03 22:20:18162
[email protected]c72834442013-10-01 03:11:29163 test_screenshot_delegate_ = NULL;
[email protected]09464c72013-04-26 07:31:28164
[email protected]09464c72013-04-26 07:31:28165 // Remove global message center state.
166 message_center::MessageCenter::Shutdown();
[email protected]09464c72013-04-26 07:31:28167
[email protected]6d8c8f02013-05-13 17:33:12168#if defined(OS_CHROMEOS)
[email protected]934e3c92013-08-01 00:18:23169 chromeos::CrasAudioHandler::Shutdown();
rkc06c92c02015-11-05 01:55:40170 if (bluez_dbus_manager_initialized_) {
skycf094f672016-09-01 19:40:12171 device::BluetoothAdapterFactory::Shutdown();
rkc06c92c02015-11-05 01:55:40172 bluez::BluezDBusManager::Shutdown();
173 bluez_dbus_manager_initialized_ = false;
174 }
[email protected]f08f39b2013-12-19 06:14:11175 if (dbus_thread_manager_initialized_) {
176 chromeos::DBusThreadManager::Shutdown();
177 dbus_thread_manager_initialized_ = false;
178 }
[email protected]6d8c8f02013-05-13 17:33:12179#endif
180
[email protected]f6333942013-10-30 17:32:55181 ui::TerminateContextFactoryForTests();
[email protected]09464c72013-04-26 07:31:28182
[email protected]f7ff7ba2013-08-21 21:03:04183 // Need to reset the initial login status.
skye79274a2016-06-08 05:39:02184 TestSystemTrayDelegate::SetInitialLoginStatus(LoginStatus::USER);
jamescooka1257f12016-06-12 07:02:25185 TestSystemTrayDelegate::SetSystemUpdateRequired(false);
[email protected]f7ff7ba2013-08-21 21:03:04186
[email protected]749bf6432013-06-12 16:00:08187 ui::ShutdownInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:28188 zero_duration_mode_.reset();
[email protected]23a2dc8d2013-08-22 15:04:22189
sky984c1892016-04-20 00:00:34190 CHECK(!::wm::ScopedCaptureClient::IsActive());
[email protected]966103542013-12-12 22:13:27191
[email protected]93e828f2014-05-14 14:08:11192 views_delegate_.reset();
[email protected]09464c72013-04-26 07:31:28193}
194
195void AshTestHelper::RunAllPendingInMessageLoop() {
[email protected]42968f72014-02-05 20:58:23196 base::RunLoop run_loop;
[email protected]09464c72013-04-26 07:31:28197 run_loop.RunUntilIdle();
[email protected]09464c72013-04-26 07:31:28198}
199
afakhry86c88842015-02-27 01:11:16200// static
201TestSessionStateDelegate* AshTestHelper::GetTestSessionStateDelegate() {
mswbc0a8b482016-06-30 02:21:14202 CHECK(WmShell::HasInstance());
afakhry86c88842015-02-27 01:11:16203 return static_cast<TestSessionStateDelegate*>(
mswbc0a8b482016-06-30 02:21:14204 WmShell::Get()->GetSessionStateDelegate());
afakhry86c88842015-02-27 01:11:16205}
206
[email protected]bf9cdb362013-10-25 19:22:45207aura::Window* AshTestHelper::CurrentContext() {
208 aura::Window* root_window = Shell::GetTargetRootWindow();
[email protected]09464c72013-04-26 07:31:28209 if (!root_window)
210 root_window = Shell::GetPrimaryRootWindow();
211 DCHECK(root_window);
212 return root_window;
213}
214
[email protected]3f4c26e2014-04-09 23:53:07215// static
216bool AshTestHelper::SupportsMultipleDisplays() {
217#if defined(OS_WIN)
[email protected]452145592014-04-18 00:04:59218 return false;
[email protected]3f4c26e2014-04-09 23:53:07219#else
220 return true;
221#endif
222}
223
[email protected]09464c72013-04-26 07:31:28224} // namespace test
225} // namespace ash