Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 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/ash_switches.h" |
| 8 | #include "ash/shell.h" |
| 9 | #include "ash/test/display_manager_test_api.h" |
| 10 | #include "ash/test/shell_test_api.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 11 | #include "ash/test/test_session_state_delegate.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 12 | #include "ash/test/test_shell_delegate.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 13 | #include "base/run_loop.h" |
| 14 | #include "ui/aura/env.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 15 | #include "ui/base/ime/input_method_initializer.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 16 | #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 17 | #include "ui/message_center/message_center.h" |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 18 | |
| 19 | #if defined(OS_CHROMEOS) |
| 20 | #include "chromeos/audio/cras_audio_handler.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | #if defined(USE_X11) |
| 24 | #include "ui/aura/root_window_host_x11.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | namespace ash { |
| 28 | namespace test { |
| 29 | |
| 30 | AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop) |
| 31 | : message_loop_(message_loop), |
| 32 | test_shell_delegate_(NULL) { |
| 33 | CHECK(message_loop_); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 34 | #if defined(USE_X11) |
| 35 | aura::test::SetUseOverrideRedirectWindowByDefault(true); |
| 36 | #endif |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | AshTestHelper::~AshTestHelper() { |
| 40 | } |
| 41 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 42 | void AshTestHelper::SetUp(bool start_session) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 43 | // Disable animations during tests. |
| 44 | zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( |
| 45 | ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 46 | ui::InitializeInputMethodForTesting(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 47 | |
| 48 | // Creates Shell and hook with Desktop. |
| 49 | test_shell_delegate_ = new TestShellDelegate; |
| 50 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 51 | // Creates MessageCenter since g_browser_process is not created in AshTestBase |
| 52 | // tests. |
| 53 | message_center::MessageCenter::Initialize(); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 54 | |
| 55 | #if defined(OS_CHROMEOS) |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 56 | // Create CrasAudioHandler for testing since g_browser_process is not |
| 57 | // created in AshTestBase tests. |
| 58 | chromeos::CrasAudioHandler::InitializeForTesting(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 59 | #endif |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 60 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 61 | ash::Shell::CreateInstance(test_shell_delegate_); |
| 62 | Shell* shell = Shell::GetInstance(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 63 | if (start_session) { |
| 64 | test_shell_delegate_->test_session_state_delegate()-> |
| 65 | SetActiveUserSessionStarted(true); |
| 66 | test_shell_delegate_->test_session_state_delegate()-> |
| 67 | SetHasActiveUser(true); |
| 68 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 69 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 70 | test::DisplayManagerTestApi(shell->display_manager()). |
| 71 | DisableChangeDisplayUponHostResize(); |
| 72 | ShellTestApi(shell).DisableOutputConfiguratorAnimation(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void AshTestHelper::TearDown() { |
| 76 | // Tear down the shell. |
| 77 | Shell::DeleteInstance(); |
| 78 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 79 | // Remove global message center state. |
| 80 | message_center::MessageCenter::Shutdown(); |
Torne (Richard Coles) | a93a17c | 2013-05-15 11:34:50 +0100 | [diff] [blame] | 81 | |
| 82 | #if defined(OS_CHROMEOS) |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 83 | chromeos::CrasAudioHandler::Shutdown(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | aura::Env::DeleteInstance(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 87 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 88 | ui::ShutdownInputMethodForTesting(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 89 | zero_duration_mode_.reset(); |
| 90 | } |
| 91 | |
| 92 | void AshTestHelper::RunAllPendingInMessageLoop() { |
| 93 | #if !defined(OS_MACOSX) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 94 | DCHECK(base::MessageLoopForUI::current() == message_loop_); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 95 | base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher()); |
| 96 | run_loop.RunUntilIdle(); |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | aura::RootWindow* AshTestHelper::CurrentContext() { |
| 101 | aura::RootWindow* root_window = Shell::GetActiveRootWindow(); |
| 102 | if (!root_window) |
| 103 | root_window = Shell::GetPrimaryRootWindow(); |
| 104 | DCHECK(root_window); |
| 105 | return root_window; |
| 106 | } |
| 107 | |
| 108 | } // namespace test |
| 109 | } // namespace ash |