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/infobar.h" | 10 #include "components/infobars/core/infobar.h" |
10 #include "components/infobars/core/infobar_manager.h" | 11 #include "components/infobars/core/infobar_manager.h" |
11 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 12 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
12 #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" |
13 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" | 14 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
14 #import "ios/chrome/browser/web/chrome_web_test.h" | 15 #import "ios/chrome/browser/web/chrome_web_test.h" |
15 #import "ios/web/public/test/fakes/test_navigation_manager.h" | 16 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
16 #import "ios/web/public/test/fakes/test_web_state.h" | 17 #import "ios/web/public/test/fakes/test_web_state.h" |
17 #include "ios/web/web_state/blocked_popup_info.h" | 18 #include "ios/web/web_state/blocked_popup_info.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 13 matching lines...) Expand all Loading... |
32 | 33 |
33 // Returns true if InfoBarManager is being observed. | 34 // Returns true if InfoBarManager is being observed. |
34 bool IsObservingSources() { | 35 bool IsObservingSources() { |
35 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); | 36 return GetBlockedPopupTabHelper()->scoped_observer_.IsObservingSources(); |
36 } | 37 } |
37 | 38 |
38 // Returns BlockedPopupTabHelper that is being tested. | 39 // Returns BlockedPopupTabHelper that is being tested. |
39 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { | 40 BlockedPopupTabHelper* GetBlockedPopupTabHelper() { |
40 return BlockedPopupTabHelper::FromWebState(&web_state_); | 41 return BlockedPopupTabHelper::FromWebState(&web_state_); |
41 } | 42 } |
| 43 // Returns InfoBarManager attached to |web_state_|. |
| 44 infobars::InfoBarManager* GetInfobarManager() { |
| 45 return InfoBarManagerImpl::FromWebState(&web_state_); |
| 46 } |
42 | 47 |
43 web::TestWebState web_state_; | 48 web::TestWebState web_state_; |
44 }; | 49 }; |
45 | 50 |
46 // Tests ShouldBlockPopup method. This test changes content settings without | 51 // Tests ShouldBlockPopup method. This test changes content settings without |
47 // restoring them back, which is fine because changes do not persist across test | 52 // restoring them back, which is fine because changes do not persist across test |
48 // runs. | 53 // runs. |
49 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { | 54 TEST_F(BlockedPopupTabHelperTest, ShouldBlockPopup) { |
50 const GURL source_url1("https://source-url1"); | 55 const GURL source_url1("https://source-url1"); |
51 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 56 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
(...skipping 12 matching lines...) Expand all Loading... |
64 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 69 EXPECT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
65 | 70 |
66 // Allow all popups. | 71 // Allow all popups. |
67 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 72 settings_map->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
68 CONTENT_SETTING_ALLOW); | 73 CONTENT_SETTING_ALLOW); |
69 | 74 |
70 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); | 75 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url1)); |
71 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); | 76 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url2)); |
72 } | 77 } |
73 | 78 |
| 79 // Tests that allowing blocked popup calls |show_popup_handler| and allows |
| 80 // future popups for the source url. |
| 81 TEST_F(BlockedPopupTabHelperTest, AllowBlockedPopup) { |
| 82 const GURL source_url("https://source-url"); |
| 83 ASSERT_TRUE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 84 |
| 85 // Block popup. |
| 86 const GURL target_url("https://target-url"); |
| 87 web::Referrer referrer(source_url, web::ReferrerPolicyDefault); |
| 88 __block bool show_popup_handler_was_called = false; |
| 89 web::BlockedPopupInfo popup_info(target_url, referrer, nil, ^{ |
| 90 show_popup_handler_was_called = true; |
| 91 }); |
| 92 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
| 93 |
| 94 // Allow blocked popup. |
| 95 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
| 96 infobars::InfoBar* infobar = GetInfobarManager()->infobar_at(0); |
| 97 auto delegate = infobar->delegate()->AsConfirmInfoBarDelegate(); |
| 98 ASSERT_TRUE(delegate); |
| 99 delegate->Accept(); |
| 100 |
| 101 // Verify that handler was called and popups are allowed for |test_url|. |
| 102 EXPECT_TRUE(show_popup_handler_was_called); |
| 103 EXPECT_FALSE(GetBlockedPopupTabHelper()->ShouldBlockPopup(source_url)); |
| 104 } |
| 105 |
74 // Tests that an infobar is added to the infobar manager when | 106 // Tests that an infobar is added to the infobar manager when |
75 // BlockedPopupTabHelper::HandlePopup() is called. | 107 // BlockedPopupTabHelper::HandlePopup() is called. |
76 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { | 108 TEST_F(BlockedPopupTabHelperTest, ShowAndDismissInfoBar) { |
77 const GURL test_url("https://popups.example.com"); | 109 const GURL test_url("https://popups.example.com"); |
78 web::BlockedPopupInfo popup_info(test_url, web::Referrer(), nil, nil); | 110 web::BlockedPopupInfo popup_info(test_url, web::Referrer(), nil, nil); |
79 | 111 |
80 // Check that there are no infobars showing and no registered observers. | 112 // Check that there are no infobars showing and no registered observers. |
81 infobars::InfoBarManager* infobar_manager = | 113 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
82 InfoBarManagerImpl::FromWebState(&web_state_); | |
83 EXPECT_EQ(0U, infobar_manager->infobar_count()); | |
84 EXPECT_FALSE(IsObservingSources()); | 114 EXPECT_FALSE(IsObservingSources()); |
85 | 115 |
86 // Call |HandlePopup| to show an infobar. | 116 // Call |HandlePopup| to show an infobar. |
87 GetBlockedPopupTabHelper()->HandlePopup(popup_info); | 117 GetBlockedPopupTabHelper()->HandlePopup(popup_info); |
88 ASSERT_EQ(1U, infobar_manager->infobar_count()); | 118 ASSERT_EQ(1U, GetInfobarManager()->infobar_count()); |
89 EXPECT_TRUE(IsObservingSources()); | 119 EXPECT_TRUE(IsObservingSources()); |
90 | 120 |
91 // Dismiss the infobar and check that the tab helper no longer has any | 121 // Dismiss the infobar and check that the tab helper no longer has any |
92 // registered observers. | 122 // registered observers. |
93 infobar_manager->infobar_at(0)->RemoveSelf(); | 123 GetInfobarManager()->infobar_at(0)->RemoveSelf(); |
94 EXPECT_EQ(0U, infobar_manager->infobar_count()); | 124 EXPECT_EQ(0U, GetInfobarManager()->infobar_count()); |
95 EXPECT_FALSE(IsObservingSources()); | 125 EXPECT_FALSE(IsObservingSources()); |
96 } | 126 } |
OLD | NEW |