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
3 // found in the LICENSE file.
5 #include "chrome/browser/autocomplete/history_quick_provider.h"
13 #include "base/format_macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
20 #include "chrome/browser/autocomplete/autocomplete_result.h"
21 #include "chrome/browser/autocomplete/history_url_provider.h"
22 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
23 #include "chrome/browser/history/history_backend.h"
24 #include "chrome/browser/history/history_database.h"
25 #include "chrome/browser/history/history_service.h"
26 #include "chrome/browser/history/history_service_factory.h"
27 #include "chrome/browser/history/in_memory_url_index.h"
28 #include "chrome/browser/history/url_database.h"
29 #include "chrome/browser/history/url_index_private_data.h"
30 #include "chrome/browser/search_engines/template_url.h"
31 #include "chrome/browser/search_engines/template_url_service.h"
32 #include "chrome/browser/search_engines/template_url_service_factory.h"
33 #include "chrome/common/pref_names.h"
34 #include "chrome/test/base/testing_browser_process.h"
35 #include "chrome/test/base/testing_profile.h"
36 #include "content/public/test/test_browser_thread.h"
37 #include "sql/transaction.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 using base::ASCIIToUTF16
;
42 using base::TimeDelta
;
44 using content::BrowserThread
;
53 {"http://www.google.com/", "Google", 3, 3, 0},
54 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0},
55 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0},
56 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0},
57 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0},
58 {"http://news.google.com/", "Google News", 1, 1, 0},
59 {"http://foo.com/", "Dir", 200, 100, 0},
60 {"http://foo.com/dir/", "Dir", 2, 1, 10},
61 {"http://foo.com/dir/another/", "Dir", 10, 5, 0},
62 {"http://foo.com/dir/another/again/", "Dir", 5, 1, 0},
63 {"http://foo.com/dir/another/again/myfile.html", "File", 3, 1, 0},
64 {"http://visitedest.com/y/a", "VA", 10, 1, 20},
65 {"http://visitedest.com/y/b", "VB", 9, 1, 20},
66 {"http://visitedest.com/x/c", "VC", 8, 1, 20},
67 {"http://visitedest.com/x/d", "VD", 7, 1, 20},
68 {"http://visitedest.com/y/e", "VE", 6, 1, 20},
69 {"http://typeredest.com/y/a", "TA", 5, 5, 0},
70 {"http://typeredest.com/y/b", "TB", 5, 4, 0},
71 {"http://typeredest.com/x/c", "TC", 5, 3, 0},
72 {"http://typeredest.com/x/d", "TD", 5, 2, 0},
73 {"http://typeredest.com/y/e", "TE", 5, 1, 0},
74 {"http://daysagoest.com/y/a", "DA", 1, 1, 0},
75 {"http://daysagoest.com/y/b", "DB", 1, 1, 1},
76 {"http://daysagoest.com/x/c", "DC", 1, 1, 2},
77 {"http://daysagoest.com/x/d", "DD", 1, 1, 3},
78 {"http://daysagoest.com/y/e", "DE", 1, 1, 4},
79 {"http://abcdefghixyzjklmnopqrstuvw.com/a", "", 3, 1, 0},
80 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0},
81 {"http://abcdefghijklxyzmnopqrstuvw.com/a", "", 3, 1, 0},
82 {"http://abcdefxyzghijklmnopqrstuvw.com/a", "", 3, 1, 0},
83 {"http://abcxyzdefghijklmnopqrstuvw.com/a", "", 3, 1, 0},
84 {"http://xyzabcdefghijklmnopqrstuvw.com/a", "", 3, 1, 0},
85 {"http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice",
86 "Dogs & Cats & Mice & Other Animals", 1, 1, 0},
87 {"https://monkeytrap.org/", "", 3, 1, 0},
88 {"http://popularsitewithpathonly.com/moo",
89 "popularsitewithpathonly.com/moo", 50, 50, 0},
90 {"http://popularsitewithroot.com/", "popularsitewithroot.com", 50, 50, 0},
91 {"http://testsearch.com/?q=thequery", "Test Search Engine", 10, 10, 0},
92 {"http://testsearch.com/", "Test Search Engine", 9, 9, 0},
93 {"http://anotherengine.com/?q=thequery", "Another Search Engine", 8, 8, 0}
96 class HistoryQuickProviderTest
: public testing::Test
,
97 public AutocompleteProviderListener
{
99 HistoryQuickProviderTest()
100 : ui_thread_(BrowserThread::UI
, &message_loop_
),
101 file_thread_(BrowserThread::FILE, &message_loop_
) {}
103 // AutocompleteProviderListener:
104 virtual void OnProviderUpdate(bool updated_matches
) OVERRIDE
{}
107 class SetShouldContain
: public std::unary_function
<const std::string
&,
108 std::set
<std::string
> > {
110 explicit SetShouldContain(const ACMatches
& matched_urls
);
112 void operator()(const std::string
& expected
);
114 std::set
<std::string
> LeftOvers() const { return matches_
; }
117 std::set
<std::string
> matches_
;
120 static BrowserContextKeyedService
* CreateTemplateURLService(
121 content::BrowserContext
* profile
) {
122 return new TemplateURLService(static_cast<Profile
*>(profile
));
125 virtual void SetUp();
126 virtual void TearDown();
128 virtual void GetTestData(size_t* data_count
, TestURLInfo
** test_data
);
130 // Fills test data into the history system.
133 // Runs an autocomplete query on |text| and checks to see that the returned
134 // results' destination URLs match those provided. |expected_urls| does not
135 // need to be in sorted order.
136 void RunTest(const base::string16 text
,
137 std::vector
<std::string
> expected_urls
,
138 bool can_inline_top_result
,
139 base::string16 expected_fill_into_edit
);
141 base::MessageLoopForUI message_loop_
;
142 content::TestBrowserThread ui_thread_
;
143 content::TestBrowserThread file_thread_
;
145 scoped_ptr
<TestingProfile
> profile_
;
146 HistoryService
* history_service_
;
148 ACMatches ac_matches_
; // The resulting matches after running RunTest.
150 scoped_refptr
<HistoryQuickProvider
> provider_
;
153 void HistoryQuickProviderTest::SetUp() {
154 profile_
.reset(new TestingProfile());
155 ASSERT_TRUE(profile_
->CreateHistoryService(true, false));
156 profile_
->CreateBookmarkModel(true);
157 test::WaitForBookmarkModelToLoad(profile_
.get());
158 profile_
->BlockUntilHistoryIndexIsRefreshed();
160 HistoryServiceFactory::GetForProfile(profile_
.get(),
161 Profile::EXPLICIT_ACCESS
);
162 EXPECT_TRUE(history_service_
);
163 provider_
= new HistoryQuickProvider(this, profile_
.get());
164 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
165 profile_
.get(), &HistoryQuickProviderTest::CreateTemplateURLService
);
167 provider_
->GetIndex()->RebuildFromHistory(
168 history_service_
->history_backend_
->db());
171 void HistoryQuickProviderTest::TearDown() {
175 void HistoryQuickProviderTest::GetTestData(size_t* data_count
,
176 TestURLInfo
** test_data
) {
179 *data_count
= arraysize(quick_test_db
);
180 *test_data
= &quick_test_db
[0];
183 void HistoryQuickProviderTest::FillData() {
184 sql::Connection
& db(history_service_
->history_backend_
->db()->GetDB());
185 ASSERT_TRUE(db
.is_open());
187 size_t data_count
= 0;
188 TestURLInfo
* test_data
= NULL
;
189 GetTestData(&data_count
, &test_data
);
191 for (size_t i
= 0; i
< data_count
; ++i
) {
192 const TestURLInfo
& cur(test_data
[i
]);
193 Time visit_time
= Time::Now() - TimeDelta::FromDays(cur
.days_from_now
);
194 sql::Transaction
transaction(&db
);
198 std::string sql_cmd_line
= base::StringPrintf(
199 "INSERT INTO \"urls\" VALUES(%" PRIuS
", \'%s\', \'%s\', %d, %d, %"
201 i
+ 1, cur
.url
.c_str(), cur
.title
.c_str(), cur
.visit_count
,
202 cur
.typed_count
, visit_time
.ToInternalValue());
203 sql::Statement
sql_stmt(db
.GetUniqueStatement(sql_cmd_line
.c_str()));
204 EXPECT_TRUE(sql_stmt
.Run());
205 transaction
.Commit();
208 for (int j
= 0; j
< cur
.visit_count
; ++j
) {
209 // Assume earlier visits are at one-day intervals.
210 visit_time
-= TimeDelta::FromDays(1);
212 // Mark the most recent |cur.typed_count| visits as typed.
213 std::string sql_cmd_line
= base::StringPrintf(
214 "INSERT INTO \"visits\" VALUES(%" PRIuS
", %" PRIuS
", %" PRId64
216 visit_id
++, i
+ 1, visit_time
.ToInternalValue(),
217 (j
< cur
.typed_count
) ? content::PAGE_TRANSITION_TYPED
:
218 content::PAGE_TRANSITION_LINK
);
220 sql::Statement
sql_stmt(db
.GetUniqueStatement(sql_cmd_line
.c_str()));
221 EXPECT_TRUE(sql_stmt
.Run());
222 transaction
.Commit();
227 HistoryQuickProviderTest::SetShouldContain::SetShouldContain(
228 const ACMatches
& matched_urls
) {
229 for (ACMatches::const_iterator iter
= matched_urls
.begin();
230 iter
!= matched_urls
.end(); ++iter
)
231 matches_
.insert(iter
->destination_url
.spec());
234 void HistoryQuickProviderTest::SetShouldContain::operator()(
235 const std::string
& expected
) {
236 EXPECT_EQ(1U, matches_
.erase(expected
))
237 << "Results did not contain '" << expected
<< "' but should have.";
241 void HistoryQuickProviderTest::RunTest(const base::string16 text
,
242 std::vector
<std::string
> expected_urls
,
243 bool can_inline_top_result
,
244 base::string16 expected_fill_into_edit
) {
245 SCOPED_TRACE(text
); // Minimal hint to query being run.
246 base::MessageLoop::current()->RunUntilIdle();
247 AutocompleteInput
input(text
, base::string16::npos
, base::string16(),
248 GURL(), AutocompleteInput::INVALID_SPEC
, false,
249 false, true, AutocompleteInput::ALL_MATCHES
);
250 provider_
->Start(input
, false);
251 EXPECT_TRUE(provider_
->done());
253 ac_matches_
= provider_
->matches();
255 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
256 EXPECT_LE(ac_matches_
.size(), AutocompleteProvider::kMaxMatches
);
258 // If the number of expected and actual matches aren't equal then we need
259 // test no further, but let's do anyway so that we know which URLs failed.
260 EXPECT_EQ(expected_urls
.size(), ac_matches_
.size());
262 // Verify that all expected URLs were found and that all found URLs
264 std::set
<std::string
> leftovers
=
265 for_each(expected_urls
.begin(), expected_urls
.end(),
266 SetShouldContain(ac_matches_
)).LeftOvers();
267 EXPECT_EQ(0U, leftovers
.size()) << "There were " << leftovers
.size()
268 << " unexpected results, one of which was: '"
269 << *(leftovers
.begin()) << "'.";
271 if (expected_urls
.empty())
274 // Verify that we got the results in the order expected.
275 int best_score
= ac_matches_
.begin()->relevance
+ 1;
277 std::vector
<std::string
>::const_iterator expected
= expected_urls
.begin();
278 for (ACMatches::const_iterator actual
= ac_matches_
.begin();
279 actual
!= ac_matches_
.end() && expected
!= expected_urls
.end();
280 ++actual
, ++expected
, ++i
) {
281 EXPECT_EQ(*expected
, actual
->destination_url
.spec())
282 << "For result #" << i
<< " we got '" << actual
->destination_url
.spec()
283 << "' but expected '" << *expected
<< "'.";
284 EXPECT_LT(actual
->relevance
, best_score
)
285 << "At result #" << i
<< " (url=" << actual
->destination_url
.spec()
286 << "), we noticed scores are not monotonically decreasing.";
287 best_score
= actual
->relevance
;
290 EXPECT_EQ(can_inline_top_result
, ac_matches_
[0].allowed_to_be_default_match
);
291 if (can_inline_top_result
) {
292 // When the top scorer is inline-able make sure we get the expected
293 // fill_into_edit and autocomplete offset.
294 EXPECT_EQ(expected_fill_into_edit
, ac_matches_
[0].fill_into_edit
)
295 << "fill_into_edit was: '" << ac_matches_
[0].fill_into_edit
296 << "' but we expected '" << expected_fill_into_edit
<< "'.";
297 size_t text_pos
= expected_fill_into_edit
.find(text
);
298 ASSERT_NE(base::string16::npos
, text_pos
);
299 EXPECT_EQ(ac_matches_
[0].fill_into_edit
.substr(text_pos
+ text
.size()),
300 ac_matches_
[0].inline_autocompletion
);
302 // When the top scorer is not inline-able autocomplete offset must be npos.
303 EXPECT_TRUE(ac_matches_
[0].inline_autocompletion
.empty());
304 // Also, the score must be too low to be inlineable.
305 // TODO(mpearson): when the controller reorders for inlining, there's no
306 // longer any connection between scores and what's inlineable / allowed
307 // to be the default match. Remove this test.
308 EXPECT_LT(ac_matches_
[0].relevance
,
309 AutocompleteResult::kLowestDefaultScore
);
313 TEST_F(HistoryQuickProviderTest
, SimpleSingleMatch
) {
314 std::vector
<std::string
> expected_urls
;
315 expected_urls
.push_back("http://slashdot.org/favorite_page.html");
316 RunTest(ASCIIToUTF16("slashdot"), expected_urls
, true,
317 ASCIIToUTF16("slashdot.org/favorite_page.html"));
320 TEST_F(HistoryQuickProviderTest
, MultiTermTitleMatch
) {
321 std::vector
<std::string
> expected_urls
;
322 expected_urls
.push_back(
323 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
324 RunTest(ASCIIToUTF16("mice other animals"), expected_urls
, false,
325 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
328 TEST_F(HistoryQuickProviderTest
, NonWordLastCharacterMatch
) {
329 std::string
expected_url("http://slashdot.org/favorite_page.html");
330 std::vector
<std::string
> expected_urls
;
331 expected_urls
.push_back(expected_url
);
332 RunTest(ASCIIToUTF16("slashdot.org/"), expected_urls
, true,
333 ASCIIToUTF16("slashdot.org/favorite_page.html"));
336 TEST_F(HistoryQuickProviderTest
, MultiMatch
) {
337 std::vector
<std::string
> expected_urls
;
338 // Scores high because of typed_count.
339 expected_urls
.push_back("http://foo.com/");
340 // Scores high because of visit count.
341 expected_urls
.push_back("http://foo.com/dir/another/");
342 // Scores high because of high visit count.
343 expected_urls
.push_back("http://foo.com/dir/another/again/");
344 RunTest(ASCIIToUTF16("foo"), expected_urls
, true, ASCIIToUTF16("foo.com"));
347 TEST_F(HistoryQuickProviderTest
, StartRelativeMatch
) {
348 std::vector
<std::string
> expected_urls
;
349 expected_urls
.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a");
350 RunTest(ASCIIToUTF16("xyza"), expected_urls
, true,
351 ASCIIToUTF16("xyzabcdefghijklmnopqrstuvw.com/a"));
354 TEST_F(HistoryQuickProviderTest
, EncodingMatch
) {
355 std::vector
<std::string
> expected_urls
;
356 expected_urls
.push_back("http://spaces.com/path%20with%20spaces/foo.html");
357 RunTest(ASCIIToUTF16("path with spaces"), expected_urls
, false,
358 ASCIIToUTF16("CANNOT AUTOCOMPLETE"));
361 TEST_F(HistoryQuickProviderTest
, VisitCountMatches
) {
362 std::vector
<std::string
> expected_urls
;
363 expected_urls
.push_back("http://visitedest.com/y/a");
364 expected_urls
.push_back("http://visitedest.com/y/b");
365 expected_urls
.push_back("http://visitedest.com/x/c");
366 RunTest(ASCIIToUTF16("visitedest"), expected_urls
, true,
367 ASCIIToUTF16("visitedest.com/y/a"));
370 TEST_F(HistoryQuickProviderTest
, TypedCountMatches
) {
371 std::vector
<std::string
> expected_urls
;
372 expected_urls
.push_back("http://typeredest.com/y/a");
373 expected_urls
.push_back("http://typeredest.com/y/b");
374 expected_urls
.push_back("http://typeredest.com/x/c");
375 RunTest(ASCIIToUTF16("typeredest"), expected_urls
, true,
376 ASCIIToUTF16("typeredest.com/y/a"));
379 TEST_F(HistoryQuickProviderTest
, DaysAgoMatches
) {
380 std::vector
<std::string
> expected_urls
;
381 expected_urls
.push_back("http://daysagoest.com/y/a");
382 expected_urls
.push_back("http://daysagoest.com/y/b");
383 expected_urls
.push_back("http://daysagoest.com/x/c");
384 RunTest(ASCIIToUTF16("daysagoest"), expected_urls
, true,
385 ASCIIToUTF16("daysagoest.com/y/a"));
388 TEST_F(HistoryQuickProviderTest
, EncodingLimitMatch
) {
389 std::vector
<std::string
> expected_urls
;
391 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
392 // First check that a mid-word match yield no results.
393 RunTest(ASCIIToUTF16("ice"), expected_urls
, false,
394 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
395 // Then check that we get results when the match is at a word start
396 // that is present because of an encoded separate (%20 = space).
397 expected_urls
.push_back(url
);
398 RunTest(ASCIIToUTF16("Mice"), expected_urls
, false,
399 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
400 // Verify that the matches' ACMatchClassifications offsets are in range.
401 ACMatchClassifications
content(ac_matches_
[0].contents_class
);
402 // The max offset accounts for 6 occurrences of '%20' plus the 'http://'.
403 const size_t max_offset
= url
.length() - ((6 * 2) + 7);
404 for (ACMatchClassifications::const_iterator citer
= content
.begin();
405 citer
!= content
.end(); ++citer
)
406 EXPECT_LT(citer
->offset
, max_offset
);
407 ACMatchClassifications
description(ac_matches_
[0].description_class
);
408 std::string
page_title("Dogs & Cats & Mice & Other Animals");
409 for (ACMatchClassifications::const_iterator diter
= description
.begin();
410 diter
!= description
.end(); ++diter
)
411 EXPECT_LT(diter
->offset
, page_title
.length());
414 TEST_F(HistoryQuickProviderTest
, Spans
) {
415 // Test SpansFromTermMatch
416 history::TermMatches matches_a
;
417 // Simulates matches: '.xx.xxx..xx...xxxxx..' which will test no match at
418 // either beginning or end as well as adjacent matches.
419 matches_a
.push_back(history::TermMatch(1, 1, 2));
420 matches_a
.push_back(history::TermMatch(2, 4, 3));
421 matches_a
.push_back(history::TermMatch(3, 9, 1));
422 matches_a
.push_back(history::TermMatch(3, 10, 1));
423 matches_a
.push_back(history::TermMatch(4, 14, 5));
424 ACMatchClassifications spans_a
=
425 HistoryQuickProvider::SpansFromTermMatch(matches_a
, 20, false);
426 // ACMatch spans should be: 'NM-NM---N-M-N--M----N-'
427 ASSERT_EQ(9U, spans_a
.size());
428 EXPECT_EQ(0U, spans_a
[0].offset
);
429 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[0].style
);
430 EXPECT_EQ(1U, spans_a
[1].offset
);
431 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[1].style
);
432 EXPECT_EQ(3U, spans_a
[2].offset
);
433 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[2].style
);
434 EXPECT_EQ(4U, spans_a
[3].offset
);
435 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[3].style
);
436 EXPECT_EQ(7U, spans_a
[4].offset
);
437 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[4].style
);
438 EXPECT_EQ(9U, spans_a
[5].offset
);
439 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[5].style
);
440 EXPECT_EQ(11U, spans_a
[6].offset
);
441 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[6].style
);
442 EXPECT_EQ(14U, spans_a
[7].offset
);
443 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[7].style
);
444 EXPECT_EQ(19U, spans_a
[8].offset
);
445 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[8].style
);
446 // Simulates matches: 'xx.xx' which will test matches at both beginning and
448 history::TermMatches matches_b
;
449 matches_b
.push_back(history::TermMatch(1, 0, 2));
450 matches_b
.push_back(history::TermMatch(2, 3, 2));
451 ACMatchClassifications spans_b
=
452 HistoryQuickProvider::SpansFromTermMatch(matches_b
, 5, true);
453 // ACMatch spans should be: 'M-NM-'
454 ASSERT_EQ(3U, spans_b
.size());
455 EXPECT_EQ(0U, spans_b
[0].offset
);
456 EXPECT_EQ(ACMatchClassification::MATCH
| ACMatchClassification::URL
,
458 EXPECT_EQ(2U, spans_b
[1].offset
);
459 EXPECT_EQ(ACMatchClassification::URL
, spans_b
[1].style
);
460 EXPECT_EQ(3U, spans_b
[2].offset
);
461 EXPECT_EQ(ACMatchClassification::MATCH
| ACMatchClassification::URL
,
465 TEST_F(HistoryQuickProviderTest
, DeleteMatch
) {
466 std::vector
<std::string
> expected_urls
;
467 expected_urls
.push_back("http://slashdot.org/favorite_page.html");
468 // Fill up ac_matches_; we don't really care about the test yet.
469 RunTest(ASCIIToUTF16("slashdot"), expected_urls
, true,
470 ASCIIToUTF16("slashdot.org/favorite_page.html"));
471 EXPECT_EQ(1U, ac_matches_
.size());
472 provider_
->DeleteMatch(ac_matches_
[0]);
473 // Verify it's no longer an indexed visit.
474 expected_urls
.clear();
475 RunTest(ASCIIToUTF16("slashdot"), expected_urls
, true,
476 ASCIIToUTF16("NONE EXPECTED"));
479 TEST_F(HistoryQuickProviderTest
, PreventBeatingURLWhatYouTypedMatch
) {
480 std::vector
<std::string
> expected_urls
;
482 expected_urls
.clear();
483 expected_urls
.push_back("http://popularsitewithroot.com/");
484 // If the user enters a hostname (no path) that he/she has visited
485 // before, we should make sure that all HistoryQuickProvider results
486 // have scores less than what HistoryURLProvider will assign the
487 // URL-what-you-typed match.
488 RunTest(ASCIIToUTF16("popularsitewithroot.com"), expected_urls
, true,
489 ASCIIToUTF16("popularsitewithroot.com"));
490 EXPECT_LT(ac_matches_
[0].relevance
,
491 HistoryURLProvider::kScoreForBestInlineableResult
);
493 // Check that if the user didn't quite enter the full hostname, this
494 // hostname would've normally scored above the URL-what-you-typed match.
495 RunTest(ASCIIToUTF16("popularsitewithroot.c"), expected_urls
, true,
496 ASCIIToUTF16("popularsitewithroot.com"));
497 EXPECT_GE(ac_matches_
[0].relevance
,
498 HistoryURLProvider::kScoreForWhatYouTypedResult
);
500 expected_urls
.clear();
501 expected_urls
.push_back("http://popularsitewithpathonly.com/moo");
502 // If the user enters a hostname of a host that he/she has visited
503 // but never visited the root page of, we should make sure that all
504 // HistoryQuickProvider results have scores less than what the
505 // HistoryURLProvider will assign the URL-what-you-typed match.
506 RunTest(ASCIIToUTF16("popularsitewithpathonly.com"), expected_urls
, true,
507 ASCIIToUTF16("popularsitewithpathonly.com/moo"));
508 EXPECT_LT(ac_matches_
[0].relevance
,
509 HistoryURLProvider::kScoreForUnvisitedIntranetResult
);
511 // Verify the same thing happens if the user adds a / to end of the
513 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/"), expected_urls
, true,
514 ASCIIToUTF16("popularsitewithpathonly.com/moo"));
515 EXPECT_LT(ac_matches_
[0].relevance
,
516 HistoryURLProvider::kScoreForUnvisitedIntranetResult
);
518 // Check that if the user didn't quite enter the full hostname, this
519 // page would've normally scored above the URL-what-you-typed match.
520 RunTest(ASCIIToUTF16("popularsitewithpathonly.co"), expected_urls
, true,
521 ASCIIToUTF16("popularsitewithpathonly.com/moo"));
522 EXPECT_GE(ac_matches_
[0].relevance
,
523 HistoryURLProvider::kScoreForWhatYouTypedResult
);
525 // If the user enters a hostname + path that he/she has not visited
526 // before (but visited other things on the host), we can allow
527 // inline autocompletions.
528 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/mo"), expected_urls
, true,
529 ASCIIToUTF16("popularsitewithpathonly.com/moo"));
530 EXPECT_GE(ac_matches_
[0].relevance
,
531 HistoryURLProvider::kScoreForWhatYouTypedResult
);
533 // If the user enters a hostname + path that he/she has visited
534 // before, we should make sure that all HistoryQuickProvider results
535 // have scores less than what the HistoryURLProvider will assign
536 // the URL-what-you-typed match.
537 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/moo"),
539 ASCIIToUTF16("popularsitewithpathonly.com/moo"));
540 EXPECT_LT(ac_matches_
[0].relevance
,
541 HistoryURLProvider::kScoreForBestInlineableResult
);
544 TEST_F(HistoryQuickProviderTest
, CullSearchResults
) {
545 // Set up a default search engine.
546 TemplateURLData data
;
547 data
.SetKeyword(ASCIIToUTF16("TestEngine"));
548 data
.SetURL("http://testsearch.com/?q={searchTerms}");
549 TemplateURLService
* template_url_service
=
550 TemplateURLServiceFactory::GetForProfile(profile_
.get());
551 TemplateURL
* template_url
= new TemplateURL(profile_
.get(), data
);
552 template_url_service
->Add(template_url
);
553 template_url_service
->SetDefaultSearchProvider(template_url
);
554 template_url_service
->Load();
556 // A search results page should not be returned when typing a query.
557 std::vector
<std::string
> expected_urls
;
558 expected_urls
.push_back("http://anotherengine.com/?q=thequery");
559 RunTest(ASCIIToUTF16("thequery"), expected_urls
, false, base::string16());
561 // A search results page should not be returned when typing the engine URL.
562 expected_urls
.clear();
563 expected_urls
.push_back("http://testsearch.com/");
564 RunTest(ASCIIToUTF16("testsearch"), expected_urls
, true,
565 ASCIIToUTF16("testsearch.com"));
568 // HQPOrderingTest -------------------------------------------------------------
570 TestURLInfo ordering_test_db
[] = {
571 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3,
573 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, "
574 "apparel, computers, books, dvds & more", 20, 20, 10},
575 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&"
576 "currentpage=83", "google images", 6, 6, 0},
577 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0},
578 {"http://www.teamfortress.com/", "", 5, 5, 6},
579 {"http://www.rottentomatoes.com/", "", 3, 3, 7},
580 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9},
581 {"https://www.emigrantdirect.com/", "high interest savings account, high "
582 "yield savings - emigrantdirect", 5, 5, 3},
583 {"http://store.steampowered.com/", "", 6, 6, 1},
584 {"http://techmeme.com/", "techmeme", 111, 110, 4},
585 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15,
587 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1},
588 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player "
589 "index", 25, 7, 219},
590 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3,
592 {"http://translate.google.com/", "google translate", 3, 3, 0},
593 {"http://arstechnica.com/", "ars technica", 3, 3, 3},
594 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - "
595 "rotten tomatoes", 3, 3, 7},
596 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
597 "gaming news", 26, 25, 3},
598 {"http://metaleater.com/", "metaleater", 4, 3, 8},
599 {"http://half.com/", "half.com: textbooks , books , music , movies , games , "
600 "video games", 4, 4, 6},
601 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
602 "gaming news", 8, 5, 9},
605 class HQPOrderingTest
: public HistoryQuickProviderTest
{
607 virtual void GetTestData(size_t* data_count
,
608 TestURLInfo
** test_data
) OVERRIDE
;
611 void HQPOrderingTest::GetTestData(size_t* data_count
, TestURLInfo
** test_data
) {
614 *data_count
= arraysize(ordering_test_db
);
615 *test_data
= &ordering_test_db
[0];
618 TEST_F(HQPOrderingTest
, TEMatch
) {
619 std::vector
<std::string
> expected_urls
;
620 expected_urls
.push_back("http://techmeme.com/");
621 expected_urls
.push_back("http://www.teamliquid.net/");
622 expected_urls
.push_back("http://www.teamliquid.net/tlpd");
623 RunTest(ASCIIToUTF16("te"), expected_urls
, true,
624 ASCIIToUTF16("techmeme.com"));
627 TEST_F(HQPOrderingTest
, TEAMatch
) {
628 std::vector
<std::string
> expected_urls
;
629 expected_urls
.push_back("http://www.teamliquid.net/");
630 expected_urls
.push_back("http://www.teamliquid.net/tlpd");
631 expected_urls
.push_back("http://www.teamliquid.net/tlpd/korean/players");
632 RunTest(ASCIIToUTF16("tea"), expected_urls
, true,
633 ASCIIToUTF16("www.teamliquid.net"));