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 "webkit/dom_storage/dom_storage_database.h" | 5 #include "webkit/dom_storage/dom_storage_database.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "sql/statement.h" | 12 #include "sql/statement.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace dom_storage { | 15 namespace dom_storage { |
16 | 16 |
17 void CreateV1Table(sql::Connection* db) { | 17 void CreateV1Table(sql::Connection* db) { |
18 ASSERT_TRUE(db->is_open()); | 18 ASSERT_TRUE(db->is_open()); |
19 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable")); | 19 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable")); |
20 ASSERT_TRUE(db->Execute( | 20 ASSERT_TRUE(db->Execute( |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 DomStorageDatabase db; | 101 DomStorageDatabase db; |
102 EXPECT_FALSE(db.IsOpen()); | 102 EXPECT_FALSE(db.IsOpen()); |
103 ASSERT_TRUE(db.LazyOpen(true)); | 103 ASSERT_TRUE(db.LazyOpen(true)); |
104 EXPECT_TRUE(db.IsOpen()); | 104 EXPECT_TRUE(db.IsOpen()); |
105 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 105 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
106 db.Close(); | 106 db.Close(); |
107 EXPECT_FALSE(db.IsOpen()); | 107 EXPECT_FALSE(db.IsOpen()); |
108 } | 108 } |
109 | 109 |
110 TEST(DomStorageDatabaseTest, CloseEmptyDatabaseDeletesFile) { | 110 TEST(DomStorageDatabaseTest, CloseEmptyDatabaseDeletesFile) { |
111 ScopedTempDir temp_dir; | 111 base::ScopedTempDir temp_dir; |
112 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 112 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
113 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); | 113 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
114 ValuesMap storage; | 114 ValuesMap storage; |
115 CreateMapWithValues(&storage); | 115 CreateMapWithValues(&storage); |
116 | 116 |
117 // First test the case that explicitly clearing the database will | 117 // First test the case that explicitly clearing the database will |
118 // trigger its deletion from disk. | 118 // trigger its deletion from disk. |
119 { | 119 { |
120 DomStorageDatabase db(file_name); | 120 DomStorageDatabase db(file_name); |
121 EXPECT_EQ(file_name, db.file_path()); | 121 EXPECT_EQ(file_name, db.file_path()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 for (; it != storage.end(); ++it) | 158 for (; it != storage.end(); ++it) |
159 it->second = NullableString16(true); | 159 it->second = NullableString16(true); |
160 ASSERT_TRUE(db.CommitChanges(false, storage)); | 160 ASSERT_TRUE(db.CommitChanges(false, storage)); |
161 } | 161 } |
162 EXPECT_FALSE(file_util::PathExists(file_name)); | 162 EXPECT_FALSE(file_util::PathExists(file_name)); |
163 } | 163 } |
164 | 164 |
165 TEST(DomStorageDatabaseTest, TestLazyOpenIsLazy) { | 165 TEST(DomStorageDatabaseTest, TestLazyOpenIsLazy) { |
166 // This test needs to operate with a file on disk to ensure that we will | 166 // This test needs to operate with a file on disk to ensure that we will |
167 // open a file that already exists when only invoking ReadAllValues. | 167 // open a file that already exists when only invoking ReadAllValues. |
168 ScopedTempDir temp_dir; | 168 base::ScopedTempDir temp_dir; |
169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
170 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); | 170 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
171 | 171 |
172 DomStorageDatabase db(file_name); | 172 DomStorageDatabase db(file_name); |
173 EXPECT_FALSE(db.IsOpen()); | 173 EXPECT_FALSE(db.IsOpen()); |
174 ValuesMap values; | 174 ValuesMap values; |
175 db.ReadAllValues(&values); | 175 db.ReadAllValues(&values); |
176 // Reading an empty db should not open the database. | 176 // Reading an empty db should not open the database. |
177 EXPECT_FALSE(db.IsOpen()); | 177 EXPECT_FALSE(db.IsOpen()); |
178 | 178 |
(...skipping 26 matching lines...) Expand all Loading... |
205 | 205 |
206 CreateV2Table(db.db_.get()); | 206 CreateV2Table(db.db_.get()); |
207 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 207 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
208 } | 208 } |
209 | 209 |
210 TEST(DomStorageDatabaseTest, TestLazyOpenUpgradesDatabase) { | 210 TEST(DomStorageDatabaseTest, TestLazyOpenUpgradesDatabase) { |
211 // This test needs to operate with a file on disk so that we | 211 // This test needs to operate with a file on disk so that we |
212 // can create a table at version 1 and then close it again | 212 // can create a table at version 1 and then close it again |
213 // so that LazyOpen sees there is work to do (LazyOpen will return | 213 // so that LazyOpen sees there is work to do (LazyOpen will return |
214 // early if the database is already open). | 214 // early if the database is already open). |
215 ScopedTempDir temp_dir; | 215 base::ScopedTempDir temp_dir; |
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
217 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); | 217 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
218 | 218 |
219 DomStorageDatabase db(file_name); | 219 DomStorageDatabase db(file_name); |
220 db.db_.reset(new sql::Connection()); | 220 db.db_.reset(new sql::Connection()); |
221 ASSERT_TRUE(db.db_->Open(file_name)); | 221 ASSERT_TRUE(db.db_->Open(file_name)); |
222 CreateV1Table(db.db_.get()); | 222 CreateV1Table(db.db_.get()); |
223 db.Close(); | 223 db.Close(); |
224 | 224 |
225 EXPECT_TRUE(db.LazyOpen(true)); | 225 EXPECT_TRUE(db.LazyOpen(true)); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 it = values.find(ASCIIToUTF16("timestamp")); | 325 it = values.find(ASCIIToUTF16("timestamp")); |
326 EXPECT_TRUE(it != values.end()); | 326 EXPECT_TRUE(it != values.end()); |
327 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string()); | 327 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string()); |
328 | 328 |
329 it = values.find(ASCIIToUTF16("not_there")); | 329 it = values.find(ASCIIToUTF16("not_there")); |
330 EXPECT_TRUE(it == values.end()); | 330 EXPECT_TRUE(it == values.end()); |
331 } | 331 } |
332 | 332 |
333 TEST(DomStorageDatabaseTest, TestCanOpenFileThatIsNotADatabase) { | 333 TEST(DomStorageDatabaseTest, TestCanOpenFileThatIsNotADatabase) { |
334 // Write into the temporary file first. | 334 // Write into the temporary file first. |
335 ScopedTempDir temp_dir; | 335 base::ScopedTempDir temp_dir; |
336 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 336 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
337 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); | 337 FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
338 | 338 |
339 const char kData[] = "I am not a database."; | 339 const char kData[] = "I am not a database."; |
340 file_util::WriteFile(file_name, kData, strlen(kData)); | 340 file_util::WriteFile(file_name, kData, strlen(kData)); |
341 | 341 |
342 { | 342 { |
343 // Try and open the file. As it's not a database, we should end up deleting | 343 // Try and open the file. As it's not a database, we should end up deleting |
344 // it and creating a new, valid file, so everything should actually | 344 // it and creating a new, valid file, so everything should actually |
345 // succeed. | 345 // succeed. |
(...skipping 21 matching lines...) Expand all Loading... |
367 | 367 |
368 db.ReadAllValues(&values); | 368 db.ReadAllValues(&values); |
369 EXPECT_EQ(0u, values.size()); | 369 EXPECT_EQ(0u, values.size()); |
370 EXPECT_FALSE(db.IsOpen()); | 370 EXPECT_FALSE(db.IsOpen()); |
371 | 371 |
372 EXPECT_TRUE(file_util::PathExists(temp_dir.path())); | 372 EXPECT_TRUE(file_util::PathExists(temp_dir.path())); |
373 } | 373 } |
374 } | 374 } |
375 | 375 |
376 } // namespace dom_storage | 376 } // namespace dom_storage |
OLD | NEW |