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