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 "content/browser/renderer_host/render_widget_host_view_aura.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/command_line.h"
      9 #include "base/memory/shared_memory.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/run_loop.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "cc/output/compositor_frame.h"
     14 #include "cc/output/compositor_frame_metadata.h"
     15 #include "cc/output/copy_output_request.h"
     16 #include "content/browser/browser_thread_impl.h"
     17 #include "content/browser/compositor/resize_lock.h"
     18 #include "content/browser/renderer_host/overscroll_controller.h"
     19 #include "content/browser/renderer_host/overscroll_controller_delegate.h"
     20 #include "content/browser/renderer_host/render_widget_host_delegate.h"
     21 #include "content/browser/renderer_host/render_widget_host_impl.h"
     22 #include "content/common/gpu/client/gl_helper.h"
     23 #include "content/common/gpu/gpu_messages.h"
     24 #include "content/common/host_shared_bitmap_manager.h"
     25 #include "content/common/input/synthetic_web_input_event_builders.h"
     26 #include "content/common/input_messages.h"
     27 #include "content/common/view_messages.h"
     28 #include "content/public/browser/render_widget_host_view.h"
     29 #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
     30 #include "content/public/test/mock_render_process_host.h"
     31 #include "content/public/test/test_browser_context.h"
     32 #include "ipc/ipc_test_sink.h"
     33 #include "testing/gmock/include/gmock/gmock.h"
     34 #include "testing/gtest/include/gtest/gtest.h"
     35 #include "ui/aura/client/aura_constants.h"
     36 #include "ui/aura/client/screen_position_client.h"
     37 #include "ui/aura/client/window_tree_client.h"
     38 #include "ui/aura/env.h"
     39 #include "ui/aura/layout_manager.h"
     40 #include "ui/aura/test/aura_test_helper.h"
     41 #include "ui/aura/test/event_generator.h"
     42 #include "ui/aura/test/test_cursor_client.h"
     43 #include "ui/aura/test/test_screen.h"
     44 #include "ui/aura/test/test_window_delegate.h"
     45 #include "ui/aura/window.h"
     46 #include "ui/aura/window_event_dispatcher.h"
     47 #include "ui/aura/window_observer.h"
     48 #include "ui/base/ui_base_types.h"
     49 #include "ui/compositor/compositor.h"
     50 #include "ui/compositor/test/draw_waiter_for_test.h"
     51 #include "ui/compositor/test/in_process_context_factory.h"
     52 #include "ui/events/event.h"
     53 #include "ui/events/event_utils.h"
     54 #include "ui/events/gestures/gesture_configuration.h"
     55 #include "ui/wm/core/default_activation_client.h"
     56 
     57 using testing::_;
     58 
     59 using blink::WebGestureEvent;
     60 using blink::WebInputEvent;
     61 using blink::WebMouseEvent;
     62 using blink::WebMouseWheelEvent;
     63 using blink::WebTouchEvent;
     64 using blink::WebTouchPoint;
     65 
     66 namespace content {
     67 namespace {
     68 
     69 // Simple screen position client to test coordinate system conversion.
     70 class TestScreenPositionClient
     71     : public aura::client::ScreenPositionClient {
     72  public:
     73   TestScreenPositionClient() {}
     74   virtual ~TestScreenPositionClient() {}
     75 
     76   // aura::client::ScreenPositionClient overrides:
     77   virtual void ConvertPointToScreen(const aura::Window* window,
     78       gfx::Point* point) OVERRIDE {
     79     point->Offset(-1, -1);
     80   }
     81 
     82   virtual void ConvertPointFromScreen(const aura::Window* window,
     83       gfx::Point* point) OVERRIDE {
     84     point->Offset(1, 1);
     85   }
     86 
     87   virtual void ConvertHostPointToScreen(aura::Window* window,
     88       gfx::Point* point) OVERRIDE {
     89     ConvertPointToScreen(window, point);
     90   }
     91 
     92   virtual void SetBounds(aura::Window* window,
     93       const gfx::Rect& bounds,
     94       const gfx::Display& display) OVERRIDE {
     95   }
     96 };
     97 
     98 class TestOverscrollDelegate : public OverscrollControllerDelegate {
     99  public:
    100   explicit TestOverscrollDelegate(RenderWidgetHostView* view)
    101       : view_(view),
    102         current_mode_(OVERSCROLL_NONE),
    103         completed_mode_(OVERSCROLL_NONE),
    104         delta_x_(0.f),
    105         delta_y_(0.f) {}
    106 
    107   virtual ~TestOverscrollDelegate() {}
    108 
    109   OverscrollMode current_mode() const { return current_mode_; }
    110   OverscrollMode completed_mode() const { return completed_mode_; }
    111   float delta_x() const { return delta_x_; }
    112   float delta_y() const { return delta_y_; }
    113 
    114   void Reset() {
    115     current_mode_ = OVERSCROLL_NONE;
    116     completed_mode_ = OVERSCROLL_NONE;
    117     delta_x_ = delta_y_ = 0.f;
    118   }
    119 
    120  private:
    121   // Overridden from OverscrollControllerDelegate:
    122   virtual gfx::Rect GetVisibleBounds() const OVERRIDE {
    123     return view_->IsShowing() ? view_->GetViewBounds() : gfx::Rect();
    124   }
    125 
    126   virtual void OnOverscrollUpdate(float delta_x, float delta_y) OVERRIDE {
    127     delta_x_ = delta_x;
    128     delta_y_ = delta_y;
    129   }
    130 
    131   virtual void OnOverscrollComplete(OverscrollMode overscroll_mode) OVERRIDE {
    132     EXPECT_EQ(current_mode_, overscroll_mode);
    133     completed_mode_ = overscroll_mode;
    134     current_mode_ = OVERSCROLL_NONE;
    135   }
    136 
    137   virtual void OnOverscrollModeChange(OverscrollMode old_mode,
    138                                       OverscrollMode new_mode) OVERRIDE {
    139     EXPECT_EQ(current_mode_, old_mode);
    140     current_mode_ = new_mode;
    141     delta_x_ = delta_y_ = 0.f;
    142   }
    143 
    144   RenderWidgetHostView* view_;
    145   OverscrollMode current_mode_;
    146   OverscrollMode completed_mode_;
    147   float delta_x_;
    148   float delta_y_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(TestOverscrollDelegate);
    151 };
    152 
    153 class MockRenderWidgetHostDelegate : public RenderWidgetHostDelegate {
    154  public:
    155   MockRenderWidgetHostDelegate() {}
    156   virtual ~MockRenderWidgetHostDelegate() {}
    157 };
    158 
    159 // Simple observer that keeps track of changes to a window for tests.
    160 class TestWindowObserver : public aura::WindowObserver {
    161  public:
    162   explicit TestWindowObserver(aura::Window* window_to_observe)
    163       : window_(window_to_observe) {
    164     window_->AddObserver(this);
    165   }
    166   virtual ~TestWindowObserver() {
    167     if (window_)
    168       window_->RemoveObserver(this);
    169   }
    170 
    171   bool destroyed() const { return destroyed_; }
    172 
    173   // aura::WindowObserver overrides:
    174   virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
    175     CHECK_EQ(window, window_);
    176     destroyed_ = true;
    177     window_ = NULL;
    178   }
    179 
    180  private:
    181   // Window that we're observing, or NULL if it's been destroyed.
    182   aura::Window* window_;
    183 
    184   // Was |window_| destroyed?
    185   bool destroyed_;
    186 
    187   DISALLOW_COPY_AND_ASSIGN(TestWindowObserver);
    188 };
    189 
    190 class FakeFrameSubscriber : public RenderWidgetHostViewFrameSubscriber {
    191  public:
    192   FakeFrameSubscriber(gfx::Size size, base::Callback<void(bool)> callback)
    193       : size_(size), callback_(callback) {}
    194 
    195   virtual bool ShouldCaptureFrame(base::TimeTicks present_time,
    196                                   scoped_refptr<media::VideoFrame>* storage,
    197                                   DeliverFrameCallback* callback) OVERRIDE {
    198     *storage = media::VideoFrame::CreateFrame(media::VideoFrame::YV12,
    199                                               size_,
    200                                               gfx::Rect(size_),
    201                                               size_,
    202                                               base::TimeDelta());
    203     *callback = base::Bind(&FakeFrameSubscriber::CallbackMethod, callback_);
    204     return true;
    205   }
    206 
    207   static void CallbackMethod(base::Callback<void(bool)> callback,
    208                              base::TimeTicks timestamp,
    209                              bool success) {
    210     callback.Run(success);
    211   }
    212 
    213  private:
    214   gfx::Size size_;
    215   base::Callback<void(bool)> callback_;
    216 };
    217 
    218 class FakeRenderWidgetHostViewAura : public RenderWidgetHostViewAura {
    219  public:
    220   FakeRenderWidgetHostViewAura(RenderWidgetHost* widget)
    221       : RenderWidgetHostViewAura(widget), has_resize_lock_(false) {}
    222 
    223   virtual ~FakeRenderWidgetHostViewAura() {}
    224 
    225   virtual scoped_ptr<ResizeLock> CreateResizeLock(
    226       bool defer_compositor_lock) OVERRIDE {
    227     gfx::Size desired_size = window()->bounds().size();
    228     return scoped_ptr<ResizeLock>(
    229         new FakeResizeLock(desired_size, defer_compositor_lock));
    230   }
    231 
    232   void RunOnCompositingDidCommit() {
    233     GetDelegatedFrameHost()->OnCompositingDidCommitForTesting(
    234         window()->GetHost()->compositor());
    235   }
    236 
    237   virtual bool ShouldCreateResizeLock() OVERRIDE {
    238     return GetDelegatedFrameHost()->ShouldCreateResizeLockForTesting();
    239   }
    240 
    241   virtual void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request)
    242       OVERRIDE {
    243     last_copy_request_ = request.Pass();
    244     if (last_copy_request_->has_texture_mailbox()) {
    245       // Give the resulting texture a size.
    246       GLHelper* gl_helper = ImageTransportFactory::GetInstance()->GetGLHelper();
    247       GLuint texture = gl_helper->ConsumeMailboxToTexture(
    248           last_copy_request_->texture_mailbox().mailbox(),
    249           last_copy_request_->texture_mailbox().sync_point());
    250       gl_helper->ResizeTexture(texture, window()->bounds().size());
    251       gl_helper->DeleteTexture(texture);
    252     }
    253   }
    254 
    255   cc::DelegatedFrameProvider* frame_provider() const {
    256     return GetDelegatedFrameHost()->FrameProviderForTesting();
    257   }
    258 
    259   // A lock that doesn't actually do anything to the compositor, and does not
    260   // time out.
    261   class FakeResizeLock : public ResizeLock {
    262    public:
    263     FakeResizeLock(const gfx::Size new_size, bool defer_compositor_lock)
    264         : ResizeLock(new_size, defer_compositor_lock) {}
    265   };
    266 
    267   bool has_resize_lock_;
    268   gfx::Size last_frame_size_;
    269   scoped_ptr<cc::CopyOutputRequest> last_copy_request_;
    270 };
    271 
    272 // A layout manager that always resizes a child to the root window size.
    273 class FullscreenLayoutManager : public aura::LayoutManager {
    274  public:
    275   explicit FullscreenLayoutManager(aura::Window* owner) : owner_(owner) {}
    276   virtual ~FullscreenLayoutManager() {}
    277 
    278   // Overridden from aura::LayoutManager:
    279   virtual void OnWindowResized() OVERRIDE {
    280     aura::Window::Windows::const_iterator i;
    281     for (i = owner_->children().begin(); i != owner_->children().end(); ++i) {
    282       (*i)->SetBounds(gfx::Rect());
    283     }
    284   }
    285   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
    286     child->SetBounds(gfx::Rect());
    287   }
    288   virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
    289   virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
    290   virtual void OnChildWindowVisibilityChanged(aura::Window* child,
    291                                               bool visible) OVERRIDE {}
    292   virtual void SetChildBounds(aura::Window* child,
    293                               const gfx::Rect& requested_bounds) OVERRIDE {
    294     SetChildBoundsDirect(child, gfx::Rect(owner_->bounds().size()));
    295   }
    296 
    297  private:
    298   aura::Window* owner_;
    299   DISALLOW_COPY_AND_ASSIGN(FullscreenLayoutManager);
    300 };
    301 
    302 class MockWindowObserver : public aura::WindowObserver {
    303  public:
    304   MOCK_METHOD2(OnWindowPaintScheduled, void(aura::Window*, const gfx::Rect&));
    305 };
    306 
    307 }  // namespace
    308 
    309 class RenderWidgetHostViewAuraTest : public testing::Test {
    310  public:
    311   RenderWidgetHostViewAuraTest()
    312       : browser_thread_for_ui_(BrowserThread::UI, &message_loop_) {}
    313 
    314   void SetUpEnvironment() {
    315     ui::ContextFactory* context_factory = new ui::InProcessContextFactory;
    316     ImageTransportFactory::InitializeForUnitTests(
    317         scoped_ptr<ui::ContextFactory>(context_factory));
    318     aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
    319     aura_test_helper_->SetUp(context_factory);
    320     new wm::DefaultActivationClient(aura_test_helper_->root_window());
    321 
    322     browser_context_.reset(new TestBrowserContext);
    323     process_host_ = new MockRenderProcessHost(browser_context_.get());
    324 
    325     sink_ = &process_host_->sink();
    326 
    327     parent_host_ = new RenderWidgetHostImpl(
    328         &delegate_, process_host_, MSG_ROUTING_NONE, false);
    329     parent_view_ = new RenderWidgetHostViewAura(parent_host_);
    330     parent_view_->InitAsChild(NULL);
    331     aura::client::ParentWindowWithContext(parent_view_->GetNativeView(),
    332                                           aura_test_helper_->root_window(),
    333                                           gfx::Rect());
    334 
    335     widget_host_ = new RenderWidgetHostImpl(
    336         &delegate_, process_host_, MSG_ROUTING_NONE, false);
    337     widget_host_->Init();
    338     view_ = new FakeRenderWidgetHostViewAura(widget_host_);
    339   }
    340 
    341   void TearDownEnvironment() {
    342     sink_ = NULL;
    343     process_host_ = NULL;
    344     if (view_)
    345       view_->Destroy();
    346     delete widget_host_;
    347 
    348     parent_view_->Destroy();
    349     delete parent_host_;
    350 
    351     browser_context_.reset();
    352     aura_test_helper_->TearDown();
    353 
    354     message_loop_.DeleteSoon(FROM_HERE, browser_context_.release());
    355     message_loop_.RunUntilIdle();
    356     ImageTransportFactory::Terminate();
    357   }
    358 
    359   virtual void SetUp() { SetUpEnvironment(); }
    360 
    361   virtual void TearDown() { TearDownEnvironment(); }
    362 
    363  protected:
    364   base::MessageLoopForUI message_loop_;
    365   BrowserThreadImpl browser_thread_for_ui_;
    366   scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
    367   scoped_ptr<BrowserContext> browser_context_;
    368   MockRenderWidgetHostDelegate delegate_;
    369   MockRenderProcessHost* process_host_;
    370 
    371   // Tests should set these to NULL if they've already triggered their
    372   // destruction.
    373   RenderWidgetHostImpl* parent_host_;
    374   RenderWidgetHostViewAura* parent_view_;
    375 
    376   // Tests should set these to NULL if they've already triggered their
    377   // destruction.
    378   RenderWidgetHostImpl* widget_host_;
    379   FakeRenderWidgetHostViewAura* view_;
    380 
    381   IPC::TestSink* sink_;
    382 
    383  private:
    384   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAuraTest);
    385 };
    386 
    387 class RenderWidgetHostViewAuraOverscrollTest
    388     : public RenderWidgetHostViewAuraTest {
    389  public:
    390   RenderWidgetHostViewAuraOverscrollTest() {}
    391 
    392   // We explicitly invoke SetUp to allow gesture debounce customization.
    393   virtual void SetUp() {}
    394 
    395  protected:
    396   void SetUpOverscrollEnvironmentWithDebounce(int debounce_interval_in_ms) {
    397     SetUpOverscrollEnvironmentImpl(debounce_interval_in_ms);
    398   }
    399 
    400   void SetUpOverscrollEnvironment() { SetUpOverscrollEnvironmentImpl(0); }
    401 
    402   void SetUpOverscrollEnvironmentImpl(int debounce_interval_in_ms) {
    403     ui::GestureConfiguration::set_scroll_debounce_interval_in_ms(
    404         debounce_interval_in_ms);
    405 
    406     RenderWidgetHostViewAuraTest::SetUp();
    407 
    408     view_->SetOverscrollControllerEnabled(true);
    409     overscroll_delegate_.reset(new TestOverscrollDelegate(view_));
    410     view_->overscroll_controller()->set_delegate(overscroll_delegate_.get());
    411 
    412     view_->InitAsChild(NULL);
    413     view_->SetBounds(gfx::Rect(0, 0, 400, 200));
    414     view_->Show();
    415 
    416     sink_->ClearMessages();
    417   }
    418 
    419   // TODO(jdduke): Simulate ui::Events, injecting through the view.
    420   void SimulateMouseEvent(WebInputEvent::Type type) {
    421     widget_host_->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build(type));
    422   }
    423 
    424   void SimulateMouseEventWithLatencyInfo(WebInputEvent::Type type,
    425                                          const ui::LatencyInfo& ui_latency) {
    426     widget_host_->ForwardMouseEventWithLatencyInfo(
    427         SyntheticWebMouseEventBuilder::Build(type), ui_latency);
    428   }
    429 
    430   void SimulateWheelEvent(float dX, float dY, int modifiers, bool precise) {
    431     widget_host_->ForwardWheelEvent(
    432         SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise));
    433   }
    434 
    435   void SimulateWheelEventWithLatencyInfo(float dX,
    436                                          float dY,
    437                                          int modifiers,
    438                                          bool precise,
    439                                          const ui::LatencyInfo& ui_latency) {
    440     widget_host_->ForwardWheelEventWithLatencyInfo(
    441         SyntheticWebMouseWheelEventBuilder::Build(dX, dY, modifiers, precise),
    442         ui_latency);
    443   }
    444 
    445   void SimulateMouseMove(int x, int y, int modifiers) {
    446     SimulateMouseEvent(WebInputEvent::MouseMove, x, y, modifiers, false);
    447   }
    448 
    449   void SimulateMouseEvent(WebInputEvent::Type type,
    450                           int x,
    451                           int y,
    452                           int modifiers,
    453                           bool pressed) {
    454     WebMouseEvent event =
    455         SyntheticWebMouseEventBuilder::Build(type, x, y, modifiers);
    456     if (pressed)
    457       event.button = WebMouseEvent::ButtonLeft;
    458     widget_host_->ForwardMouseEvent(event);
    459   }
    460 
    461   void SimulateWheelEventWithPhase(WebMouseWheelEvent::Phase phase) {
    462     widget_host_->ForwardWheelEvent(
    463         SyntheticWebMouseWheelEventBuilder::Build(phase));
    464   }
    465 
    466   // Inject provided synthetic WebGestureEvent instance.
    467   void SimulateGestureEventCore(const WebGestureEvent& gesture_event) {
    468     widget_host_->ForwardGestureEvent(gesture_event);
    469   }
    470 
    471   void SimulateGestureEventCoreWithLatencyInfo(
    472       const WebGestureEvent& gesture_event,
    473       const ui::LatencyInfo& ui_latency) {
    474     widget_host_->ForwardGestureEventWithLatencyInfo(gesture_event, ui_latency);
    475   }
    476 
    477   // Inject simple synthetic WebGestureEvent instances.
    478   void SimulateGestureEvent(WebInputEvent::Type type,
    479                             blink::WebGestureDevice sourceDevice) {
    480     SimulateGestureEventCore(
    481         SyntheticWebGestureEventBuilder::Build(type, sourceDevice));
    482   }
    483 
    484   void SimulateGestureEventWithLatencyInfo(WebInputEvent::Type type,
    485                                            blink::WebGestureDevice sourceDevice,
    486                                            const ui::LatencyInfo& ui_latency) {
    487     SimulateGestureEventCoreWithLatencyInfo(
    488         SyntheticWebGestureEventBuilder::Build(type, sourceDevice), ui_latency);
    489   }
    490 
    491   void SimulateGestureScrollUpdateEvent(float dX, float dY, int modifiers) {
    492     SimulateGestureEventCore(
    493         SyntheticWebGestureEventBuilder::BuildScrollUpdate(dX, dY, modifiers));
    494   }
    495 
    496   void SimulateGesturePinchUpdateEvent(float scale,
    497                                        float anchorX,
    498                                        float anchorY,
    499                                        int modifiers) {
    500     SimulateGestureEventCore(SyntheticWebGestureEventBuilder::BuildPinchUpdate(
    501         scale,
    502         anchorX,
    503         anchorY,
    504         modifiers,
    505         blink::WebGestureDeviceTouchscreen));
    506   }
    507 
    508   // Inject synthetic GestureFlingStart events.
    509   void SimulateGestureFlingStartEvent(float velocityX,
    510                                       float velocityY,
    511                                       blink::WebGestureDevice sourceDevice) {
    512     SimulateGestureEventCore(SyntheticWebGestureEventBuilder::BuildFling(
    513         velocityX, velocityY, sourceDevice));
    514   }
    515 
    516   void SendInputEventACK(WebInputEvent::Type type,
    517                          InputEventAckState ack_result) {
    518     InputHostMsg_HandleInputEvent_ACK_Params ack;
    519     ack.type = type;
    520     ack.state = ack_result;
    521     InputHostMsg_HandleInputEvent_ACK response(0, ack);
    522     widget_host_->OnMessageReceived(response);
    523   }
    524 
    525   bool ScrollStateIsContentScrolling() const {
    526     return scroll_state() == OverscrollController::STATE_CONTENT_SCROLLING;
    527   }
    528 
    529   bool ScrollStateIsOverscrolling() const {
    530     return scroll_state() == OverscrollController::STATE_OVERSCROLLING;
    531   }
    532 
    533   bool ScrollStateIsUnknown() const {
    534     return scroll_state() == OverscrollController::STATE_UNKNOWN;
    535   }
    536 
    537   OverscrollController::ScrollState scroll_state() const {
    538     return view_->overscroll_controller()->scroll_state_;
    539   }
    540 
    541   OverscrollMode overscroll_mode() const {
    542     return view_->overscroll_controller()->overscroll_mode_;
    543   }
    544 
    545   float overscroll_delta_x() const {
    546     return view_->overscroll_controller()->overscroll_delta_x_;
    547   }
    548 
    549   float overscroll_delta_y() const {
    550     return view_->overscroll_controller()->overscroll_delta_y_;
    551   }
    552 
    553   TestOverscrollDelegate* overscroll_delegate() {
    554     return overscroll_delegate_.get();
    555   }
    556 
    557   void SendTouchEvent() {
    558     widget_host_->ForwardTouchEventWithLatencyInfo(touch_event_,
    559                                                    ui::LatencyInfo());
    560     touch_event_.ResetPoints();
    561   }
    562 
    563   void PressTouchPoint(int x, int y) {
    564     touch_event_.PressPoint(x, y);
    565     SendTouchEvent();
    566   }
    567 
    568   void MoveTouchPoint(int index, int x, int y) {
    569     touch_event_.MovePoint(index, x, y);
    570     SendTouchEvent();
    571   }
    572 
    573   void ReleaseTouchPoint(int index) {
    574     touch_event_.ReleasePoint(index);
    575     SendTouchEvent();
    576   }
    577 
    578   size_t GetSentMessageCountAndResetSink() {
    579     size_t count = sink_->message_count();
    580     sink_->ClearMessages();
    581     return count;
    582   }
    583 
    584   void AckLastSentInputEventIfNecessary(InputEventAckState ack_result) {
    585     if (!sink_->message_count())
    586       return;
    587 
    588     InputMsg_HandleInputEvent::Param params;
    589     if (!InputMsg_HandleInputEvent::Read(
    590             sink_->GetMessageAt(sink_->message_count() - 1), &params)) {
    591       return;
    592     }
    593 
    594     if (WebInputEventTraits::IgnoresAckDisposition(*params.a))
    595       return;
    596 
    597     SendInputEventACK(params.a->type, ack_result);
    598   }
    599 
    600   SyntheticWebTouchEvent touch_event_;
    601 
    602   scoped_ptr<TestOverscrollDelegate> overscroll_delegate_;
    603 
    604  private:
    605   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAuraOverscrollTest);
    606 };
    607 
    608 class RenderWidgetHostViewAuraShutdownTest
    609     : public RenderWidgetHostViewAuraTest {
    610  public:
    611   RenderWidgetHostViewAuraShutdownTest() {}
    612 
    613   virtual void TearDown() OVERRIDE {
    614     // No TearDownEnvironment here, we do this explicitly during the test.
    615   }
    616 
    617  private:
    618   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAuraShutdownTest);
    619 };
    620 
    621 // Checks that a fullscreen view has the correct show-state and receives the
    622 // focus.
    623 TEST_F(RenderWidgetHostViewAuraTest, FocusFullscreen) {
    624   view_->InitAsFullscreen(parent_view_);
    625   aura::Window* window = view_->GetNativeView();
    626   ASSERT_TRUE(window != NULL);
    627   EXPECT_EQ(ui::SHOW_STATE_FULLSCREEN,
    628             window->GetProperty(aura::client::kShowStateKey));
    629 
    630   // Check that we requested and received the focus.
    631   EXPECT_TRUE(window->HasFocus());
    632 
    633   // Check that we'll also say it's okay to activate the window when there's an
    634   // ActivationClient defined.
    635   EXPECT_TRUE(view_->ShouldActivate());
    636 }
    637 
    638 // Checks that a popup is positioned correctly relative to its parent using
    639 // screen coordinates.
    640 TEST_F(RenderWidgetHostViewAuraTest, PositionChildPopup) {
    641   TestScreenPositionClient screen_position_client;
    642 
    643   aura::Window* window = parent_view_->GetNativeView();
    644   aura::Window* root = window->GetRootWindow();
    645   aura::client::SetScreenPositionClient(root, &screen_position_client);
    646 
    647   parent_view_->SetBounds(gfx::Rect(10, 10, 800, 600));
    648   gfx::Rect bounds_in_screen = parent_view_->GetViewBounds();
    649   int horiz = bounds_in_screen.width() / 4;
    650   int vert = bounds_in_screen.height() / 4;
    651   bounds_in_screen.Inset(horiz, vert);
    652 
    653   // Verify that when the popup is initialized for the first time, it correctly
    654   // treats the input bounds as screen coordinates.
    655   view_->InitAsPopup(parent_view_, bounds_in_screen);
    656 
    657   gfx::Rect final_bounds_in_screen = view_->GetViewBounds();
    658   EXPECT_EQ(final_bounds_in_screen.ToString(), bounds_in_screen.ToString());
    659 
    660   // Verify that directly setting the bounds via SetBounds() treats the input
    661   // as screen coordinates.
    662   bounds_in_screen = gfx::Rect(60, 60, 100, 100);
    663   view_->SetBounds(bounds_in_screen);
    664   final_bounds_in_screen = view_->GetViewBounds();
    665   EXPECT_EQ(final_bounds_in_screen.ToString(), bounds_in_screen.ToString());
    666 
    667   // Verify that setting the size does not alter the origin.
    668   gfx::Point original_origin = window->bounds().origin();
    669   view_->SetSize(gfx::Size(120, 120));
    670   gfx::Point new_origin = window->bounds().origin();
    671   EXPECT_EQ(original_origin.ToString(), new_origin.ToString());
    672 
    673   aura::client::SetScreenPositionClient(root, NULL);
    674 }
    675 
    676 // Checks that a fullscreen view is destroyed when it loses the focus.
    677 TEST_F(RenderWidgetHostViewAuraTest, DestroyFullscreenOnBlur) {
    678   view_->InitAsFullscreen(parent_view_);
    679   aura::Window* window = view_->GetNativeView();
    680   ASSERT_TRUE(window != NULL);
    681   ASSERT_TRUE(window->HasFocus());
    682 
    683   // After we create and focus another window, the RWHVA's window should be
    684   // destroyed.
    685   TestWindowObserver observer(window);
    686   aura::test::TestWindowDelegate delegate;
    687   scoped_ptr<aura::Window> sibling(new aura::Window(&delegate));
    688   sibling->Init(aura::WINDOW_LAYER_TEXTURED);
    689   sibling->Show();
    690   window->parent()->AddChild(sibling.get());
    691   sibling->Focus();
    692   ASSERT_TRUE(sibling->HasFocus());
    693   ASSERT_TRUE(observer.destroyed());
    694 
    695   widget_host_ = NULL;
    696   view_ = NULL;
    697 }
    698 
    699 // Checks that a popup view is destroyed when a user clicks outside of the popup
    700 // view and focus does not change. This is the case when the user clicks on the
    701 // desktop background on Chrome OS.
    702 TEST_F(RenderWidgetHostViewAuraTest, DestroyPopupClickOutsidePopup) {
    703   parent_view_->SetBounds(gfx::Rect(10, 10, 400, 400));
    704   parent_view_->Focus();
    705   EXPECT_TRUE(parent_view_->HasFocus());
    706 
    707   view_->InitAsPopup(parent_view_, gfx::Rect(10, 10, 100, 100));
    708   aura::Window* window = view_->GetNativeView();
    709   ASSERT_TRUE(window != NULL);
    710 
    711   gfx::Point click_point;
    712   EXPECT_FALSE(window->GetBoundsInRootWindow().Contains(click_point));
    713   aura::Window* parent_window = parent_view_->GetNativeView();
    714   EXPECT_FALSE(parent_window->GetBoundsInRootWindow().Contains(click_point));
    715 
    716   TestWindowObserver observer(window);
    717   aura::test::EventGenerator generator(window->GetRootWindow(), click_point);
    718   generator.ClickLeftButton();
    719   ASSERT_TRUE(parent_view_->HasFocus());
    720   ASSERT_TRUE(observer.destroyed());
    721 
    722   widget_host_ = NULL;
    723   view_ = NULL;
    724 }
    725 
    726 // Checks that a popup view is destroyed when a user taps outside of the popup
    727 // view and focus does not change. This is the case when the user taps the
    728 // desktop background on Chrome OS.
    729 TEST_F(RenderWidgetHostViewAuraTest, DestroyPopupTapOutsidePopup) {
    730   parent_view_->SetBounds(gfx::Rect(10, 10, 400, 400));
    731   parent_view_->Focus();
    732   EXPECT_TRUE(parent_view_->HasFocus());
    733 
    734   view_->InitAsPopup(parent_view_, gfx::Rect(10, 10, 100, 100));
    735   aura::Window* window = view_->GetNativeView();
    736   ASSERT_TRUE(window != NULL);
    737 
    738   gfx::Point tap_point;
    739   EXPECT_FALSE(window->GetBoundsInRootWindow().Contains(tap_point));
    740   aura::Window* parent_window = parent_view_->GetNativeView();
    741   EXPECT_FALSE(parent_window->GetBoundsInRootWindow().Contains(tap_point));
    742 
    743   TestWindowObserver observer(window);
    744   aura::test::EventGenerator generator(window->GetRootWindow(), tap_point);
    745   generator.GestureTapAt(tap_point);
    746   ASSERT_TRUE(parent_view_->HasFocus());
    747   ASSERT_TRUE(observer.destroyed());
    748 
    749   widget_host_ = NULL;
    750   view_ = NULL;
    751 }
    752 
    753 // Checks that IME-composition-event state is maintained correctly.
    754 TEST_F(RenderWidgetHostViewAuraTest, SetCompositionText) {
    755   view_->InitAsChild(NULL);
    756   view_->Show();
    757 
    758   ui::CompositionText composition_text;
    759   composition_text.text = base::ASCIIToUTF16("|a|b");
    760 
    761   // Focused segment
    762   composition_text.underlines.push_back(
    763       ui::CompositionUnderline(0, 3, 0xff000000, true, 0x78563412));
    764 
    765   // Non-focused segment, with different background color.
    766   composition_text.underlines.push_back(
    767       ui::CompositionUnderline(3, 4, 0xff000000, false, 0xefcdab90));
    768 
    769   const ui::CompositionUnderlines& underlines = composition_text.underlines;
    770 
    771   // Caret is at the end. (This emulates Japanese MSIME 2007 and later)
    772   composition_text.selection = gfx::Range(4);
    773 
    774   sink_->ClearMessages();
    775   view_->SetCompositionText(composition_text);
    776   EXPECT_TRUE(view_->has_composition_text_);
    777   {
    778     const IPC::Message* msg =
    779       sink_->GetFirstMessageMatching(ViewMsg_ImeSetComposition::ID);
    780     ASSERT_TRUE(msg != NULL);
    781 
    782     ViewMsg_ImeSetComposition::Param params;
    783     ViewMsg_ImeSetComposition::Read(msg, &params);
    784     // composition text
    785     EXPECT_EQ(composition_text.text, params.a);
    786     // underlines
    787     ASSERT_EQ(underlines.size(), params.b.size());
    788     for (size_t i = 0; i < underlines.size(); ++i) {
    789       EXPECT_EQ(underlines[i].start_offset, params.b[i].startOffset);
    790       EXPECT_EQ(underlines[i].end_offset, params.b[i].endOffset);
    791       EXPECT_EQ(underlines[i].color, params.b[i].color);
    792       EXPECT_EQ(underlines[i].thick, params.b[i].thick);
    793       EXPECT_EQ(underlines[i].background_color, params.b[i].backgroundColor);
    794     }
    795     // highlighted range
    796     EXPECT_EQ(4, params.c) << "Should be the same to the caret pos";
    797     EXPECT_EQ(4, params.d) << "Should be the same to the caret pos";
    798   }
    799 
    800   view_->ImeCancelComposition();
    801   EXPECT_FALSE(view_->has_composition_text_);
    802 }
    803 
    804 // Checks that touch-event state is maintained correctly.
    805 TEST_F(RenderWidgetHostViewAuraTest, TouchEventState) {
    806   view_->InitAsChild(NULL);
    807   view_->Show();
    808 
    809   // Start with no touch-event handler in the renderer.
    810   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, false));
    811   EXPECT_FALSE(widget_host_->ShouldForwardTouchEvent());
    812 
    813   ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
    814                        gfx::Point(30, 30),
    815                        0,
    816                        ui::EventTimeForNow());
    817   ui::TouchEvent move(ui::ET_TOUCH_MOVED,
    818                       gfx::Point(20, 20),
    819                       0,
    820                       ui::EventTimeForNow());
    821   ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
    822                          gfx::Point(20, 20),
    823                          0,
    824                          ui::EventTimeForNow());
    825 
    826   view_->OnTouchEvent(&press);
    827   EXPECT_FALSE(press.handled());
    828   EXPECT_EQ(blink::WebInputEvent::TouchStart, view_->touch_event_.type);
    829   EXPECT_TRUE(view_->touch_event_.cancelable);
    830   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    831   EXPECT_EQ(blink::WebTouchPoint::StatePressed,
    832             view_->touch_event_.touches[0].state);
    833 
    834   view_->OnTouchEvent(&move);
    835   EXPECT_FALSE(move.handled());
    836   EXPECT_EQ(blink::WebInputEvent::TouchMove, view_->touch_event_.type);
    837   EXPECT_TRUE(view_->touch_event_.cancelable);
    838   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    839   EXPECT_EQ(blink::WebTouchPoint::StateMoved,
    840             view_->touch_event_.touches[0].state);
    841 
    842   view_->OnTouchEvent(&release);
    843   EXPECT_FALSE(release.handled());
    844   EXPECT_EQ(blink::WebInputEvent::TouchEnd, view_->touch_event_.type);
    845   EXPECT_TRUE(view_->touch_event_.cancelable);
    846   EXPECT_EQ(0U, view_->touch_event_.touchesLength);
    847 
    848   // Now install some touch-event handlers and do the same steps. The touch
    849   // events should now be consumed. However, the touch-event state should be
    850   // updated as before.
    851   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
    852   EXPECT_TRUE(widget_host_->ShouldForwardTouchEvent());
    853 
    854   view_->OnTouchEvent(&press);
    855   EXPECT_TRUE(press.stopped_propagation());
    856   EXPECT_EQ(blink::WebInputEvent::TouchStart, view_->touch_event_.type);
    857   EXPECT_TRUE(view_->touch_event_.cancelable);
    858   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    859   EXPECT_EQ(blink::WebTouchPoint::StatePressed,
    860             view_->touch_event_.touches[0].state);
    861 
    862   view_->OnTouchEvent(&move);
    863   EXPECT_TRUE(move.stopped_propagation());
    864   EXPECT_EQ(blink::WebInputEvent::TouchMove, view_->touch_event_.type);
    865   EXPECT_TRUE(view_->touch_event_.cancelable);
    866   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    867   EXPECT_EQ(blink::WebTouchPoint::StateMoved,
    868             view_->touch_event_.touches[0].state);
    869 
    870   view_->OnTouchEvent(&release);
    871   EXPECT_TRUE(release.stopped_propagation());
    872   EXPECT_EQ(blink::WebInputEvent::TouchEnd, view_->touch_event_.type);
    873   EXPECT_TRUE(view_->touch_event_.cancelable);
    874   EXPECT_EQ(0U, view_->touch_event_.touchesLength);
    875 
    876   // Now start a touch event, and remove the event-handlers before the release.
    877   view_->OnTouchEvent(&press);
    878   EXPECT_TRUE(press.stopped_propagation());
    879   EXPECT_EQ(blink::WebInputEvent::TouchStart, view_->touch_event_.type);
    880   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    881   EXPECT_EQ(blink::WebTouchPoint::StatePressed,
    882             view_->touch_event_.touches[0].state);
    883 
    884   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, false));
    885   EXPECT_FALSE(widget_host_->ShouldForwardTouchEvent());
    886 
    887   ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(20, 20), 0,
    888       base::Time::NowFromSystemTime() - base::Time());
    889   view_->OnTouchEvent(&move2);
    890   EXPECT_FALSE(move2.handled());
    891   EXPECT_EQ(blink::WebInputEvent::TouchMove, view_->touch_event_.type);
    892   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    893   EXPECT_EQ(blink::WebTouchPoint::StateMoved,
    894             view_->touch_event_.touches[0].state);
    895 
    896   ui::TouchEvent release2(ui::ET_TOUCH_RELEASED, gfx::Point(20, 20), 0,
    897       base::Time::NowFromSystemTime() - base::Time());
    898   view_->OnTouchEvent(&release2);
    899   EXPECT_FALSE(release2.handled());
    900   EXPECT_EQ(blink::WebInputEvent::TouchEnd, view_->touch_event_.type);
    901   EXPECT_EQ(0U, view_->touch_event_.touchesLength);
    902 }
    903 
    904 // Checks that touch-events are queued properly when there is a touch-event
    905 // handler on the page.
    906 TEST_F(RenderWidgetHostViewAuraTest, TouchEventSyncAsync) {
    907   view_->InitAsChild(NULL);
    908   view_->Show();
    909 
    910   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
    911   EXPECT_TRUE(widget_host_->ShouldForwardTouchEvent());
    912 
    913   ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
    914                        gfx::Point(30, 30),
    915                        0,
    916                        ui::EventTimeForNow());
    917   ui::TouchEvent move(ui::ET_TOUCH_MOVED,
    918                       gfx::Point(20, 20),
    919                       0,
    920                       ui::EventTimeForNow());
    921   ui::TouchEvent release(ui::ET_TOUCH_RELEASED,
    922                          gfx::Point(20, 20),
    923                          0,
    924                          ui::EventTimeForNow());
    925 
    926   view_->OnTouchEvent(&press);
    927   EXPECT_TRUE(press.stopped_propagation());
    928   EXPECT_EQ(blink::WebInputEvent::TouchStart, view_->touch_event_.type);
    929   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    930   EXPECT_EQ(blink::WebTouchPoint::StatePressed,
    931             view_->touch_event_.touches[0].state);
    932 
    933   view_->OnTouchEvent(&move);
    934   EXPECT_TRUE(move.stopped_propagation());
    935   EXPECT_EQ(blink::WebInputEvent::TouchMove, view_->touch_event_.type);
    936   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    937   EXPECT_EQ(blink::WebTouchPoint::StateMoved,
    938             view_->touch_event_.touches[0].state);
    939 
    940   // Send the same move event. Since the point hasn't moved, it won't affect the
    941   // queue. However, the view should consume the event.
    942   view_->OnTouchEvent(&move);
    943   EXPECT_TRUE(move.stopped_propagation());
    944   EXPECT_EQ(blink::WebInputEvent::TouchMove, view_->touch_event_.type);
    945   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
    946   EXPECT_EQ(blink::WebTouchPoint::StateMoved,
    947             view_->touch_event_.touches[0].state);
    948 
    949   view_->OnTouchEvent(&release);
    950   EXPECT_TRUE(release.stopped_propagation());
    951   EXPECT_EQ(blink::WebInputEvent::TouchEnd, view_->touch_event_.type);
    952   EXPECT_EQ(0U, view_->touch_event_.touchesLength);
    953 }
    954 
    955 TEST_F(RenderWidgetHostViewAuraTest, PhysicalBackingSizeWithScale) {
    956   view_->InitAsChild(NULL);
    957   aura::client::ParentWindowWithContext(
    958       view_->GetNativeView(),
    959       parent_view_->GetNativeView()->GetRootWindow(),
    960       gfx::Rect());
    961   sink_->ClearMessages();
    962   view_->SetSize(gfx::Size(100, 100));
    963   EXPECT_EQ("100x100", view_->GetPhysicalBackingSize().ToString());
    964   EXPECT_EQ(1u, sink_->message_count());
    965   EXPECT_EQ(ViewMsg_Resize::ID, sink_->GetMessageAt(0)->type());
    966   {
    967     const IPC::Message* msg = sink_->GetMessageAt(0);
    968     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
    969     ViewMsg_Resize::Param params;
    970     ViewMsg_Resize::Read(msg, &params);
    971     EXPECT_EQ("100x100", params.a.new_size.ToString());  // dip size
    972     EXPECT_EQ("100x100",
    973         params.a.physical_backing_size.ToString());  // backing size
    974   }
    975 
    976   widget_host_->ResetSizeAndRepaintPendingFlags();
    977   sink_->ClearMessages();
    978 
    979   aura_test_helper_->test_screen()->SetDeviceScaleFactor(2.0f);
    980   EXPECT_EQ("200x200", view_->GetPhysicalBackingSize().ToString());
    981   // Extra ScreenInfoChanged message for |parent_view_|.
    982   EXPECT_EQ(1u, sink_->message_count());
    983   {
    984     const IPC::Message* msg = sink_->GetMessageAt(0);
    985     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
    986     ViewMsg_Resize::Param params;
    987     ViewMsg_Resize::Read(msg, &params);
    988     EXPECT_EQ(2.0f, params.a.screen_info.deviceScaleFactor);
    989     EXPECT_EQ("100x100", params.a.new_size.ToString());  // dip size
    990     EXPECT_EQ("200x200",
    991         params.a.physical_backing_size.ToString());  // backing size
    992   }
    993 
    994   widget_host_->ResetSizeAndRepaintPendingFlags();
    995   sink_->ClearMessages();
    996 
    997   aura_test_helper_->test_screen()->SetDeviceScaleFactor(1.0f);
    998   // Extra ScreenInfoChanged message for |parent_view_|.
    999   EXPECT_EQ(1u, sink_->message_count());
   1000   EXPECT_EQ("100x100", view_->GetPhysicalBackingSize().ToString());
   1001   {
   1002     const IPC::Message* msg = sink_->GetMessageAt(0);
   1003     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
   1004     ViewMsg_Resize::Param params;
   1005     ViewMsg_Resize::Read(msg, &params);
   1006     EXPECT_EQ(1.0f, params.a.screen_info.deviceScaleFactor);
   1007     EXPECT_EQ("100x100", params.a.new_size.ToString());  // dip size
   1008     EXPECT_EQ("100x100",
   1009         params.a.physical_backing_size.ToString());  // backing size
   1010   }
   1011 }
   1012 
   1013 // Checks that InputMsg_CursorVisibilityChange IPC messages are dispatched
   1014 // to the renderer at the correct times.
   1015 TEST_F(RenderWidgetHostViewAuraTest, CursorVisibilityChange) {
   1016   view_->InitAsChild(NULL);
   1017   aura::client::ParentWindowWithContext(
   1018       view_->GetNativeView(),
   1019       parent_view_->GetNativeView()->GetRootWindow(),
   1020       gfx::Rect());
   1021   view_->SetSize(gfx::Size(100, 100));
   1022 
   1023   aura::test::TestCursorClient cursor_client(
   1024       parent_view_->GetNativeView()->GetRootWindow());
   1025 
   1026   cursor_client.AddObserver(view_);
   1027 
   1028   // Expect a message the first time the cursor is shown.
   1029   view_->WasShown();
   1030   sink_->ClearMessages();
   1031   cursor_client.ShowCursor();
   1032   EXPECT_EQ(1u, sink_->message_count());
   1033   EXPECT_TRUE(sink_->GetUniqueMessageMatching(
   1034       InputMsg_CursorVisibilityChange::ID));
   1035 
   1036   // No message expected if the renderer already knows the cursor is visible.
   1037   sink_->ClearMessages();
   1038   cursor_client.ShowCursor();
   1039   EXPECT_EQ(0u, sink_->message_count());
   1040 
   1041   // Hiding the cursor should send a message.
   1042   sink_->ClearMessages();
   1043   cursor_client.HideCursor();
   1044   EXPECT_EQ(1u, sink_->message_count());
   1045   EXPECT_TRUE(sink_->GetUniqueMessageMatching(
   1046       InputMsg_CursorVisibilityChange::ID));
   1047 
   1048   // No message expected if the renderer already knows the cursor is invisible.
   1049   sink_->ClearMessages();
   1050   cursor_client.HideCursor();
   1051   EXPECT_EQ(0u, sink_->message_count());
   1052 
   1053   // No messages should be sent while the view is invisible.
   1054   view_->WasHidden();
   1055   sink_->ClearMessages();
   1056   cursor_client.ShowCursor();
   1057   EXPECT_EQ(0u, sink_->message_count());
   1058   cursor_client.HideCursor();
   1059   EXPECT_EQ(0u, sink_->message_count());
   1060 
   1061   // Show the view. Since the cursor was invisible when the view was hidden,
   1062   // no message should be sent.
   1063   sink_->ClearMessages();
   1064   view_->WasShown();
   1065   EXPECT_FALSE(sink_->GetUniqueMessageMatching(
   1066       InputMsg_CursorVisibilityChange::ID));
   1067 
   1068   // No message expected if the renderer already knows the cursor is invisible.
   1069   sink_->ClearMessages();
   1070   cursor_client.HideCursor();
   1071   EXPECT_EQ(0u, sink_->message_count());
   1072 
   1073   // Showing the cursor should send a message.
   1074   sink_->ClearMessages();
   1075   cursor_client.ShowCursor();
   1076   EXPECT_EQ(1u, sink_->message_count());
   1077   EXPECT_TRUE(sink_->GetUniqueMessageMatching(
   1078       InputMsg_CursorVisibilityChange::ID));
   1079 
   1080   // No messages should be sent while the view is invisible.
   1081   view_->WasHidden();
   1082   sink_->ClearMessages();
   1083   cursor_client.HideCursor();
   1084   EXPECT_EQ(0u, sink_->message_count());
   1085 
   1086   // Show the view. Since the cursor was visible when the view was hidden,
   1087   // a message is expected to be sent.
   1088   sink_->ClearMessages();
   1089   view_->WasShown();
   1090   EXPECT_TRUE(sink_->GetUniqueMessageMatching(
   1091       InputMsg_CursorVisibilityChange::ID));
   1092 
   1093   cursor_client.RemoveObserver(view_);
   1094 }
   1095 
   1096 TEST_F(RenderWidgetHostViewAuraTest, UpdateCursorIfOverSelf) {
   1097   view_->InitAsChild(NULL);
   1098   aura::client::ParentWindowWithContext(
   1099       view_->GetNativeView(),
   1100       parent_view_->GetNativeView()->GetRootWindow(),
   1101       gfx::Rect());
   1102 
   1103   // Note that all coordinates in this test are screen coordinates.
   1104   view_->SetBounds(gfx::Rect(60, 60, 100, 100));
   1105   view_->Show();
   1106 
   1107   aura::test::TestCursorClient cursor_client(
   1108       parent_view_->GetNativeView()->GetRootWindow());
   1109 
   1110   // Cursor is in the middle of the window.
   1111   cursor_client.reset_calls_to_set_cursor();
   1112   aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(110, 110));
   1113   view_->UpdateCursorIfOverSelf();
   1114   EXPECT_EQ(1, cursor_client.calls_to_set_cursor());
   1115 
   1116   // Cursor is near the top of the window.
   1117   cursor_client.reset_calls_to_set_cursor();
   1118   aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(80, 65));
   1119   view_->UpdateCursorIfOverSelf();
   1120   EXPECT_EQ(1, cursor_client.calls_to_set_cursor());
   1121 
   1122   // Cursor is near the bottom of the window.
   1123   cursor_client.reset_calls_to_set_cursor();
   1124   aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(159, 159));
   1125   view_->UpdateCursorIfOverSelf();
   1126   EXPECT_EQ(1, cursor_client.calls_to_set_cursor());
   1127 
   1128   // Cursor is above the window.
   1129   cursor_client.reset_calls_to_set_cursor();
   1130   aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(67, 59));
   1131   view_->UpdateCursorIfOverSelf();
   1132   EXPECT_EQ(0, cursor_client.calls_to_set_cursor());
   1133 
   1134   // Cursor is below the window.
   1135   cursor_client.reset_calls_to_set_cursor();
   1136   aura::Env::GetInstance()->set_last_mouse_location(gfx::Point(161, 161));
   1137   view_->UpdateCursorIfOverSelf();
   1138   EXPECT_EQ(0, cursor_client.calls_to_set_cursor());
   1139 }
   1140 
   1141 scoped_ptr<cc::CompositorFrame> MakeDelegatedFrame(float scale_factor,
   1142                                                    gfx::Size size,
   1143                                                    gfx::Rect damage) {
   1144   scoped_ptr<cc::CompositorFrame> frame(new cc::CompositorFrame);
   1145   frame->metadata.device_scale_factor = scale_factor;
   1146   frame->delegated_frame_data.reset(new cc::DelegatedFrameData);
   1147 
   1148   scoped_ptr<cc::RenderPass> pass = cc::RenderPass::Create();
   1149   pass->SetNew(
   1150       cc::RenderPass::Id(1, 1), gfx::Rect(size), damage, gfx::Transform());
   1151   frame->delegated_frame_data->render_pass_list.push_back(pass.Pass());
   1152   return frame.Pass();
   1153 }
   1154 
   1155 // Resizing in fullscreen mode should send the up-to-date screen info.
   1156 TEST_F(RenderWidgetHostViewAuraTest, FullscreenResize) {
   1157   aura::Window* root_window = aura_test_helper_->root_window();
   1158   root_window->SetLayoutManager(new FullscreenLayoutManager(root_window));
   1159   view_->InitAsFullscreen(parent_view_);
   1160   view_->WasShown();
   1161   widget_host_->ResetSizeAndRepaintPendingFlags();
   1162   sink_->ClearMessages();
   1163 
   1164   // Call WasResized to flush the old screen info.
   1165   view_->GetRenderWidgetHost()->WasResized();
   1166   {
   1167     // 0 is CreatingNew message.
   1168     const IPC::Message* msg = sink_->GetMessageAt(0);
   1169     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
   1170     ViewMsg_Resize::Param params;
   1171     ViewMsg_Resize::Read(msg, &params);
   1172     EXPECT_EQ("0,0 800x600",
   1173               gfx::Rect(params.a.screen_info.availableRect).ToString());
   1174     EXPECT_EQ("800x600", params.a.new_size.ToString());
   1175     // Resizes are blocked until we swapped a frame of the correct size, and
   1176     // we've committed it.
   1177     view_->OnSwapCompositorFrame(
   1178         0,
   1179         MakeDelegatedFrame(
   1180             1.f, params.a.new_size, gfx::Rect(params.a.new_size)));
   1181     ui::DrawWaiterForTest::WaitForCommit(
   1182         root_window->GetHost()->compositor());
   1183   }
   1184 
   1185   widget_host_->ResetSizeAndRepaintPendingFlags();
   1186   sink_->ClearMessages();
   1187 
   1188   // Make sure the corrent screen size is set along in the resize
   1189   // request when the screen size has changed.
   1190   aura_test_helper_->test_screen()->SetUIScale(0.5);
   1191   EXPECT_EQ(1u, sink_->message_count());
   1192   {
   1193     const IPC::Message* msg = sink_->GetMessageAt(0);
   1194     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
   1195     ViewMsg_Resize::Param params;
   1196     ViewMsg_Resize::Read(msg, &params);
   1197     EXPECT_EQ("0,0 1600x1200",
   1198               gfx::Rect(params.a.screen_info.availableRect).ToString());
   1199     EXPECT_EQ("1600x1200", params.a.new_size.ToString());
   1200     view_->OnSwapCompositorFrame(
   1201         0,
   1202         MakeDelegatedFrame(
   1203             1.f, params.a.new_size, gfx::Rect(params.a.new_size)));
   1204     ui::DrawWaiterForTest::WaitForCommit(
   1205         root_window->GetHost()->compositor());
   1206   }
   1207 }
   1208 
   1209 // Swapping a frame should notify the window.
   1210 TEST_F(RenderWidgetHostViewAuraTest, SwapNotifiesWindow) {
   1211   gfx::Size view_size(100, 100);
   1212   gfx::Rect view_rect(view_size);
   1213 
   1214   view_->InitAsChild(NULL);
   1215   aura::client::ParentWindowWithContext(
   1216       view_->GetNativeView(),
   1217       parent_view_->GetNativeView()->GetRootWindow(),
   1218       gfx::Rect());
   1219   view_->SetSize(view_size);
   1220   view_->WasShown();
   1221 
   1222   MockWindowObserver observer;
   1223   view_->window_->AddObserver(&observer);
   1224 
   1225   // Delegated renderer path
   1226   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1227   view_->OnSwapCompositorFrame(
   1228       0, MakeDelegatedFrame(1.f, view_size, view_rect));
   1229   testing::Mock::VerifyAndClearExpectations(&observer);
   1230 
   1231   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_,
   1232                                                gfx::Rect(5, 5, 5, 5)));
   1233   view_->OnSwapCompositorFrame(
   1234       0, MakeDelegatedFrame(1.f, view_size, gfx::Rect(5, 5, 5, 5)));
   1235   testing::Mock::VerifyAndClearExpectations(&observer);
   1236 
   1237   view_->window_->RemoveObserver(&observer);
   1238 }
   1239 
   1240 TEST_F(RenderWidgetHostViewAuraTest, Resize) {
   1241   gfx::Size size1(100, 100);
   1242   gfx::Size size2(200, 200);
   1243   gfx::Size size3(300, 300);
   1244 
   1245   aura::Window* root_window = parent_view_->GetNativeView()->GetRootWindow();
   1246   view_->InitAsChild(NULL);
   1247   aura::client::ParentWindowWithContext(
   1248       view_->GetNativeView(), root_window, gfx::Rect(size1));
   1249   view_->WasShown();
   1250   view_->SetSize(size1);
   1251   view_->OnSwapCompositorFrame(
   1252       0, MakeDelegatedFrame(1.f, size1, gfx::Rect(size1)));
   1253   ui::DrawWaiterForTest::WaitForCommit(
   1254       root_window->GetHost()->compositor());
   1255   ViewHostMsg_UpdateRect_Params update_params;
   1256   update_params.view_size = size1;
   1257   update_params.scale_factor = 1.f;
   1258   update_params.flags = ViewHostMsg_UpdateRect_Flags::IS_RESIZE_ACK;
   1259   widget_host_->OnMessageReceived(
   1260       ViewHostMsg_UpdateRect(widget_host_->GetRoutingID(), update_params));
   1261   sink_->ClearMessages();
   1262   // Resize logic is idle (no pending resize, no pending commit).
   1263   EXPECT_EQ(size1.ToString(), view_->GetRequestedRendererSize().ToString());
   1264 
   1265   // Resize renderer, should produce a Resize message
   1266   view_->SetSize(size2);
   1267   EXPECT_EQ(size2.ToString(), view_->GetRequestedRendererSize().ToString());
   1268   EXPECT_EQ(1u, sink_->message_count());
   1269   {
   1270     const IPC::Message* msg = sink_->GetMessageAt(0);
   1271     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
   1272     ViewMsg_Resize::Param params;
   1273     ViewMsg_Resize::Read(msg, &params);
   1274     EXPECT_EQ(size2.ToString(), params.a.new_size.ToString());
   1275   }
   1276   // Send resize ack to observe new Resize messages.
   1277   update_params.view_size = size2;
   1278   widget_host_->OnMessageReceived(
   1279       ViewHostMsg_UpdateRect(widget_host_->GetRoutingID(), update_params));
   1280   sink_->ClearMessages();
   1281 
   1282   // Resize renderer again, before receiving a frame. Should not produce a
   1283   // Resize message.
   1284   view_->SetSize(size3);
   1285   EXPECT_EQ(size2.ToString(), view_->GetRequestedRendererSize().ToString());
   1286   EXPECT_EQ(0u, sink_->message_count());
   1287 
   1288   // Receive a frame of the new size, should be skipped and not produce a Resize
   1289   // message.
   1290   view_->OnSwapCompositorFrame(
   1291       0, MakeDelegatedFrame(1.f, size3, gfx::Rect(size3)));
   1292   // Expect the frame ack;
   1293   EXPECT_EQ(1u, sink_->message_count());
   1294   EXPECT_EQ(ViewMsg_SwapCompositorFrameAck::ID, sink_->GetMessageAt(0)->type());
   1295   sink_->ClearMessages();
   1296   EXPECT_EQ(size2.ToString(), view_->GetRequestedRendererSize().ToString());
   1297 
   1298   // Receive a frame of the correct size, should not be skipped and, and should
   1299   // produce a Resize message after the commit.
   1300   view_->OnSwapCompositorFrame(
   1301       0, MakeDelegatedFrame(1.f, size2, gfx::Rect(size2)));
   1302   // No frame ack yet.
   1303   EXPECT_EQ(0u, sink_->message_count());
   1304   EXPECT_EQ(size2.ToString(), view_->GetRequestedRendererSize().ToString());
   1305 
   1306   // Wait for commit, then we should unlock the compositor and send a Resize
   1307   // message (and a frame ack)
   1308   ui::DrawWaiterForTest::WaitForCommit(
   1309       root_window->GetHost()->compositor());
   1310   EXPECT_EQ(size3.ToString(), view_->GetRequestedRendererSize().ToString());
   1311   EXPECT_EQ(2u, sink_->message_count());
   1312   EXPECT_EQ(ViewMsg_SwapCompositorFrameAck::ID, sink_->GetMessageAt(0)->type());
   1313   {
   1314     const IPC::Message* msg = sink_->GetMessageAt(1);
   1315     EXPECT_EQ(ViewMsg_Resize::ID, msg->type());
   1316     ViewMsg_Resize::Param params;
   1317     ViewMsg_Resize::Read(msg, &params);
   1318     EXPECT_EQ(size3.ToString(), params.a.new_size.ToString());
   1319   }
   1320   update_params.view_size = size3;
   1321   widget_host_->OnMessageReceived(
   1322       ViewHostMsg_UpdateRect(widget_host_->GetRoutingID(), update_params));
   1323   sink_->ClearMessages();
   1324 }
   1325 
   1326 // Skipped frames should not drop their damage.
   1327 TEST_F(RenderWidgetHostViewAuraTest, SkippedDelegatedFrames) {
   1328   gfx::Rect view_rect(100, 100);
   1329   gfx::Size frame_size = view_rect.size();
   1330 
   1331   view_->InitAsChild(NULL);
   1332   aura::client::ParentWindowWithContext(
   1333       view_->GetNativeView(),
   1334       parent_view_->GetNativeView()->GetRootWindow(),
   1335       gfx::Rect());
   1336   view_->SetSize(view_rect.size());
   1337 
   1338   MockWindowObserver observer;
   1339   view_->window_->AddObserver(&observer);
   1340 
   1341   // A full frame of damage.
   1342   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1343   view_->OnSwapCompositorFrame(
   1344       0, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1345   testing::Mock::VerifyAndClearExpectations(&observer);
   1346   view_->RunOnCompositingDidCommit();
   1347 
   1348   // A partial damage frame.
   1349   gfx::Rect partial_view_rect(30, 30, 20, 20);
   1350   EXPECT_CALL(observer,
   1351               OnWindowPaintScheduled(view_->window_, partial_view_rect));
   1352   view_->OnSwapCompositorFrame(
   1353       0, MakeDelegatedFrame(1.f, frame_size, partial_view_rect));
   1354   testing::Mock::VerifyAndClearExpectations(&observer);
   1355   view_->RunOnCompositingDidCommit();
   1356 
   1357   // Lock the compositor. Now we should drop frames.
   1358   view_rect = gfx::Rect(150, 150);
   1359   view_->SetSize(view_rect.size());
   1360 
   1361   // This frame is dropped.
   1362   gfx::Rect dropped_damage_rect_1(10, 20, 30, 40);
   1363   EXPECT_CALL(observer, OnWindowPaintScheduled(_, _)).Times(0);
   1364   view_->OnSwapCompositorFrame(
   1365       0, MakeDelegatedFrame(1.f, frame_size, dropped_damage_rect_1));
   1366   testing::Mock::VerifyAndClearExpectations(&observer);
   1367   view_->RunOnCompositingDidCommit();
   1368 
   1369   gfx::Rect dropped_damage_rect_2(40, 50, 10, 20);
   1370   EXPECT_CALL(observer, OnWindowPaintScheduled(_, _)).Times(0);
   1371   view_->OnSwapCompositorFrame(
   1372       0, MakeDelegatedFrame(1.f, frame_size, dropped_damage_rect_2));
   1373   testing::Mock::VerifyAndClearExpectations(&observer);
   1374   view_->RunOnCompositingDidCommit();
   1375 
   1376   // Unlock the compositor. This frame should damage everything.
   1377   frame_size = view_rect.size();
   1378 
   1379   gfx::Rect new_damage_rect(5, 6, 10, 10);
   1380   EXPECT_CALL(observer,
   1381               OnWindowPaintScheduled(view_->window_, view_rect));
   1382   view_->OnSwapCompositorFrame(
   1383       0, MakeDelegatedFrame(1.f, frame_size, new_damage_rect));
   1384   testing::Mock::VerifyAndClearExpectations(&observer);
   1385   view_->RunOnCompositingDidCommit();
   1386 
   1387   // A partial damage frame, this should not be dropped.
   1388   EXPECT_CALL(observer,
   1389               OnWindowPaintScheduled(view_->window_, partial_view_rect));
   1390   view_->OnSwapCompositorFrame(
   1391       0, MakeDelegatedFrame(1.f, frame_size, partial_view_rect));
   1392   testing::Mock::VerifyAndClearExpectations(&observer);
   1393   view_->RunOnCompositingDidCommit();
   1394 
   1395 
   1396   // Resize to something empty.
   1397   view_rect = gfx::Rect(100, 0);
   1398   view_->SetSize(view_rect.size());
   1399 
   1400   // We're never expecting empty frames, resize to something non-empty.
   1401   view_rect = gfx::Rect(100, 100);
   1402   view_->SetSize(view_rect.size());
   1403 
   1404   // This frame should not be dropped.
   1405   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1406   view_->OnSwapCompositorFrame(
   1407       0, MakeDelegatedFrame(1.f, view_rect.size(), view_rect));
   1408   testing::Mock::VerifyAndClearExpectations(&observer);
   1409   view_->RunOnCompositingDidCommit();
   1410 
   1411   view_->window_->RemoveObserver(&observer);
   1412 }
   1413 
   1414 TEST_F(RenderWidgetHostViewAuraTest, OutputSurfaceIdChange) {
   1415   gfx::Rect view_rect(100, 100);
   1416   gfx::Size frame_size = view_rect.size();
   1417 
   1418   view_->InitAsChild(NULL);
   1419   aura::client::ParentWindowWithContext(
   1420       view_->GetNativeView(),
   1421       parent_view_->GetNativeView()->GetRootWindow(),
   1422       gfx::Rect());
   1423   view_->SetSize(view_rect.size());
   1424 
   1425   MockWindowObserver observer;
   1426   view_->window_->AddObserver(&observer);
   1427 
   1428   // Swap a frame.
   1429   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1430   view_->OnSwapCompositorFrame(
   1431       0, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1432   testing::Mock::VerifyAndClearExpectations(&observer);
   1433   view_->RunOnCompositingDidCommit();
   1434 
   1435   // Swap a frame with a different surface id.
   1436   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1437   view_->OnSwapCompositorFrame(
   1438       1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1439   testing::Mock::VerifyAndClearExpectations(&observer);
   1440   view_->RunOnCompositingDidCommit();
   1441 
   1442   // Swap an empty frame, with a different surface id.
   1443   view_->OnSwapCompositorFrame(
   1444       2, MakeDelegatedFrame(1.f, gfx::Size(), gfx::Rect()));
   1445   testing::Mock::VerifyAndClearExpectations(&observer);
   1446   view_->RunOnCompositingDidCommit();
   1447 
   1448   // Swap another frame, with a different surface id.
   1449   EXPECT_CALL(observer, OnWindowPaintScheduled(view_->window_, view_rect));
   1450   view_->OnSwapCompositorFrame(3,
   1451                                MakeDelegatedFrame(1.f, frame_size, view_rect));
   1452   testing::Mock::VerifyAndClearExpectations(&observer);
   1453   view_->RunOnCompositingDidCommit();
   1454 
   1455   view_->window_->RemoveObserver(&observer);
   1456 }
   1457 
   1458 TEST_F(RenderWidgetHostViewAuraTest, DiscardDelegatedFrames) {
   1459   size_t max_renderer_frames =
   1460       RendererFrameManager::GetInstance()->max_number_of_saved_frames();
   1461   ASSERT_LE(2u, max_renderer_frames);
   1462   size_t renderer_count = max_renderer_frames + 1;
   1463   gfx::Rect view_rect(100, 100);
   1464   gfx::Size frame_size = view_rect.size();
   1465   DCHECK_EQ(0u, HostSharedBitmapManager::current()->AllocatedBitmapCount());
   1466 
   1467   scoped_ptr<RenderWidgetHostImpl * []> hosts(
   1468       new RenderWidgetHostImpl* [renderer_count]);
   1469   scoped_ptr<FakeRenderWidgetHostViewAura * []> views(
   1470       new FakeRenderWidgetHostViewAura* [renderer_count]);
   1471 
   1472   // Create a bunch of renderers.
   1473   for (size_t i = 0; i < renderer_count; ++i) {
   1474     hosts[i] = new RenderWidgetHostImpl(
   1475         &delegate_, process_host_, MSG_ROUTING_NONE, false);
   1476     hosts[i]->Init();
   1477     views[i] = new FakeRenderWidgetHostViewAura(hosts[i]);
   1478     views[i]->InitAsChild(NULL);
   1479     aura::client::ParentWindowWithContext(
   1480         views[i]->GetNativeView(),
   1481         parent_view_->GetNativeView()->GetRootWindow(),
   1482         gfx::Rect());
   1483     views[i]->SetSize(view_rect.size());
   1484   }
   1485 
   1486   // Make each renderer visible, and swap a frame on it, then make it invisible.
   1487   for (size_t i = 0; i < renderer_count; ++i) {
   1488     views[i]->WasShown();
   1489     views[i]->OnSwapCompositorFrame(
   1490         1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1491     EXPECT_TRUE(views[i]->frame_provider());
   1492     views[i]->WasHidden();
   1493   }
   1494 
   1495   // There should be max_renderer_frames with a frame in it, and one without it.
   1496   // Since the logic is LRU eviction, the first one should be without.
   1497   EXPECT_FALSE(views[0]->frame_provider());
   1498   for (size_t i = 1; i < renderer_count; ++i)
   1499     EXPECT_TRUE(views[i]->frame_provider());
   1500 
   1501   // LRU renderer is [0], make it visible, it shouldn't evict anything yet.
   1502   views[0]->WasShown();
   1503   EXPECT_FALSE(views[0]->frame_provider());
   1504   EXPECT_TRUE(views[1]->frame_provider());
   1505 
   1506   // Swap a frame on it, it should evict the next LRU [1].
   1507   views[0]->OnSwapCompositorFrame(
   1508       1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1509   EXPECT_TRUE(views[0]->frame_provider());
   1510   EXPECT_FALSE(views[1]->frame_provider());
   1511   views[0]->WasHidden();
   1512 
   1513   // LRU renderer is [1], still hidden. Swap a frame on it, it should evict
   1514   // the next LRU [2].
   1515   views[1]->OnSwapCompositorFrame(
   1516       1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1517   EXPECT_TRUE(views[0]->frame_provider());
   1518   EXPECT_TRUE(views[1]->frame_provider());
   1519   EXPECT_FALSE(views[2]->frame_provider());
   1520   for (size_t i = 3; i < renderer_count; ++i)
   1521     EXPECT_TRUE(views[i]->frame_provider());
   1522 
   1523   // Make all renderers but [0] visible and swap a frame on them, keep [0]
   1524   // hidden, it becomes the LRU.
   1525   for (size_t i = 1; i < renderer_count; ++i) {
   1526     views[i]->WasShown();
   1527     views[i]->OnSwapCompositorFrame(
   1528         1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1529     EXPECT_TRUE(views[i]->frame_provider());
   1530   }
   1531   EXPECT_FALSE(views[0]->frame_provider());
   1532 
   1533   // Swap a frame on [0], it should be evicted immediately.
   1534   views[0]->OnSwapCompositorFrame(
   1535       1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1536   EXPECT_FALSE(views[0]->frame_provider());
   1537 
   1538   // Make [0] visible, and swap a frame on it. Nothing should be evicted
   1539   // although we're above the limit.
   1540   views[0]->WasShown();
   1541   views[0]->OnSwapCompositorFrame(
   1542       1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1543   for (size_t i = 0; i < renderer_count; ++i)
   1544     EXPECT_TRUE(views[i]->frame_provider());
   1545 
   1546   // Make [0] hidden, it should evict its frame.
   1547   views[0]->WasHidden();
   1548   EXPECT_FALSE(views[0]->frame_provider());
   1549 
   1550   for (size_t i = 0; i < renderer_count - 1; ++i)
   1551     views[i]->WasHidden();
   1552 
   1553   // Allocate enough bitmaps so that two frames (proportionally) would be
   1554   // enough hit the handle limit.
   1555   int handles_per_frame = 5;
   1556   RendererFrameManager::GetInstance()->set_max_handles(handles_per_frame * 2);
   1557 
   1558   for (size_t i = 0; i < (renderer_count - 1) * handles_per_frame; i++) {
   1559     HostSharedBitmapManager::current()->ChildAllocatedSharedBitmap(
   1560         1,
   1561         base::SharedMemory::NULLHandle(),
   1562         base::GetCurrentProcessHandle(),
   1563         cc::SharedBitmap::GenerateId());
   1564   }
   1565 
   1566   // Hiding this last bitmap should evict all but two frames.
   1567   views[renderer_count - 1]->WasHidden();
   1568   for (size_t i = 0; i < renderer_count; ++i) {
   1569     if (i + 2 < renderer_count)
   1570       EXPECT_FALSE(views[i]->frame_provider());
   1571     else
   1572       EXPECT_TRUE(views[i]->frame_provider());
   1573   }
   1574   HostSharedBitmapManager::current()->ProcessRemoved(
   1575       base::GetCurrentProcessHandle());
   1576   RendererFrameManager::GetInstance()->set_max_handles(
   1577       base::SharedMemory::GetHandleLimit());
   1578 
   1579   for (size_t i = 0; i < renderer_count; ++i) {
   1580     views[i]->Destroy();
   1581     delete hosts[i];
   1582   }
   1583 }
   1584 
   1585 TEST_F(RenderWidgetHostViewAuraTest, DiscardDelegatedFramesWithLocking) {
   1586   size_t max_renderer_frames =
   1587       RendererFrameManager::GetInstance()->max_number_of_saved_frames();
   1588   ASSERT_LE(2u, max_renderer_frames);
   1589   size_t renderer_count = max_renderer_frames + 1;
   1590   gfx::Rect view_rect(100, 100);
   1591   gfx::Size frame_size = view_rect.size();
   1592   DCHECK_EQ(0u, HostSharedBitmapManager::current()->AllocatedBitmapCount());
   1593 
   1594   scoped_ptr<RenderWidgetHostImpl * []> hosts(
   1595       new RenderWidgetHostImpl* [renderer_count]);
   1596   scoped_ptr<FakeRenderWidgetHostViewAura * []> views(
   1597       new FakeRenderWidgetHostViewAura* [renderer_count]);
   1598 
   1599   // Create a bunch of renderers.
   1600   for (size_t i = 0; i < renderer_count; ++i) {
   1601     hosts[i] = new RenderWidgetHostImpl(
   1602         &delegate_, process_host_, MSG_ROUTING_NONE, false);
   1603     hosts[i]->Init();
   1604     views[i] = new FakeRenderWidgetHostViewAura(hosts[i]);
   1605     views[i]->InitAsChild(NULL);
   1606     aura::client::ParentWindowWithContext(
   1607         views[i]->GetNativeView(),
   1608         parent_view_->GetNativeView()->GetRootWindow(),
   1609         gfx::Rect());
   1610     views[i]->SetSize(view_rect.size());
   1611   }
   1612 
   1613   // Make each renderer visible and swap a frame on it. No eviction should
   1614   // occur because all frames are visible.
   1615   for (size_t i = 0; i < renderer_count; ++i) {
   1616     views[i]->WasShown();
   1617     views[i]->OnSwapCompositorFrame(
   1618         1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1619     EXPECT_TRUE(views[i]->frame_provider());
   1620   }
   1621 
   1622   // If we hide [0], then [0] should be evicted.
   1623   views[0]->WasHidden();
   1624   EXPECT_FALSE(views[0]->frame_provider());
   1625 
   1626   // If we lock [0] before hiding it, then [0] should not be evicted.
   1627   views[0]->WasShown();
   1628   views[0]->OnSwapCompositorFrame(
   1629         1, MakeDelegatedFrame(1.f, frame_size, view_rect));
   1630   EXPECT_TRUE(views[0]->frame_provider());
   1631   views[0]->GetDelegatedFrameHost()->LockResources();
   1632   views[0]->WasHidden();
   1633   EXPECT_TRUE(views[0]->frame_provider());
   1634 
   1635   // If we unlock [0] now, then [0] should be evicted.
   1636   views[0]->GetDelegatedFrameHost()->UnlockResources();
   1637   EXPECT_FALSE(views[0]->frame_provider());
   1638 
   1639   for (size_t i = 0; i < renderer_count; ++i) {
   1640     views[i]->Destroy();
   1641     delete hosts[i];
   1642   }
   1643 }
   1644 
   1645 TEST_F(RenderWidgetHostViewAuraTest, SoftwareDPIChange) {
   1646   gfx::Rect view_rect(100, 100);
   1647   gfx::Size frame_size(100, 100);
   1648 
   1649   view_->InitAsChild(NULL);
   1650   aura::client::ParentWindowWithContext(
   1651       view_->GetNativeView(),
   1652       parent_view_->GetNativeView()->GetRootWindow(),
   1653       gfx::Rect());
   1654   view_->SetSize(view_rect.size());
   1655   view_->WasShown();
   1656 
   1657   // With a 1x DPI UI and 1x DPI Renderer.
   1658   view_->OnSwapCompositorFrame(
   1659       1, MakeDelegatedFrame(1.f, frame_size, gfx::Rect(frame_size)));
   1660 
   1661   // Save the frame provider.
   1662   scoped_refptr<cc::DelegatedFrameProvider> frame_provider =
   1663       view_->frame_provider();
   1664 
   1665   // This frame will have the same number of physical pixels, but has a new
   1666   // scale on it.
   1667   view_->OnSwapCompositorFrame(
   1668       1, MakeDelegatedFrame(2.f, frame_size, gfx::Rect(frame_size)));
   1669 
   1670   // When we get a new frame with the same frame size in physical pixels, but a
   1671   // different scale, we should generate a new frame provider, as the final
   1672   // result will need to be scaled differently to the screen.
   1673   EXPECT_NE(frame_provider.get(), view_->frame_provider());
   1674 }
   1675 
   1676 class RenderWidgetHostViewAuraCopyRequestTest
   1677     : public RenderWidgetHostViewAuraShutdownTest {
   1678  public:
   1679   RenderWidgetHostViewAuraCopyRequestTest()
   1680       : callback_count_(0), result_(false) {}
   1681 
   1682   void CallbackMethod(const base::Closure& quit_closure, bool result) {
   1683     result_ = result;
   1684     callback_count_++;
   1685     quit_closure.Run();
   1686   }
   1687 
   1688   int callback_count_;
   1689   bool result_;
   1690 
   1691  private:
   1692   DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewAuraCopyRequestTest);
   1693 };
   1694 
   1695 TEST_F(RenderWidgetHostViewAuraCopyRequestTest, DestroyedAfterCopyRequest) {
   1696   base::RunLoop run_loop;
   1697 
   1698   gfx::Rect view_rect(100, 100);
   1699   scoped_ptr<cc::CopyOutputRequest> request;
   1700 
   1701   view_->InitAsChild(NULL);
   1702   aura::client::ParentWindowWithContext(
   1703       view_->GetNativeView(),
   1704       parent_view_->GetNativeView()->GetRootWindow(),
   1705       gfx::Rect());
   1706   view_->SetSize(view_rect.size());
   1707   view_->WasShown();
   1708 
   1709   scoped_ptr<FakeFrameSubscriber> frame_subscriber(new FakeFrameSubscriber(
   1710       view_rect.size(),
   1711       base::Bind(&RenderWidgetHostViewAuraCopyRequestTest::CallbackMethod,
   1712                  base::Unretained(this),
   1713                  run_loop.QuitClosure())));
   1714 
   1715   EXPECT_EQ(0, callback_count_);
   1716   EXPECT_FALSE(view_->last_copy_request_);
   1717 
   1718   view_->BeginFrameSubscription(
   1719       frame_subscriber.PassAs<RenderWidgetHostViewFrameSubscriber>());
   1720   view_->OnSwapCompositorFrame(
   1721       1, MakeDelegatedFrame(1.f, view_rect.size(), gfx::Rect(view_rect)));
   1722 
   1723   EXPECT_EQ(0, callback_count_);
   1724   EXPECT_TRUE(view_->last_copy_request_);
   1725   EXPECT_TRUE(view_->last_copy_request_->has_texture_mailbox());
   1726   request = view_->last_copy_request_.Pass();
   1727 
   1728   // Send back the mailbox included in the request. There's no release callback
   1729   // since the mailbox came from the RWHVA originally.
   1730   request->SendTextureResult(view_rect.size(),
   1731                              request->texture_mailbox(),
   1732                              scoped_ptr<cc::SingleReleaseCallback>());
   1733 
   1734   // This runs until the callback happens.
   1735   run_loop.Run();
   1736 
   1737   // The callback should succeed.
   1738   EXPECT_EQ(1, callback_count_);
   1739   EXPECT_TRUE(result_);
   1740 
   1741   view_->OnSwapCompositorFrame(
   1742       1, MakeDelegatedFrame(1.f, view_rect.size(), gfx::Rect(view_rect)));
   1743 
   1744   EXPECT_EQ(1, callback_count_);
   1745   request = view_->last_copy_request_.Pass();
   1746 
   1747   // Destroy the RenderWidgetHostViewAura and ImageTransportFactory.
   1748   TearDownEnvironment();
   1749 
   1750   // Send back the mailbox included in the request. There's no release callback
   1751   // since the mailbox came from the RWHVA originally.
   1752   request->SendTextureResult(view_rect.size(),
   1753                              request->texture_mailbox(),
   1754                              scoped_ptr<cc::SingleReleaseCallback>());
   1755 
   1756   // Because the copy request callback may be holding state within it, that
   1757   // state must handle the RWHVA and ImageTransportFactory going away before the
   1758   // callback is called. This test passes if it does not crash as a result of
   1759   // these things being destroyed.
   1760   EXPECT_EQ(2, callback_count_);
   1761   EXPECT_FALSE(result_);
   1762 }
   1763 
   1764 TEST_F(RenderWidgetHostViewAuraTest, VisibleViewportTest) {
   1765   gfx::Rect view_rect(100, 100);
   1766 
   1767   view_->InitAsChild(NULL);
   1768   aura::client::ParentWindowWithContext(
   1769       view_->GetNativeView(),
   1770       parent_view_->GetNativeView()->GetRootWindow(),
   1771       gfx::Rect());
   1772   view_->SetSize(view_rect.size());
   1773   view_->WasShown();
   1774 
   1775   // Defaults to full height of the view.
   1776   EXPECT_EQ(100, view_->GetVisibleViewportSize().height());
   1777 
   1778   widget_host_->ResetSizeAndRepaintPendingFlags();
   1779   sink_->ClearMessages();
   1780   view_->SetInsets(gfx::Insets(0, 0, 40, 0));
   1781 
   1782   EXPECT_EQ(60, view_->GetVisibleViewportSize().height());
   1783 
   1784   const IPC::Message *message = sink_->GetFirstMessageMatching(
   1785       ViewMsg_Resize::ID);
   1786   ASSERT_TRUE(message != NULL);
   1787 
   1788   ViewMsg_Resize::Param params;
   1789   ViewMsg_Resize::Read(message, &params);
   1790   EXPECT_EQ(60, params.a.visible_viewport_size.height());
   1791 }
   1792 
   1793 // Ensures that touch event positions are never truncated to integers.
   1794 TEST_F(RenderWidgetHostViewAuraTest, TouchEventPositionsArentRounded) {
   1795   const float kX = 30.58f;
   1796   const float kY = 50.23f;
   1797 
   1798   view_->InitAsChild(NULL);
   1799   view_->Show();
   1800 
   1801   ui::TouchEvent press(ui::ET_TOUCH_PRESSED,
   1802                        gfx::PointF(kX, kY),
   1803                        0,
   1804                        ui::EventTimeForNow());
   1805 
   1806   view_->OnTouchEvent(&press);
   1807   EXPECT_EQ(blink::WebInputEvent::TouchStart, view_->touch_event_.type);
   1808   EXPECT_TRUE(view_->touch_event_.cancelable);
   1809   EXPECT_EQ(1U, view_->touch_event_.touchesLength);
   1810   EXPECT_EQ(blink::WebTouchPoint::StatePressed,
   1811             view_->touch_event_.touches[0].state);
   1812   EXPECT_EQ(kX, view_->touch_event_.touches[0].screenPosition.x);
   1813   EXPECT_EQ(kX, view_->touch_event_.touches[0].position.x);
   1814   EXPECT_EQ(kY, view_->touch_event_.touches[0].screenPosition.y);
   1815   EXPECT_EQ(kY, view_->touch_event_.touches[0].position.y);
   1816 }
   1817 
   1818 // Tests that scroll ACKs are correctly handled by the overscroll-navigation
   1819 // controller.
   1820 TEST_F(RenderWidgetHostViewAuraOverscrollTest, WheelScrollEventOverscrolls) {
   1821   SetUpOverscrollEnvironment();
   1822 
   1823   // Simulate wheel events.
   1824   SimulateWheelEvent(-5, 0, 0, true);    // sent directly
   1825   SimulateWheelEvent(-1, 1, 0, true);    // enqueued
   1826   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
   1827   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
   1828   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
   1829   SimulateWheelEvent(-20, 6, 1, true);   // enqueued, different modifiers
   1830   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1831   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1832 
   1833   // Receive ACK the first wheel event as not processed.
   1834   SendInputEventACK(WebInputEvent::MouseWheel,
   1835                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1836   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1837   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1838   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1839 
   1840   // Receive ACK for the second (coalesced) event as not processed. This will
   1841   // start a back navigation. However, this will also cause the queued next
   1842   // event to be sent to the renderer. But since overscroll navigation has
   1843   // started, that event will also be included in the overscroll computation
   1844   // instead of being sent to the renderer. So the result will be an overscroll
   1845   // back navigation, and no event will be sent to the renderer.
   1846   SendInputEventACK(WebInputEvent::MouseWheel,
   1847                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1848   EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   1849   EXPECT_EQ(OVERSCROLL_WEST, overscroll_delegate()->current_mode());
   1850   EXPECT_EQ(-81.f, overscroll_delta_x());
   1851   EXPECT_EQ(-31.f, overscroll_delegate()->delta_x());
   1852   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   1853   EXPECT_EQ(0U, sink_->message_count());
   1854 
   1855   // Send a mouse-move event. This should cancel the overscroll navigation.
   1856   SimulateMouseMove(5, 10, 0);
   1857   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1858   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1859   EXPECT_EQ(1U, sink_->message_count());
   1860 }
   1861 
   1862 // Tests that if some scroll events are consumed towards the start, then
   1863 // subsequent scrolls do not horizontal overscroll.
   1864 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   1865        WheelScrollConsumedDoNotHorizOverscroll) {
   1866   SetUpOverscrollEnvironment();
   1867 
   1868   // Simulate wheel events.
   1869   SimulateWheelEvent(-5, 0, 0, true);    // sent directly
   1870   SimulateWheelEvent(-1, -1, 0, true);   // enqueued
   1871   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
   1872   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
   1873   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
   1874   SimulateWheelEvent(-20, 6, 1, true);   // enqueued, different modifiers
   1875   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1876   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1877 
   1878   // Receive ACK the first wheel event as processed.
   1879   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   1880   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1881   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1882   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1883 
   1884   // Receive ACK for the second (coalesced) event as not processed. This should
   1885   // not initiate overscroll, since the beginning of the scroll has been
   1886   // consumed. The queued event with different modifiers should be sent to the
   1887   // renderer.
   1888   SendInputEventACK(WebInputEvent::MouseWheel,
   1889                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1890   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1891   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1892 
   1893   SendInputEventACK(WebInputEvent::MouseWheel,
   1894                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1895   EXPECT_EQ(0U, sink_->message_count());
   1896   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1897 
   1898   // Indicate the end of the scrolling from the touchpad.
   1899   SimulateGestureFlingStartEvent(-1200.f, 0.f, blink::WebGestureDeviceTouchpad);
   1900   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1901 
   1902   // Start another scroll. This time, do not consume any scroll events.
   1903   SimulateWheelEvent(0, -5, 0, true);    // sent directly
   1904   SimulateWheelEvent(0, -1, 0, true);    // enqueued
   1905   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
   1906   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
   1907   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
   1908   SimulateWheelEvent(-20, 6, 1, true);   // enqueued, different modifiers
   1909   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1910   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1911 
   1912   // Receive ACK for the first wheel and the subsequent coalesced event as not
   1913   // processed. This should start a back-overscroll.
   1914   SendInputEventACK(WebInputEvent::MouseWheel,
   1915                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1916   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1917   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1918   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1919   SendInputEventACK(WebInputEvent::MouseWheel,
   1920                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1921   EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   1922 }
   1923 
   1924 // Tests that wheel-scrolling correctly turns overscroll on and off.
   1925 TEST_F(RenderWidgetHostViewAuraOverscrollTest, WheelScrollOverscrollToggle) {
   1926   SetUpOverscrollEnvironment();
   1927 
   1928   // Send a wheel event. ACK the event as not processed. This should not
   1929   // initiate an overscroll gesture since it doesn't cross the threshold yet.
   1930   SimulateWheelEvent(10, 0, 0, true);
   1931   SendInputEventACK(WebInputEvent::MouseWheel,
   1932                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1933   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1934   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1935   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1936 
   1937   // Scroll some more so as to not overscroll.
   1938   SimulateWheelEvent(10, 0, 0, true);
   1939   SendInputEventACK(WebInputEvent::MouseWheel,
   1940                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1941   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1942   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1943   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1944 
   1945   // Scroll some more to initiate an overscroll.
   1946   SimulateWheelEvent(40, 0, 0, true);
   1947   SendInputEventACK(WebInputEvent::MouseWheel,
   1948                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1949   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   1950   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   1951   EXPECT_EQ(60.f, overscroll_delta_x());
   1952   EXPECT_EQ(10.f, overscroll_delegate()->delta_x());
   1953   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   1954   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1955 
   1956   // Scroll in the reverse direction enough to abort the overscroll.
   1957   SimulateWheelEvent(-20, 0, 0, true);
   1958   EXPECT_EQ(0U, sink_->message_count());
   1959   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1960   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1961 
   1962   // Continue to scroll in the reverse direction.
   1963   SimulateWheelEvent(-20, 0, 0, true);
   1964   SendInputEventACK(WebInputEvent::MouseWheel,
   1965                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1966   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1967   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1968   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1969 
   1970   // Continue to scroll in the reverse direction enough to initiate overscroll
   1971   // in that direction.
   1972   SimulateWheelEvent(-55, 0, 0, true);
   1973   EXPECT_EQ(1U, sink_->message_count());
   1974   SendInputEventACK(WebInputEvent::MouseWheel,
   1975                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1976   EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   1977   EXPECT_EQ(OVERSCROLL_WEST, overscroll_delegate()->current_mode());
   1978   EXPECT_EQ(-75.f, overscroll_delta_x());
   1979   EXPECT_EQ(-25.f, overscroll_delegate()->delta_x());
   1980   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   1981 }
   1982 
   1983 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   1984        ScrollEventsOverscrollWithFling) {
   1985   SetUpOverscrollEnvironment();
   1986 
   1987   // Send a wheel event. ACK the event as not processed. This should not
   1988   // initiate an overscroll gesture since it doesn't cross the threshold yet.
   1989   SimulateWheelEvent(10, 0, 0, true);
   1990   SendInputEventACK(WebInputEvent::MouseWheel,
   1991                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   1992   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   1993   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   1994   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   1995 
   1996   // Scroll some more so as to not overscroll.
   1997   SimulateWheelEvent(20, 0, 0, true);
   1998   EXPECT_EQ(1U, sink_->message_count());
   1999   SendInputEventACK(WebInputEvent::MouseWheel,
   2000                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2001   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2002   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2003   sink_->ClearMessages();
   2004 
   2005   // Scroll some more to initiate an overscroll.
   2006   SimulateWheelEvent(30, 0, 0, true);
   2007   SendInputEventACK(WebInputEvent::MouseWheel,
   2008                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2009   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2010   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2011   EXPECT_EQ(60.f, overscroll_delta_x());
   2012   EXPECT_EQ(10.f, overscroll_delegate()->delta_x());
   2013   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2014   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2015 
   2016   // Send a fling start, but with a small velocity, so that the overscroll is
   2017   // aborted. The fling should proceed to the renderer, through the gesture
   2018   // event filter.
   2019   SimulateGestureFlingStartEvent(0.f, 0.1f, blink::WebGestureDeviceTouchpad);
   2020   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2021   EXPECT_EQ(1U, sink_->message_count());
   2022 }
   2023 
   2024 // Same as ScrollEventsOverscrollWithFling, but with zero velocity. Checks that
   2025 // the zero-velocity fling does not reach the renderer.
   2026 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2027        ScrollEventsOverscrollWithZeroFling) {
   2028   SetUpOverscrollEnvironment();
   2029 
   2030   // Send a wheel event. ACK the event as not processed. This should not
   2031   // initiate an overscroll gesture since it doesn't cross the threshold yet.
   2032   SimulateWheelEvent(10, 0, 0, true);
   2033   SendInputEventACK(WebInputEvent::MouseWheel,
   2034                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2035   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2036   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2037   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2038 
   2039   // Scroll some more so as to not overscroll.
   2040   SimulateWheelEvent(20, 0, 0, true);
   2041   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2042   SendInputEventACK(WebInputEvent::MouseWheel,
   2043                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2044   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2045   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2046 
   2047   // Scroll some more to initiate an overscroll.
   2048   SimulateWheelEvent(30, 0, 0, true);
   2049   SendInputEventACK(WebInputEvent::MouseWheel,
   2050                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2051   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2052   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2053   EXPECT_EQ(60.f, overscroll_delta_x());
   2054   EXPECT_EQ(10.f, overscroll_delegate()->delta_x());
   2055   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2056   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2057 
   2058   // Send a fling start, but with a small velocity, so that the overscroll is
   2059   // aborted. The fling should proceed to the renderer, through the gesture
   2060   // event filter.
   2061   SimulateGestureFlingStartEvent(10.f, 0.f, blink::WebGestureDeviceTouchpad);
   2062   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2063   EXPECT_EQ(1U, sink_->message_count());
   2064 }
   2065 
   2066 // Tests that a fling in the opposite direction of the overscroll cancels the
   2067 // overscroll nav instead of completing it.
   2068 TEST_F(RenderWidgetHostViewAuraOverscrollTest, ReverseFlingCancelsOverscroll) {
   2069   SetUpOverscrollEnvironment();
   2070 
   2071   {
   2072     // Start and end a gesture in the same direction without processing the
   2073     // gesture events in the renderer. This should initiate and complete an
   2074     // overscroll navigation.
   2075     SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2076                          blink::WebGestureDeviceTouchscreen);
   2077     SimulateGestureScrollUpdateEvent(300, -5, 0);
   2078     SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2079                       INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2080     EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2081     EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2082     sink_->ClearMessages();
   2083 
   2084     SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2085                          blink::WebGestureDeviceTouchscreen);
   2086     EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->completed_mode());
   2087     EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2088     EXPECT_EQ(1U, sink_->message_count());
   2089   }
   2090 
   2091   {
   2092     // Start over, except instead of ending the gesture with ScrollEnd, end it
   2093     // with a FlingStart, with velocity in the reverse direction. This should
   2094     // initiate an overscroll navigation, but it should be cancelled because of
   2095     // the fling in the opposite direction.
   2096     overscroll_delegate()->Reset();
   2097     SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2098                          blink::WebGestureDeviceTouchscreen);
   2099     SimulateGestureScrollUpdateEvent(-300, -5, 0);
   2100     SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2101                       INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2102     EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   2103     EXPECT_EQ(OVERSCROLL_WEST, overscroll_delegate()->current_mode());
   2104     sink_->ClearMessages();
   2105 
   2106     SimulateGestureFlingStartEvent(100, 0, blink::WebGestureDeviceTouchscreen);
   2107     EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->completed_mode());
   2108     EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2109     EXPECT_EQ(1U, sink_->message_count());
   2110   }
   2111 }
   2112 
   2113 // Tests that touch-scroll events are handled correctly by the overscroll
   2114 // controller. This also tests that the overscroll controller and the
   2115 // gesture-event filter play nice with each other.
   2116 TEST_F(RenderWidgetHostViewAuraOverscrollTest, GestureScrollOverscrolls) {
   2117   SetUpOverscrollEnvironment();
   2118 
   2119   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2120                        blink::WebGestureDeviceTouchscreen);
   2121   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2122   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2123   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2124 
   2125   // Send another gesture event and ACK as not being processed. This should
   2126   // initiate the navigation gesture.
   2127   SimulateGestureScrollUpdateEvent(55, -5, 0);
   2128   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2129                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2130   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2131   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2132   EXPECT_EQ(55.f, overscroll_delta_x());
   2133   EXPECT_EQ(-5.f, overscroll_delta_y());
   2134   EXPECT_EQ(5.f, overscroll_delegate()->delta_x());
   2135   EXPECT_EQ(-5.f, overscroll_delegate()->delta_y());
   2136   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2137 
   2138   // Send another gesture update event. This event should be consumed by the
   2139   // controller, and not be forwarded to the renderer. The gesture-event filter
   2140   // should not also receive this event.
   2141   SimulateGestureScrollUpdateEvent(10, -5, 0);
   2142   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2143   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2144   EXPECT_EQ(65.f, overscroll_delta_x());
   2145   EXPECT_EQ(-10.f, overscroll_delta_y());
   2146   EXPECT_EQ(15.f, overscroll_delegate()->delta_x());
   2147   EXPECT_EQ(-10.f, overscroll_delegate()->delta_y());
   2148   EXPECT_EQ(0U, sink_->message_count());
   2149 
   2150   // Now send a scroll end. This should cancel the overscroll gesture, and send
   2151   // the event to the renderer. The gesture-event filter should receive this
   2152   // event.
   2153   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2154                        blink::WebGestureDeviceTouchscreen);
   2155   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2156   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2157   EXPECT_EQ(1U, sink_->message_count());
   2158 }
   2159 
   2160 // Tests that if the page is scrolled because of a scroll-gesture, then that
   2161 // particular scroll sequence never generates overscroll if the scroll direction
   2162 // is horizontal.
   2163 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2164        GestureScrollConsumedHorizontal) {
   2165   SetUpOverscrollEnvironment();
   2166 
   2167   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2168                        blink::WebGestureDeviceTouchscreen);
   2169   SimulateGestureScrollUpdateEvent(10, 0, 0);
   2170 
   2171   // Start scrolling on content. ACK both events as being processed.
   2172   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2173                     INPUT_EVENT_ACK_STATE_CONSUMED);
   2174   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2175   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2176   sink_->ClearMessages();
   2177 
   2178   // Send another gesture event and ACK as not being processed. This should
   2179   // not initiate overscroll because the beginning of the scroll event did
   2180   // scroll some content on the page. Since there was no overscroll, the event
   2181   // should reach the renderer.
   2182   SimulateGestureScrollUpdateEvent(55, 0, 0);
   2183   EXPECT_EQ(1U, sink_->message_count());
   2184   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2185                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2186   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2187 }
   2188 
   2189 // Tests that the overscroll controller plays nice with touch-scrolls and the
   2190 // gesture event filter with debounce filtering turned on.
   2191 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2192        GestureScrollDebounceOverscrolls) {
   2193   SetUpOverscrollEnvironmentWithDebounce(100);
   2194 
   2195   // Start scrolling. Receive ACK as it being processed.
   2196   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2197                        blink::WebGestureDeviceTouchscreen);
   2198   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2199 
   2200   // Send update events.
   2201   SimulateGestureScrollUpdateEvent(25, 0, 0);
   2202   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2203 
   2204   // Quickly end and restart the scroll gesture. These two events should get
   2205   // discarded.
   2206   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2207                        blink::WebGestureDeviceTouchscreen);
   2208   EXPECT_EQ(0U, sink_->message_count());
   2209 
   2210   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2211                        blink::WebGestureDeviceTouchscreen);
   2212   EXPECT_EQ(0U, sink_->message_count());
   2213 
   2214   // Send another update event. This should get into the queue.
   2215   SimulateGestureScrollUpdateEvent(30, 0, 0);
   2216   EXPECT_EQ(0U, sink_->message_count());
   2217 
   2218   // Receive an ACK for the first scroll-update event as not being processed.
   2219   // This will contribute to the overscroll gesture, but not enough for the
   2220   // overscroll controller to start consuming gesture events. This also cause
   2221   // the queued gesture event to be forwarded to the renderer.
   2222   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2223                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2224   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2225   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2226   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2227 
   2228   // Send another update event. This should get into the queue.
   2229   SimulateGestureScrollUpdateEvent(10, 0, 0);
   2230   EXPECT_EQ(0U, sink_->message_count());
   2231 
   2232   // Receive an ACK for the second scroll-update event as not being processed.
   2233   // This will now initiate an overscroll. This will also cause the queued
   2234   // gesture event to be released. But instead of going to the renderer, it will
   2235   // be consumed by the overscroll controller.
   2236   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2237                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2238   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2239   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2240   EXPECT_EQ(65.f, overscroll_delta_x());
   2241   EXPECT_EQ(15.f, overscroll_delegate()->delta_x());
   2242   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2243   EXPECT_EQ(0U, sink_->message_count());
   2244 }
   2245 
   2246 // Tests that the gesture debounce timer plays nice with the overscroll
   2247 // controller.
   2248 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2249        GestureScrollDebounceTimerOverscroll) {
   2250   SetUpOverscrollEnvironmentWithDebounce(10);
   2251 
   2252   // Start scrolling. Receive ACK as it being processed.
   2253   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2254                        blink::WebGestureDeviceTouchscreen);
   2255   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2256 
   2257   // Send update events.
   2258   SimulateGestureScrollUpdateEvent(55, 0, 0);
   2259   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2260 
   2261   // Send an end event. This should get in the debounce queue.
   2262   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2263                        blink::WebGestureDeviceTouchscreen);
   2264   EXPECT_EQ(0U, sink_->message_count());
   2265 
   2266   // Receive ACK for the scroll-update event.
   2267   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2268                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2269   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2270   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2271   EXPECT_EQ(55.f, overscroll_delta_x());
   2272   EXPECT_EQ(5.f, overscroll_delegate()->delta_x());
   2273   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2274   EXPECT_EQ(0U, sink_->message_count());
   2275 
   2276   // Let the timer for the debounce queue fire. That should release the queued
   2277   // scroll-end event. Since overscroll has started, but there hasn't been
   2278   // enough overscroll to complete the gesture, the overscroll controller
   2279   // will reset the state. The scroll-end should therefore be dispatched to the
   2280   // renderer, and the gesture-event-filter should await an ACK for it.
   2281   base::MessageLoop::current()->PostDelayedTask(
   2282       FROM_HERE,
   2283       base::MessageLoop::QuitClosure(),
   2284       base::TimeDelta::FromMilliseconds(15));
   2285   base::MessageLoop::current()->Run();
   2286 
   2287   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2288   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2289   EXPECT_EQ(1U, sink_->message_count());
   2290 }
   2291 
   2292 // Tests that when touch-events are dispatched to the renderer, the overscroll
   2293 // gesture deals with them correctly.
   2294 TEST_F(RenderWidgetHostViewAuraOverscrollTest, OverscrollWithTouchEvents) {
   2295   SetUpOverscrollEnvironmentWithDebounce(10);
   2296   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
   2297   sink_->ClearMessages();
   2298 
   2299   // The test sends an intermingled sequence of touch and gesture events.
   2300   PressTouchPoint(0, 1);
   2301   SendInputEventACK(WebInputEvent::TouchStart,
   2302                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2303   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2304 
   2305   MoveTouchPoint(0, 20, 5);
   2306   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2307   SendInputEventACK(WebInputEvent::TouchMove,
   2308                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2309 
   2310   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2311   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2312 
   2313   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2314                        blink::WebGestureDeviceTouchscreen);
   2315   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2316   SimulateGestureScrollUpdateEvent(20, 0, 0);
   2317   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2318                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2319   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2320   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2321   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2322 
   2323   // Another touch move event should reach the renderer since overscroll hasn't
   2324   // started yet.  Note that touch events sent during the scroll period may
   2325   // not require an ack (having been marked uncancelable).
   2326   MoveTouchPoint(0, 65, 10);
   2327   AckLastSentInputEventIfNecessary(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2328   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2329 
   2330   SimulateGestureScrollUpdateEvent(45, 0, 0);
   2331   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2332                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2333   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2334   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2335   EXPECT_EQ(65.f, overscroll_delta_x());
   2336   EXPECT_EQ(15.f, overscroll_delegate()->delta_x());
   2337   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2338   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2339 
   2340   // Send another touch event. The page should get the touch-move event, even
   2341   // though overscroll has started.
   2342   MoveTouchPoint(0, 55, 5);
   2343   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2344   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2345   EXPECT_EQ(65.f, overscroll_delta_x());
   2346   EXPECT_EQ(15.f, overscroll_delegate()->delta_x());
   2347   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2348   AckLastSentInputEventIfNecessary(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2349   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2350 
   2351   SimulateGestureScrollUpdateEvent(-10, 0, 0);
   2352   EXPECT_EQ(0U, sink_->message_count());
   2353   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2354   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2355   EXPECT_EQ(55.f, overscroll_delta_x());
   2356   EXPECT_EQ(5.f, overscroll_delegate()->delta_x());
   2357   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2358 
   2359   PressTouchPoint(255, 5);
   2360   AckLastSentInputEventIfNecessary(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2361   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2362 
   2363   SimulateGestureScrollUpdateEvent(200, 0, 0);
   2364   EXPECT_EQ(0U, sink_->message_count());
   2365   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2366   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2367   EXPECT_EQ(255.f, overscroll_delta_x());
   2368   EXPECT_EQ(205.f, overscroll_delegate()->delta_x());
   2369   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2370 
   2371   // The touch-end/cancel event should always reach the renderer if the page has
   2372   // touch handlers.
   2373   ReleaseTouchPoint(1);
   2374   AckLastSentInputEventIfNecessary(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2375   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2376   ReleaseTouchPoint(0);
   2377   AckLastSentInputEventIfNecessary(INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2378   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2379 
   2380   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
   2381                        blink::WebGestureDeviceTouchscreen);
   2382   base::MessageLoop::current()->PostDelayedTask(
   2383       FROM_HERE,
   2384       base::MessageLoop::QuitClosure(),
   2385       base::TimeDelta::FromMilliseconds(10));
   2386   base::MessageLoop::current()->Run();
   2387   EXPECT_EQ(1U, sink_->message_count());
   2388   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2389   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2390   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->completed_mode());
   2391 }
   2392 
   2393 // Tests that touch-gesture end is dispatched to the renderer at the end of a
   2394 // touch-gesture initiated overscroll.
   2395 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2396        TouchGestureEndDispatchedAfterOverscrollComplete) {
   2397   SetUpOverscrollEnvironmentWithDebounce(10);
   2398   widget_host_->OnMessageReceived(ViewHostMsg_HasTouchEventHandlers(0, true));
   2399   sink_->ClearMessages();
   2400 
   2401   // Start scrolling. Receive ACK as it being processed.
   2402   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2403                        blink::WebGestureDeviceTouchscreen);
   2404   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2405   // The scroll begin event will have received a synthetic ack from the input
   2406   // router.
   2407   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2408   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2409 
   2410   // Send update events.
   2411   SimulateGestureScrollUpdateEvent(55, -5, 0);
   2412   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2413   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2414   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2415 
   2416   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2417                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2418   EXPECT_EQ(0U, sink_->message_count());
   2419   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2420   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2421   EXPECT_EQ(55.f, overscroll_delta_x());
   2422   EXPECT_EQ(5.f, overscroll_delegate()->delta_x());
   2423   EXPECT_EQ(-5.f, overscroll_delegate()->delta_y());
   2424 
   2425   // Send end event.
   2426   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
   2427                        blink::WebGestureDeviceTouchscreen);
   2428   EXPECT_EQ(0U, sink_->message_count());
   2429   base::MessageLoop::current()->PostDelayedTask(
   2430       FROM_HERE,
   2431       base::MessageLoop::QuitClosure(),
   2432       base::TimeDelta::FromMilliseconds(10));
   2433   base::MessageLoop::current()->Run();
   2434   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2435   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2436   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->completed_mode());
   2437   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2438 
   2439   // Start scrolling. Receive ACK as it being processed.
   2440   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2441                        blink::WebGestureDeviceTouchscreen);
   2442   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2443   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2444   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2445 
   2446   // Send update events.
   2447   SimulateGestureScrollUpdateEvent(235, -5, 0);
   2448   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2449   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2450   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2451 
   2452   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2453                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2454   EXPECT_EQ(0U, sink_->message_count());
   2455   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2456   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2457   EXPECT_EQ(235.f, overscroll_delta_x());
   2458   EXPECT_EQ(185.f, overscroll_delegate()->delta_x());
   2459   EXPECT_EQ(-5.f, overscroll_delegate()->delta_y());
   2460 
   2461   // Send end event.
   2462   SimulateGestureEvent(blink::WebInputEvent::GestureScrollEnd,
   2463                        blink::WebGestureDeviceTouchscreen);
   2464   EXPECT_EQ(0U, sink_->message_count());
   2465   base::MessageLoop::current()->PostDelayedTask(
   2466       FROM_HERE,
   2467       base::MessageLoop::QuitClosure(),
   2468       base::TimeDelta::FromMilliseconds(10));
   2469   base::MessageLoop::current()->Run();
   2470   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2471   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2472   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->completed_mode());
   2473   EXPECT_EQ(1U, sink_->message_count());
   2474 }
   2475 
   2476 TEST_F(RenderWidgetHostViewAuraOverscrollTest, OverscrollDirectionChange) {
   2477   SetUpOverscrollEnvironmentWithDebounce(100);
   2478 
   2479   // Start scrolling. Receive ACK as it being processed.
   2480   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2481                        blink::WebGestureDeviceTouchscreen);
   2482   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2483 
   2484   // Send update events and receive ack as not consumed.
   2485   SimulateGestureScrollUpdateEvent(125, -5, 0);
   2486   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2487 
   2488   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2489                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2490   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2491   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2492   EXPECT_EQ(0U, sink_->message_count());
   2493 
   2494   // Send another update event, but in the reverse direction. The overscroll
   2495   // controller will consume the event, and reset the overscroll mode.
   2496   SimulateGestureScrollUpdateEvent(-260, 0, 0);
   2497   EXPECT_EQ(0U, sink_->message_count());
   2498   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2499 
   2500   // Since the overscroll mode has been reset, the next scroll update events
   2501   // should reach the renderer.
   2502   SimulateGestureScrollUpdateEvent(-20, 0, 0);
   2503   EXPECT_EQ(1U, sink_->message_count());
   2504   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2505 }
   2506 
   2507 // Tests that if a mouse-move event completes the overscroll gesture, future
   2508 // move events do reach the renderer.
   2509 TEST_F(RenderWidgetHostViewAuraOverscrollTest, OverscrollMouseMoveCompletion) {
   2510   SetUpOverscrollEnvironment();
   2511 
   2512   SimulateWheelEvent(5, 0, 0, true);     // sent directly
   2513   SimulateWheelEvent(-1, 0, 0, true);    // enqueued
   2514   SimulateWheelEvent(-10, -3, 0, true);  // coalesced into previous event
   2515   SimulateWheelEvent(-15, -1, 0, true);  // coalesced into previous event
   2516   SimulateWheelEvent(-30, -3, 0, true);  // coalesced into previous event
   2517   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2518   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2519 
   2520   // Receive ACK the first wheel event as not processed.
   2521   SendInputEventACK(WebInputEvent::MouseWheel,
   2522                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2523   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2524   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2525   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2526 
   2527   // Receive ACK for the second (coalesced) event as not processed. This will
   2528   // start an overcroll gesture.
   2529   SendInputEventACK(WebInputEvent::MouseWheel,
   2530                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2531   EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   2532   EXPECT_EQ(OVERSCROLL_WEST, overscroll_delegate()->current_mode());
   2533   EXPECT_EQ(0U, sink_->message_count());
   2534 
   2535   // Send a mouse-move event. This should cancel the overscroll navigation
   2536   // (since the amount overscrolled is not above the threshold), and so the
   2537   // mouse-move should reach the renderer.
   2538   SimulateMouseMove(5, 10, 0);
   2539   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2540   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->completed_mode());
   2541   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2542   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2543 
   2544   SendInputEventACK(WebInputEvent::MouseMove,
   2545                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2546 
   2547   // Moving the mouse more should continue to send the events to the renderer.
   2548   SimulateMouseMove(5, 10, 0);
   2549   SendInputEventACK(WebInputEvent::MouseMove,
   2550                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2551   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2552 
   2553   // Now try with gestures.
   2554   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2555                        blink::WebGestureDeviceTouchscreen);
   2556   SimulateGestureScrollUpdateEvent(300, -5, 0);
   2557   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2558                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2559   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2560   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2561   sink_->ClearMessages();
   2562 
   2563   // Overscroll gesture is in progress. Send a mouse-move now. This should
   2564   // complete the gesture (because the amount overscrolled is above the
   2565   // threshold).
   2566   SimulateMouseMove(5, 10, 0);
   2567   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->completed_mode());
   2568   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2569   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2570   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2571   SendInputEventACK(WebInputEvent::MouseMove,
   2572                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2573 
   2574   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2575                        blink::WebGestureDeviceTouchscreen);
   2576   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2577   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2578 
   2579   // Move mouse some more. The mouse-move events should reach the renderer.
   2580   SimulateMouseMove(5, 10, 0);
   2581   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2582 
   2583   SendInputEventACK(WebInputEvent::MouseMove,
   2584                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2585 }
   2586 
   2587 // Tests that if a page scrolled, then the overscroll controller's states are
   2588 // reset after the end of the scroll.
   2589 TEST_F(RenderWidgetHostViewAuraOverscrollTest,
   2590        OverscrollStateResetsAfterScroll) {
   2591   SetUpOverscrollEnvironment();
   2592 
   2593   SimulateWheelEvent(0, 5, 0, true);   // sent directly
   2594   SimulateWheelEvent(0, 30, 0, true);  // enqueued
   2595   SimulateWheelEvent(0, 40, 0, true);  // coalesced into previous event
   2596   SimulateWheelEvent(0, 10, 0, true);  // coalesced into previous event
   2597   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2598   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2599 
   2600   // The first wheel event is consumed. Dispatches the queued wheel event.
   2601   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   2602   EXPECT_TRUE(ScrollStateIsContentScrolling());
   2603   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2604 
   2605   // The second wheel event is consumed.
   2606   SendInputEventACK(WebInputEvent::MouseWheel, INPUT_EVENT_ACK_STATE_CONSUMED);
   2607   EXPECT_TRUE(ScrollStateIsContentScrolling());
   2608 
   2609   // Touchpad scroll can end with a zero-velocity fling. But it is not
   2610   // dispatched, but it should still reset the overscroll controller state.
   2611   SimulateGestureFlingStartEvent(0.f, 0.f, blink::WebGestureDeviceTouchpad);
   2612   EXPECT_TRUE(ScrollStateIsUnknown());
   2613   EXPECT_EQ(0U, sink_->message_count());
   2614 
   2615   SimulateWheelEvent(-5, 0, 0, true);    // sent directly
   2616   SimulateWheelEvent(-60, 0, 0, true);   // enqueued
   2617   SimulateWheelEvent(-100, 0, 0, true);  // coalesced into previous event
   2618   EXPECT_TRUE(ScrollStateIsUnknown());
   2619   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2620 
   2621   // The first wheel scroll did not scroll content. Overscroll should not start
   2622   // yet, since enough hasn't been scrolled.
   2623   SendInputEventACK(WebInputEvent::MouseWheel,
   2624                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2625   EXPECT_TRUE(ScrollStateIsUnknown());
   2626   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2627 
   2628   SendInputEventACK(WebInputEvent::MouseWheel,
   2629                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2630   EXPECT_EQ(OVERSCROLL_WEST, overscroll_mode());
   2631   EXPECT_TRUE(ScrollStateIsOverscrolling());
   2632   EXPECT_EQ(0U, sink_->message_count());
   2633 
   2634   SimulateGestureFlingStartEvent(0.f, 0.f, blink::WebGestureDeviceTouchpad);
   2635   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2636   EXPECT_EQ(OVERSCROLL_WEST, overscroll_delegate()->completed_mode());
   2637   EXPECT_TRUE(ScrollStateIsUnknown());
   2638   EXPECT_EQ(0U, sink_->message_count());
   2639 }
   2640 
   2641 TEST_F(RenderWidgetHostViewAuraOverscrollTest, OverscrollResetsOnBlur) {
   2642   SetUpOverscrollEnvironment();
   2643 
   2644   // Start an overscroll with gesture scroll. In the middle of the scroll, blur
   2645   // the host.
   2646   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2647                        blink::WebGestureDeviceTouchscreen);
   2648   SimulateGestureScrollUpdateEvent(300, -5, 0);
   2649   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2650                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2651   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2652   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2653   EXPECT_EQ(2U, GetSentMessageCountAndResetSink());
   2654 
   2655   view_->OnWindowFocused(NULL, view_->GetAttachedWindow());
   2656   EXPECT_EQ(OVERSCROLL_NONE, overscroll_mode());
   2657   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2658   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->completed_mode());
   2659   EXPECT_EQ(0.f, overscroll_delegate()->delta_x());
   2660   EXPECT_EQ(0.f, overscroll_delegate()->delta_y());
   2661   sink_->ClearMessages();
   2662 
   2663   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2664                        blink::WebGestureDeviceTouchscreen);
   2665   EXPECT_EQ(1U, GetSentMessageCountAndResetSink());
   2666 
   2667   // Start a scroll gesture again. This should correctly start the overscroll
   2668   // after the threshold.
   2669   SimulateGestureEvent(WebInputEvent::GestureScrollBegin,
   2670                        blink::WebGestureDeviceTouchscreen);
   2671   SimulateGestureScrollUpdateEvent(300, -5, 0);
   2672   SendInputEventACK(WebInputEvent::GestureScrollUpdate,
   2673                     INPUT_EVENT_ACK_STATE_NOT_CONSUMED);
   2674   EXPECT_EQ(OVERSCROLL_EAST, overscroll_mode());
   2675   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->current_mode());
   2676   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->completed_mode());
   2677 
   2678   SimulateGestureEvent(WebInputEvent::GestureScrollEnd,
   2679                        blink::WebGestureDeviceTouchscreen);
   2680   EXPECT_EQ(OVERSCROLL_NONE, overscroll_delegate()->current_mode());
   2681   EXPECT_EQ(OVERSCROLL_EAST, overscroll_delegate()->completed_mode());
   2682   EXPECT_EQ(3U, sink_->message_count());
   2683 }
   2684 
   2685 }  // namespace content
   2686