Introduce ProfilerMetricsProvider
[chromium-blink-merge.git] / chrome / browser / content_settings / host_content_settings_map_unittest.cc
blob84b2b2a2e00603ef8141bf857e24afec43e68318
1 // Copyright (c) 2012 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.
5 #include "base/auto_reset.h"
6 #include "base/command_line.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/content_settings/content_settings_details.h"
13 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/content_settings/host_content_settings_map.h"
15 #include "chrome/browser/content_settings/mock_settings_observer.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/base/testing_pref_service_syncable.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "net/base/static_cookie_policy.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
26 using content::BrowserThread;
28 using ::testing::_;
30 class HostContentSettingsMapTest : public testing::Test {
31 public:
32 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
35 protected:
36 base::MessageLoop message_loop_;
37 content::TestBrowserThread ui_thread_;
40 TEST_F(HostContentSettingsMapTest, DefaultValues) {
41 TestingProfile profile;
42 HostContentSettingsMap* host_content_settings_map =
43 profile.GetHostContentSettingsMap();
45 // Check setting defaults.
46 EXPECT_EQ(CONTENT_SETTING_ALLOW,
47 host_content_settings_map->GetDefaultContentSetting(
48 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
49 host_content_settings_map->SetDefaultContentSetting(
50 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
51 EXPECT_EQ(CONTENT_SETTING_BLOCK,
52 host_content_settings_map->GetDefaultContentSetting(
53 CONTENT_SETTINGS_TYPE_IMAGES, NULL));
54 EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
55 GURL(chrome::kChromeUINewTabURL),
56 GURL(chrome::kChromeUINewTabURL),
57 CONTENT_SETTINGS_TYPE_IMAGES,
58 std::string()));
60 host_content_settings_map->SetDefaultContentSetting(
61 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
62 EXPECT_EQ(CONTENT_SETTING_ASK,
63 host_content_settings_map->GetDefaultContentSetting(
64 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
66 host_content_settings_map->SetDefaultContentSetting(
67 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
68 EXPECT_EQ(CONTENT_SETTING_ALLOW,
69 host_content_settings_map->GetDefaultContentSetting(
70 CONTENT_SETTINGS_TYPE_POPUPS, NULL));
73 TEST_F(HostContentSettingsMapTest, IndividualSettings) {
74 TestingProfile profile;
75 HostContentSettingsMap* host_content_settings_map =
76 profile.GetHostContentSettingsMap();
78 // Check returning individual settings.
79 GURL host("http://example.com/");
80 ContentSettingsPattern pattern =
81 ContentSettingsPattern::FromString("[*.]example.com");
82 EXPECT_EQ(CONTENT_SETTING_ALLOW,
83 host_content_settings_map->GetContentSetting(
84 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
85 host_content_settings_map->SetContentSetting(
86 pattern,
87 ContentSettingsPattern::Wildcard(),
88 CONTENT_SETTINGS_TYPE_IMAGES,
89 std::string(),
90 CONTENT_SETTING_DEFAULT);
91 EXPECT_EQ(CONTENT_SETTING_ALLOW,
92 host_content_settings_map->GetContentSetting(
93 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
94 host_content_settings_map->SetContentSetting(
95 pattern,
96 ContentSettingsPattern::Wildcard(),
97 CONTENT_SETTINGS_TYPE_IMAGES,
98 std::string(),
99 CONTENT_SETTING_BLOCK);
100 EXPECT_EQ(CONTENT_SETTING_BLOCK,
101 host_content_settings_map->GetContentSetting(
102 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
103 EXPECT_EQ(CONTENT_SETTING_ALLOW,
104 host_content_settings_map->GetContentSetting(
105 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
107 // Check returning all settings for a host.
108 host_content_settings_map->SetContentSetting(
109 pattern,
110 ContentSettingsPattern::Wildcard(),
111 CONTENT_SETTINGS_TYPE_IMAGES,
112 std::string(),
113 CONTENT_SETTING_DEFAULT);
114 EXPECT_EQ(CONTENT_SETTING_ALLOW,
115 host_content_settings_map->GetContentSetting(
116 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
117 host_content_settings_map->SetContentSetting(
118 pattern,
119 ContentSettingsPattern::Wildcard(),
120 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
121 std::string(),
122 CONTENT_SETTING_BLOCK);
123 EXPECT_EQ(CONTENT_SETTING_BLOCK,
124 host_content_settings_map->GetContentSetting(
125 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
126 host_content_settings_map->SetContentSetting(
127 pattern,
128 ContentSettingsPattern::Wildcard(),
129 CONTENT_SETTINGS_TYPE_PLUGINS,
130 std::string(),
131 CONTENT_SETTING_ALLOW);
132 EXPECT_EQ(CONTENT_SETTING_ALLOW,
133 host_content_settings_map->GetContentSetting(
134 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
135 EXPECT_EQ(CONTENT_SETTING_BLOCK,
136 host_content_settings_map->GetContentSetting(
137 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
138 EXPECT_EQ(CONTENT_SETTING_ASK,
139 host_content_settings_map->GetContentSetting(
140 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
141 EXPECT_EQ(
142 CONTENT_SETTING_ASK,
143 host_content_settings_map->GetContentSetting(
144 host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
145 EXPECT_EQ(CONTENT_SETTING_ASK,
146 host_content_settings_map->GetContentSetting(
147 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
148 EXPECT_EQ(CONTENT_SETTING_ASK,
149 host_content_settings_map->GetContentSetting(
150 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
152 // Check returning all hosts for a setting.
153 ContentSettingsPattern pattern2 =
154 ContentSettingsPattern::FromString("[*.]example.org");
155 host_content_settings_map->SetContentSetting(
156 pattern2,
157 ContentSettingsPattern::Wildcard(),
158 CONTENT_SETTINGS_TYPE_IMAGES,
159 std::string(),
160 CONTENT_SETTING_BLOCK);
161 host_content_settings_map->SetContentSetting(
162 pattern2,
163 ContentSettingsPattern::Wildcard(),
164 CONTENT_SETTINGS_TYPE_PLUGINS,
165 std::string(),
166 CONTENT_SETTING_BLOCK);
167 ContentSettingsForOneType host_settings;
168 host_content_settings_map->GetSettingsForOneType(
169 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
170 // |host_settings| contains the default setting and an exception.
171 EXPECT_EQ(2U, host_settings.size());
172 host_content_settings_map->GetSettingsForOneType(
173 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
174 // |host_settings| contains the default setting and 2 exceptions.
175 EXPECT_EQ(3U, host_settings.size());
176 host_content_settings_map->GetSettingsForOneType(
177 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings);
178 // |host_settings| contains only the default setting.
179 EXPECT_EQ(1U, host_settings.size());
182 TEST_F(HostContentSettingsMapTest, Clear) {
183 TestingProfile profile;
184 HostContentSettingsMap* host_content_settings_map =
185 profile.GetHostContentSettingsMap();
187 // Check clearing one type.
188 ContentSettingsPattern pattern =
189 ContentSettingsPattern::FromString("[*.]example.org");
190 ContentSettingsPattern pattern2 =
191 ContentSettingsPattern::FromString("[*.]example.net");
192 host_content_settings_map->SetContentSetting(
193 pattern2,
194 ContentSettingsPattern::Wildcard(),
195 CONTENT_SETTINGS_TYPE_IMAGES,
196 std::string(),
197 CONTENT_SETTING_BLOCK);
198 host_content_settings_map->SetContentSetting(
199 pattern,
200 ContentSettingsPattern::Wildcard(),
201 CONTENT_SETTINGS_TYPE_IMAGES,
202 std::string(),
203 CONTENT_SETTING_BLOCK);
204 host_content_settings_map->SetContentSetting(
205 pattern,
206 ContentSettingsPattern::Wildcard(),
207 CONTENT_SETTINGS_TYPE_PLUGINS,
208 std::string(),
209 CONTENT_SETTING_BLOCK);
210 host_content_settings_map->SetContentSetting(
211 pattern2,
212 ContentSettingsPattern::Wildcard(),
213 CONTENT_SETTINGS_TYPE_IMAGES,
214 std::string(),
215 CONTENT_SETTING_BLOCK);
216 host_content_settings_map->ClearSettingsForOneType(
217 CONTENT_SETTINGS_TYPE_IMAGES);
218 ContentSettingsForOneType host_settings;
219 host_content_settings_map->GetSettingsForOneType(
220 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
221 // |host_settings| contains only the default setting.
222 EXPECT_EQ(1U, host_settings.size());
223 host_content_settings_map->GetSettingsForOneType(
224 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
225 // |host_settings| contains the default setting and an exception.
226 EXPECT_EQ(2U, host_settings.size());
229 TEST_F(HostContentSettingsMapTest, Patterns) {
230 TestingProfile profile;
231 HostContentSettingsMap* host_content_settings_map =
232 profile.GetHostContentSettingsMap();
234 GURL host1("http://example.com/");
235 GURL host2("http://www.example.com/");
236 GURL host3("http://example.org/");
237 ContentSettingsPattern pattern1 =
238 ContentSettingsPattern::FromString("[*.]example.com");
239 ContentSettingsPattern pattern2 =
240 ContentSettingsPattern::FromString("example.org");
241 EXPECT_EQ(CONTENT_SETTING_ALLOW,
242 host_content_settings_map->GetContentSetting(
243 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
244 host_content_settings_map->SetContentSetting(
245 pattern1,
246 ContentSettingsPattern::Wildcard(),
247 CONTENT_SETTINGS_TYPE_IMAGES,
248 std::string(),
249 CONTENT_SETTING_BLOCK);
250 EXPECT_EQ(CONTENT_SETTING_BLOCK,
251 host_content_settings_map->GetContentSetting(
252 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
253 EXPECT_EQ(CONTENT_SETTING_BLOCK,
254 host_content_settings_map->GetContentSetting(
255 host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
256 EXPECT_EQ(CONTENT_SETTING_ALLOW,
257 host_content_settings_map->GetContentSetting(
258 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
259 host_content_settings_map->SetContentSetting(
260 pattern2,
261 ContentSettingsPattern::Wildcard(),
262 CONTENT_SETTINGS_TYPE_IMAGES,
263 std::string(),
264 CONTENT_SETTING_BLOCK);
265 EXPECT_EQ(CONTENT_SETTING_BLOCK,
266 host_content_settings_map->GetContentSetting(
267 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
270 TEST_F(HostContentSettingsMapTest, Observer) {
271 TestingProfile profile;
272 HostContentSettingsMap* host_content_settings_map =
273 profile.GetHostContentSettingsMap();
274 MockSettingsObserver observer;
276 ContentSettingsPattern primary_pattern =
277 ContentSettingsPattern::FromString("[*.]example.com");
278 ContentSettingsPattern secondary_pattern =
279 ContentSettingsPattern::Wildcard();
280 EXPECT_CALL(observer,
281 OnContentSettingsChanged(host_content_settings_map,
282 CONTENT_SETTINGS_TYPE_IMAGES,
283 false,
284 primary_pattern,
285 secondary_pattern,
286 false));
287 host_content_settings_map->SetContentSetting(
288 primary_pattern,
289 secondary_pattern,
290 CONTENT_SETTINGS_TYPE_IMAGES,
291 std::string(),
292 CONTENT_SETTING_ALLOW);
293 ::testing::Mock::VerifyAndClearExpectations(&observer);
295 EXPECT_CALL(observer,
296 OnContentSettingsChanged(host_content_settings_map,
297 CONTENT_SETTINGS_TYPE_IMAGES, false,
298 _, _, true));
299 host_content_settings_map->ClearSettingsForOneType(
300 CONTENT_SETTINGS_TYPE_IMAGES);
301 ::testing::Mock::VerifyAndClearExpectations(&observer);
303 EXPECT_CALL(observer,
304 OnContentSettingsChanged(host_content_settings_map,
305 CONTENT_SETTINGS_TYPE_IMAGES, false,
306 _, _, true));
307 host_content_settings_map->SetDefaultContentSetting(
308 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
311 TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
312 TestingProfile profile;
313 HostContentSettingsMap* host_content_settings_map =
314 profile.GetHostContentSettingsMap();
316 PrefService* prefs = profile.GetPrefs();
318 // Make a copy of the default pref value so we can reset it later.
319 scoped_ptr<base::Value> default_value(prefs->FindPreference(
320 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
322 GURL host("http://example.com");
324 host_content_settings_map->SetDefaultContentSetting(
325 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
326 EXPECT_EQ(CONTENT_SETTING_BLOCK,
327 host_content_settings_map->GetContentSetting(
328 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
330 // Make a copy of the pref's new value so we can reset it later.
331 scoped_ptr<base::Value> new_value(prefs->FindPreference(
332 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
334 // Clearing the backing pref should also clear the internal cache.
335 prefs->Set(prefs::kDefaultContentSettings, *default_value);
336 EXPECT_EQ(CONTENT_SETTING_ALLOW,
337 host_content_settings_map->GetContentSetting(
338 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
340 // Reseting the pref to its previous value should update the cache.
341 prefs->Set(prefs::kDefaultContentSettings, *new_value);
342 EXPECT_EQ(CONTENT_SETTING_BLOCK,
343 host_content_settings_map->GetContentSetting(
344 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
347 TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
348 TestingProfile profile;
349 HostContentSettingsMap* host_content_settings_map =
350 profile.GetHostContentSettingsMap();
352 PrefService* prefs = profile.GetPrefs();
354 // Make a copy of the default pref value so we can reset it later.
355 scoped_ptr<base::Value> default_value(prefs->FindPreference(
356 prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
358 ContentSettingsPattern pattern =
359 ContentSettingsPattern::FromString("[*.]example.com");
360 GURL host("http://example.com");
362 EXPECT_EQ(CONTENT_SETTING_ALLOW,
363 host_content_settings_map->GetContentSetting(
364 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
366 host_content_settings_map->SetContentSetting(
367 pattern,
368 ContentSettingsPattern::Wildcard(),
369 CONTENT_SETTINGS_TYPE_IMAGES,
370 std::string(),
371 CONTENT_SETTING_BLOCK);
372 EXPECT_EQ(CONTENT_SETTING_BLOCK,
373 host_content_settings_map->GetContentSetting(
374 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
376 // Make a copy of the pref's new value so we can reset it later.
377 scoped_ptr<base::Value> new_value(prefs->FindPreference(
378 prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
380 // Clearing the backing pref should also clear the internal cache.
381 prefs->Set(prefs::kContentSettingsPatternPairs, *default_value);
382 EXPECT_EQ(CONTENT_SETTING_ALLOW,
383 host_content_settings_map->GetContentSetting(
384 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
386 // Reseting the pref to its previous value should update the cache.
387 prefs->Set(prefs::kContentSettingsPatternPairs, *new_value);
388 EXPECT_EQ(CONTENT_SETTING_BLOCK,
389 host_content_settings_map->GetContentSetting(
390 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
393 TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
394 TestingProfile profile;
395 HostContentSettingsMap* host_content_settings_map =
396 profile.GetHostContentSettingsMap();
397 CookieSettings* cookie_settings =
398 CookieSettings::Factory::GetForProfile(&profile).get();
400 ContentSettingsPattern pattern =
401 ContentSettingsPattern::FromString("[*.]example.com");
402 GURL host_ending_with_dot("http://example.com./");
404 EXPECT_EQ(CONTENT_SETTING_ALLOW,
405 host_content_settings_map->GetContentSetting(
406 host_ending_with_dot,
407 host_ending_with_dot,
408 CONTENT_SETTINGS_TYPE_IMAGES,
409 std::string()));
410 host_content_settings_map->SetContentSetting(
411 pattern,
412 ContentSettingsPattern::Wildcard(),
413 CONTENT_SETTINGS_TYPE_IMAGES,
414 std::string(),
415 CONTENT_SETTING_DEFAULT);
416 EXPECT_EQ(
417 CONTENT_SETTING_ALLOW,
418 host_content_settings_map->GetContentSetting(host_ending_with_dot,
419 host_ending_with_dot,
420 CONTENT_SETTINGS_TYPE_IMAGES,
421 std::string()));
422 host_content_settings_map->SetContentSetting(
423 pattern,
424 ContentSettingsPattern::Wildcard(),
425 CONTENT_SETTINGS_TYPE_IMAGES,
426 std::string(),
427 CONTENT_SETTING_BLOCK);
428 EXPECT_EQ(
429 CONTENT_SETTING_BLOCK,
430 host_content_settings_map->GetContentSetting(host_ending_with_dot,
431 host_ending_with_dot,
432 CONTENT_SETTINGS_TYPE_IMAGES,
433 std::string()));
435 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
436 host_ending_with_dot, host_ending_with_dot));
437 host_content_settings_map->SetContentSetting(
438 pattern,
439 ContentSettingsPattern::Wildcard(),
440 CONTENT_SETTINGS_TYPE_COOKIES,
441 std::string(),
442 CONTENT_SETTING_DEFAULT);
443 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
444 host_ending_with_dot, host_ending_with_dot));
445 host_content_settings_map->SetContentSetting(
446 pattern,
447 ContentSettingsPattern::Wildcard(),
448 CONTENT_SETTINGS_TYPE_COOKIES,
449 std::string(),
450 CONTENT_SETTING_BLOCK);
451 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
452 host_ending_with_dot, host_ending_with_dot));
454 EXPECT_EQ(CONTENT_SETTING_ALLOW,
455 host_content_settings_map->GetContentSetting(
456 host_ending_with_dot,
457 host_ending_with_dot,
458 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
459 std::string()));
460 host_content_settings_map->SetContentSetting(
461 pattern,
462 ContentSettingsPattern::Wildcard(),
463 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
464 std::string(),
465 CONTENT_SETTING_DEFAULT);
466 EXPECT_EQ(CONTENT_SETTING_ALLOW,
467 host_content_settings_map->GetContentSetting(
468 host_ending_with_dot,
469 host_ending_with_dot,
470 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
471 std::string()));
472 host_content_settings_map->SetContentSetting(
473 pattern,
474 ContentSettingsPattern::Wildcard(),
475 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
476 std::string(),
477 CONTENT_SETTING_BLOCK);
478 EXPECT_EQ(CONTENT_SETTING_BLOCK,
479 host_content_settings_map->GetContentSetting(
480 host_ending_with_dot,
481 host_ending_with_dot,
482 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
483 std::string()));
485 EXPECT_EQ(CONTENT_SETTING_ALLOW,
486 host_content_settings_map->GetContentSetting(
487 host_ending_with_dot,
488 host_ending_with_dot,
489 CONTENT_SETTINGS_TYPE_PLUGINS,
490 std::string()));
491 host_content_settings_map->SetContentSetting(
492 pattern,
493 ContentSettingsPattern::Wildcard(),
494 CONTENT_SETTINGS_TYPE_PLUGINS,
495 std::string(),
496 CONTENT_SETTING_DEFAULT);
497 EXPECT_EQ(CONTENT_SETTING_ALLOW,
498 host_content_settings_map->GetContentSetting(
499 host_ending_with_dot,
500 host_ending_with_dot,
501 CONTENT_SETTINGS_TYPE_PLUGINS,
502 std::string()));
503 host_content_settings_map->SetContentSetting(
504 pattern,
505 ContentSettingsPattern::Wildcard(),
506 CONTENT_SETTINGS_TYPE_PLUGINS,
507 std::string(),
508 CONTENT_SETTING_BLOCK);
509 EXPECT_EQ(CONTENT_SETTING_BLOCK,
510 host_content_settings_map->GetContentSetting(
511 host_ending_with_dot,
512 host_ending_with_dot,
513 CONTENT_SETTINGS_TYPE_PLUGINS,
514 std::string()));
516 EXPECT_EQ(
517 CONTENT_SETTING_BLOCK,
518 host_content_settings_map->GetContentSetting(host_ending_with_dot,
519 host_ending_with_dot,
520 CONTENT_SETTINGS_TYPE_POPUPS,
521 std::string()));
522 host_content_settings_map->SetContentSetting(
523 pattern,
524 ContentSettingsPattern::Wildcard(),
525 CONTENT_SETTINGS_TYPE_POPUPS,
526 std::string(),
527 CONTENT_SETTING_DEFAULT);
528 EXPECT_EQ(
529 CONTENT_SETTING_BLOCK,
530 host_content_settings_map->GetContentSetting(host_ending_with_dot,
531 host_ending_with_dot,
532 CONTENT_SETTINGS_TYPE_POPUPS,
533 std::string()));
534 host_content_settings_map->SetContentSetting(
535 pattern,
536 ContentSettingsPattern::Wildcard(),
537 CONTENT_SETTINGS_TYPE_POPUPS,
538 std::string(),
539 CONTENT_SETTING_ALLOW);
540 EXPECT_EQ(
541 CONTENT_SETTING_ALLOW,
542 host_content_settings_map->GetContentSetting(host_ending_with_dot,
543 host_ending_with_dot,
544 CONTENT_SETTINGS_TYPE_POPUPS,
545 std::string()));
548 TEST_F(HostContentSettingsMapTest, NestedSettings) {
549 TestingProfile profile;
550 HostContentSettingsMap* host_content_settings_map =
551 profile.GetHostContentSettingsMap();
553 GURL host("http://a.b.example.com/");
554 ContentSettingsPattern pattern1 =
555 ContentSettingsPattern::FromString("[*.]example.com");
556 ContentSettingsPattern pattern2 =
557 ContentSettingsPattern::FromString("[*.]b.example.com");
558 ContentSettingsPattern pattern3 =
559 ContentSettingsPattern::FromString("a.b.example.com");
561 host_content_settings_map->SetContentSetting(
562 pattern1,
563 ContentSettingsPattern::Wildcard(),
564 CONTENT_SETTINGS_TYPE_IMAGES,
565 std::string(),
566 CONTENT_SETTING_BLOCK);
568 host_content_settings_map->SetContentSetting(
569 pattern2,
570 ContentSettingsPattern::Wildcard(),
571 CONTENT_SETTINGS_TYPE_COOKIES,
572 std::string(),
573 CONTENT_SETTING_BLOCK);
575 host_content_settings_map->SetContentSetting(
576 pattern3,
577 ContentSettingsPattern::Wildcard(),
578 CONTENT_SETTINGS_TYPE_PLUGINS,
579 std::string(),
580 CONTENT_SETTING_BLOCK);
581 host_content_settings_map->SetDefaultContentSetting(
582 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
584 EXPECT_EQ(CONTENT_SETTING_BLOCK,
585 host_content_settings_map->GetContentSetting(
586 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
587 EXPECT_EQ(CONTENT_SETTING_BLOCK,
588 host_content_settings_map->GetContentSetting(
589 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
590 EXPECT_EQ(CONTENT_SETTING_BLOCK,
591 host_content_settings_map->GetContentSetting(
592 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
593 EXPECT_EQ(CONTENT_SETTING_BLOCK,
594 host_content_settings_map->GetContentSetting(
595 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
596 EXPECT_EQ(CONTENT_SETTING_BLOCK,
597 host_content_settings_map->GetContentSetting(
598 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
599 EXPECT_EQ(CONTENT_SETTING_ASK,
600 host_content_settings_map->GetContentSetting(
601 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
602 EXPECT_EQ(
603 CONTENT_SETTING_ASK,
604 host_content_settings_map->GetContentSetting(
605 host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
606 EXPECT_EQ(CONTENT_SETTING_ASK,
607 host_content_settings_map->GetContentSetting(
608 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
609 EXPECT_EQ(CONTENT_SETTING_ASK,
610 host_content_settings_map->GetContentSetting(
611 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
614 TEST_F(HostContentSettingsMapTest, OffTheRecord) {
615 TestingProfile profile;
616 HostContentSettingsMap* host_content_settings_map =
617 profile.GetHostContentSettingsMap();
618 scoped_refptr<HostContentSettingsMap> otr_map(
619 new HostContentSettingsMap(profile.GetPrefs(),
620 true));
622 GURL host("http://example.com/");
623 ContentSettingsPattern pattern =
624 ContentSettingsPattern::FromString("[*.]example.com");
626 EXPECT_EQ(CONTENT_SETTING_ALLOW,
627 host_content_settings_map->GetContentSetting(
628 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
629 EXPECT_EQ(CONTENT_SETTING_ALLOW,
630 otr_map->GetContentSetting(
631 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
633 // Changing content settings on the main map should also affect the
634 // incognito map.
635 host_content_settings_map->SetContentSetting(
636 pattern,
637 ContentSettingsPattern::Wildcard(),
638 CONTENT_SETTINGS_TYPE_IMAGES,
639 std::string(),
640 CONTENT_SETTING_BLOCK);
641 EXPECT_EQ(CONTENT_SETTING_BLOCK,
642 host_content_settings_map->GetContentSetting(
643 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
644 EXPECT_EQ(CONTENT_SETTING_BLOCK,
645 otr_map->GetContentSetting(
646 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
648 // Changing content settings on the incognito map should NOT affect the
649 // main map.
650 otr_map->SetContentSetting(pattern,
651 ContentSettingsPattern::Wildcard(),
652 CONTENT_SETTINGS_TYPE_IMAGES,
653 std::string(),
654 CONTENT_SETTING_ALLOW);
655 EXPECT_EQ(CONTENT_SETTING_BLOCK,
656 host_content_settings_map->GetContentSetting(
657 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
658 EXPECT_EQ(CONTENT_SETTING_ALLOW,
659 otr_map->GetContentSetting(
660 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
662 otr_map->ShutdownOnUIThread();
665 // For a single Unicode encoded pattern, check if it gets converted to punycode
666 // and old pattern gets deleted.
667 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
668 TestingProfile profile;
669 PrefService* prefs = profile.GetPrefs();
671 // Set utf-8 data.
673 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatternPairs);
674 base::DictionaryValue* all_settings_dictionary = update.Get();
675 ASSERT_TRUE(NULL != all_settings_dictionary);
677 base::DictionaryValue* dummy_payload = new base::DictionaryValue;
678 dummy_payload->SetInteger("images", CONTENT_SETTING_ALLOW);
679 all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*",
680 dummy_payload);
682 profile.GetHostContentSettingsMap();
684 const base::DictionaryValue* all_settings_dictionary =
685 prefs->GetDictionary(prefs::kContentSettingsPatternPairs);
686 const base::DictionaryValue* result = NULL;
687 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
688 "[*.]\xC4\x87ira.com,*", &result));
689 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
690 "[*.]xn--ira-ppa.com,*", &result));
693 // If both Unicode and its punycode pattern exist, make sure we don't touch the
694 // settings for the punycode, and that Unicode pattern gets deleted.
695 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
696 TestingProfile profile;
698 scoped_ptr<base::Value> value(base::JSONReader::Read(
699 "{\"[*.]\\xC4\\x87ira.com,*\":{\"images\":1}}"));
700 profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *value);
702 // Set punycode equivalent, with different setting.
703 scoped_ptr<base::Value> puny_value(base::JSONReader::Read(
704 "{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}"));
705 profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *puny_value);
707 // Initialize the content map.
708 profile.GetHostContentSettingsMap();
710 const base::DictionaryValue* content_setting_prefs =
711 profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatternPairs);
712 std::string prefs_as_json;
713 base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
714 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}",
715 prefs_as_json.c_str());
718 // If a default-content-setting is managed, the managed value should be used
719 // instead of the default value.
720 TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
721 TestingProfile profile;
722 HostContentSettingsMap* host_content_settings_map =
723 profile.GetHostContentSettingsMap();
724 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
726 EXPECT_EQ(CONTENT_SETTING_ALLOW,
727 host_content_settings_map->GetDefaultContentSetting(
728 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
730 // Set managed-default-content-setting through the coresponding preferences.
731 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
732 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
733 EXPECT_EQ(CONTENT_SETTING_BLOCK,
734 host_content_settings_map->GetDefaultContentSetting(
735 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
737 // Remove managed-default-content-settings-preferences.
738 prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
739 EXPECT_EQ(CONTENT_SETTING_ALLOW,
740 host_content_settings_map->GetDefaultContentSetting(
741 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
743 // Set preference to manage the default-content-setting for Plugins.
744 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
745 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
746 EXPECT_EQ(CONTENT_SETTING_BLOCK,
747 host_content_settings_map->GetDefaultContentSetting(
748 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
750 // Remove the preference to manage the default-content-setting for Plugins.
751 prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
752 EXPECT_EQ(CONTENT_SETTING_ALLOW,
753 host_content_settings_map->GetDefaultContentSetting(
754 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
757 TEST_F(HostContentSettingsMapTest,
758 GetNonDefaultContentSettingsIfTypeManaged) {
759 TestingProfile profile;
760 HostContentSettingsMap* host_content_settings_map =
761 profile.GetHostContentSettingsMap();
762 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
764 // Set pattern for JavaScript setting.
765 ContentSettingsPattern pattern =
766 ContentSettingsPattern::FromString("[*.]example.com");
767 host_content_settings_map->SetContentSetting(
768 pattern,
769 ContentSettingsPattern::Wildcard(),
770 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
771 std::string(),
772 CONTENT_SETTING_BLOCK);
774 EXPECT_EQ(CONTENT_SETTING_ALLOW,
775 host_content_settings_map->GetDefaultContentSetting(
776 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
778 GURL host("http://example.com/");
779 EXPECT_EQ(CONTENT_SETTING_BLOCK,
780 host_content_settings_map->GetContentSetting(
781 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
783 // Set managed-default-content-setting for content-settings-type JavaScript.
784 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
785 base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
786 EXPECT_EQ(CONTENT_SETTING_ALLOW,
787 host_content_settings_map->GetContentSetting(
788 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
791 // Managed default content setting should have higher priority
792 // than user defined patterns.
793 TEST_F(HostContentSettingsMapTest,
794 ManagedDefaultContentSettingIgnoreUserPattern) {
795 TestingProfile profile;
796 HostContentSettingsMap* host_content_settings_map =
797 profile.GetHostContentSettingsMap();
798 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
800 // Block all JavaScript.
801 host_content_settings_map->SetDefaultContentSetting(
802 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
804 // Set an exception to allow "[*.]example.com"
805 ContentSettingsPattern pattern =
806 ContentSettingsPattern::FromString("[*.]example.com");
808 host_content_settings_map->SetContentSetting(
809 pattern,
810 ContentSettingsPattern::Wildcard(),
811 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
812 std::string(),
813 CONTENT_SETTING_ALLOW);
815 EXPECT_EQ(CONTENT_SETTING_BLOCK,
816 host_content_settings_map->GetDefaultContentSetting(
817 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
818 GURL host("http://example.com/");
819 EXPECT_EQ(CONTENT_SETTING_ALLOW,
820 host_content_settings_map->GetContentSetting(
821 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
823 // Set managed-default-content-settings-preferences.
824 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
825 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
826 EXPECT_EQ(CONTENT_SETTING_BLOCK,
827 host_content_settings_map->GetContentSetting(
828 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
830 // Remove managed-default-content-settings-preferences.
831 prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
832 EXPECT_EQ(CONTENT_SETTING_ALLOW,
833 host_content_settings_map->GetContentSetting(
834 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
837 // If a default-content-setting is set to managed setting, the user defined
838 // setting should be preserved.
839 TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
840 TestingProfile profile;
841 HostContentSettingsMap* host_content_settings_map =
842 profile.GetHostContentSettingsMap();
843 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
845 // Set user defined default-content-setting for Cookies.
846 host_content_settings_map->SetDefaultContentSetting(
847 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
848 EXPECT_EQ(CONTENT_SETTING_BLOCK,
849 host_content_settings_map->GetDefaultContentSetting(
850 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
852 // Set preference to manage the default-content-setting for Cookies.
853 prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
854 base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
855 EXPECT_EQ(CONTENT_SETTING_ALLOW,
856 host_content_settings_map->GetDefaultContentSetting(
857 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
859 // Remove the preference to manage the default-content-setting for Cookies.
860 prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
861 EXPECT_EQ(CONTENT_SETTING_BLOCK,
862 host_content_settings_map->GetDefaultContentSetting(
863 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
866 // If a setting for a default-content-setting-type is set while the type is
867 // managed, then the new setting should be preserved and used after the
868 // default-content-setting-type is not managed anymore.
869 TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
870 TestingProfile profile;
871 HostContentSettingsMap* host_content_settings_map =
872 profile.GetHostContentSettingsMap();
873 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
875 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
876 base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
877 EXPECT_EQ(CONTENT_SETTING_ALLOW,
878 host_content_settings_map->GetDefaultContentSetting(
879 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
881 host_content_settings_map->SetDefaultContentSetting(
882 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
883 EXPECT_EQ(CONTENT_SETTING_ALLOW,
884 host_content_settings_map->GetDefaultContentSetting(
885 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
887 prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
888 EXPECT_EQ(CONTENT_SETTING_BLOCK,
889 host_content_settings_map->GetDefaultContentSetting(
890 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
893 TEST_F(HostContentSettingsMapTest, GetContentSetting) {
894 TestingProfile profile;
895 HostContentSettingsMap* host_content_settings_map =
896 profile.GetHostContentSettingsMap();
898 GURL host("http://example.com/");
899 GURL embedder("chrome://foo");
900 ContentSettingsPattern pattern =
901 ContentSettingsPattern::FromString("[*.]example.com");
902 host_content_settings_map->SetContentSetting(
903 pattern,
904 ContentSettingsPattern::Wildcard(),
905 CONTENT_SETTINGS_TYPE_IMAGES,
906 std::string(),
907 CONTENT_SETTING_BLOCK);
908 EXPECT_EQ(CONTENT_SETTING_BLOCK,
909 host_content_settings_map->GetContentSetting(
910 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
911 EXPECT_EQ(CONTENT_SETTING_ALLOW,
912 host_content_settings_map->GetContentSetting(
913 embedder, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
916 TEST_F(HostContentSettingsMapTest, ShouldAllowAllContent) {
917 TestingProfile profile;
918 HostContentSettingsMap* host_content_settings_map =
919 profile.GetHostContentSettingsMap();
921 GURL http_host("http://example.com/");
922 GURL https_host("https://example.com/");
923 GURL embedder("chrome://foo");
924 GURL extension("chrome-extension://foo");
925 EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
926 http_host, embedder, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
927 EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
928 http_host, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION));
929 EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
930 http_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
931 EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
932 https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
933 EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
934 https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
935 EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
936 embedder, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
937 EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
938 extension, extension, CONTENT_SETTINGS_TYPE_COOKIES));
939 EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
940 extension, extension, CONTENT_SETTINGS_TYPE_PLUGINS));
941 EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
942 extension, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
945 TEST_F(HostContentSettingsMapTest, MigrateClearOnExit) {
946 TestingProfile profile;
947 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
949 prefs->SetBoolean(prefs::kClearSiteDataOnExit, true);
951 scoped_ptr<base::Value> patterns(base::JSONReader::Read(
952 "{\"[*.]example.com,*\":{\"cookies\": 1},"
953 " \"[*.]other.com,*\":{\"cookies\": 2},"
954 " \"[*.]third.com,*\":{\"cookies\": 4}}"));
955 profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *patterns);
957 scoped_ptr<base::Value> defaults(base::JSONReader::Read("{\"cookies\": 1}"));
958 profile.GetPrefs()->Set(prefs::kDefaultContentSettings, *defaults);
960 HostContentSettingsMap* host_content_settings_map =
961 profile.GetHostContentSettingsMap();
963 EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
964 host_content_settings_map->GetDefaultContentSetting(
965 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
966 EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
967 host_content_settings_map->GetContentSetting(
968 GURL("http://example.com"),
969 GURL("http://example.com"),
970 CONTENT_SETTINGS_TYPE_COOKIES,
971 std::string()));
972 EXPECT_EQ(CONTENT_SETTING_BLOCK,
973 host_content_settings_map->GetContentSetting(
974 GURL("http://other.com"),
975 GURL("http://other.com"),
976 CONTENT_SETTINGS_TYPE_COOKIES,
977 std::string()));
978 EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
979 host_content_settings_map->GetContentSetting(
980 GURL("http://third.com"),
981 GURL("http://third.com"),
982 CONTENT_SETTINGS_TYPE_COOKIES,
983 std::string()));