Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / quota / usage_tracker_unittest.cc
blob786146ab7d57157da59b4eda8d2d1e6f5f285722
1 // Copyright 2014 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.
5 #include "base/bind.h"
6 #include "base/run_loop.h"
7 #include "content/public/test/mock_special_storage_policy.h"
8 #include "net/base/net_util.h"
9 #include "storage/browser/quota/usage_tracker.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using storage::kQuotaStatusOk;
13 using storage::kStorageTypeTemporary;
14 using storage::QuotaClient;
15 using storage::QuotaClientList;
16 using storage::SpecialStoragePolicy;
17 using storage::StorageType;
18 using storage::UsageTracker;
20 namespace content {
22 namespace {
24 void DidGetGlobalUsage(bool* done,
25 int64* usage_out,
26 int64* unlimited_usage_out,
27 int64 usage,
28 int64 unlimited_usage) {
29 EXPECT_FALSE(*done);
30 *done = true;
31 *usage_out = usage;
32 *unlimited_usage_out = unlimited_usage;
35 void DidGetUsage(bool* done,
36 int64* usage_out,
37 int64 usage) {
38 EXPECT_FALSE(*done);
39 *done = true;
40 *usage_out = usage;
43 } // namespace
45 class MockQuotaClient : public QuotaClient {
46 public:
47 MockQuotaClient() {}
48 ~MockQuotaClient() override {}
50 ID id() const override { return kFileSystem; }
52 void OnQuotaManagerDestroyed() override {}
54 void GetOriginUsage(const GURL& origin,
55 StorageType type,
56 const GetUsageCallback& callback) override {
57 EXPECT_EQ(kStorageTypeTemporary, type);
58 int64 usage = GetUsage(origin);
59 base::MessageLoop::current()->PostTask(FROM_HERE,
60 base::Bind(callback, usage));
63 void GetOriginsForType(StorageType type,
64 const GetOriginsCallback& callback) override {
65 EXPECT_EQ(kStorageTypeTemporary, type);
66 std::set<GURL> origins;
67 for (UsageMap::const_iterator itr = usage_map_.begin();
68 itr != usage_map_.end(); ++itr) {
69 origins.insert(itr->first);
71 base::MessageLoop::current()->PostTask(FROM_HERE,
72 base::Bind(callback, origins));
75 void GetOriginsForHost(StorageType type,
76 const std::string& host,
77 const GetOriginsCallback& callback) override {
78 EXPECT_EQ(kStorageTypeTemporary, type);
79 std::set<GURL> origins;
80 for (UsageMap::const_iterator itr = usage_map_.begin();
81 itr != usage_map_.end(); ++itr) {
82 if (net::GetHostOrSpecFromURL(itr->first) == host)
83 origins.insert(itr->first);
85 base::MessageLoop::current()->PostTask(FROM_HERE,
86 base::Bind(callback, origins));
89 void DeleteOriginData(const GURL& origin,
90 StorageType type,
91 const DeletionCallback& callback) override {
92 EXPECT_EQ(kStorageTypeTemporary, type);
93 usage_map_.erase(origin);
94 base::MessageLoop::current()->PostTask(
95 FROM_HERE, base::Bind(callback, kQuotaStatusOk));
98 bool DoesSupport(storage::StorageType type) const override {
99 return type == storage::kStorageTypeTemporary;
102 int64 GetUsage(const GURL& origin) {
103 UsageMap::const_iterator found = usage_map_.find(origin);
104 if (found == usage_map_.end())
105 return 0;
106 return found->second;
109 void SetUsage(const GURL& origin, int64 usage) {
110 usage_map_[origin] = usage;
113 int64 UpdateUsage(const GURL& origin, int64 delta) {
114 return usage_map_[origin] += delta;
117 private:
118 typedef std::map<GURL, int64> UsageMap;
120 UsageMap usage_map_;
122 DISALLOW_COPY_AND_ASSIGN(MockQuotaClient);
125 class UsageTrackerTest : public testing::Test {
126 public:
127 UsageTrackerTest()
128 : storage_policy_(new MockSpecialStoragePolicy()),
129 usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary,
130 storage_policy_.get(), NULL) {
133 ~UsageTrackerTest() override {}
135 UsageTracker* usage_tracker() {
136 return &usage_tracker_;
139 void UpdateUsage(const GURL& origin, int64 delta) {
140 quota_client_.UpdateUsage(origin, delta);
141 usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta);
142 base::RunLoop().RunUntilIdle();
145 void UpdateUsageWithoutNotification(const GURL& origin, int64 delta) {
146 quota_client_.UpdateUsage(origin, delta);
149 void GetGlobalLimitedUsage(int64* limited_usage) {
150 bool done = false;
151 usage_tracker_.GetGlobalLimitedUsage(base::Bind(
152 &DidGetUsage, &done, limited_usage));
153 base::RunLoop().RunUntilIdle();
155 EXPECT_TRUE(done);
158 void GetGlobalUsage(int64* usage, int64* unlimited_usage) {
159 bool done = false;
160 usage_tracker_.GetGlobalUsage(base::Bind(
161 &DidGetGlobalUsage,
162 &done, usage, unlimited_usage));
163 base::RunLoop().RunUntilIdle();
165 EXPECT_TRUE(done);
168 void GetHostUsage(const std::string& host, int64* usage) {
169 bool done = false;
170 usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage));
171 base::RunLoop().RunUntilIdle();
173 EXPECT_TRUE(done);
176 void GrantUnlimitedStoragePolicy(const GURL& origin) {
177 if (!storage_policy_->IsStorageUnlimited(origin)) {
178 storage_policy_->AddUnlimited(origin);
179 storage_policy_->NotifyGranted(
180 origin, SpecialStoragePolicy::STORAGE_UNLIMITED);
184 void RevokeUnlimitedStoragePolicy(const GURL& origin) {
185 if (storage_policy_->IsStorageUnlimited(origin)) {
186 storage_policy_->RemoveUnlimited(origin);
187 storage_policy_->NotifyRevoked(
188 origin, SpecialStoragePolicy::STORAGE_UNLIMITED);
192 void SetUsageCacheEnabled(const GURL& origin, bool enabled) {
193 usage_tracker_.SetUsageCacheEnabled(
194 quota_client_.id(), origin, enabled);
197 private:
198 QuotaClientList GetUsageTrackerList() {
199 QuotaClientList client_list;
200 client_list.push_back(&quota_client_);
201 return client_list;
204 base::MessageLoop message_loop_;
206 scoped_refptr<MockSpecialStoragePolicy> storage_policy_;
207 MockQuotaClient quota_client_;
208 UsageTracker usage_tracker_;
210 DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest);
213 TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) {
214 int64 usage = 0;
215 int64 unlimited_usage = 0;
216 int64 host_usage = 0;
217 GetGlobalUsage(&usage, &unlimited_usage);
218 EXPECT_EQ(0, usage);
219 EXPECT_EQ(0, unlimited_usage);
221 const GURL origin("http://example.com");
222 const std::string host(net::GetHostOrSpecFromURL(origin));
224 UpdateUsage(origin, 100);
225 GetGlobalUsage(&usage, &unlimited_usage);
226 GetHostUsage(host, &host_usage);
227 EXPECT_EQ(100, usage);
228 EXPECT_EQ(0, unlimited_usage);
229 EXPECT_EQ(100, host_usage);
231 GrantUnlimitedStoragePolicy(origin);
232 GetGlobalUsage(&usage, &unlimited_usage);
233 GetHostUsage(host, &host_usage);
234 EXPECT_EQ(100, usage);
235 EXPECT_EQ(100, unlimited_usage);
236 EXPECT_EQ(100, host_usage);
238 RevokeUnlimitedStoragePolicy(origin);
239 GetGlobalUsage(&usage, &unlimited_usage);
240 GetHostUsage(host, &host_usage);
241 EXPECT_EQ(100, usage);
242 EXPECT_EQ(0, unlimited_usage);
243 EXPECT_EQ(100, host_usage);
246 TEST_F(UsageTrackerTest, CacheDisabledClientTest) {
247 int64 usage = 0;
248 int64 unlimited_usage = 0;
249 int64 host_usage = 0;
251 const GURL origin("http://example.com");
252 const std::string host(net::GetHostOrSpecFromURL(origin));
254 UpdateUsage(origin, 100);
255 GetGlobalUsage(&usage, &unlimited_usage);
256 GetHostUsage(host, &host_usage);
257 EXPECT_EQ(100, usage);
258 EXPECT_EQ(0, unlimited_usage);
259 EXPECT_EQ(100, host_usage);
261 UpdateUsageWithoutNotification(origin, 100);
262 GetGlobalUsage(&usage, &unlimited_usage);
263 GetHostUsage(host, &host_usage);
264 EXPECT_EQ(100, usage);
265 EXPECT_EQ(0, unlimited_usage);
266 EXPECT_EQ(100, host_usage);
268 GrantUnlimitedStoragePolicy(origin);
269 UpdateUsageWithoutNotification(origin, 100);
270 SetUsageCacheEnabled(origin, false);
271 UpdateUsageWithoutNotification(origin, 100);
273 GetGlobalUsage(&usage, &unlimited_usage);
274 GetHostUsage(host, &host_usage);
275 EXPECT_EQ(400, usage);
276 EXPECT_EQ(400, unlimited_usage);
277 EXPECT_EQ(400, host_usage);
279 RevokeUnlimitedStoragePolicy(origin);
280 GetGlobalUsage(&usage, &unlimited_usage);
281 GetHostUsage(host, &host_usage);
282 EXPECT_EQ(400, usage);
283 EXPECT_EQ(0, unlimited_usage);
284 EXPECT_EQ(400, host_usage);
286 SetUsageCacheEnabled(origin, true);
287 UpdateUsage(origin, 100);
289 GetGlobalUsage(&usage, &unlimited_usage);
290 GetHostUsage(host, &host_usage);
291 EXPECT_EQ(500, usage);
292 EXPECT_EQ(0, unlimited_usage);
293 EXPECT_EQ(500, host_usage);
296 TEST_F(UsageTrackerTest, LimitedGlobalUsageTest) {
297 const GURL kNormal("http://normal");
298 const GURL kUnlimited("http://unlimited");
299 const GURL kNonCached("http://non_cached");
300 const GURL kNonCachedUnlimited("http://non_cached-unlimited");
302 GrantUnlimitedStoragePolicy(kUnlimited);
303 GrantUnlimitedStoragePolicy(kNonCachedUnlimited);
305 SetUsageCacheEnabled(kNonCached, false);
306 SetUsageCacheEnabled(kNonCachedUnlimited, false);
308 UpdateUsageWithoutNotification(kNormal, 1);
309 UpdateUsageWithoutNotification(kUnlimited, 2);
310 UpdateUsageWithoutNotification(kNonCached, 4);
311 UpdateUsageWithoutNotification(kNonCachedUnlimited, 8);
313 int64 limited_usage = 0;
314 int64 total_usage = 0;
315 int64 unlimited_usage = 0;
317 GetGlobalLimitedUsage(&limited_usage);
318 GetGlobalUsage(&total_usage, &unlimited_usage);
319 EXPECT_EQ(1 + 4, limited_usage);
320 EXPECT_EQ(1 + 2 + 4 + 8, total_usage);
321 EXPECT_EQ(2 + 8, unlimited_usage);
323 UpdateUsageWithoutNotification(kNonCached, 16 - 4);
324 UpdateUsageWithoutNotification(kNonCachedUnlimited, 32 - 8);
326 GetGlobalLimitedUsage(&limited_usage);
327 GetGlobalUsage(&total_usage, &unlimited_usage);
328 EXPECT_EQ(1 + 16, limited_usage);
329 EXPECT_EQ(1 + 2 + 16 + 32, total_usage);
330 EXPECT_EQ(2 + 32, unlimited_usage);
334 } // namespace content