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

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

Issue 9159020: Create a class to represent a DOM Storage Database. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Disable test that uses the webcore localstorage file pending the test data getting checked in. 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "webkit/dom_storage/dom_storage_database.h"
6
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/scoped_temp_dir.h"
11 #include "base/utf_string_conversions.h"
12 #include "sql/statement.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace dom_storage {
16
17 class DomStorageDatabaseTest : public testing::Test {
18 protected:
19 virtual void SetUp() {
20 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
21 file_name_ = temp_dir_.path().AppendASCII("TestDomStorageDatabase.db");
22 }
23
24 void CreateV1Table(sql::Connection* db) {
25 ASSERT_FALSE(db->is_open());
26 ASSERT_TRUE(db->Open(file_name_));
27 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
28 ASSERT_TRUE(db->Execute(
29 "CREATE TABLE IF NOT EXISTS ItemTable ("
30 "key TEXT UNIQUE ON CONFLICT REPLACE, "
31 "value TEXT NOT NULL ON CONFLICT FAIL)"));
32 }
33
34 void CreateInvalidTable(sql::Connection* db) {
35 // Create a table with the value type as FLOAT - this is "invalid"
michaeln 2012/02/03 04:45:15 a more interesting test case would be trying to op
benm (inactive) 2012/02/03 11:46:40 Yes, I think so. An file that isn't a database wou
benm (inactive) 2012/02/03 14:21:36 Writing this test exposed some interesting behavio
36 // as far as the DOM Storage db is concerned.
37 ASSERT_FALSE(db->is_open());
38 ASSERT_TRUE(db->Open(file_name_));
39 ASSERT_TRUE(db->Execute("DROP TABLE IF EXISTS ItemTable"));
40 ASSERT_TRUE(db->Execute(
41 "CREATE TABLE IF NOT EXISTS ItemTable ("
42 "key TEXT UNIQUE ON CONFLICT REPLACE, "
43 "value FLOAT NOT NULL ON CONFLICT FAIL)"));
44 }
45
46
47 void InsertDataV1(sql::Connection* db,
48 const string16& key,
49 const string16& value) {
50 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE,
51 "INSERT INTO ItemTable VALUES (?,?)"));
52 statement.BindString16(0, key);
53 statement.BindString16(1, value);
54 ASSERT_TRUE(statement.is_valid());
55 statement.Run();
56 }
57
58 void InsertDataV2(sql::Connection* db,
michaeln 2012/02/03 04:45:15 Would it make sense for callsites to directly use
benm (inactive) 2012/02/03 11:46:40 I added the helpers up here so that we can test th
59 const string16& key,
60 const string16& value) {
61 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE,
62 "INSERT INTO ItemTable VALUES (?,?)"));
63 statement.BindString16(0, key);
64 statement.BindBlob(1, value.data(), value.length() * sizeof(char16));
65 ASSERT_TRUE(statement.is_valid());
66 statement.Run();
67 }
68
69 DomStorageDatabase::ValuesMap ReadAllRows(sql::Connection* db) {
michaeln 2012/02/03 04:45:15 similarly, would it make sense to directly use the
benm (inactive) 2012/02/03 11:46:40 See above.
70 DomStorageDatabase::ValuesMap values;
71 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE,
72 "SELECT * from ItemTable"));
73 EXPECT_TRUE(statement.is_valid());
74 while (statement.Step()) {
75 string16 key = statement.ColumnString16(0);
76 string16 value;
77 statement.ColumnBlobAsString16(1, &value);
78 values[key] = NullableString16(value, false);
79 }
80 return values;
81 }
82
83 void CheckValuesMatch(sql::Connection* db,
84 const DomStorageDatabase::ValuesMap& expected) {
85 const DomStorageDatabase::ValuesMap values_read = ReadAllRows(db);
86 EXPECT_EQ(expected.size(), values_read.size());
87
88 DomStorageDatabase::ValuesMap::const_iterator it = values_read.begin();
89 for (; it != values_read.end(); ++it) {
90 string16 key = it->first;
91 NullableString16 value = it->second;
92 EXPECT_EQ((expected.find(key)->second).string(), value.string());
93 }
94 }
95
96 void VerifySchema(sql::Connection* db) {
97 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE,
98 "SELECT * from ItemTable LIMIT 1"));
99 EXPECT_EQ(sql::COLUMN_TYPE_TEXT, statement.DeclaredColumnType(0));
100 EXPECT_EQ(sql::COLUMN_TYPE_BLOB, statement.DeclaredColumnType(1));
101 }
102
103 ScopedTempDir temp_dir_;
104 FilePath file_name_;
105 };
106
107 TEST_F(DomStorageDatabaseTest, SimpleOpenAndClose) {
michaeln 2012/02/03 04:45:15 this test could use an in-memory db
benm (inactive) 2012/02/03 11:46:40 Agreed, I've added a TODO about this but as it wil
108 DomStorageDatabase db(file_name_);
109 ASSERT_TRUE(db.LazyOpen(true));
110 EXPECT_TRUE(db.db_->DoesTableExist("ItemTable"));
111 // Ensure that we've got the colums we expect.
112 EXPECT_TRUE(db.db_->DoesColumnExist("ItemTable", "key"));
113 EXPECT_TRUE(db.db_->DoesColumnExist("ItemTable", "value"));
michaeln 2012/02/03 04:45:15 Would it make sense to put these three statements
benm (inactive) 2012/02/03 11:46:40 Done.
114 VerifySchema(db.db_.get());
115 db.Close();
116 EXPECT_FALSE(db.IsOpen());
117 }
118
119 TEST_F(DomStorageDatabaseTest, TestLazyOpenIsLazy) {
120 DomStorageDatabase db(file_name_);
121 EXPECT_FALSE(db.IsOpen());
122 DomStorageDatabase::ValuesMap values;
123 db.ReadAllValues(&values);
124 EXPECT_FALSE(db.IsOpen());
125 values[ASCIIToUTF16("key")] = NullableString16(ASCIIToUTF16("value"), false);
126 db.CommitChanges(false, values);
127 EXPECT_TRUE(db.IsOpen());
128
129 db.Close();
130 ASSERT_FALSE(db.IsOpen());
131
132 db.ReadAllValues(&values);
133 EXPECT_TRUE(db.IsOpen());
134 }
135
136 TEST_F(DomStorageDatabaseTest, TestUpgradesV1TableToV2) {
michaeln 2012/02/03 04:45:15 this one maybe could be done out of an in-mem db b
137 DomStorageDatabase db(file_name_);
138 db.db_.reset(new sql::Connection());
139 CreateV1Table(db.db_.get());
140 db.Close();
141
142 db.LazyOpen(true);
143 VerifySchema(db.db_.get());
144 }
145
146 TEST_F(DomStorageDatabaseTest, TestFailedUpgrade) {
147 DomStorageDatabase db(file_name_);
148 db.db_.reset(new sql::Connection());
149 CreateInvalidTable(db.db_.get());
150
151 EXPECT_FALSE(db.UpgradeVersion1To2IfNeeded());
152
153 db.Close();
154
155 // LazyOpen should be able to deal with the failed upgrade.
156 EXPECT_TRUE(db.LazyOpen(true));
157 VerifySchema(db.db_.get());
158 }
159
160
161 TEST_F(DomStorageDatabaseTest, TestIsOpen) {
michaeln 2012/02/03 04:45:15 could be done with an in-mem db
162 DomStorageDatabase db(file_name_);
163 EXPECT_FALSE(db.IsOpen());
164 ASSERT_TRUE(db.LazyOpen(true));
165 EXPECT_TRUE(db.IsOpen());
166 db.Close();
167 EXPECT_FALSE(db.IsOpen());
168 }
169
170 TEST_F(DomStorageDatabaseTest, SimpleRead) {
171 DomStorageDatabase db(file_name_);
172 db.LazyOpen(true);
173
174 const string16 kCannedKey = ASCIIToUTF16("name");
175 const string16 kCannedValue = ASCIIToUTF16("Joe Bloggs");
176 InsertDataV2(db.db_.get(), kCannedKey, kCannedValue);
177 DomStorageDatabase::ValuesMap values;
178 db.ReadAllValues(&values);
179
180 EXPECT_EQ(1u, values.size());
181 EXPECT_TRUE(values.find(kCannedKey) != values.end());
182 EXPECT_EQ(kCannedValue, values[kCannedKey].string());
183 }
184
185 TEST_F(DomStorageDatabaseTest, SimpleWrite) {
michaeln 2012/02/03 04:45:15 Since SimpleWrite actually also reads, maybe call
benm (inactive) 2012/02/03 11:46:40 So the idea here was that we're only testing the C
michaeln 2012/02/04 00:21:34 I see that, but my point is that it makes the test
benm (inactive) 2012/02/06 14:02:36 OK, done
186 DomStorageDatabase db(file_name_);
187
188 DomStorageDatabase::ValuesMap storage;
189 string16 kCannedKeys[] = {
190 ASCIIToUTF16("test"),
191 ASCIIToUTF16("company"),
192 ASCIIToUTF16("date")
193 };
194 NullableString16 kCannedValues[] = {
195 NullableString16(ASCIIToUTF16("123"), false),
196 NullableString16(ASCIIToUTF16("Google"), false),
197 NullableString16(ASCIIToUTF16("18-01-2012"), false)
198 };
199 for (int i = 0; i < 3; i++) {
200 storage[kCannedKeys[i]] = kCannedValues[i];
201 }
202
203 ASSERT_TRUE(db.CommitChanges(false, storage));
204
205 CheckValuesMatch(db.db_.get(), storage);
206 }
207
208 TEST_F(DomStorageDatabaseTest, WriteWithClear) {
209 DomStorageDatabase db(file_name_);
210
211 DomStorageDatabase::ValuesMap storage;
212 string16 kCannedKeys[] = {
213 ASCIIToUTF16("test"),
214 ASCIIToUTF16("company"),
215 ASCIIToUTF16("date")
216 };
217 NullableString16 kCannedValues[] = {
218 NullableString16(ASCIIToUTF16("123"), false),
219 NullableString16(ASCIIToUTF16("Google"), false),
220 NullableString16(ASCIIToUTF16("18-01-2012"), false)
221 };
222 for (int i = 0; i < 3; i++) {
223 storage[kCannedKeys[i]] = kCannedValues[i];
michaeln 2012/02/03 04:45:15 this is replicated in the previous test too, feels
benm (inactive) 2012/02/03 11:46:40 Done.
224 }
225
226 ASSERT_TRUE(db.CommitChanges(false, storage));
227 CheckValuesMatch(db.db_.get(), storage);
228
229 // Insert some values, clearing the database first.
230 storage.clear();
231 storage[ASCIIToUTF16("another_key")] =
232 NullableString16(ASCIIToUTF16("test"), false);
233 ASSERT_TRUE(db.CommitChanges(true, storage));
234 CheckValuesMatch(db.db_.get(), storage);
235
236 // Now clear the values without inserting any new ones.
237 storage.clear();
238 ASSERT_TRUE(db.CommitChanges(true, storage));
239 CheckValuesMatch(db.db_.get(), storage);
240 }
241
242 TEST_F(DomStorageDatabaseTest, UpgradeFromV1ToV2NoData) {
michaeln 2012/02/03 04:45:15 there are multiple upgrade tests, can any of them
benm (inactive) 2012/02/03 11:46:40 I think what I was thinking is that this test veri
michaeln 2012/02/04 00:21:34 Got it. Since that test also verifies the resultin
benm (inactive) 2012/02/06 14:02:36 sounds good. It's nice to have such a powerful tes
243 DomStorageDatabase db(file_name_);
244 db.db_.reset(new sql::Connection());
245 CreateV1Table(db.db_.get());
246
247 // The database has V1 structure, try to update it to V2.
248 sql::Statement statement(db.db_->GetCachedStatement(SQL_FROM_HERE,
249 "SELECT value from ItemTable LIMIT 1"));
michaeln 2012/02/03 04:45:15 can this statement be removed, what purpose does i
benm (inactive) 2012/02/03 11:46:40 I think you need a valid SQL statement to be able
michaeln 2012/02/04 00:21:34 sorry, i meant what purpose does assertion part se
benm (inactive) 2012/02/06 14:02:36 Got it - removed now.
250 ASSERT_EQ(sql::COLUMN_TYPE_TEXT, statement.DeclaredColumnType(0));
251
252 ASSERT_TRUE(db.UpgradeVersion1To2IfNeeded());
253
254 // Verify the db now has V2 structure.
255 VerifySchema(db.db_.get());
256 }
257
258 TEST_F(DomStorageDatabaseTest, UpgradeFromV1ToV2WithData) {
259 const string16 kCannedKey = ASCIIToUTF16("foo");
260 const NullableString16 kCannedValue(ASCIIToUTF16("bar"), false);
261 DomStorageDatabase::ValuesMap expected;
262 expected[kCannedKey] = kCannedValue;
263
264 {
265 DomStorageDatabase db(file_name_);
266 db.db_.reset(new sql::Connection());
267 CreateV1Table(db.db_.get());
268
269 // The database has V1 structure, try to update it to V2.
270 sql::Statement statement(db.db_->GetCachedStatement(SQL_FROM_HERE,
271 "SELECT value from ItemTable LIMIT 1"));
272 ASSERT_EQ(sql::COLUMN_TYPE_TEXT, statement.DeclaredColumnType(0));
273
274 InsertDataV1(db.db_.get(), kCannedKey, kCannedValue.string());
275
276 ASSERT_TRUE(db.UpgradeVersion1To2IfNeeded());
277
278 VerifySchema(db.db_.get());
279
280 CheckValuesMatch(db.db_.get(), expected);
281 }
282
283 // Now open the db again and check that the data is consistent.
michaeln 2012/02/03 04:45:15 provided other tests ensure that files containing
benm (inactive) 2012/02/03 11:46:40 Yes, it should be covered by the next test. will r
284 {
285 DomStorageDatabase db(file_name_);
286 ASSERT_TRUE(db.LazyOpen(true));
287 CheckValuesMatch(db.db_.get(), expected);
288 }
289 }
290
291 TEST_F(DomStorageDatabaseTest, TestOpenCloseDataPreserved) {
292 DomStorageDatabase db(file_name_);
293
294 ASSERT_TRUE(db.LazyOpen(true));
295
296 const string16 kCannedKey = ASCIIToUTF16("test");
297 const NullableString16 kCannedValue(ASCIIToUTF16("data"), false);
298 InsertDataV2(db.db_.get(), kCannedKey, kCannedValue.string());
299 db.Close();
300
301 ASSERT_TRUE(db.LazyOpen(true));
302 DomStorageDatabase::ValuesMap expected;
303 expected[kCannedKey] = kCannedValue;
304 CheckValuesMatch(db.db_.get(), expected);
305 }
306
307 TEST_F(DomStorageDatabaseTest, TestSimpleRemoveOneValue) {
308 DomStorageDatabase db(file_name_);
309
310 ASSERT_TRUE(db.LazyOpen(true));
311 const string16 kCannedKey = ASCIIToUTF16("test");
312 const NullableString16 kCannedValue(ASCIIToUTF16("data"), false);
313 InsertDataV2(db.db_.get(), kCannedKey, kCannedValue.string());
314
315 DomStorageDatabase::ValuesMap expected;
316 expected[kCannedKey] = kCannedValue;
317 CheckValuesMatch(db.db_.get(), expected);
318
319 DomStorageDatabase::ValuesMap values;
320 values[kCannedKey] = NullableString16(true);
321
322 db.CommitChanges(false, values);
323
324 expected.clear();
325 CheckValuesMatch(db.db_.get(), expected);
326 }
327
328 // TODO(benm): Enable this test in follow up patch once the test data has
329 // landed.
330 TEST_F(DomStorageDatabaseTest, DISABLED_TestCanOpenAndReadWebCoreDatabase) {
331 {
332 FilePath webcore_database;
333 PathService::Get(base::DIR_SOURCE_ROOT, &webcore_database);
334 webcore_database = webcore_database.AppendASCII("webkit");
335 webcore_database = webcore_database.AppendASCII("data");
336 webcore_database = webcore_database.AppendASCII("dom_storage");
337 webcore_database =
338 webcore_database.AppendASCII("webcore_test_database.localstorage");
339
340 ASSERT_TRUE(file_util::PathExists(webcore_database));
341
342 DomStorageDatabase db(webcore_database);
343 DomStorageDatabase::ValuesMap values;
344 db.ReadAllValues(&values);
345 EXPECT_TRUE(db.IsOpen());
346 EXPECT_EQ(2u, values.size());
347
348 DomStorageDatabase::ValuesMap::const_iterator it =
349 values.find(ASCIIToUTF16("value"));
350 EXPECT_TRUE(it != values.end());
351 EXPECT_EQ(ASCIIToUTF16("I am in local storage!"), it->second.string());
352
353 it = values.find(ASCIIToUTF16("timestamp"));
354 EXPECT_TRUE(it != values.end());
355 EXPECT_EQ(ASCIIToUTF16("1326738338841"), it->second.string());
356
357 it = values.find(ASCIIToUTF16("not_there"));
358 EXPECT_TRUE(it == values.end());
359 }
360 }
361
362 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698