overscroll_navigation_overlay_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2014 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/web_contents/aura/overscroll_navigation_overlay.h"
6
7#include "content/browser/web_contents/aura/image_window_delegate.h"
8#include "content/common/view_messages.h"
9#include "content/public/browser/web_contents_view.h"
10#include "content/public/test/mock_render_process_host.h"
11#include "content/test/test_render_view_host.h"
12#include "content/test/test_web_contents.h"
13#include "ui/aura/test/test_windows.h"
14#include "ui/aura/window.h"
15
16namespace content {
17
18class OverscrollNavigationOverlayTest : public RenderViewHostImplTestHarness {
19 public:
20  OverscrollNavigationOverlayTest() {}
21  virtual ~OverscrollNavigationOverlayTest() {}
22
23  scoped_ptr<aura::Window> CreateOverlayWinow(ImageWindowDelegate* delegate) {
24    return make_scoped_ptr(
25        aura::test::CreateTestWindowWithDelegate(delegate, 0,
26            gfx::Rect(root_window()->bounds().size()),
27            root_window()));
28  }
29
30  gfx::Image CreateDummyScreenshot() {
31    SkBitmap bitmap;
32    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
33    bitmap.allocPixels();
34    bitmap.eraseColor(SK_ColorWHITE);
35    return gfx::Image::CreateFrom1xBitmap(bitmap);
36  }
37
38 protected:
39  // RenderViewHostImplTestHarness:
40  virtual void SetUp() OVERRIDE {
41    RenderViewHostImplTestHarness::SetUp();
42
43    const GURL first("https://www.google.com");
44    contents()->NavigateAndCommit(first);
45    EXPECT_TRUE(controller().GetVisibleEntry());
46    EXPECT_FALSE(controller().CanGoBack());
47
48    const GURL second("http://www.chromium.org");
49    contents()->NavigateAndCommit(second);
50    EXPECT_TRUE(controller().CanGoBack());
51
52    // Turn on compositing.
53    ViewHostMsg_DidActivateAcceleratedCompositing msg(
54        test_rvh()->GetRoutingID(), true);
55    RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg);
56
57    // Receive a paint update. This is necessary to make sure the size is set
58    // correctly in RenderWidgetHostImpl.
59    ViewHostMsg_UpdateRect_Params params;
60    memset(&params, 0, sizeof(params));
61    params.view_size = gfx::Size(10, 10);
62    params.bitmap_rect = gfx::Rect(params.view_size);
63    params.scroll_rect = gfx::Rect();
64    params.needs_ack = false;
65    ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params);
66    RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect);
67
68    // Reset pending flags for size/paint.
69    test_rvh()->ResetSizeAndRepaintPendingFlags();
70  }
71
72 private:
73  DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlayTest);
74};
75
76TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_NoImage) {
77  // Create the overlay, and set the contents of the overlay window.
78  scoped_ptr<OverscrollNavigationOverlay> overlay(
79      new OverscrollNavigationOverlay(contents()));
80  ImageWindowDelegate* image_delegate = new ImageWindowDelegate();
81  overlay->SetOverlayWindow(CreateOverlayWinow(image_delegate),
82                            image_delegate);
83  overlay->StartObserving();
84
85  EXPECT_FALSE(overlay->received_paint_update_);
86  EXPECT_TRUE(overlay->web_contents());
87
88  // Receive a paint update.
89  ViewHostMsg_DidFirstVisuallyNonEmptyPaint msg(test_rvh()->GetRoutingID(), 0);
90  RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg);
91  EXPECT_TRUE(overlay->received_paint_update_);
92  EXPECT_FALSE(overlay->loading_complete_);
93
94  // The paint update will hide the overlay, although the page hasn't completely
95  // loaded yet. This is because the image-delegate doesn't have an image set.
96  EXPECT_FALSE(overlay->web_contents());
97}
98
99TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_WithImage) {
100  // Create the overlay, and set the contents of the overlay window.
101  scoped_ptr<OverscrollNavigationOverlay> overlay(
102      new OverscrollNavigationOverlay(contents()));
103  ImageWindowDelegate* image_delegate = new ImageWindowDelegate();
104  image_delegate->SetImage(CreateDummyScreenshot());
105  overlay->SetOverlayWindow(CreateOverlayWinow(image_delegate),
106                            image_delegate);
107  overlay->StartObserving();
108
109  EXPECT_FALSE(overlay->received_paint_update_);
110  EXPECT_TRUE(overlay->web_contents());
111
112  // Receive a paint update.
113  ViewHostMsg_DidFirstVisuallyNonEmptyPaint msg(test_rvh()->GetRoutingID(), 0);
114  RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg);
115  EXPECT_TRUE(overlay->received_paint_update_);
116  EXPECT_FALSE(overlay->loading_complete_);
117  EXPECT_TRUE(overlay->web_contents());
118
119  contents()->TestSetIsLoading(false);
120  EXPECT_TRUE(overlay->loading_complete_);
121  EXPECT_FALSE(overlay->web_contents());
122}
123
124TEST_F(OverscrollNavigationOverlayTest, PaintUpdateWithoutNonEmptyPaint) {
125  // Create the overlay, and set the contents of the overlay window.
126  scoped_ptr<OverscrollNavigationOverlay> overlay(
127      new OverscrollNavigationOverlay(contents()));
128  ImageWindowDelegate* image_delegate = new ImageWindowDelegate();
129  image_delegate->SetImage(CreateDummyScreenshot());
130  overlay->SetOverlayWindow(CreateOverlayWinow(image_delegate),
131                            image_delegate);
132  overlay->StartObserving();
133
134  process()->sink().ClearMessages();
135
136  // The page load is complete, but the overlay should still be visible, because
137  // there hasn't been any paint update.
138  // This should also send a repaint request to the renderer, so that the
139  // renderer repaints the contents.
140  contents()->TestSetIsLoading(false);
141  EXPECT_FALSE(overlay->received_paint_update_);
142  EXPECT_TRUE(overlay->loading_complete_);
143  EXPECT_TRUE(overlay->web_contents());
144  EXPECT_TRUE(process()->sink().GetFirstMessageMatching(ViewMsg_Repaint::ID));
145
146  // Receive a repaint ack update. This should hide the overlay.
147  ViewHostMsg_UpdateRect_Params params;
148  memset(&params, 0, sizeof(params));
149  params.view_size = gfx::Size(10, 10);
150  params.bitmap_rect = gfx::Rect(params.view_size);
151  params.scroll_rect = gfx::Rect();
152  params.needs_ack = false;
153  params.flags = ViewHostMsg_UpdateRect_Flags::IS_REPAINT_ACK;
154  ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params);
155  RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect);
156  EXPECT_TRUE(overlay->received_paint_update_);
157  EXPECT_FALSE(overlay->web_contents());
158}
159
160}  // namespace content
161