ash_test_helper.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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_session_state_delegate.h"
12#include "ash/test/test_shell_delegate.h"
13#include "ash/test/test_system_tray_delegate.h"
14#include "base/run_loop.h"
15#include "ui/aura/env.h"
16#include "ui/base/ime/input_method_initializer.h"
17#include "ui/compositor/scoped_animation_duration_scale_mode.h"
18#include "ui/message_center/message_center.h"
19#include "ui/views/corewm/capture_controller.h"
20
21#if defined(OS_CHROMEOS)
22#include "chromeos/audio/cras_audio_handler.h"
23#include "chromeos/network/network_handler.h"
24#endif
25
26#if defined(USE_X11)
27#include "ui/aura/root_window_host_x11.h"
28#endif
29
30namespace ash {
31namespace test {
32
33AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
34    : message_loop_(message_loop),
35      test_shell_delegate_(NULL),
36      tear_down_network_handler_(false) {
37  CHECK(message_loop_);
38#if defined(USE_X11)
39  aura::test::SetUseOverrideRedirectWindowByDefault(true);
40#endif
41}
42
43AshTestHelper::~AshTestHelper() {
44}
45
46void AshTestHelper::SetUp(bool start_session) {
47  // Disable animations during tests.
48  zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
49      ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
50  ui::InitializeInputMethodForTesting();
51
52  // Creates Shell and hook with Desktop.
53  test_shell_delegate_ = new TestShellDelegate;
54
55  // Creates MessageCenter since g_browser_process is not created in AshTestBase
56  // tests.
57  message_center::MessageCenter::Initialize();
58
59#if defined(OS_CHROMEOS)
60  // Create CrasAudioHandler for testing since g_browser_process is not
61  // created in AshTestBase tests.
62  chromeos::CrasAudioHandler::InitializeForTesting();
63
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
73  ash::Shell::CreateInstance(test_shell_delegate_);
74  Shell* shell = Shell::GetInstance();
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  }
81
82  test::DisplayManagerTestApi(shell->display_manager()).
83      DisableChangeDisplayUponHostResize();
84  ShellTestApi(shell).DisableOutputConfiguratorAnimation();
85}
86
87void AshTestHelper::TearDown() {
88  // Tear down the shell.
89  Shell::DeleteInstance();
90
91  // Remove global message center state.
92  message_center::MessageCenter::Shutdown();
93
94#if defined(OS_CHROMEOS)
95  if (tear_down_network_handler_ && chromeos::NetworkHandler::IsInitialized())
96    chromeos::NetworkHandler::Shutdown();
97  chromeos::CrasAudioHandler::Shutdown();
98#endif
99
100  aura::Env::DeleteInstance();
101
102  // Need to reset the initial login status.
103  TestSystemTrayDelegate::SetInitialLoginStatus(user::LOGGED_IN_USER);
104
105  ui::ShutdownInputMethodForTesting();
106  zero_duration_mode_.reset();
107
108  CHECK(!views::corewm::ScopedCaptureClient::IsActive());
109}
110
111void AshTestHelper::RunAllPendingInMessageLoop() {
112  DCHECK(base::MessageLoopForUI::current() == message_loop_);
113  base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
114  run_loop.RunUntilIdle();
115}
116
117aura::RootWindow* AshTestHelper::CurrentContext() {
118  aura::RootWindow* root_window = Shell::GetTargetRootWindow();
119  if (!root_window)
120    root_window = Shell::GetPrimaryRootWindow();
121  DCHECK(root_window);
122  return root_window;
123}
124
125}  // namespace test
126}  // namespace ash
127