| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webkit/dom_storage/dom_storage_area.h" | 14 #include "webkit/dom_storage/dom_storage_area.h" |
| 15 #include "webkit/dom_storage/dom_storage_database.h" |
| 16 #include "webkit/dom_storage/dom_storage_database_adapter.h" |
| 15 #include "webkit/dom_storage/dom_storage_task_runner.h" | 17 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 16 #include "webkit/dom_storage/dom_storage_types.h" | 18 #include "webkit/dom_storage/dom_storage_types.h" |
| 19 #include "webkit/dom_storage/local_storage_database_adapter.h" |
| 17 | 20 |
| 18 namespace dom_storage { | 21 namespace dom_storage { |
| 19 | 22 |
| 20 | 23 |
| 21 class DomStorageAreaTest : public testing::Test { | 24 class DomStorageAreaTest : public testing::Test { |
| 22 public: | 25 public: |
| 23 DomStorageAreaTest() | 26 DomStorageAreaTest() |
| 24 : kOrigin(GURL("http://dom_storage/")), | 27 : kOrigin(GURL("http://dom_storage/")), |
| 25 kKey(ASCIIToUTF16("key")), | 28 kKey(ASCIIToUTF16("key")), |
| 26 kValue(ASCIIToUTF16("value")), | 29 kValue(ASCIIToUTF16("value")), |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 158 } |
| 156 | 159 |
| 157 // This should set up a DomStorageArea that is correctly backed to disk. | 160 // This should set up a DomStorageArea that is correctly backed to disk. |
| 158 { | 161 { |
| 159 scoped_refptr<DomStorageArea> area( | 162 scoped_refptr<DomStorageArea> area( |
| 160 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 163 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 161 temp_dir.path(), | 164 temp_dir.path(), |
| 162 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 165 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 163 | 166 |
| 164 EXPECT_TRUE(area->backing_.get()); | 167 EXPECT_TRUE(area->backing_.get()); |
| 165 EXPECT_FALSE(area->backing_->IsOpen()); | 168 DomStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( |
| 169 area->backing_.get())->db_.get(); |
| 170 EXPECT_FALSE(database->IsOpen()); |
| 166 EXPECT_FALSE(area->is_initial_import_done_); | 171 EXPECT_FALSE(area->is_initial_import_done_); |
| 167 | 172 |
| 168 // Inject an in-memory db to speed up the test. | 173 // Inject an in-memory db to speed up the test. |
| 169 // We will verify that something is written into the database but not | 174 // We will verify that something is written into the database but not |
| 170 // that a file is written to disk - DOMStorageDatabase unit tests cover | 175 // that a file is written to disk - DOMStorageDatabase unit tests cover |
| 171 // that. | 176 // that. |
| 172 area->backing_.reset(new DomStorageDatabase()); | 177 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 173 | 178 |
| 174 // Need to write something to ensure that the database is created. | 179 // Need to write something to ensure that the database is created. |
| 175 NullableString16 old_value; | 180 NullableString16 old_value; |
| 176 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 181 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 177 ASSERT_TRUE(old_value.is_null()); | 182 ASSERT_TRUE(old_value.is_null()); |
| 178 EXPECT_TRUE(area->is_initial_import_done_); | 183 EXPECT_TRUE(area->is_initial_import_done_); |
| 179 EXPECT_TRUE(area->commit_batch_.get()); | 184 EXPECT_TRUE(area->commit_batch_.get()); |
| 180 EXPECT_EQ(0, area->commit_batches_in_flight_); | 185 EXPECT_EQ(0, area->commit_batches_in_flight_); |
| 181 | 186 |
| 182 MessageLoop::current()->RunAllPending(); | 187 MessageLoop::current()->RunAllPending(); |
| 183 | 188 |
| 184 EXPECT_FALSE(area->commit_batch_.get()); | 189 EXPECT_FALSE(area->commit_batch_.get()); |
| 185 EXPECT_EQ(0, area->commit_batches_in_flight_); | 190 EXPECT_EQ(0, area->commit_batches_in_flight_); |
| 186 EXPECT_TRUE(area->backing_->IsOpen()); | 191 database = static_cast<LocalStorageDatabaseAdapter*>( |
| 192 area->backing_.get())->db_.get(); |
| 193 EXPECT_TRUE(database->IsOpen()); |
| 187 EXPECT_EQ(1u, area->Length()); | 194 EXPECT_EQ(1u, area->Length()); |
| 188 EXPECT_EQ(kValue, area->GetItem(kKey).string()); | 195 EXPECT_EQ(kValue, area->GetItem(kKey).string()); |
| 189 | 196 |
| 190 // Verify the content made it to the in memory database. | 197 // Verify the content made it to the in memory database. |
| 191 ValuesMap values; | 198 ValuesMap values; |
| 192 area->backing_->ReadAllValues(&values); | 199 area->backing_->ReadAllValues(&values); |
| 193 EXPECT_EQ(1u, values.size()); | 200 EXPECT_EQ(1u, values.size()); |
| 194 EXPECT_EQ(kValue, values[kKey].string()); | 201 EXPECT_EQ(kValue, values[kKey].string()); |
| 195 } | 202 } |
| 196 } | 203 } |
| 197 | 204 |
| 198 TEST_F(DomStorageAreaTest, CommitTasks) { | 205 TEST_F(DomStorageAreaTest, CommitTasks) { |
| 199 ScopedTempDir temp_dir; | 206 ScopedTempDir temp_dir; |
| 200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 207 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 201 | 208 |
| 202 scoped_refptr<DomStorageArea> area( | 209 scoped_refptr<DomStorageArea> area( |
| 203 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 210 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 204 temp_dir.path(), | 211 temp_dir.path(), |
| 205 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 212 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 206 // Inject an in-memory db to speed up the test. | 213 // Inject an in-memory db to speed up the test. |
| 207 area->backing_.reset(new DomStorageDatabase()); | 214 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 208 | 215 |
| 209 // Unrelated to commits, but while we're here, see that querying Length() | 216 // Unrelated to commits, but while we're here, see that querying Length() |
| 210 // causes the backing database to be opened and presumably read from. | 217 // causes the backing database to be opened and presumably read from. |
| 211 EXPECT_FALSE(area->is_initial_import_done_); | 218 EXPECT_FALSE(area->is_initial_import_done_); |
| 212 EXPECT_EQ(0u, area->Length()); | 219 EXPECT_EQ(0u, area->Length()); |
| 213 EXPECT_TRUE(area->is_initial_import_done_); | 220 EXPECT_TRUE(area->is_initial_import_done_); |
| 214 | 221 |
| 215 ValuesMap values; | 222 ValuesMap values; |
| 216 NullableString16 old_value; | 223 NullableString16 old_value; |
| 217 | 224 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) { | 283 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) { |
| 277 ScopedTempDir temp_dir; | 284 ScopedTempDir temp_dir; |
| 278 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 285 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 279 scoped_refptr<DomStorageArea> area( | 286 scoped_refptr<DomStorageArea> area( |
| 280 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 287 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 281 temp_dir.path(), | 288 temp_dir.path(), |
| 282 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 289 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 283 | 290 |
| 284 // Inject an in-memory db to speed up the test and also to verify | 291 // Inject an in-memory db to speed up the test and also to verify |
| 285 // the final changes are commited in it's dtor. | 292 // the final changes are commited in it's dtor. |
| 286 area->backing_.reset(new VerifyChangesCommittedDatabase()); | 293 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
| 294 new VerifyChangesCommittedDatabase()); |
| 287 | 295 |
| 288 ValuesMap values; | 296 ValuesMap values; |
| 289 NullableString16 old_value; | 297 NullableString16 old_value; |
| 290 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 298 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 291 EXPECT_TRUE(area->HasUncommittedChanges()); | 299 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 292 area->backing_->ReadAllValues(&values); | 300 area->backing_->ReadAllValues(&values); |
| 293 EXPECT_TRUE(values.empty()); // not committed yet | 301 EXPECT_TRUE(values.empty()); // not committed yet |
| 294 area->Shutdown(); | 302 area->Shutdown(); |
| 295 MessageLoop::current()->RunAllPending(); | 303 MessageLoop::current()->RunAllPending(); |
| 296 EXPECT_TRUE(area->HasOneRef()); | 304 EXPECT_TRUE(area->HasOneRef()); |
| 297 EXPECT_FALSE(area->backing_.get()); | 305 EXPECT_FALSE(area->backing_.get()); |
| 298 // The VerifyChangesCommittedDatabase destructor verifies values | 306 // The VerifyChangesCommittedDatabase destructor verifies values |
| 299 // were committed. | 307 // were committed. |
| 300 } | 308 } |
| 301 | 309 |
| 302 TEST_F(DomStorageAreaTest, DeleteOrigin) { | 310 TEST_F(DomStorageAreaTest, DeleteOrigin) { |
| 303 ScopedTempDir temp_dir; | 311 ScopedTempDir temp_dir; |
| 304 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 312 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 305 scoped_refptr<DomStorageArea> area( | 313 scoped_refptr<DomStorageArea> area( |
| 306 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 314 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 307 temp_dir.path(), | 315 temp_dir.path(), |
| 308 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 316 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 309 | 317 |
| 310 // This test puts files on disk. | 318 // This test puts files on disk. |
| 311 FilePath db_file_path = area->backing_->file_path(); | 319 FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
| 320 area->backing_.get())->db_->file_path(); |
| 312 FilePath db_journal_file_path = | 321 FilePath db_journal_file_path = |
| 313 DomStorageDatabase::GetJournalFilePath(db_file_path); | 322 DomStorageDatabase::GetJournalFilePath(db_file_path); |
| 314 | 323 |
| 315 // Nothing bad should happen when invoked w/o any files on disk. | 324 // Nothing bad should happen when invoked w/o any files on disk. |
| 316 area->DeleteOrigin(); | 325 area->DeleteOrigin(); |
| 317 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 326 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
| 318 | 327 |
| 319 // Commit something in the database and then delete. | 328 // Commit something in the database and then delete. |
| 320 NullableString16 old_value; | 329 NullableString16 old_value; |
| 321 area->SetItem(kKey, kValue, &old_value); | 330 area->SetItem(kKey, kValue, &old_value); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 370 |
| 362 TEST_F(DomStorageAreaTest, PurgeMemory) { | 371 TEST_F(DomStorageAreaTest, PurgeMemory) { |
| 363 ScopedTempDir temp_dir; | 372 ScopedTempDir temp_dir; |
| 364 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 373 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 365 scoped_refptr<DomStorageArea> area( | 374 scoped_refptr<DomStorageArea> area( |
| 366 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 375 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 367 temp_dir.path(), | 376 temp_dir.path(), |
| 368 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 377 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 369 | 378 |
| 370 // Inject an in-memory db to speed up the test. | 379 // Inject an in-memory db to speed up the test. |
| 371 area->backing_.reset(new DomStorageDatabase()); | 380 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 372 | 381 |
| 373 // Unowned ptrs we use to verify that 'purge' has happened. | 382 // Unowned ptrs we use to verify that 'purge' has happened. |
| 374 DomStorageDatabase* original_backing = area->backing_.get(); | 383 DomStorageDatabase* original_backing = |
| 384 static_cast<LocalStorageDatabaseAdapter*>( |
| 385 area->backing_.get())->db_.get(); |
| 375 DomStorageMap* original_map = area->map_.get(); | 386 DomStorageMap* original_map = area->map_.get(); |
| 376 | 387 |
| 377 // Should do no harm when called on a newly constructed object. | 388 // Should do no harm when called on a newly constructed object. |
| 378 EXPECT_FALSE(area->is_initial_import_done_); | 389 EXPECT_FALSE(area->is_initial_import_done_); |
| 379 area->PurgeMemory(); | 390 area->PurgeMemory(); |
| 380 EXPECT_FALSE(area->is_initial_import_done_); | 391 EXPECT_FALSE(area->is_initial_import_done_); |
| 381 EXPECT_EQ(original_backing, area->backing_.get()); | 392 DomStorageDatabase* new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 393 area->backing_.get())->db_.get(); |
| 394 EXPECT_EQ(original_backing, new_backing); |
| 382 EXPECT_EQ(original_map, area->map_.get()); | 395 EXPECT_EQ(original_map, area->map_.get()); |
| 383 | 396 |
| 384 // Should not do anything when commits are pending. | 397 // Should not do anything when commits are pending. |
| 385 NullableString16 old_value; | 398 NullableString16 old_value; |
| 386 area->SetItem(kKey, kValue, &old_value); | 399 area->SetItem(kKey, kValue, &old_value); |
| 387 EXPECT_TRUE(area->is_initial_import_done_); | 400 EXPECT_TRUE(area->is_initial_import_done_); |
| 388 EXPECT_TRUE(area->HasUncommittedChanges()); | 401 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 389 area->PurgeMemory(); | 402 area->PurgeMemory(); |
| 390 EXPECT_TRUE(area->is_initial_import_done_); | 403 EXPECT_TRUE(area->is_initial_import_done_); |
| 391 EXPECT_TRUE(area->HasUncommittedChanges()); | 404 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 392 EXPECT_EQ(original_backing, area->backing_.get()); | 405 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 406 area->backing_.get())->db_.get(); |
| 407 EXPECT_EQ(original_backing, new_backing); |
| 393 EXPECT_EQ(original_map, area->map_.get()); | 408 EXPECT_EQ(original_map, area->map_.get()); |
| 394 | 409 |
| 395 // Commit the changes from above, | 410 // Commit the changes from above, |
| 396 MessageLoop::current()->RunAllPending(); | 411 MessageLoop::current()->RunAllPending(); |
| 397 EXPECT_FALSE(area->HasUncommittedChanges()); | 412 EXPECT_FALSE(area->HasUncommittedChanges()); |
| 398 EXPECT_EQ(original_backing, area->backing_.get()); | 413 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 414 area->backing_.get())->db_.get(); |
| 415 EXPECT_EQ(original_backing, new_backing); |
| 399 EXPECT_EQ(original_map, area->map_.get()); | 416 EXPECT_EQ(original_map, area->map_.get()); |
| 400 | 417 |
| 401 // Should drop caches and reset database connections | 418 // Should drop caches and reset database connections |
| 402 // when invoked on an area that's loaded up primed. | 419 // when invoked on an area that's loaded up primed. |
| 403 area->PurgeMemory(); | 420 area->PurgeMemory(); |
| 404 EXPECT_FALSE(area->is_initial_import_done_); | 421 EXPECT_FALSE(area->is_initial_import_done_); |
| 405 EXPECT_NE(original_backing, area->backing_.get()); | 422 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 423 area->backing_.get())->db_.get(); |
| 424 EXPECT_NE(original_backing, new_backing); |
| 406 EXPECT_NE(original_map, area->map_.get()); | 425 EXPECT_NE(original_map, area->map_.get()); |
| 407 } | 426 } |
| 408 | 427 |
| 409 TEST_F(DomStorageAreaTest, DatabaseFileNames) { | 428 TEST_F(DomStorageAreaTest, DatabaseFileNames) { |
| 410 struct { | 429 struct { |
| 411 const char* origin; | 430 const char* origin; |
| 412 const char* file_name; | 431 const char* file_name; |
| 413 const char* journal_file_name; | 432 const char* journal_file_name; |
| 414 } kCases[] = { | 433 } kCases[] = { |
| 415 { "https://www.google.com/", | 434 { "https://www.google.com/", |
| (...skipping 29 matching lines...) Expand all Loading... |
| 445 EXPECT_EQ( | 464 EXPECT_EQ( |
| 446 FilePath().AppendASCII("-journal"), | 465 FilePath().AppendASCII("-journal"), |
| 447 DomStorageDatabase::GetJournalFilePath(FilePath())); | 466 DomStorageDatabase::GetJournalFilePath(FilePath())); |
| 448 EXPECT_EQ( | 467 EXPECT_EQ( |
| 449 FilePath().AppendASCII(".extensiononly-journal"), | 468 FilePath().AppendASCII(".extensiononly-journal"), |
| 450 DomStorageDatabase::GetJournalFilePath( | 469 DomStorageDatabase::GetJournalFilePath( |
| 451 FilePath().AppendASCII(".extensiononly"))); | 470 FilePath().AppendASCII(".extensiononly"))); |
| 452 } | 471 } |
| 453 | 472 |
| 454 } // namespace dom_storage | 473 } // namespace dom_storage |
| OLD | NEW |