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