| 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 |
| 11 TEST(DomStorageAreaTest, DomStorageAreaBasics) { | 20 TEST(DomStorageAreaTest, DomStorageAreaBasics) { |
| 12 const GURL kOrigin("http://dom_storage/"); | 21 const GURL kOrigin("http://dom_storage/"); |
| 13 const string16 kKey(ASCIIToUTF16("key")); | 22 const string16 kKey(ASCIIToUTF16("key")); |
| 14 const string16 kValue(ASCIIToUTF16("value")); | 23 const string16 kValue(ASCIIToUTF16("value")); |
| 15 const string16 kKey2(ASCIIToUTF16("key2")); | 24 const string16 kKey2(ASCIIToUTF16("key2")); |
| 16 const string16 kValue2(ASCIIToUTF16("value2")); | 25 const string16 kValue2(ASCIIToUTF16("value2")); |
| 17 | 26 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 46 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); | 55 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); |
| 47 EXPECT_NE(copy->map_.get(), area->map_.get()); | 56 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 48 copy = area->ShallowCopy(2); | 57 copy = area->ShallowCopy(2); |
| 49 EXPECT_EQ(copy->map_.get(), area->map_.get()); | 58 EXPECT_EQ(copy->map_.get(), area->map_.get()); |
| 50 EXPECT_NE(0u, area->Length()); | 59 EXPECT_NE(0u, area->Length()); |
| 51 EXPECT_TRUE(area->Clear()); | 60 EXPECT_TRUE(area->Clear()); |
| 52 EXPECT_EQ(0u, area->Length()); | 61 EXPECT_EQ(0u, area->Length()); |
| 53 EXPECT_NE(copy->map_.get(), area->map_.get()); | 62 EXPECT_NE(copy->map_.get(), area->map_.get()); |
| 54 } | 63 } |
| 55 | 64 |
| 65 TEST(DomStorageAreaTest, BackingDatabaseOpened) { |
| 66 const int64 kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1; |
| 67 const GURL kOrigin("http://www.google.com"); |
| 68 |
| 69 const string16 kKey = ASCIIToUTF16("test"); |
| 70 const string16 kKey2 = ASCIIToUTF16("test2"); |
| 71 const string16 kValue = ASCIIToUTF16("value"); |
| 72 ScopedTempDir temp_dir; |
| 73 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 74 |
| 75 const FilePath kExpectedOriginFilePath = temp_dir.path().Append( |
| 76 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin)); |
| 77 |
| 78 // No directory, backing should be null. |
| 79 { |
| 80 scoped_refptr<DomStorageArea> area( |
| 81 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, FilePath(), |
| 82 NULL)); |
| 83 EXPECT_EQ(NULL, area->backing_.get()); |
| 84 EXPECT_TRUE(area->initial_import_done_); |
| 85 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); |
| 86 } |
| 87 |
| 88 // Valid directory and origin but non-local namespace id. Backing should |
| 89 // be null. |
| 90 { |
| 91 scoped_refptr<DomStorageArea> area( |
| 92 new DomStorageArea(kSessionStorageNamespaceId, kOrigin, |
| 93 temp_dir.path(), NULL)); |
| 94 EXPECT_EQ(NULL, area->backing_.get()); |
| 95 EXPECT_TRUE(area->initial_import_done_); |
| 96 |
| 97 NullableString16 old_value; |
| 98 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 99 ASSERT_TRUE(old_value.is_null()); |
| 100 |
| 101 // Check that saving a value has still left us without a backing database. |
| 102 EXPECT_EQ(NULL, area->backing_.get()); |
| 103 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); |
| 104 } |
| 105 |
| 106 // This should set up a DomStorageArea that is correctly backed to disk. |
| 107 { |
| 108 scoped_refptr<DomStorageArea> area( |
| 109 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, |
| 110 temp_dir.path(), |
| 111 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
| 112 |
| 113 EXPECT_TRUE(area->backing_.get()); |
| 114 EXPECT_FALSE(area->backing_->IsOpen()); |
| 115 EXPECT_FALSE(area->initial_import_done_); |
| 116 |
| 117 // Switch out the file-backed db with an in-memory db to speed up the test. |
| 118 // We will verify that something is written into the database but not |
| 119 // that a file is written to disk - DOMStorageDatabase unit tests cover |
| 120 // that. |
| 121 area->backing_.reset(new DomStorageDatabase()); |
| 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 EXPECT_TRUE(area->commit_in_flight_); |
| 131 |
| 132 MessageLoop::current()->RunAllPending(); |
| 133 |
| 134 EXPECT_FALSE(area->commit_in_flight_); |
| 135 EXPECT_TRUE(area->backing_->IsOpen()); |
| 136 EXPECT_EQ(2u, area->Length()); |
| 137 EXPECT_EQ(kValue, area->GetItem(kKey).string()); |
| 138 |
| 139 // Verify the content made it to the in memory database. |
| 140 ValuesMap values; |
| 141 area->backing_->ReadAllValues(&values); |
| 142 EXPECT_EQ(2u, values.size()); |
| 143 EXPECT_EQ(kValue, values[kKey].string()); |
| 144 } |
| 145 } |
| 146 |
| 147 TEST(DomStorageAreaTest, TestDatabaseFilePath) { |
| 148 EXPECT_EQ(FilePath().AppendASCII("file_path_to_0.localstorage"), |
| 149 DomStorageArea::DatabaseFileNameFromOrigin( |
| 150 GURL("file://path_to/index.html"))); |
| 151 |
| 152 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_0.localstorage"), |
| 153 DomStorageArea::DatabaseFileNameFromOrigin( |
| 154 GURL("https://www.google.com/"))); |
| 155 |
| 156 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_8080.localstorage"), |
| 157 DomStorageArea::DatabaseFileNameFromOrigin( |
| 158 GURL("https://www.google.com:8080"))); |
| 159 } |
| 160 |
| 56 } // namespace dom_storage | 161 } // namespace dom_storage |
| OLD | NEW |