Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Unified Diff: chrome/browser/content_settings/host_content_settings_map_unittest.cc

Issue 1865803002: [Policy Experimental] Add policies to allow Cookies and Pop-ups exceptions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync; compile fix in PolicyProvider::GetUserExceptionsUsageSettingForType(). Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/content_settings/host_content_settings_map_unittest.cc
diff --git a/chrome/browser/content_settings/host_content_settings_map_unittest.cc b/chrome/browser/content_settings/host_content_settings_map_unittest.cc
index 8347f04f3399693f9faa1b60bde573a69b06b5b0..ae111884e6828de6f486cf88106944e3af08ccba 100644
--- a/chrome/browser/content_settings/host_content_settings_map_unittest.cc
+++ b/chrome/browser/content_settings/host_content_settings_map_unittest.cc
@@ -19,6 +19,7 @@
#include "components/content_settings/core/browser/website_settings_info.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/pref_names.h"
+#include "components/content_settings/core/test/content_settings_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/syncable_prefs/testing_pref_service_syncable.h"
@@ -773,6 +774,150 @@ TEST_F(HostContentSettingsMapTest, OffTheRecordDontInheritSetting) {
std::string(), nullptr));
}
+TEST_F(HostContentSettingsMapTest, AreUserExceptionsAllowedForType) {
+ struct {
+ ContentSettingsType content_type;
+ const char* policy_default_setting;
+ const char* pref_exception_setting;
+ } kTypeInfos[] = {
+ {CONTENT_SETTINGS_TYPE_COOKIES, prefs::kManagedDefaultCookiesSetting,
+ prefs::kUserCookiesExceptionsUsage},
+ {CONTENT_SETTINGS_TYPE_POPUPS, prefs::kManagedDefaultPopupsSetting,
+ prefs::kUserPopupsExceptionsUsage},
+ };
+
+ TestingProfile profile;
+ syncable_prefs::TestingPrefServiceSyncable* prefs =
+ profile.GetTestingPrefService();
+ HostContentSettingsMap* host_content_settings_map =
+ HostContentSettingsMapFactory::GetForProfile(&profile);
+
+ for (const auto& type_info : kTypeInfos) {
+ // Lambdas to simplify setting valuesfor current test case.
+ auto clear_policy_default = [&]() {
+ prefs->RemoveManagedPref(type_info.policy_default_setting);
+ };
+ auto set_policy_default = [&](ContentSetting setting) {
+ prefs->SetManagedPref(type_info.policy_default_setting,
+ new base::FundamentalValue(setting));
+ };
+ auto clear_allow_user_exceptions = [&]() {
+ prefs->RemoveManagedPref(type_info.pref_exception_setting);
+ };
+ auto set_allow_user_exceptions = [&](ContentSetting setting) {
+ prefs->SetManagedPref(type_info.pref_exception_setting,
+ new base::FundamentalValue(setting));
+ };
+
+ // Main function to test: Can user edit pref exceptions?
+ auto are_pref_exceptions_allowed = [&]()->bool {
+ return host_content_settings_map->AreUserExceptionsAllowedForType(
+ type_info.content_type);
+ };
+
+ // No settings: Yes.
+ clear_allow_user_exceptions();
+ clear_policy_default();
+ EXPECT_TRUE(are_pref_exceptions_allowed());
+
+ // Policy enforces default value: No.
+ set_policy_default(CONTENT_SETTING_ALLOW);
+ EXPECT_FALSE(are_pref_exceptions_allowed());
+ set_policy_default(CONTENT_SETTING_BLOCK);
+ EXPECT_FALSE(are_pref_exceptions_allowed());
+
+ // Allow exceptions: Yes, regardless of policy default.
+ set_allow_user_exceptions(CONTENT_SETTING_ALLOW);
+ clear_policy_default();
+ EXPECT_TRUE(are_pref_exceptions_allowed());
+ set_policy_default(CONTENT_SETTING_ALLOW);
+ EXPECT_TRUE(are_pref_exceptions_allowed());
+ set_policy_default(CONTENT_SETTING_BLOCK);
+ EXPECT_TRUE(are_pref_exceptions_allowed());
+
+ // Not bothering caller clear_default() for this case; let it set.
+ }
+}
+
+TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) {
+ using content_settings::TestUtils;
+ // Arbitrarily using cookies as content type to test.
+ const ContentSettingsType kContentType = CONTENT_SETTINGS_TYPE_COOKIES;
+ const char* kPolicyDefaultSetting = prefs::kManagedDefaultCookiesSetting;
+ const char* kUserExceptionsUsage = prefs::kUserCookiesExceptionsUsage;
+ const char kUrlUserException[] = "http://user_except.com";
+ const char kUrlNonUserException[] = "http://non_user_except.com";
+
+ TestingProfile profile;
+ syncable_prefs::TestingPrefServiceSyncable* prefs =
+ profile.GetTestingPrefService();
+ HostContentSettingsMap* host_content_settings_map =
+ HostContentSettingsMapFactory::GetForProfile(&profile);
+
+ // Helpers to change policy settings.
+ auto clear_policy_default = [&]() {
+ prefs->RemoveManagedPref(kPolicyDefaultSetting);
+ };
+ auto set_policy_default = [&](ContentSetting setting) {
+ prefs->SetManagedPref(kPolicyDefaultSetting,
+ new base::FundamentalValue(setting));
+ };
+ auto clear_allow_user_exceptions = [&]() {
+ prefs->RemoveManagedPref(kUserExceptionsUsage);
+ };
+ auto set_allow_user_exceptions = [&](ContentSetting setting) {
+ prefs->SetManagedPref(kUserExceptionsUsage,
+ new base::FundamentalValue(setting));
+ };
+
+ // Wrapper to query GetWebsiteSetting(), and get the source.
+ auto get_source = [&](const std::string& url_str) ->
+ content_settings::SettingSource {
+ GURL host(url_str);
+ content_settings::SettingInfo setting_info;
+ scoped_ptr<base::Value> result =
+ host_content_settings_map->GetWebsiteSetting(
+ host, host, kContentType, std::string(),
+ &setting_info);
+ return setting_info.source;
+ };
+
+ // Add user exception.
+ TestUtils::AddPrefException(kUrlUserException, kContentType,
+ CONTENT_SETTING_ALLOW, host_content_settings_map);
+
+ using content_settings::SETTING_SOURCE_POLICY;
+ using content_settings::SETTING_SOURCE_USER;
+
+ // No policy setting: follow users settings.
+ clear_allow_user_exceptions();
+ clear_policy_default();
+ // User exceptions.
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
+ // User default.
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException));
+
+ // Policy overrids users always.
+ set_policy_default(CONTENT_SETTING_ALLOW);
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
+ set_policy_default(CONTENT_SETTING_BLOCK);
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlUserException));
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
+
+ // Allow exceptions: user if exception found, else policy default (if set).
+ set_allow_user_exceptions(CONTENT_SETTING_ALLOW);
+ clear_policy_default();
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlNonUserException));
+ set_policy_default(CONTENT_SETTING_ALLOW);
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
+ set_policy_default(CONTENT_SETTING_BLOCK);
+ EXPECT_EQ(SETTING_SOURCE_USER, get_source(kUrlUserException));
+ EXPECT_EQ(SETTING_SOURCE_POLICY, get_source(kUrlNonUserException));
+}
+
// For a single Unicode encoded pattern, check if it gets converted to punycode
// and old pattern gets deleted.
TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {

Powered by Google App Engine
This is Rietveld 408576698