content_settings_pref_provider_unittest.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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.
4
5#include "chrome/browser/content_settings/content_settings_pref_provider.h"
6
7#include "base/auto_reset.h"
8#include "base/command_line.h"
9#include "chrome/browser/content_settings/stub_settings_observer.h"
10#include "chrome/browser/prefs/browser_prefs.h"
11#include "chrome/browser/prefs/default_pref_store.h"
12#include "chrome/browser/prefs/overlay_persistent_pref_store.h"
13#include "chrome/browser/prefs/pref_service.h"
14#include "chrome/browser/prefs/testing_pref_store.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/testing_browser_process_test.h"
19#include "chrome/test/testing_pref_service.h"
20#include "chrome/test/testing_profile.h"
21#include "content/browser/browser_thread.h"
22#include "googleurl/src/gurl.h"
23#include "testing/gtest/include/gtest/gtest.h"
24
25namespace {
26class ContentSettingsPrefService : public PrefService {
27 public:
28  ContentSettingsPrefService(PrefStore* managed_platform_prefs,
29                             PrefStore* managed_cloud_prefs,
30                             PrefStore* extension_prefs,
31                             PrefStore* command_line_prefs,
32                             PersistentPrefStore* user_prefs,
33                             PrefStore* recommended_platform_prefs,
34                             PrefStore* recommended_cloud_prefs,
35                             DefaultPrefStore* default_store)
36      : PrefService(
37          managed_platform_prefs, managed_cloud_prefs, extension_prefs,
38          command_line_prefs, user_prefs, recommended_platform_prefs,
39          recommended_cloud_prefs, default_store, NULL) {}
40  virtual ~ContentSettingsPrefService() {}
41};
42}
43
44namespace content_settings {
45
46class PrefDefaultProviderTest : public TestingBrowserProcessTest {
47 public:
48  PrefDefaultProviderTest()
49      : ui_thread_(BrowserThread::UI, &message_loop_) {
50  }
51
52 protected:
53  MessageLoop message_loop_;
54  BrowserThread ui_thread_;
55};
56
57TEST_F(PrefDefaultProviderTest, DefaultValues) {
58  TestingProfile profile;
59  content_settings::PrefDefaultProvider provider(&profile);
60
61  ASSERT_FALSE(
62      provider.DefaultSettingIsManaged(CONTENT_SETTINGS_TYPE_COOKIES));
63
64  // Check setting defaults.
65  EXPECT_EQ(CONTENT_SETTING_ALLOW,
66            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
67  provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
68                                CONTENT_SETTING_BLOCK);
69  EXPECT_EQ(CONTENT_SETTING_BLOCK,
70            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
71  provider.ResetToDefaults();
72  EXPECT_EQ(CONTENT_SETTING_ALLOW,
73            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
74}
75
76TEST_F(PrefDefaultProviderTest, Observer) {
77  TestingProfile profile;
78  PrefDefaultProvider provider(&profile);
79  StubSettingsObserver observer;
80
81  provider.UpdateDefaultSetting(
82      CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
83  EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
84  EXPECT_TRUE(observer.last_update_all);
85  EXPECT_FALSE(observer.last_update_all_types);
86  EXPECT_EQ(1, observer.counter);
87}
88
89TEST_F(PrefDefaultProviderTest, ObserveDefaultPref) {
90  TestingProfile profile;
91  PrefDefaultProvider provider(&profile);
92
93  PrefService* prefs = profile.GetPrefs();
94
95  // Make a copy of the default pref value so we can reset it later.
96  scoped_ptr<Value> default_value(prefs->FindPreference(
97      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
98
99  provider.UpdateDefaultSetting(
100      CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
101  EXPECT_EQ(CONTENT_SETTING_BLOCK,
102            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
103
104  // Make a copy of the pref's new value so we can reset it later.
105  scoped_ptr<Value> new_value(prefs->FindPreference(
106      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
107
108  // Clearing the backing pref should also clear the internal cache.
109  prefs->Set(prefs::kDefaultContentSettings, *default_value);
110  EXPECT_EQ(CONTENT_SETTING_ALLOW,
111            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
112
113  // Reseting the pref to its previous value should update the cache.
114  prefs->Set(prefs::kDefaultContentSettings, *new_value);
115  EXPECT_EQ(CONTENT_SETTING_BLOCK,
116            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
117}
118
119TEST_F(PrefDefaultProviderTest, OffTheRecord) {
120  TestingProfile profile;
121  PrefDefaultProvider provider(&profile);
122
123  profile.set_incognito(true);
124  PrefDefaultProvider otr_provider(&profile);
125  profile.set_incognito(false);
126
127  EXPECT_EQ(CONTENT_SETTING_ALLOW,
128            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
129  EXPECT_EQ(CONTENT_SETTING_ALLOW,
130            otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
131
132  // Changing content settings on the main provider should also affect the
133  // incognito map.
134  provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
135                                CONTENT_SETTING_BLOCK);
136  EXPECT_EQ(CONTENT_SETTING_BLOCK,
137            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
138  EXPECT_EQ(CONTENT_SETTING_BLOCK,
139            otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
140
141  // Changing content settings on the incognito provider should be ignored.
142  otr_provider.UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
143                                   CONTENT_SETTING_ALLOW);
144  EXPECT_EQ(CONTENT_SETTING_BLOCK,
145            provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
146  EXPECT_EQ(CONTENT_SETTING_BLOCK,
147            otr_provider.ProvideDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES));
148}
149
150// ////////////////////////////////////////////////////////////////////////////
151// PrefProviderTest
152//
153
154bool SettingsEqual(const ContentSettings& settings1,
155                   const ContentSettings& settings2) {
156  for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
157    if (settings1.settings[i] != settings2.settings[i])
158      return false;
159  }
160  return true;
161}
162
163class PrefProviderTest : public TestingBrowserProcessTest {
164 public:
165  PrefProviderTest() : ui_thread_(
166      BrowserThread::UI, &message_loop_) {
167  }
168
169 protected:
170  MessageLoop message_loop_;
171  BrowserThread ui_thread_;
172};
173
174TEST_F(PrefProviderTest, Observer) {
175  TestingProfile profile;
176  profile.GetHostContentSettingsMap();
177  Profile* p = &profile;
178  PrefProvider pref_content_settings_provider(p);
179  StubSettingsObserver observer;
180  ContentSettingsPattern pattern("[*.]example.com");
181
182  pref_content_settings_provider.SetContentSetting(
183      pattern,
184      pattern,
185      CONTENT_SETTINGS_TYPE_IMAGES,
186      "",
187      CONTENT_SETTING_ALLOW);
188  EXPECT_EQ(profile.GetHostContentSettingsMap(), observer.last_notifier);
189  EXPECT_EQ(pattern, observer.last_pattern);
190  EXPECT_FALSE(observer.last_update_all);
191  EXPECT_FALSE(observer.last_update_all_types);
192  // Expect 2 calls: One from the update and one from canonicalization.
193  EXPECT_EQ(2, observer.counter);
194}
195
196// Test for regression in which the PrefProvider modified the user pref store
197// of the OTR unintentionally: http://crbug.com/74466.
198TEST_F(PrefProviderTest, Incognito) {
199  DefaultPrefStore* default_prefs = new DefaultPrefStore();
200  PersistentPrefStore* user_prefs = new TestingPrefStore();
201  OverlayPersistentPrefStore* otr_user_prefs =
202      new OverlayPersistentPrefStore(user_prefs);
203
204  PrefService* regular_prefs = new ContentSettingsPrefService(
205      NULL,  // managed_platform_prefs
206      NULL,  // managed_cloud_prefs
207      NULL,  // extension_prefs
208      NULL,  // command_line_prefs
209      user_prefs,
210      NULL,  // recommended_platform_prefs,
211      NULL,  // recommended_cloud_prefs,
212      default_prefs);
213
214  Profile::RegisterUserPrefs(regular_prefs);
215  browser::RegisterUserPrefs(regular_prefs);
216
217  PrefService* otr_prefs = new ContentSettingsPrefService(
218        NULL,  // managed_platform_prefs
219        NULL,  // managed_cloud_prefs
220        NULL,  // extension_prefs
221        NULL,  // command_line_prefs
222        otr_user_prefs,
223        NULL,  // recommended_platform_prefs,
224        NULL,  // recommended_cloud_prefs,
225        default_prefs);
226
227  TestingProfile profile;
228  TestingProfile* otr_profile = new TestingProfile;
229  profile.SetOffTheRecordProfile(otr_profile);
230  profile.SetPrefService(regular_prefs);
231  otr_profile->set_incognito(true);
232  otr_profile->SetPrefService(otr_prefs);
233  profile.GetHostContentSettingsMap();
234
235  PrefProvider pref_content_settings_provider(&profile);
236  PrefProvider pref_content_settings_provider_incognito(otr_profile);
237  ContentSettingsPattern pattern("[*.]example.com");
238  pref_content_settings_provider.SetContentSetting(
239      pattern,
240      pattern,
241      CONTENT_SETTINGS_TYPE_IMAGES,
242      "",
243      CONTENT_SETTING_ALLOW);
244
245  GURL host("http://example.com/");
246  // The value should of course be visible in the regular PrefProvider.
247  EXPECT_EQ(CONTENT_SETTING_ALLOW,
248            pref_content_settings_provider.GetContentSetting(
249                host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
250  // And also in the OTR version.
251  EXPECT_EQ(CONTENT_SETTING_ALLOW,
252            pref_content_settings_provider_incognito.GetContentSetting(
253                host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
254  // But the value should not be overridden in the OTR user prefs accidentally.
255  EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns));
256}
257
258TEST_F(PrefProviderTest, Patterns) {
259  TestingProfile testing_profile;
260  testing_profile.GetHostContentSettingsMap();
261  PrefProvider pref_content_settings_provider(
262      testing_profile.GetOriginalProfile());
263
264  GURL host1("http://example.com/");
265  GURL host2("http://www.example.com/");
266  GURL host3("http://example.org/");
267  GURL host4("file:///tmp/test.html");
268  ContentSettingsPattern pattern1("[*.]example.com");
269  ContentSettingsPattern pattern2("example.org");
270  ContentSettingsPattern pattern3("file:///tmp/test.html");
271
272  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
273            pref_content_settings_provider.GetContentSetting(
274                host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
275  pref_content_settings_provider.SetContentSetting(
276      pattern1,
277      pattern1,
278      CONTENT_SETTINGS_TYPE_IMAGES,
279      "",
280      CONTENT_SETTING_BLOCK);
281  EXPECT_EQ(CONTENT_SETTING_BLOCK,
282            pref_content_settings_provider.GetContentSetting(
283                host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, ""));
284  EXPECT_EQ(CONTENT_SETTING_BLOCK,
285            pref_content_settings_provider.GetContentSetting(
286                host2, host2, CONTENT_SETTINGS_TYPE_IMAGES, ""));
287
288  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
289            pref_content_settings_provider.GetContentSetting(
290                host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
291  pref_content_settings_provider.SetContentSetting(
292      pattern2,
293      pattern2,
294      CONTENT_SETTINGS_TYPE_IMAGES,
295      "",
296      CONTENT_SETTING_BLOCK);
297  EXPECT_EQ(CONTENT_SETTING_BLOCK,
298            pref_content_settings_provider.GetContentSetting(
299                host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, ""));
300
301  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
302            pref_content_settings_provider.GetContentSetting(
303                host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, ""));
304  pref_content_settings_provider.SetContentSetting(
305      pattern3,
306      pattern3,
307      CONTENT_SETTINGS_TYPE_IMAGES,
308      "",
309      CONTENT_SETTING_BLOCK);
310  EXPECT_EQ(CONTENT_SETTING_BLOCK,
311            pref_content_settings_provider.GetContentSetting(
312                host4, host4, CONTENT_SETTINGS_TYPE_IMAGES, ""));
313}
314
315TEST_F(PrefProviderTest, ResourceIdentifier) {
316  // This feature is currently behind a flag.
317  CommandLine* cmd = CommandLine::ForCurrentProcess();
318  AutoReset<CommandLine> auto_reset(cmd, *cmd);
319  cmd->AppendSwitch(switches::kEnableResourceContentSettings);
320
321  TestingProfile testing_profile;
322  testing_profile.GetHostContentSettingsMap();
323  PrefProvider pref_content_settings_provider(
324      testing_profile.GetOriginalProfile());
325
326  GURL host("http://example.com/");
327  ContentSettingsPattern pattern("[*.]example.com");
328  std::string resource1("someplugin");
329  std::string resource2("otherplugin");
330
331  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
332            pref_content_settings_provider.GetContentSetting(
333                host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
334  pref_content_settings_provider.SetContentSetting(
335      pattern,
336      pattern,
337      CONTENT_SETTINGS_TYPE_PLUGINS,
338      resource1,
339      CONTENT_SETTING_BLOCK);
340  EXPECT_EQ(CONTENT_SETTING_BLOCK,
341            pref_content_settings_provider.GetContentSetting(
342                host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
343  EXPECT_EQ(CONTENT_SETTING_DEFAULT,
344            pref_content_settings_provider.GetContentSetting(
345                host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource2));
346}
347
348}  // namespace content_settings
349