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