blob: b7391e31a2f0acb51184cd189bff294472f1067c [file] [log] [blame]
[email protected]09464c72013-04-26 07:31:281// 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]f7ff7ba2013-08-21 21:03:049#include "ash/system/tray/test_system_tray_delegate.h"
[email protected]09464c72013-04-26 07:31:2810#include "ash/test/display_manager_test_api.h"
11#include "ash/test/shell_test_api.h"
[email protected]ad2f5df2013-07-02 08:21:5612#include "ash/test/test_session_state_delegate.h"
[email protected]09464c72013-04-26 07:31:2813#include "ash/test/test_shell_delegate.h"
[email protected]09464c72013-04-26 07:31:2814#include "base/run_loop.h"
15#include "ui/aura/env.h"
[email protected]749bf6432013-06-12 16:00:0816#include "ui/base/ime/input_method_initializer.h"
[email protected]09464c72013-04-26 07:31:2817#include "ui/compositor/scoped_animation_duration_scale_mode.h"
[email protected]09464c72013-04-26 07:31:2818#include "ui/message_center/message_center.h"
[email protected]23a2dc8d2013-08-22 15:04:2219#include "ui/views/corewm/capture_controller.h"
[email protected]09464c72013-04-26 07:31:2820
[email protected]6d8c8f02013-05-13 17:33:1221#if defined(OS_CHROMEOS)
22#include "chromeos/audio/cras_audio_handler.h"
[email protected]07343082013-08-13 03:46:3423#include "chromeos/network/network_handler.h"
[email protected]6d8c8f02013-05-13 17:33:1224#endif
25
[email protected]6b9aa602013-05-18 09:08:4026#if defined(USE_X11)
27#include "ui/aura/root_window_host_x11.h"
28#endif
29
[email protected]09464c72013-04-26 07:31:2830namespace ash {
31namespace test {
32
33AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
34 : message_loop_(message_loop),
[email protected]07343082013-08-13 03:46:3435 test_shell_delegate_(NULL),
36 tear_down_network_handler_(false) {
[email protected]09464c72013-04-26 07:31:2837 CHECK(message_loop_);
[email protected]6b9aa602013-05-18 09:08:4038#if defined(USE_X11)
39 aura::test::SetUseOverrideRedirectWindowByDefault(true);
40#endif
[email protected]09464c72013-04-26 07:31:2841}
42
43AshTestHelper::~AshTestHelper() {
44}
45
[email protected]cf6fea22013-08-07 14:24:0146void AshTestHelper::SetUp(bool start_session) {
[email protected]09464c72013-04-26 07:31:2847 // Disable animations during tests.
48 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
49 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
[email protected]749bf6432013-06-12 16:00:0850 ui::InitializeInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:2851
52 // Creates Shell and hook with Desktop.
53 test_shell_delegate_ = new TestShellDelegate;
54
[email protected]09464c72013-04-26 07:31:2855 // Creates MessageCenter since g_browser_process is not created in AshTestBase
56 // tests.
57 message_center::MessageCenter::Initialize();
[email protected]6d8c8f02013-05-13 17:33:1258
59#if defined(OS_CHROMEOS)
[email protected]934e3c92013-08-01 00:18:2360 // Create CrasAudioHandler for testing since g_browser_process is not
61 // created in AshTestBase tests.
62 chromeos::CrasAudioHandler::InitializeForTesting();
[email protected]6d8c8f02013-05-13 17:33:1263
[email protected]07343082013-08-13 03:46:3464 // 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]09464c72013-04-26 07:31:2873 ash::Shell::CreateInstance(test_shell_delegate_);
74 Shell* shell = Shell::GetInstance();
[email protected]cf6fea22013-08-07 14:24:0175 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]ad2f5df2013-07-02 08:21:5681
[email protected]09464c72013-04-26 07:31:2882 test::DisplayManagerTestApi(shell->display_manager()).
83 DisableChangeDisplayUponHostResize();
[email protected]09464c72013-04-26 07:31:2884 ShellTestApi(shell).DisableOutputConfiguratorAnimation();
[email protected]09464c72013-04-26 07:31:2885}
86
87void AshTestHelper::TearDown() {
[email protected]09464c72013-04-26 07:31:2888 // Tear down the shell.
89 Shell::DeleteInstance();
90
[email protected]09464c72013-04-26 07:31:2891 // Remove global message center state.
92 message_center::MessageCenter::Shutdown();
[email protected]09464c72013-04-26 07:31:2893
[email protected]6d8c8f02013-05-13 17:33:1294#if defined(OS_CHROMEOS)
[email protected]07343082013-08-13 03:46:3495 if (tear_down_network_handler_ && chromeos::NetworkHandler::IsInitialized())
96 chromeos::NetworkHandler::Shutdown();
[email protected]934e3c92013-08-01 00:18:2397 chromeos::CrasAudioHandler::Shutdown();
[email protected]6d8c8f02013-05-13 17:33:1298#endif
99
[email protected]09464c72013-04-26 07:31:28100 aura::Env::DeleteInstance();
[email protected]09464c72013-04-26 07:31:28101
[email protected]f7ff7ba2013-08-21 21:03:04102 // Need to reset the initial login status.
103 TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER);
104
[email protected]749bf6432013-06-12 16:00:08105 ui::ShutdownInputMethodForTesting();
[email protected]09464c72013-04-26 07:31:28106 zero_duration_mode_.reset();
[email protected]23a2dc8d2013-08-22 15:04:22107
108 CHECK(!views::corewm::ScopedCaptureClient::IsActive());
[email protected]09464c72013-04-26 07:31:28109}
110
111void AshTestHelper::RunAllPendingInMessageLoop() {
112#if !defined(OS_MACOSX)
[email protected]9e7154122013-05-30 23:11:04113 DCHECK(base::MessageLoopForUI::current() == message_loop_);
[email protected]09464c72013-04-26 07:31:28114 base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
115 run_loop.RunUntilIdle();
116#endif
117}
118
119aura::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