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> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "chrome/browser/autocomplete/autocomplete_input.h" | 20 #include "chrome/browser/autocomplete/autocomplete_input.h" |
21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 21 #include "chrome/browser/autocomplete/autocomplete_match.h" |
22 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 22 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
23 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 23 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
24 #include "chrome/browser/autocomplete/autocomplete_result.h" | 24 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 25 #include "chrome/browser/autocomplete/shortcuts_backend.h" |
| 26 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" |
25 #include "chrome/browser/chrome_notification_types.h" | 27 #include "chrome/browser/chrome_notification_types.h" |
26 #include "chrome/browser/history/history_service.h" | 28 #include "chrome/browser/history/history_service.h" |
27 #include "chrome/browser/history/in_memory_url_index.h" | 29 #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" | 30 #include "chrome/browser/history/url_database.h" |
31 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
32 #include "chrome/test/base/testing_profile.h" | 32 #include "chrome/test/base/testing_profile.h" |
33 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
34 #include "content/public/test/test_browser_thread.h" | 34 #include "content/public/test/test_browser_thread.h" |
35 #include "extensions/common/extension.h" | 35 #include "extensions/common/extension.h" |
36 #include "extensions/common/extension_builder.h" | 36 #include "extensions/common/extension_builder.h" |
37 #include "extensions/common/value_builder.h" | 37 #include "extensions/common/value_builder.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
39 | 39 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 204 } |
205 | 205 |
206 ClassifyTest::~ClassifyTest() { | 206 ClassifyTest::~ClassifyTest() { |
207 } | 207 } |
208 | 208 |
209 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { | 209 ACMatchClassifications ClassifyTest::RunTest(const base::string16& find_text) { |
210 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, | 210 return ShortcutsProvider::ClassifyAllMatchesInString(find_text, |
211 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); | 211 ShortcutsProvider::CreateWordMapForString(find_text), text_, matches_); |
212 } | 212 } |
213 | 213 |
214 namespace history { | |
215 | |
216 | 214 |
217 // ShortcutsProviderTest ------------------------------------------------------ | 215 // ShortcutsProviderTest ------------------------------------------------------ |
218 | 216 |
219 class ShortcutsProviderTest : public testing::Test, | 217 class ShortcutsProviderTest : public testing::Test, |
220 public AutocompleteProviderListener { | 218 public AutocompleteProviderListener { |
221 public: | 219 public: |
222 ShortcutsProviderTest(); | 220 ShortcutsProviderTest(); |
223 | 221 |
224 // AutocompleteProviderListener: | 222 // AutocompleteProviderListener: |
225 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE; | 223 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE; |
(...skipping 30 matching lines...) Expand all Loading... |
256 // match, and it should have inline autocompletion | 254 // match, and it should have inline autocompletion |
257 // |top_result_inline_autocompletion|. | 255 // |top_result_inline_autocompletion|. |
258 void RunTest(const base::string16 text, | 256 void RunTest(const base::string16 text, |
259 bool prevent_inline_autocomplete, | 257 bool prevent_inline_autocomplete, |
260 const ExpectedURLs& expected_urls, | 258 const ExpectedURLs& expected_urls, |
261 std::string expected_top_result, | 259 std::string expected_top_result, |
262 base::string16 top_result_inline_autocompletion); | 260 base::string16 top_result_inline_autocompletion); |
263 | 261 |
264 // Passthrough to the private function in provider_. | 262 // Passthrough to the private function in provider_. |
265 int CalculateScore(const std::string& terms, | 263 int CalculateScore(const std::string& terms, |
266 const ShortcutsBackend::Shortcut& shortcut, | 264 const history::ShortcutsDatabase::Shortcut& shortcut, |
267 int max_relevance); | 265 int max_relevance); |
268 | 266 |
269 base::MessageLoopForUI message_loop_; | 267 base::MessageLoopForUI message_loop_; |
270 content::TestBrowserThread ui_thread_; | 268 content::TestBrowserThread ui_thread_; |
271 content::TestBrowserThread file_thread_; | 269 content::TestBrowserThread file_thread_; |
272 | 270 |
273 TestingProfile profile_; | 271 TestingProfile profile_; |
274 | 272 |
275 ACMatches ac_matches_; // The resulting matches after running RunTest. | 273 ACMatches ac_matches_; // The resulting matches after running RunTest. |
276 | 274 |
(...skipping 23 matching lines...) Expand all Loading... |
300 // and prevent it from being deleted. | 298 // and prevent it from being deleted. |
301 message_loop_.RunUntilIdle(); | 299 message_loop_.RunUntilIdle(); |
302 provider_ = NULL; | 300 provider_ = NULL; |
303 } | 301 } |
304 | 302 |
305 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { | 303 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { |
306 DCHECK(provider_.get()); | 304 DCHECK(provider_.get()); |
307 size_t expected_size = backend_->shortcuts_map().size() + db_size; | 305 size_t expected_size = backend_->shortcuts_map().size() + db_size; |
308 for (size_t i = 0; i < db_size; ++i) { | 306 for (size_t i = 0; i < db_size; ++i) { |
309 const TestShortcutInfo& cur = db[i]; | 307 const TestShortcutInfo& cur = db[i]; |
310 ShortcutsBackend::Shortcut shortcut( | 308 history::ShortcutsDatabase::Shortcut shortcut( |
311 cur.guid, ASCIIToUTF16(cur.text), | 309 cur.guid, ASCIIToUTF16(cur.text), |
312 ShortcutsBackend::Shortcut::MatchCore( | 310 history::ShortcutsDatabase::Shortcut::MatchCore( |
313 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), | 311 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), |
314 ASCIIToUTF16(cur.contents), | 312 ASCIIToUTF16(cur.contents), cur.contents_class, |
315 AutocompleteMatch::ClassificationsFromString(cur.contents_class), | 313 ASCIIToUTF16(cur.description), cur.description_class, |
316 ASCIIToUTF16(cur.description), | |
317 AutocompleteMatch::ClassificationsFromString(cur.description_class), | |
318 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), | 314 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), |
319 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), | 315 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), |
320 cur.number_of_hits); | 316 cur.number_of_hits); |
321 backend_->AddShortcut(shortcut); | 317 backend_->AddShortcut(shortcut); |
322 } | 318 } |
323 EXPECT_EQ(expected_size, backend_->shortcuts_map().size()); | 319 EXPECT_EQ(expected_size, backend_->shortcuts_map().size()); |
324 } | 320 } |
325 | 321 |
326 ShortcutsProviderTest::SetShouldContain::SetShouldContain( | 322 ShortcutsProviderTest::SetShouldContain::SetShouldContain( |
327 const ACMatches& matched_urls) { | 323 const ACMatches& matched_urls) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1, | 367 std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1, |
372 ac_matches_.end(), AutocompleteMatch::MoreRelevant); | 368 ac_matches_.end(), AutocompleteMatch::MoreRelevant); |
373 EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec()); | 369 EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec()); |
374 EXPECT_EQ(top_result_inline_autocompletion, | 370 EXPECT_EQ(top_result_inline_autocompletion, |
375 ac_matches_[0].inline_autocompletion); | 371 ac_matches_[0].inline_autocompletion); |
376 } | 372 } |
377 } | 373 } |
378 | 374 |
379 int ShortcutsProviderTest::CalculateScore( | 375 int ShortcutsProviderTest::CalculateScore( |
380 const std::string& terms, | 376 const std::string& terms, |
381 const ShortcutsBackend::Shortcut& shortcut, | 377 const history::ShortcutsDatabase::Shortcut& shortcut, |
382 int max_relevance) { | 378 int max_relevance) { |
383 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, | 379 return provider_->CalculateScore(ASCIIToUTF16(terms), shortcut, |
384 max_relevance); | 380 max_relevance); |
385 } | 381 } |
386 | 382 |
387 | 383 |
388 // Actual tests --------------------------------------------------------------- | 384 // Actual tests --------------------------------------------------------------- |
389 | 385 |
390 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { | 386 TEST_F(ShortcutsProviderTest, SimpleSingleMatch) { |
391 base::string16 text(ASCIIToUTF16("go")); | 387 base::string16 text(ASCIIToUTF16("go")); |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 matches = AutocompleteMatch::ClassificationsFromString("0,1,11,0"); | 639 matches = AutocompleteMatch::ClassificationsFromString("0,1,11,0"); |
644 ClassifyTest classify_test7(ASCIIToUTF16("http://a.co is great"), matches); | 640 ClassifyTest classify_test7(ASCIIToUTF16("http://a.co is great"), matches); |
645 | 641 |
646 ACMatchClassifications spans_l = | 642 ACMatchClassifications spans_l = |
647 classify_test7.RunTest(ASCIIToUTF16("ht co")); | 643 classify_test7.RunTest(ASCIIToUTF16("ht co")); |
648 EXPECT_EQ("0,3,2,1,9,3,11,0", | 644 EXPECT_EQ("0,3,2,1,9,3,11,0", |
649 AutocompleteMatch::ClassificationsToString(spans_l)); | 645 AutocompleteMatch::ClassificationsToString(spans_l)); |
650 } | 646 } |
651 | 647 |
652 TEST_F(ShortcutsProviderTest, CalculateScore) { | 648 TEST_F(ShortcutsProviderTest, CalculateScore) { |
653 ShortcutsBackend::Shortcut shortcut( | 649 history::ShortcutsDatabase::Shortcut shortcut( |
654 std::string(), ASCIIToUTF16("test"), | 650 std::string(), ASCIIToUTF16("test"), |
655 ShortcutsBackend::Shortcut::MatchCore( | 651 history::ShortcutsDatabase::Shortcut::MatchCore( |
656 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), | 652 ASCIIToUTF16("www.test.com"), GURL("http://www.test.com"), |
657 ASCIIToUTF16("www.test.com"), | 653 ASCIIToUTF16("www.test.com"), "0,1,4,3,8,1", |
658 AutocompleteMatch::ClassificationsFromString("0,1,4,3,8,1"), | 654 ASCIIToUTF16("A test"), "0,0,2,2", content::PAGE_TRANSITION_TYPED, |
659 ASCIIToUTF16("A test"), | 655 AutocompleteMatchType::HISTORY_URL, base::string16()), |
660 AutocompleteMatch::ClassificationsFromString("0,0,2,2"), | |
661 content::PAGE_TRANSITION_TYPED, AutocompleteMatchType::HISTORY_URL, | |
662 base::string16()), | |
663 base::Time::Now(), 1); | 656 base::Time::Now(), 1); |
664 | 657 |
665 // Maximal score. | 658 // Maximal score. |
666 const int max_relevance = AutocompleteResult::kLowestDefaultScore - 1; | 659 const int max_relevance = AutocompleteResult::kLowestDefaultScore - 1; |
667 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); | 660 const int kMaxScore = CalculateScore("test", shortcut, max_relevance); |
668 | 661 |
669 // Score decreases as percent of the match is decreased. | 662 // Score decreases as percent of the match is decreased. |
670 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); | 663 int score_three_quarters = CalculateScore("tes", shortcut, max_relevance); |
671 EXPECT_LT(score_three_quarters, kMaxScore); | 664 EXPECT_LT(score_three_quarters, kMaxScore); |
672 int score_one_half = CalculateScore("te", shortcut, max_relevance); | 665 int score_one_half = CalculateScore("te", shortcut, max_relevance); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
783 extensions::UnloadedExtensionInfo details( | 776 extensions::UnloadedExtensionInfo details( |
784 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); | 777 extension.get(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL); |
785 content::NotificationService::current()->Notify( | 778 content::NotificationService::current()->Notify( |
786 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 779 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
787 content::Source<Profile>(&profile_), | 780 content::Source<Profile>(&profile_), |
788 content::Details<extensions::UnloadedExtensionInfo>(&details)); | 781 content::Details<extensions::UnloadedExtensionInfo>(&details)); |
789 | 782 |
790 // Now the URL should have disappeared. | 783 // Now the URL should have disappeared. |
791 RunTest(text, false, ExpectedURLs(), std::string(), base::string16()); | 784 RunTest(text, false, ExpectedURLs(), std::string(), base::string16()); |
792 } | 785 } |
793 | |
794 } // namespace history | |
OLD | NEW |