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/shortcuts_provider.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.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/shortcuts_backend.h"
23 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "components/history/core/browser/history_service.h"
26 #include "components/history/core/browser/url_database.h"
27 #include "components/metrics/proto/omnibox_event.pb.h"
28 #include "components/omnibox/autocomplete_input.h"
29 #include "components/omnibox/autocomplete_match.h"
30 #include "components/omnibox/autocomplete_provider.h"
31 #include "components/omnibox/autocomplete_result.h"
32 #include "content/public/browser/notification_service.h"
33 #include "content/public/test/test_browser_thread.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 #if defined(ENABLE_EXTENSIONS)
37 #include "extensions/browser/notification_types.h"
38 #include "extensions/common/extension.h"
39 #include "extensions/common/extension_builder.h"
40 #include "extensions/common/value_builder.h"
43 using base::ASCIIToUTF16
;
45 // TestShortcutInfo -----------------------------------------------------------
49 struct TestShortcutInfo
{
52 std::string fill_into_edit
;
53 std::string destination_url
;
55 std::string contents_class
;
56 std::string description
;
57 std::string description_class
;
58 ui::PageTransition transition
;
59 AutocompleteMatch::Type type
;
63 } shortcut_test_db
[] = {
64 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com",
65 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1",
66 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
68 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org",
69 "http://slashdot.org/", "slashdot.org", "0,3,5,1",
70 "Slashdot - News for nerds, stuff that matters", "0,2,5,0",
71 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 0,
73 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org",
74 "http://slashdot.org/", "slashdot.org", "0,1",
75 "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0",
76 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 0,
78 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E3", "news", "sports.yahoo.com",
79 "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
80 "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
81 "0,0,23,2,27,0", ui::PAGE_TRANSITION_TYPED
,
82 AutocompleteMatchType::HISTORY_TITLE
, "", 2, 5 },
83 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E4", "news weather",
84 "www.cnn.com/index.html", "http://www.cnn.com/index.html",
85 "www.cnn.com/index.html", "0,1",
86 "CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video",
87 "0,0,19,2,23,0,38,2,45,0", ui::PAGE_TRANSITION_TYPED
,
88 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 10 },
89 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E5", "nhl scores", "sports.yahoo.com",
90 "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
91 "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
92 "0,0,29,2,35,0", ui::PAGE_TRANSITION_TYPED
,
93 AutocompleteMatchType::HISTORY_BODY
, "", 1, 10 },
94 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E6", "nhl scores",
95 "www.nhl.com/scores/index.html", "http://www.nhl.com/scores/index.html",
96 "www.nhl.com/scores/index.html", "0,1,4,3,7,1",
97 "January 13, 2010 - NHL.com - Scores", "0,0,19,2,22,0,29,2,35,0",
98 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 5,
100 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E7", "just", "www.testsite.com/a.html",
101 "http://www.testsite.com/a.html", "www.testsite.com/a.html", "0,1",
102 "Test - site - just a test", "0,0,14,2,18,0",
103 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 5,
105 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E8", "just", "www.testsite.com/b.html",
106 "http://www.testsite.com/b.html", "www.testsite.com/b.html", "0,1",
107 "Test - site - just a test", "0,0,14,2,18,0",
108 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 5,
110 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E9", "just", "www.testsite.com/c.html",
111 "http://www.testsite.com/c.html", "www.testsite.com/c.html", "0,1",
112 "Test - site - just a test", "0,0,14,2,18,0",
113 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 8,
115 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EA", "just a", "www.testsite.com/d.html",
116 "http://www.testsite.com/d.html", "www.testsite.com/d.html", "0,1",
117 "Test - site - just a test", "0,0,14,2,18,0",
118 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "",
120 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EB", "just a t",
121 "www.testsite.com/e.html", "http://www.testsite.com/e.html",
122 "www.testsite.com/e.html", "0,1", "Test - site - just a test",
123 "0,0,14,2,18,0", ui::PAGE_TRANSITION_TYPED
,
124 AutocompleteMatchType::HISTORY_TITLE
, "", 12, 1 },
125 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EC", "just a te",
126 "www.testsite.com/f.html", "http://www.testsite.com/f.html",
127 "www.testsite.com/f.html", "0,1", "Test - site - just a test",
128 "0,0,14,2,18,0", ui::PAGE_TRANSITION_TYPED
,
129 AutocompleteMatchType::HISTORY_TITLE
, "", 12, 1 },
130 { "BD85DBA2-8C29-49F9-84AE-48E1E90880ED", "ago", "www.daysagotest.com/a.html",
131 "http://www.daysagotest.com/a.html", "www.daysagotest.com/a.html",
132 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED
,
133 AutocompleteMatchType::HISTORY_URL
, "", 1, 1 },
134 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EE", "ago", "www.daysagotest.com/b.html",
135 "http://www.daysagotest.com/b.html", "www.daysagotest.com/b.html",
136 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED
,
137 AutocompleteMatchType::HISTORY_URL
, "", 2, 1 },
138 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html",
139 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html",
140 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED
,
141 AutocompleteMatchType::HISTORY_URL
, "", 3, 1 },
142 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html",
143 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html",
144 "0,1,8,3,11,1", "Test - site", "0,0", ui::PAGE_TRANSITION_TYPED
,
145 AutocompleteMatchType::HISTORY_URL
, "", 4, 1 },
146 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo",
147 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo",
148 "Run Echo command: echo", "0,0", "Echo", "0,4",
149 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::EXTENSION_APP
,
151 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com",
152 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1",
153 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
155 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query",
156 "https://www.google.com/search?q=query", "query", "0,0",
157 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED
,
158 AutocompleteMatchType::SEARCH_HISTORY
, "", 1, 100 },
159 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word",
160 "https://www.google.com/search?q=www.word", "www.word", "0,0",
161 "Google Search", "0,4", ui::PAGE_TRANSITION_GENERATED
,
162 AutocompleteMatchType::SEARCH_HISTORY
, "", 1, 100 },
163 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F5", "about:o", "chrome://omnibox",
164 "chrome://omnibox/", "about:omnibox", "0,3,10,1", "", "",
165 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::NAVSUGGEST
, "",
167 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F6", "www/real sp",
168 "http://www/real space/long-url-with-space.html",
169 "http://www/real%20space/long-url-with-space.html",
170 "www/real space/long-url-with-space.html", "0,3,11,1",
171 "Page With Space; Input with Space", "0,0",
172 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "",
174 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F7", "duplicate", "http://duplicate.com",
175 "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
176 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
178 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F8", "dupl", "http://duplicate.com",
179 "http://duplicate.com/", "Duplicate", "0,1", "Duplicate", "0,1",
180 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
182 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F9", "notrailing.com/",
183 "http://notrailing.com", "http://notrailing.com/", "No Trailing Slash",
184 "0,1", "No Trailing Slash on fill_into_edit", "0,1",
185 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "",
187 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FA", "http:///foo.com",
188 "http://foo.com", "http://foo.com/", "Foo - Typo in Input",
189 "0,1", "Foo - Typo in Input Corrected in fill_into_edit", "0,1",
190 ui::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "",
192 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FB", "trailing1 ",
193 "http://trailing1.com", "http://trailing1.com/",
194 "Trailing1 - Space in Shortcut", "0,1",
195 "Trailing1 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED
,
196 AutocompleteMatchType::HISTORY_URL
, "", 1, 100 },
197 { "BD85DBA2-8C29-49F9-84AE-48E1E90880FC", "about:trailing2 ",
198 "chrome://trailing2blah", "chrome://trailing2blah/",
199 "Trailing2 - Space in Shortcut", "0,1",
200 "Trailing2 - Space in Shortcut", "0,1", ui::PAGE_TRANSITION_TYPED
,
201 AutocompleteMatchType::HISTORY_URL
, "", 1, 100 },
207 // ClassifyTest ---------------------------------------------------------------
209 // Helper class to make running tests of ClassifyAllMatchesInString() more
213 ClassifyTest(const base::string16
& text
, ACMatchClassifications matches
);
216 ACMatchClassifications
RunTest(const base::string16
& find_text
);
219 const base::string16 text_
;
220 const ACMatchClassifications matches_
;
223 ClassifyTest::ClassifyTest(const base::string16
& text
,
224 ACMatchClassifications matches
)
229 ClassifyTest::~ClassifyTest() {
232 ACMatchClassifications
ClassifyTest::RunTest(const base::string16
& find_text
) {
233 return ShortcutsProvider::ClassifyAllMatchesInString(find_text
,
234 ShortcutsProvider::CreateWordMapForString(find_text
), text_
, matches_
);
238 // ShortcutsProviderTest ------------------------------------------------------
240 class ShortcutsProviderTest
: public testing::Test
{
242 ShortcutsProviderTest();
245 typedef std::pair
<std::string
, bool> ExpectedURLAndAllowedToBeDefault
;
246 typedef std::vector
<ExpectedURLAndAllowedToBeDefault
> ExpectedURLs
;
248 class SetShouldContain
249 : public std::unary_function
<const ExpectedURLAndAllowedToBeDefault
&,
250 std::set
<std::string
> > {
252 explicit SetShouldContain(const ACMatches
& matched_urls
);
254 void operator()(const ExpectedURLAndAllowedToBeDefault
& expected
);
255 std::set
<ExpectedURLAndAllowedToBeDefault
> Leftovers() const {
260 std::set
<ExpectedURLAndAllowedToBeDefault
> matches_
;
263 void SetUp() override
;
264 void TearDown() override
;
266 // Fills test data into the provider.
267 void FillData(TestShortcutInfo
* db
, size_t db_size
);
269 // Runs an autocomplete query on |text| with the provided
270 // |prevent_inline_autocomplete| setting and checks to see that the returned
271 // results' destination URLs match those provided. |expected_urls| does not
272 // need to be in sorted order, but |expected_top_result| should be the top
273 // match, and it should have inline autocompletion
274 // |top_result_inline_autocompletion|.
275 void RunTest(const base::string16 text
,
276 bool prevent_inline_autocomplete
,
277 const ExpectedURLs
& expected_urls
,
278 std::string expected_top_result
,
279 base::string16 top_result_inline_autocompletion
);
281 // Passthrough to the private function in provider_.
282 int CalculateScore(const std::string
& terms
,
283 const ShortcutsDatabase::Shortcut
& shortcut
,
286 base::MessageLoopForUI message_loop_
;
287 content::TestBrowserThread ui_thread_
;
288 content::TestBrowserThread file_thread_
;
290 TestingProfile profile_
;
292 ACMatches ac_matches_
; // The resulting matches after running RunTest.
294 scoped_refptr
<ShortcutsBackend
> backend_
;
295 scoped_refptr
<ShortcutsProvider
> provider_
;
298 ShortcutsProviderTest::ShortcutsProviderTest()
299 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
300 file_thread_(content::BrowserThread::FILE, &message_loop_
) {
303 void ShortcutsProviderTest::SetUp() {
304 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
305 &profile_
, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting
);
306 backend_
= ShortcutsBackendFactory::GetForProfile(&profile_
);
307 ASSERT_TRUE(backend_
.get());
308 ASSERT_TRUE(profile_
.CreateHistoryService(true, false));
309 provider_
= new ShortcutsProvider(&profile_
);
310 FillData(shortcut_test_db
, arraysize(shortcut_test_db
));
313 void ShortcutsProviderTest::TearDown() {
314 // Run all pending tasks or else some threads hold on to the message loop
315 // and prevent it from being deleted.
316 message_loop_
.RunUntilIdle();
317 profile_
.DestroyHistoryService();
321 void ShortcutsProviderTest::FillData(TestShortcutInfo
* db
, size_t db_size
) {
322 DCHECK(provider_
.get());
323 size_t expected_size
= backend_
->shortcuts_map().size() + db_size
;
324 for (size_t i
= 0; i
< db_size
; ++i
) {
325 const TestShortcutInfo
& cur
= db
[i
];
326 ShortcutsDatabase::Shortcut
shortcut(
327 cur
.guid
, ASCIIToUTF16(cur
.text
),
328 ShortcutsDatabase::Shortcut::MatchCore(
329 ASCIIToUTF16(cur
.fill_into_edit
), GURL(cur
.destination_url
),
330 ASCIIToUTF16(cur
.contents
), cur
.contents_class
,
331 ASCIIToUTF16(cur
.description
), cur
.description_class
,
332 cur
.transition
, cur
.type
, ASCIIToUTF16(cur
.keyword
)),
333 base::Time::Now() - base::TimeDelta::FromDays(cur
.days_from_now
),
335 backend_
->AddShortcut(shortcut
);
337 EXPECT_EQ(expected_size
, backend_
->shortcuts_map().size());
340 ShortcutsProviderTest::SetShouldContain::SetShouldContain(
341 const ACMatches
& matched_urls
) {
342 for (ACMatches::const_iterator iter
= matched_urls
.begin();
343 iter
!= matched_urls
.end(); ++iter
)
344 matches_
.insert(ExpectedURLAndAllowedToBeDefault(
345 iter
->destination_url
.spec(), iter
->allowed_to_be_default_match
));
348 void ShortcutsProviderTest::SetShouldContain::operator()(
349 const ExpectedURLAndAllowedToBeDefault
& expected
) {
350 EXPECT_EQ(1U, matches_
.erase(expected
));
353 void ShortcutsProviderTest::RunTest(
354 const base::string16 text
,
355 bool prevent_inline_autocomplete
,
356 const ExpectedURLs
& expected_urls
,
357 std::string expected_top_result
,
358 base::string16 top_result_inline_autocompletion
) {
359 base::MessageLoop::current()->RunUntilIdle();
360 AutocompleteInput
input(text
, base::string16::npos
, std::string(), GURL(),
361 metrics::OmniboxEventProto::INVALID_SPEC
,
362 prevent_inline_autocomplete
, false, true, true,
363 ChromeAutocompleteSchemeClassifier(&profile_
));
364 provider_
->Start(input
, false, false);
365 EXPECT_TRUE(provider_
->done());
367 ac_matches_
= provider_
->matches();
369 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
370 EXPECT_LE(ac_matches_
.size(), AutocompleteProvider::kMaxMatches
);
372 // If the number of expected and actual matches aren't equal then we need
373 // test no further, but let's do anyway so that we know which URLs failed.
374 EXPECT_EQ(expected_urls
.size(), ac_matches_
.size());
376 // Verify that all expected URLs were found and that all found URLs
378 std::set
<ExpectedURLAndAllowedToBeDefault
> Leftovers
=
379 for_each(expected_urls
.begin(), expected_urls
.end(),
380 SetShouldContain(ac_matches_
)).Leftovers();
381 EXPECT_EQ(0U, Leftovers
.size());
383 // See if we got the expected top scorer.
384 if (!ac_matches_
.empty()) {
385 std::partial_sort(ac_matches_
.begin(), ac_matches_
.begin() + 1,
386 ac_matches_
.end(), AutocompleteMatch::MoreRelevant
);
387 EXPECT_EQ(expected_top_result
, ac_matches_
[0].destination_url
.spec());
388 EXPECT_EQ(top_result_inline_autocompletion
,
389 ac_matches_
[0].inline_autocompletion
);
393 int ShortcutsProviderTest::CalculateScore(
394 const std::string
& terms
,
395 const ShortcutsDatabase::Shortcut
& shortcut
,
397 return provider_
->CalculateScore(ASCIIToUTF16(terms
), shortcut
,
402 // Actual tests ---------------------------------------------------------------
404 TEST_F(ShortcutsProviderTest
, SimpleSingleMatch
) {
405 base::string16
text(ASCIIToUTF16("go"));
406 std::string
expected_url("http://www.google.com/");
407 ExpectedURLs expected_urls
;
408 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
409 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("ogle.com"));
411 // Same test with prevent inline autocomplete.
412 expected_urls
.clear();
413 expected_urls
.push_back(
414 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
415 // The match will have an |inline_autocompletion| set, but the value will not
416 // be used because |allowed_to_be_default_match| will be false.
417 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("ogle.com"));
419 // A pair of analogous tests where the shortcut ends at the end of
420 // |fill_into_edit|. This exercises the inline autocompletion and default
422 text
= ASCIIToUTF16("abcdef.com");
423 expected_url
= "http://abcdef.com/";
424 expected_urls
.clear();
425 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
426 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
427 // With prevent inline autocomplete, the suggestion should be the same
428 // (because there is no completion).
429 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
431 // Another test, simply for a query match type, not a navigation URL match
433 text
= ASCIIToUTF16("que");
434 expected_url
= "https://www.google.com/search?q=query";
435 expected_urls
.clear();
436 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
437 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("ry"));
439 // Same test with prevent inline autocomplete.
440 expected_urls
.clear();
441 expected_urls
.push_back(
442 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
443 // The match will have an |inline_autocompletion| set, but the value will not
444 // be used because |allowed_to_be_default_match| will be false.
445 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("ry"));
447 // A pair of analogous tests where the shortcut ends at the end of
448 // |fill_into_edit|. This exercises the inline autocompletion and default
450 text
= ASCIIToUTF16("query");
451 expected_urls
.clear();
452 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
453 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
454 // With prevent inline autocomplete, the suggestion should be the same
455 // (because there is no completion).
456 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
458 // Now the shortcut ends at the end of |fill_into_edit| but has a
459 // non-droppable prefix. ("www.", for instance, is not droppable for
461 text
= ASCIIToUTF16("word");
462 expected_url
= "https://www.google.com/search?q=www.word";
463 expected_urls
.clear();
464 expected_urls
.push_back(
465 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
466 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
469 // These tests are like those in SimpleSingleMatch but more complex,
470 // involving URLs that need to be fixed up to match properly.
471 TEST_F(ShortcutsProviderTest
, TrickySingleMatch
) {
472 // Test that about: URLs are fixed up/transformed to chrome:// URLs.
473 base::string16
text(ASCIIToUTF16("about:o"));
474 std::string
expected_url("chrome://omnibox/");
475 ExpectedURLs expected_urls
;
476 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
477 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("mnibox"));
479 // Same test with prevent inline autocomplete.
480 expected_urls
.clear();
481 expected_urls
.push_back(
482 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
483 // The match will have an |inline_autocompletion| set, but the value will not
484 // be used because |allowed_to_be_default_match| will be false.
485 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("mnibox"));
487 // Test that an input with a space can match URLs with a (escaped) space.
488 // This would fail if we didn't try to lookup the un-fixed-up string.
489 text
= ASCIIToUTF16("www/real sp");
490 expected_url
= "http://www/real%20space/long-url-with-space.html";
491 expected_urls
.clear();
492 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
493 RunTest(text
, false, expected_urls
, expected_url
,
494 ASCIIToUTF16("ace/long-url-with-space.html"));
496 // Same test with prevent inline autocomplete.
497 expected_urls
.clear();
498 expected_urls
.push_back(
499 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
500 // The match will have an |inline_autocompletion| set, but the value will not
501 // be used because |allowed_to_be_default_match| will be false.
502 RunTest(text
, true, expected_urls
, expected_url
,
503 ASCIIToUTF16("ace/long-url-with-space.html"));
505 // Test when the user input has a trailing slash but fill_into_edit does
506 // not. This should still be allowed to be default.
507 text
= ASCIIToUTF16("notrailing.com/");
508 expected_url
= "http://notrailing.com/";
509 expected_urls
.clear();
510 expected_urls
.push_back(
511 ExpectedURLAndAllowedToBeDefault(expected_url
, true));
512 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
514 // Test when the user input has a typo that can be fixed up for matching
515 // fill_into_edit. This should still be allowed to be default.
516 text
= ASCIIToUTF16("http:///foo.com");
517 expected_url
= "http://foo.com/";
518 expected_urls
.clear();
519 expected_urls
.push_back(
520 ExpectedURLAndAllowedToBeDefault(expected_url
, true));
521 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
523 // A foursome of tests to verify that trailing spaces prevent the shortcut
524 // from being allowed to be the default match. For each of two tests, we
525 // first verify that the match is allowed to be default without the trailing
526 // space but is not allowed to be default with the trailing space. In both
527 // of these with-trailing-space cases, we actually get an
528 // inline_autocompletion, though it's never used because the match is
529 // prohibited from being default.
530 text
= ASCIIToUTF16("trailing1");
531 expected_url
= "http://trailing1.com/";
532 expected_urls
.clear();
533 expected_urls
.push_back(
534 ExpectedURLAndAllowedToBeDefault(expected_url
, true));
535 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16(".com"));
536 text
= ASCIIToUTF16("trailing1 ");
537 expected_urls
.clear();
538 expected_urls
.push_back(
539 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
540 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16(".com"));
541 text
= ASCIIToUTF16("about:trailing2");
542 expected_url
= "chrome://trailing2blah/";
543 expected_urls
.clear();
544 expected_urls
.push_back(
545 ExpectedURLAndAllowedToBeDefault(expected_url
, true));
546 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("blah"));
547 text
= ASCIIToUTF16("about:trailing2 ");
548 expected_urls
.clear();
549 expected_urls
.push_back(
550 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
551 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("blah"));
554 TEST_F(ShortcutsProviderTest
, MultiMatch
) {
555 base::string16
text(ASCIIToUTF16("NEWS"));
556 ExpectedURLs expected_urls
;
557 // Scores high because of completion length.
558 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
559 "http://slashdot.org/", false));
560 // Scores high because of visit count.
561 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
562 "http://sports.yahoo.com/", false));
563 // Scores high because of visit count but less match span,
564 // which is more important.
565 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
566 "http://www.cnn.com/index.html", false));
567 RunTest(text
, false, expected_urls
, "http://slashdot.org/", base::string16());
570 TEST_F(ShortcutsProviderTest
, RemoveDuplicates
) {
571 base::string16
text(ASCIIToUTF16("dupl"));
572 ExpectedURLs expected_urls
;
573 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
574 "http://duplicate.com/", true));
575 // Make sure the URL only appears once in the output list.
576 RunTest(text
, false, expected_urls
, "http://duplicate.com/",
577 ASCIIToUTF16("icate.com"));
580 TEST_F(ShortcutsProviderTest
, TypedCountMatches
) {
581 base::string16
text(ASCIIToUTF16("just"));
582 ExpectedURLs expected_urls
;
583 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
584 "http://www.testsite.com/b.html", false));
585 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
586 "http://www.testsite.com/a.html", false));
587 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
588 "http://www.testsite.com/c.html", false));
589 RunTest(text
, false, expected_urls
, "http://www.testsite.com/b.html",
593 TEST_F(ShortcutsProviderTest
, FragmentLengthMatches
) {
594 base::string16
text(ASCIIToUTF16("just a"));
595 ExpectedURLs expected_urls
;
596 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
597 "http://www.testsite.com/d.html", false));
598 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
599 "http://www.testsite.com/e.html", false));
600 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
601 "http://www.testsite.com/f.html", false));
602 RunTest(text
, false, expected_urls
, "http://www.testsite.com/d.html",
606 TEST_F(ShortcutsProviderTest
, DaysAgoMatches
) {
607 base::string16
text(ASCIIToUTF16("ago"));
608 ExpectedURLs expected_urls
;
609 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
610 "http://www.daysagotest.com/a.html", false));
611 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
612 "http://www.daysagotest.com/b.html", false));
613 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
614 "http://www.daysagotest.com/c.html", false));
615 RunTest(text
, false, expected_urls
, "http://www.daysagotest.com/a.html",
619 TEST_F(ShortcutsProviderTest
, ClassifyAllMatchesInString
) {
620 ACMatchClassifications matches
=
621 AutocompleteMatch::ClassificationsFromString("0,0");
622 ClassifyTest
classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"),
625 ACMatchClassifications spans_a
= classify_test
.RunTest(ASCIIToUTF16("man"));
626 // ACMatch spans should be: '--MMM------------------------'
627 EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a
));
629 ACMatchClassifications spans_b
= classify_test
.RunTest(ASCIIToUTF16("man p"));
630 // ACMatch spans should be: '--MMM----M-------------M-----'
631 EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0",
632 AutocompleteMatch::ClassificationsToString(spans_b
));
634 ACMatchClassifications spans_c
=
635 classify_test
.RunTest(ASCIIToUTF16("man plan panama"));
636 // ACMatch spans should be:'--MMM----MMMM----------MMMMMM'
637 EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2",
638 AutocompleteMatch::ClassificationsToString(spans_c
));
640 ClassifyTest
classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, "
641 "Scores, Rumors, Fantasy Games, and more"), matches
);
643 ACMatchClassifications spans_d
= classify_test2
.RunTest(ASCIIToUTF16("ne"));
644 // ACMatch spans should match first two letters of the "news".
645 EXPECT_EQ("0,0,23,2,25,0",
646 AutocompleteMatch::ClassificationsToString(spans_d
));
648 ACMatchClassifications spans_e
=
649 classify_test2
.RunTest(ASCIIToUTF16("news r"));
650 EXPECT_EQ("0,0,10,2,11,0,19,2,20,0,23,2,27,0,32,2,33,0,37,2,38,0,41,2,42,0,"
651 "66,2,67,0", AutocompleteMatch::ClassificationsToString(spans_e
));
653 matches
= AutocompleteMatch::ClassificationsFromString("0,1");
654 ClassifyTest
classify_test3(ASCIIToUTF16("livescore.goal.com"), matches
);
656 ACMatchClassifications spans_f
= classify_test3
.RunTest(ASCIIToUTF16("go"));
657 // ACMatch spans should match first two letters of the "goal".
658 EXPECT_EQ("0,1,10,3,12,1",
659 AutocompleteMatch::ClassificationsToString(spans_f
));
661 matches
= AutocompleteMatch::ClassificationsFromString("0,0,13,1");
662 ClassifyTest
classify_test4(ASCIIToUTF16("Email login: mail.somecorp.com"),
665 ACMatchClassifications spans_g
= classify_test4
.RunTest(ASCIIToUTF16("ail"));
666 EXPECT_EQ("0,0,2,2,5,0,13,1,14,3,17,1",
667 AutocompleteMatch::ClassificationsToString(spans_g
));
669 ACMatchClassifications spans_h
=
670 classify_test4
.RunTest(ASCIIToUTF16("lo log"));
671 EXPECT_EQ("0,0,6,2,9,0,13,1",
672 AutocompleteMatch::ClassificationsToString(spans_h
));
674 ACMatchClassifications spans_i
=
675 classify_test4
.RunTest(ASCIIToUTF16("ail em"));
676 // 'Email' and 'ail' should be matched.
677 EXPECT_EQ("0,2,5,0,13,1,14,3,17,1",
678 AutocompleteMatch::ClassificationsToString(spans_i
));
680 // Some web sites do not have a description. If the string being searched is
681 // empty, the classifications must also be empty: http://crbug.com/148647
682 // Extra parens in the next line hack around C++03's "most vexing parse".
683 class ClassifyTest
classify_test5((base::string16()),
684 ACMatchClassifications());
685 ACMatchClassifications spans_j
= classify_test5
.RunTest(ASCIIToUTF16("man"));
686 ASSERT_EQ(0U, spans_j
.size());
688 // Matches which end at beginning of classification merge properly.
689 matches
= AutocompleteMatch::ClassificationsFromString("0,4,9,0");
690 ClassifyTest
classify_test6(ASCIIToUTF16("html password example"), matches
);
692 // Extra space in the next string avoids having the string be a prefix of the
693 // text above, which would allow for two different valid classification sets,
694 // one of which uses two spans (the first of which would mark all of "html
695 // pass" as a match) and one which uses four (which marks the individual words
696 // as matches but not the space between them). This way only the latter is
698 ACMatchClassifications spans_k
=
699 classify_test6
.RunTest(ASCIIToUTF16("html pass"));
700 EXPECT_EQ("0,6,4,4,5,6,9,0",
701 AutocompleteMatch::ClassificationsToString(spans_k
));
703 // Multiple matches with both beginning and end at beginning of
704 // classifications merge properly.
705 matches
= AutocompleteMatch::ClassificationsFromString("0,1,11,0");
706 ClassifyTest
classify_test7(ASCIIToUTF16("http://a.co is great"), matches
);
708 ACMatchClassifications spans_l
=
709 classify_test7
.RunTest(ASCIIToUTF16("ht co"));
710 EXPECT_EQ("0,3,2,1,9,3,11,0",
711 AutocompleteMatch::ClassificationsToString(spans_l
));
714 TEST_F(ShortcutsProviderTest
, CalculateScore
) {
715 ShortcutsDatabase::Shortcut
shortcut(
716 std::string(), ASCIIToUTF16("test"),
717 ShortcutsDatabase::Shortcut::MatchCore(
718 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"),
719 ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1",
720 ASCIIToUTF16("A test"), "0,0,2,2", ui::PAGE_TRANSITION_TYPED
,
721 AutocompleteMatchType::HISTORY_URL
, base::string16()),
722 base::Time::Now(), 1);
725 const int max_relevance
=
726 ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance
;
727 const int kMaxScore
= CalculateScore("test", shortcut
, max_relevance
);
729 // Score decreases as percent of the match is decreased.
730 int score_three_quarters
= CalculateScore("tes", shortcut
, max_relevance
);
731 EXPECT_LT(score_three_quarters
, kMaxScore
);
732 int score_one_half
= CalculateScore("te", shortcut
, max_relevance
);
733 EXPECT_LT(score_one_half
, score_three_quarters
);
734 int score_one_quarter
= CalculateScore("t", shortcut
, max_relevance
);
735 EXPECT_LT(score_one_quarter
, score_one_half
);
737 // Should decay with time - one week.
738 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(7);
739 int score_week_old
= CalculateScore("test", shortcut
, max_relevance
);
740 EXPECT_LT(score_week_old
, kMaxScore
);
742 // Should decay more in two weeks.
743 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
744 int score_two_weeks_old
= CalculateScore("test", shortcut
, max_relevance
);
745 EXPECT_LT(score_two_weeks_old
, score_week_old
);
747 // But not if it was activly clicked on. 2 hits slow decaying power.
748 shortcut
.number_of_hits
= 2;
749 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
750 int score_popular_two_weeks_old
=
751 CalculateScore("test", shortcut
, max_relevance
);
752 EXPECT_LT(score_two_weeks_old
, score_popular_two_weeks_old
);
753 // But still decayed.
754 EXPECT_LT(score_popular_two_weeks_old
, kMaxScore
);
756 // 3 hits slow decaying power even more.
757 shortcut
.number_of_hits
= 3;
758 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
759 int score_more_popular_two_weeks_old
=
760 CalculateScore("test", shortcut
, max_relevance
);
761 EXPECT_LT(score_two_weeks_old
, score_more_popular_two_weeks_old
);
762 EXPECT_LT(score_popular_two_weeks_old
, score_more_popular_two_weeks_old
);
763 // But still decayed.
764 EXPECT_LT(score_more_popular_two_weeks_old
, kMaxScore
);
767 TEST_F(ShortcutsProviderTest
, DeleteMatch
) {
768 TestShortcutInfo shortcuts_to_test_delete
[] = {
769 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1",
770 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
771 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED
,
772 AutocompleteMatchType::HISTORY_URL
, "", 1, 1},
773 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1",
774 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
775 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED
,
776 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 1},
777 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2",
778 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2",
779 "Keep this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED
,
780 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 1},
781 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2",
782 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2",
783 "Erase this shortcut!", "0,0", ui::PAGE_TRANSITION_TYPED
,
784 AutocompleteMatchType::HISTORY_URL
, "", 1, 1},
787 size_t original_shortcuts_count
= backend_
->shortcuts_map().size();
789 FillData(shortcuts_to_test_delete
, arraysize(shortcuts_to_test_delete
));
791 EXPECT_EQ(original_shortcuts_count
+ 4, backend_
->shortcuts_map().size());
792 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
793 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
794 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
795 backend_
->shortcuts_map().find(ASCIIToUTF16("erase")));
797 AutocompleteMatch
match(
798 provider_
.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE
);
800 match
.destination_url
= GURL(shortcuts_to_test_delete
[0].destination_url
);
801 match
.contents
= ASCIIToUTF16(shortcuts_to_test_delete
[0].contents
);
802 match
.description
= ASCIIToUTF16(shortcuts_to_test_delete
[0].description
);
804 provider_
->DeleteMatch(match
);
806 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be
807 // deleted, but not shortcuts_to_test_delete[2] or
808 // shortcuts_to_test_delete[3], which have different URLs.
809 EXPECT_EQ(original_shortcuts_count
+ 2, backend_
->shortcuts_map().size());
810 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
811 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
812 EXPECT_TRUE(backend_
->shortcuts_map().end() ==
813 backend_
->shortcuts_map().find(ASCIIToUTF16("erase")));
815 match
.destination_url
= GURL(shortcuts_to_test_delete
[3].destination_url
);
816 match
.contents
= ASCIIToUTF16(shortcuts_to_test_delete
[3].contents
);
817 match
.description
= ASCIIToUTF16(shortcuts_to_test_delete
[3].description
);
819 provider_
->DeleteMatch(match
);
820 EXPECT_EQ(original_shortcuts_count
+ 1, backend_
->shortcuts_map().size());
821 EXPECT_TRUE(backend_
->shortcuts_map().end() ==
822 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
825 TEST_F(ShortcutsProviderTest
, DoesNotProvideOnFocus
) {
826 AutocompleteInput
input(ASCIIToUTF16("about:o"), base::string16::npos
,
827 std::string(), GURL(),
828 metrics::OmniboxEventProto::INVALID_SPEC
,
829 false, false, true, true,
830 ChromeAutocompleteSchemeClassifier(&profile_
));
831 provider_
->Start(input
, false, true);
832 EXPECT_TRUE(provider_
->matches().empty());
835 #if defined(ENABLE_EXTENSIONS)
836 TEST_F(ShortcutsProviderTest
, Extension
) {
837 // Try an input string that matches an extension URL.
838 base::string16
text(ASCIIToUTF16("echo"));
839 std::string
expected_url(
840 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo");
841 ExpectedURLs expected_urls
;
842 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
843 expected_url
, true));
844 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16(" echo"));
846 // Claim the extension has been unloaded.
847 scoped_refptr
<const extensions::Extension
> extension
=
848 extensions::ExtensionBuilder()
849 .SetManifest(extensions::DictionaryBuilder()
851 .Set("version", "1.0"))
852 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee")
854 extensions::UnloadedExtensionInfo
details(
855 extension
.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL
);
856 content::NotificationService::current()->Notify(
857 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
858 content::Source
<Profile
>(&profile_
),
859 content::Details
<extensions::UnloadedExtensionInfo
>(&details
));
861 // Now the URL should have disappeared.
862 RunTest(text
, false, ExpectedURLs(), std::string(), base::string16());