blob: 108d26e7aeb53cb51d6013b82a3c19993ec809d5 [file] [log] [blame]
[email protected]e284e6d82010-01-29 19:49:451// 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]0412aed12010-06-11 17:17:367#include "chrome/browser/pref_service.h"
[email protected]79580c62010-02-02 02:36:258#include "chrome/common/notification_registrar.h"
9#include "chrome/common/notification_service.h"
[email protected]0412aed12010-06-11 17:17:3610#include "chrome/common/pref_names.h"
[email protected]da0c8e92010-02-09 23:25:4911#include "chrome/common/url_constants.h"
[email protected]e284e6d82010-01-29 19:49:4512#include "chrome/test/testing_profile.h"
[email protected]0314ae02010-04-08 09:18:2913#include "googleurl/src/gurl.h"
[email protected]e284e6d82010-01-29 19:49:4514#include "testing/gtest/include/gtest/gtest.h"
15
16
17namespace {
18
[email protected]08bc6302010-01-30 02:53:3919bool SettingsEqual(const ContentSettings& settings1,
20 const ContentSettings& settings2) {
[email protected]ceb984322010-01-30 10:02:1621 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
[email protected]08bc6302010-01-30 02:53:3922 if (settings1.settings[i] != settings2.settings[i])
23 return false;
24 }
25 return true;
26}
27
[email protected]79580c62010-02-02 02:36:2528class StubSettingsObserver : public NotificationObserver {
29 public:
[email protected]da0c8e92010-02-09 23:25:4930 StubSettingsObserver() : last_notifier(NULL), counter(0) {
[email protected]79580c62010-02-02 02:36:2531 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
[email protected]da0c8e92010-02-09 23:25:4932 NotificationService::AllSources());
[email protected]79580c62010-02-02 02:36:2533 }
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]0314ae02010-04-08 09:18:2943 last_pattern = settings_details.ptr()->pattern();
44 last_update_all = settings_details.ptr()->update_all();
[email protected]79580c62010-02-02 02:36:2545 // This checks that calling a Get function from an observer doesn't
46 // deadlock.
[email protected]0314ae02010-04-08 09:18:2947 last_notifier->GetContentSettings(GURL("http://random-hostname.com/"));
[email protected]79580c62010-02-02 02:36:2548 }
49
50 HostContentSettingsMap* last_notifier;
[email protected]0314ae02010-04-08 09:18:2951 HostContentSettingsMap::Pattern last_pattern;
52 bool last_update_all;
[email protected]79580c62010-02-02 02:36:2553 int counter;
54
55 private:
56 NotificationRegistrar registrar_;
57};
58
[email protected]e284e6d82010-01-29 19:49:4559class HostContentSettingsMapTest : public testing::Test {
60 public:
[email protected]da0c8e92010-02-09 23:25:4961 HostContentSettingsMapTest() : ui_thread_(ChromeThread::UI, &message_loop_) {}
[email protected]e284e6d82010-01-29 19:49:4562
63 protected:
64 MessageLoop message_loop_;
65 ChromeThread ui_thread_;
66};
67
68TEST_F(HostContentSettingsMapTest, DefaultValues) {
69 TestingProfile profile;
70 HostContentSettingsMap* host_content_settings_map =
71 profile.GetHostContentSettingsMap();
72
[email protected]08bc6302010-01-30 02:53:3973 // Check setting defaults.
[email protected]ceb984322010-01-30 10:02:1674 EXPECT_EQ(CONTENT_SETTING_ALLOW,
[email protected]08bc6302010-01-30 02:53:3975 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]da0c8e92010-02-09 23:25:4982 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
83 GURL(chrome::kChromeUINewTabURL),
84 CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]08bc6302010-01-30 02:53:3985 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]ceb984322010-01-30 10:02:1696 EXPECT_EQ(CONTENT_SETTING_ALLOW,
[email protected]08bc6302010-01-30 02:53:3997 host_content_settings_map->GetDefaultContentSetting(
[email protected]ceb984322010-01-30 10:02:1698 CONTENT_SETTINGS_TYPE_PLUGINS));
[email protected]e284e6d82010-01-29 19:49:4599
[email protected]08bc6302010-01-30 02:53:39100 // Check returning individual settings.
[email protected]0314ae02010-04-08 09:18:29101 GURL host("http://example.com/");
102 HostContentSettingsMap::Pattern pattern("[*.]example.com");
[email protected]ceb984322010-01-30 10:02:16103 EXPECT_EQ(CONTENT_SETTING_ALLOW,
[email protected]08bc6302010-01-30 02:53:39104 host_content_settings_map->GetContentSetting(
105 host, CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]0314ae02010-04-08 09:18:29106 host_content_settings_map->SetContentSetting(pattern,
[email protected]08bc6302010-01-30 02:53:39107 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT);
[email protected]ceb984322010-01-30 10:02:16108 EXPECT_EQ(CONTENT_SETTING_ALLOW,
[email protected]08bc6302010-01-30 02:53:39109 host_content_settings_map->GetContentSetting(
110 host, CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]0314ae02010-04-08 09:18:29111 host_content_settings_map->SetContentSetting(pattern,
[email protected]08bc6302010-01-30 02:53:39112 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]caef0862010-02-16 20:44:08116 EXPECT_EQ(CONTENT_SETTING_ALLOW,
117 host_content_settings_map->GetContentSetting(
118 host, CONTENT_SETTINGS_TYPE_PLUGINS));
[email protected]e284e6d82010-01-29 19:49:45119
[email protected]08bc6302010-01-30 02:53:39120 // Check returning all settings for a host.
121 ContentSettings desired_settings;
[email protected]ceb984322010-01-30 10:02:16122 desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES] =
123 CONTENT_SETTING_ALLOW;
[email protected]0314ae02010-04-08 09:18:29124 host_content_settings_map->SetContentSetting(pattern,
[email protected]08bc6302010-01-30 02:53:39125 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT);
[email protected]ceb984322010-01-30 10:02:16126 desired_settings.settings[CONTENT_SETTINGS_TYPE_IMAGES] =
127 CONTENT_SETTING_ALLOW;
[email protected]0314ae02010-04-08 09:18:29128 host_content_settings_map->SetContentSetting(pattern,
[email protected]ceb984322010-01-30 10:02:16129 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
130 desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
131 CONTENT_SETTING_BLOCK;
[email protected]0314ae02010-04-08 09:18:29132 host_content_settings_map->SetContentSetting(pattern,
[email protected]08bc6302010-01-30 02:53:39133 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
134 desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
135 CONTENT_SETTING_ALLOW;
[email protected]ceb984322010-01-30 10:02:16136 desired_settings.settings[CONTENT_SETTINGS_TYPE_POPUPS] =
137 CONTENT_SETTING_BLOCK;
[email protected]4d5e7a6b2010-03-30 20:42:33138 desired_settings.settings[CONTENT_SETTINGS_TYPE_GEOLOCATION] =
139 CONTENT_SETTING_ASK;
[email protected]a9bb2722010-07-01 22:16:38140 desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] =
141 CONTENT_SETTING_ASK;
[email protected]08bc6302010-01-30 02:53:39142 ContentSettings settings =
143 host_content_settings_map->GetContentSettings(host);
144 EXPECT_TRUE(SettingsEqual(desired_settings, settings));
[email protected]e284e6d82010-01-29 19:49:45145
[email protected]08bc6302010-01-30 02:53:39146 // Check returning all hosts for a setting.
[email protected]0314ae02010-04-08 09:18:29147 HostContentSettingsMap::Pattern pattern2("[*.]example.org");
148 host_content_settings_map->SetContentSetting(pattern2,
[email protected]08bc6302010-01-30 02:53:39149 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
[email protected]0314ae02010-04-08 09:18:29150 host_content_settings_map->SetContentSetting(pattern2,
[email protected]08bc6302010-01-30 02:53:39151 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
[email protected]3d1b7c72010-01-31 00:36:23152 HostContentSettingsMap::SettingsForOneType host_settings;
153 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES,
154 &host_settings);
[email protected]08bc6302010-01-30 02:53:39155 EXPECT_EQ(1U, host_settings.size());
[email protected]3d1b7c72010-01-31 00:36:23156 host_content_settings_map->GetSettingsForOneType(
[email protected]08bc6302010-01-30 02:53:39157 CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings);
158 EXPECT_EQ(2U, host_settings.size());
[email protected]3d1b7c72010-01-31 00:36:23159 host_content_settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS,
160 &host_settings);
[email protected]08bc6302010-01-30 02:53:39161 EXPECT_EQ(0U, host_settings.size());
162 host_content_settings_map->ResetToDefaults();
[email protected]3d1b7c72010-01-31 00:36:23163 host_content_settings_map->GetSettingsForOneType(
[email protected]08bc6302010-01-30 02:53:39164 CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings);
165 EXPECT_EQ(0U, host_settings.size());
[email protected]3d1b7c72010-01-31 00:36:23166
167 // Check clearing one type.
[email protected]0314ae02010-04-08 09:18:29168 HostContentSettingsMap::Pattern pattern3("[*.]example.net");
169 host_content_settings_map->SetContentSetting(pattern,
[email protected]3d1b7c72010-01-31 00:36:23170 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
[email protected]0314ae02010-04-08 09:18:29171 host_content_settings_map->SetContentSetting(pattern2,
[email protected]3d1b7c72010-01-31 00:36:23172 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
[email protected]0314ae02010-04-08 09:18:29173 host_content_settings_map->SetContentSetting(pattern2,
[email protected]3d1b7c72010-01-31 00:36:23174 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
[email protected]0314ae02010-04-08 09:18:29175 host_content_settings_map->SetContentSetting(pattern3,
[email protected]3d1b7c72010-01-31 00:36:23176 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]e284e6d82010-01-29 19:49:45185}
186
[email protected]0314ae02010-04-08 09:18:29187TEST_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
218TEST_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]79580c62010-02-02 02:36:25241TEST_F(HostContentSettingsMapTest, Observer) {
242 TestingProfile profile;
243 HostContentSettingsMap* host_content_settings_map =
244 profile.GetHostContentSettingsMap();
245 StubSettingsObserver observer;
246
[email protected]0314ae02010-04-08 09:18:29247 HostContentSettingsMap::Pattern pattern("[*.]example.com");
248 host_content_settings_map->SetContentSetting(pattern,
[email protected]79580c62010-02-02 02:36:25249 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_ALLOW);
250 EXPECT_EQ(host_content_settings_map, observer.last_notifier);
[email protected]0314ae02010-04-08 09:18:29251 EXPECT_EQ(pattern, observer.last_pattern);
252 EXPECT_FALSE(observer.last_update_all);
[email protected]79580c62010-02-02 02:36:25253 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]0314ae02010-04-08 09:18:29258 EXPECT_TRUE(observer.last_update_all);
[email protected]79580c62010-02-02 02:36:25259 EXPECT_EQ(2, observer.counter);
260
261 host_content_settings_map->ResetToDefaults();
262 EXPECT_EQ(host_content_settings_map, observer.last_notifier);
[email protected]0314ae02010-04-08 09:18:29263 EXPECT_TRUE(observer.last_update_all);
[email protected]79580c62010-02-02 02:36:25264 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]0314ae02010-04-08 09:18:29269 EXPECT_TRUE(observer.last_update_all);
[email protected]79580c62010-02-02 02:36:25270 EXPECT_EQ(4, observer.counter);
271}
272
[email protected]0412aed12010-06-11 17:17:36273TEST_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
309TEST_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]13c340e12010-03-08 19:29:41346TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
347 TestingProfile profile;
348 HostContentSettingsMap* host_content_settings_map =
349 profile.GetHostContentSettingsMap();
350
[email protected]0314ae02010-04-08 09:18:29351 HostContentSettingsMap::Pattern pattern("[*.]example.com");
352 GURL host_ending_with_dot("http://example.com./");
[email protected]13c340e12010-03-08 19:29:41353
354 EXPECT_EQ(CONTENT_SETTING_ALLOW,
355 host_content_settings_map->GetContentSetting(
356 host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES));
[email protected]0314ae02010-04-08 09:18:29357 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41358 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]0314ae02010-04-08 09:18:29362 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41363 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]0314ae02010-04-08 09:18:29371 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41372 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]0314ae02010-04-08 09:18:29376 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41377 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]0314ae02010-04-08 09:18:29385 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41386 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]0314ae02010-04-08 09:18:29390 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41391 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]0314ae02010-04-08 09:18:29399 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41400 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]0314ae02010-04-08 09:18:29404 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41405 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]0314ae02010-04-08 09:18:29413 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41414 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]0314ae02010-04-08 09:18:29418 host_content_settings_map->SetContentSetting(pattern,
[email protected]13c340e12010-03-08 19:29:41419 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]b9a4ea7f2010-06-25 07:11:23425TEST_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]a9bb2722010-07-01 22:16:38457 desired_settings.settings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS] =
458 CONTENT_SETTING_ASK;
[email protected]b9a4ea7f2010-06-25 07:11:23459 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
476TEST_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]e284e6d82010-01-29 19:49:45518} // namespace