Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / autocomplete / history_quick_provider_unittest.cc
blobb88f0768b2967bf68862305e6ac32e49ae3bcedb
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 "components/omnibox/browser/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_factory.h"
22 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
23 #include "chrome/browser/history/history_service_factory.h"
24 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
25 #include "chrome/browser/search_engines/template_url_service_factory.h"
26 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "components/bookmarks/test/bookmark_test_helpers.h"
29 #include "components/history/core/browser/history_backend.h"
30 #include "components/history/core/browser/history_database.h"
31 #include "components/history/core/browser/history_service.h"
32 #include "components/history/core/browser/history_service_observer.h"
33 #include "components/history/core/browser/url_database.h"
34 #include "components/metrics/proto/omnibox_event.pb.h"
35 #include "components/omnibox/browser/autocomplete_match.h"
36 #include "components/omnibox/browser/autocomplete_result.h"
37 #include "components/omnibox/browser/history_url_provider.h"
38 #include "components/omnibox/browser/in_memory_url_index.h"
39 #include "components/omnibox/browser/url_index_private_data.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 provider_ = new HistoryQuickProvider(client_.get());
240 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
241 profile_.get(), &HistoryQuickProviderTest::CreateTemplateURLService);
242 FillData();
243 InMemoryURLIndex* index =
244 InMemoryURLIndexFactory::GetForProfile(profile_.get());
245 EXPECT_TRUE(index);
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, false,
343 ChromeAutocompleteSchemeClassifier(profile_.get()));
344 provider_->Start(input, 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());
409 TEST_F(HistoryQuickProviderTest, WordBoundariesWithPunctuationMatch) {
410 std::vector<std::string> expected_urls;
411 expected_urls.push_back("http://popularsitewithpathonly.com/moo");
412 RunTest(ASCIIToUTF16("/moo"), false, expected_urls, false,
413 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16());
416 TEST_F(HistoryQuickProviderTest, MultiTermTitleMatch) {
417 std::vector<std::string> expected_urls;
418 expected_urls.push_back(
419 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
420 RunTest(ASCIIToUTF16("mice other animals"), false, expected_urls, false,
421 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
422 base::string16());
425 TEST_F(HistoryQuickProviderTest, NonWordLastCharacterMatch) {
426 std::string expected_url("http://slashdot.org/favorite_page.html");
427 std::vector<std::string> expected_urls;
428 expected_urls.push_back(expected_url);
429 RunTest(ASCIIToUTF16("slashdot.org/"), false, expected_urls, true,
430 ASCIIToUTF16("slashdot.org/favorite_page.html"),
431 ASCIIToUTF16("favorite_page.html"));
434 TEST_F(HistoryQuickProviderTest, MultiMatch) {
435 std::vector<std::string> expected_urls;
436 // Scores high because of typed_count.
437 expected_urls.push_back("http://foo.com/");
438 // Scores high because of visit count.
439 expected_urls.push_back("http://foo.com/dir/another/");
440 // Scores high because of high visit count.
441 expected_urls.push_back("http://foo.com/dir/another/again/");
442 RunTest(ASCIIToUTF16("foo"), false, expected_urls, true,
443 ASCIIToUTF16("foo.com"), ASCIIToUTF16(".com"));
446 TEST_F(HistoryQuickProviderTest, StartRelativeMatch) {
447 std::vector<std::string> expected_urls;
448 expected_urls.push_back("http://xyzabcdefghijklmnopqrstuvw.com/a");
449 RunTest(ASCIIToUTF16("xyza"), false, expected_urls, true,
450 ASCIIToUTF16("xyzabcdefghijklmnopqrstuvw.com/a"),
451 ASCIIToUTF16("bcdefghijklmnopqrstuvw.com/a"));
454 TEST_F(HistoryQuickProviderTest, EncodingMatch) {
455 std::vector<std::string> expected_urls;
456 expected_urls.push_back("http://spaces.com/path%20with%20spaces/foo.html");
457 RunTest(ASCIIToUTF16("path with spaces"), false, expected_urls, false,
458 ASCIIToUTF16("spaces.com/path with spaces/foo.html"),
459 base::string16());
462 TEST_F(HistoryQuickProviderTest, ContentsClass) {
463 std::vector<std::string> expected_urls;
464 expected_urls.push_back(
465 "http://ja.wikipedia.org/wiki/%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%B8%96%E7"
466 "%95%8C%E5%A4%A7%E6%88%A6#.E3.83.B4.E3.82.A7.E3.83.AB.E3.82.B5.E3.82."
467 "A4.E3.83.A6.E4.BD.93.E5.88.B6");
468 RunTest(base::UTF8ToUTF16("第二 e3"), false, expected_urls, false,
469 base::UTF8ToUTF16("ja.wikipedia.org/wiki/第二次世界大戦#.E3.83.B4.E3."
470 "82.A7.E3.83.AB.E3.82.B5.E3.82.A4.E3.83.A6.E4.BD."
471 "93.E5.88.B6"),
472 base::string16());
473 #ifndef NDEBUG
474 ac_matches_[0].Validate();
475 #endif
476 // Verify that contents_class divides the string in the right places.
477 // [22, 24) is the "第二". All the other pairs are the "e3".
478 ACMatchClassifications contents_class(ac_matches_[0].contents_class);
479 size_t expected_offsets[] = { 0, 22, 24, 31, 33, 40, 42, 49, 51, 58, 60, 67,
480 69, 76, 78 };
481 // ScoredHistoryMatch may not highlight all the occurrences of these terms
482 // because it only highlights terms at word breaks, and it only stores word
483 // breaks up to some specified number of characters (50 at the time of this
484 // comment). This test is written flexibly so it still will pass if we
485 // increase that number in the future. Regardless, we require the first
486 // five offsets to be correct--in this example these cover at least one
487 // occurrence of each term.
488 EXPECT_LE(contents_class.size(), arraysize(expected_offsets));
489 EXPECT_GE(contents_class.size(), 5u);
490 for (size_t i = 0; i < contents_class.size(); ++i)
491 EXPECT_EQ(expected_offsets[i], contents_class[i].offset);
494 TEST_F(HistoryQuickProviderTest, VisitCountMatches) {
495 std::vector<std::string> expected_urls;
496 expected_urls.push_back("http://visitedest.com/y/a");
497 expected_urls.push_back("http://visitedest.com/y/b");
498 expected_urls.push_back("http://visitedest.com/x/c");
499 RunTest(ASCIIToUTF16("visitedest"), false, expected_urls, true,
500 ASCIIToUTF16("visitedest.com/y/a"),
501 ASCIIToUTF16(".com/y/a"));
504 TEST_F(HistoryQuickProviderTest, TypedCountMatches) {
505 std::vector<std::string> expected_urls;
506 expected_urls.push_back("http://typeredest.com/y/a");
507 expected_urls.push_back("http://typeredest.com/y/b");
508 expected_urls.push_back("http://typeredest.com/x/c");
509 RunTest(ASCIIToUTF16("typeredest"), false, expected_urls, true,
510 ASCIIToUTF16("typeredest.com/y/a"),
511 ASCIIToUTF16(".com/y/a"));
514 TEST_F(HistoryQuickProviderTest, DaysAgoMatches) {
515 std::vector<std::string> expected_urls;
516 expected_urls.push_back("http://daysagoest.com/y/a");
517 expected_urls.push_back("http://daysagoest.com/y/b");
518 expected_urls.push_back("http://daysagoest.com/x/c");
519 RunTest(ASCIIToUTF16("daysagoest"), false, expected_urls, true,
520 ASCIIToUTF16("daysagoest.com/y/a"),
521 ASCIIToUTF16(".com/y/a"));
524 TEST_F(HistoryQuickProviderTest, EncodingLimitMatch) {
525 std::vector<std::string> expected_urls;
526 std::string url(
527 "http://cda.com/Dogs%20Cats%20Gorillas%20Sea%20Slugs%20and%20Mice");
528 // First check that a mid-word match yield no results.
529 RunTest(ASCIIToUTF16("ice"), false, expected_urls, false,
530 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
531 base::string16());
532 // Then check that we get results when the match is at a word start
533 // that is present because of an encoded separate (%20 = space).
534 expected_urls.push_back(url);
535 RunTest(ASCIIToUTF16("Mice"), false, expected_urls, false,
536 ASCIIToUTF16("cda.com/Dogs Cats Gorillas Sea Slugs and Mice"),
537 base::string16());
538 // Verify that the matches' ACMatchClassifications offsets are in range.
539 ACMatchClassifications content(ac_matches_[0].contents_class);
540 // The max offset accounts for 6 occurrences of '%20' plus the 'http://'.
541 const size_t max_offset = url.length() - ((6 * 2) + 7);
542 for (ACMatchClassifications::const_iterator citer = content.begin();
543 citer != content.end(); ++citer)
544 EXPECT_LT(citer->offset, max_offset);
545 ACMatchClassifications description(ac_matches_[0].description_class);
546 std::string page_title("Dogs & Cats & Mice & Other Animals");
547 for (ACMatchClassifications::const_iterator diter = description.begin();
548 diter != description.end(); ++diter)
549 EXPECT_LT(diter->offset, page_title.length());
552 TEST_F(HistoryQuickProviderTest, Spans) {
553 // Test SpansFromTermMatch
554 TermMatches matches_a;
555 // Simulates matches: '.xx.xxx..xx...xxxxx..' which will test no match at
556 // either beginning or end as well as adjacent matches.
557 matches_a.push_back(TermMatch(1, 1, 2));
558 matches_a.push_back(TermMatch(2, 4, 3));
559 matches_a.push_back(TermMatch(3, 9, 1));
560 matches_a.push_back(TermMatch(3, 10, 1));
561 matches_a.push_back(TermMatch(4, 14, 5));
562 ACMatchClassifications spans_a =
563 HistoryQuickProvider::SpansFromTermMatch(matches_a, 20, false);
564 // ACMatch spans should be: 'NM-NM---N-M-N--M----N-'
565 ASSERT_EQ(9U, spans_a.size());
566 EXPECT_EQ(0U, spans_a[0].offset);
567 EXPECT_EQ(ACMatchClassification::NONE, spans_a[0].style);
568 EXPECT_EQ(1U, spans_a[1].offset);
569 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[1].style);
570 EXPECT_EQ(3U, spans_a[2].offset);
571 EXPECT_EQ(ACMatchClassification::NONE, spans_a[2].style);
572 EXPECT_EQ(4U, spans_a[3].offset);
573 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[3].style);
574 EXPECT_EQ(7U, spans_a[4].offset);
575 EXPECT_EQ(ACMatchClassification::NONE, spans_a[4].style);
576 EXPECT_EQ(9U, spans_a[5].offset);
577 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[5].style);
578 EXPECT_EQ(11U, spans_a[6].offset);
579 EXPECT_EQ(ACMatchClassification::NONE, spans_a[6].style);
580 EXPECT_EQ(14U, spans_a[7].offset);
581 EXPECT_EQ(ACMatchClassification::MATCH, spans_a[7].style);
582 EXPECT_EQ(19U, spans_a[8].offset);
583 EXPECT_EQ(ACMatchClassification::NONE, spans_a[8].style);
584 // Simulates matches: 'xx.xx' which will test matches at both beginning and
585 // end.
586 TermMatches matches_b;
587 matches_b.push_back(TermMatch(1, 0, 2));
588 matches_b.push_back(TermMatch(2, 3, 2));
589 ACMatchClassifications spans_b =
590 HistoryQuickProvider::SpansFromTermMatch(matches_b, 5, true);
591 // ACMatch spans should be: 'M-NM-'
592 ASSERT_EQ(3U, spans_b.size());
593 EXPECT_EQ(0U, spans_b[0].offset);
594 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL,
595 spans_b[0].style);
596 EXPECT_EQ(2U, spans_b[1].offset);
597 EXPECT_EQ(ACMatchClassification::URL, spans_b[1].style);
598 EXPECT_EQ(3U, spans_b[2].offset);
599 EXPECT_EQ(ACMatchClassification::MATCH | ACMatchClassification::URL,
600 spans_b[2].style);
603 TEST_F(HistoryQuickProviderTest, DeleteMatch) {
604 GURL test_url("http://slashdot.org/favorite_page.html");
605 std::vector<std::string> expected_urls;
606 expected_urls.push_back(test_url.spec());
607 // Fill up ac_matches_; we don't really care about the test yet.
608 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
609 ASCIIToUTF16("slashdot.org/favorite_page.html"),
610 ASCIIToUTF16(".org/favorite_page.html"));
611 EXPECT_EQ(1U, ac_matches_.size());
612 EXPECT_TRUE(history_backend()->GetURL(test_url, NULL));
613 provider_->DeleteMatch(ac_matches_[0]);
615 // Check that the underlying URL is deleted from the history DB (this implies
616 // that all visits are gone as well). Also verify that a deletion notification
617 // is sent, in response to which the secondary data stores (InMemoryDatabase,
618 // InMemoryURLIndex) will drop any data they might have pertaining to the URL.
619 // To ensure that the deletion has been propagated everywhere before we start
620 // verifying post-deletion states, first wait until we see the notification.
621 WaitForURLsDeletedNotification(history_service_);
622 EXPECT_FALSE(history_backend()->GetURL(test_url, NULL));
624 // Just to be on the safe side, explicitly verify that we have deleted enough
625 // data so that we will not be serving the same result again.
626 expected_urls.clear();
627 RunTest(ASCIIToUTF16("slashdot"), false, expected_urls, true,
628 ASCIIToUTF16("NONE EXPECTED"), base::string16());
631 TEST_F(HistoryQuickProviderTest, PreventBeatingURLWhatYouTypedMatch) {
632 std::vector<std::string> expected_urls;
634 expected_urls.clear();
635 expected_urls.push_back("http://popularsitewithroot.com/");
636 // If the user enters a hostname (no path) that they have visited
637 // before, we should make sure that all HistoryQuickProvider results
638 // have scores less than what HistoryURLProvider will assign the
639 // URL-what-you-typed match.
640 RunTest(ASCIIToUTF16("popularsitewithroot.com"), false, expected_urls, true,
641 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
642 EXPECT_LT(ac_matches_[0].relevance,
643 HistoryURLProvider::kScoreForBestInlineableResult);
645 // Check that if the user didn't quite enter the full hostname, this
646 // hostname would've normally scored above the URL-what-you-typed match.
647 RunTest(ASCIIToUTF16("popularsitewithroot.c"), false, expected_urls, true,
648 ASCIIToUTF16("popularsitewithroot.com"),
649 ASCIIToUTF16("om"));
650 EXPECT_GE(ac_matches_[0].relevance,
651 HistoryURLProvider::kScoreForWhatYouTypedResult);
653 expected_urls.clear();
654 expected_urls.push_back("http://popularsitewithpathonly.com/moo");
655 // If the user enters a hostname of a host that they have visited
656 // but never visited the root page of, we should make sure that all
657 // HistoryQuickProvider results have scores less than what the
658 // HistoryURLProvider will assign the URL-what-you-typed match.
659 RunTest(ASCIIToUTF16("popularsitewithpathonly.com"), false, expected_urls,
660 true,
661 ASCIIToUTF16("popularsitewithpathonly.com/moo"),
662 ASCIIToUTF16("/moo"));
663 EXPECT_LT(ac_matches_[0].relevance,
664 HistoryURLProvider::kScoreForUnvisitedIntranetResult);
666 // Verify the same thing happens if the user adds a / to end of the
667 // hostname.
668 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/"), false, expected_urls,
669 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"),
670 ASCIIToUTF16("moo"));
671 EXPECT_LT(ac_matches_[0].relevance,
672 HistoryURLProvider::kScoreForUnvisitedIntranetResult);
674 // Check that if the user didn't quite enter the full hostname, this
675 // page would've normally scored above the URL-what-you-typed match.
676 RunTest(ASCIIToUTF16("popularsitewithpathonly.co"), false, expected_urls,
677 true, ASCIIToUTF16("popularsitewithpathonly.com/moo"),
678 ASCIIToUTF16("m/moo"));
679 EXPECT_GE(ac_matches_[0].relevance,
680 HistoryURLProvider::kScoreForWhatYouTypedResult);
682 // If the user enters a hostname + path that they have not visited
683 // before (but visited other things on the host), we can allow
684 // inline autocompletions.
685 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/mo"), false, expected_urls,
686 true,
687 ASCIIToUTF16("popularsitewithpathonly.com/moo"),
688 ASCIIToUTF16("o"));
689 EXPECT_GE(ac_matches_[0].relevance,
690 HistoryURLProvider::kScoreForWhatYouTypedResult);
692 // If the user enters a hostname + path that they have visited
693 // before, we should make sure that all HistoryQuickProvider results
694 // have scores less than what the HistoryURLProvider will assign
695 // the URL-what-you-typed match.
696 RunTest(ASCIIToUTF16("popularsitewithpathonly.com/moo"), false,
697 expected_urls, true,
698 ASCIIToUTF16("popularsitewithpathonly.com/moo"), base::string16());
699 EXPECT_LT(ac_matches_[0].relevance,
700 HistoryURLProvider::kScoreForBestInlineableResult);
703 TEST_F(HistoryQuickProviderTest, PreventInlineAutocomplete) {
704 std::vector<std::string> expected_urls;
705 expected_urls.push_back("http://popularsitewithroot.com/");
707 // Check that the desired URL is normally allowed to be the default match
708 // against input that is a prefex of the URL.
709 RunTest(ASCIIToUTF16("popularsitewithr"), false, expected_urls, true,
710 ASCIIToUTF16("popularsitewithroot.com"),
711 ASCIIToUTF16("oot.com"));
713 // Check that it's not allowed to be the default match if
714 // prevent_inline_autocomplete is true.
715 RunTest(ASCIIToUTF16("popularsitewithr"), true, expected_urls, false,
716 ASCIIToUTF16("popularsitewithroot.com"),
717 ASCIIToUTF16("oot.com"));
719 // But the exact hostname can still match even if prevent inline autocomplete
720 // is true. i.e., there's no autocompletion necessary; this is effectively
721 // URL-what-you-typed.
722 RunTest(ASCIIToUTF16("popularsitewithroot.com"), true, expected_urls, true,
723 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
725 // The above still holds even with an extra trailing slash.
726 RunTest(ASCIIToUTF16("popularsitewithroot.com/"), true, expected_urls, true,
727 ASCIIToUTF16("popularsitewithroot.com"), base::string16());
730 TEST_F(HistoryQuickProviderTest, CullSearchResults) {
731 // Set up a default search engine.
732 TemplateURLData data;
733 data.SetShortName(ASCIIToUTF16("TestEngine"));
734 data.SetKeyword(ASCIIToUTF16("TestEngine"));
735 data.SetURL("http://testsearch.com/?q={searchTerms}");
736 TemplateURLService* template_url_service =
737 TemplateURLServiceFactory::GetForProfile(profile_.get());
738 TemplateURL* template_url = new TemplateURL(data);
739 template_url_service->Add(template_url);
740 template_url_service->SetUserSelectedDefaultSearchProvider(template_url);
741 template_url_service->Load();
743 // A search results page should not be returned when typing a query.
744 std::vector<std::string> expected_urls;
745 RunTest(ASCIIToUTF16("thequery"), false, expected_urls, false,
746 ASCIIToUTF16("anotherengine.com/?q=thequery"), base::string16());
748 // A search results page should not be returned when typing the engine URL.
749 expected_urls.clear();
750 expected_urls.push_back("http://testsearch.com/");
751 RunTest(ASCIIToUTF16("testsearch"), false, expected_urls, true,
752 ASCIIToUTF16("testsearch.com"),
753 ASCIIToUTF16(".com"));
756 TEST_F(HistoryQuickProviderTest, DoesNotProvideMatchesOnFocus) {
757 AutocompleteInput input(
758 ASCIIToUTF16("popularsite"), base::string16::npos, std::string(), GURL(),
759 metrics::OmniboxEventProto::INVALID_SPEC, false, false, true, true, true,
760 ChromeAutocompleteSchemeClassifier(profile_.get()));
761 provider_->Start(input, false);
762 EXPECT_TRUE(provider_->matches().empty());
765 // HQPOrderingTest -------------------------------------------------------------
767 TestURLInfo ordering_test_db[] = {
768 {"http://www.teamliquid.net/tlpd/korean/games/21648_bisu_vs_iris", "", 6, 3,
769 256},
770 {"http://www.amazon.com/", "amazon.com: online shopping for electronics, "
771 "apparel, computers, books, dvds & more", 20, 20, 10},
772 {"http://www.teamliquid.net/forum/viewmessage.php?topic_id=52045&"
773 "currentpage=83", "google images", 6, 6, 0},
774 {"http://www.tempurpedic.com/", "tempur-pedic", 7, 7, 0},
775 {"http://www.teamfortress.com/", "", 5, 5, 6},
776 {"http://www.rottentomatoes.com/", "", 3, 3, 7},
777 {"http://music.google.com/music/listen?u=0#start_pl", "", 3, 3, 9},
778 {"https://www.emigrantdirect.com/", "high interest savings account, high "
779 "yield savings - emigrantdirect", 5, 5, 3},
780 {"http://store.steampowered.com/", "", 6, 6, 1},
781 {"http://techmeme.com/", "techmeme", 111, 110, 4},
782 {"http://www.teamliquid.net/tlpd", "team liquid progaming database", 15, 15,
784 {"http://store.steampowered.com/", "the steam summer camp sale", 6, 6, 1},
785 {"http://www.teamliquid.net/tlpd/korean/players", "tlpd - bw korean - player "
786 "index", 25, 7, 219},
787 {"http://slashdot.org/", "slashdot: news for nerds, stuff that matters", 3, 3,
789 {"http://translate.google.com/", "google translate", 3, 3, 0},
790 {"http://arstechnica.com/", "ars technica", 3, 3, 3},
791 {"http://www.rottentomatoes.com/", "movies | movie trailers | reviews - "
792 "rotten tomatoes", 3, 3, 7},
793 {"http://www.teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
794 "gaming news", 26, 25, 3},
795 {"http://metaleater.com/", "metaleater", 4, 3, 8},
796 {"http://half.com/", "half.com: textbooks , books , music , movies , games , "
797 "video games", 4, 4, 6},
798 {"http://teamliquid.net/", "team liquid - starcraft 2 and brood war pro "
799 "gaming news", 8, 5, 9},
802 class HQPOrderingTest : public HistoryQuickProviderTest {
803 protected:
804 void GetTestData(size_t* data_count, TestURLInfo** test_data) override;
807 void HQPOrderingTest::GetTestData(size_t* data_count, TestURLInfo** test_data) {
808 DCHECK(data_count);
809 DCHECK(test_data);
810 *data_count = arraysize(ordering_test_db);
811 *test_data = &ordering_test_db[0];
814 TEST_F(HQPOrderingTest, TEMatch) {
815 std::vector<std::string> expected_urls;
816 expected_urls.push_back("http://techmeme.com/");
817 expected_urls.push_back("http://www.teamliquid.net/");
818 expected_urls.push_back("http://www.teamliquid.net/tlpd");
819 RunTest(ASCIIToUTF16("te"), false, expected_urls, true,
820 ASCIIToUTF16("techmeme.com"),
821 ASCIIToUTF16("chmeme.com"));
824 TEST_F(HQPOrderingTest, TEAMatch) {
825 std::vector<std::string> expected_urls;
826 expected_urls.push_back("http://www.teamliquid.net/");
827 expected_urls.push_back("http://www.teamliquid.net/tlpd");
828 expected_urls.push_back("http://www.teamliquid.net/tlpd/korean/players");
829 RunTest(ASCIIToUTF16("tea"), false, expected_urls, true,
830 ASCIIToUTF16("www.teamliquid.net"),
831 ASCIIToUTF16("mliquid.net"));