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