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/history/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 AddRequest(request, this); | 108 AddRequest(request, this); |
109 return request->handle(); | 109 return request->handle(); |
110 } | 110 } |
111 }; | 111 }; |
112 | 112 |
113 class HistoryBackendTestBase : public testing::Test { | 113 class HistoryBackendTestBase : public testing::Test { |
114 public: | 114 public: |
115 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; | 115 typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; |
116 | 116 |
117 HistoryBackendTestBase() | 117 HistoryBackendTestBase() |
118 : bookmark_model_(NULL), | 118 : bookmark_model_(bookmark_client_.CreateModel()), |
119 loaded_(false), | 119 loaded_(false), |
120 ui_thread_(content::BrowserThread::UI, &message_loop_) { | 120 ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
121 } | |
122 | 121 |
123 virtual ~HistoryBackendTestBase() { | 122 virtual ~HistoryBackendTestBase() { |
124 STLDeleteValues(&broadcasted_notifications_); | 123 STLDeleteValues(&broadcasted_notifications_); |
125 } | 124 } |
126 | 125 |
127 protected: | 126 protected: |
128 int num_broadcasted_notifications() const { | 127 int num_broadcasted_notifications() const { |
129 return broadcasted_notifications_.size(); | 128 return broadcasted_notifications_.size(); |
130 } | 129 } |
131 | 130 |
(...skipping 15 matching lines...) Expand all Loading... |
147 mem_backend_->Observe( | 146 mem_backend_->Observe( |
148 type, content::Source<HistoryBackendTestBase>(NULL), det); | 147 type, content::Source<HistoryBackendTestBase>(NULL), det); |
149 | 148 |
150 // The backend passes ownership of the details pointer to us. | 149 // The backend passes ownership of the details pointer to us. |
151 broadcasted_notifications_.push_back( | 150 broadcasted_notifications_.push_back( |
152 std::make_pair(type, details.release())); | 151 std::make_pair(type, details.release())); |
153 } | 152 } |
154 | 153 |
155 scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure. | 154 scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure. |
156 scoped_ptr<InMemoryHistoryBackend> mem_backend_; | 155 scoped_ptr<InMemoryHistoryBackend> mem_backend_; |
157 BookmarkModel bookmark_model_; | 156 test::TestBookmarkClient bookmark_client_; |
| 157 scoped_ptr<BookmarkModel> bookmark_model_; |
158 bool loaded_; | 158 bool loaded_; |
159 | 159 |
160 private: | 160 private: |
161 friend class HistoryBackendTestDelegate; | 161 friend class HistoryBackendTestDelegate; |
162 | 162 |
163 // testing::Test | 163 // testing::Test |
164 virtual void SetUp() { | 164 virtual void SetUp() { |
165 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("BackendTest"), | 165 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("BackendTest"), |
166 &test_dir_)) | 166 &test_dir_)) |
167 return; | 167 return; |
168 backend_ = new HistoryBackend(test_dir_, | 168 backend_ = new HistoryBackend( |
169 new HistoryBackendTestDelegate(this), | 169 test_dir_, new HistoryBackendTestDelegate(this), bookmark_model_.get()); |
170 &bookmark_model_); | |
171 backend_->Init(std::string(), false); | 170 backend_->Init(std::string(), false); |
172 } | 171 } |
173 | 172 |
174 virtual void TearDown() { | 173 virtual void TearDown() { |
175 if (backend_.get()) | 174 if (backend_.get()) |
176 backend_->Closing(); | 175 backend_->Closing(); |
177 backend_ = NULL; | 176 backend_ = NULL; |
178 mem_backend_.reset(); | 177 mem_backend_.reset(); |
179 base::DeleteFile(test_dir_, true); | 178 base::DeleteFile(test_dir_, true); |
180 base::RunLoop().RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 backend_->db_->GetVisitsForURL(row2_id, &visits); | 603 backend_->db_->GetVisitsForURL(row2_id, &visits); |
605 ASSERT_EQ(1U, visits.size()); | 604 ASSERT_EQ(1U, visits.size()); |
606 | 605 |
607 // The in-memory backend should have been set and it should have gotten the | 606 // The in-memory backend should have been set and it should have gotten the |
608 // typed URL. | 607 // typed URL. |
609 ASSERT_TRUE(mem_backend_.get()); | 608 ASSERT_TRUE(mem_backend_.get()); |
610 URLRow outrow1; | 609 URLRow outrow1; |
611 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); | 610 EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); |
612 | 611 |
613 // Star row1. | 612 // Star row1. |
614 bookmark_model_.AddURL( | 613 bookmark_model_->AddURL( |
615 bookmark_model_.bookmark_bar_node(), 0, base::string16(), row1.url()); | 614 bookmark_model_->bookmark_bar_node(), 0, base::string16(), row1.url()); |
616 | 615 |
617 // Now finally clear all history. | 616 // Now finally clear all history. |
618 ClearBroadcastedNotifications(); | 617 ClearBroadcastedNotifications(); |
619 backend_->DeleteAllHistory(); | 618 backend_->DeleteAllHistory(); |
620 | 619 |
621 // The first URL should be preserved but the time should be cleared. | 620 // The first URL should be preserved but the time should be cleared. |
622 EXPECT_TRUE(backend_->db_->GetRowForURL(row1.url(), &outrow1)); | 621 EXPECT_TRUE(backend_->db_->GetRowForURL(row1.url(), &outrow1)); |
623 EXPECT_EQ(row1.url(), outrow1.url()); | 622 EXPECT_EQ(row1.url(), outrow1.url()); |
624 EXPECT_EQ(0, outrow1.visit_count()); | 623 EXPECT_EQ(0, outrow1.visit_count()); |
625 EXPECT_EQ(0, outrow1.typed_count()); | 624 EXPECT_EQ(0, outrow1.typed_count()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 | 668 |
670 // The remaining URL should still reference the same favicon, even if its | 669 // The remaining URL should still reference the same favicon, even if its |
671 // ID has changed. | 670 // ID has changed. |
672 std::vector<IconMapping> mappings; | 671 std::vector<IconMapping> mappings; |
673 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 672 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
674 outrow1.url(), favicon_base::FAVICON, &mappings)); | 673 outrow1.url(), favicon_base::FAVICON, &mappings)); |
675 EXPECT_EQ(1u, mappings.size()); | 674 EXPECT_EQ(1u, mappings.size()); |
676 EXPECT_EQ(out_favicon1, mappings[0].icon_id); | 675 EXPECT_EQ(out_favicon1, mappings[0].icon_id); |
677 | 676 |
678 // The first URL should still be bookmarked. | 677 // The first URL should still be bookmarked. |
679 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); | 678 EXPECT_TRUE(bookmark_model_->IsBookmarked(row1.url())); |
680 | 679 |
681 // Check that we fire the notification about all history having been deleted. | 680 // Check that we fire the notification about all history having been deleted. |
682 ASSERT_EQ(1u, broadcasted_notifications().size()); | 681 ASSERT_EQ(1u, broadcasted_notifications().size()); |
683 ASSERT_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 682 ASSERT_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
684 broadcasted_notifications()[0].first); | 683 broadcasted_notifications()[0].first); |
685 const URLsDeletedDetails* details = static_cast<const URLsDeletedDetails*>( | 684 const URLsDeletedDetails* details = static_cast<const URLsDeletedDetails*>( |
686 broadcasted_notifications()[0].second); | 685 broadcasted_notifications()[0].second); |
687 EXPECT_TRUE(details->all_history); | 686 EXPECT_TRUE(details->all_history); |
688 EXPECT_FALSE(details->archived); | 687 EXPECT_FALSE(details->archived); |
689 } | 688 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 | 766 |
768 URLRows rows; | 767 URLRows rows; |
769 rows.push_back(row2); // Reversed order for the same reason as favicons. | 768 rows.push_back(row2); // Reversed order for the same reason as favicons. |
770 rows.push_back(row1); | 769 rows.push_back(row1); |
771 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); | 770 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); |
772 | 771 |
773 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); | 772 URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL); |
774 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); | 773 URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); |
775 | 774 |
776 // Star the two URLs. | 775 // Star the two URLs. |
777 bookmark_utils::AddIfNotBookmarked(&bookmark_model_, row1.url(), | 776 bookmark_utils::AddIfNotBookmarked( |
778 base::string16()); | 777 bookmark_model_.get(), row1.url(), base::string16()); |
779 bookmark_utils::AddIfNotBookmarked(&bookmark_model_, row2.url(), | 778 bookmark_utils::AddIfNotBookmarked( |
780 base::string16()); | 779 bookmark_model_.get(), row2.url(), base::string16()); |
781 | 780 |
782 // Delete url 2. Because url 2 is starred this won't delete the URL, only | 781 // Delete url 2. Because url 2 is starred this won't delete the URL, only |
783 // the visits. | 782 // the visits. |
784 backend_->expirer_.DeleteURL(row2.url()); | 783 backend_->expirer_.DeleteURL(row2.url()); |
785 | 784 |
786 // Make sure url 2 is still valid, but has no visits. | 785 // Make sure url 2 is still valid, but has no visits. |
787 URLRow tmp_url_row; | 786 URLRow tmp_url_row; |
788 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL)); | 787 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL)); |
789 VisitVector visits; | 788 VisitVector visits; |
790 backend_->db_->GetVisitsForURL(row2_id, &visits); | 789 backend_->db_->GetVisitsForURL(row2_id, &visits); |
791 EXPECT_EQ(0U, visits.size()); | 790 EXPECT_EQ(0U, visits.size()); |
792 // The favicon should still be valid. | 791 // The favicon should still be valid. |
793 EXPECT_EQ(favicon2, | 792 EXPECT_EQ(favicon2, |
794 backend_->thumbnail_db_->GetFaviconIDForFaviconURL( | 793 backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
795 favicon_url2, favicon_base::FAVICON, NULL)); | 794 favicon_url2, favicon_base::FAVICON, NULL)); |
796 | 795 |
797 // Unstar row2. | 796 // Unstar row2. |
798 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row2.url()); | 797 bookmark_utils::RemoveAllBookmarks(bookmark_model_.get(), row2.url()); |
799 | 798 |
800 // Tell the backend it was unstarred. We have to explicitly do this as | 799 // Tell the backend it was unstarred. We have to explicitly do this as |
801 // BookmarkModel isn't wired up to the backend during testing. | 800 // BookmarkModel isn't wired up to the backend during testing. |
802 std::set<GURL> unstarred_urls; | 801 std::set<GURL> unstarred_urls; |
803 unstarred_urls.insert(row2.url()); | 802 unstarred_urls.insert(row2.url()); |
804 backend_->URLsNoLongerBookmarked(unstarred_urls); | 803 backend_->URLsNoLongerBookmarked(unstarred_urls); |
805 | 804 |
806 // The URL should no longer exist. | 805 // The URL should no longer exist. |
807 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &tmp_url_row)); | 806 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &tmp_url_row)); |
808 // And the favicon should be deleted. | 807 // And the favicon should be deleted. |
809 EXPECT_EQ(0, | 808 EXPECT_EQ(0, |
810 backend_->thumbnail_db_->GetFaviconIDForFaviconURL( | 809 backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
811 favicon_url2, favicon_base::FAVICON, NULL)); | 810 favicon_url2, favicon_base::FAVICON, NULL)); |
812 | 811 |
813 // Unstar row 1. | 812 // Unstar row 1. |
814 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row1.url()); | 813 bookmark_utils::RemoveAllBookmarks(bookmark_model_.get(), row1.url()); |
815 // Tell the backend it was unstarred. We have to explicitly do this as | 814 // Tell the backend it was unstarred. We have to explicitly do this as |
816 // BookmarkModel isn't wired up to the backend during testing. | 815 // BookmarkModel isn't wired up to the backend during testing. |
817 unstarred_urls.clear(); | 816 unstarred_urls.clear(); |
818 unstarred_urls.insert(row1.url()); | 817 unstarred_urls.insert(row1.url()); |
819 backend_->URLsNoLongerBookmarked(unstarred_urls); | 818 backend_->URLsNoLongerBookmarked(unstarred_urls); |
820 | 819 |
821 // The URL should still exist (because there were visits). | 820 // The URL should still exist (because there were visits). |
822 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL)); | 821 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL)); |
823 | 822 |
824 // There should still be visits. | 823 // There should still be visits. |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 favicons.clear(); | 1075 favicons.clear(); |
1077 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico"); | 1076 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico"); |
1078 favicon.png_data.push_back('3'); | 1077 favicon.png_data.push_back('3'); |
1079 favicon.urls.insert(url3); | 1078 favicon.urls.insert(url3); |
1080 favicons.push_back(favicon); | 1079 favicons.push_back(favicon); |
1081 backend_->SetImportedFavicons(favicons); | 1080 backend_->SetImportedFavicons(favicons); |
1082 URLRow url_row3; | 1081 URLRow url_row3; |
1083 EXPECT_TRUE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); | 1082 EXPECT_TRUE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); |
1084 | 1083 |
1085 // If the URL is bookmarked, it should get added to history with 0 visits. | 1084 // If the URL is bookmarked, it should get added to history with 0 visits. |
1086 bookmark_model_.AddURL(bookmark_model_.bookmark_bar_node(), 0, | 1085 bookmark_model_->AddURL( |
1087 base::string16(), url3); | 1086 bookmark_model_->bookmark_bar_node(), 0, base::string16(), url3); |
1088 backend_->SetImportedFavicons(favicons); | 1087 backend_->SetImportedFavicons(favicons); |
1089 EXPECT_FALSE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); | 1088 EXPECT_FALSE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); |
1090 EXPECT_TRUE(url_row3.visit_count() == 0); | 1089 EXPECT_TRUE(url_row3.visit_count() == 0); |
1091 } | 1090 } |
1092 | 1091 |
1093 TEST_F(HistoryBackendTest, StripUsernamePasswordTest) { | 1092 TEST_F(HistoryBackendTest, StripUsernamePasswordTest) { |
1094 ASSERT_TRUE(backend_.get()); | 1093 ASSERT_TRUE(backend_.get()); |
1095 | 1094 |
1096 GURL url("http://anyuser:anypass@www.google.com"); | 1095 GURL url("http://anyuser:anypass@www.google.com"); |
1097 GURL stripped_url("http://www.google.com"); | 1096 GURL stripped_url("http://www.google.com"); |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1479 // in Teardown. | 1478 // in Teardown. |
1480 base::FilePath new_history_path(test_dir()); | 1479 base::FilePath new_history_path(test_dir()); |
1481 base::DeleteFile(new_history_path, true); | 1480 base::DeleteFile(new_history_path, true); |
1482 base::CreateDirectory(new_history_path); | 1481 base::CreateDirectory(new_history_path); |
1483 base::FilePath new_history_file = | 1482 base::FilePath new_history_file = |
1484 new_history_path.Append(chrome::kHistoryFilename); | 1483 new_history_path.Append(chrome::kHistoryFilename); |
1485 ASSERT_TRUE(base::CopyFile(old_history_path, new_history_file)); | 1484 ASSERT_TRUE(base::CopyFile(old_history_path, new_history_file)); |
1486 | 1485 |
1487 backend_ = new HistoryBackend(new_history_path, | 1486 backend_ = new HistoryBackend(new_history_path, |
1488 new HistoryBackendTestDelegate(this), | 1487 new HistoryBackendTestDelegate(this), |
1489 &bookmark_model_); | 1488 bookmark_model_.get()); |
1490 backend_->Init(std::string(), false); | 1489 backend_->Init(std::string(), false); |
1491 backend_->Closing(); | 1490 backend_->Closing(); |
1492 backend_ = NULL; | 1491 backend_ = NULL; |
1493 | 1492 |
1494 // Now the database should already be migrated. | 1493 // Now the database should already be migrated. |
1495 // Check version first. | 1494 // Check version first. |
1496 int cur_version = HistoryDatabase::GetCurrentVersion(); | 1495 int cur_version = HistoryDatabase::GetCurrentVersion(); |
1497 sql::Connection db; | 1496 sql::Connection db; |
1498 ASSERT_TRUE(db.Open(new_history_file)); | 1497 ASSERT_TRUE(db.Open(new_history_file)); |
1499 sql::Statement s(db.GetUniqueStatement( | 1498 sql::Statement s(db.GetUniqueStatement( |
(...skipping 1382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2882 base::CreateDirectory(new_history_path); | 2881 base::CreateDirectory(new_history_path); |
2883 base::FilePath new_history_file = | 2882 base::FilePath new_history_file = |
2884 new_history_path.Append(chrome::kHistoryFilename); | 2883 new_history_path.Append(chrome::kHistoryFilename); |
2885 base::FilePath new_archived_file = | 2884 base::FilePath new_archived_file = |
2886 new_history_path.Append(chrome::kArchivedHistoryFilename); | 2885 new_history_path.Append(chrome::kArchivedHistoryFilename); |
2887 ASSERT_TRUE(base::CopyFile(old_history, new_history_file)); | 2886 ASSERT_TRUE(base::CopyFile(old_history, new_history_file)); |
2888 ASSERT_TRUE(base::CopyFile(old_archived, new_archived_file)); | 2887 ASSERT_TRUE(base::CopyFile(old_archived, new_archived_file)); |
2889 | 2888 |
2890 backend_ = new HistoryBackend(new_history_path, | 2889 backend_ = new HistoryBackend(new_history_path, |
2891 new HistoryBackendTestDelegate(this), | 2890 new HistoryBackendTestDelegate(this), |
2892 &bookmark_model_); | 2891 bookmark_model_.get()); |
2893 backend_->Init(std::string(), false); | 2892 backend_->Init(std::string(), false); |
2894 backend_->Closing(); | 2893 backend_->Closing(); |
2895 backend_ = NULL; | 2894 backend_ = NULL; |
2896 | 2895 |
2897 // Now both history and archived_history databases should already be migrated. | 2896 // Now both history and archived_history databases should already be migrated. |
2898 | 2897 |
2899 // Check version in history database first. | 2898 // Check version in history database first. |
2900 int cur_version = HistoryDatabase::GetCurrentVersion(); | 2899 int cur_version = HistoryDatabase::GetCurrentVersion(); |
2901 sql::Connection db; | 2900 sql::Connection db; |
2902 ASSERT_TRUE(db.Open(new_history_file)); | 2901 ASSERT_TRUE(db.Open(new_history_file)); |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3384 // Verify that the second term is no longer returned as result, and also check | 3383 // Verify that the second term is no longer returned as result, and also check |
3385 // at the low level that it is gone for good. The term corresponding to the | 3384 // at the low level that it is gone for good. The term corresponding to the |
3386 // first URLRow should not be affected. | 3385 // first URLRow should not be affected. |
3387 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); | 3386 EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1)); |
3388 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); | 3387 EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2)); |
3389 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); | 3388 EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL)); |
3390 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); | 3389 EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL)); |
3391 } | 3390 } |
3392 | 3391 |
3393 } // namespace history | 3392 } // namespace history |
OLD | NEW |