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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.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/view_event_test_platform_part.h"
15 #include "ui/base/ime/input_method_initializer.h"
16 #include "ui/base/test/ui_controls.h"
17 #include "ui/compositor/test/context_factories_for_test.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/widget.h"
23 // View subclass that allows you to specify the preferred size.
24 class TestView
: public views::View
{
26 explicit TestView(ViewEventTestBase
* harness
) : harness_(harness
) {}
28 gfx::Size
GetPreferredSize() const override
{
29 return harness_
->GetPreferredSize();
32 void Layout() override
{
33 View
* child_view
= child_at(0);
34 child_view
->SetBounds(0, 0, width(), height());
38 ViewEventTestBase
* harness_
;
40 DISALLOW_COPY_AND_ASSIGN(TestView
);
43 // Delay in background thread before posting mouse move.
44 const int kMouseMoveDelayMS
= 200;
48 ViewEventTestBase::ViewEventTestBase()
51 // The TestingBrowserProcess must be created in the constructor because there
52 // are tests that require it before SetUp() is called.
53 TestingBrowserProcess::CreateInstance();
56 void ViewEventTestBase::Done() {
60 void ViewEventTestBase::SetUpTestCase() {
61 ChromeUnitTestSuite::InitializeProviders();
62 ChromeUnitTestSuite::InitializeResourceBundle();
65 void ViewEventTestBase::SetUp() {
66 ui::InitializeInputMethodForTesting();
68 // The ContextFactory must exist before any Compositors are created.
69 bool enable_pixel_output
= false;
70 ui::ContextFactory
* context_factory
=
71 ui::InitializeContextFactoryForTests(enable_pixel_output
);
72 views_delegate_
.set_context_factory(context_factory
);
73 views_delegate_
.set_use_desktop_native_widgets(true);
75 platform_part_
.reset(ViewEventTestPlatformPart::Create(context_factory
));
76 gfx::NativeWindow context
= platform_part_
->GetContext();
77 window_
= views::Widget::CreateWindowWithContext(this, context
);
81 void ViewEventTestBase::TearDown() {
84 content::RunAllPendingInMessageLoop();
88 ui::Clipboard::DestroyClipboardForCurrentThread();
89 platform_part_
.reset();
91 ui::TerminateContextFactoryForTests();
93 ui::ShutdownInputMethodForTesting();
96 gfx::Size
ViewEventTestBase::GetPreferredSize() const {
100 bool ViewEventTestBase::CanResize() const {
104 views::View
* ViewEventTestBase::GetContentsView() {
105 if (!content_view_
) {
106 // Wrap the real view (as returned by CreateContentsView) in a View so
107 // that we can customize the preferred size.
108 TestView
* test_view
= new TestView(this);
109 test_view
->AddChildView(CreateContentsView());
110 content_view_
= test_view
;
112 return content_view_
;
115 const views::Widget
* ViewEventTestBase::GetWidget() const {
116 return content_view_
->GetWidget();
119 views::Widget
* ViewEventTestBase::GetWidget() {
120 return content_view_
->GetWidget();
123 ViewEventTestBase::~ViewEventTestBase() {
124 TestingBrowserProcess::DeleteInstance();
127 void ViewEventTestBase::StartMessageLoopAndRunTest() {
129 ui_test_utils::ShowAndFocusNativeWindow(window_
->GetNativeWindow()));
131 // Flush any pending events to make sure we start with a clean slate.
132 content::RunAllPendingInMessageLoop();
134 // Schedule a task that starts the test. Need to do this as we're going to
135 // run the message loop.
136 base::ThreadTaskRunnerHandle::Get()->PostTask(
137 FROM_HERE
, base::Bind(&ViewEventTestBase::DoTestOnMessageLoop
, this));
139 content::RunThisRunLoop(&run_loop_
);
142 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x
, int y
) {
143 if (!dnd_thread_
.get()) {
144 dnd_thread_
.reset(new base::Thread("mouse-move-thread"));
145 dnd_thread_
->Start();
147 dnd_thread_
->task_runner()->PostDelayedTask(
149 base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove
), x
, y
),
150 base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS
));
153 void ViewEventTestBase::StopBackgroundThread() {
154 dnd_thread_
.reset(NULL
);
157 void ViewEventTestBase::RunTestMethod(const base::Closure
& task
) {
158 StopBackgroundThread();
161 if (HasFatalFailure())