Chromium Code Reviews| 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_table.h" | |
| 11 #include "chrome/browser/predictors/predictor_database.h" | |
| 12 #include "chrome/browser/predictors/predictor_database_factory.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/test/test_browser_thread.h" | |
| 15 #include "sql/statement.h" | |
| 16 | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using base::Time; | |
| 20 using base::TimeDelta; | |
| 21 using content::BrowserThread; | |
| 22 using predictors::AutocompleteActionPredictorTable; | |
| 23 | |
| 24 namespace predictors { | |
| 25 | |
| 26 class AutocompleteActionPredictorTableTest : public testing::Test { | |
| 27 public: | |
| 28 AutocompleteActionPredictorTableTest(); | |
| 29 virtual ~AutocompleteActionPredictorTableTest(); | |
| 30 | |
| 31 virtual void SetUp(); | |
| 32 virtual void TearDown(); | |
| 33 | |
| 34 size_t CountRecords() const; | |
| 35 | |
| 36 void AddAll(); | |
| 37 | |
| 38 bool RowsAreEqual(const AutocompleteActionPredictorTable::Row& lhs, | |
| 39 const AutocompleteActionPredictorTable::Row& rhs) const; | |
| 40 | |
| 41 TestingProfile* profile() { return &profile_; } | |
| 42 | |
| 43 protected: | |
| 44 | |
| 45 // Test functions that can be run against this text fixture or | |
| 46 // AutocompleteActionPredictorTableReopenTest that inherits from this. | |
| 47 void TestAddRow(); | |
| 48 void TestGetRow(); | |
| 49 void TestUpdateRow(); | |
| 50 void TestAddAndUpdateRows(); | |
| 51 void TestDeleteRows(); | |
| 52 void TestDeleteAllRows(); | |
| 53 | |
| 54 AutocompleteActionPredictorTable::Rows test_db_; | |
| 55 | |
| 56 private: | |
| 57 TestingProfile profile_; | |
| 58 scoped_ptr<PredictorDatabase> db_; | |
| 59 MessageLoop loop_; | |
| 60 content::TestBrowserThread db_thread_; | |
| 61 }; | |
| 62 | |
| 63 class AutocompleteActionPredictorTableReopenTest | |
| 64 : public AutocompleteActionPredictorTableTest { | |
| 65 public: | |
| 66 virtual void SetUp() { | |
| 67 // By calling SetUp twice, we make sure that the table already exists for | |
| 68 // this fixture. | |
| 69 AutocompleteActionPredictorTableTest::SetUp(); | |
| 70 AutocompleteActionPredictorTableTest::TearDown(); | |
| 71 AutocompleteActionPredictorTableTest::SetUp(); | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 AutocompleteActionPredictorTableTest::AutocompleteActionPredictorTableTest() | |
| 76 : loop_(MessageLoop::TYPE_DEFAULT), | |
| 77 db_thread_(BrowserThread::DB, &loop_) { | |
| 78 } | |
| 79 | |
| 80 AutocompleteActionPredictorTableTest::~AutocompleteActionPredictorTableTest() { | |
| 81 } | |
| 82 | |
| 83 void AutocompleteActionPredictorTableTest::SetUp() { | |
| 84 db_.reset(new PredictorDatabase(&profile_)); | |
| 85 loop_.RunAllPending(); | |
| 86 | |
| 87 test_db_.push_back(AutocompleteActionPredictorTable::Row( | |
| 88 "BD85DBA2-8C29-49F9-84AE-48E1E90880DF", | |
| 89 ASCIIToUTF16("goog"), GURL("http://www.google.com/"), | |
| 90 1, 0)); | |
| 91 test_db_.push_back(AutocompleteActionPredictorTable::Row( | |
| 92 "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", | |
| 93 ASCIIToUTF16("slash"), GURL("http://slashdot.org/"), | |
| 94 3, 2)); | |
| 95 test_db_.push_back(AutocompleteActionPredictorTable::Row( | |
| 96 "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", | |
| 97 ASCIIToUTF16("news"), GURL("http://slashdot.org/"), | |
| 98 0, 1)); | |
| 99 } | |
| 100 | |
| 101 void AutocompleteActionPredictorTableTest::TearDown() { | |
| 102 db_.reset(NULL); | |
| 103 test_db_.clear(); | |
| 104 } | |
| 105 | |
| 106 size_t AutocompleteActionPredictorTableTest::CountRecords() const { | |
| 107 sql::Statement s(db_->GetDatabase()->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 AutocompleteActionPredictorTableTest::AddAll() { | |
| 114 db_->autocomplete_table()->AddAndUpdateRows( | |
| 115 test_db_, AutocompleteActionPredictorTable::Rows()); | |
| 116 | |
| 117 EXPECT_EQ(test_db_.size(), CountRecords()); | |
| 118 } | |
| 119 | |
| 120 bool AutocompleteActionPredictorTableTest::RowsAreEqual( | |
| 121 const AutocompleteActionPredictorTable::Row& lhs, | |
| 122 const AutocompleteActionPredictorTable::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 AutocompleteActionPredictorTableTest::TestAddRow() { | |
| 131 /* EXPECT_EQ(0U, CountRecords()); | |
|
dominich
2012/05/08 20:35:28
remove this test.
Shishir
2012/05/08 21:49:03
Done.
| |
| 132 db_->autocomplete_table()->AddRow(test_db_[0]); | |
| 133 EXPECT_EQ(1U, CountRecords()); | |
| 134 db_->autocomplete_table()->AddRow(test_db_[1]); | |
| 135 EXPECT_EQ(2U, CountRecords()); | |
| 136 db_->autocomplete_table()->AddRow(test_db_[2]); | |
| 137 EXPECT_EQ(3U, CountRecords()); | |
| 138 */ | |
| 139 } | |
| 140 | |
| 141 void AutocompleteActionPredictorTableTest::TestGetRow() { | |
| 142 db_->autocomplete_table()->AddAndUpdateRows( | |
| 143 AutocompleteActionPredictorTable::Rows(1, test_db_[0]), | |
| 144 AutocompleteActionPredictorTable::Rows()); | |
| 145 AutocompleteActionPredictorTable::Row row; | |
| 146 db_->autocomplete_table()->GetRow(test_db_[0].id, &row); | |
| 147 EXPECT_TRUE(RowsAreEqual(test_db_[0], row)) | |
| 148 << "Expected: Row with id " << test_db_[0].id << "\n" | |
| 149 << "Got: Row with id " << row.id; | |
| 150 } | |
| 151 | |
| 152 void AutocompleteActionPredictorTableTest::TestAddAndUpdateRows() { | |
| 153 EXPECT_EQ(0U, CountRecords()); | |
| 154 | |
| 155 AutocompleteActionPredictorTable::Rows rows_to_add; | |
| 156 rows_to_add.push_back(test_db_[0]); | |
| 157 rows_to_add.push_back(test_db_[1]); | |
| 158 db_->autocomplete_table()->AddAndUpdateRows( | |
| 159 rows_to_add, | |
| 160 AutocompleteActionPredictorTable::Rows()); | |
| 161 EXPECT_EQ(2U, CountRecords()); | |
| 162 | |
| 163 AutocompleteActionPredictorTable::Row row1 = test_db_[1]; | |
| 164 row1.number_of_hits = row1.number_of_hits + 1; | |
| 165 db_->autocomplete_table()->AddAndUpdateRows( | |
| 166 AutocompleteActionPredictorTable::Rows(1, test_db_[2]), | |
| 167 AutocompleteActionPredictorTable::Rows(1, row1)); | |
| 168 EXPECT_EQ(3U, CountRecords()); | |
| 169 | |
| 170 AutocompleteActionPredictorTable::Row updated_row1; | |
| 171 db_->autocomplete_table()->GetRow(test_db_[1].id, &updated_row1); | |
| 172 EXPECT_TRUE(RowsAreEqual(row1, updated_row1)) | |
| 173 << "Expected: Row with id " << row1.id << "\n" | |
| 174 << "Got: Row with id " << updated_row1.id; | |
| 175 | |
| 176 AutocompleteActionPredictorTable::Row row0 = test_db_[0]; | |
| 177 row0.number_of_hits = row0.number_of_hits + 2; | |
| 178 AutocompleteActionPredictorTable::Row row2 = test_db_[2]; | |
| 179 row2.number_of_hits = row2.number_of_hits + 2; | |
| 180 AutocompleteActionPredictorTable::Rows rows_to_update; | |
| 181 rows_to_update.push_back(row0); | |
| 182 rows_to_update.push_back(row2); | |
| 183 db_->autocomplete_table()->AddAndUpdateRows( | |
| 184 AutocompleteActionPredictorTable::Rows(), | |
| 185 rows_to_update); | |
| 186 EXPECT_EQ(3U, CountRecords()); | |
| 187 | |
| 188 AutocompleteActionPredictorTable::Row updated_row0, updated_row2; | |
| 189 db_->autocomplete_table()->GetRow(test_db_[0].id, &updated_row0); | |
| 190 db_->autocomplete_table()->GetRow(test_db_[2].id, &updated_row2); | |
| 191 EXPECT_TRUE(RowsAreEqual(row0, updated_row0)) | |
| 192 << "Expected: Row with id " << row0.id << "\n" | |
| 193 << "Got: Row with id " << updated_row0.id; | |
| 194 EXPECT_TRUE(RowsAreEqual(row2, updated_row2)) | |
| 195 << "Expected: Row with id " << row2.id << "\n" | |
| 196 << "Got: Row with id " << updated_row2.id; | |
| 197 } | |
| 198 | |
| 199 void AutocompleteActionPredictorTableTest::TestUpdateRow() { | |
|
dominich
2012/05/08 20:35:28
remove this test.
Shishir
2012/05/08 21:49:03
Done.
| |
| 200 /* | |
| 201 AddAll(); | |
| 202 AutocompleteActionPredictorTable::Row row = test_db_[1]; | |
| 203 row.number_of_hits = row.number_of_hits + 1; | |
| 204 db_->autocomplete_table()->UpdateRow(row); | |
| 205 | |
| 206 AutocompleteActionPredictorTable::Row updated_row; | |
| 207 db_->autocomplete_table()->GetRow(test_db_[1].id, &updated_row); | |
| 208 | |
| 209 EXPECT_TRUE(RowsAreEqual(row, updated_row)) | |
| 210 << "Expected: Row with id " << row.id << "\n" | |
| 211 << "Got: Row with id " << updated_row.id; | |
| 212 */ | |
| 213 } | |
| 214 | |
| 215 void AutocompleteActionPredictorTableTest::TestDeleteRows() { | |
| 216 AddAll(); | |
| 217 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; | |
| 218 id_list.push_back(test_db_[0].id); | |
| 219 id_list.push_back(test_db_[2].id); | |
| 220 db_->autocomplete_table()->DeleteRows(id_list); | |
| 221 EXPECT_EQ(test_db_.size() - 2, CountRecords()); | |
| 222 | |
| 223 AutocompleteActionPredictorTable::Row row; | |
| 224 db_->autocomplete_table()->GetRow(test_db_[1].id, &row); | |
| 225 EXPECT_TRUE(RowsAreEqual(test_db_[1], row)); | |
| 226 } | |
| 227 | |
| 228 void AutocompleteActionPredictorTableTest::TestDeleteAllRows() { | |
| 229 AddAll(); | |
| 230 db_->autocomplete_table()->DeleteAllRows(); | |
| 231 EXPECT_EQ(0U, CountRecords()); | |
| 232 } | |
| 233 | |
| 234 // AutocompleteActionPredictorTableTest tests | |
| 235 TEST_F(AutocompleteActionPredictorTableTest, GetRow) { | |
| 236 TestGetRow(); | |
| 237 } | |
| 238 | |
| 239 TEST_F(AutocompleteActionPredictorTableTest, AddRow) { | |
| 240 TestAddRow(); | |
| 241 } | |
| 242 | |
| 243 TEST_F(AutocompleteActionPredictorTableTest, UpdateRow) { | |
| 244 TestUpdateRow(); | |
| 245 } | |
| 246 | |
| 247 TEST_F(AutocompleteActionPredictorTableTest, AddAndUpdateRows) { | |
| 248 TestAddAndUpdateRows(); | |
| 249 } | |
| 250 | |
| 251 TEST_F(AutocompleteActionPredictorTableTest, DeleteRows) { | |
| 252 TestDeleteRows(); | |
| 253 } | |
| 254 | |
| 255 TEST_F(AutocompleteActionPredictorTableTest, DeleteAllRows) { | |
| 256 TestDeleteAllRows(); | |
| 257 } | |
| 258 | |
| 259 // AutocompleteActionPredictorTableReopenTest tests | |
| 260 TEST_F(AutocompleteActionPredictorTableReopenTest, GetRow) { | |
| 261 TestGetRow(); | |
| 262 } | |
| 263 | |
| 264 TEST_F(AutocompleteActionPredictorTableReopenTest, AddRow) { | |
| 265 TestAddRow(); | |
| 266 } | |
| 267 | |
| 268 TEST_F(AutocompleteActionPredictorTableReopenTest, UpdateRow) { | |
| 269 TestUpdateRow(); | |
| 270 } | |
| 271 | |
| 272 TEST_F(AutocompleteActionPredictorTableReopenTest, AddAndUpdateRows) { | |
| 273 TestAddAndUpdateRows(); | |
| 274 } | |
| 275 | |
| 276 TEST_F(AutocompleteActionPredictorTableReopenTest, DeleteRows) { | |
| 277 TestDeleteRows(); | |
| 278 } | |
| 279 | |
| 280 TEST_F(AutocompleteActionPredictorTableReopenTest, DeleteAllRows) { | |
| 281 TestDeleteAllRows(); | |
| 282 } | |
| 283 | |
| 284 } // namespace predictors | |
| OLD | NEW |