Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_default_provider_unittest.cc
blobc477a00cdfdf936cb52acd0611dc9b48e631cb0f
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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/content_settings_default_provider.h"
14 #include "components/content_settings/core/browser/content_settings_utils.h"
15 #include "components/content_settings/core/test/content_settings_test_utils.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 using ::testing::_;
21 class DefaultProviderTest : public testing::Test {
22 public:
23 DefaultProviderTest()
24 : provider_(profile_.GetPrefs(), false) {
26 ~DefaultProviderTest() override { provider_.ShutdownOnUIThread(); }
28 protected:
29 TestingProfile profile_;
30 content_settings::DefaultProvider provider_;
33 TEST_F(DefaultProviderTest, DefaultValues) {
34 // Check setting defaults.
35 EXPECT_EQ(CONTENT_SETTING_ALLOW,
36 GetContentSetting(&provider_,
37 GURL(),
38 GURL(),
39 CONTENT_SETTINGS_TYPE_COOKIES,
40 std::string(),
41 false));
42 provider_.SetWebsiteSetting(
43 ContentSettingsPattern::Wildcard(),
44 ContentSettingsPattern::Wildcard(),
45 CONTENT_SETTINGS_TYPE_COOKIES,
46 std::string(),
47 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
48 EXPECT_EQ(CONTENT_SETTING_BLOCK,
49 GetContentSetting(&provider_,
50 GURL(),
51 GURL(),
52 CONTENT_SETTINGS_TYPE_COOKIES,
53 std::string(),
54 false));
56 EXPECT_EQ(CONTENT_SETTING_ASK,
57 GetContentSetting(&provider_,
58 GURL(),
59 GURL(),
60 CONTENT_SETTINGS_TYPE_GEOLOCATION,
61 std::string(),
62 false));
63 provider_.SetWebsiteSetting(
64 ContentSettingsPattern::Wildcard(),
65 ContentSettingsPattern::Wildcard(),
66 CONTENT_SETTINGS_TYPE_GEOLOCATION,
67 std::string(),
68 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
69 EXPECT_EQ(CONTENT_SETTING_BLOCK,
70 GetContentSetting(&provider_,
71 GURL(),
72 GURL(),
73 CONTENT_SETTINGS_TYPE_GEOLOCATION,
74 std::string(),
75 false));
77 scoped_ptr<base::Value> value(
78 GetContentSettingValue(&provider_,
79 GURL("http://example.com/"),
80 GURL("http://example.com/"),
81 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
82 std::string(),
83 false));
84 EXPECT_FALSE(value.get());
87 TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) {
88 GURL primary_url("http://www.google.com");
89 GURL secondary_url("http://www.google.com");
91 EXPECT_EQ(CONTENT_SETTING_ALLOW,
92 GetContentSetting(&provider_,
93 primary_url,
94 secondary_url,
95 CONTENT_SETTINGS_TYPE_COOKIES,
96 std::string(),
97 false));
98 scoped_ptr<base::Value> value(
99 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
100 bool owned = provider_.SetWebsiteSetting(
101 ContentSettingsPattern::FromURL(primary_url),
102 ContentSettingsPattern::FromURL(secondary_url),
103 CONTENT_SETTINGS_TYPE_COOKIES,
104 std::string(),
105 value.get());
106 EXPECT_FALSE(owned);
107 EXPECT_EQ(CONTENT_SETTING_ALLOW,
108 GetContentSetting(&provider_,
109 primary_url,
110 secondary_url,
111 CONTENT_SETTINGS_TYPE_COOKIES,
112 std::string(),
113 false));
116 TEST_F(DefaultProviderTest, Observer) {
117 content_settings::MockObserver mock_observer;
118 EXPECT_CALL(mock_observer,
119 OnContentSettingChanged(
120 _, _, CONTENT_SETTINGS_TYPE_IMAGES, ""));
121 provider_.AddObserver(&mock_observer);
122 provider_.SetWebsiteSetting(
123 ContentSettingsPattern::Wildcard(),
124 ContentSettingsPattern::Wildcard(),
125 CONTENT_SETTINGS_TYPE_IMAGES,
126 std::string(),
127 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
129 EXPECT_CALL(mock_observer,
130 OnContentSettingChanged(
131 _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
132 provider_.SetWebsiteSetting(
133 ContentSettingsPattern::Wildcard(),
134 ContentSettingsPattern::Wildcard(),
135 CONTENT_SETTINGS_TYPE_GEOLOCATION,
136 std::string(),
137 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
141 TEST_F(DefaultProviderTest, ObserveDefaultPref) {
142 PrefService* prefs = profile_.GetPrefs();
144 // Make a copy of the default pref value so we can reset it later.
145 scoped_ptr<base::Value> default_value(prefs->FindPreference(
146 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
148 provider_.SetWebsiteSetting(
149 ContentSettingsPattern::Wildcard(),
150 ContentSettingsPattern::Wildcard(),
151 CONTENT_SETTINGS_TYPE_COOKIES,
152 std::string(),
153 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
154 EXPECT_EQ(CONTENT_SETTING_BLOCK,
155 GetContentSetting(&provider_,
156 GURL(),
157 GURL(),
158 CONTENT_SETTINGS_TYPE_COOKIES,
159 std::string(),
160 false));
162 // Make a copy of the pref's new value so we can reset it later.
163 scoped_ptr<base::Value> new_value(prefs->FindPreference(
164 prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
166 // Clearing the backing pref should also clear the internal cache.
167 prefs->Set(prefs::kDefaultContentSettings, *default_value);
168 EXPECT_EQ(CONTENT_SETTING_ALLOW,
169 GetContentSetting(&provider_,
170 GURL(),
171 GURL(),
172 CONTENT_SETTINGS_TYPE_COOKIES,
173 std::string(),
174 false));
175 // Reseting the pref to its previous value should update the cache.
176 prefs->Set(prefs::kDefaultContentSettings, *new_value);
177 EXPECT_EQ(CONTENT_SETTING_BLOCK,
178 GetContentSetting(&provider_,
179 GURL(),
180 GURL(),
181 CONTENT_SETTINGS_TYPE_COOKIES,
182 std::string(),
183 false));
186 TEST_F(DefaultProviderTest, OffTheRecord) {
187 content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
189 EXPECT_EQ(CONTENT_SETTING_ALLOW,
190 GetContentSetting(&provider_,
191 GURL(),
192 GURL(),
193 CONTENT_SETTINGS_TYPE_COOKIES,
194 std::string(),
195 false));
196 EXPECT_EQ(CONTENT_SETTING_ALLOW,
197 GetContentSetting(&otr_provider,
198 GURL(),
199 GURL(),
200 CONTENT_SETTINGS_TYPE_COOKIES,
201 std::string(),
202 true));
204 // Changing content settings on the main provider should also affect the
205 // incognito map.
206 provider_.SetWebsiteSetting(
207 ContentSettingsPattern::Wildcard(),
208 ContentSettingsPattern::Wildcard(),
209 CONTENT_SETTINGS_TYPE_COOKIES,
210 std::string(),
211 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
212 EXPECT_EQ(CONTENT_SETTING_BLOCK,
213 GetContentSetting(&provider_,
214 GURL(),
215 GURL(),
216 CONTENT_SETTINGS_TYPE_COOKIES,
217 std::string(),
218 false));
220 EXPECT_EQ(CONTENT_SETTING_BLOCK,
221 GetContentSetting(&otr_provider,
222 GURL(),
223 GURL(),
224 CONTENT_SETTINGS_TYPE_COOKIES,
225 std::string(),
226 true));
228 // Changing content settings on the incognito provider should be ignored.
229 scoped_ptr<base::Value> value(
230 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
231 bool owned = otr_provider.SetWebsiteSetting(
232 ContentSettingsPattern::Wildcard(),
233 ContentSettingsPattern::Wildcard(),
234 CONTENT_SETTINGS_TYPE_COOKIES,
235 std::string(),
236 value.get());
237 EXPECT_FALSE(owned);
238 EXPECT_EQ(CONTENT_SETTING_BLOCK,
239 GetContentSetting(&provider_,
240 GURL(),
241 GURL(),
242 CONTENT_SETTINGS_TYPE_COOKIES,
243 std::string(),
244 false));
246 EXPECT_EQ(CONTENT_SETTING_BLOCK,
247 GetContentSetting(&otr_provider,
248 GURL(),
249 GURL(),
250 CONTENT_SETTINGS_TYPE_COOKIES,
251 std::string(),
252 true));
253 otr_provider.ShutdownOnUIThread();
257 // TODO(msramek): The two tests below test syncing between old versions
258 // of Chrome using a dictionary pref and new versions using individual integer
259 // prefs for default content settings. Remove the tests together with
260 // the dictionary setting after two stable releases.
261 TEST_F(DefaultProviderTest, SyncFromDictionaryToIndividualPreferences) {
262 PrefService* prefs = profile_.GetPrefs();
265 DictionaryPrefUpdate update(prefs, prefs::kDefaultContentSettings);
266 base::DictionaryValue* default_settings_dictionary = update.Get();
268 default_settings_dictionary->SetWithoutPathExpansion(
269 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES),
270 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
271 default_settings_dictionary->SetWithoutPathExpansion(
272 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_GEOLOCATION),
273 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
276 // Cookies should sync, but geolocation should not.
277 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(
278 prefs->GetInteger(prefs::kDefaultCookiesSetting)));
279 EXPECT_EQ(CONTENT_SETTING_ASK, IntToContentSetting(
280 prefs->GetInteger(prefs::kDefaultGeolocationSetting)));
283 TEST_F(DefaultProviderTest, SyncFromIndividualPreferencesToDictionary) {
284 PrefService* prefs = profile_.GetPrefs();
286 prefs->SetInteger(prefs::kDefaultJavaScriptSetting, CONTENT_SETTING_BLOCK);
287 prefs->SetInteger(prefs::kDefaultSSLCertDecisionsSetting,
288 CONTENT_SETTING_BLOCK);
290 // Javascript should sync, but cert decisions should not.
291 const base::DictionaryValue* default_settings_dictionary =
292 prefs->GetDictionary(prefs::kDefaultContentSettings);
293 int js_setting;
294 bool has_cd_setting;
296 default_settings_dictionary->GetIntegerWithoutPathExpansion(
297 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_JAVASCRIPT),
298 &js_setting);
299 has_cd_setting = default_settings_dictionary->HasKey(
300 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS));
302 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(js_setting));
303 EXPECT_FALSE(has_cd_setting);