Chromium Code Reviews| 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 if (run_loop_) | |
| 188 run_loop_->Quit(); | |
| 189 } | |
| 190 | |
| 191 void WaitForTasksOnIOThread() { | |
| 192 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 193 run_loop_.reset(new base::RunLoop()); | |
| 194 run_loop_->Run(); | |
|
dmurph
2017/03/27 22:22:45
What would happen if we just created a runloop and
dullweber
2017/03/28 14:06:45
That works :) I thought RunUntilIdle() only waits
| |
| 195 } | |
| 196 | |
| 197 const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; } | |
| 198 | |
| 134 private: | 199 private: |
| 135 base::ScopedTempDir temp_dir_; | 200 base::ScopedTempDir temp_dir_; |
| 136 BookmarkModel* model_ = nullptr; | 201 BookmarkModel* model_ = nullptr; |
| 202 | |
| 203 std::unique_ptr<base::RunLoop> run_loop_; | |
| 204 scoped_refptr<QuotaManager> quota_manager_; | |
| 205 std::vector<ImportantDomainInfo> domain_info_; | |
| 137 }; | 206 }; |
| 138 | 207 |
| 139 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { | 208 TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { |
| 140 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( | 209 EXPECT_TRUE(ImportantSitesUtil::GetImportantRegisterableDomains( |
| 141 profile(), kNumImportantSites) | 210 profile(), kNumImportantSites) |
| 142 .empty()); | 211 .empty()); |
| 143 } | 212 } |
| 144 | 213 |
| 145 TEST_F(ImportantSitesUtilTest, SourceOrdering) { | 214 TEST_F(ImportantSitesUtilTest, SourceOrdering) { |
| 146 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 215 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 | 538 |
| 470 // Items should still be present. | 539 // Items should still be present. |
| 471 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( | 540 important_sites = ImportantSitesUtil::GetImportantRegisterableDomains( |
| 472 profile(), kNumImportantSites); | 541 profile(), kNumImportantSites); |
| 473 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, | 542 ExpectImportantResultsEq(expected_sorted_domains, expected_sorted_origins, |
| 474 important_sites); | 543 important_sites); |
| 475 | 544 |
| 476 // Dialog should be blacklisted. | 545 // Dialog should be blacklisted. |
| 477 EXPECT_TRUE(ImportantSitesUtil::IsDialogDisabled(profile())); | 546 EXPECT_TRUE(ImportantSitesUtil::IsDialogDisabled(profile())); |
| 478 } | 547 } |
| 548 | |
| 549 TEST_F(ImportantSitesUtilTest, PopulateUsage) { | |
| 550 std::vector<ImportantDomainInfo> important_sites; | |
| 551 ImportantDomainInfo i1; | |
| 552 i1.registerable_domain = "example.com"; | |
| 553 ImportantDomainInfo i2; | |
| 554 i2.registerable_domain = "somethingelse.com"; | |
| 555 important_sites.push_back(i1); | |
| 556 important_sites.push_back(i2); | |
| 557 | |
| 558 const std::vector<content::MockOriginData> origins = { | |
| 559 {"http://example.com/", storage::kStorageTypeTemporary, 1}, | |
| 560 {"https://example.com/", storage::kStorageTypeTemporary, 2}, | |
| 561 {"https://maps.example.com/", storage::kStorageTypeTemporary, 4}, | |
| 562 {"http://google.com/", storage::kStorageTypePersistent, 8}, | |
| 563 }; | |
| 564 | |
| 565 auto* quota_manager = CreateQuotaManager(); | |
| 566 RegisterClient(origins); | |
| 567 | |
| 568 base::Time now = base::Time::Now(); | |
| 569 CreateLocalStorage(now, 16, | |
| 570 FILE_PATH_LITERAL("https_example.com_443.localstorage")); | |
| 571 CreateLocalStorage(now, 32, | |
| 572 FILE_PATH_LITERAL("https_bing.com_443.localstorage")); | |
| 573 auto* dom_storage_context = | |
| 574 content::BrowserContext::GetDefaultStoragePartition(profile()) | |
| 575 ->GetDOMStorageContext(); | |
| 576 | |
| 577 ImportantSitesUtil::PopulateUsage( | |
| 578 quota_manager, dom_storage_context, important_sites, | |
| 579 base::Bind(&ImportantSitesUtilTest::FetchCompleted, | |
| 580 base::Unretained(this))); | |
| 581 WaitForTasksOnIOThread(); | |
| 582 | |
| 583 EXPECT_EQ(important_sites.size(), domain_info().size()); | |
| 584 EXPECT_EQ("example.com", domain_info()[0].registerable_domain); | |
| 585 EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage); | |
| 586 EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain); | |
| 587 EXPECT_EQ(0, domain_info()[1].usage); | |
| 588 } | |
| OLD | NEW |