[email protected] | e0ffc6f | 2012-04-28 07:04:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4ae8002 | 2013-06-07 07:51:13 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | e464fee | 2013-06-11 12:42:20 | [diff] [blame] | 6 | #include "base/strings/string_util.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 7 | #include "base/strings/utf_string_conversions.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 8 | #include "sql/connection.h" |
| 9 | #include "sql/statement.h" |
[email protected] | ee40adcb | 2013-07-15 21:09:19 | [diff] [blame] | 10 | #include "sql/test/scoped_error_ignorer.h" |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | ee40adcb | 2013-07-15 21:09:19 | [diff] [blame] | 12 | #include "third_party/sqlite/sqlite3.h" |
[email protected] | 08b1f75f | 2013-05-22 22:02:38 | [diff] [blame] | 13 | #include "webkit/browser/database/databases_table.h" |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 14 | |
[email protected] | f729d7a | 2013-12-26 07:07:56 | [diff] [blame^] | 15 | using base::ASCIIToUTF16; |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 16 | |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 17 | namespace webkit_database { |
| 18 | |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 19 | static void CheckDetailsAreEqual(const DatabaseDetails& d1, |
| 20 | const DatabaseDetails& d2) { |
| 21 | EXPECT_EQ(d1.origin_identifier, d2.origin_identifier); |
| 22 | EXPECT_EQ(d1.database_name, d2.database_name); |
| 23 | EXPECT_EQ(d1.description, d2.description); |
| 24 | EXPECT_EQ(d1.estimated_size, d2.estimated_size); |
| 25 | } |
| 26 | |
| 27 | static bool DatabasesTableIsEmpty(sql::Connection* db) { |
| 28 | sql::Statement statement(db->GetCachedStatement( |
| 29 | SQL_FROM_HERE, "SELECT COUNT(*) FROM Databases")); |
| 30 | return (statement.is_valid() && statement.Step() && !statement.ColumnInt(0)); |
| 31 | } |
| 32 | |
| 33 | TEST(DatabasesTableTest, TestIt) { |
| 34 | // Initialize the 'Databases' table. |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 35 | sql::Connection db; |
| 36 | |
[email protected] | ee40adcb | 2013-07-15 21:09:19 | [diff] [blame] | 37 | sql::ScopedErrorIgnorer ignore_errors; |
| 38 | // TODO(shess): Suppressing SQLITE_CONSTRAINT because the code |
| 39 | // expects that and handles the resulting error. Consider revising |
| 40 | // the code to use INSERT OR IGNORE (which would not throw |
| 41 | // SQLITE_CONSTRAINT) and then check ChangeCount() to see if any |
| 42 | // changes were made. |
| 43 | ignore_errors.IgnoreError(SQLITE_CONSTRAINT); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 44 | |
| 45 | // Initialize the temp dir and the 'Databases' table. |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 46 | EXPECT_TRUE(db.OpenInMemory()); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 47 | DatabasesTable databases_table(&db); |
| 48 | EXPECT_TRUE(databases_table.Init()); |
| 49 | |
| 50 | // The 'Databases' table should be empty. |
| 51 | EXPECT_TRUE(DatabasesTableIsEmpty(&db)); |
| 52 | |
| 53 | // Create the details for a databases. |
| 54 | DatabaseDetails details_in1; |
| 55 | DatabaseDetails details_out1; |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 56 | details_in1.origin_identifier = "origin1"; |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 57 | details_in1.database_name = ASCIIToUTF16("db1"); |
| 58 | details_in1.description = ASCIIToUTF16("description_db1"); |
| 59 | details_in1.estimated_size = 100; |
| 60 | |
| 61 | // Updating details for this database should fail. |
| 62 | EXPECT_FALSE(databases_table.UpdateDatabaseDetails(details_in1)); |
| 63 | EXPECT_FALSE(databases_table.GetDatabaseDetails( |
| 64 | details_in1.origin_identifier, |
| 65 | details_in1.database_name, |
| 66 | &details_out1)); |
| 67 | |
| 68 | // Inserting details for this database should pass. |
| 69 | EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in1)); |
| 70 | EXPECT_TRUE(databases_table.GetDatabaseDetails( |
| 71 | details_in1.origin_identifier, |
| 72 | details_in1.database_name, |
| 73 | &details_out1)); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 74 | EXPECT_EQ(1, databases_table.GetDatabaseID(details_in1.origin_identifier, |
| 75 | details_in1.database_name)); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 76 | |
| 77 | // Check that the details were correctly written to the database. |
| 78 | CheckDetailsAreEqual(details_in1, details_out1); |
| 79 | |
| 80 | // Check that inserting a duplicate row fails. |
| 81 | EXPECT_FALSE(databases_table.InsertDatabaseDetails(details_in1)); |
| 82 | |
| 83 | // Insert details for another database with the same origin. |
| 84 | DatabaseDetails details_in2; |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 85 | details_in2.origin_identifier = "origin1"; |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 86 | details_in2.database_name = ASCIIToUTF16("db2"); |
| 87 | details_in2.description = ASCIIToUTF16("description_db2"); |
| 88 | details_in2.estimated_size = 200; |
| 89 | EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in2)); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 90 | EXPECT_EQ(2, databases_table.GetDatabaseID(details_in2.origin_identifier, |
| 91 | details_in2.database_name)); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 92 | |
| 93 | // Insert details for a third database with a different origin. |
| 94 | DatabaseDetails details_in3; |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 95 | details_in3.origin_identifier = "origin2"; |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 96 | details_in3.database_name = ASCIIToUTF16("db3"); |
| 97 | details_in3.description = ASCIIToUTF16("description_db3"); |
| 98 | details_in3.estimated_size = 300; |
| 99 | EXPECT_TRUE(databases_table.InsertDatabaseDetails(details_in3)); |
[email protected] | 3c5ed2c | 2009-11-13 01:30:45 | [diff] [blame] | 100 | EXPECT_EQ(3, databases_table.GetDatabaseID(details_in3.origin_identifier, |
| 101 | details_in3.database_name)); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 102 | |
| 103 | // There should be no database with origin "origin3". |
| 104 | std::vector<DatabaseDetails> details_out_origin3; |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 105 | EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier( |
| 106 | "origin3", &details_out_origin3)); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 107 | EXPECT_TRUE(details_out_origin3.empty()); |
| 108 | |
| 109 | // There should be only two databases with origin "origin1". |
| 110 | std::vector<DatabaseDetails> details_out_origin1; |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 111 | EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier( |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 112 | details_in1.origin_identifier, &details_out_origin1)); |
| 113 | EXPECT_EQ(size_t(2), details_out_origin1.size()); |
| 114 | CheckDetailsAreEqual(details_in1, details_out_origin1[0]); |
| 115 | CheckDetailsAreEqual(details_in2, details_out_origin1[1]); |
| 116 | |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 117 | // Get the list of all origins: should be "origin1" and "origin2". |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 118 | std::vector<std::string> origins_out; |
| 119 | EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out)); |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 120 | EXPECT_EQ(size_t(2), origins_out.size()); |
| 121 | EXPECT_EQ(details_in1.origin_identifier, origins_out[0]); |
| 122 | EXPECT_EQ(details_in3.origin_identifier, origins_out[1]); |
| 123 | |
| 124 | // Delete an origin and check that it's no longer in the table. |
| 125 | origins_out.clear(); |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 126 | EXPECT_TRUE(databases_table.DeleteOriginIdentifier( |
| 127 | details_in3.origin_identifier)); |
| 128 | EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out)); |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 129 | EXPECT_EQ(size_t(1), origins_out.size()); |
| 130 | EXPECT_EQ(details_in1.origin_identifier, origins_out[0]); |
| 131 | |
| 132 | // Deleting an origin that doesn't have any record in this table should fail. |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 133 | EXPECT_FALSE(databases_table.DeleteOriginIdentifier("unknown_origin")); |
[email protected] | 3ccfe53 | 2010-01-06 21:15:55 | [diff] [blame] | 134 | |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 135 | // Delete the details for 'db1' and check that they're no longer there. |
| 136 | EXPECT_TRUE(databases_table.DeleteDatabaseDetails( |
| 137 | details_in1.origin_identifier, details_in1.database_name)); |
| 138 | EXPECT_FALSE(databases_table.GetDatabaseDetails( |
| 139 | details_in1.origin_identifier, |
| 140 | details_in1.database_name, |
| 141 | &details_out1)); |
| 142 | |
| 143 | // Check that trying to delete a record that doesn't exist fails. |
| 144 | EXPECT_FALSE(databases_table.DeleteDatabaseDetails( |
[email protected] | 5e30159 | 2013-06-18 06:36:05 | [diff] [blame] | 145 | "unknown_origin", ASCIIToUTF16("unknown_database"))); |
[email protected] | ee40adcb | 2013-07-15 21:09:19 | [diff] [blame] | 146 | |
| 147 | ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); |
[email protected] | 615dedfc | 2009-11-02 21:41:56 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | } // namespace webkit_database |