OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/macros.h" |
7 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
8 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
9 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
10 #include "content/public/test/mock_special_storage_policy.h" | 13 #include "content/public/test/mock_special_storage_policy.h" |
11 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
12 #include "storage/browser/quota/usage_tracker.h" | 15 #include "storage/browser/quota/usage_tracker.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 | 17 |
15 using storage::kQuotaStatusOk; | 18 using storage::kQuotaStatusOk; |
16 using storage::kStorageTypeTemporary; | 19 using storage::kStorageTypeTemporary; |
17 using storage::QuotaClient; | 20 using storage::QuotaClient; |
18 using storage::QuotaClientList; | 21 using storage::QuotaClientList; |
19 using storage::SpecialStoragePolicy; | 22 using storage::SpecialStoragePolicy; |
20 using storage::StorageType; | 23 using storage::StorageType; |
21 using storage::UsageTracker; | 24 using storage::UsageTracker; |
22 | 25 |
23 namespace content { | 26 namespace content { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 void DidGetGlobalUsage(bool* done, | 30 void DidGetGlobalUsage(bool* done, |
28 int64* usage_out, | 31 int64_t* usage_out, |
29 int64* unlimited_usage_out, | 32 int64_t* unlimited_usage_out, |
30 int64 usage, | 33 int64_t usage, |
31 int64 unlimited_usage) { | 34 int64_t unlimited_usage) { |
32 EXPECT_FALSE(*done); | 35 EXPECT_FALSE(*done); |
33 *done = true; | 36 *done = true; |
34 *usage_out = usage; | 37 *usage_out = usage; |
35 *unlimited_usage_out = unlimited_usage; | 38 *unlimited_usage_out = unlimited_usage; |
36 } | 39 } |
37 | 40 |
38 void DidGetUsage(bool* done, | 41 void DidGetUsage(bool* done, int64_t* usage_out, int64_t usage) { |
39 int64* usage_out, | |
40 int64 usage) { | |
41 EXPECT_FALSE(*done); | 42 EXPECT_FALSE(*done); |
42 *done = true; | 43 *done = true; |
43 *usage_out = usage; | 44 *usage_out = usage; |
44 } | 45 } |
45 | 46 |
46 } // namespace | 47 } // namespace |
47 | 48 |
48 class MockQuotaClient : public QuotaClient { | 49 class MockQuotaClient : public QuotaClient { |
49 public: | 50 public: |
50 MockQuotaClient() {} | 51 MockQuotaClient() {} |
51 ~MockQuotaClient() override {} | 52 ~MockQuotaClient() override {} |
52 | 53 |
53 ID id() const override { return kFileSystem; } | 54 ID id() const override { return kFileSystem; } |
54 | 55 |
55 void OnQuotaManagerDestroyed() override {} | 56 void OnQuotaManagerDestroyed() override {} |
56 | 57 |
57 void GetOriginUsage(const GURL& origin, | 58 void GetOriginUsage(const GURL& origin, |
58 StorageType type, | 59 StorageType type, |
59 const GetUsageCallback& callback) override { | 60 const GetUsageCallback& callback) override { |
60 EXPECT_EQ(kStorageTypeTemporary, type); | 61 EXPECT_EQ(kStorageTypeTemporary, type); |
61 int64 usage = GetUsage(origin); | 62 int64_t usage = GetUsage(origin); |
62 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 63 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
63 base::Bind(callback, usage)); | 64 base::Bind(callback, usage)); |
64 } | 65 } |
65 | 66 |
66 void GetOriginsForType(StorageType type, | 67 void GetOriginsForType(StorageType type, |
67 const GetOriginsCallback& callback) override { | 68 const GetOriginsCallback& callback) override { |
68 EXPECT_EQ(kStorageTypeTemporary, type); | 69 EXPECT_EQ(kStorageTypeTemporary, type); |
69 std::set<GURL> origins; | 70 std::set<GURL> origins; |
70 for (UsageMap::const_iterator itr = usage_map_.begin(); | 71 for (UsageMap::const_iterator itr = usage_map_.begin(); |
71 itr != usage_map_.end(); ++itr) { | 72 itr != usage_map_.end(); ++itr) { |
(...skipping 23 matching lines...) Expand all Loading... |
95 EXPECT_EQ(kStorageTypeTemporary, type); | 96 EXPECT_EQ(kStorageTypeTemporary, type); |
96 usage_map_.erase(origin); | 97 usage_map_.erase(origin); |
97 base::ThreadTaskRunnerHandle::Get()->PostTask( | 98 base::ThreadTaskRunnerHandle::Get()->PostTask( |
98 FROM_HERE, base::Bind(callback, kQuotaStatusOk)); | 99 FROM_HERE, base::Bind(callback, kQuotaStatusOk)); |
99 } | 100 } |
100 | 101 |
101 bool DoesSupport(storage::StorageType type) const override { | 102 bool DoesSupport(storage::StorageType type) const override { |
102 return type == storage::kStorageTypeTemporary; | 103 return type == storage::kStorageTypeTemporary; |
103 } | 104 } |
104 | 105 |
105 int64 GetUsage(const GURL& origin) { | 106 int64_t GetUsage(const GURL& origin) { |
106 UsageMap::const_iterator found = usage_map_.find(origin); | 107 UsageMap::const_iterator found = usage_map_.find(origin); |
107 if (found == usage_map_.end()) | 108 if (found == usage_map_.end()) |
108 return 0; | 109 return 0; |
109 return found->second; | 110 return found->second; |
110 } | 111 } |
111 | 112 |
112 void SetUsage(const GURL& origin, int64 usage) { | 113 void SetUsage(const GURL& origin, int64_t usage) { |
113 usage_map_[origin] = usage; | 114 usage_map_[origin] = usage; |
114 } | 115 } |
115 | 116 |
116 int64 UpdateUsage(const GURL& origin, int64 delta) { | 117 int64_t UpdateUsage(const GURL& origin, int64_t delta) { |
117 return usage_map_[origin] += delta; | 118 return usage_map_[origin] += delta; |
118 } | 119 } |
119 | 120 |
120 private: | 121 private: |
121 typedef std::map<GURL, int64> UsageMap; | 122 typedef std::map<GURL, int64_t> UsageMap; |
122 | 123 |
123 UsageMap usage_map_; | 124 UsageMap usage_map_; |
124 | 125 |
125 DISALLOW_COPY_AND_ASSIGN(MockQuotaClient); | 126 DISALLOW_COPY_AND_ASSIGN(MockQuotaClient); |
126 }; | 127 }; |
127 | 128 |
128 class UsageTrackerTest : public testing::Test { | 129 class UsageTrackerTest : public testing::Test { |
129 public: | 130 public: |
130 UsageTrackerTest() | 131 UsageTrackerTest() |
131 : storage_policy_(new MockSpecialStoragePolicy()), | 132 : storage_policy_(new MockSpecialStoragePolicy()), |
132 usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary, | 133 usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary, |
133 storage_policy_.get(), NULL) { | 134 storage_policy_.get(), NULL) { |
134 } | 135 } |
135 | 136 |
136 ~UsageTrackerTest() override {} | 137 ~UsageTrackerTest() override {} |
137 | 138 |
138 UsageTracker* usage_tracker() { | 139 UsageTracker* usage_tracker() { |
139 return &usage_tracker_; | 140 return &usage_tracker_; |
140 } | 141 } |
141 | 142 |
142 void UpdateUsage(const GURL& origin, int64 delta) { | 143 void UpdateUsage(const GURL& origin, int64_t delta) { |
143 quota_client_.UpdateUsage(origin, delta); | 144 quota_client_.UpdateUsage(origin, delta); |
144 usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta); | 145 usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta); |
145 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
146 } | 147 } |
147 | 148 |
148 void UpdateUsageWithoutNotification(const GURL& origin, int64 delta) { | 149 void UpdateUsageWithoutNotification(const GURL& origin, int64_t delta) { |
149 quota_client_.UpdateUsage(origin, delta); | 150 quota_client_.UpdateUsage(origin, delta); |
150 } | 151 } |
151 | 152 |
152 void GetGlobalLimitedUsage(int64* limited_usage) { | 153 void GetGlobalLimitedUsage(int64_t* limited_usage) { |
153 bool done = false; | 154 bool done = false; |
154 usage_tracker_.GetGlobalLimitedUsage(base::Bind( | 155 usage_tracker_.GetGlobalLimitedUsage(base::Bind( |
155 &DidGetUsage, &done, limited_usage)); | 156 &DidGetUsage, &done, limited_usage)); |
156 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
157 | 158 |
158 EXPECT_TRUE(done); | 159 EXPECT_TRUE(done); |
159 } | 160 } |
160 | 161 |
161 void GetGlobalUsage(int64* usage, int64* unlimited_usage) { | 162 void GetGlobalUsage(int64_t* usage, int64_t* unlimited_usage) { |
162 bool done = false; | 163 bool done = false; |
163 usage_tracker_.GetGlobalUsage(base::Bind( | 164 usage_tracker_.GetGlobalUsage(base::Bind( |
164 &DidGetGlobalUsage, | 165 &DidGetGlobalUsage, |
165 &done, usage, unlimited_usage)); | 166 &done, usage, unlimited_usage)); |
166 base::RunLoop().RunUntilIdle(); | 167 base::RunLoop().RunUntilIdle(); |
167 | 168 |
168 EXPECT_TRUE(done); | 169 EXPECT_TRUE(done); |
169 } | 170 } |
170 | 171 |
171 void GetHostUsage(const std::string& host, int64* usage) { | 172 void GetHostUsage(const std::string& host, int64_t* usage) { |
172 bool done = false; | 173 bool done = false; |
173 usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage)); | 174 usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage)); |
174 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
175 | 176 |
176 EXPECT_TRUE(done); | 177 EXPECT_TRUE(done); |
177 } | 178 } |
178 | 179 |
179 void GrantUnlimitedStoragePolicy(const GURL& origin) { | 180 void GrantUnlimitedStoragePolicy(const GURL& origin) { |
180 if (!storage_policy_->IsStorageUnlimited(origin)) { | 181 if (!storage_policy_->IsStorageUnlimited(origin)) { |
181 storage_policy_->AddUnlimited(origin); | 182 storage_policy_->AddUnlimited(origin); |
(...skipping 25 matching lines...) Expand all Loading... |
207 base::MessageLoop message_loop_; | 208 base::MessageLoop message_loop_; |
208 | 209 |
209 scoped_refptr<MockSpecialStoragePolicy> storage_policy_; | 210 scoped_refptr<MockSpecialStoragePolicy> storage_policy_; |
210 MockQuotaClient quota_client_; | 211 MockQuotaClient quota_client_; |
211 UsageTracker usage_tracker_; | 212 UsageTracker usage_tracker_; |
212 | 213 |
213 DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest); | 214 DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest); |
214 }; | 215 }; |
215 | 216 |
216 TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) { | 217 TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) { |
217 int64 usage = 0; | 218 int64_t usage = 0; |
218 int64 unlimited_usage = 0; | 219 int64_t unlimited_usage = 0; |
219 int64 host_usage = 0; | 220 int64_t host_usage = 0; |
220 GetGlobalUsage(&usage, &unlimited_usage); | 221 GetGlobalUsage(&usage, &unlimited_usage); |
221 EXPECT_EQ(0, usage); | 222 EXPECT_EQ(0, usage); |
222 EXPECT_EQ(0, unlimited_usage); | 223 EXPECT_EQ(0, unlimited_usage); |
223 | 224 |
224 const GURL origin("http://example.com"); | 225 const GURL origin("http://example.com"); |
225 const std::string host(net::GetHostOrSpecFromURL(origin)); | 226 const std::string host(net::GetHostOrSpecFromURL(origin)); |
226 | 227 |
227 UpdateUsage(origin, 100); | 228 UpdateUsage(origin, 100); |
228 GetGlobalUsage(&usage, &unlimited_usage); | 229 GetGlobalUsage(&usage, &unlimited_usage); |
229 GetHostUsage(host, &host_usage); | 230 GetHostUsage(host, &host_usage); |
(...skipping 10 matching lines...) Expand all Loading... |
240 | 241 |
241 RevokeUnlimitedStoragePolicy(origin); | 242 RevokeUnlimitedStoragePolicy(origin); |
242 GetGlobalUsage(&usage, &unlimited_usage); | 243 GetGlobalUsage(&usage, &unlimited_usage); |
243 GetHostUsage(host, &host_usage); | 244 GetHostUsage(host, &host_usage); |
244 EXPECT_EQ(100, usage); | 245 EXPECT_EQ(100, usage); |
245 EXPECT_EQ(0, unlimited_usage); | 246 EXPECT_EQ(0, unlimited_usage); |
246 EXPECT_EQ(100, host_usage); | 247 EXPECT_EQ(100, host_usage); |
247 } | 248 } |
248 | 249 |
249 TEST_F(UsageTrackerTest, CacheDisabledClientTest) { | 250 TEST_F(UsageTrackerTest, CacheDisabledClientTest) { |
250 int64 usage = 0; | 251 int64_t usage = 0; |
251 int64 unlimited_usage = 0; | 252 int64_t unlimited_usage = 0; |
252 int64 host_usage = 0; | 253 int64_t host_usage = 0; |
253 | 254 |
254 const GURL origin("http://example.com"); | 255 const GURL origin("http://example.com"); |
255 const std::string host(net::GetHostOrSpecFromURL(origin)); | 256 const std::string host(net::GetHostOrSpecFromURL(origin)); |
256 | 257 |
257 UpdateUsage(origin, 100); | 258 UpdateUsage(origin, 100); |
258 GetGlobalUsage(&usage, &unlimited_usage); | 259 GetGlobalUsage(&usage, &unlimited_usage); |
259 GetHostUsage(host, &host_usage); | 260 GetHostUsage(host, &host_usage); |
260 EXPECT_EQ(100, usage); | 261 EXPECT_EQ(100, usage); |
261 EXPECT_EQ(0, unlimited_usage); | 262 EXPECT_EQ(0, unlimited_usage); |
262 EXPECT_EQ(100, host_usage); | 263 EXPECT_EQ(100, host_usage); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 GrantUnlimitedStoragePolicy(kNonCachedUnlimited); | 307 GrantUnlimitedStoragePolicy(kNonCachedUnlimited); |
307 | 308 |
308 SetUsageCacheEnabled(kNonCached, false); | 309 SetUsageCacheEnabled(kNonCached, false); |
309 SetUsageCacheEnabled(kNonCachedUnlimited, false); | 310 SetUsageCacheEnabled(kNonCachedUnlimited, false); |
310 | 311 |
311 UpdateUsageWithoutNotification(kNormal, 1); | 312 UpdateUsageWithoutNotification(kNormal, 1); |
312 UpdateUsageWithoutNotification(kUnlimited, 2); | 313 UpdateUsageWithoutNotification(kUnlimited, 2); |
313 UpdateUsageWithoutNotification(kNonCached, 4); | 314 UpdateUsageWithoutNotification(kNonCached, 4); |
314 UpdateUsageWithoutNotification(kNonCachedUnlimited, 8); | 315 UpdateUsageWithoutNotification(kNonCachedUnlimited, 8); |
315 | 316 |
316 int64 limited_usage = 0; | 317 int64_t limited_usage = 0; |
317 int64 total_usage = 0; | 318 int64_t total_usage = 0; |
318 int64 unlimited_usage = 0; | 319 int64_t unlimited_usage = 0; |
319 | 320 |
320 GetGlobalLimitedUsage(&limited_usage); | 321 GetGlobalLimitedUsage(&limited_usage); |
321 GetGlobalUsage(&total_usage, &unlimited_usage); | 322 GetGlobalUsage(&total_usage, &unlimited_usage); |
322 EXPECT_EQ(1 + 4, limited_usage); | 323 EXPECT_EQ(1 + 4, limited_usage); |
323 EXPECT_EQ(1 + 2 + 4 + 8, total_usage); | 324 EXPECT_EQ(1 + 2 + 4 + 8, total_usage); |
324 EXPECT_EQ(2 + 8, unlimited_usage); | 325 EXPECT_EQ(2 + 8, unlimited_usage); |
325 | 326 |
326 UpdateUsageWithoutNotification(kNonCached, 16 - 4); | 327 UpdateUsageWithoutNotification(kNonCached, 16 - 4); |
327 UpdateUsageWithoutNotification(kNonCachedUnlimited, 32 - 8); | 328 UpdateUsageWithoutNotification(kNonCachedUnlimited, 32 - 8); |
328 | 329 |
329 GetGlobalLimitedUsage(&limited_usage); | 330 GetGlobalLimitedUsage(&limited_usage); |
330 GetGlobalUsage(&total_usage, &unlimited_usage); | 331 GetGlobalUsage(&total_usage, &unlimited_usage); |
331 EXPECT_EQ(1 + 16, limited_usage); | 332 EXPECT_EQ(1 + 16, limited_usage); |
332 EXPECT_EQ(1 + 2 + 16 + 32, total_usage); | 333 EXPECT_EQ(1 + 2 + 16 + 32, total_usage); |
333 EXPECT_EQ(2 + 32, unlimited_usage); | 334 EXPECT_EQ(2 + 32, unlimited_usage); |
334 } | 335 } |
335 | 336 |
336 | 337 |
337 } // namespace content | 338 } // namespace content |
OLD | NEW |