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"
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/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/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/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"
47 using base::ASCIIToUTF16
;
49 using base::TimeDelta
;
51 using content::BrowserThread
;
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}
107 // Waits for OnURLsDeletedNotification and when run quits the supplied run loop.
108 class WaitForURLsDeletedObserver
: public history::HistoryServiceObserver
{
110 explicit WaitForURLsDeletedObserver(base::RunLoop
* runner
);
111 ~WaitForURLsDeletedObserver() override
;
114 // history::HistoryServiceObserver:
115 void OnURLsDeleted(history::HistoryService
* service
,
118 const history::URLRows
& deleted_rows
,
119 const std::set
<GURL
>& favicon_urls
) override
;
121 // Weak. Owned by our owner.
122 base::RunLoop
* runner_
;
124 DISALLOW_COPY_AND_ASSIGN(WaitForURLsDeletedObserver
);
127 WaitForURLsDeletedObserver::WaitForURLsDeletedObserver(base::RunLoop
* runner
)
131 WaitForURLsDeletedObserver::~WaitForURLsDeletedObserver() {
134 void WaitForURLsDeletedObserver::OnURLsDeleted(
135 history::HistoryService
* service
,
138 const history::URLRows
& deleted_rows
,
139 const std::set
<GURL
>& favicon_urls
) {
143 void WaitForURLsDeletedNotification(history::HistoryService
* history_service
) {
144 base::RunLoop runner
;
145 WaitForURLsDeletedObserver
observer(&runner
);
146 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
147 scoped_observer(&observer
);
148 scoped_observer
.Add(history_service
);
152 class HistoryQuickProviderTest
: public testing::Test
{
154 HistoryQuickProviderTest()
155 : ui_thread_(BrowserThread::UI
, &message_loop_
),
156 file_thread_(BrowserThread::FILE, &message_loop_
) {}
159 class SetShouldContain
: public std::unary_function
<const std::string
&,
160 std::set
<std::string
> > {
162 explicit SetShouldContain(const ACMatches
& matched_urls
);
164 void operator()(const std::string
& expected
);
166 std::set
<std::string
> LeftOvers() const { return matches_
; }
169 std::set
<std::string
> matches_
;
172 static KeyedService
* CreateTemplateURLService(
173 content::BrowserContext
* context
) {
174 Profile
* profile
= static_cast<Profile
*>(context
);
175 return new TemplateURLService(
176 profile
->GetPrefs(), make_scoped_ptr(new SearchTermsData
), NULL
,
177 scoped_ptr
<TemplateURLServiceClient
>(new ChromeTemplateURLServiceClient(
178 HistoryServiceFactory::GetForProfile(
179 profile
, ServiceAccessType::EXPLICIT_ACCESS
))),
180 NULL
, NULL
, base::Closure());
183 void SetUp() override
;
184 void TearDown() override
;
186 virtual void GetTestData(size_t* data_count
, TestURLInfo
** test_data
);
188 // Fills test data into the history system.
191 // Runs an autocomplete query on |text| and checks to see that the returned
192 // results' destination URLs match those provided. |expected_urls| does not
193 // need to be in sorted order.
194 void RunTest(const base::string16 text
,
195 bool prevent_inline_autocomplete
,
196 std::vector
<std::string
> expected_urls
,
197 bool can_inline_top_result
,
198 base::string16 expected_fill_into_edit
,
199 base::string16 autocompletion
);
201 // As above, simply with a cursor position specified.
202 void RunTestWithCursor(const base::string16 text
,
203 const size_t cursor_position
,
204 bool prevent_inline_autocomplete
,
205 std::vector
<std::string
> expected_urls
,
206 bool can_inline_top_result
,
207 base::string16 expected_fill_into_edit
,
208 base::string16 autocompletion
);
210 history::HistoryBackend
* history_backend() {
211 return history_service_
->history_backend_
.get();
214 base::MessageLoopForUI message_loop_
;
215 content::TestBrowserThread ui_thread_
;
216 content::TestBrowserThread file_thread_
;
218 scoped_ptr
<TestingProfile
> profile_
;
219 history::HistoryService
* history_service_
;
221 ACMatches ac_matches_
; // The resulting matches after running RunTest.
223 scoped_refptr
<HistoryQuickProvider
> provider_
;
226 void HistoryQuickProviderTest::SetUp() {
227 profile_
.reset(new TestingProfile());
228 ASSERT_TRUE(profile_
->CreateHistoryService(true, false));
229 profile_
->CreateBookmarkModel(true);
230 bookmarks::test::WaitForBookmarkModelToLoad(
231 BookmarkModelFactory::GetForProfile(profile_
.get()));
232 profile_
->BlockUntilHistoryIndexIsRefreshed();
233 history_service_
= HistoryServiceFactory::GetForProfile(
234 profile_
.get(), ServiceAccessType::EXPLICIT_ACCESS
);
235 EXPECT_TRUE(history_service_
);
236 InMemoryURLIndex
* index
=
237 InMemoryURLIndexFactory::GetForProfile(profile_
.get());
239 provider_
= new HistoryQuickProvider(profile_
.get(), index
);
240 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
241 profile_
.get(), &HistoryQuickProviderTest::CreateTemplateURLService
);
243 index
->RebuildFromHistory(history_backend()->db());
246 void HistoryQuickProviderTest::TearDown() {
250 void HistoryQuickProviderTest::GetTestData(size_t* data_count
,
251 TestURLInfo
** test_data
) {
254 *data_count
= arraysize(quick_test_db
);
255 *test_data
= &quick_test_db
[0];
258 void HistoryQuickProviderTest::FillData() {
259 sql::Connection
& db(history_backend()->db()->GetDB());
260 ASSERT_TRUE(db
.is_open());
262 size_t data_count
= 0;
263 TestURLInfo
* test_data
= NULL
;
264 GetTestData(&data_count
, &test_data
);
266 for (size_t i
= 0; i
< data_count
; ++i
) {
267 const TestURLInfo
& cur(test_data
[i
]);
268 Time visit_time
= Time::Now() - TimeDelta::FromDays(cur
.days_from_now
);
269 sql::Transaction
transaction(&db
);
273 std::string sql_cmd_line
= base::StringPrintf(
274 "INSERT INTO \"urls\" VALUES(%" PRIuS
", \'%s\', \'%s\', %d, %d, %"
276 i
+ 1, cur
.url
.c_str(), cur
.title
.c_str(), cur
.visit_count
,
277 cur
.typed_count
, visit_time
.ToInternalValue());
278 sql::Statement
sql_stmt(db
.GetUniqueStatement(sql_cmd_line
.c_str()));
279 EXPECT_TRUE(sql_stmt
.Run());
280 transaction
.Commit();
283 for (int j
= 0; j
< cur
.visit_count
; ++j
) {
284 // Assume earlier visits are at one-day intervals.
285 visit_time
-= TimeDelta::FromDays(1);
287 // Mark the most recent |cur.typed_count| visits as typed.
288 std::string sql_cmd_line
= base::StringPrintf(
289 "INSERT INTO \"visits\" VALUES(%" PRIuS
", %" PRIuS
", %" PRId64
291 visit_id
++, i
+ 1, visit_time
.ToInternalValue(),
292 (j
< cur
.typed_count
) ? ui::PAGE_TRANSITION_TYPED
:
293 ui::PAGE_TRANSITION_LINK
);
295 sql::Statement
sql_stmt(db
.GetUniqueStatement(sql_cmd_line
.c_str()));
296 EXPECT_TRUE(sql_stmt
.Run());
297 transaction
.Commit();
302 HistoryQuickProviderTest::SetShouldContain::SetShouldContain(
303 const ACMatches
& matched_urls
) {
304 for (ACMatches::const_iterator iter
= matched_urls
.begin();
305 iter
!= matched_urls
.end(); ++iter
)
306 matches_
.insert(iter
->destination_url
.spec());
309 void HistoryQuickProviderTest::SetShouldContain::operator()(
310 const std::string
& expected
) {
311 EXPECT_EQ(1U, matches_
.erase(expected
))
312 << "Results did not contain '" << expected
<< "' but should have.";
315 void HistoryQuickProviderTest::RunTest(
316 const base::string16 text
,
317 bool prevent_inline_autocomplete
,
318 std::vector
<std::string
> expected_urls
,
319 bool can_inline_top_result
,
320 base::string16 expected_fill_into_edit
,
321 base::string16 expected_autocompletion
) {
322 RunTestWithCursor(text
, base::string16::npos
, prevent_inline_autocomplete
,
323 expected_urls
, can_inline_top_result
,
324 expected_fill_into_edit
, expected_autocompletion
);
327 void HistoryQuickProviderTest::RunTestWithCursor(
328 const base::string16 text
,
329 const size_t cursor_position
,
330 bool prevent_inline_autocomplete
,
331 std::vector
<std::string
> expected_urls
,
332 bool can_inline_top_result
,
333 base::string16 expected_fill_into_edit
,
334 base::string16 expected_autocompletion
) {
335 SCOPED_TRACE(text
); // Minimal hint to query being run.
336 base::MessageLoop::current()->RunUntilIdle();
337 AutocompleteInput
input(text
, cursor_position
, std::string(), GURL(),
338 metrics::OmniboxEventProto::INVALID_SPEC
,
339 prevent_inline_autocomplete
, false, true, true,
340 ChromeAutocompleteSchemeClassifier(profile_
.get()));
341 provider_
->Start(input
, false, false);
342 EXPECT_TRUE(provider_
->done());
344 ac_matches_
= provider_
->matches();
346 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
347 EXPECT_LE(ac_matches_
.size(), AutocompleteProvider::kMaxMatches
);
349 // If the number of expected and actual matches aren't equal then we need
350 // test no further, but let's do anyway so that we know which URLs failed.
351 EXPECT_EQ(expected_urls
.size(), ac_matches_
.size());
353 // Verify that all expected URLs were found and that all found URLs
355 std::set
<std::string
> leftovers
=
356 for_each(expected_urls
.begin(), expected_urls
.end(),
357 SetShouldContain(ac_matches_
)).LeftOvers();
358 EXPECT_EQ(0U, leftovers
.size()) << "There were " << leftovers
.size()
359 << " unexpected results, one of which was: '"
360 << *(leftovers
.begin()) << "'.";
362 if (expected_urls
.empty())
365 // Verify that we got the results in the order expected.
366 int best_score
= ac_matches_
.begin()->relevance
+ 1;
368 std::vector
<std::string
>::const_iterator expected
= expected_urls
.begin();
369 for (ACMatches::const_iterator actual
= ac_matches_
.begin();
370 actual
!= ac_matches_
.end() && expected
!= expected_urls
.end();
371 ++actual
, ++expected
, ++i
) {
372 EXPECT_EQ(*expected
, actual
->destination_url
.spec())
373 << "For result #" << i
<< " we got '" << actual
->destination_url
.spec()
374 << "' but expected '" << *expected
<< "'.";
375 EXPECT_LT(actual
->relevance
, best_score
)
376 << "At result #" << i
<< " (url=" << actual
->destination_url
.spec()
377 << "), we noticed scores are not monotonically decreasing.";
378 best_score
= actual
->relevance
;
381 EXPECT_EQ(can_inline_top_result
, ac_matches_
[0].allowed_to_be_default_match
);
382 if (can_inline_top_result
)
383 EXPECT_EQ(expected_autocompletion
, ac_matches_
[0].inline_autocompletion
);
384 EXPECT_EQ(expected_fill_into_edit
, ac_matches_
[0].fill_into_edit
);
387 TEST_F(HistoryQuickProviderTest
, SimpleSingleMatch
) {
388 std::vector
<std::string
> expected_urls
;
389 expected_urls
.push_back("http://slashdot.org/favorite_page.html");
390 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls
, true,
391 ASCIIToUTF16("slashdot.org/favorite_page.html"),
392 ASCIIToUTF16(".org/favorite_page.html"));
395 TEST_F(HistoryQuickProviderTest
, SingleMatchWithCursor
) {
396 std::vector
<std::string
> expected_urls
;
397 expected_urls
.push_back("http://slashdot.org/favorite_page.html");
398 // With cursor after "slash", we should retrieve the desired result but it
399 // should not be allowed to be the default match.
400 RunTestWithCursor(ASCIIToUTF16("slashfavorite_page.html"), 5, false,
401 expected_urls
, false,
402 ASCIIToUTF16("slashdot.org/favorite_page.html"),
404 // If the cursor is in the middle of a valid URL suggestion, it should be
405 // allowed to be the default match. The inline completion will be empty
406 // though as no completion is necessary.
407 RunTestWithCursor(ASCIIToUTF16("slashdot.org/favorite_page.html"), 5, false,
409 ASCIIToUTF16("slashdot.org/favorite_page.html"),
413 TEST_F(HistoryQuickProviderTest
, WordBoundariesWithPunctuationMatch
) {
414 std::vector
<std::string
> expected_urls
;
415 expected_urls
.push_back("http://popularsitewithpathonly.com/moo");
416 RunTest(ASCIIToUTF16("/moo"), false, expected_urls
, false,
417 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16());
420 TEST_F(HistoryQuickProviderTest
, MultiTermTitleMatch
) {
421 std::vector
<std::string
> expected_urls
;
422 expected_urls
.push_back(
423 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
424 RunTest(ASCIIToUTF16("mice other animals"), false, expected_urls
, false,
425 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
429 TEST_F(HistoryQuickProviderTest
, NonWordLastCharacterMatch
) {
430 std::string
expected_url("http://slashdot.org/favorite_page.html");
431 std::vector
<std::string
> expected_urls
;
432 expected_urls
.push_back(expected_url
);
433 RunTest(ASCIIToUTF16("slashdot.org/"), false, expected_urls
, true,
434 ASCIIToUTF16("slashdot.org/favorite_page.html"),
435 ASCIIToUTF16("favorite_page.html"));
438 TEST_F(HistoryQuickProviderTest
, MultiMatch
) {
439 std::vector
<std::string
> expected_urls
;
440 // Scores high because of typed_count.
441 expected_urls
.push_back("http://foo.com/");
442 // Scores high because of visit count.
443 expected_urls
.push_back("http://foo.com/dir/another/");
444 // Scores high because of high visit count.
445 expected_urls
.push_back("http://foo.com/dir/another/again/");
446 RunTest(ASCIIToUTF16("foo"), false, expected_urls
, true,
447 ASCIIToUTF16("foo.com"), ASCIIToUTF16(".com"));
450 TEST_F(HistoryQuickProviderTest
, StartRelativeMatch
) {
451 std::vector
<std::string
> expected_urls
;
452 expected_urls
.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a");
453 RunTest(ASCIIToUTF16("xyza"), false, expected_urls
, true,
454 ASCIIToUTF16("xyzabcdefghijklmnopqrstuvw.com/a"),
455 ASCIIToUTF16("bcdefghijklmnopqrstuvw.com/a"));
458 TEST_F(HistoryQuickProviderTest
, EncodingMatch
) {
459 std::vector
<std::string
> expected_urls
;
460 expected_urls
.push_back("http://spaces.com/path%20with%20spaces/foo.html");
461 RunTest(ASCIIToUTF16("path with spaces"), false, expected_urls
, false,
462 ASCIIToUTF16("spaces.com/path with spaces/foo.html"),
466 TEST_F(HistoryQuickProviderTest
, ContentsClass
) {
467 std::vector
<std::string
> expected_urls
;
468 expected_urls
.push_back(
469 "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%B8%96%E7"
470 "%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7.E3.83.AB.E3.82.B5.E3.82."
471 "A4.E3.83.A6.E4.BD.93.E5.88.B6");
472 RunTest(base::UTF8ToUTF16("第二 e3"), false, expected_urls
, false,
473 base::UTF8ToUTF16("ja.wikipedia.org/wiki/第二次世界大戦#.E3.83.B4.E3."
474 "82.A7.E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD."
478 ac_matches_
[0].Validate();
480 // Verify that contents_class divides the string in the right places.
481 // [22, 24) is the "第二". All the other pairs are the "e3".
482 ACMatchClassifications
contents_class(ac_matches_
[0].contents_class
);
483 size_t expected_offsets
[] = { 0, 22, 24, 31, 33, 40, 42, 49, 51, 58, 60, 67,
485 // ScoredHistoryMatch may not highlight all the occurrences of these terms
486 // because it only highlights terms at word breaks, and it only stores word
487 // breaks up to some specified number of characters (50 at the time of this
488 // comment). This test is written flexibly so it still will pass if we
489 // increase that number in the future. Regardless, we require the first
490 // five offsets to be correct--in this example these cover at least one
491 // occurrence of each term.
492 EXPECT_LE(contents_class
.size(), arraysize(expected_offsets
));
493 EXPECT_GE(contents_class
.size(), 5u);
494 for (size_t i
= 0; i
< contents_class
.size(); ++i
)
495 EXPECT_EQ(expected_offsets
[i
], contents_class
[i
].offset
);
498 TEST_F(HistoryQuickProviderTest
, VisitCountMatches
) {
499 std::vector
<std::string
> expected_urls
;
500 expected_urls
.push_back("http://visitedest.com/y/a");
501 expected_urls
.push_back("http://visitedest.com/y/b");
502 expected_urls
.push_back("http://visitedest.com/x/c");
503 RunTest(ASCIIToUTF16("visitedest"), false, expected_urls
, true,
504 ASCIIToUTF16("visitedest.com/y/a"),
505 ASCIIToUTF16(".com/y/a"));
508 TEST_F(HistoryQuickProviderTest
, TypedCountMatches
) {
509 std::vector
<std::string
> expected_urls
;
510 expected_urls
.push_back("http://typeredest.com/y/a");
511 expected_urls
.push_back("http://typeredest.com/y/b");
512 expected_urls
.push_back("http://typeredest.com/x/c");
513 RunTest(ASCIIToUTF16("typeredest"), false, expected_urls
, true,
514 ASCIIToUTF16("typeredest.com/y/a"),
515 ASCIIToUTF16(".com/y/a"));
518 TEST_F(HistoryQuickProviderTest
, DaysAgoMatches
) {
519 std::vector
<std::string
> expected_urls
;
520 expected_urls
.push_back("http://daysagoest.com/y/a");
521 expected_urls
.push_back("http://daysagoest.com/y/b");
522 expected_urls
.push_back("http://daysagoest.com/x/c");
523 RunTest(ASCIIToUTF16("daysagoest"), false, expected_urls
, true,
524 ASCIIToUTF16("daysagoest.com/y/a"),
525 ASCIIToUTF16(".com/y/a"));
528 TEST_F(HistoryQuickProviderTest
, EncodingLimitMatch
) {
529 std::vector
<std::string
> expected_urls
;
531 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
532 // First check that a mid-word match yield no results.
533 RunTest(ASCIIToUTF16("ice"), false, expected_urls
, false,
534 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
536 // Then check that we get results when the match is at a word start
537 // that is present because of an encoded separate (%20 = space).
538 expected_urls
.push_back(url
);
539 RunTest(ASCIIToUTF16("Mice"), false, expected_urls
, false,
540 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
542 // Verify that the matches' ACMatchClassifications offsets are in range.
543 ACMatchClassifications
content(ac_matches_
[0].contents_class
);
544 // The max offset accounts for 6 occurrences of '%20' plus the 'http://'.
545 const size_t max_offset
= url
.length() - ((6 * 2) + 7);
546 for (ACMatchClassifications::const_iterator citer
= content
.begin();
547 citer
!= content
.end(); ++citer
)
548 EXPECT_LT(citer
->offset
, max_offset
);
549 ACMatchClassifications
description(ac_matches_
[0].description_class
);
550 std::string
page_title("Dogs & Cats & Mice & Other Animals");
551 for (ACMatchClassifications::const_iterator diter
= description
.begin();
552 diter
!= description
.end(); ++diter
)
553 EXPECT_LT(diter
->offset
, page_title
.length());
556 TEST_F(HistoryQuickProviderTest
, Spans
) {
557 // Test SpansFromTermMatch
558 TermMatches matches_a
;
559 // Simulates matches: '.xx.xxx..xx...xxxxx..' which will test no match at
560 // either beginning or end as well as adjacent matches.
561 matches_a
.push_back(TermMatch(1, 1, 2));
562 matches_a
.push_back(TermMatch(2, 4, 3));
563 matches_a
.push_back(TermMatch(3, 9, 1));
564 matches_a
.push_back(TermMatch(3, 10, 1));
565 matches_a
.push_back(TermMatch(4, 14, 5));
566 ACMatchClassifications spans_a
=
567 HistoryQuickProvider::SpansFromTermMatch(matches_a
, 20, false);
568 // ACMatch spans should be: 'NM-NM---N-M-N--M----N-'
569 ASSERT_EQ(9U, spans_a
.size());
570 EXPECT_EQ(0U, spans_a
[0].offset
);
571 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[0].style
);
572 EXPECT_EQ(1U, spans_a
[1].offset
);
573 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[1].style
);
574 EXPECT_EQ(3U, spans_a
[2].offset
);
575 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[2].style
);
576 EXPECT_EQ(4U, spans_a
[3].offset
);
577 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[3].style
);
578 EXPECT_EQ(7U, spans_a
[4].offset
);
579 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[4].style
);
580 EXPECT_EQ(9U, spans_a
[5].offset
);
581 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[5].style
);
582 EXPECT_EQ(11U, spans_a
[6].offset
);
583 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[6].style
);
584 EXPECT_EQ(14U, spans_a
[7].offset
);
585 EXPECT_EQ(ACMatchClassification::MATCH
, spans_a
[7].style
);
586 EXPECT_EQ(19U, spans_a
[8].offset
);
587 EXPECT_EQ(ACMatchClassification::NONE
, spans_a
[8].style
);
588 // Simulates matches: 'xx.xx' which will test matches at both beginning and
590 TermMatches matches_b
;
591 matches_b
.push_back(TermMatch(1, 0, 2));
592 matches_b
.push_back(TermMatch(2, 3, 2));
593 ACMatchClassifications spans_b
=
594 HistoryQuickProvider::SpansFromTermMatch(matches_b
, 5, true);
595 // ACMatch spans should be: 'M-NM-'
596 ASSERT_EQ(3U, spans_b
.size());
597 EXPECT_EQ(0U, spans_b
[0].offset
);
598 EXPECT_EQ(ACMatchClassification::MATCH
| ACMatchClassification::URL
,
600 EXPECT_EQ(2U, spans_b
[1].offset
);
601 EXPECT_EQ(ACMatchClassification::URL
, spans_b
[1].style
);
602 EXPECT_EQ(3U, spans_b
[2].offset
);
603 EXPECT_EQ(ACMatchClassification::MATCH
| ACMatchClassification::URL
,
607 TEST_F(HistoryQuickProviderTest
, DeleteMatch
) {
608 GURL
test_url("http://slashdot.org/favorite_page.html");
609 std::vector
<std::string
> expected_urls
;
610 expected_urls
.push_back(test_url
.spec());
611 // Fill up ac_matches_; we don't really care about the test yet.
612 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls
, true,
613 ASCIIToUTF16("slashdot.org/favorite_page.html"),
614 ASCIIToUTF16(".org/favorite_page.html"));
615 EXPECT_EQ(1U, ac_matches_
.size());
616 EXPECT_TRUE(history_backend()->GetURL(test_url
, NULL
));
617 provider_
->DeleteMatch(ac_matches_
[0]);
619 // Check that the underlying URL is deleted from the history DB (this implies
620 // that all visits are gone as well). Also verify that a deletion notification
621 // is sent, in response to which the secondary data stores (InMemoryDatabase,
622 // InMemoryURLIndex) will drop any data they might have pertaining to the URL.
623 // To ensure that the deletion has been propagated everywhere before we start
624 // verifying post-deletion states, first wait until we see the notification.
625 WaitForURLsDeletedNotification(history_service_
);
626 EXPECT_FALSE(history_backend()->GetURL(test_url
, NULL
));
628 // Just to be on the safe side, explicitly verify that we have deleted enough
629 // data so that we will not be serving the same result again.
630 expected_urls
.clear();
631 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls
, true,
632 ASCIIToUTF16("NONE EXPECTED"), base::string16());
635 TEST_F(HistoryQuickProviderTest
, PreventBeatingURLWhatYouTypedMatch
) {
636 std::vector
<std::string
> expected_urls
;
638 expected_urls
.clear();
639 expected_urls
.push_back("http://popularsitewithroot.com/");
640 // If the user enters a hostname (no path) that he/she has visited
641 // before, we should make sure that all HistoryQuickProvider results
642 // have scores less than what HistoryURLProvider will assign the
643 // URL-what-you-typed match.
644 RunTest(ASCIIToUTF16("popularsitewithroot.com"), false, expected_urls
, true,
645 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
646 EXPECT_LT(ac_matches_
[0].relevance
,
647 HistoryURLProvider::kScoreForBestInlineableResult
);
649 // Check that if the user didn't quite enter the full hostname, this
650 // hostname would've normally scored above the URL-what-you-typed match.
651 RunTest(ASCIIToUTF16("popularsitewithroot.c"), false, expected_urls
, true,
652 ASCIIToUTF16("popularsitewithroot.com"),
654 EXPECT_GE(ac_matches_
[0].relevance
,
655 HistoryURLProvider::kScoreForWhatYouTypedResult
);
657 expected_urls
.clear();
658 expected_urls
.push_back("http://popularsitewithpathonly.com/moo");
659 // If the user enters a hostname of a host that he/she has visited
660 // but never visited the root page of, we should make sure that all
661 // HistoryQuickProvider results have scores less than what the
662 // HistoryURLProvider will assign the URL-what-you-typed match.
663 RunTest(ASCIIToUTF16("popularsitewithpathonly.com"), false, expected_urls
,
665 ASCIIToUTF16("popularsitewithpathonly.com/moo"),
666 ASCIIToUTF16("/moo"));
667 EXPECT_LT(ac_matches_
[0].relevance
,
668 HistoryURLProvider::kScoreForUnvisitedIntranetResult
);
670 // Verify the same thing happens if the user adds a / to end of the
672 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/"), false, expected_urls
,
673 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"),
674 ASCIIToUTF16("moo"));
675 EXPECT_LT(ac_matches_
[0].relevance
,
676 HistoryURLProvider::kScoreForUnvisitedIntranetResult
);
678 // Check that if the user didn't quite enter the full hostname, this
679 // page would've normally scored above the URL-what-you-typed match.
680 RunTest(ASCIIToUTF16("popularsitewithpathonly.co"), false, expected_urls
,
681 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"),
682 ASCIIToUTF16("m/moo"));
683 EXPECT_GE(ac_matches_
[0].relevance
,
684 HistoryURLProvider::kScoreForWhatYouTypedResult
);
686 // If the user enters a hostname + path that he/she has not visited
687 // before (but visited other things on the host), we can allow
688 // inline autocompletions.
689 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/mo"), false, expected_urls
,
691 ASCIIToUTF16("popularsitewithpathonly.com/moo"),
693 EXPECT_GE(ac_matches_
[0].relevance
,
694 HistoryURLProvider::kScoreForWhatYouTypedResult
);
696 // If the user enters a hostname + path that he/she has visited
697 // before, we should make sure that all HistoryQuickProvider results
698 // have scores less than what the HistoryURLProvider will assign
699 // the URL-what-you-typed match.
700 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/moo"), false,
702 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16());
703 EXPECT_LT(ac_matches_
[0].relevance
,
704 HistoryURLProvider::kScoreForBestInlineableResult
);
707 TEST_F(HistoryQuickProviderTest
, PreventInlineAutocomplete
) {
708 std::vector
<std::string
> expected_urls
;
709 expected_urls
.push_back("http://popularsitewithroot.com/");
711 // Check that the desired URL is normally allowed to be the default match
712 // against input that is a prefex of the URL.
713 RunTest(ASCIIToUTF16("popularsitewithr"), false, expected_urls
, true,
714 ASCIIToUTF16("popularsitewithroot.com"),
715 ASCIIToUTF16("oot.com"));
717 // Check that it's not allowed to be the default match if
718 // prevent_inline_autocomplete is true.
719 RunTest(ASCIIToUTF16("popularsitewithr"), true, expected_urls
, false,
720 ASCIIToUTF16("popularsitewithroot.com"),
721 ASCIIToUTF16("oot.com"));
723 // But the exact hostname can still match even if prevent inline autocomplete
724 // is true. i.e., there's no autocompletion necessary; this is effectively
725 // URL-what-you-typed.
726 RunTest(ASCIIToUTF16("popularsitewithroot.com"), true, expected_urls
, true,
727 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
729 // The above still holds even with an extra trailing slash.
730 RunTest(ASCIIToUTF16("popularsitewithroot.com/"), true, expected_urls
, true,
731 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
734 TEST_F(HistoryQuickProviderTest
, CullSearchResults
) {
735 // Set up a default search engine.
736 TemplateURLData data
;
737 data
.SetKeyword(ASCIIToUTF16("TestEngine"));
738 data
.SetURL("http://testsearch.com/?q={searchTerms}");
739 TemplateURLService
* template_url_service
=
740 TemplateURLServiceFactory::GetForProfile(profile_
.get());
741 TemplateURL
* template_url
= new TemplateURL(data
);
742 template_url_service
->Add(template_url
);
743 template_url_service
->SetUserSelectedDefaultSearchProvider(template_url
);
744 template_url_service
->Load();
746 // A search results page should not be returned when typing a query.
747 std::vector
<std::string
> expected_urls
;
748 expected_urls
.push_back("http://anotherengine.com/?q=thequery");
749 RunTest(ASCIIToUTF16("thequery"), false, expected_urls
, false,
750 ASCIIToUTF16("anotherengine.com/?q=thequery"), base::string16());
752 // A search results page should not be returned when typing the engine URL.
753 expected_urls
.clear();
754 expected_urls
.push_back("http://testsearch.com/");
755 RunTest(ASCIIToUTF16("testsearch"), false, expected_urls
, true,
756 ASCIIToUTF16("testsearch.com"),
757 ASCIIToUTF16(".com"));
760 TEST_F(HistoryQuickProviderTest
, DoesNotProvideMatchesOnFocus
) {
761 AutocompleteInput
input(ASCIIToUTF16("popularsite"), base::string16::npos
,
762 std::string(), GURL(),
763 metrics::OmniboxEventProto::INVALID_SPEC
,
764 false, false, true, true,
765 ChromeAutocompleteSchemeClassifier(profile_
.get()));
766 provider_
->Start(input
, false, true);
767 EXPECT_TRUE(provider_
->matches().empty());
770 // HQPOrderingTest -------------------------------------------------------------
772 TestURLInfo ordering_test_db
[] = {
773 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3,
775 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, "
776 "apparel, computers, books, dvds & more", 20, 20, 10},
777 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&"
778 "currentpage=83", "google images", 6, 6, 0},
779 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0},
780 {"http://www.teamfortress.com/", "", 5, 5, 6},
781 {"http://www.rottentomatoes.com/", "", 3, 3, 7},
782 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9},
783 {"https://www.emigrantdirect.com/", "high interest savings account, high "
784 "yield savings - emigrantdirect", 5, 5, 3},
785 {"http://store.steampowered.com/", "", 6, 6, 1},
786 {"http://techmeme.com/", "techmeme", 111, 110, 4},
787 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15,
789 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1},
790 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player "
791 "index", 25, 7, 219},
792 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3,
794 {"http://translate.google.com/", "google translate", 3, 3, 0},
795 {"http://arstechnica.com/", "ars technica", 3, 3, 3},
796 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - "
797 "rotten tomatoes", 3, 3, 7},
798 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
799 "gaming news", 26, 25, 3},
800 {"http://metaleater.com/", "metaleater", 4, 3, 8},
801 {"http://half.com/", "half.com: textbooks , books , music , movies , games , "
802 "video games", 4, 4, 6},
803 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
804 "gaming news", 8, 5, 9},
807 class HQPOrderingTest
: public HistoryQuickProviderTest
{
809 void GetTestData(size_t* data_count
, TestURLInfo
** test_data
) override
;
812 void HQPOrderingTest::GetTestData(size_t* data_count
, TestURLInfo
** test_data
) {
815 *data_count
= arraysize(ordering_test_db
);
816 *test_data
= &ordering_test_db
[0];
819 TEST_F(HQPOrderingTest
, TEMatch
) {
820 std::vector
<std::string
> expected_urls
;
821 expected_urls
.push_back("http://techmeme.com/");
822 expected_urls
.push_back("http://www.teamliquid.net/");
823 expected_urls
.push_back("http://www.teamliquid.net/tlpd");
824 RunTest(ASCIIToUTF16("te"), false, expected_urls
, true,
825 ASCIIToUTF16("techmeme.com"),
826 ASCIIToUTF16("chmeme.com"));
829 TEST_F(HQPOrderingTest
, TEAMatch
) {
830 std::vector
<std::string
> expected_urls
;
831 expected_urls
.push_back("http://www.teamliquid.net/");
832 expected_urls
.push_back("http://www.teamliquid.net/tlpd");
833 expected_urls
.push_back("http://www.teamliquid.net/tlpd/korean/players");
834 RunTest(ASCIIToUTF16("tea"), false, expected_urls
, true,
835 ASCIIToUTF16("www.teamliquid.net"),
836 ASCIIToUTF16("mliquid.net"));