ash_test_helper.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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"
12#include "base/run_loop.h"
13#include "ui/aura/env.h"
14#include "ui/base/ime/text_input_test_support.h"
15#include "ui/compositor/scoped_animation_duration_scale_mode.h"
16#include "ui/message_center/message_center.h"
17
18#if defined(OS_CHROMEOS)
19#include "chromeos/audio/cras_audio_handler.h"
20#include "chromeos/power/power_manager_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::TextInputTestSupport::Initialize();
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  chromeos::PowerManagerHandler::Initialize();
62#endif
63
64  ash::Shell::CreateInstance(test_shell_delegate_);
65  Shell* shell = Shell::GetInstance();
66  test::DisplayManagerTestApi(shell->display_manager()).
67      DisableChangeDisplayUponHostResize();
68  ShellTestApi(shell).DisableOutputConfiguratorAnimation();
69
70}
71
72void AshTestHelper::TearDown() {
73  // Tear down the shell.
74  Shell::DeleteInstance();
75
76  // Remove global message center state.
77  message_center::MessageCenter::Shutdown();
78
79#if defined(OS_CHROMEOS)
80  if (ash::switches::UseNewAudioHandler())
81    chromeos::CrasAudioHandler::Shutdown();
82  chromeos::PowerManagerHandler::Shutdown();
83#endif
84
85  aura::Env::DeleteInstance();
86  ui::TextInputTestSupport::Shutdown();
87
88  zero_duration_mode_.reset();
89}
90
91void AshTestHelper::RunAllPendingInMessageLoop() {
92#if !defined(OS_MACOSX)
93  DCHECK(MessageLoopForUI::current() == message_loop_);
94  base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
95  run_loop.RunUntilIdle();
96#endif
97}
98
99aura::RootWindow* AshTestHelper::CurrentContext() {
100  aura::RootWindow* root_window = Shell::GetActiveRootWindow();
101  if (!root_window)
102    root_window = Shell::GetPrimaryRootWindow();
103  DCHECK(root_window);
104  return root_window;
105}
106
107}  // namespace test
108}  // namespace ash
109