Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: content/browser/web_contents/aura/overscroll_navigation_overlay_unittest.cc

Issue 895543005: Refactor GestureNavigation to eliminate code redundancy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Last round of comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
19 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
20 #include "ui/compositor/scoped_layer_animation_settings.h"
21 #include "ui/compositor/test/layer_animator_test_controller.h"
22 #include "ui/events/test/event_generator.h"
18 #include "ui/gfx/codec/png_codec.h" 23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/frame_time.h"
19 25
20 namespace content { 26 namespace content {
21 27
28 // A subclass of TestWebContents that offers a fake content window.
29 class OverscrollTestWebContents : public TestWebContents {
30 public:
31 ~OverscrollTestWebContents() override {}
32
33 static OverscrollTestWebContents* Create(
34 BrowserContext* browser_context,
35 SiteInstance* instance,
36 scoped_ptr<aura::Window> fake_native_view,
37 scoped_ptr<aura::Window> fake_contents_window) {
38 OverscrollTestWebContents* web_contents =
39 new OverscrollTestWebContents(browser_context,
40 fake_native_view.Pass(),
41 fake_contents_window.Pass());
42 web_contents->Init(WebContents::CreateParams(browser_context, instance));
43 web_contents->RenderFrameCreated(web_contents->GetMainFrame());
44 return web_contents;
45 }
46
47 gfx::NativeView GetNativeView() override {
48 return fake_native_view_.get();
49 }
50
51 gfx::NativeView GetContentNativeView() override {
52 return fake_contents_window_.get();
53 }
54
55 protected:
56 explicit OverscrollTestWebContents(
57 BrowserContext* browser_context,
58 scoped_ptr<aura::Window> fake_native_view,
59 scoped_ptr<aura::Window> fake_contents_window)
60 : TestWebContents(browser_context),
61 fake_native_view_(fake_native_view.Pass()),
62 fake_contents_window_(fake_contents_window.Pass()) {
63 }
64
65 private:
66 scoped_ptr<aura::Window> fake_native_view_;
67 scoped_ptr<aura::Window> fake_contents_window_;
68 };
69
22 class OverscrollNavigationOverlayTest : public RenderViewHostImplTestHarness { 70 class OverscrollNavigationOverlayTest : public RenderViewHostImplTestHarness {
23 public: 71 public:
24 OverscrollNavigationOverlayTest() {} 72 OverscrollNavigationOverlayTest()
73 : first_("https://www.google.com"),
74 second_("http://www.chromium.org"),
75 third_("https://www.kernel.org/"),
76 fourth_("https://github.com/") {
77 }
78
25 ~OverscrollNavigationOverlayTest() override {} 79 ~OverscrollNavigationOverlayTest() override {}
26 80
27 gfx::Image CreateDummyScreenshot() {
28 SkBitmap bitmap;
29 bitmap.allocN32Pixels(1, 1);
30 bitmap.eraseColor(SK_ColorWHITE);
31 return gfx::Image::CreateFrom1xBitmap(bitmap);
32 }
33
34 void SetDummyScreenshotOnNavEntry(NavigationEntry* entry) { 81 void SetDummyScreenshotOnNavEntry(NavigationEntry* entry) {
35 SkBitmap bitmap; 82 SkBitmap bitmap;
36 bitmap.allocN32Pixels(1, 1); 83 bitmap.allocN32Pixels(1, 1);
37 bitmap.eraseColor(SK_ColorWHITE); 84 bitmap.eraseColor(SK_ColorWHITE);
38 std::vector<unsigned char> png_data; 85 std::vector<unsigned char> png_data;
39 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data); 86 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data);
40 scoped_refptr<base::RefCountedBytes> png_bytes = 87 scoped_refptr<base::RefCountedBytes> png_bytes =
41 base::RefCountedBytes::TakeVector(&png_data); 88 base::RefCountedBytes::TakeVector(&png_data);
42 NavigationEntryImpl* entry_impl = 89 NavigationEntryImpl* entry_impl =
43 NavigationEntryImpl::FromNavigationEntry(entry); 90 NavigationEntryImpl::FromNavigationEntry(entry);
44 entry_impl->SetScreenshotPNGData(png_bytes); 91 entry_impl->SetScreenshotPNGData(png_bytes);
45 } 92 }
46 93
47 void ReceivePaintUpdate() { 94 void ReceivePaintUpdate() {
48 FrameHostMsg_DidFirstVisuallyNonEmptyPaint msg( 95 FrameHostMsg_DidFirstVisuallyNonEmptyPaint msg(
49 main_test_rfh()->GetRoutingID()); 96 main_test_rfh()->GetRoutingID());
50 RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg); 97 RenderViewHostTester::TestOnMessageReceived(test_rvh(), msg);
51 } 98 }
52 99
53 void PerformBackNavigationViaSliderCallbacks() { 100 void PerformBackNavigationViaSliderCallbacks() {
54 // Sets slide direction to SLIDE_BACK, sets screenshot from NavEntry at 101 // Sets slide direction to BACK, sets screenshot from NavEntry at
55 // offset -1 on layer_delegate_. 102 // offset -1 on layer_delegate_.
56 delete GetOverlay()->CreateBackLayer(); 103 scoped_ptr<aura::Window> window(GetOverlay()->CreateBackWindow());
57 // Performs BACK navigation, sets image from layer_delegate_ on 104 // Performs BACK navigation, sets image from layer_delegate_ on
58 // image_delegate_. 105 // image_delegate_.
59 GetOverlay()->OnWindowSlideCompleting(); 106 GetOverlay()->OnOverscrollCompleting();
60 GetOverlay()->OnWindowSlideCompleted(scoped_ptr<ui::Layer>()); 107 window->SetBounds(gfx::Rect(root_window()->bounds().size()));
108 GetOverlay()->OnOverscrollCompleted(window.Pass());
61 } 109 }
62 110
111 // Tests URLs.
112 const GURL first_;
113 const GURL second_;
114 const GURL third_;
115 const GURL fourth_;
116
63 protected: 117 protected:
64 // RenderViewHostImplTestHarness: 118 // RenderViewHostImplTestHarness:
65 void SetUp() override { 119 void SetUp() override {
66 RenderViewHostImplTestHarness::SetUp(); 120 RenderViewHostImplTestHarness::SetUp();
67 121
68 const GURL first("https://www.google.com"); 122 // Set up the fake web contents native view.
69 contents()->NavigateAndCommit(first); 123 scoped_ptr<aura::Window> fake_native_view(new aura::Window(nullptr));
124 fake_native_view->Init(aura::WINDOW_LAYER_SOLID_COLOR);
125 root_window()->AddChild(fake_native_view.get());
126 fake_native_view->SetBounds(gfx::Rect(root_window()->bounds().size()));
127
128 // Set up the fake contents window.
129 scoped_ptr<aura::Window> fake_contents_window(new aura::Window(nullptr));
130 fake_contents_window->Init(aura::WINDOW_LAYER_SOLID_COLOR);
131 root_window()->AddChild(fake_contents_window.get());
132 fake_contents_window->SetBounds(gfx::Rect(root_window()->bounds().size()));
133
134 // Replace the default test web contents with our custom class.
135 SetContents(OverscrollTestWebContents::Create(
136 browser_context(),
137 SiteInstance::Create(browser_context()),
138 fake_native_view.Pass(),
139 fake_contents_window.Pass()));
140
141 contents()->NavigateAndCommit(first_);
70 EXPECT_TRUE(controller().GetVisibleEntry()); 142 EXPECT_TRUE(controller().GetVisibleEntry());
71 EXPECT_FALSE(controller().CanGoBack()); 143 EXPECT_FALSE(controller().CanGoBack());
72 144
73 const GURL second("http://www.chromium.org"); 145 contents()->NavigateAndCommit(second_);
74 contents()->NavigateAndCommit(second);
75 EXPECT_TRUE(controller().CanGoBack()); 146 EXPECT_TRUE(controller().CanGoBack());
76 147
148 contents()->NavigateAndCommit(third_);
149 EXPECT_TRUE(controller().CanGoBack());
150
151 contents()->NavigateAndCommit(fourth_);
152 EXPECT_TRUE(controller().CanGoBack());
153 EXPECT_FALSE(controller().CanGoForward());
154
77 // Receive a paint update. This is necessary to make sure the size is set 155 // Receive a paint update. This is necessary to make sure the size is set
78 // correctly in RenderWidgetHostImpl. 156 // correctly in RenderWidgetHostImpl.
79 ViewHostMsg_UpdateRect_Params params; 157 ViewHostMsg_UpdateRect_Params params;
80 memset(&params, 0, sizeof(params)); 158 memset(&params, 0, sizeof(params));
81 params.view_size = gfx::Size(10, 10); 159 params.view_size = gfx::Size(10, 10);
82 ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params); 160 ViewHostMsg_UpdateRect rect(test_rvh()->GetRoutingID(), params);
83 RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect); 161 RenderViewHostTester::TestOnMessageReceived(test_rvh(), rect);
84 162
85 // Reset pending flags for size/paint. 163 // Reset pending flags for size/paint.
86 test_rvh()->ResetSizeAndRepaintPendingFlags(); 164 test_rvh()->ResetSizeAndRepaintPendingFlags();
87 165
88 // Create the overlay, and set the contents of the overlay window. 166 // Create the overlay, and set the contents of the overlay window.
89 overlay_.reset(new OverscrollNavigationOverlay(contents())); 167 overlay_.reset(new OverscrollNavigationOverlay(
90 aura_extra::ImageWindowDelegate* image_delegate = 168 contents(), root_window()));
91 new aura_extra::ImageWindowDelegate();
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 } 169 }
106 170
107 void TearDown() override { 171 void TearDown() override {
108 overlay_.reset(); 172 overlay_.reset();
109 RenderViewHostImplTestHarness::TearDown(); 173 RenderViewHostImplTestHarness::TearDown();
110 } 174 }
111 175
112 OverscrollNavigationOverlay* GetOverlay() { 176 OverscrollNavigationOverlay* GetOverlay() {
113 return overlay_.get(); 177 return overlay_.get();
114 } 178 }
115 179
116 private: 180 private:
117 scoped_ptr<OverscrollNavigationOverlay> overlay_; 181 scoped_ptr<OverscrollNavigationOverlay> overlay_;
118 182
119 DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlayTest); 183 DISALLOW_COPY_AND_ASSIGN(OverscrollNavigationOverlayTest);
120 }; 184 };
121 185
122 TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_NoImage) { 186 // Tests that if a screenshot is available, it is set in the overlay window
123 ReceivePaintUpdate(); 187 // delegate.
124 EXPECT_TRUE(GetOverlay()->received_paint_update_); 188 TEST_F(OverscrollNavigationOverlayTest, WithScreenshot) {
125 EXPECT_FALSE(GetOverlay()->loading_complete_); 189 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1));
126 // The paint update will hide the overlay. 190 PerformBackNavigationViaSliderCallbacks();
127 EXPECT_FALSE(GetOverlay()->web_contents()); 191 // Screenshot was set on NavEntry at offset -1.
192 EXPECT_TRUE(static_cast<aura_extra::ImageWindowDelegate*>(
193 GetOverlay()->window_->delegate())->has_image());
128 } 194 }
129 195
130 TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_WithImage) { 196 // Tests that if a screenshot is not available, no image is set in the overlay
131 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 197 // window delegate.
132 198 TEST_F(OverscrollNavigationOverlayTest, WithoutScreenshot) {
133 ReceivePaintUpdate(); 199 PerformBackNavigationViaSliderCallbacks();
134 EXPECT_TRUE(GetOverlay()->received_paint_update_); 200 // No screenshot was set on NavEntry at offset -1.
135 EXPECT_FALSE(GetOverlay()->loading_complete_); 201 EXPECT_FALSE(static_cast<aura_extra::ImageWindowDelegate*>(
136 // The paint update will hide the overlay. 202 GetOverlay()->window_->delegate())->has_image());
137 EXPECT_FALSE(GetOverlay()->web_contents());
138 } 203 }
139 204
140 TEST_F(OverscrollNavigationOverlayTest, LoadUpdateWithoutNonEmptyPaint) { 205 // Tests that if a navigation is attempted but there is nothing to navigate to,
141 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 206 // we return a null window.
142 process()->sink().ClearMessages(); 207 TEST_F(OverscrollNavigationOverlayTest, CannotNavigate) {
143 208 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 } 209 }
150 210
151 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_PaintUpdate) { 211 // Tests that if a navigation is aborted, no navigation is performed and the
152 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 212 // state is restored.
153 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1)); 213 TEST_F(OverscrollNavigationOverlayTest, AbortNavigation) {
214 scoped_ptr<aura::Window> window = GetOverlay()->CreateBackWindow();
215 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::BACK);
mfomitchev 2015/03/31 22:16:31 Might actually want to add this check to PerformBa
Nina 2015/04/01 21:49:05 Done.
154 216
217 GetOverlay()->OnOverscrollAborted();
218 EXPECT_FALSE(contents()->cross_navigation_pending());
219 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE);
220 }
221
222 // Performs two navigations. The second navigation is aborted, tests that the
223 // first one worked correctly.
224 TEST_F(OverscrollNavigationOverlayTest, AbortAfterSuccessfulNavigation) {
155 PerformBackNavigationViaSliderCallbacks(); 225 PerformBackNavigationViaSliderCallbacks();
156 // Screenshot was set on NavEntry at offset -1. 226 scoped_ptr<aura::Window> wrapper = GetOverlay()->CreateBackWindow();
157 EXPECT_TRUE(GetOverlay()->image_delegate_->has_image()); 227 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::BACK);
158 EXPECT_FALSE(GetOverlay()->received_paint_update_);
159 228
229 GetOverlay()->OnOverscrollAborted();
230 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE);
231
232 EXPECT_TRUE(contents()->cross_navigation_pending());
233 contents()->CommitPendingNavigation();
234 EXPECT_EQ(contents()->GetURL(), third_);
235 }
236
237 // Tests that an overscroll navigation that receives a paint update actually
238 // stops observing.
239 TEST_F(OverscrollNavigationOverlayTest, Navigation_PaintUpdate) {
240 PerformBackNavigationViaSliderCallbacks();
160 ReceivePaintUpdate(); 241 ReceivePaintUpdate();
242
161 // Paint updates until the navigation is committed typically represent updates 243 // Paint updates until the navigation is committed typically represent updates
162 // for the previous page, so they shouldn't affect the flag. 244 // for the previous page, so we should still be observing.
163 EXPECT_FALSE(GetOverlay()->received_paint_update_); 245 EXPECT_TRUE(GetOverlay()->web_contents());
164 246
165 contents()->CommitPendingNavigation(); 247 contents()->CommitPendingNavigation();
166 ReceivePaintUpdate(); 248 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 249
250 // Navigation was committed and the paint update was received - we should no
251 // longer be observing.
171 EXPECT_FALSE(GetOverlay()->web_contents()); 252 EXPECT_FALSE(GetOverlay()->web_contents());
253 EXPECT_EQ(contents()->GetURL(), third_);
172 } 254 }
173 255
174 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_LoadingUpdate) { 256 // Tests that an overscroll navigation that receives a loading update actually
175 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 257 // stops observing.
176 258 TEST_F(OverscrollNavigationOverlayTest, Navigation_LoadingUpdate) {
177 PerformBackNavigationViaSliderCallbacks(); 259 PerformBackNavigationViaSliderCallbacks();
178 // No screenshot was set on NavEntry at offset -1. 260 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 261 // DidStopLoading for any navigation should always reset the load flag and
184 // dismiss the overlay even if the pending navigation wasn't committed - 262 // 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 263 // 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 264 // (which can happen if a new navigation is performed while while a GestureNav
187 // navigation is in progress). 265 // navigation is in progress).
188 contents()->TestSetIsLoading(true); 266 contents()->TestSetIsLoading(true);
189 contents()->TestSetIsLoading(false); 267 contents()->TestSetIsLoading(false);
190 EXPECT_TRUE(GetOverlay()->loading_complete_); 268 EXPECT_FALSE(GetOverlay()->web_contents());
269 contents()->CommitPendingNavigation();
270 EXPECT_EQ(contents()->GetURL(), third_);
271 }
191 272
192 EXPECT_FALSE(GetOverlay()->web_contents()); 273 // Tests that swapping the overlay window at the end of a gesture caused by the
274 // start of a new overscroll does not crash and the events still reach the new
275 // overlay window.
276 TEST_F(OverscrollNavigationOverlayTest, OverlayWindowSwap) {
277 PerformBackNavigationViaSliderCallbacks();
278 aura::Window* first_overlay_window = GetOverlay()->window_.get();
279 EXPECT_TRUE(GetOverlay()->web_contents());
280 EXPECT_TRUE(first_overlay_window);
281
282 // At this stage, the overlay window is covering the web contents. Configure
283 // the animator of the overlay window for the test.
284 ui::ScopedAnimationDurationScaleMode normal_duration(
285 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
286 ui::LayerAnimator* animator = GetOverlay()->window_->layer()->GetAnimator();
287 animator->set_disable_timer_for_test(true);
288 ui::LayerAnimatorTestController test_controller(animator);
289
290 // Start and complete a back navigation via a gesture.
291 ui::test::EventGenerator generator(root_window());
292 generator.GestureScrollSequence(
293 gfx::Point(10, 10),
294 gfx::Point(400, 10),
295 base::TimeDelta::FromMilliseconds(10),
296 10);
297
298 ui::ScopedLayerAnimationSettings settings(animator);
299 base::TimeDelta duration = settings.GetTransitionDuration();
300 test_controller.StartThreadedAnimationsIfNeeded();
301 base::TimeTicks start_time = gfx::FrameTime::Now();
302
303 // The overlay window should now be being animated to the edge of the screen.
304 animator->Step(start_time + duration / 8);
305 EXPECT_EQ(GetOverlay()->window_.get(), first_overlay_window);
306
307 // The overlay window is halfway through, start another animation that will
308 // cancel the first one. The event that cancels the animation will go to
309 // the slide window, which will be turned into the new overlay window
mfomitchev 2015/03/31 22:16:31 which will be used as the overlay window when the
Nina 2015/04/01 21:49:05 Done.
310 // afterwards.
311 generator.GestureScrollSequence(
312 gfx::Point(550, 10),
mfomitchev 2015/03/31 22:16:31 Avoid using "magic numbers" like this in tests. Th
Nina 2015/04/01 21:49:05 Done.
313 gfx::Point(800, 10),
314 base::TimeDelta::FromMilliseconds(10),
315 10);
316 EXPECT_TRUE(GetOverlay()->window_.get());
317 // The overlay window should be a new window.
318 EXPECT_NE(GetOverlay()->window_.get(), first_overlay_window);
319
320 // Complete the animation.
321 GetOverlay()->window_->layer()->GetAnimator()->StopAnimating();
322 EXPECT_TRUE(GetOverlay()->window_.get());
323
324 // Load the page.
325 contents()->CommitPendingNavigation();
326 ReceivePaintUpdate();
327 EXPECT_FALSE(GetOverlay()->window_.get());
328 EXPECT_EQ(contents()->GetURL(), first_);
193 } 329 }
194 330
195 } // namespace content 331 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698