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_url_provider.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
15 #include "chrome/browser/autocomplete/history_quick_provider.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "components/history/core/browser/history_service.h"
23 #include "components/history/core/browser/url_database.h"
24 #include "components/metrics/proto/omnibox_event.pb.h"
25 #include "components/metrics/proto/omnibox_input_type.pb.h"
26 #include "components/omnibox/autocomplete_match.h"
27 #include "components/omnibox/autocomplete_provider.h"
28 #include "components/omnibox/autocomplete_provider_listener.h"
29 #include "components/omnibox/autocomplete_result.h"
30 #include "components/search_engines/default_search_manager.h"
31 #include "components/search_engines/search_terms_data.h"
32 #include "components/search_engines/template_url.h"
33 #include "components/search_engines/template_url_service.h"
34 #include "components/url_fixer/url_fixer.h"
35 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using base::ASCIIToUTF16
;
40 using base::TimeDelta
;
42 using content::TestBrowserThreadBundle
;
51 {"http://www.google.com/", "Google", 3, 3, 80},
53 // High-quality pages should get a host synthesized as a lower-quality match.
54 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 80},
56 // Less popular pages should have hosts synthesized as higher-quality
58 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 80},
60 // Unpopular pages should not appear in the results at all.
61 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0, 80},
63 // If a host has a match, we should pick it up during host synthesis.
64 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 80},
65 {"http://news.google.com/", "Google News", 1, 1, 80},
67 // Matches that are normally not inline-autocompletable should be
68 // autocompleted if they are shorter substitutes for longer matches that would
69 // have been inline autocompleted.
70 {"http://synthesisatest.com/foo/", "Test A", 1, 1, 80},
71 {"http://synthesisbtest.com/foo/", "Test B", 1, 1, 80},
72 {"http://synthesisbtest.com/foo/bar.html", "Test B Bar", 2, 2, 80},
74 // Suggested short URLs must be "good enough" and must match user input.
75 {"http://foo.com/", "Dir", 5, 5, 80},
76 {"http://foo.com/dir/", "Dir", 2, 2, 80},
77 {"http://foo.com/dir/another/", "Dir", 5, 1, 80},
78 {"http://foo.com/dir/another/again/", "Dir", 10, 0, 80},
79 {"http://foo.com/dir/another/again/myfile.html", "File", 10, 2, 80},
81 // We throw in a lot of extra URLs here to make sure we're testing the
82 // history database's query, not just the autocomplete provider.
83 {"http://startest.com/y/a", "A", 2, 2, 80},
84 {"http://startest.com/y/b", "B", 5, 2, 80},
85 {"http://startest.com/x/c", "C", 5, 2, 80},
86 {"http://startest.com/x/d", "D", 5, 5, 80},
87 {"http://startest.com/y/e", "E", 4, 2, 80},
88 {"http://startest.com/y/f", "F", 3, 2, 80},
89 {"http://startest.com/y/g", "G", 3, 2, 80},
90 {"http://startest.com/y/h", "H", 3, 2, 80},
91 {"http://startest.com/y/i", "I", 3, 2, 80},
92 {"http://startest.com/y/j", "J", 3, 2, 80},
93 {"http://startest.com/y/k", "K", 3, 2, 80},
94 {"http://startest.com/y/l", "L", 3, 2, 80},
95 {"http://startest.com/y/m", "M", 3, 2, 80},
97 // A file: URL is useful for testing that fixup does the right thing w.r.t.
98 // the number of trailing slashes on the user's input.
99 {"file:///C:/foo.txt", "", 2, 2, 80},
101 // Results with absurdly high typed_counts so that very generic queries like
102 // "http" will give consistent results even if more data is added above.
103 {"http://bogussite.com/a", "Bogus A", 10002, 10000, 80},
104 {"http://bogussite.com/b", "Bogus B", 10001, 10000, 80},
105 {"http://bogussite.com/c", "Bogus C", 10000, 10000, 80},
107 // Domain name with number.
108 {"http://www.17173.com/", "Domain with number", 3, 3, 80},
110 // URLs to test exact-matching behavior.
111 {"http://go/", "Intranet URL", 1, 1, 80},
112 {"http://gooey/", "Intranet URL 2", 5, 5, 80},
114 // URLs for testing offset adjustment.
115 {"http://www.\xEA\xB5\x90\xEC\x9C\xA1.kr/", "Korean", 2, 2, 80},
116 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 80},
117 {"http://ms/c++%20style%20guide", "Style guide", 2, 2, 80},
119 // URLs for testing ctrl-enter behavior.
120 {"http://binky/", "Intranet binky", 2, 2, 80},
121 {"http://winky/", "Intranet winky", 2, 2, 80},
122 {"http://www.winky.com/", "Internet winky", 5, 0, 80},
124 // URLs used by EmptyVisits.
125 {"http://pandora.com/", "Pandora", 2, 2, 80},
126 // This entry is explicitly added more recently than
127 // history::kLowQualityMatchAgeLimitInDays.
128 // {"http://p/", "p", 0, 0, 80},
130 // For intranet based tests.
131 {"http://intra/one", "Intranet", 2, 2, 80},
132 {"http://intra/two", "Intranet two", 1, 1, 80},
133 {"http://intra/three", "Intranet three", 2, 2, 80},
134 {"http://moo/bar", "Intranet moo", 1, 1, 80},
135 {"http://typedhost/typedpath", "Intranet typed", 1, 1, 80},
136 {"http://typedhost/untypedpath", "Intranet untyped", 1, 0, 80},
138 {"http://x.com/one", "Internet", 2, 2, 80},
139 {"http://x.com/two", "Internet two", 1, 1, 80},
140 {"http://x.com/three", "Internet three", 2, 2, 80},
142 // For experimental HUP scoring test.
143 {"http://7.com/1a", "One", 8, 4, 4},
144 {"http://7.com/2a", "Two A", 4, 2, 8},
145 {"http://7.com/2b", "Two B", 4, 1, 8},
146 {"http://7.com/3a", "Three", 2, 1, 16},
147 {"http://7.com/4a", "Four A", 1, 1, 32},
148 {"http://7.com/4b", "Four B", 1, 1, 64},
149 {"http://7.com/5a", "Five A", 8, 0, 64}, // never typed.
152 class HistoryURLProviderTest
: public testing::Test
,
153 public AutocompleteProviderListener
{
155 struct UrlAndLegalDefault
{
157 bool allowed_to_be_default_match
;
160 HistoryURLProviderTest()
161 : sort_matches_(false) {
162 HistoryQuickProvider::set_disabled(true);
165 ~HistoryURLProviderTest() override
{
166 HistoryQuickProvider::set_disabled(false);
169 // AutocompleteProviderListener:
170 void OnProviderUpdate(bool updated_matches
) override
;
173 static KeyedService
* CreateTemplateURLService(
174 content::BrowserContext
* context
) {
175 Profile
* profile
= static_cast<Profile
*>(context
);
176 return new TemplateURLService(
177 profile
->GetPrefs(), make_scoped_ptr(new SearchTermsData
), NULL
,
178 scoped_ptr
<TemplateURLServiceClient
>(new ChromeTemplateURLServiceClient(
179 HistoryServiceFactory::GetForProfile(
180 profile
, ServiceAccessType::EXPLICIT_ACCESS
))),
181 NULL
, NULL
, base::Closure());
185 void SetUp() override
{
186 ASSERT_TRUE(SetUpImpl(false));
188 void TearDown() override
;
190 // Does the real setup.
191 bool SetUpImpl(bool no_db
) WARN_UNUSED_RESULT
;
193 // Fills test data into the history system.
196 // Runs an autocomplete query on |text| and checks to see that the returned
197 // results' destination URLs match those provided. Also allows checking
198 // that the input type was identified correctly.
199 void RunTest(const base::string16
& text
,
200 const std::string
& desired_tld
,
201 bool prevent_inline_autocomplete
,
202 const UrlAndLegalDefault
* expected_urls
,
204 metrics::OmniboxInputType::Type
* identified_input_type
);
206 // A version of the above without the final |type| output parameter.
207 void RunTest(const base::string16
& text
,
208 const std::string
& desired_tld
,
209 bool prevent_inline_autocomplete
,
210 const UrlAndLegalDefault
* expected_urls
,
211 size_t num_results
) {
212 metrics::OmniboxInputType::Type type
;
213 return RunTest(text
, desired_tld
, prevent_inline_autocomplete
,
214 expected_urls
, num_results
, &type
);
217 content::TestBrowserThreadBundle thread_bundle_
;
219 scoped_ptr
<TestingProfile
> profile_
;
220 history::HistoryService
* history_service_
;
221 scoped_refptr
<HistoryURLProvider
> autocomplete_
;
222 // Should the matches be sorted and duplicates removed?
226 class HistoryURLProviderTestNoDB
: public HistoryURLProviderTest
{
228 void SetUp() override
{
229 ASSERT_TRUE(SetUpImpl(true));
233 class HistoryURLProviderTestNoSearchProvider
: public HistoryURLProviderTest
{
235 void SetUp() override
{
236 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
237 HistoryURLProviderTest::SetUp();
240 void TearDown() override
{
241 HistoryURLProviderTest::TearDown();
242 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
246 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches
) {
247 if (autocomplete_
->done())
248 base::MessageLoop::current()->Quit();
251 bool HistoryURLProviderTest::SetUpImpl(bool no_db
) {
252 profile_
.reset(new TestingProfile());
253 if (!(profile_
->CreateHistoryService(true, no_db
)))
256 profile_
->BlockUntilHistoryProcessesPendingRequests();
257 profile_
->BlockUntilHistoryIndexIsRefreshed();
259 profile_
->GetPrefs()->SetString(prefs::kAcceptLanguages
, "en-US,en,ko");
260 history_service_
= HistoryServiceFactory::GetForProfile(
261 profile_
.get(), ServiceAccessType::EXPLICIT_ACCESS
);
263 autocomplete_
= new HistoryURLProvider(this, profile_
.get());
264 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
265 profile_
.get(), &HistoryURLProviderTest::CreateTemplateURLService
);
270 void HistoryURLProviderTest::TearDown() {
271 autocomplete_
= NULL
;
274 void HistoryURLProviderTest::FillData() {
275 // Most visits are a long time ago (some tests require this since we do some
276 // special logic for things visited very recently). Note that this time must
277 // be more recent than the "expire history" threshold for the data to be kept
278 // in the main database.
280 // TODO(brettw) It would be nice if we could test this behavior, in which
281 // case the time would be specifed in the test_db structure.
282 const Time now
= Time::Now();
284 for (size_t i
= 0; i
< arraysize(test_db
); ++i
) {
285 const TestURLInfo
& cur
= test_db
[i
];
286 const GURL
current_url(cur
.url
);
287 history_service_
->AddPageWithDetails(
288 current_url
, base::UTF8ToUTF16(cur
.title
), cur
.visit_count
,
289 cur
.typed_count
, now
- TimeDelta::FromDays(cur
.age_in_days
), false,
290 history::SOURCE_BROWSED
);
293 history_service_
->AddPageWithDetails(
294 GURL("http://p/"), base::UTF8ToUTF16("p"), 0, 0,
296 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays
- 1),
297 false, history::SOURCE_BROWSED
);
300 void HistoryURLProviderTest::RunTest(
301 const base::string16
& text
,
302 const std::string
& desired_tld
,
303 bool prevent_inline_autocomplete
,
304 const UrlAndLegalDefault
* expected_urls
,
306 metrics::OmniboxInputType::Type
* identified_input_type
) {
307 AutocompleteInput
input(text
, base::string16::npos
, desired_tld
, GURL(),
308 metrics::OmniboxEventProto::INVALID_SPEC
,
309 prevent_inline_autocomplete
, false, true, true,
310 ChromeAutocompleteSchemeClassifier(profile_
.get()));
311 *identified_input_type
= input
.type();
312 autocomplete_
->Start(input
, false, false);
313 if (!autocomplete_
->done())
314 base::MessageLoop::current()->Run();
316 matches_
= autocomplete_
->matches();
318 TemplateURLService
* service
=
319 TemplateURLServiceFactory::GetForProfile(profile_
.get());
320 for (ACMatches::iterator i
= matches_
.begin(); i
!= matches_
.end(); ++i
)
321 i
->ComputeStrippedDestinationURL(service
);
322 AutocompleteResult::DedupMatchesByDestination(
323 input
.current_page_classification(), false, &matches_
);
324 std::sort(matches_
.begin(), matches_
.end(),
325 &AutocompleteMatch::MoreRelevant
);
327 ASSERT_EQ(num_results
, matches_
.size()) << "Input text: " << text
328 << "\nTLD: \"" << desired_tld
<< "\"";
329 for (size_t i
= 0; i
< num_results
; ++i
) {
330 EXPECT_EQ(expected_urls
[i
].url
, matches_
[i
].destination_url
.spec());
331 EXPECT_EQ(expected_urls
[i
].allowed_to_be_default_match
,
332 matches_
[i
].allowed_to_be_default_match
);
336 TEST_F(HistoryURLProviderTest
, PromoteShorterURLs
) {
337 // Test that hosts get synthesized below popular pages.
338 const UrlAndLegalDefault expected_nonsynth
[] = {
339 { "http://slashdot.org/favorite_page.html", false },
340 { "http://slashdot.org/", false }
342 RunTest(ASCIIToUTF16("slash"), std::string(), true, expected_nonsynth
,
343 arraysize(expected_nonsynth
));
345 // Test that hosts get synthesized above less popular pages.
346 const UrlAndLegalDefault expected_synth
[] = {
347 { "http://kerneltrap.org/", false },
348 { "http://kerneltrap.org/not_very_popular.html", false }
350 RunTest(ASCIIToUTF16("kernel"), std::string(), true, expected_synth
,
351 arraysize(expected_synth
));
353 // Test that unpopular pages are ignored completely.
354 RunTest(ASCIIToUTF16("fresh"), std::string(), true, NULL
, 0);
356 // Test that if we create or promote shorter suggestions that would not
357 // normally be inline autocompletable, we make them inline autocompletable if
358 // the original suggestion (that we replaced as "top") was inline
360 const UrlAndLegalDefault expected_synthesisa
[] = {
361 { "http://synthesisatest.com/", true },
362 { "http://synthesisatest.com/foo/", true }
364 RunTest(ASCIIToUTF16("synthesisa"), std::string(), false, expected_synthesisa
,
365 arraysize(expected_synthesisa
));
366 EXPECT_LT(matches_
.front().relevance
, 1200);
367 const UrlAndLegalDefault expected_synthesisb
[] = {
368 { "http://synthesisbtest.com/foo/", true },
369 { "http://synthesisbtest.com/foo/bar.html", true }
371 RunTest(ASCIIToUTF16("synthesisb"), std::string(), false, expected_synthesisb
,
372 arraysize(expected_synthesisb
));
373 EXPECT_GE(matches_
.front().relevance
, 1410);
375 // Test that if we have a synthesized host that matches a suggestion, they
376 // get combined into one.
377 const UrlAndLegalDefault expected_combine
[] = {
378 { "http://news.google.com/", false },
379 { "http://news.google.com/?ned=us&topic=n", false },
381 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), std::string(), true,
383 arraysize(expected_combine
)));
384 // The title should also have gotten set properly on the host for the
385 // synthesized one, since it was also in the results.
386 EXPECT_EQ(ASCIIToUTF16("Google News"), matches_
.front().description
);
388 // Test that short URL matching works correctly as the user types more
390 // The entry for foo.com is the best of all five foo.com* entries.
391 const UrlAndLegalDefault short_1
[] = {
392 { "http://foo.com/", false },
393 { "http://foo.com/dir/another/again/myfile.html", false },
394 { "http://foo.com/dir/", false }
396 RunTest(ASCIIToUTF16("foo"), std::string(), true, short_1
,
399 // When the user types the whole host, make sure we don't get two results for
401 const UrlAndLegalDefault short_2
[] = {
402 { "http://foo.com/", true },
403 { "http://foo.com/dir/another/again/myfile.html", false },
404 { "http://foo.com/dir/", false },
405 { "http://foo.com/dir/another/", false }
407 RunTest(ASCIIToUTF16("foo.com"), std::string(), true, short_2
,
409 RunTest(ASCIIToUTF16("foo.com/"), std::string(), true, short_2
,
412 // The filename is the second best of the foo.com* entries, but there is a
413 // shorter URL that's "good enough". The host doesn't match the user input
414 // and so should not appear.
415 const UrlAndLegalDefault short_3
[] = {
416 { "http://foo.com/dir/another/", false },
417 { "http://foo.com/d", true },
418 { "http://foo.com/dir/another/again/myfile.html", false },
419 { "http://foo.com/dir/", false }
421 RunTest(ASCIIToUTF16("foo.com/d"), std::string(), true, short_3
,
423 // If prevent_inline_autocomplete is false, we won't bother creating the
424 // URL-what-you-typed match because we have promoted inline autocompletions.
425 const UrlAndLegalDefault short_3_allow_inline
[] = {
426 { "http://foo.com/dir/another/", true },
427 { "http://foo.com/dir/another/again/myfile.html", true },
428 { "http://foo.com/dir/", true }
430 RunTest(ASCIIToUTF16("foo.com/d"), std::string(), false, short_3_allow_inline
,
431 arraysize(short_3_allow_inline
));
433 // We shouldn't promote shorter URLs than the best if they're not good
435 const UrlAndLegalDefault short_4
[] = {
436 { "http://foo.com/dir/another/again/myfile.html", false },
437 { "http://foo.com/dir/another/a", true },
438 { "http://foo.com/dir/another/again/", false }
440 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), true, short_4
,
442 // If prevent_inline_autocomplete is false, we won't bother creating the
443 // URL-what-you-typed match because we have promoted inline autocompletions.
444 const UrlAndLegalDefault short_4_allow_inline
[] = {
445 { "http://foo.com/dir/another/again/myfile.html", true },
446 { "http://foo.com/dir/another/again/", true }
448 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), false,
449 short_4_allow_inline
, arraysize(short_4_allow_inline
));
451 // Exact matches should always be best no matter how much more another match
453 const UrlAndLegalDefault short_5a
[] = {
454 { "http://gooey/", true },
455 { "http://www.google.com/", true },
456 { "http://go/", true }
458 const UrlAndLegalDefault short_5b
[] = {
459 { "http://go/", true },
460 { "http://gooey/", true },
461 { "http://www.google.com/", true }
463 RunTest(ASCIIToUTF16("g"), std::string(), false, short_5a
,
464 arraysize(short_5a
));
465 RunTest(ASCIIToUTF16("go"), std::string(), false, short_5b
,
466 arraysize(short_5b
));
469 TEST_F(HistoryURLProviderTest
, CullRedirects
) {
470 // URLs we will be using, plus the visit counts they will initially get
471 // (the redirect set below will also increment the visit counts). We want
472 // the results to be in A,B,C order. Note also that our visit counts are
473 // all high enough so that domain synthesizing won't get triggered.
478 {"http://redirects/A", 30},
479 {"http://redirects/B", 20},
480 {"http://redirects/C", 10}
482 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
483 history_service_
->AddPageWithDetails(GURL(test_cases
[i
].url
),
484 ASCIIToUTF16("Title"), test_cases
[i
].count
, test_cases
[i
].count
,
485 Time::Now(), false, history::SOURCE_BROWSED
);
488 // Create a B->C->A redirect chain, but set the visit counts such that they
489 // will appear in A,B,C order in the results. The autocomplete query will
490 // search for the most recent visit when looking for redirects, so this will
491 // be found even though the previous visits had no redirects.
492 history::RedirectList redirects_to_a
;
493 redirects_to_a
.push_back(GURL(test_cases
[1].url
));
494 redirects_to_a
.push_back(GURL(test_cases
[2].url
));
495 redirects_to_a
.push_back(GURL(test_cases
[0].url
));
496 history_service_
->AddPage(GURL(test_cases
[0].url
), base::Time::Now(),
497 NULL
, 0, GURL(), redirects_to_a
, ui::PAGE_TRANSITION_TYPED
,
498 history::SOURCE_BROWSED
, true);
500 // Because all the results are part of a redirect chain with other results,
501 // all but the first one (A) should be culled. We should get the default
502 // "what you typed" result, plus this one.
503 const base::string16
typing(ASCIIToUTF16("http://redirects/"));
504 const UrlAndLegalDefault expected_results
[] = {
505 { test_cases
[0].url
, false },
506 { base::UTF16ToUTF8(typing
), true }
508 RunTest(typing
, std::string(), true, expected_results
,
509 arraysize(expected_results
));
511 // If prevent_inline_autocomplete is false, we won't bother creating the
512 // URL-what-you-typed match because we have promoted inline autocompletions.
513 // The result set should instead consist of a single URL representing the
514 // whole set of redirects.
515 const UrlAndLegalDefault expected_results_allow_inlining
[] = {
516 { test_cases
[0].url
, true }
518 RunTest(typing
, std::string(), false, expected_results_allow_inlining
,
519 arraysize(expected_results_allow_inlining
));
522 TEST_F(HistoryURLProviderTestNoSearchProvider
, WhatYouTypedNoSearchProvider
) {
523 // When no search provider is available, make sure we provide WYT matches
524 // for text that could be a URL.
526 const UrlAndLegalDefault results_1
[] = {
527 { "http://wytmatch/", true }
529 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, results_1
,
530 arraysize(results_1
));
532 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL
, 0);
533 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL
, 0);
535 const UrlAndLegalDefault results_2
[] = {
536 { "http://wytmatch+foo+bar.com/", true }
538 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false,
539 results_2
, arraysize(results_2
));
542 TEST_F(HistoryURLProviderTest
, WhatYouTyped
) {
543 // Make sure we suggest a What You Typed match at the right times.
544 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, NULL
, 0);
545 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL
, 0);
546 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL
, 0);
547 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false, NULL
, 0);
549 const UrlAndLegalDefault results_1
[] = {
550 { "http://www.wytmatch.com/", true }
552 RunTest(ASCIIToUTF16("wytmatch"), "com", false, results_1
,
553 arraysize(results_1
));
555 const UrlAndLegalDefault results_2
[] = {
556 { "http://wytmatch%20foo%20bar/", true }
558 RunTest(ASCIIToUTF16("http://wytmatch foo bar"), std::string(), false,
559 results_2
, arraysize(results_2
));
561 const UrlAndLegalDefault results_3
[] = {
562 { "https://wytmatch%20foo%20bar/", true }
564 RunTest(ASCIIToUTF16("https://wytmatch foo bar"), std::string(), false,
565 results_3
, arraysize(results_3
));
568 TEST_F(HistoryURLProviderTest
, Fixup
) {
569 // Test for various past crashes we've had.
570 RunTest(ASCIIToUTF16("\\"), std::string(), false, NULL
, 0);
571 RunTest(ASCIIToUTF16("#"), std::string(), false, NULL
, 0);
572 RunTest(ASCIIToUTF16("%20"), std::string(), false, NULL
, 0);
573 const UrlAndLegalDefault fixup_crash
[] = {
574 { "http://%EF%BD%A5@s/", true }
576 RunTest(base::WideToUTF16(L
"\uff65@s"), std::string(), false, fixup_crash
,
577 arraysize(fixup_crash
));
578 RunTest(base::WideToUTF16(L
"\u2015\u2015@ \uff7c"), std::string(), false,
581 // Fixing up "file:" should result in an inline autocomplete offset of just
582 // after "file:", not just after "file://".
583 const base::string16
input_1(ASCIIToUTF16("file:"));
584 const UrlAndLegalDefault fixup_1
[] = {
585 { "file:///C:/foo.txt", true }
587 ASSERT_NO_FATAL_FAILURE(RunTest(input_1
, std::string(), false, fixup_1
,
588 arraysize(fixup_1
)));
589 EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
590 matches_
.front().inline_autocompletion
);
592 // Fixing up "http:/" should result in an inline autocomplete offset of just
593 // after "http:/", not just after "http:".
594 const base::string16
input_2(ASCIIToUTF16("http:/"));
595 const UrlAndLegalDefault fixup_2
[] = {
596 { "http://bogussite.com/a", true },
597 { "http://bogussite.com/b", true },
598 { "http://bogussite.com/c", true }
600 ASSERT_NO_FATAL_FAILURE(RunTest(input_2
, std::string(), false, fixup_2
,
601 arraysize(fixup_2
)));
602 EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
603 matches_
.front().inline_autocompletion
);
605 // Adding a TLD to a small number like "56" should result in "www.56.com"
606 // rather than "0.0.0.56.com".
607 const UrlAndLegalDefault fixup_3
[] = {
608 { "http://www.56.com/", true }
610 RunTest(ASCIIToUTF16("56"), "com", true, fixup_3
, arraysize(fixup_3
));
612 // An input looks like a IP address like "127.0.0.1" should result in
613 // "http://127.0.0.1/".
614 const UrlAndLegalDefault fixup_4
[] = {
615 { "http://127.0.0.1/", true }
617 RunTest(ASCIIToUTF16("127.0.0.1"), std::string(), false, fixup_4
,
620 // An number "17173" should result in "http://www.17173.com/" in db.
621 const UrlAndLegalDefault fixup_5
[] = {
622 { "http://www.17173.com/", true }
624 RunTest(ASCIIToUTF16("17173"), std::string(), false, fixup_5
,
628 // Make sure the results for the input 'p' don't change between the first and
630 TEST_F(HistoryURLProviderTest
, EmptyVisits
) {
631 // Wait for history to create the in memory DB.
632 profile_
->BlockUntilHistoryProcessesPendingRequests();
634 AutocompleteInput
input(ASCIIToUTF16("p"), base::string16::npos
,
635 std::string(), GURL(),
636 metrics::OmniboxEventProto::INVALID_SPEC
, false,
638 ChromeAutocompleteSchemeClassifier(profile_
.get()));
639 autocomplete_
->Start(input
, false, false);
640 // HistoryURLProvider shouldn't be done (waiting on async results).
641 EXPECT_FALSE(autocomplete_
->done());
643 // We should get back an entry for pandora.
644 matches_
= autocomplete_
->matches();
645 ASSERT_GT(matches_
.size(), 0u);
646 EXPECT_EQ(GURL("http://pandora.com/"), matches_
[0].destination_url
);
647 int pandora_relevance
= matches_
[0].relevance
;
649 // Run the message loop. When |autocomplete_| finishes the loop is quit.
650 base::MessageLoop::current()->Run();
651 EXPECT_TRUE(autocomplete_
->done());
652 matches_
= autocomplete_
->matches();
653 ASSERT_GT(matches_
.size(), 0u);
654 EXPECT_EQ(GURL("http://pandora.com/"), matches_
[0].destination_url
);
655 EXPECT_EQ(pandora_relevance
, matches_
[0].relevance
);
658 TEST_F(HistoryURLProviderTestNoDB
, NavigateWithoutDB
) {
659 // Ensure that we will still produce matches for navigation when there is no
661 UrlAndLegalDefault navigation_1
[] = {
662 { "http://test.com/", true }
664 RunTest(ASCIIToUTF16("test.com"), std::string(), false, navigation_1
,
665 arraysize(navigation_1
));
667 UrlAndLegalDefault navigation_2
[] = {
668 { "http://slash/", true }
670 RunTest(ASCIIToUTF16("slash"), std::string(), false, navigation_2
,
671 arraysize(navigation_2
));
673 RunTest(ASCIIToUTF16("this is a query"), std::string(), false, NULL
, 0);
676 TEST_F(HistoryURLProviderTest
, DontAutocompleteOnTrailingWhitespace
) {
677 AutocompleteInput
input(ASCIIToUTF16("slash "), base::string16::npos
,
678 std::string(), GURL(),
679 metrics::OmniboxEventProto::INVALID_SPEC
, false,
681 ChromeAutocompleteSchemeClassifier(profile_
.get()));
682 autocomplete_
->Start(input
, false, false);
683 if (!autocomplete_
->done())
684 base::MessageLoop::current()->Run();
686 // None of the matches should attempt to autocomplete.
687 matches_
= autocomplete_
->matches();
688 for (size_t i
= 0; i
< matches_
.size(); ++i
) {
689 EXPECT_TRUE(matches_
[i
].inline_autocompletion
.empty());
690 EXPECT_FALSE(matches_
[i
].allowed_to_be_default_match
);
694 TEST_F(HistoryURLProviderTest
, TreatEmailsAsSearches
) {
695 // Visiting foo.com should not make this string be treated as a navigation.
696 // That means the result should be scored around 1200 ("what you typed")
698 const UrlAndLegalDefault expected
[] = {
699 { "http://user@foo.com/", true }
701 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), std::string(),
702 false, expected
, arraysize(expected
)));
703 EXPECT_LE(1200, matches_
[0].relevance
);
704 EXPECT_LT(matches_
[0].relevance
, 1210);
707 TEST_F(HistoryURLProviderTest
, IntranetURLsWithPaths
) {
713 { "fooey/", 1200 }, // 1200 for URL would still navigate by default.
714 { "fooey/a", 1200 }, // 1200 for UNKNOWN would not.
715 { "fooey/a b", 1200 }, // Also UNKNOWN.
719 { "gooey/a b", 1400 },
721 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
722 SCOPED_TRACE(test_cases
[i
].input
);
723 if (test_cases
[i
].relevance
== 0) {
724 RunTest(ASCIIToUTF16(test_cases
[i
].input
), std::string(), false, NULL
, 0);
726 const UrlAndLegalDefault output
[] = {
727 {url_fixer::FixupURL(test_cases
[i
].input
, std::string()).spec(),
729 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
730 std::string(), false, output
, arraysize(output
)));
731 // Actual relevance should be at least what test_cases expects and
732 // and no more than 10 more.
733 EXPECT_LE(test_cases
[i
].relevance
, matches_
[0].relevance
);
734 EXPECT_LT(matches_
[0].relevance
, test_cases
[i
].relevance
+ 10);
739 // Makes sure autocompletion happens for intranet sites that have been
740 // previoulsy visited.
741 TEST_F(HistoryURLProviderTest
, IntranetURLCompletion
) {
742 sort_matches_
= true;
744 const UrlAndLegalDefault expected1
[] = {
745 { "http://intra/three", true },
746 { "http://intra/two", true }
748 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), std::string(), false,
749 expected1
, arraysize(expected1
)));
750 EXPECT_LE(1410, matches_
[0].relevance
);
751 EXPECT_LT(matches_
[0].relevance
, 1420);
752 EXPECT_EQ(matches_
[0].relevance
- 1, matches_
[1].relevance
);
754 const UrlAndLegalDefault expected2
[] = {
755 { "http://moo/b", true },
756 { "http://moo/bar", true }
758 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), std::string(), false,
759 expected2
, arraysize(expected2
)));
760 // The url what you typed match should be around 1400, otherwise the
761 // search what you typed match is going to be first.
762 EXPECT_LE(1400, matches_
[0].relevance
);
763 EXPECT_LT(matches_
[0].relevance
, 1410);
765 const UrlAndLegalDefault expected3
[] = {
766 { "http://intra/one", true },
767 { "http://intra/three", true },
768 { "http://intra/two", true }
770 RunTest(ASCIIToUTF16("intra"), std::string(), false, expected3
,
771 arraysize(expected3
));
773 const UrlAndLegalDefault expected4
[] = {
774 { "http://intra/one", true },
775 { "http://intra/three", true },
776 { "http://intra/two", true }
778 RunTest(ASCIIToUTF16("intra/"), std::string(), false, expected4
,
779 arraysize(expected4
));
781 const UrlAndLegalDefault expected5
[] = {
782 { "http://intra/one", true }
784 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), std::string(), false,
785 expected5
, arraysize(expected5
)));
786 EXPECT_LE(1410, matches_
[0].relevance
);
787 EXPECT_LT(matches_
[0].relevance
, 1420);
789 const UrlAndLegalDefault expected6
[] = {
790 { "http://intra/x", true }
792 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), std::string(), false,
793 expected6
, arraysize(expected6
)));
794 EXPECT_LE(1400, matches_
[0].relevance
);
795 EXPECT_LT(matches_
[0].relevance
, 1410);
797 const UrlAndLegalDefault expected7
[] = {
798 { "http://typedhost/untypedpath", true }
800 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
801 std::string(), false, expected7
,
802 arraysize(expected7
)));
803 EXPECT_LE(1400, matches_
[0].relevance
);
804 EXPECT_LT(matches_
[0].relevance
, 1410);
807 TEST_F(HistoryURLProviderTest
, CrashDueToFixup
) {
808 // This test passes if we don't crash. The results don't matter.
809 const char* const test_cases
[] = {
814 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
815 AutocompleteInput
input(ASCIIToUTF16(test_cases
[i
]), base::string16::npos
,
816 std::string(), GURL(),
817 metrics::OmniboxEventProto::INVALID_SPEC
,
818 false, false, true, true,
819 ChromeAutocompleteSchemeClassifier(profile_
.get()));
820 autocomplete_
->Start(input
, false, false);
821 if (!autocomplete_
->done())
822 base::MessageLoop::current()->Run();
826 TEST_F(HistoryURLProviderTest
, DoesNotProvideMatchesOnFocus
) {
827 AutocompleteInput
input(ASCIIToUTF16("foo"), base::string16::npos
,
828 std::string(), GURL(),
829 metrics::OmniboxEventProto::INVALID_SPEC
,
830 false, false, true, true,
831 ChromeAutocompleteSchemeClassifier(profile_
.get()));
832 autocomplete_
->Start(input
, false, true);
833 EXPECT_TRUE(autocomplete_
->matches().empty());
836 TEST_F(HistoryURLProviderTest
, CullSearchResults
) {
837 // Set up a default search engine.
838 TemplateURLData data
;
839 data
.SetKeyword(ASCIIToUTF16("TestEngine"));
840 data
.SetURL("http://testsearch.com/?q={searchTerms}");
841 TemplateURLService
* template_url_service
=
842 TemplateURLServiceFactory::GetForProfile(profile_
.get());
843 TemplateURL
* template_url
= new TemplateURL(data
);
844 template_url_service
->Add(template_url
);
845 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
846 template_url_service
->Load();
848 // URLs we will be using, plus the visit counts they will initially get
849 // (the redirect set below will also increment the visit counts). We want
850 // the results to be in A,B,C order. Note also that our visit counts are
851 // all high enough so that domain synthesizing won't get triggered.
856 {"https://testsearch.com/", 30},
857 {"https://testsearch.com/?q=foobar", 20},
858 {"http://foobar.com/", 10}
860 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
861 history_service_
->AddPageWithDetails(GURL(test_cases
[i
].url
),
862 base::UTF8ToUTF16("Title"), test_cases
[i
].count
, test_cases
[i
].count
,
863 Time::Now(), false, history::SOURCE_BROWSED
);
866 // We should not see search URLs when typing a previously used query.
867 const UrlAndLegalDefault expected_when_searching_query
[] = {
868 { test_cases
[2].url
, false }
870 RunTest(ASCIIToUTF16("foobar"), std::string(), true,
871 expected_when_searching_query
, arraysize(expected_when_searching_query
));
873 // We should not see search URLs when typing the search engine name.
874 const UrlAndLegalDefault expected_when_searching_site
[] = {
875 { test_cases
[0].url
, false }
877 RunTest(ASCIIToUTF16("testsearch"), std::string(), true,
878 expected_when_searching_site
, arraysize(expected_when_searching_site
));
881 TEST_F(HistoryURLProviderTest
, SuggestExactInput
) {
882 const size_t npos
= std::string::npos
;
888 const char* contents
;
889 // Offsets of the ACMatchClassifications, terminated by npos.
891 // The index of the ACMatchClassification that should have the MATCH bit
892 // set, npos if no ACMatchClassification should have the MATCH bit set.
893 size_t match_classification_index
;
895 { "http://www.somesite.com", false,
896 "http://www.somesite.com", {0, npos
, npos
}, 0 },
897 { "www.somesite.com", true,
898 "www.somesite.com", {0, npos
, npos
}, 0 },
899 { "www.somesite.com", false,
900 "http://www.somesite.com", {0, 7, npos
}, 1 },
901 { "somesite.com", true,
902 "somesite.com", {0, npos
, npos
}, 0 },
903 { "somesite.com", false,
904 "http://somesite.com", {0, 7, npos
}, 1 },
906 "w", {0, npos
, npos
}, 0 },
908 "http://w", {0, 7, npos
}, 1 },
910 "w.com", {0, npos
, npos
}, 0 },
912 "http://w.com", {0, 7, npos
}, 1 },
914 "www.w.com", {0, npos
, npos
}, 0 },
915 { "www.w.com", false,
916 "http://www.w.com", {0, 7, npos
}, 1 },
917 { "view-source:w", true,
918 "view-source:w", {0, npos
, npos
}, 0 },
919 { "view-source:www.w.com/", true,
920 "view-source:www.w.com", {0, npos
, npos
}, npos
},
921 { "view-source:www.w.com/", false,
922 "view-source:http://www.w.com", {0, npos
, npos
}, npos
},
923 { "view-source:http://www.w.com/", false,
924 "view-source:http://www.w.com", {0, npos
, npos
}, 0 },
925 { " view-source:", true,
926 "view-source:", {0, npos
, npos
}, 0 },
927 { "http:////////w.com", false,
928 "http://w.com", {0, npos
, npos
}, npos
},
929 { " http:////////www.w.com", false,
930 "http://www.w.com", {0, npos
, npos
}, npos
},
931 { "http:a///www.w.com", false,
932 "http://a///www.w.com", {0, npos
, npos
}, npos
},
933 { "mailto://a@b.com", true,
934 "mailto://a@b.com", {0, npos
, npos
}, 0 },
935 { "mailto://a@b.com", false,
936 "mailto://a@b.com", {0, npos
, npos
}, 0 },
938 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
939 SCOPED_TRACE(testing::Message() << "Index " << i
<< " input: "
940 << test_cases
[i
].input
<< ", trim_http: "
941 << test_cases
[i
].trim_http
);
943 AutocompleteInput
input(ASCIIToUTF16(test_cases
[i
].input
),
944 base::string16::npos
, std::string(),
946 metrics::OmniboxEventProto::INVALID_SPEC
, false,
948 ChromeAutocompleteSchemeClassifier(profile_
.get()));
949 AutocompleteMatch
match(autocomplete_
->SuggestExactInput(
950 input
.text(), input
.canonicalized_url(), test_cases
[i
].trim_http
));
951 EXPECT_EQ(ASCIIToUTF16(test_cases
[i
].contents
), match
.contents
);
952 for (size_t match_index
= 0; match_index
< match
.contents_class
.size();
954 EXPECT_EQ(test_cases
[i
].offsets
[match_index
],
955 match
.contents_class
[match_index
].offset
);
956 EXPECT_EQ(ACMatchClassification::URL
|
957 (match_index
== test_cases
[i
].match_classification_index
?
958 ACMatchClassification::MATCH
: 0),
959 match
.contents_class
[match_index
].style
);
961 EXPECT_EQ(npos
, test_cases
[i
].offsets
[match
.contents_class
.size()]);
965 TEST_F(HistoryURLProviderTest
, HUPScoringExperiment
) {
966 HUPScoringParams max_2000_no_time_decay
;
967 max_2000_no_time_decay
.typed_count_buckets
.buckets().push_back(
968 std::make_pair(0.0, 2000));
969 HUPScoringParams max_1250_no_time_decay
;
970 max_1250_no_time_decay
.typed_count_buckets
.buckets().push_back(
971 std::make_pair(0.0, 1250));
972 HUPScoringParams max_1000_no_time_decay
;
973 max_1000_no_time_decay
.typed_count_buckets
.buckets().push_back(
974 std::make_pair(0.0, 1000));
976 HUPScoringParams max_1100_with_time_decay_and_max_cap
;
977 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.
978 set_relevance_cap(1400);
979 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.
980 set_half_life_days(16);
981 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
982 std::make_pair(0.5, 1100));
983 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
984 std::make_pair(0.24, 200));
985 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
986 std::make_pair(0.0, 100));
988 HUPScoringParams max_1100_visit_typed_decays
;
989 max_1100_visit_typed_decays
.typed_count_buckets
.set_half_life_days(16);
990 max_1100_visit_typed_decays
.typed_count_buckets
.buckets().push_back(
991 std::make_pair(0.5, 1100));
992 max_1100_visit_typed_decays
.typed_count_buckets
.buckets().push_back(
993 std::make_pair(0.0, 100));
994 max_1100_visit_typed_decays
.visited_count_buckets
.set_half_life_days(16);
995 max_1100_visit_typed_decays
.visited_count_buckets
.buckets().push_back(
996 std::make_pair(0.5, 550));
997 max_1100_visit_typed_decays
.visited_count_buckets
.buckets().push_back(
998 std::make_pair(0.0, 50));
1000 const int kMaxMatches
= 3;
1003 HUPScoringParams scoring_params
;
1004 struct ExpectedMatch
{
1006 int control_relevance
;
1007 int experiment_relevance
;
1009 ExpectedMatch matches
[kMaxMatches
];
1011 // Max score 2000 -> no demotion.
1012 { "7.com/1", max_2000_no_time_decay
,
1013 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1015 // Limit score to 1250/1000 and make sure that the top match is unchanged.
1016 { "7.com/1", max_1250_no_time_decay
,
1017 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1018 { "7.com/2", max_1250_no_time_decay
,
1019 {{"7.com/2a", 1413, 1413}, {"7.com/2b", 1412, 1250}, {NULL
, 0, 0}} },
1020 { "7.com/4", max_1000_no_time_decay
,
1021 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 1000},
1022 {"7.com/4b", 1201, 999}} },
1024 // Max relevance cap is 1400 and half-life is 16 days.
1025 { "7.com/1", max_1100_with_time_decay_and_max_cap
,
1026 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1027 { "7.com/4", max_1100_with_time_decay_and_max_cap
,
1028 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 200},
1029 {"7.com/4b", 1201, 100}} },
1031 // Max relevance cap is 1400 and half-life is 16 days for both visit/typed.
1032 { "7.com/5", max_1100_visit_typed_decays
,
1033 {{"7.com/5", 1203, 1203}, {"7.com/5a", 1202, 50}, {NULL
, 0, 0}} },
1035 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
1036 SCOPED_TRACE(test_cases
[i
].input
);
1037 UrlAndLegalDefault output
[kMaxMatches
];
1039 for (max_matches
= 0; max_matches
< kMaxMatches
; ++max_matches
) {
1040 if (test_cases
[i
].matches
[max_matches
].url
== NULL
)
1042 output
[max_matches
].url
=
1043 url_fixer::FixupURL(test_cases
[i
].matches
[max_matches
].url
,
1044 std::string()).spec();
1045 output
[max_matches
].allowed_to_be_default_match
= true;
1047 autocomplete_
->scoring_params_
= test_cases
[i
].scoring_params
;
1049 // Test the control (scoring disabled).
1050 autocomplete_
->scoring_params_
.experimental_scoring_enabled
= false;
1051 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
1052 std::string(), false, output
, max_matches
));
1053 for (int j
= 0; j
< max_matches
; ++j
) {
1054 EXPECT_EQ(test_cases
[i
].matches
[j
].control_relevance
,
1055 matches_
[j
].relevance
);
1058 // Test the experiment (scoring enabled).
1059 autocomplete_
->scoring_params_
.experimental_scoring_enabled
= true;
1060 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
1061 std::string(), false, output
, max_matches
));
1062 for (int j
= 0; j
< max_matches
; ++j
) {
1063 EXPECT_EQ(test_cases
[i
].matches
[j
].experiment_relevance
,
1064 matches_
[j
].relevance
);