shortcuts_provider_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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. 4 5#include "chrome/browser/autocomplete/shortcuts_provider.h" 6 7#include <math.h> 8 9#include <algorithm> 10#include <functional> 11#include <set> 12#include <string> 13#include <vector> 14 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/shortcuts_backend.h" 22#include "chrome/browser/autocomplete/shortcuts_backend_factory.h" 23#include "chrome/browser/history/history_service.h" 24#include "chrome/browser/history/in_memory_url_index.h" 25#include "chrome/common/pref_names.h" 26#include "chrome/test/base/testing_profile.h" 27#include "components/history/core/browser/url_database.h" 28#include "components/metrics/proto/omnibox_event.pb.h" 29#include "components/omnibox/autocomplete_input.h" 30#include "components/omnibox/autocomplete_match.h" 31#include "components/omnibox/autocomplete_provider.h" 32#include "components/omnibox/autocomplete_result.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" 36 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" 42#endif 43 44using base::ASCIIToUTF16; 45 46// TestShortcutInfo ----------------------------------------------------------- 47 48namespace { 49 50struct TestShortcutInfo { 51 std::string guid; 52 std::string text; 53 std::string fill_into_edit; 54 std::string destination_url; 55 std::string contents; 56 std::string contents_class; 57 std::string description; 58 std::string description_class; 59 ui::PageTransition transition; 60 AutocompleteMatch::Type type; 61 std::string keyword; 62 int days_from_now; 63 int number_of_hits; 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, 68 100 }, 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, 73 100 }, 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, 78 5 }, 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, 100 1 }, 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, 105 1 }, 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, 110 2 }, 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, 115 1 }, 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, "", 120 12, 1 }, 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, 151 "echo", 1, 1 }, 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, 155 100 }, 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, "", 167 1, 100 }, 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, "", 174 1, 100 }, 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, 178 100 }, 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, 182 100 }, 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, "", 187 1, 100 }, 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, "", 192 1, 100 }, 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 }, 203}; 204 205} // namespace 206 207 208// ClassifyTest --------------------------------------------------------------- 209 210// Helper class to make running tests of ClassifyAllMatchesInString() more 211// convenient. 212class ClassifyTest { 213 public: 214 ClassifyTest(const base::string16& text, ACMatchClassifications matches); 215 ~ClassifyTest(); 216 217 ACMatchClassifications RunTest(const base::string16& find_text); 218 219 private: 220 const base::string16 text_; 221 const ACMatchClassifications matches_; 222}; 223 224ClassifyTest::ClassifyTest(const base::string16& text, 225 ACMatchClassifications matches) 226 : text_(text), 227 matches_(matches) { 228} 229 230ClassifyTest::~ClassifyTest() { 231} 232 233ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { 234 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, 235 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); 236} 237 238 239// ShortcutsProviderTest ------------------------------------------------------ 240 241class ShortcutsProviderTest : public testing::Test { 242 public: 243 ShortcutsProviderTest(); 244 245 protected: 246 typedef std::pair<std::string, bool> ExpectedURLAndAllowedToBeDefault; 247 typedef std::vector<ExpectedURLAndAllowedToBeDefault> ExpectedURLs; 248 249 class SetShouldContain 250 : public std::unary_function<const ExpectedURLAndAllowedToBeDefault&, 251 std::set<std::string> > { 252 public: 253 explicit SetShouldContain(const ACMatches& matched_urls); 254 255 void operator()(const ExpectedURLAndAllowedToBeDefault& expected); 256 std::set<ExpectedURLAndAllowedToBeDefault> Leftovers() const { 257 return matches_; 258 } 259 260 private: 261 std::set<ExpectedURLAndAllowedToBeDefault> matches_; 262 }; 263 264 virtual void SetUp(); 265 virtual void TearDown(); 266 267 // Fills test data into the provider. 268 void FillData(TestShortcutInfo* db, size_t db_size); 269 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); 281 282 // Passthrough to the private function in provider_. 283 int CalculateScore(const std::string& terms, 284 const history::ShortcutsDatabase::Shortcut& shortcut, 285 int max_relevance); 286 287 base::MessageLoopForUI message_loop_; 288 content::TestBrowserThread ui_thread_; 289 content::TestBrowserThread file_thread_; 290 291 TestingProfile profile_; 292 293 ACMatches ac_matches_; // The resulting matches after running RunTest. 294 295 scoped_refptr<ShortcutsBackend> backend_; 296 scoped_refptr<ShortcutsProvider> provider_; 297}; 298 299ShortcutsProviderTest::ShortcutsProviderTest() 300 : ui_thread_(content::BrowserThread::UI, &message_loop_), 301 file_thread_(content::BrowserThread::FILE, &message_loop_) { 302} 303 304void ShortcutsProviderTest::SetUp() { 305 ShortcutsBackendFactory::GetInstance()->SetTestingFactoryAndUse( 306 &profile_, &ShortcutsBackendFactory::BuildProfileNoDatabaseForTesting); 307 backend_ = ShortcutsBackendFactory::GetForProfile(&profile_); 308 ASSERT_TRUE(backend_.get()); 309 ASSERT_TRUE(profile_.CreateHistoryService(true, false)); 310 provider_ = new ShortcutsProvider(&profile_); 311 FillData(shortcut_test_db, arraysize(shortcut_test_db)); 312} 313 314void ShortcutsProviderTest::TearDown() { 315 // Run all pending tasks or else some threads hold on to the message loop 316 // and prevent it from being deleted. 317 message_loop_.RunUntilIdle(); 318 provider_ = NULL; 319} 320 321void 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 history::ShortcutsDatabase::Shortcut shortcut( 327 cur.guid, ASCIIToUTF16(cur.text), 328 history::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), 334 cur.number_of_hits); 335 backend_->AddShortcut(shortcut); 336 } 337 EXPECT_EQ(expected_size, backend_->shortcuts_map().size()); 338} 339 340ShortcutsProviderTest::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)); 346} 347 348void ShortcutsProviderTest::SetShouldContain::operator()( 349 const ExpectedURLAndAllowedToBeDefault& expected) { 350 EXPECT_EQ(1U, matches_.erase(expected)); 351} 352 353void 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, base::string16(), GURL(), 361 metrics::OmniboxEventProto::INVALID_SPEC, 362 prevent_inline_autocomplete, false, true, true, 363 ChromeAutocompleteSchemeClassifier(&profile_)); 364 provider_->Start(input, false); 365 EXPECT_TRUE(provider_->done()); 366 367 ac_matches_ = provider_->matches(); 368 369 // We should have gotten back at most AutocompleteProvider::kMaxMatches. 370 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); 371 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()); 375 376 // Verify that all expected URLs were found and that all found URLs 377 // were expected. 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()); 382 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); 390 } 391} 392 393int ShortcutsProviderTest::CalculateScore( 394 const std::string& terms, 395 const history::ShortcutsDatabase::Shortcut& shortcut, 396 int max_relevance) { 397 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, 398 max_relevance); 399} 400 401 402// Actual tests --------------------------------------------------------------- 403 404TEST_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")); 410 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")); 418 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 421 // match code. 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()); 430 431 // Another test, simply for a query match type, not a navigation URL match 432 // type. 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")); 438 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")); 446 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 449 // match code. 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()); 457 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 460 // queries.) 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()); 467} 468 469// These tests are like those in SimpleSingleMatch but more complex, 470// involving URLs that need to be fixed up to match properly. 471TEST_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")); 478 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")); 486 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")); 495 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")); 504 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()); 513 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()); 522 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")); 552} 553 554TEST_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()); 568} 569 570TEST_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")); 578} 579 580TEST_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", 590 base::string16()); 591} 592 593TEST_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", 603 base::string16()); 604} 605 606TEST_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", 616 base::string16()); 617} 618 619TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) { 620 ACMatchClassifications matches = 621 AutocompleteMatch::ClassificationsFromString("0,0"); 622 ClassifyTest classify_test(ASCIIToUTF16("A man, a plan, a canal Panama"), 623 matches); 624 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)); 628 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)); 633 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)); 639 640 ClassifyTest classify_test2(ASCIIToUTF16("Yahoo! Sports - Sports News, " 641 "Scores, Rumors, Fantasy Games, and more"), matches); 642 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)); 647 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)); 652 653 matches = AutocompleteMatch::ClassificationsFromString("0,1"); 654 ClassifyTest classify_test3(ASCIIToUTF16("livescore.goal.com"), matches); 655 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)); 660 661 matches = AutocompleteMatch::ClassificationsFromString("0,0,13,1"); 662 ClassifyTest classify_test4(ASCIIToUTF16("Email login: mail.somecorp.com"), 663 matches); 664 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)); 668 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)); 673 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)); 679 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()); 687 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); 691 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 697 // valid. 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)); 702 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); 707 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)); 712} 713 714TEST_F(ShortcutsProviderTest, CalculateScore) { 715 history::ShortcutsDatabase::Shortcut shortcut( 716 std::string(), ASCIIToUTF16("test"), 717 history::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); 723 724 // Maximal score. 725 const int max_relevance = 726 ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance; 727 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); 728 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); 736 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); 741 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); 746 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); 755 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); 765} 766 767TEST_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}, 785 }; 786 787 size_t original_shortcuts_count = backend_->shortcuts_map().size(); 788 789 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); 790 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"))); 796 797 AutocompleteMatch match( 798 provider_.get(), 1200, true, AutocompleteMatchType::HISTORY_TITLE); 799 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); 803 804 provider_->DeleteMatch(match); 805 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"))); 814 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); 818 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"))); 823} 824 825#if defined(ENABLE_EXTENSIONS) 826TEST_F(ShortcutsProviderTest, Extension) { 827 // Try an input string that matches an extension URL. 828 base::string16 text(ASCIIToUTF16("echo")); 829 std::string expected_url( 830 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo"); 831 ExpectedURLs expected_urls; 832 expected_urls.push_back(ExpectedURLAndAllowedToBeDefault( 833 expected_url, true)); 834 RunTest(text, false, expected_urls, expected_url, ASCIIToUTF16(" echo")); 835 836 // Claim the extension has been unloaded. 837 scoped_refptr<const extensions::Extension> extension = 838 extensions::ExtensionBuilder() 839 .SetManifest(extensions::DictionaryBuilder() 840 .Set("name", "Echo") 841 .Set("version", "1.0")) 842 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee") 843 .Build(); 844 extensions::UnloadedExtensionInfo details( 845 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); 846 content::NotificationService::current()->Notify( 847 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, 848 content::Source<Profile>(&profile_), 849 content::Details<extensions::UnloadedExtensionInfo>(&details)); 850 851 // Now the URL should have disappeared. 852 RunTest(text, false, ExpectedURLs(), std::string(), base::string16()); 853} 854#endif 855