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