Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_policy_provider_unittest.cc
blob0959493d606b4f080f06fcc85bc6de19aba02e60
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/common/content_settings_pattern.h"
23 #include "components/content_settings/core/test/content_settings_test_utils.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 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
41 PolicyProvider provider(prefs);
43 Rules rules;
45 scoped_ptr<RuleIterator> rule_iterator(
46 provider.GetRuleIterator(
47 CONTENT_SETTINGS_TYPE_GEOLOCATION,
48 std::string(),
49 false));
50 EXPECT_FALSE(rule_iterator->HasNext());
52 // Change the managed value of the default geolocation setting
53 prefs->SetManagedPref(prefs::kManagedDefaultGeolocationSetting,
54 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
56 rule_iterator.reset(
57 provider.GetRuleIterator(
58 CONTENT_SETTINGS_TYPE_GEOLOCATION,
59 std::string(),
60 false));
61 EXPECT_TRUE(rule_iterator->HasNext());
62 Rule rule = rule_iterator->Next();
63 EXPECT_FALSE(rule_iterator->HasNext());
65 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
66 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
67 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
69 provider.ShutdownOnUIThread();
72 TEST_F(PolicyProviderTest, ManagedDefaultContentSettings) {
73 TestingProfile profile;
74 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
75 PolicyProvider provider(prefs);
77 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting,
78 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
80 scoped_ptr<RuleIterator> rule_iterator(
81 provider.GetRuleIterator(
82 CONTENT_SETTINGS_TYPE_PLUGINS,
83 std::string(),
84 false));
85 EXPECT_TRUE(rule_iterator->HasNext());
86 Rule rule = rule_iterator->Next();
87 EXPECT_FALSE(rule_iterator->HasNext());
89 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.primary_pattern);
90 EXPECT_EQ(ContentSettingsPattern::Wildcard(), rule.secondary_pattern);
91 EXPECT_EQ(CONTENT_SETTING_BLOCK, ValueToContentSetting(rule.value.get()));
93 provider.ShutdownOnUIThread();
96 // When a default-content-setting is set to a managed setting a
97 // CONTENT_SETTINGS_CHANGED notification should be fired. The same should happen
98 // if the managed setting is removed.
99 TEST_F(PolicyProviderTest, ObserveManagedSettingsChange) {
100 TestingProfile profile;
101 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
102 PolicyProvider provider(prefs);
104 MockObserver mock_observer;
105 EXPECT_CALL(mock_observer,
106 OnContentSettingChanged(_,
108 CONTENT_SETTINGS_TYPE_DEFAULT,
109 ""));
110 provider.AddObserver(&mock_observer);
112 // Set the managed default-content-setting.
113 prefs->SetManagedPref(prefs::kManagedDefaultImagesSetting,
114 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
115 ::testing::Mock::VerifyAndClearExpectations(&mock_observer);
116 EXPECT_CALL(mock_observer,
117 OnContentSettingChanged(_,
119 CONTENT_SETTINGS_TYPE_DEFAULT,
120 ""));
121 // Remove the managed default-content-setting.
122 prefs->RemoveManagedPref(prefs::kManagedDefaultImagesSetting);
123 provider.ShutdownOnUIThread();
126 TEST_F(PolicyProviderTest, GettingManagedContentSettings) {
127 TestingProfile profile;
128 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
130 base::ListValue* value = new base::ListValue();
131 value->Append(new base::StringValue("[*.]google.com"));
132 prefs->SetManagedPref(prefs::kManagedImagesBlockedForUrls,
133 value);
135 PolicyProvider provider(prefs);
137 ContentSettingsPattern yt_url_pattern =
138 ContentSettingsPattern::FromString("www.youtube.com");
139 GURL youtube_url("http://www.youtube.com");
140 GURL google_url("http://mail.google.com");
142 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
143 GetContentSetting(&provider,
144 youtube_url,
145 youtube_url,
146 CONTENT_SETTINGS_TYPE_COOKIES,
147 std::string(),
148 false));
149 EXPECT_EQ(NULL,
150 GetContentSettingValue(&provider,
151 youtube_url,
152 youtube_url,
153 CONTENT_SETTINGS_TYPE_COOKIES,
154 std::string(),
155 false));
157 EXPECT_EQ(CONTENT_SETTING_BLOCK,
158 GetContentSetting(&provider,
159 google_url,
160 google_url,
161 CONTENT_SETTINGS_TYPE_IMAGES,
162 std::string(),
163 false));
164 scoped_ptr<base::Value> value_ptr(
165 GetContentSettingValue(&provider,
166 google_url,
167 google_url,
168 CONTENT_SETTINGS_TYPE_IMAGES,
169 std::string(),
170 false));
172 int int_value = -1;
173 value_ptr->GetAsInteger(&int_value);
174 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
176 // The PolicyProvider does not allow setting content settings as they are
177 // enforced via policies and not set by the user or extension. So a call to
178 // SetWebsiteSetting does nothing.
179 scoped_ptr<base::Value> value_block(
180 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
181 bool owned = provider.SetWebsiteSetting(yt_url_pattern,
182 yt_url_pattern,
183 CONTENT_SETTINGS_TYPE_COOKIES,
184 std::string(),
185 value_block.get());
186 EXPECT_FALSE(owned);
187 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
188 GetContentSetting(&provider,
189 youtube_url,
190 youtube_url,
191 CONTENT_SETTINGS_TYPE_COOKIES,
192 std::string(),
193 false));
195 provider.ShutdownOnUIThread();
198 TEST_F(PolicyProviderTest, ResourceIdentifier) {
199 TestingProfile profile;
200 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
202 base::ListValue* value = new base::ListValue();
203 value->Append(new base::StringValue("[*.]google.com"));
204 prefs->SetManagedPref(prefs::kManagedPluginsAllowedForUrls,
205 value);
207 PolicyProvider provider(prefs);
209 GURL youtube_url("http://www.youtube.com");
210 GURL google_url("http://mail.google.com");
212 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
213 GetContentSetting(
214 &provider, youtube_url, youtube_url,
215 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
217 // There is currently no policy support for resource content settings.
218 // Resource identifiers are simply ignored by the PolicyProvider.
219 EXPECT_EQ(CONTENT_SETTING_ALLOW,
220 GetContentSetting(&provider,
221 google_url,
222 google_url,
223 CONTENT_SETTINGS_TYPE_PLUGINS,
224 std::string(),
225 false));
227 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
228 GetContentSetting(
229 &provider, google_url, google_url,
230 CONTENT_SETTINGS_TYPE_PLUGINS, "someplugin", false));
232 provider.ShutdownOnUIThread();
235 TEST_F(PolicyProviderTest, AutoSelectCertificateList) {
236 TestingProfile profile;
237 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
239 PolicyProvider provider(prefs);
240 GURL google_url("https://mail.google.com");
241 // Tests the default setting for auto selecting certificates
242 EXPECT_EQ(
243 NULL,
244 GetContentSettingValue(&provider,
245 google_url,
246 google_url,
247 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
248 std::string(),
249 false));
251 // Set the content settings pattern list for origins to auto select
252 // certificates.
253 std::string pattern_str("\"pattern\":\"[*.]google.com\"");
254 std::string filter_str("\"filter\":{\"ISSUER\":{\"CN\":\"issuer name\"}}");
255 base::ListValue* value = new base::ListValue();
256 value->Append(
257 new base::StringValue("{" + pattern_str + "," + filter_str + "}"));
258 prefs->SetManagedPref(prefs::kManagedAutoSelectCertificateForUrls,
259 value);
260 GURL youtube_url("https://www.youtube.com");
261 EXPECT_EQ(
262 NULL,
263 GetContentSettingValue(&provider,
264 youtube_url,
265 youtube_url,
266 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
267 std::string(),
268 false));
269 scoped_ptr<base::Value> cert_filter(
270 GetContentSettingValue(&provider,
271 google_url,
272 google_url,
273 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
274 std::string(),
275 false));
277 ASSERT_EQ(base::Value::TYPE_DICTIONARY, cert_filter->GetType());
278 base::DictionaryValue* dict_value =
279 static_cast<base::DictionaryValue*>(cert_filter.get());
280 std::string actual_common_name;
281 ASSERT_TRUE(dict_value->GetString("ISSUER.CN", &actual_common_name));
282 EXPECT_EQ("issuer name", actual_common_name);
283 provider.ShutdownOnUIThread();
286 } // namespace content_settings