1 // Copyright 2013 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. 4 5 #include <vector> 6 7 #include "base/basictypes.h" 8 #include "base/logging.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/time/time.h" 12 #include "content/browser/renderer_host/input/gesture_event_filter.h" 13 #include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h" 14 #include "content/common/input/synthetic_web_input_event_builders.h" 15 #include "content/port/common/input_event_ack_state.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "third_party/WebKit/public/web/WebInputEvent.h" 18 19 using base::TimeDelta; 20 using blink::WebGestureEvent; 21 using blink::WebInputEvent; 22 23 namespace content { 24 25 class GestureEventFilterTest : public testing::Test, 26 public GestureEventFilterClient, 27 public TouchpadTapSuppressionControllerClient { 28 public: 29 GestureEventFilterTest() 30 : acked_gesture_event_count_(0), 31 sent_gesture_event_count_(0) {} 32 33 virtual ~GestureEventFilterTest() {} 34 35 // testing::Test 36 virtual void SetUp() OVERRIDE { 37 filter_.reset(new GestureEventFilter(this, this)); 38 } 39 40 virtual void TearDown() OVERRIDE { 41 // Process all pending tasks to avoid leaks. 42 RunUntilIdle(); 43 filter_.reset(); 44 } 45 46 // GestureEventFilterClient 47 virtual void SendGestureEventImmediately( 48 const GestureEventWithLatencyInfo& event) OVERRIDE { 49 ++sent_gesture_event_count_; 50 if (sync_ack_result_) { 51 scoped_ptr<InputEventAckState> ack_result = sync_ack_result_.Pass(); 52 SendInputEventACK(event.event.type, *ack_result); 53 } 54 } 55 56 virtual void OnGestureEventAck( 57 const GestureEventWithLatencyInfo& event, 58 InputEventAckState ack_result) OVERRIDE { 59 ++acked_gesture_event_count_; 60 last_acked_event_ = event.event; 61 if (sync_followup_event_) 62 SimulateGestureEvent(*sync_followup_event_.Pass()); 63 } 64 65 // TouchpadTapSuppressionControllerClient 66 virtual void SendMouseEventImmediately( 67 const MouseEventWithLatencyInfo& event) OVERRIDE { 68 } 69 70 protected: 71 72 // Returns the result of |GestureEventFilter::ShouldForward()|. 73 bool SimulateGestureEvent(const WebGestureEvent& gesture) { 74 GestureEventWithLatencyInfo gesture_with_latency(gesture, 75 ui::LatencyInfo()); 76 if (filter()->ShouldForward(gesture_with_latency)) { 77 SendGestureEventImmediately(gesture_with_latency); 78 return true; 79 } 80 return false; 81 } 82 83 void SimulateGestureEvent(WebInputEvent::Type type, 84 WebGestureEvent::SourceDevice sourceDevice) { 85 SimulateGestureEvent( 86 SyntheticWebGestureEventBuilder::Build(type, sourceDevice)); 87 } 88 89 void SimulateGestureScrollUpdateEvent(float dX, float dY, int modifiers) { 90 SimulateGestureEvent( 91 SyntheticWebGestureEventBuilder::BuildScrollUpdate(dX, dY, modifiers)); 92 } 93 94 void SimulateGesturePinchUpdateEvent(float scale, 95 float anchorX, 96 float anchorY, 97 int modifiers) { 98 SimulateGestureEvent( 99 SyntheticWebGestureEventBuilder::BuildPinchUpdate(scale, 100 anchorX, 101 anchorY, 102 modifiers)); 103 } 104 105 void SimulateGestureFlingStartEvent( 106 float velocityX, 107 float velocityY, 108 WebGestureEvent::SourceDevice sourceDevice) { 109 SimulateGestureEvent( 110 SyntheticWebGestureEventBuilder::BuildFling(velocityX, 111 velocityY, 112 sourceDevice)); 113 } 114 115 void SendInputEventACK(WebInputEvent::Type type, 116 InputEventAckState ack) { 117 filter()->ProcessGestureAck(ack, type, ui::LatencyInfo()); 118 } 119 120 void RunUntilIdle() { 121 base::MessageLoop::current()->RunUntilIdle(); 122 } 123 124 size_t GetAndResetSentGestureEventCount() { 125 size_t count = sent_gesture_event_count_; 126 sent_gesture_event_count_ = 0; 127 return count; 128 } 129 130 size_t GetAndResetAckedGestureEventCount() { 131 size_t count = acked_gesture_event_count_; 132 acked_gesture_event_count_ = 0; 133 return count; 134 } 135 136 const WebGestureEvent& last_acked_event() const { 137 return last_acked_event_; 138 } 139 140 void DisableDebounce() { 141 filter()->set_debounce_enabled_for_testing(false); 142 } 143 144 void set_debounce_interval_time_ms(int ms) { 145 filter()->set_debounce_interval_time_ms_for_testing(ms); 146 } 147 148 void set_synchronous_ack(InputEventAckState ack_result) { 149 sync_ack_result_.reset(new InputEventAckState(ack_result)); 150 } 151 152 void set_sync_followup_event(WebInputEvent::Type type, 153 WebGestureEvent::SourceDevice sourceDevice) { 154 sync_followup_event_.reset(new WebGestureEvent( 155 SyntheticWebGestureEventBuilder::Build(type, sourceDevice))); 156 } 157 158 unsigned GestureEventQueueSize() { 159 return filter()->coalesced_gesture_events_.size(); 160 } 161 162 WebGestureEvent GestureEventSecondFromLastQueueEvent() { 163 return filter()->coalesced_gesture_events_.at( 164 GestureEventQueueSize() - 2).event; 165 } 166 167 WebGestureEvent GestureEventLastQueueEvent() { 168 return filter()->coalesced_gesture_events_.back().event; 169 } 170 171 unsigned GestureEventDebouncingQueueSize() { 172 return filter()->debouncing_deferral_queue_.size(); 173 } 174 175 WebGestureEvent GestureEventQueueEventAt(int i) { 176 return filter()->coalesced_gesture_events_.at(i).event; 177 } 178 179 bool ScrollingInProgress() { 180 return filter()->scrolling_in_progress_; 181 } 182 183 bool FlingInProgress() { 184 return filter()->fling_in_progress_; 185 } 186 187 bool WillIgnoreNextACK() { 188 return filter()->ignore_next_ack_; 189 } 190 191 GestureEventFilter* filter() const { 192 return filter_.get(); 193 } 194 195 private: 196 scoped_ptr<GestureEventFilter> filter_; 197 size_t acked_gesture_event_count_; 198 size_t sent_gesture_event_count_; 199 WebGestureEvent last_acked_event_; 200 scoped_ptr<InputEventAckState> sync_ack_result_; 201 scoped_ptr<WebGestureEvent> sync_followup_event_; 202 base::MessageLoopForUI message_loop_; 203 }; 204 205 #if GTEST_HAS_PARAM_TEST 206 // This is for tests that are to be run for all source devices. 207 class GestureEventFilterWithSourceTest 208 : public GestureEventFilterTest, 209 public testing::WithParamInterface<WebGestureEvent::SourceDevice> { 210 }; 211 #endif // GTEST_HAS_PARAM_TEST 212 213 TEST_F(GestureEventFilterTest, CoalescesScrollGestureEvents) { 214 // Turn off debounce handling for test isolation. 215 DisableDebounce(); 216 217 // Test coalescing of only GestureScrollUpdate events. 218 // Simulate gesture events. 219 220 // Sent. 221 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 222 WebGestureEvent::Touchscreen); 223 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 224 225 // Enqueued. 226 SimulateGestureScrollUpdateEvent(8, -5, 0); 227 228 // Make sure that the queue contains what we think it should. 229 WebGestureEvent merged_event = GestureEventLastQueueEvent(); 230 EXPECT_EQ(2U, GestureEventQueueSize()); 231 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 232 233 // Coalesced. 234 SimulateGestureScrollUpdateEvent(8, -6, 0); 235 236 // Check that coalescing updated the correct values. 237 merged_event = GestureEventLastQueueEvent(); 238 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 239 EXPECT_EQ(0, merged_event.modifiers); 240 EXPECT_EQ(16, merged_event.data.scrollUpdate.deltaX); 241 EXPECT_EQ(-11, merged_event.data.scrollUpdate.deltaY); 242 243 // Enqueued. 244 SimulateGestureScrollUpdateEvent(8, -7, 1); 245 246 // Check that we didn't wrongly coalesce. 247 merged_event = GestureEventLastQueueEvent(); 248 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 249 EXPECT_EQ(1, merged_event.modifiers); 250 251 // Different. 252 SimulateGestureEvent(WebInputEvent::GestureScrollEnd, 253 WebGestureEvent::Touchscreen); 254 255 // Check that only the first event was sent. 256 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 257 258 // Check that the ACK sends the second message. 259 SendInputEventACK(WebInputEvent::GestureScrollBegin, 260 INPUT_EVENT_ACK_STATE_CONSUMED); 261 RunUntilIdle(); 262 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 263 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 264 265 // Ack for queued coalesced event. 266 SendInputEventACK(WebInputEvent::GestureScrollUpdate, 267 INPUT_EVENT_ACK_STATE_CONSUMED); 268 RunUntilIdle(); 269 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 270 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 271 272 // Ack for queued uncoalesced event. 273 SendInputEventACK(WebInputEvent::GestureScrollUpdate, 274 INPUT_EVENT_ACK_STATE_CONSUMED); 275 RunUntilIdle(); 276 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 277 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 278 279 // After the final ack, the queue should be empty. 280 SendInputEventACK(WebInputEvent::GestureScrollEnd, 281 INPUT_EVENT_ACK_STATE_CONSUMED); 282 RunUntilIdle(); 283 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 284 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 285 } 286 287 TEST_F(GestureEventFilterTest, CoalescesScrollAndPinchEvents) { 288 // Turn off debounce handling for test isolation. 289 DisableDebounce(); 290 291 // Test coalescing of only GestureScrollUpdate events. 292 // Simulate gesture events. 293 294 // Sent. 295 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 296 WebGestureEvent::Touchscreen); 297 298 // Sent. 299 SimulateGestureEvent(WebInputEvent::GesturePinchBegin, 300 WebGestureEvent::Touchscreen); 301 302 // Enqueued. 303 SimulateGestureScrollUpdateEvent(8, -4, 1); 304 305 // Make sure that the queue contains what we think it should. 306 WebGestureEvent merged_event = GestureEventLastQueueEvent(); 307 EXPECT_EQ(3U, GestureEventQueueSize()); 308 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 309 310 // Coalesced without changing event order. Note anchor at (60, 60). Anchoring 311 // from a point that is not the origin should still give us the right scroll. 312 SimulateGesturePinchUpdateEvent(1.5, 60, 60, 1); 313 EXPECT_EQ(4U, GestureEventQueueSize()); 314 merged_event = GestureEventLastQueueEvent(); 315 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 316 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 317 EXPECT_EQ(1, merged_event.modifiers); 318 merged_event = GestureEventSecondFromLastQueueEvent(); 319 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 320 EXPECT_EQ(8, merged_event.data.scrollUpdate.deltaX); 321 EXPECT_EQ(-4, merged_event.data.scrollUpdate.deltaY); 322 EXPECT_EQ(1, merged_event.modifiers); 323 324 // Enqueued. 325 SimulateGestureScrollUpdateEvent(6, -3, 1); 326 327 // Check whether coalesced correctly. 328 EXPECT_EQ(4U, GestureEventQueueSize()); 329 merged_event = GestureEventLastQueueEvent(); 330 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 331 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 332 EXPECT_EQ(1, merged_event.modifiers); 333 merged_event = GestureEventSecondFromLastQueueEvent(); 334 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 335 EXPECT_EQ(12, merged_event.data.scrollUpdate.deltaX); 336 EXPECT_EQ(-6, merged_event.data.scrollUpdate.deltaY); 337 EXPECT_EQ(1, merged_event.modifiers); 338 339 // Enqueued. 340 SimulateGesturePinchUpdateEvent(2, 60, 60, 1); 341 342 // Check whether coalesced correctly. 343 EXPECT_EQ(4U, GestureEventQueueSize()); 344 merged_event = GestureEventLastQueueEvent(); 345 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 346 EXPECT_EQ(3, merged_event.data.pinchUpdate.scale); 347 EXPECT_EQ(1, merged_event.modifiers); 348 merged_event = GestureEventSecondFromLastQueueEvent(); 349 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 350 EXPECT_EQ(12, merged_event.data.scrollUpdate.deltaX); 351 EXPECT_EQ(-6, merged_event.data.scrollUpdate.deltaY); 352 EXPECT_EQ(1, merged_event.modifiers); 353 354 // Enqueued. 355 SimulateGesturePinchUpdateEvent(2, 60, 60, 1); 356 357 // Check whether coalesced correctly. 358 EXPECT_EQ(4U, GestureEventQueueSize()); 359 merged_event = GestureEventLastQueueEvent(); 360 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 361 EXPECT_EQ(6, merged_event.data.pinchUpdate.scale); 362 EXPECT_EQ(1, merged_event.modifiers); 363 merged_event = GestureEventSecondFromLastQueueEvent(); 364 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 365 EXPECT_EQ(12, merged_event.data.scrollUpdate.deltaX); 366 EXPECT_EQ(-6, merged_event.data.scrollUpdate.deltaY); 367 EXPECT_EQ(1, merged_event.modifiers); 368 369 // Check that only the first event was sent. 370 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 371 372 // Check that the ACK sends the second message. 373 SendInputEventACK(WebInputEvent::GestureScrollBegin, 374 INPUT_EVENT_ACK_STATE_CONSUMED); 375 RunUntilIdle(); 376 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 377 378 // Enqueued. 379 SimulateGestureScrollUpdateEvent(6, -6, 1); 380 381 // Check whether coalesced correctly. 382 EXPECT_EQ(3U, GestureEventQueueSize()); 383 merged_event = GestureEventLastQueueEvent(); 384 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 385 EXPECT_EQ(6, merged_event.data.pinchUpdate.scale); 386 EXPECT_EQ(1, merged_event.modifiers); 387 merged_event = GestureEventSecondFromLastQueueEvent(); 388 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 389 EXPECT_EQ(13, merged_event.data.scrollUpdate.deltaX); 390 EXPECT_EQ(-7, merged_event.data.scrollUpdate.deltaY); 391 EXPECT_EQ(1, merged_event.modifiers); 392 393 // At this point ACKs shouldn't be getting ignored. 394 EXPECT_FALSE(WillIgnoreNextACK()); 395 396 // Check that the ACK sends both scroll and pinch updates. 397 SendInputEventACK(WebInputEvent::GesturePinchBegin, 398 INPUT_EVENT_ACK_STATE_CONSUMED); 399 RunUntilIdle(); 400 EXPECT_EQ(2U, GetAndResetSentGestureEventCount()); 401 402 // The next ACK should be getting ignored. 403 EXPECT_TRUE(WillIgnoreNextACK()); 404 405 // Enqueued. 406 SimulateGestureScrollUpdateEvent(1, -1, 1); 407 408 // Check whether coalesced correctly. 409 EXPECT_EQ(3U, GestureEventQueueSize()); 410 merged_event = GestureEventLastQueueEvent(); 411 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 412 EXPECT_EQ(1, merged_event.data.scrollUpdate.deltaX); 413 EXPECT_EQ(-1, merged_event.data.scrollUpdate.deltaY); 414 EXPECT_EQ(1, merged_event.modifiers); 415 merged_event = GestureEventSecondFromLastQueueEvent(); 416 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 417 EXPECT_EQ(6, merged_event.data.pinchUpdate.scale); 418 EXPECT_EQ(1, merged_event.modifiers); 419 420 // Enqueued. 421 SimulateGestureScrollUpdateEvent(2, -2, 1); 422 423 // Coalescing scrolls should still work. 424 EXPECT_EQ(3U, GestureEventQueueSize()); 425 merged_event = GestureEventLastQueueEvent(); 426 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 427 EXPECT_EQ(3, merged_event.data.scrollUpdate.deltaX); 428 EXPECT_EQ(-3, merged_event.data.scrollUpdate.deltaY); 429 EXPECT_EQ(1, merged_event.modifiers); 430 merged_event = GestureEventSecondFromLastQueueEvent(); 431 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 432 EXPECT_EQ(6, merged_event.data.pinchUpdate.scale); 433 EXPECT_EQ(1, merged_event.modifiers); 434 435 // Enqueued. 436 SimulateGesturePinchUpdateEvent(0.5, 60, 60, 1); 437 438 // Check whether coalesced correctly. 439 EXPECT_EQ(4U, GestureEventQueueSize()); 440 merged_event = GestureEventLastQueueEvent(); 441 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 442 EXPECT_EQ(0.5, merged_event.data.pinchUpdate.scale); 443 EXPECT_EQ(1, merged_event.modifiers); 444 merged_event = GestureEventSecondFromLastQueueEvent(); 445 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 446 EXPECT_EQ(3, merged_event.data.scrollUpdate.deltaX); 447 EXPECT_EQ(-3, merged_event.data.scrollUpdate.deltaY); 448 EXPECT_EQ(1, merged_event.modifiers); 449 450 // Check that the ACK gets ignored. 451 SendInputEventACK(WebInputEvent::GestureScrollUpdate, 452 INPUT_EVENT_ACK_STATE_CONSUMED); 453 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, last_acked_event().type); 454 RunUntilIdle(); 455 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 456 // The flag should have been flipped back to false. 457 EXPECT_FALSE(WillIgnoreNextACK()); 458 459 // Enqueued. 460 SimulateGestureScrollUpdateEvent(2, -2, 2); 461 462 // Shouldn't coalesce with different modifiers. 463 EXPECT_EQ(4U, GestureEventQueueSize()); 464 merged_event = GestureEventLastQueueEvent(); 465 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 466 EXPECT_EQ(2, merged_event.data.scrollUpdate.deltaX); 467 EXPECT_EQ(-2, merged_event.data.scrollUpdate.deltaY); 468 EXPECT_EQ(2, merged_event.modifiers); 469 merged_event = GestureEventSecondFromLastQueueEvent(); 470 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 471 EXPECT_EQ(0.5, merged_event.data.pinchUpdate.scale); 472 EXPECT_EQ(1, merged_event.modifiers); 473 474 // Check that the ACK sends the next scroll pinch pair. 475 SendInputEventACK(WebInputEvent::GesturePinchUpdate, 476 INPUT_EVENT_ACK_STATE_CONSUMED); 477 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, last_acked_event().type); 478 RunUntilIdle(); 479 EXPECT_EQ(2U, GetAndResetSentGestureEventCount()); 480 481 // Check that the ACK sends the second message. 482 SendInputEventACK(WebInputEvent::GestureScrollUpdate, 483 INPUT_EVENT_ACK_STATE_CONSUMED); 484 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, last_acked_event().type); 485 RunUntilIdle(); 486 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 487 488 // Check that the ACK sends the second event. 489 SendInputEventACK(WebInputEvent::GesturePinchUpdate, 490 INPUT_EVENT_ACK_STATE_CONSUMED); 491 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, last_acked_event().type); 492 RunUntilIdle(); 493 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 494 495 // Check that the queue is empty after ACK and no events get sent. 496 SendInputEventACK(WebInputEvent::GestureScrollUpdate, 497 INPUT_EVENT_ACK_STATE_CONSUMED); 498 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, last_acked_event().type); 499 RunUntilIdle(); 500 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 501 EXPECT_EQ(0U, GestureEventQueueSize()); 502 } 503 504 TEST_F(GestureEventFilterTest, CoalescesMultiplePinchEventSequences) { 505 // Turn off debounce handling for test isolation. 506 DisableDebounce(); 507 508 // Simulate a pinch sequence. 509 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 510 WebGestureEvent::Touchscreen); 511 SimulateGestureEvent(WebInputEvent::GesturePinchBegin, 512 WebGestureEvent::Touchscreen); 513 514 SimulateGestureScrollUpdateEvent(8, -4, 1); 515 // Make sure that the queue contains what we think it should. 516 WebGestureEvent merged_event = GestureEventLastQueueEvent(); 517 size_t expected_events_in_queue = 3; 518 EXPECT_EQ(expected_events_in_queue, GestureEventQueueSize()); 519 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 520 521 // Coalesced without changing event order. Note anchor at (60, 60). Anchoring 522 // from a point that is not the origin should still give us the right scroll. 523 SimulateGesturePinchUpdateEvent(1.5, 60, 60, 1); 524 EXPECT_EQ(++expected_events_in_queue, GestureEventQueueSize()); 525 merged_event = GestureEventLastQueueEvent(); 526 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 527 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 528 EXPECT_EQ(1, merged_event.modifiers); 529 merged_event = GestureEventSecondFromLastQueueEvent(); 530 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 531 EXPECT_EQ(8, merged_event.data.scrollUpdate.deltaX); 532 EXPECT_EQ(-4, merged_event.data.scrollUpdate.deltaY); 533 EXPECT_EQ(1, merged_event.modifiers); 534 535 // Enqueued. 536 SimulateGestureScrollUpdateEvent(6, -3, 1); 537 538 // Check whether coalesced correctly. 539 EXPECT_EQ(expected_events_in_queue, GestureEventQueueSize()); 540 merged_event = GestureEventLastQueueEvent(); 541 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 542 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 543 EXPECT_EQ(1, merged_event.modifiers); 544 merged_event = GestureEventSecondFromLastQueueEvent(); 545 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 546 EXPECT_EQ(12, merged_event.data.scrollUpdate.deltaX); 547 EXPECT_EQ(-6, merged_event.data.scrollUpdate.deltaY); 548 EXPECT_EQ(1, merged_event.modifiers); 549 550 // Now start another sequence before the previous sequence has been ack'ed. 551 SimulateGestureEvent(WebInputEvent::GesturePinchEnd, 552 WebGestureEvent::Touchscreen); 553 SimulateGestureEvent(WebInputEvent::GestureScrollEnd, 554 WebGestureEvent::Touchscreen); 555 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 556 WebGestureEvent::Touchscreen); 557 SimulateGestureEvent(WebInputEvent::GesturePinchBegin, 558 WebGestureEvent::Touchscreen); 559 560 SimulateGestureScrollUpdateEvent(8, -4, 1); 561 // Make sure that the queue contains what we think it should. 562 expected_events_in_queue += 5; 563 merged_event = GestureEventLastQueueEvent(); 564 EXPECT_EQ(expected_events_in_queue, GestureEventQueueSize()); 565 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 566 567 // Coalesced without changing event order. Note anchor at (60, 60). Anchoring 568 // from a point that is not the origin should still give us the right scroll. 569 SimulateGesturePinchUpdateEvent(1.5, 30, 30, 1); 570 EXPECT_EQ(++expected_events_in_queue, GestureEventQueueSize()); 571 merged_event = GestureEventLastQueueEvent(); 572 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 573 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 574 EXPECT_EQ(1, merged_event.modifiers); 575 merged_event = GestureEventSecondFromLastQueueEvent(); 576 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 577 EXPECT_EQ(8, merged_event.data.scrollUpdate.deltaX); 578 EXPECT_EQ(-4, merged_event.data.scrollUpdate.deltaY); 579 EXPECT_EQ(1, merged_event.modifiers); 580 581 // Enqueued. 582 SimulateGestureScrollUpdateEvent(6, -3, 1); 583 584 // Check whether coalesced correctly. 585 EXPECT_EQ(expected_events_in_queue, GestureEventQueueSize()); 586 merged_event = GestureEventLastQueueEvent(); 587 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, merged_event.type); 588 EXPECT_EQ(1.5, merged_event.data.pinchUpdate.scale); 589 EXPECT_EQ(1, merged_event.modifiers); 590 merged_event = GestureEventSecondFromLastQueueEvent(); 591 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 592 EXPECT_EQ(12, merged_event.data.scrollUpdate.deltaX); 593 EXPECT_EQ(-6, merged_event.data.scrollUpdate.deltaY); 594 EXPECT_EQ(1, merged_event.modifiers); 595 } 596 597 // Tests a single event with an synchronous ack. 598 TEST_F(GestureEventFilterTest, SimpleSyncAck) { 599 set_synchronous_ack(INPUT_EVENT_ACK_STATE_CONSUMED); 600 SimulateGestureEvent(WebInputEvent::GestureTapDown, 601 WebGestureEvent::Touchscreen); 602 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 603 EXPECT_EQ(0U, GestureEventQueueSize()); 604 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 605 } 606 607 // Tests an event with an synchronous ack which enqueues an additional event. 608 TEST_F(GestureEventFilterTest, SyncAckQueuesEvent) { 609 scoped_ptr<WebGestureEvent> queued_event; 610 set_synchronous_ack(INPUT_EVENT_ACK_STATE_CONSUMED); 611 set_sync_followup_event(WebInputEvent::GestureShowPress, 612 WebGestureEvent::Touchscreen); 613 // This event enqueues the show press event. 614 SimulateGestureEvent(WebInputEvent::GestureTapDown, 615 WebGestureEvent::Touchscreen); 616 EXPECT_EQ(2U, GetAndResetSentGestureEventCount()); 617 EXPECT_EQ(1U, GestureEventQueueSize()); 618 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 619 620 SendInputEventACK(WebInputEvent::GestureShowPress, 621 INPUT_EVENT_ACK_STATE_CONSUMED); 622 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 623 EXPECT_EQ(0U, GestureEventQueueSize()); 624 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 625 } 626 627 // Tests an event with an async ack followed by an event with a sync ack. 628 TEST_F(GestureEventFilterTest, AsyncThenSyncAck) { 629 // Turn off debounce handling for test isolation. 630 DisableDebounce(); 631 632 SimulateGestureEvent(WebInputEvent::GestureTapDown, 633 WebGestureEvent::Touchscreen); 634 635 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 636 EXPECT_EQ(1U, GestureEventQueueSize()); 637 EXPECT_EQ(0U, GetAndResetAckedGestureEventCount()); 638 639 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 640 WebGestureEvent::Touchscreen); 641 set_synchronous_ack(INPUT_EVENT_ACK_STATE_CONSUMED); 642 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 643 EXPECT_EQ(2U, GestureEventQueueSize()); 644 EXPECT_EQ(0U, GetAndResetAckedGestureEventCount()); 645 646 SendInputEventACK(WebInputEvent::GestureTapDown, 647 INPUT_EVENT_ACK_STATE_CONSUMED); 648 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 649 EXPECT_EQ(0U, GestureEventQueueSize()); 650 EXPECT_EQ(2U, GetAndResetAckedGestureEventCount()); 651 } 652 653 TEST_F(GestureEventFilterTest, CoalescesScrollAndPinchEventWithSyncAck) { 654 // Turn off debounce handling for test isolation. 655 DisableDebounce(); 656 657 // Simulate a pinch sequence. 658 SimulateGestureEvent(WebInputEvent::GestureScrollBegin, 659 WebGestureEvent::Touchscreen); 660 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 661 SimulateGestureEvent(WebInputEvent::GesturePinchBegin, 662 WebGestureEvent::Touchscreen); 663 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 664 665 SimulateGestureScrollUpdateEvent(8, -4, 1); 666 // Make sure that the queue contains what we think it should. 667 WebGestureEvent merged_event = GestureEventLastQueueEvent(); 668 EXPECT_EQ(3U, GestureEventQueueSize()); 669 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, merged_event.type); 670 671 // Coalesced without changing event order. Note anchor at (60, 60). Anchoring 672 // from a point that is not the origin should still give us the right scroll. 673 SimulateGesturePinchUpdateEvent(1.5, 60, 60, 1); 674 EXPECT_EQ(4U, GestureEventQueueSize()); 675 676 SendInputEventACK(WebInputEvent::GestureScrollBegin, 677 INPUT_EVENT_ACK_STATE_CONSUMED); 678 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 679 EXPECT_EQ(3U, GestureEventQueueSize()); 680 681 // Ack the PinchBegin, and schedule a synchronous ack for GestureScrollUpdate. 682 set_synchronous_ack(INPUT_EVENT_ACK_STATE_CONSUMED); 683 SendInputEventACK(WebInputEvent::GesturePinchBegin, 684 INPUT_EVENT_ACK_STATE_CONSUMED); 685 686 // Both GestureScrollUpdate and GesturePinchUpdate should have been sent. 687 EXPECT_EQ(WebInputEvent::GestureScrollUpdate, last_acked_event().type); 688 EXPECT_EQ(1U, GestureEventQueueSize()); 689 EXPECT_EQ(2U, GetAndResetSentGestureEventCount()); 690 691 // Ack the final GesturePinchUpdate. 692 SendInputEventACK(WebInputEvent::GesturePinchUpdate, 693 INPUT_EVENT_ACK_STATE_CONSUMED); 694 EXPECT_EQ(WebInputEvent::GesturePinchUpdate, last_acked_event().type); 695 EXPECT_EQ(0U, GestureEventQueueSize()); 696 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 697 } 698 699 #if GTEST_HAS_PARAM_TEST 700 TEST_P(GestureEventFilterWithSourceTest, GestureFlingCancelsFiltered) { 701 WebGestureEvent::SourceDevice source_device = GetParam(); 702 703 // Turn off debounce handling for test isolation. 704 DisableDebounce(); 705 // GFC without previous GFS is dropped. 706 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 707 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 708 EXPECT_EQ(0U, GestureEventQueueSize()); 709 710 // GFC after previous GFS is dispatched and acked. 711 SimulateGestureFlingStartEvent(0, -10, source_device); 712 EXPECT_TRUE(FlingInProgress()); 713 SendInputEventACK(WebInputEvent::GestureFlingStart, 714 INPUT_EVENT_ACK_STATE_CONSUMED); 715 RunUntilIdle(); 716 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 717 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 718 EXPECT_FALSE(FlingInProgress()); 719 EXPECT_EQ(2U, GetAndResetSentGestureEventCount()); 720 SendInputEventACK(WebInputEvent::GestureFlingCancel, 721 INPUT_EVENT_ACK_STATE_CONSUMED); 722 RunUntilIdle(); 723 EXPECT_EQ(1U, GetAndResetAckedGestureEventCount()); 724 EXPECT_EQ(0U, GestureEventQueueSize()); 725 726 // GFC before previous GFS is acked. 727 SimulateGestureFlingStartEvent(0, -10, source_device); 728 EXPECT_TRUE(FlingInProgress()); 729 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 730 EXPECT_FALSE(FlingInProgress()); 731 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 732 EXPECT_EQ(2U, GestureEventQueueSize()); 733 734 // Advance state realistically. 735 SendInputEventACK(WebInputEvent::GestureFlingStart, 736 INPUT_EVENT_ACK_STATE_CONSUMED); 737 RunUntilIdle(); 738 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 739 SendInputEventACK(WebInputEvent::GestureFlingCancel, 740 INPUT_EVENT_ACK_STATE_CONSUMED); 741 RunUntilIdle(); 742 EXPECT_EQ(2U, GetAndResetAckedGestureEventCount()); 743 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 744 EXPECT_EQ(0U, GestureEventQueueSize()); 745 746 // GFS is added to the queue if another event is pending 747 SimulateGestureScrollUpdateEvent(8, -7, 0); 748 SimulateGestureFlingStartEvent(0, -10, source_device); 749 EXPECT_EQ(2U, GestureEventQueueSize()); 750 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 751 WebGestureEvent merged_event = GestureEventLastQueueEvent(); 752 EXPECT_EQ(WebInputEvent::GestureFlingStart, merged_event.type); 753 EXPECT_TRUE(FlingInProgress()); 754 EXPECT_EQ(2U, GestureEventQueueSize()); 755 756 // GFS in queue means that a GFC is added to the queue 757 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 758 merged_event =GestureEventLastQueueEvent(); 759 EXPECT_EQ(WebInputEvent::GestureFlingCancel, merged_event.type); 760 EXPECT_FALSE(FlingInProgress()); 761 EXPECT_EQ(3U, GestureEventQueueSize()); 762 763 // Adding a second GFC is dropped. 764 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 765 EXPECT_FALSE(FlingInProgress()); 766 EXPECT_EQ(3U, GestureEventQueueSize()); 767 768 // Adding another GFS will add it to the queue. 769 SimulateGestureFlingStartEvent(0, -10, source_device); 770 merged_event = GestureEventLastQueueEvent(); 771 EXPECT_EQ(WebInputEvent::GestureFlingStart, merged_event.type); 772 EXPECT_TRUE(FlingInProgress()); 773 EXPECT_EQ(4U, GestureEventQueueSize()); 774 775 // GFS in queue means that a GFC is added to the queue 776 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 777 merged_event = GestureEventLastQueueEvent(); 778 EXPECT_EQ(WebInputEvent::GestureFlingCancel, merged_event.type); 779 EXPECT_FALSE(FlingInProgress()); 780 EXPECT_EQ(5U, GestureEventQueueSize()); 781 782 // Adding another GFC with a GFC already there is dropped. 783 SimulateGestureEvent(WebInputEvent::GestureFlingCancel, source_device); 784 merged_event = GestureEventLastQueueEvent(); 785 EXPECT_EQ(WebInputEvent::GestureFlingCancel, merged_event.type); 786 EXPECT_FALSE(FlingInProgress()); 787 EXPECT_EQ(5U, GestureEventQueueSize()); 788 } 789 790 INSTANTIATE_TEST_CASE_P(AllSources, 791 GestureEventFilterWithSourceTest, 792 testing::Values(WebGestureEvent::Touchscreen, 793 WebGestureEvent::Touchpad)); 794 #endif // GTEST_HAS_PARAM_TEST 795 796 // Test that a GestureScrollEnd | GestureFlingStart are deferred during the 797 // debounce interval, that Scrolls are not and that the deferred events are 798 // sent after that timer fires. 799 TEST_F(GestureEventFilterTest, DebounceDefersFollowingGestureEvents) { 800 set_debounce_interval_time_ms(3); 801 802 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate, 803 WebGestureEvent::Touchscreen); 804 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 805 EXPECT_EQ(1U, GestureEventQueueSize()); 806 EXPECT_EQ(0U, GestureEventDebouncingQueueSize()); 807 EXPECT_TRUE(ScrollingInProgress()); 808 809 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate, 810 WebGestureEvent::Touchscreen); 811 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 812 EXPECT_EQ(2U, GestureEventQueueSize()); 813 EXPECT_EQ(0U, GestureEventDebouncingQueueSize()); 814 EXPECT_TRUE(ScrollingInProgress()); 815 816 SimulateGestureEvent(WebInputEvent::GestureScrollEnd, 817 WebGestureEvent::Touchscreen); 818 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 819 EXPECT_EQ(2U, GestureEventQueueSize()); 820 EXPECT_EQ(1U, GestureEventDebouncingQueueSize()); 821 822 SimulateGestureFlingStartEvent(0, 10, WebGestureEvent::Touchscreen); 823 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 824 EXPECT_EQ(2U, GestureEventQueueSize()); 825 EXPECT_EQ(2U, GestureEventDebouncingQueueSize()); 826 827 SimulateGestureEvent(WebInputEvent::GestureTapDown, 828 WebGestureEvent::Touchscreen); 829 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 830 EXPECT_EQ(2U, GestureEventQueueSize()); 831 EXPECT_EQ(3U, GestureEventDebouncingQueueSize()); 832 833 base::MessageLoop::current()->PostDelayedTask( 834 FROM_HERE, 835 base::MessageLoop::QuitClosure(), 836 TimeDelta::FromMilliseconds(5)); 837 base::MessageLoop::current()->Run(); 838 839 // The deferred events are correctly queued in coalescing queue. 840 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 841 EXPECT_EQ(5U, GestureEventQueueSize()); 842 EXPECT_EQ(0U, GestureEventDebouncingQueueSize()); 843 EXPECT_FALSE(ScrollingInProgress()); 844 845 // Verify that the coalescing queue contains the correct events. 846 WebInputEvent::Type expected[] = { 847 WebInputEvent::GestureScrollUpdate, 848 WebInputEvent::GestureScrollUpdate, 849 WebInputEvent::GestureScrollEnd, 850 WebInputEvent::GestureFlingStart}; 851 852 for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type); 853 i++) { 854 WebGestureEvent merged_event = GestureEventQueueEventAt(i); 855 EXPECT_EQ(expected[i], merged_event.type); 856 } 857 } 858 859 // Test that non-scroll events are deferred while scrolling during the debounce 860 // interval and are discarded if a GestureScrollUpdate event arrives before the 861 // interval end. 862 TEST_F(GestureEventFilterTest, DebounceDropsDeferredEvents) { 863 set_debounce_interval_time_ms(3); 864 EXPECT_FALSE(ScrollingInProgress()); 865 866 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate, 867 WebGestureEvent::Touchscreen); 868 EXPECT_EQ(1U, GetAndResetSentGestureEventCount()); 869 EXPECT_EQ(1U, GestureEventQueueSize()); 870 EXPECT_EQ(0U, GestureEventDebouncingQueueSize()); 871 EXPECT_TRUE(ScrollingInProgress()); 872 873 // This event should get discarded. 874 SimulateGestureEvent(WebInputEvent::GestureScrollEnd, 875 WebGestureEvent::Touchscreen); 876 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 877 EXPECT_EQ(1U, GestureEventQueueSize()); 878 EXPECT_EQ(1U, GestureEventDebouncingQueueSize()); 879 880 SimulateGestureEvent(WebInputEvent::GestureScrollUpdate, 881 WebGestureEvent::Touchscreen); 882 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 883 EXPECT_EQ(2U, GestureEventQueueSize()); 884 EXPECT_EQ(0U, GestureEventDebouncingQueueSize()); 885 EXPECT_TRUE(ScrollingInProgress()); 886 887 // Verify that the coalescing queue contains the correct events. 888 WebInputEvent::Type expected[] = { 889 WebInputEvent::GestureScrollUpdate, 890 WebInputEvent::GestureScrollUpdate}; 891 892 for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type); 893 i++) { 894 WebGestureEvent merged_event = GestureEventQueueEventAt(i); 895 EXPECT_EQ(expected[i], merged_event.type); 896 } 897 } 898 899 TEST_F(GestureEventFilterTest, DropZeroVelocityFlings) { 900 WebGestureEvent gesture_event; 901 gesture_event.type = WebInputEvent::GestureFlingStart; 902 gesture_event.sourceDevice = WebGestureEvent::Touchpad; 903 gesture_event.data.flingStart.velocityX = 0.f; 904 gesture_event.data.flingStart.velocityY = 0.f; 905 ASSERT_EQ(0U, GetAndResetSentGestureEventCount()); 906 ASSERT_EQ(0U, GestureEventQueueSize()); 907 EXPECT_FALSE(SimulateGestureEvent(gesture_event)); 908 EXPECT_EQ(0U, GetAndResetSentGestureEventCount()); 909 EXPECT_EQ(0U, GestureEventQueueSize()); 910 } 911 912 } // namespace content 913