1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/content_settings/content_settings_default_provider.h"
#include "chrome/browser/content_settings/content_settings_mock_observer.h"
#include "chrome/browser/content_settings/content_settings_utils.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using content::BrowserThread;
class DefaultProviderTest : public testing::Test {
public:
DefaultProviderTest()
: ui_thread_(BrowserThread::UI, &message_loop_),
provider_(profile_.GetPrefs(), false) {
}
~DefaultProviderTest() {
provider_.ShutdownOnUIThread();
}
protected:
MessageLoop message_loop_;
content::TestBrowserThread ui_thread_;
TestingProfile profile_;
content_settings::DefaultProvider provider_;
};
TEST_F(DefaultProviderTest, DefaultValues) {
// Check setting defaults.
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_ASK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
false));
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
false));
scoped_ptr<base::Value> value(
GetContentSettingValue(&provider_,
GURL("http://example.com/"),
GURL("http://example.com/"),
CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
std::string(),
false));
EXPECT_FALSE(value.get());
}
TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) {
GURL primary_url("http://www.google.com");
GURL secondary_url("http://www.google.com");
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
primary_url,
secondary_url,
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
scoped_ptr<base::Value> value(
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
bool owned = provider_.SetWebsiteSetting(
ContentSettingsPattern::FromURL(primary_url),
ContentSettingsPattern::FromURL(secondary_url),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
value.get());
EXPECT_FALSE(owned);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
primary_url,
secondary_url,
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
}
TEST_F(DefaultProviderTest, Observer) {
content_settings::MockObserver mock_observer;
EXPECT_CALL(mock_observer,
OnContentSettingChanged(
_, _, CONTENT_SETTINGS_TYPE_IMAGES, ""));
provider_.AddObserver(&mock_observer);
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_IMAGES,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_CALL(mock_observer,
OnContentSettingChanged(
_, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
}
TEST_F(DefaultProviderTest, ObserveDefaultPref) {
PrefService* prefs = profile_.GetPrefs();
// Make a copy of the default pref value so we can reset it later.
scoped_ptr<Value> default_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
// Make a copy of the pref's new value so we can reset it later.
scoped_ptr<Value> new_value(prefs->FindPreference(
prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
// Clearing the backing pref should also clear the internal cache.
prefs->Set(prefs::kDefaultContentSettings, *default_value);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
// Reseting the pref to its previous value should update the cache.
prefs->Set(prefs::kDefaultContentSettings, *new_value);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
}
TEST_F(DefaultProviderTest, OffTheRecord) {
content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&otr_provider,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
true));
// Changing content settings on the main provider should also affect the
// incognito map.
provider_.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&otr_provider,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
true));
// Changing content settings on the incognito provider should be ignored.
scoped_ptr<base::Value> value(
Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
bool owned = otr_provider.SetWebsiteSetting(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
value.get());
EXPECT_FALSE(owned);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
false));
EXPECT_EQ(CONTENT_SETTING_BLOCK,
GetContentSetting(&otr_provider,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_COOKIES,
std::string(),
true));
otr_provider.ShutdownOnUIThread();
}
TEST_F(DefaultProviderTest, MigrateDefaultGeolocationContentSettingAfterSync) {
EXPECT_EQ(CONTENT_SETTING_ASK,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
false));
content_settings::MockObserver mock_observer;
EXPECT_CALL(mock_observer,
OnContentSettingChanged(
_, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
provider_.AddObserver(&mock_observer);
// Set obsolete preference and test if it is migrated correctly. This can
// happen when an old version of chrome syncs the obsolete default geolocation
// preference.
PrefService* prefs = profile_.GetPrefs();
prefs->SetInteger(prefs::kGeolocationDefaultContentSetting,
CONTENT_SETTING_ALLOW);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider_,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
false));
}
TEST_F(DefaultProviderTest, MigrateDefaultGeolocationContentSettingAtStartup) {
TestingProfile profile;
TestingPrefService* prefs = profile.GetTestingPrefService();
// Set obsolete preference and test if it is migrated correctly.
prefs->SetInteger(prefs::kGeolocationDefaultContentSetting,
CONTENT_SETTING_ALLOW);
// Create a new |DefaultProvider| to test whether the obsolete default
// geolocation setting is correctly migrated. The migrated settings should be
// available right after creation of the provider.
content_settings::DefaultProvider provider(prefs, false);
EXPECT_EQ(CONTENT_SETTING_ALLOW,
GetContentSetting(&provider,
GURL(),
GURL(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
false));
provider.ShutdownOnUIThread();
}
|