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