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 "components/omnibox/browser/scored_history_match.h" | 5 #include "components/omnibox/browser/scored_history_match.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/i18n/break_iterator.h" | 12 #include "base/i18n/break_iterator.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/omnibox/browser/omnibox_field_trial.h" |
16 #include "components/search_engines/search_terms_data.h" | 17 #include "components/search_engines/search_terms_data.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
21 using testing::ElementsAre; | 22 using testing::ElementsAre; |
22 using testing::Pair; | 23 using testing::Pair; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // Find the first word start. | 116 // Find the first word start. |
116 while (iter.Advance() && !iter.IsWord()) { | 117 while (iter.Advance() && !iter.IsWord()) { |
117 } | 118 } |
118 term_word_starts[0] = iter.prev(); | 119 term_word_starts[0] = iter.prev(); |
119 } | 120 } |
120 RowWordStarts row_word_starts; | 121 RowWordStarts row_word_starts; |
121 String16SetFromString16(url, &row_word_starts.url_word_starts_); | 122 String16SetFromString16(url, &row_word_starts.url_word_starts_); |
122 String16SetFromString16(title, &row_word_starts.title_word_starts_); | 123 String16SetFromString16(title, &row_word_starts.title_word_starts_); |
123 ScoredHistoryMatch scored_match(history::URLRow(GURL(url)), VisitInfoVector(), | 124 ScoredHistoryMatch scored_match(history::URLRow(GURL(url)), VisitInfoVector(), |
124 term, term_vector, term_word_starts, | 125 term, term_vector, term_word_starts, |
125 row_word_starts, false, base::Time::Max()); | 126 row_word_starts, false, 1, base::Time::Max()); |
126 scored_match.url_matches = MatchTermInString(term, url, 0); | 127 scored_match.url_matches = MatchTermInString(term, url, 0); |
127 scored_match.title_matches = MatchTermInString(term, title, 0); | 128 scored_match.title_matches = MatchTermInString(term, title, 0); |
128 scored_match.topicality_threshold_ = -1; | 129 scored_match.topicality_threshold_ = -1; |
129 return scored_match.GetTopicalityScore(1, url, term_word_starts, | 130 return scored_match.GetTopicalityScore(1, url, term_word_starts, |
130 row_word_starts); | 131 row_word_starts); |
131 } | 132 } |
132 | 133 |
133 TEST_F(ScoredHistoryMatchTest, Scoring) { | 134 TEST_F(ScoredHistoryMatchTest, Scoring) { |
134 // We use NowFromSystemTime() because MakeURLRow uses the same function | 135 // We use NowFromSystemTime() because MakeURLRow uses the same function |
135 // to calculate last visit time when building a row. | 136 // to calculate last visit time when building a row. |
136 base::Time now = base::Time::NowFromSystemTime(); | 137 base::Time now = base::Time::NowFromSystemTime(); |
137 | 138 |
138 history::URLRow row_a(MakeURLRow("http://fedcba", "abcd bcd", 3, 30, 1)); | 139 history::URLRow row_a(MakeURLRow("http://fedcba", "abcd bcd", 3, 30, 1)); |
139 RowWordStarts word_starts_a; | 140 RowWordStarts word_starts_a; |
140 PopulateWordStarts(row_a, &word_starts_a); | 141 PopulateWordStarts(row_a, &word_starts_a); |
141 WordStarts one_word_no_offset(1, 0u); | 142 WordStarts one_word_no_offset(1, 0u); |
142 VisitInfoVector visits_a = CreateVisitInfoVector(3, 30, now); | 143 VisitInfoVector visits_a = CreateVisitInfoVector(3, 30, now); |
143 // Mark one visit as typed. | 144 // Mark one visit as typed. |
144 visits_a[0].second = ui::PAGE_TRANSITION_TYPED; | 145 visits_a[0].second = ui::PAGE_TRANSITION_TYPED; |
145 ScoredHistoryMatch scored_a(row_a, visits_a, ASCIIToUTF16("abc"), | 146 ScoredHistoryMatch scored_a(row_a, visits_a, ASCIIToUTF16("abc"), |
146 Make1Term("abc"), one_word_no_offset, | 147 Make1Term("abc"), one_word_no_offset, |
147 word_starts_a, false, now); | 148 word_starts_a, false, 1, now); |
148 | 149 |
149 // Test scores based on visit_count. | 150 // Test scores based on visit_count. |
150 history::URLRow row_b(MakeURLRow("http://abcdef", "abcd bcd", 10, 30, 1)); | 151 history::URLRow row_b(MakeURLRow("http://abcdef", "abcd bcd", 10, 30, 1)); |
151 RowWordStarts word_starts_b; | 152 RowWordStarts word_starts_b; |
152 PopulateWordStarts(row_b, &word_starts_b); | 153 PopulateWordStarts(row_b, &word_starts_b); |
153 VisitInfoVector visits_b = CreateVisitInfoVector(10, 30, now); | 154 VisitInfoVector visits_b = CreateVisitInfoVector(10, 30, now); |
154 visits_b[0].second = ui::PAGE_TRANSITION_TYPED; | 155 visits_b[0].second = ui::PAGE_TRANSITION_TYPED; |
155 ScoredHistoryMatch scored_b(row_b, visits_b, ASCIIToUTF16("abc"), | 156 ScoredHistoryMatch scored_b(row_b, visits_b, ASCIIToUTF16("abc"), |
156 Make1Term("abc"), one_word_no_offset, | 157 Make1Term("abc"), one_word_no_offset, |
157 word_starts_b, false, now); | 158 word_starts_b, false, 1, now); |
158 EXPECT_GT(scored_b.raw_score, scored_a.raw_score); | 159 EXPECT_GT(scored_b.raw_score, scored_a.raw_score); |
159 | 160 |
160 // Test scores based on last_visit. | 161 // Test scores based on last_visit. |
161 history::URLRow row_c(MakeURLRow("http://abcdef", "abcd bcd", 3, 10, 1)); | 162 history::URLRow row_c(MakeURLRow("http://abcdef", "abcd bcd", 3, 10, 1)); |
162 RowWordStarts word_starts_c; | 163 RowWordStarts word_starts_c; |
163 PopulateWordStarts(row_c, &word_starts_c); | 164 PopulateWordStarts(row_c, &word_starts_c); |
164 VisitInfoVector visits_c = CreateVisitInfoVector(3, 10, now); | 165 VisitInfoVector visits_c = CreateVisitInfoVector(3, 10, now); |
165 visits_c[0].second = ui::PAGE_TRANSITION_TYPED; | 166 visits_c[0].second = ui::PAGE_TRANSITION_TYPED; |
166 ScoredHistoryMatch scored_c(row_c, visits_c, ASCIIToUTF16("abc"), | 167 ScoredHistoryMatch scored_c(row_c, visits_c, ASCIIToUTF16("abc"), |
167 Make1Term("abc"), one_word_no_offset, | 168 Make1Term("abc"), one_word_no_offset, |
168 word_starts_c, false, now); | 169 word_starts_c, false, 1, now); |
169 EXPECT_GT(scored_c.raw_score, scored_a.raw_score); | 170 EXPECT_GT(scored_c.raw_score, scored_a.raw_score); |
170 | 171 |
171 // Test scores based on typed_count. | 172 // Test scores based on typed_count. |
172 history::URLRow row_d(MakeURLRow("http://abcdef", "abcd bcd", 3, 30, 3)); | 173 history::URLRow row_d(MakeURLRow("http://abcdef", "abcd bcd", 3, 30, 3)); |
173 RowWordStarts word_starts_d; | 174 RowWordStarts word_starts_d; |
174 PopulateWordStarts(row_d, &word_starts_d); | 175 PopulateWordStarts(row_d, &word_starts_d); |
175 VisitInfoVector visits_d = CreateVisitInfoVector(3, 30, now); | 176 VisitInfoVector visits_d = CreateVisitInfoVector(3, 30, now); |
176 visits_d[0].second = ui::PAGE_TRANSITION_TYPED; | 177 visits_d[0].second = ui::PAGE_TRANSITION_TYPED; |
177 visits_d[1].second = ui::PAGE_TRANSITION_TYPED; | 178 visits_d[1].second = ui::PAGE_TRANSITION_TYPED; |
178 visits_d[2].second = ui::PAGE_TRANSITION_TYPED; | 179 visits_d[2].second = ui::PAGE_TRANSITION_TYPED; |
179 ScoredHistoryMatch scored_d(row_d, visits_d, ASCIIToUTF16("abc"), | 180 ScoredHistoryMatch scored_d(row_d, visits_d, ASCIIToUTF16("abc"), |
180 Make1Term("abc"), one_word_no_offset, | 181 Make1Term("abc"), one_word_no_offset, |
181 word_starts_d, false, now); | 182 word_starts_d, false, 1, now); |
182 EXPECT_GT(scored_d.raw_score, scored_a.raw_score); | 183 EXPECT_GT(scored_d.raw_score, scored_a.raw_score); |
183 | 184 |
184 // Test scores based on a terms appearing multiple times. | 185 // Test scores based on a terms appearing multiple times. |
185 history::URLRow row_e(MakeURLRow( | 186 history::URLRow row_e(MakeURLRow( |
186 "http://csi.csi.csi/csi_csi", | 187 "http://csi.csi.csi/csi_csi", |
187 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 3)); | 188 "CSI Guide to CSI Las Vegas, CSI New York, CSI Provo", 3, 30, 3)); |
188 RowWordStarts word_starts_e; | 189 RowWordStarts word_starts_e; |
189 PopulateWordStarts(row_e, &word_starts_e); | 190 PopulateWordStarts(row_e, &word_starts_e); |
190 const VisitInfoVector visits_e = visits_d; | 191 const VisitInfoVector visits_e = visits_d; |
191 ScoredHistoryMatch scored_e(row_e, visits_e, ASCIIToUTF16("csi"), | 192 ScoredHistoryMatch scored_e(row_e, visits_e, ASCIIToUTF16("csi"), |
192 Make1Term("csi"), one_word_no_offset, | 193 Make1Term("csi"), one_word_no_offset, |
193 word_starts_e, false, now); | 194 word_starts_e, false, 1, now); |
194 EXPECT_LT(scored_e.raw_score, 1400); | 195 EXPECT_LT(scored_e.raw_score, 1400); |
195 | 196 |
196 // Test that a result with only a mid-term match (i.e., not at a word | 197 // Test that a result with only a mid-term match (i.e., not at a word |
197 // boundary) scores 0. | 198 // boundary) scores 0. |
198 ScoredHistoryMatch scored_f(row_a, visits_a, ASCIIToUTF16("cd"), | 199 ScoredHistoryMatch scored_f(row_a, visits_a, ASCIIToUTF16("cd"), |
199 Make1Term("cd"), one_word_no_offset, | 200 Make1Term("cd"), one_word_no_offset, |
200 word_starts_a, false, now); | 201 word_starts_a, false, 1, now); |
201 EXPECT_EQ(scored_f.raw_score, 0); | 202 EXPECT_EQ(scored_f.raw_score, 0); |
202 } | 203 } |
203 | 204 |
204 TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { | 205 TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { |
205 // We use NowFromSystemTime() because MakeURLRow uses the same function | 206 // We use NowFromSystemTime() because MakeURLRow uses the same function |
206 // to calculate last visit time when building a row. | 207 // to calculate last visit time when building a row. |
207 base::Time now = base::Time::NowFromSystemTime(); | 208 base::Time now = base::Time::NowFromSystemTime(); |
208 | 209 |
209 std::string url_string("http://fedcba"); | 210 std::string url_string("http://fedcba"); |
210 const GURL url(url_string); | 211 const GURL url(url_string); |
211 history::URLRow row(MakeURLRow(url_string.c_str(), "abcd bcd", 8, 3, 1)); | 212 history::URLRow row(MakeURLRow(url_string.c_str(), "abcd bcd", 8, 3, 1)); |
212 RowWordStarts word_starts; | 213 RowWordStarts word_starts; |
213 PopulateWordStarts(row, &word_starts); | 214 PopulateWordStarts(row, &word_starts); |
214 WordStarts one_word_no_offset(1, 0u); | 215 WordStarts one_word_no_offset(1, 0u); |
215 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 216 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
216 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("abc"), Make1Term("abc"), | 217 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("abc"), Make1Term("abc"), |
217 one_word_no_offset, word_starts, false, now); | 218 one_word_no_offset, word_starts, false, 1, now); |
218 // Now check that if URL is bookmarked then its score increases. | 219 // Now check that if URL is bookmarked then its score increases. |
219 base::AutoReset<float> reset(&ScoredHistoryMatch::bookmark_value_, 5); | 220 base::AutoReset<float> reset(&ScoredHistoryMatch::bookmark_value_, 5); |
220 ScoredHistoryMatch scored_with_bookmark(row, visits, ASCIIToUTF16("abc"), | 221 ScoredHistoryMatch scored_with_bookmark(row, visits, ASCIIToUTF16("abc"), |
221 Make1Term("abc"), one_word_no_offset, | 222 Make1Term("abc"), one_word_no_offset, |
222 word_starts, true, now); | 223 word_starts, true, 1, now); |
223 EXPECT_GT(scored_with_bookmark.raw_score, scored.raw_score); | 224 EXPECT_GT(scored_with_bookmark.raw_score, scored.raw_score); |
224 } | 225 } |
225 | 226 |
226 TEST_F(ScoredHistoryMatchTest, ScoringTLD) { | 227 TEST_F(ScoredHistoryMatchTest, ScoringTLD) { |
227 // We use NowFromSystemTime() because MakeURLRow uses the same function | 228 // We use NowFromSystemTime() because MakeURLRow uses the same function |
228 // to calculate last visit time when building a row. | 229 // to calculate last visit time when building a row. |
229 base::Time now = base::Time::NowFromSystemTime(); | 230 base::Time now = base::Time::NowFromSystemTime(); |
230 | 231 |
231 // By default the URL should not be returned for a query that includes "com". | 232 // By default the URL should not be returned for a query that includes "com". |
232 std::string url_string("http://fedcba.com/"); | 233 std::string url_string("http://fedcba.com/"); |
233 const GURL url(url_string); | 234 const GURL url(url_string); |
234 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); | 235 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); |
235 RowWordStarts word_starts; | 236 RowWordStarts word_starts; |
236 PopulateWordStarts(row, &word_starts); | 237 PopulateWordStarts(row, &word_starts); |
237 WordStarts two_words_no_offsets(2, 0u); | 238 WordStarts two_words_no_offsets(2, 0u); |
238 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 239 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
239 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("fed com"), | 240 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("fed com"), |
240 Make2Terms("fed", "com"), two_words_no_offsets, | 241 Make2Terms("fed", "com"), two_words_no_offsets, |
241 word_starts, false, now); | 242 word_starts, false, 1, now); |
242 EXPECT_EQ(0, scored.raw_score); | 243 EXPECT_EQ(0, scored.raw_score); |
243 | 244 |
244 // Now allow credit for the match in the TLD. | 245 // Now allow credit for the match in the TLD. |
245 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_tld_matches_, true); | 246 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_tld_matches_, true); |
246 ScoredHistoryMatch scored_with_tld( | 247 ScoredHistoryMatch scored_with_tld( |
247 row, visits, ASCIIToUTF16("fed com"), Make2Terms("fed", "com"), | 248 row, visits, ASCIIToUTF16("fed com"), Make2Terms("fed", "com"), |
248 two_words_no_offsets, word_starts, false, now); | 249 two_words_no_offsets, word_starts, false, 1, now); |
249 EXPECT_GT(scored_with_tld.raw_score, 0); | 250 EXPECT_GT(scored_with_tld.raw_score, 0); |
250 } | 251 } |
251 | 252 |
252 TEST_F(ScoredHistoryMatchTest, ScoringScheme) { | 253 TEST_F(ScoredHistoryMatchTest, ScoringScheme) { |
253 // We use NowFromSystemTime() because MakeURLRow uses the same function | 254 // We use NowFromSystemTime() because MakeURLRow uses the same function |
254 // to calculate last visit time when building a row. | 255 // to calculate last visit time when building a row. |
255 base::Time now = base::Time::NowFromSystemTime(); | 256 base::Time now = base::Time::NowFromSystemTime(); |
256 | 257 |
257 // By default the URL should not be returned for a query that includes "http". | 258 // By default the URL should not be returned for a query that includes "http". |
258 std::string url_string("http://fedcba/"); | 259 std::string url_string("http://fedcba/"); |
259 const GURL url(url_string); | 260 const GURL url(url_string); |
260 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); | 261 history::URLRow row(MakeURLRow(url_string.c_str(), "", 8, 3, 1)); |
261 RowWordStarts word_starts; | 262 RowWordStarts word_starts; |
262 PopulateWordStarts(row, &word_starts); | 263 PopulateWordStarts(row, &word_starts); |
263 WordStarts two_words_no_offsets(2, 0u); | 264 WordStarts two_words_no_offsets(2, 0u); |
264 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); | 265 VisitInfoVector visits = CreateVisitInfoVector(8, 3, now); |
265 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("fed http"), | 266 ScoredHistoryMatch scored(row, visits, ASCIIToUTF16("fed http"), |
266 Make2Terms("fed", "http"), two_words_no_offsets, | 267 Make2Terms("fed", "http"), two_words_no_offsets, |
267 word_starts, false, now); | 268 word_starts, false, 1, now); |
268 EXPECT_EQ(0, scored.raw_score); | 269 EXPECT_EQ(0, scored.raw_score); |
269 | 270 |
270 // Now allow credit for the match in the scheme. | 271 // Now allow credit for the match in the scheme. |
271 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_scheme_matches_, true); | 272 base::AutoReset<bool> reset(&ScoredHistoryMatch::allow_scheme_matches_, true); |
272 ScoredHistoryMatch scored_with_scheme( | 273 ScoredHistoryMatch scored_with_scheme( |
273 row, visits, ASCIIToUTF16("fed http"), Make2Terms("fed", "http"), | 274 row, visits, ASCIIToUTF16("fed http"), Make2Terms("fed", "http"), |
274 two_words_no_offsets, word_starts, false, now); | 275 two_words_no_offsets, word_starts, false, 1, now); |
275 EXPECT_GT(scored_with_scheme.raw_score, 0); | 276 EXPECT_GT(scored_with_scheme.raw_score, 0); |
276 } | 277 } |
277 | 278 |
278 TEST_F(ScoredHistoryMatchTest, Inlining) { | 279 TEST_F(ScoredHistoryMatchTest, Inlining) { |
279 // We use NowFromSystemTime() because MakeURLRow uses the same function | 280 // We use NowFromSystemTime() because MakeURLRow uses the same function |
280 // to calculate last visit time when building a row. | 281 // to calculate last visit time when building a row. |
281 base::Time now = base::Time::NowFromSystemTime(); | 282 base::Time now = base::Time::NowFromSystemTime(); |
282 RowWordStarts word_starts; | 283 RowWordStarts word_starts; |
283 WordStarts one_word_no_offset(1, 0u); | 284 WordStarts one_word_no_offset(1, 0u); |
284 VisitInfoVector visits; | 285 VisitInfoVector visits; |
285 | 286 |
286 { | 287 { |
287 history::URLRow row( | 288 history::URLRow row( |
288 MakeURLRow("http://www.google.com", "abcdef", 3, 30, 1)); | 289 MakeURLRow("http://www.google.com", "abcdef", 3, 30, 1)); |
289 PopulateWordStarts(row, &word_starts); | 290 PopulateWordStarts(row, &word_starts); |
290 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("g"), Make1Term("g"), | 291 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("g"), Make1Term("g"), |
291 one_word_no_offset, word_starts, false, now); | 292 one_word_no_offset, word_starts, false, 1, now); |
292 EXPECT_FALSE(scored_a.match_in_scheme); | 293 EXPECT_FALSE(scored_a.match_in_scheme); |
293 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("w"), Make1Term("w"), | 294 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("w"), Make1Term("w"), |
294 one_word_no_offset, word_starts, false, now); | 295 one_word_no_offset, word_starts, false, 1, now); |
295 EXPECT_FALSE(scored_b.match_in_scheme); | 296 EXPECT_FALSE(scored_b.match_in_scheme); |
296 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("h"), Make1Term("h"), | 297 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("h"), Make1Term("h"), |
297 one_word_no_offset, word_starts, false, now); | 298 one_word_no_offset, word_starts, false, 1, now); |
298 EXPECT_TRUE(scored_c.match_in_scheme); | 299 EXPECT_TRUE(scored_c.match_in_scheme); |
299 ScoredHistoryMatch scored_d(row, visits, ASCIIToUTF16("o"), Make1Term("o"), | 300 ScoredHistoryMatch scored_d(row, visits, ASCIIToUTF16("o"), Make1Term("o"), |
300 one_word_no_offset, word_starts, false, now); | 301 one_word_no_offset, word_starts, false, 1, now); |
301 EXPECT_FALSE(scored_d.match_in_scheme); | 302 EXPECT_FALSE(scored_d.match_in_scheme); |
302 } | 303 } |
303 | 304 |
304 { | 305 { |
305 history::URLRow row(MakeURLRow("http://teams.foo.com", "abcdef", 3, 30, 1)); | 306 history::URLRow row(MakeURLRow("http://teams.foo.com", "abcdef", 3, 30, 1)); |
306 PopulateWordStarts(row, &word_starts); | 307 PopulateWordStarts(row, &word_starts); |
307 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("t"), Make1Term("t"), | 308 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("t"), Make1Term("t"), |
308 one_word_no_offset, word_starts, false, now); | 309 one_word_no_offset, word_starts, false, 1, now); |
309 EXPECT_FALSE(scored_a.match_in_scheme); | 310 EXPECT_FALSE(scored_a.match_in_scheme); |
310 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("f"), Make1Term("f"), | 311 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("f"), Make1Term("f"), |
311 one_word_no_offset, word_starts, false, now); | 312 one_word_no_offset, word_starts, false, 1, now); |
312 EXPECT_FALSE(scored_b.match_in_scheme); | 313 EXPECT_FALSE(scored_b.match_in_scheme); |
313 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("o"), Make1Term("o"), | 314 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("o"), Make1Term("o"), |
314 one_word_no_offset, word_starts, false, now); | 315 one_word_no_offset, word_starts, false, 1, now); |
315 EXPECT_FALSE(scored_c.match_in_scheme); | 316 EXPECT_FALSE(scored_c.match_in_scheme); |
316 } | 317 } |
317 | 318 |
318 { | 319 { |
319 history::URLRow row( | 320 history::URLRow row( |
320 MakeURLRow("https://www.testing.com", "abcdef", 3, 30, 1)); | 321 MakeURLRow("https://www.testing.com", "abcdef", 3, 30, 1)); |
321 PopulateWordStarts(row, &word_starts); | 322 PopulateWordStarts(row, &word_starts); |
322 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("t"), Make1Term("t"), | 323 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("t"), Make1Term("t"), |
323 one_word_no_offset, word_starts, false, now); | 324 one_word_no_offset, word_starts, false, 1, now); |
324 EXPECT_FALSE(scored_a.match_in_scheme); | 325 EXPECT_FALSE(scored_a.match_in_scheme); |
325 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("h"), Make1Term("h"), | 326 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("h"), Make1Term("h"), |
326 one_word_no_offset, word_starts, false, now); | 327 one_word_no_offset, word_starts, false, 1, now); |
327 EXPECT_TRUE(scored_b.match_in_scheme); | 328 EXPECT_TRUE(scored_b.match_in_scheme); |
328 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("w"), Make1Term("w"), | 329 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("w"), Make1Term("w"), |
329 one_word_no_offset, word_starts, false, now); | 330 one_word_no_offset, word_starts, false, 1, now); |
330 EXPECT_FALSE(scored_c.match_in_scheme); | 331 EXPECT_FALSE(scored_c.match_in_scheme); |
331 } | 332 } |
332 | 333 |
333 { | 334 { |
334 history::URLRow row( | 335 history::URLRow row( |
335 MakeURLRow("http://www.xn--1lq90ic7f1rc.cn/xnblah", "abcd", 3, 30, 1)); | 336 MakeURLRow("http://www.xn--1lq90ic7f1rc.cn/xnblah", "abcd", 3, 30, 1)); |
336 PopulateWordStarts(row, &word_starts); | 337 PopulateWordStarts(row, &word_starts); |
337 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("x"), Make1Term("x"), | 338 ScoredHistoryMatch scored_a(row, visits, ASCIIToUTF16("x"), Make1Term("x"), |
338 one_word_no_offset, word_starts, false, now); | 339 one_word_no_offset, word_starts, false, 1, now); |
339 EXPECT_FALSE(scored_a.match_in_scheme); | 340 EXPECT_FALSE(scored_a.match_in_scheme); |
340 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("xn"), | 341 ScoredHistoryMatch scored_b(row, visits, ASCIIToUTF16("xn"), |
341 Make1Term("xn"), one_word_no_offset, | 342 Make1Term("xn"), one_word_no_offset, |
342 word_starts, false, now); | 343 word_starts, false, 1, now); |
343 EXPECT_FALSE(scored_b.match_in_scheme); | 344 EXPECT_FALSE(scored_b.match_in_scheme); |
344 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("w"), Make1Term("w"), | 345 ScoredHistoryMatch scored_c(row, visits, ASCIIToUTF16("w"), Make1Term("w"), |
345 one_word_no_offset, word_starts, false, now); | 346 one_word_no_offset, word_starts, false, 1, now); |
346 EXPECT_FALSE(scored_c.match_in_scheme); | 347 EXPECT_FALSE(scored_c.match_in_scheme); |
347 } | 348 } |
348 } | 349 } |
349 | 350 |
350 TEST_F(ScoredHistoryMatchTest, GetTopicalityScoreTrailingSlash) { | 351 TEST_F(ScoredHistoryMatchTest, GetTopicalityScoreTrailingSlash) { |
351 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( | 352 const float hostname = GetTopicalityScoreOfTermAgainstURLAndTitle( |
352 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com/"), | 353 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com/"), |
353 ASCIIToUTF16("Non-Matching Title")); | 354 ASCIIToUTF16("Non-Matching Title")); |
354 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( | 355 const float hostname_no_slash = GetTopicalityScoreOfTermAgainstURLAndTitle( |
355 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com"), | 356 ASCIIToUTF16("def"), ASCIIToUTF16("http://abc.def.com"), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 } | 475 } |
475 | 476 |
476 TEST_F(ScoredHistoryMatchTest, GetFrequency) { | 477 TEST_F(ScoredHistoryMatchTest, GetFrequency) { |
477 // Build a fake ScoredHistoryMatch, which we'll then reuse multiple times. | 478 // Build a fake ScoredHistoryMatch, which we'll then reuse multiple times. |
478 history::URLRow row(GURL("http://foo")); | 479 history::URLRow row(GURL("http://foo")); |
479 RowWordStarts row_word_starts; | 480 RowWordStarts row_word_starts; |
480 PopulateWordStarts(row, &row_word_starts); | 481 PopulateWordStarts(row, &row_word_starts); |
481 base::Time now(base::Time::Max()); | 482 base::Time now(base::Time::Max()); |
482 VisitInfoVector visits; | 483 VisitInfoVector visits; |
483 ScoredHistoryMatch match(row, visits, ASCIIToUTF16("foo"), Make1Term("foo"), | 484 ScoredHistoryMatch match(row, visits, ASCIIToUTF16("foo"), Make1Term("foo"), |
484 WordStarts{0}, row_word_starts, false, now); | 485 WordStarts{0}, row_word_starts, false, 1, now); |
485 | 486 |
486 // Record the score for one untyped visit. | 487 // Record the score for one untyped visit. |
487 visits = {{now, ui::PAGE_TRANSITION_LINK}}; | 488 visits = {{now, ui::PAGE_TRANSITION_LINK}}; |
488 const float one_untyped_score = match.GetFrequency(now, false, visits); | 489 const float one_untyped_score = match.GetFrequency(now, false, visits); |
489 | 490 |
490 // The score for one typed visit should be larger. | 491 // The score for one typed visit should be larger. |
491 visits = VisitInfoVector{{now, ui::PAGE_TRANSITION_TYPED}}; | 492 visits = VisitInfoVector{{now, ui::PAGE_TRANSITION_TYPED}}; |
492 const float one_typed_score = match.GetFrequency(now, false, visits); | 493 const float one_typed_score = match.GetFrequency(now, false, visits); |
493 EXPECT_GT(one_typed_score, one_untyped_score); | 494 EXPECT_GT(one_typed_score, one_untyped_score); |
494 | 495 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 EXPECT_EQ(two_visits_score_uses_sum, | 575 EXPECT_EQ(two_visits_score_uses_sum, |
575 match.GetFrequency(now, false, visits)); | 576 match.GetFrequency(now, false, visits)); |
576 | 577 |
577 // Check again with the third visit being typed. | 578 // Check again with the third visit being typed. |
578 visits[2].second = ui::PAGE_TRANSITION_TYPED; | 579 visits[2].second = ui::PAGE_TRANSITION_TYPED; |
579 EXPECT_EQ(two_visits_score_uses_sum, | 580 EXPECT_EQ(two_visits_score_uses_sum, |
580 match.GetFrequency(now, false, visits)); | 581 match.GetFrequency(now, false, visits)); |
581 } | 582 } |
582 } | 583 } |
583 | 584 |
| 585 TEST_F(ScoredHistoryMatchTest, GetDocumentSpecificityScore) { |
| 586 // Build a fake ScoredHistoryMatch, which we'll then reuse multiple times. |
| 587 history::URLRow row(GURL("http://foo")); |
| 588 RowWordStarts row_word_starts; |
| 589 PopulateWordStarts(row, &row_word_starts); |
| 590 base::Time now(base::Time::Max()); |
| 591 VisitInfoVector visits; |
| 592 ScoredHistoryMatch match(row, visits, ASCIIToUTF16("foo"), Make1Term("foo"), |
| 593 WordStarts{0}, row_word_starts, false, 1, now); |
| 594 |
| 595 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(1)); |
| 596 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(5)); |
| 597 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(50)); |
| 598 |
| 599 OmniboxFieldTrial::NumMatchesScores matches_to_specificity; |
| 600 base::AutoReset<OmniboxFieldTrial::NumMatchesScores*> tmp( |
| 601 &ScoredHistoryMatch::matches_to_specificity_override_, |
| 602 &matches_to_specificity); |
| 603 |
| 604 matches_to_specificity = {{1, 3.0}}; |
| 605 EXPECT_EQ(3.0, match.GetDocumentSpecificityScore(1)); |
| 606 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(5)); |
| 607 |
| 608 matches_to_specificity = {{1, 3.0}, {3, 1.5}}; |
| 609 EXPECT_EQ(3.0, match.GetDocumentSpecificityScore(1)); |
| 610 EXPECT_EQ(1.5, match.GetDocumentSpecificityScore(2)); |
| 611 EXPECT_EQ(1.5, match.GetDocumentSpecificityScore(3)); |
| 612 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(4)); |
| 613 } |
| 614 |
584 // This function only tests scoring of single terms that match exactly | 615 // This function only tests scoring of single terms that match exactly |
585 // once somewhere in the URL or title. | 616 // once somewhere in the URL or title. |
586 TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) { | 617 TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) { |
587 base::string16 url = ASCIIToUTF16( | 618 base::string16 url = ASCIIToUTF16( |
588 "http://abc.def.com/path1/path2?" | 619 "http://abc.def.com/path1/path2?" |
589 "arg1=val1&arg2=val2#hash_component"); | 620 "arg1=val1&arg2=val2#hash_component"); |
590 base::string16 title = ASCIIToUTF16("here is a title"); | 621 base::string16 title = ASCIIToUTF16("here is a title"); |
591 auto Score = [&](const char* term) { | 622 auto Score = [&](const char* term) { |
592 return GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16(term), url, | 623 return GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16(term), url, |
593 title); | 624 title); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 TEST_F(ScoredHistoryMatchTest, GetFinalRelevancyScore) { | 680 TEST_F(ScoredHistoryMatchTest, GetFinalRelevancyScore) { |
650 // relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; | 681 // relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; |
651 ScoredHistoryMatch::ScoreMaxRelevances relevance_buckets = { | 682 ScoredHistoryMatch::ScoreMaxRelevances relevance_buckets = { |
652 {0.0, 100}, {1.0, 200}, {4.0, 500}, {8.0, 900}, {10.0, 1000}}; | 683 {0.0, 100}, {1.0, 200}, {4.0, 500}, {8.0, 900}, {10.0, 1000}}; |
653 base::AutoReset<ScoredHistoryMatch::ScoreMaxRelevances*> tmp( | 684 base::AutoReset<ScoredHistoryMatch::ScoreMaxRelevances*> tmp( |
654 &ScoredHistoryMatch::relevance_buckets_override_, &relevance_buckets); | 685 &ScoredHistoryMatch::relevance_buckets_override_, &relevance_buckets); |
655 | 686 |
656 // Check when topicality score is zero. | 687 // Check when topicality score is zero. |
657 float topicality_score = 0.0; | 688 float topicality_score = 0.0; |
658 float frequency_score = 10.0; | 689 float frequency_score = 10.0; |
659 // intermediate_score = 0.0 * 10.0 = 0.0. | 690 float specificity_score = 1.0; |
660 EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore(topicality_score, | 691 // intermediate_score = 0.0 * 10.0 * 1.0 = 0.0. |
661 frequency_score)); | 692 EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore( |
| 693 topicality_score, frequency_score, specificity_score)); |
662 | 694 |
663 // Check when intermediate score falls at the border range. | 695 // Check when intermediate score falls at the border range. |
664 topicality_score = 0.4f; | 696 topicality_score = 0.4f; |
665 frequency_score = 10.0f; | 697 frequency_score = 10.0f; |
666 // intermediate_score = 0.5 * 10.0 = 4.0. | 698 // intermediate_score = 0.4 * 10.0 * 1.0 = 4.0. |
667 EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore(topicality_score, | 699 EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore( |
668 frequency_score)); | 700 topicality_score, frequency_score, specificity_score)); |
669 | 701 |
670 // Checking the score that falls into one of the buckets. | 702 // Checking the score that falls into one of the buckets. |
671 topicality_score = 0.5f; | 703 topicality_score = 0.5f; |
672 frequency_score = 10.0f; | 704 frequency_score = 10.0f; |
673 // intermediate_score = 0.5 * 10.0 = 5.0. | 705 // intermediate_score = 0.5 * 10.0 * 1.0 = 5.0. |
674 EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. | 706 EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. |
675 ScoredHistoryMatch::GetFinalRelevancyScore(topicality_score, | 707 ScoredHistoryMatch::GetFinalRelevancyScore( |
676 frequency_score)); | 708 topicality_score, frequency_score, specificity_score)); |
677 | 709 |
678 // Never give the score greater than maximum specified. | 710 // Never give the score greater than maximum specified. |
679 topicality_score = 0.5f; | 711 topicality_score = 0.5f; |
680 frequency_score = 22.0f; | 712 frequency_score = 22.0f; |
681 // intermediate_score = 0.5 * 22.0 = 11.0 | 713 // intermediate_score = 0.5 * 22.0 * 1.0 = 11.0 |
682 EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore(topicality_score, | 714 EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore( |
683 frequency_score)); | 715 topicality_score, frequency_score, specificity_score)); |
684 } | 716 } |
685 | 717 |
686 // Test the function GetHQPBucketsFromString(). | 718 // Test the function GetHQPBucketsFromString(). |
687 TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) { | 719 TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) { |
688 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; | 720 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; |
689 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets = | 721 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets = |
690 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str); | 722 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str); |
691 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600), | 723 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600), |
692 Pair(12.0, 1300), Pair(20.0, 1399))); | 724 Pair(12.0, 1300), Pair(20.0, 1399))); |
693 // Test using an invalid string. | 725 // Test using an invalid string. |
694 buckets_str = "0.0,400,1.5,600"; | 726 buckets_str = "0.0,400,1.5,600"; |
695 hqp_buckets = ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str); | 727 hqp_buckets = ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str); |
696 EXPECT_TRUE(hqp_buckets.empty()); | 728 EXPECT_TRUE(hqp_buckets.empty()); |
697 } | 729 } |
OLD | NEW |