diff options
author | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-10 09:04:16 +0000 |
---|---|---|
committer | marja@chromium.org <marja@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-10 09:04:16 +0000 |
commit | bf98782c26763e79c37bff93873b56a693ffa88b (patch) | |
tree | 2ee066fe416dcdf76cf1b42dc930a429483484a1 /chrome/browser/content_settings/content_settings_default_provider_unittest.cc | |
parent | dad21bfdae162c31845b4b66ed2711722dc1f2f4 (diff) | |
download | chromium_src-bf98782c26763e79c37bff93873b56a693ffa88b.zip chromium_src-bf98782c26763e79c37bff93873b56a693ffa88b.tar.gz chromium_src-bf98782c26763e79c37bff93873b56a693ffa88b.tar.bz2 |
Remove content settings default providers from the host content settings map.
Add a DefaultProvider for user default settings and integrate the DefaultPolicyProvider into the PolicyProvider.
The real work was done by markusheintz@chromium.org and the original CL is
http://codereview.chromium.org/7976032/
BUG=63656
TEST=host_content_settings_map_unittest.cc,
content_settings_policy_provider_unittest.cc,
content_settings_default_provider_unittest.cc
Review URL: http://codereview.chromium.org/8137024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104704 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/content_settings/content_settings_default_provider_unittest.cc')
-rw-r--r-- | chrome/browser/content_settings/content_settings_default_provider_unittest.cc | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/chrome/browser/content_settings/content_settings_default_provider_unittest.cc b/chrome/browser/content_settings/content_settings_default_provider_unittest.cc new file mode 100644 index 0000000..1f2930d9 --- /dev/null +++ b/chrome/browser/content_settings/content_settings_default_provider_unittest.cc @@ -0,0 +1,251 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/content_settings/content_settings_default_provider.h" +#include "chrome/browser/content_settings/content_settings_mock_observer.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/base/testing_pref_service.h" +#include "chrome/test/base/testing_profile.h" +#include "content/browser/browser_thread.h" +#include "googleurl/src/gurl.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::testing::_; + +class DefaultProviderTest : public testing::Test { + public: + DefaultProviderTest() + : ui_thread_(BrowserThread::UI, &message_loop_), + provider_(profile_.GetPrefs(), false) { + } + ~DefaultProviderTest() { + provider_.ShutdownOnUIThread(); + } + + protected: + MessageLoop message_loop_; + BrowserThread ui_thread_; + TestingProfile profile_; + content_settings::DefaultProvider provider_; +}; + +TEST_F(DefaultProviderTest, DefaultValues) { + // Check setting defaults. + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), + CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + EXPECT_EQ(CONTENT_SETTING_ASK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string())); + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string(), + CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string())); +} + +TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) { + GURL primary_url("http://www.google.com"); + GURL secondary_url("http://www.google.com"); + + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(primary_url, + secondary_url, + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + provider_.SetContentSetting(ContentSettingsPattern::FromURL(primary_url), + ContentSettingsPattern::FromURL(secondary_url), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), + CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(primary_url, + secondary_url, + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); +} + + +TEST_F(DefaultProviderTest, Observer) { + content_settings::MockObserver mock_observer; + EXPECT_CALL(mock_observer, + OnContentSettingChanged( + _, _, CONTENT_SETTINGS_TYPE_IMAGES, "")); + provider_.AddObserver(&mock_observer); + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_IMAGES, + std::string(), + CONTENT_SETTING_BLOCK); + + EXPECT_CALL(mock_observer, + OnContentSettingChanged( + _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, "")); + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string(), + CONTENT_SETTING_BLOCK); +} + + +TEST_F(DefaultProviderTest, ObserveDefaultPref) { + PrefService* prefs = profile_.GetPrefs(); + + // Make a copy of the default pref value so we can reset it later. + scoped_ptr<Value> default_value(prefs->FindPreference( + prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); + + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), + CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + // Make a copy of the pref's new value so we can reset it later. + scoped_ptr<Value> new_value(prefs->FindPreference( + prefs::kDefaultContentSettings)->GetValue()->DeepCopy()); + + // Clearing the backing pref should also clear the internal cache. + prefs->Set(prefs::kDefaultContentSettings, *default_value); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + // Reseting the pref to its previous value should update the cache. + prefs->Set(prefs::kDefaultContentSettings, *new_value); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); +} + +TEST_F(DefaultProviderTest, OffTheRecord) { + content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true); + + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + EXPECT_EQ(CONTENT_SETTING_ALLOW, + otr_provider.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + // Changing content settings on the main provider should also affect the + // incognito map. + provider_.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), + CONTENT_SETTING_BLOCK); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + EXPECT_EQ(CONTENT_SETTING_BLOCK, + otr_provider.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + // Changing content settings on the incognito provider should be ignored. + otr_provider.SetContentSetting(ContentSettingsPattern::Wildcard(), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), + CONTENT_SETTING_ALLOW); + EXPECT_EQ(CONTENT_SETTING_BLOCK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + + EXPECT_EQ(CONTENT_SETTING_BLOCK, + otr_provider.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_COOKIES, + std::string())); + otr_provider.ShutdownOnUIThread(); +} + +TEST_F(DefaultProviderTest, MigrateDefaultGeolocationContentSettingAfterSync) { + EXPECT_EQ(CONTENT_SETTING_ASK, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string())); + + content_settings::MockObserver mock_observer; + EXPECT_CALL(mock_observer, + OnContentSettingChanged( + _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, "")); + provider_.AddObserver(&mock_observer); + + // Set obsolete preference and test if it is migrated correctly. This can + // happen when an old version of chrome syncs the obsolete default geolocation + // preference. + PrefService* prefs = profile_.GetPrefs(); + prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, + CONTENT_SETTING_ALLOW); + + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider_.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string())); +} + +TEST_F(DefaultProviderTest, MigrateDefaultGeolocationContentSettingAtStartup) { + TestingProfile profile; + TestingPrefService* prefs = profile.GetTestingPrefService(); + + // Set obsolete preference and test if it is migrated correctly. + prefs->SetInteger(prefs::kGeolocationDefaultContentSetting, + CONTENT_SETTING_ALLOW); + // Create a new |DefaultProvider| to test whether the obsolete default + // geolocation setting is correctly migrated. The migrated settings should be + // available right after creation of the provider. + content_settings::DefaultProvider provider(prefs, false); + + EXPECT_EQ(CONTENT_SETTING_ALLOW, + provider.GetContentSetting(GURL(), + GURL(), + CONTENT_SETTINGS_TYPE_GEOLOCATION, + std::string())); + provider.ShutdownOnUIThread(); +} |