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/autocomplete_input.h"
21 #include "chrome/browser/autocomplete/autocomplete_match.h"
22 #include "chrome/browser/autocomplete/autocomplete_provider.h"
23 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
24 #include "chrome/browser/autocomplete/autocomplete_result.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/history/history_service.h"
27 #include "chrome/browser/history/in_memory_url_index.h"
28 #include "chrome/browser/history/shortcuts_backend.h"
29 #include "chrome/browser/history/shortcuts_backend_factory.h"
30 #include "chrome/browser/history/url_database.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/testing_profile.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/test/test_browser_thread.h"
35 #include "extensions/common/extension.h"
36 #include "extensions/common/extension_builder.h"
37 #include "extensions/common/value_builder.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 using base::ASCIIToUTF16
;
42 // TestShortcutInfo -----------------------------------------------------------
46 struct TestShortcutInfo
{
49 std::string fill_into_edit
;
50 std::string destination_url
;
52 std::string contents_class
;
53 std::string description
;
54 std::string description_class
;
55 content::PageTransition transition
;
56 AutocompleteMatch::Type type
;
60 } shortcut_test_db
[] = {
61 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E0", "goog", "www.google.com",
62 "http://www.google.com/", "Google", "0,1,4,0", "Google", "0,3,4,1",
63 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
65 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E1", "slash", "slashdot.org",
66 "http://slashdot.org/", "slashdot.org", "0,3,5,1",
67 "Slashdot - News for nerds, stuff that matters", "0,2,5,0",
68 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 0,
70 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E2", "news", "slashdot.org",
71 "http://slashdot.org/", "slashdot.org", "0,1",
72 "Slashdot - News for nerds, stuff that matters", "0,0,11,2,15,0",
73 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 0,
75 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E3", "news", "sports.yahoo.com",
76 "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
77 "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
78 "0,0,23,2,27,0", content::PAGE_TRANSITION_TYPED
,
79 AutocompleteMatchType::HISTORY_TITLE
, "", 2, 5 },
80 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E4", "news weather",
81 "www.cnn.com/index.html", "http://www.cnn.com/index.html",
82 "www.cnn.com/index.html", "0,1",
83 "CNN.com - Breaking News, U.S., World, Weather, Entertainment & Video",
84 "0,0,19,2,23,0,38,2,45,0", content::PAGE_TRANSITION_TYPED
,
85 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 10 },
86 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E5", "nhl scores", "sports.yahoo.com",
87 "http://sports.yahoo.com/", "sports.yahoo.com", "0,1",
88 "Yahoo! Sports - Sports News, Scores, Rumors, Fantasy Games, and more",
89 "0,0,29,2,35,0", content::PAGE_TRANSITION_TYPED
,
90 AutocompleteMatchType::HISTORY_BODY
, "", 1, 10 },
91 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E6", "nhl scores",
92 "www.nhl.com/scores/index.html", "http://www.nhl.com/scores/index.html",
93 "www.nhl.com/scores/index.html", "0,1,4,3,7,1",
94 "January 13, 2010 - NHL.com - Scores", "0,0,19,2,22,0,29,2,35,0",
95 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 5,
97 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E7", "just", "www.testsite.com/a.html",
98 "http://www.testsite.com/a.html", "www.testsite.com/a.html", "0,1",
99 "Test - site - just a test", "0,0,14,2,18,0",
100 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 5,
102 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E8", "just", "www.testsite.com/b.html",
103 "http://www.testsite.com/b.html", "www.testsite.com/b.html", "0,1",
104 "Test - site - just a test", "0,0,14,2,18,0",
105 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 5,
107 { "BD85DBA2-8C29-49F9-84AE-48E1E90880E9", "just", "www.testsite.com/c.html",
108 "http://www.testsite.com/c.html", "www.testsite.com/c.html", "0,1",
109 "Test - site - just a test", "0,0,14,2,18,0",
110 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "", 8,
112 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EA", "just a", "www.testsite.com/d.html",
113 "http://www.testsite.com/d.html", "www.testsite.com/d.html", "0,1",
114 "Test - site - just a test", "0,0,14,2,18,0",
115 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_TITLE
, "",
117 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EB", "just a t",
118 "www.testsite.com/e.html", "http://www.testsite.com/e.html",
119 "www.testsite.com/e.html", "0,1", "Test - site - just a test",
120 "0,0,14,2,18,0", content::PAGE_TRANSITION_TYPED
,
121 AutocompleteMatchType::HISTORY_TITLE
, "", 12, 1 },
122 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EC", "just a te",
123 "www.testsite.com/f.html", "http://www.testsite.com/f.html",
124 "www.testsite.com/f.html", "0,1", "Test - site - just a test",
125 "0,0,14,2,18,0", content::PAGE_TRANSITION_TYPED
,
126 AutocompleteMatchType::HISTORY_TITLE
, "", 12, 1 },
127 { "BD85DBA2-8C29-49F9-84AE-48E1E90880ED", "ago", "www.daysagotest.com/a.html",
128 "http://www.daysagotest.com/a.html", "www.daysagotest.com/a.html",
129 "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED
,
130 AutocompleteMatchType::HISTORY_URL
, "", 1, 1 },
131 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EE", "ago", "www.daysagotest.com/b.html",
132 "http://www.daysagotest.com/b.html", "www.daysagotest.com/b.html",
133 "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED
,
134 AutocompleteMatchType::HISTORY_URL
, "", 2, 1 },
135 { "BD85DBA2-8C29-49F9-84AE-48E1E90880EF", "ago", "www.daysagotest.com/c.html",
136 "http://www.daysagotest.com/c.html", "www.daysagotest.com/c.html",
137 "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED
,
138 AutocompleteMatchType::HISTORY_URL
, "", 3, 1 },
139 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F0", "ago", "www.daysagotest.com/d.html",
140 "http://www.daysagotest.com/d.html", "www.daysagotest.com/d.html",
141 "0,1,8,3,11,1", "Test - site", "0,0", content::PAGE_TRANSITION_TYPED
,
142 AutocompleteMatchType::HISTORY_URL
, "", 4, 1 },
143 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", "echo echo", "echo echo",
144 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo",
145 "Run Echo command: echo", "0,0", "Echo", "0,4",
146 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::EXTENSION_APP
,
148 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", "abcdef.com", "http://abcdef.com",
149 "http://abcdef.com/", "Abcdef", "0,1,4,0", "Abcdef", "0,3,4,1",
150 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "", 1,
152 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", "query", "query",
153 "https://www.google.com/search?q=query", "query", "0,0",
154 "Google Search", "0,4", content::PAGE_TRANSITION_GENERATED
,
155 AutocompleteMatchType::SEARCH_HISTORY
, "", 1, 100 },
156 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F4", "word", "www.word",
157 "https://www.google.com/search?q=www.word", "www.word", "0,0",
158 "Google Search", "0,4", content::PAGE_TRANSITION_GENERATED
,
159 AutocompleteMatchType::SEARCH_HISTORY
, "", 1, 100 },
160 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F5", "about:o", "chrome://omnibox",
161 "chrome://omnibox/", "about:omnibox", "0,3,10,1", "", "",
162 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::NAVSUGGEST
, "",
164 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F6", "www/real sp",
165 "http://www/real space/long-url-with-space.html",
166 "http://www/real%20space/long-url-with-space.html",
167 "www/real space/long-url-with-space.html", "0,3,11,1",
168 "Page With Space; Input with Space", "0,0",
169 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
, "",
176 // ClassifyTest ---------------------------------------------------------------
178 // Helper class to make running tests of ClassifyAllMatchesInString() more
182 ClassifyTest(const base::string16
& text
, ACMatchClassifications matches
);
185 ACMatchClassifications
RunTest(const base::string16
& find_text
);
188 const base::string16 text_
;
189 const ACMatchClassifications matches_
;
192 ClassifyTest::ClassifyTest(const base::string16
& text
,
193 ACMatchClassifications matches
)
198 ClassifyTest::~ClassifyTest() {
201 ACMatchClassifications
ClassifyTest::RunTest(const base::string16
& find_text
) {
202 return ShortcutsProvider::ClassifyAllMatchesInString(find_text
,
203 ShortcutsProvider::CreateWordMapForString(find_text
), text_
, matches_
);
209 // ShortcutsProviderTest ------------------------------------------------------
211 class ShortcutsProviderTest
: public testing::Test
,
212 public AutocompleteProviderListener
{
214 ShortcutsProviderTest();
216 // AutocompleteProviderListener:
217 virtual void OnProviderUpdate(bool updated_matches
) OVERRIDE
;
220 typedef std::pair
<std::string
, bool> ExpectedURLAndAllowedToBeDefault
;
221 typedef std::vector
<ExpectedURLAndAllowedToBeDefault
> ExpectedURLs
;
223 class SetShouldContain
224 : public std::unary_function
<const ExpectedURLAndAllowedToBeDefault
&,
225 std::set
<std::string
> > {
227 explicit SetShouldContain(const ACMatches
& matched_urls
);
229 void operator()(const ExpectedURLAndAllowedToBeDefault
& expected
);
230 std::set
<ExpectedURLAndAllowedToBeDefault
> Leftovers() const {
235 std::set
<ExpectedURLAndAllowedToBeDefault
> matches_
;
238 virtual void SetUp();
239 virtual void TearDown();
241 // Fills test data into the provider.
242 void FillData(TestShortcutInfo
* db
, size_t db_size
);
244 // Runs an autocomplete query on |text| with the provided
245 // |prevent_inline_autocomplete| setting and checks to see that the returned
246 // results' destination URLs match those provided. |expected_urls| does not
247 // need to be in sorted order, but |expected_top_result| should be the top
248 // match, and it should have inline autocompletion
249 // |top_result_inline_autocompletion|.
250 void RunTest(const base::string16 text
,
251 bool prevent_inline_autocomplete
,
252 const ExpectedURLs
& expected_urls
,
253 std::string expected_top_result
,
254 base::string16 top_result_inline_autocompletion
);
256 // Passthrough to the private function in provider_.
257 int CalculateScore(const std::string
& terms
,
258 const ShortcutsBackend::Shortcut
& shortcut
,
261 base::MessageLoopForUI message_loop_
;
262 content::TestBrowserThread ui_thread_
;
263 content::TestBrowserThread file_thread_
;
265 TestingProfile profile_
;
267 ACMatches ac_matches_
; // The resulting matches after running RunTest.
269 scoped_refptr
<ShortcutsBackend
> backend_
;
270 scoped_refptr
<ShortcutsProvider
> provider_
;
273 ShortcutsProviderTest::ShortcutsProviderTest()
274 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
275 file_thread_(content::BrowserThread::FILE, &message_loop_
) {
278 void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches
) {}
280 void ShortcutsProviderTest::SetUp() {
281 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse(
282 &profile_
, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting
);
283 backend_
= ShortcutsBackendFactory::GetForProfile(&profile_
);
284 ASSERT_TRUE(backend_
.get());
285 ASSERT_TRUE(profile_
.CreateHistoryService(true, false));
286 provider_
= new ShortcutsProvider(this, &profile_
);
287 FillData(shortcut_test_db
, arraysize(shortcut_test_db
));
290 void ShortcutsProviderTest::TearDown() {
291 // Run all pending tasks or else some threads hold on to the message loop
292 // and prevent it from being deleted.
293 message_loop_
.RunUntilIdle();
297 void ShortcutsProviderTest::FillData(TestShortcutInfo
* db
, size_t db_size
) {
298 DCHECK(provider_
.get());
299 size_t expected_size
= backend_
->shortcuts_map().size() + db_size
;
300 for (size_t i
= 0; i
< db_size
; ++i
) {
301 const TestShortcutInfo
& cur
= db
[i
];
302 ShortcutsBackend::Shortcut
shortcut(
303 cur
.guid
, ASCIIToUTF16(cur
.text
),
304 ShortcutsBackend::Shortcut::MatchCore(
305 ASCIIToUTF16(cur
.fill_into_edit
), GURL(cur
.destination_url
),
306 ASCIIToUTF16(cur
.contents
),
307 AutocompleteMatch::ClassificationsFromString(cur
.contents_class
),
308 ASCIIToUTF16(cur
.description
),
309 AutocompleteMatch::ClassificationsFromString(cur
.description_class
),
310 cur
.transition
, cur
.type
, ASCIIToUTF16(cur
.keyword
)),
311 base::Time::Now() - base::TimeDelta::FromDays(cur
.days_from_now
),
313 backend_
->AddShortcut(shortcut
);
315 EXPECT_EQ(expected_size
, backend_
->shortcuts_map().size());
318 ShortcutsProviderTest::SetShouldContain::SetShouldContain(
319 const ACMatches
& matched_urls
) {
320 for (ACMatches::const_iterator iter
= matched_urls
.begin();
321 iter
!= matched_urls
.end(); ++iter
)
322 matches_
.insert(ExpectedURLAndAllowedToBeDefault(
323 iter
->destination_url
.spec(), iter
->allowed_to_be_default_match
));
326 void ShortcutsProviderTest::SetShouldContain::operator()(
327 const ExpectedURLAndAllowedToBeDefault
& expected
) {
328 EXPECT_EQ(1U, matches_
.erase(expected
));
331 void ShortcutsProviderTest::RunTest(
332 const base::string16 text
,
333 bool prevent_inline_autocomplete
,
334 const ExpectedURLs
& expected_urls
,
335 std::string expected_top_result
,
336 base::string16 top_result_inline_autocompletion
) {
337 base::MessageLoop::current()->RunUntilIdle();
338 AutocompleteInput
input(text
, base::string16::npos
, base::string16(), GURL(),
339 AutocompleteInput::INVALID_SPEC
,
340 prevent_inline_autocomplete
, false, true,
341 AutocompleteInput::ALL_MATCHES
);
342 provider_
->Start(input
, false);
343 EXPECT_TRUE(provider_
->done());
345 ac_matches_
= provider_
->matches();
347 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
348 EXPECT_LE(ac_matches_
.size(), AutocompleteProvider::kMaxMatches
);
350 // If the number of expected and actual matches aren't equal then we need
351 // test no further, but let's do anyway so that we know which URLs failed.
352 EXPECT_EQ(expected_urls
.size(), ac_matches_
.size());
354 // Verify that all expected URLs were found and that all found URLs
356 std::set
<ExpectedURLAndAllowedToBeDefault
> Leftovers
=
357 for_each(expected_urls
.begin(), expected_urls
.end(),
358 SetShouldContain(ac_matches_
)).Leftovers();
359 EXPECT_EQ(0U, Leftovers
.size());
361 // See if we got the expected top scorer.
362 if (!ac_matches_
.empty()) {
363 std::partial_sort(ac_matches_
.begin(), ac_matches_
.begin() + 1,
364 ac_matches_
.end(), AutocompleteMatch::MoreRelevant
);
365 EXPECT_EQ(expected_top_result
, ac_matches_
[0].destination_url
.spec());
366 EXPECT_EQ(top_result_inline_autocompletion
,
367 ac_matches_
[0].inline_autocompletion
);
371 int ShortcutsProviderTest::CalculateScore(
372 const std::string
& terms
,
373 const ShortcutsBackend::Shortcut
& shortcut
,
375 return provider_
->CalculateScore(ASCIIToUTF16(terms
), shortcut
,
380 // Actual tests ---------------------------------------------------------------
382 TEST_F(ShortcutsProviderTest
, SimpleSingleMatch
) {
383 base::string16
text(ASCIIToUTF16("go"));
384 std::string
expected_url("http://www.google.com/");
385 ExpectedURLs expected_urls
;
386 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
387 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("ogle.com"));
389 // Same test with prevent inline autocomplete.
390 expected_urls
.clear();
391 expected_urls
.push_back(
392 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
393 // The match will have an |inline_autocompletion| set, but the value will not
394 // be used because |allowed_to_be_default_match| will be false.
395 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("ogle.com"));
397 // A pair of analogous tests where the shortcut ends at the end of
398 // |fill_into_edit|. This exercises the inline autocompletion and default
400 text
= ASCIIToUTF16("abcdef.com");
401 expected_url
= "http://abcdef.com/";
402 expected_urls
.clear();
403 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
404 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
405 // With prevent inline autocomplete, the suggestion should be the same
406 // (because there is no completion).
407 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
409 // Another test, simply for a query match type, not a navigation URL match
411 text
= ASCIIToUTF16("que");
412 expected_url
= "https://www.google.com/search?q=query";
413 expected_urls
.clear();
414 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
415 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("ry"));
417 // Same test with prevent inline autocomplete.
418 expected_urls
.clear();
419 expected_urls
.push_back(
420 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
421 // The match will have an |inline_autocompletion| set, but the value will not
422 // be used because |allowed_to_be_default_match| will be false.
423 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("ry"));
425 // A pair of analogous tests where the shortcut ends at the end of
426 // |fill_into_edit|. This exercises the inline autocompletion and default
428 text
= ASCIIToUTF16("query");
429 expected_urls
.clear();
430 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
431 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
432 // With prevent inline autocomplete, the suggestion should be the same
433 // (because there is no completion).
434 RunTest(text
, true, expected_urls
, expected_url
, base::string16());
436 // Now the shortcut ends at the end of |fill_into_edit| but has a
437 // non-droppable prefix. ("www.", for instance, is not droppable for
439 text
= ASCIIToUTF16("word");
440 expected_url
= "https://www.google.com/search?q=www.word";
441 expected_urls
.clear();
442 expected_urls
.push_back(
443 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
444 RunTest(text
, false, expected_urls
, expected_url
, base::string16());
447 // These tests are like those in SimpleSingleMatch but more complex,
448 // involving URLs that need to be fixed up to match properly.
449 TEST_F(ShortcutsProviderTest
, TrickySingleMatch
) {
450 // Test that about: URLs are fixed up/transformed to chrome:// URLs.
451 base::string16
text(ASCIIToUTF16("about:o"));
452 std::string
expected_url("chrome://omnibox/");
453 ExpectedURLs expected_urls
;
454 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
455 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16("mnibox"));
457 // Same test with prevent inline autocomplete.
458 expected_urls
.clear();
459 expected_urls
.push_back(
460 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
461 // The match will have an |inline_autocompletion| set, but the value will not
462 // be used because |allowed_to_be_default_match| will be false.
463 RunTest(text
, true, expected_urls
, expected_url
, ASCIIToUTF16("mnibox"));
465 // Test that an input with a space can match URLs with a (escaped) space.
466 // This would fail if we didn't try to lookup the un-fixed-up string.
467 text
= ASCIIToUTF16("www/real sp");
468 expected_url
= "http://www/real%20space/long-url-with-space.html";
469 expected_urls
.clear();
470 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(expected_url
, true));
471 RunTest(text
, false, expected_urls
, expected_url
,
472 ASCIIToUTF16("ace/long-url-with-space.html"));
474 // Same test with prevent inline autocomplete.
475 expected_urls
.clear();
476 expected_urls
.push_back(
477 ExpectedURLAndAllowedToBeDefault(expected_url
, false));
478 // The match will have an |inline_autocompletion| set, but the value will not
479 // be used because |allowed_to_be_default_match| will be false.
480 RunTest(text
, true, expected_urls
, expected_url
,
481 ASCIIToUTF16("ace/long-url-with-space.html"));
484 TEST_F(ShortcutsProviderTest
, MultiMatch
) {
485 base::string16
text(ASCIIToUTF16("NEWS"));
486 ExpectedURLs expected_urls
;
487 // Scores high because of completion length.
488 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
489 "http://slashdot.org/", false));
490 // Scores high because of visit count.
491 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
492 "http://sports.yahoo.com/", false));
493 // Scores high because of visit count but less match span,
494 // which is more important.
495 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
496 "http://www.cnn.com/index.html", false));
497 RunTest(text
, false, expected_urls
, "http://slashdot.org/", base::string16());
500 TEST_F(ShortcutsProviderTest
, TypedCountMatches
) {
501 base::string16
text(ASCIIToUTF16("just"));
502 ExpectedURLs expected_urls
;
503 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
504 "http://www.testsite.com/b.html", false));
505 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
506 "http://www.testsite.com/a.html", false));
507 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
508 "http://www.testsite.com/c.html", false));
509 RunTest(text
, false, expected_urls
, "http://www.testsite.com/b.html",
513 TEST_F(ShortcutsProviderTest
, FragmentLengthMatches
) {
514 base::string16
text(ASCIIToUTF16("just a"));
515 ExpectedURLs expected_urls
;
516 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
517 "http://www.testsite.com/d.html", false));
518 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
519 "http://www.testsite.com/e.html", false));
520 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
521 "http://www.testsite.com/f.html", false));
522 RunTest(text
, false, expected_urls
, "http://www.testsite.com/d.html",
526 TEST_F(ShortcutsProviderTest
, DaysAgoMatches
) {
527 base::string16
text(ASCIIToUTF16("ago"));
528 ExpectedURLs expected_urls
;
529 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
530 "http://www.daysagotest.com/a.html", false));
531 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
532 "http://www.daysagotest.com/b.html", false));
533 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
534 "http://www.daysagotest.com/c.html", false));
535 RunTest(text
, false, expected_urls
, "http://www.daysagotest.com/a.html",
539 TEST_F(ShortcutsProviderTest
, ClassifyAllMatchesInString
) {
540 ACMatchClassifications matches
=
541 AutocompleteMatch::ClassificationsFromString("0,0");
542 ClassifyTest
classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"),
545 ACMatchClassifications spans_a
= classify_test
.RunTest(ASCIIToUTF16("man"));
546 // ACMatch spans should be: '--MMM------------------------'
547 EXPECT_EQ("0,0,2,2,5,0", AutocompleteMatch::ClassificationsToString(spans_a
));
549 ACMatchClassifications spans_b
= classify_test
.RunTest(ASCIIToUTF16("man p"));
550 // ACMatch spans should be: '--MMM----M-------------M-----'
551 EXPECT_EQ("0,0,2,2,5,0,9,2,10,0,23,2,24,0",
552 AutocompleteMatch::ClassificationsToString(spans_b
));
554 ACMatchClassifications spans_c
=
555 classify_test
.RunTest(ASCIIToUTF16("man plan panama"));
556 // ACMatch spans should be:'--MMM----MMMM----------MMMMMM'
557 EXPECT_EQ("0,0,2,2,5,0,9,2,13,0,23,2",
558 AutocompleteMatch::ClassificationsToString(spans_c
));
560 ClassifyTest
classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, "
561 "Scores, Rumors, Fantasy Games, and more"), matches
);
563 ACMatchClassifications spans_d
= classify_test2
.RunTest(ASCIIToUTF16("ne"));
564 // ACMatch spans should match first two letters of the "news".
565 EXPECT_EQ("0,0,23,2,25,0",
566 AutocompleteMatch::ClassificationsToString(spans_d
));
568 ACMatchClassifications spans_e
=
569 classify_test2
.RunTest(ASCIIToUTF16("news r"));
570 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,"
571 "66,2,67,0", AutocompleteMatch::ClassificationsToString(spans_e
));
573 matches
= AutocompleteMatch::ClassificationsFromString("0,1");
574 ClassifyTest
classify_test3(ASCIIToUTF16("livescore.goal.com"), matches
);
576 ACMatchClassifications spans_f
= classify_test3
.RunTest(ASCIIToUTF16("go"));
577 // ACMatch spans should match first two letters of the "goal".
578 EXPECT_EQ("0,1,10,3,12,1",
579 AutocompleteMatch::ClassificationsToString(spans_f
));
581 matches
= AutocompleteMatch::ClassificationsFromString("0,0,13,1");
582 ClassifyTest
classify_test4(ASCIIToUTF16("Email login: mail.somecorp.com"),
585 ACMatchClassifications spans_g
= classify_test4
.RunTest(ASCIIToUTF16("ail"));
586 EXPECT_EQ("0,0,2,2,5,0,13,1,14,3,17,1",
587 AutocompleteMatch::ClassificationsToString(spans_g
));
589 ACMatchClassifications spans_h
=
590 classify_test4
.RunTest(ASCIIToUTF16("lo log"));
591 EXPECT_EQ("0,0,6,2,9,0,13,1",
592 AutocompleteMatch::ClassificationsToString(spans_h
));
594 ACMatchClassifications spans_i
=
595 classify_test4
.RunTest(ASCIIToUTF16("ail em"));
596 // 'Email' and 'ail' should be matched.
597 EXPECT_EQ("0,2,5,0,13,1,14,3,17,1",
598 AutocompleteMatch::ClassificationsToString(spans_i
));
600 // Some web sites do not have a description. If the string being searched is
601 // empty, the classifications must also be empty: http://crbug.com/148647
602 // Extra parens in the next line hack around C++03's "most vexing parse".
603 class ClassifyTest
classify_test5((base::string16()),
604 ACMatchClassifications());
605 ACMatchClassifications spans_j
= classify_test5
.RunTest(ASCIIToUTF16("man"));
606 ASSERT_EQ(0U, spans_j
.size());
608 // Matches which end at beginning of classification merge properly.
609 matches
= AutocompleteMatch::ClassificationsFromString("0,4,9,0");
610 ClassifyTest
classify_test6(ASCIIToUTF16("html password example"), matches
);
612 // Extra space in the next string avoids having the string be a prefix of the
613 // text above, which would allow for two different valid classification sets,
614 // one of which uses two spans (the first of which would mark all of "html
615 // pass" as a match) and one which uses four (which marks the individual words
616 // as matches but not the space between them). This way only the latter is
618 ACMatchClassifications spans_k
=
619 classify_test6
.RunTest(ASCIIToUTF16("html pass"));
620 EXPECT_EQ("0,6,4,4,5,6,9,0",
621 AutocompleteMatch::ClassificationsToString(spans_k
));
623 // Multiple matches with both beginning and end at beginning of
624 // classifications merge properly.
625 matches
= AutocompleteMatch::ClassificationsFromString("0,1,11,0");
626 ClassifyTest
classify_test7(ASCIIToUTF16("http://a.co is great"), matches
);
628 ACMatchClassifications spans_l
=
629 classify_test7
.RunTest(ASCIIToUTF16("ht co"));
630 EXPECT_EQ("0,3,2,1,9,3,11,0",
631 AutocompleteMatch::ClassificationsToString(spans_l
));
634 TEST_F(ShortcutsProviderTest
, CalculateScore
) {
635 ShortcutsBackend::Shortcut
shortcut(
636 std::string(), ASCIIToUTF16("test"),
637 ShortcutsBackend::Shortcut::MatchCore(
638 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"),
639 ASCIIToUTF16("www.test.com"),
640 AutocompleteMatch::ClassificationsFromString("0,1,4,3,8,1"),
641 ASCIIToUTF16("A test"),
642 AutocompleteMatch::ClassificationsFromString("0,0,2,2"),
643 content::PAGE_TRANSITION_TYPED
, AutocompleteMatchType::HISTORY_URL
,
645 base::Time::Now(), 1);
648 const int max_relevance
= AutocompleteResult::kLowestDefaultScore
- 1;
649 const int kMaxScore
= CalculateScore("test", shortcut
, max_relevance
);
651 // Score decreases as percent of the match is decreased.
652 int score_three_quarters
= CalculateScore("tes", shortcut
, max_relevance
);
653 EXPECT_LT(score_three_quarters
, kMaxScore
);
654 int score_one_half
= CalculateScore("te", shortcut
, max_relevance
);
655 EXPECT_LT(score_one_half
, score_three_quarters
);
656 int score_one_quarter
= CalculateScore("t", shortcut
, max_relevance
);
657 EXPECT_LT(score_one_quarter
, score_one_half
);
659 // Should decay with time - one week.
660 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(7);
661 int score_week_old
= CalculateScore("test", shortcut
, max_relevance
);
662 EXPECT_LT(score_week_old
, kMaxScore
);
664 // Should decay more in two weeks.
665 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
666 int score_two_weeks_old
= CalculateScore("test", shortcut
, max_relevance
);
667 EXPECT_LT(score_two_weeks_old
, score_week_old
);
669 // But not if it was activly clicked on. 2 hits slow decaying power.
670 shortcut
.number_of_hits
= 2;
671 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
672 int score_popular_two_weeks_old
=
673 CalculateScore("test", shortcut
, max_relevance
);
674 EXPECT_LT(score_two_weeks_old
, score_popular_two_weeks_old
);
675 // But still decayed.
676 EXPECT_LT(score_popular_two_weeks_old
, kMaxScore
);
678 // 3 hits slow decaying power even more.
679 shortcut
.number_of_hits
= 3;
680 shortcut
.last_access_time
= base::Time::Now() - base::TimeDelta::FromDays(14);
681 int score_more_popular_two_weeks_old
=
682 CalculateScore("test", shortcut
, max_relevance
);
683 EXPECT_LT(score_two_weeks_old
, score_more_popular_two_weeks_old
);
684 EXPECT_LT(score_popular_two_weeks_old
, score_more_popular_two_weeks_old
);
685 // But still decayed.
686 EXPECT_LT(score_more_popular_two_weeks_old
, kMaxScore
);
689 TEST_F(ShortcutsProviderTest
, DeleteMatch
) {
690 TestShortcutInfo shortcuts_to_test_delete
[] = {
691 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F1", "delete", "www.deletetest.com/1",
692 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
693 "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED
,
694 AutocompleteMatchType::HISTORY_URL
, "", 1, 1},
695 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F2", "erase", "www.deletetest.com/1",
696 "http://www.deletetest.com/1", "http://www.deletetest.com/1", "0,2",
697 "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED
,
698 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 1},
699 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F3", "keep", "www.deletetest.com/1/2",
700 "http://www.deletetest.com/1/2", "http://www.deletetest.com/1/2", "0,2",
701 "Keep this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED
,
702 AutocompleteMatchType::HISTORY_TITLE
, "", 1, 1},
703 { "BD85DBA2-8C29-49F9-84AE-48E1E90881F4", "delete", "www.deletetest.com/2",
704 "http://www.deletetest.com/2", "http://www.deletetest.com/2", "0,2",
705 "Erase this shortcut!", "0,0", content::PAGE_TRANSITION_TYPED
,
706 AutocompleteMatchType::HISTORY_URL
, "", 1, 1},
709 size_t original_shortcuts_count
= backend_
->shortcuts_map().size();
711 FillData(shortcuts_to_test_delete
, arraysize(shortcuts_to_test_delete
));
713 EXPECT_EQ(original_shortcuts_count
+ 4, backend_
->shortcuts_map().size());
714 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
715 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
716 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
717 backend_
->shortcuts_map().find(ASCIIToUTF16("erase")));
719 AutocompleteMatch
match(
720 provider_
.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE
);
722 match
.destination_url
= GURL(shortcuts_to_test_delete
[0].destination_url
);
723 match
.contents
= ASCIIToUTF16(shortcuts_to_test_delete
[0].contents
);
724 match
.description
= ASCIIToUTF16(shortcuts_to_test_delete
[0].description
);
726 provider_
->DeleteMatch(match
);
728 // shortcuts_to_test_delete[0] and shortcuts_to_test_delete[1] should be
729 // deleted, but not shortcuts_to_test_delete[2] or
730 // shortcuts_to_test_delete[3], which have different URLs.
731 EXPECT_EQ(original_shortcuts_count
+ 2, backend_
->shortcuts_map().size());
732 EXPECT_FALSE(backend_
->shortcuts_map().end() ==
733 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
734 EXPECT_TRUE(backend_
->shortcuts_map().end() ==
735 backend_
->shortcuts_map().find(ASCIIToUTF16("erase")));
737 match
.destination_url
= GURL(shortcuts_to_test_delete
[3].destination_url
);
738 match
.contents
= ASCIIToUTF16(shortcuts_to_test_delete
[3].contents
);
739 match
.description
= ASCIIToUTF16(shortcuts_to_test_delete
[3].description
);
741 provider_
->DeleteMatch(match
);
742 EXPECT_EQ(original_shortcuts_count
+ 1, backend_
->shortcuts_map().size());
743 EXPECT_TRUE(backend_
->shortcuts_map().end() ==
744 backend_
->shortcuts_map().find(ASCIIToUTF16("delete")));
747 TEST_F(ShortcutsProviderTest
, Extension
) {
748 // Try an input string that matches an extension URL.
749 base::string16
text(ASCIIToUTF16("echo"));
750 std::string
expected_url(
751 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo");
752 ExpectedURLs expected_urls
;
753 expected_urls
.push_back(ExpectedURLAndAllowedToBeDefault(
754 expected_url
, true));
755 RunTest(text
, false, expected_urls
, expected_url
, ASCIIToUTF16(" echo"));
757 // Claim the extension has been unloaded.
758 scoped_refptr
<const extensions::Extension
> extension
=
759 extensions::ExtensionBuilder()
760 .SetManifest(extensions::DictionaryBuilder()
762 .Set("version", "1.0"))
763 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee")
765 extensions::UnloadedExtensionInfo
details(
766 extension
.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL
);
767 content::NotificationService::current()->Notify(
768 chrome::NOTIFICATION_EXTENSION_UNLOADED
,
769 content::Source
<Profile
>(&profile_
),
770 content::Details
<extensions::UnloadedExtensionInfo
>(&details
));
772 // Now the URL should have disappeared.
773 RunTest(text
, false, ExpectedURLs(), std::string(), base::string16());
776 } // namespace history