Chromium Code Reviews| 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 BlockedPopupTabHelperTest() |
| 26 web_state_.SetBrowserState(GetBrowserState()); | 29 : web_state_( |
| 27 web_state_.SetNavigationManager( | 30 WebState::Create(WebState::CreateParams(GetBrowserState()))) { |
|
rohitrao (ping after 24h)
2017/02/28 22:51:57
Why do we need a full WebState for this test now?
Eugene But (OOO till 7-30)
2017/02/28 23:50:22
To test that OpenURLFromWebState callback is calle
| |
| 28 base::MakeUnique<web::TestNavigationManager>()); | 31 web_state_->SetDelegate(&web_state_delegate_); |
| 29 | 32 BlockedPopupTabHelper::CreateForWebState(web_state_.get()); |
| 30 BlockedPopupTabHelper::CreateForWebState(&web_state_); | 33 InfoBarManagerImpl::CreateForWebState(web_state_.get()); |
| 31 InfoBarManagerImpl::CreateForWebState(&web_state_); | |
| 32 } | 34 } |
| 33 | 35 |
| 34 // Returns true if InfoBarManager is being observed. | 36 // Returns true if InfoBarManager is being observed. |
| 35 bool IsObservingSources() { | 37 bool IsObservingSources() { |
| 36 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); | 38 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 // Returns BlockedPopupTabHelper that is being tested. | 41 // Returns BlockedPopupTabHelper that is being tested. |
| 40 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { | 42 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { |
| 41 return BlockedPopupTabHelper::FromWebState(&web_state_); | 43 return BlockedPopupTabHelper::FromWebState(web_state_.get()); |
| 42 } | 44 } |
| 45 | |
| 43 // Returns InfoBarManager attached to |web_state_|. | 46 // Returns InfoBarManager attached to |web_state_|. |
| 44 infobars::InfoBarManager* GetInfobarManager() { | 47 infobars::InfoBarManager* GetInfobarManager() { |
| 45 return InfoBarManagerImpl::FromWebState(&web_state_); | 48 return InfoBarManagerImpl::FromWebState(web_state_.get()); |
| 46 } | 49 } |
| 47 | 50 |
| 48 web::TestWebState web_state_; | 51 web::TestWebStateDelegate web_state_delegate_; |
| 52 std::unique_ptr<WebState> web_state_; | |
| 49 }; | 53 }; |
| 50 | 54 |
| 51 // Tests ShouldBlockPopup method. This test changes content settings without | 55 // Tests ShouldBlockPopup method. This test changes content settings without |
| 52 // restoring them back, which is fine because changes do not persist across test | 56 // restoring them back, which is fine because changes do not persist across test |
| 53 // runs. | 57 // runs. |
| 54 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { | 58 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { |
| 55 const GURL source_url1("https://source-url1"); | 59 const GURL source_url1("https://source-url1"); |
| 56 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 60 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
| 57 | 61 |
| 58 // Allow popups for |source_url1|. | 62 // Allow popups for |source_url1|. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 69 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 73 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
| 70 | 74 |
| 71 // Allow all popups. | 75 // Allow all popups. |
| 72 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 76 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 73 CONTENT_SETTING_ALLOW); | 77 CONTENT_SETTING_ALLOW); |
| 74 | 78 |
| 75 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 79 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
| 76 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 80 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
| 77 } | 81 } |
| 78 | 82 |
| 79 // Tests that allowing blocked popup calls |show_popup_handler| and allows | 83 // Tests that allowing blocked popup opens a child window and allows future |
| 80 // future popups for the source url. | 84 // popups for the source url. |
| 81 TEST_F(BlockedPopupTabHelperTest, AllowBlockedPopup) { | 85 TEST_F(BlockedPopupTabHelperTest, AllowBlockedPopup) { |
| 82 const GURL source_url("https://source-url"); | 86 const GURL source_url("https://source-url"); |
| 83 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); | 87 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 84 | 88 |
| 85 // Block popup. | 89 // Block popup. |
| 86 const GURL target_url("https://target-url"); | 90 const GURL target_url("https://target-url"); |
| 87 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); | 91 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); |
| 88 __block bool show_popup_handler_was_called = false; | 92 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); | 93 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 93 | 94 |
| 94 // Allow blocked popup. | 95 // Allow blocked popup. |
| 95 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); | 96 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
| 96 infobars::InfoBar* infobar = GetInfobarManager()->infobar_at(0); | 97 infobars::InfoBar* infobar = GetInfobarManager()->infobar_at(0); |
| 97 auto delegate = infobar->delegate()->AsConfirmInfoBarDelegate(); | 98 auto delegate = infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 98 ASSERT_TRUE(delegate); | 99 ASSERT_TRUE(delegate); |
| 100 ASSERT_FALSE(web_state_delegate_.last_open_url_request()); | |
| 99 delegate->Accept(); | 101 delegate->Accept(); |
| 100 | 102 |
| 101 // Verify that handler was called and popups are allowed for |test_url|. | 103 // Verify that popups are allowed for |test_url|. |
| 102 EXPECT_TRUE(show_popup_handler_was_called); | |
| 103 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); | 104 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 105 | |
| 106 // Verify that child window was open. | |
| 107 auto open_url_request = web_state_delegate_.last_open_url_request(); | |
| 108 ASSERT_TRUE(open_url_request); | |
| 109 EXPECT_EQ(web_state_.get(), open_url_request->web_state); | |
| 110 WebState::OpenURLParams params = open_url_request->params; | |
| 111 EXPECT_EQ(target_url, params.url); | |
| 112 EXPECT_EQ(source_url, params.referrer.url); | |
| 113 EXPECT_EQ(web::ReferrerPolicyDefault, params.referrer.policy); | |
| 114 EXPECT_EQ(WindowOpenDisposition::NEW_POPUP, params.disposition); | |
| 115 EXPECT_TRUE( | |
| 116 PageTransitionCoreTypeIs(params.transition, ui::PAGE_TRANSITION_LINK)); | |
| 117 EXPECT_TRUE(params.is_renderer_initiated); | |
| 104 } | 118 } |
| 105 | 119 |
| 106 // Tests that an infobar is added to the infobar manager when | 120 // Tests that an infobar is added to the infobar manager when |
| 107 // BlockedPopupTabHelper::HandlePopup() is called. | 121 // BlockedPopupTabHelper::HandlePopup() is called. |
| 108 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { | 122 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { |
|
rohitrao (ping after 24h)
2017/02/28 22:51:57
Is there value in having a test that adds the info
Eugene But (OOO till 7-30)
2017/02/28 23:50:22
I think you added this test because you wanted to
rohitrao (ping after 24h)
2017/03/01 15:01:16
Sorry, I was asking if we should add a new test th
Eugene But (OOO till 7-30)
2017/03/01 16:30:25
Oh, that would be a useful test. Added.
Concertin
| |
| 109 const GURL test_url("https://popups.example.com"); | 123 const GURL test_url("https://popups.example.com"); |
| 110 web::BlockedPopupInfo popup_info(test_url, web::Referrer(), nil, nil); | 124 web::BlockedPopupInfo popup_info(test_url, web::Referrer()); |
| 111 | 125 |
| 112 // Check that there are no infobars showing and no registered observers. | 126 // Check that there are no infobars showing and no registered observers. |
| 113 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); | 127 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
| 114 EXPECT_FALSE(IsObservingSources()); | 128 EXPECT_FALSE(IsObservingSources()); |
| 115 | 129 |
| 116 // Call |HandlePopup| to show an infobar. | 130 // Call |HandlePopup| to show an infobar. |
| 117 GetBlockedPopupTabHelper()->HandlePopup(popup_info); | 131 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 118 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); | 132 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
| 119 EXPECT_TRUE(IsObservingSources()); | 133 EXPECT_TRUE(IsObservingSources()); |
| 120 | 134 |
| 121 // Dismiss the infobar and check that the tab helper no longer has any | 135 // Dismiss the infobar and check that the tab helper no longer has any |
| 122 // registered observers. | 136 // registered observers. |
| 123 GetInfobarManager()->infobar_at(0)->RemoveSelf(); | 137 GetInfobarManager()->infobar_at(0)->RemoveSelf(); |
| 124 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); | 138 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
| 125 EXPECT_FALSE(IsObservingSources()); | 139 EXPECT_FALSE(IsObservingSources()); |
| 126 } | 140 } |
| OLD | NEW |