OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h" | 5 #include "content/browser/web_contents/aura/overscroll_navigation_overlay.h" |
6 | 6 |
7 #include "content/browser/frame_host/navigation_entry_impl.h" | 7 #include "content/browser/frame_host/navigation_entry_impl.h" |
8 #include "content/browser/web_contents/web_contents_view.h" | 8 #include "content/browser/web_contents/web_contents_view.h" |
9 #include "content/common/frame_messages.h" | 9 #include "content/common/frame_messages.h" |
10 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
11 #include "content/public/test/mock_render_process_host.h" | 11 #include "content/public/test/mock_render_process_host.h" |
12 #include "content/test/test_render_frame_host.h" | 12 #include "content/test/test_render_frame_host.h" |
13 #include "content/test/test_render_view_host.h" | 13 #include "content/test/test_render_view_host.h" |
14 #include "content/test/test_web_contents.h" | 14 #include "content/test/test_web_contents.h" |
15 #include "ui/aura/test/test_windows.h" | 15 #include "ui/aura/test/test_windows.h" |
16 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
17 #include "ui/aura_extra/image_window_delegate.h" | 17 #include "ui/aura_extra/image_window_delegate.h" |
18 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 class OverscrollNavigationOverlayTest : public RenderViewHostImplTestHarness { | 22 class OverscrollNavigationOverlayTest : public RenderViewHostImplTestHarness { |
23 public: | 23 public: |
24 OverscrollNavigationOverlayTest() {} | 24 OverscrollNavigationOverlayTest() |
| 25 : first_("https://www.google.com"), |
| 26 second_("http://www.chromium.org"), |
| 27 third_("https://www.kernel.org/") { } |
| 28 |
25 ~OverscrollNavigationOverlayTest() override {} | 29 ~OverscrollNavigationOverlayTest() override {} |
26 | 30 |
27 gfx::Image CreateDummyScreenshot() { | 31 gfx::Image CreateDummyScreenshot() { |
28 SkBitmap bitmap; | 32 SkBitmap bitmap; |
29 bitmap.allocN32Pixels(1, 1); | 33 bitmap.allocN32Pixels(1, 1); |
30 bitmap.eraseColor(SK_ColorWHITE); | 34 bitmap.eraseColor(SK_ColorWHITE); |
31 return gfx::Image::CreateFrom1xBitmap(bitmap); | 35 return gfx::Image::CreateFrom1xBitmap(bitmap); |
32 } | 36 } |
33 | 37 |
34 void SetDummyScreenshotOnNavEntry(NavigationEntry* entry) { | 38 void SetDummyScreenshotOnNavEntry(NavigationEntry* entry) { |
35 SkBitmap bitmap; | 39 SkBitmap bitmap; |
36 bitmap.allocN32Pixels(1, 1); | 40 bitmap.allocN32Pixels(1, 1); |
37 bitmap.eraseColor(SK_ColorWHITE); | 41 bitmap.eraseColor(SK_ColorWHITE); |
38 std::vector<unsigned char> png_data; | 42 std::vector<unsigned char> png_data; |
39 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data); | 43 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data); |
40 scoped_refptr<base::RefCountedBytes> png_bytes = | 44 scoped_refptr<base::RefCountedBytes> png_bytes = |
41 base::RefCountedBytes::TakeVector(&png_data); | 45 base::RefCountedBytes::TakeVector(&png_data); |
42 NavigationEntryImpl* entry_impl = | 46 NavigationEntryImpl* entry_impl = |
43 NavigationEntryImpl::FromNavigationEntry(entry); | 47 NavigationEntryImpl::FromNavigationEntry(entry); |
44 entry_impl->SetScreenshotPNGData(png_bytes); | 48 entry_impl->SetScreenshotPNGData(png_bytes); |
45 } | 49 } |
46 | 50 |
47 void ReceivePaintUpdate() { | 51 void ReceivePaintUpdate() { |
48 FrameHostMsg_DidFirstVisuallyNonEmptyPaint msg( | 52 FrameHostMsg_DidFirstVisuallyNonEmptyPaint msg( |
49 main_test_rfh()->GetRoutingID()); | 53 main_test_rfh()->GetRoutingID()); |
50 RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg); | 54 RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg); |
51 } | 55 } |
52 | 56 |
53 void PerformBackNavigationViaSliderCallbacks() { | 57 void PerformBackNavigationViaSliderCallbacks() { |
54 // Sets slide direction to SLIDE_BACK, sets screenshot from NavEntry at | 58 // Sets slide direction to BACK, sets screenshot from NavEntry at |
55 // offset -1 on layer_delegate_. | 59 // offset -1 on layer_delegate_. |
56 delete GetOverlay()->CreateBackLayer(); | 60 scoped_ptr<SlidableWrapper> wrapper(GetOverlay()->CreateBackWrapper()); |
57 // Performs BACK navigation, sets image from layer_delegate_ on | 61 // Performs BACK navigation, sets image from layer_delegate_ on |
58 // image_delegate_. | 62 // image_delegate_. |
59 GetOverlay()->OnWindowSlideCompleting(); | 63 GetOverlay()->OnOverscrollCompleting(); |
60 GetOverlay()->OnWindowSlideCompleted(scoped_ptr<ui::Layer>()); | 64 GetOverlay()->OnOverscrollCompleted(wrapper.Pass()); |
61 } | 65 } |
62 | 66 |
| 67 // Tests URLs. |
| 68 const GURL first_; |
| 69 const GURL second_; |
| 70 const GURL third_; |
| 71 |
63 protected: | 72 protected: |
64 // RenderViewHostImplTestHarness: | 73 // RenderViewHostImplTestHarness: |
65 void SetUp() override { | 74 void SetUp() override { |
66 RenderViewHostImplTestHarness::SetUp(); | 75 RenderViewHostImplTestHarness::SetUp(); |
67 | 76 |
68 const GURL first("https://www.google.com"); | 77 contents()->NavigateAndCommit(first_); |
69 contents()->NavigateAndCommit(first); | |
70 EXPECT_TRUE(controller().GetVisibleEntry()); | 78 EXPECT_TRUE(controller().GetVisibleEntry()); |
71 EXPECT_FALSE(controller().CanGoBack()); | 79 EXPECT_FALSE(controller().CanGoBack()); |
72 | 80 |
73 const GURL second("http://www.chromium.org"); | 81 contents()->NavigateAndCommit(second_); |
74 contents()->NavigateAndCommit(second); | |
75 EXPECT_TRUE(controller().CanGoBack()); | 82 EXPECT_TRUE(controller().CanGoBack()); |
76 | 83 |
| 84 contents()->NavigateAndCommit(third_); |
| 85 EXPECT_TRUE(controller().CanGoBack()); |
| 86 EXPECT_FALSE(controller().CanGoForward()); |
| 87 |
77 // Receive a paint update. This is necessary to make sure the size is set | 88 // Receive a paint update. This is necessary to make sure the size is set |
78 // correctly in RenderWidgetHostImpl. | 89 // correctly in RenderWidgetHostImpl. |
79 ViewHostMsg_UpdateRect_Params params; | 90 ViewHostMsg_UpdateRect_Params params; |
80 memset(¶ms, 0, sizeof(params)); | 91 memset(¶ms, 0, sizeof(params)); |
81 params.view_size = gfx::Size(10, 10); | 92 params.view_size = gfx::Size(10, 10); |
82 ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params); | 93 ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params); |
83 RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect); | 94 RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect); |
84 | 95 |
85 // Reset pending flags for size/paint. | 96 // Reset pending flags for size/paint. |
86 test_rvh()->ResetSizeAndRepaintPendingFlags(); | 97 test_rvh()->ResetSizeAndRepaintPendingFlags(); |
87 | 98 |
88 // Create the overlay, and set the contents of the overlay window. | 99 // Create the overlay, and set the contents of the overlay window. |
89 overlay_.reset(new OverscrollNavigationOverlay(contents())); | 100 overlay_.reset( |
90 aura_extra::ImageWindowDelegate* image_delegate = | 101 new OverscrollNavigationOverlay(contents(), |
91 new aura_extra::ImageWindowDelegate(); | 102 contents()->GetNativeView())); |
92 scoped_ptr<aura::Window> overlay_window( | |
93 aura::test::CreateTestWindowWithDelegate( | |
94 image_delegate, | |
95 0, | |
96 gfx::Rect(root_window()->bounds().size()), | |
97 root_window())); | |
98 | |
99 overlay_->SetOverlayWindow(overlay_window.Pass(), image_delegate); | |
100 overlay_->StartObserving(); | |
101 | |
102 EXPECT_TRUE(overlay_->web_contents()); | |
103 EXPECT_FALSE(overlay_->loading_complete_); | |
104 EXPECT_FALSE(overlay_->received_paint_update_); | |
105 } | 103 } |
106 | 104 |
107 void TearDown() override { | 105 void TearDown() override { |
108 overlay_.reset(); | 106 overlay_.reset(); |
109 RenderViewHostImplTestHarness::TearDown(); | 107 RenderViewHostImplTestHarness::TearDown(); |
110 } | 108 } |
111 | 109 |
112 OverscrollNavigationOverlay* GetOverlay() { | 110 OverscrollNavigationOverlay* GetOverlay() { |
113 return overlay_.get(); | 111 return overlay_.get(); |
114 } | 112 } |
115 | 113 |
116 private: | 114 private: |
117 scoped_ptr<OverscrollNavigationOverlay> overlay_; | 115 scoped_ptr<OverscrollNavigationOverlay> overlay_; |
118 | 116 |
119 DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlayTest); | 117 DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlayTest); |
120 }; | 118 }; |
121 | 119 |
122 TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_NoImage) { | 120 // Tests that if a screenshot is available, it is set in the overlay window |
123 ReceivePaintUpdate(); | 121 // delegate. |
124 EXPECT_TRUE(GetOverlay()->received_paint_update_); | 122 TEST_F(OverscrollNavigationOverlayTest, WithScreenshot) { |
125 EXPECT_FALSE(GetOverlay()->loading_complete_); | |
126 // The paint update will hide the overlay. | |
127 EXPECT_FALSE(GetOverlay()->web_contents()); | |
128 } | |
129 | |
130 TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_WithImage) { | |
131 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); | |
132 | |
133 ReceivePaintUpdate(); | |
134 EXPECT_TRUE(GetOverlay()->received_paint_update_); | |
135 EXPECT_FALSE(GetOverlay()->loading_complete_); | |
136 // The paint update will hide the overlay. | |
137 EXPECT_FALSE(GetOverlay()->web_contents()); | |
138 } | |
139 | |
140 TEST_F(OverscrollNavigationOverlayTest, LoadUpdateWithoutNonEmptyPaint) { | |
141 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); | |
142 process()->sink().ClearMessages(); | |
143 | |
144 contents()->TestSetIsLoading(false); | |
145 EXPECT_TRUE(GetOverlay()->loading_complete_); | |
146 EXPECT_FALSE(GetOverlay()->received_paint_update_); | |
147 // The page load should hide the overlay. | |
148 EXPECT_FALSE(GetOverlay()->web_contents()); | |
149 } | |
150 | |
151 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_PaintUpdate) { | |
152 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); | |
153 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1)); | 123 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1)); |
154 | |
155 PerformBackNavigationViaSliderCallbacks(); | 124 PerformBackNavigationViaSliderCallbacks(); |
156 // Screenshot was set on NavEntry at offset -1. | 125 // Screenshot was set on NavEntry at offset -1. |
157 EXPECT_TRUE(GetOverlay()->image_delegate_->has_image()); | 126 EXPECT_TRUE(GetOverlay()->image_delegate_->has_image()); |
158 EXPECT_FALSE(GetOverlay()->received_paint_update_); | 127 } |
159 | 128 |
| 129 // Tests that if a screenshot is not available, no image is set in the overlay |
| 130 // window delegate. |
| 131 TEST_F(OverscrollNavigationOverlayTest, WithoutScreenshot) { |
| 132 PerformBackNavigationViaSliderCallbacks(); |
| 133 // No screenshot was set on NavEntry at offset -1. |
| 134 EXPECT_FALSE(GetOverlay()->image_delegate_->has_image()); |
| 135 } |
| 136 |
| 137 // Tests that if a navigation is attempted but there is nothing to navigate to, |
| 138 // we return a null wrapper. |
| 139 TEST_F(OverscrollNavigationOverlayTest, CannotNavigate) { |
| 140 EXPECT_EQ(GetOverlay()->CreateFrontWrapper(), nullptr); |
| 141 } |
| 142 |
| 143 // Tests that if a navigation is aborted, no navigation is performed and the |
| 144 // state is restored. |
| 145 TEST_F(OverscrollNavigationOverlayTest, AbortNavigation) { |
| 146 scoped_ptr<SlidableWrapper> wrapper = GetOverlay()->CreateBackWrapper(); |
| 147 EXPECT_NE(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE); |
| 148 |
| 149 GetOverlay()->OnOverscrollAborted(); |
| 150 // Make sure that if we started a navigation (we shouldn't), we commit it. |
| 151 EXPECT_FALSE(contents()->cross_navigation_pending()); |
| 152 } |
| 153 |
| 154 // Tests that if a second navigation is aborted before, the first one still |
| 155 // happens. |
| 156 TEST_F(OverscrollNavigationOverlayTest, AbortAfterSuccessfulNavigation) { |
| 157 PerformBackNavigationViaSliderCallbacks(); |
| 158 scoped_ptr<SlidableWrapper> wrapper = GetOverlay()->CreateBackWrapper(); |
| 159 EXPECT_NE(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE); |
| 160 |
| 161 GetOverlay()->OnOverscrollAborted(); |
| 162 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE); |
| 163 |
| 164 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 165 contents()->CommitPendingNavigation(); |
| 166 EXPECT_EQ(contents()->GetURL(), second_); |
| 167 } |
| 168 |
| 169 // Tests that an overscroll navigation that receives a paint update actually |
| 170 // stops observing. |
| 171 TEST_F(OverscrollNavigationOverlayTest, Navigation_PaintUpdate) { |
| 172 PerformBackNavigationViaSliderCallbacks(); |
160 ReceivePaintUpdate(); | 173 ReceivePaintUpdate(); |
| 174 |
161 // Paint updates until the navigation is committed typically represent updates | 175 // Paint updates until the navigation is committed typically represent updates |
162 // for the previous page, so they shouldn't affect the flag. | 176 // for the previous page, so we should still be observing. |
163 EXPECT_FALSE(GetOverlay()->received_paint_update_); | 177 EXPECT_TRUE(GetOverlay()->web_contents()); |
164 | 178 |
165 contents()->CommitPendingNavigation(); | 179 contents()->CommitPendingNavigation(); |
166 ReceivePaintUpdate(); | 180 ReceivePaintUpdate(); |
167 // Navigation was committed and the paint update was received - the flag | |
168 // should now be updated. | |
169 EXPECT_TRUE(GetOverlay()->received_paint_update_); | |
170 | 181 |
| 182 // Navigation was committed and the paint update was received - we should no |
| 183 // longer be observing. |
171 EXPECT_FALSE(GetOverlay()->web_contents()); | 184 EXPECT_FALSE(GetOverlay()->web_contents()); |
| 185 EXPECT_EQ(contents()->GetURL(), second_); |
172 } | 186 } |
173 | 187 |
174 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_LoadingUpdate) { | 188 // Tests that an overscroll navigation that receives a loading update actually |
175 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); | 189 // stops observing. |
176 | 190 TEST_F(OverscrollNavigationOverlayTest, Navigation_LoadingUpdate) { |
177 PerformBackNavigationViaSliderCallbacks(); | 191 PerformBackNavigationViaSliderCallbacks(); |
178 // No screenshot was set on NavEntry at offset -1. | 192 EXPECT_TRUE(GetOverlay()->web_contents()); |
179 EXPECT_FALSE(GetOverlay()->image_delegate_->has_image()); | |
180 // Navigation was started, so the loading status flag should be reset. | |
181 EXPECT_FALSE(GetOverlay()->loading_complete_); | |
182 | |
183 // DidStopLoading for any navigation should always reset the load flag and | 193 // DidStopLoading for any navigation should always reset the load flag and |
184 // dismiss the overlay even if the pending navigation wasn't committed - | 194 // dismiss the overlay even if the pending navigation wasn't committed - |
185 // this is a "safety net" in case we mis-identify the destination webpage | 195 // this is a "safety net" in case we mis-identify the destination webpage |
| 196 // (which can happen if a new navigation is performed while while a GestureNav |
| 197 // navigation is in progress). |
| 198 contents()->TestSetIsLoading(true); |
| 199 contents()->TestSetIsLoading(false); |
| 200 EXPECT_FALSE(GetOverlay()->web_contents()); |
| 201 contents()->CommitPendingNavigation(); |
| 202 EXPECT_EQ(contents()->GetURL(), second_); |
| 203 } |
| 204 |
| 205 // Tests that an overscroll gesture followed after another one before the first |
| 206 // one finishes loading works with a paint update. |
| 207 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_PaintUpdate) { |
| 208 PerformBackNavigationViaSliderCallbacks(); |
| 209 EXPECT_TRUE(GetOverlay()->web_contents()); |
| 210 PerformBackNavigationViaSliderCallbacks(); |
| 211 EXPECT_TRUE(GetOverlay()->web_contents()); |
| 212 ReceivePaintUpdate(); |
| 213 |
| 214 // Paint updates until the navigation is committed typically represent updates |
| 215 // for the previous page, so we should still be observing. |
| 216 EXPECT_TRUE(GetOverlay()->web_contents()); |
| 217 |
| 218 contents()->CommitPendingNavigation(); |
| 219 ReceivePaintUpdate(); |
| 220 |
| 221 // Navigation was committed and the paint update was received - we should no |
| 222 // longer be observing. |
| 223 EXPECT_FALSE(GetOverlay()->web_contents()); |
| 224 EXPECT_EQ(contents()->GetURL(), first_); |
| 225 } |
| 226 |
| 227 // Tests that an overscroll gesture followed after another one before the first |
| 228 // one finishes loading works with a loading update. |
| 229 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_LoadingUpdate) { |
| 230 PerformBackNavigationViaSliderCallbacks(); |
| 231 EXPECT_TRUE(GetOverlay()->web_contents()); |
| 232 PerformBackNavigationViaSliderCallbacks(); |
| 233 EXPECT_TRUE(GetOverlay()->web_contents()); |
| 234 // DidStopLoading for any navigation should always reset the load flag and |
| 235 // dismiss the overlay even if the pending navigation wasn't committed - |
| 236 // this is a "safety net" in case we mis-identify the destination webpage |
186 // (which can happen if a new navigation is performed while while a GestureNav | 237 // (which can happen if a new navigation is performed while while a GestureNav |
187 // navigation is in progress). | 238 // navigation is in progress). |
188 contents()->TestSetIsLoading(true); | 239 contents()->TestSetIsLoading(true); |
189 contents()->TestSetIsLoading(false); | 240 contents()->TestSetIsLoading(false); |
190 EXPECT_TRUE(GetOverlay()->loading_complete_); | |
191 | |
192 EXPECT_FALSE(GetOverlay()->web_contents()); | 241 EXPECT_FALSE(GetOverlay()->web_contents()); |
| 242 contents()->CommitPendingNavigation(); |
| 243 EXPECT_EQ(contents()->GetURL(), first_); |
193 } | 244 } |
194 | 245 |
195 } // namespace content | 246 } // namespace content |
OLD | NEW |