| OLD | NEW |
| (Empty) | |
| 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_interned_string.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 protected: |
| 27 base::ScopedTempDir temp_dir_; |
| 28 sql::Connection db_; |
| 29 }; |
| 30 |
| 31 // Check that initializing the database works. |
| 32 TEST_F(DatabaseStringTableTest, Init) { |
| 33 DatabaseStringTable table("test"); |
| 34 table.Initialize(&db_); |
| 35 ASSERT_TRUE(db_.DoesTableExist("test")); |
| 36 ASSERT_TRUE(db_.DoesIndexExist("test_index")); |
| 37 } |
| 38 |
| 39 // Insert a new mapping into the table, then verify the table contents. |
| 40 TEST_F(DatabaseStringTableTest, Insert) { |
| 41 DatabaseStringTable table("test"); |
| 42 table.Initialize(&db_); |
| 43 int64 id; |
| 44 ASSERT_TRUE(table.StringToInt(&db_, "abc", &id)); |
| 45 |
| 46 sql::Statement query( |
| 47 db_.GetUniqueStatement("SELECT id FROM test WHERE value = 'abc'")); |
| 48 ASSERT_TRUE(query.Step()); |
| 49 int64 raw_id = query.ColumnInt64(0); |
| 50 ASSERT_EQ(id, raw_id); |
| 51 } |
| 52 |
| 53 // Check that different strings are mapped to different values, and the same |
| 54 // string is mapped to the same value repeatably. |
| 55 TEST_F(DatabaseStringTableTest, InsertMultiple) { |
| 56 DatabaseStringTable table("test"); |
| 57 table.Initialize(&db_); |
| 58 |
| 59 int64 id1; |
| 60 int64 id2; |
| 61 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1)); |
| 62 ASSERT_TRUE(table.StringToInt(&db_, "string2", &id2)); |
| 63 ASSERT_NE(id1, id2); |
| 64 |
| 65 int64 id1a; |
| 66 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1a)); |
| 67 ASSERT_EQ(id1, id1a); |
| 68 } |
| 69 |
| 70 // Check that values can be read back from the database even after the |
| 71 // in-memory cache is cleared. |
| 72 TEST_F(DatabaseStringTableTest, CacheCleared) { |
| 73 DatabaseStringTable table("test"); |
| 74 table.Initialize(&db_); |
| 75 |
| 76 int64 id1; |
| 77 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id1)); |
| 78 |
| 79 table.ClearCache(); |
| 80 |
| 81 int64 id2; |
| 82 ASSERT_TRUE(table.StringToInt(&db_, "string1", &id2)); |
| 83 ASSERT_EQ(id1, id2); |
| 84 } |
| 85 |
| 86 // Check that direct database modifications are picked up after the cache is |
| 87 // cleared. |
| 88 TEST_F(DatabaseStringTableTest, DatabaseModified) { |
| 89 DatabaseStringTable table("test"); |
| 90 table.Initialize(&db_); |
| 91 |
| 92 int64 id1; |
| 93 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id1)); |
| 94 |
| 95 ASSERT_TRUE( |
| 96 db_.Execute("UPDATE test SET id = id + 1 WHERE value = 'modified'")); |
| 97 |
| 98 int64 id2; |
| 99 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id2)); |
| 100 ASSERT_EQ(id1, id2); |
| 101 |
| 102 table.ClearCache(); |
| 103 |
| 104 int64 id3; |
| 105 ASSERT_TRUE(table.StringToInt(&db_, "modified", &id3)); |
| 106 ASSERT_EQ(id1 + 1, id3); |
| 107 } |
| 108 |
| 109 |
| 110 } // namespace extensions |
| OLD | NEW |