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.
5 #include "ui/aura/test/aura_test_helper.h"
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_activation_client.h"
11 #include "ui/aura/client/default_capture_client.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/input_state_lookup.h"
15 #include "ui/aura/test/env_test_helper.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"
28 #include "ui/aura/window_tree_host_x11.h"
29 #include "ui/base/x/x11_util.h"
35 AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop)
36 : setup_called_(false),
37 teardown_called_(false),
40 message_loop_ = message_loop;
41 // Disable animations during tests.
42 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
43 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
45 test::SetUseOverrideRedirectWindowByDefault(true);
49 AuraTestHelper::~AuraTestHelper() {
51 << "AuraTestHelper::SetUp() never called.";
52 CHECK(teardown_called_)
53 << "AuraTestHelper::TearDown() never called.";
56 void AuraTestHelper::SetUp() {
59 Env::CreateInstance();
60 // Unit tests generally don't want to query the system, rather use the state
62 EnvTestHelper(Env::GetInstance()).SetInputStateLookup(
63 scoped_ptr<InputStateLookup>());
65 ui::InitializeInputMethodForTesting();
67 test_screen_.reset(TestScreen::Create());
68 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get());
69 host_.reset(test_screen_->CreateHostForPrimaryDisplay());
71 focus_client_.reset(new TestFocusClient);
72 client::SetFocusClient(root_window(), focus_client_.get());
73 stacking_client_.reset(new TestWindowTreeClient(root_window()));
74 activation_client_.reset(
75 new client::DefaultActivationClient(root_window()));
76 capture_client_.reset(new client::DefaultCaptureClient(root_window()));
77 test_input_method_.reset(new ui::DummyInputMethod);
78 root_window()->SetProperty(
79 client::kRootWindowInputMethodKey,
80 test_input_method_.get());
82 root_window()->Show();
83 // Ensure width != height so tests won't confuse them.
84 host()->SetBounds(gfx::Rect(800, 600));
87 void AuraTestHelper::TearDown() {
88 teardown_called_ = true;
89 test_input_method_.reset();
90 stacking_client_.reset();
91 activation_client_.reset();
92 capture_client_.reset();
93 focus_client_.reset();
94 client::SetFocusClient(root_window(), NULL);
96 ui::GestureRecognizer::Reset();
98 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, NULL);
101 ui::ResetXCursorCache();
104 ui::ShutdownInputMethodForTesting();
106 Env::DeleteInstance();
109 void 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();