| OLD | NEW |
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/events/gesture_detection/motion_event_buffer.h" | 10 #include "ui/events/gesture_detection/motion_event_buffer.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 | 40 |
| 41 class MotionEventBufferTest : public testing::Test, | 41 class MotionEventBufferTest : public testing::Test, |
| 42 public MotionEventBufferClient { | 42 public MotionEventBufferClient { |
| 43 public: | 43 public: |
| 44 MotionEventBufferTest() : needs_flush_(false) {} | 44 MotionEventBufferTest() : needs_flush_(false) {} |
| 45 ~MotionEventBufferTest() override {} | 45 ~MotionEventBufferTest() override {} |
| 46 | 46 |
| 47 // MotionEventBufferClient implementation. | 47 // MotionEventBufferClient implementation. |
| 48 void ForwardMotionEvent(const MotionEvent& event) override { | 48 void ForwardMotionEvent(const MotionEvent& event) override { |
| 49 forwarded_events_.push_back(event.Clone().release()); | 49 forwarded_events_.push_back(event.Clone()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void SetNeedsFlush() override { needs_flush_ = true; } | 52 void SetNeedsFlush() override { needs_flush_ = true; } |
| 53 | 53 |
| 54 bool GetAndResetNeedsFlush() { | 54 bool GetAndResetNeedsFlush() { |
| 55 bool needs_flush = needs_flush_; | 55 bool needs_flush = needs_flush_; |
| 56 needs_flush_ = false; | 56 needs_flush_ = false; |
| 57 return needs_flush; | 57 return needs_flush; |
| 58 } | 58 } |
| 59 | 59 |
| 60 ScopedVector<MotionEvent> GetAndResetForwardedEvents() { | 60 std::vector<std::unique_ptr<MotionEvent>> GetAndResetForwardedEvents() { |
| 61 ScopedVector<MotionEvent> forwarded_events; | 61 std::vector<std::unique_ptr<MotionEvent>> forwarded_events; |
| 62 forwarded_events.swap(forwarded_events_); | 62 forwarded_events.swap(forwarded_events_); |
| 63 return forwarded_events; | 63 return forwarded_events; |
| 64 } | 64 } |
| 65 | 65 |
| 66 const MotionEvent* GetLastEvent() const { | 66 const MotionEvent* GetLastEvent() const { |
| 67 return forwarded_events_.empty() ? NULL : forwarded_events_.back(); | 67 return forwarded_events_.empty() ? nullptr : forwarded_events_.back().get(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 static base::TimeDelta LargeDelta() { | 70 static base::TimeDelta LargeDelta() { |
| 71 return base::TimeDelta::FromMilliseconds(kLargeDeltaMs); | 71 return base::TimeDelta::FromMilliseconds(kLargeDeltaMs); |
| 72 } | 72 } |
| 73 | 73 |
| 74 static base::TimeDelta SmallDelta() { | 74 static base::TimeDelta SmallDelta() { |
| 75 return base::TimeDelta::FromMilliseconds(kSmallDeltaMs); | 75 return base::TimeDelta::FromMilliseconds(kSmallDeltaMs); |
| 76 } | 76 } |
| 77 | 77 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 last_flushed_event = current_flushed_event->Clone(); | 234 last_flushed_event = current_flushed_event->Clone(); |
| 235 events += GetAndResetForwardedEvents().size(); | 235 events += GetAndResetForwardedEvents().size(); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 events += GetAndResetForwardedEvents().size(); | 239 events += GetAndResetForwardedEvents().size(); |
| 240 EXPECT_GE(events, min_expected_events); | 240 EXPECT_GE(events, min_expected_events); |
| 241 } | 241 } |
| 242 | 242 |
| 243 private: | 243 private: |
| 244 ScopedVector<MotionEvent> forwarded_events_; | 244 std::vector<std::unique_ptr<MotionEvent>> forwarded_events_; |
| 245 bool needs_flush_; | 245 bool needs_flush_; |
| 246 }; | 246 }; |
| 247 | 247 |
| 248 TEST_F(MotionEventBufferTest, BufferEmpty) { | 248 TEST_F(MotionEventBufferTest, BufferEmpty) { |
| 249 MotionEventBuffer buffer(this, true); | 249 MotionEventBuffer buffer(this, true); |
| 250 | 250 |
| 251 buffer.Flush(base::TimeTicks::Now()); | 251 buffer.Flush(base::TimeTicks::Now()); |
| 252 EXPECT_FALSE(GetAndResetNeedsFlush()); | 252 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 253 EXPECT_FALSE(GetLastEvent()); | 253 EXPECT_FALSE(GetLastEvent()); |
| 254 } | 254 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 EXPECT_FALSE(GetAndResetNeedsFlush()); | 292 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 293 EXPECT_FALSE(GetLastEvent()); | 293 EXPECT_FALSE(GetLastEvent()); |
| 294 | 294 |
| 295 // The up should flush the buffer. | 295 // The up should flush the buffer. |
| 296 MockMotionEvent up(MotionEvent::ACTION_UP, event_time, 4.f, 4.f); | 296 MockMotionEvent up(MotionEvent::ACTION_UP, event_time, 4.f, 4.f); |
| 297 buffer.OnMotionEvent(up); | 297 buffer.OnMotionEvent(up); |
| 298 EXPECT_FALSE(GetAndResetNeedsFlush()); | 298 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 299 | 299 |
| 300 // The flushed events should include the up and the moves, with the latter | 300 // The flushed events should include the up and the moves, with the latter |
| 301 // combined into a single event with history. | 301 // combined into a single event with history. |
| 302 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 302 std::vector<std::unique_ptr<MotionEvent>> events = |
| 303 GetAndResetForwardedEvents(); |
| 303 ASSERT_EQ(2U, events.size()); | 304 ASSERT_EQ(2U, events.size()); |
| 304 EXPECT_EVENT_EQ(up, *events.back()); | 305 EXPECT_EVENT_EQ(up, *events.back()); |
| 305 EXPECT_EQ(2U, events.front()->GetHistorySize()); | 306 EXPECT_EQ(2U, events.front()->GetHistorySize()); |
| 306 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move2); | 307 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move2); |
| 307 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 308 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 308 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); | 309 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); |
| 309 } | 310 } |
| 310 | 311 |
| 311 TEST_F(MotionEventBufferTest, BufferFlushedOnIncompatibleActionMove) { | 312 TEST_F(MotionEventBufferTest, BufferFlushedOnIncompatibleActionMove) { |
| 312 base::TimeTicks event_time = base::TimeTicks::Now(); | 313 base::TimeTicks event_time = base::TimeTicks::Now(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 333 MockMotionEvent move2(move1); | 334 MockMotionEvent move2(move1); |
| 334 move2.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 335 move2.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 335 buffer.OnMotionEvent(move2); | 336 buffer.OnMotionEvent(move2); |
| 336 EXPECT_FALSE(GetAndResetNeedsFlush()); | 337 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 337 EXPECT_EVENT_EQ(move1, *GetLastEvent()); | 338 EXPECT_EVENT_EQ(move1, *GetLastEvent()); |
| 338 | 339 |
| 339 event_time += base::TimeDelta::FromMilliseconds(5); | 340 event_time += base::TimeDelta::FromMilliseconds(5); |
| 340 | 341 |
| 341 // The flushed event should only include the latest move event. | 342 // The flushed event should only include the latest move event. |
| 342 buffer.Flush(event_time); | 343 buffer.Flush(event_time); |
| 343 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 344 std::vector<std::unique_ptr<MotionEvent>> events = |
| 345 GetAndResetForwardedEvents(); |
| 344 ASSERT_EQ(3U, events.size()); | 346 ASSERT_EQ(3U, events.size()); |
| 345 EXPECT_EVENT_EQ(move2, *events.back()); | 347 EXPECT_EVENT_EQ(move2, *events.back()); |
| 346 EXPECT_FALSE(GetAndResetNeedsFlush()); | 348 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 347 | 349 |
| 348 event_time += base::TimeDelta::FromMilliseconds(5); | 350 event_time += base::TimeDelta::FromMilliseconds(5); |
| 349 | 351 |
| 350 // Events with different pointer ids should not combine. | 352 // Events with different pointer ids should not combine. |
| 351 PointerProperties pointer0(5.f, 5.f, 1.f); | 353 PointerProperties pointer0(5.f, 5.f, 1.f); |
| 352 pointer0.id = 1; | 354 pointer0.id = 1; |
| 353 PointerProperties pointer1(10.f, 10.f, 2.f); | 355 PointerProperties pointer1(10.f, 10.f, 2.f); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 buffer.OnMotionEvent(move1); | 433 buffer.OnMotionEvent(move1); |
| 432 EXPECT_FALSE(GetAndResetNeedsFlush()); | 434 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 433 ASSERT_FALSE(GetLastEvent()); | 435 ASSERT_FALSE(GetLastEvent()); |
| 434 | 436 |
| 435 // As the two events are logically the same but for ordering and time, the | 437 // As the two events are logically the same but for ordering and time, the |
| 436 // synthesized event should yield a logically identical event. | 438 // synthesized event should yield a logically identical event. |
| 437 base::TimeTicks flush_time = move1.GetEventTime() + ResampleDelta(); | 439 base::TimeTicks flush_time = move1.GetEventTime() + ResampleDelta(); |
| 438 buffer.Flush(flush_time); | 440 buffer.Flush(flush_time); |
| 439 EXPECT_FALSE(GetAndResetNeedsFlush()); | 441 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 440 ASSERT_TRUE(GetLastEvent()); | 442 ASSERT_TRUE(GetLastEvent()); |
| 441 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 443 std::vector<std::unique_ptr<MotionEvent>> events = |
| 444 GetAndResetForwardedEvents(); |
| 442 ASSERT_EQ(1U, events.size()); | 445 ASSERT_EQ(1U, events.size()); |
| 443 EXPECT_EVENT_IGNORING_HISTORY_EQ(move1, *events.front()); | 446 EXPECT_EVENT_IGNORING_HISTORY_EQ(move1, *events.front()); |
| 444 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 447 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 445 } | 448 } |
| 446 | 449 |
| 447 TEST_F(MotionEventBufferTest, FlushedEventsNeverLaterThanFlushTime) { | 450 TEST_F(MotionEventBufferTest, FlushedEventsNeverLaterThanFlushTime) { |
| 448 base::TimeTicks event_time = base::TimeTicks::Now(); | 451 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 449 MotionEventBuffer buffer(this, true); | 452 MotionEventBuffer buffer(this, true); |
| 450 | 453 |
| 451 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); | 454 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 // Flush at a time between the first and second events. | 522 // Flush at a time between the first and second events. |
| 520 base::TimeTicks interpolated_time = | 523 base::TimeTicks interpolated_time = |
| 521 move0.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; | 524 move0.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; |
| 522 base::TimeTicks flush_time = interpolated_time; | 525 base::TimeTicks flush_time = interpolated_time; |
| 523 buffer.Flush(flush_time); | 526 buffer.Flush(flush_time); |
| 524 ASSERT_TRUE(GetLastEvent()); | 527 ASSERT_TRUE(GetLastEvent()); |
| 525 EXPECT_TRUE(GetAndResetNeedsFlush()); | 528 EXPECT_TRUE(GetAndResetNeedsFlush()); |
| 526 | 529 |
| 527 // There should only be one flushed event, with the second remaining buffered | 530 // There should only be one flushed event, with the second remaining buffered |
| 528 // and no resampling having occurred. | 531 // and no resampling having occurred. |
| 529 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 532 std::vector<std::unique_ptr<MotionEvent>> events = |
| 533 GetAndResetForwardedEvents(); |
| 530 ASSERT_EQ(1U, events.size()); | 534 ASSERT_EQ(1U, events.size()); |
| 531 EXPECT_EVENT_EQ(move0, *events.front()); | 535 EXPECT_EVENT_EQ(move0, *events.front()); |
| 532 | 536 |
| 533 // The second move should be flushed without resampling. | 537 // The second move should be flushed without resampling. |
| 534 flush_time = move1.GetEventTime(); | 538 flush_time = move1.GetEventTime(); |
| 535 buffer.Flush(flush_time); | 539 buffer.Flush(flush_time); |
| 536 ASSERT_TRUE(GetLastEvent()); | 540 ASSERT_TRUE(GetLastEvent()); |
| 537 EXPECT_EVENT_EQ(move1, *GetLastEvent()); | 541 EXPECT_EVENT_EQ(move1, *GetLastEvent()); |
| 538 GetAndResetForwardedEvents(); | 542 GetAndResetForwardedEvents(); |
| 539 | 543 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()); | 591 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()); |
| 588 base::TimeTicks flush_time = extrapolated_time + ResampleDelta(); | 592 base::TimeTicks flush_time = extrapolated_time + ResampleDelta(); |
| 589 buffer.Flush(flush_time); | 593 buffer.Flush(flush_time); |
| 590 ASSERT_TRUE(GetLastEvent()); | 594 ASSERT_TRUE(GetLastEvent()); |
| 591 EXPECT_FALSE(GetAndResetNeedsFlush()); | 595 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 592 | 596 |
| 593 // There should only be one flushed event, with the event extrapolated from | 597 // There should only be one flushed event, with the event extrapolated from |
| 594 // the two events. | 598 // the two events. |
| 595 base::TimeTicks expected_time = | 599 base::TimeTicks expected_time = |
| 596 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; | 600 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; |
| 597 ScopedVector<MotionEvent> events0 = GetAndResetForwardedEvents(); | 601 std::vector<std::unique_ptr<MotionEvent>> events0 = |
| 602 GetAndResetForwardedEvents(); |
| 598 ASSERT_EQ(1U, events0.size()); | 603 ASSERT_EQ(1U, events0.size()); |
| 599 EXPECT_EQ(2U, events0.front()->GetHistorySize()); | 604 EXPECT_EQ(2U, events0.front()->GetHistorySize()); |
| 600 EXPECT_EQ(expected_time, events0.front()->GetEventTime()); | 605 EXPECT_EQ(expected_time, events0.front()->GetEventTime()); |
| 601 EXPECT_FALSE(GetAndResetNeedsFlush()); | 606 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 602 | 607 |
| 603 // Try enqueuing an event *after* the second event but *before* the | 608 // Try enqueuing an event *after* the second event but *before* the |
| 604 // extrapolated event. It should be dropped. | 609 // extrapolated event. It should be dropped. |
| 605 event_time = move1.GetEventTime() + base::TimeDelta::FromMilliseconds(1); | 610 event_time = move1.GetEventTime() + base::TimeDelta::FromMilliseconds(1); |
| 606 MockMotionEvent move2(MotionEvent::ACTION_MOVE, event_time, 15.f, 25.f); | 611 MockMotionEvent move2(MotionEvent::ACTION_MOVE, event_time, 15.f, 25.f); |
| 607 buffer.OnMotionEvent(move1); | 612 buffer.OnMotionEvent(move1); |
| 608 ASSERT_FALSE(GetLastEvent()); | 613 ASSERT_FALSE(GetLastEvent()); |
| 609 EXPECT_FALSE(GetAndResetNeedsFlush()); | 614 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 610 | 615 |
| 611 // Finally queue an event *after* the extrapolated event. | 616 // Finally queue an event *after* the extrapolated event. |
| 612 event_time = expected_time + base::TimeDelta::FromMilliseconds(1); | 617 event_time = expected_time + base::TimeDelta::FromMilliseconds(1); |
| 613 MockMotionEvent move3(MotionEvent::ACTION_MOVE, event_time, 15.f, 25.f); | 618 MockMotionEvent move3(MotionEvent::ACTION_MOVE, event_time, 15.f, 25.f); |
| 614 buffer.OnMotionEvent(move3); | 619 buffer.OnMotionEvent(move3); |
| 615 ASSERT_FALSE(GetLastEvent()); | 620 ASSERT_FALSE(GetLastEvent()); |
| 616 EXPECT_TRUE(GetAndResetNeedsFlush()); | 621 EXPECT_TRUE(GetAndResetNeedsFlush()); |
| 617 | 622 |
| 618 // The flushed event should simply be the latest event. | 623 // The flushed event should simply be the latest event. |
| 619 flush_time = event_time + ResampleDelta(); | 624 flush_time = event_time + ResampleDelta(); |
| 620 buffer.Flush(flush_time); | 625 buffer.Flush(flush_time); |
| 621 ASSERT_TRUE(GetLastEvent()); | 626 ASSERT_TRUE(GetLastEvent()); |
| 622 ScopedVector<MotionEvent> events1 = GetAndResetForwardedEvents(); | 627 std::vector<std::unique_ptr<MotionEvent>> events1 = |
| 628 GetAndResetForwardedEvents(); |
| 623 ASSERT_EQ(1U, events1.size()); | 629 ASSERT_EQ(1U, events1.size()); |
| 624 EXPECT_EVENT_EQ(move3, *events1.front()); | 630 EXPECT_EVENT_EQ(move3, *events1.front()); |
| 625 EXPECT_FALSE(GetAndResetNeedsFlush()); | 631 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 626 } | 632 } |
| 627 | 633 |
| 628 TEST_F(MotionEventBufferTest, NoResamplingWithSmallTimeDeltaBetweenMoves) { | 634 TEST_F(MotionEventBufferTest, NoResamplingWithSmallTimeDeltaBetweenMoves) { |
| 629 base::TimeTicks event_time = base::TimeTicks::Now(); | 635 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 630 MotionEventBuffer buffer(this, true); | 636 MotionEventBuffer buffer(this, true); |
| 631 | 637 |
| 632 // The first move should be buffered. | 638 // The first move should be buffered. |
| 633 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); | 639 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); |
| 634 buffer.OnMotionEvent(move0); | 640 buffer.OnMotionEvent(move0); |
| 635 ASSERT_FALSE(GetLastEvent()); | 641 ASSERT_FALSE(GetLastEvent()); |
| 636 EXPECT_TRUE(GetAndResetNeedsFlush()); | 642 EXPECT_TRUE(GetAndResetNeedsFlush()); |
| 637 | 643 |
| 638 // The second move should remain buffered. | 644 // The second move should remain buffered. |
| 639 event_time += SmallDelta(); | 645 event_time += SmallDelta(); |
| 640 MockMotionEvent move1(MotionEvent::ACTION_MOVE, event_time, 2.f, 2.f); | 646 MockMotionEvent move1(MotionEvent::ACTION_MOVE, event_time, 2.f, 2.f); |
| 641 buffer.OnMotionEvent(move1); | 647 buffer.OnMotionEvent(move1); |
| 642 ASSERT_FALSE(GetLastEvent()); | 648 ASSERT_FALSE(GetLastEvent()); |
| 643 EXPECT_FALSE(GetAndResetNeedsFlush()); | 649 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 644 | 650 |
| 645 base::TimeTicks flush_time = event_time + ResampleDelta(); | 651 base::TimeTicks flush_time = event_time + ResampleDelta(); |
| 646 buffer.Flush(flush_time); | 652 buffer.Flush(flush_time); |
| 647 EXPECT_FALSE(GetAndResetNeedsFlush()); | 653 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 648 | 654 |
| 649 // There should only be one flushed event, and no resampling should have | 655 // There should only be one flushed event, and no resampling should have |
| 650 // occured between the first and the second as they were temporally too close. | 656 // occured between the first and the second as they were temporally too close. |
| 651 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 657 std::vector<std::unique_ptr<MotionEvent>> events = |
| 658 GetAndResetForwardedEvents(); |
| 652 ASSERT_EQ(1U, events.size()); | 659 ASSERT_EQ(1U, events.size()); |
| 653 EXPECT_EQ(1U, events.front()->GetHistorySize()); | 660 EXPECT_EQ(1U, events.front()->GetHistorySize()); |
| 654 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move1); | 661 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move1); |
| 655 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 662 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 656 } | 663 } |
| 657 | 664 |
| 658 TEST_F(MotionEventBufferTest, NoResamplingWithMismatchBetweenMoves) { | 665 TEST_F(MotionEventBufferTest, NoResamplingWithMismatchBetweenMoves) { |
| 659 base::TimeTicks event_time = base::TimeTicks::Now(); | 666 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 660 MotionEventBuffer buffer(this, true); | 667 MotionEventBuffer buffer(this, true); |
| 661 | 668 |
| 662 // The first move should be buffered. | 669 // The first move should be buffered. |
| 663 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); | 670 MockMotionEvent move0(MotionEvent::ACTION_MOVE, event_time, 1.f, 1.f); |
| 664 buffer.OnMotionEvent(move0); | 671 buffer.OnMotionEvent(move0); |
| 665 ASSERT_FALSE(GetLastEvent()); | 672 ASSERT_FALSE(GetLastEvent()); |
| 666 EXPECT_TRUE(GetAndResetNeedsFlush()); | 673 EXPECT_TRUE(GetAndResetNeedsFlush()); |
| 667 | 674 |
| 668 // The second move should remain buffered. | 675 // The second move should remain buffered. |
| 669 event_time += SmallDelta(); | 676 event_time += SmallDelta(); |
| 670 MockMotionEvent move1(MotionEvent::ACTION_MOVE, event_time, 2.f, 2.f); | 677 MockMotionEvent move1(MotionEvent::ACTION_MOVE, event_time, 2.f, 2.f); |
| 671 buffer.OnMotionEvent(move1); | 678 buffer.OnMotionEvent(move1); |
| 672 ASSERT_FALSE(GetLastEvent()); | 679 ASSERT_FALSE(GetLastEvent()); |
| 673 EXPECT_FALSE(GetAndResetNeedsFlush()); | 680 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 674 | 681 |
| 675 base::TimeTicks flush_time = event_time + ResampleDelta(); | 682 base::TimeTicks flush_time = event_time + ResampleDelta(); |
| 676 buffer.Flush(flush_time); | 683 buffer.Flush(flush_time); |
| 677 EXPECT_FALSE(GetAndResetNeedsFlush()); | 684 EXPECT_FALSE(GetAndResetNeedsFlush()); |
| 678 | 685 |
| 679 // There should only be one flushed event, and no resampling should have | 686 // There should only be one flushed event, and no resampling should have |
| 680 // occured between the first and the second as they were temporally too close. | 687 // occured between the first and the second as they were temporally too close. |
| 681 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 688 std::vector<std::unique_ptr<MotionEvent>> events = |
| 689 GetAndResetForwardedEvents(); |
| 682 ASSERT_EQ(1U, events.size()); | 690 ASSERT_EQ(1U, events.size()); |
| 683 EXPECT_EQ(1U, events.front()->GetHistorySize()); | 691 EXPECT_EQ(1U, events.front()->GetHistorySize()); |
| 684 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move1); | 692 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), move1); |
| 685 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 693 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 686 } | 694 } |
| 687 | 695 |
| 688 TEST_F(MotionEventBufferTest, Interpolation) { | 696 TEST_F(MotionEventBufferTest, Interpolation) { |
| 689 base::TimeTicks event_time = base::TimeTicks::Now(); | 697 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 690 MotionEventBuffer buffer(this, true); | 698 MotionEventBuffer buffer(this, true); |
| 691 | 699 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 711 | 719 |
| 712 // There should only be one flushed event, with the event interpolated between | 720 // There should only be one flushed event, with the event interpolated between |
| 713 // the two events. The second event should remain buffered. | 721 // the two events. The second event should remain buffered. |
| 714 float alpha = (interpolated_time - move0.GetEventTime()).InMillisecondsF() / | 722 float alpha = (interpolated_time - move0.GetEventTime()).InMillisecondsF() / |
| 715 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); | 723 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); |
| 716 MockMotionEvent interpolated_event( | 724 MockMotionEvent interpolated_event( |
| 717 MotionEvent::ACTION_MOVE, | 725 MotionEvent::ACTION_MOVE, |
| 718 interpolated_time, | 726 interpolated_time, |
| 719 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * alpha, | 727 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * alpha, |
| 720 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * alpha); | 728 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * alpha); |
| 721 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 729 std::vector<std::unique_ptr<MotionEvent>> events = |
| 730 GetAndResetForwardedEvents(); |
| 722 ASSERT_EQ(1U, events.size()); | 731 ASSERT_EQ(1U, events.size()); |
| 723 EXPECT_EQ(1U, events.front()->GetHistorySize()); | 732 EXPECT_EQ(1U, events.front()->GetHistorySize()); |
| 724 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), interpolated_event); | 733 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), interpolated_event); |
| 725 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 734 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 726 | 735 |
| 727 // The second move should be flushed without resampling. | 736 // The second move should be flushed without resampling. |
| 728 flush_time = move1.GetEventTime() + ResampleDelta(); | 737 flush_time = move1.GetEventTime() + ResampleDelta(); |
| 729 buffer.Flush(flush_time); | 738 buffer.Flush(flush_time); |
| 730 ASSERT_TRUE(GetLastEvent()); | 739 ASSERT_TRUE(GetLastEvent()); |
| 731 EXPECT_EVENT_EQ(move1, *GetLastEvent()); | 740 EXPECT_EVENT_EQ(move1, *GetLastEvent()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 763 base::TimeTicks expected_time = | 772 base::TimeTicks expected_time = |
| 764 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; | 773 move1.GetEventTime() + (move1.GetEventTime() - move0.GetEventTime()) / 2; |
| 765 float expected_alpha = | 774 float expected_alpha = |
| 766 (expected_time - move0.GetEventTime()).InMillisecondsF() / | 775 (expected_time - move0.GetEventTime()).InMillisecondsF() / |
| 767 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); | 776 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); |
| 768 MockMotionEvent extrapolated_event( | 777 MockMotionEvent extrapolated_event( |
| 769 MotionEvent::ACTION_MOVE, | 778 MotionEvent::ACTION_MOVE, |
| 770 expected_time, | 779 expected_time, |
| 771 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * expected_alpha, | 780 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * expected_alpha, |
| 772 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * expected_alpha); | 781 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * expected_alpha); |
| 773 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 782 std::vector<std::unique_ptr<MotionEvent>> events = |
| 783 GetAndResetForwardedEvents(); |
| 774 ASSERT_EQ(1U, events.size()); | 784 ASSERT_EQ(1U, events.size()); |
| 775 EXPECT_EQ(2U, events.front()->GetHistorySize()); | 785 EXPECT_EQ(2U, events.front()->GetHistorySize()); |
| 776 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), extrapolated_event); | 786 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), extrapolated_event); |
| 777 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 787 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 778 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); | 788 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); |
| 779 } | 789 } |
| 780 | 790 |
| 781 TEST_F(MotionEventBufferTest, ExtrapolationHorizonLimited) { | 791 TEST_F(MotionEventBufferTest, ExtrapolationHorizonLimited) { |
| 782 base::TimeTicks event_time = base::TimeTicks::Now(); | 792 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 783 MotionEventBuffer buffer(this, true); | 793 MotionEventBuffer buffer(this, true); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 808 base::TimeTicks expected_time = | 818 base::TimeTicks expected_time = |
| 809 move1.GetEventTime() + base::TimeDelta::FromMilliseconds(8); | 819 move1.GetEventTime() + base::TimeDelta::FromMilliseconds(8); |
| 810 float expected_alpha = | 820 float expected_alpha = |
| 811 (expected_time - move0.GetEventTime()).InMillisecondsF() / | 821 (expected_time - move0.GetEventTime()).InMillisecondsF() / |
| 812 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); | 822 (move1.GetEventTime() - move0.GetEventTime()).InMillisecondsF(); |
| 813 MockMotionEvent extrapolated_event( | 823 MockMotionEvent extrapolated_event( |
| 814 MotionEvent::ACTION_MOVE, | 824 MotionEvent::ACTION_MOVE, |
| 815 expected_time, | 825 expected_time, |
| 816 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * expected_alpha, | 826 move0.GetX(0) + (move1.GetX(0) - move0.GetX(0)) * expected_alpha, |
| 817 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * expected_alpha); | 827 move0.GetY(0) + (move1.GetY(0) - move0.GetY(0)) * expected_alpha); |
| 818 ScopedVector<MotionEvent> events = GetAndResetForwardedEvents(); | 828 std::vector<std::unique_ptr<MotionEvent>> events = |
| 829 GetAndResetForwardedEvents(); |
| 819 ASSERT_EQ(1U, events.size()); | 830 ASSERT_EQ(1U, events.size()); |
| 820 EXPECT_EQ(2U, events.front()->GetHistorySize()); | 831 EXPECT_EQ(2U, events.front()->GetHistorySize()); |
| 821 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), extrapolated_event); | 832 EXPECT_EVENT_IGNORING_HISTORY_EQ(*events.front(), extrapolated_event); |
| 822 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); | 833 EXPECT_EVENT_HISTORY_EQ(*events.front(), 0, move0); |
| 823 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); | 834 EXPECT_EVENT_HISTORY_EQ(*events.front(), 1, move1); |
| 824 } | 835 } |
| 825 | 836 |
| 826 TEST_F(MotionEventBufferTest, Resampling30to60) { | 837 TEST_F(MotionEventBufferTest, Resampling30to60) { |
| 827 base::TimeDelta flush_time_delta = | 838 base::TimeDelta flush_time_delta = |
| 828 base::TimeDelta::FromMillisecondsD(1000. / 60.); | 839 base::TimeDelta::FromMillisecondsD(1000. / 60.); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 TEST_F(MotionEventBufferTest, Resampling150to60) { | 873 TEST_F(MotionEventBufferTest, Resampling150to60) { |
| 863 base::TimeDelta flush_time_delta = | 874 base::TimeDelta flush_time_delta = |
| 864 base::TimeDelta::FromMillisecondsD(1000. / 60.); | 875 base::TimeDelta::FromMillisecondsD(1000. / 60.); |
| 865 base::TimeDelta event_time_delta = | 876 base::TimeDelta event_time_delta = |
| 866 base::TimeDelta::FromMillisecondsD(1000. / 150.); | 877 base::TimeDelta::FromMillisecondsD(1000. / 150.); |
| 867 | 878 |
| 868 RunResample(flush_time_delta, event_time_delta); | 879 RunResample(flush_time_delta, event_time_delta); |
| 869 } | 880 } |
| 870 | 881 |
| 871 } // namespace ui | 882 } // namespace ui |
| OLD | NEW |