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. 4 5 #include "ash/test/ash_test_helper.h" 6 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" 28 29 #if defined(OS_CHROMEOS) 30 #include "chromeos/audio/cras_audio_handler.h" 31 #include "chromeos/dbus/dbus_thread_manager.h" 32 #include "ui/keyboard/keyboard.h" 33 #endif 34 35 #if defined(OS_WIN) 36 #include "base/win/windows_version.h" 37 #endif 38 39 #if defined(USE_X11) 40 #include "ui/aura/window_tree_host_x11.h" 41 #endif 42 43 namespace ash { 44 namespace test { 45 46 AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop) 47 : message_loop_(message_loop), 48 test_shell_delegate_(NULL), 49 test_screenshot_delegate_(NULL), 50 dbus_thread_manager_initialized_(false) { 51 CHECK(message_loop_); 52 #if defined(USE_X11) 53 aura::test::SetUseOverrideRedirectWindowByDefault(true); 54 #endif 55 aura::test::InitializeAuraEventGeneratorDelegate(); 56 } 57 58 AshTestHelper::~AshTestHelper() { 59 } 60 61 void AshTestHelper::SetUp(bool start_session) { 62 views_delegate_.reset(new AshTestViewsDelegate); 63 64 // Disable animations during tests. 65 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( 66 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); 67 ui::InitializeInputMethodForTesting(); 68 69 bool enable_pixel_output = false; 70 ui::ContextFactory* context_factory = 71 ui::InitializeContextFactoryForTests(enable_pixel_output); 72 73 // Creates Shell and hook with Desktop. 74 if (!test_shell_delegate_) 75 test_shell_delegate_ = new TestShellDelegate; 76 77 // Creates MessageCenter since g_browser_process is not created in AshTestBase 78 // tests. 79 message_center::MessageCenter::Initialize(); 80 81 #if defined(OS_CHROMEOS) 82 // Create DBusThreadManager for testing. 83 if (!chromeos::DBusThreadManager::IsInitialized()) { 84 chromeos::DBusThreadManager::Initialize(); 85 dbus_thread_manager_initialized_ = true; 86 } 87 // Create CrasAudioHandler for testing since g_browser_process is not 88 // created in AshTestBase tests. 89 chromeos::CrasAudioHandler::InitializeForTesting(); 90 #endif 91 ShellInitParams init_params; 92 init_params.delegate = test_shell_delegate_; 93 init_params.context_factory = context_factory; 94 ash::Shell::CreateInstance(init_params); 95 aura::test::EnvTestHelper(aura::Env::GetInstance()).SetInputStateLookup( 96 scoped_ptr<aura::InputStateLookup>()); 97 98 Shell* shell = Shell::GetInstance(); 99 if (start_session) { 100 test_shell_delegate_->test_session_state_delegate()-> 101 SetActiveUserSessionStarted(true); 102 test_shell_delegate_->test_session_state_delegate()-> 103 SetHasActiveUser(true); 104 } 105 106 test::DisplayManagerTestApi(shell->display_manager()). 107 DisableChangeDisplayUponHostResize(); 108 ShellTestApi(shell).DisableDisplayConfiguratorAnimation(); 109 110 test_screenshot_delegate_ = new TestScreenshotDelegate(); 111 shell->accelerator_controller()->SetScreenshotDelegate( 112 scoped_ptr<ScreenshotDelegate>(test_screenshot_delegate_)); 113 } 114 115 void AshTestHelper::TearDown() { 116 // Tear down the shell. 117 Shell::DeleteInstance(); 118 test_screenshot_delegate_ = NULL; 119 120 // Remove global message center state. 121 message_center::MessageCenter::Shutdown(); 122 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; 128 } 129 keyboard::ResetKeyboardForTesting(); 130 #endif 131 132 aura::Env::DeleteInstance(); 133 ui::TerminateContextFactoryForTests(); 134 135 // Need to reset the initial login status. 136 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER); 137 138 ui::ShutdownInputMethodForTesting(); 139 zero_duration_mode_.reset(); 140 141 CHECK(!wm::ScopedCaptureClient::IsActive()); 142 143 views_delegate_.reset(); 144 } 145 146 void AshTestHelper::RunAllPendingInMessageLoop() { 147 DCHECK(base::MessageLoopForUI::current() == message_loop_); 148 aura::Env::CreateInstance(true); 149 base::RunLoop run_loop; 150 run_loop.RunUntilIdle(); 151 } 152 153 aura::Window* AshTestHelper::CurrentContext() { 154 aura::Window* root_window = Shell::GetTargetRootWindow(); 155 if (!root_window) 156 root_window = Shell::GetPrimaryRootWindow(); 157 DCHECK(root_window); 158 return root_window; 159 } 160 161 // static 162 bool AshTestHelper::SupportsMultipleDisplays() { 163 #if defined(OS_WIN) 164 return false; 165 #else 166 return true; 167 #endif 168 } 169 170 // static 171 bool AshTestHelper::SupportsHostWindowResize() { 172 #if defined(OS_WIN) 173 return false; 174 #else 175 return true; 176 #endif 177 } 178 179 } // namespace test 180 } // namespace ash 181