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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 namespace history { | 46 namespace history { |
47 | 47 |
48 class HistoryServiceTest : public testing::Test { | 48 class HistoryServiceTest : public testing::Test { |
49 public: | 49 public: |
50 HistoryServiceTest() : query_url_success_(false) {} | 50 HistoryServiceTest() : query_url_success_(false) {} |
51 | 51 |
52 ~HistoryServiceTest() override {} | 52 ~HistoryServiceTest() override {} |
53 | 53 |
54 void OnMostVisitedURLsAvailable(const MostVisitedURLList* url_list) { | 54 void OnMostVisitedURLsAvailable(const MostVisitedURLList* url_list) { |
55 most_visited_urls_ = *url_list; | 55 most_visited_urls_ = *url_list; |
56 base::MessageLoop::current()->Quit(); | 56 base::MessageLoop::current()->QuitWhenIdle(); |
57 } | 57 } |
58 | 58 |
59 protected: | 59 protected: |
60 friend class BackendDelegate; | 60 friend class BackendDelegate; |
61 | 61 |
62 // testing::Test | 62 // testing::Test |
63 void SetUp() override { | 63 void SetUp() override { |
64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
65 history_dir_ = temp_dir_.path().AppendASCII("HistoryServiceTest"); | 65 history_dir_ = temp_dir_.path().AppendASCII("HistoryServiceTest"); |
66 ASSERT_TRUE(base::CreateDirectory(history_dir_)); | 66 ASSERT_TRUE(base::CreateDirectory(history_dir_)); |
67 history_service_.reset(new history::HistoryService); | 67 history_service_.reset(new history::HistoryService); |
68 if (!history_service_->Init( | 68 if (!history_service_->Init( |
69 std::string(), TestHistoryDatabaseParamsForPath(history_dir_))) { | 69 std::string(), TestHistoryDatabaseParamsForPath(history_dir_))) { |
70 history_service_.reset(); | 70 history_service_.reset(); |
71 ADD_FAILURE(); | 71 ADD_FAILURE(); |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 void TearDown() override { | 75 void TearDown() override { |
76 if (history_service_) | 76 if (history_service_) |
77 CleanupHistoryService(); | 77 CleanupHistoryService(); |
78 | 78 |
79 // Make sure we don't have any event pending that could disrupt the next | 79 // Make sure we don't have any event pending that could disrupt the next |
80 // test. | 80 // test. |
81 base::ThreadTaskRunnerHandle::Get()->PostTask( | 81 base::ThreadTaskRunnerHandle::Get()->PostTask( |
82 FROM_HERE, base::MessageLoop::QuitClosure()); | 82 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
83 base::MessageLoop::current()->Run(); | 83 base::MessageLoop::current()->Run(); |
84 } | 84 } |
85 | 85 |
86 void CleanupHistoryService() { | 86 void CleanupHistoryService() { |
87 DCHECK(history_service_); | 87 DCHECK(history_service_); |
88 | 88 |
89 history_service_->ClearCachedDataForContextID(0); | 89 history_service_->ClearCachedDataForContextID(0); |
90 history_service_->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure()); | 90 history_service_->SetOnBackendDestroyTask( |
| 91 base::MessageLoop::QuitWhenIdleClosure()); |
91 history_service_->Cleanup(); | 92 history_service_->Cleanup(); |
92 history_service_.reset(); | 93 history_service_.reset(); |
93 | 94 |
94 // Wait for the backend class to terminate before deleting the files and | 95 // Wait for the backend class to terminate before deleting the files and |
95 // moving to the next test. Note: if this never terminates, somebody is | 96 // moving to the next test. Note: if this never terminates, somebody is |
96 // probably leaking a reference to the history backend, so it never calls | 97 // probably leaking a reference to the history backend, so it never calls |
97 // our destroy task. | 98 // our destroy task. |
98 base::MessageLoop::current()->Run(); | 99 base::MessageLoop::current()->Run(); |
99 } | 100 } |
100 | 101 |
(...skipping 15 matching lines...) Expand all Loading... |
116 const URLRow& url_row, | 117 const URLRow& url_row, |
117 const VisitVector& visits) { | 118 const VisitVector& visits) { |
118 query_url_success_ = success; | 119 query_url_success_ = success; |
119 if (query_url_success_) { | 120 if (query_url_success_) { |
120 query_url_row_ = url_row; | 121 query_url_row_ = url_row; |
121 query_url_visits_ = visits; | 122 query_url_visits_ = visits; |
122 } else { | 123 } else { |
123 query_url_row_ = URLRow(); | 124 query_url_row_ = URLRow(); |
124 query_url_visits_.clear(); | 125 query_url_visits_.clear(); |
125 } | 126 } |
126 base::MessageLoop::current()->Quit(); | 127 base::MessageLoop::current()->QuitWhenIdle(); |
127 } | 128 } |
128 | 129 |
129 // Fills in saved_redirects_ with the redirect information for the given URL, | 130 // Fills in saved_redirects_ with the redirect information for the given URL, |
130 // returning true on success. False means the URL was not found. | 131 // returning true on success. False means the URL was not found. |
131 void QueryRedirectsFrom(history::HistoryService* history, const GURL& url) { | 132 void QueryRedirectsFrom(history::HistoryService* history, const GURL& url) { |
132 history_service_->QueryRedirectsFrom( | 133 history_service_->QueryRedirectsFrom( |
133 url, | 134 url, |
134 base::Bind(&HistoryServiceTest::OnRedirectQueryComplete, | 135 base::Bind(&HistoryServiceTest::OnRedirectQueryComplete, |
135 base::Unretained(this)), | 136 base::Unretained(this)), |
136 &tracker_); | 137 &tracker_); |
137 base::MessageLoop::current()->Run(); // Will be exited in *QueryComplete. | 138 base::MessageLoop::current()->Run(); // Will be exited in *QueryComplete. |
138 } | 139 } |
139 | 140 |
140 // Callback for QueryRedirects. | 141 // Callback for QueryRedirects. |
141 void OnRedirectQueryComplete(const history::RedirectList* redirects) { | 142 void OnRedirectQueryComplete(const history::RedirectList* redirects) { |
142 saved_redirects_.clear(); | 143 saved_redirects_.clear(); |
143 if (!redirects->empty()) { | 144 if (!redirects->empty()) { |
144 saved_redirects_.insert( | 145 saved_redirects_.insert( |
145 saved_redirects_.end(), redirects->begin(), redirects->end()); | 146 saved_redirects_.end(), redirects->begin(), redirects->end()); |
146 } | 147 } |
147 base::MessageLoop::current()->Quit(); | 148 base::MessageLoop::current()->QuitWhenIdle(); |
148 } | 149 } |
149 | 150 |
150 base::ScopedTempDir temp_dir_; | 151 base::ScopedTempDir temp_dir_; |
151 | 152 |
152 base::MessageLoopForUI message_loop_; | 153 base::MessageLoopForUI message_loop_; |
153 | 154 |
154 MostVisitedURLList most_visited_urls_; | 155 MostVisitedURLList most_visited_urls_; |
155 | 156 |
156 // When non-NULL, this will be deleted on tear down and we will block until | 157 // When non-NULL, this will be deleted on tear down and we will block until |
157 // the backend thread has completed. This allows tests for the history | 158 // the backend thread has completed. This allows tests for the history |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 561 |
561 HistoryDBTaskImpl(int* invoke_count, bool* done_invoked) | 562 HistoryDBTaskImpl(int* invoke_count, bool* done_invoked) |
562 : invoke_count_(invoke_count), done_invoked_(done_invoked) {} | 563 : invoke_count_(invoke_count), done_invoked_(done_invoked) {} |
563 | 564 |
564 bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override { | 565 bool RunOnDBThread(HistoryBackend* backend, HistoryDatabase* db) override { |
565 return (++*invoke_count_ == kWantInvokeCount); | 566 return (++*invoke_count_ == kWantInvokeCount); |
566 } | 567 } |
567 | 568 |
568 void DoneRunOnMainThread() override { | 569 void DoneRunOnMainThread() override { |
569 *done_invoked_ = true; | 570 *done_invoked_ = true; |
570 base::MessageLoop::current()->Quit(); | 571 base::MessageLoop::current()->QuitWhenIdle(); |
571 } | 572 } |
572 | 573 |
573 int* invoke_count_; | 574 int* invoke_count_; |
574 bool* done_invoked_; | 575 bool* done_invoked_; |
575 | 576 |
576 private: | 577 private: |
577 ~HistoryDBTaskImpl() override {} | 578 ~HistoryDBTaskImpl() override {} |
578 | 579 |
579 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); | 580 DISALLOW_COPY_AND_ASSIGN(HistoryDBTaskImpl); |
580 }; | 581 }; |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 // Expect two sync changes for deleting processed directives. | 854 // Expect two sync changes for deleting processed directives. |
854 const syncer::SyncChangeList& sync_changes = change_processor.changes(); | 855 const syncer::SyncChangeList& sync_changes = change_processor.changes(); |
855 ASSERT_EQ(2u, sync_changes.size()); | 856 ASSERT_EQ(2u, sync_changes.size()); |
856 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); | 857 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[0].change_type()); |
857 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); | 858 EXPECT_EQ(1, syncer::SyncDataRemote(sync_changes[0].sync_data()).GetId()); |
858 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); | 859 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, sync_changes[1].change_type()); |
859 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); | 860 EXPECT_EQ(2, syncer::SyncDataRemote(sync_changes[1].sync_data()).GetId()); |
860 } | 861 } |
861 | 862 |
862 } // namespace history | 863 } // namespace history |
OLD | NEW |