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 "components/omnibox/browser/history_url_provider.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
16 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
17 #include "chrome/browser/history/history_service_factory.h"
18 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "components/history/core/browser/history_service.h"
24 #include "components/history/core/browser/url_database.h"
25 #include "components/metrics/proto/omnibox_event.pb.h"
26 #include "components/metrics/proto/omnibox_input_type.pb.h"
27 #include "components/omnibox/browser/autocomplete_match.h"
28 #include "components/omnibox/browser/autocomplete_provider.h"
29 #include "components/omnibox/browser/autocomplete_provider_listener.h"
30 #include "components/omnibox/browser/autocomplete_result.h"
31 #include "components/omnibox/browser/history_quick_provider.h"
32 #include "components/search_engines/default_search_manager.h"
33 #include "components/search_engines/search_terms_data.h"
34 #include "components/search_engines/template_url.h"
35 #include "components/search_engines/template_url_service.h"
36 #include "components/url_fixer/url_fixer.h"
37 #include "content/public/test/test_browser_thread_bundle.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 using base::ASCIIToUTF16
;
42 using base::TimeDelta
;
44 using content::TestBrowserThreadBundle
;
53 {"http://www.google.com/", "Google", 3, 3, 80},
55 // High-quality pages should get a host synthesized as a lower-quality match.
56 {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 80},
58 // Less popular pages should have hosts synthesized as higher-quality
60 {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 80},
62 // Unpopular pages should not appear in the results at all.
63 {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0, 80},
65 // If a host has a match, we should pick it up during host synthesis.
66 {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 80},
67 {"http://news.google.com/", "Google News", 1, 1, 80},
69 // Matches that are normally not inline-autocompletable should be
70 // autocompleted if they are shorter substitutes for longer matches that would
71 // have been inline autocompleted.
72 {"http://synthesisatest.com/foo/", "Test A", 1, 1, 80},
73 {"http://synthesisbtest.com/foo/", "Test B", 1, 1, 80},
74 {"http://synthesisbtest.com/foo/bar.html", "Test B Bar", 2, 2, 80},
76 // Suggested short URLs must be "good enough" and must match user input.
77 {"http://foo.com/", "Dir", 5, 5, 80},
78 {"http://foo.com/dir/", "Dir", 2, 2, 80},
79 {"http://foo.com/dir/another/", "Dir", 5, 1, 80},
80 {"http://foo.com/dir/another/again/", "Dir", 10, 0, 80},
81 {"http://foo.com/dir/another/again/myfile.html", "File", 10, 2, 80},
83 // We throw in a lot of extra URLs here to make sure we're testing the
84 // history database's query, not just the autocomplete provider.
85 {"http://startest.com/y/a", "A", 2, 2, 80},
86 {"http://startest.com/y/b", "B", 5, 2, 80},
87 {"http://startest.com/x/c", "C", 5, 2, 80},
88 {"http://startest.com/x/d", "D", 5, 5, 80},
89 {"http://startest.com/y/e", "E", 4, 2, 80},
90 {"http://startest.com/y/f", "F", 3, 2, 80},
91 {"http://startest.com/y/g", "G", 3, 2, 80},
92 {"http://startest.com/y/h", "H", 3, 2, 80},
93 {"http://startest.com/y/i", "I", 3, 2, 80},
94 {"http://startest.com/y/j", "J", 3, 2, 80},
95 {"http://startest.com/y/k", "K", 3, 2, 80},
96 {"http://startest.com/y/l", "L", 3, 2, 80},
97 {"http://startest.com/y/m", "M", 3, 2, 80},
99 // A file: URL is useful for testing that fixup does the right thing w.r.t.
100 // the number of trailing slashes on the user's input.
101 {"file:///C:/foo.txt", "", 2, 2, 80},
103 // Results with absurdly high typed_counts so that very generic queries like
104 // "http" will give consistent results even if more data is added above.
105 {"http://bogussite.com/a", "Bogus A", 10002, 10000, 80},
106 {"http://bogussite.com/b", "Bogus B", 10001, 10000, 80},
107 {"http://bogussite.com/c", "Bogus C", 10000, 10000, 80},
109 // Domain name with number.
110 {"http://www.17173.com/", "Domain with number", 3, 3, 80},
112 // URLs to test exact-matching behavior.
113 {"http://go/", "Intranet URL", 1, 1, 80},
114 {"http://gooey/", "Intranet URL 2", 5, 5, 80},
116 // URLs for testing offset adjustment.
117 {"http://www.\xEA\xB5\x90\xEC\x9C\xA1.kr/", "Korean", 2, 2, 80},
118 {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 80},
119 {"http://ms/c++%20style%20guide", "Style guide", 2, 2, 80},
121 // URLs for testing ctrl-enter behavior.
122 {"http://binky/", "Intranet binky", 2, 2, 80},
123 {"http://winky/", "Intranet winky", 2, 2, 80},
124 {"http://www.winky.com/", "Internet winky", 5, 0, 80},
126 // URLs used by EmptyVisits.
127 {"http://pandora.com/", "Pandora", 2, 2, 80},
128 // This entry is explicitly added more recently than
129 // history::kLowQualityMatchAgeLimitInDays.
130 // {"http://p/", "p", 0, 0, 80},
132 // For intranet based tests.
133 {"http://intra/one", "Intranet", 2, 2, 80},
134 {"http://intra/two", "Intranet two", 1, 1, 80},
135 {"http://intra/three", "Intranet three", 2, 2, 80},
136 {"http://moo/bar", "Intranet moo", 1, 1, 80},
137 {"http://typedhost/typedpath", "Intranet typed", 1, 1, 80},
138 {"http://typedhost/untypedpath", "Intranet untyped", 1, 0, 80},
140 {"http://x.com/one", "Internet", 2, 2, 80},
141 {"http://x.com/two", "Internet two", 1, 1, 80},
142 {"http://x.com/three", "Internet three", 2, 2, 80},
144 // For experimental HUP scoring test.
145 {"http://7.com/1a", "One", 8, 4, 4},
146 {"http://7.com/2a", "Two A", 4, 2, 8},
147 {"http://7.com/2b", "Two B", 4, 1, 8},
148 {"http://7.com/3a", "Three", 2, 1, 16},
149 {"http://7.com/4a", "Four A", 1, 1, 32},
150 {"http://7.com/4b", "Four B", 1, 1, 64},
151 {"http://7.com/5a", "Five A", 8, 0, 64}, // never typed.
154 class HistoryURLProviderTest
: public testing::Test
,
155 public AutocompleteProviderListener
{
157 struct UrlAndLegalDefault
{
159 bool allowed_to_be_default_match
;
162 HistoryURLProviderTest()
163 : sort_matches_(false) {
164 HistoryQuickProvider::set_disabled(true);
167 ~HistoryURLProviderTest() override
{
168 HistoryQuickProvider::set_disabled(false);
171 // AutocompleteProviderListener:
172 void OnProviderUpdate(bool updated_matches
) override
;
175 static scoped_ptr
<KeyedService
> CreateTemplateURLService(
176 content::BrowserContext
* context
) {
177 Profile
* profile
= static_cast<Profile
*>(context
);
178 return make_scoped_ptr(new TemplateURLService(
179 profile
->GetPrefs(), make_scoped_ptr(new SearchTermsData
), NULL
,
180 scoped_ptr
<TemplateURLServiceClient
>(new ChromeTemplateURLServiceClient(
181 HistoryServiceFactory::GetForProfile(
182 profile
, ServiceAccessType::EXPLICIT_ACCESS
))),
183 NULL
, NULL
, base::Closure()));
187 void SetUp() override
{
188 ASSERT_TRUE(SetUpImpl(false));
190 void TearDown() override
;
192 // Does the real setup.
193 bool SetUpImpl(bool no_db
) WARN_UNUSED_RESULT
;
195 // Fills test data into the history system.
198 // Runs an autocomplete query on |text| and checks to see that the returned
199 // results' destination URLs match those provided. Also allows checking
200 // that the input type was identified correctly.
201 void RunTest(const base::string16
& text
,
202 const std::string
& desired_tld
,
203 bool prevent_inline_autocomplete
,
204 const UrlAndLegalDefault
* expected_urls
,
206 metrics::OmniboxInputType::Type
* identified_input_type
);
208 // A version of the above without the final |type| output parameter.
209 void RunTest(const base::string16
& text
,
210 const std::string
& desired_tld
,
211 bool prevent_inline_autocomplete
,
212 const UrlAndLegalDefault
* expected_urls
,
213 size_t num_results
) {
214 metrics::OmniboxInputType::Type type
;
215 return RunTest(text
, desired_tld
, prevent_inline_autocomplete
,
216 expected_urls
, num_results
, &type
);
219 content::TestBrowserThreadBundle thread_bundle_
;
221 scoped_ptr
<TestingProfile
> profile_
;
222 scoped_ptr
<ChromeAutocompleteProviderClient
> client_
;
223 history::HistoryService
* history_service_
;
224 scoped_refptr
<HistoryURLProvider
> autocomplete_
;
225 // Should the matches be sorted and duplicates removed?
229 class HistoryURLProviderTestNoDB
: public HistoryURLProviderTest
{
231 void SetUp() override
{
232 ASSERT_TRUE(SetUpImpl(true));
236 class HistoryURLProviderTestNoSearchProvider
: public HistoryURLProviderTest
{
238 void SetUp() override
{
239 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
240 HistoryURLProviderTest::SetUp();
243 void TearDown() override
{
244 HistoryURLProviderTest::TearDown();
245 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
249 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches
) {
250 if (autocomplete_
->done())
251 base::MessageLoop::current()->Quit();
254 bool HistoryURLProviderTest::SetUpImpl(bool no_db
) {
255 profile_
.reset(new TestingProfile());
256 client_
.reset(new ChromeAutocompleteProviderClient(profile_
.get()));
257 if (!(profile_
->CreateHistoryService(true, no_db
)))
260 profile_
->BlockUntilHistoryProcessesPendingRequests();
261 profile_
->BlockUntilHistoryIndexIsRefreshed();
263 profile_
->GetPrefs()->SetString(prefs::kAcceptLanguages
, "en-US,en,ko");
264 history_service_
= HistoryServiceFactory::GetForProfile(
265 profile_
.get(), ServiceAccessType::EXPLICIT_ACCESS
);
267 autocomplete_
= new HistoryURLProvider(client_
.get(), this);
268 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
269 profile_
.get(), &HistoryURLProviderTest::CreateTemplateURLService
);
274 void HistoryURLProviderTest::TearDown() {
275 autocomplete_
= NULL
;
278 void HistoryURLProviderTest::FillData() {
279 // Most visits are a long time ago (some tests require this since we do some
280 // special logic for things visited very recently). Note that this time must
281 // be more recent than the "expire history" threshold for the data to be kept
282 // in the main database.
284 // TODO(brettw) It would be nice if we could test this behavior, in which
285 // case the time would be specifed in the test_db structure.
286 const Time now
= Time::Now();
288 for (size_t i
= 0; i
< arraysize(test_db
); ++i
) {
289 const TestURLInfo
& cur
= test_db
[i
];
290 const GURL
current_url(cur
.url
);
291 history_service_
->AddPageWithDetails(
292 current_url
, base::UTF8ToUTF16(cur
.title
), cur
.visit_count
,
293 cur
.typed_count
, now
- TimeDelta::FromDays(cur
.age_in_days
), false,
294 history::SOURCE_BROWSED
);
297 history_service_
->AddPageWithDetails(
298 GURL("http://p/"), base::UTF8ToUTF16("p"), 0, 0,
300 TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays
- 1),
301 false, history::SOURCE_BROWSED
);
304 void HistoryURLProviderTest::RunTest(
305 const base::string16
& text
,
306 const std::string
& desired_tld
,
307 bool prevent_inline_autocomplete
,
308 const UrlAndLegalDefault
* expected_urls
,
310 metrics::OmniboxInputType::Type
* identified_input_type
) {
311 AutocompleteInput
input(text
, base::string16::npos
, desired_tld
, GURL(),
312 metrics::OmniboxEventProto::INVALID_SPEC
,
313 prevent_inline_autocomplete
, false, true, true, false,
314 ChromeAutocompleteSchemeClassifier(profile_
.get()));
315 *identified_input_type
= input
.type();
316 autocomplete_
->Start(input
, false);
317 if (!autocomplete_
->done())
318 base::MessageLoop::current()->Run();
320 matches_
= autocomplete_
->matches();
322 TemplateURLService
* service
=
323 TemplateURLServiceFactory::GetForProfile(profile_
.get());
324 for (ACMatches::iterator i
= matches_
.begin(); i
!= matches_
.end(); ++i
) {
325 i
->ComputeStrippedDestinationURL(
326 input
, client_
->GetAcceptLanguages(), service
);
328 AutocompleteResult::DedupMatchesByDestination(
329 input
.current_page_classification(), false, &matches_
);
330 std::sort(matches_
.begin(), matches_
.end(),
331 &AutocompleteMatch::MoreRelevant
);
333 ASSERT_EQ(num_results
, matches_
.size()) << "Input text: " << text
334 << "\nTLD: \"" << desired_tld
<< "\"";
335 for (size_t i
= 0; i
< num_results
; ++i
) {
336 EXPECT_EQ(expected_urls
[i
].url
, matches_
[i
].destination_url
.spec());
337 EXPECT_EQ(expected_urls
[i
].allowed_to_be_default_match
,
338 matches_
[i
].allowed_to_be_default_match
);
342 TEST_F(HistoryURLProviderTest
, PromoteShorterURLs
) {
343 // Test that hosts get synthesized below popular pages.
344 const UrlAndLegalDefault expected_nonsynth
[] = {
345 { "http://slashdot.org/favorite_page.html", false },
346 { "http://slashdot.org/", false }
348 RunTest(ASCIIToUTF16("slash"), std::string(), true, expected_nonsynth
,
349 arraysize(expected_nonsynth
));
351 // Test that hosts get synthesized above less popular pages.
352 const UrlAndLegalDefault expected_synth
[] = {
353 { "http://kerneltrap.org/", false },
354 { "http://kerneltrap.org/not_very_popular.html", false }
356 RunTest(ASCIIToUTF16("kernel"), std::string(), true, expected_synth
,
357 arraysize(expected_synth
));
359 // Test that unpopular pages are ignored completely.
360 RunTest(ASCIIToUTF16("fresh"), std::string(), true, NULL
, 0);
362 // Test that if we create or promote shorter suggestions that would not
363 // normally be inline autocompletable, we make them inline autocompletable if
364 // the original suggestion (that we replaced as "top") was inline
366 const UrlAndLegalDefault expected_synthesisa
[] = {
367 { "http://synthesisatest.com/", true },
368 { "http://synthesisatest.com/foo/", true }
370 RunTest(ASCIIToUTF16("synthesisa"), std::string(), false, expected_synthesisa
,
371 arraysize(expected_synthesisa
));
372 EXPECT_LT(matches_
.front().relevance
, 1200);
373 const UrlAndLegalDefault expected_synthesisb
[] = {
374 { "http://synthesisbtest.com/foo/", true },
375 { "http://synthesisbtest.com/foo/bar.html", true }
377 RunTest(ASCIIToUTF16("synthesisb"), std::string(), false, expected_synthesisb
,
378 arraysize(expected_synthesisb
));
379 EXPECT_GE(matches_
.front().relevance
, 1410);
381 // Test that if we have a synthesized host that matches a suggestion, they
382 // get combined into one.
383 const UrlAndLegalDefault expected_combine
[] = {
384 { "http://news.google.com/", false },
385 { "http://news.google.com/?ned=us&topic=n", false },
387 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), std::string(), true,
389 arraysize(expected_combine
)));
390 // The title should also have gotten set properly on the host for the
391 // synthesized one, since it was also in the results.
392 EXPECT_EQ(ASCIIToUTF16("Google News"), matches_
.front().description
);
394 // Test that short URL matching works correctly as the user types more
396 // The entry for foo.com is the best of all five foo.com* entries.
397 const UrlAndLegalDefault short_1
[] = {
398 { "http://foo.com/", false },
399 { "http://foo.com/dir/another/again/myfile.html", false },
400 { "http://foo.com/dir/", false }
402 RunTest(ASCIIToUTF16("foo"), std::string(), true, short_1
,
405 // When the user types the whole host, make sure we don't get two results for
407 const UrlAndLegalDefault short_2
[] = {
408 { "http://foo.com/", true },
409 { "http://foo.com/dir/another/again/myfile.html", false },
410 { "http://foo.com/dir/", false },
411 { "http://foo.com/dir/another/", false }
413 RunTest(ASCIIToUTF16("foo.com"), std::string(), true, short_2
,
415 RunTest(ASCIIToUTF16("foo.com/"), std::string(), true, short_2
,
418 // The filename is the second best of the foo.com* entries, but there is a
419 // shorter URL that's "good enough". The host doesn't match the user input
420 // and so should not appear.
421 const UrlAndLegalDefault short_3
[] = {
422 { "http://foo.com/dir/another/", false },
423 { "http://foo.com/d", true },
424 { "http://foo.com/dir/another/again/myfile.html", false },
425 { "http://foo.com/dir/", false }
427 RunTest(ASCIIToUTF16("foo.com/d"), std::string(), true, short_3
,
429 // If prevent_inline_autocomplete is false, we won't bother creating the
430 // URL-what-you-typed match because we have promoted inline autocompletions.
431 const UrlAndLegalDefault short_3_allow_inline
[] = {
432 { "http://foo.com/dir/another/", true },
433 { "http://foo.com/dir/another/again/myfile.html", true },
434 { "http://foo.com/dir/", true }
436 RunTest(ASCIIToUTF16("foo.com/d"), std::string(), false, short_3_allow_inline
,
437 arraysize(short_3_allow_inline
));
439 // We shouldn't promote shorter URLs than the best if they're not good
441 const UrlAndLegalDefault short_4
[] = {
442 { "http://foo.com/dir/another/again/myfile.html", false },
443 { "http://foo.com/dir/another/a", true },
444 { "http://foo.com/dir/another/again/", false }
446 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), true, short_4
,
448 // If prevent_inline_autocomplete is false, we won't bother creating the
449 // URL-what-you-typed match because we have promoted inline autocompletions.
450 const UrlAndLegalDefault short_4_allow_inline
[] = {
451 { "http://foo.com/dir/another/again/myfile.html", true },
452 { "http://foo.com/dir/another/again/", true }
454 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), false,
455 short_4_allow_inline
, arraysize(short_4_allow_inline
));
457 // Exact matches should always be best no matter how much more another match
459 const UrlAndLegalDefault short_5a
[] = {
460 { "http://gooey/", true },
461 { "http://www.google.com/", true },
462 { "http://go/", true }
464 const UrlAndLegalDefault short_5b
[] = {
465 { "http://go/", true },
466 { "http://gooey/", true },
467 { "http://www.google.com/", true }
469 RunTest(ASCIIToUTF16("g"), std::string(), false, short_5a
,
470 arraysize(short_5a
));
471 RunTest(ASCIIToUTF16("go"), std::string(), false, short_5b
,
472 arraysize(short_5b
));
475 TEST_F(HistoryURLProviderTest
, CullRedirects
) {
476 // URLs we will be using, plus the visit counts they will initially get
477 // (the redirect set below will also increment the visit counts). We want
478 // the results to be in A,B,C order. Note also that our visit counts are
479 // all high enough so that domain synthesizing won't get triggered.
484 {"http://redirects/A", 30},
485 {"http://redirects/B", 20},
486 {"http://redirects/C", 10}
488 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
489 history_service_
->AddPageWithDetails(GURL(test_cases
[i
].url
),
490 ASCIIToUTF16("Title"), test_cases
[i
].count
, test_cases
[i
].count
,
491 Time::Now(), false, history::SOURCE_BROWSED
);
494 // Create a B->C->A redirect chain, but set the visit counts such that they
495 // will appear in A,B,C order in the results. The autocomplete query will
496 // search for the most recent visit when looking for redirects, so this will
497 // be found even though the previous visits had no redirects.
498 history::RedirectList redirects_to_a
;
499 redirects_to_a
.push_back(GURL(test_cases
[1].url
));
500 redirects_to_a
.push_back(GURL(test_cases
[2].url
));
501 redirects_to_a
.push_back(GURL(test_cases
[0].url
));
502 history_service_
->AddPage(GURL(test_cases
[0].url
), base::Time::Now(),
503 NULL
, 0, GURL(), redirects_to_a
, ui::PAGE_TRANSITION_TYPED
,
504 history::SOURCE_BROWSED
, true);
506 // Because all the results are part of a redirect chain with other results,
507 // all but the first one (A) should be culled. We should get the default
508 // "what you typed" result, plus this one.
509 const base::string16
typing(ASCIIToUTF16("http://redirects/"));
510 const UrlAndLegalDefault expected_results
[] = {
511 { test_cases
[0].url
, false },
512 { base::UTF16ToUTF8(typing
), true }
514 RunTest(typing
, std::string(), true, expected_results
,
515 arraysize(expected_results
));
517 // If prevent_inline_autocomplete is false, we won't bother creating the
518 // URL-what-you-typed match because we have promoted inline autocompletions.
519 // The result set should instead consist of a single URL representing the
520 // whole set of redirects.
521 const UrlAndLegalDefault expected_results_allow_inlining
[] = {
522 { test_cases
[0].url
, true }
524 RunTest(typing
, std::string(), false, expected_results_allow_inlining
,
525 arraysize(expected_results_allow_inlining
));
528 TEST_F(HistoryURLProviderTestNoSearchProvider
, WhatYouTypedNoSearchProvider
) {
529 // When no search provider is available, make sure we provide WYT matches
530 // for text that could be a URL.
532 const UrlAndLegalDefault results_1
[] = {
533 { "http://wytmatch/", true }
535 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, results_1
,
536 arraysize(results_1
));
538 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL
, 0);
539 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL
, 0);
541 const UrlAndLegalDefault results_2
[] = {
542 { "http://wytmatch+foo+bar.com/", true }
544 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false,
545 results_2
, arraysize(results_2
));
548 TEST_F(HistoryURLProviderTest
, WhatYouTyped
) {
549 // Make sure we suggest a What You Typed match at the right times.
550 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, NULL
, 0);
551 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL
, 0);
552 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL
, 0);
553 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false, NULL
, 0);
555 const UrlAndLegalDefault results_1
[] = {
556 { "http://www.wytmatch.com/", true }
558 RunTest(ASCIIToUTF16("wytmatch"), "com", false, results_1
,
559 arraysize(results_1
));
561 const UrlAndLegalDefault results_2
[] = {
562 { "http://wytmatch%20foo%20bar/", true }
564 RunTest(ASCIIToUTF16("http://wytmatch foo bar"), std::string(), false,
565 results_2
, arraysize(results_2
));
567 const UrlAndLegalDefault results_3
[] = {
568 { "https://wytmatch%20foo%20bar/", true }
570 RunTest(ASCIIToUTF16("https://wytmatch foo bar"), std::string(), false,
571 results_3
, arraysize(results_3
));
574 TEST_F(HistoryURLProviderTest
, Fixup
) {
575 // Test for various past crashes we've had.
576 RunTest(ASCIIToUTF16("\\"), std::string(), false, NULL
, 0);
577 RunTest(ASCIIToUTF16("#"), std::string(), false, NULL
, 0);
578 RunTest(ASCIIToUTF16("%20"), std::string(), false, NULL
, 0);
579 const UrlAndLegalDefault fixup_crash
[] = {
580 { "http://%EF%BD%A5@s/", true }
582 RunTest(base::WideToUTF16(L
"\uff65@s"), std::string(), false, fixup_crash
,
583 arraysize(fixup_crash
));
584 RunTest(base::WideToUTF16(L
"\u2015\u2015@ \uff7c"), std::string(), false,
587 // Fixing up "file:" should result in an inline autocomplete offset of just
588 // after "file:", not just after "file://".
589 const base::string16
input_1(ASCIIToUTF16("file:"));
590 const UrlAndLegalDefault fixup_1
[] = {
591 { "file:///C:/foo.txt", true }
593 ASSERT_NO_FATAL_FAILURE(RunTest(input_1
, std::string(), false, fixup_1
,
594 arraysize(fixup_1
)));
595 EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
596 matches_
.front().inline_autocompletion
);
598 // Fixing up "http:/" should result in an inline autocomplete offset of just
599 // after "http:/", not just after "http:".
600 const base::string16
input_2(ASCIIToUTF16("http:/"));
601 const UrlAndLegalDefault fixup_2
[] = {
602 { "http://bogussite.com/a", true },
603 { "http://bogussite.com/b", true },
604 { "http://bogussite.com/c", true }
606 ASSERT_NO_FATAL_FAILURE(RunTest(input_2
, std::string(), false, fixup_2
,
607 arraysize(fixup_2
)));
608 EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
609 matches_
.front().inline_autocompletion
);
611 // Adding a TLD to a small number like "56" should result in "www.56.com"
612 // rather than "0.0.0.56.com".
613 const UrlAndLegalDefault fixup_3
[] = {
614 { "http://www.56.com/", true }
616 RunTest(ASCIIToUTF16("56"), "com", true, fixup_3
, arraysize(fixup_3
));
618 // An input looks like a IP address like "127.0.0.1" should result in
619 // "http://127.0.0.1/".
620 const UrlAndLegalDefault fixup_4
[] = {
621 { "http://127.0.0.1/", true }
623 RunTest(ASCIIToUTF16("127.0.0.1"), std::string(), false, fixup_4
,
626 // An number "17173" should result in "http://www.17173.com/" in db.
627 const UrlAndLegalDefault fixup_5
[] = {
628 { "http://www.17173.com/", true }
630 RunTest(ASCIIToUTF16("17173"), std::string(), false, fixup_5
,
634 // Make sure the results for the input 'p' don't change between the first and
636 TEST_F(HistoryURLProviderTest
, EmptyVisits
) {
637 // Wait for history to create the in memory DB.
638 profile_
->BlockUntilHistoryProcessesPendingRequests();
640 AutocompleteInput
input(
641 ASCIIToUTF16("p"), base::string16::npos
, std::string(), GURL(),
642 metrics::OmniboxEventProto::INVALID_SPEC
, false, false, true, true, false,
643 ChromeAutocompleteSchemeClassifier(profile_
.get()));
644 autocomplete_
->Start(input
, false);
645 // HistoryURLProvider shouldn't be done (waiting on async results).
646 EXPECT_FALSE(autocomplete_
->done());
648 // We should get back an entry for pandora.
649 matches_
= autocomplete_
->matches();
650 ASSERT_GT(matches_
.size(), 0u);
651 EXPECT_EQ(GURL("http://pandora.com/"), matches_
[0].destination_url
);
652 int pandora_relevance
= matches_
[0].relevance
;
654 // Run the message loop. When |autocomplete_| finishes the loop is quit.
655 base::MessageLoop::current()->Run();
656 EXPECT_TRUE(autocomplete_
->done());
657 matches_
= autocomplete_
->matches();
658 ASSERT_GT(matches_
.size(), 0u);
659 EXPECT_EQ(GURL("http://pandora.com/"), matches_
[0].destination_url
);
660 EXPECT_EQ(pandora_relevance
, matches_
[0].relevance
);
663 TEST_F(HistoryURLProviderTestNoDB
, NavigateWithoutDB
) {
664 // Ensure that we will still produce matches for navigation when there is no
666 UrlAndLegalDefault navigation_1
[] = {
667 { "http://test.com/", true }
669 RunTest(ASCIIToUTF16("test.com"), std::string(), false, navigation_1
,
670 arraysize(navigation_1
));
672 UrlAndLegalDefault navigation_2
[] = {
673 { "http://slash/", true }
675 RunTest(ASCIIToUTF16("slash"), std::string(), false, navigation_2
,
676 arraysize(navigation_2
));
678 RunTest(ASCIIToUTF16("this is a query"), std::string(), false, NULL
, 0);
681 TEST_F(HistoryURLProviderTest
, DontAutocompleteOnTrailingWhitespace
) {
682 AutocompleteInput
input(
683 ASCIIToUTF16("slash "), base::string16::npos
, std::string(), GURL(),
684 metrics::OmniboxEventProto::INVALID_SPEC
, false, false, true, true, false,
685 ChromeAutocompleteSchemeClassifier(profile_
.get()));
686 autocomplete_
->Start(input
, false);
687 if (!autocomplete_
->done())
688 base::MessageLoop::current()->Run();
690 // None of the matches should attempt to autocomplete.
691 matches_
= autocomplete_
->matches();
692 for (size_t i
= 0; i
< matches_
.size(); ++i
) {
693 EXPECT_TRUE(matches_
[i
].inline_autocompletion
.empty());
694 EXPECT_FALSE(matches_
[i
].allowed_to_be_default_match
);
698 TEST_F(HistoryURLProviderTest
, TreatEmailsAsSearches
) {
699 // Visiting foo.com should not make this string be treated as a navigation.
700 // That means the result should be scored around 1200 ("what you typed")
702 const UrlAndLegalDefault expected
[] = {
703 { "http://user@foo.com/", true }
705 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), std::string(),
706 false, expected
, arraysize(expected
)));
707 EXPECT_LE(1200, matches_
[0].relevance
);
708 EXPECT_LT(matches_
[0].relevance
, 1210);
711 TEST_F(HistoryURLProviderTest
, IntranetURLsWithPaths
) {
717 { "fooey/", 1200 }, // 1200 for URL would still navigate by default.
718 { "fooey/a", 1200 }, // 1200 for UNKNOWN would not.
719 { "fooey/a b", 1200 }, // Also UNKNOWN.
723 { "gooey/a b", 1400 },
725 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
726 SCOPED_TRACE(test_cases
[i
].input
);
727 if (test_cases
[i
].relevance
== 0) {
728 RunTest(ASCIIToUTF16(test_cases
[i
].input
), std::string(), false, NULL
, 0);
730 const UrlAndLegalDefault output
[] = {
731 {url_fixer::FixupURL(test_cases
[i
].input
, std::string()).spec(),
733 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
734 std::string(), false, output
, arraysize(output
)));
735 // Actual relevance should be at least what test_cases expects and
736 // and no more than 10 more.
737 EXPECT_LE(test_cases
[i
].relevance
, matches_
[0].relevance
);
738 EXPECT_LT(matches_
[0].relevance
, test_cases
[i
].relevance
+ 10);
743 // Makes sure autocompletion happens for intranet sites that have been
744 // previoulsy visited.
745 TEST_F(HistoryURLProviderTest
, IntranetURLCompletion
) {
746 sort_matches_
= true;
748 const UrlAndLegalDefault expected1
[] = {
749 { "http://intra/three", true },
750 { "http://intra/two", true }
752 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), std::string(), false,
753 expected1
, arraysize(expected1
)));
754 EXPECT_LE(1410, matches_
[0].relevance
);
755 EXPECT_LT(matches_
[0].relevance
, 1420);
756 EXPECT_EQ(matches_
[0].relevance
- 1, matches_
[1].relevance
);
758 const UrlAndLegalDefault expected2
[] = {
759 { "http://moo/b", true },
760 { "http://moo/bar", true }
762 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), std::string(), false,
763 expected2
, arraysize(expected2
)));
764 // The url what you typed match should be around 1400, otherwise the
765 // search what you typed match is going to be first.
766 EXPECT_LE(1400, matches_
[0].relevance
);
767 EXPECT_LT(matches_
[0].relevance
, 1410);
769 const UrlAndLegalDefault expected3
[] = {
770 { "http://intra/one", true },
771 { "http://intra/three", true },
772 { "http://intra/two", true }
774 RunTest(ASCIIToUTF16("intra"), std::string(), false, expected3
,
775 arraysize(expected3
));
777 const UrlAndLegalDefault expected4
[] = {
778 { "http://intra/one", true },
779 { "http://intra/three", true },
780 { "http://intra/two", true }
782 RunTest(ASCIIToUTF16("intra/"), std::string(), false, expected4
,
783 arraysize(expected4
));
785 const UrlAndLegalDefault expected5
[] = {
786 { "http://intra/one", true }
788 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), std::string(), false,
789 expected5
, arraysize(expected5
)));
790 EXPECT_LE(1410, matches_
[0].relevance
);
791 EXPECT_LT(matches_
[0].relevance
, 1420);
793 const UrlAndLegalDefault expected6
[] = {
794 { "http://intra/x", true }
796 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), std::string(), false,
797 expected6
, arraysize(expected6
)));
798 EXPECT_LE(1400, matches_
[0].relevance
);
799 EXPECT_LT(matches_
[0].relevance
, 1410);
801 const UrlAndLegalDefault expected7
[] = {
802 { "http://typedhost/untypedpath", true }
804 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
805 std::string(), false, expected7
,
806 arraysize(expected7
)));
807 EXPECT_LE(1400, matches_
[0].relevance
);
808 EXPECT_LT(matches_
[0].relevance
, 1410);
811 TEST_F(HistoryURLProviderTest
, CrashDueToFixup
) {
812 // This test passes if we don't crash. The results don't matter.
813 const char* const test_cases
[] = {
818 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
819 AutocompleteInput
input(
820 ASCIIToUTF16(test_cases
[i
]), base::string16::npos
, std::string(),
821 GURL(), metrics::OmniboxEventProto::INVALID_SPEC
, false, false, true,
822 true, false, ChromeAutocompleteSchemeClassifier(profile_
.get()));
823 autocomplete_
->Start(input
, false);
824 if (!autocomplete_
->done())
825 base::MessageLoop::current()->Run();
829 TEST_F(HistoryURLProviderTest
, DoesNotProvideMatchesOnFocus
) {
830 AutocompleteInput
input(
831 ASCIIToUTF16("foo"), base::string16::npos
, std::string(), GURL(),
832 metrics::OmniboxEventProto::INVALID_SPEC
, false, false, true, true, true,
833 ChromeAutocompleteSchemeClassifier(profile_
.get()));
834 autocomplete_
->Start(input
, false);
835 EXPECT_TRUE(autocomplete_
->matches().empty());
838 TEST_F(HistoryURLProviderTest
, CullSearchResults
) {
839 // Set up a default search engine.
840 TemplateURLData data
;
841 data
.SetShortName(ASCIIToUTF16("TestEngine"));
842 data
.SetKeyword(ASCIIToUTF16("TestEngine"));
843 data
.SetURL("http://testsearch.com/?q={searchTerms}");
844 TemplateURLService
* template_url_service
=
845 TemplateURLServiceFactory::GetForProfile(profile_
.get());
846 TemplateURL
* template_url
= new TemplateURL(data
);
847 template_url_service
->Add(template_url
);
848 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
849 template_url_service
->Load();
851 // URLs we will be using, plus the visit counts they will initially get
852 // (the redirect set below will also increment the visit counts). We want
853 // the results to be in A,B,C order. Note also that our visit counts are
854 // all high enough so that domain synthesizing won't get triggered.
859 {"https://testsearch.com/", 30},
860 {"https://testsearch.com/?q=foobar", 20},
861 {"http://foobar.com/", 10}
863 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
864 history_service_
->AddPageWithDetails(GURL(test_cases
[i
].url
),
865 base::UTF8ToUTF16("Title"), test_cases
[i
].count
, test_cases
[i
].count
,
866 Time::Now(), false, history::SOURCE_BROWSED
);
869 // We should not see search URLs when typing a previously used query.
870 const UrlAndLegalDefault expected_when_searching_query
[] = {
871 { test_cases
[2].url
, false }
873 RunTest(ASCIIToUTF16("foobar"), std::string(), true,
874 expected_when_searching_query
, arraysize(expected_when_searching_query
));
876 // We should not see search URLs when typing the search engine name.
877 const UrlAndLegalDefault expected_when_searching_site
[] = {
878 { test_cases
[0].url
, false }
880 RunTest(ASCIIToUTF16("testsearch"), std::string(), true,
881 expected_when_searching_site
, arraysize(expected_when_searching_site
));
884 TEST_F(HistoryURLProviderTest
, SuggestExactInput
) {
885 const size_t npos
= std::string::npos
;
891 const char* contents
;
892 // Offsets of the ACMatchClassifications, terminated by npos.
894 // The index of the ACMatchClassification that should have the MATCH bit
895 // set, npos if no ACMatchClassification should have the MATCH bit set.
896 size_t match_classification_index
;
898 { "http://www.somesite.com", false,
899 "http://www.somesite.com", {0, npos
, npos
}, 0 },
900 { "www.somesite.com", true,
901 "www.somesite.com", {0, npos
, npos
}, 0 },
902 { "www.somesite.com", false,
903 "http://www.somesite.com", {0, 7, npos
}, 1 },
904 { "somesite.com", true,
905 "somesite.com", {0, npos
, npos
}, 0 },
906 { "somesite.com", false,
907 "http://somesite.com", {0, 7, npos
}, 1 },
909 "w", {0, npos
, npos
}, 0 },
911 "http://w", {0, 7, npos
}, 1 },
913 "w.com", {0, npos
, npos
}, 0 },
915 "http://w.com", {0, 7, npos
}, 1 },
917 "www.w.com", {0, npos
, npos
}, 0 },
918 { "www.w.com", false,
919 "http://www.w.com", {0, 7, npos
}, 1 },
920 { "view-source:w", true,
921 "view-source:w", {0, npos
, npos
}, 0 },
922 { "view-source:www.w.com/", true,
923 "view-source:www.w.com", {0, npos
, npos
}, npos
},
924 { "view-source:www.w.com/", false,
925 "view-source:http://www.w.com", {0, npos
, npos
}, npos
},
926 { "view-source:http://www.w.com/", false,
927 "view-source:http://www.w.com", {0, npos
, npos
}, 0 },
928 { " view-source:", true,
929 "view-source:", {0, npos
, npos
}, 0 },
930 { "http:////////w.com", false,
931 "http://w.com", {0, npos
, npos
}, npos
},
932 { " http:////////www.w.com", false,
933 "http://www.w.com", {0, npos
, npos
}, npos
},
934 { "http:a///www.w.com", false,
935 "http://a///www.w.com", {0, npos
, npos
}, npos
},
936 { "mailto://a@b.com", true,
937 "mailto://a@b.com", {0, npos
, npos
}, 0 },
938 { "mailto://a@b.com", false,
939 "mailto://a@b.com", {0, npos
, npos
}, 0 },
941 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
942 SCOPED_TRACE(testing::Message() << "Index " << i
<< " input: "
943 << test_cases
[i
].input
<< ", trim_http: "
944 << test_cases
[i
].trim_http
);
946 AutocompleteInput
input(
947 ASCIIToUTF16(test_cases
[i
].input
), base::string16::npos
, std::string(),
948 GURL("about:blank"), metrics::OmniboxEventProto::INVALID_SPEC
, false,
949 false, true, true, false,
950 ChromeAutocompleteSchemeClassifier(profile_
.get()));
951 AutocompleteMatch
match(autocomplete_
->SuggestExactInput(
952 input
, input
.canonicalized_url(), test_cases
[i
].trim_http
));
953 EXPECT_EQ(ASCIIToUTF16(test_cases
[i
].contents
), match
.contents
);
954 for (size_t match_index
= 0; match_index
< match
.contents_class
.size();
956 EXPECT_EQ(test_cases
[i
].offsets
[match_index
],
957 match
.contents_class
[match_index
].offset
);
958 EXPECT_EQ(ACMatchClassification::URL
|
959 (match_index
== test_cases
[i
].match_classification_index
?
960 ACMatchClassification::MATCH
: 0),
961 match
.contents_class
[match_index
].style
);
963 EXPECT_EQ(npos
, test_cases
[i
].offsets
[match
.contents_class
.size()]);
967 TEST_F(HistoryURLProviderTest
, HUPScoringExperiment
) {
968 HUPScoringParams max_2000_no_time_decay
;
969 max_2000_no_time_decay
.typed_count_buckets
.buckets().push_back(
970 std::make_pair(0.0, 2000));
971 HUPScoringParams max_1250_no_time_decay
;
972 max_1250_no_time_decay
.typed_count_buckets
.buckets().push_back(
973 std::make_pair(0.0, 1250));
974 HUPScoringParams max_1000_no_time_decay
;
975 max_1000_no_time_decay
.typed_count_buckets
.buckets().push_back(
976 std::make_pair(0.0, 1000));
978 HUPScoringParams max_1100_with_time_decay_and_max_cap
;
979 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.
980 set_relevance_cap(1400);
981 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.
982 set_half_life_days(16);
983 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
984 std::make_pair(0.5, 1100));
985 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
986 std::make_pair(0.24, 200));
987 max_1100_with_time_decay_and_max_cap
.typed_count_buckets
.buckets().push_back(
988 std::make_pair(0.0, 100));
990 HUPScoringParams max_1100_visit_typed_decays
;
991 max_1100_visit_typed_decays
.typed_count_buckets
.set_half_life_days(16);
992 max_1100_visit_typed_decays
.typed_count_buckets
.buckets().push_back(
993 std::make_pair(0.5, 1100));
994 max_1100_visit_typed_decays
.typed_count_buckets
.buckets().push_back(
995 std::make_pair(0.0, 100));
996 max_1100_visit_typed_decays
.visited_count_buckets
.set_half_life_days(16);
997 max_1100_visit_typed_decays
.visited_count_buckets
.buckets().push_back(
998 std::make_pair(0.5, 550));
999 max_1100_visit_typed_decays
.visited_count_buckets
.buckets().push_back(
1000 std::make_pair(0.0, 50));
1002 const int kMaxMatches
= 3;
1005 HUPScoringParams scoring_params
;
1006 struct ExpectedMatch
{
1008 int control_relevance
;
1009 int experiment_relevance
;
1011 ExpectedMatch matches
[kMaxMatches
];
1013 // Max score 2000 -> no demotion.
1014 { "7.com/1", max_2000_no_time_decay
,
1015 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1017 // Limit score to 1250/1000 and make sure that the top match is unchanged.
1018 { "7.com/1", max_1250_no_time_decay
,
1019 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1020 { "7.com/2", max_1250_no_time_decay
,
1021 {{"7.com/2a", 1413, 1413}, {"7.com/2b", 1412, 1250}, {NULL
, 0, 0}} },
1022 { "7.com/4", max_1000_no_time_decay
,
1023 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 1000},
1024 {"7.com/4b", 1201, 999}} },
1026 // Max relevance cap is 1400 and half-life is 16 days.
1027 { "7.com/1", max_1100_with_time_decay_and_max_cap
,
1028 {{"7.com/1a", 1413, 1413}, {NULL
, 0, 0}, {NULL
, 0, 0}} },
1029 { "7.com/4", max_1100_with_time_decay_and_max_cap
,
1030 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 200},
1031 {"7.com/4b", 1201, 100}} },
1033 // Max relevance cap is 1400 and half-life is 16 days for both visit/typed.
1034 { "7.com/5", max_1100_visit_typed_decays
,
1035 {{"7.com/5", 1203, 1203}, {"7.com/5a", 1202, 50}, {NULL
, 0, 0}} },
1037 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
1038 SCOPED_TRACE(test_cases
[i
].input
);
1039 UrlAndLegalDefault output
[kMaxMatches
];
1041 for (max_matches
= 0; max_matches
< kMaxMatches
; ++max_matches
) {
1042 if (test_cases
[i
].matches
[max_matches
].url
== NULL
)
1044 output
[max_matches
].url
=
1045 url_fixer::FixupURL(test_cases
[i
].matches
[max_matches
].url
,
1046 std::string()).spec();
1047 output
[max_matches
].allowed_to_be_default_match
= true;
1049 autocomplete_
->scoring_params_
= test_cases
[i
].scoring_params
;
1051 // Test the control (scoring disabled).
1052 autocomplete_
->scoring_params_
.experimental_scoring_enabled
= false;
1053 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
1054 std::string(), false, output
, max_matches
));
1055 for (int j
= 0; j
< max_matches
; ++j
) {
1056 EXPECT_EQ(test_cases
[i
].matches
[j
].control_relevance
,
1057 matches_
[j
].relevance
);
1060 // Test the experiment (scoring enabled).
1061 autocomplete_
->scoring_params_
.experimental_scoring_enabled
= true;
1062 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases
[i
].input
),
1063 std::string(), false, output
, max_matches
));
1064 for (int j
= 0; j
< max_matches
; ++j
) {
1065 EXPECT_EQ(test_cases
[i
].matches
[j
].experiment_relevance
,
1066 matches_
[j
].relevance
);