| 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.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/threading/thread.h" |
| 13 #include "content/browser/browser_thread_impl.h" | 14 #include "content/browser/browser_thread_impl.h" |
| 14 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 15 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 15 #include "content/browser/indexed_db/indexed_db_quota_client.h" | 16 #include "content/browser/indexed_db/indexed_db_quota_client.h" |
| 16 #include "content/public/browser/storage_partition.h" | 17 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/test/test_browser_context.h" | 18 #include "content/public/test/test_browser_context.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webkit/browser/quota/mock_quota_manager.h" |
| 19 #include "webkit/common/database/database_identifier.h" | 22 #include "webkit/common/database/database_identifier.h" |
| 20 | 23 |
| 21 // Declared to shorten the line lengths. | 24 // Declared to shorten the line lengths. |
| 22 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; | 25 static const quota::StorageType kTemp = quota::kStorageTypeTemporary; |
| 23 static const quota::StorageType kPerm = quota::kStorageTypePersistent; | 26 static const quota::StorageType kPerm = quota::kStorageTypePersistent; |
| 24 | 27 |
| 25 namespace content { | 28 namespace content { |
| 26 | 29 |
| 27 // Base class for our test fixtures. | 30 // Base class for our test fixtures. |
| 28 class IndexedDBQuotaClientTest : public testing::Test { | 31 class IndexedDBQuotaClientTest : public testing::Test { |
| 29 public: | 32 public: |
| 30 const GURL kOriginA; | 33 const GURL kOriginA; |
| 31 const GURL kOriginB; | 34 const GURL kOriginB; |
| 32 const GURL kOriginOther; | 35 const GURL kOriginOther; |
| 33 | 36 |
| 34 IndexedDBQuotaClientTest() | 37 IndexedDBQuotaClientTest() |
| 35 : kOriginA("http://host"), | 38 : kOriginA("http://host"), |
| 36 kOriginB("http://host:8000"), | 39 kOriginB("http://host:8000"), |
| 37 kOriginOther("http://other"), | 40 kOriginOther("http://other"), |
| 38 usage_(0), | 41 usage_(0), |
| 39 weak_factory_(this), | 42 task_runner_(new base::TestSimpleTaskRunner), |
| 40 message_loop_(base::MessageLoop::TYPE_IO), | 43 weak_factory_(this) { |
| 41 db_thread_(BrowserThread::DB, &message_loop_), | |
| 42 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), | |
| 43 file_thread_(BrowserThread::FILE, &message_loop_), | |
| 44 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | |
| 45 &message_loop_), | |
| 46 io_thread_(BrowserThread::IO, &message_loop_) { | |
| 47 browser_context_.reset(new TestBrowserContext()); | 44 browser_context_.reset(new TestBrowserContext()); |
| 48 idb_context_ = static_cast<IndexedDBContextImpl*>( | 45 |
| 49 BrowserContext::GetDefaultStoragePartition(browser_context_.get()) | 46 scoped_refptr<quota::QuotaManager> quota_manager = |
| 50 ->GetIndexedDBContext()); | 47 new quota::MockQuotaManager( |
| 51 message_loop_.RunUntilIdle(); | 48 false /*in_memory*/, |
| 49 browser_context_->GetPath(), |
| 50 base::MessageLoop::current()->message_loop_proxy(), |
| 51 base::MessageLoop::current()->message_loop_proxy(), |
| 52 browser_context_->GetSpecialStoragePolicy()); |
| 53 |
| 54 idb_context_ = |
| 55 new IndexedDBContextImpl(browser_context_->GetPath(), |
| 56 browser_context_->GetSpecialStoragePolicy(), |
| 57 quota_manager->proxy(), |
| 58 task_runner_); |
| 59 base::MessageLoop::current()->RunUntilIdle(); |
| 52 setup_temp_dir(); | 60 setup_temp_dir(); |
| 53 } | 61 } |
| 62 |
| 63 void FlushIndexedDBTaskRunner() { |
| 64 task_runner_->RunUntilIdle(); |
| 65 } |
| 66 |
| 54 void setup_temp_dir() { | 67 void setup_temp_dir() { |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 68 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 56 base::FilePath indexeddb_dir = | 69 base::FilePath indexeddb_dir = |
| 57 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); | 70 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); |
| 58 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 71 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
| 59 idb_context()->set_data_path_for_testing(indexeddb_dir); | 72 idb_context()->set_data_path_for_testing(indexeddb_dir); |
| 60 } | 73 } |
| 61 | 74 |
| 62 virtual ~IndexedDBQuotaClientTest() { | 75 virtual ~IndexedDBQuotaClientTest() { |
| 63 // IndexedDBContext needs to be destructed on | 76 FlushIndexedDBTaskRunner(); |
| 64 // BrowserThread::WEBKIT_DEPRECATED, which is also a member variable of this | |
| 65 // class. Cause IndexedDBContext's destruction now to ensure that it | |
| 66 // doesn't outlive BrowserThread::WEBKIT_DEPRECATED. | |
| 67 idb_context_ = NULL; | 77 idb_context_ = NULL; |
| 68 browser_context_.reset(); | 78 browser_context_.reset(); |
| 69 base::MessageLoop::current()->RunUntilIdle(); | 79 base::MessageLoop::current()->RunUntilIdle(); |
| 70 } | 80 } |
| 71 | 81 |
| 72 int64 GetOriginUsage(quota::QuotaClient* client, | 82 int64 GetOriginUsage(quota::QuotaClient* client, |
| 73 const GURL& origin, | 83 const GURL& origin, |
| 74 quota::StorageType type) { | 84 quota::StorageType type) { |
| 75 usage_ = -1; | 85 usage_ = -1; |
| 76 client->GetOriginUsage( | 86 client->GetOriginUsage( |
| 77 origin, | 87 origin, |
| 78 type, | 88 type, |
| 79 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, | 89 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
| 80 weak_factory_.GetWeakPtr())); | 90 weak_factory_.GetWeakPtr())); |
| 91 FlushIndexedDBTaskRunner(); |
| 81 base::MessageLoop::current()->RunUntilIdle(); | 92 base::MessageLoop::current()->RunUntilIdle(); |
| 82 EXPECT_GT(usage_, -1); | 93 EXPECT_GT(usage_, -1); |
| 83 return usage_; | 94 return usage_; |
| 84 } | 95 } |
| 85 | 96 |
| 86 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, | 97 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, |
| 87 quota::StorageType type) { | 98 quota::StorageType type) { |
| 88 origins_.clear(); | 99 origins_.clear(); |
| 89 client->GetOriginsForType( | 100 client->GetOriginsForType( |
| 90 type, | 101 type, |
| 91 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 102 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 92 weak_factory_.GetWeakPtr())); | 103 weak_factory_.GetWeakPtr())); |
| 104 FlushIndexedDBTaskRunner(); |
| 93 base::MessageLoop::current()->RunUntilIdle(); | 105 base::MessageLoop::current()->RunUntilIdle(); |
| 94 return origins_; | 106 return origins_; |
| 95 } | 107 } |
| 96 | 108 |
| 97 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, | 109 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, |
| 98 quota::StorageType type, | 110 quota::StorageType type, |
| 99 const std::string& host) { | 111 const std::string& host) { |
| 100 origins_.clear(); | 112 origins_.clear(); |
| 101 client->GetOriginsForHost( | 113 client->GetOriginsForHost( |
| 102 type, | 114 type, |
| 103 host, | 115 host, |
| 104 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 116 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 105 weak_factory_.GetWeakPtr())); | 117 weak_factory_.GetWeakPtr())); |
| 118 FlushIndexedDBTaskRunner(); |
| 106 base::MessageLoop::current()->RunUntilIdle(); | 119 base::MessageLoop::current()->RunUntilIdle(); |
| 107 return origins_; | 120 return origins_; |
| 108 } | 121 } |
| 109 | 122 |
| 110 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 123 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 111 const GURL& origin_url) { | 124 const GURL& origin_url) { |
| 112 delete_status_ = quota::kQuotaStatusUnknown; | 125 delete_status_ = quota::kQuotaStatusUnknown; |
| 113 client->DeleteOriginData( | 126 client->DeleteOriginData( |
| 114 origin_url, | 127 origin_url, |
| 115 kTemp, | 128 kTemp, |
| 116 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, | 129 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
| 117 weak_factory_.GetWeakPtr())); | 130 weak_factory_.GetWeakPtr())); |
| 131 FlushIndexedDBTaskRunner(); |
| 118 base::MessageLoop::current()->RunUntilIdle(); | 132 base::MessageLoop::current()->RunUntilIdle(); |
| 119 return delete_status_; | 133 return delete_status_; |
| 120 } | 134 } |
| 121 | 135 |
| 122 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } | 136 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } |
| 123 | 137 |
| 124 void SetFileSizeTo(const base::FilePath& path, int size) { | 138 void SetFileSizeTo(const base::FilePath& path, int size) { |
| 125 std::string junk(size, 'a'); | 139 std::string junk(size, 'a'); |
| 126 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 140 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 127 } | 141 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 145 origins_ = origins; | 159 origins_ = origins; |
| 146 } | 160 } |
| 147 | 161 |
| 148 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 162 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 149 delete_status_ = code; | 163 delete_status_ = code; |
| 150 } | 164 } |
| 151 | 165 |
| 152 base::ScopedTempDir temp_dir_; | 166 base::ScopedTempDir temp_dir_; |
| 153 int64 usage_; | 167 int64 usage_; |
| 154 std::set<GURL> origins_; | 168 std::set<GURL> origins_; |
| 169 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 155 scoped_refptr<IndexedDBContextImpl> idb_context_; | 170 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 156 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; | 171 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
| 157 base::MessageLoop message_loop_; | 172 content::TestBrowserThreadBundle thread_bundle_; |
| 158 BrowserThreadImpl db_thread_; | |
| 159 BrowserThreadImpl webkit_thread_; | |
| 160 BrowserThreadImpl file_thread_; | |
| 161 BrowserThreadImpl file_user_blocking_thread_; | |
| 162 BrowserThreadImpl io_thread_; | |
| 163 scoped_ptr<TestBrowserContext> browser_context_; | 173 scoped_ptr<TestBrowserContext> browser_context_; |
| 164 quota::QuotaStatusCode delete_status_; | 174 quota::QuotaStatusCode delete_status_; |
| 165 }; | 175 }; |
| 166 | 176 |
| 167 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 177 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 168 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 178 IndexedDBQuotaClient client(idb_context()); |
| 169 idb_context()); | |
| 170 | 179 |
| 171 AddFakeIndexedDB(kOriginA, 6); | 180 AddFakeIndexedDB(kOriginA, 6); |
| 172 AddFakeIndexedDB(kOriginB, 3); | 181 AddFakeIndexedDB(kOriginB, 3); |
| 173 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | 182 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
| 174 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 183 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 175 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 184 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 176 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 185 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 177 | 186 |
| 178 AddFakeIndexedDB(kOriginA, 1000); | 187 AddFakeIndexedDB(kOriginA, 1000); |
| 179 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 188 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 180 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 189 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 181 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 190 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 182 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 191 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 183 } | 192 } |
| 184 | 193 |
| 185 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { | 194 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { |
| 186 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 195 IndexedDBQuotaClient client(idb_context()); |
| 187 idb_context()); | |
| 188 | 196 |
| 189 EXPECT_EQ(kOriginA.host(), kOriginB.host()); | 197 EXPECT_EQ(kOriginA.host(), kOriginB.host()); |
| 190 EXPECT_NE(kOriginA.host(), kOriginOther.host()); | 198 EXPECT_NE(kOriginA.host(), kOriginOther.host()); |
| 191 | 199 |
| 192 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 200 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 193 EXPECT_TRUE(origins.empty()); | 201 EXPECT_TRUE(origins.empty()); |
| 194 | 202 |
| 195 AddFakeIndexedDB(kOriginA, 1000); | 203 AddFakeIndexedDB(kOriginA, 1000); |
| 196 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 204 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 197 EXPECT_EQ(origins.size(), 1ul); | 205 EXPECT_EQ(origins.size(), 1ul); |
| 198 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 206 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 199 | 207 |
| 200 AddFakeIndexedDB(kOriginB, 1000); | 208 AddFakeIndexedDB(kOriginB, 1000); |
| 201 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 209 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 202 EXPECT_EQ(origins.size(), 2ul); | 210 EXPECT_EQ(origins.size(), 2ul); |
| 203 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 211 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 204 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); | 212 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); |
| 205 | 213 |
| 206 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); | 214 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); |
| 207 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); | 215 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); |
| 208 } | 216 } |
| 209 | 217 |
| 210 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { | 218 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { |
| 211 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 219 IndexedDBQuotaClient client(idb_context()); |
| 212 idb_context()); | |
| 213 | 220 |
| 214 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 221 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
| 215 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 222 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 216 | 223 |
| 217 AddFakeIndexedDB(kOriginA, 1000); | 224 AddFakeIndexedDB(kOriginA, 1000); |
| 218 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 225 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
| 219 EXPECT_EQ(origins.size(), 1ul); | 226 EXPECT_EQ(origins.size(), 1ul); |
| 220 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 227 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 221 | 228 |
| 222 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 229 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 223 } | 230 } |
| 224 | 231 |
| 225 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { | 232 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 226 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 233 IndexedDBQuotaClient client(idb_context()); |
| 227 idb_context()); | |
| 228 | 234 |
| 229 AddFakeIndexedDB(kOriginA, 1000); | 235 AddFakeIndexedDB(kOriginA, 1000); |
| 230 AddFakeIndexedDB(kOriginB, 50); | 236 AddFakeIndexedDB(kOriginB, 50); |
| 231 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 237 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 232 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 238 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 233 | 239 |
| 234 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 240 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 235 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 241 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 236 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 242 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 237 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 243 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 238 } | 244 } |
| 239 | 245 |
| 240 } // namespace content | 246 } // namespace content |
| OLD | NEW |