This source file includes following definitions.
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "chrome/browser/content_settings/content_settings_details.h"
#include "chrome/browser/content_settings/cookie_settings.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/content_settings/mock_settings_observer.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "net/base/static_cookie_policy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using content::BrowserThread;
using ::testing::_;
class HostContentSettingsMapTest : public testing::Test {
public:
HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) {
}
protected:
base::MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
};
TEST_F(HostContentSettingsMapTest, DefaultValues) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_IMAGES, NULL));
EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting(
GURL(chrome::kChromeUINewTabURL),
GURL(chrome::kChromeUINewTabURL),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string()));
{
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
}
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_POPUPS, NULL));
}
TEST_F(HostContentSettingsMapTest, IndividualSettings) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(
CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("[*.]example.org");
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_BLOCK);
ContentSettingsForOneType host_settings;
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
EXPECT_EQ(2U, host_settings.size());
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
EXPECT_EQ(3U, host_settings.size());
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings);
EXPECT_EQ(1U, host_settings.size());
}
TEST_F(HostContentSettingsMapTest, Clear) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.org");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("[*.]example.net");
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->ClearSettingsForOneType(
CONTENT_SETTINGS_TYPE_IMAGES);
ContentSettingsForOneType host_settings;
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_IMAGES, std::string(), &host_settings);
EXPECT_EQ(1U, host_settings.size());
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), &host_settings);
EXPECT_EQ(2U, host_settings.size());
}
TEST_F(HostContentSettingsMapTest, Patterns) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
GURL host1("http://example.com/");
GURL host2("http://www.example.com/");
GURL host3("http://example.org/");
ContentSettingsPattern pattern1 =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("example.org");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern1,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
}
TEST_F(HostContentSettingsMapTest, Observer) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
MockSettingsObserver observer;
ContentSettingsPattern primary_pattern =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern secondary_pattern =
ContentSettingsPattern::Wildcard();
EXPECT_CALL(observer,
OnContentSettingsChanged(host_content_settings_map,
CONTENT_SETTINGS_TYPE_IMAGES,
false,
primary_pattern,
secondary_pattern,
false));
host_content_settings_map->SetContentSetting(
primary_pattern,
secondary_pattern,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_ALLOW);
::testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer,
OnContentSettingsChanged(host_content_settings_map,
CONTENT_SETTINGS_TYPE_IMAGES, false,
_, _, true));
host_content_settings_map->ClearSettingsForOneType(
CONTENT_SETTINGS_TYPE_IMAGES);
::testing::Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer,
OnContentSettingsChanged(host_content_settings_map,
CONTENT_SETTINGS_TYPE_IMAGES, false,
_, _, true));
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
}
TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
PrefService* prefs = profile.GetPrefs();
scoped_ptr<base::Value> default_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
GURL host("http://example.com");
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
scoped_ptr<base::Value> new_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
prefs->Set(prefs::kDefaultContentSettings, *default_value);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
prefs->Set(prefs::kDefaultContentSettings, *new_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
}
TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
PrefService* prefs = profile.GetPrefs();
scoped_ptr<base::Value> default_value(prefs->FindPreference(
prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
GURL host("http://example.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
scoped_ptr<base::Value> new_value(prefs->FindPreference(
prefs::kContentSettingsPatternPairs)->GetValue()->DeepCopy());
prefs->Set(prefs::kContentSettingsPatternPairs, *default_value);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
prefs->Set(prefs::kContentSettingsPatternPairs, *new_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
}
TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
CookieSettings* cookie_settings =
CookieSettings::Factory::GetForProfile(&profile).get();
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
GURL host_ending_with_dot("http://example.com./");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_IMAGES,
std::string()));
EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
host_ending_with_dot, host_ending_with_dot));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
host_ending_with_dot, host_ending_with_dot));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed(
host_ending_with_dot, host_ending_with_dot));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string()));
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_POPUPS,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_POPUPS,
std::string(),
CONTENT_SETTING_DEFAULT);
EXPECT_EQ(
CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_POPUPS,
std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_POPUPS,
std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(
CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(host_ending_with_dot,
host_ending_with_dot,
CONTENT_SETTINGS_TYPE_POPUPS,
std::string()));
}
TEST_F(HostContentSettingsMapTest, NestedSettings) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
GURL host("http://a.b.example.com/");
ContentSettingsPattern pattern1 =
ContentSettingsPattern::FromString("[*.]example.com");
ContentSettingsPattern pattern2 =
ContentSettingsPattern::FromString("[*.]b.example.com");
ContentSettingsPattern pattern3 =
ContentSettingsPattern::FromString("a.b.example.com");
host_content_settings_map->SetContentSetting(
pattern1,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern2,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetContentSetting(
pattern3,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_BLOCK);
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
EXPECT_EQ(
CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
EXPECT_EQ(CONTENT_SETTING_ASK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
}
TEST_F(HostContentSettingsMapTest, OffTheRecord) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
scoped_refptr<HostContentSettingsMap> otr_map(
new HostContentSettingsMap(profile.GetPrefs(),
true));
GURL host("http://example.com/");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
otr_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
otr_map->SetContentSetting(pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
otr_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
otr_map->ShutdownOnUIThread();
}
TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
TestingProfile profile;
PrefService* prefs = profile.GetPrefs();
{
DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatternPairs);
base::DictionaryValue* all_settings_dictionary = update.Get();
ASSERT_TRUE(NULL != all_settings_dictionary);
base::DictionaryValue* dummy_payload = new base::DictionaryValue;
dummy_payload->SetInteger("images", CONTENT_SETTING_ALLOW);
all_settings_dictionary->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*",
dummy_payload);
}
profile.GetHostContentSettingsMap();
const base::DictionaryValue* all_settings_dictionary =
prefs->GetDictionary(prefs::kContentSettingsPatternPairs);
const base::DictionaryValue* result = NULL;
EXPECT_FALSE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
"[*.]\xC4\x87ira.com,*", &result));
EXPECT_TRUE(all_settings_dictionary->GetDictionaryWithoutPathExpansion(
"[*.]xn--ira-ppa.com,*", &result));
}
TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeAndPunycode) {
TestingProfile profile;
scoped_ptr<base::Value> value(base::JSONReader::Read(
"{\"[*.]\\xC4\\x87ira.com,*\":{\"images\":1}}"));
profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *value);
scoped_ptr<base::Value> puny_value(base::JSONReader::Read(
"{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}"));
profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *puny_value);
profile.GetHostContentSettingsMap();
const base::DictionaryValue* content_setting_prefs =
profile.GetPrefs()->GetDictionary(prefs::kContentSettingsPatternPairs);
std::string prefs_as_json;
base::JSONWriter::Write(content_setting_prefs, &prefs_as_json);
EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"images\":2}}",
prefs_as_json.c_str());
}
TEST_F(HostContentSettingsMapTest, ManagedDefaultContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
}
TEST_F(HostContentSettingsMapTest,
GetNonDefaultContentSettingsIfTypeManaged) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
}
TEST_F(HostContentSettingsMapTest,
ManagedDefaultContentSettingIgnoreUserPattern) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK);
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT,
std::string(),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
GURL host("http://example.com/");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
prefs->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
prefs->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
}
TEST_F(HostContentSettingsMapTest, OverwrittenDefaultContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES, NULL));
prefs->SetManagedPref(prefs::kManagedDefaultCookiesSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES, NULL));
prefs->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES, NULL));
}
TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
prefs->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PLUGINS, NULL));
}
TEST_F(HostContentSettingsMapTest, GetContentSetting) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
GURL host("http://example.com/");
GURL embedder("chrome://foo");
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString("[*.]example.com");
host_content_settings_map->SetContentSetting(
pattern,
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
host_content_settings_map->GetContentSetting(
embedder, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
}
TEST_F(HostContentSettingsMapTest, ShouldAllowAllContent) {
TestingProfile profile;
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
GURL http_host("http://example.com/");
GURL https_host("https://example.com/");
GURL embedder("chrome://foo");
GURL extension("chrome-extension://foo");
EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
http_host, embedder, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
http_host, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION));
EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
http_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
https_host, embedder, CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
embedder, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_TRUE(host_content_settings_map->ShouldAllowAllContent(
extension, extension, CONTENT_SETTINGS_TYPE_COOKIES));
EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
extension, extension, CONTENT_SETTINGS_TYPE_PLUGINS));
EXPECT_FALSE(host_content_settings_map->ShouldAllowAllContent(
extension, http_host, CONTENT_SETTINGS_TYPE_COOKIES));
}
TEST_F(HostContentSettingsMapTest, MigrateClearOnExit) {
TestingProfile profile;
TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
prefs->SetBoolean(prefs::kClearSiteDataOnExit, true);
scoped_ptr<base::Value> patterns(base::JSONReader::Read(
"{\"[*.]example.com,*\":{\"cookies\": 1},"
" \"[*.]other.com,*\":{\"cookies\": 2},"
" \"[*.]third.com,*\":{\"cookies\": 4}}"));
profile.GetPrefs()->Set(prefs::kContentSettingsPatternPairs, *patterns);
scoped_ptr<base::Value> defaults(base::JSONReader::Read("{\"cookies\": 1}"));
profile.GetPrefs()->Set(prefs::kDefaultContentSettings, *defaults);
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
host_content_settings_map->GetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES, NULL));
EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
host_content_settings_map->GetContentSetting(
GURL("http://example.com"),
GURL("http://example.com"),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string()));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
GURL("http://other.com"),
GURL("http://other.com"),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string()));
EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY,
host_content_settings_map->GetContentSetting(
GURL("http://third.com"),
GURL("http://third.com"),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string()));
}