OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "base/bind.h" | |
6 #include "base/message_loop.h" | |
7 #include "net/base/net_util.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 #include "webkit/quota/mock_special_storage_policy.h" | |
10 #include "webkit/quota/usage_tracker.h" | |
11 | |
12 namespace quota { | |
13 | |
14 namespace { | |
15 | |
16 void DidGetGlobalUsage(bool* done, | |
17 int64* usage_out, | |
18 int64* unlimited_usage_out, | |
19 int64 usage, | |
20 int64 unlimited_usage) { | |
21 EXPECT_FALSE(*done); | |
22 *done = true; | |
23 *usage_out = usage; | |
24 *unlimited_usage_out = unlimited_usage; | |
25 } | |
26 | |
27 void DidGetUsage(bool* done, | |
28 int64* usage_out, | |
29 int64 usage) { | |
30 EXPECT_FALSE(*done); | |
31 *done = true; | |
32 *usage_out = usage; | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 class MockQuotaClient : public QuotaClient { | |
38 public: | |
39 MockQuotaClient() {} | |
40 virtual ~MockQuotaClient() {} | |
41 | |
42 virtual ID id() const OVERRIDE { | |
43 return kFileSystem; | |
44 } | |
45 | |
46 virtual void OnQuotaManagerDestroyed() OVERRIDE {} | |
47 | |
48 virtual void GetOriginUsage(const GURL& origin, | |
49 StorageType type, | |
50 const GetUsageCallback& callback) OVERRIDE { | |
51 EXPECT_EQ(kStorageTypeTemporary, type); | |
52 int64 usage = GetUsage(origin); | |
53 base::MessageLoop::current()->PostTask(FROM_HERE, | |
54 base::Bind(callback, usage)); | |
55 } | |
56 | |
57 virtual void GetOriginsForType(StorageType type, | |
58 const GetOriginsCallback& callback) OVERRIDE { | |
59 EXPECT_EQ(kStorageTypeTemporary, type); | |
60 std::set<GURL> origins; | |
61 for (UsageMap::const_iterator itr = usage_map_.begin(); | |
62 itr != usage_map_.end(); ++itr) { | |
63 origins.insert(itr->first); | |
64 } | |
65 base::MessageLoop::current()->PostTask(FROM_HERE, | |
66 base::Bind(callback, origins)); | |
67 } | |
68 | |
69 virtual void GetOriginsForHost(StorageType type, | |
70 const std::string& host, | |
71 const GetOriginsCallback& callback) OVERRIDE { | |
72 EXPECT_EQ(kStorageTypeTemporary, type); | |
73 std::set<GURL> origins; | |
74 for (UsageMap::const_iterator itr = usage_map_.begin(); | |
75 itr != usage_map_.end(); ++itr) { | |
76 if (net::GetHostOrSpecFromURL(itr->first) == host) | |
77 origins.insert(itr->first); | |
78 } | |
79 base::MessageLoop::current()->PostTask(FROM_HERE, | |
80 base::Bind(callback, origins)); | |
81 } | |
82 | |
83 virtual void DeleteOriginData(const GURL& origin, | |
84 StorageType type, | |
85 const DeletionCallback& callback) OVERRIDE { | |
86 EXPECT_EQ(kStorageTypeTemporary, type); | |
87 usage_map_.erase(origin); | |
88 base::MessageLoop::current()->PostTask( | |
89 FROM_HERE, base::Bind(callback, kQuotaStatusOk)); | |
90 } | |
91 | |
92 int64 GetUsage(const GURL& origin) { | |
93 UsageMap::const_iterator found = usage_map_.find(origin); | |
94 if (found == usage_map_.end()) | |
95 return 0; | |
96 return found->second; | |
97 } | |
98 | |
99 void SetUsage(const GURL& origin, int64 usage) { | |
100 usage_map_[origin] = usage; | |
101 } | |
102 | |
103 int64 UpdateUsage(const GURL& origin, int64 delta) { | |
104 return usage_map_[origin] += delta; | |
105 } | |
106 | |
107 private: | |
108 typedef std::map<GURL, int64> UsageMap; | |
109 | |
110 UsageMap usage_map_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(MockQuotaClient); | |
113 }; | |
114 | |
115 class UsageTrackerTest : public testing::Test { | |
116 public: | |
117 UsageTrackerTest() | |
118 : storage_policy_(new MockSpecialStoragePolicy()), | |
119 usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary, | |
120 storage_policy_.get()) { | |
121 } | |
122 | |
123 virtual ~UsageTrackerTest() {} | |
124 | |
125 UsageTracker* usage_tracker() { | |
126 return &usage_tracker_; | |
127 } | |
128 | |
129 void UpdateUsage(const GURL& origin, int64 delta) { | |
130 quota_client_.UpdateUsage(origin, delta); | |
131 usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta); | |
132 message_loop_.RunUntilIdle(); | |
133 } | |
134 | |
135 void UpdateUsageWithoutNotification(const GURL& origin, int64 delta) { | |
136 quota_client_.UpdateUsage(origin, delta); | |
137 } | |
138 | |
139 void GetGlobalUsage(int64* usage, int64* unlimited_usage) { | |
140 bool done = false; | |
141 usage_tracker_.GetGlobalUsage(base::Bind( | |
142 &DidGetGlobalUsage, | |
143 &done, usage, unlimited_usage)); | |
144 message_loop_.RunUntilIdle(); | |
145 | |
146 EXPECT_TRUE(done); | |
147 } | |
148 | |
149 void GetHostUsage(const std::string& host, int64* usage) { | |
150 bool done = false; | |
151 usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage)); | |
152 message_loop_.RunUntilIdle(); | |
153 | |
154 EXPECT_TRUE(done); | |
155 } | |
156 | |
157 void GrantUnlimitedStoragePolicy(const GURL& origin) { | |
158 if (!storage_policy_->IsStorageUnlimited(origin)) { | |
159 storage_policy_->AddUnlimited(origin); | |
160 storage_policy_->NotifyGranted( | |
161 origin, SpecialStoragePolicy::STORAGE_UNLIMITED); | |
162 } | |
163 } | |
164 | |
165 void RevokeUnlimitedStoragePolicy(const GURL& origin) { | |
166 if (storage_policy_->IsStorageUnlimited(origin)) { | |
167 storage_policy_->RemoveUnlimited(origin); | |
168 storage_policy_->NotifyRevoked( | |
169 origin, SpecialStoragePolicy::STORAGE_UNLIMITED); | |
170 } | |
171 } | |
172 | |
173 void SetUsageCacheEnabled(const GURL& origin, bool enabled) { | |
174 usage_tracker_.SetUsageCacheEnabled( | |
175 quota_client_.id(), origin, enabled); | |
176 } | |
177 | |
178 private: | |
179 QuotaClientList GetUsageTrackerList() { | |
180 QuotaClientList client_list; | |
181 client_list.push_back("a_client_); | |
182 return client_list; | |
183 } | |
184 | |
185 base::MessageLoop message_loop_; | |
186 | |
187 scoped_refptr<MockSpecialStoragePolicy> storage_policy_; | |
188 MockQuotaClient quota_client_; | |
189 UsageTracker usage_tracker_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest); | |
192 }; | |
193 | |
194 TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) { | |
195 int64 usage = 0; | |
196 int64 unlimited_usage = 0; | |
197 int64 host_usage = 0; | |
198 GetGlobalUsage(&usage, &unlimited_usage); | |
199 EXPECT_EQ(0, usage); | |
200 EXPECT_EQ(0, unlimited_usage); | |
201 | |
202 const GURL origin("http://example.com"); | |
203 const std::string host(net::GetHostOrSpecFromURL(origin)); | |
204 | |
205 UpdateUsage(origin, 100); | |
206 GetGlobalUsage(&usage, &unlimited_usage); | |
207 GetHostUsage(host, &host_usage); | |
208 EXPECT_EQ(100, usage); | |
209 EXPECT_EQ(0, unlimited_usage); | |
210 EXPECT_EQ(100, host_usage); | |
211 | |
212 GrantUnlimitedStoragePolicy(origin); | |
213 GetGlobalUsage(&usage, &unlimited_usage); | |
214 GetHostUsage(host, &host_usage); | |
215 EXPECT_EQ(100, usage); | |
216 EXPECT_EQ(100, unlimited_usage); | |
217 EXPECT_EQ(100, host_usage); | |
218 | |
219 RevokeUnlimitedStoragePolicy(origin); | |
220 GetGlobalUsage(&usage, &unlimited_usage); | |
221 GetHostUsage(host, &host_usage); | |
222 EXPECT_EQ(100, usage); | |
223 EXPECT_EQ(0, unlimited_usage); | |
224 EXPECT_EQ(100, host_usage); | |
225 } | |
226 | |
227 TEST_F(UsageTrackerTest, CacheDisabledClientTest) { | |
228 int64 usage = 0; | |
229 int64 unlimited_usage = 0; | |
230 int64 host_usage = 0; | |
231 | |
232 const GURL origin("http://example.com"); | |
233 const std::string host(net::GetHostOrSpecFromURL(origin)); | |
234 | |
235 UpdateUsage(origin, 100); | |
236 GetGlobalUsage(&usage, &unlimited_usage); | |
237 GetHostUsage(host, &host_usage); | |
238 EXPECT_EQ(100, usage); | |
239 EXPECT_EQ(0, unlimited_usage); | |
240 EXPECT_EQ(100, host_usage); | |
241 | |
242 UpdateUsageWithoutNotification(origin, 100); | |
243 GetGlobalUsage(&usage, &unlimited_usage); | |
244 GetHostUsage(host, &host_usage); | |
245 EXPECT_EQ(100, usage); | |
246 EXPECT_EQ(0, unlimited_usage); | |
247 EXPECT_EQ(100, host_usage); | |
248 | |
249 GrantUnlimitedStoragePolicy(origin); | |
250 UpdateUsageWithoutNotification(origin, 100); | |
251 SetUsageCacheEnabled(origin, false); | |
252 UpdateUsageWithoutNotification(origin, 100); | |
253 | |
254 GetGlobalUsage(&usage, &unlimited_usage); | |
255 GetHostUsage(host, &host_usage); | |
256 EXPECT_EQ(400, usage); | |
257 EXPECT_EQ(400, unlimited_usage); | |
258 EXPECT_EQ(400, host_usage); | |
259 | |
260 RevokeUnlimitedStoragePolicy(origin); | |
261 GetGlobalUsage(&usage, &unlimited_usage); | |
262 GetHostUsage(host, &host_usage); | |
263 EXPECT_EQ(400, usage); | |
264 EXPECT_EQ(400, unlimited_usage); | |
265 EXPECT_EQ(400, host_usage); | |
266 | |
267 SetUsageCacheEnabled(origin, true); | |
268 UpdateUsage(origin, 100); | |
269 | |
270 GetGlobalUsage(&usage, &unlimited_usage); | |
271 GetHostUsage(host, &host_usage); | |
272 EXPECT_EQ(500, usage); | |
273 EXPECT_EQ(0, unlimited_usage); | |
274 EXPECT_EQ(500, host_usage); | |
275 } | |
276 | |
277 } // namespace quota | |
OLD | NEW |