| 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 // History unit tests come in two flavors: | 5 // History unit tests come in two flavors: |
| 6 // | 6 // |
| 7 // 1. The more complicated style is that the unit test creates a full history | 7 // 1. The more complicated style is that the unit test creates a full history |
| 8 // service. This spawns a background thread for the history backend, and | 8 // service. This spawns a background thread for the history backend, and |
| 9 // all communication is asynchronous. This is useful for testing more | 9 // all communication is asynchronous. This is useful for testing more |
| 10 // complicated things or end-to-end behavior. | 10 // complicated things or end-to-end behavior. |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 backend_->Closing(); | 149 backend_->Closing(); |
| 150 backend_ = NULL; | 150 backend_ = NULL; |
| 151 } | 151 } |
| 152 } | 152 } |
| 153 | 153 |
| 154 virtual void TearDown() { | 154 virtual void TearDown() { |
| 155 DeleteBackend(); | 155 DeleteBackend(); |
| 156 | 156 |
| 157 // Make sure we don't have any event pending that could disrupt the next | 157 // Make sure we don't have any event pending that could disrupt the next |
| 158 // test. | 158 // test. |
| 159 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 159 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 160 MessageLoop::current()->Run(); | 160 base::MessageLoop::QuitClosure()); |
| 161 base::MessageLoop::current()->Run(); |
| 161 } | 162 } |
| 162 | 163 |
| 163 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { | 164 int64 AddDownload(DownloadItem::DownloadState state, const Time& time) { |
| 164 std::vector<GURL> url_chain; | 165 std::vector<GURL> url_chain; |
| 165 url_chain.push_back(GURL("foo-url")); | 166 url_chain.push_back(GURL("foo-url")); |
| 166 | 167 |
| 167 DownloadRow download(base::FilePath(FILE_PATH_LITERAL("foo-path")), | 168 DownloadRow download(base::FilePath(FILE_PATH_LITERAL("foo-path")), |
| 168 base::FilePath(FILE_PATH_LITERAL("foo-path")), | 169 base::FilePath(FILE_PATH_LITERAL("foo-path")), |
| 169 url_chain, | 170 url_chain, |
| 170 GURL(std::string()), | 171 GURL(std::string()), |
| 171 time, | 172 time, |
| 172 time, | 173 time, |
| 173 0, | 174 0, |
| 174 512, | 175 512, |
| 175 state, | 176 state, |
| 176 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 177 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 177 content::DOWNLOAD_INTERRUPT_REASON_NONE, | 178 content::DOWNLOAD_INTERRUPT_REASON_NONE, |
| 178 0, | 179 0, |
| 179 0); | 180 0); |
| 180 return db_->CreateDownload(download); | 181 return db_->CreateDownload(download); |
| 181 } | 182 } |
| 182 | 183 |
| 183 base::ScopedTempDir temp_dir_; | 184 base::ScopedTempDir temp_dir_; |
| 184 | 185 |
| 185 MessageLoopForUI message_loop_; | 186 base::MessageLoopForUI message_loop_; |
| 186 | 187 |
| 187 // names of the database files | 188 // names of the database files |
| 188 base::FilePath history_dir_; | 189 base::FilePath history_dir_; |
| 189 | 190 |
| 190 // Created via CreateBackendAndDatabase. | 191 // Created via CreateBackendAndDatabase. |
| 191 scoped_refptr<HistoryBackend> backend_; | 192 scoped_refptr<HistoryBackend> backend_; |
| 192 scoped_ptr<InMemoryHistoryBackend> in_mem_backend_; | 193 scoped_ptr<InMemoryHistoryBackend> in_mem_backend_; |
| 193 HistoryDatabase* db_; // Cached reference to the backend's database. | 194 HistoryDatabase* db_; // Cached reference to the backend's database. |
| 194 }; | 195 }; |
| 195 | 196 |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 CreateBackendAndDatabase(); | 545 CreateBackendAndDatabase(); |
| 545 std::vector<DownloadRow> results; | 546 std::vector<DownloadRow> results; |
| 546 db_->QueryDownloads(&results); | 547 db_->QueryDownloads(&results); |
| 547 ASSERT_EQ(1u, results.size()); | 548 ASSERT_EQ(1u, results.size()); |
| 548 EXPECT_EQ(content::DownloadItem::INTERRUPTED, results[0].state); | 549 EXPECT_EQ(content::DownloadItem::INTERRUPTED, results[0].state); |
| 549 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, | 550 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, |
| 550 results[0].interrupt_reason); | 551 results[0].interrupt_reason); |
| 551 | 552 |
| 552 // Allow the update to propagate, shut down the DB, and confirm that | 553 // Allow the update to propagate, shut down the DB, and confirm that |
| 553 // the query updated the on disk database as well. | 554 // the query updated the on disk database as well. |
| 554 MessageLoop::current()->RunUntilIdle(); | 555 base::MessageLoop::current()->RunUntilIdle(); |
| 555 DeleteBackend(); | 556 DeleteBackend(); |
| 556 { | 557 { |
| 557 sql::Connection db; | 558 sql::Connection db; |
| 558 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); | 559 ASSERT_TRUE(db.Open(history_dir_.Append(chrome::kHistoryFilename))); |
| 559 sql::Statement statement(db.GetUniqueStatement( | 560 sql::Statement statement(db.GetUniqueStatement( |
| 560 "Select Count(*) from downloads")); | 561 "Select Count(*) from downloads")); |
| 561 EXPECT_TRUE(statement.Step()); | 562 EXPECT_TRUE(statement.Step()); |
| 562 EXPECT_EQ(1, statement.ColumnInt(0)); | 563 EXPECT_EQ(1, statement.ColumnInt(0)); |
| 563 | 564 |
| 564 sql::Statement statement1(db.GetUniqueStatement( | 565 sql::Statement statement1(db.GetUniqueStatement( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 676 redirect_query_success_(false), | 677 redirect_query_success_(false), |
| 677 query_url_success_(false) { | 678 query_url_success_(false) { |
| 678 } | 679 } |
| 679 | 680 |
| 680 virtual ~HistoryTest() { | 681 virtual ~HistoryTest() { |
| 681 } | 682 } |
| 682 | 683 |
| 683 void OnSegmentUsageAvailable(CancelableRequestProvider::Handle handle, | 684 void OnSegmentUsageAvailable(CancelableRequestProvider::Handle handle, |
| 684 std::vector<PageUsageData*>* data) { | 685 std::vector<PageUsageData*>* data) { |
| 685 page_usage_data_.swap(*data); | 686 page_usage_data_.swap(*data); |
| 686 MessageLoop::current()->Quit(); | 687 base::MessageLoop::current()->Quit(); |
| 687 } | 688 } |
| 688 | 689 |
| 689 void OnDeleteURLsDone(CancelableRequestProvider::Handle handle) { | 690 void OnDeleteURLsDone(CancelableRequestProvider::Handle handle) { |
| 690 MessageLoop::current()->Quit(); | 691 base::MessageLoop::current()->Quit(); |
| 691 } | 692 } |
| 692 | 693 |
| 693 void OnMostVisitedURLsAvailable(CancelableRequestProvider::Handle handle, | 694 void OnMostVisitedURLsAvailable(CancelableRequestProvider::Handle handle, |
| 694 MostVisitedURLList url_list) { | 695 MostVisitedURLList url_list) { |
| 695 most_visited_urls_.swap(url_list); | 696 most_visited_urls_.swap(url_list); |
| 696 MessageLoop::current()->Quit(); | 697 base::MessageLoop::current()->Quit(); |
| 697 } | 698 } |
| 698 | 699 |
| 699 protected: | 700 protected: |
| 700 friend class BackendDelegate; | 701 friend class BackendDelegate; |
| 701 | 702 |
| 702 // testing::Test | 703 // testing::Test |
| 703 virtual void SetUp() { | 704 virtual void SetUp() { |
| 704 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 705 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 705 history_dir_ = temp_dir_.path().AppendASCII("HistoryTest"); | 706 history_dir_ = temp_dir_.path().AppendASCII("HistoryTest"); |
| 706 ASSERT_TRUE(file_util::CreateDirectory(history_dir_)); | 707 ASSERT_TRUE(file_util::CreateDirectory(history_dir_)); |
| 707 history_service_.reset(new HistoryService); | 708 history_service_.reset(new HistoryService); |
| 708 if (!history_service_->Init(history_dir_, NULL)) { | 709 if (!history_service_->Init(history_dir_, NULL)) { |
| 709 history_service_.reset(); | 710 history_service_.reset(); |
| 710 ADD_FAILURE(); | 711 ADD_FAILURE(); |
| 711 } | 712 } |
| 712 } | 713 } |
| 713 | 714 |
| 714 virtual void TearDown() { | 715 virtual void TearDown() { |
| 715 if (history_service_) | 716 if (history_service_) |
| 716 CleanupHistoryService(); | 717 CleanupHistoryService(); |
| 717 | 718 |
| 718 // Make sure we don't have any event pending that could disrupt the next | 719 // Make sure we don't have any event pending that could disrupt the next |
| 719 // test. | 720 // test. |
| 720 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 721 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 721 MessageLoop::current()->Run(); | 722 base::MessageLoop::QuitClosure()); |
| 723 base::MessageLoop::current()->Run(); |
| 722 } | 724 } |
| 723 | 725 |
| 724 void CleanupHistoryService() { | 726 void CleanupHistoryService() { |
| 725 DCHECK(history_service_); | 727 DCHECK(history_service_); |
| 726 | 728 |
| 727 history_service_->NotifyRenderProcessHostDestruction(0); | 729 history_service_->NotifyRenderProcessHostDestruction(0); |
| 728 history_service_->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); | 730 history_service_->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure()); |
| 729 history_service_->Cleanup(); | 731 history_service_->Cleanup(); |
| 730 history_service_.reset(); | 732 history_service_.reset(); |
| 731 | 733 |
| 732 // Wait for the backend class to terminate before deleting the files and | 734 // Wait for the backend class to terminate before deleting the files and |
| 733 // moving to the next test. Note: if this never terminates, somebody is | 735 // moving to the next test. Note: if this never terminates, somebody is |
| 734 // probably leaking a reference to the history backend, so it never calls | 736 // probably leaking a reference to the history backend, so it never calls |
| 735 // our destroy task. | 737 // our destroy task. |
| 736 MessageLoop::current()->Run(); | 738 base::MessageLoop::current()->Run(); |
| 737 } | 739 } |
| 738 | 740 |
| 739 // Fills the query_url_row_ and query_url_visits_ structures with the | 741 // Fills the query_url_row_ and query_url_visits_ structures with the |
| 740 // information about the given URL and returns true. If the URL was not | 742 // information about the given URL and returns true. If the URL was not |
| 741 // found, this will return false and those structures will not be changed. | 743 // found, this will return false and those structures will not be changed. |
| 742 bool QueryURL(HistoryService* history, const GURL& url) { | 744 bool QueryURL(HistoryService* history, const GURL& url) { |
| 743 history_service_->QueryURL(url, true, &consumer_, | 745 history_service_->QueryURL(url, true, &consumer_, |
| 744 base::Bind(&HistoryTest::SaveURLAndQuit, | 746 base::Bind(&HistoryTest::SaveURLAndQuit, |
| 745 base::Unretained(this))); | 747 base::Unretained(this))); |
| 746 MessageLoop::current()->Run(); // Will be exited in SaveURLAndQuit. | 748 base::MessageLoop::current()->Run(); // Will be exited in SaveURLAndQuit. |
| 747 return query_url_success_; | 749 return query_url_success_; |
| 748 } | 750 } |
| 749 | 751 |
| 750 // Callback for HistoryService::QueryURL. | 752 // Callback for HistoryService::QueryURL. |
| 751 void SaveURLAndQuit(HistoryService::Handle handle, | 753 void SaveURLAndQuit(HistoryService::Handle handle, |
| 752 bool success, | 754 bool success, |
| 753 const URLRow* url_row, | 755 const URLRow* url_row, |
| 754 VisitVector* visit_vector) { | 756 VisitVector* visit_vector) { |
| 755 query_url_success_ = success; | 757 query_url_success_ = success; |
| 756 if (query_url_success_) { | 758 if (query_url_success_) { |
| 757 query_url_row_ = *url_row; | 759 query_url_row_ = *url_row; |
| 758 query_url_visits_.swap(*visit_vector); | 760 query_url_visits_.swap(*visit_vector); |
| 759 } else { | 761 } else { |
| 760 query_url_row_ = URLRow(); | 762 query_url_row_ = URLRow(); |
| 761 query_url_visits_.clear(); | 763 query_url_visits_.clear(); |
| 762 } | 764 } |
| 763 MessageLoop::current()->Quit(); | 765 base::MessageLoop::current()->Quit(); |
| 764 } | 766 } |
| 765 | 767 |
| 766 // Fills in saved_redirects_ with the redirect information for the given URL, | 768 // Fills in saved_redirects_ with the redirect information for the given URL, |
| 767 // returning true on success. False means the URL was not found. | 769 // returning true on success. False means the URL was not found. |
| 768 bool QueryRedirectsFrom(HistoryService* history, const GURL& url) { | 770 bool QueryRedirectsFrom(HistoryService* history, const GURL& url) { |
| 769 history_service_->QueryRedirectsFrom( | 771 history_service_->QueryRedirectsFrom( |
| 770 url, &consumer_, | 772 url, &consumer_, |
| 771 base::Bind(&HistoryTest::OnRedirectQueryComplete, | 773 base::Bind(&HistoryTest::OnRedirectQueryComplete, |
| 772 base::Unretained(this))); | 774 base::Unretained(this))); |
| 773 MessageLoop::current()->Run(); // Will be exited in *QueryComplete. | 775 base::MessageLoop::current()->Run(); // Will be exited in *QueryComplete. |
| 774 return redirect_query_success_; | 776 return redirect_query_success_; |
| 775 } | 777 } |
| 776 | 778 |
| 777 // Callback for QueryRedirects. | 779 // Callback for QueryRedirects. |
| 778 void OnRedirectQueryComplete(HistoryService::Handle handle, | 780 void OnRedirectQueryComplete(HistoryService::Handle handle, |
| 779 GURL url, | 781 GURL url, |
| 780 bool success, | 782 bool success, |
| 781 history::RedirectList* redirects) { | 783 history::RedirectList* redirects) { |
| 782 redirect_query_success_ = success; | 784 redirect_query_success_ = success; |
| 783 if (redirect_query_success_) | 785 if (redirect_query_success_) |
| 784 saved_redirects_.swap(*redirects); | 786 saved_redirects_.swap(*redirects); |
| 785 else | 787 else |
| 786 saved_redirects_.clear(); | 788 saved_redirects_.clear(); |
| 787 MessageLoop::current()->Quit(); | 789 base::MessageLoop::current()->Quit(); |
| 788 } | 790 } |
| 789 | 791 |
| 790 base::ScopedTempDir temp_dir_; | 792 base::ScopedTempDir temp_dir_; |
| 791 | 793 |
| 792 MessageLoopForUI message_loop_; | 794 base::MessageLoopForUI message_loop_; |
| 793 | 795 |
| 794 // PageUsageData vector to test segments. | 796 // PageUsageData vector to test segments. |
| 795 ScopedVector<PageUsageData> page_usage_data_; | 797 ScopedVector<PageUsageData> page_usage_data_; |
| 796 | 798 |
| 797 MostVisitedURLList most_visited_urls_; | 799 MostVisitedURLList most_visited_urls_; |
| 798 | 800 |
| 799 // When non-NULL, this will be deleted on tear down and we will block until | 801 // When non-NULL, this will be deleted on tear down and we will block until |
| 800 // the backend thread has completed. This allows tests for the history | 802 // the backend thread has completed. This allows tests for the history |
| 801 // service to use this feature, but other tests to ignore this. | 803 // service to use this feature, but other tests to ignore this. |
| 802 scoped_ptr<HistoryService> history_service_; | 804 scoped_ptr<HistoryService> history_service_; |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1097 history::RedirectList(), content::PAGE_TRANSITION_TYPED, | 1099 history::RedirectList(), content::PAGE_TRANSITION_TYPED, |
| 1098 history::SOURCE_BROWSED, false); | 1100 history::SOURCE_BROWSED, false); |
| 1099 | 1101 |
| 1100 // Make sure a segment was created. | 1102 // Make sure a segment was created. |
| 1101 history_service_->QuerySegmentUsageSince( | 1103 history_service_->QuerySegmentUsageSince( |
| 1102 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, | 1104 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, |
| 1103 base::Bind(&HistoryTest::OnSegmentUsageAvailable, | 1105 base::Bind(&HistoryTest::OnSegmentUsageAvailable, |
| 1104 base::Unretained(this))); | 1106 base::Unretained(this))); |
| 1105 | 1107 |
| 1106 // Wait for processing. | 1108 // Wait for processing. |
| 1107 MessageLoop::current()->Run(); | 1109 base::MessageLoop::current()->Run(); |
| 1108 | 1110 |
| 1109 ASSERT_EQ(1U, page_usage_data_.size()); | 1111 ASSERT_EQ(1U, page_usage_data_.size()); |
| 1110 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); | 1112 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); |
| 1111 EXPECT_DOUBLE_EQ(3.0, page_usage_data_[0]->GetScore()); | 1113 EXPECT_DOUBLE_EQ(3.0, page_usage_data_[0]->GetScore()); |
| 1112 | 1114 |
| 1113 // Add a URL which doesn't create a segment. | 1115 // Add a URL which doesn't create a segment. |
| 1114 const GURL link_url("http://yahoo.com/"); | 1116 const GURL link_url("http://yahoo.com/"); |
| 1115 history_service_->AddPage( | 1117 history_service_->AddPage( |
| 1116 link_url, base::Time::Now(), scope, 0, GURL(), | 1118 link_url, base::Time::Now(), scope, 0, GURL(), |
| 1117 history::RedirectList(), content::PAGE_TRANSITION_LINK, | 1119 history::RedirectList(), content::PAGE_TRANSITION_LINK, |
| 1118 history::SOURCE_BROWSED, false); | 1120 history::SOURCE_BROWSED, false); |
| 1119 | 1121 |
| 1120 // Query again | 1122 // Query again |
| 1121 history_service_->QuerySegmentUsageSince( | 1123 history_service_->QuerySegmentUsageSince( |
| 1122 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, | 1124 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, |
| 1123 base::Bind(&HistoryTest::OnSegmentUsageAvailable, | 1125 base::Bind(&HistoryTest::OnSegmentUsageAvailable, |
| 1124 base::Unretained(this))); | 1126 base::Unretained(this))); |
| 1125 | 1127 |
| 1126 // Wait for processing. | 1128 // Wait for processing. |
| 1127 MessageLoop::current()->Run(); | 1129 base::MessageLoop::current()->Run(); |
| 1128 | 1130 |
| 1129 // Make sure we still have one segment. | 1131 // Make sure we still have one segment. |
| 1130 ASSERT_EQ(1U, page_usage_data_.size()); | 1132 ASSERT_EQ(1U, page_usage_data_.size()); |
| 1131 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); | 1133 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); |
| 1132 | 1134 |
| 1133 // Add a page linked from existing_url. | 1135 // Add a page linked from existing_url. |
| 1134 history_service_->AddPage( | 1136 history_service_->AddPage( |
| 1135 GURL("http://www.google.com/foo"), base::Time::Now(), | 1137 GURL("http://www.google.com/foo"), base::Time::Now(), |
| 1136 scope, 3, existing_url, history::RedirectList(), | 1138 scope, 3, existing_url, history::RedirectList(), |
| 1137 content::PAGE_TRANSITION_LINK, history::SOURCE_BROWSED, | 1139 content::PAGE_TRANSITION_LINK, history::SOURCE_BROWSED, |
| 1138 false); | 1140 false); |
| 1139 | 1141 |
| 1140 // Query again | 1142 // Query again |
| 1141 history_service_->QuerySegmentUsageSince( | 1143 history_service_->QuerySegmentUsageSince( |
| 1142 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, | 1144 &consumer_, Time::Now() - TimeDelta::FromDays(1), 10, |
| 1143 base::Bind(&HistoryTest::OnSegmentUsageAvailable, | 1145 base::Bind(&HistoryTest::OnSegmentUsageAvailable, |
| 1144 base::Unretained(this))); | 1146 base::Unretained(this))); |
| 1145 | 1147 |
| 1146 // Wait for processing. | 1148 // Wait for processing. |
| 1147 MessageLoop::current()->Run(); | 1149 base::MessageLoop::current()->Run(); |
| 1148 | 1150 |
| 1149 // Make sure we still have one segment. | 1151 // Make sure we still have one segment. |
| 1150 ASSERT_EQ(1U, page_usage_data_.size()); | 1152 ASSERT_EQ(1U, page_usage_data_.size()); |
| 1151 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); | 1153 EXPECT_TRUE(page_usage_data_[0]->GetURL() == existing_url); |
| 1152 | 1154 |
| 1153 // However, the score should have increased. | 1155 // However, the score should have increased. |
| 1154 EXPECT_GT(page_usage_data_[0]->GetScore(), 5.0); | 1156 EXPECT_GT(page_usage_data_[0]->GetScore(), 5.0); |
| 1155 } | 1157 } |
| 1156 | 1158 |
| 1157 TEST_F(HistoryTest, MostVisitedURLs) { | 1159 TEST_F(HistoryTest, MostVisitedURLs) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1172 history::SOURCE_BROWSED, false); | 1174 history::SOURCE_BROWSED, false); |
| 1173 history_service_->AddPage( | 1175 history_service_->AddPage( |
| 1174 url1, base::Time::Now(), scope, 0, GURL(), | 1176 url1, base::Time::Now(), scope, 0, GURL(), |
| 1175 history::RedirectList(), content::PAGE_TRANSITION_TYPED, | 1177 history::RedirectList(), content::PAGE_TRANSITION_TYPED, |
| 1176 history::SOURCE_BROWSED, false); | 1178 history::SOURCE_BROWSED, false); |
| 1177 history_service_->QueryMostVisitedURLs( | 1179 history_service_->QueryMostVisitedURLs( |
| 1178 20, 90, &consumer_, | 1180 20, 90, &consumer_, |
| 1179 base::Bind( | 1181 base::Bind( |
| 1180 &HistoryTest::OnMostVisitedURLsAvailable, | 1182 &HistoryTest::OnMostVisitedURLsAvailable, |
| 1181 base::Unretained(this))); | 1183 base::Unretained(this))); |
| 1182 MessageLoop::current()->Run(); | 1184 base::MessageLoop::current()->Run(); |
| 1183 | 1185 |
| 1184 EXPECT_EQ(2U, most_visited_urls_.size()); | 1186 EXPECT_EQ(2U, most_visited_urls_.size()); |
| 1185 EXPECT_EQ(url0, most_visited_urls_[0].url); | 1187 EXPECT_EQ(url0, most_visited_urls_[0].url); |
| 1186 EXPECT_EQ(url1, most_visited_urls_[1].url); | 1188 EXPECT_EQ(url1, most_visited_urls_[1].url); |
| 1187 | 1189 |
| 1188 // Add another page. | 1190 // Add another page. |
| 1189 history_service_->AddPage( | 1191 history_service_->AddPage( |
| 1190 url2, base::Time::Now(), scope, 0, GURL(), | 1192 url2, base::Time::Now(), scope, 0, GURL(), |
| 1191 history::RedirectList(), content::PAGE_TRANSITION_TYPED, | 1193 history::RedirectList(), content::PAGE_TRANSITION_TYPED, |
| 1192 history::SOURCE_BROWSED, false); | 1194 history::SOURCE_BROWSED, false); |
| 1193 history_service_->QueryMostVisitedURLs( | 1195 history_service_->QueryMostVisitedURLs( |
| 1194 20, 90, &consumer_, | 1196 20, 90, &consumer_, |
| 1195 base::Bind( | 1197 base::Bind( |
| 1196 &HistoryTest::OnMostVisitedURLsAvailable, | 1198 &HistoryTest::OnMostVisitedURLsAvailable, |
| 1197 base::Unretained(this))); | 1199 base::Unretained(this))); |
| 1198 MessageLoop::current()->Run(); | 1200 base::MessageLoop::current()->Run(); |
| 1199 | 1201 |
| 1200 EXPECT_EQ(3U, most_visited_urls_.size()); | 1202 EXPECT_EQ(3U, most_visited_urls_.size()); |
| 1201 EXPECT_EQ(url0, most_visited_urls_[0].url); | 1203 EXPECT_EQ(url0, most_visited_urls_[0].url); |
| 1202 EXPECT_EQ(url1, most_visited_urls_[1].url); | 1204 EXPECT_EQ(url1, most_visited_urls_[1].url); |
| 1203 EXPECT_EQ(url2, most_visited_urls_[2].url); | 1205 EXPECT_EQ(url2, most_visited_urls_[2].url); |
| 1204 | 1206 |
| 1205 // Revisit url2, making it the top URL. | 1207 // Revisit url2, making it the top URL. |
| 1206 history_service_->AddPage( | 1208 history_service_->AddPage( |
| 1207 url2, base::Time::Now(), scope, 0, GURL(), | 1209 url2, base::Time::Now(), scope, 0, GURL(), |
| 1208 history::RedirectList(), content::PAGE_TRANSITION_TYPED, | 1210 history::RedirectList(), content::PAGE_TRANSITION_TYPED, |
| 1209 history::SOURCE_BROWSED, false); | 1211 history::SOURCE_BROWSED, false); |
| 1210 history_service_->QueryMostVisitedURLs( | 1212 history_service_->QueryMostVisitedURLs( |
| 1211 20, 90, &consumer_, | 1213 20, 90, &consumer_, |
| 1212 base::Bind( | 1214 base::Bind( |
| 1213 &HistoryTest::OnMostVisitedURLsAvailable, | 1215 &HistoryTest::OnMostVisitedURLsAvailable, |
| 1214 base::Unretained(this))); | 1216 base::Unretained(this))); |
| 1215 MessageLoop::current()->Run(); | 1217 base::MessageLoop::current()->Run(); |
| 1216 | 1218 |
| 1217 EXPECT_EQ(3U, most_visited_urls_.size()); | 1219 EXPECT_EQ(3U, most_visited_urls_.size()); |
| 1218 EXPECT_EQ(url2, most_visited_urls_[0].url); | 1220 EXPECT_EQ(url2, most_visited_urls_[0].url); |
| 1219 EXPECT_EQ(url0, most_visited_urls_[1].url); | 1221 EXPECT_EQ(url0, most_visited_urls_[1].url); |
| 1220 EXPECT_EQ(url1, most_visited_urls_[2].url); | 1222 EXPECT_EQ(url1, most_visited_urls_[2].url); |
| 1221 | 1223 |
| 1222 // Revisit url1, making it the top URL. | 1224 // Revisit url1, making it the top URL. |
| 1223 history_service_->AddPage( | 1225 history_service_->AddPage( |
| 1224 url1, base::Time::Now(), scope, 0, GURL(), | 1226 url1, base::Time::Now(), scope, 0, GURL(), |
| 1225 history::RedirectList(), content::PAGE_TRANSITION_TYPED, | 1227 history::RedirectList(), content::PAGE_TRANSITION_TYPED, |
| 1226 history::SOURCE_BROWSED, false); | 1228 history::SOURCE_BROWSED, false); |
| 1227 history_service_->QueryMostVisitedURLs( | 1229 history_service_->QueryMostVisitedURLs( |
| 1228 20, 90, &consumer_, | 1230 20, 90, &consumer_, |
| 1229 base::Bind( | 1231 base::Bind( |
| 1230 &HistoryTest::OnMostVisitedURLsAvailable, | 1232 &HistoryTest::OnMostVisitedURLsAvailable, |
| 1231 base::Unretained(this))); | 1233 base::Unretained(this))); |
| 1232 MessageLoop::current()->Run(); | 1234 base::MessageLoop::current()->Run(); |
| 1233 | 1235 |
| 1234 EXPECT_EQ(3U, most_visited_urls_.size()); | 1236 EXPECT_EQ(3U, most_visited_urls_.size()); |
| 1235 EXPECT_EQ(url1, most_visited_urls_[0].url); | 1237 EXPECT_EQ(url1, most_visited_urls_[0].url); |
| 1236 EXPECT_EQ(url2, most_visited_urls_[1].url); | 1238 EXPECT_EQ(url2, most_visited_urls_[1].url); |
| 1237 EXPECT_EQ(url0, most_visited_urls_[2].url); | 1239 EXPECT_EQ(url0, most_visited_urls_[2].url); |
| 1238 | 1240 |
| 1239 // Redirects | 1241 // Redirects |
| 1240 history::RedirectList redirects; | 1242 history::RedirectList redirects; |
| 1241 redirects.push_back(url3); | 1243 redirects.push_back(url3); |
| 1242 redirects.push_back(url4); | 1244 redirects.push_back(url4); |
| 1243 | 1245 |
| 1244 // Visit url4 using redirects. | 1246 // Visit url4 using redirects. |
| 1245 history_service_->AddPage( | 1247 history_service_->AddPage( |
| 1246 url4, base::Time::Now(), scope, 0, GURL(), | 1248 url4, base::Time::Now(), scope, 0, GURL(), |
| 1247 redirects, content::PAGE_TRANSITION_TYPED, | 1249 redirects, content::PAGE_TRANSITION_TYPED, |
| 1248 history::SOURCE_BROWSED, false); | 1250 history::SOURCE_BROWSED, false); |
| 1249 history_service_->QueryMostVisitedURLs( | 1251 history_service_->QueryMostVisitedURLs( |
| 1250 20, 90, &consumer_, | 1252 20, 90, &consumer_, |
| 1251 base::Bind( | 1253 base::Bind( |
| 1252 &HistoryTest::OnMostVisitedURLsAvailable, | 1254 &HistoryTest::OnMostVisitedURLsAvailable, |
| 1253 base::Unretained(this))); | 1255 base::Unretained(this))); |
| 1254 MessageLoop::current()->Run(); | 1256 base::MessageLoop::current()->Run(); |
| 1255 | 1257 |
| 1256 EXPECT_EQ(4U, most_visited_urls_.size()); | 1258 EXPECT_EQ(4U, most_visited_urls_.size()); |
| 1257 EXPECT_EQ(url1, most_visited_urls_[0].url); | 1259 EXPECT_EQ(url1, most_visited_urls_[0].url); |
| 1258 EXPECT_EQ(url2, most_visited_urls_[1].url); | 1260 EXPECT_EQ(url2, most_visited_urls_[1].url); |
| 1259 EXPECT_EQ(url0, most_visited_urls_[2].url); | 1261 EXPECT_EQ(url0, most_visited_urls_[2].url); |
| 1260 EXPECT_EQ(url3, most_visited_urls_[3].url); | 1262 EXPECT_EQ(url3, most_visited_urls_[3].url); |
| 1261 EXPECT_EQ(2U, most_visited_urls_[3].redirects.size()); | 1263 EXPECT_EQ(2U, most_visited_urls_[3].redirects.size()); |
| 1262 } | 1264 } |
| 1263 | 1265 |
| 1264 namespace { | 1266 namespace { |
| 1265 | 1267 |
| 1266 // A HistoryDBTask implementation. Each time RunOnDBThread is invoked | 1268 // A HistoryDBTask implementation. Each time RunOnDBThread is invoked |
| 1267 // invoke_count is increment. When invoked kWantInvokeCount times, true is | 1269 // invoke_count is increment. When invoked kWantInvokeCount times, true is |
| 1268 // returned from RunOnDBThread which should stop RunOnDBThread from being | 1270 // returned from RunOnDBThread which should stop RunOnDBThread from being |
| 1269 // invoked again. When DoneRunOnMainThread is invoked, done_invoked is set to | 1271 // invoked again. When DoneRunOnMainThread is invoked, done_invoked is set to |
| 1270 // true. | 1272 // true. |
| 1271 class HistoryDBTaskImpl : public HistoryDBTask { | 1273 class HistoryDBTaskImpl : public HistoryDBTask { |
| 1272 public: | 1274 public: |
| 1273 static const int kWantInvokeCount; | 1275 static const int kWantInvokeCount; |
| 1274 | 1276 |
| 1275 HistoryDBTaskImpl() : invoke_count(0), done_invoked(false) {} | 1277 HistoryDBTaskImpl() : invoke_count(0), done_invoked(false) {} |
| 1276 | 1278 |
| 1277 virtual bool RunOnDBThread(HistoryBackend* backend, | 1279 virtual bool RunOnDBThread(HistoryBackend* backend, |
| 1278 HistoryDatabase* db) OVERRIDE { | 1280 HistoryDatabase* db) OVERRIDE { |
| 1279 return (++invoke_count == kWantInvokeCount); | 1281 return (++invoke_count == kWantInvokeCount); |
| 1280 } | 1282 } |
| 1281 | 1283 |
| 1282 virtual void DoneRunOnMainThread() OVERRIDE { | 1284 virtual void DoneRunOnMainThread() OVERRIDE { |
| 1283 done_invoked = true; | 1285 done_invoked = true; |
| 1284 MessageLoop::current()->Quit(); | 1286 base::MessageLoop::current()->Quit(); |
| 1285 } | 1287 } |
| 1286 | 1288 |
| 1287 int invoke_count; | 1289 int invoke_count; |
| 1288 bool done_invoked; | 1290 bool done_invoked; |
| 1289 | 1291 |
| 1290 private: | 1292 private: |
| 1291 virtual ~HistoryDBTaskImpl() {} | 1293 virtual ~HistoryDBTaskImpl() {} |
| 1292 | 1294 |
| 1293 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); | 1295 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); |
| 1294 }; | 1296 }; |
| 1295 | 1297 |
| 1296 // static | 1298 // static |
| 1297 const int HistoryDBTaskImpl::kWantInvokeCount = 2; | 1299 const int HistoryDBTaskImpl::kWantInvokeCount = 2; |
| 1298 | 1300 |
| 1299 } // namespace | 1301 } // namespace |
| 1300 | 1302 |
| 1301 TEST_F(HistoryTest, HistoryDBTask) { | 1303 TEST_F(HistoryTest, HistoryDBTask) { |
| 1302 ASSERT_TRUE(history_service_.get()); | 1304 ASSERT_TRUE(history_service_.get()); |
| 1303 CancelableRequestConsumerT<int, 0> request_consumer; | 1305 CancelableRequestConsumerT<int, 0> request_consumer; |
| 1304 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl()); | 1306 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl()); |
| 1305 history_service_->ScheduleDBTask(task.get(), &request_consumer); | 1307 history_service_->ScheduleDBTask(task.get(), &request_consumer); |
| 1306 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs, | 1308 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs, |
| 1307 // it will stop the message loop. If the test hangs here, it means | 1309 // it will stop the message loop. If the test hangs here, it means |
| 1308 // DoneRunOnMainThread isn't being invoked correctly. | 1310 // DoneRunOnMainThread isn't being invoked correctly. |
| 1309 MessageLoop::current()->Run(); | 1311 base::MessageLoop::current()->Run(); |
| 1310 CleanupHistoryService(); | 1312 CleanupHistoryService(); |
| 1311 // WARNING: history has now been deleted. | 1313 // WARNING: history has now been deleted. |
| 1312 history_service_.reset(); | 1314 history_service_.reset(); |
| 1313 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, task->invoke_count); | 1315 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, task->invoke_count); |
| 1314 ASSERT_TRUE(task->done_invoked); | 1316 ASSERT_TRUE(task->done_invoked); |
| 1315 } | 1317 } |
| 1316 | 1318 |
| 1317 TEST_F(HistoryTest, HistoryDBTaskCanceled) { | 1319 TEST_F(HistoryTest, HistoryDBTaskCanceled) { |
| 1318 ASSERT_TRUE(history_service_.get()); | 1320 ASSERT_TRUE(history_service_.get()); |
| 1319 CancelableRequestConsumerT<int, 0> request_consumer; | 1321 CancelableRequestConsumerT<int, 0> request_consumer; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1431 // of sync changes. | 1433 // of sync changes. |
| 1432 void CheckDirectiveProcessingResult( | 1434 void CheckDirectiveProcessingResult( |
| 1433 Time timeout, const TestChangeProcessor* change_processor, | 1435 Time timeout, const TestChangeProcessor* change_processor, |
| 1434 uint32 num_changes) { | 1436 uint32 num_changes) { |
| 1435 if (base::Time::Now() > timeout || | 1437 if (base::Time::Now() > timeout || |
| 1436 change_processor->GetChanges().size() >= num_changes) { | 1438 change_processor->GetChanges().size() >= num_changes) { |
| 1437 return; | 1439 return; |
| 1438 } | 1440 } |
| 1439 | 1441 |
| 1440 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 1442 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 1441 MessageLoop::current()->PostTask( | 1443 base::MessageLoop::current()->PostTask( |
| 1442 FROM_HERE, | 1444 FROM_HERE, |
| 1443 base::Bind(&CheckDirectiveProcessingResult, timeout, | 1445 base::Bind(&CheckDirectiveProcessingResult, timeout, |
| 1444 change_processor, num_changes)); | 1446 change_processor, num_changes)); |
| 1445 } | 1447 } |
| 1446 | 1448 |
| 1447 // Create a delete directive for a few specific history entries, | 1449 // Create a delete directive for a few specific history entries, |
| 1448 // including ones that don't exist. The expected entries should be | 1450 // including ones that don't exist. The expected entries should be |
| 1449 // deleted. | 1451 // deleted. |
| 1450 TEST_F(HistoryTest, ProcessGlobalIdDeleteDirective) { | 1452 TEST_F(HistoryTest, ProcessGlobalIdDeleteDirective) { |
| 1451 ASSERT_TRUE(history_service_.get()); | 1453 ASSERT_TRUE(history_service_.get()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1490 EXPECT_FALSE( | 1492 EXPECT_FALSE( |
| 1491 history_service_->MergeDataAndStartSyncing( | 1493 history_service_->MergeDataAndStartSyncing( |
| 1492 syncer::HISTORY_DELETE_DIRECTIVES, | 1494 syncer::HISTORY_DELETE_DIRECTIVES, |
| 1493 directives, | 1495 directives, |
| 1494 scoped_ptr<syncer::SyncChangeProcessor>( | 1496 scoped_ptr<syncer::SyncChangeProcessor>( |
| 1495 new SyncChangeProcessorDelegate(&change_processor)), | 1497 new SyncChangeProcessorDelegate(&change_processor)), |
| 1496 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); | 1498 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); |
| 1497 | 1499 |
| 1498 // Inject a task to check status and keep message loop filled before directive | 1500 // Inject a task to check status and keep message loop filled before directive |
| 1499 // processing finishes. | 1501 // processing finishes. |
| 1500 MessageLoop::current()->PostTask( | 1502 base::MessageLoop::current()->PostTask( |
| 1501 FROM_HERE, | 1503 FROM_HERE, |
| 1502 base::Bind(&CheckDirectiveProcessingResult, | 1504 base::Bind(&CheckDirectiveProcessingResult, |
| 1503 base::Time::Now() + base::TimeDelta::FromSeconds(10), | 1505 base::Time::Now() + base::TimeDelta::FromSeconds(10), |
| 1504 &change_processor, 2)); | 1506 &change_processor, 2)); |
| 1505 MessageLoop::current()->RunUntilIdle(); | 1507 base::MessageLoop::current()->RunUntilIdle(); |
| 1506 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 1508 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
| 1507 ASSERT_EQ(5, query_url_row_.visit_count()); | 1509 ASSERT_EQ(5, query_url_row_.visit_count()); |
| 1508 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), | 1510 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), |
| 1509 query_url_visits_[0].visit_time); | 1511 query_url_visits_[0].visit_time); |
| 1510 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2), | 1512 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2), |
| 1511 query_url_visits_[1].visit_time); | 1513 query_url_visits_[1].visit_time); |
| 1512 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(11), | 1514 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(11), |
| 1513 query_url_visits_[2].visit_time); | 1515 query_url_visits_[2].visit_time); |
| 1514 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(12), | 1516 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(12), |
| 1515 query_url_visits_[3].visit_time); | 1517 query_url_visits_[3].visit_time); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1562 EXPECT_FALSE( | 1564 EXPECT_FALSE( |
| 1563 history_service_->MergeDataAndStartSyncing( | 1565 history_service_->MergeDataAndStartSyncing( |
| 1564 syncer::HISTORY_DELETE_DIRECTIVES, | 1566 syncer::HISTORY_DELETE_DIRECTIVES, |
| 1565 directives, | 1567 directives, |
| 1566 scoped_ptr<syncer::SyncChangeProcessor>( | 1568 scoped_ptr<syncer::SyncChangeProcessor>( |
| 1567 new SyncChangeProcessorDelegate(&change_processor)), | 1569 new SyncChangeProcessorDelegate(&change_processor)), |
| 1568 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); | 1570 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); |
| 1569 | 1571 |
| 1570 // Inject a task to check status and keep message loop filled before | 1572 // Inject a task to check status and keep message loop filled before |
| 1571 // directive processing finishes. | 1573 // directive processing finishes. |
| 1572 MessageLoop::current()->PostTask( | 1574 base::MessageLoop::current()->PostTask( |
| 1573 FROM_HERE, | 1575 FROM_HERE, |
| 1574 base::Bind(&CheckDirectiveProcessingResult, | 1576 base::Bind(&CheckDirectiveProcessingResult, |
| 1575 base::Time::Now() + base::TimeDelta::FromSeconds(10), | 1577 base::Time::Now() + base::TimeDelta::FromSeconds(10), |
| 1576 &change_processor, 2)); | 1578 &change_processor, 2)); |
| 1577 MessageLoop::current()->RunUntilIdle(); | 1579 base::MessageLoop::current()->RunUntilIdle(); |
| 1578 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); | 1580 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); |
| 1579 ASSERT_EQ(3, query_url_row_.visit_count()); | 1581 ASSERT_EQ(3, query_url_row_.visit_count()); |
| 1580 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), | 1582 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), |
| 1581 query_url_visits_[0].visit_time); | 1583 query_url_visits_[0].visit_time); |
| 1582 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6), | 1584 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6), |
| 1583 query_url_visits_[1].visit_time); | 1585 query_url_visits_[1].visit_time); |
| 1584 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7), | 1586 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7), |
| 1585 query_url_visits_[2].visit_time); | 1587 query_url_visits_[2].visit_time); |
| 1586 | 1588 |
| 1587 // Expect two sync changes for deleting processed directives. | 1589 // Expect two sync changes for deleting processed directives. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1656 std::vector<PageUsageData*> results; | 1658 std::vector<PageUsageData*> results; |
| 1657 db_->QuerySegmentUsage(segment_time, 10, &results); | 1659 db_->QuerySegmentUsage(segment_time, 10, &results); |
| 1658 ASSERT_EQ(1u, results.size()); | 1660 ASSERT_EQ(1u, results.size()); |
| 1659 EXPECT_EQ(url, results[0]->GetURL()); | 1661 EXPECT_EQ(url, results[0]->GetURL()); |
| 1660 EXPECT_EQ(segment_id, results[0]->GetID()); | 1662 EXPECT_EQ(segment_id, results[0]->GetID()); |
| 1661 EXPECT_EQ(title, results[0]->GetTitle()); | 1663 EXPECT_EQ(title, results[0]->GetTitle()); |
| 1662 STLDeleteElements(&results); | 1664 STLDeleteElements(&results); |
| 1663 } | 1665 } |
| 1664 | 1666 |
| 1665 } // namespace history | 1667 } // namespace history |
| OLD | NEW |