Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / history_url_provider_unittest.cc
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.
4
5 #include "chrome/browser/autocomplete/history_url_provider.h"
6
7 #include <algorithm>
8
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.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/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"
37
38 using base::ASCIIToUTF16;
39 using base::Time;
40 using base::TimeDelta;
41
42 using content::TestBrowserThreadBundle;
43
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},
52
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},
55
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},
59
60   // Unpopular pages should not appear in the results at all.
61   {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0, 80},
62
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},
66
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},
73
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},
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},
96
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},
100
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},
106
107   // Domain name with number.
108   {"http://www.17173.com/", "Domain with number", 3, 3, 80},
109
110   // URLs to test exact-matching behavior.
111   {"http://go/", "Intranet URL", 1, 1, 80},
112   {"http://gooey/", "Intranet URL 2", 5, 5, 80},
113
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},
118
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},
123
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},
129
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},
137
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},
141
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.
150 };
151
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;
158   };
159
160   HistoryURLProviderTest()
161       : sort_matches_(false) {
162     HistoryQuickProvider::set_disabled(true);
163   }
164
165   ~HistoryURLProviderTest() override {
166     HistoryQuickProvider::set_disabled(false);
167   }
168
169   // AutocompleteProviderListener:
170   void OnProviderUpdate(bool updated_matches) override;
171
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>(
179             new ChromeTemplateURLServiceClient(
180                 HistoryServiceFactory::GetForProfile(
181                     profile, Profile::EXPLICIT_ACCESS))),
182         NULL, NULL, base::Closure());
183   }
184
185   // testing::Test
186   void SetUp() override {
187     ASSERT_TRUE(SetUpImpl(false));
188   }
189   void TearDown() override;
190
191   // Does the real setup.
192   bool SetUpImpl(bool no_db) WARN_UNUSED_RESULT;
193
194   // Fills test data into the history system.
195   void FillData();
196
197   // Runs an autocomplete query on |text| and checks to see that the returned
198   // results' destination URLs match those provided.  Also allows checking
199   // that the input type was identified correctly.
200   void RunTest(const base::string16& text,
201                const std::string& desired_tld,
202                bool prevent_inline_autocomplete,
203                const UrlAndLegalDefault* expected_urls,
204                size_t num_results,
205                metrics::OmniboxInputType::Type* identified_input_type);
206
207   // A version of the above without the final |type| output parameter.
208   void RunTest(const base::string16& text,
209                const std::string& desired_tld,
210                bool prevent_inline_autocomplete,
211                const UrlAndLegalDefault* expected_urls,
212                size_t num_results) {
213     metrics::OmniboxInputType::Type type;
214     return RunTest(text, desired_tld, prevent_inline_autocomplete,
215                    expected_urls, num_results, &type);
216   }
217
218   content::TestBrowserThreadBundle thread_bundle_;
219   ACMatches matches_;
220   scoped_ptr<TestingProfile> profile_;
221   HistoryService* history_service_;
222   scoped_refptr<HistoryURLProvider> autocomplete_;
223   // Should the matches be sorted and duplicates removed?
224   bool sort_matches_;
225 };
226
227 class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
228  protected:
229   void SetUp() override {
230     ASSERT_TRUE(SetUpImpl(true));
231   }
232 };
233
234 class HistoryURLProviderTestNoSearchProvider : public HistoryURLProviderTest {
235  protected:
236   void SetUp() override {
237     DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
238     HistoryURLProviderTest::SetUp();
239   }
240
241   void TearDown() override {
242     HistoryURLProviderTest::TearDown();
243     DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
244   }
245 };
246
247 void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
248   if (autocomplete_->done())
249     base::MessageLoop::current()->Quit();
250 }
251
252 bool HistoryURLProviderTest::SetUpImpl(bool no_db) {
253   profile_.reset(new TestingProfile());
254   if (!(profile_->CreateHistoryService(true, no_db)))
255     return false;
256   if (!no_db) {
257     profile_->BlockUntilHistoryProcessesPendingRequests();
258     profile_->BlockUntilHistoryIndexIsRefreshed();
259   }
260   profile_->GetPrefs()->SetString(prefs::kAcceptLanguages, "en-US,en,ko");
261   history_service_ = HistoryServiceFactory::GetForProfile(
262       profile_.get(), Profile::EXPLICIT_ACCESS);
263
264   autocomplete_ = new HistoryURLProvider(this, profile_.get());
265   TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
266       profile_.get(), &HistoryURLProviderTest::CreateTemplateURLService);
267   FillData();
268   return true;
269 }
270
271 void HistoryURLProviderTest::TearDown() {
272   autocomplete_ = NULL;
273 }
274
275 void HistoryURLProviderTest::FillData() {
276   // Most visits are a long time ago (some tests require this since we do some
277   // special logic for things visited very recently). Note that this time must
278   // be more recent than the "expire history" threshold for the data to be kept
279   // in the main database.
280   //
281   // TODO(brettw) It would be nice if we could test this behavior, in which
282   // case the time would be specifed in the test_db structure.
283   const Time now = Time::Now();
284
285   for (size_t i = 0; i < arraysize(test_db); ++i) {
286     const TestURLInfo& cur = test_db[i];
287     const GURL current_url(cur.url);
288     history_service_->AddPageWithDetails(
289         current_url, base::UTF8ToUTF16(cur.title), cur.visit_count,
290         cur.typed_count, now - TimeDelta::FromDays(cur.age_in_days), false,
291         history::SOURCE_BROWSED);
292   }
293
294   history_service_->AddPageWithDetails(
295       GURL("http://p/"), base::UTF8ToUTF16("p"), 0, 0,
296       Time::Now() -
297       TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1),
298       false, history::SOURCE_BROWSED);
299 }
300
301 void HistoryURLProviderTest::RunTest(
302     const base::string16& text,
303     const std::string& desired_tld,
304     bool prevent_inline_autocomplete,
305     const UrlAndLegalDefault* expected_urls,
306     size_t num_results,
307     metrics::OmniboxInputType::Type* identified_input_type) {
308   AutocompleteInput input(text, base::string16::npos, desired_tld, GURL(),
309                           metrics::OmniboxEventProto::INVALID_SPEC,
310                           prevent_inline_autocomplete, false, true, true,
311                           ChromeAutocompleteSchemeClassifier(profile_.get()));
312   *identified_input_type = input.type();
313   autocomplete_->Start(input, false);
314   if (!autocomplete_->done())
315     base::MessageLoop::current()->Run();
316
317   matches_ = autocomplete_->matches();
318   if (sort_matches_) {
319     TemplateURLService* service =
320         TemplateURLServiceFactory::GetForProfile(profile_.get());
321     for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i)
322       i->ComputeStrippedDestinationURL(service);
323     AutocompleteResult::DedupMatchesByDestination(
324         input.current_page_classification(), false, &matches_);
325     std::sort(matches_.begin(), matches_.end(),
326               &AutocompleteMatch::MoreRelevant);
327   }
328   ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text
329                                           << "\nTLD: \"" << desired_tld << "\"";
330   for (size_t i = 0; i < num_results; ++i) {
331     EXPECT_EQ(expected_urls[i].url, matches_[i].destination_url.spec());
332     EXPECT_EQ(expected_urls[i].allowed_to_be_default_match,
333               matches_[i].allowed_to_be_default_match);
334   }
335 }
336
337 TEST_F(HistoryURLProviderTest, PromoteShorterURLs) {
338   // Test that hosts get synthesized below popular pages.
339   const UrlAndLegalDefault expected_nonsynth[] = {
340     { "http://slashdot.org/favorite_page.html", false },
341     { "http://slashdot.org/", false }
342   };
343   RunTest(ASCIIToUTF16("slash"), std::string(), true, expected_nonsynth,
344           arraysize(expected_nonsynth));
345
346   // Test that hosts get synthesized above less popular pages.
347   const UrlAndLegalDefault expected_synth[] = {
348     { "http://kerneltrap.org/", false },
349     { "http://kerneltrap.org/not_very_popular.html", false }
350   };
351   RunTest(ASCIIToUTF16("kernel"), std::string(), true, expected_synth,
352           arraysize(expected_synth));
353
354   // Test that unpopular pages are ignored completely.
355   RunTest(ASCIIToUTF16("fresh"), std::string(), true, NULL, 0);
356
357   // Test that if we create or promote shorter suggestions that would not
358   // normally be inline autocompletable, we make them inline autocompletable if
359   // the original suggestion (that we replaced as "top") was inline
360   // autocompletable.
361   const UrlAndLegalDefault expected_synthesisa[] = {
362     { "http://synthesisatest.com/", true },
363     { "http://synthesisatest.com/foo/", true }
364   };
365   RunTest(ASCIIToUTF16("synthesisa"), std::string(), false, expected_synthesisa,
366           arraysize(expected_synthesisa));
367   EXPECT_LT(matches_.front().relevance, 1200);
368   const UrlAndLegalDefault expected_synthesisb[] = {
369     { "http://synthesisbtest.com/foo/", true },
370     { "http://synthesisbtest.com/foo/bar.html", true }
371   };
372   RunTest(ASCIIToUTF16("synthesisb"), std::string(), false, expected_synthesisb,
373           arraysize(expected_synthesisb));
374   EXPECT_GE(matches_.front().relevance, 1410);
375
376   // Test that if we have a synthesized host that matches a suggestion, they
377   // get combined into one.
378   const UrlAndLegalDefault expected_combine[] = {
379     { "http://news.google.com/", false },
380     { "http://news.google.com/?ned=us&topic=n", false },
381   };
382   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), std::string(), true,
383                                   expected_combine,
384                                   arraysize(expected_combine)));
385   // The title should also have gotten set properly on the host for the
386   // synthesized one, since it was also in the results.
387   EXPECT_EQ(ASCIIToUTF16("Google News"), matches_.front().description);
388
389   // Test that short URL matching works correctly as the user types more
390   // (several tests):
391   // The entry for foo.com is the best of all five foo.com* entries.
392   const UrlAndLegalDefault short_1[] = {
393     { "http://foo.com/", false },
394     { "http://foo.com/dir/another/again/myfile.html", false },
395     { "http://foo.com/dir/", false }
396   };
397   RunTest(ASCIIToUTF16("foo"), std::string(), true, short_1,
398           arraysize(short_1));
399
400   // When the user types the whole host, make sure we don't get two results for
401   // it.
402   const UrlAndLegalDefault short_2[] = {
403     { "http://foo.com/", true },
404     { "http://foo.com/dir/another/again/myfile.html", false },
405     { "http://foo.com/dir/", false },
406     { "http://foo.com/dir/another/", false }
407   };
408   RunTest(ASCIIToUTF16("foo.com"), std::string(), true, short_2,
409           arraysize(short_2));
410   RunTest(ASCIIToUTF16("foo.com/"), std::string(), true, short_2,
411           arraysize(short_2));
412
413   // The filename is the second best of the foo.com* entries, but there is a
414   // shorter URL that's "good enough".  The host doesn't match the user input
415   // and so should not appear.
416   const UrlAndLegalDefault short_3[] = {
417     { "http://foo.com/d", true },
418     { "http://foo.com/dir/another/", false },
419     { "http://foo.com/dir/another/again/myfile.html", false },
420     { "http://foo.com/dir/", false }
421   };
422   RunTest(ASCIIToUTF16("foo.com/d"), std::string(), true, short_3,
423           arraysize(short_3));
424
425   // We shouldn't promote shorter URLs than the best if they're not good
426   // enough.
427   const UrlAndLegalDefault short_4[] = {
428     { "http://foo.com/dir/another/a", true },
429     { "http://foo.com/dir/another/again/myfile.html", false },
430     { "http://foo.com/dir/another/again/", false }
431   };
432   RunTest(ASCIIToUTF16("foo.com/dir/another/a"), std::string(), true, short_4,
433           arraysize(short_4));
434
435   // Exact matches should always be best no matter how much more another match
436   // has been typed.
437   const UrlAndLegalDefault short_5a[] = {
438     { "http://gooey/", true },
439     { "http://www.google.com/", true },
440     { "http://go/", true }
441   };
442   const UrlAndLegalDefault short_5b[] = {
443     { "http://go/", true },
444     { "http://gooey/", true },
445     { "http://www.google.com/", true }
446   };
447   RunTest(ASCIIToUTF16("g"), std::string(), false, short_5a,
448           arraysize(short_5a));
449   RunTest(ASCIIToUTF16("go"), std::string(), false, short_5b,
450           arraysize(short_5b));
451 }
452
453 TEST_F(HistoryURLProviderTest, CullRedirects) {
454   // URLs we will be using, plus the visit counts they will initially get
455   // (the redirect set below will also increment the visit counts). We want
456   // the results to be in A,B,C order. Note also that our visit counts are
457   // all high enough so that domain synthesizing won't get triggered.
458   struct TestCase {
459     const char* url;
460     int count;
461   } test_cases[] = {
462     {"http://redirects/A", 30},
463     {"http://redirects/B", 20},
464     {"http://redirects/C", 10}
465   };
466   for (size_t i = 0; i < arraysize(test_cases); ++i) {
467     history_service_->AddPageWithDetails(GURL(test_cases[i].url),
468         ASCIIToUTF16("Title"), test_cases[i].count, test_cases[i].count,
469         Time::Now(), false, history::SOURCE_BROWSED);
470   }
471
472   // Create a B->C->A redirect chain, but set the visit counts such that they
473   // will appear in A,B,C order in the results. The autocomplete query will
474   // search for the most recent visit when looking for redirects, so this will
475   // be found even though the previous visits had no redirects.
476   history::RedirectList redirects_to_a;
477   redirects_to_a.push_back(GURL(test_cases[1].url));
478   redirects_to_a.push_back(GURL(test_cases[2].url));
479   redirects_to_a.push_back(GURL(test_cases[0].url));
480   history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(),
481       NULL, 0, GURL(), redirects_to_a, ui::PAGE_TRANSITION_TYPED,
482       history::SOURCE_BROWSED, true);
483
484   // Because all the results are part of a redirect chain with other results,
485   // all but the first one (A) should be culled. We should get the default
486   // "what you typed" result, plus this one.
487   const base::string16 typing(ASCIIToUTF16("http://redirects/"));
488   const UrlAndLegalDefault expected_results[] = {
489     { base::UTF16ToUTF8(typing), true },
490     { test_cases[0].url, false }
491   };
492   RunTest(typing, std::string(), true, expected_results,
493           arraysize(expected_results));
494 }
495
496 TEST_F(HistoryURLProviderTestNoSearchProvider, WhatYouTypedNoSearchProvider) {
497   // When no search provider is available, make sure we provide WYT matches
498   // for text that could be a URL.
499
500   const UrlAndLegalDefault results_1[] = {
501     { "http://wytmatch/", true }
502   };
503   RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, results_1,
504           arraysize(results_1));
505
506   RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL, 0);
507   RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL, 0);
508
509   const UrlAndLegalDefault results_2[] = {
510     { "http://wytmatch+foo+bar.com/", true }
511   };
512   RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false,
513           results_2, arraysize(results_2));
514 }
515
516 TEST_F(HistoryURLProviderTest, WhatYouTyped) {
517   // Make sure we suggest a What You Typed match at the right times.
518   RunTest(ASCIIToUTF16("wytmatch"), std::string(), false, NULL, 0);
519   RunTest(ASCIIToUTF16("wytmatch foo bar"), std::string(), false, NULL, 0);
520   RunTest(ASCIIToUTF16("wytmatch+foo+bar"), std::string(), false, NULL, 0);
521   RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), std::string(), false, NULL, 0);
522
523   const UrlAndLegalDefault results_1[] = {
524     { "http://www.wytmatch.com/", true }
525   };
526   RunTest(ASCIIToUTF16("wytmatch"), "com", false, results_1,
527           arraysize(results_1));
528
529   const UrlAndLegalDefault results_2[] = {
530     { "http://wytmatch%20foo%20bar/", true }
531   };
532   RunTest(ASCIIToUTF16("http://wytmatch foo bar"), std::string(), false,
533           results_2, arraysize(results_2));
534
535   const UrlAndLegalDefault results_3[] = {
536     { "https://wytmatch%20foo%20bar/", true }
537   };
538   RunTest(ASCIIToUTF16("https://wytmatch foo bar"), std::string(), false,
539           results_3, arraysize(results_3));
540 }
541
542 TEST_F(HistoryURLProviderTest, Fixup) {
543   // Test for various past crashes we've had.
544   RunTest(ASCIIToUTF16("\\"), std::string(), false, NULL, 0);
545   RunTest(ASCIIToUTF16("#"), std::string(), false, NULL, 0);
546   RunTest(ASCIIToUTF16("%20"), std::string(), false, NULL, 0);
547   const UrlAndLegalDefault fixup_crash[] = {
548     { "http://%EF%BD%A5@s/", true }
549   };
550   RunTest(base::WideToUTF16(L"\uff65@s"), std::string(), false, fixup_crash,
551           arraysize(fixup_crash));
552   RunTest(base::WideToUTF16(L"\u2015\u2015@ \uff7c"), std::string(), false,
553           NULL, 0);
554
555   // Fixing up "file:" should result in an inline autocomplete offset of just
556   // after "file:", not just after "file://".
557   const base::string16 input_1(ASCIIToUTF16("file:"));
558   const UrlAndLegalDefault fixup_1[] = {
559     { "file:///C:/foo.txt", true }
560   };
561   ASSERT_NO_FATAL_FAILURE(RunTest(input_1, std::string(), false, fixup_1,
562                                   arraysize(fixup_1)));
563   EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
564             matches_.front().inline_autocompletion);
565
566   // Fixing up "http:/" should result in an inline autocomplete offset of just
567   // after "http:/", not just after "http:".
568   const base::string16 input_2(ASCIIToUTF16("http:/"));
569   const UrlAndLegalDefault fixup_2[] = {
570     { "http://bogussite.com/a", true },
571     { "http://bogussite.com/b", true },
572     { "http://bogussite.com/c", true }
573   };
574   ASSERT_NO_FATAL_FAILURE(RunTest(input_2, std::string(), false, fixup_2,
575                                   arraysize(fixup_2)));
576   EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
577             matches_.front().inline_autocompletion);
578
579   // Adding a TLD to a small number like "56" should result in "www.56.com"
580   // rather than "0.0.0.56.com".
581   const UrlAndLegalDefault fixup_3[] = {
582     { "http://www.56.com/", true }
583   };
584   RunTest(ASCIIToUTF16("56"), "com", true, fixup_3, arraysize(fixup_3));
585
586   // An input looks like a IP address like "127.0.0.1" should result in
587   // "http://127.0.0.1/".
588   const UrlAndLegalDefault fixup_4[] = {
589     { "http://127.0.0.1/", true }
590   };
591   RunTest(ASCIIToUTF16("127.0.0.1"), std::string(), false, fixup_4,
592           arraysize(fixup_4));
593
594   // An number "17173" should result in "http://www.17173.com/" in db.
595   const UrlAndLegalDefault fixup_5[] = {
596     { "http://www.17173.com/", true }
597   };
598   RunTest(ASCIIToUTF16("17173"), std::string(), false, fixup_5,
599           arraysize(fixup_5));
600 }
601
602 // Make sure the results for the input 'p' don't change between the first and
603 // second passes.
604 TEST_F(HistoryURLProviderTest, EmptyVisits) {
605   // Wait for history to create the in memory DB.
606   profile_->BlockUntilHistoryProcessesPendingRequests();
607
608   AutocompleteInput input(ASCIIToUTF16("p"), base::string16::npos,
609                           std::string(), GURL(),
610                           metrics::OmniboxEventProto::INVALID_SPEC, false,
611                           false, true, true,
612                           ChromeAutocompleteSchemeClassifier(profile_.get()));
613   autocomplete_->Start(input, false);
614   // HistoryURLProvider shouldn't be done (waiting on async results).
615   EXPECT_FALSE(autocomplete_->done());
616
617   // We should get back an entry for pandora.
618   matches_ = autocomplete_->matches();
619   ASSERT_GT(matches_.size(), 0u);
620   EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
621   int pandora_relevance = matches_[0].relevance;
622
623   // Run the message loop. When |autocomplete_| finishes the loop is quit.
624   base::MessageLoop::current()->Run();
625   EXPECT_TRUE(autocomplete_->done());
626   matches_ = autocomplete_->matches();
627   ASSERT_GT(matches_.size(), 0u);
628   EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
629   EXPECT_EQ(pandora_relevance, matches_[0].relevance);
630 }
631
632 TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) {
633   // Ensure that we will still produce matches for navigation when there is no
634   // database.
635   UrlAndLegalDefault navigation_1[] = {
636     { "http://test.com/", true }
637   };
638   RunTest(ASCIIToUTF16("test.com"), std::string(), false, navigation_1,
639           arraysize(navigation_1));
640
641   UrlAndLegalDefault navigation_2[] = {
642     { "http://slash/", true }
643   };
644   RunTest(ASCIIToUTF16("slash"), std::string(), false, navigation_2,
645           arraysize(navigation_2));
646
647   RunTest(ASCIIToUTF16("this is a query"), std::string(), false, NULL, 0);
648 }
649
650 TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) {
651   AutocompleteInput input(ASCIIToUTF16("slash "), base::string16::npos,
652                           std::string(), GURL(),
653                           metrics::OmniboxEventProto::INVALID_SPEC, false,
654                           false, true, true,
655                           ChromeAutocompleteSchemeClassifier(profile_.get()));
656   autocomplete_->Start(input, false);
657   if (!autocomplete_->done())
658     base::MessageLoop::current()->Run();
659
660   // None of the matches should attempt to autocomplete.
661   matches_ = autocomplete_->matches();
662   for (size_t i = 0; i < matches_.size(); ++i) {
663     EXPECT_TRUE(matches_[i].inline_autocompletion.empty());
664     EXPECT_FALSE(matches_[i].allowed_to_be_default_match);
665   }
666 }
667
668 TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) {
669   // Visiting foo.com should not make this string be treated as a navigation.
670   // That means the result should be scored around 1200 ("what you typed")
671   // and not 1400+.
672   const UrlAndLegalDefault expected[] = {
673     { "http://user@foo.com/", true }
674   };
675   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), std::string(),
676                                   false, expected, arraysize(expected)));
677   EXPECT_LE(1200, matches_[0].relevance);
678   EXPECT_LT(matches_[0].relevance, 1210);
679 }
680
681 TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) {
682   struct TestCase {
683     const char* input;
684     int relevance;
685   } test_cases[] = {
686     { "fooey", 0 },
687     { "fooey/", 1200 },     // 1200 for URL would still navigate by default.
688     { "fooey/a", 1200 },    // 1200 for UNKNOWN would not.
689     { "fooey/a b", 1200 },  // Also UNKNOWN.
690     { "gooey", 1410 },
691     { "gooey/", 1410 },
692     { "gooey/a", 1400 },
693     { "gooey/a b", 1400 },
694   };
695   for (size_t i = 0; i < arraysize(test_cases); ++i) {
696     SCOPED_TRACE(test_cases[i].input);
697     if (test_cases[i].relevance == 0) {
698       RunTest(ASCIIToUTF16(test_cases[i].input), std::string(), false, NULL, 0);
699     } else {
700       const UrlAndLegalDefault output[] = {
701           {url_fixer::FixupURL(test_cases[i].input, std::string()).spec(),
702            true}};
703       ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
704                               std::string(), false, output, arraysize(output)));
705       // Actual relevance should be at least what test_cases expects and
706       // and no more than 10 more.
707       EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
708       EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
709     }
710   }
711 }
712
713 // Makes sure autocompletion happens for intranet sites that have been
714 // previoulsy visited.
715 TEST_F(HistoryURLProviderTest, IntranetURLCompletion) {
716   sort_matches_ = true;
717
718   const UrlAndLegalDefault expected1[] = {
719     { "http://intra/three", true },
720     { "http://intra/two", true }
721   };
722   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), std::string(), false,
723                                   expected1, arraysize(expected1)));
724   EXPECT_LE(1410, matches_[0].relevance);
725   EXPECT_LT(matches_[0].relevance, 1420);
726   EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance);
727
728   const UrlAndLegalDefault expected2[] = {
729     { "http://moo/b", true },
730     { "http://moo/bar", true }
731   };
732   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), std::string(), false,
733                                   expected2, arraysize(expected2)));
734   // The url what you typed match should be around 1400, otherwise the
735   // search what you typed match is going to be first.
736   EXPECT_LE(1400, matches_[0].relevance);
737   EXPECT_LT(matches_[0].relevance, 1410);
738
739   const UrlAndLegalDefault expected3[] = {
740     { "http://intra/one", true },
741     { "http://intra/three", true },
742     { "http://intra/two", true }
743   };
744   RunTest(ASCIIToUTF16("intra"), std::string(), false, expected3,
745           arraysize(expected3));
746
747   const UrlAndLegalDefault expected4[] = {
748     { "http://intra/one", true },
749     { "http://intra/three", true },
750     { "http://intra/two", true }
751   };
752   RunTest(ASCIIToUTF16("intra/"), std::string(), false, expected4,
753           arraysize(expected4));
754
755   const UrlAndLegalDefault expected5[] = {
756     { "http://intra/one", true }
757   };
758   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), std::string(), false,
759                                   expected5, arraysize(expected5)));
760   EXPECT_LE(1410, matches_[0].relevance);
761   EXPECT_LT(matches_[0].relevance, 1420);
762
763   const UrlAndLegalDefault expected6[] = {
764     { "http://intra/x", true }
765   };
766   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), std::string(), false,
767                                   expected6, arraysize(expected6)));
768   EXPECT_LE(1400, matches_[0].relevance);
769   EXPECT_LT(matches_[0].relevance, 1410);
770
771   const UrlAndLegalDefault expected7[] = {
772     { "http://typedhost/untypedpath", true }
773   };
774   ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
775                                   std::string(), false, expected7,
776                                   arraysize(expected7)));
777   EXPECT_LE(1400, matches_[0].relevance);
778   EXPECT_LT(matches_[0].relevance, 1410);
779 }
780
781 TEST_F(HistoryURLProviderTest, CrashDueToFixup) {
782   // This test passes if we don't crash.  The results don't matter.
783   const char* const test_cases[] = {
784     "//c",
785     "\\@st",
786     "view-source:x",
787   };
788   for (size_t i = 0; i < arraysize(test_cases); ++i) {
789     AutocompleteInput input(ASCIIToUTF16(test_cases[i]), base::string16::npos,
790                             std::string(), GURL(),
791                             metrics::OmniboxEventProto::INVALID_SPEC,
792                             false, false, true, true,
793                             ChromeAutocompleteSchemeClassifier(profile_.get()));
794     autocomplete_->Start(input, false);
795     if (!autocomplete_->done())
796       base::MessageLoop::current()->Run();
797   }
798 }
799
800 TEST_F(HistoryURLProviderTest, CullSearchResults) {
801   // Set up a default search engine.
802   TemplateURLData data;
803   data.SetKeyword(ASCIIToUTF16("TestEngine"));
804   data.SetURL("http://testsearch.com/?q={searchTerms}");
805   TemplateURLService* template_url_service =
806       TemplateURLServiceFactory::GetForProfile(profile_.get());
807   TemplateURL* template_url = new TemplateURL(data);
808   template_url_service->Add(template_url);
809   template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
810   template_url_service->Load();
811
812   // URLs we will be using, plus the visit counts they will initially get
813   // (the redirect set below will also increment the visit counts). We want
814   // the results to be in A,B,C order. Note also that our visit counts are
815   // all high enough so that domain synthesizing won't get triggered.
816   struct TestCase {
817     const char* url;
818     int count;
819   } test_cases[] = {
820     {"https://testsearch.com/", 30},
821     {"https://testsearch.com/?q=foobar", 20},
822     {"http://foobar.com/", 10}
823   };
824   for (size_t i = 0; i < arraysize(test_cases); ++i) {
825     history_service_->AddPageWithDetails(GURL(test_cases[i].url),
826         base::UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
827         Time::Now(), false, history::SOURCE_BROWSED);
828   }
829
830   // We should not see search URLs when typing a previously used query.
831   const UrlAndLegalDefault expected_when_searching_query[] = {
832     { test_cases[2].url, false }
833   };
834   RunTest(ASCIIToUTF16("foobar"), std::string(), true,
835       expected_when_searching_query, arraysize(expected_when_searching_query));
836
837   // We should not see search URLs when typing the search engine name.
838   const UrlAndLegalDefault expected_when_searching_site[] = {
839     { test_cases[0].url, false }
840   };
841   RunTest(ASCIIToUTF16("testsearch"), std::string(), true,
842       expected_when_searching_site, arraysize(expected_when_searching_site));
843 }
844
845 TEST_F(HistoryURLProviderTest, SuggestExactInput) {
846   const size_t npos = std::string::npos;
847   struct TestCase {
848     // Inputs:
849     const char* input;
850     bool trim_http;
851     // Expected Outputs:
852     const char* contents;
853     // Offsets of the ACMatchClassifications, terminated by npos.
854     size_t offsets[3];
855     // The index of the ACMatchClassification that should have the MATCH bit
856     // set, npos if no ACMatchClassification should have the MATCH bit set.
857     size_t match_classification_index;
858   } test_cases[] = {
859     { "http://www.somesite.com", false,
860       "http://www.somesite.com", {0, npos, npos}, 0 },
861     { "www.somesite.com", true,
862       "www.somesite.com", {0, npos, npos}, 0 },
863     { "www.somesite.com", false,
864       "http://www.somesite.com", {0, 7, npos}, 1 },
865     { "somesite.com", true,
866       "somesite.com", {0, npos, npos}, 0 },
867     { "somesite.com", false,
868       "http://somesite.com", {0, 7, npos}, 1 },
869     { "w", true,
870       "w", {0, npos, npos}, 0 },
871     { "w", false,
872       "http://w", {0, 7, npos}, 1 },
873     { "w.com", true,
874       "w.com", {0, npos, npos}, 0 },
875     { "w.com", false,
876       "http://w.com", {0, 7, npos}, 1 },
877     { "www.w.com", true,
878       "www.w.com", {0, npos, npos}, 0 },
879     { "www.w.com", false,
880       "http://www.w.com", {0, 7, npos}, 1 },
881     { "view-source:w", true,
882       "view-source:w", {0, npos, npos}, 0 },
883     { "view-source:www.w.com/", true,
884       "view-source:www.w.com", {0, npos, npos}, npos },
885     { "view-source:www.w.com/", false,
886       "view-source:http://www.w.com", {0, npos, npos}, npos },
887     { "view-source:http://www.w.com/", false,
888       "view-source:http://www.w.com", {0, npos, npos}, 0 },
889     { "   view-source:", true,
890       "view-source:", {0, npos, npos}, 0 },
891     { "http:////////w.com", false,
892       "http://w.com", {0, npos, npos}, npos },
893     { "    http:////////www.w.com", false,
894       "http://www.w.com", {0, npos, npos}, npos },
895     { "http:a///www.w.com", false,
896       "http://a///www.w.com", {0, npos, npos}, npos },
897     { "mailto://a@b.com", true,
898       "mailto://a@b.com", {0, npos, npos}, 0 },
899     { "mailto://a@b.com", false,
900       "mailto://a@b.com", {0, npos, npos}, 0 },
901   };
902   for (size_t i = 0; i < arraysize(test_cases); ++i) {
903     SCOPED_TRACE(testing::Message() << "Index " << i << " input: "
904                                     << test_cases[i].input << ", trim_http: "
905                                     << test_cases[i].trim_http);
906
907     AutocompleteInput input(ASCIIToUTF16(test_cases[i].input),
908                             base::string16::npos, std::string(),
909                             GURL("about:blank"),
910                             metrics::OmniboxEventProto::INVALID_SPEC, false,
911                             false, true, true,
912                             ChromeAutocompleteSchemeClassifier(profile_.get()));
913     AutocompleteMatch match(autocomplete_->SuggestExactInput(
914         input.text(), input.canonicalized_url(), test_cases[i].trim_http));
915     EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents);
916     for (size_t match_index = 0; match_index < match.contents_class.size();
917          ++match_index) {
918       EXPECT_EQ(test_cases[i].offsets[match_index],
919                 match.contents_class[match_index].offset);
920       EXPECT_EQ(ACMatchClassification::URL |
921                 (match_index == test_cases[i].match_classification_index ?
922                  ACMatchClassification::MATCH : 0),
923                 match.contents_class[match_index].style);
924     }
925     EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]);
926   }
927 }
928
929 TEST_F(HistoryURLProviderTest, HUPScoringExperiment) {
930   HUPScoringParams max_2000_no_time_decay;
931   max_2000_no_time_decay.typed_count_buckets.buckets().push_back(
932       std::make_pair(0.0, 2000));
933   HUPScoringParams max_1250_no_time_decay;
934   max_1250_no_time_decay.typed_count_buckets.buckets().push_back(
935       std::make_pair(0.0, 1250));
936   HUPScoringParams max_1000_no_time_decay;
937   max_1000_no_time_decay.typed_count_buckets.buckets().push_back(
938       std::make_pair(0.0, 1000));
939
940   HUPScoringParams max_1100_with_time_decay_and_max_cap;
941   max_1100_with_time_decay_and_max_cap.typed_count_buckets.
942       set_relevance_cap(1400);
943   max_1100_with_time_decay_and_max_cap.typed_count_buckets.
944       set_half_life_days(16);
945   max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
946       std::make_pair(0.5, 1100));
947   max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
948       std::make_pair(0.24, 200));
949   max_1100_with_time_decay_and_max_cap.typed_count_buckets.buckets().push_back(
950       std::make_pair(0.0, 100));
951
952   HUPScoringParams max_1100_visit_typed_decays;
953   max_1100_visit_typed_decays.typed_count_buckets.set_half_life_days(16);
954   max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
955       std::make_pair(0.5, 1100));
956   max_1100_visit_typed_decays.typed_count_buckets.buckets().push_back(
957       std::make_pair(0.0, 100));
958   max_1100_visit_typed_decays.visited_count_buckets.set_half_life_days(16);
959   max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
960       std::make_pair(0.5, 550));
961   max_1100_visit_typed_decays.visited_count_buckets.buckets().push_back(
962       std::make_pair(0.0, 50));
963
964   const int kMaxMatches = 3;
965   struct TestCase {
966     const char* input;
967     HUPScoringParams scoring_params;
968     struct ExpectedMatch {
969       const char* url;
970       int control_relevance;
971       int experiment_relevance;
972     };
973     ExpectedMatch matches[kMaxMatches];
974   } test_cases[] = {
975     // Max score 2000 -> no demotion.
976     { "7.com/1", max_2000_no_time_decay,
977       {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
978
979     // Limit score to 1250/1000 and make sure that the top match is unchanged.
980     { "7.com/1", max_1250_no_time_decay,
981       {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
982     { "7.com/2", max_1250_no_time_decay,
983       {{"7.com/2a", 1413, 1413}, {"7.com/2b", 1412, 1250}, {NULL, 0, 0}} },
984     { "7.com/4", max_1000_no_time_decay,
985       {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 1000},
986        {"7.com/4b", 1201, 999}} },
987
988     // Max relevance cap is 1400 and half-life is 16 days.
989     { "7.com/1", max_1100_with_time_decay_and_max_cap,
990       {{"7.com/1a", 1413, 1413}, {NULL, 0, 0}, {NULL, 0, 0}} },
991     { "7.com/4", max_1100_with_time_decay_and_max_cap,
992       {{"7.com/4", 1203, 1203}, {"7.com/4a", 1202, 200},
993        {"7.com/4b", 1201, 100}} },
994
995     // Max relevance cap is 1400 and half-life is 16 days for both visit/typed.
996     { "7.com/5", max_1100_visit_typed_decays,
997       {{"7.com/5", 1203, 1203}, {"7.com/5a", 1202, 50}, {NULL, 0, 0}} },
998   };
999   for (size_t i = 0; i < arraysize(test_cases); ++i) {
1000     SCOPED_TRACE(test_cases[i].input);
1001     UrlAndLegalDefault output[kMaxMatches];
1002     int max_matches;
1003     for (max_matches = 0; max_matches < kMaxMatches; ++max_matches) {
1004       if (test_cases[i].matches[max_matches].url == NULL)
1005         break;
1006       output[max_matches].url =
1007           url_fixer::FixupURL(test_cases[i].matches[max_matches].url,
1008                               std::string()).spec();
1009       output[max_matches].allowed_to_be_default_match = true;
1010     }
1011     autocomplete_->scoring_params_ = test_cases[i].scoring_params;
1012
1013     // Test the control (scoring disabled).
1014     autocomplete_->scoring_params_.experimental_scoring_enabled = false;
1015     ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
1016                                     std::string(), false, output, max_matches));
1017     for (int j = 0; j < max_matches; ++j) {
1018       EXPECT_EQ(test_cases[i].matches[j].control_relevance,
1019                 matches_[j].relevance);
1020     }
1021
1022     // Test the experiment (scoring enabled).
1023     autocomplete_->scoring_params_.experimental_scoring_enabled = true;
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].experiment_relevance,
1028                 matches_[j].relevance);
1029     }
1030   }
1031 }