OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 5 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 148 |
149 } // namespace | 149 } // namespace |
150 | 150 |
151 | 151 |
152 // ClassifyTest --------------------------------------------------------------- | 152 // ClassifyTest --------------------------------------------------------------- |
153 | 153 |
154 // Helper class to make running tests of ClassifyAllMatchesInString() more | 154 // Helper class to make running tests of ClassifyAllMatchesInString() more |
155 // convenient. | 155 // convenient. |
156 class ClassifyTest { | 156 class ClassifyTest { |
157 public: | 157 public: |
158 ClassifyTest(const string16& text, ACMatchClassifications matches); | 158 ClassifyTest(const base::string16& text, ACMatchClassifications matches); |
159 ~ClassifyTest(); | 159 ~ClassifyTest(); |
160 | 160 |
161 ACMatchClassifications RunTest(const string16& find_text); | 161 ACMatchClassifications RunTest(const base::string16& find_text); |
162 | 162 |
163 private: | 163 private: |
164 const string16 text_; | 164 const base::string16 text_; |
165 const ACMatchClassifications matches_; | 165 const ACMatchClassifications matches_; |
166 }; | 166 }; |
167 | 167 |
168 ClassifyTest::ClassifyTest(const string16& text, ACMatchClassifications matches) | 168 ClassifyTest::ClassifyTest(const base::string16& text, ACMatchClassifications ma
tches) |
169 : text_(text), | 169 : text_(text), |
170 matches_(matches) { | 170 matches_(matches) { |
171 } | 171 } |
172 | 172 |
173 ClassifyTest::~ClassifyTest() { | 173 ClassifyTest::~ClassifyTest() { |
174 } | 174 } |
175 | 175 |
176 ACMatchClassifications ClassifyTest::RunTest(const string16& find_text) { | 176 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { |
177 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, | 177 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, |
178 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); | 178 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); |
179 } | 179 } |
180 | 180 |
181 namespace history { | 181 namespace history { |
182 | 182 |
183 | 183 |
184 // ShortcutsProviderTest ------------------------------------------------------ | 184 // ShortcutsProviderTest ------------------------------------------------------ |
185 | 185 |
186 class ShortcutsProviderTest : public testing::Test, | 186 class ShortcutsProviderTest : public testing::Test, |
(...skipping 21 matching lines...) Expand all Loading... |
208 | 208 |
209 virtual void SetUp(); | 209 virtual void SetUp(); |
210 virtual void TearDown(); | 210 virtual void TearDown(); |
211 | 211 |
212 // Fills test data into the provider. | 212 // Fills test data into the provider. |
213 void FillData(TestShortcutInfo* db, size_t db_size); | 213 void FillData(TestShortcutInfo* db, size_t db_size); |
214 | 214 |
215 // Runs an autocomplete query on |text| and checks to see that the returned | 215 // Runs an autocomplete query on |text| and checks to see that the returned |
216 // results' destination URLs match those provided. |expected_urls| does not | 216 // results' destination URLs match those provided. |expected_urls| does not |
217 // need to be in sorted order. | 217 // need to be in sorted order. |
218 void RunTest(const string16 text, | 218 void RunTest(const base::string16 text, |
219 const URLs& expected_urls, | 219 const URLs& expected_urls, |
220 std::string expected_top_result); | 220 std::string expected_top_result); |
221 | 221 |
222 // Passthrough to the private function in provider_. | 222 // Passthrough to the private function in provider_. |
223 int CalculateScore(const std::string& terms, | 223 int CalculateScore(const std::string& terms, |
224 const ShortcutsBackend::Shortcut& shortcut, | 224 const ShortcutsBackend::Shortcut& shortcut, |
225 int max_relevance); | 225 int max_relevance); |
226 | 226 |
227 base::MessageLoopForUI message_loop_; | 227 base::MessageLoopForUI message_loop_; |
228 content::TestBrowserThread ui_thread_; | 228 content::TestBrowserThread ui_thread_; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 for (ACMatches::const_iterator iter = matched_urls.begin(); | 286 for (ACMatches::const_iterator iter = matched_urls.begin(); |
287 iter != matched_urls.end(); ++iter) | 287 iter != matched_urls.end(); ++iter) |
288 matches_.insert(iter->destination_url.spec()); | 288 matches_.insert(iter->destination_url.spec()); |
289 } | 289 } |
290 | 290 |
291 void ShortcutsProviderTest::SetShouldContain::operator()( | 291 void ShortcutsProviderTest::SetShouldContain::operator()( |
292 const std::string& expected) { | 292 const std::string& expected) { |
293 EXPECT_EQ(1U, matches_.erase(expected)); | 293 EXPECT_EQ(1U, matches_.erase(expected)); |
294 } | 294 } |
295 | 295 |
296 void ShortcutsProviderTest::RunTest(const string16 text, | 296 void ShortcutsProviderTest::RunTest(const base::string16 text, |
297 const URLs& expected_urls, | 297 const URLs& expected_urls, |
298 std::string expected_top_result) { | 298 std::string expected_top_result) { |
299 base::MessageLoop::current()->RunUntilIdle(); | 299 base::MessageLoop::current()->RunUntilIdle(); |
300 AutocompleteInput input(text, string16::npos, string16(), GURL(), | 300 AutocompleteInput input(text, base::string16::npos, base::string16(), GURL(), |
301 AutocompleteInput::INVALID_SPEC, false, false, true, | 301 AutocompleteInput::INVALID_SPEC, false, false, true, |
302 AutocompleteInput::ALL_MATCHES); | 302 AutocompleteInput::ALL_MATCHES); |
303 provider_->Start(input, false); | 303 provider_->Start(input, false); |
304 EXPECT_TRUE(provider_->done()); | 304 EXPECT_TRUE(provider_->done()); |
305 | 305 |
306 ac_matches_ = provider_->matches(); | 306 ac_matches_ = provider_->matches(); |
307 | 307 |
308 // We should have gotten back at most AutocompleteProvider::kMaxMatches. | 308 // We should have gotten back at most AutocompleteProvider::kMaxMatches. |
309 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); | 309 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); |
310 | 310 |
(...skipping 27 matching lines...) Expand all Loading... |
338 const ShortcutsBackend::Shortcut& shortcut, | 338 const ShortcutsBackend::Shortcut& shortcut, |
339 int max_relevance) { | 339 int max_relevance) { |
340 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, | 340 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, |
341 max_relevance); | 341 max_relevance); |
342 } | 342 } |
343 | 343 |
344 | 344 |
345 // Actual tests --------------------------------------------------------------- | 345 // Actual tests --------------------------------------------------------------- |
346 | 346 |
347 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { | 347 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { |
348 string16 text(ASCIIToUTF16("go")); | 348 base::string16 text(ASCIIToUTF16("go")); |
349 std::string expected_url("http://www.google.com/"); | 349 std::string expected_url("http://www.google.com/"); |
350 URLs expected_urls; | 350 URLs expected_urls; |
351 expected_urls.push_back(expected_url); | 351 expected_urls.push_back(expected_url); |
352 RunTest(text, expected_urls, expected_url); | 352 RunTest(text, expected_urls, expected_url); |
353 } | 353 } |
354 | 354 |
355 TEST_F(ShortcutsProviderTest, MultiMatch) { | 355 TEST_F(ShortcutsProviderTest, MultiMatch) { |
356 string16 text(ASCIIToUTF16("NEWS")); | 356 base::string16 text(ASCIIToUTF16("NEWS")); |
357 URLs expected_urls; | 357 URLs expected_urls; |
358 // Scores high because of completion length. | 358 // Scores high because of completion length. |
359 expected_urls.push_back("http://slashdot.org/"); | 359 expected_urls.push_back("http://slashdot.org/"); |
360 // Scores high because of visit count. | 360 // Scores high because of visit count. |
361 expected_urls.push_back("http://sports.yahoo.com/"); | 361 expected_urls.push_back("http://sports.yahoo.com/"); |
362 // Scores high because of visit count but less match span, | 362 // Scores high because of visit count but less match span, |
363 // which is more important. | 363 // which is more important. |
364 expected_urls.push_back("http://www.cnn.com/index.html"); | 364 expected_urls.push_back("http://www.cnn.com/index.html"); |
365 RunTest(text, expected_urls, "http://slashdot.org/"); | 365 RunTest(text, expected_urls, "http://slashdot.org/"); |
366 } | 366 } |
367 | 367 |
368 TEST_F(ShortcutsProviderTest, TypedCountMatches) { | 368 TEST_F(ShortcutsProviderTest, TypedCountMatches) { |
369 string16 text(ASCIIToUTF16("just")); | 369 base::string16 text(ASCIIToUTF16("just")); |
370 URLs expected_urls; | 370 URLs expected_urls; |
371 expected_urls.push_back("http://www.testsite.com/b.html"); | 371 expected_urls.push_back("http://www.testsite.com/b.html"); |
372 expected_urls.push_back("http://www.testsite.com/a.html"); | 372 expected_urls.push_back("http://www.testsite.com/a.html"); |
373 expected_urls.push_back("http://www.testsite.com/c.html"); | 373 expected_urls.push_back("http://www.testsite.com/c.html"); |
374 RunTest(text, expected_urls, "http://www.testsite.com/b.html"); | 374 RunTest(text, expected_urls, "http://www.testsite.com/b.html"); |
375 } | 375 } |
376 | 376 |
377 TEST_F(ShortcutsProviderTest, FragmentLengthMatches) { | 377 TEST_F(ShortcutsProviderTest, FragmentLengthMatches) { |
378 string16 text(ASCIIToUTF16("just a")); | 378 base::string16 text(ASCIIToUTF16("just a")); |
379 URLs expected_urls; | 379 URLs expected_urls; |
380 expected_urls.push_back("http://www.testsite.com/d.html"); | 380 expected_urls.push_back("http://www.testsite.com/d.html"); |
381 expected_urls.push_back("http://www.testsite.com/e.html"); | 381 expected_urls.push_back("http://www.testsite.com/e.html"); |
382 expected_urls.push_back("http://www.testsite.com/f.html"); | 382 expected_urls.push_back("http://www.testsite.com/f.html"); |
383 RunTest(text, expected_urls, "http://www.testsite.com/d.html"); | 383 RunTest(text, expected_urls, "http://www.testsite.com/d.html"); |
384 } | 384 } |
385 | 385 |
386 TEST_F(ShortcutsProviderTest, DaysAgoMatches) { | 386 TEST_F(ShortcutsProviderTest, DaysAgoMatches) { |
387 string16 text(ASCIIToUTF16("ago")); | 387 base::string16 text(ASCIIToUTF16("ago")); |
388 URLs expected_urls; | 388 URLs expected_urls; |
389 expected_urls.push_back("http://www.daysagotest.com/a.html"); | 389 expected_urls.push_back("http://www.daysagotest.com/a.html"); |
390 expected_urls.push_back("http://www.daysagotest.com/b.html"); | 390 expected_urls.push_back("http://www.daysagotest.com/b.html"); |
391 expected_urls.push_back("http://www.daysagotest.com/c.html"); | 391 expected_urls.push_back("http://www.daysagotest.com/c.html"); |
392 RunTest(text, expected_urls, "http://www.daysagotest.com/a.html"); | 392 RunTest(text, expected_urls, "http://www.daysagotest.com/a.html"); |
393 } | 393 } |
394 | 394 |
395 TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) { | 395 TEST_F(ShortcutsProviderTest, ClassifyAllMatchesInString) { |
396 ACMatchClassifications matches = | 396 ACMatchClassifications matches = |
397 AutocompleteMatch::ClassificationsFromString("0,0"); | 397 AutocompleteMatch::ClassificationsFromString("0,0"); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 TEST_F(ShortcutsProviderTest, CalculateScore) { | 489 TEST_F(ShortcutsProviderTest, CalculateScore) { |
490 ShortcutsBackend::Shortcut shortcut( | 490 ShortcutsBackend::Shortcut shortcut( |
491 std::string(), ASCIIToUTF16("test"), | 491 std::string(), ASCIIToUTF16("test"), |
492 ShortcutsBackend::Shortcut::MatchCore( | 492 ShortcutsBackend::Shortcut::MatchCore( |
493 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), | 493 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), |
494 ASCIIToUTF16("www.test.com"), | 494 ASCIIToUTF16("www.test.com"), |
495 AutocompleteMatch::ClassificationsFromString("0,1,4,3,8,1"), | 495 AutocompleteMatch::ClassificationsFromString("0,1,4,3,8,1"), |
496 ASCIIToUTF16("A test"), | 496 ASCIIToUTF16("A test"), |
497 AutocompleteMatch::ClassificationsFromString("0,0,2,2"), | 497 AutocompleteMatch::ClassificationsFromString("0,0,2,2"), |
498 content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, | 498 content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, |
499 string16()), | 499 base::string16()), |
500 base::Time::Now(), 1); | 500 base::Time::Now(), 1); |
501 | 501 |
502 // Maximal score. | 502 // Maximal score. |
503 const int max_relevance = AutocompleteResult::kLowestDefaultScore - 1; | 503 const int max_relevance = AutocompleteResult::kLowestDefaultScore - 1; |
504 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); | 504 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); |
505 | 505 |
506 // Score decreases as percent of the match is decreased. | 506 // Score decreases as percent of the match is decreased. |
507 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); | 507 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); |
508 EXPECT_LT(score_three_quarters, kMaxScore); | 508 EXPECT_LT(score_three_quarters, kMaxScore); |
509 int score_one_half = CalculateScore("te", shortcut, max_relevance); | 509 int score_one_half = CalculateScore("te", shortcut, max_relevance); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); | 594 match.description = ASCIIToUTF16(shortcuts_to_test_delete[3].description); |
595 | 595 |
596 provider_->DeleteMatch(match); | 596 provider_->DeleteMatch(match); |
597 EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size()); | 597 EXPECT_EQ(original_shortcuts_count + 1, backend_->shortcuts_map().size()); |
598 EXPECT_TRUE(backend_->shortcuts_map().end() == | 598 EXPECT_TRUE(backend_->shortcuts_map().end() == |
599 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); | 599 backend_->shortcuts_map().find(ASCIIToUTF16("delete"))); |
600 } | 600 } |
601 | 601 |
602 TEST_F(ShortcutsProviderTest, Extension) { | 602 TEST_F(ShortcutsProviderTest, Extension) { |
603 // Try an input string that matches an extension URL. | 603 // Try an input string that matches an extension URL. |
604 string16 text(ASCIIToUTF16("echo")); | 604 base::string16 text(ASCIIToUTF16("echo")); |
605 std::string expected_url( | 605 std::string expected_url( |
606 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo"); | 606 "chrome-extension://cedabbhfglmiikkmdgcpjdkocfcmbkee/?q=echo"); |
607 URLs expected_urls; | 607 URLs expected_urls; |
608 expected_urls.push_back(expected_url); | 608 expected_urls.push_back(expected_url); |
609 RunTest(text, expected_urls, expected_url); | 609 RunTest(text, expected_urls, expected_url); |
610 | 610 |
611 // Claim the extension has been unloaded. | 611 // Claim the extension has been unloaded. |
612 scoped_refptr<const extensions::Extension> extension = | 612 scoped_refptr<const extensions::Extension> extension = |
613 extensions::ExtensionBuilder() | 613 extensions::ExtensionBuilder() |
614 .SetManifest(extensions::DictionaryBuilder() | 614 .SetManifest(extensions::DictionaryBuilder() |
615 .Set("name", "Echo") | 615 .Set("name", "Echo") |
616 .Set("version", "1.0")) | 616 .Set("version", "1.0")) |
617 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee") | 617 .SetID("cedabbhfglmiikkmdgcpjdkocfcmbkee") |
618 .Build(); | 618 .Build(); |
619 extensions::UnloadedExtensionInfo details( | 619 extensions::UnloadedExtensionInfo details( |
620 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); | 620 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); |
621 content::NotificationService::current()->Notify( | 621 content::NotificationService::current()->Notify( |
622 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 622 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
623 content::Source<Profile>(&profile_), | 623 content::Source<Profile>(&profile_), |
624 content::Details<extensions::UnloadedExtensionInfo>(&details)); | 624 content::Details<extensions::UnloadedExtensionInfo>(&details)); |
625 | 625 |
626 // Now the URL should have disappeared. | 626 // Now the URL should have disappeared. |
627 RunTest(text, URLs(), std::string()); | 627 RunTest(text, URLs(), std::string()); |
628 } | 628 } |
629 | 629 |
630 } // namespace history | 630 } // namespace history |
OLD | NEW |