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

Side by Side Diff: content/browser/renderer_host/input/touch_event_queue_unittest.cc

Issue 521453002: Defer flushing the touch queue when all touch handlers removed (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Aura unit test fix Created 6 years, 3 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 2013 The Chromium Authors. All rights reserved. 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 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "content/browser/renderer_host/input/timeout_monitor.h" 9 #include "content/browser/renderer_host/input/timeout_monitor.h"
10 #include "content/browser/renderer_host/input/touch_event_queue.h" 10 #include "content/browser/renderer_host/input/touch_event_queue.h"
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 278
279 // Receive an ACK for the second touch-event. 279 // Receive an ACK for the second touch-event.
280 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED); 280 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
281 EXPECT_EQ(0U, queued_event_count()); 281 EXPECT_EQ(0U, queued_event_count());
282 EXPECT_EQ(0U, GetAndResetSentEventCount()); 282 EXPECT_EQ(0U, GetAndResetSentEventCount());
283 EXPECT_EQ(1U, GetAndResetAckedEventCount()); 283 EXPECT_EQ(1U, GetAndResetAckedEventCount());
284 EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type); 284 EXPECT_EQ(WebInputEvent::TouchMove, acked_event().type);
285 EXPECT_TRUE(acked_event().cancelable); 285 EXPECT_TRUE(acked_event().cancelable);
286 } 286 }
287 287
288 // Tests that the touch-queue is emptied if a page stops listening for touch 288 // Tests that the touch-queue is emptied after the outstanding ack is received
289 // events. 289 // if a page stops listening for touch events.
290 TEST_F(TouchEventQueueTest, QueueFlushedWhenHandlersRemoved) { 290 TEST_F(TouchEventQueueTest, QueueFlushedOnAckAfterHandlersRemoved) {
291 OnHasTouchEventHandlers(true); 291 OnHasTouchEventHandlers(true);
292 EXPECT_EQ(0U, queued_event_count()); 292 EXPECT_EQ(0U, queued_event_count());
293 EXPECT_EQ(0U, GetAndResetSentEventCount()); 293 EXPECT_EQ(0U, GetAndResetSentEventCount());
294 294
295 // Send a touch-press event. 295 // Send a touch-press event.
296 PressTouchPoint(1, 1); 296 PressTouchPoint(1, 1);
297 EXPECT_EQ(1U, GetAndResetSentEventCount()); 297 EXPECT_EQ(1U, GetAndResetSentEventCount());
298 EXPECT_EQ(1U, queued_event_count());
298 299
300 // Signal that all touch handlers have been removed.
301 OnHasTouchEventHandlers(false);
302 EXPECT_EQ(0U, GetAndResetAckedEventCount());
303 EXPECT_EQ(1U, queued_event_count());
304
305 // Process the ack for the sent touch, ensuring that it is honored (despite
306 // the touch handler having been removed).
307 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
308 EXPECT_EQ(1U, GetAndResetAckedEventCount());
309 EXPECT_EQ(0U, queued_event_count());
310 EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, acked_event_state());
311
312 // The release should not be forwarded.
299 ReleaseTouchPoint(0); 313 ReleaseTouchPoint(0);
314 EXPECT_EQ(1U, GetAndResetAckedEventCount());
315 EXPECT_EQ(0U, queued_event_count());
316 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, acked_event_state());
317
318 OnHasTouchEventHandlers(true);
300 319
301 // Events will be queued until the first sent event is ack'ed. 320 // Events will be queued until the first sent event is ack'ed.
302 for (int i = 5; i < 15; ++i) { 321 for (int i = 0; i < 10; ++i) {
303 PressTouchPoint(1, 1); 322 PressTouchPoint(1, 1);
304 MoveTouchPoint(0, i, i); 323 MoveTouchPoint(0, i, i);
305 ReleaseTouchPoint(0); 324 ReleaseTouchPoint(0);
306 } 325 }
307 EXPECT_EQ(32U, queued_event_count()); 326 EXPECT_EQ(30U, queued_event_count());
327 EXPECT_EQ(1U, GetAndResetSentEventCount());
328
329 // Signal that all touch handlers have been removed. Note that flushing of
330 // the queue will not occur until *after* the outstanding ack is received.
331 OnHasTouchEventHandlers(false);
332 EXPECT_EQ(30U, queued_event_count());
308 EXPECT_EQ(0U, GetAndResetSentEventCount()); 333 EXPECT_EQ(0U, GetAndResetSentEventCount());
334 EXPECT_EQ(0U, GetAndResetAckedEventCount());
309 335
310 // Receive an ACK for the first touch-event. One of the queued touch-event 336 // Receive an ACK for the first touch-event. All remaining touch events should
311 // should be forwarded. 337 // be flushed with the appropriate ack type.
312 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED); 338 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
313 EXPECT_EQ(31U, queued_event_count());
314 EXPECT_EQ(1U, GetAndResetSentEventCount());
315 EXPECT_EQ(1U, GetAndResetAckedEventCount());
316 EXPECT_EQ(WebInputEvent::TouchStart, acked_event().type);
317
318 // Flush the queue. The touch-event queue should now be emptied, but none of
319 // the queued touch-events should be sent to the renderer.
320 OnHasTouchEventHandlers(false);
321 EXPECT_EQ(0U, queued_event_count()); 339 EXPECT_EQ(0U, queued_event_count());
322 EXPECT_EQ(0U, GetAndResetSentEventCount()); 340 EXPECT_EQ(0U, GetAndResetSentEventCount());
323 EXPECT_EQ(31U, GetAndResetAckedEventCount()); 341 EXPECT_EQ(30U, GetAndResetAckedEventCount());
342 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, acked_event_state());
324 } 343 }
325 344
326 // Tests that addition of a touch handler during a touch sequence will not cause 345 // Tests that addition of a touch handler during a touch sequence will not cause
327 // the remaining sequence to be forwarded. 346 // the remaining sequence to be forwarded.
328 TEST_F(TouchEventQueueTest, ActiveSequenceNotForwardedWhenHandlersAdded) { 347 TEST_F(TouchEventQueueTest, ActiveSequenceNotForwardedWhenHandlersAdded) {
329 OnHasTouchEventHandlers(false); 348 OnHasTouchEventHandlers(false);
330 349
331 // Send a touch-press event while there is no handler. 350 // Send a touch-press event while there is no handler.
332 PressTouchPoint(1, 1); 351 PressTouchPoint(1, 1);
333 EXPECT_EQ(1U, GetAndResetAckedEventCount()); 352 EXPECT_EQ(1U, GetAndResetAckedEventCount());
(...skipping 23 matching lines...) Expand all
357 PressTouchPoint(1, 1); 376 PressTouchPoint(1, 1);
358 EXPECT_EQ(1U, GetAndResetSentEventCount()); 377 EXPECT_EQ(1U, GetAndResetSentEventCount());
359 EXPECT_EQ(1U, queued_event_count()); 378 EXPECT_EQ(1U, queued_event_count());
360 379
361 // Queue a touch-move event. 380 // Queue a touch-move event.
362 MoveTouchPoint(0, 5, 5); 381 MoveTouchPoint(0, 5, 5);
363 EXPECT_EQ(2U, queued_event_count()); 382 EXPECT_EQ(2U, queued_event_count());
364 EXPECT_EQ(0U, GetAndResetAckedEventCount()); 383 EXPECT_EQ(0U, GetAndResetAckedEventCount());
365 EXPECT_EQ(0U, GetAndResetSentEventCount()); 384 EXPECT_EQ(0U, GetAndResetSentEventCount());
366 385
367 // Touch handle deregistration should flush the queue. 386 // Unregister all touch handlers.
368 OnHasTouchEventHandlers(false); 387 OnHasTouchEventHandlers(false);
388 EXPECT_EQ(0U, GetAndResetAckedEventCount());
389 EXPECT_EQ(2U, queued_event_count());
390
391 // The ack should be flush the queue.
392 SendTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
369 EXPECT_EQ(2U, GetAndResetAckedEventCount()); 393 EXPECT_EQ(2U, GetAndResetAckedEventCount());
370 EXPECT_EQ(0U, queued_event_count()); 394 EXPECT_EQ(0U, queued_event_count());
371 395
372 // The ack should be ignored as the touch queue is now empty.
373 SendTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
374 EXPECT_EQ(0U, GetAndResetAckedEventCount());
375 EXPECT_EQ(0U, queued_event_count());
376
377 // Events should be dropped while there is no touch handler. 396 // Events should be dropped while there is no touch handler.
378 MoveTouchPoint(0, 10, 10); 397 MoveTouchPoint(0, 10, 10);
379 EXPECT_EQ(0U, queued_event_count()); 398 EXPECT_EQ(0U, queued_event_count());
380 EXPECT_EQ(1U, GetAndResetAckedEventCount()); 399 EXPECT_EQ(1U, GetAndResetAckedEventCount());
381 EXPECT_EQ(0U, GetAndResetSentEventCount()); 400 EXPECT_EQ(0U, GetAndResetSentEventCount());
382 401
383 // Simulate touch handler registration in the middle of a touch sequence. 402 // Simulate touch handler registration in the middle of a touch sequence.
384 OnHasTouchEventHandlers(true); 403 OnHasTouchEventHandlers(true);
385 404
386 // The touch end for the interrupted sequence should be dropped. 405 // The touch end for the interrupted sequence should be dropped.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 MoveTouchPoint(1, 25, 25); 508 MoveTouchPoint(1, 25, 25);
490 EXPECT_EQ(4U, queued_event_count()); 509 EXPECT_EQ(4U, queued_event_count());
491 510
492 // Make sure both fingers are marked as having been moved in the coalesced 511 // Make sure both fingers are marked as having been moved in the coalesced
493 // event. 512 // event.
494 const WebTouchEvent& event = latest_event(); 513 const WebTouchEvent& event = latest_event();
495 EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[0].state); 514 EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[0].state);
496 EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[1].state); 515 EXPECT_EQ(WebTouchPoint::StateMoved, event.touches[1].state);
497 } 516 }
498 517
499 // Tests that if a touch-event queue is destroyed in response to a touch-event 518 // Tests that the touch-event queue is robust to redundant acks.
500 // in the renderer, then there is no crash when the ACK for that touch-event 519 TEST_F(TouchEventQueueTest, SpuriousAcksIgnored) {
501 // comes back. 520 // Trigger a spurious ack.
502 TEST_F(TouchEventQueueTest, AckAfterQueueFlushed) { 521 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
503 // Send some touch-events to the renderer. 522 EXPECT_EQ(0U, GetAndResetAckedEventCount());
523
524 // Send and ack a touch press.
504 PressTouchPoint(1, 1); 525 PressTouchPoint(1, 1);
505 EXPECT_EQ(1U, GetAndResetSentEventCount()); 526 EXPECT_EQ(1U, GetAndResetSentEventCount());
506 EXPECT_EQ(1U, queued_event_count()); 527 EXPECT_EQ(1U, queued_event_count());
507
508 MoveTouchPoint(0, 10, 10);
509 EXPECT_EQ(0U, GetAndResetSentEventCount());
510 EXPECT_EQ(2U, queued_event_count());
511
512 // Receive an ACK for the press. This should cause the queued touch-move to
513 // be sent to the renderer.
514 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED); 528 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
515 EXPECT_EQ(1U, GetAndResetSentEventCount()); 529 EXPECT_EQ(1U, GetAndResetAckedEventCount());
516 EXPECT_EQ(1U, queued_event_count());
517
518 OnHasTouchEventHandlers(false);
519 EXPECT_EQ(0U, GetAndResetSentEventCount());
520 EXPECT_EQ(0U, queued_event_count()); 530 EXPECT_EQ(0U, queued_event_count());
521 531
522 // Now receive an ACK for the move. 532 // Trigger a spurious ack.
523 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED); 533 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
524 EXPECT_EQ(0U, GetAndResetSentEventCount()); 534 EXPECT_EQ(0U, GetAndResetAckedEventCount());
525 EXPECT_EQ(0U, queued_event_count());
526 } 535 }
527 536
528 // Tests that touch-move events are not sent to the renderer if the preceding 537 // Tests that touch-move events are not sent to the renderer if the preceding
529 // touch-press event did not have a consumer (and consequently, did not hit the 538 // touch-press event did not have a consumer (and consequently, did not hit the
530 // main thread in the renderer). Also tests that all queued/coalesced touch 539 // main thread in the renderer). Also tests that all queued/coalesced touch
531 // events are flushed immediately when the ACK for the touch-press comes back 540 // events are flushed immediately when the ACK for the touch-press comes back
532 // with NO_CONSUMER status. 541 // with NO_CONSUMER status.
533 TEST_F(TouchEventQueueTest, NoConsumer) { 542 TEST_F(TouchEventQueueTest, NoConsumer) {
534 // The first touch-press should reach the renderer. 543 // The first touch-press should reach the renderer.
535 PressTouchPoint(1, 1); 544 PressTouchPoint(1, 1);
(...skipping 1571 matching lines...) Expand 10 before | Expand all | Expand 10 after
2107 EXPECT_EQ(1U, GetAndResetSentEventCount()); 2116 EXPECT_EQ(1U, GetAndResetSentEventCount());
2108 2117
2109 // Touch move event is throttled. 2118 // Touch move event is throttled.
2110 MoveTouchPoint(0, 60, 5); 2119 MoveTouchPoint(0, 60, 5);
2111 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED); 2120 SendTouchEventAck(INPUT_EVENT_ACK_STATE_CONSUMED);
2112 EXPECT_EQ(0U, queued_event_count()); 2121 EXPECT_EQ(0U, queued_event_count());
2113 EXPECT_EQ(0U, GetAndResetSentEventCount()); 2122 EXPECT_EQ(0U, GetAndResetSentEventCount());
2114 } 2123 }
2115 2124
2116 } // namespace content 2125 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698