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"
10 #include "ash/test/display_manager_test_api.h"
11 #include "ash/test/shell_test_api.h"
12 #include "ash/test/test_screenshot_delegate.h"
13 #include "ash/test/test_session_state_delegate.h"
14 #include "ash/test/test_shell_delegate.h"
15 #include "ash/test/test_system_tray_delegate.h"
16 #include "base/run_loop.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/input_state_lookup.h"
19 #include "ui/aura/test/env_test_helper.h"
20 #include "ui/base/ime/input_method_initializer.h"
21 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
22 #include "ui/compositor/test/context_factories_for_test.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/views/corewm/capture_controller.h"
25 #include "ui/views/corewm/wm_state.h"
27 #if defined(OS_CHROMEOS)
28 #include "chromeos/audio/cras_audio_handler.h"
29 #include "chromeos/dbus/dbus_thread_manager.h"
30 #include "ui/keyboard/keyboard.h"
34 #include "ui/aura/window_tree_host_x11.h"
40 AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
41 : message_loop_(message_loop),
42 test_shell_delegate_(NULL),
43 test_screenshot_delegate_(NULL),
44 dbus_thread_manager_initialized_(false) {
47 aura::test::SetUseOverrideRedirectWindowByDefault(true);
51 AshTestHelper::~AshTestHelper() {
54 void AshTestHelper::SetUp(bool start_session) {
55 wm_state_.reset(new views::corewm::WMState);
57 // Disable animations during tests.
58 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
59 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
60 ui::InitializeInputMethodForTesting();
62 bool allow_test_contexts = true;
63 ui::InitializeContextFactoryForTests(allow_test_contexts);
65 // Creates Shell and hook with Desktop.
66 test_shell_delegate_ = new TestShellDelegate;
68 // Creates MessageCenter since g_browser_process is not created in AshTestBase
70 message_center::MessageCenter::Initialize();
72 #if defined(OS_CHROMEOS)
73 // Create DBusThreadManager for testing.
74 if (!chromeos::DBusThreadManager::IsInitialized()) {
75 chromeos::DBusThreadManager::InitializeWithStub();
76 dbus_thread_manager_initialized_ = true;
78 // Create CrasAudioHandler for testing since g_browser_process is not
79 // created in AshTestBase tests.
80 chromeos::CrasAudioHandler::InitializeForTesting();
82 ash::Shell::CreateInstance(test_shell_delegate_);
83 aura::test::EnvTestHelper(aura::Env::GetInstance()).SetInputStateLookup(
84 scoped_ptr<aura::InputStateLookup>());
86 Shell* shell = Shell::GetInstance();
88 test_shell_delegate_->test_session_state_delegate()->
89 SetActiveUserSessionStarted(true);
90 test_shell_delegate_->test_session_state_delegate()->
91 SetHasActiveUser(true);
94 test::DisplayManagerTestApi(shell->display_manager()).
95 DisableChangeDisplayUponHostResize();
96 ShellTestApi(shell).DisableOutputConfiguratorAnimation();
98 test_screenshot_delegate_ = new TestScreenshotDelegate();
99 shell->accelerator_controller()->SetScreenshotDelegate(
100 scoped_ptr<ScreenshotDelegate>(test_screenshot_delegate_));
103 void AshTestHelper::TearDown() {
104 // Tear down the shell.
105 Shell::DeleteInstance();
106 test_screenshot_delegate_ = NULL;
108 // Remove global message center state.
109 message_center::MessageCenter::Shutdown();
111 #if defined(OS_CHROMEOS)
112 chromeos::CrasAudioHandler::Shutdown();
113 if (dbus_thread_manager_initialized_) {
114 chromeos::DBusThreadManager::Shutdown();
115 dbus_thread_manager_initialized_ = false;
117 keyboard::ResetKeyboardForTesting();
120 aura::Env::DeleteInstance();
121 ui::TerminateContextFactoryForTests();
123 // Need to reset the initial login status.
124 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER);
126 ui::ShutdownInputMethodForTesting();
127 zero_duration_mode_.reset();
129 CHECK(!views::corewm::ScopedCaptureClient::IsActive());
134 void AshTestHelper::RunAllPendingInMessageLoop() {
135 DCHECK(base::MessageLoopForUI::current() == message_loop_);
136 aura::Env::CreateInstance();
137 base::RunLoop run_loop;
138 run_loop.RunUntilIdle();
141 aura::Window* AshTestHelper::CurrentContext() {
142 aura::Window* root_window = Shell::GetTargetRootWindow();
144 root_window = Shell::GetPrimaryRootWindow();