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