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