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

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

Issue 375863005: Touch emulator: allow multiple touch streams. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Do not clear sequence count when reenabling Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 aura::Env::DeleteInstance(); 64 aura::Env::DeleteInstance();
65 screen_.reset(); 65 screen_.reset();
66 #endif 66 #endif
67 } 67 }
68 68
69 virtual void ForwardGestureEvent( 69 virtual void ForwardGestureEvent(
70 const blink::WebGestureEvent& event) OVERRIDE { 70 const blink::WebGestureEvent& event) OVERRIDE {
71 forwarded_events_.push_back(event.type); 71 forwarded_events_.push_back(event.type);
72 } 72 }
73 73
74 virtual void ForwardTouchEvent( 74 virtual void ForwardEmulatedTouchEvent(
75 const blink::WebTouchEvent& event) OVERRIDE { 75 const blink::WebTouchEvent& event) OVERRIDE {
76 forwarded_events_.push_back(event.type); 76 forwarded_events_.push_back(event.type);
77 EXPECT_EQ(1U, event.touchesLength); 77 EXPECT_EQ(1U, event.touchesLength);
78 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x); 78 EXPECT_EQ(last_mouse_x_, event.touches[0].position.x);
79 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y); 79 EXPECT_EQ(last_mouse_y_, event.touches[0].position.y);
80 int expectedCancelable = event.type != WebInputEvent::TouchCancel; 80 int expectedCancelable = event.type != WebInputEvent::TouchCancel;
81 EXPECT_EQ(expectedCancelable, event.cancelable); 81 EXPECT_EQ(expectedCancelable, event.cancelable);
82 emulator()->HandleTouchEventAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); 82 emulator()->HandleTouchEventAck(
83 event, INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS);
83 } 84 }
84 85
85 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {} 86 virtual void SetCursor(const WebCursor& cursor) OVERRIDE {}
86 87
87 protected: 88 protected:
88 TouchEmulator* emulator() const { 89 TouchEmulator* emulator() const {
89 return emulator_.get(); 90 return emulator_.get();
90 } 91 }
91 92
92 int modifiers() const { 93 int modifiers() const {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 } 175 }
175 176
176 void MouseUp(int x, int y) { 177 void MouseUp(int x, int y) {
177 DCHECK(mouse_pressed_); 178 DCHECK(mouse_pressed_);
178 if (x != last_mouse_x_ || y != last_mouse_y_) 179 if (x != last_mouse_x_ || y != last_mouse_y_)
179 SendMouseEvent(WebInputEvent::MouseMove, x, y); 180 SendMouseEvent(WebInputEvent::MouseMove, x, y);
180 SendMouseEvent(WebInputEvent::MouseUp, x, y); 181 SendMouseEvent(WebInputEvent::MouseUp, x, y);
181 mouse_pressed_ = false; 182 mouse_pressed_ = false;
182 } 183 }
183 184
185 bool TouchStart(int x, int y, bool ack) {
186 return SendTouchEvent(
187 WebInputEvent::TouchStart, WebTouchPoint::StatePressed, x, y, ack);
188 }
189
190 bool TouchMove(int x, int y, bool ack) {
191 return SendTouchEvent(
192 WebInputEvent::TouchMove, WebTouchPoint::StateMoved, x, y, ack);
193 }
194
195 bool TouchEnd(int x, int y, bool ack) {
196 return SendTouchEvent(
197 WebInputEvent::TouchEnd, WebTouchPoint::StateReleased, x, y, ack);
198 }
199
200 WebTouchEvent MakeTouchEvent(WebInputEvent::Type type,
201 WebTouchPoint::State state, int x, int y) {
202 WebTouchEvent event;
203 event.type = type;
204 event.timeStampSeconds = GetNextEventTimeSeconds();
205 event.touchesLength = 1;
206 event.touches[0].id = 0;
207 event.touches[0].state = state;
208 event.touches[0].position.x = x;
209 event.touches[0].position.y = y;
210 event.touches[0].screenPosition.x = x;
211 event.touches[0].screenPosition.y = y;
212 return event;
213 }
214
215 bool SendTouchEvent(WebInputEvent::Type type, WebTouchPoint::State state,
216 int x, int y, bool ack) {
217 WebTouchEvent event = MakeTouchEvent(type, state, x, y);
218 if (emulator()->HandleTouchEvent(event)) {
219 // Touch event is not forwarded.
220 return false;
221 }
222
223 if (ack) {
224 // Can't send ack if there are some pending acks.
225 DCHECK(!touch_events_to_ack_.size());
226
227 // Touch event is forwarded, ack should not be handled by emulator.
228 EXPECT_FALSE(emulator()->HandleTouchEventAck(
229 event, INPUT_EVENT_ACK_STATE_CONSUMED));
230 } else {
231 touch_events_to_ack_.push_back(event);
232 }
233 return true;
234 }
235
236 void AckOldestTouchEvent() {
237 DCHECK(touch_events_to_ack_.size());
238 WebTouchEvent event = touch_events_to_ack_[0];
239 touch_events_to_ack_.erase(touch_events_to_ack_.begin());
240 // Emulator should not handle ack from native stream.
241 EXPECT_FALSE(emulator()->HandleTouchEventAck(
242 event, INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS));
243 }
244
184 private: 245 private:
185 scoped_ptr<TouchEmulator> emulator_; 246 scoped_ptr<TouchEmulator> emulator_;
186 std::vector<WebInputEvent::Type> forwarded_events_; 247 std::vector<WebInputEvent::Type> forwarded_events_;
187 #if defined(USE_AURA) 248 #if defined(USE_AURA)
188 scoped_ptr<gfx::Screen> screen_; 249 scoped_ptr<gfx::Screen> screen_;
189 #endif 250 #endif
190 double last_event_time_seconds_; 251 double last_event_time_seconds_;
191 double event_time_delta_seconds_; 252 double event_time_delta_seconds_;
192 bool shift_pressed_; 253 bool shift_pressed_;
193 bool mouse_pressed_; 254 bool mouse_pressed_;
194 int last_mouse_x_; 255 int last_mouse_x_;
195 int last_mouse_y_; 256 int last_mouse_y_;
257 std::vector<WebTouchEvent> touch_events_to_ack_;
196 base::MessageLoopForUI message_loop_; 258 base::MessageLoopForUI message_loop_;
197 }; 259 };
198 260
199 261
200 TEST_F(TouchEmulatorTest, NoTouches) { 262 TEST_F(TouchEmulatorTest, NoTouches) {
201 MouseMove(100, 200); 263 MouseMove(100, 200);
202 MouseMove(300, 300); 264 MouseMove(300, 300);
203 EXPECT_EQ("", ExpectedEvents()); 265 EXPECT_EQ("", ExpectedEvents());
204 } 266 }
205 267
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 MouseDown(300, 200); 401 MouseDown(300, 200);
340 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents()); 402 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents());
341 EXPECT_FALSE(SendMouseWheelEvent()); 403 EXPECT_FALSE(SendMouseWheelEvent());
342 emulator()->Disable(); 404 emulator()->Disable();
343 EXPECT_EQ("TouchCancel GestureTapCancel", ExpectedEvents()); 405 EXPECT_EQ("TouchCancel GestureTapCancel", ExpectedEvents());
344 EXPECT_TRUE(SendMouseWheelEvent()); 406 EXPECT_TRUE(SendMouseWheelEvent());
345 emulator()->Enable(true /* allow_pinch */); 407 emulator()->Enable(true /* allow_pinch */);
346 EXPECT_TRUE(SendMouseWheelEvent()); 408 EXPECT_TRUE(SendMouseWheelEvent());
347 } 409 }
348 410
411 TEST_F(TouchEmulatorTest, MultipleTouchStreams) {
412 // Native stream should be blocked while emulated is active.
413 MouseMove(100, 200);
414 EXPECT_EQ("", ExpectedEvents());
415 MouseDown(100, 200);
416 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents());
417 EXPECT_FALSE(TouchStart(10, 10, true));
418 EXPECT_FALSE(TouchMove(20, 20, true));
419 MouseUp(200, 200);
420 EXPECT_EQ(
421 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate"
422 " TouchEnd GestureScrollEnd",
423 ExpectedEvents());
424 EXPECT_FALSE(TouchEnd(20, 20, true));
425
426 // Emulated stream should be blocked while native is active.
427 EXPECT_TRUE(TouchStart(10, 10, true));
428 EXPECT_TRUE(TouchMove(20, 20, true));
429 MouseDown(300, 200);
430 EXPECT_EQ("", ExpectedEvents());
431 // Re-enabling in the middle of a touch sequence should not affect this.
432 emulator()->Disable();
433 emulator()->Enable(true);
434 MouseDrag(300, 300);
435 EXPECT_EQ("", ExpectedEvents());
436 MouseUp(300, 300);
437 EXPECT_EQ("", ExpectedEvents());
438 EXPECT_TRUE(TouchEnd(20, 20, true));
439 EXPECT_EQ("", ExpectedEvents());
440
441 // Late ack for TouchEnd should not mess things up.
442 EXPECT_TRUE(TouchStart(10, 10, false));
443 EXPECT_TRUE(TouchMove(20, 20, false));
444 emulator()->Disable();
445 EXPECT_TRUE(TouchEnd(20, 20, false));
446 EXPECT_TRUE(TouchStart(30, 30, false));
447 AckOldestTouchEvent(); // TouchStart.
448 emulator()->Enable(true);
449 AckOldestTouchEvent(); // TouchMove.
450 AckOldestTouchEvent(); // TouchEnd.
451 MouseDown(300, 200);
452 EXPECT_EQ("", ExpectedEvents());
453 MouseDrag(300, 300);
454 EXPECT_EQ("", ExpectedEvents());
455 MouseUp(300, 300);
456 EXPECT_EQ("", ExpectedEvents());
457 AckOldestTouchEvent(); // TouchStart.
458 MouseDown(300, 200);
459 EXPECT_EQ("", ExpectedEvents());
460 EXPECT_TRUE(TouchMove(30, 40, true));
461 EXPECT_TRUE(TouchEnd(30, 40, true));
462 MouseUp(300, 200);
463 EXPECT_EQ("", ExpectedEvents());
464
465 // Emulation should be back to normal.
466 MouseDown(100, 200);
467 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents());
468 MouseUp(200, 200);
469 EXPECT_EQ(
470 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate"
471 " TouchEnd GestureScrollEnd",
472 ExpectedEvents());
473 }
474
475 TEST_F(TouchEmulatorTest, MultipleTouchStreamsLateEnable) {
476 // Enabling in the middle of native touch sequence should be handled.
477 // Send artificial late TouchEnd ack, like it is the first thing emulator
478 // does see.
479 WebTouchEvent event = MakeTouchEvent(
480 WebInputEvent::TouchEnd, WebTouchPoint::StateReleased, 10, 10);
481 EXPECT_FALSE(emulator()->HandleTouchEventAck(
482 event, INPUT_EVENT_ACK_STATE_CONSUMED));
483
484 MouseDown(100, 200);
485 EXPECT_EQ("TouchStart GestureTapDown", ExpectedEvents());
486 MouseUp(200, 200);
487 EXPECT_EQ(
488 "TouchMove GestureTapCancel GestureScrollBegin GestureScrollUpdate"
489 " TouchEnd GestureScrollEnd",
490 ExpectedEvents());
491 }
492
349 } // namespace content 493 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698