OLD | NEW |
1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/network_action_predictor.h" | 5 #include "chrome/browser/predictors/autocomplete_action_predictor.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/autocomplete_match.h" | 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
14 #include "chrome/browser/history/history.h" | 14 #include "chrome/browser/history/history.h" |
15 #include "chrome/browser/history/in_memory_database.h" | 15 #include "chrome/browser/history/in_memory_database.h" |
16 #include "chrome/browser/history/url_database.h" | 16 #include "chrome/browser/history/url_database.h" |
17 #include "chrome/browser/prerender/prerender_field_trial.h" | 17 #include "chrome/browser/prerender/prerender_field_trial.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/guid.h" | 19 #include "chrome/common/guid.h" |
20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
21 #include "content/test/test_browser_thread.h" | 21 #include "content/test/test_browser_thread.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 using predictors::AutocompleteActionPredictor; |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 struct TestUrlInfo { | 29 struct TestUrlInfo { |
29 GURL url; | 30 GURL url; |
30 string16 title; | 31 string16 title; |
31 int days_from_now; | 32 int days_from_now; |
32 string16 user_text; | 33 string16 user_text; |
33 int number_of_hits; | 34 int number_of_hits; |
34 int number_of_misses; | 35 int number_of_misses; |
35 NetworkActionPredictor::Action expected_action; | 36 AutocompleteActionPredictor::Action expected_action; |
36 } test_url_db[] = { | 37 } test_url_db[] = { |
37 { GURL("http://www.testsite.com/a.html"), | 38 { GURL("http://www.testsite.com/a.html"), |
38 ASCIIToUTF16("Test - site - just a test"), 1, | 39 ASCIIToUTF16("Test - site - just a test"), 1, |
39 ASCIIToUTF16("j"), 5, 0, | 40 ASCIIToUTF16("j"), 5, 0, |
40 NetworkActionPredictor::ACTION_PRERENDER }, | 41 AutocompleteActionPredictor::ACTION_PRERENDER }, |
41 { GURL("http://www.testsite.com/b.html"), | 42 { GURL("http://www.testsite.com/b.html"), |
42 ASCIIToUTF16("Test - site - just a test"), 1, | 43 ASCIIToUTF16("Test - site - just a test"), 1, |
43 ASCIIToUTF16("ju"), 3, 0, | 44 ASCIIToUTF16("ju"), 3, 0, |
44 NetworkActionPredictor::ACTION_PRERENDER }, | 45 AutocompleteActionPredictor::ACTION_PRERENDER }, |
45 { GURL("http://www.testsite.com/c.html"), | 46 { GURL("http://www.testsite.com/c.html"), |
46 ASCIIToUTF16("Test - site - just a test"), 5, | 47 ASCIIToUTF16("Test - site - just a test"), 5, |
47 ASCIIToUTF16("just"), 3, 1, | 48 ASCIIToUTF16("just"), 3, 1, |
48 NetworkActionPredictor::ACTION_PRECONNECT }, | 49 AutocompleteActionPredictor::ACTION_PRECONNECT }, |
49 { GURL("http://www.testsite.com/d.html"), | 50 { GURL("http://www.testsite.com/d.html"), |
50 ASCIIToUTF16("Test - site - just a test"), 5, | 51 ASCIIToUTF16("Test - site - just a test"), 5, |
51 ASCIIToUTF16("just"), 3, 0, | 52 ASCIIToUTF16("just"), 3, 0, |
52 NetworkActionPredictor::ACTION_PRERENDER }, | 53 AutocompleteActionPredictor::ACTION_PRERENDER }, |
53 { GURL("http://www.testsite.com/e.html"), | 54 { GURL("http://www.testsite.com/e.html"), |
54 ASCIIToUTF16("Test - site - just a test"), 8, | 55 ASCIIToUTF16("Test - site - just a test"), 8, |
55 ASCIIToUTF16("just"), 3, 1, | 56 ASCIIToUTF16("just"), 3, 1, |
56 NetworkActionPredictor::ACTION_PRECONNECT }, | 57 AutocompleteActionPredictor::ACTION_PRECONNECT }, |
57 { GURL("http://www.testsite.com/f.html"), | 58 { GURL("http://www.testsite.com/f.html"), |
58 ASCIIToUTF16("Test - site - just a test"), 8, | 59 ASCIIToUTF16("Test - site - just a test"), 8, |
59 ASCIIToUTF16("just"), 3, 0, | 60 ASCIIToUTF16("just"), 3, 0, |
60 NetworkActionPredictor::ACTION_PRERENDER }, | 61 AutocompleteActionPredictor::ACTION_PRERENDER }, |
61 { GURL("http://www.testsite.com/g.html"), | 62 { GURL("http://www.testsite.com/g.html"), |
62 ASCIIToUTF16("Test - site - just a test"), 12, | 63 ASCIIToUTF16("Test - site - just a test"), 12, |
63 ASCIIToUTF16(""), 5, 0, | 64 ASCIIToUTF16(""), 5, 0, |
64 NetworkActionPredictor::ACTION_NONE }, | 65 AutocompleteActionPredictor::ACTION_NONE }, |
65 { GURL("http://www.testsite.com/h.html"), | 66 { GURL("http://www.testsite.com/h.html"), |
66 ASCIIToUTF16("Test - site - just a test"), 21, | 67 ASCIIToUTF16("Test - site - just a test"), 21, |
67 ASCIIToUTF16("just a test"), 2, 0, | 68 ASCIIToUTF16("just a test"), 2, 0, |
68 NetworkActionPredictor::ACTION_NONE }, | 69 AutocompleteActionPredictor::ACTION_NONE }, |
69 { GURL("http://www.testsite.com/i.html"), | 70 { GURL("http://www.testsite.com/i.html"), |
70 ASCIIToUTF16("Test - site - just a test"), 28, | 71 ASCIIToUTF16("Test - site - just a test"), 28, |
71 ASCIIToUTF16("just a test"), 2, 0, | 72 ASCIIToUTF16("just a test"), 2, 0, |
72 NetworkActionPredictor::ACTION_NONE } | 73 AutocompleteActionPredictor::ACTION_NONE } |
73 }; | 74 }; |
74 | 75 |
75 } // end namespace | 76 } // end namespace |
76 | 77 |
77 class NetworkActionPredictorTest : public testing::Test { | 78 namespace predictors { |
| 79 |
| 80 class AutocompleteActionPredictorTest : public testing::Test { |
78 public: | 81 public: |
79 NetworkActionPredictorTest() | 82 AutocompleteActionPredictorTest() |
80 : loop_(MessageLoop::TYPE_DEFAULT), | 83 : loop_(MessageLoop::TYPE_DEFAULT), |
81 ui_thread_(BrowserThread::UI, &loop_), | 84 ui_thread_(BrowserThread::UI, &loop_), |
82 db_thread_(BrowserThread::DB, &loop_), | 85 db_thread_(BrowserThread::DB, &loop_), |
83 file_thread_(BrowserThread::FILE, &loop_), | 86 file_thread_(BrowserThread::FILE, &loop_), |
84 predictor_(new NetworkActionPredictor(&profile_)) { | 87 predictor_(new AutocompleteActionPredictor(&profile_)) { |
85 } | 88 } |
86 | 89 |
87 void SetUp() { | 90 void SetUp() { |
88 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 91 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
89 switches::kPrerenderFromOmnibox, | 92 switches::kPrerenderFromOmnibox, |
90 switches::kPrerenderFromOmniboxSwitchValueEnabled); | 93 switches::kPrerenderFromOmniboxSwitchValueEnabled); |
91 | 94 |
92 profile_.CreateHistoryService(true, false); | 95 profile_.CreateHistoryService(true, false); |
93 profile_.BlockUntilHistoryProcessesPendingRequests(); | 96 profile_.BlockUntilHistoryProcessesPendingRequests(); |
94 | 97 |
95 ASSERT_TRUE(predictor_->initialized_); | 98 ASSERT_TRUE(predictor_->initialized_); |
96 ASSERT_TRUE(db_cache()->empty()); | 99 ASSERT_TRUE(db_cache()->empty()); |
97 ASSERT_TRUE(db_id_cache()->empty()); | 100 ASSERT_TRUE(db_id_cache()->empty()); |
98 } | 101 } |
99 | 102 |
100 void TearDown() { | 103 void TearDown() { |
101 profile_.DestroyHistoryService(); | 104 profile_.DestroyHistoryService(); |
102 predictor_->Shutdown(); | 105 predictor_->Shutdown(); |
103 } | 106 } |
104 | 107 |
105 protected: | 108 protected: |
106 typedef NetworkActionPredictor::DBCacheKey DBCacheKey; | 109 typedef AutocompleteActionPredictor::DBCacheKey DBCacheKey; |
107 typedef NetworkActionPredictor::DBCacheValue DBCacheValue; | 110 typedef AutocompleteActionPredictor::DBCacheValue DBCacheValue; |
108 typedef NetworkActionPredictor::DBCacheMap DBCacheMap; | 111 typedef AutocompleteActionPredictor::DBCacheMap DBCacheMap; |
109 typedef NetworkActionPredictor::DBIdCacheMap DBIdCacheMap; | 112 typedef AutocompleteActionPredictor::DBIdCacheMap DBIdCacheMap; |
110 | 113 |
111 void AddAllRowsToHistory() { | 114 void AddAllRowsToHistory() { |
112 for (size_t i = 0; i < arraysize(test_url_db); ++i) | 115 for (size_t i = 0; i < arraysize(test_url_db); ++i) |
113 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); | 116 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); |
114 } | 117 } |
115 | 118 |
116 history::URLID AddRowToHistory(const TestUrlInfo& test_row) { | 119 history::URLID AddRowToHistory(const TestUrlInfo& test_row) { |
117 HistoryService* history = | 120 HistoryService* history = |
118 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 121 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
119 CHECK(history); | 122 CHECK(history); |
120 history::URLDatabase* url_db = history->InMemoryDatabase(); | 123 history::URLDatabase* url_db = history->InMemoryDatabase(); |
121 CHECK(url_db); | 124 CHECK(url_db); |
122 | 125 |
123 const base::Time visit_time = | 126 const base::Time visit_time = |
124 base::Time::Now() - base::TimeDelta::FromDays( | 127 base::Time::Now() - base::TimeDelta::FromDays( |
125 test_row.days_from_now); | 128 test_row.days_from_now); |
126 | 129 |
127 history::URLRow row(test_row.url); | 130 history::URLRow row(test_row.url); |
128 row.set_title(test_row.title); | 131 row.set_title(test_row.title); |
129 row.set_last_visit(visit_time); | 132 row.set_last_visit(visit_time); |
130 | 133 |
131 return url_db->AddURL(row); | 134 return url_db->AddURL(row); |
132 } | 135 } |
133 | 136 |
134 NetworkActionPredictorDatabase::Row CreateRowFromTestUrlInfo( | 137 AutocompleteActionPredictorTable::Row CreateRowFromTestUrlInfo( |
135 const TestUrlInfo& test_row) const { | 138 const TestUrlInfo& test_row) const { |
136 NetworkActionPredictorDatabase::Row row; | 139 AutocompleteActionPredictorTable::Row row; |
137 row.id = guid::GenerateGUID(); | 140 row.id = guid::GenerateGUID(); |
138 row.user_text = test_row.user_text; | 141 row.user_text = test_row.user_text; |
139 row.url = test_row.url; | 142 row.url = test_row.url; |
140 row.number_of_hits = test_row.number_of_hits; | 143 row.number_of_hits = test_row.number_of_hits; |
141 row.number_of_misses = test_row.number_of_misses; | 144 row.number_of_misses = test_row.number_of_misses; |
142 return row; | 145 return row; |
143 } | 146 } |
144 | 147 |
145 void AddAllRows() { | 148 void AddAllRows() { |
146 for (size_t i = 0; i < arraysize(test_url_db); ++i) | 149 for (size_t i = 0; i < arraysize(test_url_db); ++i) |
147 AddRow(test_url_db[i]); | 150 AddRow(test_url_db[i]); |
148 } | 151 } |
149 | 152 |
150 std::string AddRow(const TestUrlInfo& test_row) { | 153 std::string AddRow(const TestUrlInfo& test_row) { |
151 NetworkActionPredictor::DBCacheKey key = { test_row.user_text, | 154 AutocompleteActionPredictor::DBCacheKey key = { test_row.user_text, |
152 test_row.url }; | 155 test_row.url }; |
153 NetworkActionPredictorDatabase::Row row = | 156 AutocompleteActionPredictorTable::Row row = |
154 CreateRowFromTestUrlInfo(test_row); | 157 CreateRowFromTestUrlInfo(test_row); |
155 predictor_->AddRow(key, row); | 158 predictor_->AddRow(key, row); |
156 | 159 |
157 return row.id; | 160 return row.id; |
158 } | 161 } |
159 | 162 |
160 void UpdateRow(NetworkActionPredictor::DBCacheKey key, | 163 void UpdateRow(AutocompleteActionPredictor::DBCacheKey key, |
161 const NetworkActionPredictorDatabase::Row& row) { | 164 const AutocompleteActionPredictorTable::Row& row) { |
162 NetworkActionPredictor::DBCacheMap::iterator it = | 165 AutocompleteActionPredictor::DBCacheMap::iterator it = |
163 db_cache()->find(key); | 166 db_cache()->find(key); |
164 ASSERT_TRUE(it != db_cache()->end()); | 167 ASSERT_TRUE(it != db_cache()->end()); |
165 | 168 |
166 predictor_->UpdateRow(it, row); | 169 predictor_->UpdateRow(it, row); |
167 } | 170 } |
168 | 171 |
169 void DeleteAllRows() { | 172 void DeleteAllRows() { |
170 predictor_->DeleteAllRows(); | 173 predictor_->DeleteAllRows(); |
171 } | 174 } |
172 | 175 |
173 void DeleteRowsWithURLs(const std::set<GURL>& urls) { | 176 void DeleteRowsWithURLs(const std::set<GURL>& urls) { |
174 predictor_->DeleteRowsWithURLs(urls); | 177 predictor_->DeleteRowsWithURLs(urls); |
175 } | 178 } |
176 | 179 |
177 void DeleteOldIdsFromCaches( | 180 void DeleteOldIdsFromCaches( |
178 std::vector<NetworkActionPredictorDatabase::Row::Id>* id_list) { | 181 std::vector<AutocompleteActionPredictorTable::Row::Id>* id_list) { |
179 HistoryService* history_service = | 182 HistoryService* history_service = |
180 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 183 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
181 ASSERT_TRUE(history_service); | 184 ASSERT_TRUE(history_service); |
182 | 185 |
183 history::URLDatabase* url_db = history_service->InMemoryDatabase(); | 186 history::URLDatabase* url_db = history_service->InMemoryDatabase(); |
184 ASSERT_TRUE(url_db); | 187 ASSERT_TRUE(url_db); |
185 | 188 |
186 predictor_->DeleteOldIdsFromCaches(url_db, id_list); | 189 predictor_->DeleteOldIdsFromCaches(url_db, id_list); |
187 } | 190 } |
188 | 191 |
189 NetworkActionPredictor* predictor() { return predictor_.get(); } | 192 AutocompleteActionPredictor* predictor() { return predictor_.get(); } |
190 | 193 |
191 DBCacheMap* db_cache() { return &predictor_->db_cache_; } | 194 DBCacheMap* db_cache() { return &predictor_->db_cache_; } |
192 DBIdCacheMap* db_id_cache() { return &predictor_->db_id_cache_; } | 195 DBIdCacheMap* db_id_cache() { return &predictor_->db_id_cache_; } |
193 | 196 |
194 static int maximum_days_to_keep_entry() { | 197 static int maximum_days_to_keep_entry() { |
195 return NetworkActionPredictor::kMaximumDaysToKeepEntry; | 198 return AutocompleteActionPredictor::kMaximumDaysToKeepEntry; |
196 } | 199 } |
197 | 200 |
198 private: | 201 private: |
199 MessageLoop loop_; | 202 MessageLoop loop_; |
200 content::TestBrowserThread ui_thread_; | 203 content::TestBrowserThread ui_thread_; |
201 content::TestBrowserThread db_thread_; | 204 content::TestBrowserThread db_thread_; |
202 content::TestBrowserThread file_thread_; | 205 content::TestBrowserThread file_thread_; |
203 TestingProfile profile_; | 206 TestingProfile profile_; |
204 scoped_ptr<NetworkActionPredictor> predictor_; | 207 scoped_ptr<AutocompleteActionPredictor> predictor_; |
205 }; | 208 }; |
206 | 209 |
207 | 210 |
208 TEST_F(NetworkActionPredictorTest, AddRow) { | 211 TEST_F(AutocompleteActionPredictorTest, AddRow) { |
209 // Add a test entry to the predictor. | 212 // Add a test entry to the predictor. |
210 std::string guid = AddRow(test_url_db[0]); | 213 std::string guid = AddRow(test_url_db[0]); |
211 | 214 |
212 // Get the data back out of the cache. | 215 // Get the data back out of the cache. |
213 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; | 216 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; |
214 DBCacheMap::const_iterator it = db_cache()->find(key); | 217 DBCacheMap::const_iterator it = db_cache()->find(key); |
215 EXPECT_TRUE(it != db_cache()->end()); | 218 EXPECT_TRUE(it != db_cache()->end()); |
216 | 219 |
217 const DBCacheValue value = { test_url_db[0].number_of_hits, | 220 const DBCacheValue value = { test_url_db[0].number_of_hits, |
218 test_url_db[0].number_of_misses }; | 221 test_url_db[0].number_of_misses }; |
219 EXPECT_EQ(value.number_of_hits, it->second.number_of_hits); | 222 EXPECT_EQ(value.number_of_hits, it->second.number_of_hits); |
220 EXPECT_EQ(value.number_of_misses, it->second.number_of_misses); | 223 EXPECT_EQ(value.number_of_misses, it->second.number_of_misses); |
221 | 224 |
222 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); | 225 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); |
223 EXPECT_TRUE(id_it != db_id_cache()->end()); | 226 EXPECT_TRUE(id_it != db_id_cache()->end()); |
224 EXPECT_EQ(guid, id_it->second); | 227 EXPECT_EQ(guid, id_it->second); |
225 } | 228 } |
226 | 229 |
227 TEST_F(NetworkActionPredictorTest, UpdateRow) { | 230 TEST_F(AutocompleteActionPredictorTest, UpdateRow) { |
228 ASSERT_NO_FATAL_FAILURE(AddAllRows()); | 231 ASSERT_NO_FATAL_FAILURE(AddAllRows()); |
229 | 232 |
230 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); | 233 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); |
231 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); | 234 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); |
232 | 235 |
233 // Get the data back out of the cache. | 236 // Get the data back out of the cache. |
234 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; | 237 const DBCacheKey key = { test_url_db[0].user_text, test_url_db[0].url }; |
235 DBCacheMap::const_iterator it = db_cache()->find(key); | 238 DBCacheMap::const_iterator it = db_cache()->find(key); |
236 EXPECT_TRUE(it != db_cache()->end()); | 239 EXPECT_TRUE(it != db_cache()->end()); |
237 | 240 |
238 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); | 241 DBIdCacheMap::const_iterator id_it = db_id_cache()->find(key); |
239 EXPECT_TRUE(id_it != db_id_cache()->end()); | 242 EXPECT_TRUE(id_it != db_id_cache()->end()); |
240 | 243 |
241 NetworkActionPredictorDatabase::Row update_row; | 244 AutocompleteActionPredictorTable::Row update_row; |
242 update_row.id = id_it->second; | 245 update_row.id = id_it->second; |
243 update_row.user_text = key.user_text; | 246 update_row.user_text = key.user_text; |
244 update_row.url = key.url; | 247 update_row.url = key.url; |
245 update_row.number_of_hits = it->second.number_of_hits + 1; | 248 update_row.number_of_hits = it->second.number_of_hits + 1; |
246 update_row.number_of_misses = it->second.number_of_misses + 2; | 249 update_row.number_of_misses = it->second.number_of_misses + 2; |
247 | 250 |
248 UpdateRow(key, update_row); | 251 UpdateRow(key, update_row); |
249 | 252 |
250 // Get the updated version. | 253 // Get the updated version. |
251 DBCacheMap::const_iterator update_it = db_cache()->find(key); | 254 DBCacheMap::const_iterator update_it = db_cache()->find(key); |
252 EXPECT_TRUE(update_it != db_cache()->end()); | 255 EXPECT_TRUE(update_it != db_cache()->end()); |
253 | 256 |
254 EXPECT_EQ(update_row.number_of_hits, update_it->second.number_of_hits); | 257 EXPECT_EQ(update_row.number_of_hits, update_it->second.number_of_hits); |
255 EXPECT_EQ(update_row.number_of_misses, update_it->second.number_of_misses); | 258 EXPECT_EQ(update_row.number_of_misses, update_it->second.number_of_misses); |
256 | 259 |
257 DBIdCacheMap::const_iterator update_id_it = db_id_cache()->find(key); | 260 DBIdCacheMap::const_iterator update_id_it = db_id_cache()->find(key); |
258 EXPECT_TRUE(update_id_it != db_id_cache()->end()); | 261 EXPECT_TRUE(update_id_it != db_id_cache()->end()); |
259 | 262 |
260 EXPECT_EQ(id_it->second, update_id_it->second); | 263 EXPECT_EQ(id_it->second, update_id_it->second); |
261 } | 264 } |
262 | 265 |
263 TEST_F(NetworkActionPredictorTest, DeleteAllRows) { | 266 TEST_F(AutocompleteActionPredictorTest, DeleteAllRows) { |
264 ASSERT_NO_FATAL_FAILURE(AddAllRows()); | 267 ASSERT_NO_FATAL_FAILURE(AddAllRows()); |
265 | 268 |
266 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); | 269 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); |
267 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); | 270 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); |
268 | 271 |
269 DeleteAllRows(); | 272 DeleteAllRows(); |
270 | 273 |
271 EXPECT_TRUE(db_cache()->empty()); | 274 EXPECT_TRUE(db_cache()->empty()); |
272 EXPECT_TRUE(db_id_cache()->empty()); | 275 EXPECT_TRUE(db_id_cache()->empty()); |
273 } | 276 } |
274 | 277 |
275 TEST_F(NetworkActionPredictorTest, DeleteRowsWithURLs) { | 278 TEST_F(AutocompleteActionPredictorTest, DeleteRowsWithURLs) { |
276 ASSERT_NO_FATAL_FAILURE(AddAllRows()); | 279 ASSERT_NO_FATAL_FAILURE(AddAllRows()); |
277 | 280 |
278 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); | 281 EXPECT_EQ(arraysize(test_url_db), db_cache()->size()); |
279 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); | 282 EXPECT_EQ(arraysize(test_url_db), db_id_cache()->size()); |
280 | 283 |
281 std::set<GURL> urls; | 284 std::set<GURL> urls; |
282 for (size_t i = 0; i < 2; ++i) | 285 for (size_t i = 0; i < 2; ++i) |
283 urls.insert(test_url_db[i].url); | 286 urls.insert(test_url_db[i].url); |
284 | 287 |
285 DeleteRowsWithURLs(urls); | 288 DeleteRowsWithURLs(urls); |
286 | 289 |
287 EXPECT_EQ(arraysize(test_url_db) - 2, db_cache()->size()); | 290 EXPECT_EQ(arraysize(test_url_db) - 2, db_cache()->size()); |
288 EXPECT_EQ(arraysize(test_url_db) - 2, db_id_cache()->size()); | 291 EXPECT_EQ(arraysize(test_url_db) - 2, db_id_cache()->size()); |
289 | 292 |
290 for (size_t i = 0; i < arraysize(test_url_db); ++i) { | 293 for (size_t i = 0; i < arraysize(test_url_db); ++i) { |
291 DBCacheKey key = { test_url_db[i].user_text, test_url_db[i].url }; | 294 DBCacheKey key = { test_url_db[i].user_text, test_url_db[i].url }; |
292 | 295 |
293 bool deleted = (i < 2); | 296 bool deleted = (i < 2); |
294 EXPECT_EQ(deleted, db_cache()->find(key) == db_cache()->end()); | 297 EXPECT_EQ(deleted, db_cache()->find(key) == db_cache()->end()); |
295 EXPECT_EQ(deleted, db_id_cache()->find(key) == db_id_cache()->end()); | 298 EXPECT_EQ(deleted, db_id_cache()->find(key) == db_id_cache()->end()); |
296 } | 299 } |
297 } | 300 } |
298 | 301 |
299 TEST_F(NetworkActionPredictorTest, DeleteOldIdsFromCaches) { | 302 TEST_F(AutocompleteActionPredictorTest, DeleteOldIdsFromCaches) { |
300 std::vector<NetworkActionPredictorDatabase::Row::Id> expected; | 303 std::vector<AutocompleteActionPredictorTable::Row::Id> expected; |
301 std::vector<NetworkActionPredictorDatabase::Row::Id> all_ids; | 304 std::vector<AutocompleteActionPredictorTable::Row::Id> all_ids; |
302 | 305 |
303 for (size_t i = 0; i < arraysize(test_url_db); ++i) { | 306 for (size_t i = 0; i < arraysize(test_url_db); ++i) { |
304 std::string row_id = AddRow(test_url_db[i]); | 307 std::string row_id = AddRow(test_url_db[i]); |
305 all_ids.push_back(row_id); | 308 all_ids.push_back(row_id); |
306 | 309 |
307 bool exclude_url = StartsWithASCII(test_url_db[i].url.path(), "/d", true) || | 310 bool exclude_url = StartsWithASCII(test_url_db[i].url.path(), "/d", true) || |
308 (test_url_db[i].days_from_now > maximum_days_to_keep_entry()); | 311 (test_url_db[i].days_from_now > maximum_days_to_keep_entry()); |
309 | 312 |
310 if (exclude_url) | 313 if (exclude_url) |
311 expected.push_back(row_id); | 314 expected.push_back(row_id); |
312 else | 315 else |
313 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); | 316 ASSERT_TRUE(AddRowToHistory(test_url_db[i])); |
314 } | 317 } |
315 | 318 |
316 std::vector<NetworkActionPredictorDatabase::Row::Id> id_list; | 319 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; |
317 DeleteOldIdsFromCaches(&id_list); | 320 DeleteOldIdsFromCaches(&id_list); |
318 EXPECT_EQ(expected.size(), id_list.size()); | 321 EXPECT_EQ(expected.size(), id_list.size()); |
319 EXPECT_EQ(all_ids.size() - expected.size(), db_cache()->size()); | 322 EXPECT_EQ(all_ids.size() - expected.size(), db_cache()->size()); |
320 EXPECT_EQ(all_ids.size() - expected.size(), db_id_cache()->size()); | 323 EXPECT_EQ(all_ids.size() - expected.size(), db_id_cache()->size()); |
321 | 324 |
322 for (std::vector<NetworkActionPredictorDatabase::Row::Id>::iterator it = | 325 for (std::vector<AutocompleteActionPredictorTable::Row::Id>::iterator it = |
323 all_ids.begin(); | 326 all_ids.begin(); |
324 it != all_ids.end(); ++it) { | 327 it != all_ids.end(); ++it) { |
325 bool in_expected = | 328 bool in_expected = |
326 (std::find(expected.begin(), expected.end(), *it) != expected.end()); | 329 (std::find(expected.begin(), expected.end(), *it) != expected.end()); |
327 bool in_list = | 330 bool in_list = |
328 (std::find(id_list.begin(), id_list.end(), *it) != id_list.end()); | 331 (std::find(id_list.begin(), id_list.end(), *it) != id_list.end()); |
329 EXPECT_EQ(in_expected, in_list); | 332 EXPECT_EQ(in_expected, in_list); |
330 } | 333 } |
331 } | 334 } |
332 | 335 |
333 TEST_F(NetworkActionPredictorTest, RecommendActionURL) { | 336 TEST_F(AutocompleteActionPredictorTest, RecommendActionURL) { |
334 ASSERT_NO_FATAL_FAILURE(AddAllRows()); | 337 ASSERT_NO_FATAL_FAILURE(AddAllRows()); |
335 | 338 |
336 AutocompleteMatch match; | 339 AutocompleteMatch match; |
337 match.type = AutocompleteMatch::HISTORY_URL; | 340 match.type = AutocompleteMatch::HISTORY_URL; |
338 | 341 |
339 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_url_db); ++i) { | 342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_url_db); ++i) { |
340 match.destination_url = GURL(test_url_db[i].url); | 343 match.destination_url = GURL(test_url_db[i].url); |
341 EXPECT_EQ(test_url_db[i].expected_action, | 344 EXPECT_EQ(test_url_db[i].expected_action, |
342 predictor()->RecommendAction(test_url_db[i].user_text, match)) | 345 predictor()->RecommendAction(test_url_db[i].user_text, match)) |
343 << "Unexpected action for " << match.destination_url; | 346 << "Unexpected action for " << match.destination_url; |
344 } | 347 } |
345 } | 348 } |
346 | 349 |
347 TEST_F(NetworkActionPredictorTest, RecommendActionSearch) { | 350 TEST_F(AutocompleteActionPredictorTest, RecommendActionSearch) { |
348 ASSERT_NO_FATAL_FAILURE(AddAllRows()); | 351 ASSERT_NO_FATAL_FAILURE(AddAllRows()); |
349 | 352 |
350 AutocompleteMatch match; | 353 AutocompleteMatch match; |
351 match.type = AutocompleteMatch::SEARCH_WHAT_YOU_TYPED; | 354 match.type = AutocompleteMatch::SEARCH_WHAT_YOU_TYPED; |
352 | 355 |
353 for (size_t i = 0; i < arraysize(test_url_db); ++i) { | 356 for (size_t i = 0; i < arraysize(test_url_db); ++i) { |
354 match.destination_url = GURL(test_url_db[i].url); | 357 match.destination_url = GURL(test_url_db[i].url); |
355 NetworkActionPredictor::Action expected_action = | 358 AutocompleteActionPredictor::Action expected_action = |
356 (test_url_db[i].expected_action == | 359 (test_url_db[i].expected_action == |
357 NetworkActionPredictor::ACTION_PRERENDER) ? | 360 AutocompleteActionPredictor::ACTION_PRERENDER) ? |
358 NetworkActionPredictor::ACTION_PRECONNECT : | 361 AutocompleteActionPredictor::ACTION_PRECONNECT : |
359 test_url_db[i].expected_action; | 362 test_url_db[i].expected_action; |
360 EXPECT_EQ(expected_action, | 363 EXPECT_EQ(expected_action, |
361 predictor()->RecommendAction(test_url_db[i].user_text, match)) | 364 predictor()->RecommendAction(test_url_db[i].user_text, match)) |
362 << "Unexpected action for " << match.destination_url; | 365 << "Unexpected action for " << match.destination_url; |
363 } | 366 } |
364 } | 367 } |
| 368 |
| 369 } // namespace predictors |
OLD | NEW |