| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <set> | 5 #include <set> |
| 6 #include <sstream> | 6 #include <sstream> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_callback_factory.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/message_loop_proxy.h" | 13 #include "base/message_loop_proxy.h" |
| 14 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageQuotaError.
h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageQuotaError.
h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 35 const StorageType kPerm = kStorageTypePersistent; | 35 const StorageType kPerm = kStorageTypePersistent; |
| 36 | 36 |
| 37 class QuotaManagerTest : public testing::Test { | 37 class QuotaManagerTest : public testing::Test { |
| 38 protected: | 38 protected: |
| 39 typedef QuotaManager::QuotaTableEntry QuotaTableEntry; | 39 typedef QuotaManager::QuotaTableEntry QuotaTableEntry; |
| 40 typedef QuotaManager::QuotaTableEntries QuotaTableEntries; | 40 typedef QuotaManager::QuotaTableEntries QuotaTableEntries; |
| 41 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries; | 41 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries; |
| 42 | 42 |
| 43 public: | 43 public: |
| 44 QuotaManagerTest() | 44 QuotaManagerTest() |
| 45 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 45 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 46 mock_time_counter_(0) { | 46 mock_time_counter_(0) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 void SetUp() { | 49 void SetUp() { |
| 50 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 50 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 51 mock_special_storage_policy_ = new MockSpecialStoragePolicy; | 51 mock_special_storage_policy_ = new MockSpecialStoragePolicy; |
| 52 quota_manager_ = new QuotaManager( | 52 quota_manager_ = new QuotaManager( |
| 53 false /* is_incognito */, | 53 false /* is_incognito */, |
| 54 data_dir_.path(), | 54 data_dir_.path(), |
| 55 MessageLoopProxy::current(), | 55 MessageLoopProxy::current(), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 74 } | 74 } |
| 75 | 75 |
| 76 void RegisterClient(MockStorageClient* client) { | 76 void RegisterClient(MockStorageClient* client) { |
| 77 quota_manager_->proxy()->RegisterClient(client); | 77 quota_manager_->proxy()->RegisterClient(client); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void GetUsageAndQuota(const GURL& origin, StorageType type) { | 80 void GetUsageAndQuota(const GURL& origin, StorageType type) { |
| 81 quota_status_ = kQuotaStatusUnknown; | 81 quota_status_ = kQuotaStatusUnknown; |
| 82 usage_ = -1; | 82 usage_ = -1; |
| 83 quota_ = -1; | 83 quota_ = -1; |
| 84 quota_manager_->GetUsageAndQuota(origin, type, | 84 quota_manager_->GetUsageAndQuota( |
| 85 callback_factory_.NewCallback( | 85 origin, type, |
| 86 &QuotaManagerTest::DidGetUsageAndQuota)); | 86 base::Bind(&QuotaManagerTest::DidGetUsageAndQuota, |
| 87 weak_factory_.GetWeakPtr())); |
| 87 } | 88 } |
| 88 | 89 |
| 89 void GetTemporaryGlobalQuota() { | 90 void GetTemporaryGlobalQuota() { |
| 90 quota_status_ = kQuotaStatusUnknown; | 91 quota_status_ = kQuotaStatusUnknown; |
| 91 quota_ = -1; | 92 quota_ = -1; |
| 92 quota_manager_->GetTemporaryGlobalQuota( | 93 quota_manager_->GetTemporaryGlobalQuota( |
| 93 callback_factory_.NewCallback( | 94 base::Bind(&QuotaManagerTest::DidGetQuota, |
| 94 &QuotaManagerTest::DidGetQuota)); | 95 weak_factory_.GetWeakPtr())); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void SetTemporaryGlobalQuota(int64 new_quota) { | 98 void SetTemporaryGlobalQuota(int64 new_quota) { |
| 98 quota_status_ = kQuotaStatusUnknown; | 99 quota_status_ = kQuotaStatusUnknown; |
| 99 quota_ = -1; | 100 quota_ = -1; |
| 100 quota_manager_->SetTemporaryGlobalQuota(new_quota, | 101 quota_manager_->SetTemporaryGlobalQuota( |
| 101 callback_factory_.NewCallback( | 102 new_quota, |
| 102 &QuotaManagerTest::DidGetQuota)); | 103 base::Bind(&QuotaManagerTest::DidGetQuota, |
| 104 weak_factory_.GetWeakPtr())); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void GetPersistentHostQuota(const std::string& host) { | 107 void GetPersistentHostQuota(const std::string& host) { |
| 106 quota_status_ = kQuotaStatusUnknown; | 108 quota_status_ = kQuotaStatusUnknown; |
| 107 host_.clear(); | 109 host_.clear(); |
| 108 type_ = kStorageTypeUnknown; | 110 type_ = kStorageTypeUnknown; |
| 109 quota_ = -1; | 111 quota_ = -1; |
| 110 quota_manager_->GetPersistentHostQuota(host, | 112 quota_manager_->GetPersistentHostQuota( |
| 111 callback_factory_.NewCallback( | 113 host, |
| 112 &QuotaManagerTest::DidGetHostQuota)); | 114 base::Bind(&QuotaManagerTest::DidGetHostQuota, |
| 115 weak_factory_.GetWeakPtr())); |
| 113 } | 116 } |
| 114 | 117 |
| 115 void SetPersistentHostQuota(const std::string& host, int64 new_quota) { | 118 void SetPersistentHostQuota(const std::string& host, int64 new_quota) { |
| 116 quota_status_ = kQuotaStatusUnknown; | 119 quota_status_ = kQuotaStatusUnknown; |
| 117 host_.clear(); | 120 host_.clear(); |
| 118 type_ = kStorageTypeUnknown; | 121 type_ = kStorageTypeUnknown; |
| 119 quota_ = -1; | 122 quota_ = -1; |
| 120 quota_manager_->SetPersistentHostQuota(host, new_quota, | 123 quota_manager_->SetPersistentHostQuota( |
| 121 callback_factory_.NewCallback( | 124 host, new_quota, |
| 122 &QuotaManagerTest::DidGetHostQuota)); | 125 base::Bind(&QuotaManagerTest::DidGetHostQuota, |
| 126 weak_factory_.GetWeakPtr())); |
| 123 } | 127 } |
| 124 | 128 |
| 125 void GetGlobalUsage(StorageType type) { | 129 void GetGlobalUsage(StorageType type) { |
| 126 type_ = kStorageTypeUnknown; | 130 type_ = kStorageTypeUnknown; |
| 127 usage_ = -1; | 131 usage_ = -1; |
| 128 unlimited_usage_ = -1; | 132 unlimited_usage_ = -1; |
| 129 quota_manager_->GetGlobalUsage(type, | 133 quota_manager_->GetGlobalUsage( |
| 130 callback_factory_.NewCallback( | 134 type, |
| 131 &QuotaManagerTest::DidGetGlobalUsage)); | 135 base::Bind(&QuotaManagerTest::DidGetGlobalUsage, |
| 136 weak_factory_.GetWeakPtr())); |
| 132 } | 137 } |
| 133 | 138 |
| 134 void GetHostUsage(const std::string& host, StorageType type) { | 139 void GetHostUsage(const std::string& host, StorageType type) { |
| 135 host_.clear(); | 140 host_.clear(); |
| 136 type_ = kStorageTypeUnknown; | 141 type_ = kStorageTypeUnknown; |
| 137 usage_ = -1; | 142 usage_ = -1; |
| 138 quota_manager_->GetHostUsage(host, type, | 143 quota_manager_->GetHostUsage( |
| 139 callback_factory_.NewCallback( | 144 host, type, |
| 140 &QuotaManagerTest::DidGetHostUsage)); | 145 base::Bind(&QuotaManagerTest::DidGetHostUsage, |
| 146 weak_factory_.GetWeakPtr())); |
| 141 } | 147 } |
| 142 | 148 |
| 143 void RunAdditionalUsageAndQuotaTask(const GURL& origin, StorageType type) { | 149 void RunAdditionalUsageAndQuotaTask(const GURL& origin, StorageType type) { |
| 144 quota_manager_->GetUsageAndQuota(origin, type, | 150 quota_manager_->GetUsageAndQuota( |
| 145 callback_factory_.NewCallback( | 151 origin, type, |
| 146 &QuotaManagerTest::DidGetUsageAndQuotaAdditional)); | 152 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaAdditional, |
| 153 weak_factory_.GetWeakPtr())); |
| 147 } | 154 } |
| 148 | 155 |
| 149 void DeleteClientOriginData(QuotaClient* client, | 156 void DeleteClientOriginData(QuotaClient* client, |
| 150 const GURL& origin, | 157 const GURL& origin, |
| 151 StorageType type) { | 158 StorageType type) { |
| 152 DCHECK(client); | 159 DCHECK(client); |
| 153 quota_status_ = kQuotaStatusUnknown; | 160 quota_status_ = kQuotaStatusUnknown; |
| 154 client->DeleteOriginData(origin, type, | 161 client->DeleteOriginData( |
| 155 callback_factory_.NewCallback( | 162 origin, type, |
| 156 &QuotaManagerTest::StatusCallback)); | 163 base::Bind(&QuotaManagerTest::StatusCallback, |
| 164 weak_factory_.GetWeakPtr())); |
| 157 } | 165 } |
| 158 | 166 |
| 159 void EvictOriginData(const GURL& origin, | 167 void EvictOriginData(const GURL& origin, |
| 160 StorageType type) { | 168 StorageType type) { |
| 161 quota_status_ = kQuotaStatusUnknown; | 169 quota_status_ = kQuotaStatusUnknown; |
| 162 quota_manager_->EvictOriginData(origin, type, | 170 quota_manager_->EvictOriginData( |
| 163 callback_factory_.NewCallback( | 171 origin, type, |
| 164 &QuotaManagerTest::StatusCallback)); | 172 base::Bind(&QuotaManagerTest::StatusCallback, |
| 173 weak_factory_.GetWeakPtr())); |
| 165 } | 174 } |
| 166 | 175 |
| 167 void DeleteOriginData(const GURL& origin, | 176 void DeleteOriginData(const GURL& origin, |
| 168 StorageType type) { | 177 StorageType type) { |
| 169 quota_status_ = kQuotaStatusUnknown; | 178 quota_status_ = kQuotaStatusUnknown; |
| 170 quota_manager_->DeleteOriginData(origin, type, | 179 quota_manager_->DeleteOriginData( |
| 171 callback_factory_.NewCallback( | 180 origin, type, |
| 172 &QuotaManagerTest::StatusCallback)); | 181 base::Bind(&QuotaManagerTest::StatusCallback, |
| 182 weak_factory_.GetWeakPtr())); |
| 173 } | 183 } |
| 174 | 184 |
| 175 void GetAvailableSpace() { | 185 void GetAvailableSpace() { |
| 176 quota_status_ = kQuotaStatusUnknown; | 186 quota_status_ = kQuotaStatusUnknown; |
| 177 available_space_ = -1; | 187 available_space_ = -1; |
| 178 quota_manager_->GetAvailableSpace( | 188 quota_manager_->GetAvailableSpace( |
| 179 callback_factory_.NewCallback( | 189 base::Bind(&QuotaManagerTest::DidGetAvailableSpace, |
| 180 &QuotaManagerTest::DidGetAvailableSpace)); | 190 weak_factory_.GetWeakPtr())); |
| 181 } | 191 } |
| 182 | 192 |
| 183 void GetUsageAndQuotaForEviction() { | 193 void GetUsageAndQuotaForEviction() { |
| 184 quota_status_ = kQuotaStatusUnknown; | 194 quota_status_ = kQuotaStatusUnknown; |
| 185 usage_ = -1; | 195 usage_ = -1; |
| 186 unlimited_usage_ = -1; | 196 unlimited_usage_ = -1; |
| 187 quota_ = -1; | 197 quota_ = -1; |
| 188 available_space_ = -1; | 198 available_space_ = -1; |
| 189 quota_manager_->GetUsageAndQuotaForEviction( | 199 quota_manager_->GetUsageAndQuotaForEviction( |
| 190 callback_factory_.NewCallback( | 200 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaForEviction, |
| 191 &QuotaManagerTest::DidGetUsageAndQuotaForEviction)); | 201 weak_factory_.GetWeakPtr())); |
| 192 } | 202 } |
| 193 | 203 |
| 194 void GetCachedOrigins(StorageType type, std::set<GURL>* origins) { | 204 void GetCachedOrigins(StorageType type, std::set<GURL>* origins) { |
| 195 ASSERT_TRUE(origins != NULL); | 205 ASSERT_TRUE(origins != NULL); |
| 196 origins->clear(); | 206 origins->clear(); |
| 197 quota_manager_->GetCachedOrigins(type, origins); | 207 quota_manager_->GetCachedOrigins(type, origins); |
| 198 } | 208 } |
| 199 | 209 |
| 200 void NotifyStorageAccessed(QuotaClient* client, | 210 void NotifyStorageAccessed(QuotaClient* client, |
| 201 const GURL& origin, | 211 const GURL& origin, |
| 202 StorageType type) { | 212 StorageType type) { |
| 203 DCHECK(client); | 213 DCHECK(client); |
| 204 quota_manager_->NotifyStorageAccessedInternal( | 214 quota_manager_->NotifyStorageAccessedInternal( |
| 205 client->id(), origin, type, IncrementMockTime()); | 215 client->id(), origin, type, IncrementMockTime()); |
| 206 } | 216 } |
| 207 | 217 |
| 208 void DeleteOriginFromDatabase(const GURL& origin, StorageType type) { | 218 void DeleteOriginFromDatabase(const GURL& origin, StorageType type) { |
| 209 quota_manager_->DeleteOriginFromDatabase(origin, type); | 219 quota_manager_->DeleteOriginFromDatabase(origin, type); |
| 210 } | 220 } |
| 211 | 221 |
| 212 void GetLRUOrigin(StorageType type) { | 222 void GetLRUOrigin(StorageType type) { |
| 213 lru_origin_ = GURL(); | 223 lru_origin_ = GURL(); |
| 214 quota_manager_->GetLRUOrigin(type, | 224 quota_manager_->GetLRUOrigin( |
| 215 callback_factory_.NewCallback(&QuotaManagerTest::DidGetLRUOrigin)); | 225 type, |
| 226 base::Bind(&QuotaManagerTest::DidGetLRUOrigin, |
| 227 weak_factory_.GetWeakPtr())); |
| 216 } | 228 } |
| 217 | 229 |
| 218 void NotifyOriginInUse(const GURL& origin) { | 230 void NotifyOriginInUse(const GURL& origin) { |
| 219 quota_manager_->NotifyOriginInUse(origin); | 231 quota_manager_->NotifyOriginInUse(origin); |
| 220 } | 232 } |
| 221 | 233 |
| 222 void NotifyOriginNoLongerInUse(const GURL& origin) { | 234 void NotifyOriginNoLongerInUse(const GURL& origin) { |
| 223 quota_manager_->NotifyOriginNoLongerInUse(origin); | 235 quota_manager_->NotifyOriginNoLongerInUse(origin); |
| 224 } | 236 } |
| 225 | 237 |
| 226 void GetOriginsModifiedSince(StorageType type, base::Time modified_since) { | 238 void GetOriginsModifiedSince(StorageType type, base::Time modified_since) { |
| 227 modified_origins_.clear(); | 239 modified_origins_.clear(); |
| 228 modified_origins_type_ = kStorageTypeUnknown; | 240 modified_origins_type_ = kStorageTypeUnknown; |
| 229 quota_manager_->GetOriginsModifiedSince(type, modified_since, | 241 quota_manager_->GetOriginsModifiedSince( |
| 230 callback_factory_.NewCallback( | 242 type, modified_since, |
| 231 &QuotaManagerTest::DidGetModifiedOrigins)); | 243 base::Bind(&QuotaManagerTest::DidGetModifiedOrigins, |
| 244 weak_factory_.GetWeakPtr())); |
| 232 } | 245 } |
| 233 | 246 |
| 234 void DumpQuotaTable() { | 247 void DumpQuotaTable() { |
| 235 quota_entries_.clear(); | 248 quota_entries_.clear(); |
| 236 quota_manager_->DumpQuotaTable( | 249 quota_manager_->DumpQuotaTable( |
| 237 callback_factory_.NewCallback( | 250 base::Bind(&QuotaManagerTest::DidDumpQuotaTable, |
| 238 &QuotaManagerTest::DidDumpQuotaTable)); | 251 weak_factory_.GetWeakPtr())); |
| 239 } | 252 } |
| 240 | 253 |
| 241 void DumpOriginInfoTable() { | 254 void DumpOriginInfoTable() { |
| 242 origin_info_entries_.clear(); | 255 origin_info_entries_.clear(); |
| 243 quota_manager_->DumpOriginInfoTable( | 256 quota_manager_->DumpOriginInfoTable( |
| 244 callback_factory_.NewCallback( | 257 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable, |
| 245 &QuotaManagerTest::DidDumpOriginInfoTable)); | 258 weak_factory_.GetWeakPtr())); |
| 246 } | 259 } |
| 247 | 260 |
| 248 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { | 261 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { |
| 249 quota_status_ = status; | 262 quota_status_ = status; |
| 250 usage_ = usage; | 263 usage_ = usage; |
| 251 quota_ = quota; | 264 quota_ = quota; |
| 252 } | 265 } |
| 253 | 266 |
| 254 void DidGetQuota(QuotaStatusCode status, | 267 void DidGetQuota(QuotaStatusCode status, |
| 255 StorageType type, | 268 StorageType type, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 int status_callback_count() const { return status_callback_count_; } | 369 int status_callback_count() const { return status_callback_count_; } |
| 357 void reset_status_callback_count() { status_callback_count_ = 0; } | 370 void reset_status_callback_count() { status_callback_count_ = 0; } |
| 358 | 371 |
| 359 private: | 372 private: |
| 360 base::Time IncrementMockTime() { | 373 base::Time IncrementMockTime() { |
| 361 ++mock_time_counter_; | 374 ++mock_time_counter_; |
| 362 return base::Time::FromDoubleT(mock_time_counter_ * 10.0); | 375 return base::Time::FromDoubleT(mock_time_counter_ * 10.0); |
| 363 } | 376 } |
| 364 | 377 |
| 365 ScopedTempDir data_dir_; | 378 ScopedTempDir data_dir_; |
| 366 base::ScopedCallbackFactory<QuotaManagerTest> callback_factory_; | 379 base::WeakPtrFactory<QuotaManagerTest> weak_factory_; |
| 367 | 380 |
| 368 scoped_refptr<QuotaManager> quota_manager_; | 381 scoped_refptr<QuotaManager> quota_manager_; |
| 369 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_; | 382 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_; |
| 370 | 383 |
| 371 QuotaStatusCode quota_status_; | 384 QuotaStatusCode quota_status_; |
| 372 std::string host_; | 385 std::string host_; |
| 373 StorageType type_; | 386 StorageType type_; |
| 374 int64 usage_; | 387 int64 usage_; |
| 375 int64 unlimited_usage_; | 388 int64 unlimited_usage_; |
| 376 int64 quota_; | 389 int64 quota_; |
| (...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1585 GetPersistentHostQuota(std::string()); | 1598 GetPersistentHostQuota(std::string()); |
| 1586 MessageLoop::current()->RunAllPending(); | 1599 MessageLoop::current()->RunAllPending(); |
| 1587 EXPECT_EQ(kQuotaStatusOk, status()); | 1600 EXPECT_EQ(kQuotaStatusOk, status()); |
| 1588 EXPECT_EQ(0, quota()); | 1601 EXPECT_EQ(0, quota()); |
| 1589 | 1602 |
| 1590 SetPersistentHostQuota(std::string(), 10); | 1603 SetPersistentHostQuota(std::string(), 10); |
| 1591 MessageLoop::current()->RunAllPending(); | 1604 MessageLoop::current()->RunAllPending(); |
| 1592 EXPECT_EQ(kQuotaErrorNotSupported, status()); | 1605 EXPECT_EQ(kQuotaErrorNotSupported, status()); |
| 1593 } | 1606 } |
| 1594 } // namespace quota | 1607 } // namespace quota |
| OLD | NEW |