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