OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/timer/timer.h" | 9 #include "base/timer/timer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 ui::EventType wait_until_event_; | 330 ui::EventType wait_until_event_; |
331 | 331 |
332 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate); | 332 DISALLOW_COPY_AND_ASSIGN(GestureEventConsumeDelegate); |
333 }; | 333 }; |
334 | 334 |
335 class QueueTouchEventDelegate : public GestureEventConsumeDelegate { | 335 class QueueTouchEventDelegate : public GestureEventConsumeDelegate { |
336 public: | 336 public: |
337 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher) | 337 explicit QueueTouchEventDelegate(WindowEventDispatcher* dispatcher) |
338 : window_(NULL), | 338 : window_(NULL), |
339 dispatcher_(dispatcher), | 339 dispatcher_(dispatcher), |
340 queue_events_(true) { | 340 queue_events_(true), |
| 341 synchronous_ack_for_next_event_(AckState::PENDING) { |
341 } | 342 } |
342 ~QueueTouchEventDelegate() override { | 343 ~QueueTouchEventDelegate() override { |
343 while(!queue_.empty()) { | 344 while(!queue_.empty()) { |
344 delete queue_.front(); | 345 delete queue_.front(); |
345 queue_.pop(); | 346 queue_.pop(); |
346 } | 347 } |
347 } | 348 } |
348 | 349 |
349 void OnTouchEvent(ui::TouchEvent* event) override { | 350 void OnTouchEvent(ui::TouchEvent* event) override { |
350 if (queue_events_) { | 351 event->DisableSynchronousHandling(); |
| 352 if (synchronous_ack_for_next_event_ != AckState::PENDING) { |
| 353 ui::GestureRecognizer::Get()->AckSyncTouchEvent( |
| 354 event->unique_event_id(), |
| 355 synchronous_ack_for_next_event_ == AckState::CONSUMED |
| 356 ? ui::ER_CONSUMED |
| 357 : ui::ER_UNHANDLED, |
| 358 window_); |
| 359 synchronous_ack_for_next_event_ = AckState::PENDING; |
| 360 } |
| 361 if (queue_events_) |
351 queue_.push(new ui::TouchEvent(*event, window_, window_)); | 362 queue_.push(new ui::TouchEvent(*event, window_, window_)); |
352 event->StopPropagation(); | |
353 } | |
354 } | 363 } |
355 | 364 |
356 void ReceivedAck() { | 365 void ReceivedAck() { |
357 ReceivedAckImpl(false); | 366 ReceivedAckImpl(false); |
358 } | 367 } |
359 | 368 |
360 void ReceivedAckPreventDefaulted() { | 369 void ReceivedAckPreventDefaulted() { |
361 ReceivedAckImpl(true); | 370 ReceivedAckImpl(true); |
362 } | 371 } |
363 | 372 |
364 void set_window(Window* w) { window_ = w; } | 373 void set_window(Window* w) { window_ = w; } |
365 void set_queue_events(bool queue) { queue_events_ = queue; } | 374 void set_queue_events(bool queue) { queue_events_ = queue; } |
| 375 void set_synchronous_ack_for_next_event(bool consumed) { |
| 376 DCHECK(synchronous_ack_for_next_event_ == AckState::PENDING); |
| 377 synchronous_ack_for_next_event_ = |
| 378 consumed ? AckState::CONSUMED : AckState::UNCONSUMED; |
| 379 } |
366 | 380 |
367 private: | 381 private: |
| 382 enum class AckState { |
| 383 PENDING, |
| 384 CONSUMED, |
| 385 UNCONSUMED, |
| 386 }; |
| 387 |
368 void ReceivedAckImpl(bool prevent_defaulted) { | 388 void ReceivedAckImpl(bool prevent_defaulted) { |
369 scoped_ptr<ui::TouchEvent> event(queue_.front()); | 389 scoped_ptr<ui::TouchEvent> event(queue_.front()); |
370 dispatcher_->ProcessedTouchEvent(event.get(), window_, | 390 dispatcher_->ProcessedTouchEvent(event.get(), window_, |
371 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED); | 391 prevent_defaulted ? ui::ER_HANDLED : ui::ER_UNHANDLED); |
372 queue_.pop(); | 392 queue_.pop(); |
373 } | 393 } |
374 | 394 |
375 std::queue<ui::TouchEvent*> queue_; | 395 std::queue<ui::TouchEvent*> queue_; |
376 Window* window_; | 396 Window* window_; |
377 WindowEventDispatcher* dispatcher_; | 397 WindowEventDispatcher* dispatcher_; |
378 bool queue_events_; | 398 bool queue_events_; |
| 399 AckState synchronous_ack_for_next_event_; |
379 | 400 |
380 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate); | 401 DISALLOW_COPY_AND_ASSIGN(QueueTouchEventDelegate); |
381 }; | 402 }; |
382 | 403 |
383 // A delegate that ignores gesture events but keeps track of [synthetic] mouse | 404 // A delegate that ignores gesture events but keeps track of [synthetic] mouse |
384 // events. | 405 // events. |
385 class GestureEventSynthDelegate : public TestWindowDelegate { | 406 class GestureEventSynthDelegate : public TestWindowDelegate { |
386 public: | 407 public: |
387 GestureEventSynthDelegate() | 408 GestureEventSynthDelegate() |
388 : mouse_enter_(false), | 409 : mouse_enter_(false), |
(...skipping 3851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4240 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202), | 4261 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(65, 202), |
4241 kTouchId1, tes.Now()); | 4262 kTouchId1, tes.Now()); |
4242 DispatchEventUsingWindowDispatcher(&move2); | 4263 DispatchEventUsingWindowDispatcher(&move2); |
4243 EXPECT_2_EVENTS(delegate->events(), | 4264 EXPECT_2_EVENTS(delegate->events(), |
4244 ui::ET_GESTURE_SCROLL_UPDATE, | 4265 ui::ET_GESTURE_SCROLL_UPDATE, |
4245 ui::ET_GESTURE_PINCH_UPDATE); | 4266 ui::ET_GESTURE_PINCH_UPDATE); |
4246 } | 4267 } |
4247 | 4268 |
4248 // Tests that delaying the ack of a touch release doesn't trigger a long press | 4269 // Tests that delaying the ack of a touch release doesn't trigger a long press |
4249 // gesture. | 4270 // gesture. |
4250 TEST_F(GestureRecognizerTest, DISABLED_EagerGestureDetection) { | 4271 TEST_F(GestureRecognizerTest, EagerGestureDetection) { |
4251 scoped_ptr<QueueTouchEventDelegate> delegate( | 4272 scoped_ptr<QueueTouchEventDelegate> delegate( |
4252 new QueueTouchEventDelegate(host()->dispatcher())); | 4273 new QueueTouchEventDelegate(host()->dispatcher())); |
4253 TimedEvents tes; | 4274 TimedEvents tes; |
4254 const int kTouchId = 2; | 4275 const int kTouchId = 2; |
4255 gfx::Rect bounds(100, 200, 100, 100); | 4276 gfx::Rect bounds(100, 200, 100, 100); |
4256 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( | 4277 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( |
4257 delegate.get(), -1234, bounds, root_window())); | 4278 delegate.get(), -1234, bounds, root_window())); |
4258 delegate->set_window(window.get()); | 4279 delegate->set_window(window.get()); |
4259 | 4280 |
4260 delegate->Reset(); | 4281 delegate->Reset(); |
(...skipping 12 matching lines...) Expand all Loading... |
4273 delegate->Reset(); | 4294 delegate->Reset(); |
4274 // Wait until the long press event would fire (if we weren't eager). | 4295 // Wait until the long press event would fire (if we weren't eager). |
4275 DelayByLongPressTimeout(); | 4296 DelayByLongPressTimeout(); |
4276 | 4297 |
4277 // Ack the touch release. | 4298 // Ack the touch release. |
4278 delegate->ReceivedAck(); | 4299 delegate->ReceivedAck(); |
4279 EXPECT_TRUE(delegate->tap()); | 4300 EXPECT_TRUE(delegate->tap()); |
4280 EXPECT_FALSE(delegate->long_press()); | 4301 EXPECT_FALSE(delegate->long_press()); |
4281 } | 4302 } |
4282 | 4303 |
4283 // This tests crbug.com/405519, in which events which the gesture detector | 4304 // This tests crbug.com/405519, in which touch events which the gesture detector |
4284 // ignores cause future events to also be thrown away. | 4305 // ignores interfere with gesture recognition. |
4285 TEST_F(GestureRecognizerTest, IgnoredEventsDontPreventFutureEvents) { | 4306 TEST_F(GestureRecognizerTest, IgnoredEventsDontBreakGestureRecognition) { |
4286 scoped_ptr<QueueTouchEventDelegate> delegate( | 4307 scoped_ptr<QueueTouchEventDelegate> delegate( |
4287 new QueueTouchEventDelegate(host()->dispatcher())); | 4308 new QueueTouchEventDelegate(host()->dispatcher())); |
4288 TimedEvents tes; | 4309 TimedEvents tes; |
4289 const int kWindowWidth = 300; | 4310 const int kWindowWidth = 300; |
4290 const int kWindowHeight = 400; | 4311 const int kWindowHeight = 400; |
4291 const int kTouchId1 = 3; | 4312 const int kTouchId1 = 3; |
4292 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); | 4313 gfx::Rect bounds(5, 5, kWindowWidth, kWindowHeight); |
4293 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( | 4314 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( |
4294 delegate.get(), -1234, bounds, root_window())); | 4315 delegate.get(), -1234, bounds, root_window())); |
4295 delegate->set_window(window.get()); | 4316 delegate->set_window(window.get()); |
(...skipping 12 matching lines...) Expand all Loading... |
4308 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now()); | 4329 ui::ET_TOUCH_MOVED, gfx::Point(65, 201), kTouchId1, tes.Now()); |
4309 DispatchEventUsingWindowDispatcher(&move1); | 4330 DispatchEventUsingWindowDispatcher(&move1); |
4310 delegate->ReceivedAck(); | 4331 delegate->ReceivedAck(); |
4311 | 4332 |
4312 EXPECT_3_EVENTS(delegate->events(), | 4333 EXPECT_3_EVENTS(delegate->events(), |
4313 ui::ET_GESTURE_TAP_CANCEL, | 4334 ui::ET_GESTURE_TAP_CANCEL, |
4314 ui::ET_GESTURE_SCROLL_BEGIN, | 4335 ui::ET_GESTURE_SCROLL_BEGIN, |
4315 ui::ET_GESTURE_SCROLL_UPDATE); | 4336 ui::ET_GESTURE_SCROLL_UPDATE); |
4316 | 4337 |
4317 delegate->Reset(); | 4338 delegate->Reset(); |
| 4339 |
| 4340 // Send a valid event, but don't ack it. |
4318 ui::TouchEvent move2( | 4341 ui::TouchEvent move2( |
4319 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); | 4342 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); |
4320 DispatchEventUsingWindowDispatcher(&move2); | 4343 DispatchEventUsingWindowDispatcher(&move2); |
| 4344 EXPECT_0_EVENTS(delegate->events()); |
4321 | 4345 |
4322 // Send a touchmove event at the same location as the previous touchmove | 4346 // Send a touchmove event at the same location as the previous touchmove |
4323 // event. This shouldn't do anything. | 4347 // event. This shouldn't do anything. |
4324 ui::TouchEvent move3( | 4348 ui::TouchEvent move3( |
4325 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); | 4349 ui::ET_TOUCH_MOVED, gfx::Point(65, 202), kTouchId1, tes.Now()); |
4326 DispatchEventUsingWindowDispatcher(&move3); | 4350 DispatchEventUsingWindowDispatcher(&move3); |
4327 | 4351 |
| 4352 // Ack the previous valid event. The intermediary invalid event shouldn't |
| 4353 // interfere. |
4328 delegate->ReceivedAck(); | 4354 delegate->ReceivedAck(); |
4329 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); | 4355 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); |
4330 } | 4356 } |
4331 | 4357 |
| 4358 // Tests that an event stream can have a mix of sync and async acks. |
| 4359 TEST_F(GestureRecognizerTest, |
| 4360 MixedSyncAndAsyncAcksDontCauseOutOfOrderDispatch) { |
| 4361 scoped_ptr<QueueTouchEventDelegate> delegate( |
| 4362 new QueueTouchEventDelegate(host()->dispatcher())); |
| 4363 TimedEvents tes; |
| 4364 const int kWindowWidth = 300; |
| 4365 const int kWindowHeight = 400; |
| 4366 const int kTouchId1 = 3; |
| 4367 gfx::Rect bounds(0, 0, kWindowWidth, kWindowHeight); |
| 4368 scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate( |
| 4369 delegate.get(), -1234, bounds, root_window())); |
| 4370 delegate->set_window(window.get()); |
| 4371 |
| 4372 // Start a scroll gesture. |
| 4373 ui::TouchEvent press1( |
| 4374 ui::ET_TOUCH_PRESSED, gfx::Point(0, 0), kTouchId1, tes.Now()); |
| 4375 DispatchEventUsingWindowDispatcher(&press1); |
| 4376 delegate->ReceivedAck(); |
| 4377 |
| 4378 ui::TouchEvent move1( |
| 4379 ui::ET_TOUCH_MOVED, gfx::Point(100, 100), kTouchId1, tes.Now()); |
| 4380 DispatchEventUsingWindowDispatcher(&move1); |
| 4381 delegate->ReceivedAck(); |
| 4382 |
| 4383 delegate->Reset(); |
| 4384 // Dispatch a synchronously consumed touch move, which should be ignored. |
| 4385 delegate->set_synchronous_ack_for_next_event(true); |
| 4386 ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(200, 200), kTouchId1, |
| 4387 tes.Now()); |
| 4388 DispatchEventUsingWindowDispatcher(&move2); |
| 4389 EXPECT_0_EVENTS(delegate->events()); |
| 4390 |
| 4391 // Dispatch a touch move, but don't ack it. |
| 4392 ui::TouchEvent move3(ui::ET_TOUCH_MOVED, gfx::Point(300, 300), kTouchId1, |
| 4393 tes.Now()); |
| 4394 DispatchEventUsingWindowDispatcher(&move3); |
| 4395 |
| 4396 // Dispatch two synchronously consumed touch moves, which should be ignored. |
| 4397 delegate->set_synchronous_ack_for_next_event(true); |
| 4398 ui::TouchEvent move4( |
| 4399 ui::ET_TOUCH_MOVED, gfx::Point(400, 400), kTouchId1, tes.Now()); |
| 4400 DispatchEventUsingWindowDispatcher(&move4); |
| 4401 |
| 4402 delegate->set_synchronous_ack_for_next_event(true); |
| 4403 ui::TouchEvent move5( |
| 4404 ui::ET_TOUCH_MOVED, gfx::Point(500, 500), kTouchId1, tes.Now()); |
| 4405 DispatchEventUsingWindowDispatcher(&move5); |
| 4406 |
| 4407 EXPECT_0_EVENTS(delegate->events()); |
| 4408 EXPECT_EQ(100, delegate->bounding_box().x()); |
| 4409 // Ack the pending touch move, and ensure the most recent gesture event |
| 4410 // used its co-ordinates. |
| 4411 delegate->ReceivedAck(); |
| 4412 EXPECT_EQ(300, delegate->bounding_box().x()); |
| 4413 EXPECT_1_EVENT(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE); |
| 4414 |
| 4415 // Dispatch a touch move, but don't ack it. |
| 4416 delegate->Reset(); |
| 4417 ui::TouchEvent move6(ui::ET_TOUCH_MOVED, gfx::Point(600, 600), kTouchId1, |
| 4418 tes.Now()); |
| 4419 DispatchEventUsingWindowDispatcher(&move6); |
| 4420 |
| 4421 // Dispatch a synchronously unconsumed touch move. |
| 4422 delegate->set_synchronous_ack_for_next_event(false); |
| 4423 ui::TouchEvent move7( |
| 4424 ui::ET_TOUCH_MOVED, gfx::Point(700, 700), kTouchId1, tes.Now()); |
| 4425 DispatchEventUsingWindowDispatcher(&move7); |
| 4426 |
| 4427 // The synchronous ack is stuck behind the pending touch move. |
| 4428 EXPECT_0_EVENTS(delegate->events()); |
| 4429 |
| 4430 delegate->ReceivedAck(); |
| 4431 EXPECT_2_EVENTS(delegate->events(), ui::ET_GESTURE_SCROLL_UPDATE, |
| 4432 ui::ET_GESTURE_SCROLL_UPDATE); |
| 4433 } |
| 4434 |
4332 } // namespace test | 4435 } // namespace test |
4333 } // namespace aura | 4436 } // namespace aura |
OLD | NEW |