Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / render_widget_host_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/basictypes.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/shared_memory.h"
10 #include "base/timer/timer.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/input/gesture_event_queue.h"
13 #include "content/browser/renderer_host/input/input_router_impl.h"
14 #include "content/browser/renderer_host/input/touch_event_queue.h"
15 #include "content/browser/renderer_host/overscroll_controller.h"
16 #include "content/browser/renderer_host/overscroll_controller_delegate.h"
17 #include "content/browser/renderer_host/render_widget_host_delegate.h"
18 #include "content/browser/renderer_host/render_widget_host_view_base.h"
19 #include "content/common/input/synthetic_web_input_event_builders.h"
20 #include "content/common/input_messages.h"
21 #include "content/common/view_messages.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "content/test/test_render_view_host.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/events/keycodes/keyboard_codes.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/gfx/screen.h"
30
31 #if defined(OS_ANDROID)
32 #include "content/browser/renderer_host/render_widget_host_view_android.h"
33 #endif
34
35 #if defined(USE_AURA)
36 #include "content/browser/compositor/image_transport_factory.h"
37 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
38 #include "content/browser/renderer_host/ui_events_helper.h"
39 #include "ui/aura/env.h"
40 #include "ui/aura/test/test_screen.h"
41 #include "ui/compositor/test/in_process_context_factory.h"
42 #include "ui/events/event.h"
43 #endif
44
45 using base::TimeDelta;
46 using blink::WebGestureEvent;
47 using blink::WebInputEvent;
48 using blink::WebKeyboardEvent;
49 using blink::WebMouseEvent;
50 using blink::WebMouseWheelEvent;
51 using blink::WebTouchEvent;
52 using blink::WebTouchPoint;
53
54 namespace content {
55
56 // TestOverscrollDelegate ------------------------------------------------------
57
58 class TestOverscrollDelegate : public OverscrollControllerDelegate {
59  public:
60   explicit TestOverscrollDelegate(RenderWidgetHostView* view)
61       : view_(view),
62         current_mode_(OVERSCROLL_NONE),
63         completed_mode_(OVERSCROLL_NONE),
64         delta_x_(0.f),
65         delta_y_(0.f) {
66   }
67
68   virtual ~TestOverscrollDelegate() {}
69
70   OverscrollMode current_mode() const { return current_mode_; }
71   OverscrollMode completed_mode() const { return completed_mode_; }
72   float delta_x() const { return delta_x_; }
73   float delta_y() const { return delta_y_; }
74
75   void Reset() {
76     current_mode_ = OVERSCROLL_NONE;
77     completed_mode_ = OVERSCROLL_NONE;
78     delta_x_ = delta_y_ = 0.f;
79   }
80
81  private:
82   // Overridden from OverscrollControllerDelegate:
83   virtual gfx::Rect GetVisibleBounds() const OVERRIDE {
84     return view_->IsShowing() ? view_->GetViewBounds() : gfx::Rect();
85   }
86
87   virtual void OnOverscrollUpdate(float delta_x, float delta_y) OVERRIDE {
88     delta_x_ = delta_x;
89     delta_y_ = delta_y;
90   }
91
92   virtual void OnOverscrollComplete(OverscrollMode overscroll_mode) OVERRIDE {
93     EXPECT_EQ(current_mode_, overscroll_mode);
94     completed_mode_ = overscroll_mode;
95     current_mode_ = OVERSCROLL_NONE;
96   }
97
98   virtual void OnOverscrollModeChange(OverscrollMode old_mode,
99                                       OverscrollMode new_mode) OVERRIDE {
100     EXPECT_EQ(current_mode_, old_mode);
101     current_mode_ = new_mode;
102     delta_x_ = delta_y_ = 0.f;
103   }
104
105   RenderWidgetHostView* view_;
106   OverscrollMode current_mode_;
107   OverscrollMode completed_mode_;
108   float delta_x_;
109   float delta_y_;
110
111   DISALLOW_COPY_AND_ASSIGN(TestOverscrollDelegate);
112 };
113
114 // MockInputRouter -------------------------------------------------------------
115
116 class MockInputRouter : public InputRouter {
117  public:
118   explicit MockInputRouter(InputRouterClient* client)
119       : send_event_called_(false),
120         sent_mouse_event_(false),
121         sent_wheel_event_(false),
122         sent_keyboard_event_(false),
123         sent_gesture_event_(false),
124         send_touch_event_not_cancelled_(false),
125         message_received_(false),
126         client_(client) {
127   }
128   virtual ~MockInputRouter() {}
129
130   // InputRouter
131   virtual void Flush() OVERRIDE {
132     flush_called_ = true;
133   }
134   virtual bool SendInput(scoped_ptr<IPC::Message> message) OVERRIDE {
135     send_event_called_ = true;
136     return true;
137   }
138   virtual void SendMouseEvent(
139       const MouseEventWithLatencyInfo& mouse_event) OVERRIDE {
140     sent_mouse_event_ = true;
141   }
142   virtual void SendWheelEvent(
143       const MouseWheelEventWithLatencyInfo& wheel_event) OVERRIDE {
144     sent_wheel_event_ = true;
145   }
146   virtual void SendKeyboardEvent(
147       const NativeWebKeyboardEvent& key_event,
148       const ui::LatencyInfo& latency_info,
149       bool is_shortcut) OVERRIDE {
150     sent_keyboard_event_ = true;
151   }
152   virtual void SendGestureEvent(
153       const GestureEventWithLatencyInfo& gesture_event) OVERRIDE {
154     sent_gesture_event_ = true;
155   }
156   virtual void SendTouchEvent(
157       const TouchEventWithLatencyInfo& touch_event) OVERRIDE {
158     send_touch_event_not_cancelled_ =
159         client_->FilterInputEvent(touch_event.event, touch_event.latency) ==
160         INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
161   }
162   virtual const NativeWebKeyboardEvent* GetLastKeyboardEvent() const OVERRIDE {
163     NOTREACHED();
164     return NULL;
165   }
166   virtual bool ShouldForwardTouchEvent() const OVERRIDE { return true; }
167   virtual void OnViewUpdated(int view_flags) OVERRIDE {}
168
169   // IPC::Listener
170   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
171     message_received_ = true;
172     return false;
173   }
174
175   bool flush_called_;
176   bool send_event_called_;
177   bool sent_mouse_event_;
178   bool sent_wheel_event_;
179   bool sent_keyboard_event_;
180   bool sent_gesture_event_;
181   bool send_touch_event_not_cancelled_;
182   bool message_received_;
183
184  private:
185   InputRouterClient* client_;
186
187   DISALLOW_COPY_AND_ASSIGN(MockInputRouter);
188 };
189
190 // MockRenderWidgetHost ----------------------------------------------------
191
192 class MockRenderWidgetHost : public RenderWidgetHostImpl {
193  public:
194   MockRenderWidgetHost(
195       RenderWidgetHostDelegate* delegate,
196       RenderProcessHost* process,
197       int routing_id)
198       : RenderWidgetHostImpl(delegate, process, routing_id, false),
199         unresponsive_timer_fired_(false) {
200     acked_touch_event_type_ = blink::WebInputEvent::Undefined;
201   }
202
203   // Allow poking at a few private members.
204   using RenderWidgetHostImpl::OnUpdateRect;
205   using RenderWidgetHostImpl::RendererExited;
206   using RenderWidgetHostImpl::last_requested_size_;
207   using RenderWidgetHostImpl::is_hidden_;
208   using RenderWidgetHostImpl::resize_ack_pending_;
209   using RenderWidgetHostImpl::input_router_;
210
211   bool unresponsive_timer_fired() const {
212     return unresponsive_timer_fired_;
213   }
214
215   void set_hung_renderer_delay_ms(int delay_ms) {
216     hung_renderer_delay_ms_ = delay_ms;
217   }
218
219   unsigned GestureEventLastQueueEventSize() const {
220     return gesture_event_queue().coalesced_gesture_events_.size();
221   }
222
223   WebGestureEvent GestureEventSecondFromLastQueueEvent() const {
224     return gesture_event_queue().coalesced_gesture_events_.at(
225       GestureEventLastQueueEventSize() - 2).event;
226   }
227
228   WebGestureEvent GestureEventLastQueueEvent() const {
229     return gesture_event_queue().coalesced_gesture_events_.back().event;
230   }
231
232   unsigned GestureEventDebouncingQueueSize() const {
233     return gesture_event_queue().debouncing_deferral_queue_.size();
234   }
235
236   WebGestureEvent GestureEventQueueEventAt(int i) const {
237     return gesture_event_queue().coalesced_gesture_events_.at(i).event;
238   }
239
240   bool ScrollingInProgress() const {
241     return gesture_event_queue().scrolling_in_progress_;
242   }
243
244   void SetupForOverscrollControllerTest() {
245     SetOverscrollControllerEnabled(true);
246     overscroll_delegate_.reset(new TestOverscrollDelegate(GetView()));
247     overscroll_controller_->set_delegate(overscroll_delegate_.get());
248   }
249
250   void DisableGestureDebounce() { set_debounce_interval_time_ms(0); }
251
252   void set_debounce_interval_time_ms(int delay_ms) {
253     gesture_event_queue().set_debounce_interval_time_ms_for_testing(delay_ms);
254   }
255
256   bool TouchEventQueueEmpty() const {
257     return touch_event_queue().empty();
258   }
259
260   virtual void OnTouchEventAck(
261       const TouchEventWithLatencyInfo& event,
262       InputEventAckState ack_result) OVERRIDE {
263     // Sniff touch acks.
264     acked_touch_event_type_ = event.event.type;
265     RenderWidgetHostImpl::OnTouchEventAck(event, ack_result);
266   }
267
268   WebInputEvent::Type acked_touch_event_type() const {
269     return acked_touch_event_type_;
270   }
271
272   bool ScrollStateIsContentScrolling() const {
273     return scroll_state() == OverscrollController::STATE_CONTENT_SCROLLING;
274   }
275
276   bool ScrollStateIsOverscrolling() const {
277     return scroll_state() == OverscrollController::STATE_OVERSCROLLING;
278   }
279
280   bool ScrollStateIsUnknown() const {
281     return scroll_state() == OverscrollController::STATE_UNKNOWN;
282   }
283
284   OverscrollController::ScrollState scroll_state() const {
285     return overscroll_controller_->scroll_state_;
286   }
287
288   OverscrollMode overscroll_mode() const {
289     return overscroll_controller_->overscroll_mode_;
290   }
291
292   float overscroll_delta_x() const {
293     return overscroll_controller_->overscroll_delta_x_;
294   }
295
296   float overscroll_delta_y() const {
297     return overscroll_controller_->overscroll_delta_y_;
298   }
299
300   TestOverscrollDelegate* overscroll_delegate() {
301     return overscroll_delegate_.get();
302   }
303
304   void SetupForInputRouterTest() {
305     input_router_.reset(new MockInputRouter(this));
306   }
307
308   MockInputRouter* mock_input_router() {
309     return static_cast<MockInputRouter*>(input_router_.get());
310   }
311
312  protected:
313   virtual void NotifyRendererUnresponsive() OVERRIDE {
314     unresponsive_timer_fired_ = true;
315   }
316
317   const TouchEventQueue& touch_event_queue() const {
318     return input_router_impl()->touch_event_queue_;
319   }
320
321   const GestureEventQueue& gesture_event_queue() const {
322     return input_router_impl()->gesture_event_queue_;
323   }
324
325   GestureEventQueue& gesture_event_queue() {
326     return input_router_impl()->gesture_event_queue_;
327   }
328
329  private:
330   const InputRouterImpl* input_router_impl() const {
331     return static_cast<InputRouterImpl*>(input_router_.get());
332   }
333
334   InputRouterImpl* input_router_impl() {
335     return static_cast<InputRouterImpl*>(input_router_.get());
336   }
337
338   bool unresponsive_timer_fired_;
339   WebInputEvent::Type acked_touch_event_type_;
340
341   scoped_ptr<TestOverscrollDelegate> overscroll_delegate_;
342
343   DISALLOW_COPY_AND_ASSIGN(MockRenderWidgetHost);
344 };
345
346 namespace  {
347
348 // RenderWidgetHostProcess -----------------------------------------------------
349
350 class RenderWidgetHostProcess : public MockRenderProcessHost {
351  public:
352   explicit RenderWidgetHostProcess(BrowserContext* browser_context)
353       : MockRenderProcessHost(browser_context),
354         update_msg_should_reply_(false),
355         update_msg_reply_flags_(0) {
356   }
357   virtual ~RenderWidgetHostProcess() {
358   }
359
360   void set_update_msg_should_reply(bool reply) {
361     update_msg_should_reply_ = reply;
362   }
363   void set_update_msg_reply_flags(int flags) {
364     update_msg_reply_flags_ = flags;
365   }
366
367   // Fills the given update parameters with resonable default values.
368   void InitUpdateRectParams(ViewHostMsg_UpdateRect_Params* params);
369
370   virtual bool HasConnection() const OVERRIDE { return true; }
371
372  protected:
373   virtual bool WaitForBackingStoreMsg(int render_widget_id,
374                                       const base::TimeDelta& max_delay,
375                                       IPC::Message* msg) OVERRIDE;
376
377   // Set to true when WaitForBackingStoreMsg should return a successful update
378   // message reply. False implies timeout.
379   bool update_msg_should_reply_;
380
381   // Indicates the flags that should be sent with a repaint request. This
382   // only has an effect when update_msg_should_reply_ is true.
383   int update_msg_reply_flags_;
384
385   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostProcess);
386 };
387
388 void RenderWidgetHostProcess::InitUpdateRectParams(
389     ViewHostMsg_UpdateRect_Params* params) {
390   const int w = 100, h = 100;
391
392   params->view_size = gfx::Size(w, h);
393   params->flags = update_msg_reply_flags_;
394 }
395
396 bool RenderWidgetHostProcess::WaitForBackingStoreMsg(
397     int render_widget_id,
398     const base::TimeDelta& max_delay,
399     IPC::Message* msg) {
400   if (!update_msg_should_reply_)
401     return false;
402
403   // Construct a fake update reply.
404   ViewHostMsg_UpdateRect_Params params;
405   InitUpdateRectParams(&params);
406
407   ViewHostMsg_UpdateRect message(render_widget_id, params);
408   *msg = message;
409   return true;
410 }
411
412 // TestView --------------------------------------------------------------------
413
414 // This test view allows us to specify the size, and keep track of acked
415 // touch-events.
416 class TestView : public TestRenderWidgetHostView {
417  public:
418   explicit TestView(RenderWidgetHostImpl* rwh)
419       : TestRenderWidgetHostView(rwh),
420         unhandled_wheel_event_count_(0),
421         acked_event_count_(0),
422         gesture_event_type_(-1),
423         use_fake_physical_backing_size_(false),
424         ack_result_(INPUT_EVENT_ACK_STATE_UNKNOWN) {
425   }
426
427   // Sets the bounds returned by GetViewBounds.
428   void set_bounds(const gfx::Rect& bounds) {
429     bounds_ = bounds;
430   }
431
432   const WebTouchEvent& acked_event() const { return acked_event_; }
433   int acked_event_count() const { return acked_event_count_; }
434   void ClearAckedEvent() {
435     acked_event_.type = blink::WebInputEvent::Undefined;
436     acked_event_count_ = 0;
437   }
438
439   const WebMouseWheelEvent& unhandled_wheel_event() const {
440     return unhandled_wheel_event_;
441   }
442   int unhandled_wheel_event_count() const {
443     return unhandled_wheel_event_count_;
444   }
445   int gesture_event_type() const { return gesture_event_type_; }
446   InputEventAckState ack_result() const { return ack_result_; }
447
448   void SetMockPhysicalBackingSize(const gfx::Size& mock_physical_backing_size) {
449     use_fake_physical_backing_size_ = true;
450     mock_physical_backing_size_ = mock_physical_backing_size;
451   }
452   void ClearMockPhysicalBackingSize() {
453     use_fake_physical_backing_size_ = false;
454   }
455
456   // RenderWidgetHostView override.
457   virtual gfx::Rect GetViewBounds() const OVERRIDE {
458     return bounds_;
459   }
460   virtual void ProcessAckedTouchEvent(const TouchEventWithLatencyInfo& touch,
461                                       InputEventAckState ack_result) OVERRIDE {
462     acked_event_ = touch.event;
463     ++acked_event_count_;
464   }
465   virtual void UnhandledWheelEvent(const WebMouseWheelEvent& event) OVERRIDE {
466     unhandled_wheel_event_count_++;
467     unhandled_wheel_event_ = event;
468   }
469   virtual void GestureEventAck(const WebGestureEvent& event,
470                                InputEventAckState ack_result) OVERRIDE {
471     gesture_event_type_ = event.type;
472     ack_result_ = ack_result;
473   }
474   virtual gfx::Size GetPhysicalBackingSize() const OVERRIDE {
475     if (use_fake_physical_backing_size_)
476       return mock_physical_backing_size_;
477     return TestRenderWidgetHostView::GetPhysicalBackingSize();
478   }
479
480  protected:
481   WebMouseWheelEvent unhandled_wheel_event_;
482   int unhandled_wheel_event_count_;
483   WebTouchEvent acked_event_;
484   int acked_event_count_;
485   int gesture_event_type_;
486   gfx::Rect bounds_;
487   bool use_fake_physical_backing_size_;
488   gfx::Size mock_physical_backing_size_;
489   InputEventAckState ack_result_;
490
491   DISALLOW_COPY_AND_ASSIGN(TestView);
492 };
493
494 // MockRenderWidgetHostDelegate --------------------------------------------
495
496 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
497  public:
498   MockRenderWidgetHostDelegate()
499       : prehandle_keyboard_event_(false),
500         prehandle_keyboard_event_called_(false),
501         prehandle_keyboard_event_type_(WebInputEvent::Undefined),
502         unhandled_keyboard_event_called_(false),
503         unhandled_keyboard_event_type_(WebInputEvent::Undefined),
504         handle_wheel_event_(false),
505         handle_wheel_event_called_(false) {
506   }
507   virtual ~MockRenderWidgetHostDelegate() {}
508
509   // Tests that make sure we ignore keyboard event acknowledgments to events we
510   // didn't send work by making sure we didn't call UnhandledKeyboardEvent().
511   bool unhandled_keyboard_event_called() const {
512     return unhandled_keyboard_event_called_;
513   }
514
515   WebInputEvent::Type unhandled_keyboard_event_type() const {
516     return unhandled_keyboard_event_type_;
517   }
518
519   bool prehandle_keyboard_event_called() const {
520     return prehandle_keyboard_event_called_;
521   }
522
523   WebInputEvent::Type prehandle_keyboard_event_type() const {
524     return prehandle_keyboard_event_type_;
525   }
526
527   void set_prehandle_keyboard_event(bool handle) {
528     prehandle_keyboard_event_ = handle;
529   }
530
531   void set_handle_wheel_event(bool handle) {
532     handle_wheel_event_ = handle;
533   }
534
535   bool handle_wheel_event_called() {
536     return handle_wheel_event_called_;
537   }
538
539  protected:
540   virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
541                                       bool* is_keyboard_shortcut) OVERRIDE {
542     prehandle_keyboard_event_type_ = event.type;
543     prehandle_keyboard_event_called_ = true;
544     return prehandle_keyboard_event_;
545   }
546
547   virtual void HandleKeyboardEvent(
548       const NativeWebKeyboardEvent& event) OVERRIDE {
549     unhandled_keyboard_event_type_ = event.type;
550     unhandled_keyboard_event_called_ = true;
551   }
552
553   virtual bool HandleWheelEvent(
554       const blink::WebMouseWheelEvent& event) OVERRIDE {
555     handle_wheel_event_called_ = true;
556     return handle_wheel_event_;
557   }
558
559  private:
560   bool prehandle_keyboard_event_;
561   bool prehandle_keyboard_event_called_;
562   WebInputEvent::Type prehandle_keyboard_event_type_;
563
564   bool unhandled_keyboard_event_called_;
565   WebInputEvent::Type unhandled_keyboard_event_type_;
566
567   bool handle_wheel_event_;
568   bool handle_wheel_event_called_;
569 };
570
571 // RenderWidgetHostTest --------------------------------------------------------
572
573 class RenderWidgetHostTest : public testing::Test {
574  public:
575   RenderWidgetHostTest()
576       : process_(NULL),
577         handle_key_press_event_(false),
578         handle_mouse_event_(false),
579         simulated_event_time_delta_seconds_(0) {
580     last_simulated_event_time_seconds_ =
581         (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
582   }
583   virtual ~RenderWidgetHostTest() {
584   }
585
586   bool KeyPressEventCallback(const NativeWebKeyboardEvent& /* event */) {
587     return handle_key_press_event_;
588   }
589   bool MouseEventCallback(const blink::WebMouseEvent& /* event */) {
590     return handle_mouse_event_;
591   }
592
593  protected:
594   // testing::Test
595   virtual void SetUp() {
596     CommandLine* command_line = CommandLine::ForCurrentProcess();
597     command_line->AppendSwitch(switches::kValidateInputEventStream);
598
599     browser_context_.reset(new TestBrowserContext());
600     delegate_.reset(new MockRenderWidgetHostDelegate());
601     process_ = new RenderWidgetHostProcess(browser_context_.get());
602 #if defined(USE_AURA)
603     ImageTransportFactory::InitializeForUnitTests(
604         scoped_ptr<ui::ContextFactory>(new ui::InProcessContextFactory));
605     aura::Env::CreateInstance(true);
606     screen_.reset(aura::TestScreen::Create());
607     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
608 #endif
609     host_.reset(
610         new MockRenderWidgetHost(delegate_.get(), process_, MSG_ROUTING_NONE));
611     view_.reset(new TestView(host_.get()));
612     host_->SetView(view_.get());
613     host_->Init();
614
615     // Tests for debounce-related behavior will explicitly enable debouncing.
616     host_->DisableGestureDebounce();
617   }
618   virtual void TearDown() {
619     view_.reset();
620     host_.reset();
621     delegate_.reset();
622     process_ = NULL;
623     browser_context_.reset();
624
625 #if defined(USE_AURA)
626     aura::Env::DeleteInstance();
627     screen_.reset();
628     ImageTransportFactory::Terminate();
629 #endif
630
631     // Process all pending tasks to avoid leaks.
632     base::MessageLoop::current()->RunUntilIdle();
633   }
634
635   int64 GetLatencyComponentId() {
636     return host_->GetLatencyComponentId();
637   }
638
639   void SendInputEventACK(WebInputEvent::Type type,
640                          InputEventAckState ack_result) {
641     scoped_ptr<IPC::Message> response(
642         new InputHostMsg_HandleInputEvent_ACK(0, type, ack_result,
643                                               ui::LatencyInfo()));
644     host_->OnMessageReceived(*response);
645   }
646
647   double GetNextSimulatedEventTimeSeconds() {
648     last_simulated_event_time_seconds_ += simulated_event_time_delta_seconds_;
649     return last_simulated_event_time_seconds_;
650   }
651
652   void SimulateKeyboardEvent(WebInputEvent::Type type) {
653     SimulateKeyboardEvent(type, 0);
654   }
655
656   void SimulateKeyboardEvent(WebInputEvent::Type type, int modifiers) {
657     WebKeyboardEvent event = SyntheticWebKeyboardEventBuilder::Build(type);
658     event.modifiers = modifiers;
659     NativeWebKeyboardEvent native_event;
660     memcpy(&native_event, &event, sizeof(event));
661     host_->ForwardKeyboardEvent(native_event);
662   }
663
664   void SimulateMouseEvent(WebInputEvent::Type type) {
665     host_->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build(type));
666   }
667
668   void SimulateMouseEventWithLatencyInfo(WebInputEvent::Type type,
669                                          const ui::LatencyInfo& ui_latency) {
670     host_->ForwardMouseEventWithLatencyInfo(
671         SyntheticWebMouseEventBuilder::Build(type),
672         ui_latency);
673   }
674
675   void SimulateWheelEvent(float dX, float dY, int modifiers, bool precise) {
676     host_->ForwardWheelEvent(
677         SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise));
678   }
679
680   void SimulateWheelEventWithLatencyInfo(float dX,
681                                          float dY,
682                                          int modifiers,
683                                          bool precise,
684                                          const ui::LatencyInfo& ui_latency) {
685     host_->ForwardWheelEventWithLatencyInfo(
686         SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise),
687         ui_latency);
688   }
689
690   void SimulateMouseMove(int x, int y, int modifiers) {
691     SimulateMouseEvent(WebInputEvent::MouseMove, x, y, modifiers, false);
692   }
693
694   void SimulateMouseEvent(
695       WebInputEvent::Type type, int x, int y, int modifiers, bool pressed) {
696     WebMouseEvent event =
697         SyntheticWebMouseEventBuilder::Build(type, x, y, modifiers);
698     if (pressed)
699       event.button = WebMouseEvent::ButtonLeft;
700     event.timeStampSeconds = GetNextSimulatedEventTimeSeconds();
701     host_->ForwardMouseEvent(event);
702   }
703
704   void SimulateWheelEventWithPhase(WebMouseWheelEvent::Phase phase) {
705     host_->ForwardWheelEvent(SyntheticWebMouseWheelEventBuilder::Build(phase));
706   }
707
708   // Inject provided synthetic WebGestureEvent instance.
709   void SimulateGestureEventCore(const WebGestureEvent& gesture_event) {
710     host_->ForwardGestureEvent(gesture_event);
711   }
712
713   void SimulateGestureEventCoreWithLatencyInfo(
714       const WebGestureEvent& gesture_event,
715       const ui::LatencyInfo& ui_latency) {
716     host_->ForwardGestureEventWithLatencyInfo(gesture_event, ui_latency);
717   }
718
719   // Inject simple synthetic WebGestureEvent instances.
720   void SimulateGestureEvent(WebInputEvent::Type type,
721                             WebGestureEvent::SourceDevice sourceDevice) {
722     SimulateGestureEventCore(
723         SyntheticWebGestureEventBuilder::Build(type, sourceDevice));
724   }
725
726   void SimulateGestureEventWithLatencyInfo(
727       WebInputEvent::Type type,
728       WebGestureEvent::SourceDevice sourceDevice,
729       const ui::LatencyInfo& ui_latency) {
730     SimulateGestureEventCoreWithLatencyInfo(
731         SyntheticWebGestureEventBuilder::Build(type, sourceDevice),
732         ui_latency);
733   }
734
735   void SimulateGestureScrollUpdateEvent(float dX, float dY, int modifiers) {
736     SimulateGestureEventCore(
737         SyntheticWebGestureEventBuilder::BuildScrollUpdate(dX, dY, modifiers));
738   }
739
740   void SimulateGesturePinchUpdateEvent(float scale,
741                                        float anchorX,
742                                        float anchorY,
743                                        int modifiers) {
744     SimulateGestureEventCore(SyntheticWebGestureEventBuilder::BuildPinchUpdate(
745         scale, anchorX, anchorY, modifiers, WebGestureEvent::Touchscreen));
746   }
747
748   // Inject synthetic GestureFlingStart events.
749   void SimulateGestureFlingStartEvent(
750       float velocityX,
751       float velocityY,
752       WebGestureEvent::SourceDevice sourceDevice) {
753     SimulateGestureEventCore(
754         SyntheticWebGestureEventBuilder::BuildFling(velocityX,
755                                                     velocityY,
756                                                     sourceDevice));
757   }
758
759   // Set the timestamp for the touch-event.
760   void SetTouchTimestamp(base::TimeDelta timestamp) {
761     touch_event_.SetTimestamp(timestamp);
762   }
763
764   // Sends a touch event (irrespective of whether the page has a touch-event
765   // handler or not).
766   void SendTouchEvent() {
767     host_->ForwardTouchEventWithLatencyInfo(touch_event_, ui::LatencyInfo());
768
769     touch_event_.ResetPoints();
770   }
771
772   int PressTouchPoint(int x, int y) {
773     return touch_event_.PressPoint(x, y);
774   }
775
776   void MoveTouchPoint(int index, int x, int y) {
777     touch_event_.MovePoint(index, x, y);
778   }
779
780   void ReleaseTouchPoint(int index) {
781     touch_event_.ReleasePoint(index);
782   }
783
784   const WebInputEvent* GetInputEventFromMessage(const IPC::Message& message) {
785     PickleIterator iter(message);
786     const char* data;
787     int data_length;
788     if (!message.ReadData(&iter, &data, &data_length))
789       return NULL;
790     return reinterpret_cast<const WebInputEvent*>(data);
791   }
792
793   base::MessageLoopForUI message_loop_;
794
795   scoped_ptr<TestBrowserContext> browser_context_;
796   RenderWidgetHostProcess* process_;  // Deleted automatically by the widget.
797   scoped_ptr<MockRenderWidgetHostDelegate> delegate_;
798   scoped_ptr<MockRenderWidgetHost> host_;
799   scoped_ptr<TestView> view_;
800   scoped_ptr<gfx::Screen> screen_;
801   bool handle_key_press_event_;
802   bool handle_mouse_event_;
803   double last_simulated_event_time_seconds_;
804   double simulated_event_time_delta_seconds_;
805
806  private:
807   SyntheticWebTouchEvent touch_event_;
808
809   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostTest);
810 };
811
812 #if GTEST_HAS_PARAM_TEST
813 // RenderWidgetHostWithSourceTest ----------------------------------------------
814
815 // This is for tests that are to be run for all source devices.
816 class RenderWidgetHostWithSourceTest
817     : public RenderWidgetHostTest,
818       public testing::WithParamInterface<WebGestureEvent::SourceDevice> {
819 };
820 #endif  // GTEST_HAS_PARAM_TEST
821
822 }  // namespace
823
824 // -----------------------------------------------------------------------------
825
826 TEST_F(RenderWidgetHostTest, Resize) {
827   // The initial bounds is the empty rect, and the screen info hasn't been sent
828   // yet, so setting it to the same thing shouldn't send the resize message.
829   view_->set_bounds(gfx::Rect());
830   host_->WasResized();
831   EXPECT_FALSE(host_->resize_ack_pending_);
832   EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
833
834   // Setting the bounds to a "real" rect should send out the notification.
835   // but should not expect ack for empty physical backing size.
836   gfx::Rect original_size(0, 0, 100, 100);
837   process_->sink().ClearMessages();
838   view_->set_bounds(original_size);
839   view_->SetMockPhysicalBackingSize(gfx::Size());
840   host_->WasResized();
841   EXPECT_FALSE(host_->resize_ack_pending_);
842   EXPECT_EQ(original_size.size(), host_->last_requested_size_);
843   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
844
845   // Setting the bounds to a "real" rect should send out the notification.
846   // but should not expect ack for only physical backing size change.
847   process_->sink().ClearMessages();
848   view_->ClearMockPhysicalBackingSize();
849   host_->WasResized();
850   EXPECT_FALSE(host_->resize_ack_pending_);
851   EXPECT_EQ(original_size.size(), host_->last_requested_size_);
852   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
853
854   // Send out a update that's not a resize ack after setting resize ack pending
855   // flag. This should not clean the resize ack pending flag.
856   process_->sink().ClearMessages();
857   gfx::Rect second_size(0, 0, 110, 110);
858   EXPECT_FALSE(host_->resize_ack_pending_);
859   view_->set_bounds(second_size);
860   host_->WasResized();
861   EXPECT_TRUE(host_->resize_ack_pending_);
862   ViewHostMsg_UpdateRect_Params params;
863   process_->InitUpdateRectParams(&params);
864   host_->OnUpdateRect(params);
865   EXPECT_TRUE(host_->resize_ack_pending_);
866   EXPECT_EQ(second_size.size(), host_->last_requested_size_);
867
868   // Sending out a new notification should NOT send out a new IPC message since
869   // a resize ACK is pending.
870   gfx::Rect third_size(0, 0, 120, 120);
871   process_->sink().ClearMessages();
872   view_->set_bounds(third_size);
873   host_->WasResized();
874   EXPECT_TRUE(host_->resize_ack_pending_);
875   EXPECT_EQ(second_size.size(), host_->last_requested_size_);
876   EXPECT_FALSE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
877
878   // Send a update that's a resize ack, but for the original_size we sent. Since
879   // this isn't the second_size, the message handler should immediately send
880   // a new resize message for the new size to the renderer.
881   process_->sink().ClearMessages();
882   params.flags = ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
883   params.view_size = original_size.size();
884   host_->OnUpdateRect(params);
885   EXPECT_TRUE(host_->resize_ack_pending_);
886   EXPECT_EQ(third_size.size(), host_->last_requested_size_);
887   ASSERT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
888
889   // Send the resize ack for the latest size.
890   process_->sink().ClearMessages();
891   params.view_size = third_size.size();
892   host_->OnUpdateRect(params);
893   EXPECT_FALSE(host_->resize_ack_pending_);
894   EXPECT_EQ(third_size.size(), host_->last_requested_size_);
895   ASSERT_FALSE(process_->sink().GetFirstMessageMatching(ViewMsg_Resize::ID));
896
897   // Now clearing the bounds should send out a notification but we shouldn't
898   // expect a resize ack (since the renderer won't ack empty sizes). The message
899   // should contain the new size (0x0) and not the previous one that we skipped
900   process_->sink().ClearMessages();
901   view_->set_bounds(gfx::Rect());
902   host_->WasResized();
903   EXPECT_FALSE(host_->resize_ack_pending_);
904   EXPECT_EQ(gfx::Size(), host_->last_requested_size_);
905   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
906
907   // Send a rect that has no area but has either width or height set.
908   process_->sink().ClearMessages();
909   view_->set_bounds(gfx::Rect(0, 0, 0, 30));
910   host_->WasResized();
911   EXPECT_FALSE(host_->resize_ack_pending_);
912   EXPECT_EQ(gfx::Size(0, 30), host_->last_requested_size_);
913   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
914
915   // Set the same size again. It should not be sent again.
916   process_->sink().ClearMessages();
917   host_->WasResized();
918   EXPECT_FALSE(host_->resize_ack_pending_);
919   EXPECT_EQ(gfx::Size(0, 30), host_->last_requested_size_);
920   EXPECT_FALSE(process_->sink().GetFirstMessageMatching(ViewMsg_Resize::ID));
921
922   // A different size should be sent again, however.
923   view_->set_bounds(gfx::Rect(0, 0, 0, 31));
924   host_->WasResized();
925   EXPECT_FALSE(host_->resize_ack_pending_);
926   EXPECT_EQ(gfx::Size(0, 31), host_->last_requested_size_);
927   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
928 }
929
930 // Test for crbug.com/25097.  If a renderer crashes between a resize and the
931 // corresponding update message, we must be sure to clear the resize ack logic.
932 TEST_F(RenderWidgetHostTest, ResizeThenCrash) {
933   // Clear the first Resize message that carried screen info.
934   process_->sink().ClearMessages();
935
936   // Setting the bounds to a "real" rect should send out the notification.
937   gfx::Rect original_size(0, 0, 100, 100);
938   view_->set_bounds(original_size);
939   host_->WasResized();
940   EXPECT_TRUE(host_->resize_ack_pending_);
941   EXPECT_EQ(original_size.size(), host_->last_requested_size_);
942   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_Resize::ID));
943
944   // Simulate a renderer crash before the update message.  Ensure all the
945   // resize ack logic is cleared.  Must clear the view first so it doesn't get
946   // deleted.
947   host_->SetView(NULL);
948   host_->RendererExited(base::TERMINATION_STATUS_PROCESS_CRASHED, -1);
949   EXPECT_FALSE(host_->resize_ack_pending_);
950   EXPECT_EQ(gfx::Size(), host_->last_requested_size_);
951
952   // Reset the view so we can exit the test cleanly.
953   host_->SetView(view_.get());
954 }
955
956 // Tests setting custom background
957 TEST_F(RenderWidgetHostTest, Background) {
958 #if !defined(OS_MACOSX)
959   scoped_ptr<RenderWidgetHostViewBase> view;
960 #if defined(USE_AURA)
961   view.reset(new RenderWidgetHostViewAura(host_.get()));
962   // TODO(derat): Call this on all platforms: http://crbug.com/102450.
963   view->InitAsChild(NULL);
964 #elif defined(OS_ANDROID)
965   view.reset(new RenderWidgetHostViewAndroid(host_.get(), NULL));
966 #endif
967   host_->SetView(view.get());
968
969   // Create a checkerboard background to test with.
970   gfx::Canvas canvas(gfx::Size(4, 4), 1.0f, true);
971   canvas.FillRect(gfx::Rect(0, 0, 2, 2), SK_ColorBLACK);
972   canvas.FillRect(gfx::Rect(2, 0, 2, 2), SK_ColorWHITE);
973   canvas.FillRect(gfx::Rect(0, 2, 2, 2), SK_ColorWHITE);
974   canvas.FillRect(gfx::Rect(2, 2, 2, 2), SK_ColorBLACK);
975   const SkBitmap& background =
976       canvas.sk_canvas()->getDevice()->accessBitmap(false);
977
978   // Set the background and make sure we get back a copy.
979   view->SetBackground(background);
980   EXPECT_EQ(4, view->GetBackground().width());
981   EXPECT_EQ(4, view->GetBackground().height());
982   EXPECT_EQ(background.getSize(), view->GetBackground().getSize());
983   background.lockPixels();
984   view->GetBackground().lockPixels();
985   EXPECT_TRUE(0 == memcmp(background.getPixels(),
986                           view->GetBackground().getPixels(),
987                           background.getSize()));
988   view->GetBackground().unlockPixels();
989   background.unlockPixels();
990
991   const IPC::Message* set_background =
992       process_->sink().GetUniqueMessageMatching(ViewMsg_SetBackground::ID);
993   ASSERT_TRUE(set_background);
994   Tuple1<SkBitmap> sent_background;
995   ViewMsg_SetBackground::Read(set_background, &sent_background);
996   EXPECT_EQ(background.getSize(), sent_background.a.getSize());
997   background.lockPixels();
998   sent_background.a.lockPixels();
999   EXPECT_TRUE(0 == memcmp(background.getPixels(),
1000                           sent_background.a.getPixels(),
1001                           background.getSize()));
1002   sent_background.a.unlockPixels();
1003   background.unlockPixels();
1004
1005 #if defined(USE_AURA)
1006   // See the comment above |InitAsChild(NULL)|.
1007   host_->SetView(NULL);
1008   static_cast<RenderWidgetHostViewBase*>(view.release())->Destroy();
1009 #endif
1010
1011 #else
1012   // TODO(port): Mac does not have gfx::Canvas. Maybe we can just change this
1013   // test to use SkCanvas directly?
1014 #endif
1015
1016   // TODO(aa): It would be nice to factor out the painting logic so that we
1017   // could test that, but it appears that would mean painting everything twice
1018   // since windows HDC structures are opaque.
1019 }
1020
1021 // Test that we don't paint when we're hidden, but we still send the ACK. Most
1022 // of the rest of the painting is tested in the GetBackingStore* ones.
1023 TEST_F(RenderWidgetHostTest, HiddenPaint) {
1024   BrowserThreadImpl ui_thread(BrowserThread::UI, base::MessageLoop::current());
1025   // Hide the widget, it should have sent out a message to the renderer.
1026   EXPECT_FALSE(host_->is_hidden_);
1027   host_->WasHidden();
1028   EXPECT_TRUE(host_->is_hidden_);
1029   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(ViewMsg_WasHidden::ID));
1030
1031   // Send it an update as from the renderer.
1032   process_->sink().ClearMessages();
1033   ViewHostMsg_UpdateRect_Params params;
1034   process_->InitUpdateRectParams(&params);
1035   host_->OnUpdateRect(params);
1036
1037   // Now unhide.
1038   process_->sink().ClearMessages();
1039   host_->WasShown();
1040   EXPECT_FALSE(host_->is_hidden_);
1041
1042   // It should have sent out a restored message with a request to paint.
1043   const IPC::Message* restored = process_->sink().GetUniqueMessageMatching(
1044       ViewMsg_WasShown::ID);
1045   ASSERT_TRUE(restored);
1046   Tuple1<bool> needs_repaint;
1047   ViewMsg_WasShown::Read(restored, &needs_repaint);
1048   EXPECT_TRUE(needs_repaint.a);
1049 }
1050
1051 TEST_F(RenderWidgetHostTest, IgnoreKeyEventsHandledByRenderer) {
1052   // Simulate a keyboard event.
1053   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
1054
1055   // Make sure we sent the input event to the renderer.
1056   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
1057                   InputMsg_HandleInputEvent::ID));
1058   process_->sink().ClearMessages();
1059
1060   // Send the simulated response from the renderer back.
1061   SendInputEventACK(WebInputEvent::RawKeyDown,
1062                     INPUT_EVENT_ACK_STATE_CONSUMED);
1063   EXPECT_FALSE(delegate_->unhandled_keyboard_event_called());
1064 }
1065
1066 TEST_F(RenderWidgetHostTest, PreHandleRawKeyDownEvent) {
1067   // Simluate the situation that the browser handled the key down event during
1068   // pre-handle phrase.
1069   delegate_->set_prehandle_keyboard_event(true);
1070   process_->sink().ClearMessages();
1071
1072   // Simulate a keyboard event.
1073   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
1074
1075   EXPECT_TRUE(delegate_->prehandle_keyboard_event_called());
1076   EXPECT_EQ(WebInputEvent::RawKeyDown,
1077             delegate_->prehandle_keyboard_event_type());
1078
1079   // Make sure the RawKeyDown event is not sent to the renderer.
1080   EXPECT_EQ(0U, process_->sink().message_count());
1081
1082   // The browser won't pre-handle a Char event.
1083   delegate_->set_prehandle_keyboard_event(false);
1084
1085   // Forward the Char event.
1086   SimulateKeyboardEvent(WebInputEvent::Char);
1087
1088   // Make sure the Char event is suppressed.
1089   EXPECT_EQ(0U, process_->sink().message_count());
1090
1091   // Forward the KeyUp event.
1092   SimulateKeyboardEvent(WebInputEvent::KeyUp);
1093
1094   // Make sure only KeyUp was sent to the renderer.
1095   EXPECT_EQ(1U, process_->sink().message_count());
1096   EXPECT_EQ(InputMsg_HandleInputEvent::ID,
1097             process_->sink().GetMessageAt(0)->type());
1098   process_->sink().ClearMessages();
1099
1100   // Send the simulated response from the renderer back.
1101   SendInputEventACK(WebInputEvent::KeyUp,
1102                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1103
1104   EXPECT_TRUE(delegate_->unhandled_keyboard_event_called());
1105   EXPECT_EQ(WebInputEvent::KeyUp, delegate_->unhandled_keyboard_event_type());
1106 }
1107
1108 TEST_F(RenderWidgetHostTest, UnhandledWheelEvent) {
1109   SimulateWheelEvent(-5, 0, 0, true);
1110
1111   // Make sure we sent the input event to the renderer.
1112   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
1113                   InputMsg_HandleInputEvent::ID));
1114   process_->sink().ClearMessages();
1115
1116   // Send the simulated response from the renderer back.
1117   SendInputEventACK(WebInputEvent::MouseWheel,
1118                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1119   EXPECT_TRUE(delegate_->handle_wheel_event_called());
1120   EXPECT_EQ(1, view_->unhandled_wheel_event_count());
1121   EXPECT_EQ(-5, view_->unhandled_wheel_event().deltaX);
1122 }
1123
1124 TEST_F(RenderWidgetHostTest, HandleWheelEvent) {
1125   // Indicate that we're going to handle this wheel event
1126   delegate_->set_handle_wheel_event(true);
1127
1128   SimulateWheelEvent(-5, 0, 0, true);
1129
1130   // Make sure we sent the input event to the renderer.
1131   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
1132                   InputMsg_HandleInputEvent::ID));
1133   process_->sink().ClearMessages();
1134
1135   // Send the simulated response from the renderer back.
1136   SendInputEventACK(WebInputEvent::MouseWheel,
1137                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1138
1139   // ensure the wheel event handler was invoked
1140   EXPECT_TRUE(delegate_->handle_wheel_event_called());
1141
1142   // and that it suppressed the unhandled wheel event handler.
1143   EXPECT_EQ(0, view_->unhandled_wheel_event_count());
1144 }
1145
1146 TEST_F(RenderWidgetHostTest, UnhandledGestureEvent) {
1147   SimulateGestureEvent(WebInputEvent::GestureTwoFingerTap,
1148                        WebGestureEvent::Touchscreen);
1149
1150   // Make sure we sent the input event to the renderer.
1151   EXPECT_TRUE(process_->sink().GetUniqueMessageMatching(
1152                   InputMsg_HandleInputEvent::ID));
1153   process_->sink().ClearMessages();
1154
1155   // Send the simulated response from the renderer back.
1156   SendInputEventACK(WebInputEvent::GestureTwoFingerTap,
1157                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1158   EXPECT_EQ(WebInputEvent::GestureTwoFingerTap, view_->gesture_event_type());
1159   EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, view_->ack_result());
1160 }
1161
1162 // Test that the hang monitor timer expires properly if a new timer is started
1163 // while one is in progress (see crbug.com/11007).
1164 TEST_F(RenderWidgetHostTest, DontPostponeHangMonitorTimeout) {
1165   // Start with a short timeout.
1166   host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10));
1167
1168   // Immediately try to add a long 30 second timeout.
1169   EXPECT_FALSE(host_->unresponsive_timer_fired());
1170   host_->StartHangMonitorTimeout(TimeDelta::FromSeconds(30));
1171
1172   // Wait long enough for first timeout and see if it fired.
1173   base::MessageLoop::current()->PostDelayedTask(
1174       FROM_HERE,
1175       base::MessageLoop::QuitClosure(),
1176       TimeDelta::FromMilliseconds(10));
1177   base::MessageLoop::current()->Run();
1178   EXPECT_TRUE(host_->unresponsive_timer_fired());
1179 }
1180
1181 // Test that the hang monitor timer expires properly if it is started, stopped,
1182 // and then started again.
1183 TEST_F(RenderWidgetHostTest, StopAndStartHangMonitorTimeout) {
1184   // Start with a short timeout, then stop it.
1185   host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10));
1186   host_->StopHangMonitorTimeout();
1187
1188   // Start it again to ensure it still works.
1189   EXPECT_FALSE(host_->unresponsive_timer_fired());
1190   host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(10));
1191
1192   // Wait long enough for first timeout and see if it fired.
1193   base::MessageLoop::current()->PostDelayedTask(
1194       FROM_HERE,
1195       base::MessageLoop::QuitClosure(),
1196       TimeDelta::FromMilliseconds(40));
1197   base::MessageLoop::current()->Run();
1198   EXPECT_TRUE(host_->unresponsive_timer_fired());
1199 }
1200
1201 // Test that the hang monitor timer expires properly if it is started, then
1202 // updated to a shorter duration.
1203 TEST_F(RenderWidgetHostTest, ShorterDelayHangMonitorTimeout) {
1204   // Start with a timeout.
1205   host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(100));
1206
1207   // Start it again with shorter delay.
1208   EXPECT_FALSE(host_->unresponsive_timer_fired());
1209   host_->StartHangMonitorTimeout(TimeDelta::FromMilliseconds(20));
1210
1211   // Wait long enough for the second timeout and see if it fired.
1212   base::MessageLoop::current()->PostDelayedTask(
1213       FROM_HERE,
1214       base::MessageLoop::QuitClosure(),
1215       TimeDelta::FromMilliseconds(25));
1216   base::MessageLoop::current()->Run();
1217   EXPECT_TRUE(host_->unresponsive_timer_fired());
1218 }
1219
1220 // Test that the hang monitor catches two input events but only one ack.
1221 // This can happen if the second input event causes the renderer to hang.
1222 // This test will catch a regression of crbug.com/111185.
1223 TEST_F(RenderWidgetHostTest, MultipleInputEvents) {
1224   // Configure the host to wait 10ms before considering
1225   // the renderer hung.
1226   host_->set_hung_renderer_delay_ms(10);
1227
1228   // Send two events but only one ack.
1229   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
1230   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
1231   SendInputEventACK(WebInputEvent::RawKeyDown,
1232                     INPUT_EVENT_ACK_STATE_CONSUMED);
1233
1234   // Wait long enough for first timeout and see if it fired.
1235   base::MessageLoop::current()->PostDelayedTask(
1236       FROM_HERE,
1237       base::MessageLoop::QuitClosure(),
1238       TimeDelta::FromMilliseconds(40));
1239   base::MessageLoop::current()->Run();
1240   EXPECT_TRUE(host_->unresponsive_timer_fired());
1241 }
1242
1243 // Tests that scroll ACKs are correctly handled by the overscroll-navigation
1244 // controller.
1245 TEST_F(RenderWidgetHostTest, WheelScrollEventOverscrolls) {
1246   host_->SetupForOverscrollControllerTest();
1247   process_->sink().ClearMessages();
1248
1249   // Simulate wheel events.
1250   SimulateWheelEvent(-5, 0, 0, true);  // sent directly
1251   SimulateWheelEvent(-1, 1, 0, true);  // enqueued
1252   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
1253   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
1254   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
1255   SimulateWheelEvent(-20, 6, 1, true);  // enqueued, different modifiers
1256   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1257   EXPECT_EQ(1U, process_->sink().message_count());
1258   process_->sink().ClearMessages();
1259
1260   // Receive ACK the first wheel event as not processed.
1261   SendInputEventACK(WebInputEvent::MouseWheel,
1262                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1263   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1264   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1265   EXPECT_EQ(1U, process_->sink().message_count());
1266   process_->sink().ClearMessages();
1267
1268   // Receive ACK for the second (coalesced) event as not processed. This will
1269   // start a back navigation. However, this will also cause the queued next
1270   // event to be sent to the renderer. But since overscroll navigation has
1271   // started, that event will also be included in the overscroll computation
1272   // instead of being sent to the renderer. So the result will be an overscroll
1273   // back navigation, and no event will be sent to the renderer.
1274   SendInputEventACK(WebInputEvent::MouseWheel,
1275                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1276   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
1277   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_delegate()->current_mode());
1278   EXPECT_EQ(-81.f, host_->overscroll_delta_x());
1279   EXPECT_EQ(-31.f, host_->overscroll_delegate()->delta_x());
1280   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1281   EXPECT_EQ(0U, process_->sink().message_count());
1282
1283   // Send a mouse-move event. This should cancel the overscroll navigation.
1284   SimulateMouseMove(5, 10, 0);
1285   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1286   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1287   EXPECT_EQ(1U, process_->sink().message_count());
1288 }
1289
1290 // Tests that if some scroll events are consumed towards the start, then
1291 // subsequent scrolls do not horizontal overscroll.
1292 TEST_F(RenderWidgetHostTest, WheelScrollConsumedDoNotHorizOverscroll) {
1293   host_->SetupForOverscrollControllerTest();
1294   process_->sink().ClearMessages();
1295
1296   // Simulate wheel events.
1297   SimulateWheelEvent(-5, 0, 0, true);  // sent directly
1298   SimulateWheelEvent(-1, -1, 0, true);  // enqueued
1299   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
1300   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
1301   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
1302   SimulateWheelEvent(-20, 6, 1, true);  // enqueued, different modifiers
1303   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1304   EXPECT_EQ(1U, process_->sink().message_count());
1305   process_->sink().ClearMessages();
1306
1307   // Receive ACK the first wheel event as processed.
1308   SendInputEventACK(WebInputEvent::MouseWheel,
1309                     INPUT_EVENT_ACK_STATE_CONSUMED);
1310   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1311   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1312   EXPECT_EQ(1U, process_->sink().message_count());
1313   process_->sink().ClearMessages();
1314
1315   // Receive ACK for the second (coalesced) event as not processed. This should
1316   // not initiate overscroll, since the beginning of the scroll has been
1317   // consumed. The queued event with different modifiers should be sent to the
1318   // renderer.
1319   SendInputEventACK(WebInputEvent::MouseWheel,
1320                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1321   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1322   EXPECT_EQ(1U, process_->sink().message_count());
1323
1324   process_->sink().ClearMessages();
1325   SendInputEventACK(WebInputEvent::MouseWheel,
1326                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1327   EXPECT_EQ(0U, process_->sink().message_count());
1328   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1329
1330   // Indicate the end of the scrolling from the touchpad.
1331   SimulateGestureFlingStartEvent(-1200.f, 0.f, WebGestureEvent::Touchpad);
1332   EXPECT_EQ(1U, process_->sink().message_count());
1333
1334   // Start another scroll. This time, do not consume any scroll events.
1335   process_->sink().ClearMessages();
1336   SimulateWheelEvent(0, -5, 0, true);  // sent directly
1337   SimulateWheelEvent(0, -1, 0, true);  // enqueued
1338   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
1339   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
1340   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
1341   SimulateWheelEvent(-20, 6, 1, true);  // enqueued, different modifiers
1342   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1343   EXPECT_EQ(1U, process_->sink().message_count());
1344   process_->sink().ClearMessages();
1345
1346   // Receive ACK for the first wheel and the subsequent coalesced event as not
1347   // processed. This should start a back-overscroll.
1348   SendInputEventACK(WebInputEvent::MouseWheel,
1349                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1350   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1351   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1352   EXPECT_EQ(1U, process_->sink().message_count());
1353   process_->sink().ClearMessages();
1354   SendInputEventACK(WebInputEvent::MouseWheel,
1355                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1356   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
1357 }
1358
1359 // Tests that wheel-scrolling correctly turns overscroll on and off.
1360 TEST_F(RenderWidgetHostTest, WheelScrollOverscrollToggle) {
1361   host_->SetupForOverscrollControllerTest();
1362   process_->sink().ClearMessages();
1363
1364   // Send a wheel event. ACK the event as not processed. This should not
1365   // initiate an overscroll gesture since it doesn't cross the threshold yet.
1366   SimulateWheelEvent(10, 0, 0, true);
1367   EXPECT_EQ(1U, process_->sink().message_count());
1368   SendInputEventACK(WebInputEvent::MouseWheel,
1369                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1370   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1371   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1372   process_->sink().ClearMessages();
1373
1374   // Scroll some more so as to not overscroll.
1375   SimulateWheelEvent(10, 0, 0, true);
1376   EXPECT_EQ(1U, process_->sink().message_count());
1377   SendInputEventACK(WebInputEvent::MouseWheel,
1378                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1379   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1380   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1381   process_->sink().ClearMessages();
1382
1383   // Scroll some more to initiate an overscroll.
1384   SimulateWheelEvent(40, 0, 0, true);
1385   EXPECT_EQ(1U, process_->sink().message_count());
1386   SendInputEventACK(WebInputEvent::MouseWheel,
1387                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1388   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1389   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1390   EXPECT_EQ(60.f, host_->overscroll_delta_x());
1391   EXPECT_EQ(10.f, host_->overscroll_delegate()->delta_x());
1392   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1393   process_->sink().ClearMessages();
1394
1395   // Scroll in the reverse direction enough to abort the overscroll.
1396   SimulateWheelEvent(-20, 0, 0, true);
1397   EXPECT_EQ(0U, process_->sink().message_count());
1398   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1399   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1400
1401   // Continue to scroll in the reverse direction.
1402   SimulateWheelEvent(-20, 0, 0, true);
1403   EXPECT_EQ(1U, process_->sink().message_count());
1404   SendInputEventACK(WebInputEvent::MouseWheel,
1405                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1406   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1407   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1408   process_->sink().ClearMessages();
1409
1410   // Continue to scroll in the reverse direction enough to initiate overscroll
1411   // in that direction.
1412   SimulateWheelEvent(-55, 0, 0, true);
1413   EXPECT_EQ(1U, process_->sink().message_count());
1414   SendInputEventACK(WebInputEvent::MouseWheel,
1415                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1416   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
1417   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_delegate()->current_mode());
1418   EXPECT_EQ(-75.f, host_->overscroll_delta_x());
1419   EXPECT_EQ(-25.f, host_->overscroll_delegate()->delta_x());
1420   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1421 }
1422
1423 TEST_F(RenderWidgetHostTest, ScrollEventsOverscrollWithFling) {
1424   host_->SetupForOverscrollControllerTest();
1425   process_->sink().ClearMessages();
1426
1427   // Send a wheel event. ACK the event as not processed. This should not
1428   // initiate an overscroll gesture since it doesn't cross the threshold yet.
1429   SimulateWheelEvent(10, 0, 0, true);
1430   EXPECT_EQ(1U, process_->sink().message_count());
1431   SendInputEventACK(WebInputEvent::MouseWheel,
1432                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1433   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1434   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1435   process_->sink().ClearMessages();
1436
1437   // Scroll some more so as to not overscroll.
1438   SimulateWheelEvent(20, 0, 0, true);
1439   EXPECT_EQ(1U, process_->sink().message_count());
1440   SendInputEventACK(WebInputEvent::MouseWheel,
1441                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1442   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1443   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1444   process_->sink().ClearMessages();
1445
1446   // Scroll some more to initiate an overscroll.
1447   SimulateWheelEvent(30, 0, 0, true);
1448   EXPECT_EQ(1U, process_->sink().message_count());
1449   SendInputEventACK(WebInputEvent::MouseWheel,
1450                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1451   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1452   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1453   EXPECT_EQ(60.f, host_->overscroll_delta_x());
1454   EXPECT_EQ(10.f, host_->overscroll_delegate()->delta_x());
1455   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1456   process_->sink().ClearMessages();
1457   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1458
1459   // Send a fling start, but with a small velocity, so that the overscroll is
1460   // aborted. The fling should proceed to the renderer, through the gesture
1461   // event filter.
1462   SimulateGestureFlingStartEvent(0.f, 0.1f, WebGestureEvent::Touchpad);
1463   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1464   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1465   EXPECT_EQ(1U, process_->sink().message_count());
1466 }
1467
1468 // Same as ScrollEventsOverscrollWithFling, but with zero velocity. Checks that
1469 // the zero-velocity fling does not reach the renderer.
1470 TEST_F(RenderWidgetHostTest, ScrollEventsOverscrollWithZeroFling) {
1471   host_->SetupForOverscrollControllerTest();
1472   process_->sink().ClearMessages();
1473
1474   // Send a wheel event. ACK the event as not processed. This should not
1475   // initiate an overscroll gesture since it doesn't cross the threshold yet.
1476   SimulateWheelEvent(10, 0, 0, true);
1477   EXPECT_EQ(1U, process_->sink().message_count());
1478   SendInputEventACK(WebInputEvent::MouseWheel,
1479                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1480   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1481   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1482   process_->sink().ClearMessages();
1483
1484   // Scroll some more so as to not overscroll.
1485   SimulateWheelEvent(20, 0, 0, true);
1486   EXPECT_EQ(1U, process_->sink().message_count());
1487   SendInputEventACK(WebInputEvent::MouseWheel,
1488                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1489   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1490   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1491   process_->sink().ClearMessages();
1492
1493   // Scroll some more to initiate an overscroll.
1494   SimulateWheelEvent(30, 0, 0, true);
1495   EXPECT_EQ(1U, process_->sink().message_count());
1496   SendInputEventACK(WebInputEvent::MouseWheel,
1497                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1498   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1499   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1500   EXPECT_EQ(60.f, host_->overscroll_delta_x());
1501   EXPECT_EQ(10.f, host_->overscroll_delegate()->delta_x());
1502   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1503   process_->sink().ClearMessages();
1504   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1505
1506   // Send a fling start, but with a small velocity, so that the overscroll is
1507   // aborted. The fling should proceed to the renderer, through the gesture
1508   // event filter.
1509   SimulateGestureFlingStartEvent(10.f, 0.f, WebGestureEvent::Touchpad);
1510   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1511   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1512   EXPECT_EQ(1U, process_->sink().message_count());
1513 }
1514
1515 // Tests that a fling in the opposite direction of the overscroll cancels the
1516 // overscroll nav instead of completing it.
1517 TEST_F(RenderWidgetHostTest, ReverseFlingCancelsOverscroll) {
1518   host_->SetupForOverscrollControllerTest();
1519   process_->sink().ClearMessages();
1520   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
1521   view_->Show();
1522
1523   {
1524     // Start and end a gesture in the same direction without processing the
1525     // gesture events in the renderer. This should initiate and complete an
1526     // overscroll navigation.
1527     SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1528                          WebGestureEvent::Touchscreen);
1529     SimulateGestureScrollUpdateEvent(300, -5, 0);
1530     SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1531                       INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1532     EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1533     EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1534     process_->sink().ClearMessages();
1535
1536     SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1537                          WebGestureEvent::Touchscreen);
1538     EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->completed_mode());
1539     EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1540     EXPECT_EQ(1U, process_->sink().message_count());
1541   }
1542
1543   {
1544     // Start over, except instead of ending the gesture with ScrollEnd, end it
1545     // with a FlingStart, with velocity in the reverse direction. This should
1546     // initiate an overscroll navigation, but it should be cancelled because of
1547     // the fling in the opposite direction.
1548     host_->overscroll_delegate()->Reset();
1549     SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1550                          WebGestureEvent::Touchscreen);
1551     SimulateGestureScrollUpdateEvent(-300, -5, 0);
1552     SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1553                       INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1554     EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
1555     EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_delegate()->current_mode());
1556     process_->sink().ClearMessages();
1557
1558     SimulateGestureFlingStartEvent(100, 0, WebGestureEvent::Touchscreen);
1559     EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->completed_mode());
1560     EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1561     EXPECT_EQ(1U, process_->sink().message_count());
1562   }
1563 }
1564
1565 // Tests that touch-scroll events are handled correctly by the overscroll
1566 // controller. This also tests that the overscroll controller and the
1567 // gesture-event filter play nice with each other.
1568 TEST_F(RenderWidgetHostTest, GestureScrollOverscrolls) {
1569   // Turn off debounce handling for test isolation.
1570   host_->SetupForOverscrollControllerTest();
1571   process_->sink().ClearMessages();
1572
1573   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1574                        WebGestureEvent::Touchscreen);
1575   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1576   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1577
1578   // Send another gesture event and ACK as not being processed. This should
1579   // initiate the navigation gesture.
1580   SimulateGestureScrollUpdateEvent(55, -5, 0);
1581   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1582                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1583   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1584   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1585   EXPECT_EQ(55.f, host_->overscroll_delta_x());
1586   EXPECT_EQ(-5.f, host_->overscroll_delta_y());
1587   EXPECT_EQ(5.f, host_->overscroll_delegate()->delta_x());
1588   EXPECT_EQ(-5.f, host_->overscroll_delegate()->delta_y());
1589   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1590   process_->sink().ClearMessages();
1591
1592   // Send another gesture update event. This event should be consumed by the
1593   // controller, and not be forwarded to the renderer. The gesture-event filter
1594   // should not also receive this event.
1595   SimulateGestureScrollUpdateEvent(10, -5, 0);
1596   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1597   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1598   EXPECT_EQ(65.f, host_->overscroll_delta_x());
1599   EXPECT_EQ(-10.f, host_->overscroll_delta_y());
1600   EXPECT_EQ(15.f, host_->overscroll_delegate()->delta_x());
1601   EXPECT_EQ(-10.f, host_->overscroll_delegate()->delta_y());
1602   EXPECT_EQ(0U, process_->sink().message_count());
1603   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1604
1605   // Now send a scroll end. This should cancel the overscroll gesture, and send
1606   // the event to the renderer. The gesture-event filter should receive this
1607   // event.
1608   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1609                        WebGestureEvent::Touchscreen);
1610   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1611   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1612   EXPECT_EQ(1U, process_->sink().message_count());
1613   // The scroll end event will have received a synthetic ack from the input
1614   // router.
1615   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1616 }
1617
1618 // Tests that if the page is scrolled because of a scroll-gesture, then that
1619 // particular scroll sequence never generates overscroll if the scroll direction
1620 // is horizontal.
1621 TEST_F(RenderWidgetHostTest, GestureScrollConsumedHorizontal) {
1622   // Turn off debounce handling for test isolation.
1623   host_->SetupForOverscrollControllerTest();
1624   process_->sink().ClearMessages();
1625
1626   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1627                        WebGestureEvent::Touchscreen);
1628   SimulateGestureScrollUpdateEvent(10, 0, 0);
1629
1630   // Start scrolling on content. ACK both events as being processed.
1631   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1632                     INPUT_EVENT_ACK_STATE_CONSUMED);
1633   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1634   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1635   process_->sink().ClearMessages();
1636
1637   // Send another gesture event and ACK as not being processed. This should
1638   // not initiate overscroll because the beginning of the scroll event did
1639   // scroll some content on the page. Since there was no overscroll, the event
1640   // should reach the renderer.
1641   SimulateGestureScrollUpdateEvent(55, 0, 0);
1642   EXPECT_EQ(1U, process_->sink().message_count());
1643   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1644                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1645   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1646 }
1647
1648 // Tests that the overscroll controller plays nice with touch-scrolls and the
1649 // gesture event filter with debounce filtering turned on.
1650 TEST_F(RenderWidgetHostTest, GestureScrollDebounceOverscrolls) {
1651   host_->SetupForOverscrollControllerTest();
1652   host_->set_debounce_interval_time_ms(100);
1653   process_->sink().ClearMessages();
1654
1655   // Start scrolling. Receive ACK as it being processed.
1656   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1657                        WebGestureEvent::Touchscreen);
1658   EXPECT_EQ(1U, process_->sink().message_count());
1659   process_->sink().ClearMessages();
1660
1661   // Send update events.
1662   SimulateGestureScrollUpdateEvent(25, 0, 0);
1663   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1664   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1665   EXPECT_TRUE(host_->ScrollingInProgress());
1666   EXPECT_EQ(1U, process_->sink().message_count());
1667   process_->sink().ClearMessages();
1668
1669   // Quickly end and restart the scroll gesture. These two events should get
1670   // discarded.
1671   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1672                        WebGestureEvent::Touchscreen);
1673   EXPECT_EQ(0U, process_->sink().message_count());
1674   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1675   EXPECT_EQ(1U, host_->GestureEventDebouncingQueueSize());
1676
1677   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1678                        WebGestureEvent::Touchscreen);
1679   EXPECT_EQ(0U, process_->sink().message_count());
1680   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1681   EXPECT_EQ(2U, host_->GestureEventDebouncingQueueSize());
1682
1683   // Send another update event. This should get into the queue.
1684   SimulateGestureScrollUpdateEvent(30, 0, 0);
1685   EXPECT_EQ(0U, process_->sink().message_count());
1686   EXPECT_EQ(2U, host_->GestureEventLastQueueEventSize());
1687   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1688   EXPECT_TRUE(host_->ScrollingInProgress());
1689
1690   // Receive an ACK for the first scroll-update event as not being processed.
1691   // This will contribute to the overscroll gesture, but not enough for the
1692   // overscroll controller to start consuming gesture events. This also cause
1693   // the queued gesture event to be forwarded to the renderer.
1694   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1695                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1696   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1697   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1698   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1699   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1700   EXPECT_EQ(1U, process_->sink().message_count());
1701   process_->sink().ClearMessages();
1702
1703   // Send another update event. This should get into the queue.
1704   SimulateGestureScrollUpdateEvent(10, 0, 0);
1705   EXPECT_EQ(0U, process_->sink().message_count());
1706   EXPECT_EQ(2U, host_->GestureEventLastQueueEventSize());
1707   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1708   EXPECT_TRUE(host_->ScrollingInProgress());
1709
1710   // Receive an ACK for the second scroll-update event as not being processed.
1711   // This will now initiate an overscroll. This will also cause the queued
1712   // gesture event to be released. But instead of going to the renderer, it will
1713   // be consumed by the overscroll controller.
1714   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1715                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1716   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1717   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1718   EXPECT_EQ(65.f, host_->overscroll_delta_x());
1719   EXPECT_EQ(15.f, host_->overscroll_delegate()->delta_x());
1720   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1721   EXPECT_EQ(0U, process_->sink().message_count());
1722   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1723   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1724 }
1725
1726 // Tests that the gesture debounce timer plays nice with the overscroll
1727 // controller.
1728 TEST_F(RenderWidgetHostTest, GestureScrollDebounceTimerOverscroll) {
1729   host_->SetupForOverscrollControllerTest();
1730   host_->set_debounce_interval_time_ms(10);
1731   process_->sink().ClearMessages();
1732
1733   // Start scrolling. Receive ACK as it being processed.
1734   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1735                        WebGestureEvent::Touchscreen);
1736   EXPECT_EQ(1U, process_->sink().message_count());
1737   process_->sink().ClearMessages();
1738
1739   // Send update events.
1740   SimulateGestureScrollUpdateEvent(55, 0, 0);
1741   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1742   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1743   EXPECT_TRUE(host_->ScrollingInProgress());
1744   EXPECT_EQ(1U, process_->sink().message_count());
1745   process_->sink().ClearMessages();
1746
1747   // Send an end event. This should get in the debounce queue.
1748   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
1749                        WebGestureEvent::Touchscreen);
1750   EXPECT_EQ(0U, process_->sink().message_count());
1751   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1752   EXPECT_EQ(1U, host_->GestureEventDebouncingQueueSize());
1753
1754   // Receive ACK for the scroll-update event.
1755   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1756                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1757   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1758   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1759   EXPECT_EQ(55.f, host_->overscroll_delta_x());
1760   EXPECT_EQ(5.f, host_->overscroll_delegate()->delta_x());
1761   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1762   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1763   EXPECT_EQ(1U, host_->GestureEventDebouncingQueueSize());
1764   EXPECT_EQ(0U, process_->sink().message_count());
1765
1766   // Let the timer for the debounce queue fire. That should release the queued
1767   // scroll-end event. Since overscroll has started, but there hasn't been
1768   // enough overscroll to complete the gesture, the overscroll controller
1769   // will reset the state. The scroll-end should therefore be dispatched to the
1770   // renderer, and the gesture-event-filter should await an ACK for it.
1771   base::MessageLoop::current()->PostDelayedTask(
1772       FROM_HERE,
1773       base::MessageLoop::QuitClosure(),
1774       TimeDelta::FromMilliseconds(15));
1775   base::MessageLoop::current()->Run();
1776
1777   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1778   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1779   // The scroll end event will have received a synthetic ack from the input
1780   // router.
1781   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1782   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1783   EXPECT_EQ(1U, process_->sink().message_count());
1784 }
1785
1786 // Tests that when touch-events are dispatched to the renderer, the overscroll
1787 // gesture deals with them correctly.
1788 TEST_F(RenderWidgetHostTest, OverscrollWithTouchEvents) {
1789   host_->SetupForOverscrollControllerTest();
1790   host_->set_debounce_interval_time_ms(10);
1791   host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
1792   process_->sink().ClearMessages();
1793   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
1794   view_->Show();
1795
1796   // The test sends an intermingled sequence of touch and gesture events.
1797
1798   PressTouchPoint(0, 1);
1799   SendTouchEvent();
1800   EXPECT_EQ(1U, process_->sink().message_count());
1801   process_->sink().ClearMessages();
1802   SendInputEventACK(WebInputEvent::TouchStart,
1803                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1804
1805   MoveTouchPoint(0, 20, 5);
1806   SendTouchEvent();
1807   EXPECT_EQ(1U, process_->sink().message_count());
1808   process_->sink().ClearMessages();
1809   SendInputEventACK(WebInputEvent::TouchMove,
1810                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1811
1812   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1813   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1814
1815   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1816                        WebGestureEvent::Touchscreen);
1817   SimulateGestureScrollUpdateEvent(20, 0, 0);
1818   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1819                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1820   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1821   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1822   process_->sink().ClearMessages();
1823
1824   // Another touch move event should reach the renderer since overscroll hasn't
1825   // started yet.
1826   MoveTouchPoint(0, 65, 10);
1827   SendTouchEvent();
1828   EXPECT_EQ(1U, process_->sink().message_count());
1829   process_->sink().ClearMessages();
1830
1831   SendInputEventACK(WebInputEvent::TouchMove,
1832                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1833   SimulateGestureScrollUpdateEvent(45, 0, 0);
1834   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1835                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1836   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1837   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1838   EXPECT_EQ(65.f, host_->overscroll_delta_x());
1839   EXPECT_EQ(15.f, host_->overscroll_delegate()->delta_x());
1840   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1841   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1842   process_->sink().ClearMessages();
1843
1844   // Send another touch event. The page should get the touch-move event, even
1845   // though overscroll has started.
1846   MoveTouchPoint(0, 55, 5);
1847   SendTouchEvent();
1848   EXPECT_EQ(1U, process_->sink().message_count());
1849   EXPECT_FALSE(host_->TouchEventQueueEmpty());
1850   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1851   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1852   EXPECT_EQ(65.f, host_->overscroll_delta_x());
1853   EXPECT_EQ(15.f, host_->overscroll_delegate()->delta_x());
1854   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1855
1856   SendInputEventACK(WebInputEvent::TouchMove,
1857                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1858   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1859   process_->sink().ClearMessages();
1860
1861   SimulateGestureScrollUpdateEvent(-10, 0, 0);
1862   EXPECT_EQ(0U, process_->sink().message_count());
1863   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1864   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1865   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1866   EXPECT_EQ(55.f, host_->overscroll_delta_x());
1867   EXPECT_EQ(5.f, host_->overscroll_delegate()->delta_x());
1868   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1869
1870   MoveTouchPoint(0, 255, 5);
1871   SendTouchEvent();
1872   EXPECT_EQ(1U, process_->sink().message_count());
1873   EXPECT_FALSE(host_->TouchEventQueueEmpty());
1874   process_->sink().ClearMessages();
1875   SendInputEventACK(WebInputEvent::TouchMove,
1876                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1877
1878   SimulateGestureScrollUpdateEvent(200, 0, 0);
1879   EXPECT_EQ(0U, process_->sink().message_count());
1880   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1881   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1882   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1883   EXPECT_EQ(255.f, host_->overscroll_delta_x());
1884   EXPECT_EQ(205.f, host_->overscroll_delegate()->delta_x());
1885   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
1886
1887   // The touch-end/cancel event should always reach the renderer if the page has
1888   // touch handlers.
1889   ReleaseTouchPoint(0);
1890   SendTouchEvent();
1891   EXPECT_EQ(1U, process_->sink().message_count());
1892   EXPECT_FALSE(host_->TouchEventQueueEmpty());
1893   process_->sink().ClearMessages();
1894
1895   SendInputEventACK(WebInputEvent::TouchEnd,
1896                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1897   EXPECT_EQ(0U, process_->sink().message_count());
1898   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1899
1900   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
1901                        WebGestureEvent::Touchscreen);
1902   base::MessageLoop::current()->PostDelayedTask(
1903       FROM_HERE,
1904       base::MessageLoop::QuitClosure(),
1905       TimeDelta::FromMilliseconds(10));
1906   base::MessageLoop::current()->Run();
1907   EXPECT_EQ(1U, process_->sink().message_count());
1908   EXPECT_TRUE(host_->TouchEventQueueEmpty());
1909   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1910   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1911   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->completed_mode());
1912 }
1913
1914 // Tests that touch-gesture end is dispatched to the renderer at the end of a
1915 // touch-gesture initiated overscroll.
1916 TEST_F(RenderWidgetHostTest, TouchGestureEndDispatchedAfterOverscrollComplete) {
1917   host_->SetupForOverscrollControllerTest();
1918   host_->set_debounce_interval_time_ms(10);
1919   host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
1920   process_->sink().ClearMessages();
1921   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
1922   view_->Show();
1923
1924   // Start scrolling. Receive ACK as it being processed.
1925   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1926                        WebGestureEvent::Touchscreen);
1927   EXPECT_EQ(1U, process_->sink().message_count());
1928   // The scroll begin event will have received a synthetic ack from the input
1929   // router.
1930   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1931   process_->sink().ClearMessages();
1932   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1933   EXPECT_EQ(0U, process_->sink().message_count());
1934   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1935   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1936
1937   // Send update events.
1938   SimulateGestureScrollUpdateEvent(55, -5, 0);
1939   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1940   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1941   EXPECT_TRUE(host_->ScrollingInProgress());
1942   EXPECT_EQ(1U, process_->sink().message_count());
1943   process_->sink().ClearMessages();
1944   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1945   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1946
1947   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
1948                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
1949   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1950   EXPECT_EQ(0U, process_->sink().message_count());
1951   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
1952   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
1953   EXPECT_EQ(55.f, host_->overscroll_delta_x());
1954   EXPECT_EQ(5.f, host_->overscroll_delegate()->delta_x());
1955   EXPECT_EQ(-5.f, host_->overscroll_delegate()->delta_y());
1956
1957   // Send end event.
1958   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
1959                        WebGestureEvent::Touchscreen);
1960   EXPECT_EQ(0U, process_->sink().message_count());
1961   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1962   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1963   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->completed_mode());
1964   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1965   EXPECT_EQ(1U, host_->GestureEventDebouncingQueueSize());
1966   base::MessageLoop::current()->PostDelayedTask(
1967       FROM_HERE,
1968       base::MessageLoop::QuitClosure(),
1969       TimeDelta::FromMilliseconds(10));
1970   base::MessageLoop::current()->Run();
1971   EXPECT_EQ(1U, process_->sink().message_count());
1972   process_->sink().ClearMessages();
1973   // The scroll end event will have received a synthetic ack from the input
1974   // router.
1975   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1976   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1977   EXPECT_EQ(0U, process_->sink().message_count());
1978
1979   // Start scrolling. Receive ACK as it being processed.
1980   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
1981                        WebGestureEvent::Touchscreen);
1982   EXPECT_EQ(1U, process_->sink().message_count());
1983   // The scroll begin event will have received a synthetic ack from the input
1984   // router.
1985   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1986   process_->sink().ClearMessages();
1987   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
1988   EXPECT_EQ(0U, process_->sink().message_count());
1989   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
1990   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
1991
1992   // Send update events.
1993   SimulateGestureScrollUpdateEvent(235, -5, 0);
1994   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
1995   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
1996   EXPECT_TRUE(host_->ScrollingInProgress());
1997   EXPECT_EQ(1U, process_->sink().message_count());
1998   process_->sink().ClearMessages();
1999   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2000   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2001
2002   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2003                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2004   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
2005   EXPECT_EQ(0U, process_->sink().message_count());
2006   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
2007   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
2008   EXPECT_EQ(235.f, host_->overscroll_delta_x());
2009   EXPECT_EQ(185.f, host_->overscroll_delegate()->delta_x());
2010   EXPECT_EQ(-5.f, host_->overscroll_delegate()->delta_y());
2011
2012   // Send end event.
2013   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
2014                        WebGestureEvent::Touchscreen);
2015   EXPECT_EQ(0U, process_->sink().message_count());
2016   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2017   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2018   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->completed_mode());
2019   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
2020   EXPECT_EQ(1U, host_->GestureEventDebouncingQueueSize());
2021
2022   base::MessageLoop::current()->PostDelayedTask(
2023       FROM_HERE,
2024       base::MessageLoop::QuitClosure(),
2025       TimeDelta::FromMilliseconds(10));
2026   base::MessageLoop::current()->Run();
2027   EXPECT_EQ(1U, process_->sink().message_count());
2028   process_->sink().ClearMessages();
2029   // The scroll end event will have received a synthetic ack from the input
2030   // router.
2031   EXPECT_EQ(0U, host_->GestureEventLastQueueEventSize());
2032   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
2033   EXPECT_EQ(0U, process_->sink().message_count());
2034 }
2035
2036 TEST_F(RenderWidgetHostTest, OverscrollDirectionChange) {
2037   host_->SetupForOverscrollControllerTest();
2038   host_->set_debounce_interval_time_ms(100);
2039   process_->sink().ClearMessages();
2040
2041   // Start scrolling. Receive ACK as it being processed.
2042   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2043                        WebGestureEvent::Touchscreen);
2044   EXPECT_EQ(1U, process_->sink().message_count());
2045   process_->sink().ClearMessages();
2046
2047   // Send update events and receive ack as not consumed.
2048   SimulateGestureScrollUpdateEvent(125, -5, 0);
2049   EXPECT_EQ(1U, host_->GestureEventLastQueueEventSize());
2050   EXPECT_EQ(0U, host_->GestureEventDebouncingQueueSize());
2051   EXPECT_TRUE(host_->ScrollingInProgress());
2052   EXPECT_EQ(1U, process_->sink().message_count());
2053   process_->sink().ClearMessages();
2054
2055   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2056                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2057   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
2058   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
2059   EXPECT_EQ(0U, process_->sink().message_count());
2060
2061   // Send another update event, but in the reverse direction. The overscroll
2062   // controller will consume the event, and reset the overscroll mode.
2063   SimulateGestureScrollUpdateEvent(-260, 0, 0);
2064   EXPECT_EQ(0U, process_->sink().message_count());
2065   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2066
2067   // Since the overscroll mode has been reset, the next scroll update events
2068   // should reach the renderer.
2069   SimulateGestureScrollUpdateEvent(-20, 0, 0);
2070   EXPECT_EQ(1U, process_->sink().message_count());
2071   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2072 }
2073
2074 // Tests that if a mouse-move event completes the overscroll gesture, future
2075 // move events do reach the renderer.
2076 TEST_F(RenderWidgetHostTest, OverscrollMouseMoveCompletion) {
2077   host_->SetupForOverscrollControllerTest();
2078   process_->sink().ClearMessages();
2079   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
2080   view_->Show();
2081
2082   SimulateWheelEvent(5, 0, 0, true);  // sent directly
2083   SimulateWheelEvent(-1, 0, 0, true);  // enqueued
2084   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
2085   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
2086   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
2087   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2088   EXPECT_EQ(1U, process_->sink().message_count());
2089   process_->sink().ClearMessages();
2090
2091   // Receive ACK the first wheel event as not processed.
2092   SendInputEventACK(WebInputEvent::MouseWheel,
2093                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2094   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2095   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2096   EXPECT_EQ(1U, process_->sink().message_count());
2097   process_->sink().ClearMessages();
2098
2099   // Receive ACK for the second (coalesced) event as not processed. This will
2100   // start an overcroll gesture.
2101   SendInputEventACK(WebInputEvent::MouseWheel,
2102                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2103   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
2104   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_delegate()->current_mode());
2105   EXPECT_EQ(0U, process_->sink().message_count());
2106
2107   // Send a mouse-move event. This should cancel the overscroll navigation
2108   // (since the amount overscrolled is not above the threshold), and so the
2109   // mouse-move should reach the renderer.
2110   SimulateMouseMove(5, 10, 0);
2111   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2112   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->completed_mode());
2113   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2114   EXPECT_EQ(1U, process_->sink().message_count());
2115   process_->sink().ClearMessages();
2116
2117   SendInputEventACK(WebInputEvent::MouseMove,
2118                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2119
2120   // Moving the mouse more should continue to send the events to the renderer.
2121   SimulateMouseMove(5, 10, 0);
2122   SendInputEventACK(WebInputEvent::MouseMove,
2123                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2124   EXPECT_EQ(1U, process_->sink().message_count());
2125   process_->sink().ClearMessages();
2126
2127   // Now try with gestures.
2128   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2129                        WebGestureEvent::Touchscreen);
2130   SimulateGestureScrollUpdateEvent(300, -5, 0);
2131   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2132                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2133   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
2134   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
2135   process_->sink().ClearMessages();
2136
2137   // Overscroll gesture is in progress. Send a mouse-move now. This should
2138   // complete the gesture (because the amount overscrolled is above the
2139   // threshold).
2140   SimulateMouseMove(5, 10, 0);
2141   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->completed_mode());
2142   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2143   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2144   EXPECT_EQ(1U, process_->sink().message_count());
2145   process_->sink().ClearMessages();
2146   SendInputEventACK(WebInputEvent::MouseMove,
2147                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2148
2149   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
2150                        WebGestureEvent::Touchscreen);
2151   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2152   EXPECT_EQ(1U, process_->sink().message_count());
2153   process_->sink().ClearMessages();
2154
2155   // Move mouse some more. The mouse-move events should reach the renderer.
2156   SimulateMouseMove(5, 10, 0);
2157   EXPECT_EQ(1U, process_->sink().message_count());
2158
2159   SendInputEventACK(WebInputEvent::MouseMove,
2160                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2161   process_->sink().ClearMessages();
2162 }
2163
2164 // Tests that if a page scrolled, then the overscroll controller's states are
2165 // reset after the end of the scroll.
2166 TEST_F(RenderWidgetHostTest, OverscrollStateResetsAfterScroll) {
2167   host_->SetupForOverscrollControllerTest();
2168   process_->sink().ClearMessages();
2169   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
2170   view_->Show();
2171
2172   SimulateWheelEvent(0, 5, 0, true);  // sent directly
2173   SimulateWheelEvent(0, 30, 0, true);  // enqueued
2174   SimulateWheelEvent(0, 40, 0, true);  // coalesced into previous event
2175   SimulateWheelEvent(0, 10, 0, true);  // coalesced into previous event
2176   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2177   EXPECT_EQ(1U, process_->sink().message_count());
2178   process_->sink().ClearMessages();
2179
2180   // The first wheel event is consumed. Dispatches the queued wheel event.
2181   SendInputEventACK(WebInputEvent::MouseWheel,
2182                     INPUT_EVENT_ACK_STATE_CONSUMED);
2183   EXPECT_TRUE(host_->ScrollStateIsContentScrolling());
2184   EXPECT_EQ(1U, process_->sink().message_count());
2185   process_->sink().ClearMessages();
2186
2187   // The second wheel event is consumed.
2188   SendInputEventACK(WebInputEvent::MouseWheel,
2189                     INPUT_EVENT_ACK_STATE_CONSUMED);
2190   EXPECT_TRUE(host_->ScrollStateIsContentScrolling());
2191
2192   // Touchpad scroll can end with a zero-velocity fling. But it is not
2193   // dispatched, but it should still reset the overscroll controller state.
2194   SimulateGestureFlingStartEvent(0.f, 0.f, WebGestureEvent::Touchpad);
2195   EXPECT_TRUE(host_->ScrollStateIsUnknown());
2196   EXPECT_EQ(0U, process_->sink().message_count());
2197
2198   SimulateWheelEvent(-5, 0, 0, true);  // sent directly
2199   SimulateWheelEvent(-60, 0, 0, true);  // enqueued
2200   SimulateWheelEvent(-100, 0, 0, true);  // coalesced into previous event
2201   EXPECT_EQ(1U, process_->sink().message_count());
2202   EXPECT_TRUE(host_->ScrollStateIsUnknown());
2203   process_->sink().ClearMessages();
2204
2205   // The first wheel scroll did not scroll content. Overscroll should not start
2206   // yet, since enough hasn't been scrolled.
2207   SendInputEventACK(WebInputEvent::MouseWheel,
2208                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2209   EXPECT_TRUE(host_->ScrollStateIsUnknown());
2210   EXPECT_EQ(1U, process_->sink().message_count());
2211   process_->sink().ClearMessages();
2212
2213   SendInputEventACK(WebInputEvent::MouseWheel,
2214                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2215   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_mode());
2216   EXPECT_TRUE(host_->ScrollStateIsOverscrolling());
2217   EXPECT_EQ(0U, process_->sink().message_count());
2218
2219   SimulateGestureFlingStartEvent(0.f, 0.f, WebGestureEvent::Touchpad);
2220   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2221   EXPECT_EQ(OVERSCROLL_WEST, host_->overscroll_delegate()->completed_mode());
2222   EXPECT_TRUE(host_->ScrollStateIsUnknown());
2223   EXPECT_EQ(0U, process_->sink().message_count());
2224   process_->sink().ClearMessages();
2225 }
2226
2227 TEST_F(RenderWidgetHostTest, OverscrollResetsOnBlur) {
2228   host_->SetupForOverscrollControllerTest();
2229   process_->sink().ClearMessages();
2230   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
2231   view_->Show();
2232
2233   // Start an overscroll with gesture scroll. In the middle of the scroll, blur
2234   // the host.
2235   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2236                        WebGestureEvent::Touchscreen);
2237   SimulateGestureScrollUpdateEvent(300, -5, 0);
2238   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2239                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2240   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
2241   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
2242   EXPECT_EQ(2U, process_->sink().message_count());
2243
2244   host_->Blur();
2245   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_mode());
2246   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2247   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->completed_mode());
2248   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_x());
2249   EXPECT_EQ(0.f, host_->overscroll_delegate()->delta_y());
2250   process_->sink().ClearMessages();
2251
2252   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
2253                        WebGestureEvent::Touchscreen);
2254   EXPECT_EQ(1U, process_->sink().message_count());
2255   process_->sink().ClearMessages();
2256
2257   // Start a scroll gesture again. This should correctly start the overscroll
2258   // after the threshold.
2259   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2260                        WebGestureEvent::Touchscreen);
2261   SimulateGestureScrollUpdateEvent(300, -5, 0);
2262   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2263                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
2264   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_mode());
2265   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->current_mode());
2266   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->completed_mode());
2267
2268   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
2269                        WebGestureEvent::Touchscreen);
2270   EXPECT_EQ(OVERSCROLL_NONE, host_->overscroll_delegate()->current_mode());
2271   EXPECT_EQ(OVERSCROLL_EAST, host_->overscroll_delegate()->completed_mode());
2272   EXPECT_EQ(3U, process_->sink().message_count());
2273 }
2274
2275 std::string GetInputMessageTypes(RenderWidgetHostProcess* process) {
2276   const WebInputEvent* event = NULL;
2277   ui::LatencyInfo latency_info;
2278   bool is_keyboard_shortcut;
2279   std::string result;
2280   for (size_t i = 0; i < process->sink().message_count(); ++i) {
2281     const IPC::Message *message = process->sink().GetMessageAt(i);
2282     EXPECT_EQ(InputMsg_HandleInputEvent::ID, message->type());
2283     EXPECT_TRUE(InputMsg_HandleInputEvent::Read(
2284         message, &event, &latency_info, &is_keyboard_shortcut));
2285     if (i != 0)
2286       result += " ";
2287     result += WebInputEventTraits::GetName(event->type);
2288   }
2289   process->sink().ClearMessages();
2290   return result;
2291 }
2292
2293 TEST_F(RenderWidgetHostTest, TouchEmulator) {
2294   simulated_event_time_delta_seconds_ = 0.1;
2295   // Immediately ack all touches instead of sending them to the renderer.
2296   host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, false));
2297   host_->OnMessageReceived(
2298       ViewHostMsg_SetTouchEventEmulationEnabled(0, true, true));
2299   process_->sink().ClearMessages();
2300   view_->set_bounds(gfx::Rect(0, 0, 400, 200));
2301   view_->Show();
2302
2303   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 10, 0, false);
2304   EXPECT_EQ(0U, process_->sink().message_count());
2305
2306   // Mouse press becomes touch start which in turn becomes tap.
2307   SimulateMouseEvent(WebInputEvent::MouseDown, 10, 10, 0, true);
2308   EXPECT_EQ(WebInputEvent::TouchStart, host_->acked_touch_event_type());
2309   EXPECT_EQ("GestureTapDown", GetInputMessageTypes(process_));
2310
2311   // Mouse drag generates touch move, cancels tap and starts scroll.
2312   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 30, 0, true);
2313   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2314   EXPECT_EQ(
2315       "GestureTapCancel GestureScrollBegin GestureScrollUpdate",
2316       GetInputMessageTypes(process_));
2317   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2318                     INPUT_EVENT_ACK_STATE_CONSUMED);
2319   EXPECT_EQ(0U, process_->sink().message_count());
2320
2321   // Mouse drag with shift becomes pinch.
2322   SimulateMouseEvent(
2323       WebInputEvent::MouseMove, 10, 40, WebInputEvent::ShiftKey, true);
2324   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2325   EXPECT_EQ("GesturePinchBegin",
2326             GetInputMessageTypes(process_));
2327   EXPECT_EQ(0U, process_->sink().message_count());
2328
2329   SimulateMouseEvent(
2330       WebInputEvent::MouseMove, 10, 50, WebInputEvent::ShiftKey, true);
2331   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2332   EXPECT_EQ("GesturePinchUpdate",
2333             GetInputMessageTypes(process_));
2334   SendInputEventACK(WebInputEvent::GesturePinchUpdate,
2335                     INPUT_EVENT_ACK_STATE_CONSUMED);
2336   EXPECT_EQ(0U, process_->sink().message_count());
2337
2338   // Mouse drag without shift becomes scroll again.
2339   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 60, 0, true);
2340   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2341   EXPECT_EQ("GesturePinchEnd GestureScrollUpdate",
2342             GetInputMessageTypes(process_));
2343   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2344                     INPUT_EVENT_ACK_STATE_CONSUMED);
2345   EXPECT_EQ(0U, process_->sink().message_count());
2346
2347   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 70, 0, true);
2348   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2349   EXPECT_EQ("GestureScrollUpdate",
2350             GetInputMessageTypes(process_));
2351   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2352                     INPUT_EVENT_ACK_STATE_CONSUMED);
2353   EXPECT_EQ(0U, process_->sink().message_count());
2354
2355   SimulateMouseEvent(WebInputEvent::MouseUp, 10, 70, 0, true);
2356   EXPECT_EQ(WebInputEvent::TouchEnd, host_->acked_touch_event_type());
2357   EXPECT_EQ("GestureScrollEnd", GetInputMessageTypes(process_));
2358   EXPECT_EQ(0U, process_->sink().message_count());
2359
2360   // Mouse move does nothing.
2361   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 80, 0, false);
2362   EXPECT_EQ(0U, process_->sink().message_count());
2363
2364   // Another mouse down continues scroll.
2365   SimulateMouseEvent(WebInputEvent::MouseDown, 10, 80, 0, true);
2366   EXPECT_EQ(WebInputEvent::TouchStart, host_->acked_touch_event_type());
2367   EXPECT_EQ("GestureTapDown", GetInputMessageTypes(process_));
2368   EXPECT_EQ(0U, process_->sink().message_count());
2369
2370   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 100, 0, true);
2371   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2372   EXPECT_EQ(
2373       "GestureTapCancel GestureScrollBegin GestureScrollUpdate",
2374       GetInputMessageTypes(process_));
2375   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2376                     INPUT_EVENT_ACK_STATE_CONSUMED);
2377   EXPECT_EQ(0U, process_->sink().message_count());
2378
2379   // Another pinch.
2380   SimulateMouseEvent(
2381       WebInputEvent::MouseMove, 10, 110, WebInputEvent::ShiftKey, true);
2382   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2383   EXPECT_EQ("GesturePinchBegin",
2384             GetInputMessageTypes(process_));
2385   EXPECT_EQ(0U, process_->sink().message_count());
2386
2387   SimulateMouseEvent(
2388       WebInputEvent::MouseMove, 10, 120, WebInputEvent::ShiftKey, true);
2389   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2390   EXPECT_EQ("GesturePinchUpdate",
2391             GetInputMessageTypes(process_));
2392   SendInputEventACK(WebInputEvent::GesturePinchUpdate,
2393                     INPUT_EVENT_ACK_STATE_CONSUMED);
2394   EXPECT_EQ(0U, process_->sink().message_count());
2395
2396   // Turn off emulation during a pinch.
2397   host_->OnMessageReceived(
2398       ViewHostMsg_SetTouchEventEmulationEnabled(0, false, false));
2399   EXPECT_EQ(WebInputEvent::TouchCancel, host_->acked_touch_event_type());
2400   EXPECT_EQ("GesturePinchEnd GestureScrollEnd",
2401             GetInputMessageTypes(process_));
2402   EXPECT_EQ(0U, process_->sink().message_count());
2403
2404   // Mouse event should pass untouched.
2405   SimulateMouseEvent(
2406       WebInputEvent::MouseMove, 10, 10, WebInputEvent::ShiftKey, true);
2407   EXPECT_EQ("MouseMove", GetInputMessageTypes(process_));
2408   SendInputEventACK(WebInputEvent::MouseMove,
2409                     INPUT_EVENT_ACK_STATE_CONSUMED);
2410   EXPECT_EQ(0U, process_->sink().message_count());
2411
2412   // Turn on emulation.
2413   host_->OnMessageReceived(
2414       ViewHostMsg_SetTouchEventEmulationEnabled(0, true, true));
2415   EXPECT_EQ(0U, process_->sink().message_count());
2416
2417   // Another touch.
2418   SimulateMouseEvent(WebInputEvent::MouseDown, 10, 10, 0, true);
2419   EXPECT_EQ(WebInputEvent::TouchStart, host_->acked_touch_event_type());
2420   EXPECT_EQ("GestureTapDown", GetInputMessageTypes(process_));
2421   EXPECT_EQ(0U, process_->sink().message_count());
2422
2423   // Scroll.
2424   SimulateMouseEvent(WebInputEvent::MouseMove, 10, 30, 0, true);
2425   EXPECT_EQ(WebInputEvent::TouchMove, host_->acked_touch_event_type());
2426   EXPECT_EQ(
2427       "GestureTapCancel GestureScrollBegin GestureScrollUpdate",
2428       GetInputMessageTypes(process_));
2429   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2430                     INPUT_EVENT_ACK_STATE_CONSUMED);
2431
2432   // Turn off emulation during a scroll.
2433   host_->OnMessageReceived(
2434       ViewHostMsg_SetTouchEventEmulationEnabled(0, false, false));
2435   EXPECT_EQ(WebInputEvent::TouchCancel, host_->acked_touch_event_type());
2436
2437   EXPECT_EQ("GestureScrollEnd", GetInputMessageTypes(process_));
2438   EXPECT_EQ(0U, process_->sink().message_count());
2439 }
2440
2441 #define TEST_InputRouterRoutes_NOARGS(INPUTMSG) \
2442   TEST_F(RenderWidgetHostTest, InputRouterRoutes##INPUTMSG) { \
2443     host_->SetupForInputRouterTest(); \
2444     host_->INPUTMSG(); \
2445     EXPECT_TRUE(host_->mock_input_router()->send_event_called_); \
2446   }
2447
2448 TEST_InputRouterRoutes_NOARGS(Focus);
2449 TEST_InputRouterRoutes_NOARGS(Blur);
2450 TEST_InputRouterRoutes_NOARGS(LostCapture);
2451
2452 #undef TEST_InputRouterRoutes_NOARGS
2453
2454 #define TEST_InputRouterRoutes_NOARGS_FromRFH(INPUTMSG) \
2455   TEST_F(RenderWidgetHostTest, InputRouterRoutes##INPUTMSG) { \
2456     host_->SetupForInputRouterTest(); \
2457     host_->Send(new INPUTMSG(host_->GetRoutingID())); \
2458     EXPECT_TRUE(host_->mock_input_router()->send_event_called_); \
2459   }
2460
2461 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Undo);
2462 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Redo);
2463 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Cut);
2464 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Copy);
2465 #if defined(OS_MACOSX)
2466 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_CopyToFindPboard);
2467 #endif
2468 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Paste);
2469 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_PasteAndMatchStyle);
2470 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Delete);
2471 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_SelectAll);
2472 TEST_InputRouterRoutes_NOARGS_FromRFH(InputMsg_Unselect);
2473
2474 #undef TEST_InputRouterRoutes_NOARGS_FromRFH
2475
2476 TEST_F(RenderWidgetHostTest, InputRouterRoutesReplace) {
2477   host_->SetupForInputRouterTest();
2478   host_->Send(new InputMsg_Replace(host_->GetRoutingID(), base::string16()));
2479   EXPECT_TRUE(host_->mock_input_router()->send_event_called_);
2480 }
2481
2482 TEST_F(RenderWidgetHostTest, InputRouterRoutesReplaceMisspelling) {
2483   host_->SetupForInputRouterTest();
2484   host_->Send(new InputMsg_ReplaceMisspelling(host_->GetRoutingID(),
2485                                               base::string16()));
2486   EXPECT_TRUE(host_->mock_input_router()->send_event_called_);
2487 }
2488
2489 TEST_F(RenderWidgetHostTest, IgnoreInputEvent) {
2490   host_->SetupForInputRouterTest();
2491
2492   host_->SetIgnoreInputEvents(true);
2493
2494   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
2495   EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
2496
2497   SimulateMouseEvent(WebInputEvent::MouseMove);
2498   EXPECT_FALSE(host_->mock_input_router()->sent_mouse_event_);
2499
2500   SimulateWheelEvent(0, 100, 0, true);
2501   EXPECT_FALSE(host_->mock_input_router()->sent_wheel_event_);
2502
2503   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2504                        WebGestureEvent::Touchscreen);
2505   EXPECT_FALSE(host_->mock_input_router()->sent_gesture_event_);
2506
2507   PressTouchPoint(100, 100);
2508   SendTouchEvent();
2509   EXPECT_FALSE(host_->mock_input_router()->send_touch_event_not_cancelled_);
2510 }
2511
2512 TEST_F(RenderWidgetHostTest, KeyboardListenerIgnoresEvent) {
2513   host_->SetupForInputRouterTest();
2514   host_->AddKeyPressEventCallback(
2515       base::Bind(&RenderWidgetHostTest::KeyPressEventCallback,
2516                  base::Unretained(this)));
2517   handle_key_press_event_ = false;
2518   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
2519
2520   EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
2521 }
2522
2523 TEST_F(RenderWidgetHostTest, KeyboardListenerSuppressFollowingEvents) {
2524   host_->SetupForInputRouterTest();
2525
2526   host_->AddKeyPressEventCallback(
2527       base::Bind(&RenderWidgetHostTest::KeyPressEventCallback,
2528                  base::Unretained(this)));
2529
2530   // The callback handles the first event
2531   handle_key_press_event_ = true;
2532   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
2533
2534   EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
2535
2536   // Following Char events should be suppressed
2537   handle_key_press_event_ = false;
2538   SimulateKeyboardEvent(WebInputEvent::Char);
2539   EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
2540   SimulateKeyboardEvent(WebInputEvent::Char);
2541   EXPECT_FALSE(host_->mock_input_router()->sent_keyboard_event_);
2542
2543   // Sending RawKeyDown event should stop suppression
2544   SimulateKeyboardEvent(WebInputEvent::RawKeyDown);
2545   EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
2546
2547   host_->mock_input_router()->sent_keyboard_event_ = false;
2548   SimulateKeyboardEvent(WebInputEvent::Char);
2549   EXPECT_TRUE(host_->mock_input_router()->sent_keyboard_event_);
2550 }
2551
2552 TEST_F(RenderWidgetHostTest, MouseEventCallbackCanHandleEvent) {
2553   host_->SetupForInputRouterTest();
2554
2555   host_->AddMouseEventCallback(
2556       base::Bind(&RenderWidgetHostTest::MouseEventCallback,
2557                  base::Unretained(this)));
2558
2559   handle_mouse_event_ = true;
2560   SimulateMouseEvent(WebInputEvent::MouseDown);
2561
2562   EXPECT_FALSE(host_->mock_input_router()->sent_mouse_event_);
2563
2564   handle_mouse_event_ = false;
2565   SimulateMouseEvent(WebInputEvent::MouseDown);
2566
2567   EXPECT_TRUE(host_->mock_input_router()->sent_mouse_event_);
2568 }
2569
2570 TEST_F(RenderWidgetHostTest, InputRouterReceivesHandleInputEvent_ACK) {
2571   host_->SetupForInputRouterTest();
2572
2573   SendInputEventACK(WebInputEvent::RawKeyDown,
2574                     INPUT_EVENT_ACK_STATE_CONSUMED);
2575
2576   EXPECT_TRUE(host_->mock_input_router()->message_received_);
2577 }
2578
2579 TEST_F(RenderWidgetHostTest, InputRouterReceivesMoveCaret_ACK) {
2580   host_->SetupForInputRouterTest();
2581
2582   host_->OnMessageReceived(ViewHostMsg_MoveCaret_ACK(0));
2583
2584   EXPECT_TRUE(host_->mock_input_router()->message_received_);
2585 }
2586
2587 TEST_F(RenderWidgetHostTest, InputRouterReceivesSelectRange_ACK) {
2588   host_->SetupForInputRouterTest();
2589
2590   host_->OnMessageReceived(ViewHostMsg_SelectRange_ACK(0));
2591
2592   EXPECT_TRUE(host_->mock_input_router()->message_received_);
2593 }
2594
2595 TEST_F(RenderWidgetHostTest, InputRouterReceivesHasTouchEventHandlers) {
2596   host_->SetupForInputRouterTest();
2597
2598   host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
2599
2600   EXPECT_TRUE(host_->mock_input_router()->message_received_);
2601 }
2602
2603
2604 void CheckLatencyInfoComponentInMessage(RenderWidgetHostProcess* process,
2605                                         int64 component_id,
2606                                         WebInputEvent::Type input_type) {
2607   const WebInputEvent* event = NULL;
2608   ui::LatencyInfo latency_info;
2609   bool is_keyboard_shortcut;
2610   const IPC::Message* message = process->sink().GetUniqueMessageMatching(
2611       InputMsg_HandleInputEvent::ID);
2612   ASSERT_TRUE(message);
2613   EXPECT_TRUE(InputMsg_HandleInputEvent::Read(
2614       message, &event, &latency_info, &is_keyboard_shortcut));
2615   EXPECT_TRUE(latency_info.FindLatency(
2616       ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT,
2617       component_id,
2618       NULL));
2619   process->sink().ClearMessages();
2620 }
2621
2622 // Tests that after input event passes through RWHI through ForwardXXXEvent()
2623 // or ForwardXXXEventWithLatencyInfo(), LatencyInfo component
2624 // ui::INPUT_EVENT_LATENCY_BEGIN_RWH_COMPONENT will always present in the
2625 // event's LatencyInfo.
2626 TEST_F(RenderWidgetHostTest, InputEventRWHLatencyComponent) {
2627   host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
2628   process_->sink().ClearMessages();
2629
2630   // Tests RWHI::ForwardWheelEvent().
2631   SimulateWheelEvent(-5, 0, 0, true);
2632   CheckLatencyInfoComponentInMessage(
2633       process_, GetLatencyComponentId(), WebInputEvent::MouseWheel);
2634   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
2635
2636   // Tests RWHI::ForwardWheelEventWithLatencyInfo().
2637   SimulateWheelEventWithLatencyInfo(-5, 0, 0, true, ui::LatencyInfo());
2638   CheckLatencyInfoComponentInMessage(
2639       process_, GetLatencyComponentId(), WebInputEvent::MouseWheel);
2640   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
2641
2642   // Tests RWHI::ForwardMouseEvent().
2643   SimulateMouseEvent(WebInputEvent::MouseMove);
2644   CheckLatencyInfoComponentInMessage(
2645       process_, GetLatencyComponentId(), WebInputEvent::MouseMove);
2646   SendInputEventACK(WebInputEvent::MouseMove, INPUT_EVENT_ACK_STATE_CONSUMED);
2647
2648   // Tests RWHI::ForwardMouseEventWithLatencyInfo().
2649   SimulateMouseEventWithLatencyInfo(WebInputEvent::MouseMove,
2650                                     ui::LatencyInfo());
2651   CheckLatencyInfoComponentInMessage(
2652       process_, GetLatencyComponentId(), WebInputEvent::MouseMove);
2653   SendInputEventACK(WebInputEvent::MouseMove, INPUT_EVENT_ACK_STATE_CONSUMED);
2654
2655   // Tests RWHI::ForwardGestureEvent().
2656   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
2657                        WebGestureEvent::Touchscreen);
2658   CheckLatencyInfoComponentInMessage(
2659       process_, GetLatencyComponentId(), WebInputEvent::GestureScrollBegin);
2660
2661   // Tests RWHI::ForwardGestureEventWithLatencyInfo().
2662   SimulateGestureEventWithLatencyInfo(WebInputEvent::GestureScrollUpdate,
2663                                       WebGestureEvent::Touchscreen,
2664                                       ui::LatencyInfo());
2665   CheckLatencyInfoComponentInMessage(
2666       process_, GetLatencyComponentId(), WebInputEvent::GestureScrollUpdate);
2667   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
2668                     INPUT_EVENT_ACK_STATE_CONSUMED);
2669
2670   // Tests RWHI::ForwardTouchEventWithLatencyInfo().
2671   PressTouchPoint(0, 1);
2672   SendTouchEvent();
2673   CheckLatencyInfoComponentInMessage(
2674       process_, GetLatencyComponentId(), WebInputEvent::TouchStart);
2675   SendInputEventACK(WebInputEvent::TouchStart, INPUT_EVENT_ACK_STATE_CONSUMED);
2676 }
2677
2678 }  // namespace content