OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/location.h" | |
9 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
12 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
14 #include "base/time/time.h" | 12 #include "base/time/time.h" |
15 #include "content/browser/dom_storage/dom_storage_area.h" | 13 #include "content/browser/dom_storage/dom_storage_area.h" |
16 #include "content/browser/dom_storage/dom_storage_database.h" | 14 #include "content/browser/dom_storage/dom_storage_database.h" |
17 #include "content/browser/dom_storage/dom_storage_database_adapter.h" | 15 #include "content/browser/dom_storage/dom_storage_database_adapter.h" |
18 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 16 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
19 #include "content/browser/dom_storage/local_storage_database_adapter.h" | 17 #include "content/browser/dom_storage/local_storage_database_adapter.h" |
20 #include "content/common/dom_storage/dom_storage_types.h" | 18 #include "content/common/dom_storage/dom_storage_types.h" |
(...skipping 19 matching lines...) Expand all Loading... |
40 const base::string16 kValue; | 38 const base::string16 kValue; |
41 const base::string16 kKey2; | 39 const base::string16 kKey2; |
42 const base::string16 kValue2; | 40 const base::string16 kValue2; |
43 | 41 |
44 // Method used in the CommitTasks test case. | 42 // Method used in the CommitTasks test case. |
45 void InjectedCommitSequencingTask1( | 43 void InjectedCommitSequencingTask1( |
46 const scoped_refptr<DOMStorageArea>& area) { | 44 const scoped_refptr<DOMStorageArea>& area) { |
47 // At this point the StartCommitTimer task has run and | 45 // At this point the StartCommitTimer task has run and |
48 // the OnCommitTimer task is queued. We want to inject after | 46 // the OnCommitTimer task is queued. We want to inject after |
49 // that. | 47 // that. |
50 base::ThreadTaskRunnerHandle::Get()->PostTask( | 48 base::MessageLoop::current()->PostTask( |
51 FROM_HERE, | 49 FROM_HERE, |
52 base::Bind(&DOMStorageAreaTest::InjectedCommitSequencingTask2, | 50 base::Bind(&DOMStorageAreaTest::InjectedCommitSequencingTask2, |
53 base::Unretained(this), area)); | 51 base::Unretained(this), |
| 52 area)); |
54 } | 53 } |
55 | 54 |
56 void InjectedCommitSequencingTask2( | 55 void InjectedCommitSequencingTask2( |
57 const scoped_refptr<DOMStorageArea>& area) { | 56 const scoped_refptr<DOMStorageArea>& area) { |
58 // At this point the OnCommitTimer has run. | 57 // At this point the OnCommitTimer has run. |
59 // Verify that it put a commit in flight. | 58 // Verify that it put a commit in flight. |
60 EXPECT_EQ(1, area->commit_batches_in_flight_); | 59 EXPECT_EQ(1, area->commit_batches_in_flight_); |
61 EXPECT_FALSE(area->commit_batch_.get()); | 60 EXPECT_FALSE(area->commit_batch_.get()); |
62 EXPECT_TRUE(area->HasUncommittedChanges()); | 61 EXPECT_TRUE(area->HasUncommittedChanges()); |
63 // Make additional change and verify that a new commit batch | 62 // Make additional change and verify that a new commit batch |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 166 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
168 ASSERT_TRUE(old_value.is_null()); | 167 ASSERT_TRUE(old_value.is_null()); |
169 | 168 |
170 // Check that saving a value has still left us without a backing database. | 169 // Check that saving a value has still left us without a backing database. |
171 EXPECT_EQ(NULL, area->backing_.get()); | 170 EXPECT_EQ(NULL, area->backing_.get()); |
172 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); | 171 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); |
173 } | 172 } |
174 | 173 |
175 // This should set up a DOMStorageArea that is correctly backed to disk. | 174 // This should set up a DOMStorageArea that is correctly backed to disk. |
176 { | 175 { |
177 scoped_refptr<DOMStorageArea> area( | 176 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
178 new DOMStorageArea(kOrigin, temp_dir.path(), | 177 kOrigin, |
179 new MockDOMStorageTaskRunner( | 178 temp_dir.path(), |
180 base::ThreadTaskRunnerHandle::Get().get()))); | 179 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
181 | 180 |
182 EXPECT_TRUE(area->backing_.get()); | 181 EXPECT_TRUE(area->backing_.get()); |
183 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( | 182 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( |
184 area->backing_.get())->db_.get(); | 183 area->backing_.get())->db_.get(); |
185 EXPECT_FALSE(database->IsOpen()); | 184 EXPECT_FALSE(database->IsOpen()); |
186 EXPECT_FALSE(area->is_initial_import_done_); | 185 EXPECT_FALSE(area->is_initial_import_done_); |
187 | 186 |
188 // Inject an in-memory db to speed up the test. | 187 // Inject an in-memory db to speed up the test. |
189 // We will verify that something is written into the database but not | 188 // We will verify that something is written into the database but not |
190 // that a file is written to disk - DOMStorageDatabase unit tests cover | 189 // that a file is written to disk - DOMStorageDatabase unit tests cover |
(...skipping 24 matching lines...) Expand all Loading... |
215 EXPECT_EQ(1u, values.size()); | 214 EXPECT_EQ(1u, values.size()); |
216 EXPECT_EQ(kValue, values[kKey].string()); | 215 EXPECT_EQ(kValue, values[kKey].string()); |
217 } | 216 } |
218 } | 217 } |
219 | 218 |
220 TEST_F(DOMStorageAreaTest, CommitTasks) { | 219 TEST_F(DOMStorageAreaTest, CommitTasks) { |
221 base::ScopedTempDir temp_dir; | 220 base::ScopedTempDir temp_dir; |
222 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 221 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
223 | 222 |
224 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 223 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
225 kOrigin, temp_dir.path(), | 224 kOrigin, |
226 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 225 temp_dir.path(), |
| 226 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
227 // Inject an in-memory db to speed up the test. | 227 // Inject an in-memory db to speed up the test. |
228 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 228 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
229 | 229 |
230 // Unrelated to commits, but while we're here, see that querying Length() | 230 // Unrelated to commits, but while we're here, see that querying Length() |
231 // causes the backing database to be opened and presumably read from. | 231 // causes the backing database to be opened and presumably read from. |
232 EXPECT_FALSE(area->is_initial_import_done_); | 232 EXPECT_FALSE(area->is_initial_import_done_); |
233 EXPECT_EQ(0u, area->Length()); | 233 EXPECT_EQ(0u, area->Length()); |
234 EXPECT_TRUE(area->is_initial_import_done_); | 234 EXPECT_TRUE(area->is_initial_import_done_); |
235 | 235 |
236 DOMStorageValuesMap values; | 236 DOMStorageValuesMap values; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 area->backing_->ReadAllValues(&values); | 293 area->backing_->ReadAllValues(&values); |
294 EXPECT_EQ(2u, values.size()); | 294 EXPECT_EQ(2u, values.size()); |
295 EXPECT_EQ(kValue, values[kKey].string()); | 295 EXPECT_EQ(kValue, values[kKey].string()); |
296 EXPECT_EQ(kValue2, values[kKey2].string()); | 296 EXPECT_EQ(kValue2, values[kKey2].string()); |
297 } | 297 } |
298 | 298 |
299 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { | 299 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { |
300 base::ScopedTempDir temp_dir; | 300 base::ScopedTempDir temp_dir; |
301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
302 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 302 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
303 kOrigin, temp_dir.path(), | 303 kOrigin, |
304 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 304 temp_dir.path(), |
| 305 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
305 | 306 |
306 // Inject an in-memory db to speed up the test and also to verify | 307 // Inject an in-memory db to speed up the test and also to verify |
307 // the final changes are commited in it's dtor. | 308 // the final changes are commited in it's dtor. |
308 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | 309 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
309 new VerifyChangesCommittedDatabase()); | 310 new VerifyChangesCommittedDatabase()); |
310 | 311 |
311 DOMStorageValuesMap values; | 312 DOMStorageValuesMap values; |
312 base::NullableString16 old_value; | 313 base::NullableString16 old_value; |
313 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 314 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
314 EXPECT_TRUE(area->HasUncommittedChanges()); | 315 EXPECT_TRUE(area->HasUncommittedChanges()); |
315 area->backing_->ReadAllValues(&values); | 316 area->backing_->ReadAllValues(&values); |
316 EXPECT_TRUE(values.empty()); // not committed yet | 317 EXPECT_TRUE(values.empty()); // not committed yet |
317 area->Shutdown(); | 318 area->Shutdown(); |
318 base::MessageLoop::current()->RunUntilIdle(); | 319 base::MessageLoop::current()->RunUntilIdle(); |
319 EXPECT_TRUE(area->HasOneRef()); | 320 EXPECT_TRUE(area->HasOneRef()); |
320 EXPECT_FALSE(area->backing_.get()); | 321 EXPECT_FALSE(area->backing_.get()); |
321 // The VerifyChangesCommittedDatabase destructor verifies values | 322 // The VerifyChangesCommittedDatabase destructor verifies values |
322 // were committed. | 323 // were committed. |
323 } | 324 } |
324 | 325 |
325 TEST_F(DOMStorageAreaTest, DeleteOrigin) { | 326 TEST_F(DOMStorageAreaTest, DeleteOrigin) { |
326 base::ScopedTempDir temp_dir; | 327 base::ScopedTempDir temp_dir; |
327 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 328 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
328 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 329 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
329 kOrigin, temp_dir.path(), | 330 kOrigin, |
330 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 331 temp_dir.path(), |
| 332 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
331 | 333 |
332 // This test puts files on disk. | 334 // This test puts files on disk. |
333 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 335 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
334 area->backing_.get())->db_->file_path(); | 336 area->backing_.get())->db_->file_path(); |
335 base::FilePath db_journal_file_path = | 337 base::FilePath db_journal_file_path = |
336 DOMStorageDatabase::GetJournalFilePath(db_file_path); | 338 DOMStorageDatabase::GetJournalFilePath(db_file_path); |
337 | 339 |
338 // Nothing bad should happen when invoked w/o any files on disk. | 340 // Nothing bad should happen when invoked w/o any files on disk. |
339 area->DeleteOrigin(); | 341 area->DeleteOrigin(); |
340 EXPECT_FALSE(base::PathExists(db_file_path)); | 342 EXPECT_FALSE(base::PathExists(db_file_path)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 EXPECT_TRUE(base::PathExists(db_file_path)); | 381 EXPECT_TRUE(base::PathExists(db_file_path)); |
380 area->Shutdown(); | 382 area->Shutdown(); |
381 base::MessageLoop::current()->RunUntilIdle(); | 383 base::MessageLoop::current()->RunUntilIdle(); |
382 EXPECT_FALSE(base::PathExists(db_file_path)); | 384 EXPECT_FALSE(base::PathExists(db_file_path)); |
383 } | 385 } |
384 | 386 |
385 TEST_F(DOMStorageAreaTest, PurgeMemory) { | 387 TEST_F(DOMStorageAreaTest, PurgeMemory) { |
386 base::ScopedTempDir temp_dir; | 388 base::ScopedTempDir temp_dir; |
387 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 389 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
388 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 390 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
389 kOrigin, temp_dir.path(), | 391 kOrigin, |
390 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); | 392 temp_dir.path(), |
| 393 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); |
391 | 394 |
392 // Inject an in-memory db to speed up the test. | 395 // Inject an in-memory db to speed up the test. |
393 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 396 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
394 | 397 |
395 // Unowned ptrs we use to verify that 'purge' has happened. | 398 // Unowned ptrs we use to verify that 'purge' has happened. |
396 DOMStorageDatabase* original_backing = | 399 DOMStorageDatabase* original_backing = |
397 static_cast<LocalStorageDatabaseAdapter*>( | 400 static_cast<LocalStorageDatabaseAdapter*>( |
398 area->backing_.get())->db_.get(); | 401 area->backing_.get())->db_.get(); |
399 DOMStorageMap* original_map = area->map_.get(); | 402 DOMStorageMap* original_map = area->map_.get(); |
400 | 403 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 base::TimeDelta::FromMilliseconds(750))); | 528 base::TimeDelta::FromMilliseconds(750))); |
526 EXPECT_EQ(base::TimeDelta(), | 529 EXPECT_EQ(base::TimeDelta(), |
527 rate_limiter.ComputeDelayNeeded( | 530 rate_limiter.ComputeDelayNeeded( |
528 base::TimeDelta::FromMilliseconds(1500))); | 531 base::TimeDelta::FromMilliseconds(1500))); |
529 EXPECT_EQ(base::TimeDelta(), | 532 EXPECT_EQ(base::TimeDelta(), |
530 rate_limiter.ComputeDelayNeeded( | 533 rate_limiter.ComputeDelayNeeded( |
531 base::TimeDelta::FromDays(1))); | 534 base::TimeDelta::FromDays(1))); |
532 } | 535 } |
533 | 536 |
534 } // namespace content | 537 } // namespace content |
OLD | NEW |