[Chromoting] Run jscompile over JS for HTML files (GN-only)
[chromium-blink-merge.git] / content / browser / databases_table_unittest.cc
blobee864cd3a8e50e38e1d90cd5352837427afc275d
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "sql/connection.h"
9 #include "sql/statement.h"
10 #include "sql/test/scoped_error_ignorer.h"
11 #include "storage/browser/database/databases_table.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h"
15 using base::ASCIIToUTF16;
16 using storage::DatabaseDetails;
17 using storage::DatabasesTable;
19 namespace content {
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);
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));
35 TEST(DatabasesTableTest, TestIt) {
36 // Initialize the 'Databases' table.
37 sql::Connection db;
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);
47 // Initialize the temp dir and the 'Databases' table.
48 EXPECT_TRUE(db.OpenInMemory());
49 DatabasesTable databases_table(&db);
50 EXPECT_TRUE(databases_table.Init());
52 // The 'Databases' table should be empty.
53 EXPECT_TRUE(DatabasesTableIsEmpty(&db));
55 // Create the details for a databases.
56 DatabaseDetails details_in1;
57 DatabaseDetails details_out1;
58 details_in1.origin_identifier = "origin1";
59 details_in1.database_name = ASCIIToUTF16("db1");
60 details_in1.description = ASCIIToUTF16("description_db1");
61 details_in1.estimated_size = 100;
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));
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));
76 EXPECT_EQ(1, databases_table.GetDatabaseID(details_in1.origin_identifier,
77 details_in1.database_name));
79 // Check that the details were correctly written to the database.
80 CheckDetailsAreEqual(details_in1, details_out1);
82 // Check that inserting a duplicate row fails.
83 EXPECT_FALSE(databases_table.InsertDatabaseDetails(details_in1));
85 // Insert details for another database with the same origin.
86 DatabaseDetails details_in2;
87 details_in2.origin_identifier = "origin1";
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));
92 EXPECT_EQ(2, databases_table.GetDatabaseID(details_in2.origin_identifier,
93 details_in2.database_name));
95 // Insert details for a third database with a different origin.
96 DatabaseDetails details_in3;
97 details_in3.origin_identifier = "origin2";
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));
102 EXPECT_EQ(3, databases_table.GetDatabaseID(details_in3.origin_identifier,
103 details_in3.database_name));
105 // There should be no database with origin "origin3".
106 std::vector<DatabaseDetails> details_out_origin3;
107 EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
108 "origin3", &details_out_origin3));
109 EXPECT_TRUE(details_out_origin3.empty());
111 // There should be only two databases with origin "origin1".
112 std::vector<DatabaseDetails> details_out_origin1;
113 EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
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]);
119 // Get the list of all origins: should be "origin1" and "origin2".
120 std::vector<std::string> origins_out;
121 EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
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]);
126 // Delete an origin and check that it's no longer in the table.
127 origins_out.clear();
128 EXPECT_TRUE(databases_table.DeleteOriginIdentifier(
129 details_in3.origin_identifier));
130 EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
131 EXPECT_EQ(size_t(1), origins_out.size());
132 EXPECT_EQ(details_in1.origin_identifier, origins_out[0]);
134 // Deleting an origin that doesn't have any record in this table should fail.
135 EXPECT_FALSE(databases_table.DeleteOriginIdentifier("unknown_origin"));
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));
145 // Check that trying to delete a record that doesn't exist fails.
146 EXPECT_FALSE(databases_table.DeleteDatabaseDetails(
147 "unknown_origin", ASCIIToUTF16("unknown_database")));
149 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
152 } // namespace content