blob: 45a1740953e8ab330ebcbea6c1528f9c87a7c0c9 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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"
Ben Murdocheb525c52013-07-10 11:40:50 +010011#include "ash/test/test_session_state_delegate.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010012#include "ash/test/test_shell_delegate.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010013#include "base/run_loop.h"
14#include "ui/aura/env.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010015#include "ui/base/ime/input_method_initializer.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010016#include "ui/compositor/scoped_animation_duration_scale_mode.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010017#include "ui/message_center/message_center.h"
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +010018
19#if defined(OS_CHROMEOS)
20#include "chromeos/audio/cras_audio_handler.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010021#endif
22
23#if defined(USE_X11)
24#include "ui/aura/root_window_host_x11.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010025#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_);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010034#if defined(USE_X11)
35 aura::test::SetUseOverrideRedirectWindowByDefault(true);
36#endif
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010037}
38
39AshTestHelper::~AshTestHelper() {
40}
41
Ben Murdochbb1529c2013-08-08 10:24:53 +010042void AshTestHelper::SetUp(bool start_session) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010043 // Disable animations during tests.
44 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
45 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010046 ui::InitializeInputMethodForTesting();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010047
48 // Creates Shell and hook with Desktop.
49 test_shell_delegate_ = new TestShellDelegate;
50
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010051 // Creates MessageCenter since g_browser_process is not created in AshTestBase
52 // tests.
53 message_center::MessageCenter::Initialize();
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +010054
55#if defined(OS_CHROMEOS)
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010056 // Create CrasAudioHandler for testing since g_browser_process is not
57 // created in AshTestBase tests.
58 chromeos::CrasAudioHandler::InitializeForTesting();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010059#endif
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +010060
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010061 ash::Shell::CreateInstance(test_shell_delegate_);
62 Shell* shell = Shell::GetInstance();
Ben Murdochbb1529c2013-08-08 10:24:53 +010063 if (start_session) {
64 test_shell_delegate_->test_session_state_delegate()->
65 SetActiveUserSessionStarted(true);
66 test_shell_delegate_->test_session_state_delegate()->
67 SetHasActiveUser(true);
68 }
Ben Murdocheb525c52013-07-10 11:40:50 +010069
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010070 test::DisplayManagerTestApi(shell->display_manager()).
71 DisableChangeDisplayUponHostResize();
72 ShellTestApi(shell).DisableOutputConfiguratorAnimation();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010073}
74
75void AshTestHelper::TearDown() {
76 // Tear down the shell.
77 Shell::DeleteInstance();
78
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010079 // Remove global message center state.
80 message_center::MessageCenter::Shutdown();
Torne (Richard Coles)a93a17c2013-05-15 11:34:50 +010081
82#if defined(OS_CHROMEOS)
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010083 chromeos::CrasAudioHandler::Shutdown();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010084#endif
85
86 aura::Env::DeleteInstance();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010087
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010088 ui::ShutdownInputMethodForTesting();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010089 zero_duration_mode_.reset();
90}
91
92void AshTestHelper::RunAllPendingInMessageLoop() {
93#if !defined(OS_MACOSX)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010094 DCHECK(base::MessageLoopForUI::current() == message_loop_);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010095 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