| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #import "ios/chrome/browser/web/blocked_popup_tab_helper.h" | 5 #import "ios/chrome/browser/web/blocked_popup_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/content_settings/core/browser/host_content_settings_map.h" | 8 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 9 #include "components/infobars/core/confirm_infobar_delegate.h" | 9 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 10 #include "components/infobars/core/infobar.h" | 10 #include "components/infobars/core/infobar.h" |
| 11 #include "components/infobars/core/infobar_manager.h" | 11 #include "components/infobars/core/infobar_manager.h" |
| 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 13 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" | 13 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 14 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 14 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 15 #import "ios/chrome/browser/web/chrome_web_test.h" | 15 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 16 #import "ios/web/public/test/fakes/test_navigation_manager.h" | 16 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
| 17 #import "ios/web/public/test/fakes/test_web_state.h" | 17 #import "ios/web/public/test/fakes/test_web_state.h" |
| 18 #import "ios/web/public/test/fakes/test_web_state_delegate.h" |
| 18 #include "ios/web/web_state/blocked_popup_info.h" | 19 #include "ios/web/web_state/blocked_popup_info.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 23 using web::WebState; |
| 24 |
| 22 // Test fixture for BlockedPopupTabHelper class. | 25 // Test fixture for BlockedPopupTabHelper class. |
| 23 class BlockedPopupTabHelperTest : public ChromeWebTest { | 26 class BlockedPopupTabHelperTest : public ChromeWebTest { |
| 24 protected: | 27 protected: |
| 25 BlockedPopupTabHelperTest() { | 28 void SetUp() override { |
| 26 web_state_.SetBrowserState(GetBrowserState()); | 29 ChromeWebTest::SetUp(); |
| 27 web_state_.SetNavigationManager( | 30 web_state()->SetDelegate(&web_state_delegate_); |
| 28 base::MakeUnique<web::TestNavigationManager>()); | 31 BlockedPopupTabHelper::CreateForWebState(web_state()); |
| 29 | 32 InfoBarManagerImpl::CreateForWebState(web_state()); |
| 30 BlockedPopupTabHelper::CreateForWebState(&web_state_); | |
| 31 InfoBarManagerImpl::CreateForWebState(&web_state_); | |
| 32 } | 33 } |
| 33 | 34 |
| 34 // Returns true if InfoBarManager is being observed. | 35 // Returns true if InfoBarManager is being observed. |
| 35 bool IsObservingSources() { | 36 bool IsObservingSources() { |
| 36 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); | 37 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Returns BlockedPopupTabHelper that is being tested. | 40 // Returns BlockedPopupTabHelper that is being tested. |
| 40 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { | 41 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { |
| 41 return BlockedPopupTabHelper::FromWebState(&web_state_); | 42 return BlockedPopupTabHelper::FromWebState(web_state()); |
| 42 } | |
| 43 // Returns InfoBarManager attached to |web_state_|. | |
| 44 infobars::InfoBarManager* GetInfobarManager() { | |
| 45 return InfoBarManagerImpl::FromWebState(&web_state_); | |
| 46 } | 43 } |
| 47 | 44 |
| 48 web::TestWebState web_state_; | 45 // Returns InfoBarManager attached to |web_state()|. |
| 46 infobars::InfoBarManager* GetInfobarManager() { |
| 47 return InfoBarManagerImpl::FromWebState(web_state()); |
| 48 } |
| 49 |
| 50 web::TestWebStateDelegate web_state_delegate_; |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 // Tests ShouldBlockPopup method. This test changes content settings without | 53 // Tests ShouldBlockPopup method. This test changes content settings without |
| 52 // restoring them back, which is fine because changes do not persist across test | 54 // restoring them back, which is fine because changes do not persist across test |
| 53 // runs. | 55 // runs. |
| 54 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { | 56 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { |
| 55 const GURL source_url1("https://source-url1"); | 57 const GURL source_url1("https://source-url1"); |
| 56 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 58 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
| 57 | 59 |
| 58 // Allow popups for |source_url1|. | 60 // Allow popups for |source_url1|. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 71 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
| 70 | 72 |
| 71 // Allow all popups. | 73 // Allow all popups. |
| 72 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 74 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 73 CONTENT_SETTING_ALLOW); | 75 CONTENT_SETTING_ALLOW); |
| 74 | 76 |
| 75 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 77 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
| 76 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 78 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
| 77 } | 79 } |
| 78 | 80 |
| 79 // Tests that allowing blocked popup calls |show_popup_handler| and allows | 81 // Tests that allowing blocked popup opens a child window and allows future |
| 80 // future popups for the source url. | 82 // popups for the source url. |
| 81 TEST_F(BlockedPopupTabHelperTest, AllowBlockedPopup) { | 83 TEST_F(BlockedPopupTabHelperTest, AllowBlockedPopup) { |
| 82 const GURL source_url("https://source-url"); | 84 const GURL source_url("https://source-url"); |
| 83 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); | 85 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 84 | 86 |
| 85 // Block popup. | 87 // Block popup. |
| 86 const GURL target_url("https://target-url"); | 88 const GURL target_url("https://target-url"); |
| 87 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); | 89 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); |
| 88 __block bool show_popup_handler_was_called = false; | 90 web::BlockedPopupInfo popup_info(target_url, referrer); |
| 89 web::BlockedPopupInfo popup_info(target_url, referrer, nil, ^{ | |
| 90 show_popup_handler_was_called = true; | |
| 91 }); | |
| 92 GetBlockedPopupTabHelper()->HandlePopup(popup_info); | 91 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 93 | 92 |
| 94 // Allow blocked popup. | 93 // Allow blocked popup. |
| 95 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); | 94 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
| 96 infobars::InfoBar* infobar = GetInfobarManager()->infobar_at(0); | 95 infobars::InfoBar* infobar = GetInfobarManager()->infobar_at(0); |
| 97 auto* delegate = infobar->delegate()->AsConfirmInfoBarDelegate(); | 96 auto* delegate = infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 98 ASSERT_TRUE(delegate); | 97 ASSERT_TRUE(delegate); |
| 98 ASSERT_FALSE(web_state_delegate_.last_open_url_request()); |
| 99 delegate->Accept(); | 99 delegate->Accept(); |
| 100 | 100 |
| 101 // Verify that handler was called and popups are allowed for |test_url|. | 101 // Verify that popups are allowed for |test_url|. |
| 102 EXPECT_TRUE(show_popup_handler_was_called); | |
| 103 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); | 102 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 103 |
| 104 // Verify that child window was open. |
| 105 auto open_url_request = web_state_delegate_.last_open_url_request(); |
| 106 ASSERT_TRUE(open_url_request); |
| 107 EXPECT_EQ(web_state(), open_url_request->web_state); |
| 108 WebState::OpenURLParams params = open_url_request->params; |
| 109 EXPECT_EQ(target_url, params.url); |
| 110 EXPECT_EQ(source_url, params.referrer.url); |
| 111 EXPECT_EQ(web::ReferrerPolicyDefault, params.referrer.policy); |
| 112 EXPECT_EQ(WindowOpenDisposition::NEW_POPUP, params.disposition); |
| 113 EXPECT_TRUE( |
| 114 PageTransitionCoreTypeIs(params.transition, ui::PAGE_TRANSITION_LINK)); |
| 115 EXPECT_TRUE(params.is_renderer_initiated); |
| 116 } |
| 117 |
| 118 // Tests that destroying WebState while Infobar is presented does not crash. |
| 119 TEST_F(BlockedPopupTabHelperTest, DestroyWebState) { |
| 120 const GURL source_url("https://source-url"); |
| 121 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 122 |
| 123 // Block popup. |
| 124 const GURL target_url("https://target-url"); |
| 125 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); |
| 126 web::BlockedPopupInfo popup_info(target_url, referrer); |
| 127 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 128 |
| 129 // Verify that destroying WebState does not crash. |
| 130 DestroyWebState(); |
| 104 } | 131 } |
| 105 | 132 |
| 106 // Tests that an infobar is added to the infobar manager when | 133 // Tests that an infobar is added to the infobar manager when |
| 107 // BlockedPopupTabHelper::HandlePopup() is called. | 134 // BlockedPopupTabHelper::HandlePopup() is called. |
| 108 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { | 135 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { |
| 109 const GURL test_url("https://popups.example.com"); | 136 const GURL test_url("https://popups.example.com"); |
| 110 web::BlockedPopupInfo popup_info(test_url, web::Referrer(), nil, nil); | 137 web::BlockedPopupInfo popup_info(test_url, web::Referrer()); |
| 111 | 138 |
| 112 // Check that there are no infobars showing and no registered observers. | 139 // Check that there are no infobars showing and no registered observers. |
| 113 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); | 140 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
| 114 EXPECT_FALSE(IsObservingSources()); | 141 EXPECT_FALSE(IsObservingSources()); |
| 115 | 142 |
| 116 // Call |HandlePopup| to show an infobar. | 143 // Call |HandlePopup| to show an infobar. |
| 117 GetBlockedPopupTabHelper()->HandlePopup(popup_info); | 144 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 118 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); | 145 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
| 119 EXPECT_TRUE(IsObservingSources()); | 146 EXPECT_TRUE(IsObservingSources()); |
| 120 | 147 |
| 121 // Dismiss the infobar and check that the tab helper no longer has any | 148 // Dismiss the infobar and check that the tab helper no longer has any |
| 122 // registered observers. | 149 // registered observers. |
| 123 GetInfobarManager()->infobar_at(0)->RemoveSelf(); | 150 GetInfobarManager()->infobar_at(0)->RemoveSelf(); |
| 124 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); | 151 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
| 125 EXPECT_FALSE(IsObservingSources()); | 152 EXPECT_FALSE(IsObservingSources()); |
| 126 } | 153 } |
| OLD | NEW |