Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(375)

Side by Side Diff: chrome/browser/history/history_unittest.cc

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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_.get()) 716 if (history_service_.get())
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_.get()); 727 DCHECK(history_service_.get());
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
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
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 // The version of the history database should be current in the "typical 1266 // The version of the history database should be current in the "typical
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 1304
1303 HistoryDBTaskImpl() : invoke_count(0), done_invoked(false) {} 1305 HistoryDBTaskImpl() : invoke_count(0), done_invoked(false) {}
1304 1306
1305 virtual bool RunOnDBThread(HistoryBackend* backend, 1307 virtual bool RunOnDBThread(HistoryBackend* backend,
1306 HistoryDatabase* db) OVERRIDE { 1308 HistoryDatabase* db) OVERRIDE {
1307 return (++invoke_count == kWantInvokeCount); 1309 return (++invoke_count == kWantInvokeCount);
1308 } 1310 }
1309 1311
1310 virtual void DoneRunOnMainThread() OVERRIDE { 1312 virtual void DoneRunOnMainThread() OVERRIDE {
1311 done_invoked = true; 1313 done_invoked = true;
1312 MessageLoop::current()->Quit(); 1314 base::MessageLoop::current()->Quit();
1313 } 1315 }
1314 1316
1315 int invoke_count; 1317 int invoke_count;
1316 bool done_invoked; 1318 bool done_invoked;
1317 1319
1318 private: 1320 private:
1319 virtual ~HistoryDBTaskImpl() {} 1321 virtual ~HistoryDBTaskImpl() {}
1320 1322
1321 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); 1323 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl);
1322 }; 1324 };
1323 1325
1324 // static 1326 // static
1325 const int HistoryDBTaskImpl::kWantInvokeCount = 2; 1327 const int HistoryDBTaskImpl::kWantInvokeCount = 2;
1326 1328
1327 } // namespace 1329 } // namespace
1328 1330
1329 TEST_F(HistoryTest, HistoryDBTask) { 1331 TEST_F(HistoryTest, HistoryDBTask) {
1330 ASSERT_TRUE(history_service_.get()); 1332 ASSERT_TRUE(history_service_.get());
1331 CancelableRequestConsumerT<int, 0> request_consumer; 1333 CancelableRequestConsumerT<int, 0> request_consumer;
1332 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl()); 1334 scoped_refptr<HistoryDBTaskImpl> task(new HistoryDBTaskImpl());
1333 history_service_->ScheduleDBTask(task.get(), &request_consumer); 1335 history_service_->ScheduleDBTask(task.get(), &request_consumer);
1334 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs, 1336 // Run the message loop. When HistoryDBTaskImpl::DoneRunOnMainThread runs,
1335 // it will stop the message loop. If the test hangs here, it means 1337 // it will stop the message loop. If the test hangs here, it means
1336 // DoneRunOnMainThread isn't being invoked correctly. 1338 // DoneRunOnMainThread isn't being invoked correctly.
1337 MessageLoop::current()->Run(); 1339 base::MessageLoop::current()->Run();
1338 CleanupHistoryService(); 1340 CleanupHistoryService();
1339 // WARNING: history has now been deleted. 1341 // WARNING: history has now been deleted.
1340 history_service_.reset(); 1342 history_service_.reset();
1341 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, task->invoke_count); 1343 ASSERT_EQ(HistoryDBTaskImpl::kWantInvokeCount, task->invoke_count);
1342 ASSERT_TRUE(task->done_invoked); 1344 ASSERT_TRUE(task->done_invoked);
1343 } 1345 }
1344 1346
1345 TEST_F(HistoryTest, HistoryDBTaskCanceled) { 1347 TEST_F(HistoryTest, HistoryDBTaskCanceled) {
1346 ASSERT_TRUE(history_service_.get()); 1348 ASSERT_TRUE(history_service_.get());
1347 CancelableRequestConsumerT<int, 0> request_consumer; 1349 CancelableRequestConsumerT<int, 0> request_consumer;
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 // of sync changes. 1461 // of sync changes.
1460 void CheckDirectiveProcessingResult( 1462 void CheckDirectiveProcessingResult(
1461 Time timeout, const TestChangeProcessor* change_processor, 1463 Time timeout, const TestChangeProcessor* change_processor,
1462 uint32 num_changes) { 1464 uint32 num_changes) {
1463 if (base::Time::Now() > timeout || 1465 if (base::Time::Now() > timeout ||
1464 change_processor->GetChanges().size() >= num_changes) { 1466 change_processor->GetChanges().size() >= num_changes) {
1465 return; 1467 return;
1466 } 1468 }
1467 1469
1468 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); 1470 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
1469 MessageLoop::current()->PostTask( 1471 base::MessageLoop::current()->PostTask(
1470 FROM_HERE, 1472 FROM_HERE,
1471 base::Bind(&CheckDirectiveProcessingResult, timeout, 1473 base::Bind(&CheckDirectiveProcessingResult,
1472 change_processor, num_changes)); 1474 timeout,
1475 change_processor,
1476 num_changes));
1473 } 1477 }
1474 1478
1475 // Create a delete directive for a few specific history entries, 1479 // Create a delete directive for a few specific history entries,
1476 // including ones that don't exist. The expected entries should be 1480 // including ones that don't exist. The expected entries should be
1477 // deleted. 1481 // deleted.
1478 TEST_F(HistoryTest, ProcessGlobalIdDeleteDirective) { 1482 TEST_F(HistoryTest, ProcessGlobalIdDeleteDirective) {
1479 ASSERT_TRUE(history_service_.get()); 1483 ASSERT_TRUE(history_service_.get());
1480 const GURL test_url("http://www.google.com/"); 1484 const GURL test_url("http://www.google.com/");
1481 for (int64 i = 1; i <= 20; i++) { 1485 for (int64 i = 1; i <= 20; i++) {
1482 base::Time t = 1486 base::Time t =
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1518 EXPECT_FALSE( 1522 EXPECT_FALSE(
1519 history_service_->MergeDataAndStartSyncing( 1523 history_service_->MergeDataAndStartSyncing(
1520 syncer::HISTORY_DELETE_DIRECTIVES, 1524 syncer::HISTORY_DELETE_DIRECTIVES,
1521 directives, 1525 directives,
1522 scoped_ptr<syncer::SyncChangeProcessor>( 1526 scoped_ptr<syncer::SyncChangeProcessor>(
1523 new SyncChangeProcessorDelegate(&change_processor)), 1527 new SyncChangeProcessorDelegate(&change_processor)),
1524 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); 1528 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet());
1525 1529
1526 // Inject a task to check status and keep message loop filled before directive 1530 // Inject a task to check status and keep message loop filled before directive
1527 // processing finishes. 1531 // processing finishes.
1528 MessageLoop::current()->PostTask( 1532 base::MessageLoop::current()->PostTask(
1529 FROM_HERE, 1533 FROM_HERE,
1530 base::Bind(&CheckDirectiveProcessingResult, 1534 base::Bind(&CheckDirectiveProcessingResult,
1531 base::Time::Now() + base::TimeDelta::FromSeconds(10), 1535 base::Time::Now() + base::TimeDelta::FromSeconds(10),
1532 &change_processor, 2)); 1536 &change_processor,
1533 MessageLoop::current()->RunUntilIdle(); 1537 2));
1538 base::MessageLoop::current()->RunUntilIdle();
1534 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); 1539 EXPECT_TRUE(QueryURL(history_service_.get(), test_url));
1535 ASSERT_EQ(5, query_url_row_.visit_count()); 1540 ASSERT_EQ(5, query_url_row_.visit_count());
1536 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), 1541 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1),
1537 query_url_visits_[0].visit_time); 1542 query_url_visits_[0].visit_time);
1538 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2), 1543 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(2),
1539 query_url_visits_[1].visit_time); 1544 query_url_visits_[1].visit_time);
1540 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(11), 1545 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(11),
1541 query_url_visits_[2].visit_time); 1546 query_url_visits_[2].visit_time);
1542 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(12), 1547 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(12),
1543 query_url_visits_[3].visit_time); 1548 query_url_visits_[3].visit_time);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1590 EXPECT_FALSE( 1595 EXPECT_FALSE(
1591 history_service_->MergeDataAndStartSyncing( 1596 history_service_->MergeDataAndStartSyncing(
1592 syncer::HISTORY_DELETE_DIRECTIVES, 1597 syncer::HISTORY_DELETE_DIRECTIVES,
1593 directives, 1598 directives,
1594 scoped_ptr<syncer::SyncChangeProcessor>( 1599 scoped_ptr<syncer::SyncChangeProcessor>(
1595 new SyncChangeProcessorDelegate(&change_processor)), 1600 new SyncChangeProcessorDelegate(&change_processor)),
1596 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet()); 1601 scoped_ptr<syncer::SyncErrorFactory>()).error().IsSet());
1597 1602
1598 // Inject a task to check status and keep message loop filled before 1603 // Inject a task to check status and keep message loop filled before
1599 // directive processing finishes. 1604 // directive processing finishes.
1600 MessageLoop::current()->PostTask( 1605 base::MessageLoop::current()->PostTask(
1601 FROM_HERE, 1606 FROM_HERE,
1602 base::Bind(&CheckDirectiveProcessingResult, 1607 base::Bind(&CheckDirectiveProcessingResult,
1603 base::Time::Now() + base::TimeDelta::FromSeconds(10), 1608 base::Time::Now() + base::TimeDelta::FromSeconds(10),
1604 &change_processor, 2)); 1609 &change_processor,
1605 MessageLoop::current()->RunUntilIdle(); 1610 2));
1611 base::MessageLoop::current()->RunUntilIdle();
1606 EXPECT_TRUE(QueryURL(history_service_.get(), test_url)); 1612 EXPECT_TRUE(QueryURL(history_service_.get(), test_url));
1607 ASSERT_EQ(3, query_url_row_.visit_count()); 1613 ASSERT_EQ(3, query_url_row_.visit_count());
1608 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1), 1614 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(1),
1609 query_url_visits_[0].visit_time); 1615 query_url_visits_[0].visit_time);
1610 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6), 1616 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(6),
1611 query_url_visits_[1].visit_time); 1617 query_url_visits_[1].visit_time);
1612 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7), 1618 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(7),
1613 query_url_visits_[2].visit_time); 1619 query_url_visits_[2].visit_time);
1614 1620
1615 // Expect two sync changes for deleting processed directives. 1621 // Expect two sync changes for deleting processed directives.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1684 std::vector<PageUsageData*> results; 1690 std::vector<PageUsageData*> results;
1685 db_->QuerySegmentUsage(segment_time, 10, &results); 1691 db_->QuerySegmentUsage(segment_time, 10, &results);
1686 ASSERT_EQ(1u, results.size()); 1692 ASSERT_EQ(1u, results.size());
1687 EXPECT_EQ(url, results[0]->GetURL()); 1693 EXPECT_EQ(url, results[0]->GetURL());
1688 EXPECT_EQ(segment_id, results[0]->GetID()); 1694 EXPECT_EQ(segment_id, results[0]->GetID());
1689 EXPECT_EQ(title, results[0]->GetTitle()); 1695 EXPECT_EQ(title, results[0]->GetTitle());
1690 STLDeleteElements(&results); 1696 STLDeleteElements(&results);
1691 } 1697 }
1692 1698
1693 } // namespace history 1699 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698