Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / autocomplete / history_url_provider_unittest.cc
blob38810aa3c553286e685f561248352eb79f7bf137
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"
7 #include <algorithm>
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;
39 using base::Time;
40 using base::TimeDelta;
42 using content::TestBrowserThreadBundle;
44 struct TestURLInfo {
45 const char* url;
46 const char* title;
47 int visit_count;
48 int typed_count;
49 int age_in_days;
50 } test_db[] = {
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
57 // matches.
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 {
154 public:
155 struct UrlAndLegalDefault {
156 std::string url;
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;
172 protected:
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());
184 // testing::Test
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.
194 void FillData();
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,
203 size_t num_results,
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_;
218 ACMatches matches_;
219 scoped_ptr<TestingProfile> profile_;
220 history::HistoryService* history_service_;
221 scoped_refptr<HistoryURLProvider> autocomplete_;
222 // Should the matches be sorted and duplicates removed?
223 bool sort_matches_;
226 class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
227 protected:
228 void SetUp() override {
229 ASSERT_TRUE(SetUpImpl(true));
233 class HistoryURLProviderTestNoSearchProvider : public HistoryURLProviderTest {
234 protected:
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)))
254 return false;
255 if (!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);
266 FillData();
267 return true;
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,
295 Time::Now() -
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,
305 size_t num_results,
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();
317 if (sort_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
359 // autocompletable.
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,
382 expected_combine,
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
389 // (several tests):
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,
397 arraysize(short_1));
399 // When the user types the whole host, make sure we don't get two results for
400 // it.
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,
408 arraysize(short_2));
409 RunTest(ASCIIToUTF16("foo.com/"), std::string(), true, short_2,
410 arraysize(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/d", true },
417 { "http://foo.com/dir/another/", false },
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,
422 arraysize(short_3));
424 // We shouldn't promote shorter URLs than the best if they're not good
425 // enough.
426 const UrlAndLegalDefault short_4[] = {
427 { "http://foo.com/dir/another/a", true },
428 { "http://foo.com/dir/another/again/myfile.html", false },
429 { "http://foo.com/dir/another/again/", false }
431 RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), true, short_4,
432 arraysize(short_4));
434 // Exact matches should always be best no matter how much more another match
435 // has been typed.
436 const UrlAndLegalDefault short_5a[] = {
437 { "http://gooey/", true },
438 { "http://www.google.com/", true },
439 { "http://go/", true }
441 const UrlAndLegalDefault short_5b[] = {
442 { "http://go/", true },
443 { "http://gooey/", true },
444 { "http://www.google.com/", true }
446 RunTest(ASCIIToUTF16("g"), std::string(), false, short_5a,
447 arraysize(short_5a));
448 RunTest(ASCIIToUTF16("go"), std::string(), false, short_5b,
449 arraysize(short_5b));
452 TEST_F(HistoryURLProviderTest, CullRedirects) {
453 // URLs we will be using, plus the visit counts they will initially get
454 // (the redirect set below will also increment the visit counts). We want
455 // the results to be in A,B,C order. Note also that our visit counts are
456 // all high enough so that domain synthesizing won't get triggered.
457 struct TestCase {
458 const char* url;
459 int count;
460 } test_cases[] = {
461 {"http://redirects/A", 30},
462 {"http://redirects/B", 20},
463 {"http://redirects/C", 10}
465 for (size_t i = 0; i < arraysize(test_cases); ++i) {
466 history_service_->AddPageWithDetails(GURL(test_cases[i].url),
467 ASCIIToUTF16("Title"), test_cases[i].count, test_cases[i].count,
468 Time::Now(), false, history::SOURCE_BROWSED);
471 // Create a B->C->A redirect chain, but set the visit counts such that they
472 // will appear in A,B,C order in the results. The autocomplete query will
473 // search for the most recent visit when looking for redirects, so this will
474 // be found even though the previous visits had no redirects.
475 history::RedirectList redirects_to_a;
476 redirects_to_a.push_back(GURL(test_cases[1].url));
477 redirects_to_a.push_back(GURL(test_cases[2].url));
478 redirects_to_a.push_back(GURL(test_cases[0].url));
479 history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(),
480 NULL, 0, GURL(), redirects_to_a, ui::PAGE_TRANSITION_TYPED,
481 history::SOURCE_BROWSED, true);
483 // Because all the results are part of a redirect chain with other results,
484 // all but the first one (A) should be culled. We should get the default
485 // "what you typed" result, plus this one.
486 const base::string16 typing(ASCIIToUTF16("http://redirects/"));
487 const UrlAndLegalDefault expected_results[] = {
488 { base::UTF16ToUTF8(typing), true },
489 { test_cases[0].url, false }
491 RunTest(typing, std::string(), true, expected_results,
492 arraysize(expected_results));
495 TEST_F(HistoryURLProviderTestNoSearchProvider, WhatYouTypedNoSearchProvider) {
496 // When no search provider is available, make sure we provide WYT matches
497 // for text that could be a URL.
499 const UrlAndLegalDefault results_1[] = {
500 { "http://wytmatch/", true }
502 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, results_1,
503 arraysize(results_1));
505 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL, 0);
506 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL, 0);
508 const UrlAndLegalDefault results_2[] = {
509 { "http://wytmatch+foo+bar.com/", true }
511 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false,
512 results_2, arraysize(results_2));
515 TEST_F(HistoryURLProviderTest, WhatYouTyped) {
516 // Make sure we suggest a What You Typed match at the right times.
517 RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, NULL, 0);
518 RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL, 0);
519 RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL, 0);
520 RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false, NULL, 0);
522 const UrlAndLegalDefault results_1[] = {
523 { "http://www.wytmatch.com/", true }
525 RunTest(ASCIIToUTF16("wytmatch"), "com", false, results_1,
526 arraysize(results_1));
528 const UrlAndLegalDefault results_2[] = {
529 { "http://wytmatch%20foo%20bar/", true }
531 RunTest(ASCIIToUTF16("http://wytmatch foo bar"), std::string(), false,
532 results_2, arraysize(results_2));
534 const UrlAndLegalDefault results_3[] = {
535 { "https://wytmatch%20foo%20bar/", true }
537 RunTest(ASCIIToUTF16("https://wytmatch foo bar"), std::string(), false,
538 results_3, arraysize(results_3));
541 TEST_F(HistoryURLProviderTest, Fixup) {
542 // Test for various past crashes we've had.
543 RunTest(ASCIIToUTF16("\\"), std::string(), false, NULL, 0);
544 RunTest(ASCIIToUTF16("#"), std::string(), false, NULL, 0);
545 RunTest(ASCIIToUTF16("%20"), std::string(), false, NULL, 0);
546 const UrlAndLegalDefault fixup_crash[] = {
547 { "http://%EF%BD%A5@s/", true }
549 RunTest(base::WideToUTF16(L"\uff65@s"), std::string(), false, fixup_crash,
550 arraysize(fixup_crash));
551 RunTest(base::WideToUTF16(L"\u2015\u2015@ \uff7c"), std::string(), false,
552 NULL, 0);
554 // Fixing up "file:" should result in an inline autocomplete offset of just
555 // after "file:", not just after "file://".
556 const base::string16 input_1(ASCIIToUTF16("file:"));
557 const UrlAndLegalDefault fixup_1[] = {
558 { "file:///C:/foo.txt", true }
560 ASSERT_NO_FATAL_FAILURE(RunTest(input_1, std::string(), false, fixup_1,
561 arraysize(fixup_1)));
562 EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
563 matches_.front().inline_autocompletion);
565 // Fixing up "http:/" should result in an inline autocomplete offset of just
566 // after "http:/", not just after "http:".
567 const base::string16 input_2(ASCIIToUTF16("http:/"));
568 const UrlAndLegalDefault fixup_2[] = {
569 { "http://bogussite.com/a", true },
570 { "http://bogussite.com/b", true },
571 { "http://bogussite.com/c", true }
573 ASSERT_NO_FATAL_FAILURE(RunTest(input_2, std::string(), false, fixup_2,
574 arraysize(fixup_2)));
575 EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
576 matches_.front().inline_autocompletion);
578 // Adding a TLD to a small number like "56" should result in "www.56.com"
579 // rather than "0.0.0.56.com".
580 const UrlAndLegalDefault fixup_3[] = {
581 { "http://www.56.com/", true }
583 RunTest(ASCIIToUTF16("56"), "com", true, fixup_3, arraysize(fixup_3));
585 // An input looks like a IP address like "127.0.0.1" should result in
586 // "http://127.0.0.1/".
587 const UrlAndLegalDefault fixup_4[] = {
588 { "http://127.0.0.1/", true }
590 RunTest(ASCIIToUTF16("127.0.0.1"), std::string(), false, fixup_4,
591 arraysize(fixup_4));
593 // An number "17173" should result in "http://www.17173.com/" in db.
594 const UrlAndLegalDefault fixup_5[] = {
595 { "http://www.17173.com/", true }
597 RunTest(ASCIIToUTF16("17173"), std::string(), false, fixup_5,
598 arraysize(fixup_5));
601 // Make sure the results for the input 'p' don't change between the first and
602 // second passes.
603 TEST_F(HistoryURLProviderTest, EmptyVisits) {
604 // Wait for history to create the in memory DB.
605 profile_->BlockUntilHistoryProcessesPendingRequests();
607 AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos,
608 std::string(), GURL(),
609 metrics::OmniboxEventProto::INVALID_SPEC, false,
610 false, true, true,
611 ChromeAutocompleteSchemeClassifier(profile_.get()));
612 autocomplete_->Start(input, false, false);
613 // HistoryURLProvider shouldn't be done (waiting on async results).
614 EXPECT_FALSE(autocomplete_->done());
616 // We should get back an entry for pandora.
617 matches_ = autocomplete_->matches();
618 ASSERT_GT(matches_.size(), 0u);
619 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
620 int pandora_relevance = matches_[0].relevance;
622 // Run the message loop. When |autocomplete_| finishes the loop is quit.
623 base::MessageLoop::current()->Run();
624 EXPECT_TRUE(autocomplete_->done());
625 matches_ = autocomplete_->matches();
626 ASSERT_GT(matches_.size(), 0u);
627 EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
628 EXPECT_EQ(pandora_relevance, matches_[0].relevance);
631 TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) {
632 // Ensure that we will still produce matches for navigation when there is no
633 // database.
634 UrlAndLegalDefault navigation_1[] = {
635 { "http://test.com/", true }
637 RunTest(ASCIIToUTF16("test.com"), std::string(), false, navigation_1,
638 arraysize(navigation_1));
640 UrlAndLegalDefault navigation_2[] = {
641 { "http://slash/", true }
643 RunTest(ASCIIToUTF16("slash"), std::string(), false, navigation_2,
644 arraysize(navigation_2));
646 RunTest(ASCIIToUTF16("this is a query"), std::string(), false, NULL, 0);
649 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) {
650 AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos,
651 std::string(), GURL(),
652 metrics::OmniboxEventProto::INVALID_SPEC, false,
653 false, true, true,
654 ChromeAutocompleteSchemeClassifier(profile_.get()));
655 autocomplete_->Start(input, false, false);
656 if (!autocomplete_->done())
657 base::MessageLoop::current()->Run();
659 // None of the matches should attempt to autocomplete.
660 matches_ = autocomplete_->matches();
661 for (size_t i = 0; i < matches_.size(); ++i) {
662 EXPECT_TRUE(matches_[i].inline_autocompletion.empty());
663 EXPECT_FALSE(matches_[i].allowed_to_be_default_match);
667 TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) {
668 // Visiting foo.com should not make this string be treated as a navigation.
669 // That means the result should be scored around 1200 ("what you typed")
670 // and not 1400+.
671 const UrlAndLegalDefault expected[] = {
672 { "http://user@foo.com/", true }
674 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), std::string(),
675 false, expected, arraysize(expected)));
676 EXPECT_LE(1200, matches_[0].relevance);
677 EXPECT_LT(matches_[0].relevance, 1210);
680 TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) {
681 struct TestCase {
682 const char* input;
683 int relevance;
684 } test_cases[] = {
685 { "fooey", 0 },
686 { "fooey/", 1200 }, // 1200 for URL would still navigate by default.
687 { "fooey/a", 1200 }, // 1200 for UNKNOWN would not.
688 { "fooey/a b", 1200 }, // Also UNKNOWN.
689 { "gooey", 1410 },
690 { "gooey/", 1410 },
691 { "gooey/a", 1400 },
692 { "gooey/a b", 1400 },
694 for (size_t i = 0; i < arraysize(test_cases); ++i) {
695 SCOPED_TRACE(test_cases[i].input);
696 if (test_cases[i].relevance == 0) {
697 RunTest(ASCIIToUTF16(test_cases[i].input), std::string(), false, NULL, 0);
698 } else {
699 const UrlAndLegalDefault output[] = {
700 {url_fixer::FixupURL(test_cases[i].input, std::string()).spec(),
701 true}};
702 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
703 std::string(), false, output, arraysize(output)));
704 // Actual relevance should be at least what test_cases expects and
705 // and no more than 10 more.
706 EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
707 EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
712 // Makes sure autocompletion happens for intranet sites that have been
713 // previoulsy visited.
714 TEST_F(HistoryURLProviderTest, IntranetURLCompletion) {
715 sort_matches_ = true;
717 const UrlAndLegalDefault expected1[] = {
718 { "http://intra/three", true },
719 { "http://intra/two", true }
721 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), std::string(), false,
722 expected1, arraysize(expected1)));
723 EXPECT_LE(1410, matches_[0].relevance);
724 EXPECT_LT(matches_[0].relevance, 1420);
725 EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance);
727 const UrlAndLegalDefault expected2[] = {
728 { "http://moo/b", true },
729 { "http://moo/bar", true }
731 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), std::string(), false,
732 expected2, arraysize(expected2)));
733 // The url what you typed match should be around 1400, otherwise the
734 // search what you typed match is going to be first.
735 EXPECT_LE(1400, matches_[0].relevance);
736 EXPECT_LT(matches_[0].relevance, 1410);
738 const UrlAndLegalDefault expected3[] = {
739 { "http://intra/one", true },
740 { "http://intra/three", true },
741 { "http://intra/two", true }
743 RunTest(ASCIIToUTF16("intra"), std::string(), false, expected3,
744 arraysize(expected3));
746 const UrlAndLegalDefault expected4[] = {
747 { "http://intra/one", true },
748 { "http://intra/three", true },
749 { "http://intra/two", true }
751 RunTest(ASCIIToUTF16("intra/"), std::string(), false, expected4,
752 arraysize(expected4));
754 const UrlAndLegalDefault expected5[] = {
755 { "http://intra/one", true }
757 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), std::string(), false,
758 expected5, arraysize(expected5)));
759 EXPECT_LE(1410, matches_[0].relevance);
760 EXPECT_LT(matches_[0].relevance, 1420);
762 const UrlAndLegalDefault expected6[] = {
763 { "http://intra/x", true }
765 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), std::string(), false,
766 expected6, arraysize(expected6)));
767 EXPECT_LE(1400, matches_[0].relevance);
768 EXPECT_LT(matches_[0].relevance, 1410);
770 const UrlAndLegalDefault expected7[] = {
771 { "http://typedhost/untypedpath", true }
773 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
774 std::string(), false, expected7,
775 arraysize(expected7)));
776 EXPECT_LE(1400, matches_[0].relevance);
777 EXPECT_LT(matches_[0].relevance, 1410);
780 TEST_F(HistoryURLProviderTest, CrashDueToFixup) {
781 // This test passes if we don't crash. The results don't matter.
782 const char* const test_cases[] = {
783 "//c",
784 "\\@st",
785 "view-source:x",
787 for (size_t i = 0; i < arraysize(test_cases); ++i) {
788 AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos,
789 std::string(), GURL(),
790 metrics::OmniboxEventProto::INVALID_SPEC,
791 false, false, true, true,
792 ChromeAutocompleteSchemeClassifier(profile_.get()));
793 autocomplete_->Start(input, false, false);
794 if (!autocomplete_->done())
795 base::MessageLoop::current()->Run();
799 TEST_F(HistoryURLProviderTest, DoesNotProvideMatchesOnFocus) {
800 AutocompleteInput input(ASCIIToUTF16("foo"), base::string16::npos,
801 std::string(), GURL(),
802 metrics::OmniboxEventProto::INVALID_SPEC,
803 false, false, true, true,
804 ChromeAutocompleteSchemeClassifier(profile_.get()));
805 autocomplete_->Start(input, false, true);
806 EXPECT_TRUE(autocomplete_->matches().empty());
809 TEST_F(HistoryURLProviderTest, CullSearchResults) {
810 // Set up a default search engine.
811 TemplateURLData data;
812 data.SetKeyword(ASCIIToUTF16("TestEngine"));
813 data.SetURL("http://testsearch.com/?q={searchTerms}");
814 TemplateURLService* template_url_service =
815 TemplateURLServiceFactory::GetForProfile(profile_.get());
816 TemplateURL* template_url = new TemplateURL(data);
817 template_url_service->Add(template_url);
818 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
819 template_url_service->Load();
821 // URLs we will be using, plus the visit counts they will initially get
822 // (the redirect set below will also increment the visit counts). We want
823 // the results to be in A,B,C order. Note also that our visit counts are
824 // all high enough so that domain synthesizing won't get triggered.
825 struct TestCase {
826 const char* url;
827 int count;
828 } test_cases[] = {
829 {"https://testsearch.com/", 30},
830 {"https://testsearch.com/?q=foobar", 20},
831 {"http://foobar.com/", 10}
833 for (size_t i = 0; i < arraysize(test_cases); ++i) {
834 history_service_->AddPageWithDetails(GURL(test_cases[i].url),
835 base::UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
836 Time::Now(), false, history::SOURCE_BROWSED);
839 // We should not see search URLs when typing a previously used query.
840 const UrlAndLegalDefault expected_when_searching_query[] = {
841 { test_cases[2].url, false }
843 RunTest(ASCIIToUTF16("foobar"), std::string(), true,
844 expected_when_searching_query, arraysize(expected_when_searching_query));
846 // We should not see search URLs when typing the search engine name.
847 const UrlAndLegalDefault expected_when_searching_site[] = {
848 { test_cases[0].url, false }
850 RunTest(ASCIIToUTF16("testsearch"), std::string(), true,
851 expected_when_searching_site, arraysize(expected_when_searching_site));
854 TEST_F(HistoryURLProviderTest, SuggestExactInput) {
855 const size_t npos = std::string::npos;
856 struct TestCase {
857 // Inputs:
858 const char* input;
859 bool trim_http;
860 // Expected Outputs:
861 const char* contents;
862 // Offsets of the ACMatchClassifications, terminated by npos.
863 size_t offsets[3];
864 // The index of the ACMatchClassification that should have the MATCH bit
865 // set, npos if no ACMatchClassification should have the MATCH bit set.
866 size_t match_classification_index;
867 } test_cases[] = {
868 { "http://www.somesite.com", false,
869 "http://www.somesite.com", {0, npos, npos}, 0 },
870 { "www.somesite.com", true,
871 "www.somesite.com", {0, npos, npos}, 0 },
872 { "www.somesite.com", false,
873 "http://www.somesite.com", {0, 7, npos}, 1 },
874 { "somesite.com", true,
875 "somesite.com", {0, npos, npos}, 0 },
876 { "somesite.com", false,
877 "http://somesite.com", {0, 7, npos}, 1 },
878 { "w", true,
879 "w", {0, npos, npos}, 0 },
880 { "w", false,
881 "http://w", {0, 7, npos}, 1 },
882 { "w.com", true,
883 "w.com", {0, npos, npos}, 0 },
884 { "w.com", false,
885 "http://w.com", {0, 7, npos}, 1 },
886 { "www.w.com", true,
887 "www.w.com", {0, npos, npos}, 0 },
888 { "www.w.com", false,
889 "http://www.w.com", {0, 7, npos}, 1 },
890 { "view-source:w", true,
891 "view-source:w", {0, npos, npos}, 0 },
892 { "view-source:www.w.com/", true,
893 "view-source:www.w.com", {0, npos, npos}, npos },
894 { "view-source:www.w.com/", false,
895 "view-source:http://www.w.com", {0, npos, npos}, npos },
896 { "view-source:http://www.w.com/", false,
897 "view-source:http://www.w.com", {0, npos, npos}, 0 },
898 { " view-source:", true,
899 "view-source:", {0, npos, npos}, 0 },
900 { "http:////////w.com", false,
901 "http://w.com", {0, npos, npos}, npos },
902 { " http:////////www.w.com", false,
903 "http://www.w.com", {0, npos, npos}, npos },
904 { "http:a///www.w.com", false,
905 "http://a///www.w.com", {0, npos, npos}, npos },
906 { "mailto://a@b.com", true,
907 "mailto://a@b.com", {0, npos, npos}, 0 },
908 { "mailto://a@b.com", false,
909 "mailto://a@b.com", {0, npos, npos}, 0 },
911 for (size_t i = 0; i < arraysize(test_cases); ++i) {
912 SCOPED_TRACE(testing::Message() << "Index " << i << " input: "
913 << test_cases[i].input << ", trim_http: "
914 << test_cases[i].trim_http);
916 AutocompleteInput input(ASCIIToUTF16(test_cases[i].input),
917 base::string16::npos, std::string(),
918 GURL("about:blank"),
919 metrics::OmniboxEventProto::INVALID_SPEC, false,
920 false, true, true,
921 ChromeAutocompleteSchemeClassifier(profile_.get()));
922 AutocompleteMatch match(autocomplete_->SuggestExactInput(
923 input.text(), input.canonicalized_url(), test_cases[i].trim_http));
924 EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents);
925 for (size_t match_index = 0; match_index < match.contents_class.size();
926 ++match_index) {
927 EXPECT_EQ(test_cases[i].offsets[match_index],
928 match.contents_class[match_index].offset);
929 EXPECT_EQ(ACMatchClassification::URL |
930 (match_index == test_cases[i].match_classification_index ?
931 ACMatchClassification::MATCH : 0),
932 match.contents_class[match_index].style);
934 EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]);
938 TEST_F(HistoryURLProviderTest, HUPScoringExperiment) {
939 HUPScoringParams max_2000_no_time_decay;
940 max_2000_no_time_decay.typed_count_buckets.buckets().push_back(
941 std::make_pair(0.0, 2000));
942 HUPScoringParams max_1250_no_time_decay;
943 max_1250_no_time_decay.typed_count_buckets.buckets().push_back(
944 std::make_pair(0.0, 1250));
945 HUPScoringParams max_1000_no_time_decay;
946 max_1000_no_time_decay.typed_count_buckets.buckets().push_back(
947 std::make_pair(0.0, 1000));
949 HUPScoringParams max_1100_with_time_decay_and_max_cap;
950 max_1100_with_time_decay_and_max_cap.typed_count_buckets.
951 set_relevance_cap(1400);
952 max_1100_with_time_decay_and_max_cap.typed_count_buckets.
953 set_half_life_days(16);
954 max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
955 std::make_pair(0.5, 1100));
956 max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
957 std::make_pair(0.24, 200));
958 max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
959 std::make_pair(0.0, 100));
961 HUPScoringParams max_1100_visit_typed_decays;
962 max_1100_visit_typed_decays.typed_count_buckets.set_half_life_days(16);
963 max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
964 std::make_pair(0.5, 1100));
965 max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
966 std::make_pair(0.0, 100));
967 max_1100_visit_typed_decays.visited_count_buckets.set_half_life_days(16);
968 max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
969 std::make_pair(0.5, 550));
970 max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
971 std::make_pair(0.0, 50));
973 const int kMaxMatches = 3;
974 struct TestCase {
975 const char* input;
976 HUPScoringParams scoring_params;
977 struct ExpectedMatch {
978 const char* url;
979 int control_relevance;
980 int experiment_relevance;
982 ExpectedMatch matches[kMaxMatches];
983 } test_cases[] = {
984 // Max score 2000 -> no demotion.
985 { "7.com/1", max_2000_no_time_decay,
986 {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
988 // Limit score to 1250/1000 and make sure that the top match is unchanged.
989 { "7.com/1", max_1250_no_time_decay,
990 {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
991 { "7.com/2", max_1250_no_time_decay,
992 {{"7.com/2a", 1413, 1413}, {"7.com/2b", 1412, 1250}, {NULL, 0, 0}} },
993 { "7.com/4", max_1000_no_time_decay,
994 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 1000},
995 {"7.com/4b", 1201, 999}} },
997 // Max relevance cap is 1400 and half-life is 16 days.
998 { "7.com/1", max_1100_with_time_decay_and_max_cap,
999 {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
1000 { "7.com/4", max_1100_with_time_decay_and_max_cap,
1001 {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 200},
1002 {"7.com/4b", 1201, 100}} },
1004 // Max relevance cap is 1400 and half-life is 16 days for both visit/typed.
1005 { "7.com/5", max_1100_visit_typed_decays,
1006 {{"7.com/5", 1203, 1203}, {"7.com/5a", 1202, 50}, {NULL, 0, 0}} },
1008 for (size_t i = 0; i < arraysize(test_cases); ++i) {
1009 SCOPED_TRACE(test_cases[i].input);
1010 UrlAndLegalDefault output[kMaxMatches];
1011 int max_matches;
1012 for (max_matches = 0; max_matches < kMaxMatches; ++max_matches) {
1013 if (test_cases[i].matches[max_matches].url == NULL)
1014 break;
1015 output[max_matches].url =
1016 url_fixer::FixupURL(test_cases[i].matches[max_matches].url,
1017 std::string()).spec();
1018 output[max_matches].allowed_to_be_default_match = true;
1020 autocomplete_->scoring_params_ = test_cases[i].scoring_params;
1022 // Test the control (scoring disabled).
1023 autocomplete_->scoring_params_.experimental_scoring_enabled = false;
1024 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
1025 std::string(), false, output, max_matches));
1026 for (int j = 0; j < max_matches; ++j) {
1027 EXPECT_EQ(test_cases[i].matches[j].control_relevance,
1028 matches_[j].relevance);
1031 // Test the experiment (scoring enabled).
1032 autocomplete_->scoring_params_.experimental_scoring_enabled = true;
1033 ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
1034 std::string(), false, output, max_matches));
1035 for (int j = 0; j < max_matches; ++j) {
1036 EXPECT_EQ(test_cases[i].matches[j].experiment_relevance,
1037 matches_[j].relevance);