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

Side by Side Diff: ui/views/controls/webview/webview_unittest.cc

Issue 662013003: Add unit tests for ui::WebView embedded fullscreen mode. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE Created 6 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ui/views/controls/webview/webview.h" 5 #include "ui/views/controls/webview/webview.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "content/browser/web_contents/web_contents_impl.h" 8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/browser/web_contents_observer.h" 9 #include "content/public/browser/web_contents_observer.h"
10 #include "content/public/test/test_browser_context.h" 10 #include "content/public/test/test_browser_context.h"
11 #include "content/public/test/test_browser_thread.h" 11 #include "content/public/test/test_browser_thread.h"
12 #include "content/public/test/web_contents_tester.h" 12 #include "content/public/test/web_contents_tester.h"
13 #include "content/test/test_content_browser_client.h" 13 #include "content/test/test_content_browser_client.h"
14 #include "ui/aura/window.h" 14 #include "ui/aura/window.h"
15 #include "ui/views/controls/native/native_view_host.h"
15 #include "ui/views/test/test_views_delegate.h" 16 #include "ui/views/test/test_views_delegate.h"
16 #include "ui/views/test/widget_test.h" 17 #include "ui/views/test/widget_test.h"
17 18
18 namespace { 19 namespace {
19 20
20 // Provides functionality to create a test WebContents. 21 // Provides functionality to create a test WebContents.
21 class WebViewTestViewsDelegate : public views::TestViewsDelegate { 22 class WebViewTestViewsDelegate : public views::TestViewsDelegate {
22 public: 23 public:
23 WebViewTestViewsDelegate() {} 24 WebViewTestViewsDelegate() {}
24 ~WebViewTestViewsDelegate() override {} 25 ~WebViewTestViewsDelegate() override {}
(...skipping 10 matching lines...) Expand all
35 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate); 36 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate);
36 }; 37 };
37 38
38 // Provides functionality to test a WebView. 39 // Provides functionality to test a WebView.
39 class WebViewUnitTest : public views::test::WidgetTest { 40 class WebViewUnitTest : public views::test::WidgetTest {
40 public: 41 public:
41 WebViewUnitTest() 42 WebViewUnitTest()
42 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), 43 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()),
43 file_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING, 44 file_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING,
44 base::MessageLoop::current()), 45 base::MessageLoop::current()),
45 io_thread_(content::BrowserThread::IO, base::MessageLoop::current()) {} 46 io_thread_(content::BrowserThread::IO, base::MessageLoop::current()),
47 top_level_widget_(nullptr) {}
46 48
47 ~WebViewUnitTest() override {} 49 ~WebViewUnitTest() override {}
48 50
49 void SetUp() override { 51 void SetUp() override {
50 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down. 52 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down.
51 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate); 53 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate);
52 browser_context_.reset(new content::TestBrowserContext); 54 browser_context_.reset(new content::TestBrowserContext);
53 WidgetTest::SetUp(); 55 WidgetTest::SetUp();
54 // Set the test content browser client to avoid pulling in needless 56 // Set the test content browser client to avoid pulling in needless
55 // dependencies from content. 57 // dependencies from content.
56 SetBrowserClientForTesting(&test_browser_client_); 58 SetBrowserClientForTesting(&test_browser_client_);
59
60 // Create a top level widget and add a child, and give it a WebView as a
61 // child.
62 top_level_widget_ = CreateTopLevelFramelessPlatformWidget();
63 top_level_widget_->SetBounds(gfx::Rect(0, 10, 100, 100));
64 views::View* const contents_view = new views::View();
65 top_level_widget_->SetContentsView(contents_view);
66 web_view_ = new views::WebView(browser_context_.get());
67 web_view_->SetBoundsRect(gfx::Rect(contents_view->size()));
68 contents_view->AddChildView(web_view_);
69 top_level_widget_->Show();
70 ASSERT_EQ(gfx::Rect(0, 0, 100, 100), web_view_->bounds());
57 } 71 }
58 72
59 void TearDown() override { 73 void TearDown() override {
74 top_level_widget_->Close(); // Deletes all children and itself.
75 RunPendingMessages();
76
60 browser_context_.reset(NULL); 77 browser_context_.reset(NULL);
61 // Flush the message loop to execute pending relase tasks as this would 78 // Flush the message loop to execute pending relase tasks as this would
62 // upset ASAN and Valgrind. 79 // upset ASAN and Valgrind.
63 RunPendingMessages(); 80 RunPendingMessages();
64 WidgetTest::TearDown(); 81 WidgetTest::TearDown();
65 } 82 }
66 83
67 protected: 84 protected:
68 content::BrowserContext* browser_context() { return browser_context_.get(); } 85 views::Widget* top_level_widget() const { return top_level_widget_; }
86 views::WebView* web_view() const { return web_view_; }
87
88 scoped_ptr<content::WebContents> CreateWebContents() const {
89 return make_scoped_ptr(content::WebContents::Create(
90 content::WebContents::CreateParams(browser_context_.get())));
91 }
69 92
70 private: 93 private:
71 content::TestBrowserThread ui_thread_; 94 content::TestBrowserThread ui_thread_;
72 content::TestBrowserThread file_blocking_thread_; 95 content::TestBrowserThread file_blocking_thread_;
73 content::TestBrowserThread io_thread_; 96 content::TestBrowserThread io_thread_;
74 scoped_ptr<content::TestBrowserContext> browser_context_; 97 scoped_ptr<content::TestBrowserContext> browser_context_;
75 scoped_ptr<WebViewTestViewsDelegate> views_delegate_; 98 scoped_ptr<WebViewTestViewsDelegate> views_delegate_;
76 content::TestContentBrowserClient test_browser_client_; 99 content::TestContentBrowserClient test_browser_client_;
77 100
101 views::Widget* top_level_widget_;
102 views::WebView* web_view_;
103
78 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest); 104 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest);
79 }; 105 };
80 106
81 // Provides functionaity to observe events on a WebContents like WasShown/ 107 // Provides functionaity to observe events on a WebContents like WasShown/
82 // WasHidden/WebContentsDestroyed. 108 // WasHidden/WebContentsDestroyed.
83 class WebViewTestWebContentsObserver : public content::WebContentsObserver { 109 class WebViewTestWebContentsObserver : public content::WebContentsObserver {
84 public: 110 public:
85 WebViewTestWebContentsObserver(content::WebContents* web_contents) 111 WebViewTestWebContentsObserver(content::WebContents* web_contents)
86 : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)), 112 : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)),
87 was_shown_(false), 113 was_shown_(false),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 content::WebContentsImpl* web_contents_; 151 content::WebContentsImpl* web_contents_;
126 bool was_shown_; 152 bool was_shown_;
127 int32 shown_count_; 153 int32 shown_count_;
128 int32 hidden_count_; 154 int32 hidden_count_;
129 // Set to true if the view containing the webcontents has a valid root window. 155 // Set to true if the view containing the webcontents has a valid root window.
130 bool valid_root_while_shown_; 156 bool valid_root_while_shown_;
131 157
132 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver); 158 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver);
133 }; 159 };
134 160
161 // Fakes the fullscreen browser state reported to WebContents and WebView.
162 class WebViewTestWebContentsDelegate : public content::WebContentsDelegate {
163 public:
164 WebViewTestWebContentsDelegate() : is_fullscreened_(false) {}
165 ~WebViewTestWebContentsDelegate() override {}
166
167 void set_is_fullscreened(bool fs) { is_fullscreened_ = fs; }
168
169 // content::WebContentsDelegate overrides.
170 bool IsFullscreenForTabOrPending(
171 const content::WebContents* ignored) const override {
172 return is_fullscreened_;
173 }
174
175 private:
176 bool is_fullscreened_;
177
178 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsDelegate);
179 };
180
135 // Tests that attaching and detaching a WebContents to a WebView makes the 181 // Tests that attaching and detaching a WebContents to a WebView makes the
136 // WebContents visible and hidden respectively. 182 // WebContents visible and hidden respectively.
137 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { 183 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) {
138 // Create a top level widget and a webview as its content.
139 views::Widget* parent = CreateTopLevelFramelessPlatformWidget();
140 parent->SetBounds(gfx::Rect(0, 10, 100, 100));
141
142 views::Widget* widget = CreateChildNativeWidgetWithParent(parent);
143 widget->SetBounds(gfx::Rect(0, 10, 100, 100));
144 views::WebView* webview = new views::WebView(browser_context());
145 widget->SetContentsView(webview);
146 parent->Show();
147 widget->Show();
148
149 // Case 1: Create a new WebContents and set it in the webview via 184 // Case 1: Create a new WebContents and set it in the webview via
150 // SetWebContents. This should make the WebContents visible. 185 // SetWebContents. This should make the WebContents visible.
151 content::WebContents::CreateParams params(browser_context()); 186 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
152 scoped_ptr<content::WebContents> web_contents1(
153 content::WebContents::Create(params));
154 WebViewTestWebContentsObserver observer1(web_contents1.get()); 187 WebViewTestWebContentsObserver observer1(web_contents1.get());
155 EXPECT_FALSE(observer1.was_shown()); 188 EXPECT_FALSE(observer1.was_shown());
156 189
157 webview->SetWebContents(web_contents1.get()); 190 web_view()->SetWebContents(web_contents1.get());
158 EXPECT_TRUE(observer1.was_shown()); 191 EXPECT_TRUE(observer1.was_shown());
159 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); 192 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible());
160 EXPECT_EQ(observer1.shown_count(), 1); 193 EXPECT_EQ(observer1.shown_count(), 1);
161 EXPECT_EQ(observer1.hidden_count(), 0); 194 EXPECT_EQ(observer1.hidden_count(), 0);
162 EXPECT_TRUE(observer1.valid_root_while_shown()); 195 EXPECT_TRUE(observer1.valid_root_while_shown());
163 196
164 // Case 2: Create another WebContents and replace the current WebContents 197 // Case 2: Create another WebContents and replace the current WebContents
165 // via SetWebContents(). This should hide the current WebContents and show 198 // via SetWebContents(). This should hide the current WebContents and show
166 // the new one. 199 // the new one.
167 content::WebContents::CreateParams params2(browser_context()); 200 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
168 scoped_ptr<content::WebContents> web_contents2(
169 content::WebContents::Create(params2));
170
171 WebViewTestWebContentsObserver observer2(web_contents2.get()); 201 WebViewTestWebContentsObserver observer2(web_contents2.get());
172 EXPECT_FALSE(observer2.was_shown()); 202 EXPECT_FALSE(observer2.was_shown());
173 203
174 // Setting the new WebContents should hide the existing one. 204 // Setting the new WebContents should hide the existing one.
175 webview->SetWebContents(web_contents2.get()); 205 web_view()->SetWebContents(web_contents2.get());
176 EXPECT_FALSE(observer1.was_shown()); 206 EXPECT_FALSE(observer1.was_shown());
177 EXPECT_TRUE(observer2.was_shown()); 207 EXPECT_TRUE(observer2.was_shown());
178 EXPECT_TRUE(observer2.valid_root_while_shown()); 208 EXPECT_TRUE(observer2.valid_root_while_shown());
179 209
180 // WebContents1 should not get stray show calls when WebContents2 is set. 210 // WebContents1 should not get stray show calls when WebContents2 is set.
181 EXPECT_EQ(observer1.shown_count(), 1); 211 EXPECT_EQ(observer1.shown_count(), 1);
182 EXPECT_EQ(observer1.hidden_count(), 1); 212 EXPECT_EQ(observer1.hidden_count(), 1);
183 EXPECT_EQ(observer2.shown_count(), 1); 213 EXPECT_EQ(observer2.shown_count(), 1);
184 EXPECT_EQ(observer2.hidden_count(), 0); 214 EXPECT_EQ(observer2.hidden_count(), 0);
185 215
186 // Case 3: Test that attaching to a hidden webview does not show the web 216 // Case 3: Test that attaching to a hidden webview does not show the web
187 // contents. 217 // contents.
188 webview->SetVisible(false); 218 web_view()->SetVisible(false);
189 EXPECT_EQ(1, observer2.hidden_count()); // Now hidden. 219 EXPECT_EQ(1, observer2.hidden_count()); // Now hidden.
190 220
191 EXPECT_EQ(1, observer1.shown_count()); 221 EXPECT_EQ(1, observer1.shown_count());
192 webview->SetWebContents(web_contents1.get()); 222 web_view()->SetWebContents(web_contents1.get());
193 EXPECT_EQ(1, observer1.shown_count()); 223 EXPECT_EQ(1, observer1.shown_count());
194 224
195 // Nothing else should change. 225 // Nothing else should change.
196 EXPECT_EQ(1, observer1.hidden_count()); 226 EXPECT_EQ(1, observer1.hidden_count());
197 EXPECT_EQ(1, observer2.shown_count()); 227 EXPECT_EQ(1, observer2.shown_count());
198 EXPECT_EQ(1, observer2.hidden_count()); 228 EXPECT_EQ(1, observer2.hidden_count());
199 229
200 // Case 4: Test that making the webview visible when a window has an invisible 230 // Case 4: Test that making the webview visible when a window has an invisible
201 // parent does not make the web contents visible. 231 // parent does not make the web contents visible.
202 parent->Hide(); 232 top_level_widget()->Hide();
203 webview->SetVisible(true); 233 web_view()->SetVisible(true);
204 // TODO(tapted): The following line is wrong, the shown_count() should still 234 // TODO(tapted): The following line is wrong, the shown_count() should still
205 // be 1, until the parent window is made visible on the line after. 235 // be 1, until the parent window is made visible on the line after.
206 EXPECT_EQ(2, observer1.shown_count()); 236 EXPECT_EQ(2, observer1.shown_count());
207 parent->Show(); 237 top_level_widget()->Show();
208 EXPECT_EQ(2, observer1.shown_count()); 238 EXPECT_EQ(2, observer1.shown_count());
209 parent->Hide(); 239 top_level_widget()->Hide();
210 EXPECT_EQ(2, observer1.hidden_count()); 240 EXPECT_EQ(2, observer1.hidden_count());
211 241
212 // Case 5: Test that moving from a hidden parent to a visible parent makes the 242 // Case 5: Test that moving from a hidden parent to a visible parent makes the
213 // web contents visible. 243 // web contents visible.
214 views::Widget* parent2 = CreateTopLevelFramelessPlatformWidget(); 244 views::Widget* parent2 = CreateTopLevelFramelessPlatformWidget();
215 parent2->SetBounds(gfx::Rect(0, 10, 100, 100)); 245 parent2->SetBounds(gfx::Rect(0, 10, 100, 100));
216 parent2->Show(); 246 parent2->Show();
217
218 EXPECT_EQ(2, observer1.shown_count()); 247 EXPECT_EQ(2, observer1.shown_count());
219 // Note: that reparenting the windows directly, after the windows have been 248 // Note: that reparenting the windows directly, after the windows have been
220 // created, e.g., views::Widget::ReparentNativeView(widget, parent2), is not a 249 // created, e.g., views::Widget::ReparentNativeView(widget, parent2), is not a
221 // supported use case. Instead, move the WebView over. 250 // supported use case. Instead, move the WebView over.
222 parent2->SetContentsView(webview); 251 parent2->SetContentsView(web_view());
223 EXPECT_EQ(3, observer1.shown_count()); 252 EXPECT_EQ(3, observer1.shown_count());
253 parent2->Close();
254 }
224 255
225 widget->Close(); 256 // Tests that the layout of the NativeViewHost within WebView behaves as
226 parent->Close(); 257 // expected when embedding a fullscreen widget during WebContents screen
227 parent2->Close(); 258 // capture.
228 RunPendingMessages(); 259 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Layout) {
260 web_view()->SetEmbedFullscreenWidgetMode(true);
261 ASSERT_EQ(1, web_view()->child_count());
262 views::NativeViewHost* const holder =
263 static_cast<views::NativeViewHost*>(web_view()->child_at(0));
sky 2014/10/29 19:25:37 Code like this is fragile, in so far as it's easy
miu 2014/10/29 21:23:24 Done. I had to move the WebViewUnitTest class out
264
265 const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
266 WebViewTestWebContentsDelegate delegate;
267 web_contents->SetDelegate(&delegate);
268 web_view()->SetWebContents(web_contents.get());
269
270 // Initially, the |holder| should fill the entire WebView.
271 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder->bounds());
272
273 // Simulate a transition into fullscreen mode, but without screen capture
274 // active on the WebContents, the |holder| should still fill the entire
275 // WebView like before.
276 delegate.set_is_fullscreened(true);
277 static_cast<content::WebContentsObserver*>(web_view())->
278 DidToggleFullscreenModeForTab(true);
279 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder->bounds());
280
281 // ...and transition back out of fullscreen mode.
282 delegate.set_is_fullscreened(false);
283 static_cast<content::WebContentsObserver*>(web_view())->
284 DidToggleFullscreenModeForTab(false);
285 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder->bounds());
286
287 // Now, begin screen capture of the WebContents and then enter fullscreen
288 // mode. This time, the |holder| should be centered within WebView and
289 // sized to match the capture size.
290 const gfx::Size capture_size(64, 48);
291 web_contents->IncrementCapturerCount(capture_size);
292 delegate.set_is_fullscreened(true);
293 static_cast<content::WebContentsObserver*>(web_view())->
294 DidToggleFullscreenModeForTab(true);
295 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder->bounds());
296
297 // Resize the WebView so that its width is smaller than the capture width.
298 // Expect the |holder| to be scaled-down, letterboxed style.
299 web_view()->SetBoundsRect(gfx::Rect(0, 0, 32, 32));
300 EXPECT_EQ(gfx::Rect(0, 4, 32, 24), holder->bounds());
301
302 // Transition back out of fullscreen mode a final time and confirm the bounds
303 // of the |holder| fill the entire WebView once again.
304 delegate.set_is_fullscreened(false);
305 static_cast<content::WebContentsObserver*>(web_view())->
306 DidToggleFullscreenModeForTab(false);
307 EXPECT_EQ(gfx::Rect(0, 0, 32, 32), holder->bounds());
308 }
309
310 // Tests that a WebView correctly switches between WebContentses when one of
311 // them is embedding a fullscreen widget during WebContents screen capture.
312 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Switching) {
313 web_view()->SetEmbedFullscreenWidgetMode(true);
314 ASSERT_EQ(1, web_view()->child_count());
315 views::NativeViewHost* const holder =
316 static_cast<views::NativeViewHost*>(web_view()->child_at(0));
317 const gfx::NativeView unset_native_view = holder->native_view();
318
319 // Create two WebContentses to switch between.
320 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
321 WebViewTestWebContentsDelegate delegate1;
322 web_contents1->SetDelegate(&delegate1);
323 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
324 WebViewTestWebContentsDelegate delegate2;
325 web_contents2->SetDelegate(&delegate2);
326
327 EXPECT_NE(web_contents1->GetNativeView(), holder->native_view());
328 web_view()->SetWebContents(web_contents1.get());
329 EXPECT_EQ(web_contents1->GetNativeView(), holder->native_view());
330 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder->bounds());
331
332 // Begin screen capture of the WebContents and then enter fullscreen mode.
333 // The native view should not have changed, but the layout of its |holder|
334 // will have (indicates WebView has responded).
335 const gfx::Size capture_size(64, 48);
336 web_contents1->IncrementCapturerCount(capture_size);
337 delegate1.set_is_fullscreened(true);
338 static_cast<content::WebContentsObserver*>(web_view())->
339 DidToggleFullscreenModeForTab(true);
340 EXPECT_EQ(web_contents1->GetNativeView(), holder->native_view());
341 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder->bounds());
342
343 // When setting the WebContents to nullptr, the native view should become
344 // unset.
345 web_view()->SetWebContents(nullptr);
346 EXPECT_EQ(unset_native_view, holder->native_view());
347
348 // ...and when setting the WebContents back to the currently-fullscreened
349 // instance, expect the native view and layout to reflect that.
350 web_view()->SetWebContents(web_contents1.get());
351 EXPECT_EQ(web_contents1->GetNativeView(), holder->native_view());
352 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder->bounds());
353
354 // Now, switch to a different, non-null WebContents instance and check that
355 // the native view has changed and the |holder| is filling WebView again.
356 web_view()->SetWebContents(web_contents2.get());
357 EXPECT_EQ(web_contents2->GetNativeView(), holder->native_view());
358 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder->bounds());
359
360 // Finally, switch back to the first WebContents (still fullscreened).
361 web_view()->SetWebContents(web_contents1.get());
362 EXPECT_EQ(web_contents1->GetNativeView(), holder->native_view());
363 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder->bounds());
229 } 364 }
230 365
231 } // namespace 366 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698