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

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: rebase Created 4 years, 6 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
« no previous file with comments | « ui/events/test/event_generator.h ('k') | ui/events/test/events_test_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 base::Bind(&DummyCallback)); 394 base::Bind(&DummyCallback));
395 } 395 }
396 396
397 void EventGenerator::GestureScrollSequenceWithCallback( 397 void EventGenerator::GestureScrollSequenceWithCallback(
398 const gfx::Point& start, 398 const gfx::Point& start,
399 const gfx::Point& end, 399 const gfx::Point& end,
400 const base::TimeDelta& step_delay, 400 const base::TimeDelta& step_delay,
401 int steps, 401 int steps,
402 const ScrollStepCallback& callback) { 402 const ScrollStepCallback& callback) {
403 const int kTouchId = 5; 403 const int kTouchId = 5;
404 base::TimeDelta timestamp = Now(); 404 base::TimeTicks timestamp = Now();
405 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, 0, kTouchId, 405 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, start, 0, kTouchId,
406 timestamp, 5.0f, 5.0f, 0.0f, 1.0f); 406 timestamp, 5.0f, 5.0f, 0.0f, 1.0f);
407 Dispatch(&press); 407 Dispatch(&press);
408 408
409 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF()); 409 callback.Run(ui::ET_GESTURE_SCROLL_BEGIN, gfx::Vector2dF());
410 410
411 float dx = static_cast<float>(end.x() - start.x()) / steps; 411 float dx = static_cast<float>(end.x() - start.x()) / steps;
412 float dy = static_cast<float>(end.y() - start.y()) / steps; 412 float dy = static_cast<float>(end.y() - start.y()) / steps;
413 gfx::PointF location(start); 413 gfx::PointF location(start);
414 for (int i = 0; i < steps; ++i) { 414 for (int i = 0; i < steps; ++i) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 CHECK_LE(count, kMaxTouchPoints); 454 CHECK_LE(count, kMaxTouchPoints);
455 CHECK_GT(steps, 0); 455 CHECK_GT(steps, 0);
456 456
457 int delta_x = move_x / steps; 457 int delta_x = move_x / steps;
458 int delta_y = move_y / steps; 458 int delta_y = move_y / steps;
459 459
460 for (int i = 0; i < count; ++i) { 460 for (int i = 0; i < count; ++i) {
461 points[i] = start[i]; 461 points[i] = start[i];
462 } 462 }
463 463
464 base::TimeDelta press_time_first = Now(); 464 base::TimeTicks press_time_first = Now();
465 base::TimeDelta press_time[kMaxTouchPoints]; 465 base::TimeTicks press_time[kMaxTouchPoints];
466 bool pressed[kMaxTouchPoints]; 466 bool pressed[kMaxTouchPoints];
467 for (int i = 0; i < count; ++i) { 467 for (int i = 0; i < count; ++i) {
468 pressed[i] = false; 468 pressed[i] = false;
469 press_time[i] = press_time_first + 469 press_time[i] = press_time_first +
470 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]); 470 base::TimeDelta::FromMilliseconds(delay_adding_finger_ms[i]);
471 } 471 }
472 472
473 int last_id = 0; 473 int last_id = 0;
474 for (int step = 0; step < steps; ++step) { 474 for (int step = 0; step < steps; ++step) {
475 base::TimeDelta move_time = press_time_first + 475 base::TimeTicks move_time =
476 press_time_first +
476 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step); 477 base::TimeDelta::FromMilliseconds(event_separation_time_ms * step);
477 478
478 while (last_id < count && 479 while (last_id < count &&
479 !pressed[last_id] && 480 !pressed[last_id] &&
480 move_time >= press_time[last_id]) { 481 move_time >= press_time[last_id]) {
481 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, 482 ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
482 points[last_id], 483 points[last_id],
483 last_id, 484 last_id,
484 press_time[last_id]); 485 press_time[last_id]);
485 Dispatch(&press); 486 Dispatch(&press);
486 pressed[last_id] = true; 487 pressed[last_id] = true;
487 last_id++; 488 last_id++;
488 } 489 }
489 490
490 for (int i = 0; i < count; ++i) { 491 for (int i = 0; i < count; ++i) {
491 points[i].Offset(delta_x, delta_y); 492 points[i].Offset(delta_x, delta_y);
492 if (i >= last_id) 493 if (i >= last_id)
493 continue; 494 continue;
494 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time); 495 ui::TouchEvent move(ui::ET_TOUCH_MOVED, points[i], i, move_time);
495 Dispatch(&move); 496 Dispatch(&move);
496 } 497 }
497 } 498 }
498 499
499 base::TimeDelta release_time = press_time_first + 500 base::TimeTicks release_time =
501 press_time_first +
500 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps); 502 base::TimeDelta::FromMilliseconds(event_separation_time_ms * steps);
501 for (int i = 0; i < last_id; ++i) { 503 for (int i = 0; i < last_id; ++i) {
502 ui::TouchEvent release( 504 ui::TouchEvent release(
503 ui::ET_TOUCH_RELEASED, points[i], i, release_time); 505 ui::ET_TOUCH_RELEASED, points[i], i, release_time);
504 Dispatch(&release); 506 Dispatch(&release);
505 } 507 }
506 } 508 }
507 509
508 void EventGenerator::ScrollSequence(const gfx::Point& start, 510 void EventGenerator::ScrollSequence(const gfx::Point& start,
509 const base::TimeDelta& step_delay, 511 const base::TimeDelta& step_delay,
510 float x_offset, 512 float x_offset,
511 float y_offset, 513 float y_offset,
512 int steps, 514 int steps,
513 int num_fingers) { 515 int num_fingers) {
514 base::TimeDelta timestamp = Now(); 516 base::TimeTicks timestamp = Now();
515 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, 517 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
516 start, 518 start,
517 timestamp, 519 timestamp,
518 0, 520 0,
519 0, 0, 521 0, 0,
520 0, 0, 522 0, 0,
521 num_fingers); 523 num_fingers);
522 Dispatch(&fling_cancel); 524 Dispatch(&fling_cancel);
523 525
524 float dx = x_offset / steps; 526 float dx = x_offset / steps;
(...skipping 18 matching lines...) Expand all
543 x_offset, y_offset, 545 x_offset, y_offset,
544 num_fingers); 546 num_fingers);
545 Dispatch(&fling_start); 547 Dispatch(&fling_start);
546 } 548 }
547 549
548 void EventGenerator::ScrollSequence(const gfx::Point& start, 550 void EventGenerator::ScrollSequence(const gfx::Point& start,
549 const base::TimeDelta& step_delay, 551 const base::TimeDelta& step_delay,
550 const std::vector<gfx::PointF>& offsets, 552 const std::vector<gfx::PointF>& offsets,
551 int num_fingers) { 553 int num_fingers) {
552 size_t steps = offsets.size(); 554 size_t steps = offsets.size();
553 base::TimeDelta timestamp = Now(); 555 base::TimeTicks timestamp = Now();
554 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL, 556 ui::ScrollEvent fling_cancel(ui::ET_SCROLL_FLING_CANCEL,
555 start, 557 start,
556 timestamp, 558 timestamp,
557 0, 559 0,
558 0, 0, 560 0, 0,
559 0, 0, 561 0, 0,
560 num_fingers); 562 num_fingers);
561 Dispatch(&fling_cancel); 563 Dispatch(&fling_cancel);
562 564
563 for (size_t i = 0; i < steps; ++i) { 565 for (size_t i = 0; i < steps; ++i) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 598
597 void EventGenerator::SetTickClock(std::unique_ptr<base::TickClock> tick_clock) { 599 void EventGenerator::SetTickClock(std::unique_ptr<base::TickClock> tick_clock) {
598 scoped_refptr<ClonableTickClock> clonable = 600 scoped_refptr<ClonableTickClock> clonable =
599 new ClonableTickClock(std::move(tick_clock)); 601 new ClonableTickClock(std::move(tick_clock));
600 #if defined(USE_X11) 602 #if defined(USE_X11)
601 ResetTimestampRolloverCountersForTesting(clonable->Clone()); 603 ResetTimestampRolloverCountersForTesting(clonable->Clone());
602 #endif 604 #endif
603 tick_clock_ = clonable->Clone(); 605 tick_clock_ = clonable->Clone();
604 } 606 }
605 607
606 base::TimeDelta EventGenerator::Now() { 608 base::TimeTicks EventGenerator::Now() {
607 // This is the same as what EventTimeForNow() does, but here we do it 609 // This is the same as what EventTimeForNow() does, but here we do it
608 // with a tick clock that can be replaced with a simulated clock for tests. 610 // with a tick clock that can be replaced with a simulated clock for tests.
609 return base::TimeDelta::FromInternalValue( 611 return tick_clock_->NowTicks();
610 tick_clock_->NowTicks().ToInternalValue());
611 } 612 }
612 613
613 void EventGenerator::Init(gfx::NativeWindow root_window, 614 void EventGenerator::Init(gfx::NativeWindow root_window,
614 gfx::NativeWindow window_context) { 615 gfx::NativeWindow window_context) {
615 SetTickClock(WrapUnique(new TestTickClock())); 616 SetTickClock(WrapUnique(new TestTickClock()));
616 delegate()->SetContext(this, root_window, window_context); 617 delegate()->SetContext(this, root_window, window_context);
617 if (window_context) 618 if (window_context)
618 current_location_ = delegate()->CenterOfWindow(window_context); 619 current_location_ = delegate()->CenterOfWindow(window_context);
619 current_target_ = delegate()->GetTargetAt(current_location_); 620 current_target_ = delegate()->GetTargetAt(current_location_);
620 } 621 }
621 622
622 void EventGenerator::DispatchKeyEvent(bool is_press, 623 void EventGenerator::DispatchKeyEvent(bool is_press,
623 ui::KeyboardCode key_code, 624 ui::KeyboardCode key_code,
624 int flags) { 625 int flags) {
625 #if defined(OS_WIN) 626 #if defined(OS_WIN)
626 UINT key_press = WM_KEYDOWN; 627 UINT key_press = WM_KEYDOWN;
627 uint16_t character = ui::DomCodeToUsLayoutCharacter( 628 uint16_t character = ui::DomCodeToUsLayoutCharacter(
628 ui::UsLayoutKeyboardCodeToDomCode(key_code), flags); 629 ui::UsLayoutKeyboardCodeToDomCode(key_code), flags);
629 if (is_press && character) { 630 if (is_press && character) {
630 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 }; 631 MSG native_event = { NULL, WM_KEYDOWN, key_code, 0 };
631 TestKeyEvent keyev(native_event, flags); 632 TestKeyEvent keyev(native_event, flags);
632 Dispatch(&keyev); 633 Dispatch(&keyev);
633 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character 634 // On Windows, WM_KEYDOWN event is followed by WM_CHAR with a character
634 // if the key event cooresponds to a real character. 635 // if the key event cooresponds to a real character.
635 key_press = WM_CHAR; 636 key_press = WM_CHAR;
636 key_code = static_cast<ui::KeyboardCode>(character); 637 key_code = static_cast<ui::KeyboardCode>(character);
637 } 638 }
638 MSG native_event = 639 MSG native_event =
639 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 }; 640 { NULL, (is_press ? key_press : WM_KEYUP), key_code, 0 };
640 native_event.time = Now().InMicroseconds(); 641 native_event.time = (Now() - base::TimeTicks()).InMicroseconds();
641 TestKeyEvent keyev(native_event, flags); 642 TestKeyEvent keyev(native_event, flags);
642 #elif defined(USE_X11) 643 #elif defined(USE_X11)
643 ui::ScopedXI2Event xevent; 644 ui::ScopedXI2Event xevent;
644 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED, 645 xevent.InitKeyEvent(is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED,
645 key_code, 646 key_code,
646 flags); 647 flags);
647 static_cast<XEvent*>(xevent)->xkey.time = Now().InMilliseconds() & UINT32_MAX; 648 static_cast<XEvent*>(xevent)->xkey.time =
649 (Now() - base::TimeTicks()).InMilliseconds() & UINT32_MAX;
648 ui::KeyEvent keyev(xevent); 650 ui::KeyEvent keyev(xevent);
649 #else 651 #else
650 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED; 652 ui::EventType type = is_press ? ui::ET_KEY_PRESSED : ui::ET_KEY_RELEASED;
651 ui::KeyEvent keyev(type, key_code, flags); 653 ui::KeyEvent keyev(type, key_code, flags);
652 #endif // OS_WIN 654 #endif // OS_WIN
653 Dispatch(&keyev); 655 Dispatch(&keyev);
654 } 656 }
655 657
656 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) { 658 void EventGenerator::UpdateCurrentDispatcher(const gfx::Point& point) {
657 current_target_ = delegate()->GetTargetAt(point); 659 current_target_ = delegate()->GetTargetAt(point);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 return default_delegate; 736 return default_delegate;
735 } 737 }
736 738
737 EventGeneratorDelegate* EventGenerator::delegate() { 739 EventGeneratorDelegate* EventGenerator::delegate() {
738 return const_cast<EventGeneratorDelegate*>( 740 return const_cast<EventGeneratorDelegate*>(
739 const_cast<const EventGenerator*>(this)->delegate()); 741 const_cast<const EventGenerator*>(this)->delegate());
740 } 742 }
741 743
742 } // namespace test 744 } // namespace test
743 } // namespace ui 745 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/test/event_generator.h ('k') | ui/events/test/events_test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698