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

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

Issue 1975533002: Change ui::Event::time_stamp from TimeDelta to TimeTicks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gesture recognizer tests Created 4 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/events/test/event_generator.h" 5 #include "ui/events/test/event_generator.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 set_flags(flags); 98 set_flags(flags);
99 } 99 }
100 }; 100 };
101 101
102 class TestTouchEvent : public ui::TouchEvent { 102 class TestTouchEvent : public ui::TouchEvent {
103 public: 103 public:
104 TestTouchEvent(ui::EventType type, 104 TestTouchEvent(ui::EventType type,
105 const gfx::Point& root_location, 105 const gfx::Point& root_location,
106 int touch_id, 106 int touch_id,
107 int flags, 107 int flags,
108 base::TimeDelta timestamp) 108 base::TimeTicks timestamp)
109 : TouchEvent(type, 109 : TouchEvent(type,
110 root_location, 110 root_location,
111 flags, 111 flags,
112 touch_id, 112 touch_id,
113 timestamp, 113 timestamp,
114 1.0f, 114 1.0f,
115 1.0f, 115 1.0f,
116 0.0f, 116 0.0f,
117 0.0f) {} 117 0.0f) {}
118 118
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 ui::EventTimeForNow(), flags_, 0); 215 ui::EventTimeForNow(), flags_, 0);
216 Dispatch(&mouseev); 216 Dispatch(&mouseev);
217 } 217 }
218 218
219 void EventGenerator::MoveMouseToWithNative(const gfx::Point& point_in_host, 219 void EventGenerator::MoveMouseToWithNative(const gfx::Point& point_in_host,
220 const gfx::Point& point_for_native) { 220 const gfx::Point& point_for_native) {
221 #if defined(USE_X11) 221 #if defined(USE_X11)
222 ui::ScopedXI2Event xevent; 222 ui::ScopedXI2Event xevent;
223 xevent.InitMotionEvent(point_in_host, point_for_native, flags_); 223 xevent.InitMotionEvent(point_in_host, point_for_native, flags_);
224 static_cast<XEvent*>(xevent)->xmotion.time = 224 static_cast<XEvent*>(xevent)->xmotion.time =
225 Now().InMilliseconds() & UINT32_MAX; 225 (Now() - base::TimeTicks()).InMilliseconds() & UINT32_MAX;
226 ui::MouseEvent mouseev(xevent); 226 ui::MouseEvent mouseev(xevent);
227 #elif defined(USE_OZONE) 227 #elif defined(USE_OZONE)
228 // Ozone uses the location in native event as a system location. 228 // Ozone uses the location in native event as a system location.
229 // Create a fake event with the point in host, which will be passed 229 // Create a fake event with the point in host, which will be passed
230 // to the non native event, then update the native event with the native 230 // to the non native event, then update the native event with the native
231 // (root) one. 231 // (root) one.
232 std::unique_ptr<ui::MouseEvent> native_event(new ui::MouseEvent( 232 std::unique_ptr<ui::MouseEvent> native_event(new ui::MouseEvent(
233 ui::ET_MOUSE_MOVED, point_in_host, point_in_host, Now(), flags_, 0)); 233 ui::ET_MOUSE_MOVED, point_in_host, point_in_host, Now(), flags_, 0));
234 ui::MouseEvent mouseev(native_event.get()); 234 ui::MouseEvent mouseev(native_event.get());
235 native_event->set_location(point_for_native); 235 native_event->set_location(point_for_native);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 base::Bind(&DummyCallback)); 393 base::Bind(&DummyCallback));
394 } 394 }
395 395
396 void EventGenerator::GestureScrollSequenceWithCallback( 396 void EventGenerator::GestureScrollSequenceWithCallback(
397 const gfx::Point& start, 397 const gfx::Point& start,
398 const gfx::Point& end, 398 const gfx::Point& end,
399 const base::TimeDelta& step_delay, 399 const base::TimeDelta& step_delay,
400 int steps, 400 int steps,
401 const ScrollStepCallback& callback) { 401 const ScrollStepCallback& callback) {
402 const int kTouchId = 5; 402 const int kTouchId = 5;
403 base::TimeDelta timestamp = Now(); 403 base::TimeTicks timestamp = Now();
404 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, 0, kTouchId, 404 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, 0, kTouchId,
405 timestamp, 5.0f, 5.0f, 0.0f, 1.0f); 405 timestamp, 5.0f, 5.0f, 0.0f, 1.0f);
406 Dispatch(&press); 406 Dispatch(&press);
407 407
408 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF()); 408 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF());
409 409
410 float dx = static_cast<float>(end.x() - start.x()) / steps; 410 float dx = static_cast<float>(end.x() - start.x()) / steps;
411 float dy = static_cast<float>(end.y() - start.y()) / steps; 411 float dy = static_cast<float>(end.y() - start.y()) / steps;
412 gfx::PointF location(start); 412 gfx::PointF location(start);
413 for (int i = 0; i < steps; ++i) { 413 for (int i = 0; i < steps; ++i) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 CHECK_LE(count, kMaxTouchPoints); 453 CHECK_LE(count, kMaxTouchPoints);
454 CHECK_GT(steps, 0); 454 CHECK_GT(steps, 0);
455 455
456 int delta_x = move_x / steps; 456 int delta_x = move_x / steps;
457 int delta_y = move_y / steps; 457 int delta_y = move_y / steps;
458 458
459 for (int i = 0; i < count; ++i) { 459 for (int i = 0; i < count; ++i) {
460 points[i] = start[i]; 460 points[i] = start[i];
461 } 461 }
462 462
463 base::TimeDelta press_time_first = Now(); 463 base::TimeTicks press_time_first = Now();
464 base::TimeDelta press_time[kMaxTouchPoints]; 464 base::TimeTicks press_time[kMaxTouchPoints];
465 bool pressed[kMaxTouchPoints]; 465 bool pressed[kMaxTouchPoints];
466 for (int i = 0; i < count; ++i) { 466 for (int i = 0; i < count; ++i) {
467 pressed[i] = false; 467 pressed[i] = false;
468 press_time[i] = press_time_first + 468 press_time[i] = press_time_first +
469 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]); 469 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]);
470 } 470 }
471 471
472 int last_id = 0; 472 int last_id = 0;
473 for (int step = 0; step < steps; ++step) { 473 for (int step = 0; step < steps; ++step) {
474 base::TimeDelta move_time = press_time_first + 474 base::TimeTicks move_time =
475 press_time_first +
475 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step); 476 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step);
476 477
477 while (last_id < count && 478 while (last_id < count &&
478 !pressed[last_id] && 479 !pressed[last_id] &&
479 move_time >= press_time[last_id]) { 480 move_time >= press_time[last_id]) {
480 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, 481 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
481 points[last_id], 482 points[last_id],
482 last_id, 483 last_id,
483 press_time[last_id]); 484 press_time[last_id]);
484 Dispatch(&press); 485 Dispatch(&press);
485 pressed[last_id] = true; 486 pressed[last_id] = true;
486 last_id++; 487 last_id++;
487 } 488 }
488 489
489 for (int i = 0; i < count; ++i) { 490 for (int i = 0; i < count; ++i) {
490 points[i].Offset(delta_x, delta_y); 491 points[i].Offset(delta_x, delta_y);
491 if (i >= last_id) 492 if (i >= last_id)
492 continue; 493 continue;
493 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time); 494 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time);
494 Dispatch(&move); 495 Dispatch(&move);
495 } 496 }
496 } 497 }
497 498
498 base::TimeDelta release_time = press_time_first + 499 base::TimeTicks release_time =
500 press_time_first +
499 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps); 501 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps);
500 for (int i = 0; i < last_id; ++i) { 502 for (int i = 0; i < last_id; ++i) {
501 ui::TouchEvent release( 503 ui::TouchEvent release(
502 ui::ET_TOUCH_RELEASED, points[i], i, release_time); 504 ui::ET_TOUCH_RELEASED, points[i], i, release_time);
503 Dispatch(&release); 505 Dispatch(&release);
504 } 506 }
505 } 507 }
506 508
507 void EventGenerator::ScrollSequence(const gfx::Point& start, 509 void EventGenerator::ScrollSequence(const gfx::Point& start,
508 const base::TimeDelta& step_delay, 510 const base::TimeDelta& step_delay,
509 float x_offset, 511 float x_offset,
510 float y_offset, 512 float y_offset,
511 int steps, 513 int steps,
512 int num_fingers) { 514 int num_fingers) {
513 base::TimeDelta timestamp = Now(); 515 base::TimeTicks timestamp = Now();
514 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, 516 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
515 start, 517 start,
516 timestamp, 518 timestamp,
517 0, 519 0,
518 0, 0, 520 0, 0,
519 0, 0, 521 0, 0,
520 num_fingers); 522 num_fingers);
521 Dispatch(&fling_cancel); 523 Dispatch(&fling_cancel);
522 524
523 float dx = x_offset / steps; 525 float dx = x_offset / steps;
(...skipping 18 matching lines...) Expand all
542 x_offset, y_offset, 544 x_offset, y_offset,
543 num_fingers); 545 num_fingers);
544 Dispatch(&fling_start); 546 Dispatch(&fling_start);
545 } 547 }
546 548
547 void EventGenerator::ScrollSequence(const gfx::Point& start, 549 void EventGenerator::ScrollSequence(const gfx::Point& start,
548 const base::TimeDelta& step_delay, 550 const base::TimeDelta& step_delay,
549 const std::vector<gfx::PointF>& offsets, 551 const std::vector<gfx::PointF>& offsets,
550 int num_fingers) { 552 int num_fingers) {
551 size_t steps = offsets.size(); 553 size_t steps = offsets.size();
552 base::TimeDelta timestamp = Now(); 554 base::TimeTicks timestamp = Now();
553 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, 555 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
554 start, 556 start,
555 timestamp, 557 timestamp,
556 0, 558 0,
557 0, 0, 559 0, 0,
558 0, 0, 560 0, 0,
559 num_fingers); 561 num_fingers);
560 Dispatch(&fling_cancel); 562 Dispatch(&fling_cancel);
561 563
562 for (size_t i = 0; i < steps; ++i) { 564 for (size_t i = 0; i < steps; ++i) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 597
596 void EventGenerator::SetTickClock(std::unique_ptr<base::TickClock> tick_clock) { 598 void EventGenerator::SetTickClock(std::unique_ptr<base::TickClock> tick_clock) {
597 scoped_refptr<ClonableTickClock> clonable = 599 scoped_refptr<ClonableTickClock> clonable =
598 new ClonableTickClock(std::move(tick_clock)); 600 new ClonableTickClock(std::move(tick_clock));
599 #if defined(USE_X11) 601 #if defined(USE_X11)
600 ResetTimestampRolloverCountersForTesting(clonable->Clone()); 602 ResetTimestampRolloverCountersForTesting(clonable->Clone());
601 #endif 603 #endif
602 tick_clock_ = clonable->Clone(); 604 tick_clock_ = clonable->Clone();
603 } 605 }
604 606
605 base::TimeDelta EventGenerator::Now() { 607 base::TimeTicks EventGenerator::Now() {
606 // This is the same as what EventTimeForNow() does, but here we do it 608 // This is the same as what EventTimeForNow() does, but here we do it
607 // with a tick clock that can be replaced with a simulated clock for tests. 609 // with a tick clock that can be replaced with a simulated clock for tests.
608 return base::TimeDelta::FromInternalValue( 610 return tick_clock_->NowTicks();
609 tick_clock_->NowTicks().ToInternalValue());
610 } 611 }
611 612
612 void EventGenerator::Init(gfx::NativeWindow root_window, 613 void EventGenerator::Init(gfx::NativeWindow root_window,
613 gfx::NativeWindow window_context) { 614 gfx::NativeWindow window_context) {
614 SetTickClock(WrapUnique(new TestTickClock())); 615 SetTickClock(WrapUnique(new TestTickClock()));
615 delegate()->SetContext(this, root_window, window_context); 616 delegate()->SetContext(this, root_window, window_context);
616 if (window_context) 617 if (window_context)
617 current_location_ = delegate()->CenterOfWindow(window_context); 618 current_location_ = delegate()->CenterOfWindow(window_context);
618 current_target_ = delegate()->GetTargetAt(current_location_); 619 current_target_ = delegate()->GetTargetAt(current_location_);
619 } 620 }
620 621
621 void EventGenerator::DispatchKeyEvent(bool is_press, 622 void EventGenerator::DispatchKeyEvent(bool is_press,
622 ui::KeyboardCode key_code, 623 ui::KeyboardCode key_code,
623 int flags) { 624 int flags) {
624 #if defined(OS_WIN) 625 #if defined(OS_WIN)
625 UINT key_press = WM_KEYDOWN; 626 UINT key_press = WM_KEYDOWN;
626 uint16_t character = ui::DomCodeToUsLayoutCharacter( 627 uint16_t character = ui::DomCodeToUsLayoutCharacter(
627 ui::UsLayoutKeyboardCodeToDomCode(key_code), flags); 628 ui::UsLayoutKeyboardCodeToDomCode(key_code), flags);
628 if (is_press && character) { 629 if (is_press && character) {
629 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; 630 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 };
630 TestKeyEvent keyev(native_event, flags); 631 TestKeyEvent keyev(native_event, flags);
631 Dispatch(&keyev); 632 Dispatch(&keyev);
632 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character 633 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character
633 // if the key event cooresponds to a real character. 634 // if the key event cooresponds to a real character.
634 key_press = WM_CHAR; 635 key_press = WM_CHAR;
635 key_code = static_cast<ui::KeyboardCode>(character); 636 key_code = static_cast<ui::KeyboardCode>(character);
636 } 637 }
637 MSG native_event = 638 MSG native_event =
638 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 }; 639 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 };
639 native_event.time = Now().InMicroseconds(); 640 native_event.time = (Now() - base::TimeTicks()).InMicroseconds();
640 TestKeyEvent keyev(native_event, flags); 641 TestKeyEvent keyev(native_event, flags);
641 #elif defined(USE_X11) 642 #elif defined(USE_X11)
642 ui::ScopedXI2Event xevent; 643 ui::ScopedXI2Event xevent;
643 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, 644 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED,
644 key_code, 645 key_code,
645 flags); 646 flags);
646 static_cast<XEvent*>(xevent)->xkey.time = Now().InMilliseconds() & UINT32_MAX; 647 static_cast<XEvent*>(xevent)->xkey.time =
648 (Now() - base::TimeTicks()).InMilliseconds() & UINT32_MAX;
647 ui::KeyEvent keyev(xevent); 649 ui::KeyEvent keyev(xevent);
648 #else 650 #else
649 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; 651 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
650 ui::KeyEvent keyev(type, key_code, flags); 652 ui::KeyEvent keyev(type, key_code, flags);
651 #endif // OS_WIN 653 #endif // OS_WIN
652 Dispatch(&keyev); 654 Dispatch(&keyev);
653 } 655 }
654 656
655 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { 657 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) {
656 current_target_ = delegate()->GetTargetAt(point); 658 current_target_ = delegate()->GetTargetAt(point);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 return default_delegate; 735 return default_delegate;
734 } 736 }
735 737
736 EventGeneratorDelegate* EventGenerator::delegate() { 738 EventGeneratorDelegate* EventGenerator::delegate() {
737 return const_cast<EventGeneratorDelegate*>( 739 return const_cast<EventGeneratorDelegate*>(
738 const_cast<const EventGenerator*>(this)->delegate()); 740 const_cast<const EventGenerator*>(this)->delegate());
739 } 741 }
740 742
741 } // namespace test 743 } // namespace test
742 } // namespace ui 744 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698