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