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/chrome_unit_test_suite.h"
12 #include "chrome/test/base/interactive_test_utils.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "ui/aura/client/event_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/test/aura_test_helper.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/aura/window_tree_host.h"
20 #include "ui/base/ime/input_method_initializer.h"
21 #include "ui/base/test/ui_controls.h"
22 #include "ui/compositor/test/context_factories_for_test.h"
23 #include "ui/compositor/test/context_factories_for_test.h"
24 #include "ui/message_center/message_center.h"
25 #include "ui/views/view.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/wm/core/default_activation_client.h"
28 #include "ui/wm/core/wm_state.h"
31 #include "ash/shell.h"
32 #include "ash/test/test_session_state_delegate.h"
33 #include "ash/test/test_shell_delegate.h"
36 #if defined(OS_CHROMEOS)
37 #include "chromeos/audio/cras_audio_handler.h"
38 #include "chromeos/dbus/dbus_thread_manager.h"
39 #include "chromeos/network/network_handler.h"
40 #else // !defined(OS_CHROMEOS)
41 #include "ui/views/widget/desktop_aura/desktop_screen.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::SetUpTestCase() {
98 ChromeUnitTestSuite::InitializeProviders();
99 ChromeUnitTestSuite::InitializeResourceBundle();
102 void ViewEventTestBase::SetUp() {
103 wm_state_.reset(new wm::WMState);
105 views::ViewsDelegate::views_delegate = &views_delegate_;
106 ui::InitializeInputMethodForTesting();
107 gfx::NativeView context = NULL;
109 // The ContextFactory must exist before any Compositors are created.
110 bool enable_pixel_output = false;
111 ui::InitializeContextFactoryForTests(enable_pixel_output);
115 // http://crbug.com/154081 use ash::Shell code path below on win_ash bots when
116 // interactive_ui_tests is brought up on that platform.
117 gfx::Screen::SetScreenInstance(
118 gfx::SCREEN_TYPE_NATIVE, views::CreateDesktopScreen());
121 // Ash Shell can't just live on its own without a browser process, we need to
122 // also create the message center.
123 message_center::MessageCenter::Initialize();
124 #if defined(OS_CHROMEOS)
125 chromeos::DBusThreadManager::InitializeWithStub();
126 chromeos::CrasAudioHandler::InitializeForTesting();
127 chromeos::NetworkHandler::Initialize();
128 #endif // OS_CHROMEOS
129 ash::test::TestShellDelegate* shell_delegate =
130 new ash::test::TestShellDelegate();
131 ash::Shell::CreateInstance(shell_delegate);
132 shell_delegate->test_session_state_delegate()
133 ->SetActiveUserSessionStarted(true);
134 context = ash::Shell::GetPrimaryRootWindow();
135 context->GetHost()->Show();
137 aura::Env::CreateInstance(true);
138 #elif defined(USE_AURA)
139 // Instead of using the ash shell, use an AuraTestHelper to create and manage
141 aura_test_helper_.reset(
142 new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
143 aura_test_helper_->SetUp();
144 new wm::DefaultActivationClient(aura_test_helper_->root_window());
145 context = aura_test_helper_->root_window();
146 #endif // !USE_ASH && USE_AURA
148 window_ = views::Widget::CreateWindowWithContext(this, context);
151 void ViewEventTestBase::TearDown() {
154 content::RunAllPendingInMessageLoop();
158 ui::Clipboard::DestroyClipboardForCurrentThread();
162 ash::Shell::DeleteInstance();
163 #if defined(OS_CHROMEOS)
164 chromeos::NetworkHandler::Shutdown();
165 chromeos::CrasAudioHandler::Shutdown();
166 chromeos::DBusThreadManager::Shutdown();
168 // Ash Shell can't just live on its own without a browser process, we need to
169 // also shut down the message center.
170 message_center::MessageCenter::Shutdown();
172 aura::Env::DeleteInstance();
173 #elif defined(USE_AURA)
174 aura_test_helper_->TearDown();
175 #endif // !USE_ASH && USE_AURA
177 ui::TerminateContextFactoryForTests();
179 ui::ShutdownInputMethodForTesting();
180 views::ViewsDelegate::views_delegate = NULL;
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())