Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / webview / webview_unittest.cc
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.
4
5 #include "ui/views/controls/webview/webview.h"
6
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"
18
19 namespace views {
20
21 namespace {
22
23 // Provides functionality to create a test WebContents.
24 class WebViewTestViewsDelegate : public views::TestViewsDelegate {
25  public:
26   WebViewTestViewsDelegate() {}
27   ~WebViewTestViewsDelegate() override {}
28
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,
34                                                              site_instance);
35   }
36
37  private:
38   DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate);
39 };
40
41 // Provides functionaity to observe events on a WebContents like WasShown/
42 // WasHidden/WebContentsDestroyed.
43 class WebViewTestWebContentsObserver : public content::WebContentsObserver {
44  public:
45   WebViewTestWebContentsObserver(content::WebContents* web_contents)
46       : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)),
47         was_shown_(false),
48         shown_count_(0),
49         hidden_count_(0) {
50     content::WebContentsObserver::Observe(web_contents);
51   }
52
53   ~WebViewTestWebContentsObserver() override {
54     if (web_contents_)
55       content::WebContentsObserver::Observe(NULL);
56   }
57
58   void WebContentsDestroyed() override {
59     DCHECK(web_contents_);
60     content::WebContentsObserver::Observe(NULL);
61     web_contents_ = NULL;
62   }
63
64   void WasShown() override {
65     valid_root_while_shown_ =
66         web_contents()->GetNativeView()->GetRootWindow() != NULL;
67     was_shown_ = true;
68     ++shown_count_;
69   }
70
71   void WasHidden() override {
72     was_shown_ = false;
73     ++hidden_count_;
74   }
75
76   bool was_shown() const { return was_shown_; }
77
78   int shown_count() const { return shown_count_; }
79
80   int hidden_count() const { return hidden_count_; }
81
82   bool valid_root_while_shown() const { return valid_root_while_shown_; }
83
84  private:
85   content::WebContentsImpl* web_contents_;
86   bool was_shown_;
87   int32 shown_count_;
88   int32 hidden_count_;
89   // Set to true if the view containing the webcontents has a valid root window.
90   bool valid_root_while_shown_;
91
92   DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver);
93 };
94
95 // Fakes the fullscreen browser state reported to WebContents and WebView.
96 class WebViewTestWebContentsDelegate : public content::WebContentsDelegate {
97  public:
98   WebViewTestWebContentsDelegate() : is_fullscreened_(false) {}
99   ~WebViewTestWebContentsDelegate() override {}
100
101   void set_is_fullscreened(bool fs) { is_fullscreened_ = fs; }
102
103   // content::WebContentsDelegate overrides.
104   bool IsFullscreenForTabOrPending(
105       const content::WebContents* ignored) const override {
106     return is_fullscreened_;
107   }
108
109  private:
110   bool is_fullscreened_;
111
112   DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsDelegate);
113 };
114
115 }  // namespace
116
117 // Provides functionality to test a WebView.
118 class WebViewUnitTest : public views::test::WidgetTest {
119  public:
120   WebViewUnitTest()
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) {}
126
127   ~WebViewUnitTest() override {}
128
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);
133     WidgetTest::SetUp();
134     // Set the test content browser client to avoid pulling in needless
135     // dependencies from content.
136     SetBrowserClientForTesting(&test_browser_client_);
137
138     // Create a top level widget and add a child, and give it a WebView as a
139     // child.
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());
149   }
150
151   void TearDown() override {
152     top_level_widget_->Close();  // Deletes all children and itself.
153     RunPendingMessages();
154
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();
160   }
161
162  protected:
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_; }
166
167   scoped_ptr<content::WebContents> CreateWebContents() const {
168     return make_scoped_ptr(content::WebContents::Create(
169         content::WebContents::CreateParams(browser_context_.get())));
170   }
171
172  private:
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_;
179
180   Widget* top_level_widget_;
181   WebView* web_view_;
182
183   DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest);
184 };
185
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());
194
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());
201
202   // Case 2: Create another WebContents and replace the current WebContents
203   // via SetWebContents(). This should hide the current WebContents and show
204   // the new one.
205   const scoped_ptr<content::WebContents> web_contents2(CreateWebContents());
206   WebViewTestWebContentsObserver observer2(web_contents2.get());
207   EXPECT_FALSE(observer2.was_shown());
208
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());
214
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);
220
221   // Case 3: Test that attaching to a hidden webview does not show the web
222   // contents.
223   web_view()->SetVisible(false);
224   EXPECT_EQ(1, observer2.hidden_count());  // Now hidden.
225
226   EXPECT_EQ(1, observer1.shown_count());
227   web_view()->SetWebContents(web_contents1.get());
228   EXPECT_EQ(1, observer1.shown_count());
229
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());
234
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());
246
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));
251   parent2->Show();
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());
258   parent2->Close();
259 }
260
261 // Tests that the layout of the NativeViewHost within WebView behaves as
262 // expected when embedding a fullscreen widget during WebContents screen
263 // capture.
264 TEST_F(WebViewUnitTest, EmbeddedFullscreenDuringScreenCapture_Layout) {
265   web_view()->SetEmbedFullscreenWidgetMode(true);
266   ASSERT_EQ(1, web_view()->child_count());
267
268   const scoped_ptr<content::WebContents> web_contents(CreateWebContents());
269   WebViewTestWebContentsDelegate delegate;
270   web_contents->SetDelegate(&delegate);
271   web_view()->SetWebContents(web_contents.get());
272
273   // Initially, the holder should fill the entire WebView.
274   EXPECT_EQ(gfx::Rect(0, 0, 100, 100), holder()->bounds());
275
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());
283
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());
289
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());
299
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());
304
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());
311 }
312
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();
319
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);
327
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());
332
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());
343
344   // When setting the WebContents to nullptr, the native view should become
345   // unset.
346   web_view()->SetWebContents(nullptr);
347   EXPECT_EQ(unset_native_view, holder()->native_view());
348
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());
354
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());
360
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());
365 }
366
367 }  // namespace views