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 <map> | 5 #include <map> |
6 #include <set> | 6 #include <set> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "content/browser/appcache/appcache_quota_client.h" | 11 #include "content/browser/appcache/appcache_quota_client.h" |
12 #include "content/browser/appcache/mock_appcache_service.h" | 12 #include "content/browser/appcache/mock_appcache_service.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 // Declared to shorten the line lengths. | 18 // Declared to shorten the line lengths. |
19 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; | 19 static const storage::StorageType kTemp = storage::kStorageTypeTemporary; |
20 static const quota::StorageType kPerm = quota::kStorageTypePersistent; | 20 static const storage::StorageType kPerm = storage::kStorageTypePersistent; |
21 | 21 |
22 // Base class for our test fixtures. | 22 // Base class for our test fixtures. |
23 class AppCacheQuotaClientTest : public testing::Test { | 23 class AppCacheQuotaClientTest : public testing::Test { |
24 public: | 24 public: |
25 const GURL kOriginA; | 25 const GURL kOriginA; |
26 const GURL kOriginB; | 26 const GURL kOriginB; |
27 const GURL kOriginOther; | 27 const GURL kOriginOther; |
28 | 28 |
29 AppCacheQuotaClientTest() | 29 AppCacheQuotaClientTest() |
30 : kOriginA("http://host"), | 30 : kOriginA("http://host"), |
31 kOriginB("http://host:8000"), | 31 kOriginB("http://host:8000"), |
32 kOriginOther("http://other"), | 32 kOriginOther("http://other"), |
33 usage_(0), | 33 usage_(0), |
34 delete_status_(quota::kQuotaStatusUnknown), | 34 delete_status_(storage::kQuotaStatusUnknown), |
35 num_get_origin_usage_completions_(0), | 35 num_get_origin_usage_completions_(0), |
36 num_get_origins_completions_(0), | 36 num_get_origins_completions_(0), |
37 num_delete_origins_completions_(0), | 37 num_delete_origins_completions_(0), |
38 weak_factory_(this) { | 38 weak_factory_(this) {} |
39 } | |
40 | 39 |
41 int64 GetOriginUsage( | 40 int64 GetOriginUsage(storage::QuotaClient* client, |
42 quota::QuotaClient* client, | 41 const GURL& origin, |
43 const GURL& origin, | 42 storage::StorageType type) { |
44 quota::StorageType type) { | |
45 usage_ = -1; | 43 usage_ = -1; |
46 AsyncGetOriginUsage(client, origin, type); | 44 AsyncGetOriginUsage(client, origin, type); |
47 base::RunLoop().RunUntilIdle(); | 45 base::RunLoop().RunUntilIdle(); |
48 return usage_; | 46 return usage_; |
49 } | 47 } |
50 | 48 |
51 const std::set<GURL>& GetOriginsForType( | 49 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client, |
52 quota::QuotaClient* client, | 50 storage::StorageType type) { |
53 quota::StorageType type) { | |
54 origins_.clear(); | 51 origins_.clear(); |
55 AsyncGetOriginsForType(client, type); | 52 AsyncGetOriginsForType(client, type); |
56 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
57 return origins_; | 54 return origins_; |
58 } | 55 } |
59 | 56 |
60 const std::set<GURL>& GetOriginsForHost( | 57 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client, |
61 quota::QuotaClient* client, | 58 storage::StorageType type, |
62 quota::StorageType type, | 59 const std::string& host) { |
63 const std::string& host) { | |
64 origins_.clear(); | 60 origins_.clear(); |
65 AsyncGetOriginsForHost(client, type, host); | 61 AsyncGetOriginsForHost(client, type, host); |
66 base::RunLoop().RunUntilIdle(); | 62 base::RunLoop().RunUntilIdle(); |
67 return origins_; | 63 return origins_; |
68 } | 64 } |
69 | 65 |
70 quota::QuotaStatusCode DeleteOriginData( | 66 storage::QuotaStatusCode DeleteOriginData(storage::QuotaClient* client, |
71 quota::QuotaClient* client, | 67 storage::StorageType type, |
72 quota::StorageType type, | 68 const GURL& origin) { |
73 const GURL& origin) { | 69 delete_status_ = storage::kQuotaStatusUnknown; |
74 delete_status_ = quota::kQuotaStatusUnknown; | |
75 AsyncDeleteOriginData(client, type, origin); | 70 AsyncDeleteOriginData(client, type, origin); |
76 base::RunLoop().RunUntilIdle(); | 71 base::RunLoop().RunUntilIdle(); |
77 return delete_status_; | 72 return delete_status_; |
78 } | 73 } |
79 | 74 |
80 void AsyncGetOriginUsage( | 75 void AsyncGetOriginUsage(storage::QuotaClient* client, |
81 quota::QuotaClient* client, | 76 const GURL& origin, |
82 const GURL& origin, | 77 storage::StorageType type) { |
83 quota::StorageType type) { | |
84 client->GetOriginUsage( | 78 client->GetOriginUsage( |
85 origin, type, | 79 origin, type, |
86 base::Bind(&AppCacheQuotaClientTest::OnGetOriginUsageComplete, | 80 base::Bind(&AppCacheQuotaClientTest::OnGetOriginUsageComplete, |
87 weak_factory_.GetWeakPtr())); | 81 weak_factory_.GetWeakPtr())); |
88 } | 82 } |
89 | 83 |
90 void AsyncGetOriginsForType( | 84 void AsyncGetOriginsForType(storage::QuotaClient* client, |
91 quota::QuotaClient* client, | 85 storage::StorageType type) { |
92 quota::StorageType type) { | |
93 client->GetOriginsForType( | 86 client->GetOriginsForType( |
94 type, | 87 type, |
95 base::Bind(&AppCacheQuotaClientTest::OnGetOriginsComplete, | 88 base::Bind(&AppCacheQuotaClientTest::OnGetOriginsComplete, |
96 weak_factory_.GetWeakPtr())); | 89 weak_factory_.GetWeakPtr())); |
97 } | 90 } |
98 | 91 |
99 void AsyncGetOriginsForHost( | 92 void AsyncGetOriginsForHost(storage::QuotaClient* client, |
100 quota::QuotaClient* client, | 93 storage::StorageType type, |
101 quota::StorageType type, | 94 const std::string& host) { |
102 const std::string& host) { | |
103 client->GetOriginsForHost( | 95 client->GetOriginsForHost( |
104 type, host, | 96 type, host, |
105 base::Bind(&AppCacheQuotaClientTest::OnGetOriginsComplete, | 97 base::Bind(&AppCacheQuotaClientTest::OnGetOriginsComplete, |
106 weak_factory_.GetWeakPtr())); | 98 weak_factory_.GetWeakPtr())); |
107 } | 99 } |
108 | 100 |
109 void AsyncDeleteOriginData( | 101 void AsyncDeleteOriginData(storage::QuotaClient* client, |
110 quota::QuotaClient* client, | 102 storage::StorageType type, |
111 quota::StorageType type, | 103 const GURL& origin) { |
112 const GURL& origin) { | |
113 client->DeleteOriginData( | 104 client->DeleteOriginData( |
114 origin, type, | 105 origin, type, |
115 base::Bind(&AppCacheQuotaClientTest::OnDeleteOriginDataComplete, | 106 base::Bind(&AppCacheQuotaClientTest::OnDeleteOriginDataComplete, |
116 weak_factory_.GetWeakPtr())); | 107 weak_factory_.GetWeakPtr())); |
117 } | 108 } |
118 | 109 |
119 void SetUsageMapEntry(const GURL& origin, int64 usage) { | 110 void SetUsageMapEntry(const GURL& origin, int64 usage) { |
120 mock_service_.storage()->usage_map_[origin] = usage; | 111 mock_service_.storage()->usage_map_[origin] = usage; |
121 } | 112 } |
122 | 113 |
(...skipping 17 matching lines...) Expand all Loading... |
140 void OnGetOriginUsageComplete(int64 usage) { | 131 void OnGetOriginUsageComplete(int64 usage) { |
141 ++num_get_origin_usage_completions_; | 132 ++num_get_origin_usage_completions_; |
142 usage_ = usage; | 133 usage_ = usage; |
143 } | 134 } |
144 | 135 |
145 void OnGetOriginsComplete(const std::set<GURL>& origins) { | 136 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
146 ++num_get_origins_completions_; | 137 ++num_get_origins_completions_; |
147 origins_ = origins; | 138 origins_ = origins; |
148 } | 139 } |
149 | 140 |
150 void OnDeleteOriginDataComplete(quota::QuotaStatusCode status) { | 141 void OnDeleteOriginDataComplete(storage::QuotaStatusCode status) { |
151 ++num_delete_origins_completions_; | 142 ++num_delete_origins_completions_; |
152 delete_status_ = status; | 143 delete_status_ = status; |
153 } | 144 } |
154 | 145 |
155 base::MessageLoop message_loop_; | 146 base::MessageLoop message_loop_; |
156 int64 usage_; | 147 int64 usage_; |
157 std::set<GURL> origins_; | 148 std::set<GURL> origins_; |
158 quota::QuotaStatusCode delete_status_; | 149 storage::QuotaStatusCode delete_status_; |
159 int num_get_origin_usage_completions_; | 150 int num_get_origin_usage_completions_; |
160 int num_get_origins_completions_; | 151 int num_get_origins_completions_; |
161 int num_delete_origins_completions_; | 152 int num_delete_origins_completions_; |
162 MockAppCacheService mock_service_; | 153 MockAppCacheService mock_service_; |
163 base::WeakPtrFactory<AppCacheQuotaClientTest> weak_factory_; | 154 base::WeakPtrFactory<AppCacheQuotaClientTest> weak_factory_; |
164 }; | 155 }; |
165 | 156 |
166 | 157 |
167 TEST_F(AppCacheQuotaClientTest, BasicCreateDestroy) { | 158 TEST_F(AppCacheQuotaClientTest, BasicCreateDestroy) { |
168 AppCacheQuotaClient* client = CreateClient(); | 159 AppCacheQuotaClient* client = CreateClient(); |
169 Call_NotifyAppCacheReady(client); | 160 Call_NotifyAppCacheReady(client); |
170 Call_OnQuotaManagerDestroyed(client); | 161 Call_OnQuotaManagerDestroyed(client); |
171 Call_NotifyAppCacheDestroyed(client); | 162 Call_NotifyAppCacheDestroyed(client); |
172 } | 163 } |
173 | 164 |
174 TEST_F(AppCacheQuotaClientTest, EmptyService) { | 165 TEST_F(AppCacheQuotaClientTest, EmptyService) { |
175 AppCacheQuotaClient* client = CreateClient(); | 166 AppCacheQuotaClient* client = CreateClient(); |
176 Call_NotifyAppCacheReady(client); | 167 Call_NotifyAppCacheReady(client); |
177 | 168 |
178 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kTemp)); | 169 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kTemp)); |
179 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kPerm)); | 170 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kPerm)); |
180 EXPECT_TRUE(GetOriginsForType(client, kTemp).empty()); | 171 EXPECT_TRUE(GetOriginsForType(client, kTemp).empty()); |
181 EXPECT_TRUE(GetOriginsForType(client, kPerm).empty()); | 172 EXPECT_TRUE(GetOriginsForType(client, kPerm).empty()); |
182 EXPECT_TRUE(GetOriginsForHost(client, kTemp, kOriginA.host()).empty()); | 173 EXPECT_TRUE(GetOriginsForHost(client, kTemp, kOriginA.host()).empty()); |
183 EXPECT_TRUE(GetOriginsForHost(client, kPerm, kOriginA.host()).empty()); | 174 EXPECT_TRUE(GetOriginsForHost(client, kPerm, kOriginA.host()).empty()); |
184 EXPECT_EQ(quota::kQuotaStatusOk, DeleteOriginData(client, kTemp, kOriginA)); | 175 EXPECT_EQ(storage::kQuotaStatusOk, DeleteOriginData(client, kTemp, kOriginA)); |
185 EXPECT_EQ(quota::kQuotaStatusOk, DeleteOriginData(client, kPerm, kOriginA)); | 176 EXPECT_EQ(storage::kQuotaStatusOk, DeleteOriginData(client, kPerm, kOriginA)); |
186 | 177 |
187 Call_NotifyAppCacheDestroyed(client); | 178 Call_NotifyAppCacheDestroyed(client); |
188 Call_OnQuotaManagerDestroyed(client); | 179 Call_OnQuotaManagerDestroyed(client); |
189 } | 180 } |
190 | 181 |
191 TEST_F(AppCacheQuotaClientTest, NoService) { | 182 TEST_F(AppCacheQuotaClientTest, NoService) { |
192 AppCacheQuotaClient* client = CreateClient(); | 183 AppCacheQuotaClient* client = CreateClient(); |
193 Call_NotifyAppCacheReady(client); | 184 Call_NotifyAppCacheReady(client); |
194 Call_NotifyAppCacheDestroyed(client); | 185 Call_NotifyAppCacheDestroyed(client); |
195 | 186 |
196 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kTemp)); | 187 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kTemp)); |
197 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kPerm)); | 188 EXPECT_EQ(0, GetOriginUsage(client, kOriginA, kPerm)); |
198 EXPECT_TRUE(GetOriginsForType(client, kTemp).empty()); | 189 EXPECT_TRUE(GetOriginsForType(client, kTemp).empty()); |
199 EXPECT_TRUE(GetOriginsForType(client, kPerm).empty()); | 190 EXPECT_TRUE(GetOriginsForType(client, kPerm).empty()); |
200 EXPECT_TRUE(GetOriginsForHost(client, kTemp, kOriginA.host()).empty()); | 191 EXPECT_TRUE(GetOriginsForHost(client, kTemp, kOriginA.host()).empty()); |
201 EXPECT_TRUE(GetOriginsForHost(client, kPerm, kOriginA.host()).empty()); | 192 EXPECT_TRUE(GetOriginsForHost(client, kPerm, kOriginA.host()).empty()); |
202 EXPECT_EQ(quota::kQuotaErrorAbort, | 193 EXPECT_EQ(storage::kQuotaErrorAbort, |
203 DeleteOriginData(client, kTemp, kOriginA)); | 194 DeleteOriginData(client, kTemp, kOriginA)); |
204 EXPECT_EQ(quota::kQuotaErrorAbort, | 195 EXPECT_EQ(storage::kQuotaErrorAbort, |
205 DeleteOriginData(client, kPerm, kOriginA)); | 196 DeleteOriginData(client, kPerm, kOriginA)); |
206 | 197 |
207 Call_OnQuotaManagerDestroyed(client); | 198 Call_OnQuotaManagerDestroyed(client); |
208 } | 199 } |
209 | 200 |
210 TEST_F(AppCacheQuotaClientTest, GetOriginUsage) { | 201 TEST_F(AppCacheQuotaClientTest, GetOriginUsage) { |
211 AppCacheQuotaClient* client = CreateClient(); | 202 AppCacheQuotaClient* client = CreateClient(); |
212 Call_NotifyAppCacheReady(client); | 203 Call_NotifyAppCacheReady(client); |
213 | 204 |
214 SetUsageMapEntry(kOriginA, 1000); | 205 SetUsageMapEntry(kOriginA, 1000); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 Call_NotifyAppCacheDestroyed(client); | 260 Call_NotifyAppCacheDestroyed(client); |
270 Call_OnQuotaManagerDestroyed(client); | 261 Call_OnQuotaManagerDestroyed(client); |
271 } | 262 } |
272 | 263 |
273 TEST_F(AppCacheQuotaClientTest, DeleteOriginData) { | 264 TEST_F(AppCacheQuotaClientTest, DeleteOriginData) { |
274 AppCacheQuotaClient* client = CreateClient(); | 265 AppCacheQuotaClient* client = CreateClient(); |
275 Call_NotifyAppCacheReady(client); | 266 Call_NotifyAppCacheReady(client); |
276 | 267 |
277 // Perm deletions are short circuited in the Client and | 268 // Perm deletions are short circuited in the Client and |
278 // should not reach the AppCacheServiceImpl. | 269 // should not reach the AppCacheServiceImpl. |
279 EXPECT_EQ(quota::kQuotaStatusOk, | 270 EXPECT_EQ(storage::kQuotaStatusOk, DeleteOriginData(client, kPerm, kOriginA)); |
280 DeleteOriginData(client, kPerm, kOriginA)); | |
281 EXPECT_EQ(0, mock_service_.delete_called_count()); | 271 EXPECT_EQ(0, mock_service_.delete_called_count()); |
282 | 272 |
283 EXPECT_EQ(quota::kQuotaStatusOk, | 273 EXPECT_EQ(storage::kQuotaStatusOk, DeleteOriginData(client, kTemp, kOriginA)); |
284 DeleteOriginData(client, kTemp, kOriginA)); | |
285 EXPECT_EQ(1, mock_service_.delete_called_count()); | 274 EXPECT_EQ(1, mock_service_.delete_called_count()); |
286 | 275 |
287 mock_service_.set_mock_delete_appcaches_for_origin_result( | 276 mock_service_.set_mock_delete_appcaches_for_origin_result( |
288 net::ERR_ABORTED); | 277 net::ERR_ABORTED); |
289 EXPECT_EQ(quota::kQuotaErrorAbort, | 278 EXPECT_EQ(storage::kQuotaErrorAbort, |
290 DeleteOriginData(client, kTemp, kOriginA)); | 279 DeleteOriginData(client, kTemp, kOriginA)); |
291 EXPECT_EQ(2, mock_service_.delete_called_count()); | 280 EXPECT_EQ(2, mock_service_.delete_called_count()); |
292 | 281 |
293 Call_OnQuotaManagerDestroyed(client); | 282 Call_OnQuotaManagerDestroyed(client); |
294 Call_NotifyAppCacheDestroyed(client); | 283 Call_NotifyAppCacheDestroyed(client); |
295 } | 284 } |
296 | 285 |
297 TEST_F(AppCacheQuotaClientTest, PendingRequests) { | 286 TEST_F(AppCacheQuotaClientTest, PendingRequests) { |
298 AppCacheQuotaClient* client = CreateClient(); | 287 AppCacheQuotaClient* client = CreateClient(); |
299 | 288 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 350 |
362 // Kill the service. | 351 // Kill the service. |
363 Call_NotifyAppCacheDestroyed(client); | 352 Call_NotifyAppCacheDestroyed(client); |
364 | 353 |
365 // All should have been aborted and called completion. | 354 // All should have been aborted and called completion. |
366 EXPECT_EQ(2, num_get_origin_usage_completions_); | 355 EXPECT_EQ(2, num_get_origin_usage_completions_); |
367 EXPECT_EQ(4, num_get_origins_completions_); | 356 EXPECT_EQ(4, num_get_origins_completions_); |
368 EXPECT_EQ(3, num_delete_origins_completions_); | 357 EXPECT_EQ(3, num_delete_origins_completions_); |
369 EXPECT_EQ(0, usage_); | 358 EXPECT_EQ(0, usage_); |
370 EXPECT_TRUE(origins_.empty()); | 359 EXPECT_TRUE(origins_.empty()); |
371 EXPECT_EQ(quota::kQuotaErrorAbort, delete_status_); | 360 EXPECT_EQ(storage::kQuotaErrorAbort, delete_status_); |
372 | 361 |
373 Call_OnQuotaManagerDestroyed(client); | 362 Call_OnQuotaManagerDestroyed(client); |
374 } | 363 } |
375 | 364 |
376 TEST_F(AppCacheQuotaClientTest, DestroyQuotaManagerWithPending) { | 365 TEST_F(AppCacheQuotaClientTest, DestroyQuotaManagerWithPending) { |
377 AppCacheQuotaClient* client = CreateClient(); | 366 AppCacheQuotaClient* client = CreateClient(); |
378 | 367 |
379 SetUsageMapEntry(kOriginA, 1000); | 368 SetUsageMapEntry(kOriginA, 1000); |
380 SetUsageMapEntry(kOriginB, 10); | 369 SetUsageMapEntry(kOriginB, 10); |
381 SetUsageMapEntry(kOriginOther, 500); | 370 SetUsageMapEntry(kOriginOther, 500); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 403 |
415 // Start an async delete. | 404 // Start an async delete. |
416 AsyncDeleteOriginData(client, kTemp, kOriginB); | 405 AsyncDeleteOriginData(client, kTemp, kOriginB); |
417 EXPECT_EQ(0, num_delete_origins_completions_); | 406 EXPECT_EQ(0, num_delete_origins_completions_); |
418 | 407 |
419 // Kill the service. | 408 // Kill the service. |
420 Call_NotifyAppCacheDestroyed(client); | 409 Call_NotifyAppCacheDestroyed(client); |
421 | 410 |
422 // Should have been aborted. | 411 // Should have been aborted. |
423 EXPECT_EQ(1, num_delete_origins_completions_); | 412 EXPECT_EQ(1, num_delete_origins_completions_); |
424 EXPECT_EQ(quota::kQuotaErrorAbort, delete_status_); | 413 EXPECT_EQ(storage::kQuotaErrorAbort, delete_status_); |
425 | 414 |
426 // A real completion callback from the service should | 415 // A real completion callback from the service should |
427 // be dropped if it comes in after NotifyAppCacheDestroyed. | 416 // be dropped if it comes in after NotifyAppCacheDestroyed. |
428 base::RunLoop().RunUntilIdle(); | 417 base::RunLoop().RunUntilIdle(); |
429 EXPECT_EQ(1, num_delete_origins_completions_); | 418 EXPECT_EQ(1, num_delete_origins_completions_); |
430 EXPECT_EQ(quota::kQuotaErrorAbort, delete_status_); | 419 EXPECT_EQ(storage::kQuotaErrorAbort, delete_status_); |
431 | 420 |
432 Call_OnQuotaManagerDestroyed(client); | 421 Call_OnQuotaManagerDestroyed(client); |
433 } | 422 } |
434 | 423 |
435 } // namespace content | 424 } // namespace content |
OLD | NEW |