Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_default_provider_unittest.cc
blobe3be44f5d1d8e5d335a054627ef7e7051cd2c343
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 "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 using ::testing::_;
22 class DefaultProviderTest : public testing::Test {
23 public:
24 DefaultProviderTest()
25 : provider_(profile_.GetPrefs(), false) {
27 ~DefaultProviderTest() override { provider_.ShutdownOnUIThread(); }
29 protected:
30 content::TestBrowserThreadBundle thread_bundle_;
31 TestingProfile profile_;
32 content_settings::DefaultProvider provider_;
35 TEST_F(DefaultProviderTest, DefaultValues) {
36 // Check setting defaults.
37 EXPECT_EQ(CONTENT_SETTING_ALLOW,
38 GetContentSetting(&provider_,
39 GURL(),
40 GURL(),
41 CONTENT_SETTINGS_TYPE_COOKIES,
42 std::string(),
43 false));
44 provider_.SetWebsiteSetting(
45 ContentSettingsPattern::Wildcard(),
46 ContentSettingsPattern::Wildcard(),
47 CONTENT_SETTINGS_TYPE_COOKIES,
48 std::string(),
49 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
50 EXPECT_EQ(CONTENT_SETTING_BLOCK,
51 GetContentSetting(&provider_,
52 GURL(),
53 GURL(),
54 CONTENT_SETTINGS_TYPE_COOKIES,
55 std::string(),
56 false));
58 EXPECT_EQ(CONTENT_SETTING_ASK,
59 GetContentSetting(&provider_,
60 GURL(),
61 GURL(),
62 CONTENT_SETTINGS_TYPE_GEOLOCATION,
63 std::string(),
64 false));
65 provider_.SetWebsiteSetting(
66 ContentSettingsPattern::Wildcard(),
67 ContentSettingsPattern::Wildcard(),
68 CONTENT_SETTINGS_TYPE_GEOLOCATION,
69 std::string(),
70 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
71 EXPECT_EQ(CONTENT_SETTING_BLOCK,
72 GetContentSetting(&provider_,
73 GURL(),
74 GURL(),
75 CONTENT_SETTINGS_TYPE_GEOLOCATION,
76 std::string(),
77 false));
79 scoped_ptr<base::Value> value(
80 GetContentSettingValue(&provider_,
81 GURL("http://example.com/"),
82 GURL("http://example.com/"),
83 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
84 std::string(),
85 false));
86 EXPECT_FALSE(value.get());
89 TEST_F(DefaultProviderTest, IgnoreNonDefaultSettings) {
90 GURL primary_url("http://www.google.com");
91 GURL secondary_url("http://www.google.com");
93 EXPECT_EQ(CONTENT_SETTING_ALLOW,
94 GetContentSetting(&provider_,
95 primary_url,
96 secondary_url,
97 CONTENT_SETTINGS_TYPE_COOKIES,
98 std::string(),
99 false));
100 scoped_ptr<base::Value> value(
101 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
102 bool owned = provider_.SetWebsiteSetting(
103 ContentSettingsPattern::FromURL(primary_url),
104 ContentSettingsPattern::FromURL(secondary_url),
105 CONTENT_SETTINGS_TYPE_COOKIES,
106 std::string(),
107 value.get());
108 EXPECT_FALSE(owned);
109 EXPECT_EQ(CONTENT_SETTING_ALLOW,
110 GetContentSetting(&provider_,
111 primary_url,
112 secondary_url,
113 CONTENT_SETTINGS_TYPE_COOKIES,
114 std::string(),
115 false));
118 TEST_F(DefaultProviderTest, Observer) {
119 content_settings::MockObserver mock_observer;
120 EXPECT_CALL(mock_observer,
121 OnContentSettingChanged(
122 _, _, CONTENT_SETTINGS_TYPE_IMAGES, ""));
123 provider_.AddObserver(&mock_observer);
124 provider_.SetWebsiteSetting(
125 ContentSettingsPattern::Wildcard(),
126 ContentSettingsPattern::Wildcard(),
127 CONTENT_SETTINGS_TYPE_IMAGES,
128 std::string(),
129 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
131 EXPECT_CALL(mock_observer,
132 OnContentSettingChanged(
133 _, _, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
134 provider_.SetWebsiteSetting(
135 ContentSettingsPattern::Wildcard(),
136 ContentSettingsPattern::Wildcard(),
137 CONTENT_SETTINGS_TYPE_GEOLOCATION,
138 std::string(),
139 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
143 TEST_F(DefaultProviderTest, ObservePref) {
144 PrefService* prefs = profile_.GetPrefs();
146 provider_.SetWebsiteSetting(
147 ContentSettingsPattern::Wildcard(),
148 ContentSettingsPattern::Wildcard(),
149 CONTENT_SETTINGS_TYPE_COOKIES,
150 std::string(),
151 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
152 EXPECT_EQ(CONTENT_SETTING_BLOCK,
153 GetContentSetting(&provider_,
154 GURL(),
155 GURL(),
156 CONTENT_SETTINGS_TYPE_COOKIES,
157 std::string(),
158 false));
160 // Clearing the backing pref should also clear the internal cache.
161 prefs->ClearPref(prefs::kDefaultCookiesSetting);
162 EXPECT_EQ(CONTENT_SETTING_ALLOW,
163 GetContentSetting(&provider_,
164 GURL(),
165 GURL(),
166 CONTENT_SETTINGS_TYPE_COOKIES,
167 std::string(),
168 false));
169 // Reseting the pref to its previous value should update the cache.
170 prefs->SetInteger(prefs::kDefaultCookiesSetting, CONTENT_SETTING_BLOCK);
171 EXPECT_EQ(CONTENT_SETTING_BLOCK,
172 GetContentSetting(&provider_,
173 GURL(),
174 GURL(),
175 CONTENT_SETTINGS_TYPE_COOKIES,
176 std::string(),
177 false));
180 TEST_F(DefaultProviderTest, OffTheRecord) {
181 content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
183 EXPECT_EQ(CONTENT_SETTING_ALLOW,
184 GetContentSetting(&provider_,
185 GURL(),
186 GURL(),
187 CONTENT_SETTINGS_TYPE_COOKIES,
188 std::string(),
189 false));
190 EXPECT_EQ(CONTENT_SETTING_ALLOW,
191 GetContentSetting(&otr_provider,
192 GURL(),
193 GURL(),
194 CONTENT_SETTINGS_TYPE_COOKIES,
195 std::string(),
196 true));
198 // Changing content settings on the main provider should also affect the
199 // incognito map.
200 provider_.SetWebsiteSetting(
201 ContentSettingsPattern::Wildcard(),
202 ContentSettingsPattern::Wildcard(),
203 CONTENT_SETTINGS_TYPE_COOKIES,
204 std::string(),
205 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
206 EXPECT_EQ(CONTENT_SETTING_BLOCK,
207 GetContentSetting(&provider_,
208 GURL(),
209 GURL(),
210 CONTENT_SETTINGS_TYPE_COOKIES,
211 std::string(),
212 false));
214 EXPECT_EQ(CONTENT_SETTING_BLOCK,
215 GetContentSetting(&otr_provider,
216 GURL(),
217 GURL(),
218 CONTENT_SETTINGS_TYPE_COOKIES,
219 std::string(),
220 true));
222 // Changing content settings on the incognito provider should be ignored.
223 scoped_ptr<base::Value> value(
224 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
225 bool owned = otr_provider.SetWebsiteSetting(
226 ContentSettingsPattern::Wildcard(),
227 ContentSettingsPattern::Wildcard(),
228 CONTENT_SETTINGS_TYPE_COOKIES,
229 std::string(),
230 value.get());
231 EXPECT_FALSE(owned);
232 EXPECT_EQ(CONTENT_SETTING_BLOCK,
233 GetContentSetting(&provider_,
234 GURL(),
235 GURL(),
236 CONTENT_SETTINGS_TYPE_COOKIES,
237 std::string(),
238 false));
240 EXPECT_EQ(CONTENT_SETTING_BLOCK,
241 GetContentSetting(&otr_provider,
242 GURL(),
243 GURL(),
244 CONTENT_SETTINGS_TYPE_COOKIES,
245 std::string(),
246 true));
247 otr_provider.ShutdownOnUIThread();