OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/webview/webview.h" | 5 #include "ui/views/controls/webview/webview.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "content/browser/web_contents/web_contents_impl.h" | 8 #include "content/browser/web_contents/web_contents_impl.h" |
9 #include "content/public/browser/web_contents_observer.h" | 9 #include "content/public/browser/web_contents_observer.h" |
10 #include "content/public/test/test_browser_context.h" | 10 #include "content/public/test/test_browser_context.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 content::WebContentsObserver::Observe(NULL); | 95 content::WebContentsObserver::Observe(NULL); |
96 } | 96 } |
97 | 97 |
98 virtual void WebContentsDestroyed() OVERRIDE { | 98 virtual void WebContentsDestroyed() OVERRIDE { |
99 DCHECK(web_contents_); | 99 DCHECK(web_contents_); |
100 content::WebContentsObserver::Observe(NULL); | 100 content::WebContentsObserver::Observe(NULL); |
101 web_contents_ = NULL; | 101 web_contents_ = NULL; |
102 } | 102 } |
103 | 103 |
104 virtual void WasShown() OVERRIDE { | 104 virtual void WasShown() OVERRIDE { |
| 105 valid_root_while_shown_ = |
| 106 web_contents()->GetNativeView()->GetRootWindow() != NULL; |
105 was_shown_ = true; | 107 was_shown_ = true; |
106 ++shown_count_; | 108 ++shown_count_; |
107 } | 109 } |
108 | 110 |
109 virtual void WasHidden() OVERRIDE { | 111 virtual void WasHidden() OVERRIDE { |
110 was_shown_ = false; | 112 was_shown_ = false; |
111 ++hidden_count_; | 113 ++hidden_count_; |
112 } | 114 } |
113 | 115 |
114 bool was_shown() const { return was_shown_; } | 116 bool was_shown() const { return was_shown_; } |
115 | 117 |
116 int shown_count() const { return shown_count_; } | 118 int shown_count() const { return shown_count_; } |
117 | 119 |
118 int hidden_count() const { return hidden_count_; } | 120 int hidden_count() const { return hidden_count_; } |
119 | 121 |
| 122 bool valid_root_while_shown() const { return valid_root_while_shown_; } |
| 123 |
120 private: | 124 private: |
121 content::WebContentsImpl* web_contents_; | 125 content::WebContentsImpl* web_contents_; |
122 bool was_shown_; | 126 bool was_shown_; |
123 int32 shown_count_; | 127 int32 shown_count_; |
124 int32 hidden_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_; |
125 | 131 |
126 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver); | 132 DISALLOW_COPY_AND_ASSIGN(WebViewTestWebContentsObserver); |
127 }; | 133 }; |
128 | 134 |
129 // Tests that attaching and detaching a WebContents to a WebView makes the | 135 // Tests that attaching and detaching a WebContents to a WebView makes the |
130 // WebContents visible and hidden respectively. | 136 // WebContents visible and hidden respectively. |
131 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { | 137 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { |
132 // Create a top level widget and a webview as its content. | 138 // Create a top level widget and a webview as its content. |
133 views::Widget* widget = CreateTopLevelFramelessPlatformWidget(); | 139 views::Widget* widget = CreateTopLevelFramelessPlatformWidget(); |
134 widget->SetBounds(gfx::Rect(0, 10, 100, 100)); | 140 widget->SetBounds(gfx::Rect(0, 10, 100, 100)); |
135 views::WebView* webview = new views::WebView(browser_context()); | 141 views::WebView* webview = new views::WebView(browser_context()); |
136 widget->SetContentsView(webview); | 142 widget->SetContentsView(webview); |
137 widget->Show(); | 143 widget->Show(); |
138 | 144 |
139 // Case 1: Create a new WebContents and set it in the webview via | 145 // Case 1: Create a new WebContents and set it in the webview via |
140 // SetWebContents. This should make the WebContents visible. | 146 // SetWebContents. This should make the WebContents visible. |
141 content::WebContents::CreateParams params(browser_context()); | 147 content::WebContents::CreateParams params(browser_context()); |
142 scoped_ptr<content::WebContents> web_contents1( | 148 scoped_ptr<content::WebContents> web_contents1( |
143 content::WebContents::Create(params)); | 149 content::WebContents::Create(params)); |
144 WebViewTestWebContentsObserver observer1(web_contents1.get()); | 150 WebViewTestWebContentsObserver observer1(web_contents1.get()); |
145 EXPECT_FALSE(observer1.was_shown()); | 151 EXPECT_FALSE(observer1.was_shown()); |
146 | 152 |
147 webview->SetWebContents(web_contents1.get()); | 153 webview->SetWebContents(web_contents1.get()); |
148 EXPECT_TRUE(observer1.was_shown()); | 154 EXPECT_TRUE(observer1.was_shown()); |
149 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); | 155 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); |
150 EXPECT_EQ(observer1.shown_count(), 1); | 156 EXPECT_EQ(observer1.shown_count(), 1); |
151 EXPECT_EQ(observer1.hidden_count(), 0); | 157 EXPECT_EQ(observer1.hidden_count(), 0); |
| 158 EXPECT_TRUE(observer1.valid_root_while_shown()); |
152 | 159 |
153 // Case 2: Create another WebContents and replace the current WebContents | 160 // Case 2: Create another WebContents and replace the current WebContents |
154 // via SetWebContents(). This should hide the current WebContents and show | 161 // via SetWebContents(). This should hide the current WebContents and show |
155 // the new one. | 162 // the new one. |
156 content::WebContents::CreateParams params2(browser_context()); | 163 content::WebContents::CreateParams params2(browser_context()); |
157 scoped_ptr<content::WebContents> web_contents2( | 164 scoped_ptr<content::WebContents> web_contents2( |
158 content::WebContents::Create(params2)); | 165 content::WebContents::Create(params2)); |
| 166 |
159 WebViewTestWebContentsObserver observer2(web_contents2.get()); | 167 WebViewTestWebContentsObserver observer2(web_contents2.get()); |
160 EXPECT_FALSE(observer2.was_shown()); | 168 EXPECT_FALSE(observer2.was_shown()); |
161 | 169 |
162 // Setting the new WebContents should hide the existing one. | 170 // Setting the new WebContents should hide the existing one. |
163 webview->SetWebContents(web_contents2.get()); | 171 webview->SetWebContents(web_contents2.get()); |
164 EXPECT_FALSE(observer1.was_shown()); | 172 EXPECT_FALSE(observer1.was_shown()); |
165 EXPECT_TRUE(observer2.was_shown()); | 173 EXPECT_TRUE(observer2.was_shown()); |
| 174 EXPECT_TRUE(observer2.valid_root_while_shown()); |
166 | 175 |
167 // WebContents1 should not get stray show calls when WebContents2 is set. | 176 // WebContents1 should not get stray show calls when WebContents2 is set. |
168 EXPECT_EQ(observer1.shown_count(), 1); | 177 EXPECT_EQ(observer1.shown_count(), 1); |
169 EXPECT_EQ(observer1.hidden_count(), 1); | 178 EXPECT_EQ(observer1.hidden_count(), 1); |
170 EXPECT_EQ(observer2.shown_count(), 1); | 179 EXPECT_EQ(observer2.shown_count(), 1); |
171 EXPECT_EQ(observer2.hidden_count(), 0); | 180 EXPECT_EQ(observer2.hidden_count(), 0); |
172 | 181 |
173 widget->Close(); | 182 widget->Close(); |
174 RunPendingMessages(); | 183 RunPendingMessages(); |
175 } | 184 } |
176 | 185 |
177 } // namespace | 186 } // namespace |
OLD | NEW |