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

Side by Side Diff: chrome/browser/content_settings/host_content_settings_map_unittest.cc

Issue 1873343002: [Policy] HostContentSettingsMap: Add AreUserExceptionsAllowedForType() and tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add DCHECK in AreUserExceptionsAllowedForType(). 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 unified diff | Download patch
« no previous file with comments | « no previous file | components/content_settings/core/browser/host_content_settings_map.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory>
6 #include <string>
7
5 #include "base/auto_reset.h" 8 #include "base/auto_reset.h"
6 #include "base/command_line.h" 9 #include "base/command_line.h"
7 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/content_settings/content_settings_mock_observer.h" 13 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
11 #include "chrome/browser/content_settings/cookie_settings_factory.h" 14 #include "chrome/browser/content_settings/cookie_settings_factory.h"
12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
13 #include "chrome/browser/content_settings/mock_settings_observer.h" 16 #include "chrome/browser/content_settings/mock_settings_observer.h"
14 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
(...skipping 25 matching lines...) Expand all
40 const std::string& GetPrefName(ContentSettingsType type) { 43 const std::string& GetPrefName(ContentSettingsType type) {
41 return content_settings::WebsiteSettingsRegistry::GetInstance() 44 return content_settings::WebsiteSettingsRegistry::GetInstance()
42 ->Get(type) 45 ->Get(type)
43 ->pref_name(); 46 ->pref_name();
44 } 47 }
45 48
46 base::MessageLoop message_loop_; 49 base::MessageLoop message_loop_;
47 content::TestBrowserThread ui_thread_; 50 content::TestBrowserThread ui_thread_;
48 }; 51 };
49 52
53 // Wrapper to TestingProfile to reduce test boilerplates, by keeping a fixed
54 // |content_type| so caller only need to specify it once.
55 class TesterForType {
56 public:
57 TesterForType(TestingProfile *profile, ContentSettingsType content_type)
58 : prefs_(profile->GetTestingPrefService()),
59 host_content_settings_map_(
60 HostContentSettingsMapFactory::GetForProfile(profile)),
61 content_type_(content_type) {
62 switch (content_type_) {
63 case CONTENT_SETTINGS_TYPE_COOKIES:
64 policy_default_setting_ = prefs::kManagedDefaultCookiesSetting;
65 break;
66 case CONTENT_SETTINGS_TYPE_POPUPS:
67 policy_default_setting_ = prefs::kManagedDefaultPopupsSetting;
68 break;
69 default:
70 // Add support as needed.
71 NOTREACHED();
72 }
73 }
74
75 void ClearPolicyDefault() {
76 prefs_->RemoveManagedPref(policy_default_setting_);
77 }
78
79 void SetPolicyDefault(ContentSetting setting) {
80 prefs_->SetManagedPref(policy_default_setting_,
81 new base::FundamentalValue(setting));
82 }
83
84 bool AreUserExceptionsAllowed() {
85 return host_content_settings_map_->AreUserExceptionsAllowedForType(
86 content_type_);
87 }
88
89 void AddUserException(std::string exception,
90 ContentSetting content_settings) {
91 ContentSettingsPattern pattern =
92 ContentSettingsPattern::FromString(exception);
93 host_content_settings_map_->SetContentSettingCustomScope(
94 pattern, pattern, content_type_, std::string(), content_settings);
95 }
96
97 // Wrapper to query GetWebsiteSetting(), and only return the source.
98 content_settings::SettingSource GetSettingSourceForURL(
99 const std::string& url_str) {
100 GURL url(url_str);
101 content_settings::SettingInfo setting_info;
102 scoped_ptr<base::Value> result =
103 host_content_settings_map_->GetWebsiteSetting(
104 url, url, content_type_, std::string(),
105 &setting_info);
106 return setting_info.source;
107 };
108
109 private:
110 syncable_prefs::TestingPrefServiceSyncable* prefs_;
111 HostContentSettingsMap* host_content_settings_map_;
112 ContentSettingsType content_type_;
113 const char* policy_default_setting_;
114
115 DISALLOW_COPY_AND_ASSIGN(TesterForType);
116 };
117
50 TEST_F(HostContentSettingsMapTest, DefaultValues) { 118 TEST_F(HostContentSettingsMapTest, DefaultValues) {
51 TestingProfile profile; 119 TestingProfile profile;
52 HostContentSettingsMap* host_content_settings_map = 120 HostContentSettingsMap* host_content_settings_map =
53 HostContentSettingsMapFactory::GetForProfile(&profile); 121 HostContentSettingsMapFactory::GetForProfile(&profile);
54 122
55 // Check setting defaults. 123 // Check setting defaults.
56 EXPECT_EQ(CONTENT_SETTING_ALLOW, 124 EXPECT_EQ(CONTENT_SETTING_ALLOW,
57 host_content_settings_map->GetDefaultContentSetting( 125 host_content_settings_map->GetDefaultContentSetting(
58 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL)); 126 CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL));
59 host_content_settings_map->SetDefaultContentSetting( 127 host_content_settings_map->SetDefaultContentSetting(
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 scoped_ptr<base::Value> stored_value = 834 scoped_ptr<base::Value> stored_value =
767 host_content_settings_map->GetWebsiteSetting( 835 host_content_settings_map->GetWebsiteSetting(
768 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(), 836 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, std::string(),
769 nullptr); 837 nullptr);
770 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value)); 838 EXPECT_TRUE(stored_value && stored_value->Equals(&test_value));
771 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting( 839 EXPECT_EQ(nullptr, otr_map->GetWebsiteSetting(
772 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, 840 host, host, CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA,
773 std::string(), nullptr)); 841 std::string(), nullptr));
774 } 842 }
775 843
844 TEST_F(HostContentSettingsMapTest, AreUserExceptionsAllowedForType) {
845 ContentSettingsType kContentTypesToTest[] = {
846 CONTENT_SETTINGS_TYPE_COOKIES,
847 CONTENT_SETTINGS_TYPE_POPUPS,
848 };
849
850 TestingProfile profile;
851
852 for (ContentSettingsType type : kContentTypesToTest) {
853 TesterForType tester(&profile, type);
854
855 // No settings: Yes.
856 tester.ClearPolicyDefault();
857 EXPECT_TRUE(tester.AreUserExceptionsAllowed());
858
859 // Policy enforces default value: No.
860 tester.SetPolicyDefault(CONTENT_SETTING_ALLOW);
861 EXPECT_FALSE(tester.AreUserExceptionsAllowed());
862 tester.SetPolicyDefault(CONTENT_SETTING_BLOCK);
863 EXPECT_FALSE(tester.AreUserExceptionsAllowed());
864
865 // Cleanup for next iteration.
866 tester.ClearPolicyDefault();
867 }
868 }
869
870 TEST_F(HostContentSettingsMapTest, PrefExceptionsOperation) {
871 using content_settings::SETTING_SOURCE_POLICY;
872 using content_settings::SETTING_SOURCE_USER;
873
874 const char kUrl1[] = "http://user_exception_allow.com";
875 const char kUrl2[] = "http://user_exception_block.com";
876 const char kUrl3[] = "http://non_exception.com";
877
878 TestingProfile profile;
879 // Arbitrarily using cookies as content type to test.
880 TesterForType tester(&profile, CONTENT_SETTINGS_TYPE_COOKIES);
881
882 // Add |kUrl1| and |kUrl2| only.
883 tester.AddUserException(kUrl1, CONTENT_SETTING_ALLOW);
884 tester.AddUserException(kUrl2, CONTENT_SETTING_BLOCK);
885
886 // No policy setting: follow users settings.
887 tester.ClearPolicyDefault();
888 // User exceptions.
889 EXPECT_EQ(SETTING_SOURCE_USER, tester.GetSettingSourceForURL(kUrl1));
890 EXPECT_EQ(SETTING_SOURCE_USER, tester.GetSettingSourceForURL(kUrl2));
891 // User default.
892 EXPECT_EQ(SETTING_SOURCE_USER, tester.GetSettingSourceForURL(kUrl3));
893
894 // Policy overrides users always.
895 tester.SetPolicyDefault(CONTENT_SETTING_ALLOW);
896 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl1));
897 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl2));
898 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl3));
899 tester.SetPolicyDefault(CONTENT_SETTING_BLOCK);
900 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl1));
901 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl2));
902 EXPECT_EQ(SETTING_SOURCE_POLICY, tester.GetSettingSourceForURL(kUrl3));
903 }
904
776 // For a single Unicode encoded pattern, check if it gets converted to punycode 905 // For a single Unicode encoded pattern, check if it gets converted to punycode
777 // and old pattern gets deleted. 906 // and old pattern gets deleted.
778 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) { 907 TEST_F(HostContentSettingsMapTest, CanonicalizeExceptionsUnicodeOnly) {
779 TestingProfile profile; 908 TestingProfile profile;
780 PrefService* prefs = profile.GetPrefs(); 909 PrefService* prefs = profile.GetPrefs();
781 910
782 // Set utf-8 data. 911 // Set utf-8 data.
783 { 912 {
784 DictionaryPrefUpdate update(prefs, 913 DictionaryPrefUpdate update(prefs,
785 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS)); 914 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS));
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
1184 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); 1313 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1185 1314
1186 // After migrating old settings, changes to the setting works. 1315 // After migrating old settings, changes to the setting works.
1187 host_content_settings_map->SetContentSettingDefaultScope( 1316 host_content_settings_map->SetContentSettingDefaultScope(
1188 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), 1317 host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(),
1189 CONTENT_SETTING_BLOCK); 1318 CONTENT_SETTING_BLOCK);
1190 EXPECT_EQ(CONTENT_SETTING_BLOCK, 1319 EXPECT_EQ(CONTENT_SETTING_BLOCK,
1191 host_content_settings_map->GetContentSetting( 1320 host_content_settings_map->GetContentSetting(
1192 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); 1321 host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string()));
1193 } 1322 }
OLDNEW
« no previous file with comments | « no previous file | components/content_settings/core/browser/host_content_settings_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698