1 // Copyright 2013 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/files/scoped_temp_dir.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/extensions/activity_log/database_string_table.h"
8 #include "sql/connection.h"
9 #include "sql/statement.h"
10 #include "sql/transaction.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions
{
15 class DatabaseStringTableTest
: public testing::Test
{
17 void SetUp() override
{
18 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
19 base::FilePath db_file
= temp_dir_
.path().AppendASCII("StringTable.db");
21 ASSERT_TRUE(db_
.Open(db_file
));
24 void TearDown() override
{ db_
.Close(); }
26 base::ScopedTempDir temp_dir_
;
30 // Check that initializing the database works.
31 TEST_F(DatabaseStringTableTest
, Init
) {
32 DatabaseStringTable
table("test");
33 table
.Initialize(&db_
);
34 ASSERT_TRUE(db_
.DoesTableExist("test"));
35 ASSERT_TRUE(db_
.DoesIndexExist("test_index"));
38 // Insert a new mapping into the table, then verify the table contents.
39 TEST_F(DatabaseStringTableTest
, Insert
) {
40 DatabaseStringTable
table("test");
41 table
.Initialize(&db_
);
43 ASSERT_TRUE(table
.StringToInt(&db_
, "abc", &id
));
46 db_
.GetUniqueStatement("SELECT id FROM test WHERE value = 'abc'"));
47 ASSERT_TRUE(query
.Step());
48 int64 raw_id
= query
.ColumnInt64(0);
49 ASSERT_EQ(id
, raw_id
);
52 // Check that different strings are mapped to different values, and the same
53 // string is mapped to the same value repeatably.
54 TEST_F(DatabaseStringTableTest
, InsertMultiple
) {
55 DatabaseStringTable
table("test");
56 table
.Initialize(&db_
);
60 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1
));
61 ASSERT_TRUE(table
.StringToInt(&db_
, "string2", &id2
));
65 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1a
));
69 // Check that values can be read back from the database even after the
70 // in-memory cache is cleared.
71 TEST_F(DatabaseStringTableTest
, CacheCleared
) {
72 DatabaseStringTable
table("test");
73 table
.Initialize(&db_
);
76 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1
));
81 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id2
));
85 // Check that direct database modifications are picked up after the cache is
87 TEST_F(DatabaseStringTableTest
, DatabaseModified
) {
88 DatabaseStringTable
table("test");
89 table
.Initialize(&db_
);
92 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id1
));
95 db_
.Execute("UPDATE test SET id = id + 1 WHERE value = 'modified'"));
98 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id2
));
104 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id3
));
105 ASSERT_EQ(id1
+ 1, id3
);
108 // Check that looking up an unknown id returns an error.
109 TEST_F(DatabaseStringTableTest
, BadLookup
) {
110 DatabaseStringTable
table("test");
111 table
.Initialize(&db_
);
113 ASSERT_FALSE(table
.IntToString(&db_
, 1, &value
));
116 // Check looking up an inserted value, both cached and not cached.
117 TEST_F(DatabaseStringTableTest
, Lookup
) {
118 DatabaseStringTable
table("test");
119 table
.Initialize(&db_
);
121 ASSERT_TRUE(table
.StringToInt(&db_
, "abc", &id
));
124 ASSERT_TRUE(table
.IntToString(&db_
, id
, &value
));
125 ASSERT_EQ("abc", value
);
129 ASSERT_TRUE(table
.IntToString(&db_
, id
, &value
));
130 ASSERT_EQ("abc", value
);
133 // Check that the in-memory cache for the string table does not become too
134 // large, even if many items are inserted.
135 TEST_F(DatabaseStringTableTest
, Prune
) {
136 DatabaseStringTable
table("size_test");
137 table
.Initialize(&db_
);
139 // Wrap the lookups in a transaction to improve performance.
140 sql::Transaction
transaction(&db_
);
143 for (int i
= 0; i
< 2000; i
++) {
145 ASSERT_TRUE(table
.StringToInt(&db_
, base::StringPrintf("value-%d", i
),
148 transaction
.Commit();
150 // The maximum size below should correspond to kMaximumCacheSize in
151 // database_string_table.cc, with a small amount of additional slop (an entry
152 // might be inserted after doing the pruning).
153 ASSERT_LE(table
.id_to_value_
.size(), 1005U);
154 ASSERT_LE(table
.value_to_id_
.size(), 1005U);
157 } // namespace extensions