Don't preload rarely seen large images
[chromium-blink-merge.git] / components / webdata / common / web_database_migration_unittest.cc
blob9919098f3d9c62fd56df56319a9b94af3c73ce6b
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.
5 #include <string>
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/guid.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "base/values.h"
19 #include "components/autofill/core/browser/autofill_country.h"
20 #include "components/autofill/core/browser/autofill_profile.h"
21 #include "components/autofill/core/browser/autofill_type.h"
22 #include "components/autofill/core/browser/credit_card.h"
23 #include "components/autofill/core/browser/webdata/autofill_change.h"
24 #include "components/autofill/core/browser/webdata/autofill_entry.h"
25 #include "components/autofill/core/browser/webdata/autofill_table.h"
26 #include "components/password_manager/core/browser/webdata/logins_table.h"
27 #include "components/search_engines/keyword_table.h"
28 #include "components/signin/core/browser/webdata/token_service_table.h"
29 #include "components/webdata/common/web_database.h"
30 #include "sql/statement.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using autofill::AutofillProfile;
34 using autofill::AutofillTable;
35 using autofill::CreditCard;
36 using base::ASCIIToUTF16;
37 using base::Time;
39 namespace {
41 std::string RemoveQuotes(const std::string& has_quotes) {
42 std::string no_quotes;
43 // SQLite quotes: http://www.sqlite.org/lang_keywords.html
44 base::RemoveChars(has_quotes, "\"[]`", &no_quotes);
45 return no_quotes;
48 } // anonymous namespace
50 // The WebDatabaseMigrationTest encapsulates testing of database migrations.
51 // Specifically, these tests are intended to exercise any schema changes in
52 // the WebDatabase and data migrations that occur in
53 // |WebDatabase::MigrateOldVersionsAsNeeded()|.
54 class WebDatabaseMigrationTest : public testing::Test {
55 public:
56 WebDatabaseMigrationTest() {}
57 ~WebDatabaseMigrationTest() override {}
59 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
61 // Load the database via the WebDatabase class and migrate the database to
62 // the current version.
63 void DoMigration() {
64 // TODO(joi): This whole unit test file needs to stay in //chrome
65 // for now, as it needs to know about all the different table
66 // types. Once all webdata datatypes have been componentized, this
67 // could move to components_unittests.
68 AutofillTable autofill_table("en-US");
69 KeywordTable keyword_table;
70 LoginsTable logins_table;
71 TokenServiceTable token_service_table;
73 WebDatabase db;
74 db.AddTable(&autofill_table);
75 db.AddTable(&keyword_table);
76 db.AddTable(&logins_table);
77 db.AddTable(&token_service_table);
79 // This causes the migration to occur.
80 ASSERT_EQ(sql::INIT_OK, db.Init(GetDatabasePath()));
83 protected:
84 // Current tested version number. When adding a migration in
85 // |WebDatabase::MigrateOldVersionsAsNeeded()| and changing the version number
86 // |kCurrentVersionNumber| this value should change to reflect the new version
87 // number and a new migration test added below.
88 static const int kCurrentTestedVersionNumber;
90 base::FilePath GetDatabasePath() {
91 const base::FilePath::CharType kWebDatabaseFilename[] =
92 FILE_PATH_LITERAL("TestWebDatabase.sqlite3");
93 return temp_dir_.path().Append(base::FilePath(kWebDatabaseFilename));
96 // The textual contents of |file| are read from
97 // "components/test/data/web_database" and returned in the string |contents|.
98 // Returns true if the file exists and is read successfully, false otherwise.
99 bool GetWebDatabaseData(const base::FilePath& file, std::string* contents) {
100 base::FilePath source_path;
101 PathService::Get(base::DIR_SOURCE_ROOT, &source_path);
102 source_path = source_path.AppendASCII("components");
103 source_path = source_path.AppendASCII("test");
104 source_path = source_path.AppendASCII("data");
105 source_path = source_path.AppendASCII("web_database");
106 source_path = source_path.Append(file);
107 return base::PathExists(source_path) &&
108 base::ReadFileToString(source_path, contents);
111 static int VersionFromConnection(sql::Connection* connection) {
112 // Get version.
113 sql::Statement s(connection->GetUniqueStatement(
114 "SELECT value FROM meta WHERE key='version'"));
115 if (!s.Step())
116 return 0;
117 return s.ColumnInt(0);
120 // The sql files located in "components/test/data/web_database" were generated
121 // by launching the Chromium application prior to schema change, then using
122 // the sqlite3 command-line application to dump the contents of the "Web Data"
123 // database.
124 // Like this:
125 // > .output version_nn.sql
126 // > .dump
127 void LoadDatabase(const base::FilePath::StringType& file);
129 private:
130 base::ScopedTempDir temp_dir_;
132 DISALLOW_COPY_AND_ASSIGN(WebDatabaseMigrationTest);
135 const int WebDatabaseMigrationTest::kCurrentTestedVersionNumber = 65;
137 void WebDatabaseMigrationTest::LoadDatabase(
138 const base::FilePath::StringType& file) {
139 std::string contents;
140 ASSERT_TRUE(GetWebDatabaseData(base::FilePath(file), &contents));
142 sql::Connection connection;
143 ASSERT_TRUE(connection.Open(GetDatabasePath()));
144 ASSERT_TRUE(connection.Execute(contents.data()));
147 // Tests that migrating from the golden files version_XX.sql results in the same
148 // schema as migrating from an empty database.
149 TEST_F(WebDatabaseMigrationTest, VersionXxSqlFilesAreGolden) {
150 DoMigration();
151 sql::Connection connection;
152 ASSERT_TRUE(connection.Open(GetDatabasePath()));
153 const std::string& expected_schema = RemoveQuotes(connection.GetSchema());
154 for (int i = WebDatabase::kDeprecatedVersionNumber + 1;
155 i < kCurrentTestedVersionNumber; ++i) {
156 // We don't test version 52 because there's a slight discrepancy in the
157 // initialization code and the migration code (relating to schema
158 // formatting). Fixing the bug is possible, but would require updating every
159 // version_nn.sql file.
160 if (i == 52)
161 continue;
163 connection.Raze();
164 const base::FilePath& file_name = base::FilePath::FromUTF8Unsafe(
165 "version_" + base::IntToString(i) + ".sql");
166 ASSERT_NO_FATAL_FAILURE(LoadDatabase(file_name.value()))
167 << "Failed to load " << file_name.MaybeAsASCII();
168 DoMigration();
169 EXPECT_EQ(expected_schema, RemoveQuotes(connection.GetSchema()))
170 << "For version " << i;
174 // Tests that the all migrations from an empty database succeed.
175 TEST_F(WebDatabaseMigrationTest, MigrateEmptyToCurrent) {
176 DoMigration();
178 // Verify post-conditions. These are expectations for current version of the
179 // database.
181 sql::Connection connection;
182 ASSERT_TRUE(connection.Open(GetDatabasePath()));
184 // Check version.
185 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
187 // Check that expected tables are present.
188 EXPECT_TRUE(connection.DoesTableExist("autofill"));
189 // The autofill_dates table is obsolete. (It's been merged into the autofill
190 // table.)
191 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
192 EXPECT_TRUE(connection.DoesTableExist("autofill_profiles"));
193 EXPECT_TRUE(connection.DoesTableExist("credit_cards"));
194 EXPECT_TRUE(connection.DoesTableExist("keywords"));
195 // The logins table is obsolete. (We used to store saved passwords here.)
196 EXPECT_FALSE(connection.DoesTableExist("logins"));
197 EXPECT_TRUE(connection.DoesTableExist("meta"));
198 EXPECT_TRUE(connection.DoesTableExist("token_service"));
199 // The web_apps and web_apps_icons tables are obsolete as of version 58.
200 EXPECT_FALSE(connection.DoesTableExist("web_apps"));
201 EXPECT_FALSE(connection.DoesTableExist("web_app_icons"));
202 // The web_intents and web_intents_defaults tables are obsolete as of
203 // version 58.
204 EXPECT_FALSE(connection.DoesTableExist("web_intents"));
205 EXPECT_FALSE(connection.DoesTableExist("web_intents_defaults"));
209 // Versions below 52 are deprecated. This verifies that old databases are razed.
210 TEST_F(WebDatabaseMigrationTest, RazeDeprecatedVersionAndReinit) {
211 ASSERT_NO_FATAL_FAILURE(
212 LoadDatabase(FILE_PATH_LITERAL("version_50.sql")));
214 // Verify pre-conditions. These are expectations for version 50 of the
215 // database.
217 sql::Connection connection;
218 ASSERT_TRUE(connection.Open(GetDatabasePath()));
219 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
221 sql::MetaTable meta_table;
222 ASSERT_TRUE(meta_table.Init(&connection, 50, 50));
224 ASSERT_FALSE(connection.DoesColumnExist("keywords", "image_url"));
225 ASSERT_FALSE(connection.DoesColumnExist("keywords",
226 "search_url_post_params"));
227 ASSERT_FALSE(connection.DoesColumnExist("keywords",
228 "suggest_url_post_params"));
229 ASSERT_FALSE(connection.DoesColumnExist("keywords",
230 "instant_url_post_params"));
231 ASSERT_FALSE(connection.DoesColumnExist("keywords",
232 "image_url_post_params"));
235 DoMigration();
237 // Verify post-conditions. These are expectations for current version of the
238 // database.
240 sql::Connection connection;
241 ASSERT_TRUE(connection.Open(GetDatabasePath()));
242 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
244 // Check version.
245 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
247 // New columns should have been created.
248 EXPECT_TRUE(connection.DoesColumnExist("keywords", "image_url"));
249 EXPECT_TRUE(connection.DoesColumnExist("keywords",
250 "search_url_post_params"));
251 EXPECT_TRUE(connection.DoesColumnExist("keywords",
252 "suggest_url_post_params"));
253 EXPECT_TRUE(connection.DoesColumnExist("keywords",
254 "instant_url_post_params"));
255 EXPECT_TRUE(connection.DoesColumnExist("keywords",
256 "image_url_post_params"));
260 // Tests that the column |new_tab_url| is added to the keyword table schema for
261 // a version 52 database.
262 TEST_F(WebDatabaseMigrationTest, MigrateVersion52ToCurrent) {
263 ASSERT_NO_FATAL_FAILURE(
264 LoadDatabase(FILE_PATH_LITERAL("version_52.sql")));
266 // Verify pre-conditions. These are expectations for version 52 of the
267 // database.
269 sql::Connection connection;
270 ASSERT_TRUE(connection.Open(GetDatabasePath()));
271 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
273 sql::MetaTable meta_table;
274 ASSERT_TRUE(meta_table.Init(&connection, 52, 52));
276 ASSERT_FALSE(connection.DoesColumnExist("keywords", "new_tab_url"));
279 DoMigration();
281 // Verify post-conditions. These are expectations for current version of the
282 // database.
284 sql::Connection connection;
285 ASSERT_TRUE(connection.Open(GetDatabasePath()));
286 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
288 // Check version.
289 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
291 // New columns should have been created.
292 EXPECT_TRUE(connection.DoesColumnExist("keywords", "new_tab_url"));
296 // Tests that for a version 54 database,
297 // (a) The street_address, dependent_locality, and sorting_code columns are
298 // added to the autofill_profiles table schema.
299 // (b) The address_line1, address_line2, and country columns are dropped from
300 // the autofill_profiles table schema.
301 // (c) The type column is dropped from the autofill_profile_phones schema.
302 TEST_F(WebDatabaseMigrationTest, MigrateVersion53ToCurrent) {
303 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_53.sql")));
305 // Verify pre-conditions. These are expectations for version 53 of the
306 // database.
308 sql::Connection connection;
309 ASSERT_TRUE(connection.Open(GetDatabasePath()));
311 EXPECT_TRUE(
312 connection.DoesColumnExist("autofill_profiles", "address_line_1"));
313 EXPECT_TRUE(
314 connection.DoesColumnExist("autofill_profiles", "address_line_2"));
315 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "country"));
316 EXPECT_FALSE(
317 connection.DoesColumnExist("autofill_profiles", "street_address"));
318 EXPECT_FALSE(
319 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
320 EXPECT_FALSE(
321 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
322 EXPECT_TRUE(connection.DoesColumnExist("autofill_profile_phones", "type"));
325 DoMigration();
327 // Verify post-conditions. These are expectations for current version of the
328 // database.
330 sql::Connection connection;
331 ASSERT_TRUE(connection.Open(GetDatabasePath()));
332 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
334 // Check version.
335 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
337 // Columns should have been added and removed appropriately.
338 EXPECT_FALSE(
339 connection.DoesColumnExist("autofill_profiles", "address_line1"));
340 EXPECT_FALSE(
341 connection.DoesColumnExist("autofill_profiles", "address_line2"));
342 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "country"));
343 EXPECT_TRUE(
344 connection.DoesColumnExist("autofill_profiles", "street_address"));
345 EXPECT_TRUE(
346 connection.DoesColumnExist("autofill_profiles", "dependent_locality"));
347 EXPECT_TRUE(
348 connection.DoesColumnExist("autofill_profiles", "sorting_code"));
349 EXPECT_FALSE(connection.DoesColumnExist("autofill_profile_phones", "type"));
351 // Data should have been preserved.
352 sql::Statement s_profiles(
353 connection.GetUniqueStatement(
354 "SELECT guid, company_name, street_address, dependent_locality,"
355 " city, state, zipcode, sorting_code, country_code, date_modified,"
356 " origin "
357 "FROM autofill_profiles"));
359 // Address lines 1 and 2.
360 ASSERT_TRUE(s_profiles.Step());
361 EXPECT_EQ("00000000-0000-0000-0000-000000000001",
362 s_profiles.ColumnString(0));
363 EXPECT_EQ(ASCIIToUTF16("Google, Inc."), s_profiles.ColumnString16(1));
364 EXPECT_EQ(ASCIIToUTF16("1950 Charleston Rd.\n"
365 "(2nd floor)"),
366 s_profiles.ColumnString16(2));
367 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
368 EXPECT_EQ(ASCIIToUTF16("Mountain View"), s_profiles.ColumnString16(4));
369 EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
370 EXPECT_EQ(ASCIIToUTF16("94043"), s_profiles.ColumnString16(6));
371 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
372 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
373 EXPECT_EQ(1386046731, s_profiles.ColumnInt(9));
374 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
376 // Only address line 1.
377 ASSERT_TRUE(s_profiles.Step());
378 EXPECT_EQ("00000000-0000-0000-0000-000000000002",
379 s_profiles.ColumnString(0));
380 EXPECT_EQ(ASCIIToUTF16("Google!"), s_profiles.ColumnString16(1));
381 EXPECT_EQ(ASCIIToUTF16("1600 Amphitheatre Pkwy."),
382 s_profiles.ColumnString16(2));
383 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
384 EXPECT_EQ(ASCIIToUTF16("Mtn. View"), s_profiles.ColumnString16(4));
385 EXPECT_EQ(ASCIIToUTF16("California"), s_profiles.ColumnString16(5));
386 EXPECT_EQ(ASCIIToUTF16("94043-1234"), s_profiles.ColumnString16(6));
387 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
388 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
389 EXPECT_EQ(1386046800, s_profiles.ColumnInt(9));
390 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
392 // Only address line 2.
393 ASSERT_TRUE(s_profiles.Step());
394 EXPECT_EQ("00000000-0000-0000-0000-000000000003",
395 s_profiles.ColumnString(0));
396 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
397 EXPECT_EQ(ASCIIToUTF16("\nOnly line 2???"), s_profiles.ColumnString16(2));
398 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
399 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
400 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(5));
401 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
402 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
403 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
404 EXPECT_EQ(1386046834, s_profiles.ColumnInt(9));
405 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
407 // No address lines.
408 ASSERT_TRUE(s_profiles.Step());
409 EXPECT_EQ("00000000-0000-0000-0000-000000000004",
410 s_profiles.ColumnString(0));
411 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(1));
412 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(2));
413 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
414 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(4));
415 EXPECT_EQ(ASCIIToUTF16("Texas"), s_profiles.ColumnString16(5));
416 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(6));
417 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
418 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(8));
419 EXPECT_EQ(1386046847, s_profiles.ColumnInt(9));
420 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
422 // That should be it.
423 EXPECT_FALSE(s_profiles.Step());
425 // Verify the phone number data as well.
426 sql::Statement s_phones(
427 connection.GetUniqueStatement(
428 "SELECT guid, number FROM autofill_profile_phones"));
430 ASSERT_TRUE(s_phones.Step());
431 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
432 EXPECT_EQ(ASCIIToUTF16("1.800.555.1234"), s_phones.ColumnString16(1));
434 ASSERT_TRUE(s_phones.Step());
435 EXPECT_EQ("00000000-0000-0000-0000-000000000001", s_phones.ColumnString(0));
436 EXPECT_EQ(ASCIIToUTF16("+1 (800) 555-4321"), s_phones.ColumnString16(1));
438 ASSERT_TRUE(s_phones.Step());
439 EXPECT_EQ("00000000-0000-0000-0000-000000000002", s_phones.ColumnString(0));
440 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
442 ASSERT_TRUE(s_phones.Step());
443 EXPECT_EQ("00000000-0000-0000-0000-000000000003", s_phones.ColumnString(0));
444 EXPECT_EQ(ASCIIToUTF16("6505557890"), s_phones.ColumnString16(1));
446 ASSERT_TRUE(s_phones.Step());
447 EXPECT_EQ("00000000-0000-0000-0000-000000000004", s_phones.ColumnString(0));
448 EXPECT_EQ(base::string16(), s_phones.ColumnString16(1));
450 EXPECT_FALSE(s_phones.Step());
454 // Tests that migrating from version 54 to version 55 drops the autofill_dates
455 // table, and merges the appropriate dates into the autofill table.
456 TEST_F(WebDatabaseMigrationTest, MigrateVersion54ToCurrent) {
457 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_54.sql")));
459 // Verify pre-conditions. These are expectations for version 54 of the
460 // database.
462 sql::Connection connection;
463 ASSERT_TRUE(connection.Open(GetDatabasePath()));
465 EXPECT_TRUE(connection.DoesTableExist("autofill_dates"));
466 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_created"));
467 EXPECT_FALSE(connection.DoesColumnExist("autofill", "date_last_used"));
469 // Verify the incoming data.
470 sql::Statement s_autofill(connection.GetUniqueStatement(
471 "SELECT name, value, value_lower, pair_id, count FROM autofill"));
472 sql::Statement s_dates(connection.GetUniqueStatement(
473 "SELECT pair_id, date_created FROM autofill_dates"));
475 // An entry with one timestamp.
476 ASSERT_TRUE(s_autofill.Step());
477 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
478 EXPECT_EQ(ASCIIToUTF16("John Doe"), s_autofill.ColumnString16(1));
479 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
480 EXPECT_EQ(10, s_autofill.ColumnInt(3));
481 EXPECT_EQ(1, s_autofill.ColumnInt(4));
482 ASSERT_TRUE(s_dates.Step());
483 EXPECT_EQ(10, s_dates.ColumnInt(0));
484 EXPECT_EQ(1384299100, s_dates.ColumnInt64(1));
486 // Another entry with one timestamp, differing from the previous one in case
487 // only.
488 ASSERT_TRUE(s_autofill.Step());
489 EXPECT_EQ(ASCIIToUTF16("Name"), s_autofill.ColumnString16(0));
490 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(1));
491 EXPECT_EQ(ASCIIToUTF16("john doe"), s_autofill.ColumnString16(2));
492 EXPECT_EQ(11, s_autofill.ColumnInt(3));
493 EXPECT_EQ(1, s_autofill.ColumnInt(4));
494 ASSERT_TRUE(s_dates.Step());
495 EXPECT_EQ(11, s_dates.ColumnInt(0));
496 EXPECT_EQ(1384299200, s_dates.ColumnInt64(1));
498 // An entry with two timestamps (with count > 2; this is realistic).
499 ASSERT_TRUE(s_autofill.Step());
500 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
501 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(1));
502 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s_autofill.ColumnString16(2));
503 EXPECT_EQ(20, s_autofill.ColumnInt(3));
504 EXPECT_EQ(3, s_autofill.ColumnInt(4));
505 ASSERT_TRUE(s_dates.Step());
506 EXPECT_EQ(20, s_dates.ColumnInt(0));
507 EXPECT_EQ(1384299300, s_dates.ColumnInt64(1));
508 ASSERT_TRUE(s_dates.Step());
509 EXPECT_EQ(20, s_dates.ColumnInt(0));
510 EXPECT_EQ(1384299301, s_dates.ColumnInt64(1));
512 // An entry with more than two timestamps, which are stored out of order.
513 ASSERT_TRUE(s_autofill.Step());
514 EXPECT_EQ(ASCIIToUTF16("Email"), s_autofill.ColumnString16(0));
515 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
516 s_autofill.ColumnString16(1));
517 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"),
518 s_autofill.ColumnString16(2));
519 EXPECT_EQ(21, s_autofill.ColumnInt(3));
520 EXPECT_EQ(4, s_autofill.ColumnInt(4));
521 ASSERT_TRUE(s_dates.Step());
522 EXPECT_EQ(21, s_dates.ColumnInt(0));
523 EXPECT_EQ(1384299401, s_dates.ColumnInt64(1));
524 ASSERT_TRUE(s_dates.Step());
525 EXPECT_EQ(21, s_dates.ColumnInt(0));
526 EXPECT_EQ(1384299400, s_dates.ColumnInt64(1));
527 ASSERT_TRUE(s_dates.Step());
528 EXPECT_EQ(21, s_dates.ColumnInt(0));
529 EXPECT_EQ(1384299403, s_dates.ColumnInt64(1));
530 ASSERT_TRUE(s_dates.Step());
531 EXPECT_EQ(21, s_dates.ColumnInt(0));
532 EXPECT_EQ(1384299402, s_dates.ColumnInt64(1));
534 // No more entries expected.
535 ASSERT_FALSE(s_autofill.Step());
536 ASSERT_FALSE(s_dates.Step());
539 DoMigration();
541 // Verify post-conditions. These are expectations for current version of the
542 // database.
544 sql::Connection connection;
545 ASSERT_TRUE(connection.Open(GetDatabasePath()));
546 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
548 // Check version.
549 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
551 // The autofill_dates table should have been dropped, and its columns should
552 // have been migrated to the autofill table.
553 EXPECT_FALSE(connection.DoesTableExist("autofill_dates"));
554 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_created"));
555 EXPECT_TRUE(connection.DoesColumnExist("autofill", "date_last_used"));
557 // Data should have been preserved. Note that it appears out of order
558 // relative to the previous table, as it's been alphabetized. That's ok.
559 sql::Statement s(
560 connection.GetUniqueStatement(
561 "SELECT name, value, value_lower, date_created, date_last_used,"
562 " count "
563 "FROM autofill "
564 "ORDER BY name, value ASC"));
566 // "jane.doe@example.org": Timestamps should be parsed correctly, and only
567 // the first and last should be kept.
568 ASSERT_TRUE(s.Step());
569 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
570 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(1));
571 EXPECT_EQ(ASCIIToUTF16("jane.doe@example.org"), s.ColumnString16(2));
572 EXPECT_EQ(1384299400, s.ColumnInt64(3));
573 EXPECT_EQ(1384299403, s.ColumnInt64(4));
574 EXPECT_EQ(4, s.ColumnInt(5));
576 // "jane@example.com": Timestamps should be parsed correctly.
577 ASSERT_TRUE(s.Step());
578 EXPECT_EQ(ASCIIToUTF16("Email"), s.ColumnString16(0));
579 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(1));
580 EXPECT_EQ(ASCIIToUTF16("jane@example.com"), s.ColumnString16(2));
581 EXPECT_EQ(1384299300, s.ColumnInt64(3));
582 EXPECT_EQ(1384299301, s.ColumnInt64(4));
583 EXPECT_EQ(3, s.ColumnInt(5));
585 // "John Doe": The single timestamp should be assigned as both the creation
586 // and the last use timestamp.
587 ASSERT_TRUE(s.Step());
588 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
589 EXPECT_EQ(ASCIIToUTF16("John Doe"), s.ColumnString16(1));
590 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
591 EXPECT_EQ(1384299100, s.ColumnInt64(3));
592 EXPECT_EQ(1384299100, s.ColumnInt64(4));
593 EXPECT_EQ(1, s.ColumnInt(5));
595 // "john doe": Should not be merged with "John Doe" (case-sensitivity).
596 ASSERT_TRUE(s.Step());
597 EXPECT_EQ(ASCIIToUTF16("Name"), s.ColumnString16(0));
598 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(1));
599 EXPECT_EQ(ASCIIToUTF16("john doe"), s.ColumnString16(2));
600 EXPECT_EQ(1384299200, s.ColumnInt64(3));
601 EXPECT_EQ(1384299200, s.ColumnInt64(4));
602 EXPECT_EQ(1, s.ColumnInt(5));
604 // No more entries expected.
605 ASSERT_FALSE(s.Step());
609 // Tests that migrating from version 55 to version 56 adds the language_code
610 // column to autofill_profiles table.
611 TEST_F(WebDatabaseMigrationTest, MigrateVersion55ToCurrent) {
612 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_55.sql")));
614 // Verify pre-conditions. These are expectations for version 55 of the
615 // database.
617 sql::Connection connection;
618 ASSERT_TRUE(connection.Open(GetDatabasePath()));
619 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
621 EXPECT_FALSE(
622 connection.DoesColumnExist("autofill_profiles", "language_code"));
625 DoMigration();
627 // Verify post-conditions. These are expectations for current version of the
628 // database.
630 sql::Connection connection;
631 ASSERT_TRUE(connection.Open(GetDatabasePath()));
632 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
634 // Check version.
635 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
637 // The language_code column should have been added to autofill_profiles
638 // table.
639 EXPECT_TRUE(
640 connection.DoesColumnExist("autofill_profiles", "language_code"));
642 // Data should have been preserved. Language code should have been set to
643 // empty string.
644 sql::Statement s_profiles(
645 connection.GetUniqueStatement(
646 "SELECT guid, company_name, street_address, dependent_locality,"
647 " city, state, zipcode, sorting_code, country_code, date_modified,"
648 " origin, language_code "
649 "FROM autofill_profiles"));
651 ASSERT_TRUE(s_profiles.Step());
652 EXPECT_EQ("00000000-0000-0000-0000-000000000001",
653 s_profiles.ColumnString(0));
654 EXPECT_EQ(ASCIIToUTF16("Google Inc"), s_profiles.ColumnString16(1));
655 EXPECT_EQ(ASCIIToUTF16("340 Main St"),
656 s_profiles.ColumnString16(2));
657 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(3));
658 EXPECT_EQ(ASCIIToUTF16("Los Angeles"), s_profiles.ColumnString16(4));
659 EXPECT_EQ(ASCIIToUTF16("CA"), s_profiles.ColumnString16(5));
660 EXPECT_EQ(ASCIIToUTF16("90291"), s_profiles.ColumnString16(6));
661 EXPECT_EQ(base::string16(), s_profiles.ColumnString16(7));
662 EXPECT_EQ(ASCIIToUTF16("US"), s_profiles.ColumnString16(8));
663 EXPECT_EQ(1395948829, s_profiles.ColumnInt(9));
664 EXPECT_EQ(ASCIIToUTF16("Chrome settings"), s_profiles.ColumnString16(10));
665 EXPECT_EQ(std::string(), s_profiles.ColumnString(11));
667 // No more entries expected.
668 ASSERT_FALSE(s_profiles.Step());
672 // Tests that migrating from version 56 to version 57 adds the full_name
673 // column to autofill_profile_names table.
674 TEST_F(WebDatabaseMigrationTest, MigrateVersion56ToCurrent) {
675 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_56.sql")));
677 // Verify pre-conditions. These are expectations for version 56 of the
678 // database.
680 sql::Connection connection;
681 ASSERT_TRUE(connection.Open(GetDatabasePath()));
682 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
684 EXPECT_FALSE(
685 connection.DoesColumnExist("autofill_profile_names", "full_name"));
687 // Verify the starting data.
688 sql::Statement s_names(
689 connection.GetUniqueStatement(
690 "SELECT guid, first_name, middle_name, last_name "
691 "FROM autofill_profile_names"));
692 ASSERT_TRUE(s_names.Step());
693 EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
694 EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
695 EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
696 EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
699 DoMigration();
701 // Verify post-conditions. These are expectations for current version of the
702 // database.
704 sql::Connection connection;
705 ASSERT_TRUE(connection.Open(GetDatabasePath()));
706 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
708 // Check version.
709 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
711 // The full_name column should have been added to autofill_profile_names
712 // table.
713 EXPECT_TRUE(
714 connection.DoesColumnExist("autofill_profile_names", "full_name"));
716 // Data should have been preserved. Full name should have been set to the
717 // empty string.
718 sql::Statement s_names(
719 connection.GetUniqueStatement(
720 "SELECT guid, first_name, middle_name, last_name, full_name "
721 "FROM autofill_profile_names"));
723 ASSERT_TRUE(s_names.Step());
724 EXPECT_EQ("B41FE6E0-B13E-2A2A-BF0B-29FCE2C3ADBD", s_names.ColumnString(0));
725 EXPECT_EQ(ASCIIToUTF16("Jon"), s_names.ColumnString16(1));
726 EXPECT_EQ(base::string16(), s_names.ColumnString16(2));
727 EXPECT_EQ(ASCIIToUTF16("Smith"), s_names.ColumnString16(3));
728 EXPECT_EQ(base::string16(), s_names.ColumnString16(4));
730 // No more entries expected.
731 ASSERT_FALSE(s_names.Step());
735 // Tests that migrating from version 57 to version 58 drops the web_intents and
736 // web_apps tables.
737 TEST_F(WebDatabaseMigrationTest, MigrateVersion57ToCurrent) {
738 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_57.sql")));
740 // Verify pre-conditions. These are expectations for version 57 of the
741 // database.
743 sql::Connection connection;
744 ASSERT_TRUE(connection.Open(GetDatabasePath()));
745 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
747 EXPECT_TRUE(connection.DoesTableExist("web_apps"));
748 EXPECT_TRUE(connection.DoesTableExist("web_app_icons"));
749 EXPECT_TRUE(connection.DoesTableExist("web_intents"));
750 EXPECT_TRUE(connection.DoesTableExist("web_intents_defaults"));
753 DoMigration();
755 // Verify post-conditions. These are expectations for current version of the
756 // database.
758 sql::Connection connection;
759 ASSERT_TRUE(connection.Open(GetDatabasePath()));
760 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
762 // Check version.
763 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
765 EXPECT_FALSE(connection.DoesTableExist("web_apps"));
766 EXPECT_FALSE(connection.DoesTableExist("web_app_icons"));
767 EXPECT_FALSE(connection.DoesTableExist("web_intents"));
768 EXPECT_FALSE(connection.DoesTableExist("web_intents_defaults"));
772 // Tests that migrating from version 58 to version 59 drops the omnibox
773 // extension keywords.
774 TEST_F(WebDatabaseMigrationTest, MigrateVersion58ToCurrent) {
775 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_58.sql")));
777 const char query_extensions[] = "SELECT * FROM keywords "
778 "WHERE url='chrome-extension://iphchnegaodmijmkdlbhbanjhfphhikp/"
779 "?q={searchTerms}'";
780 // Verify pre-conditions.
782 sql::Connection connection;
783 ASSERT_TRUE(connection.Open(GetDatabasePath()));
784 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
786 sql::MetaTable meta_table;
787 ASSERT_TRUE(meta_table.Init(&connection, 58, 58));
789 sql::Statement s(connection.GetUniqueStatement(query_extensions));
790 ASSERT_TRUE(s.is_valid());
791 int count = 0;
792 while (s.Step()) {
793 ++count;
795 EXPECT_EQ(1, count);
798 DoMigration();
800 // Verify post-conditions.
802 sql::Connection connection;
803 ASSERT_TRUE(connection.Open(GetDatabasePath()));
804 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
806 // Check version.
807 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
809 sql::Statement s(connection.GetUniqueStatement(query_extensions));
810 ASSERT_TRUE(s.is_valid());
811 int count = 0;
812 while (s.Step()) {
813 ++count;
815 EXPECT_EQ(0, count);
817 s.Assign(connection.GetUniqueStatement("SELECT * FROM keywords "
818 "WHERE short_name='Google'"));
819 ASSERT_TRUE(s.is_valid());
820 count = 0;
821 while (s.Step()) {
822 ++count;
824 EXPECT_EQ(1, count);
828 // Tests creation of the server_credit_cards table.
829 TEST_F(WebDatabaseMigrationTest, MigrateVersion59ToCurrent) {
830 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_59.sql")));
832 // Verify pre-conditions.
834 sql::Connection connection;
835 ASSERT_TRUE(connection.Open(GetDatabasePath()));
836 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
838 sql::MetaTable meta_table;
839 ASSERT_TRUE(meta_table.Init(&connection, 59, 59));
841 ASSERT_FALSE(connection.DoesTableExist("masked_credit_cards"));
842 ASSERT_FALSE(connection.DoesTableExist("unmasked_credit_cards"));
843 ASSERT_FALSE(connection.DoesTableExist("server_addresses"));
846 DoMigration();
848 // Verify post-conditions.
850 sql::Connection connection;
851 ASSERT_TRUE(connection.Open(GetDatabasePath()));
852 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
854 // Check version.
855 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
857 ASSERT_TRUE(connection.DoesTableExist("masked_credit_cards"));
858 ASSERT_TRUE(connection.DoesTableExist("unmasked_credit_cards"));
859 ASSERT_TRUE(connection.DoesTableExist("server_addresses"));
863 // Tests addition of use_count and use_date fields to autofill profiles and
864 // credit cards.
865 TEST_F(WebDatabaseMigrationTest, MigrateVersion60ToCurrent) {
866 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_60.sql")));
868 // Verify pre-conditions.
870 sql::Connection connection;
871 ASSERT_TRUE(connection.Open(GetDatabasePath()));
872 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
874 sql::MetaTable meta_table;
875 ASSERT_TRUE(meta_table.Init(&connection, 60, 60));
877 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "use_count"));
878 EXPECT_FALSE(connection.DoesColumnExist("credit_cards", "use_date"));
879 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "use_count"));
880 EXPECT_FALSE(connection.DoesColumnExist("autofill_profiles", "use_date"));
883 DoMigration();
885 // Verify post-conditions.
887 sql::Connection connection;
888 ASSERT_TRUE(connection.Open(GetDatabasePath()));
889 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
891 // Check version.
892 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
894 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "use_count"));
895 EXPECT_TRUE(connection.DoesColumnExist("credit_cards", "use_date"));
896 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "use_count"));
897 EXPECT_TRUE(connection.DoesColumnExist("autofill_profiles", "use_date"));
901 // Tests addition of use_count and use_date fields to unmasked server cards.
902 TEST_F(WebDatabaseMigrationTest, MigrateVersion61ToCurrent) {
903 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_61.sql")));
905 // Verify pre-conditions.
907 sql::Connection connection;
908 ASSERT_TRUE(connection.Open(GetDatabasePath()));
909 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
911 sql::MetaTable meta_table;
912 ASSERT_TRUE(meta_table.Init(&connection, 61, 61));
914 EXPECT_FALSE(connection.DoesColumnExist("unmasked_credit_cards",
915 "use_count"));
916 EXPECT_FALSE(connection.DoesColumnExist("unmasked_credit_cards",
917 "use_date"));
920 DoMigration();
922 // Verify post-conditions.
924 sql::Connection connection;
925 ASSERT_TRUE(connection.Open(GetDatabasePath()));
926 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
928 // Check version.
929 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
931 EXPECT_TRUE(connection.DoesColumnExist("unmasked_credit_cards",
932 "use_count"));
933 EXPECT_TRUE(connection.DoesColumnExist("unmasked_credit_cards",
934 "use_date"));
938 // Tests addition of server metadata tables.
939 TEST_F(WebDatabaseMigrationTest, MigrateVersion64ToCurrent) {
940 ASSERT_NO_FATAL_FAILURE(LoadDatabase(FILE_PATH_LITERAL("version_64.sql")));
942 // Verify pre-conditions.
944 sql::Connection connection;
945 ASSERT_TRUE(connection.Open(GetDatabasePath()));
946 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
948 sql::MetaTable meta_table;
949 ASSERT_TRUE(meta_table.Init(&connection, 64, 64));
951 EXPECT_FALSE(connection.DoesTableExist("server_card_metadata"));
952 EXPECT_FALSE(connection.DoesTableExist("server_address_metadata"));
954 // Add a server address --- make sure it gets an ID.
955 sql::Statement insert_profiles(
956 connection.GetUniqueStatement(
957 "INSERT INTO server_addresses(id, postal_code) "
958 "VALUES ('', 90210)"));
959 insert_profiles.Run();
962 DoMigration();
964 // Verify post-conditions.
966 sql::Connection connection;
967 ASSERT_TRUE(connection.Open(GetDatabasePath()));
968 ASSERT_TRUE(sql::MetaTable::DoesTableExist(&connection));
970 // Check version.
971 EXPECT_EQ(kCurrentTestedVersionNumber, VersionFromConnection(&connection));
973 EXPECT_TRUE(connection.DoesTableExist("server_card_metadata"));
974 EXPECT_TRUE(connection.DoesTableExist("server_address_metadata"));
976 sql::Statement read_profiles(
977 connection.GetUniqueStatement(
978 "SELECT id, postal_code FROM server_addresses"));
979 ASSERT_TRUE(read_profiles.Step());
980 EXPECT_FALSE(read_profiles.ColumnString(0).empty());
981 EXPECT_EQ("90210", read_profiles.ColumnString(1));