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