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