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/public/browser/web_contents.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/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/views/controls/native/native_view_host.h"
18 #include "ui/views/test/test_views_delegate.h"
19 #include "ui/views/test/widget_test.h"
25 // Provides functionality to create a test WebContents.
26 class WebViewTestViewsDelegate
: public views::TestViewsDelegate
{
28 WebViewTestViewsDelegate() {}
29 ~WebViewTestViewsDelegate() override
{}
31 // Overriden from TestViewsDelegate.
32 content::WebContents
* CreateWebContents(
33 content::BrowserContext
* browser_context
,
34 content::SiteInstance
* site_instance
) override
{
35 return content::WebContentsTester::CreateTestWebContents(browser_context
,
40 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate
);
43 // Provides functionaity to observe events on a WebContents like WasShown/
44 // WasHidden/WebContentsDestroyed.
45 class WebViewTestWebContentsObserver
: public content::WebContentsObserver
{
47 WebViewTestWebContentsObserver(content::WebContents
* web_contents
)
48 : web_contents_(web_contents
),
52 content::WebContentsObserver::Observe(web_contents
);
55 ~WebViewTestWebContentsObserver() override
{
57 content::WebContentsObserver::Observe(NULL
);
60 void WebContentsDestroyed() override
{
61 DCHECK(web_contents_
);
62 content::WebContentsObserver::Observe(NULL
);
66 void WasShown() override
{
67 valid_root_while_shown_
=
68 web_contents()->GetNativeView()->GetRootWindow() != NULL
;
73 void WasHidden() override
{
78 bool was_shown() const { return was_shown_
; }
80 int shown_count() const { return shown_count_
; }
82 int hidden_count() const { return hidden_count_
; }
84 bool valid_root_while_shown() const { return valid_root_while_shown_
; }
87 content::WebContents
* web_contents_
;
91 // Set to true if the view containing the webcontents has a valid root window.
92 bool valid_root_while_shown_
;
94 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver
);
97 // Fakes the fullscreen browser state reported to WebContents and WebView.
98 class WebViewTestWebContentsDelegate
: public content::WebContentsDelegate
{
100 WebViewTestWebContentsDelegate() : is_fullscreened_(false) {}
101 ~WebViewTestWebContentsDelegate() override
{}
103 void set_is_fullscreened(bool fs
) { is_fullscreened_
= fs
; }
105 // content::WebContentsDelegate overrides.
106 bool IsFullscreenForTabOrPending(
107 const content::WebContents
* ignored
) const override
{
108 return is_fullscreened_
;
112 bool is_fullscreened_
;
114 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsDelegate
);
119 // Provides functionality to test a WebView.
120 class WebViewUnitTest
: public views::test::WidgetTest
{
123 : ui_thread_(content::BrowserThread::UI
, base::MessageLoop::current()),
124 file_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING
,
125 base::MessageLoop::current()),
126 io_thread_(content::BrowserThread::IO
, base::MessageLoop::current()),
127 top_level_widget_(nullptr) {}
129 ~WebViewUnitTest() override
{}
131 void SetUp() override
{
132 set_views_delegate(make_scoped_ptr(new WebViewTestViewsDelegate
));
133 browser_context_
.reset(new content::TestBrowserContext
);
135 // Set the test content browser client to avoid pulling in needless
136 // dependencies from content.
137 SetBrowserClientForTesting(&test_browser_client_
);
139 // Create a top level widget and add a child, and give it a WebView as a
141 top_level_widget_
= CreateTopLevelFramelessPlatformWidget();
142 top_level_widget_
->SetBounds(gfx::Rect(0, 10, 100, 100));
143 View
* const contents_view
= new View();
144 top_level_widget_
->SetContentsView(contents_view
);
145 web_view_
= new WebView(browser_context_
.get());
146 web_view_
->SetBoundsRect(gfx::Rect(contents_view
->size()));
147 contents_view
->AddChildView(web_view_
);
148 top_level_widget_
->Show();
149 ASSERT_EQ(gfx::Rect(0, 0, 100, 100), web_view_
->bounds());
152 void TearDown() override
{
153 top_level_widget_
->Close(); // Deletes all children and itself.
154 RunPendingMessages();
156 browser_context_
.reset(NULL
);
157 // Flush the message loop to execute pending relase tasks as this would
158 // upset ASAN and Valgrind.
159 RunPendingMessages();
160 WidgetTest::TearDown();
164 Widget
* top_level_widget() const { return top_level_widget_
; }
165 WebView
* web_view() const { return web_view_
; }
166 NativeViewHost
* holder() const { return web_view_
->holder_
; }
168 scoped_ptr
<content::WebContents
> CreateWebContents() const {
169 return make_scoped_ptr(content::WebContents::Create(
170 content::WebContents::CreateParams(browser_context_
.get())));
174 content::TestBrowserThread ui_thread_
;
175 content::TestBrowserThread file_blocking_thread_
;
176 content::TestBrowserThread io_thread_
;
177 scoped_ptr
<content::TestBrowserContext
> browser_context_
;
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());
367 // Tests that clicking anywhere within the bounds of WebView, and either outside
368 // or inside the bounds of its child NativeViewHost, causes WebView to gain
370 TEST_F(WebViewUnitTest
, EmbeddedFullscreenDuringScreenCapture_ClickToFocus
) {
371 // For this test, add another View that can take focus away from WebView.
372 web_view()->SetBoundsRect(gfx::Rect(0, 0, 100, 90));
373 views::View
* const something_to_focus
= new views::View();
374 something_to_focus
->SetBoundsRect(gfx::Rect(0, 90, 100, 10));
375 something_to_focus
->SetFocusable(true);
376 top_level_widget()->GetContentsView()->AddChildView(something_to_focus
);
378 web_view()->SetEmbedFullscreenWidgetMode(true);
379 ASSERT_EQ(1, web_view()->child_count());
381 const scoped_ptr
<content::WebContents
> web_contents(CreateWebContents());
382 WebViewTestWebContentsDelegate delegate
;
383 web_contents
->SetDelegate(&delegate
);
384 web_view()->SetWebContents(web_contents
.get());
386 // Begin screen capture of the WebContents and then enter fullscreen mode.
387 // The holder should be centered within WebView and sized to match the capture
389 const gfx::Size
capture_size(64, 48);
390 web_contents
->IncrementCapturerCount(capture_size
);
391 delegate
.set_is_fullscreened(true);
392 static_cast<content::WebContentsObserver
*>(web_view())->
393 DidToggleFullscreenModeForTab(true);
394 EXPECT_EQ(gfx::Rect(18, 21, 64, 48), holder()->bounds());
396 // Focus the other widget.
397 something_to_focus
->RequestFocus();
398 EXPECT_FALSE(web_view()->HasFocus());
399 EXPECT_FALSE(holder()->HasFocus());
400 EXPECT_TRUE(something_to_focus
->HasFocus());
402 // Send mouse press event to WebView outside the bounds of the holder, and
403 // confirm WebView took focus.
404 const ui::MouseEvent
click_outside_holder(
405 ui::ET_MOUSE_PRESSED
, gfx::Point(1, 1),
406 gfx::Point(), // Immaterial.
407 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
, 0);
408 EXPECT_TRUE(static_cast<views::View
*>(web_view())->
409 OnMousePressed(click_outside_holder
));
410 EXPECT_TRUE(web_view()->HasFocus());
411 EXPECT_FALSE(holder()->HasFocus());
412 EXPECT_FALSE(something_to_focus
->HasFocus());
414 // Focus the other widget again.
415 something_to_focus
->RequestFocus();
416 EXPECT_FALSE(web_view()->HasFocus());
417 EXPECT_FALSE(holder()->HasFocus());
418 EXPECT_TRUE(something_to_focus
->HasFocus());
420 // Send a mouse press event within the bounds of the holder and expect no
421 // focus change. The reason is that WebView is not supposed to handle mouse
422 // events within the bounds of the holder, and it would be up to the
423 // WebContents native view to grab the focus instead. In this test
424 // environment, the WebContents native view doesn't include the implementation
425 // needed to grab focus, so no focus change will occur.
426 const ui::MouseEvent
click_inside_holder(
427 ui::ET_MOUSE_PRESSED
, web_view()->bounds().CenterPoint(),
428 gfx::Point(), // Immaterial.
429 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
, 0);
430 EXPECT_FALSE(static_cast<views::View
*>(web_view())->
431 OnMousePressed(click_inside_holder
));
432 EXPECT_FALSE(web_view()->HasFocus());
433 EXPECT_FALSE(holder()->HasFocus());
434 EXPECT_TRUE(something_to_focus
->HasFocus());