ash_test_helper.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/command_line.h"
13#include "base/run_loop.h"
14#include "ui/aura/env.h"
15#include "ui/base/ime/text_input_test_support.h"
16#include "ui/compositor/scoped_animation_duration_scale_mode.h"
17
18
19#if defined(ENABLE_MESSAGE_CENTER)
20#include "ui/message_center/message_center.h"
21#endif
22
23namespace ash {
24namespace test {
25
26AshTestHelper::AshTestHelper(base::MessageLoopForUI* message_loop)
27    : message_loop_(message_loop),
28      test_shell_delegate_(NULL) {
29  CHECK(message_loop_);
30}
31
32AshTestHelper::~AshTestHelper() {
33}
34
35void AshTestHelper::SetUp() {
36  // TODO(jennyz): Create mock or test AudioHandler so we can instantiate
37  // an ash::Shell with the new CrasAudioHandler. crbug.com/233266
38  CommandLine::ForCurrentProcess()->AppendSwitch(
39      ash::switches::kAshDisableNewAudioHandler);
40  // Disable animations during tests.
41  zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
42      ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
43  ui::TextInputTestSupport::Initialize();
44
45  // Creates Shell and hook with Desktop.
46  test_shell_delegate_ = new TestShellDelegate;
47
48#if defined(ENABLE_MESSAGE_CENTER)
49  // Creates MessageCenter since g_browser_process is not created in AshTestBase
50  // tests.
51  message_center::MessageCenter::Initialize();
52#endif
53  ash::Shell::CreateInstance(test_shell_delegate_);
54  Shell* shell = Shell::GetInstance();
55  test::DisplayManagerTestApi(shell->display_manager()).
56      DisableChangeDisplayUponHostResize();
57  ShellTestApi(shell).DisableOutputConfiguratorAnimation();
58
59}
60
61void AshTestHelper::TearDown() {
62  // Tear down the shell.
63  Shell::DeleteInstance();
64
65#if defined(ENABLE_MESSAGE_CENTER)
66  // Remove global message center state.
67  message_center::MessageCenter::Shutdown();
68#endif
69
70  aura::Env::DeleteInstance();
71  ui::TextInputTestSupport::Shutdown();
72
73  zero_duration_mode_.reset();
74}
75
76void AshTestHelper::RunAllPendingInMessageLoop() {
77#if !defined(OS_MACOSX)
78  DCHECK(MessageLoopForUI::current() == message_loop_);
79  base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
80  run_loop.RunUntilIdle();
81#endif
82}
83
84aura::RootWindow* AshTestHelper::CurrentContext() {
85  aura::RootWindow* root_window = Shell::GetActiveRootWindow();
86  if (!root_window)
87    root_window = Shell::GetPrimaryRootWindow();
88  DCHECK(root_window);
89  return root_window;
90}
91
92}  // namespace test
93}  // namespace ash
94