| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "sql/connection.h" | 10 #include "sql/connection.h" |
| 11 #include "sql/statement.h" | 11 #include "sql/statement.h" |
| 12 #include "sql/test/scoped_error_ignorer.h" | 12 #include "sql/test/scoped_error_expecter.h" |
| 13 #include "storage/browser/database/databases_table.h" | 13 #include "storage/browser/database/databases_table.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/sqlite/sqlite3.h" | 15 #include "third_party/sqlite/sqlite3.h" |
| 16 | 16 |
| 17 using base::ASCIIToUTF16; | 17 using base::ASCIIToUTF16; |
| 18 using storage::DatabaseDetails; | 18 using storage::DatabaseDetails; |
| 19 using storage::DatabasesTable; | 19 using storage::DatabasesTable; |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 static void CheckDetailsAreEqual(const DatabaseDetails& d1, | 23 static void CheckDetailsAreEqual(const DatabaseDetails& d1, |
| 24 const DatabaseDetails& d2) { | 24 const DatabaseDetails& d2) { |
| 25 EXPECT_EQ(d1.origin_identifier, d2.origin_identifier); | 25 EXPECT_EQ(d1.origin_identifier, d2.origin_identifier); |
| 26 EXPECT_EQ(d1.database_name, d2.database_name); | 26 EXPECT_EQ(d1.database_name, d2.database_name); |
| 27 EXPECT_EQ(d1.description, d2.description); | 27 EXPECT_EQ(d1.description, d2.description); |
| 28 EXPECT_EQ(d1.estimated_size, d2.estimated_size); | 28 EXPECT_EQ(d1.estimated_size, d2.estimated_size); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static bool DatabasesTableIsEmpty(sql::Connection* db) { | 31 static bool DatabasesTableIsEmpty(sql::Connection* db) { |
| 32 sql::Statement statement(db->GetCachedStatement( | 32 sql::Statement statement(db->GetCachedStatement( |
| 33 SQL_FROM_HERE, "SELECT COUNT(*) FROM Databases")); | 33 SQL_FROM_HERE, "SELECT COUNT(*) FROM Databases")); |
| 34 return (statement.is_valid() && statement.Step() && !statement.ColumnInt(0)); | 34 return (statement.is_valid() && statement.Step() && !statement.ColumnInt(0)); |
| 35 } | 35 } |
| 36 | 36 |
| 37 TEST(DatabasesTableTest, TestIt) { | 37 TEST(DatabasesTableTest, TestIt) { |
| 38 // Initialize the 'Databases' table. | 38 // Initialize the 'Databases' table. |
| 39 sql::Connection db; | 39 sql::Connection db; |
| 40 | 40 |
| 41 sql::ScopedErrorIgnorer ignore_errors; | 41 sql::test::ScopedErrorExpecter expecter; |
| 42 // TODO(shess): Suppressing SQLITE_CONSTRAINT because the code | 42 // TODO(shess): Suppressing SQLITE_CONSTRAINT because the code |
| 43 // expects that and handles the resulting error. Consider revising | 43 // expects that and handles the resulting error. Consider revising |
| 44 // the code to use INSERT OR IGNORE (which would not throw | 44 // the code to use INSERT OR IGNORE (which would not throw |
| 45 // SQLITE_CONSTRAINT) and then check ChangeCount() to see if any | 45 // SQLITE_CONSTRAINT) and then check ChangeCount() to see if any |
| 46 // changes were made. | 46 // changes were made. |
| 47 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); | 47 expecter.ExpectError(SQLITE_CONSTRAINT); |
| 48 | 48 |
| 49 // Initialize the temp dir and the 'Databases' table. | 49 // Initialize the temp dir and the 'Databases' table. |
| 50 EXPECT_TRUE(db.OpenInMemory()); | 50 EXPECT_TRUE(db.OpenInMemory()); |
| 51 DatabasesTable databases_table(&db); | 51 DatabasesTable databases_table(&db); |
| 52 EXPECT_TRUE(databases_table.Init()); | 52 EXPECT_TRUE(databases_table.Init()); |
| 53 | 53 |
| 54 // The 'Databases' table should be empty. | 54 // The 'Databases' table should be empty. |
| 55 EXPECT_TRUE(DatabasesTableIsEmpty(&db)); | 55 EXPECT_TRUE(DatabasesTableIsEmpty(&db)); |
| 56 | 56 |
| 57 // Create the details for a databases. | 57 // Create the details for a databases. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 details_in1.origin_identifier, details_in1.database_name)); | 141 details_in1.origin_identifier, details_in1.database_name)); |
| 142 EXPECT_FALSE(databases_table.GetDatabaseDetails( | 142 EXPECT_FALSE(databases_table.GetDatabaseDetails( |
| 143 details_in1.origin_identifier, | 143 details_in1.origin_identifier, |
| 144 details_in1.database_name, | 144 details_in1.database_name, |
| 145 &details_out1)); | 145 &details_out1)); |
| 146 | 146 |
| 147 // Check that trying to delete a record that doesn't exist fails. | 147 // Check that trying to delete a record that doesn't exist fails. |
| 148 EXPECT_FALSE(databases_table.DeleteDatabaseDetails( | 148 EXPECT_FALSE(databases_table.DeleteDatabaseDetails( |
| 149 "unknown_origin", ASCIIToUTF16("unknown_database"))); | 149 "unknown_origin", ASCIIToUTF16("unknown_database"))); |
| 150 | 150 |
| 151 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 151 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace content | 154 } // namespace content |
| OLD | NEW |