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