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

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

Issue 2816723002: Add "Site Settings" option to Clear Browsing Data on Android (Closed)
Patch Set: fix deps Created 3 years, 7 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 <memory> 5 #include <memory>
6 #include <string> 6 #include <string>
7 #include <utility> 7 #include <utility>
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/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h" 12 #include "base/json/json_writer.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/test/scoped_feature_list.h" 14 #include "base/test/scoped_feature_list.h"
15 #include "base/test/test_timeouts.h" 15 #include "base/test/simple_test_clock.h"
16 #include "base/threading/platform_thread.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/content_settings/cookie_settings_factory.h" 17 #include "chrome/browser/content_settings/cookie_settings_factory.h"
19 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
20 #include "chrome/browser/content_settings/mock_settings_observer.h" 19 #include "chrome/browser/content_settings/mock_settings_observer.h"
21 #include "chrome/common/chrome_features.h" 20 #include "chrome/common/chrome_features.h"
22 #include "chrome/common/url_constants.h" 21 #include "chrome/common/url_constants.h"
23 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
24 #include "components/content_settings/core/browser/content_settings_details.h" 23 #include "components/content_settings/core/browser/content_settings_details.h"
25 #include "components/content_settings/core/browser/cookie_settings.h" 24 #include "components/content_settings/core/browser/cookie_settings.h"
26 #include "components/content_settings/core/browser/host_content_settings_map.h" 25 #include "components/content_settings/core/browser/host_content_settings_map.h"
(...skipping 1710 matching lines...) Expand 10 before | Expand all | Expand 10 after
1737 // Verify we only have one, and it's url1. 1736 // Verify we only have one, and it's url1.
1738 host_content_settings_map->GetSettingsForOneType( 1737 host_content_settings_map->GetSettingsForOneType(
1739 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); 1738 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings);
1740 EXPECT_EQ(1u, host_settings.size()); 1739 EXPECT_EQ(1u, host_settings.size());
1741 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), 1740 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1),
1742 host_settings[0].primary_pattern); 1741 host_settings[0].primary_pattern);
1743 } 1742 }
1744 1743
1745 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) { 1744 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) {
1746 base::test::ScopedFeatureList feature_list; 1745 base::test::ScopedFeatureList feature_list;
1746 // Enable kTabsInCbd to activate last_modified timestmap recording.
1747 feature_list.InitAndEnableFeature(features::kTabsInCbd); 1747 feature_list.InitAndEnableFeature(features::kTabsInCbd);
1748 1748
1749 TestingProfile profile; 1749 TestingProfile profile;
1750 HostContentSettingsMap* host_content_settings_map = 1750 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
1751 HostContentSettingsMapFactory::GetForProfile(&profile); 1751
1752 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
1753 test_clock->SetNow(base::Time::Now());
1754 base::SimpleTestClock* clock = test_clock.get();
1755 map->SetClockForTesting(std::move(test_clock));
1756
1752 ContentSettingsForOneType host_settings; 1757 ContentSettingsForOneType host_settings;
1753 1758
1754 GURL url1("https://www.google.com/"); 1759 GURL url1("https://www.google.com/");
1755 GURL url2("https://maps.google.com/"); 1760 GURL url2("https://maps.google.com/");
1756 1761
1757 // Add setting for url1. 1762 // Add setting for url1.
1758 host_content_settings_map->SetContentSettingDefaultScope( 1763 map->SetContentSettingDefaultScope(url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
1759 url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), 1764 std::string(), CONTENT_SETTING_BLOCK);
1760 CONTENT_SETTING_BLOCK);
1761 1765
1762 // Make sure that the timestamp for url1 is different from |t|. 1766 // Make sure that the timestamp for url1 is different from |t|.
1763 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); 1767 clock->Advance(base::TimeDelta::FromSeconds(1));
1764 base::Time t = base::Time::Now(); 1768 base::Time t = clock->Now();
1765 1769
1766 // Add setting for url2. 1770 // Add setting for url2.
1767 host_content_settings_map->SetContentSettingDefaultScope( 1771 map->SetContentSettingDefaultScope(url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
1768 url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), 1772 std::string(), CONTENT_SETTING_BLOCK);
1769 CONTENT_SETTING_BLOCK);
1770 1773
1771 // Verify we have two pattern and the default. 1774 // Verify we have two pattern and the default.
1772 host_content_settings_map->GetSettingsForOneType( 1775 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(),
1773 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); 1776 &host_settings);
1774 EXPECT_EQ(3u, host_settings.size()); 1777 EXPECT_EQ(3u, host_settings.size());
1775 1778
1776 // Clear all settings since |t|. 1779 // Clear all settings since |t|.
1777 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( 1780 map->ClearSettingsForOneTypeWithPredicate(
1778 CONTENT_SETTINGS_TYPE_POPUPS, t, 1781 CONTENT_SETTINGS_TYPE_POPUPS, t,
1779 HostContentSettingsMap::PatternSourcePredicate()); 1782 HostContentSettingsMap::PatternSourcePredicate());
1780 1783
1781 // Verify we only have one pattern (url1) and the default. 1784 // Verify we only have one pattern (url1) and the default.
1782 host_content_settings_map->GetSettingsForOneType( 1785 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(),
1783 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); 1786 &host_settings);
1784 EXPECT_EQ(2u, host_settings.size()); 1787 EXPECT_EQ(2u, host_settings.size());
1785 EXPECT_EQ("https://www.google.com:443", 1788 EXPECT_EQ("https://www.google.com:443",
1786 host_settings[0].primary_pattern.ToString()); 1789 host_settings[0].primary_pattern.ToString());
1787 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); 1790 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString());
1788 1791
1789 // Clear all settings since the beginning of time. 1792 // Clear all settings since the beginning of time.
1790 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( 1793 map->ClearSettingsForOneTypeWithPredicate(
1791 CONTENT_SETTINGS_TYPE_POPUPS, base::Time(), 1794 CONTENT_SETTINGS_TYPE_POPUPS, base::Time(),
1792 HostContentSettingsMap::PatternSourcePredicate()); 1795 HostContentSettingsMap::PatternSourcePredicate());
1793 1796
1794 // Verify we only have the default setting. 1797 // Verify we only have the default setting.
1795 host_content_settings_map->GetSettingsForOneType( 1798 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(),
1796 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); 1799 &host_settings);
1797 EXPECT_EQ(1u, host_settings.size()); 1800 EXPECT_EQ(1u, host_settings.size());
1798 EXPECT_EQ("*", host_settings[0].primary_pattern.ToString()); 1801 EXPECT_EQ("*", host_settings[0].primary_pattern.ToString());
1799 } 1802 }
1800 1803
1804 TEST_F(HostContentSettingsMapTest, GetSettingLastModified) {
1805 base::test::ScopedFeatureList feature_list;
1806 feature_list.InitAndEnableFeature(features::kTabsInCbd);
1807
1808 TestingProfile profile;
1809 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
1810
1811 auto test_clock = base::MakeUnique<base::SimpleTestClock>();
1812 test_clock->SetNow(base::Time::Now());
1813 base::SimpleTestClock* clock = test_clock.get();
1814 map->SetClockForTesting(std::move(test_clock));
1815
1816 ContentSettingsForOneType host_settings;
1817
1818 GURL url("https://www.google.com/");
1819 ContentSettingsPattern pattern =
1820 ContentSettingsPattern::FromURLNoWildcard(url);
1821
1822 // Last modified date for non existant settings should be base::Time().
1823 base::Time t = map->GetSettingLastModifiedDate(
1824 pattern, ContentSettingsPattern::Wildcard(),
1825 CONTENT_SETTINGS_TYPE_POPUPS);
1826 EXPECT_EQ(base::Time(), t);
1827
1828 // Add setting for url.
1829 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
1830 std::string(), CONTENT_SETTING_BLOCK);
1831 t = map->GetSettingLastModifiedDate(pattern,
1832 ContentSettingsPattern::Wildcard(),
1833 CONTENT_SETTINGS_TYPE_POPUPS);
1834 EXPECT_EQ(t, clock->Now());
1835
1836 clock->Advance(base::TimeDelta::FromSeconds(1));
1837 // Modify setting.
1838 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
1839 std::string(), CONTENT_SETTING_ALLOW);
1840
1841 t = map->GetSettingLastModifiedDate(pattern,
1842 ContentSettingsPattern::Wildcard(),
1843 CONTENT_SETTINGS_TYPE_POPUPS);
1844 EXPECT_EQ(t, clock->Now());
1845 }
1846
1847 TEST_F(HostContentSettingsMapTest, LastModifiedIsNotRecordedWhenDisabled) {
1848 base::test::ScopedFeatureList feature_list;
1849 feature_list.InitAndDisableFeature(features::kTabsInCbd);
1850
1851 TestingProfile profile;
1852 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
1853 ContentSettingsForOneType host_settings;
1854
1855 GURL url("https://www.google.com/");
1856 ContentSettingsPattern pattern =
1857 ContentSettingsPattern::FromURLNoWildcard(url);
1858
1859 // Add setting for url.
1860 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS,
1861 std::string(), CONTENT_SETTING_BLOCK);
1862
1863 base::Time t = map->GetSettingLastModifiedDate(
1864 pattern, ContentSettingsPattern::Wildcard(),
1865 CONTENT_SETTINGS_TYPE_POPUPS);
1866 EXPECT_EQ(base::Time(), t);
1867 }
1868
1801 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { 1869 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) {
1802 TestingProfile profile; 1870 TestingProfile profile;
1803 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); 1871 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile);
1804 1872
1805 GURL valid_url("http://google.com"); 1873 GURL valid_url("http://google.com");
1806 EXPECT_TRUE(map->CanSetNarrowestContentSetting( 1874 EXPECT_TRUE(map->CanSetNarrowestContentSetting(
1807 valid_url, valid_url, 1875 valid_url, valid_url,
1808 CONTENT_SETTINGS_TYPE_POPUPS)); 1876 CONTENT_SETTINGS_TYPE_POPUPS));
1809 1877
1810 GURL invalid_url("about:blank"); 1878 GURL invalid_url("about:blank");
1811 EXPECT_FALSE(map->CanSetNarrowestContentSetting( 1879 EXPECT_FALSE(map->CanSetNarrowestContentSetting(
1812 invalid_url, invalid_url, 1880 invalid_url, invalid_url,
1813 CONTENT_SETTINGS_TYPE_POPUPS)); 1881 CONTENT_SETTINGS_TYPE_POPUPS));
1814 } 1882 }
OLDNEW
« no previous file with comments | « chrome/browser/content_settings/content_settings_pref_provider_unittest.cc ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698