Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Side by Side Diff: components/omnibox/browser/scored_history_match_unittest.cc

Issue 2541143002: Omnibox - Boost Frequency Scores Based on Number of Matching Pages (Closed)
Patch Set: fix leak Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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 num_matches_to_document_specificity_score;
Peter Kasting 2016/12/10 02:22:26 Nit: Might want a blank line here since the lack o
Mark P 2016/12/11 05:11:37 Done.
600 {
601 num_matches_to_document_specificity_score = {{1, 3.0}};
602 base::AutoReset<OmniboxFieldTrial::NumMatchesScores*> tmp(
Peter Kasting 2016/12/10 02:22:26 Nit: If you're going to declare the override outsi
Mark P 2016/12/11 05:11:37 Done. (I forgot that I could modify the replaceme
603 &ScoredHistoryMatch::
604 num_matches_to_document_specificity_score_override_,
605 &num_matches_to_document_specificity_score);
606 EXPECT_EQ(3.0, match.GetDocumentSpecificityScore(1));
607 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(5));
608 }
609
610 {
611 num_matches_to_document_specificity_score = {{1, 3.0}, {3, 1.5}};
612 base::AutoReset<OmniboxFieldTrial::NumMatchesScores*> tmp(
613 &ScoredHistoryMatch::
614 num_matches_to_document_specificity_score_override_,
615 &num_matches_to_document_specificity_score);
616 EXPECT_EQ(3.0, match.GetDocumentSpecificityScore(1));
617 EXPECT_EQ(1.5, match.GetDocumentSpecificityScore(2));
618 EXPECT_EQ(1.5, match.GetDocumentSpecificityScore(3));
619 EXPECT_EQ(1.0, match.GetDocumentSpecificityScore(4));
620 }
621 }
622
584 // This function only tests scoring of single terms that match exactly 623 // This function only tests scoring of single terms that match exactly
585 // once somewhere in the URL or title. 624 // once somewhere in the URL or title.
586 TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) { 625 TEST_F(ScoredHistoryMatchTest, GetTopicalityScore) {
587 base::string16 url = ASCIIToUTF16( 626 base::string16 url = ASCIIToUTF16(
588 "http://abc.def.com/path1/path2?" 627 "http://abc.def.com/path1/path2?"
589 "arg1=val1&arg2=val2#hash_component"); 628 "arg1=val1&arg2=val2#hash_component");
590 base::string16 title = ASCIIToUTF16("here is a title"); 629 base::string16 title = ASCIIToUTF16("here is a title");
591 auto Score = [&](const char* term) { 630 auto Score = [&](const char* term) {
592 return GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16(term), url, 631 return GetTopicalityScoreOfTermAgainstURLAndTitle(ASCIIToUTF16(term), url,
593 title); 632 title);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 // hqp_relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000"; 689 // hqp_relevance_buckets = "0.0:100,1.0:200,4.0:500,8.0:900,10.0:1000";
651 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; 690 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets;
652 hqp_buckets.push_back(std::make_pair(0.0, 100)); 691 hqp_buckets.push_back(std::make_pair(0.0, 100));
653 hqp_buckets.push_back(std::make_pair(1.0, 200)); 692 hqp_buckets.push_back(std::make_pair(1.0, 200));
654 hqp_buckets.push_back(std::make_pair(4.0, 500)); 693 hqp_buckets.push_back(std::make_pair(4.0, 500));
655 hqp_buckets.push_back(std::make_pair(8.0, 900)); 694 hqp_buckets.push_back(std::make_pair(8.0, 900));
656 hqp_buckets.push_back(std::make_pair(10.0, 1000)); 695 hqp_buckets.push_back(std::make_pair(10.0, 1000));
657 // Check when topicality score is zero. 696 // Check when topicality score is zero.
658 float topicality_score = 0.0; 697 float topicality_score = 0.0;
659 float frequency_score = 10.0; 698 float frequency_score = 10.0;
660 // intermediate_score = 0.0 * 10.0 = 0.0. 699 float specificity_score = 1.0;
700 // intermediate_score = 0.0 * 10.0 * 1.0 = 0.0.
661 EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore( 701 EXPECT_EQ(0, ScoredHistoryMatch::GetFinalRelevancyScore(
662 topicality_score, frequency_score, hqp_buckets)); 702 topicality_score, frequency_score, specificity_score,
703 hqp_buckets));
663 704
664 // Check when intermediate score falls at the border range. 705 // Check when intermediate score falls at the border range.
665 topicality_score = 0.4f; 706 topicality_score = 0.4f;
666 frequency_score = 10.0f; 707 frequency_score = 10.0f;
667 // intermediate_score = 0.5 * 10.0 = 4.0. 708 // intermediate_score = 0.4 * 10.0 * 1.0 = 4.0.
668 EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore( 709 EXPECT_EQ(500, ScoredHistoryMatch::GetFinalRelevancyScore(
669 topicality_score, frequency_score, hqp_buckets)); 710 topicality_score, frequency_score, specificity_score,
711 hqp_buckets));
670 712
671 // Checking the score that falls into one of the buckets. 713 // Checking the score that falls into one of the buckets.
672 topicality_score = 0.5f; 714 topicality_score = 0.5f;
673 frequency_score = 10.0f; 715 frequency_score = 10.0f;
674 // intermediate_score = 0.5 * 10.0 = 5.0. 716 // intermediate_score = 0.5 * 10.0 * 1.0 = 5.0.
675 EXPECT_EQ(600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600. 717 EXPECT_EQ(
676 ScoredHistoryMatch::GetFinalRelevancyScore( 718 600, // 500 + (((900 - 500)/(8 -4)) * 1) = 600.
677 topicality_score, frequency_score, hqp_buckets)); 719 ScoredHistoryMatch::GetFinalRelevancyScore(
720 topicality_score, frequency_score, specificity_score, hqp_buckets));
678 721
679 // Never give the score greater than maximum specified. 722 // Never give the score greater than maximum specified.
680 topicality_score = 0.5f; 723 topicality_score = 0.5f;
681 frequency_score = 22.0f; 724 frequency_score = 22.0f;
682 // intermediate_score = 0.5 * 22.0 = 11.0 725 // intermediate_score = 0.5 * 22.0 * 1.0 = 11.0
683 EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore( 726 EXPECT_EQ(1000, ScoredHistoryMatch::GetFinalRelevancyScore(
684 topicality_score, frequency_score, hqp_buckets)); 727 topicality_score, frequency_score, specificity_score,
728 hqp_buckets));
685 } 729 }
686 730
687 // Test the function GetHQPBucketsFromString(). 731 // Test the function GetHQPBucketsFromString().
688 TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) { 732 TEST_F(ScoredHistoryMatchTest, GetHQPBucketsFromString) {
689 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399"; 733 std::string buckets_str = "0.0:400,1.5:600,12.0:1300,20.0:1399";
690 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets; 734 std::vector<ScoredHistoryMatch::ScoreMaxRelevance> hqp_buckets;
691 735
692 EXPECT_TRUE( 736 EXPECT_TRUE(
693 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); 737 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets));
694 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600), 738 EXPECT_THAT(hqp_buckets, ElementsAre(Pair(0.0, 400), Pair(1.5, 600),
695 Pair(12.0, 1300), Pair(20.0, 1399))); 739 Pair(12.0, 1300), Pair(20.0, 1399)));
696 // invalid string. 740 // invalid string.
697 buckets_str = "0.0,400,1.5,600"; 741 buckets_str = "0.0,400,1.5,600";
698 EXPECT_FALSE( 742 EXPECT_FALSE(
699 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets)); 743 ScoredHistoryMatch::GetHQPBucketsFromString(buckets_str, &hqp_buckets));
700 } 744 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698