| 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" |
| 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "base/time.h" |
| 5 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "webkit/dom_storage/dom_storage_area.h" | 14 #include "webkit/dom_storage/dom_storage_area.h" |
| 15 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 16 #include "webkit/dom_storage/dom_storage_types.h" |
| 8 | 17 |
| 9 namespace dom_storage { | 18 namespace dom_storage { |
| 10 | 19 |
| 20 void QuitMessageLoop() { |
| 21 MessageLoop::current()->Quit(); |
| 22 } |
| 23 |
| 11 TEST(DomStorageAreaTest, DomStorageAreaBasics) { | 24 TEST(DomStorageAreaTest, DomStorageAreaBasics) { |
| 12 const GURL kOrigin("http://dom_storage/"); | 25 const GURL kOrigin("http://dom_storage/"); |
| 13 const string16 kKey(ASCIIToUTF16("key")); | 26 const string16 kKey(ASCIIToUTF16("key")); |
| 14 const string16 kValue(ASCIIToUTF16("value")); | 27 const string16 kValue(ASCIIToUTF16("value")); |
| 15 const string16 kKey2(ASCIIToUTF16("key2")); | 28 const string16 kKey2(ASCIIToUTF16("key2")); |
| 16 const string16 kValue2(ASCIIToUTF16("value2")); | 29 const string16 kValue2(ASCIIToUTF16("value2")); |
| 17 | 30 |
| 18 scoped_refptr<DomStorageArea> area( | 31 scoped_refptr<DomStorageArea> area( |
| 19 new DomStorageArea(1, kOrigin, FilePath(), NULL)); | 32 new DomStorageArea(1, kOrigin, FilePath(), NULL)); |
| 20 string16 old_value; | 33 string16 old_value; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 46 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); | 59 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 47 EXPECT_NE(copy->map_.get(), area->map_.get()); | 60 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 48 copy = area->ShallowCopy(2); | 61 copy = area->ShallowCopy(2); |
| 49 EXPECT_EQ(copy->map_.get(), area->map_.get()); | 62 EXPECT_EQ(copy->map_.get(), area->map_.get()); |
| 50 EXPECT_NE(0u, area->Length()); | 63 EXPECT_NE(0u, area->Length()); |
| 51 EXPECT_TRUE(area->Clear()); | 64 EXPECT_TRUE(area->Clear()); |
| 52 EXPECT_EQ(0u, area->Length()); | 65 EXPECT_EQ(0u, area->Length()); |
| 53 EXPECT_NE(copy->map_.get(), area->map_.get()); | 66 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 54 } | 67 } |
| 55 | 68 |
| 69 TEST(DomStorageAreaTest, BackingDatabaseOpened) { |
| 70 const int64 kNonLocalStorageNamespaceId = kLocalStorageNamespaceId + 1; |
| 71 const GURL origin("http://www.google.com"); |
| 72 const string16 kKey = ASCIIToUTF16("test"); |
| 73 const string16 kKey2 = ASCIIToUTF16("test2"); |
| 74 const string16 kValue = ASCIIToUTF16("value"); |
| 75 ScopedTempDir temp_dir; |
| 76 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 77 |
| 78 // No directory, backing should be null. |
| 79 { |
| 80 scoped_refptr<DomStorageArea> area( |
| 81 new DomStorageArea(kLocalStorageNamespaceId, origin, FilePath(), |
| 82 NULL)); |
| 83 EXPECT_EQ(NULL, area->backing_.get()); |
| 84 EXPECT_TRUE(area->initial_import_done_); |
| 85 EXPECT_FALSE(file_util::PathExists( |
| 86 temp_dir.path().Append( |
| 87 DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
| 88 } |
| 89 |
| 90 // Valid directory and origin but non-local namespace id. Backing should |
| 91 // be null. |
| 92 { |
| 93 scoped_refptr<DomStorageArea> area( |
| 94 new DomStorageArea(kNonLocalStorageNamespaceId, origin, |
| 95 temp_dir.path(), NULL)); |
| 96 EXPECT_EQ(NULL, area->backing_.get()); |
| 97 EXPECT_TRUE(area->initial_import_done_); |
| 98 |
| 99 NullableString16 old_value; |
| 100 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 101 ASSERT_TRUE(old_value.is_null()); |
| 102 |
| 103 // Check that saving a value has still left us without a backing database. |
| 104 EXPECT_EQ(NULL, area->backing_.get()); |
| 105 EXPECT_FALSE(file_util::PathExists( |
| 106 temp_dir.path().Append( |
| 107 DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
| 108 } |
| 109 |
| 110 // This should set up a DomStorageArea that is correctly backed to disk and |
| 111 // uses a SequencedWorkerPool to commit the changes lazily. |
| 112 { |
| 113 base::SequencedWorkerPool pool(2, "dom_storage_area_test"); |
| 114 scoped_refptr<DomStorageArea> area( |
| 115 new DomStorageArea(kLocalStorageNamespaceId, origin, |
| 116 temp_dir.path(), |
| 117 new DomStorageWorkerPoolTaskRunner(&pool, |
| 118 base::MessageLoopProxy::current()))); |
| 119 |
| 120 EXPECT_TRUE(area->backing_.get()); |
| 121 EXPECT_FALSE(area->initial_import_done_); |
| 122 |
| 123 // Need to write something to ensure that the database is created. |
| 124 NullableString16 old_value; |
| 125 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 126 ASSERT_TRUE(old_value.is_null()); |
| 127 EXPECT_TRUE(area->SetItem(kKey2, kValue, &old_value)); |
| 128 ASSERT_TRUE(old_value.is_null()); |
| 129 EXPECT_TRUE(area->initial_import_done_); |
| 130 |
| 131 // Run the message loop to ensure that the delayed task to commit changes |
| 132 // to disk makes it to the SequencedWorkerPool. |
| 133 MessageLoop::current()->PostDelayedTask( |
| 134 FROM_HERE, base::Bind(&QuitMessageLoop), |
| 135 base::TimeDelta::FromSeconds(2)); |
| 136 MessageLoop::current()->Run(); |
| 137 |
| 138 // Now make sure that the SequencedWorkerPool finishes processing the |
| 139 // commits. |
| 140 pool.FlushForTesting(); |
| 141 |
| 142 EXPECT_TRUE(area->backing_->IsOpen()); |
| 143 EXPECT_EQ(2u, area->Length()); |
| 144 EXPECT_TRUE(file_util::PathExists( |
| 145 temp_dir.path().Append( |
| 146 DomStorageArea::DatabaseFileNameFromOrigin(origin)))); |
| 147 EXPECT_EQ(kValue, area->GetItem(kKey).string()); |
| 148 pool.Shutdown(); |
| 149 } |
| 150 } |
| 151 |
| 152 TEST(DomStorageAreaTest, TestDatabaseFilePath) { |
| 153 EXPECT_EQ(FilePath().AppendASCII("file_path_to_0.localstorage"), |
| 154 DomStorageArea::DatabaseFileNameFromOrigin( |
| 155 GURL("file://path_to/index.html"))); |
| 156 |
| 157 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_0.localstorage"), |
| 158 DomStorageArea::DatabaseFileNameFromOrigin( |
| 159 GURL("https://www.google.com/"))); |
| 160 |
| 161 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_8080.localstorage"), |
| 162 DomStorageArea::DatabaseFileNameFromOrigin( |
| 163 GURL("https://www.google.com:8080"))); |
| 164 } |
| 165 |
| 56 } // namespace dom_storage | 166 } // namespace dom_storage |
| OLD | NEW |