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