Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_default_provider_unittest.cc
blobadd61aa6c3c0e53899c1d6a12fddb7057490f0fd
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/browser/website_settings_info.h"
16 #include "components/content_settings/core/browser/website_settings_registry.h"
17 #include "components/content_settings/core/test/content_settings_test_utils.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
22 using ::testing::_;
24 class DefaultProviderTest : public testing::Test {
25 public:
26 DefaultProviderTest()
27 : provider_(profile_.GetPrefs(), false) {
29 ~DefaultProviderTest() override { provider_.ShutdownOnUIThread(); }
31 protected:
32 content::TestBrowserThreadBundle thread_bundle_;
33 TestingProfile profile_;
34 content_settings::DefaultProvider provider_;
37 TEST_F(DefaultProviderTest, DefaultValues) {
38 // Check setting defaults.
39 EXPECT_EQ(CONTENT_SETTING_ALLOW,
40 GetContentSetting(&provider_,
41 GURL(),
42 GURL(),
43 CONTENT_SETTINGS_TYPE_COOKIES,
44 std::string(),
45 false));
46 provider_.SetWebsiteSetting(
47 ContentSettingsPattern::Wildcard(),
48 ContentSettingsPattern::Wildcard(),
49 CONTENT_SETTINGS_TYPE_COOKIES,
50 std::string(),
51 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
52 EXPECT_EQ(CONTENT_SETTING_BLOCK,
53 GetContentSetting(&provider_,
54 GURL(),
55 GURL(),
56 CONTENT_SETTINGS_TYPE_COOKIES,
57 std::string(),
58 false));
60 EXPECT_EQ(CONTENT_SETTING_ASK,
61 GetContentSetting(&provider_,
62 GURL(),
63 GURL(),
64 CONTENT_SETTINGS_TYPE_GEOLOCATION,
65 std::string(),
66 false));
67 provider_.SetWebsiteSetting(
68 ContentSettingsPattern::Wildcard(),
69 ContentSettingsPattern::Wildcard(),
70 CONTENT_SETTINGS_TYPE_GEOLOCATION,
71 std::string(),
72 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
73 EXPECT_EQ(CONTENT_SETTING_BLOCK,
74 GetContentSetting(&provider_,
75 GURL(),
76 GURL(),
77 CONTENT_SETTINGS_TYPE_GEOLOCATION,
78 std::string(),
79 false));
81 scoped_ptr<base::Value> value(
82 GetContentSettingValue(&provider_,
83 GURL("http://example.com/"),
84 GURL("http://example.com/"),
85 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
86 std::string(),
87 false));
88 EXPECT_FALSE(value.get());
91 TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) {
92 GURL primary_url("http://www.google.com");
93 GURL secondary_url("http://www.google.com");
95 EXPECT_EQ(CONTENT_SETTING_ALLOW,
96 GetContentSetting(&provider_,
97 primary_url,
98 secondary_url,
99 CONTENT_SETTINGS_TYPE_COOKIES,
100 std::string(),
101 false));
102 scoped_ptr<base::Value> value(
103 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
104 bool owned = provider_.SetWebsiteSetting(
105 ContentSettingsPattern::FromURL(primary_url),
106 ContentSettingsPattern::FromURL(secondary_url),
107 CONTENT_SETTINGS_TYPE_COOKIES,
108 std::string(),
109 value.get());
110 EXPECT_FALSE(owned);
111 EXPECT_EQ(CONTENT_SETTING_ALLOW,
112 GetContentSetting(&provider_,
113 primary_url,
114 secondary_url,
115 CONTENT_SETTINGS_TYPE_COOKIES,
116 std::string(),
117 false));
120 TEST_F(DefaultProviderTest, Observer) {
121 content_settings::MockObserver mock_observer;
122 EXPECT_CALL(mock_observer,
123 OnContentSettingChanged(
124 _, _, CONTENT_SETTINGS_TYPE_IMAGES, ""));
125 provider_.AddObserver(&mock_observer);
126 provider_.SetWebsiteSetting(
127 ContentSettingsPattern::Wildcard(),
128 ContentSettingsPattern::Wildcard(),
129 CONTENT_SETTINGS_TYPE_IMAGES,
130 std::string(),
131 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
133 EXPECT_CALL(mock_observer,
134 OnContentSettingChanged(
135 _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
136 provider_.SetWebsiteSetting(
137 ContentSettingsPattern::Wildcard(),
138 ContentSettingsPattern::Wildcard(),
139 CONTENT_SETTINGS_TYPE_GEOLOCATION,
140 std::string(),
141 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
145 TEST_F(DefaultProviderTest, ObservePref) {
146 PrefService* prefs = profile_.GetPrefs();
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));
161 const content_settings::WebsiteSettingsInfo* info =
162 content_settings::WebsiteSettingsRegistry::GetInstance()->Get(
163 CONTENT_SETTINGS_TYPE_COOKIES);
164 // Clearing the backing pref should also clear the internal cache.
165 prefs->ClearPref(info->default_value_pref_name());
166 EXPECT_EQ(CONTENT_SETTING_ALLOW,
167 GetContentSetting(&provider_,
168 GURL(),
169 GURL(),
170 CONTENT_SETTINGS_TYPE_COOKIES,
171 std::string(),
172 false));
173 // Reseting the pref to its previous value should update the cache.
174 prefs->SetInteger(info->default_value_pref_name(), CONTENT_SETTING_BLOCK);
175 EXPECT_EQ(CONTENT_SETTING_BLOCK,
176 GetContentSetting(&provider_,
177 GURL(),
178 GURL(),
179 CONTENT_SETTINGS_TYPE_COOKIES,
180 std::string(),
181 false));
184 TEST_F(DefaultProviderTest, OffTheRecord) {
185 content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
187 EXPECT_EQ(CONTENT_SETTING_ALLOW,
188 GetContentSetting(&provider_,
189 GURL(),
190 GURL(),
191 CONTENT_SETTINGS_TYPE_COOKIES,
192 std::string(),
193 false));
194 EXPECT_EQ(CONTENT_SETTING_ALLOW,
195 GetContentSetting(&otr_provider,
196 GURL(),
197 GURL(),
198 CONTENT_SETTINGS_TYPE_COOKIES,
199 std::string(),
200 true));
202 // Changing content settings on the main provider should also affect the
203 // incognito map.
204 provider_.SetWebsiteSetting(
205 ContentSettingsPattern::Wildcard(),
206 ContentSettingsPattern::Wildcard(),
207 CONTENT_SETTINGS_TYPE_COOKIES,
208 std::string(),
209 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
210 EXPECT_EQ(CONTENT_SETTING_BLOCK,
211 GetContentSetting(&provider_,
212 GURL(),
213 GURL(),
214 CONTENT_SETTINGS_TYPE_COOKIES,
215 std::string(),
216 false));
218 EXPECT_EQ(CONTENT_SETTING_BLOCK,
219 GetContentSetting(&otr_provider,
220 GURL(),
221 GURL(),
222 CONTENT_SETTINGS_TYPE_COOKIES,
223 std::string(),
224 true));
226 // Changing content settings on the incognito provider should be ignored.
227 scoped_ptr<base::Value> value(
228 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
229 bool owned = otr_provider.SetWebsiteSetting(
230 ContentSettingsPattern::Wildcard(),
231 ContentSettingsPattern::Wildcard(),
232 CONTENT_SETTINGS_TYPE_COOKIES,
233 std::string(),
234 value.get());
235 EXPECT_FALSE(owned);
236 EXPECT_EQ(CONTENT_SETTING_BLOCK,
237 GetContentSetting(&provider_,
238 GURL(),
239 GURL(),
240 CONTENT_SETTINGS_TYPE_COOKIES,
241 std::string(),
242 false));
244 EXPECT_EQ(CONTENT_SETTING_BLOCK,
245 GetContentSetting(&otr_provider,
246 GURL(),
247 GURL(),
248 CONTENT_SETTINGS_TYPE_COOKIES,
249 std::string(),
250 true));
251 otr_provider.ShutdownOnUIThread();