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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "chrome/browser/download/download_history.h" | 10 #include "chrome/browser/download/download_history.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 FakeHistoryAdapter() | 68 FakeHistoryAdapter() |
69 : DownloadHistory::HistoryAdapter(NULL), | 69 : DownloadHistory::HistoryAdapter(NULL), |
70 slow_create_download_(false), | 70 slow_create_download_(false), |
71 fail_create_download_(false) { | 71 fail_create_download_(false) { |
72 } | 72 } |
73 | 73 |
74 ~FakeHistoryAdapter() override {} | 74 ~FakeHistoryAdapter() override {} |
75 | 75 |
76 void QueryDownloads( | 76 void QueryDownloads( |
77 const history::HistoryService::DownloadQueryCallback& callback) override { | 77 const history::HistoryService::DownloadQueryCallback& callback) override { |
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
79 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 79 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
80 base::Bind(&FakeHistoryAdapter::QueryDownloadsDone, | 80 base::Bind(&FakeHistoryAdapter::QueryDownloadsDone, |
81 base::Unretained(this), callback)); | 81 base::Unretained(this), callback)); |
82 } | 82 } |
83 | 83 |
84 void QueryDownloadsDone( | 84 void QueryDownloadsDone( |
85 const history::HistoryService::DownloadQueryCallback& callback) { | 85 const history::HistoryService::DownloadQueryCallback& callback) { |
86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
87 CHECK(expect_query_downloads_.get()); | 87 CHECK(expect_query_downloads_.get()); |
88 callback.Run(expect_query_downloads_.Pass()); | 88 callback.Run(expect_query_downloads_.Pass()); |
89 } | 89 } |
90 | 90 |
91 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } | 91 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } |
92 | 92 |
93 void CreateDownload(const history::DownloadRow& info, | 93 void CreateDownload(const history::DownloadRow& info, |
94 const history::HistoryService::DownloadCreateCallback& | 94 const history::HistoryService::DownloadCreateCallback& |
95 callback) override { | 95 callback) override { |
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
97 create_download_info_ = info; | 97 create_download_info_ = info; |
98 // Must not call CreateDownload() again before FinishCreateDownload()! | 98 // Must not call CreateDownload() again before FinishCreateDownload()! |
99 DCHECK(create_download_callback_.is_null()); | 99 DCHECK(create_download_callback_.is_null()); |
100 create_download_callback_ = base::Bind(callback, !fail_create_download_); | 100 create_download_callback_ = base::Bind(callback, !fail_create_download_); |
101 fail_create_download_ = false; | 101 fail_create_download_ = false; |
102 if (!slow_create_download_) | 102 if (!slow_create_download_) |
103 FinishCreateDownload(); | 103 FinishCreateDownload(); |
104 } | 104 } |
105 | 105 |
106 void FinishCreateDownload() { | 106 void FinishCreateDownload() { |
107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
108 create_download_callback_.Run(); | 108 create_download_callback_.Run(); |
109 create_download_callback_.Reset(); | 109 create_download_callback_.Reset(); |
110 } | 110 } |
111 | 111 |
112 void UpdateDownload(const history::DownloadRow& info) override { | 112 void UpdateDownload(const history::DownloadRow& info) override { |
113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
114 update_download_ = info; | 114 update_download_ = info; |
115 } | 115 } |
116 | 116 |
117 void RemoveDownloads(const IdSet& ids) override { | 117 void RemoveDownloads(const IdSet& ids) override { |
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
119 for (IdSet::const_iterator it = ids.begin(); | 119 for (IdSet::const_iterator it = ids.begin(); |
120 it != ids.end(); ++it) { | 120 it != ids.end(); ++it) { |
121 remove_downloads_.insert(*it); | 121 remove_downloads_.insert(*it); |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | 125 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { |
126 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 126 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
127 expect_query_downloads_ = infos.Pass(); | 127 expect_query_downloads_ = infos.Pass(); |
128 } | 128 } |
129 | 129 |
130 void ExpectQueryDownloadsDone() { | 130 void ExpectQueryDownloadsDone() { |
131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
132 EXPECT_TRUE(NULL == expect_query_downloads_.get()); | 132 EXPECT_TRUE(NULL == expect_query_downloads_.get()); |
133 } | 133 } |
134 | 134 |
135 void FailCreateDownload() { | 135 void FailCreateDownload() { |
136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
137 fail_create_download_ = true; | 137 fail_create_download_ = true; |
138 } | 138 } |
139 | 139 |
140 void ExpectDownloadCreated( | 140 void ExpectDownloadCreated( |
141 const history::DownloadRow& info) { | 141 const history::DownloadRow& info) { |
142 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
143 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 143 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
144 CheckInfoEqual(info, create_download_info_); | 144 CheckInfoEqual(info, create_download_info_); |
145 create_download_info_ = history::DownloadRow(); | 145 create_download_info_ = history::DownloadRow(); |
146 } | 146 } |
147 | 147 |
148 void ExpectNoDownloadCreated() { | 148 void ExpectNoDownloadCreated() { |
149 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
150 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 150 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
151 CheckInfoEqual(history::DownloadRow(), create_download_info_); | 151 CheckInfoEqual(history::DownloadRow(), create_download_info_); |
152 } | 152 } |
153 | 153 |
154 void ExpectDownloadUpdated(const history::DownloadRow& info) { | 154 void ExpectDownloadUpdated(const history::DownloadRow& info) { |
155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
156 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 156 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
157 CheckInfoEqual(update_download_, info); | 157 CheckInfoEqual(update_download_, info); |
158 update_download_ = history::DownloadRow(); | 158 update_download_ = history::DownloadRow(); |
159 } | 159 } |
160 | 160 |
161 void ExpectNoDownloadUpdated() { | 161 void ExpectNoDownloadUpdated() { |
162 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 162 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
163 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 163 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
164 CheckInfoEqual(history::DownloadRow(), update_download_); | 164 CheckInfoEqual(history::DownloadRow(), update_download_); |
165 } | 165 } |
166 | 166 |
167 void ExpectNoDownloadsRemoved() { | 167 void ExpectNoDownloadsRemoved() { |
168 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 168 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
169 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 169 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
170 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); | 170 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); |
171 } | 171 } |
172 | 172 |
173 void ExpectDownloadsRemoved(const IdSet& ids) { | 173 void ExpectDownloadsRemoved(const IdSet& ids) { |
174 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
175 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 175 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
176 IdSet differences = base::STLSetDifference<IdSet>(ids, remove_downloads_); | 176 IdSet differences = base::STLSetDifference<IdSet>(ids, remove_downloads_); |
177 for (IdSet::const_iterator different = differences.begin(); | 177 for (IdSet::const_iterator different = differences.begin(); |
178 different != differences.end(); ++different) { | 178 different != differences.end(); ++different) { |
179 EXPECT_TRUE(false) << *different; | 179 EXPECT_TRUE(false) << *different; |
180 } | 180 } |
181 remove_downloads_.clear(); | 181 remove_downloads_.clear(); |
182 } | 182 } |
183 | 183 |
184 private: | 184 private: |
(...skipping 30 matching lines...) Expand all Loading... |
215 | 215 |
216 void SetManagerObserver( | 216 void SetManagerObserver( |
217 content::DownloadManager::Observer* manager_observer) { | 217 content::DownloadManager::Observer* manager_observer) { |
218 manager_observer_ = manager_observer; | 218 manager_observer_ = manager_observer; |
219 } | 219 } |
220 content::DownloadManager::Observer* manager_observer() { | 220 content::DownloadManager::Observer* manager_observer() { |
221 return manager_observer_; | 221 return manager_observer_; |
222 } | 222 } |
223 | 223 |
224 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { | 224 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { |
225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
226 CHECK(infos.get()); | 226 CHECK(infos.get()); |
227 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 227 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
228 this, &DownloadHistoryTest::SetManagerObserver))); | 228 this, &DownloadHistoryTest::SetManagerObserver))); |
229 EXPECT_CALL(manager(), RemoveObserver(_)); | 229 EXPECT_CALL(manager(), RemoveObserver(_)); |
230 download_created_index_ = 0; | 230 download_created_index_ = 0; |
231 for (size_t index = 0; index < infos->size(); ++index) { | 231 for (size_t index = 0; index < infos->size(); ++index) { |
232 const history::DownloadRow& row = infos->at(index); | 232 const history::DownloadRow& row = infos->at(index); |
233 content::MockDownloadManager::CreateDownloadItemAdapter adapter( | 233 content::MockDownloadManager::CreateDownloadItemAdapter adapter( |
234 history::ToContentDownloadId(row.id), | 234 history::ToContentDownloadId(row.id), |
235 row.current_path, | 235 row.current_path, |
(...skipping 22 matching lines...) Expand all Loading... |
258 history_ = new FakeHistoryAdapter(); | 258 history_ = new FakeHistoryAdapter(); |
259 history_->ExpectWillQueryDownloads(infos.Pass()); | 259 history_->ExpectWillQueryDownloads(infos.Pass()); |
260 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | 260 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); |
261 download_history_.reset(new DownloadHistory( | 261 download_history_.reset(new DownloadHistory( |
262 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); | 262 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); |
263 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 263 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
264 history_->ExpectQueryDownloadsDone(); | 264 history_->ExpectQueryDownloadsDone(); |
265 } | 265 } |
266 | 266 |
267 void CallOnDownloadCreated(size_t index) { | 267 void CallOnDownloadCreated(size_t index) { |
268 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 268 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
269 if (!pre_on_create_handler_.is_null()) | 269 if (!pre_on_create_handler_.is_null()) |
270 pre_on_create_handler_.Run(&item(index)); | 270 pre_on_create_handler_.Run(&item(index)); |
271 manager_observer()->OnDownloadCreated(&manager(), &item(index)); | 271 manager_observer()->OnDownloadCreated(&manager(), &item(index)); |
272 if (!post_on_create_handler_.is_null()) | 272 if (!post_on_create_handler_.is_null()) |
273 post_on_create_handler_.Run(&item(index)); | 273 post_on_create_handler_.Run(&item(index)); |
274 } | 274 } |
275 | 275 |
276 void CallOnDownloadCreatedInOrder() { | 276 void CallOnDownloadCreatedInOrder() { |
277 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 277 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
278 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe | 278 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe |
279 // gmock needs to learn about base::Callback. | 279 // gmock needs to learn about base::Callback. |
280 CallOnDownloadCreated(download_created_index_++); | 280 CallOnDownloadCreated(download_created_index_++); |
281 } | 281 } |
282 | 282 |
283 void set_slow_create_download(bool slow) { | 283 void set_slow_create_download(bool slow) { |
284 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 284 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
285 history_->set_slow_create_download(slow); | 285 history_->set_slow_create_download(slow); |
286 } | 286 } |
287 | 287 |
288 void FinishCreateDownload() { | 288 void FinishCreateDownload() { |
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 289 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
290 history_->FinishCreateDownload(); | 290 history_->FinishCreateDownload(); |
291 } | 291 } |
292 | 292 |
293 void FailCreateDownload() { | 293 void FailCreateDownload() { |
294 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 294 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
295 history_->FailCreateDownload(); | 295 history_->FailCreateDownload(); |
296 } | 296 } |
297 | 297 |
298 void ExpectDownloadCreated( | 298 void ExpectDownloadCreated( |
299 const history::DownloadRow& info) { | 299 const history::DownloadRow& info) { |
300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 300 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
301 history_->ExpectDownloadCreated(info); | 301 history_->ExpectDownloadCreated(info); |
302 } | 302 } |
303 | 303 |
304 void ExpectNoDownloadCreated() { | 304 void ExpectNoDownloadCreated() { |
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 305 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
306 history_->ExpectNoDownloadCreated(); | 306 history_->ExpectNoDownloadCreated(); |
307 } | 307 } |
308 | 308 |
309 void ExpectDownloadUpdated(const history::DownloadRow& info) { | 309 void ExpectDownloadUpdated(const history::DownloadRow& info) { |
310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 310 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
311 history_->ExpectDownloadUpdated(info); | 311 history_->ExpectDownloadUpdated(info); |
312 } | 312 } |
313 | 313 |
314 void ExpectNoDownloadUpdated() { | 314 void ExpectNoDownloadUpdated() { |
315 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 315 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
316 history_->ExpectNoDownloadUpdated(); | 316 history_->ExpectNoDownloadUpdated(); |
317 } | 317 } |
318 | 318 |
319 void ExpectNoDownloadsRemoved() { | 319 void ExpectNoDownloadsRemoved() { |
320 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 320 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
321 history_->ExpectNoDownloadsRemoved(); | 321 history_->ExpectNoDownloadsRemoved(); |
322 } | 322 } |
323 | 323 |
324 void ExpectDownloadsRemoved(const IdSet& ids) { | 324 void ExpectDownloadsRemoved(const IdSet& ids) { |
325 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 325 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
326 history_->ExpectDownloadsRemoved(ids); | 326 history_->ExpectDownloadsRemoved(ids); |
327 } | 327 } |
328 | 328 |
329 void ExpectDownloadsRestoredFromHistory(bool expected_value) { | 329 void ExpectDownloadsRestoredFromHistory(bool expected_value) { |
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 330 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
331 pre_on_create_handler_ = | 331 pre_on_create_handler_ = |
332 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, | 332 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, |
333 base::Unretained(this), | 333 base::Unretained(this), |
334 expected_value); | 334 expected_value); |
335 post_on_create_handler_ = | 335 post_on_create_handler_ = |
336 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, | 336 base::Bind(&DownloadHistoryTest::CheckDownloadWasRestoredFromHistory, |
337 base::Unretained(this), | 337 base::Unretained(this), |
338 expected_value); | 338 expected_value); |
339 } | 339 } |
340 | 340 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 const std::string& last_modified, | 382 const std::string& last_modified, |
383 int64 received_bytes, | 383 int64 received_bytes, |
384 int64 total_bytes, | 384 int64 total_bytes, |
385 content::DownloadItem::DownloadState state, | 385 content::DownloadItem::DownloadState state, |
386 content::DownloadDangerType danger_type, | 386 content::DownloadDangerType danger_type, |
387 content::DownloadInterruptReason interrupt_reason, | 387 content::DownloadInterruptReason interrupt_reason, |
388 bool opened, | 388 bool opened, |
389 const std::string& by_extension_id, | 389 const std::string& by_extension_id, |
390 const std::string& by_extension_name, | 390 const std::string& by_extension_name, |
391 history::DownloadRow* info) { | 391 history::DownloadRow* info) { |
392 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 392 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
393 | 393 |
394 size_t index = items_.size(); | 394 size_t index = items_.size(); |
395 StrictMockDownloadItem* mock_item = new StrictMockDownloadItem(); | 395 StrictMockDownloadItem* mock_item = new StrictMockDownloadItem(); |
396 items_.push_back(mock_item); | 396 items_.push_back(mock_item); |
397 | 397 |
398 info->current_path = current_path; | 398 info->current_path = current_path; |
399 info->target_path = target_path; | 399 info->target_path = target_path; |
400 info->url_chain = url_chain; | 400 info->url_chain = url_chain; |
401 info->referrer_url = referrer; | 401 info->referrer_url = referrer; |
402 info->mime_type = mime_type; | 402 info->mime_type = mime_type; |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
861 FinishCreateDownload(); | 861 FinishCreateDownload(); |
862 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 862 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
863 | 863 |
864 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 864 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
865 // changed while it was being added and call UpdateDownload immediately. | 865 // changed while it was being added and call UpdateDownload immediately. |
866 info.opened = true; | 866 info.opened = true; |
867 ExpectDownloadUpdated(info); | 867 ExpectDownloadUpdated(info); |
868 } | 868 } |
869 | 869 |
870 } // anonymous namespace | 870 } // anonymous namespace |
OLD | NEW |