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