[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" | ||||
[email protected] | f7ff7ba | 2013-08-21 21:03:04 | [diff] [blame] | 9 | #include "ash/system/tray/test_system_tray_delegate.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 10 | #include "ash/test/display_manager_test_api.h" |
11 | #include "ash/test/shell_test_api.h" | ||||
[email protected] | ad2f5df | 2013-07-02 08:21:56 | [diff] [blame] | 12 | #include "ash/test/test_session_state_delegate.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 13 | #include "ash/test/test_shell_delegate.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 14 | #include "base/run_loop.h" |
15 | #include "ui/aura/env.h" | ||||
[email protected] | 749bf643 | 2013-06-12 16:00:08 | [diff] [blame] | 16 | #include "ui/base/ime/input_method_initializer.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 17 | #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 18 | #include "ui/message_center/message_center.h" |
[email protected] | 23a2dc8d | 2013-08-22 15:04:22 | [diff] [blame^] | 19 | #include "ui/views/corewm/capture_controller.h" |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 20 | |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 21 | #if defined(OS_CHROMEOS) |
22 | #include "chromeos/audio/cras_audio_handler.h" | ||||
[email protected] | 0734308 | 2013-08-13 03:46:34 | [diff] [blame] | 23 | #include "chromeos/network/network_handler.h" |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 24 | #endif |
25 | |||||
[email protected] | 6b9aa60 | 2013-05-18 09:08:40 | [diff] [blame] | 26 | #if defined(USE_X11) |
27 | #include "ui/aura/root_window_host_x11.h" | ||||
28 | #endif | ||||
29 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 30 | namespace ash { |
31 | namespace test { | ||||
32 | |||||
33 | AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop) | ||||
34 | : message_loop_(message_loop), | ||||
[email protected] | 0734308 | 2013-08-13 03:46:34 | [diff] [blame] | 35 | test_shell_delegate_(NULL), |
36 | tear_down_network_handler_(false) { | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 37 | CHECK(message_loop_); |
[email protected] | 6b9aa60 | 2013-05-18 09:08:40 | [diff] [blame] | 38 | #if defined(USE_X11) |
39 | aura::test::SetUseOverrideRedirectWindowByDefault(true); | ||||
40 | #endif | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 41 | } |
42 | |||||
43 | AshTestHelper::~AshTestHelper() { | ||||
44 | } | ||||
45 | |||||
[email protected] | cf6fea2 | 2013-08-07 14:24:01 | [diff] [blame] | 46 | void AshTestHelper::SetUp(bool start_session) { |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 47 | // Disable animations during tests. |
48 | zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | ||||
49 | ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | ||||
[email protected] | 749bf643 | 2013-06-12 16:00:08 | [diff] [blame] | 50 | ui::InitializeInputMethodForTesting(); |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 51 | |
52 | // Creates Shell and hook with Desktop. | ||||
53 | test_shell_delegate_ = new TestShellDelegate; | ||||
54 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 55 | // Creates MessageCenter since g_browser_process is not created in AshTestBase |
56 | // tests. | ||||
57 | message_center::MessageCenter::Initialize(); | ||||
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 58 | |
59 | #if defined(OS_CHROMEOS) | ||||
[email protected] | 934e3c9 | 2013-08-01 00:18:23 | [diff] [blame] | 60 | // Create CrasAudioHandler for testing since g_browser_process is not |
61 | // created in AshTestBase tests. | ||||
62 | chromeos::CrasAudioHandler::InitializeForTesting(); | ||||
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 63 | |
[email protected] | 0734308 | 2013-08-13 03:46:34 | [diff] [blame] | 64 | // Some tests may not initialize NetworkHandler. Initialize it here if that |
65 | // is the case. | ||||
66 | if (!chromeos::NetworkHandler::IsInitialized()) { | ||||
67 | tear_down_network_handler_ = true; | ||||
68 | chromeos::NetworkHandler::Initialize(); | ||||
69 | } | ||||
70 | |||||
71 | RunAllPendingInMessageLoop(); | ||||
72 | #endif | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 73 | ash::Shell::CreateInstance(test_shell_delegate_); |
74 | Shell* shell = Shell::GetInstance(); | ||||
[email protected] | cf6fea2 | 2013-08-07 14:24:01 | [diff] [blame] | 75 | if (start_session) { |
76 | test_shell_delegate_->test_session_state_delegate()-> | ||||
77 | SetActiveUserSessionStarted(true); | ||||
78 | test_shell_delegate_->test_session_state_delegate()-> | ||||
79 | SetHasActiveUser(true); | ||||
80 | } | ||||
[email protected] | ad2f5df | 2013-07-02 08:21:56 | [diff] [blame] | 81 | |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 82 | test::DisplayManagerTestApi(shell->display_manager()). |
83 | DisableChangeDisplayUponHostResize(); | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 84 | ShellTestApi(shell).DisableOutputConfiguratorAnimation(); |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 85 | } |
86 | |||||
87 | void AshTestHelper::TearDown() { | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 88 | // Tear down the shell. |
89 | Shell::DeleteInstance(); | ||||
90 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 91 | // Remove global message center state. |
92 | message_center::MessageCenter::Shutdown(); | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 93 | |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 94 | #if defined(OS_CHROMEOS) |
[email protected] | 0734308 | 2013-08-13 03:46:34 | [diff] [blame] | 95 | if (tear_down_network_handler_ && chromeos::NetworkHandler::IsInitialized()) |
96 | chromeos::NetworkHandler::Shutdown(); | ||||
[email protected] | 934e3c9 | 2013-08-01 00:18:23 | [diff] [blame] | 97 | chromeos::CrasAudioHandler::Shutdown(); |
[email protected] | 6d8c8f0 | 2013-05-13 17:33:12 | [diff] [blame] | 98 | #endif |
99 | |||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 100 | aura::Env::DeleteInstance(); |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 101 | |
[email protected] | f7ff7ba | 2013-08-21 21:03:04 | [diff] [blame] | 102 | // Need to reset the initial login status. |
103 | TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER); | ||||
104 | |||||
[email protected] | 749bf643 | 2013-06-12 16:00:08 | [diff] [blame] | 105 | ui::ShutdownInputMethodForTesting(); |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 106 | zero_duration_mode_.reset(); |
[email protected] | 23a2dc8d | 2013-08-22 15:04:22 | [diff] [blame^] | 107 | |
108 | CHECK(!views::corewm::ScopedCaptureClient::IsActive()); | ||||
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 109 | } |
110 | |||||
111 | void AshTestHelper::RunAllPendingInMessageLoop() { | ||||
112 | #if !defined(OS_MACOSX) | ||||
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 113 | DCHECK(base::MessageLoopForUI::current() == message_loop_); |
[email protected] | 09464c7 | 2013-04-26 07:31:28 | [diff] [blame] | 114 | base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher()); |
115 | run_loop.RunUntilIdle(); | ||||
116 | #endif | ||||
117 | } | ||||
118 | |||||
119 | aura::RootWindow* AshTestHelper::CurrentContext() { | ||||
120 | aura::RootWindow* root_window = Shell::GetActiveRootWindow(); | ||||
121 | if (!root_window) | ||||
122 | root_window = Shell::GetPrimaryRootWindow(); | ||||
123 | DCHECK(root_window); | ||||
124 | return root_window; | ||||
125 | } | ||||
126 | |||||
127 | } // namespace test | ||||
128 | } // namespace ash |