Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: webkit/dom_storage/dom_storage_area_unittest.cc

Issue 9963107: Persist sessionStorage on disk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test update. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/message_loop_proxy.h" 8 #include "base/message_loop_proxy.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/threading/sequenced_worker_pool.h" 10 #include "base/threading/sequenced_worker_pool.h"
11 #include "base/time.h" 11 #include "base/time.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webkit/dom_storage/dom_storage_area.h" 14 #include "webkit/dom_storage/dom_storage_area.h"
15 #include "webkit/dom_storage/dom_storage_database.h"
16 #include "webkit/dom_storage/dom_storage_database_adapter.h"
15 #include "webkit/dom_storage/dom_storage_task_runner.h" 17 #include "webkit/dom_storage/dom_storage_task_runner.h"
16 #include "webkit/dom_storage/dom_storage_types.h" 18 #include "webkit/dom_storage/dom_storage_types.h"
19 #include "webkit/dom_storage/local_storage_database_adapter.h"
17 20
18 namespace dom_storage { 21 namespace dom_storage {
19 22
20 23
21 class DomStorageAreaTest : public testing::Test { 24 class DomStorageAreaTest : public testing::Test {
22 public: 25 public:
23 DomStorageAreaTest() 26 DomStorageAreaTest()
24 : kOrigin(GURL("http://dom_storage/")), 27 : kOrigin(GURL("http://dom_storage/")),
25 kKey(ASCIIToUTF16("key")), 28 kKey(ASCIIToUTF16("key")),
26 kValue(ASCIIToUTF16("value")), 29 kValue(ASCIIToUTF16("value")),
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 ValuesMap values; 63 ValuesMap values;
61 ReadAllValues(&values); 64 ReadAllValues(&values);
62 EXPECT_EQ(1u, values.size()); 65 EXPECT_EQ(1u, values.size());
63 EXPECT_EQ(kValue, values[kKey].string()); 66 EXPECT_EQ(kValue, values[kKey].string());
64 } 67 }
65 }; 68 };
66 }; 69 };
67 70
68 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) { 71 TEST_F(DomStorageAreaTest, DomStorageAreaBasics) {
69 scoped_refptr<DomStorageArea> area( 72 scoped_refptr<DomStorageArea> area(
70 new DomStorageArea(1, kOrigin, FilePath(), NULL)); 73 new DomStorageArea(1, kOrigin, NULL, NULL));
71 string16 old_value; 74 string16 old_value;
72 NullableString16 old_nullable_value; 75 NullableString16 old_nullable_value;
73 scoped_refptr<DomStorageArea> copy; 76 scoped_refptr<DomStorageArea> copy;
74 77
75 // We don't focus on the underlying DomStorageMap functionality 78 // We don't focus on the underlying DomStorageMap functionality
76 // since that's covered by seperate unit tests. 79 // since that's covered by seperate unit tests.
77 EXPECT_EQ(kOrigin, area->origin()); 80 EXPECT_EQ(kOrigin, area->origin());
78 EXPECT_EQ(1, area->namespace_id()); 81 EXPECT_EQ(1, area->namespace_id());
79 EXPECT_EQ(0u, area->Length()); 82 EXPECT_EQ(0u, area->Length());
80 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); 83 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value));
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // No directory, backing should be null. 129 // No directory, backing should be null.
127 { 130 {
128 scoped_refptr<DomStorageArea> area( 131 scoped_refptr<DomStorageArea> area(
129 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, FilePath(), 132 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, FilePath(),
130 NULL)); 133 NULL));
131 EXPECT_EQ(NULL, area->backing_.get()); 134 EXPECT_EQ(NULL, area->backing_.get());
132 EXPECT_TRUE(area->is_initial_import_done_); 135 EXPECT_TRUE(area->is_initial_import_done_);
133 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); 136 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath));
134 } 137 }
135 138
136 // Valid directory and origin but non-local namespace id. Backing should 139 // Valid directory and origin but no session storage backing. Backing should
137 // be null. 140 // be null.
138 { 141 {
139 scoped_refptr<DomStorageArea> area( 142 scoped_refptr<DomStorageArea> area(
140 new DomStorageArea(kSessionStorageNamespaceId, kOrigin, 143 new DomStorageArea(kSessionStorageNamespaceId, kOrigin,
141 temp_dir.path(), NULL)); 144 NULL, NULL));
142 EXPECT_EQ(NULL, area->backing_.get()); 145 EXPECT_EQ(NULL, area->backing_.get());
143 EXPECT_TRUE(area->is_initial_import_done_); 146 EXPECT_TRUE(area->is_initial_import_done_);
144 147
145 NullableString16 old_value; 148 NullableString16 old_value;
146 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); 149 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value));
147 ASSERT_TRUE(old_value.is_null()); 150 ASSERT_TRUE(old_value.is_null());
148 151
149 // Check that saving a value has still left us without a backing database. 152 // Check that saving a value has still left us without a backing database.
150 EXPECT_EQ(NULL, area->backing_.get()); 153 EXPECT_EQ(NULL, area->backing_.get());
151 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath)); 154 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath));
152 } 155 }
153 156
154 // This should set up a DomStorageArea that is correctly backed to disk. 157 // This should set up a DomStorageArea that is correctly backed to disk.
155 { 158 {
156 scoped_refptr<DomStorageArea> area( 159 scoped_refptr<DomStorageArea> area(
157 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, 160 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
158 temp_dir.path(), 161 temp_dir.path(),
159 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); 162 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
160 163
161 EXPECT_TRUE(area->backing_.get()); 164 EXPECT_TRUE(area->backing_.get());
162 EXPECT_FALSE(area->backing_->IsOpen()); 165 DomStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>(
166 area->backing_.get())->db_.get();
167 EXPECT_FALSE(database->IsOpen());
163 EXPECT_FALSE(area->is_initial_import_done_); 168 EXPECT_FALSE(area->is_initial_import_done_);
164 169
165 // Inject an in-memory db to speed up the test. 170 // Inject an in-memory db to speed up the test.
166 // We will verify that something is written into the database but not 171 // We will verify that something is written into the database but not
167 // that a file is written to disk - DOMStorageDatabase unit tests cover 172 // that a file is written to disk - DOMStorageDatabase unit tests cover
168 // that. 173 // that.
169 area->backing_.reset(new DomStorageDatabase()); 174 area->backing_.reset(new LocalStorageDatabaseAdapter());
170 175
171 // Need to write something to ensure that the database is created. 176 // Need to write something to ensure that the database is created.
172 NullableString16 old_value; 177 NullableString16 old_value;
173 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); 178 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value));
174 ASSERT_TRUE(old_value.is_null()); 179 ASSERT_TRUE(old_value.is_null());
175 EXPECT_TRUE(area->is_initial_import_done_); 180 EXPECT_TRUE(area->is_initial_import_done_);
176 EXPECT_TRUE(area->commit_batch_.get()); 181 EXPECT_TRUE(area->commit_batch_.get());
177 EXPECT_EQ(0, area->commit_batches_in_flight_); 182 EXPECT_EQ(0, area->commit_batches_in_flight_);
178 183
179 MessageLoop::current()->RunAllPending(); 184 MessageLoop::current()->RunAllPending();
180 185
181 EXPECT_FALSE(area->commit_batch_.get()); 186 EXPECT_FALSE(area->commit_batch_.get());
182 EXPECT_EQ(0, area->commit_batches_in_flight_); 187 EXPECT_EQ(0, area->commit_batches_in_flight_);
183 EXPECT_TRUE(area->backing_->IsOpen()); 188 database = static_cast<LocalStorageDatabaseAdapter*>(
189 area->backing_.get())->db_.get();
190 EXPECT_TRUE(database->IsOpen());
184 EXPECT_EQ(1u, area->Length()); 191 EXPECT_EQ(1u, area->Length());
185 EXPECT_EQ(kValue, area->GetItem(kKey).string()); 192 EXPECT_EQ(kValue, area->GetItem(kKey).string());
186 193
187 // Verify the content made it to the in memory database. 194 // Verify the content made it to the in memory database.
188 ValuesMap values; 195 ValuesMap values;
189 area->backing_->ReadAllValues(&values); 196 area->backing_->ReadAllValues(&values);
190 EXPECT_EQ(1u, values.size()); 197 EXPECT_EQ(1u, values.size());
191 EXPECT_EQ(kValue, values[kKey].string()); 198 EXPECT_EQ(kValue, values[kKey].string());
192 } 199 }
193 } 200 }
194 201
195 TEST_F(DomStorageAreaTest, CommitTasks) { 202 TEST_F(DomStorageAreaTest, CommitTasks) {
196 ScopedTempDir temp_dir; 203 ScopedTempDir temp_dir;
197 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 204 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
198 205
199 scoped_refptr<DomStorageArea> area( 206 scoped_refptr<DomStorageArea> area(
200 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, 207 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
201 temp_dir.path(), 208 temp_dir.path(),
202 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); 209 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
203 // Inject an in-memory db to speed up the test. 210 // Inject an in-memory db to speed up the test.
204 area->backing_.reset(new DomStorageDatabase()); 211 area->backing_.reset(new LocalStorageDatabaseAdapter());
205 212
206 // Unrelated to commits, but while we're here, see that querying Length() 213 // Unrelated to commits, but while we're here, see that querying Length()
207 // causes the backing database to be opened and presumably read from. 214 // causes the backing database to be opened and presumably read from.
208 EXPECT_FALSE(area->is_initial_import_done_); 215 EXPECT_FALSE(area->is_initial_import_done_);
209 EXPECT_EQ(0u, area->Length()); 216 EXPECT_EQ(0u, area->Length());
210 EXPECT_TRUE(area->is_initial_import_done_); 217 EXPECT_TRUE(area->is_initial_import_done_);
211 218
212 ValuesMap values; 219 ValuesMap values;
213 NullableString16 old_value; 220 NullableString16 old_value;
214 221
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) { 280 TEST_F(DomStorageAreaTest, CommitChangesAtShutdown) {
274 ScopedTempDir temp_dir; 281 ScopedTempDir temp_dir;
275 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 282 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
276 scoped_refptr<DomStorageArea> area( 283 scoped_refptr<DomStorageArea> area(
277 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, 284 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
278 temp_dir.path(), 285 temp_dir.path(),
279 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); 286 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
280 287
281 // Inject an in-memory db to speed up the test and also to verify 288 // Inject an in-memory db to speed up the test and also to verify
282 // the final changes are commited in it's dtor. 289 // the final changes are commited in it's dtor.
283 area->backing_.reset(new VerifyChangesCommittedDatabase()); 290 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset(
291 new VerifyChangesCommittedDatabase());
284 292
285 ValuesMap values; 293 ValuesMap values;
286 NullableString16 old_value; 294 NullableString16 old_value;
287 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); 295 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value));
288 EXPECT_TRUE(area->HasUncommittedChanges()); 296 EXPECT_TRUE(area->HasUncommittedChanges());
289 area->backing_->ReadAllValues(&values); 297 area->backing_->ReadAllValues(&values);
290 EXPECT_TRUE(values.empty()); // not committed yet 298 EXPECT_TRUE(values.empty()); // not committed yet
291 area->Shutdown(); 299 area->Shutdown();
292 MessageLoop::current()->RunAllPending(); 300 MessageLoop::current()->RunAllPending();
293 EXPECT_TRUE(area->HasOneRef()); 301 EXPECT_TRUE(area->HasOneRef());
294 EXPECT_FALSE(area->backing_.get()); 302 EXPECT_FALSE(area->backing_.get());
295 // The VerifyChangesCommittedDatabase destructor verifies values 303 // The VerifyChangesCommittedDatabase destructor verifies values
296 // were committed. 304 // were committed.
297 } 305 }
298 306
299 TEST_F(DomStorageAreaTest, DeleteOrigin) { 307 TEST_F(DomStorageAreaTest, DeleteOrigin) {
300 ScopedTempDir temp_dir; 308 ScopedTempDir temp_dir;
301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 309 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
302 scoped_refptr<DomStorageArea> area( 310 scoped_refptr<DomStorageArea> area(
303 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, 311 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
304 temp_dir.path(), 312 temp_dir.path(),
305 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); 313 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
306 314
307 // This test puts files on disk. 315 // This test puts files on disk.
308 FilePath db_file_path = area->backing_->file_path(); 316 FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>(
317 area->backing_.get())->db_->file_path();
309 FilePath db_journal_file_path = 318 FilePath db_journal_file_path =
310 DomStorageDatabase::GetJournalFilePath(db_file_path); 319 DomStorageDatabase::GetJournalFilePath(db_file_path);
311 320
312 // Nothing bad should happen when invoked w/o any files on disk. 321 // Nothing bad should happen when invoked w/o any files on disk.
313 area->DeleteOrigin(); 322 area->DeleteOrigin();
314 EXPECT_FALSE(file_util::PathExists(db_file_path)); 323 EXPECT_FALSE(file_util::PathExists(db_file_path));
315 324
316 // Commit something in the database and then delete. 325 // Commit something in the database and then delete.
317 NullableString16 old_value; 326 NullableString16 old_value;
318 area->SetItem(kKey, kValue, &old_value); 327 area->SetItem(kKey, kValue, &old_value);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 367
359 TEST_F(DomStorageAreaTest, PurgeMemory) { 368 TEST_F(DomStorageAreaTest, PurgeMemory) {
360 ScopedTempDir temp_dir; 369 ScopedTempDir temp_dir;
361 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 370 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
362 scoped_refptr<DomStorageArea> area( 371 scoped_refptr<DomStorageArea> area(
363 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, 372 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
364 temp_dir.path(), 373 temp_dir.path(),
365 new MockDomStorageTaskRunner(base::MessageLoopProxy::current()))); 374 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
366 375
367 // Inject an in-memory db to speed up the test. 376 // Inject an in-memory db to speed up the test.
368 area->backing_.reset(new DomStorageDatabase()); 377 area->backing_.reset(new LocalStorageDatabaseAdapter());
369 378
370 // Unowned ptrs we use to verify that 'purge' has happened. 379 // Unowned ptrs we use to verify that 'purge' has happened.
371 DomStorageDatabase* original_backing = area->backing_.get(); 380 DomStorageDatabase* original_backing =
381 static_cast<LocalStorageDatabaseAdapter*>(
382 area->backing_.get())->db_.get();
372 DomStorageMap* original_map = area->map_.get(); 383 DomStorageMap* original_map = area->map_.get();
373 384
374 // Should do no harm when called on a newly constructed object. 385 // Should do no harm when called on a newly constructed object.
375 EXPECT_FALSE(area->is_initial_import_done_); 386 EXPECT_FALSE(area->is_initial_import_done_);
376 area->PurgeMemory(); 387 area->PurgeMemory();
377 EXPECT_FALSE(area->is_initial_import_done_); 388 EXPECT_FALSE(area->is_initial_import_done_);
378 EXPECT_EQ(original_backing, area->backing_.get()); 389 DomStorageDatabase* new_backing = static_cast<LocalStorageDatabaseAdapter*>(
390 area->backing_.get())->db_.get();
391 EXPECT_EQ(original_backing, new_backing);
379 EXPECT_EQ(original_map, area->map_.get()); 392 EXPECT_EQ(original_map, area->map_.get());
380 393
381 // Should not do anything when commits are pending. 394 // Should not do anything when commits are pending.
382 NullableString16 old_value; 395 NullableString16 old_value;
383 area->SetItem(kKey, kValue, &old_value); 396 area->SetItem(kKey, kValue, &old_value);
384 EXPECT_TRUE(area->is_initial_import_done_); 397 EXPECT_TRUE(area->is_initial_import_done_);
385 EXPECT_TRUE(area->HasUncommittedChanges()); 398 EXPECT_TRUE(area->HasUncommittedChanges());
386 area->PurgeMemory(); 399 area->PurgeMemory();
387 EXPECT_TRUE(area->is_initial_import_done_); 400 EXPECT_TRUE(area->is_initial_import_done_);
388 EXPECT_TRUE(area->HasUncommittedChanges()); 401 EXPECT_TRUE(area->HasUncommittedChanges());
389 EXPECT_EQ(original_backing, area->backing_.get()); 402 new_backing = static_cast<LocalStorageDatabaseAdapter*>(
403 area->backing_.get())->db_.get();
404 EXPECT_EQ(original_backing, new_backing);
390 EXPECT_EQ(original_map, area->map_.get()); 405 EXPECT_EQ(original_map, area->map_.get());
391 406
392 // Commit the changes from above, 407 // Commit the changes from above,
393 MessageLoop::current()->RunAllPending(); 408 MessageLoop::current()->RunAllPending();
394 EXPECT_FALSE(area->HasUncommittedChanges()); 409 EXPECT_FALSE(area->HasUncommittedChanges());
395 EXPECT_EQ(original_backing, area->backing_.get()); 410 new_backing = static_cast<LocalStorageDatabaseAdapter*>(
411 area->backing_.get())->db_.get();
412 EXPECT_EQ(original_backing, new_backing);
396 EXPECT_EQ(original_map, area->map_.get()); 413 EXPECT_EQ(original_map, area->map_.get());
397 414
398 // Should drop caches and reset database connections 415 // Should drop caches and reset database connections
399 // when invoked on an area that's loaded up primed. 416 // when invoked on an area that's loaded up primed.
400 area->PurgeMemory(); 417 area->PurgeMemory();
401 EXPECT_FALSE(area->is_initial_import_done_); 418 EXPECT_FALSE(area->is_initial_import_done_);
402 EXPECT_NE(original_backing, area->backing_.get()); 419 new_backing = static_cast<LocalStorageDatabaseAdapter*>(
420 area->backing_.get())->db_.get();
421 EXPECT_NE(original_backing, new_backing);
403 EXPECT_NE(original_map, area->map_.get()); 422 EXPECT_NE(original_map, area->map_.get());
404 } 423 }
405 424
406 TEST_F(DomStorageAreaTest, DatabaseFileNames) { 425 TEST_F(DomStorageAreaTest, DatabaseFileNames) {
407 struct { 426 struct {
408 const char* origin; 427 const char* origin;
409 const char* file_name; 428 const char* file_name;
410 const char* journal_file_name; 429 const char* journal_file_name;
411 } kCases[] = { 430 } kCases[] = {
412 { "https://www.google.com/", 431 { "https://www.google.com/",
(...skipping 29 matching lines...) Expand all
442 EXPECT_EQ( 461 EXPECT_EQ(
443 FilePath().AppendASCII("-journal"), 462 FilePath().AppendASCII("-journal"),
444 DomStorageDatabase::GetJournalFilePath(FilePath())); 463 DomStorageDatabase::GetJournalFilePath(FilePath()));
445 EXPECT_EQ( 464 EXPECT_EQ(
446 FilePath().AppendASCII(".extensiononly-journal"), 465 FilePath().AppendASCII(".extensiononly-journal"),
447 DomStorageDatabase::GetJournalFilePath( 466 DomStorageDatabase::GetJournalFilePath(
448 FilePath().AppendASCII(".extensiononly"))); 467 FilePath().AppendASCII(".extensiononly")));
449 } 468 }
450 469
451 } // namespace dom_storage 470 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698