[email protected] | 09464c7 | 2013-04-26 07:31:28 | [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" | ||||
11 | #include "ash/test/test_shell_delegate.h" | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 12 | #include "base/run_loop.h" |
13 | #include "ui/aura/env.h" | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 14 | #include "ui/base/ime/text_input_test_support.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 15 | #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 16 | #include "ui/message_center/message_center.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 17 | |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 18 | #if defined(OS_CHROMEOS) |
19 | #include "chromeos/audio/cras_audio_handler.h" | ||||
[email protected] | 1d28ca0 | 2013-05-16 05:40:38 | [diff] [blame] | 20 | #include "chromeos/power/power_manager_handler.h" |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 21 | #endif |
22 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 23 | namespace ash { |
24 | namespace test { | ||||
25 | |||||
26 | AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop) | ||||
27 | : message_loop_(message_loop), | ||||
28 | test_shell_delegate_(NULL) { | ||||
29 | CHECK(message_loop_); | ||||
30 | } | ||||
31 | |||||
32 | AshTestHelper::~AshTestHelper() { | ||||
33 | } | ||||
34 | |||||
35 | void AshTestHelper::SetUp() { | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 36 | // Disable animations during tests. |
37 | zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | ||||
38 | ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | ||||
39 | ui::TextInputTestSupport::Initialize(); | ||||
40 | |||||
41 | // Creates Shell and hook with Desktop. | ||||
42 | test_shell_delegate_ = new TestShellDelegate; | ||||
43 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 44 | // Creates MessageCenter since g_browser_process is not created in AshTestBase |
45 | // tests. | ||||
46 | message_center::MessageCenter::Initialize(); | ||||
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 47 | |
48 | #if defined(OS_CHROMEOS) | ||||
49 | if (ash::switches::UseNewAudioHandler()) { | ||||
50 | // Create CrasAuidoHandler for testing since g_browser_process is not | ||||
51 | // created in AshTestBase tests. | ||||
52 | chromeos::CrasAudioHandler::InitializeForTesting(); | ||||
53 | } | ||||
[email protected] | 1d28ca0 | 2013-05-16 05:40:38 | [diff] [blame] | 54 | chromeos::PowerManagerHandler::Initialize(); |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 55 | #endif |
56 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 57 | ash::Shell::CreateInstance(test_shell_delegate_); |
58 | Shell* shell = Shell::GetInstance(); | ||||
59 | test::DisplayManagerTestApi(shell->display_manager()). | ||||
60 | DisableChangeDisplayUponHostResize(); | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 61 | ShellTestApi(shell).DisableOutputConfiguratorAnimation(); |
62 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 63 | } |
64 | |||||
65 | void AshTestHelper::TearDown() { | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 66 | // Tear down the shell. |
67 | Shell::DeleteInstance(); | ||||
68 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 69 | // Remove global message center state. |
70 | message_center::MessageCenter::Shutdown(); | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 71 | |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 72 | #if defined(OS_CHROMEOS) |
73 | if (ash::switches::UseNewAudioHandler()) | ||||
74 | chromeos::CrasAudioHandler::Shutdown(); | ||||
[email protected] | 1d28ca0 | 2013-05-16 05:40:38 | [diff] [blame] | 75 | chromeos::PowerManagerHandler::Shutdown(); |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 76 | #endif |
77 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 78 | aura::Env::DeleteInstance(); |
79 | ui::TextInputTestSupport::Shutdown(); | ||||
80 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 81 | zero_duration_mode_.reset(); |
82 | } | ||||
83 | |||||
84 | void AshTestHelper::RunAllPendingInMessageLoop() { | ||||
85 | #if !defined(OS_MACOSX) | ||||
86 | DCHECK(MessageLoopForUI::current() == message_loop_); | ||||
87 | base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher()); | ||||
88 | run_loop.RunUntilIdle(); | ||||
89 | #endif | ||||
90 | } | ||||
91 | |||||
92 | aura::RootWindow* AshTestHelper::CurrentContext() { | ||||
93 | aura::RootWindow* root_window = Shell::GetActiveRootWindow(); | ||||
94 | if (!root_window) | ||||
95 | root_window = Shell::GetPrimaryRootWindow(); | ||||
96 | DCHECK(root_window); | ||||
97 | return root_window; | ||||
98 | } | ||||
99 | |||||
100 | } // namespace test | ||||
101 | } // namespace ash |