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