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