| 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/thread_test_helper.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 idb_thread_(new base::Thread("IndexedDBTest")), |
| 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_thread_->Start(); |
| 55 idb_context_ = |
| 56 new IndexedDBContextImpl(browser_context_->GetPath(), |
| 57 browser_context_->GetSpecialStoragePolicy(), |
| 58 quota_manager->proxy(), |
| 59 idb_thread_->message_loop()); |
| 60 base::MessageLoop::current()->RunUntilIdle(); |
| 52 setup_temp_dir(); | 61 setup_temp_dir(); |
| 53 } | 62 } |
| 63 |
| 64 void FlushIndexedDBTaskRunner() { |
| 65 scoped_refptr<base::ThreadTestHelper> helper( |
| 66 new base::ThreadTestHelper(idb_context_->MessageLoopProxy())); |
| 67 ASSERT_TRUE(helper->Run()); |
| 68 } |
| 69 |
| 54 void setup_temp_dir() { | 70 void setup_temp_dir() { |
| 55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 56 base::FilePath indexeddb_dir = | 72 base::FilePath indexeddb_dir = |
| 57 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); | 73 temp_dir_.path().Append(IndexedDBContextImpl::kIndexedDBDirectory); |
| 58 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 74 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
| 59 idb_context()->set_data_path_for_testing(indexeddb_dir); | 75 idb_context()->set_data_path_for_testing(indexeddb_dir); |
| 60 } | 76 } |
| 61 | 77 |
| 62 virtual ~IndexedDBQuotaClientTest() { | 78 virtual ~IndexedDBQuotaClientTest() { |
| 63 // IndexedDBContext needs to be destructed on | 79 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; | 80 idb_context_ = NULL; |
| 68 browser_context_.reset(); | 81 browser_context_.reset(); |
| 69 base::MessageLoop::current()->RunUntilIdle(); | 82 base::MessageLoop::current()->RunUntilIdle(); |
| 70 } | 83 } |
| 71 | 84 |
| 72 int64 GetOriginUsage(quota::QuotaClient* client, | 85 int64 GetOriginUsage(quota::QuotaClient* client, |
| 73 const GURL& origin, | 86 const GURL& origin, |
| 74 quota::StorageType type) { | 87 quota::StorageType type) { |
| 75 usage_ = -1; | 88 usage_ = -1; |
| 76 client->GetOriginUsage( | 89 client->GetOriginUsage( |
| 77 origin, | 90 origin, |
| 78 type, | 91 type, |
| 79 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, | 92 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
| 80 weak_factory_.GetWeakPtr())); | 93 weak_factory_.GetWeakPtr())); |
| 94 FlushIndexedDBTaskRunner(); |
| 81 base::MessageLoop::current()->RunUntilIdle(); | 95 base::MessageLoop::current()->RunUntilIdle(); |
| 82 EXPECT_GT(usage_, -1); | 96 EXPECT_GT(usage_, -1); |
| 83 return usage_; | 97 return usage_; |
| 84 } | 98 } |
| 85 | 99 |
| 86 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, | 100 const std::set<GURL>& GetOriginsForType(quota::QuotaClient* client, |
| 87 quota::StorageType type) { | 101 quota::StorageType type) { |
| 88 origins_.clear(); | 102 origins_.clear(); |
| 89 client->GetOriginsForType( | 103 client->GetOriginsForType( |
| 90 type, | 104 type, |
| 91 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 105 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 92 weak_factory_.GetWeakPtr())); | 106 weak_factory_.GetWeakPtr())); |
| 107 FlushIndexedDBTaskRunner(); |
| 93 base::MessageLoop::current()->RunUntilIdle(); | 108 base::MessageLoop::current()->RunUntilIdle(); |
| 94 return origins_; | 109 return origins_; |
| 95 } | 110 } |
| 96 | 111 |
| 97 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, | 112 const std::set<GURL>& GetOriginsForHost(quota::QuotaClient* client, |
| 98 quota::StorageType type, | 113 quota::StorageType type, |
| 99 const std::string& host) { | 114 const std::string& host) { |
| 100 origins_.clear(); | 115 origins_.clear(); |
| 101 client->GetOriginsForHost( | 116 client->GetOriginsForHost( |
| 102 type, | 117 type, |
| 103 host, | 118 host, |
| 104 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, | 119 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 105 weak_factory_.GetWeakPtr())); | 120 weak_factory_.GetWeakPtr())); |
| 121 FlushIndexedDBTaskRunner(); |
| 106 base::MessageLoop::current()->RunUntilIdle(); | 122 base::MessageLoop::current()->RunUntilIdle(); |
| 107 return origins_; | 123 return origins_; |
| 108 } | 124 } |
| 109 | 125 |
| 110 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 126 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 111 const GURL& origin_url) { | 127 const GURL& origin_url) { |
| 112 delete_status_ = quota::kQuotaStatusUnknown; | 128 delete_status_ = quota::kQuotaStatusUnknown; |
| 113 client->DeleteOriginData( | 129 client->DeleteOriginData( |
| 114 origin_url, | 130 origin_url, |
| 115 kTemp, | 131 kTemp, |
| 116 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, | 132 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
| 117 weak_factory_.GetWeakPtr())); | 133 weak_factory_.GetWeakPtr())); |
| 134 FlushIndexedDBTaskRunner(); |
| 118 base::MessageLoop::current()->RunUntilIdle(); | 135 base::MessageLoop::current()->RunUntilIdle(); |
| 119 return delete_status_; | 136 return delete_status_; |
| 120 } | 137 } |
| 121 | 138 |
| 122 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } | 139 IndexedDBContextImpl* idb_context() { return idb_context_.get(); } |
| 123 | 140 |
| 124 void SetFileSizeTo(const base::FilePath& path, int size) { | 141 void SetFileSizeTo(const base::FilePath& path, int size) { |
| 125 std::string junk(size, 'a'); | 142 std::string junk(size, 'a'); |
| 126 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 143 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 127 } | 144 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 145 origins_ = origins; | 162 origins_ = origins; |
| 146 } | 163 } |
| 147 | 164 |
| 148 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 165 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 149 delete_status_ = code; | 166 delete_status_ = code; |
| 150 } | 167 } |
| 151 | 168 |
| 152 base::ScopedTempDir temp_dir_; | 169 base::ScopedTempDir temp_dir_; |
| 153 int64 usage_; | 170 int64 usage_; |
| 154 std::set<GURL> origins_; | 171 std::set<GURL> origins_; |
| 172 scoped_ptr<base::Thread> idb_thread_; |
| 155 scoped_refptr<IndexedDBContextImpl> idb_context_; | 173 scoped_refptr<IndexedDBContextImpl> idb_context_; |
| 156 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; | 174 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
| 157 base::MessageLoop message_loop_; | 175 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_; | 176 scoped_ptr<TestBrowserContext> browser_context_; |
| 164 quota::QuotaStatusCode delete_status_; | 177 quota::QuotaStatusCode delete_status_; |
| 165 }; | 178 }; |
| 166 | 179 |
| 167 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 180 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 168 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 181 IndexedDBQuotaClient client(idb_context()); |
| 169 idb_context()); | |
| 170 | 182 |
| 171 AddFakeIndexedDB(kOriginA, 6); | 183 AddFakeIndexedDB(kOriginA, 6); |
| 172 AddFakeIndexedDB(kOriginB, 3); | 184 AddFakeIndexedDB(kOriginB, 3); |
| 173 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); | 185 EXPECT_EQ(6, GetOriginUsage(&client, kOriginA, kTemp)); |
| 174 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 186 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 175 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 187 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 176 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 188 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 177 | 189 |
| 178 AddFakeIndexedDB(kOriginA, 1000); | 190 AddFakeIndexedDB(kOriginA, 1000); |
| 179 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 191 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 180 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); | 192 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kPerm)); |
| 181 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); | 193 EXPECT_EQ(3, GetOriginUsage(&client, kOriginB, kTemp)); |
| 182 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); | 194 EXPECT_EQ(0, GetOriginUsage(&client, kOriginB, kPerm)); |
| 183 } | 195 } |
| 184 | 196 |
| 185 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { | 197 TEST_F(IndexedDBQuotaClientTest, GetOriginsForHost) { |
| 186 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 198 IndexedDBQuotaClient client(idb_context()); |
| 187 idb_context()); | |
| 188 | 199 |
| 189 EXPECT_EQ(kOriginA.host(), kOriginB.host()); | 200 EXPECT_EQ(kOriginA.host(), kOriginB.host()); |
| 190 EXPECT_NE(kOriginA.host(), kOriginOther.host()); | 201 EXPECT_NE(kOriginA.host(), kOriginOther.host()); |
| 191 | 202 |
| 192 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 203 std::set<GURL> origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 193 EXPECT_TRUE(origins.empty()); | 204 EXPECT_TRUE(origins.empty()); |
| 194 | 205 |
| 195 AddFakeIndexedDB(kOriginA, 1000); | 206 AddFakeIndexedDB(kOriginA, 1000); |
| 196 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 207 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 197 EXPECT_EQ(origins.size(), 1ul); | 208 EXPECT_EQ(origins.size(), 1ul); |
| 198 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 209 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 199 | 210 |
| 200 AddFakeIndexedDB(kOriginB, 1000); | 211 AddFakeIndexedDB(kOriginB, 1000); |
| 201 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); | 212 origins = GetOriginsForHost(&client, kTemp, kOriginA.host()); |
| 202 EXPECT_EQ(origins.size(), 2ul); | 213 EXPECT_EQ(origins.size(), 2ul); |
| 203 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 214 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 204 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); | 215 EXPECT_TRUE(origins.find(kOriginB) != origins.end()); |
| 205 | 216 |
| 206 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); | 217 EXPECT_TRUE(GetOriginsForHost(&client, kPerm, kOriginA.host()).empty()); |
| 207 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); | 218 EXPECT_TRUE(GetOriginsForHost(&client, kTemp, kOriginOther.host()).empty()); |
| 208 } | 219 } |
| 209 | 220 |
| 210 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { | 221 TEST_F(IndexedDBQuotaClientTest, GetOriginsForType) { |
| 211 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 222 IndexedDBQuotaClient client(idb_context()); |
| 212 idb_context()); | |
| 213 | 223 |
| 214 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 224 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
| 215 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 225 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 216 | 226 |
| 217 AddFakeIndexedDB(kOriginA, 1000); | 227 AddFakeIndexedDB(kOriginA, 1000); |
| 218 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 228 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
| 219 EXPECT_EQ(origins.size(), 1ul); | 229 EXPECT_EQ(origins.size(), 1ul); |
| 220 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 230 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 221 | 231 |
| 222 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 232 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 223 } | 233 } |
| 224 | 234 |
| 225 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { | 235 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 226 IndexedDBQuotaClient client(base::MessageLoopProxy::current().get(), | 236 IndexedDBQuotaClient client(idb_context()); |
| 227 idb_context()); | |
| 228 | 237 |
| 229 AddFakeIndexedDB(kOriginA, 1000); | 238 AddFakeIndexedDB(kOriginA, 1000); |
| 230 AddFakeIndexedDB(kOriginB, 50); | 239 AddFakeIndexedDB(kOriginB, 50); |
| 231 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 240 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 232 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 241 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 233 | 242 |
| 234 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 243 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 235 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 244 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 236 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 245 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 237 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 246 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 238 } | 247 } |
| 239 | 248 |
| 240 } // namespace content | 249 } // namespace content |
| OLD | NEW |