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