OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "base/test/ios/wait_util.h" |
| 12 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 13 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 14 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 15 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 16 #import "ios/chrome/browser/ui/settings/block_popups_collection_view_controller.
h" |
| 17 #include "ios/chrome/grit/ios_strings.h" |
| 18 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" |
| 21 |
| 22 @interface SettingsRootCollectionViewController (ExposedForTesting) |
| 23 - (void)editButtonPressed; |
| 24 @end |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char* kAllowedPattern = "[*.]example.com"; |
| 29 const char* kAllowedURL = "http://example.com"; |
| 30 |
| 31 class BlockPopupsCollectionViewControllerTest |
| 32 : public CollectionViewControllerTest { |
| 33 protected: |
| 34 void SetUp() override { |
| 35 CollectionViewControllerTest::SetUp(); |
| 36 TestChromeBrowserState::Builder test_cbs_builder; |
| 37 chrome_browser_state_ = test_cbs_builder.Build(); |
| 38 } |
| 39 |
| 40 CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
| 41 return [[BlockPopupsCollectionViewController alloc] |
| 42 initWithBrowserState:chrome_browser_state_.get()]; |
| 43 } |
| 44 |
| 45 void SetDisallowPopups() { |
| 46 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 47 chrome_browser_state_.get()) |
| 48 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 49 CONTENT_SETTING_BLOCK); |
| 50 } |
| 51 |
| 52 void SetAllowPopups() { |
| 53 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 54 chrome_browser_state_.get()) |
| 55 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 56 CONTENT_SETTING_ALLOW); |
| 57 } |
| 58 |
| 59 void AddAllowedPattern(const std::string& pattern, const GURL& url) { |
| 60 ContentSettingsPattern allowed_pattern = |
| 61 ContentSettingsPattern::FromString(pattern); |
| 62 |
| 63 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 64 chrome_browser_state_.get()) |
| 65 ->SetContentSettingCustomScope( |
| 66 allowed_pattern, ContentSettingsPattern::Wildcard(), |
| 67 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 68 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 69 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 70 chrome_browser_state_.get()) |
| 71 ->GetContentSetting(url, url, CONTENT_SETTINGS_TYPE_POPUPS, |
| 72 std::string())); |
| 73 } |
| 74 |
| 75 web::TestWebThreadBundle thread_bundle_; |
| 76 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 77 }; |
| 78 |
| 79 TEST_F(BlockPopupsCollectionViewControllerTest, TestPopupsNotAllowed) { |
| 80 SetDisallowPopups(); |
| 81 CreateController(); |
| 82 CheckController(); |
| 83 EXPECT_EQ(1, NumberOfSections()); |
| 84 } |
| 85 |
| 86 TEST_F(BlockPopupsCollectionViewControllerTest, TestPopupsAllowed) { |
| 87 SetAllowPopups(); |
| 88 CreateController(); |
| 89 CheckController(); |
| 90 EXPECT_EQ(1, NumberOfSections()); |
| 91 EXPECT_FALSE([controller() navigationItem].rightBarButtonItem); |
| 92 } |
| 93 |
| 94 TEST_F(BlockPopupsCollectionViewControllerTest, TestPopupsAllowedWithOneItem) { |
| 95 // Ensure that even if there are 'allowed' patterns, if block popups is |
| 96 // turned off (popups are allowed), there is no list of patterns. |
| 97 AddAllowedPattern(kAllowedPattern, GURL(kAllowedURL)); |
| 98 SetAllowPopups(); |
| 99 |
| 100 CreateController(); |
| 101 |
| 102 EXPECT_EQ(1, NumberOfSections()); |
| 103 EXPECT_FALSE([controller() navigationItem].rightBarButtonItem); |
| 104 } |
| 105 |
| 106 TEST_F(BlockPopupsCollectionViewControllerTest, TestOneAllowedItem) { |
| 107 AddAllowedPattern(kAllowedPattern, GURL(kAllowedURL)); |
| 108 |
| 109 CreateController(); |
| 110 |
| 111 EXPECT_EQ(2, NumberOfSections()); |
| 112 EXPECT_EQ(1, NumberOfItemsInSection(1)); |
| 113 CheckSectionHeaderWithId(IDS_IOS_POPUPS_ALLOWED, 1); |
| 114 CheckTextCellTitle(base::SysUTF8ToNSString(kAllowedPattern), 1, 0); |
| 115 EXPECT_TRUE([controller() navigationItem].rightBarButtonItem); |
| 116 } |
| 117 |
| 118 TEST_F(BlockPopupsCollectionViewControllerTest, TestOneAllowedItemDeleted) { |
| 119 // Get the number of entries before testing, to ensure after adding and |
| 120 // deleting, the entries are the same. |
| 121 ContentSettingsForOneType initial_entries; |
| 122 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 123 chrome_browser_state_.get()) |
| 124 ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 125 &initial_entries); |
| 126 |
| 127 // Add the pattern to be deleted. |
| 128 AddAllowedPattern(kAllowedPattern, GURL(kAllowedURL)); |
| 129 |
| 130 // Make sure adding the pattern changed the settings size. |
| 131 ContentSettingsForOneType added_entries; |
| 132 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 133 chrome_browser_state_.get()) |
| 134 ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 135 &added_entries); |
| 136 EXPECT_NE(initial_entries.size(), added_entries.size()); |
| 137 |
| 138 CreateController(); |
| 139 |
| 140 BlockPopupsCollectionViewController* popups_controller = |
| 141 static_cast<BlockPopupsCollectionViewController*>(controller()); |
| 142 // Put the collectionView in 'edit' mode. |
| 143 [popups_controller editButtonPressed]; |
| 144 // This is a bit of a shortcut, since actually clicking on the 'delete' |
| 145 // button would be tough. |
| 146 void (^delete_item_with_wait)(int, int) = ^(int i, int j) { |
| 147 __block BOOL completion_called = NO; |
| 148 this->DeleteItem(i, j, ^{ |
| 149 completion_called = YES; |
| 150 }); |
| 151 base::test::ios::WaitUntilCondition(^bool() { |
| 152 return completion_called; |
| 153 }); |
| 154 }; |
| 155 |
| 156 delete_item_with_wait(1, 0); |
| 157 // Exit 'edit' mode. |
| 158 [popups_controller editButtonPressed]; |
| 159 |
| 160 // Verify the resulting UI. |
| 161 EXPECT_EQ(1, NumberOfSections()); |
| 162 EXPECT_EQ(1, NumberOfItemsInSection(0)); |
| 163 |
| 164 // Verify that there are no longer any allowed patterns in |profile_|. |
| 165 ContentSettingsForOneType final_entries; |
| 166 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 167 chrome_browser_state_.get()) |
| 168 ->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 169 &final_entries); |
| 170 EXPECT_EQ(initial_entries.size(), final_entries.size()); |
| 171 } |
| 172 |
| 173 } // namespace |
OLD | NEW |