| 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 15 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 15 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 16 #include "content/browser/indexed_db/indexed_db_quota_client.h" | 16 #include "content/browser/indexed_db/indexed_db_quota_client.h" |
| 17 #include "content/browser/quota/mock_quota_manager.h" | 17 #include "content/browser/quota/mock_quota_manager.h" |
| 18 #include "content/public/browser/storage_partition.h" | 18 #include "content/public/browser/storage_partition.h" |
| 19 #include "content/public/test/test_browser_context.h" | 19 #include "content/public/test/test_browser_context.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "webkit/common/database/database_identifier.h" | 22 #include "webkit/common/database/database_identifier.h" |
| 23 | 23 |
| 24 // Declared to shorten the line lengths. | 24 // Declared to shorten the line lengths. |
| 25 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; | 25 static const storage::StorageType kTemp = storage::kStorageTypeTemporary; |
| 26 static const quota::StorageType kPerm = quota::kStorageTypePersistent; | 26 static const storage::StorageType kPerm = storage::kStorageTypePersistent; |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 // Base class for our test fixtures. | 30 // Base class for our test fixtures. |
| 31 class IndexedDBQuotaClientTest : public testing::Test { | 31 class IndexedDBQuotaClientTest : public testing::Test { |
| 32 public: | 32 public: |
| 33 const GURL kOriginA; | 33 const GURL kOriginA; |
| 34 const GURL kOriginB; | 34 const GURL kOriginB; |
| 35 const GURL kOriginOther; | 35 const GURL kOriginOther; |
| 36 | 36 |
| 37 IndexedDBQuotaClientTest() | 37 IndexedDBQuotaClientTest() |
| 38 : kOriginA("http://host"), | 38 : kOriginA("http://host"), |
| 39 kOriginB("http://host:8000"), | 39 kOriginB("http://host:8000"), |
| 40 kOriginOther("http://other"), | 40 kOriginOther("http://other"), |
| 41 usage_(0), | 41 usage_(0), |
| 42 task_runner_(new base::TestSimpleTaskRunner), | 42 task_runner_(new base::TestSimpleTaskRunner), |
| 43 weak_factory_(this) { | 43 weak_factory_(this) { |
| 44 browser_context_.reset(new TestBrowserContext()); | 44 browser_context_.reset(new TestBrowserContext()); |
| 45 | 45 |
| 46 scoped_refptr<quota::QuotaManager> quota_manager = | 46 scoped_refptr<storage::QuotaManager> quota_manager = |
| 47 new MockQuotaManager( | 47 new MockQuotaManager(false /*in_memory*/, |
| 48 false /*in_memory*/, | 48 browser_context_->GetPath(), |
| 49 browser_context_->GetPath(), | 49 base::MessageLoop::current()->message_loop_proxy(), |
| 50 base::MessageLoop::current()->message_loop_proxy(), | 50 base::MessageLoop::current()->message_loop_proxy(), |
| 51 base::MessageLoop::current()->message_loop_proxy(), | 51 browser_context_->GetSpecialStoragePolicy()); |
| 52 browser_context_->GetSpecialStoragePolicy()); | |
| 53 | 52 |
| 54 idb_context_ = | 53 idb_context_ = |
| 55 new IndexedDBContextImpl(browser_context_->GetPath(), | 54 new IndexedDBContextImpl(browser_context_->GetPath(), |
| 56 browser_context_->GetSpecialStoragePolicy(), | 55 browser_context_->GetSpecialStoragePolicy(), |
| 57 quota_manager->proxy(), | 56 quota_manager->proxy(), |
| 58 task_runner_); | 57 task_runner_); |
| 59 base::MessageLoop::current()->RunUntilIdle(); | 58 base::MessageLoop::current()->RunUntilIdle(); |
| 60 setup_temp_dir(); | 59 setup_temp_dir(); |
| 61 } | 60 } |
| 62 | 61 |
| 63 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); } | 62 void FlushIndexedDBTaskRunner() { task_runner_->RunUntilIdle(); } |
| 64 | 63 |
| 65 void setup_temp_dir() { | 64 void setup_temp_dir() { |
| 66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 65 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 67 base::FilePath indexeddb_dir = | 66 base::FilePath indexeddb_dir = |
| 68 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); | 67 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); |
| 69 ASSERT_TRUE(base::CreateDirectory(indexeddb_dir)); | 68 ASSERT_TRUE(base::CreateDirectory(indexeddb_dir)); |
| 70 idb_context()->set_data_path_for_testing(indexeddb_dir); | 69 idb_context()->set_data_path_for_testing(indexeddb_dir); |
| 71 } | 70 } |
| 72 | 71 |
| 73 virtual ~IndexedDBQuotaClientTest() { | 72 virtual ~IndexedDBQuotaClientTest() { |
| 74 FlushIndexedDBTaskRunner(); | 73 FlushIndexedDBTaskRunner(); |
| 75 idb_context_ = NULL; | 74 idb_context_ = NULL; |
| 76 browser_context_.reset(); | 75 browser_context_.reset(); |
| 77 base::MessageLoop::current()->RunUntilIdle(); | 76 base::MessageLoop::current()->RunUntilIdle(); |
| 78 } | 77 } |
| 79 | 78 |
| 80 int64 GetOriginUsage(quota::QuotaClient* client, | 79 int64 GetOriginUsage(storage::QuotaClient* client, |
| 81 const GURL& origin, | 80 const GURL& origin, |
| 82 quota::StorageType type) { | 81 storage::StorageType type) { |
| 83 usage_ = -1; | 82 usage_ = -1; |
| 84 client->GetOriginUsage( | 83 client->GetOriginUsage( |
| 85 origin, | 84 origin, |
| 86 type, | 85 type, |
| 87 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, | 86 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
| 88 weak_factory_.GetWeakPtr())); | 87 weak_factory_.GetWeakPtr())); |
| 89 FlushIndexedDBTaskRunner(); | 88 FlushIndexedDBTaskRunner(); |
| 90 base::MessageLoop::current()->RunUntilIdle(); | 89 base::MessageLoop::current()->RunUntilIdle(); |
| 91 EXPECT_GT(usage_, -1); | 90 EXPECT_GT(usage_, -1); |
| 92 return usage_; | 91 return usage_; |
| 93 } | 92 } |
| 94 | 93 |
| 95 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, | 94 const std::set<GURL>& GetOriginsForType(storage::QuotaClient* client, |
| 96 quota::StorageType type) { | 95 storage::StorageType type) { |
| 97 origins_.clear(); | 96 origins_.clear(); |
| 98 client->GetOriginsForType( | 97 client->GetOriginsForType( |
| 99 type, | 98 type, |
| 100 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 99 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 101 weak_factory_.GetWeakPtr())); | 100 weak_factory_.GetWeakPtr())); |
| 102 FlushIndexedDBTaskRunner(); | 101 FlushIndexedDBTaskRunner(); |
| 103 base::MessageLoop::current()->RunUntilIdle(); | 102 base::MessageLoop::current()->RunUntilIdle(); |
| 104 return origins_; | 103 return origins_; |
| 105 } | 104 } |
| 106 | 105 |
| 107 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, | 106 const std::set<GURL>& GetOriginsForHost(storage::QuotaClient* client, |
| 108 quota::StorageType type, | 107 storage::StorageType type, |
| 109 const std::string& host) { | 108 const std::string& host) { |
| 110 origins_.clear(); | 109 origins_.clear(); |
| 111 client->GetOriginsForHost( | 110 client->GetOriginsForHost( |
| 112 type, | 111 type, |
| 113 host, | 112 host, |
| 114 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 113 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 115 weak_factory_.GetWeakPtr())); | 114 weak_factory_.GetWeakPtr())); |
| 116 FlushIndexedDBTaskRunner(); | 115 FlushIndexedDBTaskRunner(); |
| 117 base::MessageLoop::current()->RunUntilIdle(); | 116 base::MessageLoop::current()->RunUntilIdle(); |
| 118 return origins_; | 117 return origins_; |
| 119 } | 118 } |
| 120 | 119 |
| 121 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 120 storage::QuotaStatusCode DeleteOrigin(storage::QuotaClient* client, |
| 122 const GURL& origin_url) { | 121 const GURL& origin_url) { |
| 123 delete_status_ = quota::kQuotaStatusUnknown; | 122 delete_status_ = storage::kQuotaStatusUnknown; |
| 124 client->DeleteOriginData( | 123 client->DeleteOriginData( |
| 125 origin_url, | 124 origin_url, |
| 126 kTemp, | 125 kTemp, |
| 127 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, | 126 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
| 128 weak_factory_.GetWeakPtr())); | 127 weak_factory_.GetWeakPtr())); |
| 129 FlushIndexedDBTaskRunner(); | 128 FlushIndexedDBTaskRunner(); |
| 130 base::MessageLoop::current()->RunUntilIdle(); | 129 base::MessageLoop::current()->RunUntilIdle(); |
| 131 return delete_status_; | 130 return delete_status_; |
| 132 } | 131 } |
| 133 | 132 |
| 134 IndexedDBContextImpl* idb_context() { return idb_context_; } | 133 IndexedDBContextImpl* idb_context() { return idb_context_; } |
| 135 | 134 |
| 136 void SetFileSizeTo(const base::FilePath& path, int size) { | 135 void SetFileSizeTo(const base::FilePath& path, int size) { |
| 137 std::string junk(size, 'a'); | 136 std::string junk(size, 'a'); |
| 138 ASSERT_EQ(size, base::WriteFile(path, junk.c_str(), size)); | 137 ASSERT_EQ(size, base::WriteFile(path, junk.c_str(), size)); |
| 139 } | 138 } |
| 140 | 139 |
| 141 void AddFakeIndexedDB(const GURL& origin, int size) { | 140 void AddFakeIndexedDB(const GURL& origin, int size) { |
| 142 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( | 141 base::FilePath file_path_origin = idb_context()->GetFilePathForTesting( |
| 143 webkit_database::GetIdentifierFromOrigin(origin)); | 142 storage::GetIdentifierFromOrigin(origin)); |
| 144 if (!base::CreateDirectory(file_path_origin)) { | 143 if (!base::CreateDirectory(file_path_origin)) { |
| 145 LOG(ERROR) << "failed to base::CreateDirectory " | 144 LOG(ERROR) << "failed to base::CreateDirectory " |
| 146 << file_path_origin.value(); | 145 << file_path_origin.value(); |
| 147 } | 146 } |
| 148 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 147 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
| 149 SetFileSizeTo(file_path_origin, size); | 148 SetFileSizeTo(file_path_origin, size); |
| 150 idb_context()->ResetCaches(); | 149 idb_context()->ResetCaches(); |
| 151 } | 150 } |
| 152 | 151 |
| 153 private: | 152 private: |
| 154 void OnGetOriginUsageComplete(int64 usage) { usage_ = usage; } | 153 void OnGetOriginUsageComplete(int64 usage) { usage_ = usage; } |
| 155 | 154 |
| 156 void OnGetOriginsComplete(const std::set<GURL>& origins) { | 155 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
| 157 origins_ = origins; | 156 origins_ = origins; |
| 158 } | 157 } |
| 159 | 158 |
| 160 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 159 void OnDeleteOriginComplete(storage::QuotaStatusCode code) { |
| 161 delete_status_ = code; | 160 delete_status_ = code; |
| 162 } | 161 } |
| 163 | 162 |
| 164 base::ScopedTempDir temp_dir_; | 163 base::ScopedTempDir temp_dir_; |
| 165 int64 usage_; | 164 int64 usage_; |
| 166 std::set<GURL> origins_; | 165 std::set<GURL> origins_; |
| 167 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 166 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 168 scoped_refptr<IndexedDBContextImpl> idb_context_; | 167 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 169 content::TestBrowserThreadBundle thread_bundle_; | 168 content::TestBrowserThreadBundle thread_bundle_; |
| 170 scoped_ptr<TestBrowserContext> browser_context_; | 169 scoped_ptr<TestBrowserContext> browser_context_; |
| 171 quota::QuotaStatusCode delete_status_; | 170 storage::QuotaStatusCode delete_status_; |
| 172 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; | 171 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
| 173 | 172 |
| 174 DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest); | 173 DISALLOW_COPY_AND_ASSIGN(IndexedDBQuotaClientTest); |
| 175 }; | 174 }; |
| 176 | 175 |
| 177 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 176 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 178 IndexedDBQuotaClient client(idb_context()); | 177 IndexedDBQuotaClient client(idb_context()); |
| 179 | 178 |
| 180 AddFakeIndexedDB(kOriginA, 6); | 179 AddFakeIndexedDB(kOriginA, 6); |
| 181 AddFakeIndexedDB(kOriginB, 3); | 180 AddFakeIndexedDB(kOriginB, 3); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 229 } |
| 231 | 230 |
| 232 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { | 231 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 233 IndexedDBQuotaClient client(idb_context()); | 232 IndexedDBQuotaClient client(idb_context()); |
| 234 | 233 |
| 235 AddFakeIndexedDB(kOriginA, 1000); | 234 AddFakeIndexedDB(kOriginA, 1000); |
| 236 AddFakeIndexedDB(kOriginB, 50); | 235 AddFakeIndexedDB(kOriginB, 50); |
| 237 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 236 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 238 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 237 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 239 | 238 |
| 240 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 239 storage::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 241 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 240 EXPECT_EQ(storage::kQuotaStatusOk, delete_status); |
| 242 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 241 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 243 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 242 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 244 } | 243 } |
| 245 | 244 |
| 246 } // namespace content | 245 } // namespace content |
| OLD | NEW |