| OLD | NEW |
| (Empty) |
| 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/test/test_views_delegate.h" | |
| 16 #include "ui/views/test/widget_test.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Provides functionality to create a test WebContents. | |
| 21 class WebViewTestViewsDelegate : public views::TestViewsDelegate { | |
| 22 public: | |
| 23 WebViewTestViewsDelegate() {} | |
| 24 virtual ~WebViewTestViewsDelegate() {} | |
| 25 | |
| 26 // Overriden from TestViewsDelegate. | |
| 27 virtual content::WebContents* CreateWebContents( | |
| 28 content::BrowserContext* browser_context, | |
| 29 content::SiteInstance* site_instance) override { | |
| 30 return content::WebContentsTester::CreateTestWebContents(browser_context, | |
| 31 site_instance); | |
| 32 } | |
| 33 | |
| 34 private: | |
| 35 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate); | |
| 36 }; | |
| 37 | |
| 38 // Provides functionality to test a WebView. | |
| 39 class WebViewUnitTest : public views::test::WidgetTest { | |
| 40 public: | |
| 41 WebViewUnitTest() | |
| 42 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), | |
| 43 file_blocking_thread_(content::BrowserThread::FILE_USER_BLOCKING, | |
| 44 base::MessageLoop::current()), | |
| 45 io_thread_(content::BrowserThread::IO, base::MessageLoop::current()) {} | |
| 46 | |
| 47 virtual ~WebViewUnitTest() {} | |
| 48 | |
| 49 virtual void SetUp() override { | |
| 50 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down. | |
| 51 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate); | |
| 52 browser_context_.reset(new content::TestBrowserContext); | |
| 53 WidgetTest::SetUp(); | |
| 54 // Set the test content browser client to avoid pulling in needless | |
| 55 // dependencies from content. | |
| 56 SetBrowserClientForTesting(&test_browser_client_); | |
| 57 } | |
| 58 | |
| 59 virtual void TearDown() override { | |
| 60 browser_context_.reset(NULL); | |
| 61 // Flush the message loop to execute pending relase tasks as this would | |
| 62 // upset ASAN and Valgrind. | |
| 63 RunPendingMessages(); | |
| 64 WidgetTest::TearDown(); | |
| 65 } | |
| 66 | |
| 67 protected: | |
| 68 content::BrowserContext* browser_context() { return browser_context_.get(); } | |
| 69 | |
| 70 private: | |
| 71 content::TestBrowserThread ui_thread_; | |
| 72 content::TestBrowserThread file_blocking_thread_; | |
| 73 content::TestBrowserThread io_thread_; | |
| 74 scoped_ptr<content::TestBrowserContext> browser_context_; | |
| 75 scoped_ptr<WebViewTestViewsDelegate> views_delegate_; | |
| 76 content::TestContentBrowserClient test_browser_client_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest); | |
| 79 }; | |
| 80 | |
| 81 // Provides functionaity to observe events on a WebContents like WasShown/ | |
| 82 // WasHidden/WebContentsDestroyed. | |
| 83 class WebViewTestWebContentsObserver : public content::WebContentsObserver { | |
| 84 public: | |
| 85 WebViewTestWebContentsObserver(content::WebContents* web_contents) | |
| 86 : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)), | |
| 87 was_shown_(false), | |
| 88 shown_count_(0), | |
| 89 hidden_count_(0) { | |
| 90 content::WebContentsObserver::Observe(web_contents); | |
| 91 } | |
| 92 | |
| 93 virtual ~WebViewTestWebContentsObserver() { | |
| 94 if (web_contents_) | |
| 95 content::WebContentsObserver::Observe(NULL); | |
| 96 } | |
| 97 | |
| 98 virtual void WebContentsDestroyed() override { | |
| 99 DCHECK(web_contents_); | |
| 100 content::WebContentsObserver::Observe(NULL); | |
| 101 web_contents_ = NULL; | |
| 102 } | |
| 103 | |
| 104 virtual void WasShown() override { | |
| 105 valid_root_while_shown_ = | |
| 106 web_contents()->GetNativeView()->GetRootWindow() != NULL; | |
| 107 was_shown_ = true; | |
| 108 ++shown_count_; | |
| 109 } | |
| 110 | |
| 111 virtual void WasHidden() override { | |
| 112 was_shown_ = false; | |
| 113 ++hidden_count_; | |
| 114 } | |
| 115 | |
| 116 bool was_shown() const { return was_shown_; } | |
| 117 | |
| 118 int shown_count() const { return shown_count_; } | |
| 119 | |
| 120 int hidden_count() const { return hidden_count_; } | |
| 121 | |
| 122 bool valid_root_while_shown() const { return valid_root_while_shown_; } | |
| 123 | |
| 124 private: | |
| 125 content::WebContentsImpl* web_contents_; | |
| 126 bool was_shown_; | |
| 127 int32 shown_count_; | |
| 128 int32 hidden_count_; | |
| 129 // Set to true if the view containing the webcontents has a valid root window. | |
| 130 bool valid_root_while_shown_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver); | |
| 133 }; | |
| 134 | |
| 135 // Tests that attaching and detaching a WebContents to a WebView makes the | |
| 136 // WebContents visible and hidden respectively. | |
| 137 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 | |
| 150 // SetWebContents. This should make the WebContents visible. | |
| 151 content::WebContents::CreateParams params(browser_context()); | |
| 152 scoped_ptr<content::WebContents> web_contents1( | |
| 153 content::WebContents::Create(params)); | |
| 154 WebViewTestWebContentsObserver observer1(web_contents1.get()); | |
| 155 EXPECT_FALSE(observer1.was_shown()); | |
| 156 | |
| 157 webview->SetWebContents(web_contents1.get()); | |
| 158 EXPECT_TRUE(observer1.was_shown()); | |
| 159 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); | |
| 160 EXPECT_EQ(observer1.shown_count(), 1); | |
| 161 EXPECT_EQ(observer1.hidden_count(), 0); | |
| 162 EXPECT_TRUE(observer1.valid_root_while_shown()); | |
| 163 | |
| 164 // Case 2: Create another WebContents and replace the current WebContents | |
| 165 // via SetWebContents(). This should hide the current WebContents and show | |
| 166 // the new one. | |
| 167 content::WebContents::CreateParams params2(browser_context()); | |
| 168 scoped_ptr<content::WebContents> web_contents2( | |
| 169 content::WebContents::Create(params2)); | |
| 170 | |
| 171 WebViewTestWebContentsObserver observer2(web_contents2.get()); | |
| 172 EXPECT_FALSE(observer2.was_shown()); | |
| 173 | |
| 174 // Setting the new WebContents should hide the existing one. | |
| 175 webview->SetWebContents(web_contents2.get()); | |
| 176 EXPECT_FALSE(observer1.was_shown()); | |
| 177 EXPECT_TRUE(observer2.was_shown()); | |
| 178 EXPECT_TRUE(observer2.valid_root_while_shown()); | |
| 179 | |
| 180 // WebContents1 should not get stray show calls when WebContents2 is set. | |
| 181 EXPECT_EQ(observer1.shown_count(), 1); | |
| 182 EXPECT_EQ(observer1.hidden_count(), 1); | |
| 183 EXPECT_EQ(observer2.shown_count(), 1); | |
| 184 EXPECT_EQ(observer2.hidden_count(), 0); | |
| 185 | |
| 186 // Case 3: Test that attaching to a hidden webview does not show the web | |
| 187 // contents. | |
| 188 webview->SetVisible(false); | |
| 189 EXPECT_EQ(1, observer2.hidden_count()); // Now hidden. | |
| 190 | |
| 191 EXPECT_EQ(1, observer1.shown_count()); | |
| 192 webview->SetWebContents(web_contents1.get()); | |
| 193 EXPECT_EQ(1, observer1.shown_count()); | |
| 194 | |
| 195 // Nothing else should change. | |
| 196 EXPECT_EQ(1, observer1.hidden_count()); | |
| 197 EXPECT_EQ(1, observer2.shown_count()); | |
| 198 EXPECT_EQ(1, observer2.hidden_count()); | |
| 199 | |
| 200 // Case 4: Test that making the webview visible when a window has an invisible | |
| 201 // parent does not make the web contents visible. | |
| 202 parent->Hide(); | |
| 203 webview->SetVisible(true); | |
| 204 // 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. | |
| 206 EXPECT_EQ(2, observer1.shown_count()); | |
| 207 parent->Show(); | |
| 208 EXPECT_EQ(2, observer1.shown_count()); | |
| 209 parent->Hide(); | |
| 210 EXPECT_EQ(2, observer1.hidden_count()); | |
| 211 | |
| 212 // Case 5: Test that moving from a hidden parent to a visible parent makes the | |
| 213 // web contents visible. | |
| 214 views::Widget* parent2 = CreateTopLevelFramelessPlatformWidget(); | |
| 215 parent2->SetBounds(gfx::Rect(0, 10, 100, 100)); | |
| 216 parent2->Show(); | |
| 217 | |
| 218 EXPECT_EQ(2, observer1.shown_count()); | |
| 219 // Note: that reparenting the windows directly, after the windows have been | |
| 220 // created, e.g., views::Widget::ReparentNativeView(widget, parent2), is not a | |
| 221 // supported use case. Instead, move the WebView over. | |
| 222 parent2->SetContentsView(webview); | |
| 223 EXPECT_EQ(3, observer1.shown_count()); | |
| 224 | |
| 225 widget->Close(); | |
| 226 parent->Close(); | |
| 227 parent2->Close(); | |
| 228 RunPendingMessages(); | |
| 229 } | |
| 230 | |
| 231 } // namespace | |
| OLD | NEW |