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 virtual 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 virtual void TearDown() OVERRIDE
{
28 base::ScopedTempDir temp_dir_
;
32 // Check that initializing the database works.
33 TEST_F(DatabaseStringTableTest
, Init
) {
34 DatabaseStringTable
table("test");
35 table
.Initialize(&db_
);
36 ASSERT_TRUE(db_
.DoesTableExist("test"));
37 ASSERT_TRUE(db_
.DoesIndexExist("test_index"));
40 // Insert a new mapping into the table, then verify the table contents.
41 TEST_F(DatabaseStringTableTest
, Insert
) {
42 DatabaseStringTable
table("test");
43 table
.Initialize(&db_
);
45 ASSERT_TRUE(table
.StringToInt(&db_
, "abc", &id
));
48 db_
.GetUniqueStatement("SELECT id FROM test WHERE value = 'abc'"));
49 ASSERT_TRUE(query
.Step());
50 int64 raw_id
= query
.ColumnInt64(0);
51 ASSERT_EQ(id
, raw_id
);
54 // Check that different strings are mapped to different values, and the same
55 // string is mapped to the same value repeatably.
56 TEST_F(DatabaseStringTableTest
, InsertMultiple
) {
57 DatabaseStringTable
table("test");
58 table
.Initialize(&db_
);
62 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1
));
63 ASSERT_TRUE(table
.StringToInt(&db_
, "string2", &id2
));
67 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1a
));
71 // Check that values can be read back from the database even after the
72 // in-memory cache is cleared.
73 TEST_F(DatabaseStringTableTest
, CacheCleared
) {
74 DatabaseStringTable
table("test");
75 table
.Initialize(&db_
);
78 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id1
));
83 ASSERT_TRUE(table
.StringToInt(&db_
, "string1", &id2
));
87 // Check that direct database modifications are picked up after the cache is
89 TEST_F(DatabaseStringTableTest
, DatabaseModified
) {
90 DatabaseStringTable
table("test");
91 table
.Initialize(&db_
);
94 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id1
));
97 db_
.Execute("UPDATE test SET id = id + 1 WHERE value = 'modified'"));
100 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id2
));
106 ASSERT_TRUE(table
.StringToInt(&db_
, "modified", &id3
));
107 ASSERT_EQ(id1
+ 1, id3
);
110 // Check that looking up an unknown id returns an error.
111 TEST_F(DatabaseStringTableTest
, BadLookup
) {
112 DatabaseStringTable
table("test");
113 table
.Initialize(&db_
);
115 ASSERT_FALSE(table
.IntToString(&db_
, 1, &value
));
118 // Check looking up an inserted value, both cached and not cached.
119 TEST_F(DatabaseStringTableTest
, Lookup
) {
120 DatabaseStringTable
table("test");
121 table
.Initialize(&db_
);
123 ASSERT_TRUE(table
.StringToInt(&db_
, "abc", &id
));
126 ASSERT_TRUE(table
.IntToString(&db_
, id
, &value
));
127 ASSERT_EQ("abc", value
);
131 ASSERT_TRUE(table
.IntToString(&db_
, id
, &value
));
132 ASSERT_EQ("abc", value
);
135 // Check that the in-memory cache for the string table does not become too
136 // large, even if many items are inserted.
137 TEST_F(DatabaseStringTableTest
, Prune
) {
138 DatabaseStringTable
table("size_test");
139 table
.Initialize(&db_
);
141 // Wrap the lookups in a transaction to improve performance.
142 sql::Transaction
transaction(&db_
);
145 for (int i
= 0; i
< 2000; i
++) {
147 ASSERT_TRUE(table
.StringToInt(&db_
, base::StringPrintf("value-%d", i
),
150 transaction
.Commit();
152 // The maximum size below should correspond to kMaximumCacheSize in
153 // database_string_table.cc, with a small amount of additional slop (an entry
154 // might be inserted after doing the pruning).
155 ASSERT_LE(table
.id_to_value_
.size(), 1005U);
156 ASSERT_LE(table
.value_to_id_
.size(), 1005U);
159 } // namespace extensions