Chromium Code Reviews| Index: chrome/browser/engagement/important_sites_util_unittest.cc |
| diff --git a/chrome/browser/engagement/important_sites_util_unittest.cc b/chrome/browser/engagement/important_sites_util_unittest.cc |
| index 20eea71e223c51498aff03fa52580bfa5afbf12f..8316bb199a71b9d5f446f4fe0ea0a1d86431e351 100644 |
| --- a/chrome/browser/engagement/important_sites_util_unittest.cc |
| +++ b/chrome/browser/engagement/important_sites_util_unittest.cc |
| @@ -7,10 +7,13 @@ |
| #include <memory> |
| #include <utility> |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| #include "base/files/scoped_temp_dir.h" |
| #include "base/macros.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "base/metrics/sample_vector.h" |
| +#include "base/run_loop.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/test/histogram_tester.h" |
| #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| @@ -29,13 +32,20 @@ |
| #include "components/history/core/browser/history_service.h" |
| #include "components/history/core/test/test_history_database.h" |
| #include "components/keyed_service/core/keyed_service.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "content/public/test/mock_storage_client.h" |
| +#include "storage/browser/quota/quota_manager_proxy.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace { |
| using BookmarkModel = bookmarks::BookmarkModel; |
| using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
| +using content::BrowserThread; |
| +using content::DOMStorageContext; |
| +using storage::QuotaManager; |
| const size_t kNumImportantSites = 5; |
| base::FilePath g_temp_history_dir; |
| @@ -76,6 +86,12 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { |
| profile(), &BuildTestHistoryService); |
| } |
| + void TearDown() override { |
| + base::RunLoop().RunUntilIdle(); |
| + quota_manager_ = nullptr; |
| + ChromeRenderViewHostTestHarness::TearDown(); |
| + } |
| + |
| void AddContentSetting(ContentSettingsType type, |
| ContentSetting setting, |
| const GURL& origin) { |
| @@ -131,9 +147,53 @@ class ImportantSitesUtilTest : public ChromeRenderViewHostTestHarness { |
| testing::UnorderedElementsAreArray(expected_sorted_origins)); |
| } |
| + QuotaManager* CreateQuotaManager() { |
| + quota_manager_ = new QuotaManager( |
| + false, temp_dir_.GetPath(), |
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get(), |
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::DB).get(), nullptr, |
| + storage::GetQuotaSettingsFunc()); |
| + return quota_manager_.get(); |
| + } |
| + |
| + void RegisterClient(const std::vector<content::MockOriginData>& data) { |
| + auto* client = new content::MockStorageClient( |
| + quota_manager_->proxy(), data.data(), storage::QuotaClient::kFileSystem, |
| + data.size()); |
| + quota_manager_->proxy()->RegisterClient(client); |
| + client->TouchAllOriginsAndNotify(); |
| + } |
| + |
| + void CreateLocalStorage( |
| + base::Time creation_time, |
| + int length, |
| + const base::FilePath::StringPieceType& storage_origin) { |
| + // Note: This test depends on details of how the dom_storage library |
| + // stores data in the host file system. |
| + base::FilePath storage_path = |
| + profile()->GetPath().AppendASCII("Local Storage"); |
| + base::CreateDirectory(storage_path); |
| + |
| + std::string data(' ', length); |
| + // Write file to localstorage. |
| + base::FilePath file_path = storage_path.Append(storage_origin); |
| + base::WriteFile(file_path, data.c_str(), length); |
| + base::TouchFile(file_path, creation_time, creation_time); |
| + } |
| + |
| + void FetchCompleted(std::vector<ImportantDomainInfo> domain_info) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + domain_info_ = domain_info; |
| + } |
| + |
| + const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; } |
| + |
| private: |
| base::ScopedTempDir temp_dir_; |
| BookmarkModel* model_ = nullptr; |
| + |
| + scoped_refptr<QuotaManager> quota_manager_; |
| + std::vector<ImportantDomainInfo> domain_info_; |
| }; |
| TEST_F(ImportantSitesUtilTest, TestNoImportantSites) { |
| @@ -476,3 +536,44 @@ TEST_F(ImportantSitesUtilTest, DialogBlacklisting) { |
| // Dialog should be blacklisted. |
| EXPECT_TRUE(ImportantSitesUtil::IsDialogDisabled(profile())); |
| } |
| + |
| +TEST_F(ImportantSitesUtilTest, PopulateUsage) { |
| + std::vector<ImportantDomainInfo> important_sites; |
| + ImportantDomainInfo i1; |
| + i1.registerable_domain = "example.com"; |
| + ImportantDomainInfo i2; |
| + i2.registerable_domain = "somethingelse.com"; |
| + important_sites.push_back(i1); |
| + important_sites.push_back(i2); |
| + |
| + const std::vector<content::MockOriginData> origins = { |
| + {"http://example.com/", storage::kStorageTypeTemporary, 1}, |
| + {"https://example.com/", storage::kStorageTypeTemporary, 2}, |
| + {"https://maps.example.com/", storage::kStorageTypeTemporary, 4}, |
| + {"http://google.com/", storage::kStorageTypePersistent, 8}, |
| + }; |
| + |
| + auto* quota_manager = CreateQuotaManager(); |
| + RegisterClient(origins); |
| + |
| + base::Time now = base::Time::Now(); |
| + CreateLocalStorage(now, 16, |
| + FILE_PATH_LITERAL("https_example.com_443.localstorage")); |
| + CreateLocalStorage(now, 32, |
| + FILE_PATH_LITERAL("https_bing.com_443.localstorage")); |
| + auto* dom_storage_context = |
| + content::BrowserContext::GetDefaultStoragePartition(profile()) |
| + ->GetDOMStorageContext(); |
| + |
| + ImportantSitesUtil::PopulateUsage( |
| + quota_manager, dom_storage_context, important_sites, |
| + base::Bind(&ImportantSitesUtilTest::FetchCompleted, |
| + base::Unretained(this))); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + 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.
|
| + EXPECT_EQ("example.com", domain_info()[0].registerable_domain); |
| + EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage); |
| + EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain); |
| + EXPECT_EQ(0, domain_info()[1].usage); |
| +} |