Prepare for VS2015 toolchain
[chromium-blink-merge.git] / chrome / browser / content_settings / host_content_settings_map_unittest.cc
blobe862023892facb8f32a5744ebfd407d037429d22
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_mock_observer.h"
13 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/content_settings/mock_settings_observer.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/content_settings/core/browser/content_settings_details.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.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_ALLOW);
62 EXPECT_EQ(CONTENT_SETTING_ALLOW,
63 host_content_settings_map->GetDefaultContentSetting(
64 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
65 host_content_settings_map->SetDefaultContentSetting(
66 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
67 EXPECT_EQ(CONTENT_SETTING_BLOCK,
68 host_content_settings_map->GetDefaultContentSetting(
69 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
70 host_content_settings_map->SetDefaultContentSetting(
71 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT);
72 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT,
73 host_content_settings_map->GetDefaultContentSetting(
74 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
76 host_content_settings_map->SetDefaultContentSetting(
77 CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
78 EXPECT_EQ(CONTENT_SETTING_ALLOW,
79 host_content_settings_map->GetDefaultContentSetting(
80 CONTENT_SETTINGS_TYPE_POPUPS, NULL));
83 TEST_F(HostContentSettingsMapTest, IndividualSettings) {
84 TestingProfile profile;
85 HostContentSettingsMap* host_content_settings_map =
86 profile.GetHostContentSettingsMap();
88 // Check returning individual settings.
89 GURL host("http://example.com/");
90 ContentSettingsPattern pattern =
91 ContentSettingsPattern::FromString("[*.]example.com");
92 EXPECT_EQ(CONTENT_SETTING_ALLOW,
93 host_content_settings_map->GetContentSetting(
94 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
95 host_content_settings_map->SetContentSetting(
96 pattern,
97 ContentSettingsPattern::Wildcard(),
98 CONTENT_SETTINGS_TYPE_IMAGES,
99 std::string(),
100 CONTENT_SETTING_DEFAULT);
101 EXPECT_EQ(CONTENT_SETTING_ALLOW,
102 host_content_settings_map->GetContentSetting(
103 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
104 host_content_settings_map->SetContentSetting(
105 pattern,
106 ContentSettingsPattern::Wildcard(),
107 CONTENT_SETTINGS_TYPE_IMAGES,
108 std::string(),
109 CONTENT_SETTING_BLOCK);
110 EXPECT_EQ(CONTENT_SETTING_BLOCK,
111 host_content_settings_map->GetContentSetting(
112 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
113 EXPECT_EQ(CONTENT_SETTING_ALLOW,
114 host_content_settings_map->GetContentSetting(
115 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
117 // Check returning all settings for a host.
118 host_content_settings_map->SetContentSetting(
119 pattern,
120 ContentSettingsPattern::Wildcard(),
121 CONTENT_SETTINGS_TYPE_IMAGES,
122 std::string(),
123 CONTENT_SETTING_DEFAULT);
124 EXPECT_EQ(CONTENT_SETTING_ALLOW,
125 host_content_settings_map->GetContentSetting(
126 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
127 host_content_settings_map->SetContentSetting(
128 pattern,
129 ContentSettingsPattern::Wildcard(),
130 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
131 std::string(),
132 CONTENT_SETTING_BLOCK);
133 EXPECT_EQ(CONTENT_SETTING_BLOCK,
134 host_content_settings_map->GetContentSetting(
135 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
136 host_content_settings_map->SetContentSetting(
137 pattern,
138 ContentSettingsPattern::Wildcard(),
139 CONTENT_SETTINGS_TYPE_PLUGINS,
140 std::string(),
141 CONTENT_SETTING_ALLOW);
142 EXPECT_EQ(CONTENT_SETTING_ALLOW,
143 host_content_settings_map->GetContentSetting(
144 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
145 EXPECT_EQ(CONTENT_SETTING_BLOCK,
146 host_content_settings_map->GetContentSetting(
147 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
148 EXPECT_EQ(CONTENT_SETTING_ASK,
149 host_content_settings_map->GetContentSetting(
150 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
151 EXPECT_EQ(
152 CONTENT_SETTING_ASK,
153 host_content_settings_map->GetContentSetting(
154 host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
155 EXPECT_EQ(CONTENT_SETTING_ASK,
156 host_content_settings_map->GetContentSetting(
157 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
158 EXPECT_EQ(CONTENT_SETTING_ASK,
159 host_content_settings_map->GetContentSetting(
160 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
162 // Check returning all hosts for a setting.
163 ContentSettingsPattern pattern2 =
164 ContentSettingsPattern::FromString("[*.]example.org");
165 host_content_settings_map->SetContentSetting(
166 pattern2,
167 ContentSettingsPattern::Wildcard(),
168 CONTENT_SETTINGS_TYPE_IMAGES,
169 std::string(),
170 CONTENT_SETTING_BLOCK);
171 host_content_settings_map->SetContentSetting(
172 pattern2,
173 ContentSettingsPattern::Wildcard(),
174 CONTENT_SETTINGS_TYPE_PLUGINS,
175 std::string(),
176 CONTENT_SETTING_BLOCK);
177 ContentSettingsForOneType host_settings;
178 host_content_settings_map->GetSettingsForOneType(
179 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
180 // |host_settings| contains the default setting and an exception.
181 EXPECT_EQ(2U, host_settings.size());
182 host_content_settings_map->GetSettingsForOneType(
183 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
184 // |host_settings| contains the default setting and 2 exceptions.
185 EXPECT_EQ(3U, host_settings.size());
186 host_content_settings_map->GetSettingsForOneType(
187 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings);
188 // |host_settings| contains only the default setting.
189 EXPECT_EQ(1U, host_settings.size());
192 TEST_F(HostContentSettingsMapTest, Clear) {
193 TestingProfile profile;
194 HostContentSettingsMap* host_content_settings_map =
195 profile.GetHostContentSettingsMap();
197 // Check clearing one type.
198 ContentSettingsPattern pattern =
199 ContentSettingsPattern::FromString("[*.]example.org");
200 ContentSettingsPattern pattern2 =
201 ContentSettingsPattern::FromString("[*.]example.net");
202 host_content_settings_map->SetContentSetting(
203 pattern2,
204 ContentSettingsPattern::Wildcard(),
205 CONTENT_SETTINGS_TYPE_IMAGES,
206 std::string(),
207 CONTENT_SETTING_BLOCK);
208 host_content_settings_map->SetContentSetting(
209 pattern,
210 ContentSettingsPattern::Wildcard(),
211 CONTENT_SETTINGS_TYPE_IMAGES,
212 std::string(),
213 CONTENT_SETTING_BLOCK);
214 host_content_settings_map->SetContentSetting(
215 pattern,
216 ContentSettingsPattern::Wildcard(),
217 CONTENT_SETTINGS_TYPE_PLUGINS,
218 std::string(),
219 CONTENT_SETTING_BLOCK);
220 host_content_settings_map->SetContentSetting(
221 pattern2,
222 ContentSettingsPattern::Wildcard(),
223 CONTENT_SETTINGS_TYPE_IMAGES,
224 std::string(),
225 CONTENT_SETTING_BLOCK);
226 host_content_settings_map->ClearSettingsForOneType(
227 CONTENT_SETTINGS_TYPE_IMAGES);
228 ContentSettingsForOneType host_settings;
229 host_content_settings_map->GetSettingsForOneType(
230 CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
231 // |host_settings| contains only the default setting.
232 EXPECT_EQ(1U, host_settings.size());
233 host_content_settings_map->GetSettingsForOneType(
234 CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
235 // |host_settings| contains the default setting and an exception.
236 EXPECT_EQ(2U, host_settings.size());
239 TEST_F(HostContentSettingsMapTest, Patterns) {
240 TestingProfile profile;
241 HostContentSettingsMap* host_content_settings_map =
242 profile.GetHostContentSettingsMap();
244 GURL host1("http://example.com/");
245 GURL host2("http://www.example.com/");
246 GURL host3("http://example.org/");
247 ContentSettingsPattern pattern1 =
248 ContentSettingsPattern::FromString("[*.]example.com");
249 ContentSettingsPattern pattern2 =
250 ContentSettingsPattern::FromString("example.org");
251 EXPECT_EQ(CONTENT_SETTING_ALLOW,
252 host_content_settings_map->GetContentSetting(
253 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
254 host_content_settings_map->SetContentSetting(
255 pattern1,
256 ContentSettingsPattern::Wildcard(),
257 CONTENT_SETTINGS_TYPE_IMAGES,
258 std::string(),
259 CONTENT_SETTING_BLOCK);
260 EXPECT_EQ(CONTENT_SETTING_BLOCK,
261 host_content_settings_map->GetContentSetting(
262 host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
263 EXPECT_EQ(CONTENT_SETTING_BLOCK,
264 host_content_settings_map->GetContentSetting(
265 host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
266 EXPECT_EQ(CONTENT_SETTING_ALLOW,
267 host_content_settings_map->GetContentSetting(
268 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
269 host_content_settings_map->SetContentSetting(
270 pattern2,
271 ContentSettingsPattern::Wildcard(),
272 CONTENT_SETTINGS_TYPE_IMAGES,
273 std::string(),
274 CONTENT_SETTING_BLOCK);
275 EXPECT_EQ(CONTENT_SETTING_BLOCK,
276 host_content_settings_map->GetContentSetting(
277 host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
280 TEST_F(HostContentSettingsMapTest, Observer) {
281 TestingProfile profile;
282 HostContentSettingsMap* host_content_settings_map =
283 profile.GetHostContentSettingsMap();
284 MockSettingsObserver observer(host_content_settings_map);
286 ContentSettingsPattern primary_pattern =
287 ContentSettingsPattern::FromString("[*.]example.com");
288 ContentSettingsPattern secondary_pattern =
289 ContentSettingsPattern::Wildcard();
290 EXPECT_CALL(observer,
291 OnContentSettingsChanged(host_content_settings_map,
292 CONTENT_SETTINGS_TYPE_IMAGES,
293 false,
294 primary_pattern,
295 secondary_pattern,
296 false));
297 host_content_settings_map->SetContentSetting(
298 primary_pattern,
299 secondary_pattern,
300 CONTENT_SETTINGS_TYPE_IMAGES,
301 std::string(),
302 CONTENT_SETTING_ALLOW);
303 ::testing::Mock::VerifyAndClearExpectations(&observer);
305 EXPECT_CALL(observer,
306 OnContentSettingsChanged(host_content_settings_map,
307 CONTENT_SETTINGS_TYPE_IMAGES, false,
308 _, _, true));
309 host_content_settings_map->ClearSettingsForOneType(
310 CONTENT_SETTINGS_TYPE_IMAGES);
311 ::testing::Mock::VerifyAndClearExpectations(&observer);
313 EXPECT_CALL(observer,
314 OnContentSettingsChanged(host_content_settings_map,
315 CONTENT_SETTINGS_TYPE_IMAGES, false,
316 _, _, true));
317 host_content_settings_map->SetDefaultContentSetting(
318 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
321 TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
322 TestingProfile profile;
323 HostContentSettingsMap* host_content_settings_map =
324 profile.GetHostContentSettingsMap();
326 PrefService* prefs = profile.GetPrefs();
328 // Make a copy of the default pref value so we can reset it later.
329 scoped_ptr<base::Value> default_value(prefs->FindPreference(
330 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
332 GURL host("http://example.com");
334 host_content_settings_map->SetDefaultContentSetting(
335 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
336 EXPECT_EQ(CONTENT_SETTING_BLOCK,
337 host_content_settings_map->GetContentSetting(
338 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
340 // Make a copy of the pref's new value so we can reset it later.
341 scoped_ptr<base::Value> new_value(prefs->FindPreference(
342 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
344 // Clearing the backing pref should also clear the internal cache.
345 prefs->Set(prefs::kDefaultContentSettings, *default_value);
346 EXPECT_EQ(CONTENT_SETTING_ALLOW,
347 host_content_settings_map->GetContentSetting(
348 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
350 // Reseting the pref to its previous value should update the cache.
351 prefs->Set(prefs::kDefaultContentSettings, *new_value);
352 EXPECT_EQ(CONTENT_SETTING_BLOCK,
353 host_content_settings_map->GetContentSetting(
354 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
357 TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
358 TestingProfile profile;
359 HostContentSettingsMap* host_content_settings_map =
360 profile.GetHostContentSettingsMap();
362 PrefService* prefs = profile.GetPrefs();
364 // Make a copy of the default pref value so we can reset it later.
365 scoped_ptr<base::Value> default_value(prefs->FindPreference(
366 prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
368 ContentSettingsPattern pattern =
369 ContentSettingsPattern::FromString("[*.]example.com");
370 GURL host("http://example.com");
372 EXPECT_EQ(CONTENT_SETTING_ALLOW,
373 host_content_settings_map->GetContentSetting(
374 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
376 host_content_settings_map->SetContentSetting(
377 pattern,
378 ContentSettingsPattern::Wildcard(),
379 CONTENT_SETTINGS_TYPE_IMAGES,
380 std::string(),
381 CONTENT_SETTING_BLOCK);
382 EXPECT_EQ(CONTENT_SETTING_BLOCK,
383 host_content_settings_map->GetContentSetting(
384 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
386 // Make a copy of the pref's new value so we can reset it later.
387 scoped_ptr<base::Value> new_value(prefs->FindPreference(
388 prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
390 // Clearing the backing pref should also clear the internal cache.
391 prefs->Set(prefs::kContentSettingsPatternPairs, *default_value);
392 EXPECT_EQ(CONTENT_SETTING_ALLOW,
393 host_content_settings_map->GetContentSetting(
394 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
396 // Reseting the pref to its previous value should update the cache.
397 prefs->Set(prefs::kContentSettingsPatternPairs, *new_value);
398 EXPECT_EQ(CONTENT_SETTING_BLOCK,
399 host_content_settings_map->GetContentSetting(
400 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
403 TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
404 TestingProfile profile;
405 HostContentSettingsMap* host_content_settings_map =
406 profile.GetHostContentSettingsMap();
407 CookieSettings* cookie_settings =
408 CookieSettings::Factory::GetForProfile(&profile).get();
410 ContentSettingsPattern pattern =
411 ContentSettingsPattern::FromString("[*.]example.com");
412 GURL host_ending_with_dot("http://example.com./");
414 EXPECT_EQ(CONTENT_SETTING_ALLOW,
415 host_content_settings_map->GetContentSetting(
416 host_ending_with_dot,
417 host_ending_with_dot,
418 CONTENT_SETTINGS_TYPE_IMAGES,
419 std::string()));
420 host_content_settings_map->SetContentSetting(
421 pattern,
422 ContentSettingsPattern::Wildcard(),
423 CONTENT_SETTINGS_TYPE_IMAGES,
424 std::string(),
425 CONTENT_SETTING_DEFAULT);
426 EXPECT_EQ(
427 CONTENT_SETTING_ALLOW,
428 host_content_settings_map->GetContentSetting(host_ending_with_dot,
429 host_ending_with_dot,
430 CONTENT_SETTINGS_TYPE_IMAGES,
431 std::string()));
432 host_content_settings_map->SetContentSetting(
433 pattern,
434 ContentSettingsPattern::Wildcard(),
435 CONTENT_SETTINGS_TYPE_IMAGES,
436 std::string(),
437 CONTENT_SETTING_BLOCK);
438 EXPECT_EQ(
439 CONTENT_SETTING_BLOCK,
440 host_content_settings_map->GetContentSetting(host_ending_with_dot,
441 host_ending_with_dot,
442 CONTENT_SETTINGS_TYPE_IMAGES,
443 std::string()));
445 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
446 host_ending_with_dot, host_ending_with_dot));
447 host_content_settings_map->SetContentSetting(
448 pattern,
449 ContentSettingsPattern::Wildcard(),
450 CONTENT_SETTINGS_TYPE_COOKIES,
451 std::string(),
452 CONTENT_SETTING_DEFAULT);
453 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
454 host_ending_with_dot, host_ending_with_dot));
455 host_content_settings_map->SetContentSetting(
456 pattern,
457 ContentSettingsPattern::Wildcard(),
458 CONTENT_SETTINGS_TYPE_COOKIES,
459 std::string(),
460 CONTENT_SETTING_BLOCK);
461 EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
462 host_ending_with_dot, host_ending_with_dot));
464 EXPECT_EQ(CONTENT_SETTING_ALLOW,
465 host_content_settings_map->GetContentSetting(
466 host_ending_with_dot,
467 host_ending_with_dot,
468 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
469 std::string()));
470 host_content_settings_map->SetContentSetting(
471 pattern,
472 ContentSettingsPattern::Wildcard(),
473 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
474 std::string(),
475 CONTENT_SETTING_DEFAULT);
476 EXPECT_EQ(CONTENT_SETTING_ALLOW,
477 host_content_settings_map->GetContentSetting(
478 host_ending_with_dot,
479 host_ending_with_dot,
480 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
481 std::string()));
482 host_content_settings_map->SetContentSetting(
483 pattern,
484 ContentSettingsPattern::Wildcard(),
485 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
486 std::string(),
487 CONTENT_SETTING_BLOCK);
488 EXPECT_EQ(CONTENT_SETTING_BLOCK,
489 host_content_settings_map->GetContentSetting(
490 host_ending_with_dot,
491 host_ending_with_dot,
492 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
493 std::string()));
495 EXPECT_EQ(CONTENT_SETTING_ALLOW,
496 host_content_settings_map->GetContentSetting(
497 host_ending_with_dot,
498 host_ending_with_dot,
499 CONTENT_SETTINGS_TYPE_PLUGINS,
500 std::string()));
501 host_content_settings_map->SetContentSetting(
502 pattern,
503 ContentSettingsPattern::Wildcard(),
504 CONTENT_SETTINGS_TYPE_PLUGINS,
505 std::string(),
506 CONTENT_SETTING_DEFAULT);
507 EXPECT_EQ(CONTENT_SETTING_ALLOW,
508 host_content_settings_map->GetContentSetting(
509 host_ending_with_dot,
510 host_ending_with_dot,
511 CONTENT_SETTINGS_TYPE_PLUGINS,
512 std::string()));
513 host_content_settings_map->SetContentSetting(
514 pattern,
515 ContentSettingsPattern::Wildcard(),
516 CONTENT_SETTINGS_TYPE_PLUGINS,
517 std::string(),
518 CONTENT_SETTING_BLOCK);
519 EXPECT_EQ(CONTENT_SETTING_BLOCK,
520 host_content_settings_map->GetContentSetting(
521 host_ending_with_dot,
522 host_ending_with_dot,
523 CONTENT_SETTINGS_TYPE_PLUGINS,
524 std::string()));
526 EXPECT_EQ(
527 CONTENT_SETTING_BLOCK,
528 host_content_settings_map->GetContentSetting(host_ending_with_dot,
529 host_ending_with_dot,
530 CONTENT_SETTINGS_TYPE_POPUPS,
531 std::string()));
532 host_content_settings_map->SetContentSetting(
533 pattern,
534 ContentSettingsPattern::Wildcard(),
535 CONTENT_SETTINGS_TYPE_POPUPS,
536 std::string(),
537 CONTENT_SETTING_DEFAULT);
538 EXPECT_EQ(
539 CONTENT_SETTING_BLOCK,
540 host_content_settings_map->GetContentSetting(host_ending_with_dot,
541 host_ending_with_dot,
542 CONTENT_SETTINGS_TYPE_POPUPS,
543 std::string()));
544 host_content_settings_map->SetContentSetting(
545 pattern,
546 ContentSettingsPattern::Wildcard(),
547 CONTENT_SETTINGS_TYPE_POPUPS,
548 std::string(),
549 CONTENT_SETTING_ALLOW);
550 EXPECT_EQ(
551 CONTENT_SETTING_ALLOW,
552 host_content_settings_map->GetContentSetting(host_ending_with_dot,
553 host_ending_with_dot,
554 CONTENT_SETTINGS_TYPE_POPUPS,
555 std::string()));
558 TEST_F(HostContentSettingsMapTest, NestedSettings) {
559 TestingProfile profile;
560 HostContentSettingsMap* host_content_settings_map =
561 profile.GetHostContentSettingsMap();
563 GURL host("http://a.b.example.com/");
564 ContentSettingsPattern pattern1 =
565 ContentSettingsPattern::FromString("[*.]example.com");
566 ContentSettingsPattern pattern2 =
567 ContentSettingsPattern::FromString("[*.]b.example.com");
568 ContentSettingsPattern pattern3 =
569 ContentSettingsPattern::FromString("a.b.example.com");
571 host_content_settings_map->SetContentSetting(
572 pattern1,
573 ContentSettingsPattern::Wildcard(),
574 CONTENT_SETTINGS_TYPE_IMAGES,
575 std::string(),
576 CONTENT_SETTING_BLOCK);
578 host_content_settings_map->SetContentSetting(
579 pattern2,
580 ContentSettingsPattern::Wildcard(),
581 CONTENT_SETTINGS_TYPE_COOKIES,
582 std::string(),
583 CONTENT_SETTING_BLOCK);
585 host_content_settings_map->SetContentSetting(
586 pattern3,
587 ContentSettingsPattern::Wildcard(),
588 CONTENT_SETTINGS_TYPE_PLUGINS,
589 std::string(),
590 CONTENT_SETTING_BLOCK);
591 host_content_settings_map->SetDefaultContentSetting(
592 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
594 EXPECT_EQ(CONTENT_SETTING_BLOCK,
595 host_content_settings_map->GetContentSetting(
596 host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
597 EXPECT_EQ(CONTENT_SETTING_BLOCK,
598 host_content_settings_map->GetContentSetting(
599 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
600 EXPECT_EQ(CONTENT_SETTING_BLOCK,
601 host_content_settings_map->GetContentSetting(
602 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
603 EXPECT_EQ(CONTENT_SETTING_BLOCK,
604 host_content_settings_map->GetContentSetting(
605 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
606 EXPECT_EQ(CONTENT_SETTING_BLOCK,
607 host_content_settings_map->GetContentSetting(
608 host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
609 EXPECT_EQ(CONTENT_SETTING_ASK,
610 host_content_settings_map->GetContentSetting(
611 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
612 EXPECT_EQ(
613 CONTENT_SETTING_ASK,
614 host_content_settings_map->GetContentSetting(
615 host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
616 EXPECT_EQ(CONTENT_SETTING_ASK,
617 host_content_settings_map->GetContentSetting(
618 host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
619 EXPECT_EQ(CONTENT_SETTING_ASK,
620 host_content_settings_map->GetContentSetting(
621 host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
624 TEST_F(HostContentSettingsMapTest, OffTheRecord) {
625 TestingProfile profile;
626 HostContentSettingsMap* host_content_settings_map =
627 profile.GetHostContentSettingsMap();
628 scoped_refptr<HostContentSettingsMap> otr_map(
629 new HostContentSettingsMap(profile.GetPrefs(),
630 true));
632 GURL host("http://example.com/");
633 ContentSettingsPattern pattern =
634 ContentSettingsPattern::FromString("[*.]example.com");
636 EXPECT_EQ(CONTENT_SETTING_ALLOW,
637 host_content_settings_map->GetContentSetting(
638 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
639 EXPECT_EQ(CONTENT_SETTING_ALLOW,
640 otr_map->GetContentSetting(
641 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
643 // Changing content settings on the main map should also affect the
644 // incognito map.
645 host_content_settings_map->SetContentSetting(
646 pattern,
647 ContentSettingsPattern::Wildcard(),
648 CONTENT_SETTINGS_TYPE_IMAGES,
649 std::string(),
650 CONTENT_SETTING_BLOCK);
651 EXPECT_EQ(CONTENT_SETTING_BLOCK,
652 host_content_settings_map->GetContentSetting(
653 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
654 EXPECT_EQ(CONTENT_SETTING_BLOCK,
655 otr_map->GetContentSetting(
656 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
658 // Changing content settings on the incognito map should NOT affect the
659 // main map.
660 otr_map->SetContentSetting(pattern,
661 ContentSettingsPattern::Wildcard(),
662 CONTENT_SETTINGS_TYPE_IMAGES,
663 std::string(),
664 CONTENT_SETTING_ALLOW);
665 EXPECT_EQ(CONTENT_SETTING_BLOCK,
666 host_content_settings_map->GetContentSetting(
667 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
668 EXPECT_EQ(CONTENT_SETTING_ALLOW,
669 otr_map->GetContentSetting(
670 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
672 otr_map->ShutdownOnUIThread();
675 // For a single Unicode encoded pattern, check if it gets converted to punycode
676 // and old pattern gets deleted.
677 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
678 TestingProfile profile;
679 PrefService* prefs = profile.GetPrefs();
681 // Set utf-8 data.
683 DictionaryPrefUpdate update(
684 prefs, prefs::kContentSettingsPatternPairs);
685 base::DictionaryValue* all_settings_dictionary = update.Get();
686 ASSERT_TRUE(NULL != all_settings_dictionary);
688 base::DictionaryValue* dummy_payload = new base::DictionaryValue;
689 dummy_payload->SetInteger("images", CONTENT_SETTING_ALLOW);
690 all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*",
691 dummy_payload);
694 profile.GetHostContentSettingsMap();
696 const base::DictionaryValue* all_settings_dictionary =
697 prefs->GetDictionary(prefs::kContentSettingsImagesPatternPairs);
698 const base::DictionaryValue* result = NULL;
699 EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
700 "[*.]\xC4\x87ira.com,*", &result));
701 EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
702 "[*.]xn--ira-ppa.com,*", &result));
705 // If both Unicode and its punycode pattern exist, make sure we don't touch the
706 // settings for the punycode, and that Unicode pattern gets deleted.
707 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
708 TestingProfile profile;
710 scoped_ptr<base::Value> value(base::JSONReader::DeprecatedRead(
711 "{\"[*.]\\xC4\\x87ira.com,*\":{\"images\":1}}"));
712 profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *value);
714 // Set punycode equivalent, with different setting.
715 scoped_ptr<base::Value> puny_value(base::JSONReader::DeprecatedRead(
716 "{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}"));
717 profile.GetPrefs()->Set(
718 prefs::kContentSettingsPatternPairs, *puny_value);
720 // Initialize the content map.
721 profile.GetHostContentSettingsMap();
723 const base::DictionaryValue& content_setting_prefs =
724 *profile.GetPrefs()->GetDictionary(
725 prefs::kContentSettingsImagesPatternPairs);
726 std::string prefs_as_json;
727 base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
728 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}",
729 prefs_as_json.c_str());
732 // If a default-content-setting is managed, the managed value should be used
733 // instead of the default value.
734 TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
735 TestingProfile profile;
736 HostContentSettingsMap* host_content_settings_map =
737 profile.GetHostContentSettingsMap();
738 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
740 EXPECT_EQ(CONTENT_SETTING_ALLOW,
741 host_content_settings_map->GetDefaultContentSetting(
742 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
744 // Set managed-default-content-setting through the coresponding preferences.
745 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
746 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
747 EXPECT_EQ(CONTENT_SETTING_BLOCK,
748 host_content_settings_map->GetDefaultContentSetting(
749 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
751 // Remove managed-default-content-settings-preferences.
752 prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
753 EXPECT_EQ(CONTENT_SETTING_ALLOW,
754 host_content_settings_map->GetDefaultContentSetting(
755 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
757 // Set preference to manage the default-content-setting for Plugins.
758 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
759 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
760 EXPECT_EQ(CONTENT_SETTING_BLOCK,
761 host_content_settings_map->GetDefaultContentSetting(
762 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
764 // Remove the preference to manage the default-content-setting for Plugins.
765 prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
766 EXPECT_EQ(CONTENT_SETTING_ALLOW,
767 host_content_settings_map->GetDefaultContentSetting(
768 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
771 TEST_F(HostContentSettingsMapTest,
772 GetNonDefaultContentSettingsIfTypeManaged) {
773 TestingProfile profile;
774 HostContentSettingsMap* host_content_settings_map =
775 profile.GetHostContentSettingsMap();
776 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
778 // Set pattern for JavaScript setting.
779 ContentSettingsPattern pattern =
780 ContentSettingsPattern::FromString("[*.]example.com");
781 host_content_settings_map->SetContentSetting(
782 pattern,
783 ContentSettingsPattern::Wildcard(),
784 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
785 std::string(),
786 CONTENT_SETTING_BLOCK);
788 EXPECT_EQ(CONTENT_SETTING_ALLOW,
789 host_content_settings_map->GetDefaultContentSetting(
790 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
792 GURL host("http://example.com/");
793 EXPECT_EQ(CONTENT_SETTING_BLOCK,
794 host_content_settings_map->GetContentSetting(
795 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
797 // Set managed-default-content-setting for content-settings-type JavaScript.
798 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
799 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
800 EXPECT_EQ(CONTENT_SETTING_ALLOW,
801 host_content_settings_map->GetContentSetting(
802 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
805 // Managed default content setting should have higher priority
806 // than user defined patterns.
807 TEST_F(HostContentSettingsMapTest,
808 ManagedDefaultContentSettingIgnoreUserPattern) {
809 TestingProfile profile;
810 HostContentSettingsMap* host_content_settings_map =
811 profile.GetHostContentSettingsMap();
812 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
814 // Block all JavaScript.
815 host_content_settings_map->SetDefaultContentSetting(
816 CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
818 // Set an exception to allow "[*.]example.com"
819 ContentSettingsPattern pattern =
820 ContentSettingsPattern::FromString("[*.]example.com");
822 host_content_settings_map->SetContentSetting(
823 pattern,
824 ContentSettingsPattern::Wildcard(),
825 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
826 std::string(),
827 CONTENT_SETTING_ALLOW);
829 EXPECT_EQ(CONTENT_SETTING_BLOCK,
830 host_content_settings_map->GetDefaultContentSetting(
831 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
832 GURL host("http://example.com/");
833 EXPECT_EQ(CONTENT_SETTING_ALLOW,
834 host_content_settings_map->GetContentSetting(
835 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
837 // Set managed-default-content-settings-preferences.
838 prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
839 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
840 EXPECT_EQ(CONTENT_SETTING_BLOCK,
841 host_content_settings_map->GetContentSetting(
842 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
844 // Remove managed-default-content-settings-preferences.
845 prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
846 EXPECT_EQ(CONTENT_SETTING_ALLOW,
847 host_content_settings_map->GetContentSetting(
848 host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
851 // If a default-content-setting is set to managed setting, the user defined
852 // setting should be preserved.
853 TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
854 TestingProfile profile;
855 HostContentSettingsMap* host_content_settings_map =
856 profile.GetHostContentSettingsMap();
857 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
859 // Set user defined default-content-setting for Cookies.
860 host_content_settings_map->SetDefaultContentSetting(
861 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
862 EXPECT_EQ(CONTENT_SETTING_BLOCK,
863 host_content_settings_map->GetDefaultContentSetting(
864 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
866 // Set preference to manage the default-content-setting for Cookies.
867 prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
868 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
869 EXPECT_EQ(CONTENT_SETTING_ALLOW,
870 host_content_settings_map->GetDefaultContentSetting(
871 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
873 // Remove the preference to manage the default-content-setting for Cookies.
874 prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
875 EXPECT_EQ(CONTENT_SETTING_BLOCK,
876 host_content_settings_map->GetDefaultContentSetting(
877 CONTENT_SETTINGS_TYPE_COOKIES, NULL));
880 // If a setting for a default-content-setting-type is set while the type is
881 // managed, then the new setting should be preserved and used after the
882 // default-content-setting-type is not managed anymore.
883 TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
884 TestingProfile profile;
885 HostContentSettingsMap* host_content_settings_map =
886 profile.GetHostContentSettingsMap();
887 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
889 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
890 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
891 EXPECT_EQ(CONTENT_SETTING_ALLOW,
892 host_content_settings_map->GetDefaultContentSetting(
893 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
895 host_content_settings_map->SetDefaultContentSetting(
896 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
897 EXPECT_EQ(CONTENT_SETTING_ALLOW,
898 host_content_settings_map->GetDefaultContentSetting(
899 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
901 prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
902 EXPECT_EQ(CONTENT_SETTING_BLOCK,
903 host_content_settings_map->GetDefaultContentSetting(
904 CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
907 TEST_F(HostContentSettingsMapTest, GetContentSetting) {
908 TestingProfile profile;
909 HostContentSettingsMap* host_content_settings_map =
910 profile.GetHostContentSettingsMap();
912 GURL host("http://example.com/");
913 GURL embedder("chrome://foo");
914 ContentSettingsPattern pattern =
915 ContentSettingsPattern::FromString("[*.]example.com");
916 host_content_settings_map->SetContentSetting(
917 pattern,
918 ContentSettingsPattern::Wildcard(),
919 CONTENT_SETTINGS_TYPE_IMAGES,
920 std::string(),
921 CONTENT_SETTING_BLOCK);
922 EXPECT_EQ(CONTENT_SETTING_BLOCK,
923 host_content_settings_map->GetContentSetting(
924 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
925 EXPECT_EQ(CONTENT_SETTING_ALLOW,
926 host_content_settings_map->GetContentSetting(
927 embedder, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
930 TEST_F(HostContentSettingsMapTest, ShouldAllowAllContent) {
931 GURL http_host("http://example.com/");
932 GURL https_host("https://example.com/");
933 GURL embedder("chrome://foo");
934 GURL extension("chrome-extension://foo");
935 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
936 http_host, embedder, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
937 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
938 http_host, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION));
939 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
940 http_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
941 EXPECT_TRUE(HostContentSettingsMap::ShouldAllowAllContent(
942 https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
943 EXPECT_TRUE(HostContentSettingsMap::ShouldAllowAllContent(
944 https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
945 EXPECT_TRUE(HostContentSettingsMap::ShouldAllowAllContent(
946 embedder, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
947 #if defined(ENABLE_EXTENSIONS)
948 EXPECT_TRUE(HostContentSettingsMap::ShouldAllowAllContent(
949 extension, extension, CONTENT_SETTINGS_TYPE_COOKIES));
950 #else
951 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
952 extension, extension, CONTENT_SETTINGS_TYPE_COOKIES));
953 #endif
954 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
955 extension, extension, CONTENT_SETTINGS_TYPE_PLUGINS));
956 EXPECT_FALSE(HostContentSettingsMap::ShouldAllowAllContent(
957 extension, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
960 TEST_F(HostContentSettingsMapTest, IsSettingAllowedForType) {
961 TestingProfile profile;
962 PrefService* prefs = profile.GetPrefs();
964 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
965 prefs, CONTENT_SETTING_ASK,
966 CONTENT_SETTINGS_TYPE_FULLSCREEN));
968 // The mediastream setting is deprecated.
969 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
970 prefs, CONTENT_SETTING_ALLOW,
971 CONTENT_SETTINGS_TYPE_MEDIASTREAM));
972 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
973 prefs, CONTENT_SETTING_ASK,
974 CONTENT_SETTINGS_TYPE_MEDIASTREAM));
975 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
976 prefs, CONTENT_SETTING_BLOCK,
977 CONTENT_SETTINGS_TYPE_MEDIASTREAM));
979 // We support the ALLOW value for media permission exceptions,
980 // but not as the default setting.
981 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
982 prefs, CONTENT_SETTING_ALLOW,
983 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
984 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
985 prefs, CONTENT_SETTING_ALLOW,
986 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
987 EXPECT_FALSE(HostContentSettingsMap::IsDefaultSettingAllowedForType(
988 prefs, CONTENT_SETTING_ALLOW,
989 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
990 EXPECT_FALSE(HostContentSettingsMap::IsDefaultSettingAllowedForType(
991 prefs, CONTENT_SETTING_ALLOW,
992 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
994 // TODO(msramek): Add more checks for setting type - setting pairs where
995 // it is not obvious whether or not they are allowed.
998 TEST_F(HostContentSettingsMapTest, AddContentSettingsObserver) {
999 TestingProfile profile;
1000 HostContentSettingsMap* host_content_settings_map =
1001 profile.GetHostContentSettingsMap();
1002 content_settings::MockObserver mock_observer;
1004 GURL host("http://example.com/");
1005 ContentSettingsPattern pattern =
1006 ContentSettingsPattern::FromString("[*.]example.com");
1007 EXPECT_CALL(mock_observer,
1008 OnContentSettingChanged(pattern,
1009 ContentSettingsPattern::Wildcard(),
1010 CONTENT_SETTINGS_TYPE_IMAGES,
1011 ""));
1013 host_content_settings_map->AddObserver(&mock_observer);
1015 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1016 host_content_settings_map->GetContentSetting(
1017 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1018 host_content_settings_map->SetContentSetting(
1019 pattern,
1020 ContentSettingsPattern::Wildcard(),
1021 CONTENT_SETTINGS_TYPE_IMAGES,
1022 std::string(),
1023 CONTENT_SETTING_DEFAULT);
1026 TEST_F(HostContentSettingsMapTest, OverrideAllowedWebsiteSetting) {
1027 TestingProfile profile;
1028 HostContentSettingsMap* host_content_settings_map =
1029 profile.GetHostContentSettingsMap();
1030 GURL host("http://example.com/");
1031 ContentSettingsPattern pattern =
1032 ContentSettingsPattern::FromString("[*.]example.com");
1033 host_content_settings_map->SetContentSetting(
1034 pattern,
1035 ContentSettingsPattern::Wildcard(),
1036 CONTENT_SETTINGS_TYPE_GEOLOCATION,
1037 std::string(),
1038 CONTENT_SETTING_ALLOW);
1040 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1041 host_content_settings_map->GetContentSetting(
1042 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
1044 // Disabling should override an allowed exception.
1045 host_content_settings_map->SetContentSettingOverride(
1046 CONTENT_SETTINGS_TYPE_GEOLOCATION, false);
1047 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1048 host_content_settings_map->GetContentSetting(
1049 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
1051 host_content_settings_map->SetContentSettingOverride(
1052 CONTENT_SETTINGS_TYPE_GEOLOCATION, true);
1053 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1054 host_content_settings_map->GetContentSetting(
1055 host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
1058 TEST_F(HostContentSettingsMapTest, OverrideAllowedDefaultSetting) {
1059 TestingProfile profile;
1060 HostContentSettingsMap* host_content_settings_map =
1061 profile.GetHostContentSettingsMap();
1063 // Check setting defaults.
1064 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1065 host_content_settings_map->GetDefaultContentSetting(
1066 CONTENT_SETTINGS_TYPE_IMAGES, NULL));
1068 GURL host("http://example.com/");
1069 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1070 host_content_settings_map->GetContentSetting(
1071 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1073 // Disabling should override an allowed default setting.
1074 host_content_settings_map->SetContentSettingOverride(
1075 CONTENT_SETTINGS_TYPE_IMAGES, false);
1076 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1077 host_content_settings_map->GetContentSetting(
1078 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1080 // Enabling shouldn't override positively.
1081 host_content_settings_map->SetContentSettingOverride(
1082 CONTENT_SETTINGS_TYPE_IMAGES, true);
1083 EXPECT_EQ(CONTENT_SETTING_ALLOW,
1084 host_content_settings_map->GetContentSetting(
1085 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
1086 host_content_settings_map->SetDefaultContentSetting(
1087 CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
1088 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1089 host_content_settings_map->GetContentSetting(
1090 host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));