OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/engagement/important_sites_util.h" | 5 #include "chrome/browser/engagement/important_sites_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_path.h" | |
11 #include "base/files/file_util.h" | |
10 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
13 #include "base/metrics/sample_vector.h" | 15 #include "base/metrics/sample_vector.h" |
16 #include "base/run_loop.h" | |
14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
15 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 19 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 20 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
18 #include "chrome/browser/engagement/site_engagement_score.h" | 21 #include "chrome/browser/engagement/site_engagement_score.h" |
19 #include "chrome/browser/engagement/site_engagement_service.h" | 22 #include "chrome/browser/engagement/site_engagement_service.h" |
20 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" |
21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
22 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
23 #include "components/bookmarks/browser/bookmark_model.h" | 26 #include "components/bookmarks/browser/bookmark_model.h" |
24 #include "components/bookmarks/test/bookmark_test_helpers.h" | 27 #include "components/bookmarks/test/bookmark_test_helpers.h" |
25 #include "components/content_settings/core/browser/host_content_settings_map.h" | 28 #include "components/content_settings/core/browser/host_content_settings_map.h" |
26 #include "components/content_settings/core/common/content_settings.h" | 29 #include "components/content_settings/core/common/content_settings.h" |
27 #include "components/content_settings/core/common/content_settings_pattern.h" | 30 #include "components/content_settings/core/common/content_settings_pattern.h" |
28 #include "components/history/core/browser/history_database_params.h" | 31 #include "components/history/core/browser/history_database_params.h" |
29 #include "components/history/core/browser/history_service.h" | 32 #include "components/history/core/browser/history_service.h" |
30 #include "components/history/core/test/test_history_database.h" | 33 #include "components/history/core/test/test_history_database.h" |
31 #include "components/keyed_service/core/keyed_service.h" | 34 #include "components/keyed_service/core/keyed_service.h" |
35 #include "content/public/browser/browser_thread.h" | |
36 #include "content/public/browser/storage_partition.h" | |
32 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
38 #include "content/public/test/mock_storage_client.h" | |
39 #include "storage/browser/quota/quota_manager_proxy.h" | |
33 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
35 | 42 |
36 namespace { | 43 namespace { |
37 using BookmarkModel = bookmarks::BookmarkModel; | 44 using BookmarkModel = bookmarks::BookmarkModel; |
38 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; | 45 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
46 using content::BrowserThread; | |
47 using content::DOMStorageContext; | |
48 using storage::QuotaManager; | |
39 | 49 |
40 const size_t kNumImportantSites = 5; | 50 const size_t kNumImportantSites = 5; |
41 base::FilePath g_temp_history_dir; | 51 base::FilePath g_temp_history_dir; |
42 | 52 |
43 std::unique_ptr<KeyedService> BuildTestHistoryService( | 53 std::unique_ptr<KeyedService> BuildTestHistoryService( |
44 content::BrowserContext* context) { | 54 content::BrowserContext* context) { |
45 std::unique_ptr<history::HistoryService> service( | 55 std::unique_ptr<history::HistoryService> service( |
46 new history::HistoryService()); | 56 new history::HistoryService()); |
47 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); | 57 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); |
48 return std::move(service); | 58 return std::move(service); |
(...skipping 20 matching lines...) Expand all Loading... | |
69 public: | 79 public: |
70 void SetUp() override { | 80 void SetUp() override { |
71 ChromeRenderViewHostTestHarness::SetUp(); | 81 ChromeRenderViewHostTestHarness::SetUp(); |
72 SiteEngagementScore::SetParamValuesForTesting(); | 82 SiteEngagementScore::SetParamValuesForTesting(); |
73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 83 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
74 g_temp_history_dir = temp_dir_.GetPath(); | 84 g_temp_history_dir = temp_dir_.GetPath(); |
75 HistoryServiceFactory::GetInstance()->SetTestingFactory( | 85 HistoryServiceFactory::GetInstance()->SetTestingFactory( |
76 profile(), &BuildTestHistoryService); | 86 profile(), &BuildTestHistoryService); |
77 } | 87 } |
78 | 88 |
89 void TearDown() override { | |
90 base::RunLoop().RunUntilIdle(); | |
91 quota_manager_ = nullptr; | |
92 ChromeRenderViewHostTestHarness::TearDown(); | |
93 } | |
94 | |
79 void AddContentSetting(ContentSettingsType type, | 95 void AddContentSetting(ContentSettingsType type, |
80 ContentSetting setting, | 96 ContentSetting setting, |
81 const GURL& origin) { | 97 const GURL& origin) { |
82 HostContentSettingsMapFactory::GetForProfile(profile()) | 98 HostContentSettingsMapFactory::GetForProfile(profile()) |
83 ->SetContentSettingCustomScope( | 99 ->SetContentSettingCustomScope( |
84 ContentSettingsPattern::FromURLNoWildcard(origin), | 100 ContentSettingsPattern::FromURLNoWildcard(origin), |
85 ContentSettingsPattern::Wildcard(), type, | 101 ContentSettingsPattern::Wildcard(), type, |
86 content_settings::ResourceIdentifier(), setting); | 102 content_settings::ResourceIdentifier(), setting); |
87 EXPECT_EQ(setting, | 103 EXPECT_EQ(setting, |
88 HostContentSettingsMapFactory::GetForProfile(profile()) | 104 HostContentSettingsMapFactory::GetForProfile(profile()) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 std::vector<GURL> actual_origins; | 140 std::vector<GURL> actual_origins; |
125 for (size_t i = 0; i < important_sites.size(); i++) { | 141 for (size_t i = 0; i < important_sites.size(); i++) { |
126 actual_domains.push_back(important_sites[i].registerable_domain); | 142 actual_domains.push_back(important_sites[i].registerable_domain); |
127 actual_origins.push_back(important_sites[i].example_origin); | 143 actual_origins.push_back(important_sites[i].example_origin); |
128 } | 144 } |
129 EXPECT_THAT(actual_domains, testing::UnorderedElementsAreArray(domains)); | 145 EXPECT_THAT(actual_domains, testing::UnorderedElementsAreArray(domains)); |
130 EXPECT_THAT(actual_origins, | 146 EXPECT_THAT(actual_origins, |
131 testing::UnorderedElementsAreArray(expected_sorted_origins)); | 147 testing::UnorderedElementsAreArray(expected_sorted_origins)); |
132 } | 148 } |
133 | 149 |
150 QuotaManager* CreateQuotaManager() { | |
151 quota_manager_ = new QuotaManager( | |
152 false, temp_dir_.GetPath(), | |
153 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get(), | |
154 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB).get(), nullptr, | |
155 storage::GetQuotaSettingsFunc()); | |
156 return quota_manager_.get(); | |
157 } | |
158 | |
159 void RegisterClient(const std::vector<content::MockOriginData>& data) { | |
160 auto* client = new content::MockStorageClient( | |
161 quota_manager_->proxy(), data.data(), storage::QuotaClient::kFileSystem, | |
162 data.size()); | |
163 quota_manager_->proxy()->RegisterClient(client); | |
164 client->TouchAllOriginsAndNotify(); | |
165 } | |
166 | |
167 void CreateLocalStorage( | |
168 base::Time creation_time, | |
169 int length, | |
170 const base::FilePath::StringPieceType& storage_origin) { | |
171 // Note: This test depends on details of how the dom_storage library | |
172 // stores data in the host file system. | |
173 base::FilePath storage_path = | |
174 profile()->GetPath().AppendASCII("Local Storage"); | |
175 base::CreateDirectory(storage_path); | |
176 | |
177 std::string data(' ', length); | |
178 // Write file to localstorage. | |
179 base::FilePath file_path = storage_path.Append(storage_origin); | |
180 base::WriteFile(file_path, data.c_str(), length); | |
181 base::TouchFile(file_path, creation_time, creation_time); | |
182 } | |
183 | |
184 void FetchCompleted(std::vector<ImportantDomainInfo> domain_info) { | |
185 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
186 domain_info_ = domain_info; | |
187 } | |
188 | |
189 const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; } | |
190 | |
134 private: | 191 private: |
135 base::ScopedTempDir temp_dir_; | 192 base::ScopedTempDir temp_dir_; |
136 BookmarkModel* model_ = nullptr; | 193 BookmarkModel* model_ = nullptr; |
194 | |
195 scoped_refptr<QuotaManager> quota_manager_; | |
196 std::vector<ImportantDomainInfo> domain_info_; | |
137 }; | 197 }; |
138 | 198 |
139 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { | 199 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { |
140 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( | 200 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( |
141 profile(), kNumImportantSites) | 201 profile(), kNumImportantSites) |
142 .empty()); | 202 .empty()); |
143 } | 203 } |
144 | 204 |
145 TEST_F(ImportantSitesUtilTest, SourceOrdering) { | 205 TEST_F(ImportantSitesUtilTest, SourceOrdering) { |
146 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 206 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
469 | 529 |
470 // Items should still be present. | 530 // Items should still be present. |
471 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 531 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
472 profile(), kNumImportantSites); | 532 profile(), kNumImportantSites); |
473 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 533 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
474 important_sites); | 534 important_sites); |
475 | 535 |
476 // Dialog should be blacklisted. | 536 // Dialog should be blacklisted. |
477 EXPECT_TRUE(ImportantSitesUtil::IsDialogDisabled(profile())); | 537 EXPECT_TRUE(ImportantSitesUtil::IsDialogDisabled(profile())); |
478 } | 538 } |
539 | |
540 TEST_F(ImportantSitesUtilTest, PopulateUsage) { | |
541 std::vector<ImportantDomainInfo> important_sites; | |
542 ImportantDomainInfo i1; | |
543 i1.registerable_domain = "example.com"; | |
544 ImportantDomainInfo i2; | |
545 i2.registerable_domain = "somethingelse.com"; | |
546 important_sites.push_back(i1); | |
547 important_sites.push_back(i2); | |
548 | |
549 const std::vector<content::MockOriginData> origins = { | |
550 {"http://example.com/", storage::kStorageTypeTemporary, 1}, | |
551 {"https://example.com/", storage::kStorageTypeTemporary, 2}, | |
552 {"https://maps.example.com/", storage::kStorageTypeTemporary, 4}, | |
553 {"http://google.com/", storage::kStorageTypePersistent, 8}, | |
554 }; | |
555 | |
556 auto* quota_manager = CreateQuotaManager(); | |
557 RegisterClient(origins); | |
558 | |
559 base::Time now = base::Time::Now(); | |
560 CreateLocalStorage(now, 16, | |
561 FILE_PATH_LITERAL("https_example.com_443.localstorage")); | |
562 CreateLocalStorage(now, 32, | |
563 FILE_PATH_LITERAL("https_bing.com_443.localstorage")); | |
564 auto* dom_storage_context = | |
565 content::BrowserContext::GetDefaultStoragePartition(profile()) | |
566 ->GetDOMStorageContext(); | |
567 | |
568 ImportantSitesUtil::PopulateUsage( | |
569 quota_manager, dom_storage_context, important_sites, | |
570 base::Bind(&ImportantSitesUtilTest::FetchCompleted, | |
571 base::Unretained(this))); | |
572 base::RunLoop().RunUntilIdle(); | |
573 | |
574 EXPECT_EQ(important_sites.size(), domain_info().size()); | |
dominickn
2017/04/10 05:06:12
Can you add an explicit comment explaining why you
dullweber
2017/04/10 13:29:39
Done.
| |
575 EXPECT_EQ("example.com", domain_info()[0].registerable_domain); | |
576 EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage); | |
577 EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain); | |
578 EXPECT_EQ(0, domain_info()[1].usage); | |
579 } | |
OLD | NEW |