history_url_provider_unittest.cc revision d3868032626d59662ff73b372b5d584c1d144c53
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/path_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/autocomplete_match.h"
15#include "chrome/browser/autocomplete/autocomplete_provider.h"
16#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
17#include "chrome/browser/autocomplete/history_quick_provider.h"
18#include "chrome/browser/history/history_service.h"
19#include "chrome/browser/history/history_service_factory.h"
20#include "chrome/browser/search_engines/template_url.h"
21#include "chrome/browser/search_engines/template_url_service.h"
22#include "chrome/browser/search_engines/template_url_service_factory.h"
23#include "chrome/common/net/url_fixer_upper.h"
24#include "chrome/test/base/testing_browser_process.h"
25#include "chrome/test/base/testing_profile.h"
26#include "content/public/test/test_browser_thread_bundle.h"
27#include "testing/gtest/include/gtest/gtest.h"
28
29using base::Time;
30using base::TimeDelta;
31
32using content::TestBrowserThreadBundle;
33
34struct TestURLInfo {
35  const char* url;
36  const char* title;
37  int visit_count;
38  int typed_count;
39} test_db[] = {
40  {"http://www.google.com/", "Google", 3, 3},
41
42  // High-quality pages should get a host synthesized as a lower-quality match.
43  {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100},
44
45  // Less popular pages should have hosts synthesized as higher-quality
46  // matches.
47  {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0},
48
49  // Unpopular pages should not appear in the results at all.
50  {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 0},
51
52  // If a host has a match, we should pick it up during host synthesis.
53  {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2},
54  {"http://news.google.com/", "Google News", 1, 1},
55
56  // Matches that are normally not inline-autocompletable should be
57  // autocompleted if they are shorter substitutes for longer matches that would
58  // have been inline autocompleted.
59  {"http://synthesisatest.com/foo/", "Test A", 1, 1},
60  {"http://synthesisbtest.com/foo/", "Test B", 1, 1},
61  {"http://synthesisbtest.com/foo/bar.html", "Test B Bar", 2, 2},
62
63  // Suggested short URLs must be "good enough" and must match user input.
64  {"http://foo.com/", "Dir", 5, 5},
65  {"http://foo.com/dir/", "Dir", 2, 2},
66  {"http://foo.com/dir/another/", "Dir", 5, 1},
67  {"http://foo.com/dir/another/again/", "Dir", 10, 0},
68  {"http://foo.com/dir/another/again/myfile.html", "File", 10, 2},
69
70  // We throw in a lot of extra URLs here to make sure we're testing the
71  // history database's query, not just the autocomplete provider.
72  {"http://startest.com/y/a", "A", 2, 2},
73  {"http://startest.com/y/b", "B", 5, 2},
74  {"http://startest.com/x/c", "C", 5, 2},
75  {"http://startest.com/x/d", "D", 5, 5},
76  {"http://startest.com/y/e", "E", 4, 2},
77  {"http://startest.com/y/f", "F", 3, 2},
78  {"http://startest.com/y/g", "G", 3, 2},
79  {"http://startest.com/y/h", "H", 3, 2},
80  {"http://startest.com/y/i", "I", 3, 2},
81  {"http://startest.com/y/j", "J", 3, 2},
82  {"http://startest.com/y/k", "K", 3, 2},
83  {"http://startest.com/y/l", "L", 3, 2},
84  {"http://startest.com/y/m", "M", 3, 2},
85
86  // A file: URL is useful for testing that fixup does the right thing w.r.t.
87  // the number of trailing slashes on the user's input.
88  {"file:///C:/foo.txt", "", 2, 2},
89
90  // Results with absurdly high typed_counts so that very generic queries like
91  // "http" will give consistent results even if more data is added above.
92  {"http://bogussite.com/a", "Bogus A", 10002, 10000},
93  {"http://bogussite.com/b", "Bogus B", 10001, 10000},
94  {"http://bogussite.com/c", "Bogus C", 10000, 10000},
95
96  // Domain name with number.
97  {"http://www.17173.com/", "Domain with number", 3, 3},
98
99  // URLs to test exact-matching behavior.
100  {"http://go/", "Intranet URL", 1, 1},
101  {"http://gooey/", "Intranet URL 2", 5, 5},
102
103  // URLs for testing offset adjustment.
104  {"http://www.\xEA\xB5\x90\xEC\x9C\xA1.kr/", "Korean", 2, 2},
105  {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2},
106  {"http://ms/c++%20style%20guide", "Style guide", 2, 2},
107
108  // URLs for testing ctrl-enter behavior.
109  {"http://binky/", "Intranet binky", 2, 2},
110  {"http://winky/", "Intranet winky", 2, 2},
111  {"http://www.winky.com/", "Internet winky", 5, 0},
112
113  // URLs used by EmptyVisits.
114  {"http://pandora.com/", "Pandora", 2, 2},
115  // This entry is explicitly added more recently than
116  // history::kLowQualityMatchAgeLimitInDays.
117  // {"http://p/", "p", 0, 0},
118
119  // For intranet based tests.
120  {"http://intra/one", "Intranet", 2, 2},
121  {"http://intra/two", "Intranet two", 1, 1},
122  {"http://intra/three", "Intranet three", 2, 2},
123  {"http://moo/bar", "Intranet moo", 1, 1},
124  {"http://typedhost/typedpath", "Intranet typed", 1, 1},
125  {"http://typedhost/untypedpath", "Intranet untyped", 1, 0},
126
127  {"http://x.com/one", "Internet", 2, 2},
128  {"http://x.com/two", "Internet two", 1, 1},
129  {"http://x.com/three", "Internet three", 2, 2},
130};
131
132class HistoryURLProviderTest : public testing::Test,
133                               public AutocompleteProviderListener {
134 public:
135  HistoryURLProviderTest()
136      : sort_matches_(false) {
137    HistoryQuickProvider::set_disabled(true);
138  }
139
140  virtual ~HistoryURLProviderTest() {
141    HistoryQuickProvider::set_disabled(false);
142  }
143
144  // AutocompleteProviderListener:
145  virtual void OnProviderUpdate(bool updated_matches) OVERRIDE;
146
147 protected:
148  static BrowserContextKeyedService* CreateTemplateURLService(
149      content::BrowserContext* profile) {
150    return new TemplateURLService(static_cast<Profile*>(profile));
151  }
152
153  // testing::Test
154  virtual void SetUp() {
155    ASSERT_TRUE(SetUpImpl(false));
156  }
157  virtual void TearDown();
158
159  // Does the real setup.
160  bool SetUpImpl(bool no_db) WARN_UNUSED_RESULT;
161
162  // Fills test data into the history system.
163  void FillData();
164
165  // Runs an autocomplete query on |text| and checks to see that the returned
166  // results' destination URLs match those provided.  Also allows checking
167  // that the input type was identified correctly.
168  void RunTest(const string16 text,
169               const string16& desired_tld,
170               bool prevent_inline_autocomplete,
171               const std::string* expected_urls,
172               size_t num_results,
173               AutocompleteInput::Type* identified_input_type);
174
175  // A version of the above without the final |type| output parameter.
176  void RunTest(const string16 text,
177               const string16& desired_tld,
178               bool prevent_inline_autocomplete,
179               const std::string* expected_urls,
180               size_t num_results) {
181    AutocompleteInput::Type type;
182    return RunTest(text, desired_tld, prevent_inline_autocomplete,
183                   expected_urls, num_results, &type);
184  }
185
186  content::TestBrowserThreadBundle thread_bundle_;
187  ACMatches matches_;
188  scoped_ptr<TestingProfile> profile_;
189  HistoryService* history_service_;
190  scoped_refptr<HistoryURLProvider> autocomplete_;
191  // Should the matches be sorted and duplicates removed?
192  bool sort_matches_;
193};
194
195class HistoryURLProviderTestNoDB : public HistoryURLProviderTest {
196 protected:
197  virtual void SetUp() {
198    ASSERT_TRUE(SetUpImpl(true));
199  }
200};
201
202void HistoryURLProviderTest::OnProviderUpdate(bool updated_matches) {
203  if (autocomplete_->done())
204    base::MessageLoop::current()->Quit();
205}
206
207bool HistoryURLProviderTest::SetUpImpl(bool no_db) {
208  profile_.reset(new TestingProfile());
209  if (!(profile_->CreateHistoryService(true, no_db)))
210    return false;
211  if (!no_db) {
212    profile_->BlockUntilHistoryProcessesPendingRequests();
213    profile_->BlockUntilHistoryIndexIsRefreshed();
214  }
215  history_service_ =
216      HistoryServiceFactory::GetForProfile(profile_.get(),
217                                           Profile::EXPLICIT_ACCESS);
218
219  autocomplete_ = new HistoryURLProvider(this, profile_.get(), "en-US,en,ko");
220  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
221      profile_.get(), &HistoryURLProviderTest::CreateTemplateURLService);
222  FillData();
223  return true;
224}
225
226void HistoryURLProviderTest::TearDown() {
227  autocomplete_ = NULL;
228}
229
230void HistoryURLProviderTest::FillData() {
231  // All visits are a long time ago (some tests require this since we do some
232  // special logic for things visited very recently). Note that this time must
233  // be more recent than the "archived history" threshold for the data to go
234  // into the main database.
235  //
236  // TODO(brettw) It would be nice if we could test this behavior, in which
237  // case the time would be specifed in the test_db structure.
238  Time visit_time = Time::Now() - TimeDelta::FromDays(80);
239
240  for (size_t i = 0; i < arraysize(test_db); ++i) {
241    const TestURLInfo& cur = test_db[i];
242    const GURL current_url(cur.url);
243    history_service_->AddPageWithDetails(current_url, UTF8ToUTF16(cur.title),
244                                         cur.visit_count, cur.typed_count,
245                                         visit_time, false,
246                                         history::SOURCE_BROWSED);
247  }
248
249  history_service_->AddPageWithDetails(
250      GURL("http://p/"), UTF8ToUTF16("p"), 0, 0,
251      Time::Now() -
252      TimeDelta::FromDays(history::kLowQualityMatchAgeLimitInDays - 1),
253      false, history::SOURCE_BROWSED);
254}
255
256void HistoryURLProviderTest::RunTest(
257    const string16 text,
258    const string16& desired_tld,
259    bool prevent_inline_autocomplete,
260    const std::string* expected_urls,
261    size_t num_results,
262    AutocompleteInput::Type* identified_input_type) {
263  AutocompleteInput input(text, string16::npos, desired_tld, GURL(),
264                          AutocompleteInput::INVALID_SPEC,
265                          prevent_inline_autocomplete, false, true,
266                          AutocompleteInput::ALL_MATCHES);
267  *identified_input_type = input.type();
268  autocomplete_->Start(input, false);
269  if (!autocomplete_->done())
270    base::MessageLoop::current()->Run();
271
272  matches_ = autocomplete_->matches();
273  if (sort_matches_) {
274    for (ACMatches::iterator i = matches_.begin(); i != matches_.end(); ++i)
275      i->ComputeStrippedDestinationURL(profile_.get());
276    std::sort(matches_.begin(), matches_.end(),
277              &AutocompleteMatch::DestinationSortFunc);
278    matches_.erase(std::unique(matches_.begin(), matches_.end(),
279                               &AutocompleteMatch::DestinationsEqual),
280                   matches_.end());
281    std::sort(matches_.begin(), matches_.end(),
282              &AutocompleteMatch::MoreRelevant);
283  }
284  ASSERT_EQ(num_results, matches_.size()) << "Input text: " << text
285                                          << "\nTLD: \"" << desired_tld << "\"";
286  for (size_t i = 0; i < num_results; ++i)
287    EXPECT_EQ(expected_urls[i], matches_[i].destination_url.spec());
288}
289
290TEST_F(HistoryURLProviderTest, PromoteShorterURLs) {
291  // Test that hosts get synthesized below popular pages.
292  const std::string expected_nonsynth[] = {
293    "http://slashdot.org/favorite_page.html",
294    "http://slashdot.org/",
295  };
296  RunTest(ASCIIToUTF16("slash"), string16(), true, expected_nonsynth,
297          arraysize(expected_nonsynth));
298
299  // Test that hosts get synthesized above less popular pages.
300  const std::string expected_synth[] = {
301    "http://kerneltrap.org/",
302    "http://kerneltrap.org/not_very_popular.html",
303  };
304  RunTest(ASCIIToUTF16("kernel"), string16(), true, expected_synth,
305          arraysize(expected_synth));
306
307  // Test that unpopular pages are ignored completely.
308  RunTest(ASCIIToUTF16("fresh"), string16(), true, NULL, 0);
309
310  // Test that if we create or promote shorter suggestions that would not
311  // normally be inline autocompletable, we make them inline autocompletable if
312  // the original suggestion (that we replaced as "top") was inline
313  // autocompletable.
314  const std::string expected_synthesisa[] = {
315    "http://synthesisatest.com/",
316    "http://synthesisatest.com/foo/",
317  };
318  RunTest(ASCIIToUTF16("synthesisa"), string16(), false, expected_synthesisa,
319          arraysize(expected_synthesisa));
320  EXPECT_LT(matches_.front().relevance, 1200);
321  const std::string expected_synthesisb[] = {
322    "http://synthesisbtest.com/foo/",
323    "http://synthesisbtest.com/foo/bar.html",
324  };
325  RunTest(ASCIIToUTF16("synthesisb"), string16(), false, expected_synthesisb,
326          arraysize(expected_synthesisb));
327  EXPECT_GE(matches_.front().relevance, 1410);
328
329  // Test that if we have a synthesized host that matches a suggestion, they
330  // get combined into one.
331  const std::string expected_combine[] = {
332    "http://news.google.com/",
333    "http://news.google.com/?ned=us&topic=n",
334  };
335  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("news"), string16(), true,
336      expected_combine, arraysize(expected_combine)));
337  // The title should also have gotten set properly on the host for the
338  // synthesized one, since it was also in the results.
339  EXPECT_EQ(ASCIIToUTF16("Google News"), matches_.front().description);
340
341  // Test that short URL matching works correctly as the user types more
342  // (several tests):
343  // The entry for foo.com is the best of all five foo.com* entries.
344  const std::string short_1[] = {
345    "http://foo.com/",
346    "http://foo.com/dir/another/again/myfile.html",
347    "http://foo.com/dir/",
348  };
349  RunTest(ASCIIToUTF16("foo"), string16(), true, short_1, arraysize(short_1));
350
351  // When the user types the whole host, make sure we don't get two results for
352  // it.
353  const std::string short_2[] = {
354    "http://foo.com/",
355    "http://foo.com/dir/another/again/myfile.html",
356    "http://foo.com/dir/",
357    "http://foo.com/dir/another/",
358  };
359  RunTest(ASCIIToUTF16("foo.com"), string16(), true, short_2,
360          arraysize(short_2));
361  RunTest(ASCIIToUTF16("foo.com/"), string16(), true, short_2,
362          arraysize(short_2));
363
364  // The filename is the second best of the foo.com* entries, but there is a
365  // shorter URL that's "good enough".  The host doesn't match the user input
366  // and so should not appear.
367  const std::string short_3[] = {
368    "http://foo.com/d",
369    "http://foo.com/dir/another/",
370    "http://foo.com/dir/another/again/myfile.html",
371    "http://foo.com/dir/",
372  };
373  RunTest(ASCIIToUTF16("foo.com/d"), string16(), true, short_3,
374          arraysize(short_3));
375
376  // We shouldn't promote shorter URLs than the best if they're not good
377  // enough.
378  const std::string short_4[] = {
379    "http://foo.com/dir/another/a",
380    "http://foo.com/dir/another/again/myfile.html",
381    "http://foo.com/dir/another/again/",
382  };
383  RunTest(ASCIIToUTF16("foo.com/dir/another/a"), string16(), true, short_4,
384          arraysize(short_4));
385
386  // Exact matches should always be best no matter how much more another match
387  // has been typed.
388  const std::string short_5a[] = {
389    "http://gooey/",
390    "http://www.google.com/",
391    "http://go/",
392  };
393  const std::string short_5b[] = {
394    "http://go/",
395    "http://gooey/",
396    "http://www.google.com/",
397  };
398  RunTest(ASCIIToUTF16("g"), string16(), false, short_5a, arraysize(short_5a));
399  RunTest(ASCIIToUTF16("go"), string16(), false, short_5b, arraysize(short_5b));
400}
401
402TEST_F(HistoryURLProviderTest, CullRedirects) {
403  // URLs we will be using, plus the visit counts they will initially get
404  // (the redirect set below will also increment the visit counts). We want
405  // the results to be in A,B,C order. Note also that our visit counts are
406  // all high enough so that domain synthesizing won't get triggered.
407  struct TestCase {
408    const char* url;
409    int count;
410  } test_cases[] = {
411    {"http://redirects/A", 30},
412    {"http://redirects/B", 20},
413    {"http://redirects/C", 10}
414  };
415  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
416    history_service_->AddPageWithDetails(GURL(test_cases[i].url),
417        UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
418        Time::Now(), false, history::SOURCE_BROWSED);
419  }
420
421  // Create a B->C->A redirect chain, but set the visit counts such that they
422  // will appear in A,B,C order in the results. The autocomplete query will
423  // search for the most recent visit when looking for redirects, so this will
424  // be found even though the previous visits had no redirects.
425  history::RedirectList redirects_to_a;
426  redirects_to_a.push_back(GURL(test_cases[1].url));
427  redirects_to_a.push_back(GURL(test_cases[2].url));
428  redirects_to_a.push_back(GURL(test_cases[0].url));
429  history_service_->AddPage(GURL(test_cases[0].url), base::Time::Now(),
430      NULL, 0, GURL(), redirects_to_a, content::PAGE_TRANSITION_TYPED,
431      history::SOURCE_BROWSED, true);
432
433  // Because all the results are part of a redirect chain with other results,
434  // all but the first one (A) should be culled. We should get the default
435  // "what you typed" result, plus this one.
436  const string16 typing(ASCIIToUTF16("http://redirects/"));
437  const std::string expected_results[] = {
438    UTF16ToUTF8(typing),
439    test_cases[0].url,
440  };
441  RunTest(typing, string16(), true, expected_results,
442          arraysize(expected_results));
443}
444
445TEST_F(HistoryURLProviderTest, WhatYouTyped) {
446  // Make sure we suggest a What You Typed match at the right times.
447  RunTest(ASCIIToUTF16("wytmatch"), string16(), false, NULL, 0);
448  RunTest(ASCIIToUTF16("wytmatch foo bar"), string16(), false, NULL, 0);
449  RunTest(ASCIIToUTF16("wytmatch+foo+bar"), string16(), false, NULL, 0);
450  RunTest(ASCIIToUTF16("wytmatch+foo+bar.com"), string16(), false, NULL, 0);
451
452  const std::string results_1[] = {"http://www.wytmatch.com/"};
453  RunTest(ASCIIToUTF16("wytmatch"), ASCIIToUTF16("com"), false, results_1,
454          arraysize(results_1));
455
456  const std::string results_2[] = {"http://wytmatch%20foo%20bar/"};
457  RunTest(ASCIIToUTF16("http://wytmatch foo bar"), string16(), false, results_2,
458          arraysize(results_2));
459
460  const std::string results_3[] = {"https://wytmatch%20foo%20bar/"};
461  RunTest(ASCIIToUTF16("https://wytmatch foo bar"), string16(), false,
462          results_3, arraysize(results_3));
463}
464
465TEST_F(HistoryURLProviderTest, Fixup) {
466  // Test for various past crashes we've had.
467  RunTest(ASCIIToUTF16("\\"), string16(), false, NULL, 0);
468  RunTest(ASCIIToUTF16("#"), string16(), false, NULL, 0);
469  RunTest(ASCIIToUTF16("%20"), string16(), false, NULL, 0);
470  const std::string fixup_crash[] = {"http://%EF%BD%A5@s/"};
471  RunTest(WideToUTF16(L"\uff65@s"), string16(), false, fixup_crash,
472          arraysize(fixup_crash));
473  RunTest(WideToUTF16(L"\u2015\u2015@ \uff7c"), string16(), false, NULL, 0);
474
475  // Fixing up "file:" should result in an inline autocomplete offset of just
476  // after "file:", not just after "file://".
477  const string16 input_1(ASCIIToUTF16("file:"));
478  const std::string fixup_1[] = {"file:///C:/foo.txt"};
479  ASSERT_NO_FATAL_FAILURE(RunTest(input_1, string16(), false, fixup_1,
480                                  arraysize(fixup_1)));
481  EXPECT_EQ(ASCIIToUTF16("///C:/foo.txt"),
482            matches_.front().inline_autocompletion);
483
484  // Fixing up "http:/" should result in an inline autocomplete offset of just
485  // after "http:/", not just after "http:".
486  const string16 input_2(ASCIIToUTF16("http:/"));
487  const std::string fixup_2[] = {
488    "http://bogussite.com/a",
489    "http://bogussite.com/b",
490    "http://bogussite.com/c",
491  };
492  ASSERT_NO_FATAL_FAILURE(RunTest(input_2, string16(), false, fixup_2,
493                                  arraysize(fixup_2)));
494  EXPECT_EQ(ASCIIToUTF16("/bogussite.com/a"),
495            matches_.front().inline_autocompletion);
496
497  // Adding a TLD to a small number like "56" should result in "www.56.com"
498  // rather than "0.0.0.56.com".
499  const std::string fixup_3[] = {"http://www.56.com/"};
500  RunTest(ASCIIToUTF16("56"), ASCIIToUTF16("com"), true, fixup_3,
501          arraysize(fixup_3));
502
503  // An input looks like a IP address like "127.0.0.1" should result in
504  // "http://127.0.0.1/".
505  const std::string fixup_4[] = {"http://127.0.0.1/"};
506  RunTest(ASCIIToUTF16("127.0.0.1"), string16(), false, fixup_4,
507          arraysize(fixup_4));
508
509  // An number "17173" should result in "http://www.17173.com/" in db.
510  const std::string fixup_5[] = {"http://www.17173.com/"};
511  RunTest(ASCIIToUTF16("17173"), string16(), false, fixup_5,
512          arraysize(fixup_5));
513}
514
515// Make sure the results for the input 'p' don't change between the first and
516// second passes.
517TEST_F(HistoryURLProviderTest, EmptyVisits) {
518  // Wait for history to create the in memory DB.
519  profile_->BlockUntilHistoryProcessesPendingRequests();
520
521  AutocompleteInput input(ASCIIToUTF16("p"), string16::npos, string16(), GURL(),
522                          AutocompleteInput::INVALID_SPEC, false, false, true,
523                          AutocompleteInput::ALL_MATCHES);
524  autocomplete_->Start(input, false);
525  // HistoryURLProvider shouldn't be done (waiting on async results).
526  EXPECT_FALSE(autocomplete_->done());
527
528  // We should get back an entry for pandora.
529  matches_ = autocomplete_->matches();
530  ASSERT_GT(matches_.size(), 0u);
531  EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
532  int pandora_relevance = matches_[0].relevance;
533
534  // Run the message loop. When |autocomplete_| finishes the loop is quit.
535  base::MessageLoop::current()->Run();
536  EXPECT_TRUE(autocomplete_->done());
537  matches_ = autocomplete_->matches();
538  ASSERT_GT(matches_.size(), 0u);
539  EXPECT_EQ(GURL("http://pandora.com/"), matches_[0].destination_url);
540  EXPECT_EQ(pandora_relevance, matches_[0].relevance);
541}
542
543TEST_F(HistoryURLProviderTestNoDB, NavigateWithoutDB) {
544  // Ensure that we will still produce matches for navigation when there is no
545  // database.
546  std::string navigation_1[] = {"http://test.com/"};
547  RunTest(ASCIIToUTF16("test.com"), string16(), false, navigation_1,
548          arraysize(navigation_1));
549
550  std::string navigation_2[] = {"http://slash/"};
551  RunTest(ASCIIToUTF16("slash"), string16(), false, navigation_2,
552          arraysize(navigation_2));
553
554  RunTest(ASCIIToUTF16("this is a query"), string16(), false, NULL, 0);
555}
556
557TEST_F(HistoryURLProviderTest, DontAutocompleteOnTrailingWhitespace) {
558  AutocompleteInput input(ASCIIToUTF16("slash "), string16::npos, string16(),
559                          GURL(), AutocompleteInput::INVALID_SPEC, false, false,
560                          true, AutocompleteInput::ALL_MATCHES);
561  autocomplete_->Start(input, false);
562  if (!autocomplete_->done())
563    base::MessageLoop::current()->Run();
564
565  // None of the matches should attempt to autocomplete.
566  matches_ = autocomplete_->matches();
567  for (size_t i = 0; i < matches_.size(); ++i)
568    EXPECT_TRUE(matches_[i].inline_autocompletion.empty());
569}
570
571TEST_F(HistoryURLProviderTest, TreatEmailsAsSearches) {
572  // Visiting foo.com should not make this string be treated as a navigation.
573  // That means the result should be scored around 1200 ("what you typed")
574  // and not 1400+.
575  const std::string expected[] = {"http://user@foo.com/"};
576  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("user@foo.com"), string16(),
577                                  false, expected, arraysize(expected)));
578  EXPECT_LE(1200, matches_[0].relevance);
579  EXPECT_LT(matches_[0].relevance, 1210);
580}
581
582TEST_F(HistoryURLProviderTest, IntranetURLsWithPaths) {
583  struct TestCase {
584    const char* input;
585    int relevance;
586  } test_cases[] = {
587    { "fooey", 0 },
588    { "fooey/", 1200 },     // 1200 for URL would still navigate by default.
589    { "fooey/a", 1200 },    // 1200 for UNKNOWN would not.
590    { "fooey/a b", 1200 },  // Also UNKNOWN.
591    { "gooey", 1410 },
592    { "gooey/", 1410 },
593    { "gooey/a", 1400 },
594    { "gooey/a b", 1400 },
595  };
596  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
597    SCOPED_TRACE(test_cases[i].input);
598    if (test_cases[i].relevance == 0) {
599      RunTest(ASCIIToUTF16(test_cases[i].input), string16(), false, NULL, 0);
600    } else {
601      const std::string output[] = {
602        URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec()
603      };
604      ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16(test_cases[i].input),
605                              string16(), false, output, arraysize(output)));
606      // Actual relevance should be at least what test_cases expects and
607      // and no more than 10 more.
608      EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
609      EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
610    }
611  }
612}
613
614TEST_F(HistoryURLProviderTest, IntranetURLsWithRefs) {
615  struct TestCase {
616    const char* input;
617    int relevance;
618    AutocompleteInput::Type type;
619  } test_cases[] = {
620    { "gooey", 1410, AutocompleteInput::UNKNOWN },
621    { "gooey/", 1410, AutocompleteInput::URL },
622    { "gooey#", 1200, AutocompleteInput::UNKNOWN },
623    { "gooey/#", 1200, AutocompleteInput::URL },
624    { "gooey#foo", 1200, AutocompleteInput::UNKNOWN },
625    { "gooey/#foo", 1200, AutocompleteInput::URL },
626    { "gooey# foo", 1200, AutocompleteInput::UNKNOWN },
627    { "gooey/# foo", 1200, AutocompleteInput::URL },
628  };
629  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
630    SCOPED_TRACE(test_cases[i].input);
631    const std::string output[] = {
632      URLFixerUpper::FixupURL(test_cases[i].input, std::string()).spec()
633    };
634    AutocompleteInput::Type type;
635    ASSERT_NO_FATAL_FAILURE(
636        RunTest(ASCIIToUTF16(test_cases[i].input),
637                string16(), false, output, arraysize(output), &type));
638    // Actual relevance should be at least what test_cases expects and
639    // and no more than 10 more.
640    EXPECT_LE(test_cases[i].relevance, matches_[0].relevance);
641    EXPECT_LT(matches_[0].relevance, test_cases[i].relevance + 10);
642    // Input type should be what we expect.  This is important because
643    // this provider counts on SearchProvider to give queries a relevance
644    // score >1200 for UNKNOWN inputs and <1200 for URL inputs.  (That's
645    // already tested in search_provider_unittest.cc.)  For this test
646    // here to test that the user sees the correct behavior, it needs
647    // to check that the input type was identified correctly.
648    EXPECT_EQ(test_cases[i].type, type);
649  }
650}
651
652// Makes sure autocompletion happens for intranet sites that have been
653// previoulsy visited.
654TEST_F(HistoryURLProviderTest, IntranetURLCompletion) {
655  sort_matches_ = true;
656
657  const std::string expected1[] = {
658    "http://intra/three",
659    "http://intra/two",
660  };
661  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/t"), string16(), false,
662                                  expected1, arraysize(expected1)));
663  EXPECT_LE(1410, matches_[0].relevance);
664  EXPECT_LT(matches_[0].relevance, 1420);
665  EXPECT_EQ(matches_[0].relevance - 1, matches_[1].relevance);
666
667  const std::string expected2[] = {
668    "http://moo/b",
669    "http://moo/bar",
670  };
671  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("moo/b"), string16(), false,
672                                  expected2, arraysize(expected2)));
673  // The url what you typed match should be around 1400, otherwise the
674  // search what you typed match is going to be first.
675  EXPECT_LE(1400, matches_[0].relevance);
676  EXPECT_LT(matches_[0].relevance, 1410);
677
678  const std::string expected3[] = {
679    "http://intra/one",
680    "http://intra/three",
681    "http://intra/two",
682  };
683  RunTest(ASCIIToUTF16("intra"), string16(), false, expected3,
684          arraysize(expected3));
685
686  const std::string expected4[] = {
687    "http://intra/one",
688    "http://intra/three",
689    "http://intra/two",
690  };
691  RunTest(ASCIIToUTF16("intra/"), string16(), false, expected4,
692          arraysize(expected4));
693
694  const std::string expected5[] = {
695    "http://intra/one",
696  };
697  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/o"), string16(), false,
698                                  expected5, arraysize(expected5)));
699  EXPECT_LE(1410, matches_[0].relevance);
700  EXPECT_LT(matches_[0].relevance, 1420);
701
702  const std::string expected6[] = {
703    "http://intra/x",
704  };
705  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("intra/x"), string16(), false,
706                                  expected6, arraysize(expected6)));
707  EXPECT_LE(1400, matches_[0].relevance);
708  EXPECT_LT(matches_[0].relevance, 1410);
709
710  const std::string expected7[] = {
711    "http://typedhost/untypedpath",
712  };
713  ASSERT_NO_FATAL_FAILURE(RunTest(ASCIIToUTF16("typedhost/untypedpath"),
714      string16(), false, expected7, arraysize(expected7)));
715  EXPECT_LE(1400, matches_[0].relevance);
716  EXPECT_LT(matches_[0].relevance, 1410);
717}
718
719TEST_F(HistoryURLProviderTest, CrashDueToFixup) {
720  // This test passes if we don't crash.  The results don't matter.
721  const char* const test_cases[] = {
722    "//c",
723    "\\@st"
724  };
725  for (size_t i = 0; i < arraysize(test_cases); ++i) {
726    AutocompleteInput input(ASCIIToUTF16(test_cases[i]), string16::npos,
727                            string16(), GURL(), AutocompleteInput::INVALID_SPEC,
728                            false, false, true, AutocompleteInput::ALL_MATCHES);
729    autocomplete_->Start(input, false);
730    if (!autocomplete_->done())
731      base::MessageLoop::current()->Run();
732  }
733}
734
735TEST_F(HistoryURLProviderTest, CullSearchResults) {
736  // Set up a default search engine.
737  TemplateURLData data;
738  data.SetKeyword(ASCIIToUTF16("TestEngine"));
739  data.SetURL("http://testsearch.com/?q={searchTerms}");
740  TemplateURLService* template_url_service =
741      TemplateURLServiceFactory::GetForProfile(profile_.get());
742  TemplateURL* template_url = new TemplateURL(profile_.get(), data);
743  template_url_service->Add(template_url);
744  template_url_service->SetDefaultSearchProvider(template_url);
745  template_url_service->Load();
746
747  // URLs we will be using, plus the visit counts they will initially get
748  // (the redirect set below will also increment the visit counts). We want
749  // the results to be in A,B,C order. Note also that our visit counts are
750  // all high enough so that domain synthesizing won't get triggered.
751  struct TestCase {
752    const char* url;
753    int count;
754  } test_cases[] = {
755    {"https://testsearch.com/", 30},
756    {"https://testsearch.com/?q=foobar", 20},
757    {"http://foobar.com/", 10}
758  };
759  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
760    history_service_->AddPageWithDetails(GURL(test_cases[i].url),
761        UTF8ToUTF16("Title"), test_cases[i].count, test_cases[i].count,
762        Time::Now(), false, history::SOURCE_BROWSED);
763  }
764
765  // We should not see search URLs when typing a previously used query.
766  const std::string expected_when_searching_query[] = {
767    test_cases[2].url
768  };
769  RunTest(ASCIIToUTF16("foobar"), string16(), true,
770      expected_when_searching_query, arraysize(expected_when_searching_query));
771
772  // We should not see search URLs when typing the search engine name.
773  const std::string expected_when_searching_site[] = {
774    test_cases[0].url
775  };
776  RunTest(ASCIIToUTF16("testsearch"), string16(), true,
777      expected_when_searching_site, arraysize(expected_when_searching_site));
778}
779
780TEST_F(HistoryURLProviderTest, SuggestExactInput) {
781  const size_t npos = std::string::npos;
782  struct TestCase {
783    // Inputs:
784    const char* input;
785    bool trim_http;
786    // Expected Outputs:
787    const char* contents;
788    // Offsets of the ACMatchClassifications, terminated by npos.
789    size_t offsets[3];
790    // The index of the ACMatchClassification that should have the MATCH bit
791    // set, npos if no ACMatchClassification should have the MATCH bit set.
792    size_t match_classification_index;
793  } test_cases[] = {
794    { "http://www.somesite.com", false,
795      "http://www.somesite.com", {0, npos, npos}, 0 },
796    { "www.somesite.com", true,
797      "www.somesite.com", {0, npos, npos}, 0 },
798    { "www.somesite.com", false,
799      "http://www.somesite.com", {0, 7, npos}, 1 },
800    { "somesite.com", true,
801      "somesite.com", {0, npos, npos}, 0 },
802    { "somesite.com", false,
803      "http://somesite.com", {0, 7, npos}, 1 },
804    { "w", true,
805      "w", {0, npos, npos}, 0 },
806    { "w", false,
807      "http://w", {0, 7, npos}, 1 },
808    { "w.com", true,
809      "w.com", {0, npos, npos}, 0 },
810    { "w.com", false,
811      "http://w.com", {0, 7, npos}, 1 },
812    { "www.w.com", true,
813      "www.w.com", {0, npos, npos}, 0 },
814    { "www.w.com", false,
815      "http://www.w.com", {0, 7, npos}, 1 },
816    { "view-source:www.w.com/", true,
817      "view-source:www.w.com", {0, npos, npos}, npos },
818    { "view-source:www.w.com/", false,
819      "view-source:http://www.w.com", {0, npos, npos}, npos },
820    { "view-source:http://www.w.com/", false,
821      "view-source:http://www.w.com", {0, npos, npos}, 0 },
822    { "   view-source:", true,
823      "view-source:", {0, npos, npos}, 0 },
824    { "http:////////w.com", false,
825      "http://w.com", {0, npos, npos}, npos },
826    { "    http:////////www.w.com", false,
827      "http://www.w.com", {0, npos, npos}, npos },
828    { "http:a///www.w.com", false,
829      "http://a///www.w.com", {0, npos, npos}, npos },
830    { "mailto://a@b.com", true,
831      "mailto://a@b.com", {0, npos, npos}, 0 },
832    { "mailto://a@b.com", false,
833      "mailto://a@b.com", {0, npos, npos}, 0 },
834  };
835  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
836    SCOPED_TRACE(testing::Message() << "Index " << i << " input: "
837                                    << test_cases[i].input << ", trim_http: "
838                                    << test_cases[i].trim_http);
839
840    AutocompleteInput input(ASCIIToUTF16(test_cases[i].input), string16::npos,
841                            string16(), GURL("about:blank"),
842                            AutocompleteInput::INVALID_SPEC, false, false, true,
843                            AutocompleteInput::ALL_MATCHES);
844    AutocompleteMatch match = HistoryURLProvider::SuggestExactInput(
845        autocomplete_.get(), input, test_cases[i].trim_http);
846    EXPECT_EQ(ASCIIToUTF16(test_cases[i].contents), match.contents);
847    for (size_t match_index = 0; match_index < match.contents_class.size();
848         ++match_index) {
849      EXPECT_EQ(test_cases[i].offsets[match_index],
850                match.contents_class[match_index].offset);
851      EXPECT_EQ(ACMatchClassification::URL |
852                (match_index == test_cases[i].match_classification_index ?
853                 ACMatchClassification::MATCH : 0),
854                match.contents_class[match_index].style);
855    }
856    EXPECT_EQ(npos, test_cases[i].offsets[match.contents_class.size()]);
857  }
858}
859