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