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

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: adjust a comment 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 static const char kGeolocationPrefPath[] =
184 "profile.content_settings.exceptions.geolocation";
raymes 2017/02/16 11:01:38 nit: You should be able to get this from ContentSe
Timothy Loh 2017/02/17 06:54:38 Done.
185 static const char kMicPrefPath[] =
186 "profile.content_settings.exceptions.media_stream_mic";
187 static const char kObsoleteLastUsed[] = "last_used";
raymes 2017/02/16 11:01:38 nit: I think static is unneeded here and below
Timothy Loh 2017/02/17 06:54:38 Done.
188
189 TestingProfile profile;
190 PrefService* prefs = profile.GetPrefs();
191
192 // Content settings prefs are structured as follows:
193 // "media_stream_mic": {
194 // "https://example.com:443,*": {
195 // "last_used": 1486968992.758971,
196 // "setting": 1
197 // }
198 // }
199 static const char kPattern[] = "https://example.com:443,*";
200
201 auto geolocation_pattern_data = base::MakeUnique<base::DictionaryValue>();
202 geolocation_pattern_data->SetDouble(kObsoleteLastUsed, 1485000000.0);
203 base::DictionaryValue geolocation_pref_data;
204 geolocation_pref_data.SetWithoutPathExpansion(
205 kPattern, std::move(geolocation_pattern_data));
206 prefs->Set(kGeolocationPrefPath, geolocation_pref_data);
207
208 auto mic_pattern_data = base::MakeUnique<base::DictionaryValue>();
209 mic_pattern_data->SetInteger("setting", CONTENT_SETTING_ALLOW);
210 mic_pattern_data->SetDouble(kObsoleteLastUsed, 1480000000.0);
211 base::DictionaryValue mic_pref_data;
212 mic_pref_data.SetWithoutPathExpansion(kPattern, std::move(mic_pattern_data));
213 prefs->Set(kMicPrefPath, mic_pref_data);
214
215 // Instantiate a new PrefProvider here, because we want to test the
216 // constructor's behavior after setting the above.
217 PrefProvider provider(prefs, false);
218 provider.ShutdownOnUIThread();
219
220 // Check that last_used data has been deleted.
221 EXPECT_TRUE(prefs->GetDictionary(kGeolocationPrefPath)->empty());
222 auto mic_prefs = prefs->GetDictionary(kMicPrefPath);
223 const base::DictionaryValue* mic_result_pattern_data;
224 ASSERT_TRUE(mic_prefs->GetDictionaryWithoutPathExpansion(
225 kPattern, &mic_result_pattern_data));
226 EXPECT_EQ(mic_result_pattern_data->size(), static_cast<size_t>(1));
227 int mic_result_setting;
228 EXPECT_TRUE(
229 mic_result_pattern_data->GetInteger("setting", &mic_result_setting));
230 EXPECT_EQ(mic_result_setting, CONTENT_SETTING_ALLOW);
raymes 2017/02/16 11:01:38 In this part, could we also query the content sett
Timothy Loh 2017/02/17 06:54:38 Done.
231 }
232
182 // Test for regression in which the PrefProvider modified the user pref store 233 // Test for regression in which the PrefProvider modified the user pref store
183 // of the OTR unintentionally: http://crbug.com/74466. 234 // of the OTR unintentionally: http://crbug.com/74466.
184 TEST_F(PrefProviderTest, Incognito) { 235 TEST_F(PrefProviderTest, Incognito) {
185 PersistentPrefStore* user_prefs = new TestingPrefStore(); 236 PersistentPrefStore* user_prefs = new TestingPrefStore();
186 OverlayUserPrefStore* otr_user_prefs = 237 OverlayUserPrefStore* otr_user_prefs =
187 new OverlayUserPrefStore(user_prefs); 238 new OverlayUserPrefStore(user_prefs);
188 239
189 sync_preferences::PrefServiceMockFactory factory; 240 sync_preferences::PrefServiceMockFactory factory;
190 factory.set_user_prefs(make_scoped_refptr(user_prefs)); 241 factory.set_user_prefs(make_scoped_refptr(user_prefs));
191 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( 242 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()); 443 DictionaryPrefUpdate update(&prefs, info->pref_name());
393 base::DictionaryValue* mutable_settings = update.Get(); 444 base::DictionaryValue* mutable_settings = update.Get();
394 mutable_settings->SetWithoutPathExpansion("www.example.com,*", 445 mutable_settings->SetWithoutPathExpansion("www.example.com,*",
395 new base::DictionaryValue()); 446 new base::DictionaryValue());
396 } 447 }
397 EXPECT_TRUE(observer.notification_received()); 448 EXPECT_TRUE(observer.notification_received());
398 449
399 provider.ShutdownOnUIThread(); 450 provider.ShutdownOnUIThread();
400 } 451 }
401 452
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) { 453 TEST_F(PrefProviderTest, IncognitoInheritsValueMap) {
438 sync_preferences::TestingPrefServiceSyncable prefs; 454 sync_preferences::TestingPrefServiceSyncable prefs;
439 PrefProvider::RegisterProfilePrefs(prefs.registry()); 455 PrefProvider::RegisterProfilePrefs(prefs.registry());
440 456
441 ContentSettingsPattern pattern_1 = 457 ContentSettingsPattern pattern_1 =
442 ContentSettingsPattern::FromString("google.com"); 458 ContentSettingsPattern::FromString("google.com");
443 ContentSettingsPattern pattern_2 = 459 ContentSettingsPattern pattern_2 =
444 ContentSettingsPattern::FromString("www.google.com"); 460 ContentSettingsPattern::FromString("www.google.com");
445 ContentSettingsPattern wildcard = 461 ContentSettingsPattern wildcard =
446 ContentSettingsPattern::FromString("*"); 462 ContentSettingsPattern::FromString("*");
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 for (const char* pref : nonempty_prefs) { 585 for (const char* pref : nonempty_prefs) {
570 DictionaryPrefUpdate update(&prefs, pref); 586 DictionaryPrefUpdate update(&prefs, pref);
571 const base::DictionaryValue* dictionary = update.Get(); 587 const base::DictionaryValue* dictionary = update.Get();
572 EXPECT_EQ(1u, dictionary->size()); 588 EXPECT_EQ(1u, dictionary->size());
573 } 589 }
574 590
575 provider.ShutdownOnUIThread(); 591 provider.ShutdownOnUIThread();
576 } 592 }
577 593
578 } // namespace content_settings 594 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698