| 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/files/scoped_temp_dir.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 base::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 base::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()); |
| 122 ASSERT_TRUE(db.CommitChanges(false, storage)); | 122 ASSERT_TRUE(db.CommitChanges(false, storage)); |
| 123 } | 123 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 base::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 base::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 |
| 179 values[ASCIIToUTF16("key")] = NullableString16(ASCIIToUTF16("value"), false); | 179 values[ASCIIToUTF16("key")] = NullableString16(ASCIIToUTF16("value"), false); |
| 180 db.CommitChanges(false, values); | 180 db.CommitChanges(false, values); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 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 base::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 base::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)); |
| 226 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 226 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
| 227 } | 227 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 // A null string in the map should mean that that key gets | 294 // A null string in the map should mean that that key gets |
| 295 // removed. | 295 // removed. |
| 296 values[kCannedKey] = NullableString16(true); | 296 values[kCannedKey] = NullableString16(true); |
| 297 EXPECT_TRUE(db.CommitChanges(false, values)); | 297 EXPECT_TRUE(db.CommitChanges(false, values)); |
| 298 | 298 |
| 299 expected.clear(); | 299 expected.clear(); |
| 300 CheckValuesMatch(&db, expected); | 300 CheckValuesMatch(&db, expected); |
| 301 } | 301 } |
| 302 | 302 |
| 303 TEST(DomStorageDatabaseTest, TestCanOpenAndReadWebCoreDatabase) { | 303 TEST(DomStorageDatabaseTest, TestCanOpenAndReadWebCoreDatabase) { |
| 304 FilePath webcore_database; | 304 base::FilePath webcore_database; |
| 305 PathService::Get(base::DIR_SOURCE_ROOT, &webcore_database); | 305 PathService::Get(base::DIR_SOURCE_ROOT, &webcore_database); |
| 306 webcore_database = webcore_database.AppendASCII("webkit"); | 306 webcore_database = webcore_database.AppendASCII("webkit"); |
| 307 webcore_database = webcore_database.AppendASCII("data"); | 307 webcore_database = webcore_database.AppendASCII("data"); |
| 308 webcore_database = webcore_database.AppendASCII("dom_storage"); | 308 webcore_database = webcore_database.AppendASCII("dom_storage"); |
| 309 webcore_database = | 309 webcore_database = |
| 310 webcore_database.AppendASCII("webcore_test_database.localstorage"); | 310 webcore_database.AppendASCII("webcore_test_database.localstorage"); |
| 311 | 311 |
| 312 ASSERT_TRUE(file_util::PathExists(webcore_database)); | 312 ASSERT_TRUE(file_util::PathExists(webcore_database)); |
| 313 | 313 |
| 314 DomStorageDatabase db(webcore_database); | 314 DomStorageDatabase db(webcore_database); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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 base::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 base::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. |
| 346 DomStorageDatabase db(file_name); | 346 DomStorageDatabase db(file_name); |
| 347 ValuesMap values; | 347 ValuesMap values; |
| (...skipping 19 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 |