Chromium Code Reviews| Index: webkit/dom_storage/dom_storage_area_unittest.cc |
| diff --git a/webkit/dom_storage/dom_storage_area_unittest.cc b/webkit/dom_storage/dom_storage_area_unittest.cc |
| index 1d51fe3b70bf4c45b8949e1f09ab3ae2cfeccc3d..908b2940319aa40ff3343cd5809a93e65fe72bcd 100644 |
| --- a/webkit/dom_storage/dom_storage_area_unittest.cc |
| +++ b/webkit/dom_storage/dom_storage_area_unittest.cc |
| @@ -2,12 +2,25 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/bind.h" |
| +#include "base/file_util.h" |
| +#include "base/message_loop.h" |
| +#include "base/message_loop_proxy.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "base/time.h" |
| #include "base/utf_string_conversions.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "webkit/dom_storage/dom_storage_area.h" |
| +#include "webkit/dom_storage/dom_storage_task_runner.h" |
| +#include "webkit/dom_storage/dom_storage_types.h" |
| namespace dom_storage { |
| +void QuitMessageLoop() { |
|
michaeln
2012/02/23 20:48:48
should either make this static or put it in an ano
benm (inactive)
2012/02/24 12:42:01
Done.
|
| + MessageLoop::current()->Quit(); |
| +} |
| + |
| TEST(DomStorageAreaTest, DomStorageAreaBasics) { |
| const GURL kOrigin("http://dom_storage/"); |
| const string16 kKey(ASCIIToUTF16("key")); |
| @@ -53,4 +66,101 @@ TEST(DomStorageAreaTest, DomStorageAreaBasics) { |
| EXPECT_NE(copy->map_.get(), area->map_.get()); |
| } |
| +TEST(DomStorageAreaTest, BackingDatabaseOpened) { |
| + const int64 kNonLocalStorageNamespaceId = kLocalStorageNamespaceId + 1; |
|
michaeln
2012/02/23 20:48:48
kSessionStorageNamespaceId?
benm (inactive)
2012/02/24 12:42:01
Done.
|
| + const GURL origin("http://www.google.com"); |
|
michaeln
2012/02/23 20:48:48
kOrigin
benm (inactive)
2012/02/24 12:42:01
Done.
|
| + const string16 kKey = ASCIIToUTF16("test"); |
| + const string16 kKey2 = ASCIIToUTF16("test2"); |
| + const string16 kValue = ASCIIToUTF16("value"); |
| + ScopedTempDir temp_dir; |
| + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| + |
| + // No directory, backing should be null. |
| + { |
| + scoped_refptr<DomStorageArea> area( |
| + new DomStorageArea(kLocalStorageNamespaceId, origin, FilePath(), |
| + NULL)); |
| + EXPECT_EQ(NULL, area->backing_.get()); |
| + EXPECT_TRUE(area->initial_import_done_); |
| + EXPECT_FALSE(file_util::PathExists( |
| + temp_dir.path().Append( |
| + DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
|
michaeln
2012/02/23 20:48:48
consider making a local const for this FilePath to
benm (inactive)
2012/02/24 12:42:01
Done.
|
| + } |
| + |
| + // Valid directory and origin but non-local namespace id. Backing should |
| + // be null. |
| + { |
| + scoped_refptr<DomStorageArea> area( |
| + new DomStorageArea(kNonLocalStorageNamespaceId, origin, |
| + temp_dir.path(), NULL)); |
| + EXPECT_EQ(NULL, area->backing_.get()); |
| + EXPECT_TRUE(area->initial_import_done_); |
| + |
| + NullableString16 old_value; |
| + EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| + ASSERT_TRUE(old_value.is_null()); |
| + |
| + // Check that saving a value has still left us without a backing database. |
| + EXPECT_EQ(NULL, area->backing_.get()); |
| + EXPECT_FALSE(file_util::PathExists( |
| + temp_dir.path().Append( |
| + DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
| + } |
| + |
| + // This should set up a DomStorageArea that is correctly backed to disk and |
| + // uses a SequencedWorkerPool to commit the changes lazily. |
| + { |
| + base::SequencedWorkerPool pool(2, "dom_storage_area_test"); |
|
michaeln
2012/02/23 20:48:48
might be easier to compose this test w/o using a r
benm (inactive)
2012/02/24 12:42:01
Done - created a mock task runner.
|
| + scoped_refptr<DomStorageArea> area( |
| + new DomStorageArea(kLocalStorageNamespaceId, origin, |
| + temp_dir.path(), |
| + new DomStorageWorkerPoolTaskRunner(&pool, |
| + base::MessageLoopProxy::current()))); |
| + |
| + EXPECT_TRUE(area->backing_.get()); |
| + EXPECT_FALSE(area->initial_import_done_); |
| + |
| + // Need to write something to ensure that the database is created. |
| + NullableString16 old_value; |
| + EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| + ASSERT_TRUE(old_value.is_null()); |
| + EXPECT_TRUE(area->SetItem(kKey2, kValue, &old_value)); |
| + ASSERT_TRUE(old_value.is_null()); |
| + EXPECT_TRUE(area->initial_import_done_); |
| + |
| + // Run the message loop to ensure that the delayed task to commit changes |
| + // to disk makes it to the SequencedWorkerPool. |
| + MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, base::Bind(&QuitMessageLoop), |
| + base::TimeDelta::FromSeconds(2)); |
|
michaeln
2012/02/23 20:48:48
Does this result in a 2 second delay of test execu
benm (inactive)
2012/02/24 12:42:01
Done - the mock task runner ignores the timeout pa
|
| + MessageLoop::current()->Run(); |
| + |
| + // Now make sure that the SequencedWorkerPool finishes processing the |
| + // commits. |
| + pool.FlushForTesting(); |
| + |
| + EXPECT_TRUE(area->backing_->IsOpen()); |
| + EXPECT_EQ(2u, area->Length()); |
| + EXPECT_TRUE(file_util::PathExists( |
| + temp_dir.path().Append( |
| + DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
| + EXPECT_EQ(kValue, area->GetItem(kKey).string()); |
| + pool.Shutdown(); |
| + } |
| +} |
| + |
| +TEST(DomStorageAreaTest, TestDatabaseFilePath) { |
| + EXPECT_EQ(FilePath().AppendASCII("file_path_to_0.localstorage"), |
| + DomStorageArea::DatabaseFileNameFromOrigin( |
| + GURL("file://path_to/index.html"))); |
| + |
| + EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_0.localstorage"), |
| + DomStorageArea::DatabaseFileNameFromOrigin( |
| + GURL("https://www.google.com/"))); |
| + |
| + EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_8080.localstorage"), |
| + DomStorageArea::DatabaseFileNameFromOrigin( |
| + GURL("https://www.google.com:8080"))); |
| +} |
| + |
| } // namespace dom_storage |