Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_policy_provider_unittest.cc
blobff861bd332a36c23b2f0cac1620e0433753183c6
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_pref_service_syncable.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/content_settings/core/browser/content_settings_rule.h"
21 #include "components/content_settings/core/browser/content_settings_utils.h"
22 #include "components/content_settings/core/test/content_settings_test_utils.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h"
27 using ::testing::_;
29 namespace content_settings {
31 typedef std::vector<Rule> Rules;
33 class PolicyProviderTest : public testing::Test {
34 content::TestBrowserThreadBundle thread_bundle_;
37 TEST_F(PolicyProviderTest, DefaultGeolocationContentSetting) {
38 TestingProfile profile;
39 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
40 PolicyProvider provider(prefs);
42 Rules rules;
44 scoped_ptr<RuleIterator> rule_iterator(
45 provider.GetRuleIterator(
46 CONTENT_SETTINGS_TYPE_GEOLOCATION,
47 std::string(),
48 false));
49 EXPECT_FALSE(rule_iterator->HasNext());
51 // Change the managed value of the default geolocation setting
52 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting,
53 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
55 rule_iterator.reset(
56 provider.GetRuleIterator(
57 CONTENT_SETTINGS_TYPE_GEOLOCATION,
58 std::string(),
59 false));
60 EXPECT_TRUE(rule_iterator->HasNext());
61 Rule rule = rule_iterator->Next();
62 EXPECT_FALSE(rule_iterator->HasNext());
64 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
65 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
66 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
68 provider.ShutdownOnUIThread();
71 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) {
72 TestingProfile profile;
73 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
74 PolicyProvider provider(prefs);
76 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
77 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
79 scoped_ptr<RuleIterator> rule_iterator(
80 provider.GetRuleIterator(
81 CONTENT_SETTINGS_TYPE_PLUGINS,
82 std::string(),
83 false));
84 EXPECT_TRUE(rule_iterator->HasNext());
85 Rule rule = rule_iterator->Next();
86 EXPECT_FALSE(rule_iterator->HasNext());
88 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
89 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
90 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
92 provider.ShutdownOnUIThread();
95 // When a default-content-setting is set to a managed setting a
96 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
97 // if the managed setting is removed.
98 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) {
99 TestingProfile profile;
100 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
101 PolicyProvider provider(prefs);
103 MockObserver mock_observer;
104 EXPECT_CALL(mock_observer,
105 OnContentSettingChanged(_,
107 CONTENT_SETTINGS_TYPE_DEFAULT,
108 ""));
109 provider.AddObserver(&mock_observer);
111 // Set the managed default-content-setting.
112 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting,
113 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
114 ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
115 EXPECT_CALL(mock_observer,
116 OnContentSettingChanged(_,
118 CONTENT_SETTINGS_TYPE_DEFAULT,
119 ""));
120 // Remove the managed default-content-setting.
121 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting);
122 provider.ShutdownOnUIThread();
125 TEST_F(PolicyProviderTest, GettingManagedContentSettings) {
126 TestingProfile profile;
127 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
129 base::ListValue* value = new base::ListValue();
130 value->Append(new base::StringValue("[*.]google.com"));
131 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls,
132 value);
134 PolicyProvider provider(prefs);
136 ContentSettingsPattern yt_url_pattern =
137 ContentSettingsPattern::FromString("www.youtube.com");
138 GURL youtube_url("http://www.youtube.com");
139 GURL google_url("http://mail.google.com");
141 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
142 GetContentSetting(&provider,
143 youtube_url,
144 youtube_url,
145 CONTENT_SETTINGS_TYPE_COOKIES,
146 std::string(),
147 false));
148 EXPECT_EQ(NULL,
149 GetContentSettingValue(&provider,
150 youtube_url,
151 youtube_url,
152 CONTENT_SETTINGS_TYPE_COOKIES,
153 std::string(),
154 false));
156 EXPECT_EQ(CONTENT_SETTING_BLOCK,
157 GetContentSetting(&provider,
158 google_url,
159 google_url,
160 CONTENT_SETTINGS_TYPE_IMAGES,
161 std::string(),
162 false));
163 scoped_ptr<base::Value> value_ptr(
164 GetContentSettingValue(&provider,
165 google_url,
166 google_url,
167 CONTENT_SETTINGS_TYPE_IMAGES,
168 std::string(),
169 false));
171 int int_value = -1;
172 value_ptr->GetAsInteger(&int_value);
173 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
175 // The PolicyProvider does not allow setting content settings as they are
176 // enforced via policies and not set by the user or extension. So a call to
177 // SetWebsiteSetting does nothing.
178 scoped_ptr<base::Value> value_block(
179 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
180 bool owned = provider.SetWebsiteSetting(yt_url_pattern,
181 yt_url_pattern,
182 CONTENT_SETTINGS_TYPE_COOKIES,
183 std::string(),
184 value_block.get());
185 EXPECT_FALSE(owned);
186 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
187 GetContentSetting(&provider,
188 youtube_url,
189 youtube_url,
190 CONTENT_SETTINGS_TYPE_COOKIES,
191 std::string(),
192 false));
194 provider.ShutdownOnUIThread();
197 TEST_F(PolicyProviderTest, ResourceIdentifier) {
198 TestingProfile profile;
199 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
201 base::ListValue* value = new base::ListValue();
202 value->Append(new base::StringValue("[*.]google.com"));
203 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls,
204 value);
206 PolicyProvider provider(prefs);
208 GURL youtube_url("http://www.youtube.com");
209 GURL google_url("http://mail.google.com");
211 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
212 GetContentSetting(
213 &provider, youtube_url, youtube_url,
214 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
216 // There is currently no policy support for resource content settings.
217 // Resource identifiers are simply ignored by the PolicyProvider.
218 EXPECT_EQ(CONTENT_SETTING_ALLOW,
219 GetContentSetting(&provider,
220 google_url,
221 google_url,
222 CONTENT_SETTINGS_TYPE_PLUGINS,
223 std::string(),
224 false));
226 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
227 GetContentSetting(
228 &provider, google_url, google_url,
229 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
231 provider.ShutdownOnUIThread();
234 TEST_F(PolicyProviderTest, AutoSelectCertificateList) {
235 TestingProfile profile;
236 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
238 PolicyProvider provider(prefs);
239 GURL google_url("https://mail.google.com");
240 // Tests the default setting for auto selecting certificates
241 EXPECT_EQ(
242 NULL,
243 GetContentSettingValue(&provider,
244 google_url,
245 google_url,
246 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
247 std::string(),
248 false));
250 // Set the content settings pattern list for origins to auto select
251 // certificates.
252 std::string pattern_str("\"pattern\":\"[*.]google.com\"");
253 std::string filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
254 base::ListValue* value = new base::ListValue();
255 value->Append(
256 new base::StringValue("{" + pattern_str + "," + filter_str + "}"));
257 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls,
258 value);
259 GURL youtube_url("https://www.youtube.com");
260 EXPECT_EQ(
261 NULL,
262 GetContentSettingValue(&provider,
263 youtube_url,
264 youtube_url,
265 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
266 std::string(),
267 false));
268 scoped_ptr<base::Value> cert_filter(
269 GetContentSettingValue(&provider,
270 google_url,
271 google_url,
272 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
273 std::string(),
274 false));
276 ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType());
277 base::DictionaryValue* dict_value =
278 static_cast<base::DictionaryValue*>(cert_filter.get());
279 std::string actual_common_name;
280 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name));
281 EXPECT_EQ("issuer name", actual_common_name);
282 provider.ShutdownOnUIThread();
285 } // namespace content_settings