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

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: Brought back gesture cancellation by mouse and linted code. 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"
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(&params, 0, sizeof(params)); 92 memset(&params, 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(new OverscrollNavigationOverlay(
90 aura_extra::ImageWindowDelegate* image_delegate = 102 contents(), contents()->GetNativeView()));
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 } 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_); 123 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1));
126 // The paint update will hide the overlay. 124 PerformBackNavigationViaSliderCallbacks();
127 EXPECT_FALSE(GetOverlay()->web_contents()); 125 // Screenshot was set on NavEntry at offset -1.
126 EXPECT_TRUE(static_cast<aura_extra::ImageWindowDelegate*>(
127 GetOverlay()->window_->delegate())->has_image());
128 } 128 }
129 129
130 TEST_F(OverscrollNavigationOverlayTest, FirstVisuallyNonEmptyPaint_WithImage) { 130 // Tests that if a screenshot is not available, no image is set in the overlay
131 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 131 // window delegate.
132 132 TEST_F(OverscrollNavigationOverlayTest, WithoutScreenshot) {
133 ReceivePaintUpdate(); 133 PerformBackNavigationViaSliderCallbacks();
134 EXPECT_TRUE(GetOverlay()->received_paint_update_); 134 // No screenshot was set on NavEntry at offset -1.
135 EXPECT_FALSE(GetOverlay()->loading_complete_); 135 EXPECT_FALSE(static_cast<aura_extra::ImageWindowDelegate*>(
136 // The paint update will hide the overlay. 136 GetOverlay()->window_->delegate())->has_image());
137 EXPECT_FALSE(GetOverlay()->web_contents());
138 } 137 }
139 138
140 TEST_F(OverscrollNavigationOverlayTest, LoadUpdateWithoutNonEmptyPaint) { 139 // Tests that if a navigation is attempted but there is nothing to navigate to,
141 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 140 // we return a null window.
142 process()->sink().ClearMessages(); 141 TEST_F(OverscrollNavigationOverlayTest, CannotNavigate) {
143 142 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 } 143 }
150 144
151 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_PaintUpdate) { 145 // Tests that if a navigation is aborted, no navigation is performed and the
152 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 146 // state is restored.
153 SetDummyScreenshotOnNavEntry(controller().GetEntryAtOffset(-1)); 147 TEST_F(OverscrollNavigationOverlayTest, AbortNavigation) {
148 scoped_ptr<aura::Window> window = GetOverlay()->CreateBackWindow();
149 EXPECT_NE(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE);
mfomitchev 2015/03/26 15:36:00 Why don't we test that the direction is BACK inste
Nina 2015/03/27 17:52:35 Done.
154 150
151 GetOverlay()->OnOverscrollAborted();
152 // Make sure that if we started a navigation (we shouldn't), we commit it.
153 EXPECT_FALSE(contents()->cross_navigation_pending());
mfomitchev 2015/03/26 15:36:00 Also confirm direction is NONE?
Nina 2015/03/27 17:52:35 Done.
154 }
155
156 // Tests that if a second navigation is aborted before, the first one still
mfomitchev 2015/03/26 15:36:00 "a second navigation is aborted before" is not cle
Nina 2015/03/27 17:52:35 Done.
157 // happens.
158 TEST_F(OverscrollNavigationOverlayTest, AbortAfterSuccessfulNavigation) {
155 PerformBackNavigationViaSliderCallbacks(); 159 PerformBackNavigationViaSliderCallbacks();
156 // Screenshot was set on NavEntry at offset -1. 160 scoped_ptr<aura::Window> wrapper = GetOverlay()->CreateBackWindow();
157 EXPECT_TRUE(GetOverlay()->image_delegate_->has_image()); 161 EXPECT_NE(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE);
158 EXPECT_FALSE(GetOverlay()->received_paint_update_);
159 162
163 GetOverlay()->OnOverscrollAborted();
164 EXPECT_EQ(GetOverlay()->direction_, OverscrollNavigationOverlay::NONE);
165
166 EXPECT_TRUE(contents()->cross_navigation_pending());
167 contents()->CommitPendingNavigation();
168 EXPECT_EQ(contents()->GetURL(), second_);
169 }
170
171 // Tests that an overscroll navigation that receives a paint update actually
172 // stops observing.
173 TEST_F(OverscrollNavigationOverlayTest, Navigation_PaintUpdate) {
174 PerformBackNavigationViaSliderCallbacks();
160 ReceivePaintUpdate(); 175 ReceivePaintUpdate();
176
161 // Paint updates until the navigation is committed typically represent updates 177 // Paint updates until the navigation is committed typically represent updates
162 // for the previous page, so they shouldn't affect the flag. 178 // for the previous page, so we should still be observing.
163 EXPECT_FALSE(GetOverlay()->received_paint_update_); 179 EXPECT_TRUE(GetOverlay()->web_contents());
164 180
165 contents()->CommitPendingNavigation(); 181 contents()->CommitPendingNavigation();
166 ReceivePaintUpdate(); 182 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 183
184 // Navigation was committed and the paint update was received - we should no
185 // longer be observing.
171 EXPECT_FALSE(GetOverlay()->web_contents()); 186 EXPECT_FALSE(GetOverlay()->web_contents());
187 EXPECT_EQ(contents()->GetURL(), second_);
172 } 188 }
173 189
174 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_LoadingUpdate) { 190 // Tests that an overscroll navigation that receives a loading update actually
175 GetOverlay()->image_delegate_->SetImage(CreateDummyScreenshot()); 191 // stops observing.
176 192 TEST_F(OverscrollNavigationOverlayTest, Navigation_LoadingUpdate) {
177 PerformBackNavigationViaSliderCallbacks(); 193 PerformBackNavigationViaSliderCallbacks();
178 // No screenshot was set on NavEntry at offset -1. 194 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 195 // DidStopLoading for any navigation should always reset the load flag and
184 // dismiss the overlay even if the pending navigation wasn't committed - 196 // 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 197 // this is a "safety net" in case we mis-identify the destination webpage
198 // (which can happen if a new navigation is performed while while a GestureNav
199 // navigation is in progress).
200 contents()->TestSetIsLoading(true);
201 contents()->TestSetIsLoading(false);
202 EXPECT_FALSE(GetOverlay()->web_contents());
203 contents()->CommitPendingNavigation();
204 EXPECT_EQ(contents()->GetURL(), second_);
205 }
206
207 // Tests that an overscroll gesture followed after another one before the first
208 // one finishes loading works with a paint update.
209 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_PaintUpdate) {
210 PerformBackNavigationViaSliderCallbacks();
mfomitchev 2015/03/26 15:36:00 Do you think we still need the multinavigation tes
Nina 2015/03/27 17:52:35 Yeah... it does not make that much sense to have t
211 EXPECT_TRUE(GetOverlay()->web_contents());
212 PerformBackNavigationViaSliderCallbacks();
213 EXPECT_TRUE(GetOverlay()->web_contents());
214 ReceivePaintUpdate();
215
216 // Paint updates until the navigation is committed typically represent updates
217 // for the previous page, so we should still be observing.
218 EXPECT_TRUE(GetOverlay()->web_contents());
219
220 contents()->CommitPendingNavigation();
221 ReceivePaintUpdate();
222
223 // Navigation was committed and the paint update was received - we should no
224 // longer be observing.
225 EXPECT_FALSE(GetOverlay()->web_contents());
226 EXPECT_EQ(contents()->GetURL(), first_);
227 }
228
229 // Tests that an overscroll gesture followed after another one before the first
230 // one finishes loading works with a loading update.
231 TEST_F(OverscrollNavigationOverlayTest, MultiNavigation_LoadingUpdate) {
232 PerformBackNavigationViaSliderCallbacks();
233 EXPECT_TRUE(GetOverlay()->web_contents());
234 PerformBackNavigationViaSliderCallbacks();
235 EXPECT_TRUE(GetOverlay()->web_contents());
236 // DidStopLoading for any navigation should always reset the load flag and
237 // dismiss the overlay even if the pending navigation wasn't committed -
238 // 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 239 // (which can happen if a new navigation is performed while while a GestureNav
187 // navigation is in progress). 240 // navigation is in progress).
188 contents()->TestSetIsLoading(true); 241 contents()->TestSetIsLoading(true);
189 contents()->TestSetIsLoading(false); 242 contents()->TestSetIsLoading(false);
190 EXPECT_TRUE(GetOverlay()->loading_complete_);
191
192 EXPECT_FALSE(GetOverlay()->web_contents()); 243 EXPECT_FALSE(GetOverlay()->web_contents());
244 contents()->CommitPendingNavigation();
245 EXPECT_EQ(contents()->GetURL(), first_);
193 } 246 }
194 247
195 } // namespace content 248 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698