blob: acdf42eaa3d9b3af539d0832164ce1a8ad7df672 [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
[email protected]c72834442013-10-01 03:11:297#include "ash/accelerators/accelerator_controller.h"
mswd9b1b342016-06-17 20:19:578#include "ash/common/ash_switches.h"
msw7f1ec922016-06-22 23:58:569#include "ash/common/display/display_info.h"
tdanderson008ee992016-06-08 21:43:4110#include "ash/common/material_design/material_design_controller.h"
mswbc0a8b482016-06-30 02:21:1411#include "ash/common/wm_shell.h"
[email protected]09464c72013-04-26 07:31:2812#include "ash/shell.h"
[email protected]1b4c7472014-05-15 18:32:2313#include "ash/shell_init_params.h"
[email protected]93e828f2014-05-14 14:08:1114#include "ash/test/ash_test_views_delegate.h"
benb91330f2015-10-03 22:20:1815#include "ash/test/content/test_shell_content_state.h"
[email protected]09464c72013-04-26 07:31:2816#include "ash/test/display_manager_test_api.h"
varkhab6a597112016-04-28 00:50:4117#include "ash/test/material_design_controller_test_api.h"
[email protected]09464c72013-04-26 07:31:2818#include "ash/test/shell_test_api.h"
[email protected]c72834442013-10-01 03:11:2919#include "ash/test/test_screenshot_delegate.h"
[email protected]ad2f5df2013-07-02 08:21:5620#include "ash/test/test_session_state_delegate.h"
[email protected]09464c72013-04-26 07:31:2821#include "ash/test/test_shell_delegate.h"
[email protected]9f19d5c2013-09-13 17:01:4822#include "ash/test/test_system_tray_delegate.h"
[email protected]09464c72013-04-26 07:31:2823#include "base/run_loop.h"
ben0d6be2d42015-09-26 04:37:1924#include "content/public/browser/browser_thread.h"
[email protected]09464c72013-04-26 07:31:2825#include "ui/aura/env.h"
[email protected]ccf28b82013-10-14 17:19:1526#include "ui/aura/input_state_lookup.h"
27#include "ui/aura/test/env_test_helper.h"
[email protected]1f0ece912014-08-11 05:52:2028#include "ui/aura/test/event_generator_delegate_aura.h"
[email protected]749bf6432013-06-12 16:00:0829#include "ui/base/ime/input_method_initializer.h"
varkha0b40cc392016-04-14 01:13:0530#include "ui/base/material_design/material_design_controller.h"
31#include "ui/base/test/material_design_controller_test_api.h"
[email protected]09464c72013-04-26 07:31:2832#include "ui/compositor/scoped_animation_duration_scale_mode.h"
[email protected]f6333942013-10-30 17:32:5533#include "ui/compositor/test/context_factories_for_test.h"
[email protected]09464c72013-04-26 07:31:2834#include "ui/message_center/message_center.h"
[email protected]ee3ed10772014-03-11 22:02:0135#include "ui/wm/core/capture_controller.h"
anantaf2b280f2016-04-13 21:49:3536#include "ui/wm/core/cursor_manager.h"
[email protected]09464c72013-04-26 07:31:2837
[email protected]6d8c8f02013-05-13 17:33:1238#if defined(OS_CHROMEOS)
39#include "chromeos/audio/cras_audio_handler.h"
[email protected]f08f39b2013-12-19 06:14:1140#include "chromeos/dbus/dbus_thread_manager.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
55AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
56 : message_loop_(message_loop),
ben93072442015-10-05 19:14:3257 test_shell_delegate_(nullptr),
58 test_screenshot_delegate_(nullptr),
benf6de9852015-10-06 21:29:2859 content_state_(nullptr),
60 test_shell_content_state_(nullptr) {
[email protected]09464c72013-04-26 07:31:2861 CHECK(message_loop_);
tfarinad7f1236f2015-01-16 16:21:2062#if defined(OS_CHROMEOS)
63 dbus_thread_manager_initialized_ = false;
rkc06c92c02015-11-05 01:55:4064 bluez_dbus_manager_initialized_ = false;
tfarinad7f1236f2015-01-16 16:21:2065#endif
[email protected]6b9aa602013-05-18 09:08:4066#if defined(USE_X11)
67 aura::test::SetUseOverrideRedirectWindowByDefault(true);
68#endif
[email protected]1f0ece912014-08-11 05:52:2069 aura::test::InitializeAuraEventGeneratorDelegate();
[email protected]09464c72013-04-26 07:31:2870}
71
jamescookb8dcef522016-06-25 14:42:5572AshTestHelper::~AshTestHelper() {}
[email protected]09464c72013-04-26 07:31:2873
[email protected]cf6fea22013-08-07 14:24:0174void AshTestHelper::SetUp(bool start_session) {
oshima5cab2292015-07-31 17:07:4375 ResetDisplayIdForTest();
[email protected]93e828f2014-05-14 14:08:1176 views_delegate_.reset(new AshTestViewsDelegate);
[email protected]ef663fa2013-12-20 17:55:0077
[email protected]09464c72013-04-26 07:31:2878 // Disable animations during tests.
79 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
80 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
[email protected]749bf6432013-06-12 16:00:0881 ui::InitializeInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:2882
[email protected]380a8bdf2014-02-20 19:02:5483 bool enable_pixel_output = false;
[email protected]fa69f2b62014-05-22 21:47:5884 ui::ContextFactory* context_factory =
85 ui::InitializeContextFactoryForTests(enable_pixel_output);
[email protected]f6333942013-10-30 17:32:5586
[email protected]09464c72013-04-26 07:31:2887 // Creates Shell and hook with Desktop.
[email protected]b2e2a4a2014-03-15 05:39:2888 if (!test_shell_delegate_)
89 test_shell_delegate_ = new TestShellDelegate;
[email protected]09464c72013-04-26 07:31:2890
[email protected]09464c72013-04-26 07:31:2891 // Creates MessageCenter since g_browser_process is not created in AshTestBase
92 // tests.
93 message_center::MessageCenter::Initialize();
[email protected]6d8c8f02013-05-13 17:33:1294
95#if defined(OS_CHROMEOS)
[email protected]f08f39b2013-12-19 06:14:1196 // Create DBusThreadManager for testing.
97 if (!chromeos::DBusThreadManager::IsInitialized()) {
zelidrag29fe3382014-08-27 01:44:4898 chromeos::DBusThreadManager::Initialize();
[email protected]f08f39b2013-12-19 06:14:1199 dbus_thread_manager_initialized_ = true;
100 }
rkc06c92c02015-11-05 01:55:40101
102 if (!bluez::BluezDBusManager::IsInitialized()) {
103 bluez::BluezDBusManager::Initialize(
104 chromeos::DBusThreadManager::Get()->GetSystemBus(),
105 chromeos::DBusThreadManager::Get()->IsUsingStub(
106 chromeos::DBusClientBundle::BLUETOOTH));
107 bluez_dbus_manager_initialized_ = true;
108 }
109
[email protected]934e3c92013-08-01 00:18:23110 // Create CrasAudioHandler for testing since g_browser_process is not
111 // created in AshTestBase tests.
112 chromeos::CrasAudioHandler::InitializeForTesting();
[email protected]07343082013-08-13 03:46:34113#endif
benf6de9852015-10-06 21:29:28114 ShellContentState* content_state = content_state_;
115 if (!content_state) {
116 test_shell_content_state_ = new TestShellContentState;
117 content_state = test_shell_content_state_;
118 }
119 ShellContentState::SetInstance(content_state);
benb91330f2015-10-03 22:20:18120
anantaf2b280f2016-04-13 21:49:35121 // Reset the global state for the cursor manager. This includes the
122 // last cursor visibility state, etc.
sky984c1892016-04-20 00:00:34123 ::wm::CursorManager::ResetCursorVisibilityStateForTest();
anantaf2b280f2016-04-13 21:49:35124
varkha0b40cc392016-04-14 01:13:05125 // ContentTestSuiteBase might have already initialized
126 // MaterialDesignController in unit_tests suite.
127 ui::test::MaterialDesignControllerTestAPI::Uninitialize();
128 ui::MaterialDesignController::Initialize();
varkhab6a597112016-04-28 00:50:41129 ash::MaterialDesignController::Initialize();
[email protected]1b4c7472014-05-15 18:32:23130 ShellInitParams init_params;
131 init_params.delegate = test_shell_delegate_;
[email protected]fa69f2b62014-05-22 21:47:58132 init_params.context_factory = context_factory;
ben0d6be2d42015-09-26 04:37:19133 init_params.blocking_pool = content::BrowserThread::GetBlockingPool();
mswbc0a8b482016-06-30 02:21:14134 Shell::CreateInstance(init_params);
dchenga94547472016-04-08 08:41:11135 aura::test::EnvTestHelper(aura::Env::GetInstance())
136 .SetInputStateLookup(std::unique_ptr<aura::InputStateLookup>());
[email protected]ccf28b82013-10-14 17:19:15137
[email protected]09464c72013-04-26 07:31:28138 Shell* shell = Shell::GetInstance();
[email protected]cf6fea22013-08-07 14:24:01139 if (start_session) {
afakhry86c88842015-02-27 01:11:16140 GetTestSessionStateDelegate()->SetActiveUserSessionStarted(true);
141 GetTestSessionStateDelegate()->SetHasActiveUser(true);
[email protected]cf6fea22013-08-07 14:24:01142 }
[email protected]ad2f5df2013-07-02 08:21:56143
oshima81d33282015-07-27 21:16:01144 test::DisplayManagerTestApi().DisableChangeDisplayUponHostResize();
stevenjbf402ac52016-01-16 00:30:49145 ShellTestApi(shell).DisableDisplayAnimator();
[email protected]c72834442013-10-01 03:11:29146
147 test_screenshot_delegate_ = new TestScreenshotDelegate();
148 shell->accelerator_controller()->SetScreenshotDelegate(
dchenga94547472016-04-08 08:41:11149 std::unique_ptr<ScreenshotDelegate>(test_screenshot_delegate_));
[email protected]09464c72013-04-26 07:31:28150}
151
152void AshTestHelper::TearDown() {
[email protected]09464c72013-04-26 07:31:28153 // Tear down the shell.
154 Shell::DeleteInstance();
varkhab6a597112016-04-28 00:50:41155 test::MaterialDesignControllerTestAPI::Uninitialize();
benb91330f2015-10-03 22:20:18156 ShellContentState::DestroyInstance();
157
[email protected]c72834442013-10-01 03:11:29158 test_screenshot_delegate_ = NULL;
[email protected]09464c72013-04-26 07:31:28159
[email protected]09464c72013-04-26 07:31:28160 // Remove global message center state.
161 message_center::MessageCenter::Shutdown();
[email protected]09464c72013-04-26 07:31:28162
[email protected]6d8c8f02013-05-13 17:33:12163#if defined(OS_CHROMEOS)
[email protected]934e3c92013-08-01 00:18:23164 chromeos::CrasAudioHandler::Shutdown();
rkc06c92c02015-11-05 01:55:40165 if (bluez_dbus_manager_initialized_) {
166 bluez::BluezDBusManager::Shutdown();
167 bluez_dbus_manager_initialized_ = false;
168 }
[email protected]f08f39b2013-12-19 06:14:11169 if (dbus_thread_manager_initialized_) {
170 chromeos::DBusThreadManager::Shutdown();
171 dbus_thread_manager_initialized_ = false;
172 }
[email protected]6d8c8f02013-05-13 17:33:12173#endif
174
[email protected]f6333942013-10-30 17:32:55175 ui::TerminateContextFactoryForTests();
[email protected]09464c72013-04-26 07:31:28176
[email protected]f7ff7ba2013-08-21 21:03:04177 // Need to reset the initial login status.
skye79274a2016-06-08 05:39:02178 TestSystemTrayDelegate::SetInitialLoginStatus(LoginStatus::USER);
jamescooka1257f12016-06-12 07:02:25179 TestSystemTrayDelegate::SetSystemUpdateRequired(false);
[email protected]f7ff7ba2013-08-21 21:03:04180
[email protected]749bf6432013-06-12 16:00:08181 ui::ShutdownInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:28182 zero_duration_mode_.reset();
[email protected]23a2dc8d2013-08-22 15:04:22183
sky984c1892016-04-20 00:00:34184 CHECK(!::wm::ScopedCaptureClient::IsActive());
[email protected]966103542013-12-12 22:13:27185
[email protected]93e828f2014-05-14 14:08:11186 views_delegate_.reset();
[email protected]09464c72013-04-26 07:31:28187}
188
189void AshTestHelper::RunAllPendingInMessageLoop() {
[email protected]9e7154122013-05-30 23:11:04190 DCHECK(base::MessageLoopForUI::current() == message_loop_);
[email protected]42968f72014-02-05 20:58:23191 base::RunLoop run_loop;
[email protected]09464c72013-04-26 07:31:28192 run_loop.RunUntilIdle();
[email protected]09464c72013-04-26 07:31:28193}
194
afakhry86c88842015-02-27 01:11:16195// static
196TestSessionStateDelegate* AshTestHelper::GetTestSessionStateDelegate() {
mswbc0a8b482016-06-30 02:21:14197 CHECK(WmShell::HasInstance());
afakhry86c88842015-02-27 01:11:16198 return static_cast<TestSessionStateDelegate*>(
mswbc0a8b482016-06-30 02:21:14199 WmShell::Get()->GetSessionStateDelegate());
afakhry86c88842015-02-27 01:11:16200}
201
[email protected]bf9cdb362013-10-25 19:22:45202aura::Window* AshTestHelper::CurrentContext() {
203 aura::Window* root_window = Shell::GetTargetRootWindow();
[email protected]09464c72013-04-26 07:31:28204 if (!root_window)
205 root_window = Shell::GetPrimaryRootWindow();
206 DCHECK(root_window);
207 return root_window;
208}
209
[email protected]3f4c26e2014-04-09 23:53:07210// static
211bool AshTestHelper::SupportsMultipleDisplays() {
212#if defined(OS_WIN)
[email protected]452145592014-04-18 00:04:59213 return false;
[email protected]3f4c26e2014-04-09 23:53:07214#else
215 return true;
216#endif
217}
218
219// static
220bool AshTestHelper::SupportsHostWindowResize() {
221#if defined(OS_WIN)
[email protected]452145592014-04-18 00:04:59222 return false;
[email protected]3f4c26e2014-04-09 23:53:07223#else
224 return true;
225#endif
226}
227
[email protected]09464c72013-04-26 07:31:28228} // namespace test
229} // namespace ash