| 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/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // Method used in the CommitTasks test case. | 40 // Method used in the CommitTasks test case. |
| 41 void InjectedCommitSequencingTask(DomStorageArea* area) { | 41 void InjectedCommitSequencingTask(DomStorageArea* area) { |
| 42 // At this point the OnCommitTimer has run. | 42 // At this point the OnCommitTimer has run. |
| 43 // Verify that it put a commit in flight. | 43 // Verify that it put a commit in flight. |
| 44 EXPECT_EQ(1, area->commit_batches_in_flight_); | 44 EXPECT_EQ(1, area->commit_batches_in_flight_); |
| 45 EXPECT_FALSE(area->commit_batch_.get()); | 45 EXPECT_FALSE(area->commit_batch_.get()); |
| 46 EXPECT_TRUE(area->HasUncommittedChanges()); | 46 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 47 // Make additional change and verify that a new commit batch | 47 // Make additional change and verify that a new commit batch |
| 48 // is created for that change. | 48 // is created for that change. |
| 49 NullableString16 old_value; | 49 base::NullableString16 old_value; |
| 50 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); | 50 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); |
| 51 EXPECT_TRUE(area->commit_batch_.get()); | 51 EXPECT_TRUE(area->commit_batch_.get()); |
| 52 EXPECT_EQ(1, area->commit_batches_in_flight_); | 52 EXPECT_EQ(1, area->commit_batches_in_flight_); |
| 53 EXPECT_TRUE(area->HasUncommittedChanges()); | 53 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Class used in the CommitChangesAtShutdown test case. | 56 // Class used in the CommitChangesAtShutdown test case. |
| 57 class VerifyChangesCommittedDatabase : public DomStorageDatabase { | 57 class VerifyChangesCommittedDatabase : public DomStorageDatabase { |
| 58 public: | 58 public: |
| 59 VerifyChangesCommittedDatabase() {} | 59 VerifyChangesCommittedDatabase() {} |
| 60 virtual ~VerifyChangesCommittedDatabase() { | 60 virtual ~VerifyChangesCommittedDatabase() { |
| 61 const base::string16 kKey(ASCIIToUTF16("key")); | 61 const base::string16 kKey(ASCIIToUTF16("key")); |
| 62 const base::string16 kValue(ASCIIToUTF16("value")); | 62 const base::string16 kValue(ASCIIToUTF16("value")); |
| 63 ValuesMap values; | 63 ValuesMap values; |
| 64 ReadAllValues(&values); | 64 ReadAllValues(&values); |
| 65 EXPECT_EQ(1u, values.size()); | 65 EXPECT_EQ(1u, values.size()); |
| 66 EXPECT_EQ(kValue, values[kKey].string()); | 66 EXPECT_EQ(kValue, values[kKey].string()); |
| 67 } | 67 } |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 private: | 70 private: |
| 71 base::MessageLoop message_loop_; | 71 base::MessageLoop message_loop_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) { | 74 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) { |
| 75 scoped_refptr<DomStorageArea> area( | 75 scoped_refptr<DomStorageArea> area( |
| 76 new DomStorageArea(1, std::string(), kOrigin, NULL, NULL)); | 76 new DomStorageArea(1, std::string(), kOrigin, NULL, NULL)); |
| 77 base::string16 old_value; | 77 base::string16 old_value; |
| 78 NullableString16 old_nullable_value; | 78 base::NullableString16 old_nullable_value; |
| 79 scoped_refptr<DomStorageArea> copy; | 79 scoped_refptr<DomStorageArea> copy; |
| 80 | 80 |
| 81 // We don't focus on the underlying DomStorageMap functionality | 81 // We don't focus on the underlying DomStorageMap functionality |
| 82 // since that's covered by seperate unit tests. | 82 // since that's covered by seperate unit tests. |
| 83 EXPECT_EQ(kOrigin, area->origin()); | 83 EXPECT_EQ(kOrigin, area->origin()); |
| 84 EXPECT_EQ(1, area->namespace_id()); | 84 EXPECT_EQ(1, area->namespace_id()); |
| 85 EXPECT_EQ(0u, area->Length()); | 85 EXPECT_EQ(0u, area->Length()); |
| 86 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); | 86 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 87 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_nullable_value)); | 87 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_nullable_value)); |
| 88 EXPECT_FALSE(area->HasUncommittedChanges()); | 88 EXPECT_FALSE(area->HasUncommittedChanges()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 140 |
| 141 // Valid directory and origin but no session storage backing. Backing should | 141 // Valid directory and origin but no session storage backing. Backing should |
| 142 // be null. | 142 // be null. |
| 143 { | 143 { |
| 144 scoped_refptr<DomStorageArea> area( | 144 scoped_refptr<DomStorageArea> area( |
| 145 new DomStorageArea(kSessionStorageNamespaceId, std::string(), kOrigin, | 145 new DomStorageArea(kSessionStorageNamespaceId, std::string(), kOrigin, |
| 146 NULL, NULL)); | 146 NULL, NULL)); |
| 147 EXPECT_EQ(NULL, area->backing_.get()); | 147 EXPECT_EQ(NULL, area->backing_.get()); |
| 148 EXPECT_TRUE(area->is_initial_import_done_); | 148 EXPECT_TRUE(area->is_initial_import_done_); |
| 149 | 149 |
| 150 NullableString16 old_value; | 150 base::NullableString16 old_value; |
| 151 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 151 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 152 ASSERT_TRUE(old_value.is_null()); | 152 ASSERT_TRUE(old_value.is_null()); |
| 153 | 153 |
| 154 // Check that saving a value has still left us without a backing database. | 154 // Check that saving a value has still left us without a backing database. |
| 155 EXPECT_EQ(NULL, area->backing_.get()); | 155 EXPECT_EQ(NULL, area->backing_.get()); |
| 156 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); | 156 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); |
| 157 } | 157 } |
| 158 | 158 |
| 159 // 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. |
| 160 { | 160 { |
| 161 scoped_refptr<DomStorageArea> area(new DomStorageArea( | 161 scoped_refptr<DomStorageArea> area(new DomStorageArea( |
| 162 kOrigin, | 162 kOrigin, |
| 163 temp_dir.path(), | 163 temp_dir.path(), |
| 164 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 164 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
| 165 | 165 |
| 166 EXPECT_TRUE(area->backing_.get()); | 166 EXPECT_TRUE(area->backing_.get()); |
| 167 DomStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( | 167 DomStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( |
| 168 area->backing_.get())->db_.get(); | 168 area->backing_.get())->db_.get(); |
| 169 EXPECT_FALSE(database->IsOpen()); | 169 EXPECT_FALSE(database->IsOpen()); |
| 170 EXPECT_FALSE(area->is_initial_import_done_); | 170 EXPECT_FALSE(area->is_initial_import_done_); |
| 171 | 171 |
| 172 // Inject an in-memory db to speed up the test. | 172 // Inject an in-memory db to speed up the test. |
| 173 // 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 |
| 174 // that a file is written to disk - DOMStorageDatabase unit tests cover | 174 // that a file is written to disk - DOMStorageDatabase unit tests cover |
| 175 // that. | 175 // that. |
| 176 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 176 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 177 | 177 |
| 178 // Need to write something to ensure that the database is created. | 178 // Need to write something to ensure that the database is created. |
| 179 NullableString16 old_value; | 179 base::NullableString16 old_value; |
| 180 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 180 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 181 ASSERT_TRUE(old_value.is_null()); | 181 ASSERT_TRUE(old_value.is_null()); |
| 182 EXPECT_TRUE(area->is_initial_import_done_); | 182 EXPECT_TRUE(area->is_initial_import_done_); |
| 183 EXPECT_TRUE(area->commit_batch_.get()); | 183 EXPECT_TRUE(area->commit_batch_.get()); |
| 184 EXPECT_EQ(0, area->commit_batches_in_flight_); | 184 EXPECT_EQ(0, area->commit_batches_in_flight_); |
| 185 | 185 |
| 186 base::MessageLoop::current()->RunUntilIdle(); | 186 base::MessageLoop::current()->RunUntilIdle(); |
| 187 | 187 |
| 188 EXPECT_FALSE(area->commit_batch_.get()); | 188 EXPECT_FALSE(area->commit_batch_.get()); |
| 189 EXPECT_EQ(0, area->commit_batches_in_flight_); | 189 EXPECT_EQ(0, area->commit_batches_in_flight_); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 212 // Inject an in-memory db to speed up the test. | 212 // Inject an in-memory db to speed up the test. |
| 213 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 213 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 214 | 214 |
| 215 // 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() |
| 216 // causes the backing database to be opened and presumably read from. | 216 // causes the backing database to be opened and presumably read from. |
| 217 EXPECT_FALSE(area->is_initial_import_done_); | 217 EXPECT_FALSE(area->is_initial_import_done_); |
| 218 EXPECT_EQ(0u, area->Length()); | 218 EXPECT_EQ(0u, area->Length()); |
| 219 EXPECT_TRUE(area->is_initial_import_done_); | 219 EXPECT_TRUE(area->is_initial_import_done_); |
| 220 | 220 |
| 221 ValuesMap values; | 221 ValuesMap values; |
| 222 NullableString16 old_value; | 222 base::NullableString16 old_value; |
| 223 | 223 |
| 224 // See that changes are batched up. | 224 // See that changes are batched up. |
| 225 EXPECT_FALSE(area->commit_batch_.get()); | 225 EXPECT_FALSE(area->commit_batch_.get()); |
| 226 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 226 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 227 EXPECT_TRUE(area->HasUncommittedChanges()); | 227 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 228 EXPECT_TRUE(area->commit_batch_.get()); | 228 EXPECT_TRUE(area->commit_batch_.get()); |
| 229 EXPECT_FALSE(area->commit_batch_->clear_all_first); | 229 EXPECT_FALSE(area->commit_batch_->clear_all_first); |
| 230 EXPECT_EQ(1u, area->commit_batch_->changed_values.size()); | 230 EXPECT_EQ(1u, area->commit_batch_->changed_values.size()); |
| 231 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); | 231 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); |
| 232 EXPECT_TRUE(area->commit_batch_.get()); | 232 EXPECT_TRUE(area->commit_batch_.get()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 kOrigin, | 287 kOrigin, |
| 288 temp_dir.path(), | 288 temp_dir.path(), |
| 289 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 289 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
| 290 | 290 |
| 291 // 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 |
| 292 // the final changes are commited in it's dtor. | 292 // the final changes are commited in it's dtor. |
| 293 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | 293 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
| 294 new VerifyChangesCommittedDatabase()); | 294 new VerifyChangesCommittedDatabase()); |
| 295 | 295 |
| 296 ValuesMap values; | 296 ValuesMap values; |
| 297 NullableString16 old_value; | 297 base::NullableString16 old_value; |
| 298 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 298 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 299 EXPECT_TRUE(area->HasUncommittedChanges()); | 299 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 300 area->backing_->ReadAllValues(&values); | 300 area->backing_->ReadAllValues(&values); |
| 301 EXPECT_TRUE(values.empty()); // not committed yet | 301 EXPECT_TRUE(values.empty()); // not committed yet |
| 302 area->Shutdown(); | 302 area->Shutdown(); |
| 303 base::MessageLoop::current()->RunUntilIdle(); | 303 base::MessageLoop::current()->RunUntilIdle(); |
| 304 EXPECT_TRUE(area->HasOneRef()); | 304 EXPECT_TRUE(area->HasOneRef()); |
| 305 EXPECT_FALSE(area->backing_.get()); | 305 EXPECT_FALSE(area->backing_.get()); |
| 306 // The VerifyChangesCommittedDatabase destructor verifies values | 306 // The VerifyChangesCommittedDatabase destructor verifies values |
| 307 // were committed. | 307 // were committed. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 319 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 319 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
| 320 area->backing_.get())->db_->file_path(); | 320 area->backing_.get())->db_->file_path(); |
| 321 base::FilePath db_journal_file_path = | 321 base::FilePath db_journal_file_path = |
| 322 DomStorageDatabase::GetJournalFilePath(db_file_path); | 322 DomStorageDatabase::GetJournalFilePath(db_file_path); |
| 323 | 323 |
| 324 // 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. |
| 325 area->DeleteOrigin(); | 325 area->DeleteOrigin(); |
| 326 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 326 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
| 327 | 327 |
| 328 // Commit something in the database and then delete. | 328 // Commit something in the database and then delete. |
| 329 NullableString16 old_value; | 329 base::NullableString16 old_value; |
| 330 area->SetItem(kKey, kValue, &old_value); | 330 area->SetItem(kKey, kValue, &old_value); |
| 331 base::MessageLoop::current()->RunUntilIdle(); | 331 base::MessageLoop::current()->RunUntilIdle(); |
| 332 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 332 EXPECT_TRUE(file_util::PathExists(db_file_path)); |
| 333 area->DeleteOrigin(); | 333 area->DeleteOrigin(); |
| 334 EXPECT_EQ(0u, area->Length()); | 334 EXPECT_EQ(0u, area->Length()); |
| 335 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 335 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
| 336 EXPECT_FALSE(file_util::PathExists(db_journal_file_path)); | 336 EXPECT_FALSE(file_util::PathExists(db_journal_file_path)); |
| 337 | 337 |
| 338 // Put some uncommitted changes to a non-existing database in | 338 // Put some uncommitted changes to a non-existing database in |
| 339 // and then delete. No file ever gets created in this case. | 339 // and then delete. No file ever gets created in this case. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 // Should do no harm when called on a newly constructed object. | 388 // Should do no harm when called on a newly constructed object. |
| 389 EXPECT_FALSE(area->is_initial_import_done_); | 389 EXPECT_FALSE(area->is_initial_import_done_); |
| 390 area->PurgeMemory(); | 390 area->PurgeMemory(); |
| 391 EXPECT_FALSE(area->is_initial_import_done_); | 391 EXPECT_FALSE(area->is_initial_import_done_); |
| 392 DomStorageDatabase* new_backing = static_cast<LocalStorageDatabaseAdapter*>( | 392 DomStorageDatabase* new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 393 area->backing_.get())->db_.get(); | 393 area->backing_.get())->db_.get(); |
| 394 EXPECT_EQ(original_backing, new_backing); | 394 EXPECT_EQ(original_backing, new_backing); |
| 395 EXPECT_EQ(original_map, area->map_.get()); | 395 EXPECT_EQ(original_map, area->map_.get()); |
| 396 | 396 |
| 397 // Should not do anything when commits are pending. | 397 // Should not do anything when commits are pending. |
| 398 NullableString16 old_value; | 398 base::NullableString16 old_value; |
| 399 area->SetItem(kKey, kValue, &old_value); | 399 area->SetItem(kKey, kValue, &old_value); |
| 400 EXPECT_TRUE(area->is_initial_import_done_); | 400 EXPECT_TRUE(area->is_initial_import_done_); |
| 401 EXPECT_TRUE(area->HasUncommittedChanges()); | 401 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 402 area->PurgeMemory(); | 402 area->PurgeMemory(); |
| 403 EXPECT_TRUE(area->is_initial_import_done_); | 403 EXPECT_TRUE(area->is_initial_import_done_); |
| 404 EXPECT_TRUE(area->HasUncommittedChanges()); | 404 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 405 new_backing = static_cast<LocalStorageDatabaseAdapter*>( | 405 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
| 406 area->backing_.get())->db_.get(); | 406 area->backing_.get())->db_.get(); |
| 407 EXPECT_EQ(original_backing, new_backing); | 407 EXPECT_EQ(original_backing, new_backing); |
| 408 EXPECT_EQ(original_map, area->map_.get()); | 408 EXPECT_EQ(original_map, area->map_.get()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 EXPECT_EQ( | 464 EXPECT_EQ( |
| 465 base::FilePath().AppendASCII("-journal"), | 465 base::FilePath().AppendASCII("-journal"), |
| 466 DomStorageDatabase::GetJournalFilePath(base::FilePath())); | 466 DomStorageDatabase::GetJournalFilePath(base::FilePath())); |
| 467 EXPECT_EQ( | 467 EXPECT_EQ( |
| 468 base::FilePath().AppendASCII(".extensiononly-journal"), | 468 base::FilePath().AppendASCII(".extensiononly-journal"), |
| 469 DomStorageDatabase::GetJournalFilePath( | 469 DomStorageDatabase::GetJournalFilePath( |
| 470 base::FilePath().AppendASCII(".extensiononly"))); | 470 base::FilePath().AppendASCII(".extensiononly"))); |
| 471 } | 471 } |
| 472 | 472 |
| 473 } // namespace dom_storage | 473 } // namespace dom_storage |
| OLD | NEW |