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

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

Issue 2697473002: Remove last usage functions from HostContentSettingsMap and clean up prefs (Closed)
Patch Set: fix up unit test Created 3 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/content_settings/core/browser/content_settings_pref_provide r.h" 5 #include "components/content_settings/core/browser/content_settings_pref_provide r.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/test/simple_test_clock.h"
15 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
16 #include "base/values.h" 15 #include "base/values.h"
17 #include "chrome/browser/content_settings/content_settings_mock_observer.h" 16 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
18 #include "chrome/browser/prefs/browser_prefs.h" 17 #include "chrome/browser/prefs/browser_prefs.h"
19 #include "chrome/common/chrome_switches.h" 18 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h" 20 #include "chrome/common/url_constants.h"
22 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
23 #include "components/content_settings/core/browser/content_settings_pref.h" 22 #include "components/content_settings/core/browser/content_settings_pref.h"
24 #include "components/content_settings/core/browser/content_settings_registry.h" 23 #include "components/content_settings/core/browser/content_settings_registry.h"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 127
129 pref_content_settings_provider.SetWebsiteSetting( 128 pref_content_settings_provider.SetWebsiteSetting(
130 pattern, ContentSettingsPattern::Wildcard(), 129 pattern, ContentSettingsPattern::Wildcard(),
131 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), 130 CONTENT_SETTINGS_TYPE_COOKIES, std::string(),
132 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); 131 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
133 132
134 pref_content_settings_provider.ShutdownOnUIThread(); 133 pref_content_settings_provider.ShutdownOnUIThread();
135 } 134 }
136 135
137 // Tests that fullscreen and mouselock content settings are cleared. 136 // Tests that fullscreen and mouselock content settings are cleared.
138 TEST_F(PrefProviderTest, DiscardObsoletePreferences) { 137 TEST_F(PrefProviderTest, DiscardObsoleteFullscreenAndMouselockPreferences) {
139 static const char kFullscreenPrefPath[] = 138 static const char kFullscreenPrefPath[] =
140 "profile.content_settings.exceptions.fullscreen"; 139 "profile.content_settings.exceptions.fullscreen";
141 #if !defined(OS_ANDROID) 140 #if !defined(OS_ANDROID)
142 static const char kMouselockPrefPath[] = 141 static const char kMouselockPrefPath[] =
143 "profile.content_settings.exceptions.mouselock"; 142 "profile.content_settings.exceptions.mouselock";
144 #endif 143 #endif
145 static const char kGeolocationPrefPath[] = 144 static const char kGeolocationPrefPath[] =
146 "profile.content_settings.exceptions.geolocation"; 145 "profile.content_settings.exceptions.geolocation";
147 static const char kPattern[] = "[*.]example.com"; 146 static const char kPattern[] = "[*.]example.com";
148 147
(...skipping 23 matching lines...) Expand all
172 EXPECT_FALSE(prefs->HasPrefPath(kMouselockPrefPath)); 171 EXPECT_FALSE(prefs->HasPrefPath(kMouselockPrefPath));
173 #endif 172 #endif
174 EXPECT_TRUE(prefs->HasPrefPath(kGeolocationPrefPath)); 173 EXPECT_TRUE(prefs->HasPrefPath(kGeolocationPrefPath));
175 GURL primary_url("http://example.com/"); 174 GURL primary_url("http://example.com/");
176 EXPECT_EQ(CONTENT_SETTING_ALLOW, 175 EXPECT_EQ(CONTENT_SETTING_ALLOW,
177 TestUtils::GetContentSetting(&provider, primary_url, primary_url, 176 TestUtils::GetContentSetting(&provider, primary_url, primary_url,
178 CONTENT_SETTINGS_TYPE_GEOLOCATION, 177 CONTENT_SETTINGS_TYPE_GEOLOCATION,
179 std::string(), false)); 178 std::string(), false));
180 } 179 }
181 180
181 // Tests that last usage content settings are cleared.
182 TEST_F(PrefProviderTest, DiscardObsoleteLastUsagePreferences) {
183 std::string kGeolocationPrefPath =
184 ContentSettingsRegistry::GetInstance()
185 ->Get(CONTENT_SETTINGS_TYPE_GEOLOCATION)
186 ->website_settings_info()
187 ->pref_name();
188 std::string kMicPrefPath = ContentSettingsRegistry::GetInstance()
189 ->Get(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)
190 ->website_settings_info()
191 ->pref_name();
192 const char kObsoleteLastUsed[] = "last_used";
193
194 TestingProfile profile;
195 PrefService* prefs = profile.GetPrefs();
196
197 // Content settings prefs are structured as follows:
198 // "media_stream_mic": {
199 // "https://example.com:443,*": {
200 // "last_used": 1486968992.758971,
201 // "setting": 1
202 // }
203 // }
204 const char kPattern[] = "https://example.com:443,*";
205 GURL host("https://example.com/");
206
207 auto geolocation_pattern_data = base::MakeUnique<base::DictionaryValue>();
208 geolocation_pattern_data->SetDouble(kObsoleteLastUsed, 1485000000.0);
209 base::DictionaryValue geolocation_pref_data;
210 geolocation_pref_data.SetWithoutPathExpansion(
211 kPattern, std::move(geolocation_pattern_data));
212 prefs->Set(kGeolocationPrefPath, geolocation_pref_data);
213
214 auto mic_pattern_data = base::MakeUnique<base::DictionaryValue>();
215 mic_pattern_data->SetInteger("setting", CONTENT_SETTING_ALLOW);
216 mic_pattern_data->SetDouble(kObsoleteLastUsed, 1480000000.0);
217 base::DictionaryValue mic_pref_data;
218 mic_pref_data.SetWithoutPathExpansion(kPattern, std::move(mic_pattern_data));
219 prefs->Set(kMicPrefPath, mic_pref_data);
220
221 // Instantiate a new PrefProvider here, because we want to test the
222 // constructor's behavior after setting the above.
223 PrefProvider provider(prefs, false);
224
225 // Check that last_used data has been deleted.
226 EXPECT_TRUE(prefs->GetDictionary(kGeolocationPrefPath)->empty());
227 auto mic_prefs = prefs->GetDictionary(kMicPrefPath);
228 const base::DictionaryValue* mic_result_pattern_data;
229 ASSERT_TRUE(mic_prefs->GetDictionaryWithoutPathExpansion(
230 kPattern, &mic_result_pattern_data));
231 EXPECT_EQ(static_cast<size_t>(1), mic_result_pattern_data->size());
232 int mic_result_setting;
233 EXPECT_TRUE(
234 mic_result_pattern_data->GetInteger("setting", &mic_result_setting));
235 EXPECT_EQ(CONTENT_SETTING_ALLOW, mic_result_setting);
236 EXPECT_EQ(CONTENT_SETTING_ALLOW,
237 TestUtils::GetContentSetting(&provider, host, host,
238 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
239 std::string(), false));
240 provider.ShutdownOnUIThread();
241 }
242
182 // Test for regression in which the PrefProvider modified the user pref store 243 // Test for regression in which the PrefProvider modified the user pref store
183 // of the OTR unintentionally: http://crbug.com/74466. 244 // of the OTR unintentionally: http://crbug.com/74466.
184 TEST_F(PrefProviderTest, Incognito) { 245 TEST_F(PrefProviderTest, Incognito) {
185 PersistentPrefStore* user_prefs = new TestingPrefStore(); 246 PersistentPrefStore* user_prefs = new TestingPrefStore();
186 OverlayUserPrefStore* otr_user_prefs = 247 OverlayUserPrefStore* otr_user_prefs =
187 new OverlayUserPrefStore(user_prefs); 248 new OverlayUserPrefStore(user_prefs);
188 249
189 sync_preferences::PrefServiceMockFactory factory; 250 sync_preferences::PrefServiceMockFactory factory;
190 factory.set_user_prefs(make_scoped_refptr(user_prefs)); 251 factory.set_user_prefs(make_scoped_refptr(user_prefs));
191 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( 252 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 DictionaryPrefUpdate update(&prefs, info->pref_name()); 453 DictionaryPrefUpdate update(&prefs, info->pref_name());
393 base::DictionaryValue* mutable_settings = update.Get(); 454 base::DictionaryValue* mutable_settings = update.Get();
394 mutable_settings->SetWithoutPathExpansion("www.example.com,*", 455 mutable_settings->SetWithoutPathExpansion("www.example.com,*",
395 new base::DictionaryValue()); 456 new base::DictionaryValue());
396 } 457 }
397 EXPECT_TRUE(observer.notification_received()); 458 EXPECT_TRUE(observer.notification_received());
398 459
399 provider.ShutdownOnUIThread(); 460 provider.ShutdownOnUIThread();
400 } 461 }
401 462
402 TEST_F(PrefProviderTest, LastUsage) {
403 TestingProfile testing_profile;
404 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
405 false);
406 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
407 test_clock->SetNow(base::Time::Now());
408
409 pref_content_settings_provider.SetClockForTesting(
410 std::unique_ptr<base::Clock>(test_clock));
411 GURL host("http://example.com/");
412 ContentSettingsPattern pattern =
413 ContentSettingsPattern::FromString("[*.]example.com");
414
415 base::Time no_usage = pref_content_settings_provider.GetLastUsage(
416 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
417 EXPECT_EQ(no_usage.ToDoubleT(), 0);
418
419 pref_content_settings_provider.UpdateLastUsage(
420 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
421 base::Time first = pref_content_settings_provider.GetLastUsage(
422 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
423
424 test_clock->Advance(base::TimeDelta::FromSeconds(10));
425
426 pref_content_settings_provider.UpdateLastUsage(
427 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
428 base::Time second = pref_content_settings_provider.GetLastUsage(
429 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
430
431 base::TimeDelta delta = second - first;
432 EXPECT_EQ(delta.InSeconds(), 10);
433
434 pref_content_settings_provider.ShutdownOnUIThread();
435 }
436
437 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) { 463 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) {
438 sync_preferences::TestingPrefServiceSyncable prefs; 464 sync_preferences::TestingPrefServiceSyncable prefs;
439 PrefProvider::RegisterProfilePrefs(prefs.registry()); 465 PrefProvider::RegisterProfilePrefs(prefs.registry());
440 466
441 ContentSettingsPattern pattern_1 = 467 ContentSettingsPattern pattern_1 =
442 ContentSettingsPattern::FromString("google.com"); 468 ContentSettingsPattern::FromString("google.com");
443 ContentSettingsPattern pattern_2 = 469 ContentSettingsPattern pattern_2 =
444 ContentSettingsPattern::FromString("www.google.com"); 470 ContentSettingsPattern::FromString("www.google.com");
445 ContentSettingsPattern wildcard = 471 ContentSettingsPattern wildcard =
446 ContentSettingsPattern::FromString("*"); 472 ContentSettingsPattern::FromString("*");
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 for (const char* pref : nonempty_prefs) { 595 for (const char* pref : nonempty_prefs) {
570 DictionaryPrefUpdate update(&prefs, pref); 596 DictionaryPrefUpdate update(&prefs, pref);
571 const base::DictionaryValue* dictionary = update.Get(); 597 const base::DictionaryValue* dictionary = update.Get();
572 EXPECT_EQ(1u, dictionary->size()); 598 EXPECT_EQ(1u, dictionary->size());
573 } 599 }
574 600
575 provider.ShutdownOnUIThread(); 601 provider.ShutdownOnUIThread();
576 } 602 }
577 603
578 } // namespace content_settings 604 } // namespace content_settings
OLDNEW
« no previous file with comments | « android_webview/browser/aw_permission_manager.cc ('k') | chrome/browser/geolocation/geolocation_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698