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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
129 const base::FilePath kExpectedOriginFilePath = temp_dir.path().Append( | 129 const base::FilePath kExpectedOriginFilePath = temp_dir.path().Append( |
130 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin)); | 130 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin)); |
131 | 131 |
132 // No directory, backing should be null. | 132 // No directory, backing should be null. |
133 { | 133 { |
134 scoped_refptr<DomStorageArea> area( | 134 scoped_refptr<DomStorageArea> area( |
135 new DomStorageArea(kOrigin, base::FilePath(), NULL)); | 135 new DomStorageArea(kOrigin, base::FilePath(), NULL)); |
136 EXPECT_EQ(NULL, area->backing_.get()); | 136 EXPECT_EQ(NULL, area->backing_.get()); |
137 EXPECT_TRUE(area->is_initial_import_done_); | 137 EXPECT_TRUE(area->is_initial_import_done_); |
138 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); | 138 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); |
139 } | 139 } |
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 base::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(base::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()); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 316 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
317 | 317 |
318 // This test puts files on disk. | 318 // This test puts files on disk. |
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(base::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 base::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(base::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(base::PathExists(db_file_path)); |
336 EXPECT_FALSE(file_util::PathExists(db_journal_file_path)); | 336 EXPECT_FALSE(base::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. |
340 area->SetItem(kKey, kValue, &old_value); | 340 area->SetItem(kKey, kValue, &old_value); |
341 EXPECT_TRUE(area->HasUncommittedChanges()); | 341 EXPECT_TRUE(area->HasUncommittedChanges()); |
342 EXPECT_EQ(1u, area->Length()); | 342 EXPECT_EQ(1u, area->Length()); |
343 area->DeleteOrigin(); | 343 area->DeleteOrigin(); |
344 EXPECT_TRUE(area->HasUncommittedChanges()); | 344 EXPECT_TRUE(area->HasUncommittedChanges()); |
345 EXPECT_EQ(0u, area->Length()); | 345 EXPECT_EQ(0u, area->Length()); |
346 base::MessageLoop::current()->RunUntilIdle(); | 346 base::MessageLoop::current()->RunUntilIdle(); |
347 EXPECT_FALSE(area->HasUncommittedChanges()); | 347 EXPECT_FALSE(area->HasUncommittedChanges()); |
348 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 348 EXPECT_FALSE(base::PathExists(db_file_path)); |
349 | 349 |
350 // Put some uncommitted changes to a an existing database in | 350 // Put some uncommitted changes to a an existing database in |
351 // and then delete. | 351 // and then delete. |
352 area->SetItem(kKey, kValue, &old_value); | 352 area->SetItem(kKey, kValue, &old_value); |
353 base::MessageLoop::current()->RunUntilIdle(); | 353 base::MessageLoop::current()->RunUntilIdle(); |
354 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 354 EXPECT_TRUE(base::PathExists(db_file_path)); |
355 area->SetItem(kKey2, kValue2, &old_value); | 355 area->SetItem(kKey2, kValue2, &old_value); |
356 EXPECT_TRUE(area->HasUncommittedChanges()); | 356 EXPECT_TRUE(area->HasUncommittedChanges()); |
357 EXPECT_EQ(2u, area->Length()); | 357 EXPECT_EQ(2u, area->Length()); |
358 area->DeleteOrigin(); | 358 area->DeleteOrigin(); |
359 EXPECT_TRUE(area->HasUncommittedChanges()); | 359 EXPECT_TRUE(area->HasUncommittedChanges()); |
360 EXPECT_EQ(0u, area->Length()); | 360 EXPECT_EQ(0u, area->Length()); |
361 base::MessageLoop::current()->RunUntilIdle(); | 361 base::MessageLoop::current()->RunUntilIdle(); |
362 EXPECT_FALSE(area->HasUncommittedChanges()); | 362 EXPECT_FALSE(area->HasUncommittedChanges()); |
363 // Since the area had uncommitted changes at the time delete | 363 // Since the area had uncommitted changes at the time delete |
364 // was called, the file will linger until the shutdown time. | 364 // was called, the file will linger until the shutdown time. |
365 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 365 EXPECT_TRUE(base::PathExists(db_file_path)); |
366 area->Shutdown(); | 366 area->Shutdown(); |
367 base::MessageLoop::current()->RunUntilIdle(); | 367 base::MessageLoop::current()->RunUntilIdle(); |
368 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 368 EXPECT_FALSE(base::PathExists(db_file_path)); |
369 } | 369 } |
370 | 370 |
371 TEST_F(DomStorageAreaTest, PurgeMemory) { | 371 TEST_F(DomStorageAreaTest, PurgeMemory) { |
372 base::ScopedTempDir temp_dir; | 372 base::ScopedTempDir temp_dir; |
373 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 373 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
374 scoped_refptr<DomStorageArea> area(new DomStorageArea( | 374 scoped_refptr<DomStorageArea> area(new DomStorageArea( |
375 kOrigin, | 375 kOrigin, |
376 temp_dir.path(), | 376 temp_dir.path(), |
377 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 377 new MockDomStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
378 | 378 |
(...skipping 85 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 |