| 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" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 EXPECT_EQ(kValue, values[kKey].string()); | 63 EXPECT_EQ(kValue, values[kKey].string()); |
| 64 } | 64 } |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 MessageLoop message_loop_; | 68 MessageLoop message_loop_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) { | 71 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) { |
| 72 scoped_refptr<DomStorageArea> area( | 72 scoped_refptr<DomStorageArea> area( |
| 73 new DomStorageArea(1, kOrigin, FilePath(), NULL)); | 73 new DomStorageArea(1, std::string(), kOrigin, NULL)); |
| 74 string16 old_value; | 74 string16 old_value; |
| 75 NullableString16 old_nullable_value; | 75 NullableString16 old_nullable_value; |
| 76 scoped_refptr<DomStorageArea> copy; | 76 scoped_refptr<DomStorageArea> copy; |
| 77 | 77 |
| 78 // We don't focus on the underlying DomStorageMap functionality | 78 // We don't focus on the underlying DomStorageMap functionality |
| 79 // since that's covered by seperate unit tests. | 79 // since that's covered by seperate unit tests. |
| 80 EXPECT_EQ(kOrigin, area->origin()); | 80 EXPECT_EQ(kOrigin, area->origin()); |
| 81 EXPECT_EQ(1, area->namespace_id()); | 81 EXPECT_EQ(1, area->namespace_id()); |
| 82 EXPECT_EQ(0u, area->Length()); | 82 EXPECT_EQ(0u, area->Length()); |
| 83 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); | 83 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 84 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_nullable_value)); | 84 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_nullable_value)); |
| 85 EXPECT_FALSE(area->HasUncommittedChanges()); | 85 EXPECT_FALSE(area->HasUncommittedChanges()); |
| 86 | 86 |
| 87 // Verify that a copy shares the same map. | 87 // Verify that a copy shares the same map. |
| 88 copy = area->ShallowCopy(2); | 88 copy = area->ShallowCopy(2, std::string()); |
| 89 EXPECT_EQ(kOrigin, copy->origin()); | 89 EXPECT_EQ(kOrigin, copy->origin()); |
| 90 EXPECT_EQ(2, copy->namespace_id()); | 90 EXPECT_EQ(2, copy->namespace_id()); |
| 91 EXPECT_EQ(area->Length(), copy->Length()); | 91 EXPECT_EQ(area->Length(), copy->Length()); |
| 92 EXPECT_EQ(area->GetItem(kKey).string(), copy->GetItem(kKey).string()); | 92 EXPECT_EQ(area->GetItem(kKey).string(), copy->GetItem(kKey).string()); |
| 93 EXPECT_EQ(area->Key(0).string(), copy->Key(0).string()); | 93 EXPECT_EQ(area->Key(0).string(), copy->Key(0).string()); |
| 94 EXPECT_EQ(copy->map_.get(), area->map_.get()); | 94 EXPECT_EQ(copy->map_.get(), area->map_.get()); |
| 95 | 95 |
| 96 // But will deep copy-on-write as needed. | 96 // But will deep copy-on-write as needed. |
| 97 EXPECT_TRUE(area->RemoveItem(kKey, &old_value)); | 97 EXPECT_TRUE(area->RemoveItem(kKey, &old_value)); |
| 98 EXPECT_NE(copy->map_.get(), area->map_.get()); | 98 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 99 copy = area->ShallowCopy(2); | 99 copy = area->ShallowCopy(2, std::string()); |
| 100 EXPECT_EQ(copy->map_.get(), area->map_.get()); | 100 EXPECT_EQ(copy->map_.get(), area->map_.get()); |
| 101 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); | 101 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 102 EXPECT_NE(copy->map_.get(), area->map_.get()); | 102 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 103 copy = area->ShallowCopy(2); | 103 copy = area->ShallowCopy(2, std::string()); |
| 104 EXPECT_EQ(copy->map_.get(), area->map_.get()); | 104 EXPECT_EQ(copy->map_.get(), area->map_.get()); |
| 105 EXPECT_NE(0u, area->Length()); | 105 EXPECT_NE(0u, area->Length()); |
| 106 EXPECT_TRUE(area->Clear()); | 106 EXPECT_TRUE(area->Clear()); |
| 107 EXPECT_EQ(0u, area->Length()); | 107 EXPECT_EQ(0u, area->Length()); |
| 108 EXPECT_NE(copy->map_.get(), area->map_.get()); | 108 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 109 | 109 |
| 110 // Verify that once Shutdown(), behaves that way. | 110 // Verify that once Shutdown(), behaves that way. |
| 111 area->Shutdown(); | 111 area->Shutdown(); |
| 112 EXPECT_TRUE(area->is_shutdown_); | 112 EXPECT_TRUE(area->is_shutdown_); |
| 113 EXPECT_FALSE(area->map_.get()); | 113 EXPECT_FALSE(area->map_.get()); |
| 114 EXPECT_EQ(0u, area->Length()); | 114 EXPECT_EQ(0u, area->Length()); |
| 115 EXPECT_TRUE(area->Key(0).is_null()); | 115 EXPECT_TRUE(area->Key(0).is_null()); |
| 116 EXPECT_TRUE(area->GetItem(kKey).is_null()); | 116 EXPECT_TRUE(area->GetItem(kKey).is_null()); |
| 117 EXPECT_FALSE(area->SetItem(kKey, kValue, &old_nullable_value)); | 117 EXPECT_FALSE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 118 EXPECT_FALSE(area->RemoveItem(kKey, &old_value)); | 118 EXPECT_FALSE(area->RemoveItem(kKey, &old_value)); |
| 119 EXPECT_FALSE(area->Clear()); | 119 EXPECT_FALSE(area->Clear()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(DomStorageAreaTest, BackingDatabaseOpened) { | 122 TEST_F(DomStorageAreaTest, BackingDatabaseOpened) { |
| 123 const int64 kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1; | 123 const int64 kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1; |
| 124 ScopedTempDir temp_dir; | 124 ScopedTempDir temp_dir; |
| 125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 125 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 126 const FilePath kExpectedOriginFilePath = temp_dir.path().Append( | 126 const FilePath kExpectedOriginFilePath = temp_dir.path().Append( |
| 127 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin)); | 127 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin)); |
| 128 | 128 |
| 129 // No directory, backing should be null. | 129 // No directory, backing should be null. |
| 130 { | 130 { |
| 131 scoped_refptr<DomStorageArea> area( | 131 scoped_refptr<DomStorageArea> area( |
| 132 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, FilePath(), | 132 new DomStorageArea(kOrigin, FilePath(), NULL)); |
| 133 NULL)); | |
| 134 EXPECT_EQ(NULL, area->backing_.get()); | 133 EXPECT_EQ(NULL, area->backing_.get()); |
| 135 EXPECT_TRUE(area->is_initial_import_done_); | 134 EXPECT_TRUE(area->is_initial_import_done_); |
| 136 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); | 135 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); |
| 137 } | 136 } |
| 138 | 137 |
| 139 // Valid directory and origin but non-local namespace id. Backing should | 138 // Valid directory and origin but non-local namespace id. Backing should |
| 140 // be null. | 139 // be null. |
| 141 { | 140 { |
| 142 scoped_refptr<DomStorageArea> area( | 141 scoped_refptr<DomStorageArea> area( |
| 143 new DomStorageArea(kSessionStorageNamespaceId, kOrigin, | 142 new DomStorageArea(kSessionStorageNamespaceId, std::string(), kOrigin, |
| 144 temp_dir.path(), NULL)); | 143 NULL)); |
| 145 EXPECT_EQ(NULL, area->backing_.get()); | 144 EXPECT_EQ(NULL, area->backing_.get()); |
| 146 EXPECT_TRUE(area->is_initial_import_done_); | 145 EXPECT_TRUE(area->is_initial_import_done_); |
| 147 | 146 |
| 148 NullableString16 old_value; | 147 NullableString16 old_value; |
| 149 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 148 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 150 ASSERT_TRUE(old_value.is_null()); | 149 ASSERT_TRUE(old_value.is_null()); |
| 151 | 150 |
| 152 // Check that saving a value has still left us without a backing database. | 151 // Check that saving a value has still left us without a backing database. |
| 153 EXPECT_EQ(NULL, area->backing_.get()); | 152 EXPECT_EQ(NULL, area->backing_.get()); |
| 154 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); | 153 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); |
| 155 } | 154 } |
| 156 | 155 |
| 157 // This should set up a DomStorageArea that is correctly backed to disk. | 156 // This should set up a DomStorageArea that is correctly backed to disk. |
| 158 { | 157 { |
| 159 scoped_refptr<DomStorageArea> area( | 158 scoped_refptr<DomStorageArea> area( |
| 160 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 159 new DomStorageArea(kOrigin, |
| 161 temp_dir.path(), | 160 temp_dir.path(), |
| 162 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 161 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 163 | 162 |
| 164 EXPECT_TRUE(area->backing_.get()); | 163 EXPECT_TRUE(area->backing_.get()); |
| 165 EXPECT_FALSE(area->backing_->IsOpen()); | 164 EXPECT_FALSE(area->backing_->IsOpen()); |
| 166 EXPECT_FALSE(area->is_initial_import_done_); | 165 EXPECT_FALSE(area->is_initial_import_done_); |
| 167 | 166 |
| 168 // Inject an in-memory db to speed up the test. | 167 // Inject an in-memory db to speed up the test. |
| 169 // We will verify that something is written into the database but not | 168 // We will verify that something is written into the database but not |
| 170 // that a file is written to disk - DOMStorageDatabase unit tests cover | 169 // that a file is written to disk - DOMStorageDatabase unit tests cover |
| (...skipping 22 matching lines...) Expand all Loading... |
| 193 EXPECT_EQ(1u, values.size()); | 192 EXPECT_EQ(1u, values.size()); |
| 194 EXPECT_EQ(kValue, values[kKey].string()); | 193 EXPECT_EQ(kValue, values[kKey].string()); |
| 195 } | 194 } |
| 196 } | 195 } |
| 197 | 196 |
| 198 TEST_F(DomStorageAreaTest, CommitTasks) { | 197 TEST_F(DomStorageAreaTest, CommitTasks) { |
| 199 ScopedTempDir temp_dir; | 198 ScopedTempDir temp_dir; |
| 200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 199 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 201 | 200 |
| 202 scoped_refptr<DomStorageArea> area( | 201 scoped_refptr<DomStorageArea> area( |
| 203 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 202 new DomStorageArea(kOrigin, |
| 204 temp_dir.path(), | 203 temp_dir.path(), |
| 205 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 204 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 206 // Inject an in-memory db to speed up the test. | 205 // Inject an in-memory db to speed up the test. |
| 207 area->backing_.reset(new DomStorageDatabase()); | 206 area->backing_.reset(new DomStorageDatabase()); |
| 208 | 207 |
| 209 // Unrelated to commits, but while we're here, see that querying Length() | 208 // Unrelated to commits, but while we're here, see that querying Length() |
| 210 // causes the backing database to be opened and presumably read from. | 209 // causes the backing database to be opened and presumably read from. |
| 211 EXPECT_FALSE(area->is_initial_import_done_); | 210 EXPECT_FALSE(area->is_initial_import_done_); |
| 212 EXPECT_EQ(0u, area->Length()); | 211 EXPECT_EQ(0u, area->Length()); |
| 213 EXPECT_TRUE(area->is_initial_import_done_); | 212 EXPECT_TRUE(area->is_initial_import_done_); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 area->backing_->ReadAllValues(&values); | 269 area->backing_->ReadAllValues(&values); |
| 271 EXPECT_EQ(2u, values.size()); | 270 EXPECT_EQ(2u, values.size()); |
| 272 EXPECT_EQ(kValue, values[kKey].string()); | 271 EXPECT_EQ(kValue, values[kKey].string()); |
| 273 EXPECT_EQ(kValue2, values[kKey2].string()); | 272 EXPECT_EQ(kValue2, values[kKey2].string()); |
| 274 } | 273 } |
| 275 | 274 |
| 276 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) { | 275 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) { |
| 277 ScopedTempDir temp_dir; | 276 ScopedTempDir temp_dir; |
| 278 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 277 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 279 scoped_refptr<DomStorageArea> area( | 278 scoped_refptr<DomStorageArea> area( |
| 280 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 279 new DomStorageArea(kOrigin, |
| 281 temp_dir.path(), | 280 temp_dir.path(), |
| 282 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 281 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 283 | 282 |
| 284 // Inject an in-memory db to speed up the test and also to verify | 283 // Inject an in-memory db to speed up the test and also to verify |
| 285 // the final changes are commited in it's dtor. | 284 // the final changes are commited in it's dtor. |
| 286 area->backing_.reset(new VerifyChangesCommittedDatabase()); | 285 area->backing_.reset(new VerifyChangesCommittedDatabase()); |
| 287 | 286 |
| 288 ValuesMap values; | 287 ValuesMap values; |
| 289 NullableString16 old_value; | 288 NullableString16 old_value; |
| 290 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 289 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 291 EXPECT_TRUE(area->HasUncommittedChanges()); | 290 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 292 area->backing_->ReadAllValues(&values); | 291 area->backing_->ReadAllValues(&values); |
| 293 EXPECT_TRUE(values.empty()); // not committed yet | 292 EXPECT_TRUE(values.empty()); // not committed yet |
| 294 area->Shutdown(); | 293 area->Shutdown(); |
| 295 MessageLoop::current()->RunAllPending(); | 294 MessageLoop::current()->RunAllPending(); |
| 296 EXPECT_TRUE(area->HasOneRef()); | 295 EXPECT_TRUE(area->HasOneRef()); |
| 297 EXPECT_FALSE(area->backing_.get()); | 296 EXPECT_FALSE(area->backing_.get()); |
| 298 // The VerifyChangesCommittedDatabase destructor verifies values | 297 // The VerifyChangesCommittedDatabase destructor verifies values |
| 299 // were committed. | 298 // were committed. |
| 300 } | 299 } |
| 301 | 300 |
| 302 TEST_F(DomStorageAreaTest, DeleteOrigin) { | 301 TEST_F(DomStorageAreaTest, DeleteOrigin) { |
| 303 ScopedTempDir temp_dir; | 302 ScopedTempDir temp_dir; |
| 304 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 303 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 305 scoped_refptr<DomStorageArea> area( | 304 scoped_refptr<DomStorageArea> area( |
| 306 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 305 new DomStorageArea(kOrigin, |
| 307 temp_dir.path(), | 306 temp_dir.path(), |
| 308 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 307 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 309 | 308 |
| 310 // This test puts files on disk. | 309 // This test puts files on disk. |
| 311 FilePath db_file_path = area->backing_->file_path(); | 310 FilePath db_file_path = area->backing_->file_path(); |
| 312 FilePath db_journal_file_path = | 311 FilePath db_journal_file_path = |
| 313 DomStorageDatabase::GetJournalFilePath(db_file_path); | 312 DomStorageDatabase::GetJournalFilePath(db_file_path); |
| 314 | 313 |
| 315 // Nothing bad should happen when invoked w/o any files on disk. | 314 // Nothing bad should happen when invoked w/o any files on disk. |
| 316 area->DeleteOrigin(); | 315 area->DeleteOrigin(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 355 EXPECT_TRUE(file_util::PathExists(db_file_path)); |
| 357 area->Shutdown(); | 356 area->Shutdown(); |
| 358 MessageLoop::current()->RunAllPending(); | 357 MessageLoop::current()->RunAllPending(); |
| 359 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 358 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
| 360 } | 359 } |
| 361 | 360 |
| 362 TEST_F(DomStorageAreaTest, PurgeMemory) { | 361 TEST_F(DomStorageAreaTest, PurgeMemory) { |
| 363 ScopedTempDir temp_dir; | 362 ScopedTempDir temp_dir; |
| 364 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 365 scoped_refptr<DomStorageArea> area( | 364 scoped_refptr<DomStorageArea> area( |
| 366 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, | 365 new DomStorageArea(kOrigin, |
| 367 temp_dir.path(), | 366 temp_dir.path(), |
| 368 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 367 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 369 | 368 |
| 370 // Inject an in-memory db to speed up the test. | 369 // Inject an in-memory db to speed up the test. |
| 371 area->backing_.reset(new DomStorageDatabase()); | 370 area->backing_.reset(new DomStorageDatabase()); |
| 372 | 371 |
| 373 // Unowned ptrs we use to verify that 'purge' has happened. | 372 // Unowned ptrs we use to verify that 'purge' has happened. |
| 374 DomStorageDatabase* original_backing = area->backing_.get(); | 373 DomStorageDatabase* original_backing = area->backing_.get(); |
| 375 DomStorageMap* original_map = area->map_.get(); | 374 DomStorageMap* original_map = area->map_.get(); |
| 376 | 375 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 EXPECT_EQ( | 444 EXPECT_EQ( |
| 446 FilePath().AppendASCII("-journal"), | 445 FilePath().AppendASCII("-journal"), |
| 447 DomStorageDatabase::GetJournalFilePath(FilePath())); | 446 DomStorageDatabase::GetJournalFilePath(FilePath())); |
| 448 EXPECT_EQ( | 447 EXPECT_EQ( |
| 449 FilePath().AppendASCII(".extensiononly-journal"), | 448 FilePath().AppendASCII(".extensiononly-journal"), |
| 450 DomStorageDatabase::GetJournalFilePath( | 449 DomStorageDatabase::GetJournalFilePath( |
| 451 FilePath().AppendASCII(".extensiononly"))); | 450 FilePath().AppendASCII(".extensiononly"))); |
| 452 } | 451 } |
| 453 | 452 |
| 454 } // namespace dom_storage | 453 } // namespace dom_storage |
| OLD | NEW |