OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/engagement/important_sites_usage_counter.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/test/histogram_tester.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/storage_partition.h" |
| 16 #include "content/public/test/mock_storage_client.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "storage/browser/quota/quota_manager_proxy.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
| 22 using content::BrowserThread; |
| 23 using content::DOMStorageContext; |
| 24 using storage::QuotaManager; |
| 25 |
| 26 class ImportantSitesUsageCounterTest : public testing::Test { |
| 27 public: |
| 28 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 29 |
| 30 void TearDown() override { base::RunLoop().RunUntilIdle(); } |
| 31 |
| 32 TestingProfile* profile() { return &profile_; } |
| 33 |
| 34 QuotaManager* CreateQuotaManager() { |
| 35 quota_manager_ = new QuotaManager( |
| 36 false, temp_dir_.GetPath(), |
| 37 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO).get(), |
| 38 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB).get(), nullptr, |
| 39 storage::GetQuotaSettingsFunc()); |
| 40 return quota_manager_.get(); |
| 41 } |
| 42 |
| 43 void RegisterClient(const std::vector<content::MockOriginData>& data) { |
| 44 auto* client = new content::MockStorageClient( |
| 45 quota_manager_->proxy(), data.data(), storage::QuotaClient::kFileSystem, |
| 46 data.size()); |
| 47 quota_manager_->proxy()->RegisterClient(client); |
| 48 client->TouchAllOriginsAndNotify(); |
| 49 } |
| 50 |
| 51 void CreateLocalStorage( |
| 52 base::Time creation_time, |
| 53 int length, |
| 54 const base::FilePath::StringPieceType& storage_origin) { |
| 55 // Note: This test depends on details of how the dom_storage library |
| 56 // stores data in the host file system. |
| 57 base::FilePath storage_path = |
| 58 profile()->GetPath().AppendASCII("Local Storage"); |
| 59 base::CreateDirectory(storage_path); |
| 60 |
| 61 std::string data(' ', length); |
| 62 // Write file to localstorage. |
| 63 base::FilePath file_path = storage_path.Append(storage_origin); |
| 64 base::WriteFile(file_path, data.c_str(), length); |
| 65 base::TouchFile(file_path, creation_time, creation_time); |
| 66 } |
| 67 |
| 68 void FetchCompleted(std::vector<ImportantDomainInfo> domain_info) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 domain_info_ = domain_info; |
| 71 } |
| 72 |
| 73 const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; } |
| 74 |
| 75 private: |
| 76 content::TestBrowserThreadBundle thread_bundle_; |
| 77 TestingProfile profile_; |
| 78 base::ScopedTempDir temp_dir_; |
| 79 scoped_refptr<QuotaManager> quota_manager_; |
| 80 std::vector<ImportantDomainInfo> domain_info_; |
| 81 }; |
| 82 |
| 83 TEST_F(ImportantSitesUsageCounterTest, PopulateUsage) { |
| 84 std::vector<ImportantDomainInfo> important_sites; |
| 85 ImportantDomainInfo i1; |
| 86 i1.registerable_domain = "example.com"; |
| 87 ImportantDomainInfo i2; |
| 88 i2.registerable_domain = "somethingelse.com"; |
| 89 important_sites.push_back(i1); |
| 90 important_sites.push_back(i2); |
| 91 |
| 92 const std::vector<content::MockOriginData> origins = { |
| 93 {"http://example.com/", storage::kStorageTypeTemporary, 1}, |
| 94 {"https://example.com/", storage::kStorageTypeTemporary, 2}, |
| 95 {"https://maps.example.com/", storage::kStorageTypeTemporary, 4}, |
| 96 {"http://google.com/", storage::kStorageTypePersistent, 8}, |
| 97 }; |
| 98 |
| 99 auto* quota_manager = CreateQuotaManager(); |
| 100 RegisterClient(origins); |
| 101 |
| 102 base::Time now = base::Time::Now(); |
| 103 CreateLocalStorage(now, 16, |
| 104 FILE_PATH_LITERAL("https_example.com_443.localstorage")); |
| 105 CreateLocalStorage(now, 32, |
| 106 FILE_PATH_LITERAL("https_bing.com_443.localstorage")); |
| 107 auto* dom_storage_context = |
| 108 content::BrowserContext::GetDefaultStoragePartition(profile()) |
| 109 ->GetDOMStorageContext(); |
| 110 |
| 111 (new ImportantSitesUsageCounter( |
| 112 important_sites, quota_manager, dom_storage_context, |
| 113 base::Bind(&ImportantSitesUsageCounterTest::FetchCompleted, |
| 114 base::Unretained(this)))) |
| 115 ->RunAndDestroySelf(); |
| 116 base::RunLoop().RunUntilIdle(); |
| 117 |
| 118 EXPECT_EQ(important_sites.size(), domain_info().size()); |
| 119 // The first important site is example.com. It uses 1B quota storage for |
| 120 // http://example.com/, 2B for https://example.com and 4B for |
| 121 // https://maps.example.com. On top of that it uses 16B localstorage. |
| 122 EXPECT_EQ("example.com", domain_info()[0].registerable_domain); |
| 123 EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage); |
| 124 // The second important site is somethingelse.com but it doesn't use any |
| 125 // quota. We still expect it to be returned and not dropped. |
| 126 EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain); |
| 127 EXPECT_EQ(0, domain_info()[1].usage); |
| 128 } |
OLD | NEW |