Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / views / controls / webview / webview_unittest.cc
blob349ad3f732d4a25c87e3c1a99b4aa3e6dd02b4a7
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"
21 namespace views {
23 namespace {
25 // Provides functionality to create a test WebContents.
26 class WebViewTestViewsDelegate : public views::TestViewsDelegate {
27 public:
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,
36 site_instance);
39 private:
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 {
46 public:
47 WebViewTestWebContentsObserver(content::WebContents* web_contents)
48 : web_contents_(web_contents),
49 was_shown_(false),
50 shown_count_(0),
51 hidden_count_(0) {
52 content::WebContentsObserver::Observe(web_contents);
55 ~WebViewTestWebContentsObserver() override {
56 if (web_contents_)
57 content::WebContentsObserver::Observe(NULL);
60 void WebContentsDestroyed() override {
61 DCHECK(web_contents_);
62 content::WebContentsObserver::Observe(NULL);
63 web_contents_ = NULL;
66 void WasShown() override {
67 valid_root_while_shown_ =
68 web_contents()->GetNativeView()->GetRootWindow() != NULL;
69 was_shown_ = true;
70 ++shown_count_;
73 void WasHidden() override {
74 was_shown_ = false;
75 ++hidden_count_;
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_; }
86 private:
87 content::WebContents* web_contents_;
88 bool was_shown_;
89 int32 shown_count_;
90 int32 hidden_count_;
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 {
99 public:
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_;
111 private:
112 bool is_fullscreened_;
114 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsDelegate);
117 } // namespace
119 // Provides functionality to test a WebView.
120 class WebViewUnitTest : public views::test::WidgetTest {
121 public:
122 WebViewUnitTest()
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 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down.
133 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate);
134 browser_context_.reset(new content::TestBrowserContext);
135 WidgetTest::SetUp();
136 // Set the test content browser client to avoid pulling in needless
137 // dependencies from content.
138 SetBrowserClientForTesting(&test_browser_client_);
140 // Create a top level widget and add a child, and give it a WebView as a
141 // child.
142 top_level_widget_ = CreateTopLevelFramelessPlatformWidget();
143 top_level_widget_->SetBounds(gfx::Rect(0, 10, 100, 100));
144 View* const contents_view = new View();
145 top_level_widget_->SetContentsView(contents_view);
146 web_view_ = new WebView(browser_context_.get());
147 web_view_->SetBoundsRect(gfx::Rect(contents_view->size()));
148 contents_view->AddChildView(web_view_);
149 top_level_widget_->Show();
150 ASSERT_EQ(gfx::Rect(0, 0, 100, 100), web_view_->bounds());
153 void TearDown() override {
154 top_level_widget_->Close(); // Deletes all children and itself.
155 RunPendingMessages();
157 browser_context_.reset(NULL);
158 // Flush the message loop to execute pending relase tasks as this would
159 // upset ASAN and Valgrind.
160 RunPendingMessages();
161 WidgetTest::TearDown();
164 protected:
165 Widget* top_level_widget() const { return top_level_widget_; }
166 WebView* web_view() const { return web_view_; }
167 NativeViewHost* holder() const { return web_view_->holder_; }
169 scoped_ptr<content::WebContents> CreateWebContents() const {
170 return make_scoped_ptr(content::WebContents::Create(
171 content::WebContents::CreateParams(browser_context_.get())));
174 private:
175 content::TestBrowserThread ui_thread_;
176 content::TestBrowserThread file_blocking_thread_;
177 content::TestBrowserThread io_thread_;
178 scoped_ptr<content::TestBrowserContext> browser_context_;
179 scoped_ptr<WebViewTestViewsDelegate> views_delegate_;
180 content::TestContentBrowserClient test_browser_client_;
182 Widget* top_level_widget_;
183 WebView* web_view_;
185 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest);
188 // Tests that attaching and detaching a WebContents to a WebView makes the
189 // WebContents visible and hidden respectively.
190 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) {
191 // Case 1: Create a new WebContents and set it in the webview via
192 // SetWebContents. This should make the WebContents visible.
193 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
194 WebViewTestWebContentsObserver observer1(web_contents1.get());
195 EXPECT_FALSE(observer1.was_shown());
197 web_view()->SetWebContents(web_contents1.get());
198 EXPECT_TRUE(observer1.was_shown());
199 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible());
200 EXPECT_EQ(observer1.shown_count(), 1);
201 EXPECT_EQ(observer1.hidden_count(), 0);
202 EXPECT_TRUE(observer1.valid_root_while_shown());
204 // Case 2: Create another WebContents and replace the current WebContents
205 // via SetWebContents(). This should hide the current WebContents and show
206 // the new one.
207 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
208 WebViewTestWebContentsObserver observer2(web_contents2.get());
209 EXPECT_FALSE(observer2.was_shown());
211 // Setting the new WebContents should hide the existing one.
212 web_view()->SetWebContents(web_contents2.get());
213 EXPECT_FALSE(observer1.was_shown());
214 EXPECT_TRUE(observer2.was_shown());
215 EXPECT_TRUE(observer2.valid_root_while_shown());
217 // WebContents1 should not get stray show calls when WebContents2 is set.
218 EXPECT_EQ(observer1.shown_count(), 1);
219 EXPECT_EQ(observer1.hidden_count(), 1);
220 EXPECT_EQ(observer2.shown_count(), 1);
221 EXPECT_EQ(observer2.hidden_count(), 0);
223 // Case 3: Test that attaching to a hidden webview does not show the web
224 // contents.
225 web_view()->SetVisible(false);
226 EXPECT_EQ(1, observer2.hidden_count()); // Now hidden.
228 EXPECT_EQ(1, observer1.shown_count());
229 web_view()->SetWebContents(web_contents1.get());
230 EXPECT_EQ(1, observer1.shown_count());
232 // Nothing else should change.
233 EXPECT_EQ(1, observer1.hidden_count());
234 EXPECT_EQ(1, observer2.shown_count());
235 EXPECT_EQ(1, observer2.hidden_count());
237 // Case 4: Test that making the webview visible when a window has an invisible
238 // parent does not make the web contents visible.
239 top_level_widget()->Hide();
240 web_view()->SetVisible(true);
241 // TODO(tapted): The following line is wrong, the shown_count() should still
242 // be 1, until the parent window is made visible on the line after.
243 EXPECT_EQ(2, observer1.shown_count());
244 top_level_widget()->Show();
245 EXPECT_EQ(2, observer1.shown_count());
246 top_level_widget()->Hide();
247 EXPECT_EQ(2, observer1.hidden_count());
249 // Case 5: Test that moving from a hidden parent to a visible parent makes the
250 // web contents visible.
251 Widget* parent2 = CreateTopLevelFramelessPlatformWidget();
252 parent2->SetBounds(gfx::Rect(0, 10, 100, 100));
253 parent2->Show();
254 EXPECT_EQ(2, observer1.shown_count());
255 // Note: that reparenting the windows directly, after the windows have been
256 // created, e.g., Widget::ReparentNativeView(widget, parent2), is not a
257 // supported use case. Instead, move the WebView over.
258 parent2->SetContentsView(web_view());
259 EXPECT_EQ(3, observer1.shown_count());
260 parent2->Close();
263 // Tests that the layout of the NativeViewHost within WebView behaves as
264 // expected when embedding a fullscreen widget during WebContents screen
265 // capture.
266 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Layout) {
267 web_view()->SetEmbedFullscreenWidgetMode(true);
268 ASSERT_EQ(1, web_view()->child_count());
270 const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
271 WebViewTestWebContentsDelegate delegate;
272 web_contents->SetDelegate(&delegate);
273 web_view()->SetWebContents(web_contents.get());
275 // Initially, the holder should fill the entire WebView.
276 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
278 // Simulate a transition into fullscreen mode, but without screen capture
279 // active on the WebContents, the holder should still fill the entire
280 // WebView like before.
281 delegate.set_is_fullscreened(true);
282 static_cast<content::WebContentsObserver*>(web_view())->
283 DidToggleFullscreenModeForTab(true);
284 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
286 // ...and transition back out of fullscreen mode.
287 delegate.set_is_fullscreened(false);
288 static_cast<content::WebContentsObserver*>(web_view())->
289 DidToggleFullscreenModeForTab(false);
290 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
292 // Now, begin screen capture of the WebContents and then enter fullscreen
293 // mode. This time, the holder should be centered within WebView and
294 // sized to match the capture size.
295 const gfx::Size capture_size(64, 48);
296 web_contents->IncrementCapturerCount(capture_size);
297 delegate.set_is_fullscreened(true);
298 static_cast<content::WebContentsObserver*>(web_view())->
299 DidToggleFullscreenModeForTab(true);
300 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
302 // Resize the WebView so that its width is smaller than the capture width.
303 // Expect the holder to be scaled-down, letterboxed style.
304 web_view()->SetBoundsRect(gfx::Rect(0, 0, 32, 32));
305 EXPECT_EQ(gfx::Rect(0, 4, 32, 24), holder()->bounds());
307 // Transition back out of fullscreen mode a final time and confirm the bounds
308 // of the holder fill the entire WebView once again.
309 delegate.set_is_fullscreened(false);
310 static_cast<content::WebContentsObserver*>(web_view())->
311 DidToggleFullscreenModeForTab(false);
312 EXPECT_EQ(gfx::Rect(0, 0, 32, 32), holder()->bounds());
315 // Tests that a WebView correctly switches between WebContentses when one of
316 // them is embedding a fullscreen widget during WebContents screen capture.
317 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Switching) {
318 web_view()->SetEmbedFullscreenWidgetMode(true);
319 ASSERT_EQ(1, web_view()->child_count());
320 const gfx::NativeView unset_native_view = holder()->native_view();
322 // Create two WebContentses to switch between.
323 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents());
324 WebViewTestWebContentsDelegate delegate1;
325 web_contents1->SetDelegate(&delegate1);
326 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
327 WebViewTestWebContentsDelegate delegate2;
328 web_contents2->SetDelegate(&delegate2);
330 EXPECT_NE(web_contents1->GetNativeView(), holder()->native_view());
331 web_view()->SetWebContents(web_contents1.get());
332 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
333 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
335 // Begin screen capture of the WebContents and then enter fullscreen mode.
336 // The native view should not have changed, but the layout of its holder will
337 // have (indicates WebView has responded).
338 const gfx::Size capture_size(64, 48);
339 web_contents1->IncrementCapturerCount(capture_size);
340 delegate1.set_is_fullscreened(true);
341 static_cast<content::WebContentsObserver*>(web_view())->
342 DidToggleFullscreenModeForTab(true);
343 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
344 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
346 // When setting the WebContents to nullptr, the native view should become
347 // unset.
348 web_view()->SetWebContents(nullptr);
349 EXPECT_EQ(unset_native_view, holder()->native_view());
351 // ...and when setting the WebContents back to the currently-fullscreened
352 // instance, expect the native view and layout to reflect that.
353 web_view()->SetWebContents(web_contents1.get());
354 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
355 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
357 // Now, switch to a different, non-null WebContents instance and check that
358 // the native view has changed and the holder is filling WebView again.
359 web_view()->SetWebContents(web_contents2.get());
360 EXPECT_EQ(web_contents2->GetNativeView(), holder()->native_view());
361 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
363 // Finally, switch back to the first WebContents (still fullscreened).
364 web_view()->SetWebContents(web_contents1.get());
365 EXPECT_EQ(web_contents1->GetNativeView(), holder()->native_view());
366 EXPECT_EQ(gfx::Rect(18, 26, 64, 48), holder()->bounds());
369 // Tests that clicking anywhere within the bounds of WebView, and either outside
370 // or inside the bounds of its child NativeViewHost, causes WebView to gain
371 // focus.
372 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_ClickToFocus) {
373 // For this test, add another View that can take focus away from WebView.
374 web_view()->SetBoundsRect(gfx::Rect(0, 0, 100, 90));
375 views::View* const something_to_focus = new views::View();
376 something_to_focus->SetBoundsRect(gfx::Rect(0, 90, 100, 10));
377 something_to_focus->SetFocusable(true);
378 top_level_widget()->GetContentsView()->AddChildView(something_to_focus);
380 web_view()->SetEmbedFullscreenWidgetMode(true);
381 ASSERT_EQ(1, web_view()->child_count());
383 const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
384 WebViewTestWebContentsDelegate delegate;
385 web_contents->SetDelegate(&delegate);
386 web_view()->SetWebContents(web_contents.get());
388 // Begin screen capture of the WebContents and then enter fullscreen mode.
389 // The holder should be centered within WebView and sized to match the capture
390 // size.
391 const gfx::Size capture_size(64, 48);
392 web_contents->IncrementCapturerCount(capture_size);
393 delegate.set_is_fullscreened(true);
394 static_cast<content::WebContentsObserver*>(web_view())->
395 DidToggleFullscreenModeForTab(true);
396 EXPECT_EQ(gfx::Rect(18, 21, 64, 48), holder()->bounds());
398 // Focus the other widget.
399 something_to_focus->RequestFocus();
400 EXPECT_FALSE(web_view()->HasFocus());
401 EXPECT_FALSE(holder()->HasFocus());
402 EXPECT_TRUE(something_to_focus->HasFocus());
404 // Send mouse press event to WebView outside the bounds of the holder, and
405 // confirm WebView took focus.
406 const ui::MouseEvent click_outside_holder(
407 ui::ET_MOUSE_PRESSED, gfx::Point(1, 1),
408 gfx::Point(), // Immaterial.
409 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
410 EXPECT_TRUE(static_cast<views::View*>(web_view())->
411 OnMousePressed(click_outside_holder));
412 EXPECT_TRUE(web_view()->HasFocus());
413 EXPECT_FALSE(holder()->HasFocus());
414 EXPECT_FALSE(something_to_focus->HasFocus());
416 // Focus the other widget again.
417 something_to_focus->RequestFocus();
418 EXPECT_FALSE(web_view()->HasFocus());
419 EXPECT_FALSE(holder()->HasFocus());
420 EXPECT_TRUE(something_to_focus->HasFocus());
422 // Send a mouse press event within the bounds of the holder and expect no
423 // focus change. The reason is that WebView is not supposed to handle mouse
424 // events within the bounds of the holder, and it would be up to the
425 // WebContents native view to grab the focus instead. In this test
426 // environment, the WebContents native view doesn't include the implementation
427 // needed to grab focus, so no focus change will occur.
428 const ui::MouseEvent click_inside_holder(
429 ui::ET_MOUSE_PRESSED, web_view()->bounds().CenterPoint(),
430 gfx::Point(), // Immaterial.
431 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
432 EXPECT_FALSE(static_cast<views::View*>(web_view())->
433 OnMousePressed(click_inside_holder));
434 EXPECT_FALSE(web_view()->HasFocus());
435 EXPECT_FALSE(holder()->HasFocus());
436 EXPECT_TRUE(something_to_focus->HasFocus());
439 } // namespace views