Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / database_string_table_unittest.cc
blob90433b973c33d489c7e68038c85b96bbbeeaa3e2
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 {
16 protected:
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_;
27 sql::Connection db_;
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_);
42 int64 id;
43 ASSERT_TRUE(table.StringToInt(&db_, "abc", &id));
45 sql::Statement query(
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_);
58 int64 id1;
59 int64 id2;
60 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1));
61 ASSERT_TRUE(table.StringToInt(&db_, "string2", &id2));
62 ASSERT_NE(id1, id2);
64 int64 id1a;
65 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1a));
66 ASSERT_EQ(id1, 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_);
75 int64 id1;
76 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1));
78 table.ClearCache();
80 int64 id2;
81 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id2));
82 ASSERT_EQ(id1, id2);
85 // Check that direct database modifications are picked up after the cache is
86 // cleared.
87 TEST_F(DatabaseStringTableTest, DatabaseModified) {
88 DatabaseStringTable table("test");
89 table.Initialize(&db_);
91 int64 id1;
92 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id1));
94 ASSERT_TRUE(
95 db_.Execute("UPDATE test SET id = id + 1 WHERE value = 'modified'"));
97 int64 id2;
98 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id2));
99 ASSERT_EQ(id1, id2);
101 table.ClearCache();
103 int64 id3;
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_);
112 std::string value;
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_);
120 int64 id;
121 ASSERT_TRUE(table.StringToInt(&db_, "abc", &id));
123 std::string value;
124 ASSERT_TRUE(table.IntToString(&db_, id, &value));
125 ASSERT_EQ("abc", value);
127 table.ClearCache();
128 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_);
142 transaction.Begin();
143 for (int i = 0; i < 2000; i++) {
144 int64 id;
145 ASSERT_TRUE(table.StringToInt(&db_, base::StringPrintf("value-%d", i),
146 &id));
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