Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chrome/browser/content_settings/content_settings_pref_provider_unittest.cc

Issue 7344008: Make the hcsm and its providers communicate via an observer interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
9 #include "chrome/browser/content_settings/mock_settings_observer.h" 10 #include "chrome/browser/content_settings/mock_settings_observer.h"
10 #include "chrome/browser/prefs/browser_prefs.h" 11 #include "chrome/browser/prefs/browser_prefs.h"
11 #include "chrome/browser/prefs/default_pref_store.h" 12 #include "chrome/browser/prefs/default_pref_store.h"
12 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" 13 #include "chrome/browser/prefs/overlay_persistent_pref_store.h"
13 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/prefs/pref_service_mock_builder.h" 15 #include "chrome/browser/prefs/pref_service_mock_builder.h"
15 #include "chrome/browser/prefs/testing_pref_store.h" 16 #include "chrome/browser/prefs/testing_pref_store.h"
16 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 BrowserThread::UI, &message_loop_) { 153 BrowserThread::UI, &message_loop_) {
153 } 154 }
154 155
155 protected: 156 protected:
156 MessageLoop message_loop_; 157 MessageLoop message_loop_;
157 BrowserThread ui_thread_; 158 BrowserThread ui_thread_;
158 }; 159 };
159 160
160 TEST_F(PrefProviderTest, Observer) { 161 TEST_F(PrefProviderTest, Observer) {
161 TestingProfile profile; 162 TestingProfile profile;
162 PrefProvider pref_content_settings_provider( 163 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false);
163 profile.GetHostContentSettingsMap(), profile.GetPrefs(), false); 164
164 MockSettingsObserver observer;
165 ContentSettingsPattern pattern = 165 ContentSettingsPattern pattern =
166 ContentSettingsPattern::FromString("[*.]example.com"); 166 ContentSettingsPattern::FromString("[*.]example.com");
167 content_settings::MockObserver mock_observer;
168 EXPECT_CALL(mock_observer,
169 OnContentSettingChanged(pattern,
170 ContentSettingsPattern::Wildcard(),
171 CONTENT_SETTINGS_TYPE_IMAGES,
172 ""));
167 173
168 // There are two PrefProvider instances, one in the host content settings map 174 pref_content_settings_provider.AddObserver(&mock_observer);
169 // and one instance that is created here. Setting a content setting will fire 175
170 // one notification. This will change the underlying preferences which will
171 // cause the second PrefProvider instance to reload these and to
172 // sync the obsolete prefences. Hence we get two more notifications which we
173 // will not get in the real world.
174 EXPECT_CALL(observer,
175 OnContentSettingsChanged(profile.GetHostContentSettingsMap(),
176 CONTENT_SETTINGS_TYPE_IMAGES,
177 false,
178 pattern,
179 ContentSettingsPattern::Wildcard(),
180 false));
181 EXPECT_CALL(observer,
182 OnContentSettingsChanged(profile.GetHostContentSettingsMap(),
183 CONTENT_SETTINGS_TYPE_DEFAULT, true,
184 _, _, true)).Times(2);
185 pref_content_settings_provider.SetContentSetting( 176 pref_content_settings_provider.SetContentSetting(
186 pattern, 177 pattern,
187 ContentSettingsPattern::Wildcard(), 178 ContentSettingsPattern::Wildcard(),
188 CONTENT_SETTINGS_TYPE_IMAGES, 179 CONTENT_SETTINGS_TYPE_IMAGES,
189 "", 180 "",
190 CONTENT_SETTING_ALLOW); 181 CONTENT_SETTING_ALLOW);
191 182
192 pref_content_settings_provider.ShutdownOnUIThread(); 183 pref_content_settings_provider.ShutdownOnUIThread();
193 } 184 }
194 185
(...skipping 15 matching lines...) Expand all
210 Profile::RegisterUserPrefs(otr_prefs); 201 Profile::RegisterUserPrefs(otr_prefs);
211 browser::RegisterUserPrefs(otr_prefs); 202 browser::RegisterUserPrefs(otr_prefs);
212 203
213 TestingProfile profile; 204 TestingProfile profile;
214 TestingProfile* otr_profile = new TestingProfile; 205 TestingProfile* otr_profile = new TestingProfile;
215 profile.SetOffTheRecordProfile(otr_profile); 206 profile.SetOffTheRecordProfile(otr_profile);
216 profile.SetPrefService(regular_prefs); 207 profile.SetPrefService(regular_prefs);
217 otr_profile->set_incognito(true); 208 otr_profile->set_incognito(true);
218 otr_profile->SetPrefService(otr_prefs); 209 otr_profile->SetPrefService(otr_prefs);
219 210
220 PrefProvider pref_content_settings_provider( 211 PrefProvider pref_content_settings_provider(regular_prefs, false);
221 profile.GetHostContentSettingsMap(), regular_prefs, false); 212 PrefProvider pref_content_settings_provider_incognito(otr_prefs, true);
222 PrefProvider pref_content_settings_provider_incognito(
223 otr_profile->GetHostContentSettingsMap(), otr_prefs, true);
224 ContentSettingsPattern pattern = 213 ContentSettingsPattern pattern =
225 ContentSettingsPattern::FromString("[*.]example.com"); 214 ContentSettingsPattern::FromString("[*.]example.com");
226 pref_content_settings_provider.SetContentSetting( 215 pref_content_settings_provider.SetContentSetting(
227 pattern, 216 pattern,
228 pattern, 217 pattern,
229 CONTENT_SETTINGS_TYPE_IMAGES, 218 CONTENT_SETTINGS_TYPE_IMAGES,
230 "", 219 "",
231 CONTENT_SETTING_ALLOW); 220 CONTENT_SETTING_ALLOW);
232 221
233 GURL host("http://example.com/"); 222 GURL host("http://example.com/");
234 // The value should of course be visible in the regular PrefProvider. 223 // The value should of course be visible in the regular PrefProvider.
235 EXPECT_EQ(CONTENT_SETTING_ALLOW, 224 EXPECT_EQ(CONTENT_SETTING_ALLOW,
236 pref_content_settings_provider.GetContentSetting( 225 pref_content_settings_provider.GetContentSetting(
237 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); 226 host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
238 // And also in the OTR version. 227 // And also in the OTR version.
239 EXPECT_EQ(CONTENT_SETTING_ALLOW, 228 EXPECT_EQ(CONTENT_SETTING_ALLOW,
240 pref_content_settings_provider_incognito.GetContentSetting( 229 pref_content_settings_provider_incognito.GetContentSetting(
241 host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); 230 host, host, CONTENT_SETTINGS_TYPE_IMAGES, ""));
242 // But the value should not be overridden in the OTR user prefs accidentally. 231 // But the value should not be overridden in the OTR user prefs accidentally.
243 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns)); 232 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(prefs::kContentSettingsPatterns));
244 233
245 pref_content_settings_provider.ShutdownOnUIThread(); 234 pref_content_settings_provider.ShutdownOnUIThread();
246 pref_content_settings_provider_incognito.ShutdownOnUIThread(); 235 pref_content_settings_provider_incognito.ShutdownOnUIThread();
247 } 236 }
248 237
249 TEST_F(PrefProviderTest, Patterns) { 238 TEST_F(PrefProviderTest, Patterns) {
250 TestingProfile testing_profile; 239 TestingProfile testing_profile;
251 PrefProvider pref_content_settings_provider( 240 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
252 testing_profile.GetHostContentSettingsMap(), 241 false);
253 testing_profile.GetPrefs(), false);
254 242
255 GURL host1("http://example.com/"); 243 GURL host1("http://example.com/");
256 GURL host2("http://www.example.com/"); 244 GURL host2("http://www.example.com/");
257 GURL host3("http://example.org/"); 245 GURL host3("http://example.org/");
258 GURL host4("file:///tmp/test.html"); 246 GURL host4("file:///tmp/test.html");
259 ContentSettingsPattern pattern1 = 247 ContentSettingsPattern pattern1 =
260 ContentSettingsPattern::FromString("[*.]example.com"); 248 ContentSettingsPattern::FromString("[*.]example.com");
261 ContentSettingsPattern pattern2 = 249 ContentSettingsPattern pattern2 =
262 ContentSettingsPattern::FromString("example.org"); 250 ContentSettingsPattern::FromString("example.org");
263 ContentSettingsPattern pattern3 = 251 ContentSettingsPattern pattern3 =
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 pref_content_settings_provider.ShutdownOnUIThread(); 296 pref_content_settings_provider.ShutdownOnUIThread();
309 } 297 }
310 298
311 TEST_F(PrefProviderTest, ResourceIdentifier) { 299 TEST_F(PrefProviderTest, ResourceIdentifier) {
312 // This feature is currently behind a flag. 300 // This feature is currently behind a flag.
313 CommandLine* cmd = CommandLine::ForCurrentProcess(); 301 CommandLine* cmd = CommandLine::ForCurrentProcess();
314 AutoReset<CommandLine> auto_reset(cmd, *cmd); 302 AutoReset<CommandLine> auto_reset(cmd, *cmd);
315 cmd->AppendSwitch(switches::kEnableResourceContentSettings); 303 cmd->AppendSwitch(switches::kEnableResourceContentSettings);
316 304
317 TestingProfile testing_profile; 305 TestingProfile testing_profile;
318 PrefProvider pref_content_settings_provider( 306 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
319 testing_profile.GetHostContentSettingsMap(), 307 false);
320 testing_profile.GetPrefs(),
321 false);
322 308
323 GURL host("http://example.com/"); 309 GURL host("http://example.com/");
324 ContentSettingsPattern pattern = 310 ContentSettingsPattern pattern =
325 ContentSettingsPattern::FromString("[*.]example.com"); 311 ContentSettingsPattern::FromString("[*.]example.com");
326 std::string resource1("someplugin"); 312 std::string resource1("someplugin");
327 std::string resource2("otherplugin"); 313 std::string resource2("otherplugin");
328 314
329 EXPECT_EQ(CONTENT_SETTING_DEFAULT, 315 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
330 pref_content_settings_provider.GetContentSetting( 316 pref_content_settings_provider.GetContentSetting(
331 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1)); 317 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, resource1));
(...skipping 26 matching lines...) Expand all
358 ContentSettingsPattern pattern = 344 ContentSettingsPattern pattern =
359 ContentSettingsPattern::FromString("http://www.example.com"); 345 ContentSettingsPattern::FromString("http://www.example.com");
360 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue()); 346 scoped_ptr<DictionaryValue> all_settings_dictionary(new DictionaryValue());
361 all_settings_dictionary->SetWithoutPathExpansion( 347 all_settings_dictionary->SetWithoutPathExpansion(
362 pattern.ToString(), settings_dictionary); 348 pattern.ToString(), settings_dictionary);
363 349
364 // Set Obsolete preference. 350 // Set Obsolete preference.
365 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary); 351 prefs->Set(prefs::kContentSettingsPatterns, *all_settings_dictionary);
366 352
367 // Test if single pattern settings are properly migrated. 353 // Test if single pattern settings are properly migrated.
368 content_settings::PrefProvider provider(profile.GetHostContentSettingsMap(), 354 content_settings::PrefProvider provider(prefs, false);
369 prefs, false);
370 355
371 // Validate migrated preferences 356 // Validate migrated preferences
372 const DictionaryValue* const_all_settings_dictionary = 357 const DictionaryValue* const_all_settings_dictionary =
373 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); 358 prefs->GetDictionary(prefs::kContentSettingsPatternPairs);
374 EXPECT_EQ(1U, const_all_settings_dictionary->size()); 359 EXPECT_EQ(1U, const_all_settings_dictionary->size());
375 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString())); 360 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString()));
376 EXPECT_TRUE(const_all_settings_dictionary->HasKey( 361 EXPECT_TRUE(const_all_settings_dictionary->HasKey(
377 pattern.ToString() + "," + 362 pattern.ToString() + "," +
378 ContentSettingsPattern::Wildcard().ToString())); 363 ContentSettingsPattern::Wildcard().ToString()));
379 364
380 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( 365 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
381 GURL("http://www.example.com"), 366 GURL("http://www.example.com"),
382 GURL("http://www.example.com"), 367 GURL("http://www.example.com"),
383 CONTENT_SETTINGS_TYPE_IMAGES, 368 CONTENT_SETTINGS_TYPE_IMAGES,
384 "")); 369 ""));
385 370
386 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( 371 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
387 GURL("http://www.example.com"), 372 GURL("http://www.example.com"),
388 GURL("http://www.example.com"), 373 GURL("http://www.example.com"),
389 CONTENT_SETTINGS_TYPE_POPUPS, 374 CONTENT_SETTINGS_TYPE_POPUPS,
390 "")); 375 ""));
391 376
392 provider.ShutdownOnUIThread(); 377 provider.ShutdownOnUIThread();
393 } 378 }
394 379
395 } // namespace content_settings 380 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698