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