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