Mandoline: Don't run mandoline_browser_apptests on Android until failures are fixed
[chromium-blink-merge.git] / ash / test / ash_test_helper.cc
blob60b8f3ed9fa1630aa2986c7d3b709dfdf5d11af9
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/shell.h"
10 #include "ash/shell_init_params.h"
11 #include "ash/test/ash_test_views_delegate.h"
12 #include "ash/test/display_manager_test_api.h"
13 #include "ash/test/shell_test_api.h"
14 #include "ash/test/test_screenshot_delegate.h"
15 #include "ash/test/test_session_state_delegate.h"
16 #include "ash/test/test_shell_delegate.h"
17 #include "ash/test/test_system_tray_delegate.h"
18 #include "base/run_loop.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/input_state_lookup.h"
21 #include "ui/aura/test/env_test_helper.h"
22 #include "ui/aura/test/event_generator_delegate_aura.h"
23 #include "ui/base/ime/input_method_initializer.h"
24 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
25 #include "ui/compositor/test/context_factories_for_test.h"
26 #include "ui/message_center/message_center.h"
27 #include "ui/wm/core/capture_controller.h"
29 #if defined(OS_CHROMEOS)
30 #include "chromeos/audio/cras_audio_handler.h"
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #endif
34 #if defined(OS_WIN)
35 #include "base/win/windows_version.h"
36 #endif
38 #if defined(USE_X11)
39 #include "ui/aura/window_tree_host_x11.h"
40 #endif
42 namespace ash {
43 namespace test {
45 AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
46 : message_loop_(message_loop),
47 test_shell_delegate_(NULL),
48 test_screenshot_delegate_(NULL) {
49 CHECK(message_loop_);
50 #if defined(OS_CHROMEOS)
51 dbus_thread_manager_initialized_ = false;
52 #endif
53 #if defined(USE_X11)
54 aura::test::SetUseOverrideRedirectWindowByDefault(true);
55 #endif
56 aura::test::InitializeAuraEventGeneratorDelegate();
59 AshTestHelper::~AshTestHelper() {
62 void AshTestHelper::SetUp(bool start_session) {
63 views_delegate_.reset(new AshTestViewsDelegate);
65 // Disable animations during tests.
66 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
67 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
68 ui::InitializeInputMethodForTesting();
70 bool enable_pixel_output = false;
71 ui::ContextFactory* context_factory =
72 ui::InitializeContextFactoryForTests(enable_pixel_output);
74 // Creates Shell and hook with Desktop.
75 if (!test_shell_delegate_)
76 test_shell_delegate_ = new TestShellDelegate;
78 // Creates MessageCenter since g_browser_process is not created in AshTestBase
79 // tests.
80 message_center::MessageCenter::Initialize();
82 #if defined(OS_CHROMEOS)
83 // Create DBusThreadManager for testing.
84 if (!chromeos::DBusThreadManager::IsInitialized()) {
85 chromeos::DBusThreadManager::Initialize();
86 dbus_thread_manager_initialized_ = true;
88 // Create CrasAudioHandler for testing since g_browser_process is not
89 // created in AshTestBase tests.
90 chromeos::CrasAudioHandler::InitializeForTesting();
91 #endif
92 ShellInitParams init_params;
93 init_params.delegate = test_shell_delegate_;
94 init_params.context_factory = context_factory;
95 ash::Shell::CreateInstance(init_params);
96 aura::test::EnvTestHelper(aura::Env::GetInstance()).SetInputStateLookup(
97 scoped_ptr<aura::InputStateLookup>());
99 Shell* shell = Shell::GetInstance();
100 if (start_session) {
101 GetTestSessionStateDelegate()->SetActiveUserSessionStarted(true);
102 GetTestSessionStateDelegate()->SetHasActiveUser(true);
105 test::DisplayManagerTestApi(shell->display_manager()).
106 DisableChangeDisplayUponHostResize();
107 ShellTestApi(shell).DisableDisplayConfiguratorAnimation();
109 test_screenshot_delegate_ = new TestScreenshotDelegate();
110 shell->accelerator_controller()->SetScreenshotDelegate(
111 scoped_ptr<ScreenshotDelegate>(test_screenshot_delegate_));
114 void AshTestHelper::TearDown() {
115 // Tear down the shell.
116 Shell::DeleteInstance();
117 test_screenshot_delegate_ = NULL;
119 // Remove global message center state.
120 message_center::MessageCenter::Shutdown();
122 #if defined(OS_CHROMEOS)
123 chromeos::CrasAudioHandler::Shutdown();
124 if (dbus_thread_manager_initialized_) {
125 chromeos::DBusThreadManager::Shutdown();
126 dbus_thread_manager_initialized_ = false;
128 #endif
130 aura::Env::DeleteInstance();
131 ui::TerminateContextFactoryForTests();
133 // Need to reset the initial login status.
134 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER);
136 ui::ShutdownInputMethodForTesting();
137 zero_duration_mode_.reset();
139 CHECK(!wm::ScopedCaptureClient::IsActive());
141 views_delegate_.reset();
144 void AshTestHelper::RunAllPendingInMessageLoop() {
145 DCHECK(base::MessageLoopForUI::current() == message_loop_);
146 aura::Env::CreateInstance(true);
147 base::RunLoop run_loop;
148 run_loop.RunUntilIdle();
151 // static
152 TestSessionStateDelegate* AshTestHelper::GetTestSessionStateDelegate() {
153 CHECK(Shell::HasInstance());
154 return static_cast<TestSessionStateDelegate*>(
155 Shell::GetInstance()->session_state_delegate());
158 aura::Window* AshTestHelper::CurrentContext() {
159 aura::Window* root_window = Shell::GetTargetRootWindow();
160 if (!root_window)
161 root_window = Shell::GetPrimaryRootWindow();
162 DCHECK(root_window);
163 return root_window;
166 // static
167 bool AshTestHelper::SupportsMultipleDisplays() {
168 #if defined(OS_WIN)
169 return false;
170 #else
171 return true;
172 #endif
175 // static
176 bool AshTestHelper::SupportsHostWindowResize() {
177 #if defined(OS_WIN)
178 return false;
179 #else
180 return true;
181 #endif
184 } // namespace test
185 } // namespace ash