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