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