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