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 10 matching lines...) Expand all Loading... |
21 #include "chrome/browser/history/history.h" | 21 #include "chrome/browser/history/history.h" |
22 #include "chrome/browser/history/in_memory_url_index.h" | 22 #include "chrome/browser/history/in_memory_url_index.h" |
23 #include "chrome/browser/history/shortcuts_backend.h" | 23 #include "chrome/browser/history/shortcuts_backend.h" |
24 #include "chrome/browser/history/url_database.h" | 24 #include "chrome/browser/history/url_database.h" |
25 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
28 #include "content/test/test_browser_thread.h" | 28 #include "content/test/test_browser_thread.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 | 30 |
31 using base::Time; | |
32 using base::TimeDelta; | |
33 | |
34 using content::BrowserThread; | 31 using content::BrowserThread; |
35 | 32 |
36 namespace { | 33 namespace { |
37 | 34 |
38 struct TestShortcutInfo { | 35 struct TestShortcutInfo { |
39 std::string guid; | 36 std::string guid; |
40 std::string url; | 37 std::string url; |
41 std::string title; // The text that orginally was searched for. | 38 std::string title; // The text that orginally was searched for. |
42 std::string contents; | 39 std::string contents; |
43 std::string contents_class; | 40 std::string contents_class; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 std::set<std::string> Leftovers() const { return matches_; } | 135 std::set<std::string> Leftovers() const { return matches_; } |
139 | 136 |
140 private: | 137 private: |
141 std::set<std::string> matches_; | 138 std::set<std::string> matches_; |
142 }; | 139 }; |
143 | 140 |
144 void SetUp(); | 141 void SetUp(); |
145 void TearDown(); | 142 void TearDown(); |
146 | 143 |
147 // Fills test data into the provider. | 144 // Fills test data into the provider. |
148 void FillData(); | 145 void FillData(TestShortcutInfo* db, size_t db_size); |
149 | 146 |
150 // Runs an autocomplete query on |text| and checks to see that the returned | 147 // Runs an autocomplete query on |text| and checks to see that the returned |
151 // results' destination URLs match those provided. |expected_urls| does not | 148 // results' destination URLs match those provided. |expected_urls| does not |
152 // need to be in sorted order. | 149 // need to be in sorted order. |
153 void RunTest(const string16 text, | 150 void RunTest(const string16 text, |
154 std::vector<std::string> expected_urls, | 151 std::vector<std::string> expected_urls, |
155 std::string expected_top_result); | 152 std::string expected_top_result); |
156 | 153 |
157 MessageLoopForUI message_loop_; | 154 MessageLoopForUI message_loop_; |
158 content::TestBrowserThread ui_thread_; | 155 content::TestBrowserThread ui_thread_; |
(...skipping 15 matching lines...) Expand all Loading... |
174 void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches) { | 171 void ShortcutsProviderTest::OnProviderUpdate(bool updated_matches) { |
175 } | 172 } |
176 | 173 |
177 void ShortcutsProviderTest::SetUp() { | 174 void ShortcutsProviderTest::SetUp() { |
178 profile_.reset(new TestingProfile()); | 175 profile_.reset(new TestingProfile()); |
179 profile_->CreateHistoryService(true, false); | 176 profile_->CreateHistoryService(true, false); |
180 provider_ = new ShortcutsProvider(this, profile_.get()); | 177 provider_ = new ShortcutsProvider(this, profile_.get()); |
181 mock_backend_ = new history::ShortcutsBackend(FilePath(), profile_.get()); | 178 mock_backend_ = new history::ShortcutsBackend(FilePath(), profile_.get()); |
182 mock_backend_->Init(); | 179 mock_backend_->Init(); |
183 provider_->set_shortcuts_backend(mock_backend_.get()); | 180 provider_->set_shortcuts_backend(mock_backend_.get()); |
184 FillData(); | 181 FillData(shortcut_test_db, arraysize(shortcut_test_db)); |
185 } | 182 } |
186 | 183 |
187 void ShortcutsProviderTest::TearDown() { | 184 void ShortcutsProviderTest::TearDown() { |
188 provider_ = NULL; | 185 provider_ = NULL; |
189 } | 186 } |
190 | 187 |
191 void ShortcutsProviderTest::FillData() { | 188 void ShortcutsProviderTest::FillData(TestShortcutInfo* db, size_t db_size) { |
192 DCHECK(provider_.get()); | 189 DCHECK(provider_.get()); |
193 mock_backend_->DeleteAllShortcuts(); | 190 for (size_t i = 0; i < db_size; ++i) { |
194 for (size_t i = 0; i < arraysize(shortcut_test_db); ++i) { | 191 const TestShortcutInfo& cur = db[i]; |
195 const TestShortcutInfo& cur = shortcut_test_db[i]; | 192 history::ShortcutsBackend::Shortcut shortcut(cur.guid, |
196 const GURL current_url(cur.url); | 193 ASCIIToUTF16(cur.title), GURL(cur.url), ASCIIToUTF16(cur.contents), |
197 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | 194 AutocompleteMatch::ClassificationsFromString(cur.contents_class), |
198 shortcuts_provider::Shortcut shortcut( | |
199 ASCIIToUTF16(cur.title), | |
200 current_url, | |
201 ASCIIToUTF16(cur.contents), | |
202 shortcuts_provider::SpansFromString(ASCIIToUTF16(cur.contents_class)), | |
203 ASCIIToUTF16(cur.description), | 195 ASCIIToUTF16(cur.description), |
204 shortcuts_provider::SpansFromString( | 196 AutocompleteMatch::ClassificationsFromString(cur.description_class), |
205 ASCIIToUTF16(cur.description_class))); | 197 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), |
206 shortcut.last_access_time = visit_time; | 198 cur.typed_count); |
207 shortcut.number_of_hits = cur.typed_count; | |
208 shortcut.id = cur.guid; | |
209 mock_backend_->AddShortcut(shortcut); | 199 mock_backend_->AddShortcut(shortcut); |
210 } | 200 } |
211 } | 201 } |
212 | 202 |
213 ShortcutsProviderTest::SetShouldContain::SetShouldContain( | 203 ShortcutsProviderTest::SetShouldContain::SetShouldContain( |
214 const ACMatches& matched_urls) { | 204 const ACMatches& matched_urls) { |
215 for (ACMatches::const_iterator iter = matched_urls.begin(); | 205 for (ACMatches::const_iterator iter = matched_urls.begin(); |
216 iter != matched_urls.end(); ++iter) | 206 iter != matched_urls.end(); ++iter) |
217 matches_.insert(iter->destination_url.spec()); | 207 matches_.insert(iter->destination_url.spec()); |
218 } | 208 } |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 ACMatchClassifications spans_content; | 548 ACMatchClassifications spans_content; |
559 spans_content.push_back(ACMatchClassification(0, ACMatchClassification::URL)); | 549 spans_content.push_back(ACMatchClassification(0, ACMatchClassification::URL)); |
560 spans_content.push_back(ACMatchClassification( | 550 spans_content.push_back(ACMatchClassification( |
561 4, ACMatchClassification::MATCH | ACMatchClassification::URL)); | 551 4, ACMatchClassification::MATCH | ACMatchClassification::URL)); |
562 spans_content.push_back(ACMatchClassification(8, ACMatchClassification::URL)); | 552 spans_content.push_back(ACMatchClassification(8, ACMatchClassification::URL)); |
563 ACMatchClassifications spans_description; | 553 ACMatchClassifications spans_description; |
564 spans_description.push_back( | 554 spans_description.push_back( |
565 ACMatchClassification(0, ACMatchClassification::NONE)); | 555 ACMatchClassification(0, ACMatchClassification::NONE)); |
566 spans_description.push_back( | 556 spans_description.push_back( |
567 ACMatchClassification(2, ACMatchClassification::MATCH)); | 557 ACMatchClassification(2, ACMatchClassification::MATCH)); |
568 shortcuts_provider::Shortcut shortcut(ASCIIToUTF16("test"), | 558 history::ShortcutsBackend::Shortcut shortcut(std::string(), |
569 GURL("http://www.test.com"), | 559 ASCIIToUTF16("test"), GURL("http://www.test.com"), |
570 ASCIIToUTF16("www.test.com"), | 560 ASCIIToUTF16("www.test.com"), spans_content, ASCIIToUTF16("A test"), |
571 spans_content, | 561 spans_description, base::Time::Now(), 1); |
572 ASCIIToUTF16("A test"), | |
573 spans_description); | |
574 | 562 |
575 // Maximal score. | 563 // Maximal score. |
576 shortcut.last_access_time = Time::Now(); | |
577 const int kMaxScore = ShortcutsProvider::CalculateScore( | 564 const int kMaxScore = ShortcutsProvider::CalculateScore( |
578 ASCIIToUTF16("test"), shortcut); | 565 ASCIIToUTF16("test"), shortcut); |
579 | 566 |
580 // Score decreases as percent of the match is decreased. | 567 // Score decreases as percent of the match is decreased. |
581 int score_three_quarters = | 568 int score_three_quarters = |
582 ShortcutsProvider::CalculateScore(ASCIIToUTF16("tes"), shortcut); | 569 ShortcutsProvider::CalculateScore(ASCIIToUTF16("tes"), shortcut); |
583 EXPECT_LT(score_three_quarters, kMaxScore); | 570 EXPECT_LT(score_three_quarters, kMaxScore); |
584 int score_one_half = | 571 int score_one_half = |
585 ShortcutsProvider::CalculateScore(ASCIIToUTF16("te"), shortcut); | 572 ShortcutsProvider::CalculateScore(ASCIIToUTF16("te"), shortcut); |
586 EXPECT_LT(score_one_half, score_three_quarters); | 573 EXPECT_LT(score_one_half, score_three_quarters); |
587 int score_one_quarter = | 574 int score_one_quarter = |
588 ShortcutsProvider::CalculateScore(ASCIIToUTF16("t"), shortcut); | 575 ShortcutsProvider::CalculateScore(ASCIIToUTF16("t"), shortcut); |
589 EXPECT_LT(score_one_quarter, score_one_half); | 576 EXPECT_LT(score_one_quarter, score_one_half); |
590 | 577 |
591 // Should decay with time - one week. | 578 // Should decay with time - one week. |
592 shortcut.last_access_time = Time::Now() - TimeDelta::FromDays(7); | 579 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(7); |
593 int score_week_old = | 580 int score_week_old = |
594 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); | 581 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); |
595 EXPECT_LT(score_week_old, kMaxScore); | 582 EXPECT_LT(score_week_old, kMaxScore); |
596 | 583 |
597 // Should decay more in two weeks. | 584 // Should decay more in two weeks. |
598 shortcut.last_access_time = Time::Now() - TimeDelta::FromDays(14); | 585 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14); |
599 int score_two_weeks_old = | 586 int score_two_weeks_old = |
600 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); | 587 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); |
601 EXPECT_LT(score_two_weeks_old, score_week_old); | 588 EXPECT_LT(score_two_weeks_old, score_week_old); |
602 | 589 |
603 // But not if it was activly clicked on. 2 hits slow decaying power. | 590 // But not if it was activly clicked on. 2 hits slow decaying power. |
604 shortcut.number_of_hits = 2; | 591 shortcut.number_of_hits = 2; |
605 shortcut.last_access_time = Time::Now() - TimeDelta::FromDays(14); | 592 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14); |
606 int score_popular_two_weeks_old = | 593 int score_popular_two_weeks_old = |
607 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); | 594 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); |
608 EXPECT_LT(score_two_weeks_old, score_popular_two_weeks_old); | 595 EXPECT_LT(score_two_weeks_old, score_popular_two_weeks_old); |
609 // But still decayed. | 596 // But still decayed. |
610 EXPECT_LT(score_popular_two_weeks_old, kMaxScore); | 597 EXPECT_LT(score_popular_two_weeks_old, kMaxScore); |
611 | 598 |
612 // 3 hits slow decaying power even more. | 599 // 3 hits slow decaying power even more. |
613 shortcut.number_of_hits = 3; | 600 shortcut.number_of_hits = 3; |
614 shortcut.last_access_time = Time::Now() - TimeDelta::FromDays(14); | 601 shortcut.last_access_time = base::Time::Now() - base::TimeDelta::FromDays(14); |
615 int score_more_popular_two_weeks_old = | 602 int score_more_popular_two_weeks_old = |
616 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); | 603 ShortcutsProvider::CalculateScore(ASCIIToUTF16("test"), shortcut); |
617 EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old); | 604 EXPECT_LT(score_two_weeks_old, score_more_popular_two_weeks_old); |
618 EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old); | 605 EXPECT_LT(score_popular_two_weeks_old, score_more_popular_two_weeks_old); |
619 // But still decayed. | 606 // But still decayed. |
620 EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore); | 607 EXPECT_LT(score_more_popular_two_weeks_old, kMaxScore); |
621 } | 608 } |
622 | 609 |
623 TEST_F(ShortcutsProviderTest, DeleteMatch) { | 610 TEST_F(ShortcutsProviderTest, DeleteMatch) { |
624 TestShortcutInfo shortcuts_to_test_delete[3] = { | 611 TestShortcutInfo shortcuts_to_test_delete[3] = { |
625 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", | 612 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F1", |
626 "http://www.deletetest.com/1.html", "delete", | 613 "http://www.deletetest.com/1.html", "delete", |
627 "http://www.deletetest.com/1.html", "0,2", | 614 "http://www.deletetest.com/1.html", "0,2", |
628 "Erase this shortcut!", "0,0", 1, 1}, | 615 "Erase this shortcut!", "0,0", 1, 1}, |
629 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", | 616 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F2", |
630 "http://www.deletetest.com/1.html", "erase", | 617 "http://www.deletetest.com/1.html", "erase", |
631 "http://www.deletetest.com/1.html", "0,2", | 618 "http://www.deletetest.com/1.html", "0,2", |
632 "Erase this shortcut!", "0,0", 1, 1}, | 619 "Erase this shortcut!", "0,0", 1, 1}, |
633 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", | 620 { "BD85DBA2-8C29-49F9-84AE-48E1E90880F3", |
634 "http://www.deletetest.com/2.html", "delete", | 621 "http://www.deletetest.com/2.html", "delete", |
635 "http://www.deletetest.com/2.html", "0,2", | 622 "http://www.deletetest.com/2.html", "0,2", |
636 "Erase this shortcut!", "0,0", 1, 1}, | 623 "Erase this shortcut!", "0,0", 1, 1}, |
637 }; | 624 }; |
638 | 625 |
639 size_t original_shortcuts_count = | 626 size_t original_shortcuts_count = |
640 provider_->shortcuts_backend_->shortcuts_map().size(); | 627 provider_->shortcuts_backend_->shortcuts_map().size(); |
641 | 628 |
642 for (size_t i = 0; i < arraysize(shortcuts_to_test_delete); ++i) { | 629 FillData(shortcuts_to_test_delete, arraysize(shortcuts_to_test_delete)); |
643 const TestShortcutInfo& cur = shortcuts_to_test_delete[i]; | |
644 const GURL current_url(cur.url); | |
645 Time visit_time = Time::Now() - TimeDelta::FromDays(cur.days_from_now); | |
646 shortcuts_provider::Shortcut shortcut( | |
647 ASCIIToUTF16(cur.title), | |
648 current_url, | |
649 ASCIIToUTF16(cur.contents), | |
650 shortcuts_provider::SpansFromString(ASCIIToUTF16(cur.contents_class)), | |
651 ASCIIToUTF16(cur.description), | |
652 shortcuts_provider::SpansFromString( | |
653 ASCIIToUTF16(cur.description_class))); | |
654 shortcut.last_access_time = visit_time; | |
655 shortcut.id = cur.guid; | |
656 mock_backend_->AddShortcut(shortcut); | |
657 } | |
658 | 630 |
659 EXPECT_EQ(original_shortcuts_count + 3, | 631 EXPECT_EQ(original_shortcuts_count + 3, |
660 provider_->shortcuts_backend_->shortcuts_map().size()); | 632 provider_->shortcuts_backend_->shortcuts_map().size()); |
661 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == | 633 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == |
662 provider_->shortcuts_backend_->shortcuts_map().find( | 634 provider_->shortcuts_backend_->shortcuts_map().find( |
663 ASCIIToUTF16("delete"))); | 635 ASCIIToUTF16("delete"))); |
664 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == | 636 EXPECT_FALSE(provider_->shortcuts_backend_->shortcuts_map().end() == |
665 provider_->shortcuts_backend_->shortcuts_map().find( | 637 provider_->shortcuts_backend_->shortcuts_map().find( |
666 ASCIIToUTF16("erase"))); | 638 ASCIIToUTF16("erase"))); |
667 | 639 |
(...skipping 21 matching lines...) Expand all Loading... |
689 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[2].contents); | 661 match.contents = ASCIIToUTF16(shortcuts_to_test_delete[2].contents); |
690 match.description = ASCIIToUTF16(shortcuts_to_test_delete[2].description); | 662 match.description = ASCIIToUTF16(shortcuts_to_test_delete[2].description); |
691 | 663 |
692 provider_->DeleteMatch(match); | 664 provider_->DeleteMatch(match); |
693 EXPECT_EQ(original_shortcuts_count, | 665 EXPECT_EQ(original_shortcuts_count, |
694 provider_->shortcuts_backend_->shortcuts_map().size()); | 666 provider_->shortcuts_backend_->shortcuts_map().size()); |
695 EXPECT_TRUE(provider_->shortcuts_backend_->shortcuts_map().end() == | 667 EXPECT_TRUE(provider_->shortcuts_backend_->shortcuts_map().end() == |
696 provider_->shortcuts_backend_->shortcuts_map().find( | 668 provider_->shortcuts_backend_->shortcuts_map().find( |
697 ASCIIToUTF16("delete"))); | 669 ASCIIToUTF16("delete"))); |
698 } | 670 } |
OLD | NEW |