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
3 // found in the LICENSE file.
5 #include "ui/views/controls/webview/webview.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/public/browser/web_contents_observer.h"
10 #include "content/public/test/test_browser_context.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "content/public/test/web_contents_tester.h"
13 #include "content/test/test_content_browser_client.h"
14 #include "ui/aura/window.h"
15 #include "ui/views/controls/native/native_view_host.h"
16 #include "ui/views/test/test_views_delegate.h"
17 #include "ui/views/test/widget_test.h"
23 // Provides functionality to create a test WebContents.
24 class WebViewTestViewsDelegate : public views::TestViewsDelegate {
26 WebViewTestViewsDelegate() {}
27 ~WebViewTestViewsDelegate() override {}
29 // Overriden from TestViewsDelegate.
30 content::WebContents* CreateWebContents(
31 content::BrowserContext* browser_context,
32 content::SiteInstance* site_instance) override {
33 return content::WebContentsTester::CreateTestWebContents(browser_context,
38 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate);
41 // Provides functionaity to observe events on a WebContents like WasShown/
42 // WasHidden/WebContentsDestroyed.
43 class WebViewTestWebContentsObserver : public content::WebContentsObserver {
45 WebViewTestWebContentsObserver(content::WebContents* web_contents)
46 : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)),
50 content::WebContentsObserver::Observe(web_contents);
53 ~WebViewTestWebContentsObserver() override {
55 content::WebContentsObserver::Observe(NULL);
58 void WebContentsDestroyed() override {
59 DCHECK(web_contents_);
60 content::WebContentsObserver::Observe(NULL);
64 void WasShown() override {
65 valid_root_while_shown_ =
66 web_contents()->GetNativeView()->GetRootWindow() != NULL;
71 void WasHidden() override {
76 bool was_shown() const { return was_shown_; }
78 int shown_count() const { return shown_count_; }
80 int hidden_count() const { return hidden_count_; }
82 bool valid_root_while_shown() const { return valid_root_while_shown_; }
85 content::WebContentsImpl* web_contents_;
89 // Set to true if the view containing the webcontents has a valid root window.
90 bool valid_root_while_shown_;
92 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver);
95 // Fakes the fullscreen browser state reported to WebContents and WebView.
96 class WebViewTestWebContentsDelegate : public content::WebContentsDelegate {
98 WebViewTestWebContentsDelegate() : is_fullscreened_(false) {}
99 ~WebViewTestWebContentsDelegate() override {}
101 void set_is_fullscreened(bool fs) { is_fullscreened_ = fs; }
103 // content::WebContentsDelegate overrides.
104 bool IsFullscreenForTabOrPending(
105 const content::WebContents* ignored) const override {
106 return is_fullscreened_;
110 bool is_fullscreened_;
112 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsDelegate);
117 // Provides functionality to test a WebView.
118 class WebViewUnitTest : public views::test::WidgetTest {
121 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()),
122 file_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING,
123 base::MessageLoop::current()),
124 io_thread_(content::BrowserThread::IO, base::MessageLoop::current()),
125 top_level_widget_(nullptr) {}
127 ~WebViewUnitTest() override {}
129 void SetUp() override {
130 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down.
131 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate);
132 browser_context_.reset(new content::TestBrowserContext);
134 // Set the test content browser client to avoid pulling in needless
135 // dependencies from content.
136 SetBrowserClientForTesting(&test_browser_client_);
138 // Create a top level widget and add a child, and give it a WebView as a
140 top_level_widget_ = CreateTopLevelFramelessPlatformWidget();
141 top_level_widget_->SetBounds(gfx::Rect(0, 10, 100, 100));
142 View* const contents_view = new View();
143 top_level_widget_->SetContentsView(contents_view);
144 web_view_ = new WebView(browser_context_.get());
145 web_view_->SetBoundsRect(gfx::Rect(contents_view->size()));
146 contents_view->AddChildView(web_view_);
147 top_level_widget_->Show();
148 ASSERT_EQ(gfx::Rect(0, 0, 100, 100), web_view_->bounds());
151 void TearDown() override {
152 top_level_widget_->Close(); // Deletes all children and itself.
153 RunPendingMessages();
155 browser_context_.reset(NULL);
156 // Flush the message loop to execute pending relase tasks as this would
157 // upset ASAN and Valgrind.
158 RunPendingMessages();
159 WidgetTest::TearDown();
163 Widget* top_level_widget() const { return top_level_widget_; }
164 WebView* web_view() const { return web_view_; }
165 NativeViewHost* holder() const { return web_view_->holder_; }
167 scoped_ptr<content::WebContents> CreateWebContents() const {
168 return make_scoped_ptr(content::WebContents::Create(
169 content::WebContents::CreateParams(browser_context_.get())));
173 content::TestBrowserThread ui_thread_;
174 content::TestBrowserThread file_blocking_thread_;
175 content::TestBrowserThread io_thread_;
176 scoped_ptr<content::TestBrowserContext> browser_context_;
177 scoped_ptr<WebViewTestViewsDelegate> views_delegate_;
178 content::TestContentBrowserClient test_browser_client_;
180 Widget* top_level_widget_;
183 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest);
186 // Tests that attaching and detaching a WebContents to a WebView makes the
187 // WebContents visible and hidden respectively.
188 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) {
189 // Case 1: Create a new WebContents and set it in the webview via
190 // SetWebContents. This should make the WebContents visible.
191 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
192 WebViewTestWebContentsObserver observer1(web_contents1.get());
193 EXPECT_FALSE(observer1.was_shown());
195 web_view()->SetWebContents(web_contents1.get());
196 EXPECT_TRUE(observer1.was_shown());
197 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible());
198 EXPECT_EQ(observer1.shown_count(), 1);
199 EXPECT_EQ(observer1.hidden_count(), 0);
200 EXPECT_TRUE(observer1.valid_root_while_shown());
202 // Case 2: Create another WebContents and replace the current WebContents
203 // via SetWebContents(). This should hide the current WebContents and show
205 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
206 WebViewTestWebContentsObserver observer2(web_contents2.get());
207 EXPECT_FALSE(observer2.was_shown());
209 // Setting the new WebContents should hide the existing one.
210 web_view()->SetWebContents(web_contents2.get());
211 EXPECT_FALSE(observer1.was_shown());
212 EXPECT_TRUE(observer2.was_shown());
213 EXPECT_TRUE(observer2.valid_root_while_shown());
215 // WebContents1 should not get stray show calls when WebContents2 is set.
216 EXPECT_EQ(observer1.shown_count(), 1);
217 EXPECT_EQ(observer1.hidden_count(), 1);
218 EXPECT_EQ(observer2.shown_count(), 1);
219 EXPECT_EQ(observer2.hidden_count(), 0);
221 // Case 3: Test that attaching to a hidden webview does not show the web
223 web_view()->SetVisible(false);
224 EXPECT_EQ(1, observer2.hidden_count()); // Now hidden.
226 EXPECT_EQ(1, observer1.shown_count());
227 web_view()->SetWebContents(web_contents1.get());
228 EXPECT_EQ(1, observer1.shown_count());
230 // Nothing else should change.
231 EXPECT_EQ(1, observer1.hidden_count());
232 EXPECT_EQ(1, observer2.shown_count());
233 EXPECT_EQ(1, observer2.hidden_count());
235 // Case 4: Test that making the webview visible when a window has an invisible
236 // parent does not make the web contents visible.
237 top_level_widget()->Hide();
238 web_view()->SetVisible(true);
239 // TODO(tapted): The following line is wrong, the shown_count() should still
240 // be 1, until the parent window is made visible on the line after.
241 EXPECT_EQ(2, observer1.shown_count());
242 top_level_widget()->Show();
243 EXPECT_EQ(2, observer1.shown_count());
244 top_level_widget()->Hide();
245 EXPECT_EQ(2, observer1.hidden_count());
247 // Case 5: Test that moving from a hidden parent to a visible parent makes the
248 // web contents visible.
249 Widget* parent2 = CreateTopLevelFramelessPlatformWidget();
250 parent2->SetBounds(gfx::Rect(0, 10, 100, 100));
252 EXPECT_EQ(2, observer1.shown_count());
253 // Note: that reparenting the windows directly, after the windows have been
254 // created, e.g., Widget::ReparentNativeView(widget, parent2), is not a
255 // supported use case. Instead, move the WebView over.
256 parent2->SetContentsView(web_view());
257 EXPECT_EQ(3, observer1.shown_count());
261 // Tests that the layout of the NativeViewHost within WebView behaves as
262 // expected when embedding a fullscreen widget during WebContents screen
264 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Layout) {
265 web_view()->SetEmbedFullscreenWidgetMode(true);
266 ASSERT_EQ(1, web_view()->child_count());
268 const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
269 WebViewTestWebContentsDelegate delegate;
270 web_contents->SetDelegate(&delegate);
271 web_view()->SetWebContents(web_contents.get());
273 // Initially, the holder should fill the entire WebView.
274 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
276 // Simulate a transition into fullscreen mode, but without screen capture
277 // active on the WebContents, the holder should still fill the entire
278 // WebView like before.
279 delegate.set_is_fullscreened(true);
280 static_cast<content::WebContentsObserver*>(web_view())->
281 DidToggleFullscreenModeForTab(true);
282 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
284 // ...and transition back out of fullscreen mode.
285 delegate.set_is_fullscreened(false);
286 static_cast<content::WebContentsObserver*>(web_view())->
287 DidToggleFullscreenModeForTab(false);
288 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
290 // Now, begin screen capture of the WebContents and then enter fullscreen
291 // mode. This time, the holder should be centered within WebView and
292 // sized to match the capture size.
293 const gfx::Size capture_size(64, 48);
294 web_contents->IncrementCapturerCount(capture_size);
295 delegate.set_is_fullscreened(true);
296 static_cast<content::WebContentsObserver*>(web_view())->
297 DidToggleFullscreenModeForTab(true);
298 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
300 // Resize the WebView so that its width is smaller than the capture width.
301 // Expect the holder to be scaled-down, letterboxed style.
302 web_view()->SetBoundsRect(gfx::Rect(0, 0, 32, 32));
303 EXPECT_EQ(gfx::Rect(0, 4, 32, 24), holder()->bounds());
305 // Transition back out of fullscreen mode a final time and confirm the bounds
306 // of the holder fill the entire WebView once again.
307 delegate.set_is_fullscreened(false);
308 static_cast<content::WebContentsObserver*>(web_view())->
309 DidToggleFullscreenModeForTab(false);
310 EXPECT_EQ(gfx::Rect(0, 0, 32, 32), holder()->bounds());
313 // Tests that a WebView correctly switches between WebContentses when one of
314 // them is embedding a fullscreen widget during WebContents screen capture.
315 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Switching) {
316 web_view()->SetEmbedFullscreenWidgetMode(true);
317 ASSERT_EQ(1, web_view()->child_count());
318 const gfx::NativeView unset_native_view = holder()->native_view();
320 // Create two WebContentses to switch between.
321 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
322 WebViewTestWebContentsDelegate delegate1;
323 web_contents1->SetDelegate(&delegate1);
324 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
325 WebViewTestWebContentsDelegate delegate2;
326 web_contents2->SetDelegate(&delegate2);
328 EXPECT_NE(web_contents1->GetNativeView(), holder()->native_view());
329 web_view()->SetWebContents(web_contents1.get());
330 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
331 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
333 // Begin screen capture of the WebContents and then enter fullscreen mode.
334 // The native view should not have changed, but the layout of its holder will
335 // have (indicates WebView has responded).
336 const gfx::Size capture_size(64, 48);
337 web_contents1->IncrementCapturerCount(capture_size);
338 delegate1.set_is_fullscreened(true);
339 static_cast<content::WebContentsObserver*>(web_view())->
340 DidToggleFullscreenModeForTab(true);
341 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
342 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
344 // When setting the WebContents to nullptr, the native view should become
346 web_view()->SetWebContents(nullptr);
347 EXPECT_EQ(unset_native_view, holder()->native_view());
349 // ...and when setting the WebContents back to the currently-fullscreened
350 // instance, expect the native view and layout to reflect that.
351 web_view()->SetWebContents(web_contents1.get());
352 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
353 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
355 // Now, switch to a different, non-null WebContents instance and check that
356 // the native view has changed and the holder is filling WebView again.
357 web_view()->SetWebContents(web_contents2.get());
358 EXPECT_EQ(web_contents2->GetNativeView(), holder()->native_view());
359 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
361 // Finally, switch back to the first WebContents (still fullscreened).
362 web_view()->SetWebContents(web_contents1.get());
363 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
364 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());