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/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/transient_window_stacking_client.h" 26 27 #if defined(OS_CHROMEOS) 28 #include "chromeos/audio/cras_audio_handler.h" 29 #endif 30 31 #if defined(USE_X11) 32 #include "ui/aura/root_window_host_x11.h" 33 #endif 34 35 namespace ash { 36 namespace test { 37 AshTestHelper(base::MessageLoopForUI * message_loop)38AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop) 39 : message_loop_(message_loop), 40 test_shell_delegate_(NULL), 41 test_screenshot_delegate_(NULL) { 42 CHECK(message_loop_); 43 #if defined(USE_X11) 44 aura::test::SetUseOverrideRedirectWindowByDefault(true); 45 #endif 46 } 47 ~AshTestHelper()48AshTestHelper::~AshTestHelper() { 49 } 50 SetUp(bool start_session)51void AshTestHelper::SetUp(bool start_session) { 52 // Disable animations during tests. 53 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( 54 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); 55 ui::InitializeInputMethodForTesting(); 56 57 bool allow_test_contexts = true; 58 ui::InitializeContextFactoryForTests(allow_test_contexts); 59 60 // Creates Shell and hook with Desktop. 61 test_shell_delegate_ = new TestShellDelegate; 62 63 // Creates MessageCenter since g_browser_process is not created in AshTestBase 64 // tests. 65 message_center::MessageCenter::Initialize(); 66 67 #if defined(OS_CHROMEOS) 68 // Create CrasAudioHandler for testing since g_browser_process is not 69 // created in AshTestBase tests. 70 chromeos::CrasAudioHandler::InitializeForTesting(); 71 #endif 72 ash::Shell::CreateInstance(test_shell_delegate_); 73 aura::test::EnvTestHelper(aura::Env::GetInstance()).SetInputStateLookup( 74 scoped_ptr<aura::InputStateLookup>()); 75 76 Shell* shell = Shell::GetInstance(); 77 if (start_session) { 78 test_shell_delegate_->test_session_state_delegate()-> 79 SetActiveUserSessionStarted(true); 80 test_shell_delegate_->test_session_state_delegate()-> 81 SetHasActiveUser(true); 82 } 83 84 test::DisplayManagerTestApi(shell->display_manager()). 85 DisableChangeDisplayUponHostResize(); 86 ShellTestApi(shell).DisableOutputConfiguratorAnimation(); 87 88 test_screenshot_delegate_ = new TestScreenshotDelegate(); 89 shell->accelerator_controller()->SetScreenshotDelegate( 90 scoped_ptr<ScreenshotDelegate>(test_screenshot_delegate_)); 91 92 // SetWindowStackingClient() takes ownership of TransientWindowStackingClient. 93 aura::client::SetWindowStackingClient( 94 new views::corewm::TransientWindowStackingClient); 95 } 96 TearDown()97void AshTestHelper::TearDown() { 98 // Tear down the shell. 99 Shell::DeleteInstance(); 100 test_screenshot_delegate_ = NULL; 101 102 // Remove global message center state. 103 message_center::MessageCenter::Shutdown(); 104 105 #if defined(OS_CHROMEOS) 106 chromeos::CrasAudioHandler::Shutdown(); 107 #endif 108 109 aura::Env::DeleteInstance(); 110 ui::TerminateContextFactoryForTests(); 111 112 // Need to reset the initial login status. 113 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER); 114 115 ui::ShutdownInputMethodForTesting(); 116 zero_duration_mode_.reset(); 117 118 CHECK(!views::corewm::ScopedCaptureClient::IsActive()); 119 120 aura::client::SetWindowStackingClient(NULL); 121 } 122 RunAllPendingInMessageLoop()123void AshTestHelper::RunAllPendingInMessageLoop() { 124 DCHECK(base::MessageLoopForUI::current() == message_loop_); 125 aura::Env::CreateInstance(); 126 base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher()); 127 run_loop.RunUntilIdle(); 128 } 129 CurrentContext()130aura::Window* AshTestHelper::CurrentContext() { 131 aura::Window* root_window = Shell::GetTargetRootWindow(); 132 if (!root_window) 133 root_window = Shell::GetPrimaryRootWindow(); 134 DCHECK(root_window); 135 return root_window; 136 } 137 138 } // namespace test 139 } // namespace ash 140