blob: ee864cd3a8e50e38e1d90cd5352837427afc275d [file] [log] [blame]
[email protected]8ca51502014-05-21 03:37:401// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]615dedfc2009-11-02 21:41:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4ae80022013-06-07 07:51:135#include "base/bind.h"
[email protected]e464fee2013-06-11 12:42:206#include "base/strings/string_util.h"
[email protected]906265872013-06-07 22:40:457#include "base/strings/utf_string_conversions.h"
[email protected]f0a54b22011-07-19 18:40:218#include "sql/connection.h"
9#include "sql/statement.h"
[email protected]ee40adcb2013-07-15 21:09:1910#include "sql/test/scoped_error_ignorer.h"
pilgrime92c5fcd2014-09-10 23:31:2311#include "storage/browser/database/databases_table.h"
[email protected]615dedfc2009-11-02 21:41:5612#include "testing/gtest/include/gtest/gtest.h"
[email protected]ee40adcb2013-07-15 21:09:1913#include "third_party/sqlite/sqlite3.h"
[email protected]615dedfc2009-11-02 21:41:5614
[email protected]f729d7a2013-12-26 07:07:5615using base::ASCIIToUTF16;
[email protected]cd501a72014-08-22 19:58:3116using storage::DatabaseDetails;
17using storage::DatabasesTable;
[email protected]6c3bf032013-12-25 19:37:0318
[email protected]8ca51502014-05-21 03:37:4019namespace content {
[email protected]3ccfe532010-01-06 21:15:5520
[email protected]615dedfc2009-11-02 21:41:5621static 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
29static 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
35TEST(DatabasesTableTest, TestIt) {
36 // Initialize the 'Databases' table.
[email protected]615dedfc2009-11-02 21:41:5637 sql::Connection db;
38
[email protected]ee40adcb2013-07-15 21:09:1939 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]615dedfc2009-11-02 21:41:5646
47 // Initialize the temp dir and the 'Databases' table.
[email protected]3ccfe532010-01-06 21:15:5548 EXPECT_TRUE(db.OpenInMemory());
[email protected]615dedfc2009-11-02 21:41:5649 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]5e301592013-06-18 06:36:0558 details_in1.origin_identifier = "origin1";
[email protected]615dedfc2009-11-02 21:41:5659 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]3c5ed2c2009-11-13 01:30:4576 EXPECT_EQ(1, databases_table.GetDatabaseID(details_in1.origin_identifier,
77 details_in1.database_name));
[email protected]615dedfc2009-11-02 21:41:5678
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]5e301592013-06-18 06:36:0587 details_in2.origin_identifier = "origin1";
[email protected]615dedfc2009-11-02 21:41:5688 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]3c5ed2c2009-11-13 01:30:4592 EXPECT_EQ(2, databases_table.GetDatabaseID(details_in2.origin_identifier,
93 details_in2.database_name));
[email protected]615dedfc2009-11-02 21:41:5694
95 // Insert details for a third database with a different origin.
96 DatabaseDetails details_in3;
[email protected]5e301592013-06-18 06:36:0597 details_in3.origin_identifier = "origin2";
[email protected]615dedfc2009-11-02 21:41:5698 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]3c5ed2c2009-11-13 01:30:45102 EXPECT_EQ(3, databases_table.GetDatabaseID(details_in3.origin_identifier,
103 details_in3.database_name));
[email protected]615dedfc2009-11-02 21:41:56104
105 // There should be no database with origin "origin3".
106 std::vector<DatabaseDetails> details_out_origin3;
[email protected]5e301592013-06-18 06:36:05107 EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
108 "origin3", &details_out_origin3));
[email protected]615dedfc2009-11-02 21:41:56109 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]5e301592013-06-18 06:36:05113 EXPECT_TRUE(databases_table.GetAllDatabaseDetailsForOriginIdentifier(
[email protected]615dedfc2009-11-02 21:41:56114 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]3ccfe532010-01-06 21:15:55119 // Get the list of all origins: should be "origin1" and "origin2".
[email protected]5e301592013-06-18 06:36:05120 std::vector<std::string> origins_out;
121 EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
[email protected]3ccfe532010-01-06 21:15:55122 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]5e301592013-06-18 06:36:05128 EXPECT_TRUE(databases_table.DeleteOriginIdentifier(
129 details_in3.origin_identifier));
130 EXPECT_TRUE(databases_table.GetAllOriginIdentifiers(&origins_out));
[email protected]3ccfe532010-01-06 21:15:55131 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]5e301592013-06-18 06:36:05135 EXPECT_FALSE(databases_table.DeleteOriginIdentifier("unknown_origin"));
[email protected]3ccfe532010-01-06 21:15:55136
[email protected]615dedfc2009-11-02 21:41:56137 // 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]5e301592013-06-18 06:36:05147 "unknown_origin", ASCIIToUTF16("unknown_database")));
[email protected]ee40adcb2013-07-15 21:09:19148
149 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors());
[email protected]615dedfc2009-11-02 21:41:56150}
151
[email protected]8ca51502014-05-21 03:37:40152} // namespace content