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