1// Copyright (c) 2012 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 "ui/aura/test/aura_test_helper.h"
6
7#include "base/message_loop/message_loop.h"
8#include "base/run_loop.h"
9#include "ui/aura/client/aura_constants.h"
10#include "ui/aura/client/default_capture_client.h"
11#include "ui/aura/client/focus_client.h"
12#include "ui/aura/env.h"
13#include "ui/aura/input_state_lookup.h"
14#include "ui/aura/test/env_test_helper.h"
15#include "ui/aura/test/event_generator_delegate_aura.h"
16#include "ui/aura/test/test_focus_client.h"
17#include "ui/aura/test/test_screen.h"
18#include "ui/aura/test/test_window_tree_client.h"
19#include "ui/aura/window_event_dispatcher.h"
20#include "ui/base/ime/dummy_input_method.h"
21#include "ui/base/ime/input_method_initializer.h"
22#include "ui/compositor/compositor.h"
23#include "ui/compositor/layer_animator.h"
24#include "ui/compositor/scoped_animation_duration_scale_mode.h"
25#include "ui/gfx/screen.h"
26
27#if defined(USE_X11)
28#include "ui/aura/window_tree_host_x11.h"
29#include "ui/base/x/x11_util.h"
30#endif
31
32namespace aura {
33namespace test {
34
35AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop)
36    : setup_called_(false),
37      teardown_called_(false),
38      owns_host_(false) {
39  DCHECK(message_loop);
40  message_loop_ = message_loop;
41  // Disable animations during tests.
42  zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
43      ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
44#if defined(USE_X11)
45  test::SetUseOverrideRedirectWindowByDefault(true);
46#endif
47  InitializeAuraEventGeneratorDelegate();
48}
49
50AuraTestHelper::~AuraTestHelper() {
51  CHECK(setup_called_)
52      << "AuraTestHelper::SetUp() never called.";
53  CHECK(teardown_called_)
54      << "AuraTestHelper::TearDown() never called.";
55}
56
57void AuraTestHelper::SetUp(ui::ContextFactory* context_factory) {
58  setup_called_ = true;
59
60  Env::CreateInstance(true);
61  Env::GetInstance()->set_context_factory(context_factory);
62  // Unit tests generally don't want to query the system, rather use the state
63  // from RootWindow.
64  EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
65      scoped_ptr<InputStateLookup>());
66
67  ui::InitializeInputMethodForTesting();
68
69  gfx::Size host_size(800, 600);
70  test_screen_.reset(TestScreen::Create(host_size));
71  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get());
72  host_.reset(test_screen_->CreateHostForPrimaryDisplay());
73
74  focus_client_.reset(new TestFocusClient);
75  client::SetFocusClient(root_window(), focus_client_.get());
76  stacking_client_.reset(new TestWindowTreeClient(root_window()));
77  capture_client_.reset(new client::DefaultCaptureClient(root_window()));
78  test_input_method_.reset(new ui::DummyInputMethod);
79  root_window()->SetProperty(
80      client::kRootWindowInputMethodKey,
81      test_input_method_.get());
82
83  root_window()->Show();
84  // Ensure width != height so tests won't confuse them.
85  host()->SetBounds(gfx::Rect(host_size));
86}
87
88void AuraTestHelper::TearDown() {
89  teardown_called_ = true;
90  test_input_method_.reset();
91  stacking_client_.reset();
92  capture_client_.reset();
93  focus_client_.reset();
94  client::SetFocusClient(root_window(), NULL);
95  host_.reset();
96  ui::GestureRecognizer::Reset();
97  test_screen_.reset();
98  gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
99
100#if defined(USE_X11)
101  ui::test::ResetXCursorCache();
102#endif
103
104  ui::ShutdownInputMethodForTesting();
105
106  Env::DeleteInstance();
107}
108
109void AuraTestHelper::RunAllPendingInMessageLoop() {
110  // TODO(jbates) crbug.com/134753 Find quitters of this RunLoop and have them
111  //              use run_loop.QuitClosure().
112  base::RunLoop run_loop;
113  run_loop.RunUntilIdle();
114}
115
116}  // namespace test
117}  // namespace aura
118