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/test_browser_thread_bundle.h" | |
17 #include "storage/browser/quota/quota_manager_proxy.h" | |
18 #include "storage/browser/test/mock_storage_client.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 local storage. | |
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 if (run_loop_) | |
Bernhard Bauer
2017/05/05 09:36:05
Do you expect not to have a run loop? If this ends
dullweber
2017/05/05 10:51:00
Thanks! That sounds like a much better approach fo
| |
72 run_loop_->Quit(); | |
73 } | |
74 | |
75 void WaitForResult() { | |
76 run_loop_.reset(new base::RunLoop()); | |
77 run_loop_->Run(); | |
78 } | |
79 | |
80 const std::vector<ImportantDomainInfo>& domain_info() { return domain_info_; } | |
81 | |
82 private: | |
83 content::TestBrowserThreadBundle thread_bundle_; | |
84 TestingProfile profile_; | |
85 base::ScopedTempDir temp_dir_; | |
86 scoped_refptr<QuotaManager> quota_manager_; | |
87 std::vector<ImportantDomainInfo> domain_info_; | |
88 std::unique_ptr<base::RunLoop> run_loop_; | |
89 }; | |
90 | |
91 TEST_F(ImportantSitesUsageCounterTest, PopulateUsage) { | |
92 std::vector<ImportantDomainInfo> important_sites; | |
93 ImportantDomainInfo i1; | |
94 i1.registerable_domain = "example.com"; | |
95 ImportantDomainInfo i2; | |
96 i2.registerable_domain = "somethingelse.com"; | |
97 important_sites.push_back(i1); | |
98 important_sites.push_back(i2); | |
99 | |
100 const std::vector<content::MockOriginData> origins = { | |
101 {"http://example.com/", storage::kStorageTypeTemporary, 1}, | |
102 {"https://example.com/", storage::kStorageTypeTemporary, 2}, | |
103 {"https://maps.example.com/", storage::kStorageTypeTemporary, 4}, | |
104 {"http://google.com/", storage::kStorageTypePersistent, 8}, | |
105 }; | |
106 | |
107 QuotaManager* quota_manager = CreateQuotaManager(); | |
108 RegisterClient(origins); | |
109 | |
110 base::Time now = base::Time::Now(); | |
111 CreateLocalStorage(now, 16, | |
112 FILE_PATH_LITERAL("https_example.com_443.localstorage")); | |
113 CreateLocalStorage(now, 32, | |
114 FILE_PATH_LITERAL("https_bing.com_443.localstorage")); | |
115 DOMStorageContext* dom_storage_context = | |
116 content::BrowserContext::GetDefaultStoragePartition(profile()) | |
117 ->GetDOMStorageContext(); | |
118 | |
119 ImportantSitesUsageCounter::GetUsage( | |
120 important_sites, quota_manager, dom_storage_context, | |
121 base::Bind(&ImportantSitesUsageCounterTest::FetchCompleted, | |
122 base::Unretained(this))); | |
123 WaitForResult(); | |
124 | |
125 EXPECT_EQ(important_sites.size(), domain_info().size()); | |
126 // The first important site is example.com. It uses 1B quota storage for | |
127 // http://example.com/, 2B for https://example.com and 4B for | |
128 // https://maps.example.com. On top of that it uses 16B local storage. | |
129 EXPECT_EQ("example.com", domain_info()[0].registerable_domain); | |
130 EXPECT_EQ(1 + 2 + 4 + 16, domain_info()[0].usage); | |
131 // The second important site is somethingelse.com but it doesn't use any | |
132 // quota. We still expect it to be returned and not dropped. | |
133 EXPECT_EQ("somethingelse.com", domain_info()[1].registerable_domain); | |
134 EXPECT_EQ(0, domain_info()[1].usage); | |
135 } | |
OLD | NEW |