[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | #include "chrome/browser/host_content_settings_map.h" |
| 6 | |
[email protected] | 0412aed1 | 2010-06-11 17:17:36 | [diff] [blame] | 7 | #include "chrome/browser/pref_service.h" |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 8 | #include "chrome/common/notification_registrar.h" |
| 9 | #include "chrome/common/notification_service.h" |
[email protected] | 0412aed1 | 2010-06-11 17:17:36 | [diff] [blame] | 10 | #include "chrome/common/pref_names.h" |
[email protected] | da0c8e9 | 2010-02-09 23:25:49 | [diff] [blame] | 11 | #include "chrome/common/url_constants.h" |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 12 | #include "chrome/test/testing_profile.h" |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 13 | #include "googleurl/src/gurl.h" |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | |
| 17 | namespace { |
| 18 | |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 19 | bool SettingsEqual(const ContentSettings& settings1, |
| 20 | const ContentSettings& settings2) { |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 21 | for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 22 | if (settings1.settings[i] != settings2.settings[i]) |
| 23 | return false; |
| 24 | } |
| 25 | return true; |
| 26 | } |
| 27 | |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 28 | class StubSettingsObserver : public NotificationObserver { |
| 29 | public: |
[email protected] | da0c8e9 | 2010-02-09 23:25:49 | [diff] [blame] | 30 | StubSettingsObserver() : last_notifier(NULL), counter(0) { |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 31 | registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED, |
[email protected] | da0c8e9 | 2010-02-09 23:25:49 | [diff] [blame] | 32 | NotificationService::AllSources()); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | virtual void Observe(NotificationType type, |
| 36 | const NotificationSource& source, |
| 37 | const NotificationDetails& details) { |
| 38 | ++counter; |
| 39 | Source<HostContentSettingsMap> content_settings(source); |
| 40 | Details<HostContentSettingsMap::ContentSettingsDetails> |
| 41 | settings_details(details); |
| 42 | last_notifier = content_settings.ptr(); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 43 | last_pattern = settings_details.ptr()->pattern(); |
| 44 | last_update_all = settings_details.ptr()->update_all(); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 45 | // This checks that calling a Get function from an observer doesn't |
| 46 | // deadlock. |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 47 | last_notifier->GetContentSettings(GURL("http://random-hostname.com/")); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | HostContentSettingsMap* last_notifier; |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 51 | HostContentSettingsMap::Pattern last_pattern; |
| 52 | bool last_update_all; |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 53 | int counter; |
| 54 | |
| 55 | private: |
| 56 | NotificationRegistrar registrar_; |
| 57 | }; |
| 58 | |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 59 | class HostContentSettingsMapTest : public testing::Test { |
| 60 | public: |
[email protected] | da0c8e9 | 2010-02-09 23:25:49 | [diff] [blame] | 61 | HostContentSettingsMapTest() : ui_thread_(ChromeThread::UI, &message_loop_) {} |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 62 | |
| 63 | protected: |
| 64 | MessageLoop message_loop_; |
| 65 | ChromeThread ui_thread_; |
| 66 | }; |
| 67 | |
| 68 | TEST_F(HostContentSettingsMapTest, DefaultValues) { |
| 69 | TestingProfile profile; |
| 70 | HostContentSettingsMap* host_content_settings_map = |
| 71 | profile.GetHostContentSettingsMap(); |
| 72 | |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 73 | // Check setting defaults. |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 74 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 75 | host_content_settings_map->GetDefaultContentSetting( |
| 76 | CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 77 | host_content_settings_map->SetDefaultContentSetting( |
| 78 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 79 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 80 | host_content_settings_map->GetDefaultContentSetting( |
| 81 | CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | da0c8e9 | 2010-02-09 23:25:49 | [diff] [blame] | 82 | EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( |
| 83 | GURL(chrome::kChromeUINewTabURL), |
| 84 | CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 85 | host_content_settings_map->SetDefaultContentSetting( |
| 86 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK); |
| 87 | EXPECT_EQ(CONTENT_SETTING_ASK, |
| 88 | host_content_settings_map->GetDefaultContentSetting( |
| 89 | CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 90 | host_content_settings_map->SetDefaultContentSetting( |
| 91 | CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); |
| 92 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 93 | host_content_settings_map->GetDefaultContentSetting( |
| 94 | CONTENT_SETTINGS_TYPE_POPUPS)); |
| 95 | host_content_settings_map->ResetToDefaults(); |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 96 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 97 | host_content_settings_map->GetDefaultContentSetting( |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 98 | CONTENT_SETTINGS_TYPE_PLUGINS)); |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 99 | |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 100 | // Check returning individual settings. |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 101 | GURL host("http://example.com/"); |
| 102 | HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 103 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 104 | host_content_settings_map->GetContentSetting( |
| 105 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 106 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 107 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 108 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 109 | host_content_settings_map->GetContentSetting( |
| 110 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 111 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 112 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 113 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 114 | host_content_settings_map->GetContentSetting( |
| 115 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | caef086 | 2010-02-16 20:44:08 | [diff] [blame] | 116 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 117 | host_content_settings_map->GetContentSetting( |
| 118 | host, CONTENT_SETTINGS_TYPE_PLUGINS)); |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 119 | |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 120 | // Check returning all settings for a host. |
| 121 | ContentSettings desired_settings; |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 122 | desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] = |
| 123 | CONTENT_SETTING_ALLOW; |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 124 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 125 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 126 | desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = |
| 127 | CONTENT_SETTING_ALLOW; |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 128 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 129 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); |
| 130 | desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = |
| 131 | CONTENT_SETTING_BLOCK; |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 132 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 133 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW); |
| 134 | desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = |
| 135 | CONTENT_SETTING_ALLOW; |
[email protected] | ceb98432 | 2010-01-30 10:02:16 | [diff] [blame] | 136 | desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS] = |
| 137 | CONTENT_SETTING_BLOCK; |
[email protected] | 4d5e7a6b | 2010-03-30 20:42:33 | [diff] [blame] | 138 | desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION] = |
| 139 | CONTENT_SETTING_ASK; |
[email protected] | a9bb272 | 2010-07-01 22:16:38 | [diff] [blame] | 140 | desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] = |
| 141 | CONTENT_SETTING_ASK; |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 142 | ContentSettings settings = |
| 143 | host_content_settings_map->GetContentSettings(host); |
| 144 | EXPECT_TRUE(SettingsEqual(desired_settings, settings)); |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 145 | |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 146 | // Check returning all hosts for a setting. |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 147 | HostContentSettingsMap::Pattern pattern2("[*.]example.org"); |
| 148 | host_content_settings_map->SetContentSetting(pattern2, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 149 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 150 | host_content_settings_map->SetContentSetting(pattern2, |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 151 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 152 | HostContentSettingsMap::SettingsForOneType host_settings; |
| 153 | host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, |
| 154 | &host_settings); |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 155 | EXPECT_EQ(1U, host_settings.size()); |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 156 | host_content_settings_map->GetSettingsForOneType( |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 157 | CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings); |
| 158 | EXPECT_EQ(2U, host_settings.size()); |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 159 | host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, |
| 160 | &host_settings); |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 161 | EXPECT_EQ(0U, host_settings.size()); |
| 162 | host_content_settings_map->ResetToDefaults(); |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 163 | host_content_settings_map->GetSettingsForOneType( |
[email protected] | 08bc630 | 2010-01-30 02:53:39 | [diff] [blame] | 164 | CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings); |
| 165 | EXPECT_EQ(0U, host_settings.size()); |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 166 | |
| 167 | // Check clearing one type. |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 168 | HostContentSettingsMap::Pattern pattern3("[*.]example.net"); |
| 169 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 170 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 171 | host_content_settings_map->SetContentSetting(pattern2, |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 172 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 173 | host_content_settings_map->SetContentSetting(pattern2, |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 174 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 175 | host_content_settings_map->SetContentSetting(pattern3, |
[email protected] | 3d1b7c7 | 2010-01-31 00:36:23 | [diff] [blame] | 176 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 177 | host_content_settings_map->ClearSettingsForOneType( |
| 178 | CONTENT_SETTINGS_TYPE_IMAGES); |
| 179 | host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, |
| 180 | &host_settings); |
| 181 | EXPECT_EQ(0U, host_settings.size()); |
| 182 | host_content_settings_map->GetSettingsForOneType( |
| 183 | CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings); |
| 184 | EXPECT_EQ(1U, host_settings.size()); |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 187 | TEST_F(HostContentSettingsMapTest, Patterns) { |
| 188 | TestingProfile profile; |
| 189 | HostContentSettingsMap* host_content_settings_map = |
| 190 | profile.GetHostContentSettingsMap(); |
| 191 | |
| 192 | GURL host1("http://example.com/"); |
| 193 | GURL host2("http://www.example.com/"); |
| 194 | GURL host3("http://example.org/"); |
| 195 | HostContentSettingsMap::Pattern pattern1("[*.]example.com"); |
| 196 | HostContentSettingsMap::Pattern pattern2("example.org"); |
| 197 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 198 | host_content_settings_map->GetContentSetting( |
| 199 | host1, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 200 | host_content_settings_map->SetContentSetting(pattern1, |
| 201 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 202 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 203 | host_content_settings_map->GetContentSetting( |
| 204 | host1, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 205 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 206 | host_content_settings_map->GetContentSetting( |
| 207 | host2, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 208 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 209 | host_content_settings_map->GetContentSetting( |
| 210 | host3, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 211 | host_content_settings_map->SetContentSetting(pattern2, |
| 212 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 213 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 214 | host_content_settings_map->GetContentSetting( |
| 215 | host3, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 216 | } |
| 217 | |
| 218 | TEST_F(HostContentSettingsMapTest, PatternSupport) { |
| 219 | EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").IsValid()); |
| 220 | EXPECT_TRUE(HostContentSettingsMap::Pattern("example.com").IsValid()); |
| 221 | EXPECT_TRUE(HostContentSettingsMap::Pattern("192.168.0.1").IsValid()); |
| 222 | EXPECT_TRUE(HostContentSettingsMap::Pattern("[::1]").IsValid()); |
| 223 | EXPECT_FALSE(HostContentSettingsMap::Pattern("*example.com").IsValid()); |
| 224 | EXPECT_FALSE(HostContentSettingsMap::Pattern("example.*").IsValid()); |
| 225 | EXPECT_FALSE(HostContentSettingsMap::Pattern("http://example.com").IsValid()); |
| 226 | |
| 227 | EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( |
| 228 | GURL("http://example.com/"))); |
| 229 | EXPECT_TRUE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( |
| 230 | GURL("http://www.example.com/"))); |
| 231 | EXPECT_TRUE(HostContentSettingsMap::Pattern("www.example.com").Matches( |
| 232 | GURL("http://www.example.com/"))); |
| 233 | EXPECT_FALSE(HostContentSettingsMap::Pattern("").Matches( |
| 234 | GURL("http://www.example.com/"))); |
| 235 | EXPECT_FALSE(HostContentSettingsMap::Pattern("[*.]example.com").Matches( |
| 236 | GURL("http://example.org/"))); |
| 237 | EXPECT_FALSE(HostContentSettingsMap::Pattern("example.com").Matches( |
| 238 | GURL("http://example.org/"))); |
| 239 | } |
| 240 | |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 241 | TEST_F(HostContentSettingsMapTest, Observer) { |
| 242 | TestingProfile profile; |
| 243 | HostContentSettingsMap* host_content_settings_map = |
| 244 | profile.GetHostContentSettingsMap(); |
| 245 | StubSettingsObserver observer; |
| 246 | |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 247 | HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 248 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 249 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_ALLOW); |
| 250 | EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 251 | EXPECT_EQ(pattern, observer.last_pattern); |
| 252 | EXPECT_FALSE(observer.last_update_all); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 253 | EXPECT_EQ(1, observer.counter); |
| 254 | |
| 255 | host_content_settings_map->ClearSettingsForOneType( |
| 256 | CONTENT_SETTINGS_TYPE_IMAGES); |
| 257 | EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 258 | EXPECT_TRUE(observer.last_update_all); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 259 | EXPECT_EQ(2, observer.counter); |
| 260 | |
| 261 | host_content_settings_map->ResetToDefaults(); |
| 262 | EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 263 | EXPECT_TRUE(observer.last_update_all); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 264 | EXPECT_EQ(3, observer.counter); |
| 265 | |
| 266 | host_content_settings_map->SetDefaultContentSetting( |
| 267 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 268 | EXPECT_EQ(host_content_settings_map, observer.last_notifier); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 269 | EXPECT_TRUE(observer.last_update_all); |
[email protected] | 79580c6 | 2010-02-02 02:36:25 | [diff] [blame] | 270 | EXPECT_EQ(4, observer.counter); |
| 271 | } |
| 272 | |
[email protected] | 0412aed1 | 2010-06-11 17:17:36 | [diff] [blame] | 273 | TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) { |
| 274 | TestingProfile profile; |
| 275 | HostContentSettingsMap* host_content_settings_map = |
| 276 | profile.GetHostContentSettingsMap(); |
| 277 | |
| 278 | PrefService* prefs = profile.GetPrefs(); |
| 279 | |
| 280 | // Make a copy of the default pref value so we can reset it later. |
| 281 | scoped_ptr<Value> default_value(prefs->FindPreference( |
| 282 | prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); |
| 283 | |
| 284 | GURL host("http://example.com"); |
| 285 | |
| 286 | host_content_settings_map->SetDefaultContentSetting( |
| 287 | CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
| 288 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 289 | host_content_settings_map->GetContentSetting( |
| 290 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 291 | |
| 292 | // Make a copy of the pref's new value so we can reset it later. |
| 293 | scoped_ptr<Value> new_value(prefs->FindPreference( |
| 294 | prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); |
| 295 | |
| 296 | // Clearing the backing pref should also clear the internal cache. |
| 297 | prefs->Set(prefs::kDefaultContentSettings, *default_value); |
| 298 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 299 | host_content_settings_map->GetContentSetting( |
| 300 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 301 | |
| 302 | // Reseting the pref to its previous value should update the cache. |
| 303 | prefs->Set(prefs::kDefaultContentSettings, *new_value); |
| 304 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 305 | host_content_settings_map->GetContentSetting( |
| 306 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 307 | } |
| 308 | |
| 309 | TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { |
| 310 | TestingProfile profile; |
| 311 | HostContentSettingsMap* host_content_settings_map = |
| 312 | profile.GetHostContentSettingsMap(); |
| 313 | |
| 314 | PrefService* prefs = profile.GetPrefs(); |
| 315 | |
| 316 | // Make a copy of the default pref value so we can reset it later. |
| 317 | scoped_ptr<Value> default_value(prefs->FindPreference( |
| 318 | prefs::kContentSettingsPatterns)->GetValue()->DeepCopy()); |
| 319 | |
| 320 | HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 321 | GURL host("http://example.com"); |
| 322 | |
| 323 | host_content_settings_map->SetContentSetting(pattern, |
| 324 | CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
| 325 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 326 | host_content_settings_map->GetContentSetting( |
| 327 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 328 | |
| 329 | // Make a copy of the pref's new value so we can reset it later. |
| 330 | scoped_ptr<Value> new_value(prefs->FindPreference( |
| 331 | prefs::kContentSettingsPatterns)->GetValue()->DeepCopy()); |
| 332 | |
| 333 | // Clearing the backing pref should also clear the internal cache. |
| 334 | prefs->Set(prefs::kContentSettingsPatterns, *default_value); |
| 335 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 336 | host_content_settings_map->GetContentSetting( |
| 337 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 338 | |
| 339 | // Reseting the pref to its previous value should update the cache. |
| 340 | prefs->Set(prefs::kContentSettingsPatterns, *new_value); |
| 341 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 342 | host_content_settings_map->GetContentSetting( |
| 343 | host, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 344 | } |
| 345 | |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 346 | TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { |
| 347 | TestingProfile profile; |
| 348 | HostContentSettingsMap* host_content_settings_map = |
| 349 | profile.GetHostContentSettingsMap(); |
| 350 | |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 351 | HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 352 | GURL host_ending_with_dot("http://example.com./"); |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 353 | |
| 354 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 355 | host_content_settings_map->GetContentSetting( |
| 356 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 357 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 358 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT); |
| 359 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 360 | host_content_settings_map->GetContentSetting( |
| 361 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 362 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 363 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 364 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 365 | host_content_settings_map->GetContentSetting( |
| 366 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 367 | |
| 368 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 369 | host_content_settings_map->GetContentSetting( |
| 370 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 371 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 372 | CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_DEFAULT); |
| 373 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 374 | host_content_settings_map->GetContentSetting( |
| 375 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 376 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 377 | CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
| 378 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 379 | host_content_settings_map->GetContentSetting( |
| 380 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_COOKIES)); |
| 381 | |
| 382 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 383 | host_content_settings_map->GetContentSetting( |
| 384 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 385 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 386 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_DEFAULT); |
| 387 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 388 | host_content_settings_map->GetContentSetting( |
| 389 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 390 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 391 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); |
| 392 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 393 | host_content_settings_map->GetContentSetting( |
| 394 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT)); |
| 395 | |
| 396 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 397 | host_content_settings_map->GetContentSetting( |
| 398 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 399 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 400 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DEFAULT); |
| 401 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 402 | host_content_settings_map->GetContentSetting( |
| 403 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 404 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 405 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); |
| 406 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 407 | host_content_settings_map->GetContentSetting( |
| 408 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 409 | |
| 410 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 411 | host_content_settings_map->GetContentSetting( |
| 412 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 413 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 414 | CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_DEFAULT); |
| 415 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 416 | host_content_settings_map->GetContentSetting( |
| 417 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS)); |
[email protected] | 0314ae0 | 2010-04-08 09:18:29 | [diff] [blame] | 418 | host_content_settings_map->SetContentSetting(pattern, |
[email protected] | 13c340e1 | 2010-03-08 19:29:41 | [diff] [blame] | 419 | CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW); |
| 420 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 421 | host_content_settings_map->GetContentSetting( |
| 422 | host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS)); |
| 423 | } |
| 424 | |
[email protected] | b9a4ea7f | 2010-06-25 07:11:23 | [diff] [blame] | 425 | TEST_F(HostContentSettingsMapTest, NestedSettings) { |
| 426 | TestingProfile profile; |
| 427 | HostContentSettingsMap* host_content_settings_map = |
| 428 | profile.GetHostContentSettingsMap(); |
| 429 | |
| 430 | GURL host("http://a.b.example.com/"); |
| 431 | HostContentSettingsMap::Pattern pattern1("[*.]example.com"); |
| 432 | HostContentSettingsMap::Pattern pattern2("[*.]b.example.com"); |
| 433 | HostContentSettingsMap::Pattern pattern3("a.b.example.com"); |
| 434 | |
| 435 | host_content_settings_map->SetContentSetting(pattern1, |
| 436 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 437 | host_content_settings_map->SetContentSetting(pattern2, |
| 438 | CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); |
| 439 | host_content_settings_map->SetContentSetting(pattern3, |
| 440 | CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); |
| 441 | host_content_settings_map->SetDefaultContentSetting( |
| 442 | CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); |
| 443 | |
| 444 | ContentSettings desired_settings; |
| 445 | desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] = |
| 446 | CONTENT_SETTING_BLOCK; |
| 447 | desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] = |
| 448 | CONTENT_SETTING_BLOCK; |
| 449 | desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = |
| 450 | CONTENT_SETTING_BLOCK; |
| 451 | desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = |
| 452 | CONTENT_SETTING_BLOCK; |
| 453 | desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS] = |
| 454 | CONTENT_SETTING_BLOCK; |
| 455 | desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION] = |
| 456 | CONTENT_SETTING_ASK; |
[email protected] | a9bb272 | 2010-07-01 22:16:38 | [diff] [blame] | 457 | desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] = |
| 458 | CONTENT_SETTING_ASK; |
[email protected] | b9a4ea7f | 2010-06-25 07:11:23 | [diff] [blame] | 459 | ContentSettings settings = |
| 460 | host_content_settings_map->GetContentSettings(host); |
| 461 | EXPECT_TRUE(SettingsEqual(desired_settings, settings)); |
| 462 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES], |
| 463 | settings.settings[CONTENT_SETTINGS_TYPE_COOKIES]); |
| 464 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES], |
| 465 | settings.settings[CONTENT_SETTINGS_TYPE_IMAGES]); |
| 466 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS], |
| 467 | settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS]); |
| 468 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS], |
| 469 | settings.settings[CONTENT_SETTINGS_TYPE_POPUPS]); |
| 470 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION], |
| 471 | settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION]); |
| 472 | EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES], |
| 473 | settings.settings[CONTENT_SETTINGS_TYPE_COOKIES]); |
| 474 | } |
| 475 | |
| 476 | TEST_F(HostContentSettingsMapTest, OffTheRecord) { |
| 477 | TestingProfile profile; |
| 478 | HostContentSettingsMap* host_content_settings_map = |
| 479 | profile.GetHostContentSettingsMap(); |
| 480 | profile.set_off_the_record(true); |
| 481 | scoped_refptr<HostContentSettingsMap> otr_map = |
| 482 | new HostContentSettingsMap(&profile); |
| 483 | profile.set_off_the_record(false); |
| 484 | |
| 485 | GURL host("http://example.com/"); |
| 486 | HostContentSettingsMap::Pattern pattern("[*.]example.com"); |
| 487 | |
| 488 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 489 | host_content_settings_map->GetContentSetting( |
| 490 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 491 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 492 | otr_map->GetContentSetting( |
| 493 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 494 | |
| 495 | // Changing content settings on the main map should also affect the |
| 496 | // off-the-record map. |
| 497 | host_content_settings_map->SetContentSetting(pattern, |
| 498 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); |
| 499 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 500 | host_content_settings_map->GetContentSetting( |
| 501 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 502 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 503 | otr_map->GetContentSetting( |
| 504 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 505 | |
| 506 | // Changing content settings on the off-the-record map should NOT affect the |
| 507 | // main map. |
| 508 | otr_map->SetContentSetting(pattern, |
| 509 | CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_ALLOW); |
| 510 | EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 511 | host_content_settings_map->GetContentSetting( |
| 512 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 513 | EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 514 | otr_map->GetContentSetting( |
| 515 | host, CONTENT_SETTINGS_TYPE_IMAGES)); |
| 516 | } |
| 517 | |
[email protected] | e284e6d8 | 2010-01-29 19:49:45 | [diff] [blame] | 518 | } // namespace |