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 "chrome/test/base/view_event_test_base.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/test/base/interactive_test_utils.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "ui/base/ime/input_method_initializer.h"
15 #include "ui/base/test/ui_controls.h"
16 #include "ui/compositor/test/context_factories_for_test.h"
17 #include "ui/message_center/message_center.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/desktop_aura/desktop_screen.h"
20 #include "ui/views/widget/widget.h"
23 #include "ash/shell.h"
24 #include "ash/test/test_session_state_delegate.h"
25 #include "ash/test/test_shell_delegate.h"
29 #include "ui/aura/client/event_client.h"
30 #include "ui/aura/env.h"
31 #include "ui/aura/test/aura_test_helper.h"
32 #include "ui/aura/window_event_dispatcher.h"
33 #include "ui/aura/window_tree_host.h"
34 #include "ui/compositor/test/context_factories_for_test.h"
35 #include "ui/views/corewm/wm_state.h"
38 #if defined(OS_CHROMEOS)
39 #include "chromeos/audio/cras_audio_handler.h"
40 #include "chromeos/dbus/dbus_thread_manager.h"
41 #include "chromeos/network/network_handler.h"
46 // View subclass that allows you to specify the preferred size.
47 class TestView
: public views::View
{
51 void SetPreferredSize(const gfx::Size
& size
) {
52 preferred_size_
= size
;
53 PreferredSizeChanged();
56 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
57 if (!preferred_size_
.IsEmpty())
58 return preferred_size_
;
59 return View::GetPreferredSize();
62 virtual void Layout() OVERRIDE
{
63 View
* child_view
= child_at(0);
64 child_view
->SetBounds(0, 0, width(), height());
68 gfx::Size preferred_size_
;
70 DISALLOW_COPY_AND_ASSIGN(TestView
);
73 // Delay in background thread before posting mouse move.
74 const int kMouseMoveDelayMS
= 200;
78 ViewEventTestBase::ViewEventTestBase()
81 // The TestingBrowserProcess must be created in the constructor because there
82 // are tests that require it before SetUp() is called.
83 TestingBrowserProcess::CreateInstance();
86 void ViewEventTestBase::Done() {
87 base::MessageLoop::current()->Quit();
89 // If we're in a nested message loop, as is the case with menus, we
90 // need to quit twice. The second quit does that for us. Finish all
91 // pending UI events before posting closure because events it may be
92 // executed before UI events are executed.
93 ui_controls::RunClosureAfterAllPendingUIEvents(
94 base::MessageLoop::QuitClosure());
97 void ViewEventTestBase::SetUp() {
99 wm_state_
.reset(new views::corewm::WMState
);
102 views::ViewsDelegate::views_delegate
= &views_delegate_
;
103 ui::InitializeInputMethodForTesting();
104 gfx::NativeView context
= NULL
;
106 #if defined(USE_AURA)
107 // The ContextFactory must exist before any Compositors are created.
108 bool enable_pixel_output
= false;
109 ui::InitializeContextFactoryForTests(enable_pixel_output
);
114 // http://crbug.com/154081 use ash::Shell code path below on win_ash bots when
115 // interactive_ui_tests is brought up on that platform.
116 gfx::Screen::SetScreenInstance(
117 gfx::SCREEN_TYPE_NATIVE
, views::CreateDesktopScreen());
120 // Ash Shell can't just live on its own without a browser process, we need to
121 // also create the message center.
122 message_center::MessageCenter::Initialize();
123 #if defined(OS_CHROMEOS)
124 chromeos::DBusThreadManager::InitializeWithStub();
125 chromeos::CrasAudioHandler::InitializeForTesting();
126 chromeos::NetworkHandler::Initialize();
127 #endif // OS_CHROMEOS
128 ash::test::TestShellDelegate
* shell_delegate
=
129 new ash::test::TestShellDelegate();
130 ash::Shell::CreateInstance(shell_delegate
);
131 shell_delegate
->test_session_state_delegate()
132 ->SetActiveUserSessionStarted(true);
133 context
= ash::Shell::GetPrimaryRootWindow();
134 context
->GetDispatcher()->host()->Show();
136 aura::Env::CreateInstance();
137 #elif defined(USE_AURA)
138 // Instead of using the ash shell, use an AuraTestHelper to create and manage
140 aura_test_helper_
.reset(
141 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
142 aura_test_helper_
->SetUp();
143 context
= aura_test_helper_
->root_window();
144 #endif // !USE_ASH && USE_AURA
146 window_
= views::Widget::CreateWindowWithContext(this, context
);
149 void ViewEventTestBase::TearDown() {
152 content::RunAllPendingInMessageLoop();
158 ash::Shell::DeleteInstance();
159 #if defined(OS_CHROMEOS)
160 chromeos::NetworkHandler::Shutdown();
161 chromeos::CrasAudioHandler::Shutdown();
162 chromeos::DBusThreadManager::Shutdown();
164 // Ash Shell can't just live on its own without a browser process, we need to
165 // also shut down the message center.
166 message_center::MessageCenter::Shutdown();
168 aura::Env::DeleteInstance();
169 #elif defined(USE_AURA)
170 aura_test_helper_
->TearDown();
171 #endif // !USE_ASH && USE_AURA
173 #if defined(USE_AURA)
174 ui::TerminateContextFactoryForTests();
177 ui::ShutdownInputMethodForTesting();
178 views::ViewsDelegate::views_delegate
= NULL
;
180 #if defined(USE_AURA)
185 bool ViewEventTestBase::CanResize() const {
189 views::View
* ViewEventTestBase::GetContentsView() {
190 if (!content_view_
) {
191 // Wrap the real view (as returned by CreateContentsView) in a View so
192 // that we can customize the preferred size.
193 TestView
* test_view
= new TestView();
194 test_view
->SetPreferredSize(GetPreferredSize());
195 test_view
->AddChildView(CreateContentsView());
196 content_view_
= test_view
;
198 return content_view_
;
201 const views::Widget
* ViewEventTestBase::GetWidget() const {
202 return content_view_
->GetWidget();
205 views::Widget
* ViewEventTestBase::GetWidget() {
206 return content_view_
->GetWidget();
209 ViewEventTestBase::~ViewEventTestBase() {
210 TestingBrowserProcess::DeleteInstance();
213 void ViewEventTestBase::StartMessageLoopAndRunTest() {
215 ui_test_utils::ShowAndFocusNativeWindow(window_
->GetNativeWindow()));
217 // Flush any pending events to make sure we start with a clean slate.
218 content::RunAllPendingInMessageLoop();
220 // Schedule a task that starts the test. Need to do this as we're going to
221 // run the message loop.
222 base::MessageLoop::current()->PostTask(
223 FROM_HERE
, base::Bind(&ViewEventTestBase::DoTestOnMessageLoop
, this));
225 content::RunMessageLoop();
228 gfx::Size
ViewEventTestBase::GetPreferredSize() {
232 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x
, int y
) {
233 if (!dnd_thread_
.get()) {
234 dnd_thread_
.reset(new base::Thread("mouse-move-thread"));
235 dnd_thread_
->Start();
237 dnd_thread_
->message_loop()->PostDelayedTask(
239 base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove
), x
, y
),
240 base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS
));
243 void ViewEventTestBase::StopBackgroundThread() {
244 dnd_thread_
.reset(NULL
);
247 void ViewEventTestBase::RunTestMethod(const base::Closure
& task
) {
248 StopBackgroundThread();
251 if (HasFatalFailure())