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" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 176 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
177 | 177 |
178 // Need to write something to ensure that the database is created. | 178 // Need to write something to ensure that the database is created. |
179 NullableString16 old_value; | 179 NullableString16 old_value; |
180 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 180 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
181 ASSERT_TRUE(old_value.is_null()); | 181 ASSERT_TRUE(old_value.is_null()); |
182 EXPECT_TRUE(area->is_initial_import_done_); | 182 EXPECT_TRUE(area->is_initial_import_done_); |
183 EXPECT_TRUE(area->commit_batch_.get()); | 183 EXPECT_TRUE(area->commit_batch_.get()); |
184 EXPECT_EQ(0, area->commit_batches_in_flight_); | 184 EXPECT_EQ(0, area->commit_batches_in_flight_); |
185 | 185 |
186 MessageLoop::current()->RunAllPending(); | 186 MessageLoop::current()->RunUntilIdle(); |
187 | 187 |
188 EXPECT_FALSE(area->commit_batch_.get()); | 188 EXPECT_FALSE(area->commit_batch_.get()); |
189 EXPECT_EQ(0, area->commit_batches_in_flight_); | 189 EXPECT_EQ(0, area->commit_batches_in_flight_); |
190 database = static_cast<LocalStorageDatabaseAdapter*>( | 190 database = static_cast<LocalStorageDatabaseAdapter*>( |
191 area->backing_.get())->db_.get(); | 191 area->backing_.get())->db_.get(); |
192 EXPECT_TRUE(database->IsOpen()); | 192 EXPECT_TRUE(database->IsOpen()); |
193 EXPECT_EQ(1u, area->Length()); | 193 EXPECT_EQ(1u, area->Length()); |
194 EXPECT_EQ(kValue, area->GetItem(kKey).string()); | 194 EXPECT_EQ(kValue, area->GetItem(kKey).string()); |
195 | 195 |
196 // Verify the content made it to the in memory database. | 196 // Verify the content made it to the in memory database. |
(...skipping 28 matching lines...) Expand all Loading... |
225 EXPECT_FALSE(area->commit_batch_.get()); | 225 EXPECT_FALSE(area->commit_batch_.get()); |
226 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 226 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
227 EXPECT_TRUE(area->HasUncommittedChanges()); | 227 EXPECT_TRUE(area->HasUncommittedChanges()); |
228 EXPECT_TRUE(area->commit_batch_.get()); | 228 EXPECT_TRUE(area->commit_batch_.get()); |
229 EXPECT_FALSE(area->commit_batch_->clear_all_first); | 229 EXPECT_FALSE(area->commit_batch_->clear_all_first); |
230 EXPECT_EQ(1u, area->commit_batch_->changed_values.size()); | 230 EXPECT_EQ(1u, area->commit_batch_->changed_values.size()); |
231 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); | 231 EXPECT_TRUE(area->SetItem(kKey2, kValue2, &old_value)); |
232 EXPECT_TRUE(area->commit_batch_.get()); | 232 EXPECT_TRUE(area->commit_batch_.get()); |
233 EXPECT_FALSE(area->commit_batch_->clear_all_first); | 233 EXPECT_FALSE(area->commit_batch_->clear_all_first); |
234 EXPECT_EQ(2u, area->commit_batch_->changed_values.size()); | 234 EXPECT_EQ(2u, area->commit_batch_->changed_values.size()); |
235 MessageLoop::current()->RunAllPending(); | 235 MessageLoop::current()->RunUntilIdle(); |
236 EXPECT_FALSE(area->HasUncommittedChanges()); | 236 EXPECT_FALSE(area->HasUncommittedChanges()); |
237 EXPECT_FALSE(area->commit_batch_.get()); | 237 EXPECT_FALSE(area->commit_batch_.get()); |
238 EXPECT_EQ(0, area->commit_batches_in_flight_); | 238 EXPECT_EQ(0, area->commit_batches_in_flight_); |
239 // Verify the changes made it to the database. | 239 // Verify the changes made it to the database. |
240 values.clear(); | 240 values.clear(); |
241 area->backing_->ReadAllValues(&values); | 241 area->backing_->ReadAllValues(&values); |
242 EXPECT_EQ(2u, values.size()); | 242 EXPECT_EQ(2u, values.size()); |
243 EXPECT_EQ(kValue, values[kKey].string()); | 243 EXPECT_EQ(kValue, values[kKey].string()); |
244 EXPECT_EQ(kValue2, values[kKey2].string()); | 244 EXPECT_EQ(kValue2, values[kKey2].string()); |
245 | 245 |
246 // See that clear is handled properly. | 246 // See that clear is handled properly. |
247 EXPECT_TRUE(area->Clear()); | 247 EXPECT_TRUE(area->Clear()); |
248 EXPECT_TRUE(area->commit_batch_.get()); | 248 EXPECT_TRUE(area->commit_batch_.get()); |
249 EXPECT_TRUE(area->commit_batch_->clear_all_first); | 249 EXPECT_TRUE(area->commit_batch_->clear_all_first); |
250 EXPECT_TRUE(area->commit_batch_->changed_values.empty()); | 250 EXPECT_TRUE(area->commit_batch_->changed_values.empty()); |
251 MessageLoop::current()->RunAllPending(); | 251 MessageLoop::current()->RunUntilIdle(); |
252 EXPECT_FALSE(area->commit_batch_.get()); | 252 EXPECT_FALSE(area->commit_batch_.get()); |
253 EXPECT_EQ(0, area->commit_batches_in_flight_); | 253 EXPECT_EQ(0, area->commit_batches_in_flight_); |
254 // Verify the changes made it to the database. | 254 // Verify the changes made it to the database. |
255 values.clear(); | 255 values.clear(); |
256 area->backing_->ReadAllValues(&values); | 256 area->backing_->ReadAllValues(&values); |
257 EXPECT_TRUE(values.empty()); | 257 EXPECT_TRUE(values.empty()); |
258 | 258 |
259 // See that if changes accrue while a commit is "in flight" | 259 // See that if changes accrue while a commit is "in flight" |
260 // those will also get committed. | 260 // those will also get committed. |
261 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 261 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
262 EXPECT_TRUE(area->HasUncommittedChanges()); | 262 EXPECT_TRUE(area->HasUncommittedChanges()); |
263 // At this point the OnCommitTimer task has been posted. We inject | 263 // At this point the OnCommitTimer task has been posted. We inject |
264 // another task in the queue that will execute after the timer task, | 264 // another task in the queue that will execute after the timer task, |
265 // but before the CommitChanges task. From within our injected task, | 265 // but before the CommitChanges task. From within our injected task, |
266 // we'll make an additional SetItem() call. | 266 // we'll make an additional SetItem() call. |
267 MessageLoop::current()->PostTask( | 267 MessageLoop::current()->PostTask( |
268 FROM_HERE, | 268 FROM_HERE, |
269 base::Bind(&DomStorageAreaTest::InjectedCommitSequencingTask, | 269 base::Bind(&DomStorageAreaTest::InjectedCommitSequencingTask, |
270 base::Unretained(this), area)); | 270 base::Unretained(this), area)); |
271 MessageLoop::current()->RunAllPending(); | 271 MessageLoop::current()->RunUntilIdle(); |
272 EXPECT_TRUE(area->HasOneRef()); | 272 EXPECT_TRUE(area->HasOneRef()); |
273 EXPECT_FALSE(area->HasUncommittedChanges()); | 273 EXPECT_FALSE(area->HasUncommittedChanges()); |
274 // Verify the changes made it to the database. | 274 // Verify the changes made it to the database. |
275 values.clear(); | 275 values.clear(); |
276 area->backing_->ReadAllValues(&values); | 276 area->backing_->ReadAllValues(&values); |
277 EXPECT_EQ(2u, values.size()); | 277 EXPECT_EQ(2u, values.size()); |
278 EXPECT_EQ(kValue, values[kKey].string()); | 278 EXPECT_EQ(kValue, values[kKey].string()); |
279 EXPECT_EQ(kValue2, values[kKey2].string()); | 279 EXPECT_EQ(kValue2, values[kKey2].string()); |
280 } | 280 } |
281 | 281 |
(...skipping 10 matching lines...) Expand all Loading... |
292 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | 292 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
293 new VerifyChangesCommittedDatabase()); | 293 new VerifyChangesCommittedDatabase()); |
294 | 294 |
295 ValuesMap values; | 295 ValuesMap values; |
296 NullableString16 old_value; | 296 NullableString16 old_value; |
297 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 297 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
298 EXPECT_TRUE(area->HasUncommittedChanges()); | 298 EXPECT_TRUE(area->HasUncommittedChanges()); |
299 area->backing_->ReadAllValues(&values); | 299 area->backing_->ReadAllValues(&values); |
300 EXPECT_TRUE(values.empty()); // not committed yet | 300 EXPECT_TRUE(values.empty()); // not committed yet |
301 area->Shutdown(); | 301 area->Shutdown(); |
302 MessageLoop::current()->RunAllPending(); | 302 MessageLoop::current()->RunUntilIdle(); |
303 EXPECT_TRUE(area->HasOneRef()); | 303 EXPECT_TRUE(area->HasOneRef()); |
304 EXPECT_FALSE(area->backing_.get()); | 304 EXPECT_FALSE(area->backing_.get()); |
305 // The VerifyChangesCommittedDatabase destructor verifies values | 305 // The VerifyChangesCommittedDatabase destructor verifies values |
306 // were committed. | 306 // were committed. |
307 } | 307 } |
308 | 308 |
309 TEST_F(DomStorageAreaTest, DeleteOrigin) { | 309 TEST_F(DomStorageAreaTest, DeleteOrigin) { |
310 base::ScopedTempDir temp_dir; | 310 base::ScopedTempDir temp_dir; |
311 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 311 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
312 scoped_refptr<DomStorageArea> area( | 312 scoped_refptr<DomStorageArea> area( |
313 new DomStorageArea(kOrigin, | 313 new DomStorageArea(kOrigin, |
314 temp_dir.path(), | 314 temp_dir.path(), |
315 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 315 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
316 | 316 |
317 // This test puts files on disk. | 317 // This test puts files on disk. |
318 FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 318 FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
319 area->backing_.get())->db_->file_path(); | 319 area->backing_.get())->db_->file_path(); |
320 FilePath db_journal_file_path = | 320 FilePath db_journal_file_path = |
321 DomStorageDatabase::GetJournalFilePath(db_file_path); | 321 DomStorageDatabase::GetJournalFilePath(db_file_path); |
322 | 322 |
323 // Nothing bad should happen when invoked w/o any files on disk. | 323 // Nothing bad should happen when invoked w/o any files on disk. |
324 area->DeleteOrigin(); | 324 area->DeleteOrigin(); |
325 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 325 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
326 | 326 |
327 // Commit something in the database and then delete. | 327 // Commit something in the database and then delete. |
328 NullableString16 old_value; | 328 NullableString16 old_value; |
329 area->SetItem(kKey, kValue, &old_value); | 329 area->SetItem(kKey, kValue, &old_value); |
330 MessageLoop::current()->RunAllPending(); | 330 MessageLoop::current()->RunUntilIdle(); |
331 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 331 EXPECT_TRUE(file_util::PathExists(db_file_path)); |
332 area->DeleteOrigin(); | 332 area->DeleteOrigin(); |
333 EXPECT_EQ(0u, area->Length()); | 333 EXPECT_EQ(0u, area->Length()); |
334 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 334 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
335 EXPECT_FALSE(file_util::PathExists(db_journal_file_path)); | 335 EXPECT_FALSE(file_util::PathExists(db_journal_file_path)); |
336 | 336 |
337 // Put some uncommitted changes to a non-existing database in | 337 // Put some uncommitted changes to a non-existing database in |
338 // and then delete. No file ever gets created in this case. | 338 // and then delete. No file ever gets created in this case. |
339 area->SetItem(kKey, kValue, &old_value); | 339 area->SetItem(kKey, kValue, &old_value); |
340 EXPECT_TRUE(area->HasUncommittedChanges()); | 340 EXPECT_TRUE(area->HasUncommittedChanges()); |
341 EXPECT_EQ(1u, area->Length()); | 341 EXPECT_EQ(1u, area->Length()); |
342 area->DeleteOrigin(); | 342 area->DeleteOrigin(); |
343 EXPECT_TRUE(area->HasUncommittedChanges()); | 343 EXPECT_TRUE(area->HasUncommittedChanges()); |
344 EXPECT_EQ(0u, area->Length()); | 344 EXPECT_EQ(0u, area->Length()); |
345 MessageLoop::current()->RunAllPending(); | 345 MessageLoop::current()->RunUntilIdle(); |
346 EXPECT_FALSE(area->HasUncommittedChanges()); | 346 EXPECT_FALSE(area->HasUncommittedChanges()); |
347 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 347 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
348 | 348 |
349 // Put some uncommitted changes to a an existing database in | 349 // Put some uncommitted changes to a an existing database in |
350 // and then delete. | 350 // and then delete. |
351 area->SetItem(kKey, kValue, &old_value); | 351 area->SetItem(kKey, kValue, &old_value); |
352 MessageLoop::current()->RunAllPending(); | 352 MessageLoop::current()->RunUntilIdle(); |
353 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 353 EXPECT_TRUE(file_util::PathExists(db_file_path)); |
354 area->SetItem(kKey2, kValue2, &old_value); | 354 area->SetItem(kKey2, kValue2, &old_value); |
355 EXPECT_TRUE(area->HasUncommittedChanges()); | 355 EXPECT_TRUE(area->HasUncommittedChanges()); |
356 EXPECT_EQ(2u, area->Length()); | 356 EXPECT_EQ(2u, area->Length()); |
357 area->DeleteOrigin(); | 357 area->DeleteOrigin(); |
358 EXPECT_TRUE(area->HasUncommittedChanges()); | 358 EXPECT_TRUE(area->HasUncommittedChanges()); |
359 EXPECT_EQ(0u, area->Length()); | 359 EXPECT_EQ(0u, area->Length()); |
360 MessageLoop::current()->RunAllPending(); | 360 MessageLoop::current()->RunUntilIdle(); |
361 EXPECT_FALSE(area->HasUncommittedChanges()); | 361 EXPECT_FALSE(area->HasUncommittedChanges()); |
362 // Since the area had uncommitted changes at the time delete | 362 // Since the area had uncommitted changes at the time delete |
363 // was called, the file will linger until the shutdown time. | 363 // was called, the file will linger until the shutdown time. |
364 EXPECT_TRUE(file_util::PathExists(db_file_path)); | 364 EXPECT_TRUE(file_util::PathExists(db_file_path)); |
365 area->Shutdown(); | 365 area->Shutdown(); |
366 MessageLoop::current()->RunAllPending(); | 366 MessageLoop::current()->RunUntilIdle(); |
367 EXPECT_FALSE(file_util::PathExists(db_file_path)); | 367 EXPECT_FALSE(file_util::PathExists(db_file_path)); |
368 } | 368 } |
369 | 369 |
370 TEST_F(DomStorageAreaTest, PurgeMemory) { | 370 TEST_F(DomStorageAreaTest, PurgeMemory) { |
371 base::ScopedTempDir temp_dir; | 371 base::ScopedTempDir temp_dir; |
372 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 372 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
373 scoped_refptr<DomStorageArea> area( | 373 scoped_refptr<DomStorageArea> area( |
374 new DomStorageArea(kOrigin, | 374 new DomStorageArea(kOrigin, |
375 temp_dir.path(), | 375 temp_dir.path(), |
376 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); | 376 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); |
(...skipping 23 matching lines...) Expand all Loading... |
400 EXPECT_TRUE(area->HasUncommittedChanges()); | 400 EXPECT_TRUE(area->HasUncommittedChanges()); |
401 area->PurgeMemory(); | 401 area->PurgeMemory(); |
402 EXPECT_TRUE(area->is_initial_import_done_); | 402 EXPECT_TRUE(area->is_initial_import_done_); |
403 EXPECT_TRUE(area->HasUncommittedChanges()); | 403 EXPECT_TRUE(area->HasUncommittedChanges()); |
404 new_backing = static_cast<LocalStorageDatabaseAdapter*>( | 404 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
405 area->backing_.get())->db_.get(); | 405 area->backing_.get())->db_.get(); |
406 EXPECT_EQ(original_backing, new_backing); | 406 EXPECT_EQ(original_backing, new_backing); |
407 EXPECT_EQ(original_map, area->map_.get()); | 407 EXPECT_EQ(original_map, area->map_.get()); |
408 | 408 |
409 // Commit the changes from above, | 409 // Commit the changes from above, |
410 MessageLoop::current()->RunAllPending(); | 410 MessageLoop::current()->RunUntilIdle(); |
411 EXPECT_FALSE(area->HasUncommittedChanges()); | 411 EXPECT_FALSE(area->HasUncommittedChanges()); |
412 new_backing = static_cast<LocalStorageDatabaseAdapter*>( | 412 new_backing = static_cast<LocalStorageDatabaseAdapter*>( |
413 area->backing_.get())->db_.get(); | 413 area->backing_.get())->db_.get(); |
414 EXPECT_EQ(original_backing, new_backing); | 414 EXPECT_EQ(original_backing, new_backing); |
415 EXPECT_EQ(original_map, area->map_.get()); | 415 EXPECT_EQ(original_map, area->map_.get()); |
416 | 416 |
417 // Should drop caches and reset database connections | 417 // Should drop caches and reset database connections |
418 // when invoked on an area that's loaded up primed. | 418 // when invoked on an area that's loaded up primed. |
419 area->PurgeMemory(); | 419 area->PurgeMemory(); |
420 EXPECT_FALSE(area->is_initial_import_done_); | 420 EXPECT_FALSE(area->is_initial_import_done_); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 EXPECT_EQ( | 463 EXPECT_EQ( |
464 FilePath().AppendASCII("-journal"), | 464 FilePath().AppendASCII("-journal"), |
465 DomStorageDatabase::GetJournalFilePath(FilePath())); | 465 DomStorageDatabase::GetJournalFilePath(FilePath())); |
466 EXPECT_EQ( | 466 EXPECT_EQ( |
467 FilePath().AppendASCII(".extensiononly-journal"), | 467 FilePath().AppendASCII(".extensiononly-journal"), |
468 DomStorageDatabase::GetJournalFilePath( | 468 DomStorageDatabase::GetJournalFilePath( |
469 FilePath().AppendASCII(".extensiononly"))); | 469 FilePath().AppendASCII(".extensiononly"))); |
470 } | 470 } |
471 | 471 |
472 } // namespace dom_storage | 472 } // namespace dom_storage |
OLD | NEW |