Make hitting "Enter" submit the add/change profile dialog.
[chromium-blink-merge.git] / chrome / test / base / view_event_test_base.cc
blob19216d58499379faf1b83589c5c4d3c12fe57f4c
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop.h"
10 #include "base/string_number_conversions.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ui/base/ime/text_input_test_support.h"
14 #include "ui/compositor/test/compositor_test_support.h"
15 #include "ui/ui_controls/ui_controls.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/desktop_aura/desktop_screen.h"
18 #include "ui/views/widget/widget.h"
20 #if defined(USE_ASH)
21 #include "ash/shell.h"
22 #include "ash/test/test_shell_delegate.h"
23 #endif
25 #if defined(USE_AURA)
26 #include "ui/aura/client/event_client.h"
27 #include "ui/aura/env.h"
28 #include "ui/aura/root_window.h"
29 #include "ui/aura/test/aura_test_helper.h"
30 #endif
32 namespace {
34 // View subclass that allows you to specify the preferred size.
35 class TestView : public views::View {
36 public:
37 TestView() {}
39 void SetPreferredSize(const gfx::Size& size) {
40 preferred_size_ = size;
41 PreferredSizeChanged();
44 gfx::Size GetPreferredSize() {
45 if (!preferred_size_.IsEmpty())
46 return preferred_size_;
47 return View::GetPreferredSize();
50 virtual void Layout() {
51 View* child_view = child_at(0);
52 child_view->SetBounds(0, 0, width(), height());
55 private:
56 gfx::Size preferred_size_;
58 DISALLOW_COPY_AND_ASSIGN(TestView);
61 // Delay in background thread before posting mouse move.
62 const int kMouseMoveDelayMS = 200;
64 } // namespace
66 ViewEventTestBase::ViewEventTestBase()
67 : window_(NULL),
68 content_view_(NULL),
69 ui_thread_(content::BrowserThread::UI, &message_loop_) {
72 void ViewEventTestBase::Done() {
73 MessageLoop::current()->Quit();
75 #if defined(OS_WIN) && !defined(USE_AURA)
76 // We need to post a message to tickle the Dispatcher getting called and
77 // exiting out of the nested loop. Without this the quit never runs.
78 if (window_)
79 PostMessage(window_->GetNativeWindow(), WM_USER, 0, 0);
80 #endif
82 // If we're in a nested message loop, as is the case with menus, we
83 // need to quit twice. The second quit does that for us. Finish all
84 // pending UI events before posting closure because events it may be
85 // executed before UI events are executed.
86 ui_controls::RunClosureAfterAllPendingUIEvents(MessageLoop::QuitClosure());
89 void ViewEventTestBase::SetUp() {
90 ui::TextInputTestSupport::Initialize();
91 ui::CompositorTestSupport::Initialize();
92 gfx::NativeView context = NULL;
93 #if defined(USE_ASH)
94 #if defined(OS_WIN)
95 // http://crbug.com/154081 use ash::Shell code path below on win_ash bots when
96 // interactive_ui_tests is brought up on that platform.
97 gfx::Screen::SetScreenInstance(
98 gfx::SCREEN_TYPE_NATIVE, views::CreateDesktopScreen());
99 #else
100 ash::Shell::CreateInstance(new ash::test::TestShellDelegate());
101 context = ash::Shell::GetPrimaryRootWindow();
102 #endif
103 #elif defined(USE_AURA)
104 // Instead of using the ash shell, use an AuraTestHelper to create and manage
105 // the test screen.
106 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
107 aura_test_helper_->SetUp();
108 context = aura_test_helper_->root_window();
109 #endif
111 window_ = views::Widget::CreateWindowWithContext(this, context);
114 void ViewEventTestBase::TearDown() {
115 if (window_) {
116 #if defined(OS_WIN) && !defined(USE_AURA)
117 DestroyWindow(window_->GetNativeWindow());
118 #else
119 window_->Close();
120 content::RunAllPendingInMessageLoop();
121 #endif
122 window_ = NULL;
124 #if defined(USE_ASH)
125 #if defined(OS_WIN)
126 #else
127 ash::Shell::DeleteInstance();
128 aura::Env::DeleteInstance();
129 #endif
130 #elif defined(USE_AURA)
131 aura_test_helper_->TearDown();
132 #endif
133 ui::CompositorTestSupport::Terminate();
134 ui::TextInputTestSupport::Shutdown();
137 bool ViewEventTestBase::CanResize() const {
138 return true;
141 views::View* ViewEventTestBase::GetContentsView() {
142 if (!content_view_) {
143 // Wrap the real view (as returned by CreateContentsView) in a View so
144 // that we can customize the preferred size.
145 TestView* test_view = new TestView();
146 test_view->SetPreferredSize(GetPreferredSize());
147 test_view->AddChildView(CreateContentsView());
148 content_view_ = test_view;
150 return content_view_;
153 const views::Widget* ViewEventTestBase::GetWidget() const {
154 return content_view_->GetWidget();
157 views::Widget* ViewEventTestBase::GetWidget() {
158 return content_view_->GetWidget();
161 ViewEventTestBase::~ViewEventTestBase() {
164 void ViewEventTestBase::StartMessageLoopAndRunTest() {
165 window_->Show();
166 // Make sure the window is the foreground window, otherwise none of the
167 // mouse events are going to be targeted correctly.
168 #if defined(OS_WIN)
169 #if defined(USE_AURA)
170 HWND window =
171 window_->GetNativeWindow()->GetRootWindow()->GetAcceleratedWidget();
172 #else
173 HWND window = window_->GetNativeWindow();
174 #endif
175 SetForegroundWindow(window);
176 #endif
178 // Flush any pending events to make sure we start with a clean slate.
179 content::RunAllPendingInMessageLoop();
181 // Schedule a task that starts the test. Need to do this as we're going to
182 // run the message loop.
183 MessageLoop::current()->PostTask(
184 FROM_HERE,
185 base::Bind(&ViewEventTestBase::DoTestOnMessageLoop, this));
187 content::RunMessageLoop();
190 gfx::Size ViewEventTestBase::GetPreferredSize() {
191 return gfx::Size();
194 void ViewEventTestBase::ScheduleMouseMoveInBackground(int x, int y) {
195 if (!dnd_thread_.get()) {
196 dnd_thread_.reset(new base::Thread("mouse-move-thread"));
197 dnd_thread_->Start();
199 dnd_thread_->message_loop()->PostDelayedTask(
200 FROM_HERE,
201 base::Bind(base::IgnoreResult(&ui_controls::SendMouseMove), x, y),
202 base::TimeDelta::FromMilliseconds(kMouseMoveDelayMS));
205 void ViewEventTestBase::StopBackgroundThread() {
206 dnd_thread_.reset(NULL);
209 void ViewEventTestBase::RunTestMethod(const base::Closure& task) {
210 StopBackgroundThread();
212 task.Run();
213 if (HasFatalFailure())
214 Done();