| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/public/test/test_browser_context.h" | 13 #include "content/public/test/test_browser_context.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "content/public/test/web_contents_tester.h" | 15 #include "content/public/test/web_contents_tester.h" |
| 16 #include "content/test/test_content_browser_client.h" | 16 #include "content/test/test_content_browser_client.h" |
| 17 #include "ui/aura/window.h" | |
| 18 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
| 19 #include "ui/events/event_utils.h" | 18 #include "ui/events/event_utils.h" |
| 20 #include "ui/views/controls/native/native_view_host.h" | 19 #include "ui/views/controls/native/native_view_host.h" |
| 21 #include "ui/views/test/test_views_delegate.h" | 20 #include "ui/views/test/test_views_delegate.h" |
| 22 #include "ui/views/test/widget_test.h" | 21 #include "ui/views/test/widget_test.h" |
| 23 | 22 |
| 23 #if defined(USE_AURA) |
| 24 #include "ui/aura/window.h" |
| 25 #endif |
| 26 |
| 24 namespace views { | 27 namespace views { |
| 25 | 28 |
| 26 namespace { | 29 namespace { |
| 27 | 30 |
| 28 // Provides functionality to create a test WebContents. | 31 // Provides functionality to create a test WebContents. |
| 29 class WebViewTestViewsDelegate : public views::TestViewsDelegate { | 32 class WebViewTestViewsDelegate : public views::TestViewsDelegate { |
| 30 public: | 33 public: |
| 31 WebViewTestViewsDelegate() {} | 34 WebViewTestViewsDelegate() {} |
| 32 ~WebViewTestViewsDelegate() override {} | 35 ~WebViewTestViewsDelegate() override {} |
| 33 | 36 |
| 34 // Overriden from TestViewsDelegate. | 37 // Overriden from TestViewsDelegate. |
| 35 content::WebContents* CreateWebContents( | 38 content::WebContents* CreateWebContents( |
| 36 content::BrowserContext* browser_context, | 39 content::BrowserContext* browser_context, |
| 37 content::SiteInstance* site_instance) override { | 40 content::SiteInstance* site_instance) override { |
| 38 return content::WebContentsTester::CreateTestWebContents(browser_context, | 41 return content::WebContentsTester::CreateTestWebContents(browser_context, |
| 39 site_instance); | 42 site_instance); |
| 40 } | 43 } |
| 41 | 44 |
| 42 private: | 45 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate); | 46 DISALLOW_COPY_AND_ASSIGN(WebViewTestViewsDelegate); |
| 44 }; | 47 }; |
| 45 | 48 |
| 46 // Provides functionaity to observe events on a WebContents like WasShown/ | 49 // Provides functionality to observe events on a WebContents like WasShown/ |
| 47 // WasHidden/WebContentsDestroyed. | 50 // WasHidden/WebContentsDestroyed. |
| 48 class WebViewTestWebContentsObserver : public content::WebContentsObserver { | 51 class WebViewTestWebContentsObserver : public content::WebContentsObserver { |
| 49 public: | 52 public: |
| 50 WebViewTestWebContentsObserver(content::WebContents* web_contents) | 53 WebViewTestWebContentsObserver(content::WebContents* web_contents) |
| 51 : web_contents_(web_contents), | 54 : web_contents_(web_contents), |
| 52 was_shown_(false), | 55 was_shown_(false), |
| 53 shown_count_(0), | 56 shown_count_(0), |
| 54 hidden_count_(0) { | 57 hidden_count_(0), |
| 58 valid_root_while_shown_(true) { |
| 55 content::WebContentsObserver::Observe(web_contents); | 59 content::WebContentsObserver::Observe(web_contents); |
| 56 } | 60 } |
| 57 | 61 |
| 58 ~WebViewTestWebContentsObserver() override { | 62 ~WebViewTestWebContentsObserver() override { |
| 59 if (web_contents_) | 63 if (web_contents_) |
| 60 content::WebContentsObserver::Observe(NULL); | 64 content::WebContentsObserver::Observe(NULL); |
| 61 } | 65 } |
| 62 | 66 |
| 63 void WebContentsDestroyed() override { | 67 void WebContentsDestroyed() override { |
| 64 DCHECK(web_contents_); | 68 DCHECK(web_contents_); |
| 65 content::WebContentsObserver::Observe(NULL); | 69 content::WebContentsObserver::Observe(NULL); |
| 66 web_contents_ = NULL; | 70 web_contents_ = NULL; |
| 67 } | 71 } |
| 68 | 72 |
| 69 void WasShown() override { | 73 void WasShown() override { |
| 74 #if defined(USE_AURA) |
| 70 valid_root_while_shown_ = | 75 valid_root_while_shown_ = |
| 71 web_contents()->GetNativeView()->GetRootWindow() != NULL; | 76 web_contents()->GetNativeView()->GetRootWindow() != NULL; |
| 77 #endif |
| 72 was_shown_ = true; | 78 was_shown_ = true; |
| 73 ++shown_count_; | 79 ++shown_count_; |
| 74 } | 80 } |
| 75 | 81 |
| 76 void WasHidden() override { | 82 void WasHidden() override { |
| 77 was_shown_ = false; | 83 was_shown_ = false; |
| 78 ++hidden_count_; | 84 ++hidden_count_; |
| 79 } | 85 } |
| 80 | 86 |
| 81 bool was_shown() const { return was_shown_; } | 87 bool was_shown() const { return was_shown_; } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // WebContents visible and hidden respectively. | 196 // WebContents visible and hidden respectively. |
| 191 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { | 197 TEST_F(WebViewUnitTest, TestWebViewAttachDetachWebContents) { |
| 192 // Case 1: Create a new WebContents and set it in the webview via | 198 // Case 1: Create a new WebContents and set it in the webview via |
| 193 // SetWebContents. This should make the WebContents visible. | 199 // SetWebContents. This should make the WebContents visible. |
| 194 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents()); | 200 const scoped_ptr<content::WebContents> web_contents1(CreateWebContents()); |
| 195 WebViewTestWebContentsObserver observer1(web_contents1.get()); | 201 WebViewTestWebContentsObserver observer1(web_contents1.get()); |
| 196 EXPECT_FALSE(observer1.was_shown()); | 202 EXPECT_FALSE(observer1.was_shown()); |
| 197 | 203 |
| 198 web_view()->SetWebContents(web_contents1.get()); | 204 web_view()->SetWebContents(web_contents1.get()); |
| 199 EXPECT_TRUE(observer1.was_shown()); | 205 EXPECT_TRUE(observer1.was_shown()); |
| 206 #if defined(USE_AURA) |
| 200 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); | 207 EXPECT_TRUE(web_contents1->GetNativeView()->IsVisible()); |
| 208 #endif |
| 201 EXPECT_EQ(observer1.shown_count(), 1); | 209 EXPECT_EQ(observer1.shown_count(), 1); |
| 202 EXPECT_EQ(observer1.hidden_count(), 0); | 210 EXPECT_EQ(observer1.hidden_count(), 0); |
| 203 EXPECT_TRUE(observer1.valid_root_while_shown()); | 211 EXPECT_TRUE(observer1.valid_root_while_shown()); |
| 204 | 212 |
| 205 // Case 2: Create another WebContents and replace the current WebContents | 213 // Case 2: Create another WebContents and replace the current WebContents |
| 206 // via SetWebContents(). This should hide the current WebContents and show | 214 // via SetWebContents(). This should hide the current WebContents and show |
| 207 // the new one. | 215 // the new one. |
| 208 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents()); | 216 const scoped_ptr<content::WebContents> web_contents2(CreateWebContents()); |
| 209 WebViewTestWebContentsObserver observer2(web_contents2.get()); | 217 WebViewTestWebContentsObserver observer2(web_contents2.get()); |
| 210 EXPECT_FALSE(observer2.was_shown()); | 218 EXPECT_FALSE(observer2.was_shown()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 228 | 236 |
| 229 EXPECT_EQ(1, observer1.shown_count()); | 237 EXPECT_EQ(1, observer1.shown_count()); |
| 230 web_view()->SetWebContents(web_contents1.get()); | 238 web_view()->SetWebContents(web_contents1.get()); |
| 231 EXPECT_EQ(1, observer1.shown_count()); | 239 EXPECT_EQ(1, observer1.shown_count()); |
| 232 | 240 |
| 233 // Nothing else should change. | 241 // Nothing else should change. |
| 234 EXPECT_EQ(1, observer1.hidden_count()); | 242 EXPECT_EQ(1, observer1.hidden_count()); |
| 235 EXPECT_EQ(1, observer2.shown_count()); | 243 EXPECT_EQ(1, observer2.shown_count()); |
| 236 EXPECT_EQ(1, observer2.hidden_count()); | 244 EXPECT_EQ(1, observer2.hidden_count()); |
| 237 | 245 |
| 246 #if defined(USE_AURA) |
| 238 // Case 4: Test that making the webview visible when a window has an invisible | 247 // Case 4: Test that making the webview visible when a window has an invisible |
| 239 // parent does not make the web contents visible. | 248 // parent does not make the web contents visible. |
| 240 top_level_widget()->Hide(); | 249 top_level_widget()->Hide(); |
| 241 web_view()->SetVisible(true); | 250 web_view()->SetVisible(true); |
| 242 // TODO(tapted): The following line is wrong, the shown_count() should still | 251 // TODO(tapted): The following line is wrong, the shown_count() should still |
| 243 // be 1, until the parent window is made visible on the line after. | 252 // be 1, until the parent window is made visible on the line after. |
| 244 EXPECT_EQ(2, observer1.shown_count()); | 253 EXPECT_EQ(2, observer1.shown_count()); |
| 245 top_level_widget()->Show(); | 254 top_level_widget()->Show(); |
| 246 EXPECT_EQ(2, observer1.shown_count()); | 255 EXPECT_EQ(2, observer1.shown_count()); |
| 247 top_level_widget()->Hide(); | 256 top_level_widget()->Hide(); |
| 248 EXPECT_EQ(2, observer1.hidden_count()); | 257 EXPECT_EQ(2, observer1.hidden_count()); |
| 249 | 258 #else |
| 259 // On Mac, changes to window visibility do not trigger calls to WebContents:: |
| 260 // WasShown() or WasHidden(), since the OS does not provide good signals for |
| 261 // window visibility. However, we can still test that moving a visible WebView |
| 262 // whose WebContents is not currently showing to a new, visible window will |
| 263 // show the WebContents. Simulate the "hide window with visible WebView" step |
| 264 // simply by detaching the WebContents. |
| 265 web_view()->SetVisible(true); |
| 266 EXPECT_EQ(2, observer1.shown_count()); |
| 267 web_view()->holder()->Detach(); |
| 268 EXPECT_EQ(2, observer1.hidden_count()); |
| 269 #endif |
| 250 // Case 5: Test that moving from a hidden parent to a visible parent makes the | 270 // Case 5: Test that moving from a hidden parent to a visible parent makes the |
| 251 // web contents visible. | 271 // web contents visible. |
| 252 Widget* parent2 = CreateTopLevelFramelessPlatformWidget(); | 272 Widget* parent2 = CreateTopLevelFramelessPlatformWidget(); |
| 253 parent2->SetBounds(gfx::Rect(0, 10, 100, 100)); | 273 parent2->SetBounds(gfx::Rect(0, 10, 100, 100)); |
| 254 parent2->Show(); | 274 parent2->Show(); |
| 255 EXPECT_EQ(2, observer1.shown_count()); | 275 EXPECT_EQ(2, observer1.shown_count()); |
| 256 // Note: that reparenting the windows directly, after the windows have been | 276 // Note: that reparenting the windows directly, after the windows have been |
| 257 // created, e.g., Widget::ReparentNativeView(widget, parent2), is not a | 277 // created, e.g., Widget::ReparentNativeView(widget, parent2), is not a |
| 258 // supported use case. Instead, move the WebView over. | 278 // supported use case. Instead, move the WebView over. |
| 259 parent2->SetContentsView(web_view()); | 279 parent2->SetContentsView(web_view()); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 gfx::Point(), // Immaterial. | 451 gfx::Point(), // Immaterial. |
| 432 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); | 452 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); |
| 433 EXPECT_FALSE(static_cast<views::View*>(web_view())-> | 453 EXPECT_FALSE(static_cast<views::View*>(web_view())-> |
| 434 OnMousePressed(click_inside_holder)); | 454 OnMousePressed(click_inside_holder)); |
| 435 EXPECT_FALSE(web_view()->HasFocus()); | 455 EXPECT_FALSE(web_view()->HasFocus()); |
| 436 EXPECT_FALSE(holder()->HasFocus()); | 456 EXPECT_FALSE(holder()->HasFocus()); |
| 437 EXPECT_TRUE(something_to_focus->HasFocus()); | 457 EXPECT_TRUE(something_to_focus->HasFocus()); |
| 438 } | 458 } |
| 439 | 459 |
| 440 } // namespace views | 460 } // namespace views |
| OLD | NEW |