Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_policy_provider_unittest.cc
bloba40cdfb8862ba12af83627414cabfe3519eee2c0
1 // Copyright (c) 2011 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.
5 #include "components/content_settings/core/browser/content_settings_policy_provider.h"
7 #include <string>
9 #include "base/auto_reset.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/content_settings/core/browser/content_settings_rule.h"
20 #include "components/content_settings/core/browser/content_settings_utils.h"
21 #include "components/content_settings/core/common/content_settings_pattern.h"
22 #include "components/content_settings/core/test/content_settings_test_utils.h"
23 #include "components/syncable_prefs/testing_pref_service_syncable.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 using ::testing::_;
30 namespace content_settings {
32 typedef std::vector<Rule> Rules;
34 class PolicyProviderTest : public testing::Test {
35 content::TestBrowserThreadBundle thread_bundle_;
38 TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) {
39 TestingProfile profile;
40 syncable_prefs::TestingPrefServiceSyncable* prefs =
41 profile.GetTestingPrefService();
42 PolicyProvider provider(prefs);
44 Rules rules;
46 scoped_ptr<RuleIterator> rule_iterator(
47 provider.GetRuleIterator(
48 CONTENT_SETTINGS_TYPE_GEOLOCATION,
49 std::string(),
50 false));
51 EXPECT_FALSE(rule_iterator->HasNext());
53 // Change the managed value of the default geolocation setting
54 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting,
55 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
57 rule_iterator.reset(
58 provider.GetRuleIterator(
59 CONTENT_SETTINGS_TYPE_GEOLOCATION,
60 std::string(),
61 false));
62 EXPECT_TRUE(rule_iterator->HasNext());
63 Rule rule = rule_iterator->Next();
64 EXPECT_FALSE(rule_iterator->HasNext());
66 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
67 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
68 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
70 provider.ShutdownOnUIThread();
73 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) {
74 TestingProfile profile;
75 syncable_prefs::TestingPrefServiceSyncable* prefs =
76 profile.GetTestingPrefService();
77 PolicyProvider provider(prefs);
79 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
80 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
82 scoped_ptr<RuleIterator> rule_iterator(
83 provider.GetRuleIterator(
84 CONTENT_SETTINGS_TYPE_PLUGINS,
85 std::string(),
86 false));
87 EXPECT_TRUE(rule_iterator->HasNext());
88 Rule rule = rule_iterator->Next();
89 EXPECT_FALSE(rule_iterator->HasNext());
91 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
92 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
93 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
95 provider.ShutdownOnUIThread();
98 // When a default-content-setting is set to a managed setting a
99 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
100 // if the managed setting is removed.
101 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) {
102 TestingProfile profile;
103 syncable_prefs::TestingPrefServiceSyncable* prefs =
104 profile.GetTestingPrefService();
105 PolicyProvider provider(prefs);
107 MockObserver mock_observer;
108 EXPECT_CALL(mock_observer,
109 OnContentSettingChanged(_,
111 CONTENT_SETTINGS_TYPE_DEFAULT,
112 ""));
113 provider.AddObserver(&mock_observer);
115 // Set the managed default-content-setting.
116 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting,
117 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
118 ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
119 EXPECT_CALL(mock_observer,
120 OnContentSettingChanged(_,
122 CONTENT_SETTINGS_TYPE_DEFAULT,
123 ""));
124 // Remove the managed default-content-setting.
125 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting);
126 provider.ShutdownOnUIThread();
129 TEST_F(PolicyProviderTest, GettingManagedContentSettings) {
130 TestingProfile profile;
131 syncable_prefs::TestingPrefServiceSyncable* prefs =
132 profile.GetTestingPrefService();
134 base::ListValue* value = new base::ListValue();
135 value->Append(new base::StringValue("[*.]google.com"));
136 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls,
137 value);
139 PolicyProvider provider(prefs);
141 ContentSettingsPattern yt_url_pattern =
142 ContentSettingsPattern::FromString("www.youtube.com");
143 GURL youtube_url("http://www.youtube.com");
144 GURL google_url("http://mail.google.com");
146 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
147 GetContentSetting(&provider,
148 youtube_url,
149 youtube_url,
150 CONTENT_SETTINGS_TYPE_COOKIES,
151 std::string(),
152 false));
153 EXPECT_EQ(NULL,
154 GetContentSettingValue(&provider,
155 youtube_url,
156 youtube_url,
157 CONTENT_SETTINGS_TYPE_COOKIES,
158 std::string(),
159 false));
161 EXPECT_EQ(CONTENT_SETTING_BLOCK,
162 GetContentSetting(&provider,
163 google_url,
164 google_url,
165 CONTENT_SETTINGS_TYPE_IMAGES,
166 std::string(),
167 false));
168 scoped_ptr<base::Value> value_ptr(
169 GetContentSettingValue(&provider,
170 google_url,
171 google_url,
172 CONTENT_SETTINGS_TYPE_IMAGES,
173 std::string(),
174 false));
176 int int_value = -1;
177 value_ptr->GetAsInteger(&int_value);
178 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
180 // The PolicyProvider does not allow setting content settings as they are
181 // enforced via policies and not set by the user or extension. So a call to
182 // SetWebsiteSetting does nothing.
183 scoped_ptr<base::Value> value_block(
184 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
185 bool owned = provider.SetWebsiteSetting(yt_url_pattern,
186 yt_url_pattern,
187 CONTENT_SETTINGS_TYPE_COOKIES,
188 std::string(),
189 value_block.get());
190 EXPECT_FALSE(owned);
191 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
192 GetContentSetting(&provider,
193 youtube_url,
194 youtube_url,
195 CONTENT_SETTINGS_TYPE_COOKIES,
196 std::string(),
197 false));
199 provider.ShutdownOnUIThread();
202 TEST_F(PolicyProviderTest, ResourceIdentifier) {
203 TestingProfile profile;
204 syncable_prefs::TestingPrefServiceSyncable* prefs =
205 profile.GetTestingPrefService();
207 base::ListValue* value = new base::ListValue();
208 value->Append(new base::StringValue("[*.]google.com"));
209 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls,
210 value);
212 PolicyProvider provider(prefs);
214 GURL youtube_url("http://www.youtube.com");
215 GURL google_url("http://mail.google.com");
217 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
218 GetContentSetting(
219 &provider, youtube_url, youtube_url,
220 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
222 // There is currently no policy support for resource content settings.
223 // Resource identifiers are simply ignored by the PolicyProvider.
224 EXPECT_EQ(CONTENT_SETTING_ALLOW,
225 GetContentSetting(&provider,
226 google_url,
227 google_url,
228 CONTENT_SETTINGS_TYPE_PLUGINS,
229 std::string(),
230 false));
232 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
233 GetContentSetting(
234 &provider, google_url, google_url,
235 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
237 provider.ShutdownOnUIThread();
240 TEST_F(PolicyProviderTest, AutoSelectCertificateList) {
241 TestingProfile profile;
242 syncable_prefs::TestingPrefServiceSyncable* prefs =
243 profile.GetTestingPrefService();
245 PolicyProvider provider(prefs);
246 GURL google_url("https://mail.google.com");
247 // Tests the default setting for auto selecting certificates
248 EXPECT_EQ(
249 NULL,
250 GetContentSettingValue(&provider,
251 google_url,
252 google_url,
253 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
254 std::string(),
255 false));
257 // Set the content settings pattern list for origins to auto select
258 // certificates.
259 std::string pattern_str("\"pattern\":\"[*.]google.com\"");
260 std::string filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
261 base::ListValue* value = new base::ListValue();
262 value->Append(
263 new base::StringValue("{" + pattern_str + "," + filter_str + "}"));
264 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls,
265 value);
266 GURL youtube_url("https://www.youtube.com");
267 EXPECT_EQ(
268 NULL,
269 GetContentSettingValue(&provider,
270 youtube_url,
271 youtube_url,
272 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
273 std::string(),
274 false));
275 scoped_ptr<base::Value> cert_filter(
276 GetContentSettingValue(&provider,
277 google_url,
278 google_url,
279 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
280 std::string(),
281 false));
283 ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType());
284 base::DictionaryValue* dict_value =
285 static_cast<base::DictionaryValue*>(cert_filter.get());
286 std::string actual_common_name;
287 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name));
288 EXPECT_EQ("issuer name", actual_common_name);
289 provider.ShutdownOnUIThread();
292 } // namespace content_settings