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 |
| 44 virtual ~WebViewUnitTest() {} |
| 45 |
| 46 virtual void SetUp() OVERRIDE { |
| 47 // The ViewsDelegate is deleted when the ViewsTestBase class is torn down. |
| 48 WidgetTest::set_views_delegate(new WebViewTestViewsDelegate); |
| 49 WidgetTest::SetUp(); |
| 50 // Set the test content browser client to avoid pulling in needless |
| 51 // dependencies from content. |
| 52 SetBrowserClientForTesting(&test_browser_client_); |
| 53 } |
| 54 |
| 55 protected: |
| 56 content::BrowserContext* browser_context() { return &browser_context_; } |
| 57 |
| 58 private: |
| 59 content::TestBrowserThread ui_thread_; |
| 60 content::TestBrowserContext browser_context_; |
| 61 scoped_ptr<WebViewTestViewsDelegate> views_delegate_; |
| 62 content::TestContentBrowserClient test_browser_client_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(WebViewUnitTest); |
| 65 }; |
| 66 |
| 67 // Provides functionaity to observe events on a WebContents like WasShown/ |
| 68 // WasHidden/WebContentsDestroyed. |
| 69 class WebViewTestWebContentsObserver : public content::WebContentsObserver { |
| 70 public: |
| 71 WebViewTestWebContentsObserver(content::WebContents* web_contents) |
| 72 : web_contents_(static_cast<content::WebContentsImpl*>(web_contents)), |
| 73 was_shown_(false), |
| 74 shown_count_(0), |
| 75 hidden_count_(0) { |
| 76 content::WebContentsObserver::Observe(web_contents); |
| 77 } |
| 78 |
| 79 virtual ~WebViewTestWebContentsObserver() { |
| 80 if (web_contents_) |
| 81 content::WebContentsObserver::Observe(NULL); |
| 82 } |
| 83 |
| 84 virtual void WebContentsDestroyed() OVERRIDE { |
| 85 DCHECK(web_contents_); |
| 86 content::WebContentsObserver::Observe(NULL); |
| 87 web_contents_ = NULL; |
| 88 } |
| 89 |
| 90 virtual void WasShown() OVERRIDE { |
| 91 was_shown_ = true; |
| 92 ++shown_count_; |
| 93 } |
| 94 |
| 95 virtual void WasHidden() OVERRIDE { |
| 96 was_shown_ = false; |
| 97 ++hidden_count_; |
| 98 } |
| 99 |
| 100 bool was_shown() const { return was_shown_; } |
| 101 |
| 102 int shown_count() const { return shown_count_; } |
| 103 |
| 104 int hidden_count() const { return hidden_count_; } |
| 105 |
| 106 private: |
| 107 content::WebContentsImpl* web_contents_; |
| 108 bool was_shown_; |
| 109 int32 shown_count_; |
| 110 int32 hidden_count_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver); |
| 113 }; |
| 114 |
| 115 // Tests that attaching and detaching a WebContents to a WebView makes the |
| 116 // WebContents visible and hidden respectively. |
| 117 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { |
| 118 // Create a top level widget and a webview as its content. |
| 119 views::Widget* widget = CreateTopLevelFramelessPlatformWidget(); |
| 120 widget->SetBounds(gfx::Rect(0, 10, 100, 100)); |
| 121 views::WebView* webview = new views::WebView(browser_context()); |
| 122 widget->SetContentsView(webview); |
| 123 widget->Show(); |
| 124 |
| 125 // Case 1: Create a new WebContents and set it in the webview via |
| 126 // SetWebContents. This should make the WebContents visible. |
| 127 content::WebContents::CreateParams params(browser_context()); |
| 128 scoped_ptr<content::WebContents> web_contents1( |
| 129 content::WebContents::Create(params)); |
| 130 WebViewTestWebContentsObserver observer1(web_contents1.get()); |
| 131 EXPECT_FALSE(observer1.was_shown()); |
| 132 |
| 133 webview->SetWebContents(web_contents1.get()); |
| 134 EXPECT_TRUE(observer1.was_shown()); |
| 135 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); |
| 136 EXPECT_EQ(observer1.shown_count(), 1); |
| 137 EXPECT_EQ(observer1.hidden_count(), 0); |
| 138 |
| 139 // Case 2: Create another WebContents and replace the current WebContents |
| 140 // via SetWebContents(). This should hide the current WebContents and show |
| 141 // the new one. |
| 142 content::WebContents::CreateParams params2(browser_context()); |
| 143 scoped_ptr<content::WebContents> web_contents2( |
| 144 content::WebContents::Create(params2)); |
| 145 WebViewTestWebContentsObserver observer2(web_contents2.get()); |
| 146 EXPECT_FALSE(observer2.was_shown()); |
| 147 |
| 148 // Setting the new WebContents should hide the existing one. |
| 149 webview->SetWebContents(web_contents2.get()); |
| 150 EXPECT_FALSE(observer1.was_shown()); |
| 151 EXPECT_TRUE(observer2.was_shown()); |
| 152 |
| 153 // WebContents1 should not get stray show calls when WebContents2 is set. |
| 154 EXPECT_EQ(observer1.shown_count(), 1); |
| 155 EXPECT_EQ(observer1.hidden_count(), 1); |
| 156 EXPECT_EQ(observer2.shown_count(), 1); |
| 157 EXPECT_EQ(observer2.hidden_count(), 0); |
| 158 |
| 159 widget->Close(); |
| 160 RunPendingMessages(); |
| 161 } |
| 162 |
| 163 } // namespace |
OLD | NEW |