Update V8 to version 4.6.54.
[chromium-blink-merge.git] / ash / test / ash_test_helper.cc
blob22f16bb80db4c5633e37040cb65c43817edd4014
1 // 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.
5 #include "ash/test/ash_test_helper.h"
7 #include "ash/accelerators/accelerator_controller.h"
8 #include "ash/ash_switches.h"
9 #include "ash/display/display_info.h"
10 #include "ash/shell.h"
11 #include "ash/shell_init_params.h"
12 #include "ash/test/ash_test_views_delegate.h"
13 #include "ash/test/display_manager_test_api.h"
14 #include "ash/test/shell_test_api.h"
15 #include "ash/test/test_screenshot_delegate.h"
16 #include "ash/test/test_session_state_delegate.h"
17 #include "ash/test/test_shell_delegate.h"
18 #include "ash/test/test_system_tray_delegate.h"
19 #include "base/run_loop.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/input_state_lookup.h"
22 #include "ui/aura/test/env_test_helper.h"
23 #include "ui/aura/test/event_generator_delegate_aura.h"
24 #include "ui/base/ime/input_method_initializer.h"
25 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
26 #include "ui/compositor/test/context_factories_for_test.h"
27 #include "ui/message_center/message_center.h"
28 #include "ui/wm/core/capture_controller.h"
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/audio/cras_audio_handler.h"
32 #include "chromeos/dbus/dbus_thread_manager.h"
33 #endif
35 #if defined(OS_WIN)
36 #include "base/win/windows_version.h"
37 #endif
39 #if defined(USE_X11)
40 #include "ui/aura/window_tree_host_x11.h"
41 #endif
43 namespace ash {
44 namespace test {
46 AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
47 : message_loop_(message_loop),
48 test_shell_delegate_(NULL),
49 test_screenshot_delegate_(NULL) {
50 CHECK(message_loop_);
51 #if defined(OS_CHROMEOS)
52 dbus_thread_manager_initialized_ = false;
53 #endif
54 #if defined(USE_X11)
55 aura::test::SetUseOverrideRedirectWindowByDefault(true);
56 #endif
57 aura::test::InitializeAuraEventGeneratorDelegate();
60 AshTestHelper::~AshTestHelper() {
63 void AshTestHelper::SetUp(bool start_session) {
64 ResetDisplayIdForTest();
65 views_delegate_.reset(new AshTestViewsDelegate);
67 // Disable animations during tests.
68 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
69 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
70 ui::InitializeInputMethodForTesting();
72 bool enable_pixel_output = false;
73 ui::ContextFactory* context_factory =
74 ui::InitializeContextFactoryForTests(enable_pixel_output);
76 // Creates Shell and hook with Desktop.
77 if (!test_shell_delegate_)
78 test_shell_delegate_ = new TestShellDelegate;
80 // Creates MessageCenter since g_browser_process is not created in AshTestBase
81 // tests.
82 message_center::MessageCenter::Initialize();
84 #if defined(OS_CHROMEOS)
85 // Create DBusThreadManager for testing.
86 if (!chromeos::DBusThreadManager::IsInitialized()) {
87 chromeos::DBusThreadManager::Initialize();
88 dbus_thread_manager_initialized_ = true;
90 // Create CrasAudioHandler for testing since g_browser_process is not
91 // created in AshTestBase tests.
92 chromeos::CrasAudioHandler::InitializeForTesting();
93 #endif
94 ShellInitParams init_params;
95 init_params.delegate = test_shell_delegate_;
96 init_params.context_factory = context_factory;
97 ash::Shell::CreateInstance(init_params);
98 aura::test::EnvTestHelper(aura::Env::GetInstance()).SetInputStateLookup(
99 scoped_ptr<aura::InputStateLookup>());
101 Shell* shell = Shell::GetInstance();
102 if (start_session) {
103 GetTestSessionStateDelegate()->SetActiveUserSessionStarted(true);
104 GetTestSessionStateDelegate()->SetHasActiveUser(true);
107 test::DisplayManagerTestApi().DisableChangeDisplayUponHostResize();
108 ShellTestApi(shell).DisableDisplayConfiguratorAnimation();
110 test_screenshot_delegate_ = new TestScreenshotDelegate();
111 shell->accelerator_controller()->SetScreenshotDelegate(
112 scoped_ptr<ScreenshotDelegate>(test_screenshot_delegate_));
115 void AshTestHelper::TearDown() {
116 // Tear down the shell.
117 Shell::DeleteInstance();
118 test_screenshot_delegate_ = NULL;
120 // Remove global message center state.
121 message_center::MessageCenter::Shutdown();
123 #if defined(OS_CHROMEOS)
124 chromeos::CrasAudioHandler::Shutdown();
125 if (dbus_thread_manager_initialized_) {
126 chromeos::DBusThreadManager::Shutdown();
127 dbus_thread_manager_initialized_ = false;
129 #endif
131 aura::Env::DeleteInstance();
132 ui::TerminateContextFactoryForTests();
134 // Need to reset the initial login status.
135 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER);
137 ui::ShutdownInputMethodForTesting();
138 zero_duration_mode_.reset();
140 CHECK(!wm::ScopedCaptureClient::IsActive());
142 views_delegate_.reset();
145 void AshTestHelper::RunAllPendingInMessageLoop() {
146 DCHECK(base::MessageLoopForUI::current() == message_loop_);
147 aura::Env::CreateInstance(true);
148 base::RunLoop run_loop;
149 run_loop.RunUntilIdle();
152 // static
153 TestSessionStateDelegate* AshTestHelper::GetTestSessionStateDelegate() {
154 CHECK(Shell::HasInstance());
155 return static_cast<TestSessionStateDelegate*>(
156 Shell::GetInstance()->session_state_delegate());
159 aura::Window* AshTestHelper::CurrentContext() {
160 aura::Window* root_window = Shell::GetTargetRootWindow();
161 if (!root_window)
162 root_window = Shell::GetPrimaryRootWindow();
163 DCHECK(root_window);
164 return root_window;
167 // static
168 bool AshTestHelper::SupportsMultipleDisplays() {
169 #if defined(OS_WIN)
170 return false;
171 #else
172 return true;
173 #endif
176 // static
177 bool AshTestHelper::SupportsHostWindowResize() {
178 #if defined(OS_WIN)
179 return false;
180 #else
181 return true;
182 #endif
185 } // namespace test
186 } // namespace ash