Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: ui/aura/test/event_generator.cc

Issue 322893005: MacViews: Add WidgetEventGenerator to abstract platform-specific event generation for tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase at r282511 Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/aura/test/event_generator.h" 5 #include "ui/aura/test/event_generator.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop_proxy.h" 9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/time/default_tick_clock.h" 10 #include "base/time/default_tick_clock.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 int flags, 73 int flags,
74 base::TimeDelta timestamp) 74 base::TimeDelta timestamp)
75 : TouchEvent(type, root_location, flags, touch_id, timestamp, 75 : TouchEvent(type, root_location, flags, touch_id, timestamp,
76 1.0f, 1.0f, 1.0f, 1.0f) { 76 1.0f, 1.0f, 1.0f, 1.0f) {
77 } 77 }
78 78
79 private: 79 private:
80 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent); 80 DISALLOW_COPY_AND_ASSIGN(TestTouchEvent);
81 }; 81 };
82 82
83 const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON;
84
85 } // namespace 83 } // namespace
86 84
87 EventGenerator::EventGenerator(Window* root_window) 85 EventGenerator::EventGenerator(Window* root_window)
88 : delegate_(new DefaultEventGeneratorDelegate(root_window)), 86 : EventGeneratorBase(gfx::Point()),
89 current_host_(delegate_->GetHostAt(current_location_)), 87 delegate_(new DefaultEventGeneratorDelegate(root_window)),
90 flags_(0), 88 current_host_(delegate_->GetHostAt(current_location())),
91 grab_(false),
92 async_(false), 89 async_(false),
93 tick_clock_(new base::DefaultTickClock()) { 90 tick_clock_(new base::DefaultTickClock()) {
94 } 91 }
95 92
96 EventGenerator::EventGenerator(Window* root_window, const gfx::Point& point) 93 EventGenerator::EventGenerator(Window* root_window, const gfx::Point& point)
97 : delegate_(new DefaultEventGeneratorDelegate(root_window)), 94 : EventGeneratorBase(point),
98 current_location_(point), 95 delegate_(new DefaultEventGeneratorDelegate(root_window)),
99 current_host_(delegate_->GetHostAt(current_location_)), 96 current_host_(delegate_->GetHostAt(current_location())),
100 flags_(0),
101 grab_(false),
102 async_(false), 97 async_(false),
103 tick_clock_(new base::DefaultTickClock()) { 98 tick_clock_(new base::DefaultTickClock()) {
104 } 99 }
105 100
106 EventGenerator::EventGenerator(Window* root_window, Window* window) 101 EventGenerator::EventGenerator(Window* root_window, Window* window)
107 : delegate_(new DefaultEventGeneratorDelegate(root_window)), 102 : EventGeneratorBase(gfx::Point()),
108 current_location_(CenterOfWindow(window)), 103 delegate_(new DefaultEventGeneratorDelegate(root_window)),
109 current_host_(delegate_->GetHostAt(current_location_)), 104 current_host_(NULL),
110 flags_(0), 105 async_(false),
111 grab_(false), 106 tick_clock_(new base::DefaultTickClock()) {
107 set_current_location(CenterOfWindow(window));
108 current_host_ = delegate_->GetHostAt(current_location());
109 }
110
111 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate)
112 : EventGeneratorBase(gfx::Point()),
113 delegate_(delegate),
114 current_host_(delegate_->GetHostAt(current_location())),
112 async_(false), 115 async_(false),
113 tick_clock_(new base::DefaultTickClock()) { 116 tick_clock_(new base::DefaultTickClock()) {
114 } 117 }
115
116 EventGenerator::EventGenerator(EventGeneratorDelegate* delegate)
117 : delegate_(delegate),
118 current_host_(delegate_->GetHostAt(current_location_)),
119 flags_(0),
120 grab_(false),
121 async_(false),
122 tick_clock_(new base::DefaultTickClock()) {
123 }
124 118
125 EventGenerator::~EventGenerator() { 119 EventGenerator::~EventGenerator() {
126 for (std::list<ui::Event*>::iterator i = pending_events_.begin(); 120 for (std::list<ui::Event*>::iterator i = pending_events_.begin();
127 i != pending_events_.end(); ++i) 121 i != pending_events_.end(); ++i)
128 delete *i; 122 delete *i;
129 pending_events_.clear(); 123 pending_events_.clear();
130 } 124 }
131 125
132 void EventGenerator::PressLeftButton() {
133 PressButton(ui::EF_LEFT_MOUSE_BUTTON);
134 }
135
136 void EventGenerator::ReleaseLeftButton() {
137 ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON);
138 }
139
140 void EventGenerator::ClickLeftButton() {
141 PressLeftButton();
142 ReleaseLeftButton();
143 }
144
145 void EventGenerator::DoubleClickLeftButton() {
146 flags_ |= ui::EF_IS_DOUBLE_CLICK;
147 PressLeftButton();
148 flags_ ^= ui::EF_IS_DOUBLE_CLICK;
149 ReleaseLeftButton();
150 }
151
152 void EventGenerator::PressRightButton() {
153 PressButton(ui::EF_RIGHT_MOUSE_BUTTON);
154 }
155
156 void EventGenerator::ReleaseRightButton() {
157 ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON);
158 }
159
160 void EventGenerator::MoveMouseWheel(int delta_x, int delta_y) { 126 void EventGenerator::MoveMouseWheel(int delta_x, int delta_y) {
161 gfx::Point location = GetLocationInCurrentRoot(); 127 gfx::Point location = GetLocationInCurrentRoot();
162 ui::MouseEvent mouseev(ui::ET_MOUSEWHEEL, location, location, flags_, 0); 128 ui::MouseEvent mouseev(ui::ET_MOUSEWHEEL, location, location, flags(), 0);
163 ui::MouseWheelEvent wheelev(mouseev, delta_x, delta_y); 129 ui::MouseWheelEvent wheelev(mouseev, delta_x, delta_y);
164 Dispatch(&wheelev); 130 Dispatch(&wheelev);
165 } 131 }
166 132
167 void EventGenerator::SendMouseExit() { 133 void EventGenerator::SendMouseExit() {
168 gfx::Point exit_location(current_location_); 134 gfx::Point exit_location(current_location());
169 ConvertPointToTarget(current_host_->window(), &exit_location); 135 ConvertPointToTarget(current_host_->window(), &exit_location);
170 ui::MouseEvent mouseev(ui::ET_MOUSE_EXITED, exit_location, exit_location, 136 ui::MouseEvent mouseev(ui::ET_MOUSE_EXITED, exit_location, exit_location,
171 flags_, 0); 137 flags(), 0);
172 Dispatch(&mouseev); 138 Dispatch(&mouseev);
173 } 139 }
174 140
175 void EventGenerator::MoveMouseToInHost(const gfx::Point& point_in_host) { 141 void EventGenerator::MoveMouseToInHost(const gfx::Point& point_in_host) {
176 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ? 142 const ui::EventType event_type = (flags() & ui::EF_LEFT_MOUSE_BUTTON) ?
177 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED; 143 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
178 ui::MouseEvent mouseev(event_type, point_in_host, point_in_host, flags_, 0); 144 ui::MouseEvent mouseev(event_type, point_in_host, point_in_host, flags(), 0);
179 Dispatch(&mouseev); 145 Dispatch(&mouseev);
180 146
181 current_location_ = point_in_host; 147 gfx::Point new_location(point_in_host);
182 current_host_->ConvertPointFromHost(&current_location_); 148 current_host_->ConvertPointFromHost(&new_location);
183 } 149 set_current_location(new_location);
184
185 void EventGenerator::MoveMouseTo(const gfx::Point& point_in_screen,
186 int count) {
187 DCHECK_GT(count, 0);
188 const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ?
189 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
190
191 gfx::Vector2dF diff(point_in_screen - current_location_);
192 for (float i = 1; i <= count; i++) {
193 gfx::Vector2dF step(diff);
194 step.Scale(i / count);
195 gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step);
196 if (!grab_)
197 UpdateCurrentDispatcher(move_point);
198 ConvertPointToTarget(current_host_->window(), &move_point);
199 ui::MouseEvent mouseev(event_type, move_point, move_point, flags_, 0);
200 Dispatch(&mouseev);
201 }
202 current_location_ = point_in_screen;
203 } 150 }
204 151
205 void EventGenerator::MoveMouseRelativeTo(const Window* window, 152 void EventGenerator::MoveMouseRelativeTo(const Window* window,
206 const gfx::Point& point_in_parent) { 153 const gfx::Point& point_in_parent) {
207 gfx::Point point(point_in_parent); 154 gfx::Point point(point_in_parent);
208 ConvertPointFromTarget(window, &point); 155 ConvertPointFromTarget(window, &point);
209 MoveMouseTo(point); 156 MoveMouseTo(point);
210 } 157 }
211 158
212 void EventGenerator::DragMouseTo(const gfx::Point& point) {
213 PressLeftButton();
214 MoveMouseTo(point);
215 ReleaseLeftButton();
216 }
217
218 void EventGenerator::MoveMouseToCenterOf(Window* window) { 159 void EventGenerator::MoveMouseToCenterOf(Window* window) {
219 MoveMouseTo(CenterOfWindow(window)); 160 MoveMouseTo(CenterOfWindow(window));
220 } 161 }
221 162
222 void EventGenerator::PressTouch() { 163 void EventGenerator::PressTouch() {
223 PressTouchId(0); 164 PressTouchId(0);
224 } 165 }
225 166
226 void EventGenerator::PressTouchId(int touch_id) { 167 void EventGenerator::PressTouchId(int touch_id) {
227 TestTouchEvent touchev( 168 TestTouchEvent touchev(
228 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags_, 169 ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), touch_id, flags(),
229 Now()); 170 Now());
230 Dispatch(&touchev); 171 Dispatch(&touchev);
231 } 172 }
232 173
233 void EventGenerator::MoveTouch(const gfx::Point& point) { 174 void EventGenerator::MoveTouch(const gfx::Point& point) {
234 MoveTouchId(point, 0); 175 MoveTouchId(point, 0);
235 } 176 }
236 177
237 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) { 178 void EventGenerator::MoveTouchId(const gfx::Point& point, int touch_id) {
238 current_location_ = point; 179 set_current_location(point);
239 TestTouchEvent touchev( 180 TestTouchEvent touchev(
240 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags_, 181 ui::ET_TOUCH_MOVED, GetLocationInCurrentRoot(), touch_id, flags(),
241 Now()); 182 Now());
242 Dispatch(&touchev); 183 Dispatch(&touchev);
243 184
244 if (!grab_) 185 if (!grab())
245 UpdateCurrentDispatcher(point); 186 UpdateCurrentDispatcher(point);
246 } 187 }
247 188
248 void EventGenerator::ReleaseTouch() { 189 void EventGenerator::ReleaseTouch() {
249 ReleaseTouchId(0); 190 ReleaseTouchId(0);
250 } 191 }
251 192
252 void EventGenerator::ReleaseTouchId(int touch_id) { 193 void EventGenerator::ReleaseTouchId(int touch_id) {
253 TestTouchEvent touchev( 194 TestTouchEvent touchev(
254 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags_, 195 ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), touch_id, flags(),
255 Now()); 196 Now());
256 Dispatch(&touchev); 197 Dispatch(&touchev);
257 } 198 }
258 199
259 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) { 200 void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) {
260 PressTouch(); 201 PressTouch();
261 MoveTouch(point); 202 MoveTouch(point);
262 ReleaseTouch(); 203 ReleaseTouch();
263 } 204 }
264 205
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 tick_clock_ = tick_clock.Pass(); 454 tick_clock_ = tick_clock.Pass();
514 } 455 }
515 456
516 base::TimeDelta EventGenerator::Now() { 457 base::TimeDelta EventGenerator::Now() {
517 // This is the same as what EventTimeForNow() does, but here we do it 458 // This is the same as what EventTimeForNow() does, but here we do it
518 // with a tick clock that can be replaced with a simulated clock for tests. 459 // with a tick clock that can be replaced with a simulated clock for tests.
519 return base::TimeDelta::FromInternalValue( 460 return base::TimeDelta::FromInternalValue(
520 tick_clock_->NowTicks().ToInternalValue()); 461 tick_clock_->NowTicks().ToInternalValue());
521 } 462 }
522 463
464 gfx::Point EventGenerator::GetLocationInCurrentRoot() const {
465 gfx::Point p(current_location());
466 ConvertPointToTarget(current_host_->window(), &p);
467 return p;
468 }
469
470 void EventGenerator::DoMoveMouseTo(const gfx::Point& point_in_screen,
471 int count) {
472 DCHECK_GT(count, 0);
473 const ui::EventType event_type = (flags() & ui::EF_LEFT_MOUSE_BUTTON) ?
474 ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
475
476 gfx::Vector2dF diff(point_in_screen - current_location());
477 for (float i = 1; i <= count; i++) {
478 gfx::Vector2dF step(diff);
479 step.Scale(i / count);
480 gfx::Point move_point = current_location() + gfx::ToRoundedVector2d(step);
481 if (!grab())
482 UpdateCurrentDispatcher(move_point);
483 ConvertPointToTarget(current_host_->window(), &move_point);
484 ui::MouseEvent mouseev(event_type, move_point, move_point, flags(), 0);
485 Dispatch(&mouseev);
486 }
487 set_current_location(point_in_screen);
488 }
489
490 void EventGenerator::DispatchMouseEvent(ui::EventType type,
491 const gfx::Point& location_in_root,
492 int flags,
493 int changed_button_flags) {
494 ui::MouseEvent mouseev(type,
495 location_in_root,
496 location_in_root,
497 flags,
498 changed_button_flags);
499 Dispatch(&mouseev);
500 }
501
523 void EventGenerator::DispatchKeyEvent(bool is_press, 502 void EventGenerator::DispatchKeyEvent(bool is_press,
524 ui::KeyboardCode key_code, 503 ui::KeyboardCode key_code,
525 int flags) { 504 int flags) {
526 #if defined(OS_WIN) 505 #if defined(OS_WIN)
527 UINT key_press = WM_KEYDOWN; 506 UINT key_press = WM_KEYDOWN;
528 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags); 507 uint16 character = ui::GetCharacterFromKeyCode(key_code, flags);
529 if (is_press && character) { 508 if (is_press && character) {
530 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; 509 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 };
531 TestKeyEvent keyev(native_event, flags, false); 510 TestKeyEvent keyev(native_event, flags, false);
532 Dispatch(&keyev); 511 Dispatch(&keyev);
(...skipping 15 matching lines...) Expand all
548 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; 527 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
549 ui::KeyEvent keyev(type, key_code, flags, false); 528 ui::KeyEvent keyev(type, key_code, flags, false);
550 #endif // OS_WIN 529 #endif // OS_WIN
551 Dispatch(&keyev); 530 Dispatch(&keyev);
552 } 531 }
553 532
554 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { 533 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) {
555 current_host_ = delegate_->GetHostAt(point); 534 current_host_ = delegate_->GetHostAt(point);
556 } 535 }
557 536
558 void EventGenerator::PressButton(int flag) {
559 if (!(flags_ & flag)) {
560 flags_ |= flag;
561 grab_ = flags_ & kAllButtonMask;
562 gfx::Point location = GetLocationInCurrentRoot();
563 ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_,
564 flag);
565 Dispatch(&mouseev);
566 }
567 }
568
569 void EventGenerator::ReleaseButton(int flag) {
570 if (flags_ & flag) {
571 gfx::Point location = GetLocationInCurrentRoot();
572 ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location,
573 location, flags_, flag);
574 Dispatch(&mouseev);
575 flags_ ^= flag;
576 }
577 grab_ = flags_ & kAllButtonMask;
578 }
579
580 void EventGenerator::ConvertPointFromTarget(const aura::Window* target, 537 void EventGenerator::ConvertPointFromTarget(const aura::Window* target,
581 gfx::Point* point) const { 538 gfx::Point* point) const {
582 DCHECK(point); 539 DCHECK(point);
583 aura::client::ScreenPositionClient* client = 540 aura::client::ScreenPositionClient* client =
584 delegate_->GetScreenPositionClient(target); 541 delegate_->GetScreenPositionClient(target);
585 if (client) 542 if (client)
586 client->ConvertPointToScreen(target, point); 543 client->ConvertPointToScreen(target, point);
587 else 544 else
588 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point); 545 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point);
589 } 546 }
590 547
591 void EventGenerator::ConvertPointToTarget(const aura::Window* target, 548 void EventGenerator::ConvertPointToTarget(const aura::Window* target,
592 gfx::Point* point) const { 549 gfx::Point* point) const {
593 DCHECK(point); 550 DCHECK(point);
594 aura::client::ScreenPositionClient* client = 551 aura::client::ScreenPositionClient* client =
595 delegate_->GetScreenPositionClient(target); 552 delegate_->GetScreenPositionClient(target);
596 if (client) 553 if (client)
597 client->ConvertPointFromScreen(target, point); 554 client->ConvertPointFromScreen(target, point);
598 else 555 else
599 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point); 556 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point);
600 } 557 }
601 558
602 gfx::Point EventGenerator::GetLocationInCurrentRoot() const {
603 gfx::Point p(current_location_);
604 ConvertPointToTarget(current_host_->window(), &p);
605 return p;
606 }
607
608 gfx::Point EventGenerator::CenterOfWindow(const Window* window) const { 559 gfx::Point EventGenerator::CenterOfWindow(const Window* window) const {
609 gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint(); 560 gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint();
610 ConvertPointFromTarget(window, &center); 561 ConvertPointFromTarget(window, &center);
611 return center; 562 return center;
612 } 563 }
613 564
614 void EventGenerator::DoDispatchEvent(ui::Event* event, bool async) { 565 void EventGenerator::DoDispatchEvent(ui::Event* event, bool async) {
615 if (async) { 566 if (async) {
616 ui::Event* pending_event; 567 ui::Event* pending_event;
617 if (event->IsKeyEvent()) { 568 if (event->IsKeyEvent()) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 if (!pending_events_.empty()) { 603 if (!pending_events_.empty()) {
653 base::MessageLoopProxy::current()->PostTask( 604 base::MessageLoopProxy::current()->PostTask(
654 FROM_HERE, 605 FROM_HERE,
655 base::Bind(&EventGenerator::DispatchNextPendingEvent, 606 base::Bind(&EventGenerator::DispatchNextPendingEvent,
656 base::Unretained(this))); 607 base::Unretained(this)));
657 } 608 }
658 } 609 }
659 610
660 } // namespace test 611 } // namespace test
661 } // namespace aura 612 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698