history_quick_provider_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_quick_provider.h"
6
7#include <algorithm>
8#include <functional>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/memory/scoped_ptr.h"
14#include "base/message_loop.h"
15#include "base/prefs/pref_service.h"
16#include "base/utf_string_conversions.h"
17#include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
18#include "chrome/browser/autocomplete/autocomplete_result.h"
19#include "chrome/browser/autocomplete/history_url_provider.h"
20#include "chrome/browser/history/history_service.h"
21#include "chrome/browser/history/history_service_factory.h"
22#include "chrome/browser/history/in_memory_url_index.h"
23#include "chrome/browser/history/url_database.h"
24#include "chrome/browser/history/url_index_private_data.h"
25#include "chrome/browser/search_engines/template_url.h"
26#include "chrome/browser/search_engines/template_url_service.h"
27#include "chrome/browser/search_engines/template_url_service_factory.h"
28#include "chrome/common/pref_names.h"
29#include "chrome/test/base/testing_browser_process.h"
30#include "chrome/test/base/testing_profile.h"
31#include "chrome/test/base/ui_test_utils.h"
32#include "content/public/test/test_browser_thread.h"
33#include "testing/gtest/include/gtest/gtest.h"
34
35using base::Time;
36using base::TimeDelta;
37
38using content::BrowserThread;
39
40struct TestURLInfo {
41  std::string url;
42  std::string title;
43  int visit_count;
44  int typed_count;
45  int days_from_now;
46} quick_test_db[] = {
47  {"http://www.google.com/", "Google", 3, 3, 0},
48  {"http://slashdot.org/favorite_page.html", "Favorite page", 200, 100, 0},
49  {"http://kerneltrap.org/not_very_popular.html", "Less popular", 4, 0, 0},
50  {"http://freshmeat.net/unpopular.html", "Unpopular", 1, 1, 0},
51  {"http://news.google.com/?ned=us&topic=n", "Google News - U.S.", 2, 2, 0},
52  {"http://news.google.com/", "Google News", 1, 1, 0},
53  {"http://foo.com/", "Dir", 200, 100, 0},
54  {"http://foo.com/dir/", "Dir", 2, 1, 10},
55  {"http://foo.com/dir/another/", "Dir", 5, 10, 0},
56  {"http://foo.com/dir/another/again/", "Dir", 5, 1, 0},
57  {"http://foo.com/dir/another/again/myfile.html", "File", 3, 2, 0},
58  {"http://visitedest.com/y/a", "VA", 10, 1, 20},
59  {"http://visitedest.com/y/b", "VB", 9, 1, 20},
60  {"http://visitedest.com/x/c", "VC", 8, 1, 20},
61  {"http://visitedest.com/x/d", "VD", 7, 1, 20},
62  {"http://visitedest.com/y/e", "VE", 6, 1, 20},
63  {"http://typeredest.com/y/a", "TA", 3, 5, 0},
64  {"http://typeredest.com/y/b", "TB", 3, 4, 0},
65  {"http://typeredest.com/x/c", "TC", 3, 3, 0},
66  {"http://typeredest.com/x/d", "TD", 3, 2, 0},
67  {"http://typeredest.com/y/e", "TE", 3, 1, 0},
68  {"http://daysagoest.com/y/a", "DA", 1, 1, 0},
69  {"http://daysagoest.com/y/b", "DB", 1, 1, 1},
70  {"http://daysagoest.com/x/c", "DC", 1, 1, 2},
71  {"http://daysagoest.com/x/d", "DD", 1, 1, 3},
72  {"http://daysagoest.com/y/e", "DE", 1, 1, 4},
73  {"http://abcdefghixyzjklmnopqrstuvw.com/a", "", 3, 1, 0},
74  {"http://spaces.com/path%20with%20spaces/foo.html", "Spaces", 2, 2, 0},
75  {"http://abcdefghijklxyzmnopqrstuvw.com/a", "", 3, 1, 0},
76  {"http://abcdefxyzghijklmnopqrstuvw.com/a", "", 3, 1, 0},
77  {"http://abcxyzdefghijklmnopqrstuvw.com/a", "", 3, 1, 0},
78  {"http://xyzabcdefghijklmnopqrstuvw.com/a", "", 3, 1, 0},
79  {"http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice",
80   "Dogs & Cats & Mice & Other Animals", 1, 1, 0},
81  {"https://monkeytrap.org/", "", 3, 1, 0},
82  {"http://popularsitewithpathonly.com/moo",
83   "popularsitewithpathonly.com/moo", 50, 50, 0},
84  {"http://popularsitewithroot.com/", "popularsitewithroot.com", 50, 50, 0},
85  {"http://testsearch.com/?q=thequery", "Test Search Engine", 10, 10, 0},
86  {"http://testsearch.com/", "Test Search Engine", 9, 9, 0},
87  {"http://anotherengine.com/?q=thequery", "Another Search Engine", 8, 8, 0}
88};
89
90class HistoryQuickProviderTest : public testing::Test,
91                                 public AutocompleteProviderListener {
92 public:
93  HistoryQuickProviderTest()
94      : ui_thread_(BrowserThread::UI, &message_loop_),
95        file_thread_(BrowserThread::FILE, &message_loop_) {}
96
97  // AutocompleteProviderListener:
98  virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {}
99
100 protected:
101  class SetShouldContain : public std::unary_function<const std::string&,
102                                                      std::set<std::string> > {
103   public:
104    explicit SetShouldContain(const ACMatches& matched_urls);
105
106    void operator()(const std::string& expected);
107
108    std::set<std::string> LeftOvers() const { return matches_; }
109
110   private:
111    std::set<std::string> matches_;
112  };
113
114  static ProfileKeyedService* CreateTemplateURLService(
115      content::BrowserContext* profile) {
116    return new TemplateURLService(static_cast<Profile*>(profile));
117  }
118
119  virtual void SetUp();
120  virtual void TearDown();
121
122  virtual void GetTestData(size_t* data_count, TestURLInfo** test_data);
123
124  // Fills test data into the history system.
125  void FillData();
126
127  // Runs an autocomplete query on |text| and checks to see that the returned
128  // results' destination URLs match those provided. |expected_urls| does not
129  // need to be in sorted order.
130  void RunTest(const string16 text,
131               std::vector<std::string> expected_urls,
132               bool can_inline_top_result,
133               string16 expected_fill_into_edit);
134
135  // Pass-through functions to simplify our friendship with URLIndexPrivateData.
136  bool UpdateURL(const history::URLRow& row);
137
138  MessageLoopForUI message_loop_;
139  content::TestBrowserThread ui_thread_;
140  content::TestBrowserThread file_thread_;
141
142  scoped_ptr<TestingProfile> profile_;
143  HistoryService* history_service_;
144
145  ACMatches ac_matches_;  // The resulting matches after running RunTest.
146
147  scoped_refptr<HistoryQuickProvider> provider_;
148};
149
150void HistoryQuickProviderTest::SetUp() {
151  profile_.reset(new TestingProfile());
152  profile_->CreateHistoryService(true, false);
153  profile_->CreateBookmarkModel(true);
154  ui_test_utils::WaitForBookmarkModelToLoad(profile_.get());
155  profile_->BlockUntilHistoryIndexIsRefreshed();
156  history_service_ =
157      HistoryServiceFactory::GetForProfile(profile_.get(),
158                                           Profile::EXPLICIT_ACCESS);
159  EXPECT_TRUE(history_service_);
160  provider_ = new HistoryQuickProvider(this, profile_.get());
161  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
162      profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService);
163  FillData();
164}
165
166void HistoryQuickProviderTest::TearDown() {
167  provider_ = NULL;
168}
169
170bool HistoryQuickProviderTest::UpdateURL(const history::URLRow& row) {
171  history::URLDatabase* db = history_service_->InMemoryDatabase();
172  DCHECK(db);
173  EXPECT_NE(db->AddURL(row), 0);
174  history::InMemoryURLIndex* index = provider_->GetIndex();
175  DCHECK(index);
176  history::URLIndexPrivateData* private_data = index->private_data();
177  DCHECK(private_data);
178  return private_data->UpdateURL(history_service_, row, index->languages_,
179                                 index->scheme_whitelist_);
180}
181
182void HistoryQuickProviderTest::GetTestData(size_t* data_count,
183                                           TestURLInfo** test_data) {
184  DCHECK(data_count);
185  DCHECK(test_data);
186  *data_count = arraysize(quick_test_db);
187  *test_data = &quick_test_db[0];
188}
189
190void HistoryQuickProviderTest::FillData() {
191  history::URLDatabase* db = history_service_->InMemoryDatabase();
192  ASSERT_TRUE(db != NULL);
193  size_t data_count = 0;
194  TestURLInfo* test_data = NULL;
195  GetTestData(&data_count, &test_data);
196  for (size_t i = 0; i < data_count; ++i) {
197    const TestURLInfo& cur(test_data[i]);
198    const GURL current_url(cur.url);
199    Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now);
200
201    history::URLRow url_info(current_url);
202    url_info.set_id(i + 5000);
203    url_info.set_title(UTF8ToUTF16(cur.title));
204    url_info.set_visit_count(cur.visit_count);
205    url_info.set_typed_count(cur.typed_count);
206    url_info.set_last_visit(visit_time);
207    url_info.set_hidden(false);
208    UpdateURL(url_info);
209  }
210}
211
212HistoryQuickProviderTest::SetShouldContain::SetShouldContain(
213    const ACMatches& matched_urls) {
214  for (ACMatches::const_iterator iter = matched_urls.begin();
215       iter != matched_urls.end(); ++iter)
216    matches_.insert(iter->destination_url.spec());
217}
218
219void HistoryQuickProviderTest::SetShouldContain::operator()(
220    const std::string& expected) {
221  EXPECT_EQ(1U, matches_.erase(expected))
222      << "Results did not contain '" << expected << "' but should have.";
223}
224
225
226void HistoryQuickProviderTest::RunTest(const string16 text,
227                                       std::vector<std::string> expected_urls,
228                                       bool can_inline_top_result,
229                                       string16 expected_fill_into_edit) {
230  SCOPED_TRACE(text);  // Minimal hint to query being run.
231  MessageLoop::current()->RunUntilIdle();
232  AutocompleteInput input(text, string16::npos, string16(), GURL(),false, false,
233                          true, AutocompleteInput::ALL_MATCHES);
234  provider_->Start(input, false);
235  EXPECT_TRUE(provider_->done());
236
237  ac_matches_ = provider_->matches();
238
239  // We should have gotten back at most AutocompleteProvider::kMaxMatches.
240  EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches);
241
242  // If the number of expected and actual matches aren't equal then we need
243  // test no further, but let's do anyway so that we know which URLs failed.
244  EXPECT_EQ(expected_urls.size(), ac_matches_.size());
245
246  // Verify that all expected URLs were found and that all found URLs
247  // were expected.
248  std::set<std::string> leftovers =
249      for_each(expected_urls.begin(), expected_urls.end(),
250               SetShouldContain(ac_matches_)).LeftOvers();
251  EXPECT_EQ(0U, leftovers.size()) << "There were " << leftovers.size()
252      << " unexpected results, one of which was: '"
253      << *(leftovers.begin()) << "'.";
254
255  if (expected_urls.empty())
256    return;
257
258  // Verify that we got the results in the order expected.
259  int best_score = ac_matches_.begin()->relevance + 1;
260  int i = 0;
261  std::vector<std::string>::const_iterator expected = expected_urls.begin();
262  for (ACMatches::const_iterator actual = ac_matches_.begin();
263       actual != ac_matches_.end() && expected != expected_urls.end();
264       ++actual, ++expected, ++i) {
265    EXPECT_EQ(*expected, actual->destination_url.spec())
266        << "For result #" << i << " we got '" << actual->destination_url.spec()
267        << "' but expected '" << *expected << "'.";
268    EXPECT_LT(actual->relevance, best_score)
269      << "At result #" << i << " (url=" << actual->destination_url.spec()
270      << "), we noticed scores are not monotonically decreasing.";
271    best_score = actual->relevance;
272  }
273
274  if (can_inline_top_result) {
275    // When the top scorer is inline-able make sure we get the expected
276    // fill_into_edit and autocomplete offset.
277    EXPECT_EQ(expected_fill_into_edit, ac_matches_[0].fill_into_edit)
278        << "fill_into_edit was: '" << ac_matches_[0].fill_into_edit
279        << "' but we expected '" << expected_fill_into_edit << "'.";
280    size_t text_pos = expected_fill_into_edit.find(text);
281    ASSERT_NE(string16::npos, text_pos);
282    EXPECT_EQ(text_pos + text.size(),
283              ac_matches_[0].inline_autocomplete_offset);
284  } else {
285    // When the top scorer is not inline-able autocomplete offset must be npos.
286    EXPECT_EQ(string16::npos, ac_matches_[0].inline_autocomplete_offset);
287    // Also, the score must be too low to be inlineable.
288    EXPECT_LT(ac_matches_[0].relevance,
289              AutocompleteResult::kLowestDefaultScore);
290  }
291}
292
293TEST_F(HistoryQuickProviderTest, SimpleSingleMatch) {
294  std::vector<std::string> expected_urls;
295  expected_urls.push_back("http://slashdot.org/favorite_page.html");
296  RunTest(ASCIIToUTF16("slashdot"), expected_urls, true,
297          ASCIIToUTF16("slashdot.org/favorite_page.html"));
298}
299
300TEST_F(HistoryQuickProviderTest, MultiTermTitleMatch) {
301  std::vector<std::string> expected_urls;
302  expected_urls.push_back(
303      "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
304  RunTest(ASCIIToUTF16("mice other animals"), expected_urls, false,
305          ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
306}
307
308TEST_F(HistoryQuickProviderTest, NonWordLastCharacterMatch) {
309  std::string expected_url("http://slashdot.org/favorite_page.html");
310  std::vector<std::string> expected_urls;
311  expected_urls.push_back(expected_url);
312  RunTest(ASCIIToUTF16("slashdot.org/"), expected_urls, true,
313          ASCIIToUTF16("slashdot.org/favorite_page.html"));
314}
315
316TEST_F(HistoryQuickProviderTest, MultiMatch) {
317  std::vector<std::string> expected_urls;
318  // Scores high because of typed_count.
319  expected_urls.push_back("http://foo.com/");
320  // Scores high because of visit count.
321  expected_urls.push_back("http://foo.com/dir/another/");
322  // Scores high because of high visit count.
323  expected_urls.push_back("http://foo.com/dir/another/again/");
324  RunTest(ASCIIToUTF16("foo"), expected_urls, true, ASCIIToUTF16("foo.com"));
325}
326
327TEST_F(HistoryQuickProviderTest, StartRelativeMatch) {
328  std::vector<std::string> expected_urls;
329  expected_urls.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a");
330  RunTest(ASCIIToUTF16("xyz"), expected_urls, true,
331          ASCIIToUTF16("xyzabcdefghijklmnopqrstuvw.com/a"));
332}
333
334TEST_F(HistoryQuickProviderTest, PrefixOnlyMatch) {
335  std::vector<std::string> expected_urls;
336  expected_urls.push_back("http://foo.com/");
337  expected_urls.push_back("http://popularsitewithroot.com/");
338  expected_urls.push_back("http://slashdot.org/favorite_page.html");
339  RunTest(ASCIIToUTF16("http://"), expected_urls, true,
340          ASCIIToUTF16("http://foo.com"));
341}
342
343TEST_F(HistoryQuickProviderTest, EncodingMatch) {
344  std::vector<std::string> expected_urls;
345  expected_urls.push_back("http://spaces.com/path%20with%20spaces/foo.html");
346  RunTest(ASCIIToUTF16("path with spaces"), expected_urls, false,
347          ASCIIToUTF16("CANNOT AUTOCOMPLETE"));
348}
349
350TEST_F(HistoryQuickProviderTest, VisitCountMatches) {
351  std::vector<std::string> expected_urls;
352  expected_urls.push_back("http://visitedest.com/y/a");
353  expected_urls.push_back("http://visitedest.com/y/b");
354  expected_urls.push_back("http://visitedest.com/x/c");
355  RunTest(ASCIIToUTF16("visitedest"), expected_urls, true,
356          ASCIIToUTF16("visitedest.com/y/a"));
357}
358
359TEST_F(HistoryQuickProviderTest, TypedCountMatches) {
360  std::vector<std::string> expected_urls;
361  expected_urls.push_back("http://typeredest.com/y/a");
362  expected_urls.push_back("http://typeredest.com/y/b");
363  expected_urls.push_back("http://typeredest.com/x/c");
364  RunTest(ASCIIToUTF16("typeredest"), expected_urls, true,
365          ASCIIToUTF16("typeredest.com/y/a"));
366}
367
368TEST_F(HistoryQuickProviderTest, DaysAgoMatches) {
369  std::vector<std::string> expected_urls;
370  expected_urls.push_back("http://daysagoest.com/y/a");
371  expected_urls.push_back("http://daysagoest.com/y/b");
372  expected_urls.push_back("http://daysagoest.com/x/c");
373  RunTest(ASCIIToUTF16("daysagoest"), expected_urls, true,
374          ASCIIToUTF16("daysagoest.com/y/a"));
375}
376
377TEST_F(HistoryQuickProviderTest, EncodingLimitMatch) {
378  std::vector<std::string> expected_urls;
379  std::string url(
380      "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
381  // First check that a mid-word match yield no results.
382  RunTest(ASCIIToUTF16("ice"), expected_urls, false,
383          ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
384  // Then check that we get results when the match is at a word start
385  // that is present because of an encoded separate (%20 = space).
386  expected_urls.push_back(url);
387  RunTest(ASCIIToUTF16("Mice"), expected_urls, false,
388          ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"));
389  // Verify that the matches' ACMatchClassifications offsets are in range.
390  ACMatchClassifications content(ac_matches_[0].contents_class);
391  // The max offset accounts for 6 occurrences of '%20' plus the 'http://'.
392  const size_t max_offset = url.length() - ((6 * 2) + 7);
393  for (ACMatchClassifications::const_iterator citer = content.begin();
394       citer != content.end(); ++citer)
395    EXPECT_LT(citer->offset, max_offset);
396  ACMatchClassifications description(ac_matches_[0].description_class);
397  std::string page_title("Dogs & Cats & Mice & Other Animals");
398  for (ACMatchClassifications::const_iterator diter = description.begin();
399       diter != description.end(); ++diter)
400    EXPECT_LT(diter->offset, page_title.length());
401}
402
403TEST_F(HistoryQuickProviderTest, Spans) {
404  // Test SpansFromTermMatch
405  history::TermMatches matches_a;
406  // Simulates matches: '.xx.xxx..xx...xxxxx..' which will test no match at
407  // either beginning or end as well as adjacent matches.
408  matches_a.push_back(history::TermMatch(1, 1, 2));
409  matches_a.push_back(history::TermMatch(2, 4, 3));
410  matches_a.push_back(history::TermMatch(3, 9, 1));
411  matches_a.push_back(history::TermMatch(3, 10, 1));
412  matches_a.push_back(history::TermMatch(4, 14, 5));
413  ACMatchClassifications spans_a =
414      HistoryQuickProvider::SpansFromTermMatch(matches_a, 20, false);
415  // ACMatch spans should be: 'NM-NM---N-M-N--M----N-'
416  ASSERT_EQ(9U, spans_a.size());
417  EXPECT_EQ(0U, spans_a[0].offset);
418  EXPECT_EQ(ACMatchClassification::NONE, spans_a[0].style);
419  EXPECT_EQ(1U, spans_a[1].offset);
420  EXPECT_EQ(ACMatchClassification::MATCH, spans_a[1].style);
421  EXPECT_EQ(3U, spans_a[2].offset);
422  EXPECT_EQ(ACMatchClassification::NONE, spans_a[2].style);
423  EXPECT_EQ(4U, spans_a[3].offset);
424  EXPECT_EQ(ACMatchClassification::MATCH, spans_a[3].style);
425  EXPECT_EQ(7U, spans_a[4].offset);
426  EXPECT_EQ(ACMatchClassification::NONE, spans_a[4].style);
427  EXPECT_EQ(9U, spans_a[5].offset);
428  EXPECT_EQ(ACMatchClassification::MATCH, spans_a[5].style);
429  EXPECT_EQ(11U, spans_a[6].offset);
430  EXPECT_EQ(ACMatchClassification::NONE, spans_a[6].style);
431  EXPECT_EQ(14U, spans_a[7].offset);
432  EXPECT_EQ(ACMatchClassification::MATCH, spans_a[7].style);
433  EXPECT_EQ(19U, spans_a[8].offset);
434  EXPECT_EQ(ACMatchClassification::NONE, spans_a[8].style);
435  // Simulates matches: 'xx.xx' which will test matches at both beginning and
436  // end.
437  history::TermMatches matches_b;
438  matches_b.push_back(history::TermMatch(1, 0, 2));
439  matches_b.push_back(history::TermMatch(2, 3, 2));
440  ACMatchClassifications spans_b =
441      HistoryQuickProvider::SpansFromTermMatch(matches_b, 5, true);
442  // ACMatch spans should be: 'M-NM-'
443  ASSERT_EQ(3U, spans_b.size());
444  EXPECT_EQ(0U, spans_b[0].offset);
445  EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL,
446            spans_b[0].style);
447  EXPECT_EQ(2U, spans_b[1].offset);
448  EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style);
449  EXPECT_EQ(3U, spans_b[2].offset);
450  EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL,
451            spans_b[2].style);
452}
453
454TEST_F(HistoryQuickProviderTest, DeleteMatch) {
455  std::vector<std::string> expected_urls;
456  expected_urls.push_back("http://slashdot.org/favorite_page.html");
457  // Fill up ac_matches_; we don't really care about the test yet.
458  RunTest(ASCIIToUTF16("slashdot"), expected_urls, true,
459          ASCIIToUTF16("slashdot.org/favorite_page.html"));
460  EXPECT_EQ(1U, ac_matches_.size());
461  provider_->DeleteMatch(ac_matches_[0]);
462  // Verify it's no longer an indexed visit.
463  expected_urls.clear();
464  RunTest(ASCIIToUTF16("slashdot"), expected_urls, true,
465          ASCIIToUTF16("NONE EXPECTED"));
466}
467
468TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) {
469  std::vector<std::string> expected_urls;
470
471  expected_urls.clear();
472  expected_urls.push_back("http://popularsitewithroot.com/");
473  // If the user enters a hostname (no path) that he/she has visited
474  // before, we should make sure that all HistoryQuickProvider results
475  // have scores less than what HistoryURLProvider will assign the
476  // URL-what-you-typed match.
477  RunTest(ASCIIToUTF16("popularsitewithroot.com"), expected_urls, true,
478          ASCIIToUTF16("popularsitewithroot.com"));
479  EXPECT_LT(ac_matches_[0].relevance,
480            HistoryURLProvider::kScoreForBestInlineableResult);
481
482  // Check that if the user didn't quite enter the full hostname, this
483  // hostname would've normally scored above the URL-what-you-typed match.
484  RunTest(ASCIIToUTF16("popularsitewithroot.c"), expected_urls, true,
485          ASCIIToUTF16("popularsitewithroot.com"));
486  EXPECT_GE(ac_matches_[0].relevance,
487            HistoryURLProvider::kScoreForWhatYouTypedResult);
488
489  expected_urls.clear();
490  expected_urls.push_back("http://popularsitewithpathonly.com/moo");
491  // If the user enters a hostname of a host that he/she has visited
492  // but never visited the root page of, we should make sure that all
493  // HistoryQuickProvider results have scores less than what the
494  // HistoryURLProvider will assign the URL-what-you-typed match.
495  RunTest(ASCIIToUTF16("popularsitewithpathonly.com"), expected_urls, true,
496          ASCIIToUTF16("popularsitewithpathonly.com/moo"));
497  EXPECT_LT(ac_matches_[0].relevance,
498            HistoryURLProvider::kScoreForWhatYouTypedResult);
499
500  // Verify the same thing happens if the user adds a / to end of the
501  // hostname.
502  RunTest(ASCIIToUTF16("popularsitewithpathonly.com/"), expected_urls, true,
503          ASCIIToUTF16("popularsitewithpathonly.com/moo"));
504  EXPECT_LT(ac_matches_[0].relevance,
505            HistoryURLProvider::kScoreForWhatYouTypedResult);
506
507
508  // Check that if the user didn't quite enter the full hostname, this
509  // page would've normally scored above the URL-what-you-typed match.
510  RunTest(ASCIIToUTF16("popularsitewithpathonly.co"), expected_urls, true,
511          ASCIIToUTF16("popularsitewithpathonly.com/moo"));
512  EXPECT_GE(ac_matches_[0].relevance,
513            HistoryURLProvider::kScoreForWhatYouTypedResult);
514
515  // If the user enters a hostname + path that he/she has not visited
516  // before (but visited other things on the host), we can allow
517  // inline autocompletions.
518  RunTest(ASCIIToUTF16("popularsitewithpathonly.com/mo"), expected_urls, true,
519          ASCIIToUTF16("popularsitewithpathonly.com/moo"));
520  EXPECT_GE(ac_matches_[0].relevance,
521            HistoryURLProvider::kScoreForWhatYouTypedResult);
522
523  // If the user enters a hostname + path that he/she has visited
524  // before, we should make sure that all HistoryQuickProvider results
525  // have scores less than what the HistoryURLProvider will assign
526  // the URL-what-you-typed match.
527  RunTest(ASCIIToUTF16("popularsitewithpathonly.com/moo"),
528          expected_urls, true,
529          ASCIIToUTF16("popularsitewithpathonly.com/moo"));
530  EXPECT_LT(ac_matches_[0].relevance,
531            HistoryURLProvider::kScoreForBestInlineableResult);
532}
533
534TEST_F(HistoryQuickProviderTest, CullSearchResults) {
535  // Set up a default search engine.
536  TemplateURLData data;
537  data.SetKeyword(ASCIIToUTF16("TestEngine"));
538  data.SetURL("http://testsearch.com/?q={searchTerms}");
539  TemplateURLService* template_url_service =
540      TemplateURLServiceFactory::GetForProfile(profile_.get());
541  TemplateURL* template_url = new TemplateURL(profile_.get(), data);
542  template_url_service->Add(template_url);
543  template_url_service->SetDefaultSearchProvider(template_url);
544  template_url_service->Load();
545
546  // A search results page should not be returned when typing a query.
547  std::vector<std::string> expected_urls;
548  expected_urls.push_back("http://anotherengine.com/?q=thequery");
549  RunTest(ASCIIToUTF16("thequery"), expected_urls, false, string16());
550
551  // A search results page should not be returned when typing the engine URL.
552  expected_urls.clear();
553  expected_urls.push_back("http://testsearch.com/");
554  RunTest(ASCIIToUTF16("testsearch"), expected_urls, true,
555          ASCIIToUTF16("testsearch.com"));
556}
557
558// HQPOrderingTest -------------------------------------------------------------
559
560TestURLInfo ordering_test_db[] = {
561  {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3,
562      256},
563  {"http://www.amazon.com/", "amazon.com: online shopping for electronics, "
564      "apparel, computers, books, dvds & more", 20, 20, 10},
565  {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&"
566      "currentpage=83", "google images", 6, 6, 0},
567  {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0},
568  {"http://www.teamfortress.com/", "", 5, 5, 6},
569  {"http://www.rottentomatoes.com/", "", 3, 3, 7},
570  {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9},
571  {"https://www.emigrantdirect.com/", "high interest savings account, high "
572      "yield savings - emigrantdirect", 5, 5, 3},
573  {"http://store.steampowered.com/", "", 6, 6, 1},
574  {"http://techmeme.com/", "techmeme", 111, 110, 4},
575  {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15,
576      2},
577  {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1},
578  {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player "
579      "index", 100, 45, 219},
580  {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3,
581      6},
582  {"http://translate.google.com/", "google translate", 3, 3, 0},
583  {"http://arstechnica.com/", "ars technica", 3, 3, 3},
584  {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - "
585      "rotten tomatoes", 3, 3, 7},
586  {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
587      "gaming news", 26, 25, 3},
588  {"http://metaleater.com/", "metaleater", 4, 3, 8},
589  {"http://half.com/", "half.com: textbooks , books , music , movies , games , "
590      "video games", 4, 4, 6},
591  {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
592      "gaming news", 8, 5, 9},
593};
594
595class HQPOrderingTest : public HistoryQuickProviderTest {
596 protected:
597  virtual void GetTestData(size_t* data_count,
598                           TestURLInfo** test_data) OVERRIDE;
599};
600
601void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) {
602  DCHECK(data_count);
603  DCHECK(test_data);
604  *data_count = arraysize(ordering_test_db);
605  *test_data = &ordering_test_db[0];
606}
607
608TEST_F(HQPOrderingTest, TEMatch) {
609  std::vector<std::string> expected_urls;
610  expected_urls.push_back("http://techmeme.com/");
611  expected_urls.push_back("http://www.teamliquid.net/");
612  expected_urls.push_back("http://www.teamliquid.net/tlpd");
613  RunTest(ASCIIToUTF16("te"), expected_urls, true,
614          ASCIIToUTF16("techmeme.com"));
615}
616
617TEST_F(HQPOrderingTest, TEAMatch) {
618  std::vector<std::string> expected_urls;
619  expected_urls.push_back("http://www.teamliquid.net/");
620  expected_urls.push_back("http://www.teamliquid.net/tlpd");
621  expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players");
622  RunTest(ASCIIToUTF16("tea"), expected_urls, true,
623          ASCIIToUTF16("www.teamliquid.net"));
624}
625