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/browser/dom_storage/dom_storage_database.h" | 5 #include "webkit/browser/dom_storage/dom_storage_database.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 void CheckValuesMatch(DomStorageDatabase* db, | 67 void CheckValuesMatch(DomStorageDatabase* db, |
68 const ValuesMap& expected) { | 68 const ValuesMap& expected) { |
69 ValuesMap values_read; | 69 ValuesMap values_read; |
70 db->ReadAllValues(&values_read); | 70 db->ReadAllValues(&values_read); |
71 EXPECT_EQ(expected.size(), values_read.size()); | 71 EXPECT_EQ(expected.size(), values_read.size()); |
72 | 72 |
73 ValuesMap::const_iterator it = values_read.begin(); | 73 ValuesMap::const_iterator it = values_read.begin(); |
74 for (; it != values_read.end(); ++it) { | 74 for (; it != values_read.end(); ++it) { |
75 base::string16 key = it->first; | 75 base::string16 key = it->first; |
76 NullableString16 value = it->second; | 76 base::NullableString16 value = it->second; |
77 NullableString16 expected_value = expected.find(key)->second; | 77 base::NullableString16 expected_value = expected.find(key)->second; |
78 EXPECT_EQ(expected_value.string(), value.string()); | 78 EXPECT_EQ(expected_value.string(), value.string()); |
79 EXPECT_EQ(expected_value.is_null(), value.is_null()); | 79 EXPECT_EQ(expected_value.is_null(), value.is_null()); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 void CreateMapWithValues(ValuesMap* values) { | 83 void CreateMapWithValues(ValuesMap* values) { |
84 base::string16 kCannedKeys[] = { | 84 base::string16 kCannedKeys[] = { |
85 ASCIIToUTF16("test"), | 85 ASCIIToUTF16("test"), |
86 ASCIIToUTF16("company"), | 86 ASCIIToUTF16("company"), |
87 ASCIIToUTF16("date"), | 87 ASCIIToUTF16("date"), |
88 ASCIIToUTF16("empty") | 88 ASCIIToUTF16("empty") |
89 }; | 89 }; |
90 NullableString16 kCannedValues[] = { | 90 base::NullableString16 kCannedValues[] = { |
91 NullableString16(ASCIIToUTF16("123"), false), | 91 base::NullableString16(ASCIIToUTF16("123"), false), |
92 NullableString16(ASCIIToUTF16("Google"), false), | 92 base::NullableString16(ASCIIToUTF16("Google"), false), |
93 NullableString16(ASCIIToUTF16("18-01-2012"), false), | 93 base::NullableString16(ASCIIToUTF16("18-01-2012"), false), |
94 NullableString16(base::string16(), false) | 94 base::NullableString16(base::string16(), false) |
95 }; | 95 }; |
96 for (unsigned i = 0; i < sizeof(kCannedKeys) / sizeof(kCannedKeys[0]); i++) | 96 for (unsigned i = 0; i < sizeof(kCannedKeys) / sizeof(kCannedKeys[0]); i++) |
97 (*values)[kCannedKeys[i]] = kCannedValues[i]; | 97 (*values)[kCannedKeys[i]] = kCannedValues[i]; |
98 } | 98 } |
99 | 99 |
100 TEST(DomStorageDatabaseTest, SimpleOpenAndClose) { | 100 TEST(DomStorageDatabaseTest, SimpleOpenAndClose) { |
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 base::ScopedTempDir temp_dir; | 111 base::ScopedTempDir temp_dir; |
112 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 112 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
113 base::FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase
.db"); | 113 base::FilePath file_name = |
| 114 temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
114 ValuesMap storage; | 115 ValuesMap storage; |
115 CreateMapWithValues(&storage); | 116 CreateMapWithValues(&storage); |
116 | 117 |
117 // First test the case that explicitly clearing the database will | 118 // First test the case that explicitly clearing the database will |
118 // trigger its deletion from disk. | 119 // trigger its deletion from disk. |
119 { | 120 { |
120 DomStorageDatabase db(file_name); | 121 DomStorageDatabase db(file_name); |
121 EXPECT_EQ(file_name, db.file_path()); | 122 EXPECT_EQ(file_name, db.file_path()); |
122 ASSERT_TRUE(db.CommitChanges(false, storage)); | 123 ASSERT_TRUE(db.CommitChanges(false, storage)); |
123 } | 124 } |
(...skipping 25 matching lines...) Expand all Loading... |
149 ASSERT_TRUE(db.CommitChanges(false, storage)); | 150 ASSERT_TRUE(db.CommitChanges(false, storage)); |
150 } | 151 } |
151 | 152 |
152 EXPECT_TRUE(file_util::PathExists(file_name)); | 153 EXPECT_TRUE(file_util::PathExists(file_name)); |
153 | 154 |
154 { | 155 { |
155 DomStorageDatabase db(file_name); | 156 DomStorageDatabase db(file_name); |
156 ASSERT_TRUE(db.CommitChanges(false, storage)); | 157 ASSERT_TRUE(db.CommitChanges(false, storage)); |
157 ValuesMap::iterator it = storage.begin(); | 158 ValuesMap::iterator it = storage.begin(); |
158 for (; it != storage.end(); ++it) | 159 for (; it != storage.end(); ++it) |
159 it->second = NullableString16(true); | 160 it->second = base::NullableString16(true); |
160 ASSERT_TRUE(db.CommitChanges(false, storage)); | 161 ASSERT_TRUE(db.CommitChanges(false, storage)); |
161 } | 162 } |
162 EXPECT_FALSE(file_util::PathExists(file_name)); | 163 EXPECT_FALSE(file_util::PathExists(file_name)); |
163 } | 164 } |
164 | 165 |
165 TEST(DomStorageDatabaseTest, TestLazyOpenIsLazy) { | 166 TEST(DomStorageDatabaseTest, TestLazyOpenIsLazy) { |
166 // This test needs to operate with a file on disk to ensure that we will | 167 // 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. | 168 // open a file that already exists when only invoking ReadAllValues. |
168 base::ScopedTempDir temp_dir; | 169 base::ScopedTempDir temp_dir; |
169 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 170 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
170 base::FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase
.db"); | 171 base::FilePath file_name = |
| 172 temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
171 | 173 |
172 DomStorageDatabase db(file_name); | 174 DomStorageDatabase db(file_name); |
173 EXPECT_FALSE(db.IsOpen()); | 175 EXPECT_FALSE(db.IsOpen()); |
174 ValuesMap values; | 176 ValuesMap values; |
175 db.ReadAllValues(&values); | 177 db.ReadAllValues(&values); |
176 // Reading an empty db should not open the database. | 178 // Reading an empty db should not open the database. |
177 EXPECT_FALSE(db.IsOpen()); | 179 EXPECT_FALSE(db.IsOpen()); |
178 | 180 |
179 values[ASCIIToUTF16("key")] = NullableString16(ASCIIToUTF16("value"), false); | 181 values[ASCIIToUTF16("key")] = |
| 182 base::NullableString16(ASCIIToUTF16("value"), false); |
180 db.CommitChanges(false, values); | 183 db.CommitChanges(false, values); |
181 // Writing content should open the database. | 184 // Writing content should open the database. |
182 EXPECT_TRUE(db.IsOpen()); | 185 EXPECT_TRUE(db.IsOpen()); |
183 | 186 |
184 db.Close(); | 187 db.Close(); |
185 ASSERT_FALSE(db.IsOpen()); | 188 ASSERT_FALSE(db.IsOpen()); |
186 | 189 |
187 // Reading from an existing database should open the database. | 190 // Reading from an existing database should open the database. |
188 CheckValuesMatch(&db, values); | 191 CheckValuesMatch(&db, values); |
189 EXPECT_TRUE(db.IsOpen()); | 192 EXPECT_TRUE(db.IsOpen()); |
(...skipping 17 matching lines...) Expand all Loading... |
207 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 210 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
208 } | 211 } |
209 | 212 |
210 TEST(DomStorageDatabaseTest, TestLazyOpenUpgradesDatabase) { | 213 TEST(DomStorageDatabaseTest, TestLazyOpenUpgradesDatabase) { |
211 // This test needs to operate with a file on disk so that we | 214 // 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 | 215 // 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 | 216 // so that LazyOpen sees there is work to do (LazyOpen will return |
214 // early if the database is already open). | 217 // early if the database is already open). |
215 base::ScopedTempDir temp_dir; | 218 base::ScopedTempDir temp_dir; |
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 219 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
217 base::FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase
.db"); | 220 base::FilePath file_name = |
| 221 temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
218 | 222 |
219 DomStorageDatabase db(file_name); | 223 DomStorageDatabase db(file_name); |
220 db.db_.reset(new sql::Connection()); | 224 db.db_.reset(new sql::Connection()); |
221 ASSERT_TRUE(db.db_->Open(file_name)); | 225 ASSERT_TRUE(db.db_->Open(file_name)); |
222 CreateV1Table(db.db_.get()); | 226 CreateV1Table(db.db_.get()); |
223 db.Close(); | 227 db.Close(); |
224 | 228 |
225 EXPECT_TRUE(db.LazyOpen(true)); | 229 EXPECT_TRUE(db.LazyOpen(true)); |
226 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 230 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
227 } | 231 } |
(...skipping 13 matching lines...) Expand all Loading... |
241 | 245 |
242 ValuesMap storage; | 246 ValuesMap storage; |
243 CreateMapWithValues(&storage); | 247 CreateMapWithValues(&storage); |
244 | 248 |
245 ASSERT_TRUE(db.CommitChanges(false, storage)); | 249 ASSERT_TRUE(db.CommitChanges(false, storage)); |
246 CheckValuesMatch(&db, storage); | 250 CheckValuesMatch(&db, storage); |
247 | 251 |
248 // Insert some values, clearing the database first. | 252 // Insert some values, clearing the database first. |
249 storage.clear(); | 253 storage.clear(); |
250 storage[ASCIIToUTF16("another_key")] = | 254 storage[ASCIIToUTF16("another_key")] = |
251 NullableString16(ASCIIToUTF16("test"), false); | 255 base::NullableString16(ASCIIToUTF16("test"), false); |
252 ASSERT_TRUE(db.CommitChanges(true, storage)); | 256 ASSERT_TRUE(db.CommitChanges(true, storage)); |
253 CheckValuesMatch(&db, storage); | 257 CheckValuesMatch(&db, storage); |
254 | 258 |
255 // Now clear the values without inserting any new ones. | 259 // Now clear the values without inserting any new ones. |
256 storage.clear(); | 260 storage.clear(); |
257 ASSERT_TRUE(db.CommitChanges(true, storage)); | 261 ASSERT_TRUE(db.CommitChanges(true, storage)); |
258 CheckValuesMatch(&db, storage); | 262 CheckValuesMatch(&db, storage); |
259 } | 263 } |
260 | 264 |
261 TEST(DomStorageDatabaseTest, UpgradeFromV1ToV2WithData) { | 265 TEST(DomStorageDatabaseTest, UpgradeFromV1ToV2WithData) { |
262 const base::string16 kCannedKey = ASCIIToUTF16("foo"); | 266 const base::string16 kCannedKey = ASCIIToUTF16("foo"); |
263 const NullableString16 kCannedValue(ASCIIToUTF16("bar"), false); | 267 const base::NullableString16 kCannedValue(ASCIIToUTF16("bar"), false); |
264 ValuesMap expected; | 268 ValuesMap expected; |
265 expected[kCannedKey] = kCannedValue; | 269 expected[kCannedKey] = kCannedValue; |
266 | 270 |
267 DomStorageDatabase db; | 271 DomStorageDatabase db; |
268 db.db_.reset(new sql::Connection()); | 272 db.db_.reset(new sql::Connection()); |
269 ASSERT_TRUE(db.db_->OpenInMemory()); | 273 ASSERT_TRUE(db.db_->OpenInMemory()); |
270 CreateV1Table(db.db_.get()); | 274 CreateV1Table(db.db_.get()); |
271 InsertDataV1(db.db_.get(), kCannedKey, kCannedValue.string()); | 275 InsertDataV1(db.db_.get(), kCannedKey, kCannedValue.string()); |
272 | 276 |
273 ASSERT_TRUE(db.UpgradeVersion1To2()); | 277 ASSERT_TRUE(db.UpgradeVersion1To2()); |
274 | 278 |
275 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); | 279 EXPECT_EQ(DomStorageDatabase::V2, db.DetectSchemaVersion()); |
276 | 280 |
277 CheckValuesMatch(&db, expected); | 281 CheckValuesMatch(&db, expected); |
278 } | 282 } |
279 | 283 |
280 TEST(DomStorageDatabaseTest, TestSimpleRemoveOneValue) { | 284 TEST(DomStorageDatabaseTest, TestSimpleRemoveOneValue) { |
281 DomStorageDatabase db; | 285 DomStorageDatabase db; |
282 | 286 |
283 ASSERT_TRUE(db.LazyOpen(true)); | 287 ASSERT_TRUE(db.LazyOpen(true)); |
284 const base::string16 kCannedKey = ASCIIToUTF16("test"); | 288 const base::string16 kCannedKey = ASCIIToUTF16("test"); |
285 const NullableString16 kCannedValue(ASCIIToUTF16("data"), false); | 289 const base::NullableString16 kCannedValue(ASCIIToUTF16("data"), false); |
286 ValuesMap expected; | 290 ValuesMap expected; |
287 expected[kCannedKey] = kCannedValue; | 291 expected[kCannedKey] = kCannedValue; |
288 | 292 |
289 // First write some data into the database. | 293 // First write some data into the database. |
290 ASSERT_TRUE(db.CommitChanges(false, expected)); | 294 ASSERT_TRUE(db.CommitChanges(false, expected)); |
291 CheckValuesMatch(&db, expected); | 295 CheckValuesMatch(&db, expected); |
292 | 296 |
293 ValuesMap values; | 297 ValuesMap values; |
294 // A null string in the map should mean that that key gets | 298 // A null string in the map should mean that that key gets |
295 // removed. | 299 // removed. |
296 values[kCannedKey] = NullableString16(true); | 300 values[kCannedKey] = base::NullableString16(true); |
297 EXPECT_TRUE(db.CommitChanges(false, values)); | 301 EXPECT_TRUE(db.CommitChanges(false, values)); |
298 | 302 |
299 expected.clear(); | 303 expected.clear(); |
300 CheckValuesMatch(&db, expected); | 304 CheckValuesMatch(&db, expected); |
301 } | 305 } |
302 | 306 |
303 TEST(DomStorageDatabaseTest, TestCanOpenAndReadWebCoreDatabase) { | 307 TEST(DomStorageDatabaseTest, TestCanOpenAndReadWebCoreDatabase) { |
304 base::FilePath webcore_database; | 308 base::FilePath webcore_database; |
305 PathService::Get(base::DIR_SOURCE_ROOT, &webcore_database); | 309 PathService::Get(base::DIR_SOURCE_ROOT, &webcore_database); |
306 webcore_database = webcore_database.AppendASCII("webkit"); | 310 webcore_database = webcore_database.AppendASCII("webkit"); |
(...skipping 20 matching lines...) Expand all Loading... |
327 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string()); | 331 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string()); |
328 | 332 |
329 it = values.find(ASCIIToUTF16("not_there")); | 333 it = values.find(ASCIIToUTF16("not_there")); |
330 EXPECT_TRUE(it == values.end()); | 334 EXPECT_TRUE(it == values.end()); |
331 } | 335 } |
332 | 336 |
333 TEST(DomStorageDatabaseTest, TestCanOpenFileThatIsNotADatabase) { | 337 TEST(DomStorageDatabaseTest, TestCanOpenFileThatIsNotADatabase) { |
334 // Write into the temporary file first. | 338 // Write into the temporary file first. |
335 base::ScopedTempDir temp_dir; | 339 base::ScopedTempDir temp_dir; |
336 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 340 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
337 base::FilePath file_name = temp_dir.path().AppendASCII("TestDomStorageDatabase
.db"); | 341 base::FilePath file_name = |
| 342 temp_dir.path().AppendASCII("TestDomStorageDatabase.db"); |
338 | 343 |
339 const char kData[] = "I am not a database."; | 344 const char kData[] = "I am not a database."; |
340 file_util::WriteFile(file_name, kData, strlen(kData)); | 345 file_util::WriteFile(file_name, kData, strlen(kData)); |
341 | 346 |
342 { | 347 { |
343 // Try and open the file. As it's not a database, we should end up deleting | 348 // 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 | 349 // it and creating a new, valid file, so everything should actually |
345 // succeed. | 350 // succeed. |
346 DomStorageDatabase db(file_name); | 351 DomStorageDatabase db(file_name); |
347 ValuesMap values; | 352 ValuesMap values; |
(...skipping 19 matching lines...) Expand all Loading... |
367 | 372 |
368 db.ReadAllValues(&values); | 373 db.ReadAllValues(&values); |
369 EXPECT_EQ(0u, values.size()); | 374 EXPECT_EQ(0u, values.size()); |
370 EXPECT_FALSE(db.IsOpen()); | 375 EXPECT_FALSE(db.IsOpen()); |
371 | 376 |
372 EXPECT_TRUE(file_util::PathExists(temp_dir.path())); | 377 EXPECT_TRUE(file_util::PathExists(temp_dir.path())); |
373 } | 378 } |
374 } | 379 } |
375 | 380 |
376 } // namespace dom_storage | 381 } // namespace dom_storage |
OLD | NEW |