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

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

Issue 9389009: Hook up DomStorageArea with a DomStorageDatabase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Create and use a MockDomStorageTaskRunner in unit test Created 8 years, 10 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"
6 #include "base/file_util.h"
7 #include "base/message_loop.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/scoped_temp_dir.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "base/time.h"
5 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
6 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
7 #include "webkit/dom_storage/dom_storage_area.h" 14 #include "webkit/dom_storage/dom_storage_area.h"
15 #include "webkit/dom_storage/dom_storage_task_runner.h"
16 #include "webkit/dom_storage/dom_storage_types.h"
8 17
9 namespace dom_storage { 18 namespace dom_storage {
10 19
11 TEST(DomStorageAreaTest, DomStorageAreaBasics) { 20 TEST(DomStorageAreaTest, DomStorageAreaBasics) {
12 const GURL kOrigin("http://dom_storage/"); 21 const GURL kOrigin("http://dom_storage/");
13 const string16 kKey(ASCIIToUTF16("key")); 22 const string16 kKey(ASCIIToUTF16("key"));
14 const string16 kValue(ASCIIToUTF16("value")); 23 const string16 kValue(ASCIIToUTF16("value"));
15 const string16 kKey2(ASCIIToUTF16("key2")); 24 const string16 kKey2(ASCIIToUTF16("key2"));
16 const string16 kValue2(ASCIIToUTF16("value2")); 25 const string16 kValue2(ASCIIToUTF16("value2"));
17 26
(...skipping 28 matching lines...) Expand all
46 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value)); 55 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_nullable_value));
47 EXPECT_NE(copy->map_.get(), area->map_.get()); 56 EXPECT_NE(copy->map_.get(), area->map_.get());
48 copy = area->ShallowCopy(2); 57 copy = area->ShallowCopy(2);
49 EXPECT_EQ(copy->map_.get(), area->map_.get()); 58 EXPECT_EQ(copy->map_.get(), area->map_.get());
50 EXPECT_NE(0u, area->Length()); 59 EXPECT_NE(0u, area->Length());
51 EXPECT_TRUE(area->Clear()); 60 EXPECT_TRUE(area->Clear());
52 EXPECT_EQ(0u, area->Length()); 61 EXPECT_EQ(0u, area->Length());
53 EXPECT_NE(copy->map_.get(), area->map_.get()); 62 EXPECT_NE(copy->map_.get(), area->map_.get());
54 } 63 }
55 64
65 TEST(DomStorageAreaTest, BackingDatabaseOpened) {
66 const int64 kSessionStorageNamespaceId = kLocalStorageNamespaceId + 1;
67 const GURL kOrigin("http://www.google.com");
68
69 const string16 kKey = ASCIIToUTF16("test");
70 const string16 kKey2 = ASCIIToUTF16("test2");
71 const string16 kValue = ASCIIToUTF16("value");
72 ScopedTempDir temp_dir;
73 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
74
75 const FilePath kExpectedOriginFilePath = temp_dir.path().Append(
76 DomStorageArea::DatabaseFileNameFromOrigin(kOrigin));
77
78 // No directory, backing should be null.
79 {
80 scoped_refptr<DomStorageArea> area(
81 new DomStorageArea(kLocalStorageNamespaceId, kOrigin, FilePath(),
82 NULL));
83 EXPECT_EQ(NULL, area->backing_.get());
84 EXPECT_TRUE(area->initial_import_done_);
85 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath));
86 }
87
88 // Valid directory and origin but non-local namespace id. Backing should
89 // be null.
90 {
91 scoped_refptr<DomStorageArea> area(
92 new DomStorageArea(kSessionStorageNamespaceId, kOrigin,
93 temp_dir.path(), NULL));
94 EXPECT_EQ(NULL, area->backing_.get());
95 EXPECT_TRUE(area->initial_import_done_);
96
97 NullableString16 old_value;
98 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value));
99 ASSERT_TRUE(old_value.is_null());
100
101 // Check that saving a value has still left us without a backing database.
102 EXPECT_EQ(NULL, area->backing_.get());
103 EXPECT_FALSE(file_util::PathExists(kExpectedOriginFilePath));
104 }
105
106 // This should set up a DomStorageArea that is correctly backed to disk.
107 {
108 scoped_refptr<DomStorageArea> area(
109 new DomStorageArea(kLocalStorageNamespaceId, kOrigin,
110 temp_dir.path(),
111 new MockDomStorageTaskRunner(base::MessageLoopProxy::current())));
112
113 EXPECT_TRUE(area->backing_.get());
michaeln 2012/02/24 19:29:52 maybe EXPECT_FALSE(area->backing_->IsOpen()) here
benm (inactive) 2012/02/27 10:39:27 Done.
114 EXPECT_FALSE(area->initial_import_done_);
michaeln 2012/02/24 19:29:52 Consider replacing the 'file' backed DomStorageDat
benm (inactive) 2012/02/27 10:39:27 Good idea. I thought it was nice to assert that th
115
116 // Need to write something to ensure that the database is created.
117 NullableString16 old_value;
118 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value));
119 ASSERT_TRUE(old_value.is_null());
120 EXPECT_TRUE(area->SetItem(kKey2, kValue, &old_value));
121 ASSERT_TRUE(old_value.is_null());
122 EXPECT_TRUE(area->initial_import_done_);
123
michaeln 2012/02/24 19:29:52 maybe EXPECT_TRUE(area->commit_in_flight_) before
benm (inactive) 2012/02/27 10:39:27 Done.
124 MessageLoop::current()->RunAllPending();
125
126 EXPECT_TRUE(area->backing_->IsOpen());
127 EXPECT_EQ(2u, area->Length());
128 EXPECT_TRUE(file_util::PathExists(kExpectedOriginFilePath));
129 EXPECT_EQ(kValue, area->GetItem(kKey).string());
130 }
131 }
132
133 TEST(DomStorageAreaTest, TestDatabaseFilePath) {
134 EXPECT_EQ(FilePath().AppendASCII("file_path_to_0.localstorage"),
135 DomStorageArea::DatabaseFileNameFromOrigin(
136 GURL("file://path_to/index.html")));
137
138 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_0.localstorage"),
139 DomStorageArea::DatabaseFileNameFromOrigin(
140 GURL("https://www.google.com/")));
141
142 EXPECT_EQ(FilePath().AppendASCII("https_www.google.com_8080.localstorage"),
143 DomStorageArea::DatabaseFileNameFromOrigin(
144 GURL("https://www.google.com:8080")));
145 }
146
56 } // namespace dom_storage 147 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698