| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 <vector> | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/time.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/predictors/autocomplete_action_predictor_database.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "content/test/test_browser_thread.h" | |
| 13 #include "sql/statement.h" | |
| 14 | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using base::Time; | |
| 18 using base::TimeDelta; | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 struct AutocompleteActionPredictorDatabase::Row test_db[] = { | |
| 24 AutocompleteActionPredictorDatabase::Row( | |
| 25 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", | |
| 26 ASCIIToUTF16("goog"), GURL("http://www.google.com/"), | |
| 27 1, 0), | |
| 28 AutocompleteActionPredictorDatabase::Row( | |
| 29 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", | |
| 30 ASCIIToUTF16("slash"), GURL("http://slashdot.org/"), | |
| 31 3, 2), | |
| 32 AutocompleteActionPredictorDatabase::Row( | |
| 33 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", | |
| 34 ASCIIToUTF16("news"), GURL("http://slashdot.org/"), | |
| 35 0, 1), | |
| 36 }; | |
| 37 | |
| 38 } // end namespace | |
| 39 | |
| 40 class AutocompleteActionPredictorDatabaseTest : public testing::Test { | |
| 41 public: | |
| 42 AutocompleteActionPredictorDatabaseTest(); | |
| 43 virtual ~AutocompleteActionPredictorDatabaseTest(); | |
| 44 | |
| 45 virtual void SetUp(); | |
| 46 virtual void TearDown(); | |
| 47 | |
| 48 size_t CountRecords() const; | |
| 49 | |
| 50 void AddAll(); | |
| 51 | |
| 52 bool RowsAreEqual(const AutocompleteActionPredictorDatabase::Row& lhs, | |
| 53 const AutocompleteActionPredictorDatabase::Row& rhs) const; | |
| 54 | |
| 55 TestingProfile* profile() { return &profile_; } | |
| 56 | |
| 57 protected: | |
| 58 | |
| 59 // Test functions that can be run against this text fixture or | |
| 60 // AutocompleteActionPredictorDatabaseReopenTest that inherits from this. | |
| 61 void TestAddRow(); | |
| 62 void TestGetRow(); | |
| 63 void TestUpdateRow(); | |
| 64 void TestDeleteRow(); | |
| 65 void TestDeleteRows(); | |
| 66 void TestDeleteAllRows(); | |
| 67 | |
| 68 private: | |
| 69 TestingProfile profile_; | |
| 70 scoped_refptr<AutocompleteActionPredictorDatabase> db_; | |
| 71 MessageLoop loop_; | |
| 72 content::TestBrowserThread db_thread_; | |
| 73 }; | |
| 74 | |
| 75 class AutocompleteActionPredictorDatabaseReopenTest | |
| 76 : public AutocompleteActionPredictorDatabaseTest { | |
| 77 public: | |
| 78 virtual void SetUp() { | |
| 79 // By calling SetUp twice, we make sure that the table already exists for | |
| 80 // this fixture. | |
| 81 AutocompleteActionPredictorDatabaseTest::SetUp(); | |
| 82 AutocompleteActionPredictorDatabaseTest::TearDown(); | |
| 83 AutocompleteActionPredictorDatabaseTest::SetUp(); | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 AutocompleteActionPredictorDatabaseTest:: | |
| 88 AutocompleteActionPredictorDatabaseTest() | |
| 89 : loop_(MessageLoop::TYPE_DEFAULT), | |
| 90 db_thread_(BrowserThread::DB, &loop_) { | |
| 91 } | |
| 92 | |
| 93 AutocompleteActionPredictorDatabaseTest:: | |
| 94 ~AutocompleteActionPredictorDatabaseTest() { | |
| 95 } | |
| 96 | |
| 97 void AutocompleteActionPredictorDatabaseTest::SetUp() { | |
| 98 db_ = new AutocompleteActionPredictorDatabase(&profile_); | |
| 99 db_->Initialize(); | |
| 100 } | |
| 101 | |
| 102 void AutocompleteActionPredictorDatabaseTest::TearDown() { | |
| 103 db_ = NULL; | |
| 104 } | |
| 105 | |
| 106 size_t AutocompleteActionPredictorDatabaseTest::CountRecords() const { | |
| 107 sql::Statement s(db_->db_.GetUniqueStatement( | |
| 108 "SELECT count(*) FROM network_action_predictor")); | |
| 109 EXPECT_TRUE(s.Step()); | |
| 110 return static_cast<size_t>(s.ColumnInt(0)); | |
| 111 } | |
| 112 | |
| 113 void AutocompleteActionPredictorDatabaseTest::AddAll() { | |
| 114 for (size_t i = 0; i < arraysize(test_db); ++i) | |
| 115 db_->AddRow(test_db[i]); | |
| 116 | |
| 117 EXPECT_EQ(arraysize(test_db), CountRecords()); | |
| 118 } | |
| 119 | |
| 120 bool AutocompleteActionPredictorDatabaseTest::RowsAreEqual( | |
| 121 const AutocompleteActionPredictorDatabase::Row& lhs, | |
| 122 const AutocompleteActionPredictorDatabase::Row& rhs) const { | |
| 123 return (lhs.id == rhs.id && | |
| 124 lhs.user_text == rhs.user_text && | |
| 125 lhs.url == rhs.url && | |
| 126 lhs.number_of_hits == rhs.number_of_hits && | |
| 127 lhs.number_of_misses == rhs.number_of_misses); | |
| 128 } | |
| 129 | |
| 130 void AutocompleteActionPredictorDatabaseTest::TestAddRow() { | |
| 131 EXPECT_EQ(0U, CountRecords()); | |
| 132 db_->AddRow(test_db[0]); | |
| 133 EXPECT_EQ(1U, CountRecords()); | |
| 134 db_->AddRow(test_db[1]); | |
| 135 EXPECT_EQ(2U, CountRecords()); | |
| 136 db_->AddRow(test_db[2]); | |
| 137 EXPECT_EQ(3U, CountRecords()); | |
| 138 } | |
| 139 | |
| 140 void AutocompleteActionPredictorDatabaseTest::TestGetRow() { | |
| 141 db_->AddRow(test_db[0]); | |
| 142 AutocompleteActionPredictorDatabase::Row row; | |
| 143 db_->GetRow(test_db[0].id, &row); | |
| 144 EXPECT_TRUE(RowsAreEqual(test_db[0], row)) | |
| 145 << "Expected: Row with id " << test_db[0].id << "\n" | |
| 146 << "Got: Row with id " << row.id; | |
| 147 } | |
| 148 | |
| 149 void AutocompleteActionPredictorDatabaseTest::TestUpdateRow() { | |
| 150 AddAll(); | |
| 151 AutocompleteActionPredictorDatabase::Row row = test_db[1]; | |
| 152 row.number_of_hits = row.number_of_hits + 1; | |
| 153 db_->UpdateRow(row); | |
| 154 | |
| 155 AutocompleteActionPredictorDatabase::Row updated_row; | |
| 156 db_->GetRow(test_db[1].id, &updated_row); | |
| 157 | |
| 158 EXPECT_TRUE(RowsAreEqual(row, updated_row)) | |
| 159 << "Expected: Row with id " << row.id << "\n" | |
| 160 << "Got: Row with id " << updated_row.id; | |
| 161 } | |
| 162 | |
| 163 void AutocompleteActionPredictorDatabaseTest::TestDeleteRow() { | |
| 164 AddAll(); | |
| 165 db_->DeleteRow(test_db[2].id); | |
| 166 EXPECT_EQ(arraysize(test_db) - 1, CountRecords()); | |
| 167 } | |
| 168 | |
| 169 void AutocompleteActionPredictorDatabaseTest::TestDeleteRows() { | |
| 170 AddAll(); | |
| 171 std::vector<AutocompleteActionPredictorDatabase::Row::Id> id_list; | |
| 172 id_list.push_back(test_db[0].id); | |
| 173 id_list.push_back(test_db[2].id); | |
| 174 db_->DeleteRows(id_list); | |
| 175 EXPECT_EQ(arraysize(test_db) - 2, CountRecords()); | |
| 176 | |
| 177 AutocompleteActionPredictorDatabase::Row row; | |
| 178 db_->GetRow(test_db[1].id, &row); | |
| 179 EXPECT_TRUE(RowsAreEqual(test_db[1], row)); | |
| 180 } | |
| 181 | |
| 182 void AutocompleteActionPredictorDatabaseTest::TestDeleteAllRows() { | |
| 183 AddAll(); | |
| 184 db_->DeleteAllRows(); | |
| 185 EXPECT_EQ(0U, CountRecords()); | |
| 186 } | |
| 187 | |
| 188 // AutocompleteActionPredictorDatabaseTest tests | |
| 189 TEST_F(AutocompleteActionPredictorDatabaseTest, AddRow) { | |
| 190 TestAddRow(); | |
| 191 } | |
| 192 | |
| 193 TEST_F(AutocompleteActionPredictorDatabaseTest, GetRow) { | |
| 194 TestGetRow(); | |
| 195 } | |
| 196 | |
| 197 TEST_F(AutocompleteActionPredictorDatabaseTest, UpdateRow) { | |
| 198 TestUpdateRow(); | |
| 199 } | |
| 200 | |
| 201 TEST_F(AutocompleteActionPredictorDatabaseTest, DeleteRow) { | |
| 202 TestDeleteRow(); | |
| 203 } | |
| 204 | |
| 205 TEST_F(AutocompleteActionPredictorDatabaseTest, DeleteRows) { | |
| 206 TestDeleteRows(); | |
| 207 } | |
| 208 | |
| 209 TEST_F(AutocompleteActionPredictorDatabaseTest, DeleteAllRows) { | |
| 210 TestDeleteAllRows(); | |
| 211 } | |
| 212 | |
| 213 // AutocompleteActionPredictorDatabaseReopenTest tests | |
| 214 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, AddRow) { | |
| 215 TestAddRow(); | |
| 216 } | |
| 217 | |
| 218 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, GetRow) { | |
| 219 TestGetRow(); | |
| 220 } | |
| 221 | |
| 222 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, UpdateRow) { | |
| 223 TestUpdateRow(); | |
| 224 } | |
| 225 | |
| 226 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, DeleteRow) { | |
| 227 TestDeleteRow(); | |
| 228 } | |
| 229 | |
| 230 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, DeleteRows) { | |
| 231 TestDeleteRows(); | |
| 232 } | |
| 233 | |
| 234 TEST_F(AutocompleteActionPredictorDatabaseReopenTest, DeleteAllRows) { | |
| 235 TestDeleteAllRows(); | |
| 236 } | |
| OLD | NEW |