OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <list> | 5 #include <list> |
6 #include <map> | 6 #include <map> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/quota/mock_storage_client.h" | 16 #include "webkit/quota/mock_storage_client.h" |
17 #include "webkit/quota/quota_manager.h" | 17 #include "webkit/quota/quota_manager.h" |
18 #include "webkit/quota/quota_temporary_storage_evictor.h" | 18 #include "webkit/quota/quota_temporary_storage_evictor.h" |
19 | 19 |
20 namespace quota { | 20 namespace quota { |
21 | 21 |
22 class QuotaTemporaryStorageEvictorTest; | 22 class QuotaTemporaryStorageEvictorTest; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler { | 26 class MockQuotaEvictionHandler : public quota::QuotaEvictionHandler { |
27 public: | 27 public: |
28 explicit MockQuotaEvictionHandler(QuotaTemporaryStorageEvictorTest *test) | 28 explicit MockQuotaEvictionHandler(QuotaTemporaryStorageEvictorTest *test) |
29 : quota_(0), | 29 : quota_(0), |
30 unlimited_usage_(0), | |
31 available_space_(0), | 30 available_space_(0), |
32 error_on_evict_origin_data_(false), | 31 error_on_evict_origin_data_(false), |
33 error_on_get_usage_and_quota_(false) {} | 32 error_on_get_usage_and_quota_(false) {} |
34 | 33 |
35 virtual void EvictOriginData( | 34 virtual void EvictOriginData( |
36 const GURL& origin, | 35 const GURL& origin, |
37 StorageType type, | 36 StorageType type, |
38 const EvictOriginDataCallback& callback) OVERRIDE { | 37 const EvictOriginDataCallback& callback) OVERRIDE { |
39 if (error_on_evict_origin_data_) { | 38 if (error_on_evict_origin_data_) { |
40 callback.Run(quota::kQuotaErrorInvalidModification); | 39 callback.Run(quota::kQuotaErrorInvalidModification); |
41 return; | 40 return; |
42 } | 41 } |
43 int64 origin_usage = EnsureOriginRemoved(origin); | 42 int64 origin_usage = EnsureOriginRemoved(origin); |
44 if (origin_usage >= 0) | 43 if (origin_usage >= 0) |
45 available_space_ += origin_usage; | 44 available_space_ += origin_usage; |
46 callback.Run(quota::kQuotaStatusOk); | 45 callback.Run(quota::kQuotaStatusOk); |
47 } | 46 } |
48 | 47 |
49 virtual void GetUsageAndQuotaForEviction( | 48 virtual void GetUsageAndQuotaForEviction( |
50 const UsageAndQuotaCallback& callback) OVERRIDE { | 49 const UsageAndQuotaCallback& callback) OVERRIDE { |
51 if (error_on_get_usage_and_quota_) { | 50 if (error_on_get_usage_and_quota_) { |
52 callback.Run(quota::kQuotaErrorInvalidAccess, UsageAndQuota()); | 51 callback.Run(quota::kQuotaErrorInvalidAccess, UsageAndQuota()); |
53 return; | 52 return; |
54 } | 53 } |
55 if (!task_for_get_usage_and_quota_.is_null()) | 54 if (!task_for_get_usage_and_quota_.is_null()) |
56 task_for_get_usage_and_quota_.Run(); | 55 task_for_get_usage_and_quota_.Run(); |
57 UsageAndQuota quota_and_usage( | 56 UsageAndQuota quota_and_usage(-1, GetUsage(), quota_, available_space_); |
58 0, GetUsage(), unlimited_usage_, quota_, available_space_); | |
59 callback.Run(quota::kQuotaStatusOk, quota_and_usage); | 57 callback.Run(quota::kQuotaStatusOk, quota_and_usage); |
60 } | 58 } |
61 | 59 |
62 virtual void GetLRUOrigin( | 60 virtual void GetLRUOrigin( |
63 StorageType type, | 61 StorageType type, |
64 const GetLRUOriginCallback& callback) OVERRIDE { | 62 const GetLRUOriginCallback& callback) OVERRIDE { |
65 if (origin_order_.empty()) | 63 if (origin_order_.empty()) |
66 callback.Run(GURL()); | 64 callback.Run(GURL()); |
67 else | 65 else |
68 callback.Run(GURL(origin_order_.front())); | 66 callback.Run(GURL(origin_order_.front())); |
69 } | 67 } |
70 | 68 |
71 int64 GetUsage() const { | 69 int64 GetUsage() const { |
72 int64 total_usage = 0; | 70 int64 total_usage = 0; |
73 for (std::map<GURL, int64>::const_iterator p = origins_.begin(); | 71 for (std::map<GURL, int64>::const_iterator p = origins_.begin(); |
74 p != origins_.end(); | 72 p != origins_.end(); |
75 ++p) | 73 ++p) |
76 total_usage += p->second; | 74 total_usage += p->second; |
77 return total_usage; | 75 return total_usage; |
78 } | 76 } |
79 | 77 |
80 void set_quota(int64 quota) { | 78 void set_quota(int64 quota) { |
81 quota_ = quota; | 79 quota_ = quota; |
82 } | 80 } |
83 void set_unlimited_usage(int64 usage) { | |
84 unlimited_usage_ = usage; | |
85 } | |
86 void set_available_space(int64 available_space) { | 81 void set_available_space(int64 available_space) { |
87 available_space_ = available_space; | 82 available_space_ = available_space; |
88 } | 83 } |
89 void set_task_for_get_usage_and_quota(const base::Closure& task) { | 84 void set_task_for_get_usage_and_quota(const base::Closure& task) { |
90 task_for_get_usage_and_quota_= task; | 85 task_for_get_usage_and_quota_= task; |
91 } | 86 } |
92 void set_error_on_evict_origin_data(bool error_on_evict_origin_data) { | 87 void set_error_on_evict_origin_data(bool error_on_evict_origin_data) { |
93 error_on_evict_origin_data_ = error_on_evict_origin_data; | 88 error_on_evict_origin_data_ = error_on_evict_origin_data; |
94 } | 89 } |
95 void set_error_on_get_usage_and_quota(bool error_on_get_usage_and_quota) { | 90 void set_error_on_get_usage_and_quota(bool error_on_get_usage_and_quota) { |
(...skipping 24 matching lines...) Expand all Loading... |
120 return -1; | 115 return -1; |
121 else | 116 else |
122 origin_usage = origins_[origin]; | 117 origin_usage = origins_[origin]; |
123 | 118 |
124 origins_.erase(origin); | 119 origins_.erase(origin); |
125 origin_order_.remove(origin); | 120 origin_order_.remove(origin); |
126 return origin_usage; | 121 return origin_usage; |
127 } | 122 } |
128 | 123 |
129 int64 quota_; | 124 int64 quota_; |
130 int64 unlimited_usage_; | |
131 int64 available_space_; | 125 int64 available_space_; |
132 std::list<GURL> origin_order_; | 126 std::list<GURL> origin_order_; |
133 std::map<GURL, int64> origins_; | 127 std::map<GURL, int64> origins_; |
134 bool error_on_evict_origin_data_; | 128 bool error_on_evict_origin_data_; |
135 bool error_on_get_usage_and_quota_; | 129 bool error_on_get_usage_and_quota_; |
136 | 130 |
137 base::Closure task_for_get_usage_and_quota_; | 131 base::Closure task_for_get_usage_and_quota_; |
138 }; | 132 }; |
139 | 133 |
140 } // anonymous namespace | 134 } // anonymous namespace |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 base::MessageLoop::current()->RunUntilIdle(); | 400 base::MessageLoop::current()->RunUntilIdle(); |
407 EXPECT_EQ(150 + 300, quota_eviction_handler()->GetUsage()); | 401 EXPECT_EQ(150 + 300, quota_eviction_handler()->GetUsage()); |
408 | 402 |
409 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); | 403 EXPECT_EQ(0, statistics().num_errors_on_evicting_origin); |
410 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); | 404 EXPECT_EQ(0, statistics().num_errors_on_getting_usage_and_quota); |
411 EXPECT_EQ(2, statistics().num_evicted_origins); | 405 EXPECT_EQ(2, statistics().num_evicted_origins); |
412 EXPECT_EQ(1, statistics().num_eviction_rounds); | 406 EXPECT_EQ(1, statistics().num_eviction_rounds); |
413 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds); | 407 EXPECT_EQ(0, statistics().num_skipped_eviction_rounds); |
414 } | 408 } |
415 | 409 |
416 TEST_F(QuotaTemporaryStorageEvictorTest, UnlimitedExclusionEvictionTest) { | |
417 quota_eviction_handler()->AddOrigin(GURL("http://www.z.com"), 3000); | |
418 quota_eviction_handler()->AddOrigin(GURL("http://www.y.com"), 200); | |
419 quota_eviction_handler()->AddOrigin(GURL("http://www.x.com"), 500000); | |
420 quota_eviction_handler()->set_unlimited_usage(500000); | |
421 quota_eviction_handler()->set_quota(5000); | |
422 quota_eviction_handler()->set_available_space(1000000000); | |
423 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); | |
424 set_repeated_eviction(false); | |
425 temporary_storage_evictor()->Start(); | |
426 base::MessageLoop::current()->RunUntilIdle(); | |
427 // Nothing should have been evicted. | |
428 EXPECT_EQ(3000 + 200 + 500000, quota_eviction_handler()->GetUsage()); | |
429 } | |
430 | |
431 } // namespace quota | 410 } // namespace quota |
OLD | NEW |