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