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. 4 5 #include "base/files/scoped_temp_dir.h" 6 #include "chrome/browser/extensions/activity_log/database_string_table.h" 7 #include "sql/connection.h" 8 #include "sql/statement.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace extensions { 12 13 class DatabaseStringTableTest : public testing::Test { 14 protected: 15 virtual void SetUp() OVERRIDE { 16 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 17 base::FilePath db_file = temp_dir_.path().AppendASCII("StringTable.db"); 18 19 ASSERT_TRUE(db_.Open(db_file)); 20 } 21 22 virtual void TearDown() OVERRIDE { 23 db_.Close(); 24 } 25 26 base::ScopedTempDir temp_dir_; 27 sql::Connection db_; 28 }; 29 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")); 36 } 37 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)); 44 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); 50 } 51 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_); 57 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); 63 64 int64 id1a; 65 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1a)); 66 ASSERT_EQ(id1, id1a); 67 } 68 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_); 74 75 int64 id1; 76 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1)); 77 78 table.ClearCache(); 79 80 int64 id2; 81 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id2)); 82 ASSERT_EQ(id1, id2); 83 } 84 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_); 90 91 int64 id1; 92 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id1)); 93 94 ASSERT_TRUE( 95 db_.Execute("UPDATE test SET id = id + 1 WHERE value = 'modified'")); 96 97 int64 id2; 98 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id2)); 99 ASSERT_EQ(id1, id2); 100 101 table.ClearCache(); 102 103 int64 id3; 104 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id3)); 105 ASSERT_EQ(id1 + 1, id3); 106 } 107 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)); 114 } 115 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)); 122 123 std::string value; 124 ASSERT_TRUE(table.IntToString(&db_, id, &value)); 125 ASSERT_EQ("abc", value); 126 127 table.ClearCache(); 128 value = ""; 129 ASSERT_TRUE(table.IntToString(&db_, id, &value)); 130 ASSERT_EQ("abc", value); 131 } 132 133 } // namespace extensions 134