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 "base/memory/scoped_ptr.h"
6#include "base/message_loop/message_loop.h"
7#include "base/prefs/pref_service.h"
8#include "base/prefs/testing_pref_service.h"
9#include "chrome/browser/content_settings/content_settings_default_provider.h"
10#include "chrome/browser/content_settings/content_settings_mock_observer.h"
11#include "chrome/browser/content_settings/content_settings_utils.h"
12#include "chrome/common/pref_names.h"
13#include "chrome/test/base/testing_profile.h"
14#include "content/public/test/test_browser_thread.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "url/gurl.h"
17
18using ::testing::_;
19using content::BrowserThread;
20
21class DefaultProviderTest : public testing::Test {
22 public:
23  DefaultProviderTest()
24      : ui_thread_(BrowserThread::UI, &message_loop_),
25        provider_(profile_.GetPrefs(), false) {
26  }
27  virtual ~DefaultProviderTest() {
28    provider_.ShutdownOnUIThread();
29  }
30
31 protected:
32  base::MessageLoop message_loop_;
33  content::TestBrowserThread ui_thread_;
34  TestingProfile profile_;
35  content_settings::DefaultProvider provider_;
36};
37
38TEST_F(DefaultProviderTest, DefaultValues) {
39  // Check setting defaults.
40  EXPECT_EQ(CONTENT_SETTING_ALLOW,
41            GetContentSetting(&provider_,
42                              GURL(),
43                              GURL(),
44                              CONTENT_SETTINGS_TYPE_COOKIES,
45                              std::string(),
46                              false));
47  provider_.SetWebsiteSetting(
48      ContentSettingsPattern::Wildcard(),
49      ContentSettingsPattern::Wildcard(),
50      CONTENT_SETTINGS_TYPE_COOKIES,
51      std::string(),
52      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
53  EXPECT_EQ(CONTENT_SETTING_BLOCK,
54            GetContentSetting(&provider_,
55                              GURL(),
56                              GURL(),
57                              CONTENT_SETTINGS_TYPE_COOKIES,
58                              std::string(),
59                              false));
60
61  EXPECT_EQ(CONTENT_SETTING_ASK,
62            GetContentSetting(&provider_,
63                              GURL(),
64                              GURL(),
65                              CONTENT_SETTINGS_TYPE_GEOLOCATION,
66                              std::string(),
67                              false));
68  provider_.SetWebsiteSetting(
69      ContentSettingsPattern::Wildcard(),
70      ContentSettingsPattern::Wildcard(),
71      CONTENT_SETTINGS_TYPE_GEOLOCATION,
72      std::string(),
73      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
74  EXPECT_EQ(CONTENT_SETTING_BLOCK,
75            GetContentSetting(&provider_,
76                              GURL(),
77                              GURL(),
78                              CONTENT_SETTINGS_TYPE_GEOLOCATION,
79                              std::string(),
80                              false));
81
82  scoped_ptr<base::Value> value(
83      GetContentSettingValue(&provider_,
84                             GURL("http://example.com/"),
85                             GURL("http://example.com/"),
86                             CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
87                             std::string(),
88                             false));
89  EXPECT_FALSE(value.get());
90}
91
92TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) {
93  GURL primary_url("http://www.google.com");
94  GURL secondary_url("http://www.google.com");
95
96  EXPECT_EQ(CONTENT_SETTING_ALLOW,
97            GetContentSetting(&provider_,
98                              primary_url,
99                              secondary_url,
100                              CONTENT_SETTINGS_TYPE_COOKIES,
101                              std::string(),
102                              false));
103  scoped_ptr<base::Value> value(
104      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
105  bool owned = provider_.SetWebsiteSetting(
106      ContentSettingsPattern::FromURL(primary_url),
107      ContentSettingsPattern::FromURL(secondary_url),
108      CONTENT_SETTINGS_TYPE_COOKIES,
109      std::string(),
110      value.get());
111  EXPECT_FALSE(owned);
112  EXPECT_EQ(CONTENT_SETTING_ALLOW,
113            GetContentSetting(&provider_,
114                              primary_url,
115                              secondary_url,
116                              CONTENT_SETTINGS_TYPE_COOKIES,
117                              std::string(),
118                              false));
119}
120
121TEST_F(DefaultProviderTest, Observer) {
122  content_settings::MockObserver mock_observer;
123  EXPECT_CALL(mock_observer,
124              OnContentSettingChanged(
125                  _, _, CONTENT_SETTINGS_TYPE_IMAGES, ""));
126  provider_.AddObserver(&mock_observer);
127  provider_.SetWebsiteSetting(
128      ContentSettingsPattern::Wildcard(),
129      ContentSettingsPattern::Wildcard(),
130      CONTENT_SETTINGS_TYPE_IMAGES,
131      std::string(),
132      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
133
134  EXPECT_CALL(mock_observer,
135              OnContentSettingChanged(
136                  _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
137  provider_.SetWebsiteSetting(
138      ContentSettingsPattern::Wildcard(),
139      ContentSettingsPattern::Wildcard(),
140      CONTENT_SETTINGS_TYPE_GEOLOCATION,
141      std::string(),
142      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
143}
144
145
146TEST_F(DefaultProviderTest, ObserveDefaultPref) {
147  PrefService* prefs = profile_.GetPrefs();
148
149  // Make a copy of the default pref value so we can reset it later.
150  scoped_ptr<base::Value> default_value(prefs->FindPreference(
151      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
152
153  provider_.SetWebsiteSetting(
154      ContentSettingsPattern::Wildcard(),
155      ContentSettingsPattern::Wildcard(),
156      CONTENT_SETTINGS_TYPE_COOKIES,
157      std::string(),
158      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
159  EXPECT_EQ(CONTENT_SETTING_BLOCK,
160            GetContentSetting(&provider_,
161                              GURL(),
162                              GURL(),
163                              CONTENT_SETTINGS_TYPE_COOKIES,
164                              std::string(),
165                              false));
166
167  // Make a copy of the pref's new value so we can reset it later.
168  scoped_ptr<base::Value> new_value(prefs->FindPreference(
169      prefs::kDefaultContentSettings)->GetValue()->DeepCopy());
170
171  // Clearing the backing pref should also clear the internal cache.
172  prefs->Set(prefs::kDefaultContentSettings, *default_value);
173  EXPECT_EQ(CONTENT_SETTING_ALLOW,
174            GetContentSetting(&provider_,
175                              GURL(),
176                              GURL(),
177                              CONTENT_SETTINGS_TYPE_COOKIES,
178                              std::string(),
179                              false));
180  // Reseting the pref to its previous value should update the cache.
181  prefs->Set(prefs::kDefaultContentSettings, *new_value);
182  EXPECT_EQ(CONTENT_SETTING_BLOCK,
183            GetContentSetting(&provider_,
184                              GURL(),
185                              GURL(),
186                              CONTENT_SETTINGS_TYPE_COOKIES,
187                              std::string(),
188                              false));
189}
190
191TEST_F(DefaultProviderTest, OffTheRecord) {
192  content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
193
194  EXPECT_EQ(CONTENT_SETTING_ALLOW,
195            GetContentSetting(&provider_,
196                              GURL(),
197                              GURL(),
198                              CONTENT_SETTINGS_TYPE_COOKIES,
199                              std::string(),
200                              false));
201  EXPECT_EQ(CONTENT_SETTING_ALLOW,
202            GetContentSetting(&otr_provider,
203                              GURL(),
204                              GURL(),
205                              CONTENT_SETTINGS_TYPE_COOKIES,
206                              std::string(),
207                              true));
208
209  // Changing content settings on the main provider should also affect the
210  // incognito map.
211  provider_.SetWebsiteSetting(
212      ContentSettingsPattern::Wildcard(),
213      ContentSettingsPattern::Wildcard(),
214      CONTENT_SETTINGS_TYPE_COOKIES,
215      std::string(),
216      new base::FundamentalValue(CONTENT_SETTING_BLOCK));
217  EXPECT_EQ(CONTENT_SETTING_BLOCK,
218            GetContentSetting(&provider_,
219                              GURL(),
220                              GURL(),
221                              CONTENT_SETTINGS_TYPE_COOKIES,
222                              std::string(),
223                              false));
224
225  EXPECT_EQ(CONTENT_SETTING_BLOCK,
226            GetContentSetting(&otr_provider,
227                              GURL(),
228                              GURL(),
229                              CONTENT_SETTINGS_TYPE_COOKIES,
230                              std::string(),
231                              true));
232
233  // Changing content settings on the incognito provider should be ignored.
234  scoped_ptr<base::Value> value(
235      new base::FundamentalValue(CONTENT_SETTING_ALLOW));
236  bool owned = otr_provider.SetWebsiteSetting(
237      ContentSettingsPattern::Wildcard(),
238      ContentSettingsPattern::Wildcard(),
239      CONTENT_SETTINGS_TYPE_COOKIES,
240      std::string(),
241      value.get());
242  EXPECT_FALSE(owned);
243  EXPECT_EQ(CONTENT_SETTING_BLOCK,
244            GetContentSetting(&provider_,
245                              GURL(),
246                              GURL(),
247                              CONTENT_SETTINGS_TYPE_COOKIES,
248                              std::string(),
249                              false));
250
251  EXPECT_EQ(CONTENT_SETTING_BLOCK,
252            GetContentSetting(&otr_provider,
253                              GURL(),
254                              GURL(),
255                              CONTENT_SETTINGS_TYPE_COOKIES,
256                              std::string(),
257                              true));
258  otr_provider.ShutdownOnUIThread();
259}
260