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 "content/browser/download/download_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <iterator> | 9 #include <iterator> |
| 10 #include <map> |
| 11 #include <memory> |
10 #include <queue> | 12 #include <queue> |
11 #include <utility> | 13 #include <utility> |
12 | 14 |
13 #include "base/callback.h" | 15 #include "base/callback.h" |
14 #include "base/feature_list.h" | 16 #include "base/feature_list.h" |
| 17 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
17 #include "base/stl_util.h" | |
18 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
19 #include "content/browser/byte_stream.h" | 21 #include "content/browser/byte_stream.h" |
20 #include "content/browser/download/download_create_info.h" | 22 #include "content/browser/download/download_create_info.h" |
21 #include "content/browser/download/download_destination_observer.h" | 23 #include "content/browser/download/download_destination_observer.h" |
22 #include "content/browser/download/download_file_factory.h" | 24 #include "content/browser/download/download_file_factory.h" |
23 #include "content/browser/download/download_item_impl_delegate.h" | 25 #include "content/browser/download/download_item_impl_delegate.h" |
24 #include "content/browser/download/download_request_handle.h" | 26 #include "content/browser/download/download_request_handle.h" |
25 #include "content/browser/download/mock_download_file.h" | 27 #include "content/browser/download/mock_download_file.h" |
26 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/download_interrupt_reasons.h" | 29 #include "content/public/browser/download_interrupt_reasons.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 create_info_.reset(new DownloadCreateInfo()); | 249 create_info_.reset(new DownloadCreateInfo()); |
248 create_info_->save_info = | 250 create_info_->save_info = |
249 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 251 std::unique_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
250 create_info_->save_info->prompt_for_save_location = false; | 252 create_info_->save_info->prompt_for_save_location = false; |
251 create_info_->url_chain.push_back(GURL("http://example.com/download")); | 253 create_info_->url_chain.push_back(GURL("http://example.com/download")); |
252 create_info_->etag = "SomethingToSatisfyResumption"; | 254 create_info_->etag = "SomethingToSatisfyResumption"; |
253 } | 255 } |
254 | 256 |
255 ~DownloadItemTest() { | 257 ~DownloadItemTest() { |
256 RunAllPendingInMessageLoops(); | 258 RunAllPendingInMessageLoops(); |
257 base::STLDeleteElements(&allocated_downloads_); | |
258 } | 259 } |
259 | 260 |
260 DownloadItemImpl* CreateDownloadItemWithCreateInfo( | 261 DownloadItemImpl* CreateDownloadItemWithCreateInfo( |
261 std::unique_ptr<DownloadCreateInfo> info) { | 262 std::unique_ptr<DownloadCreateInfo> info) { |
262 DownloadItemImpl* download = | 263 DownloadItemImpl* download = |
263 new DownloadItemImpl(&delegate_, next_download_id_++, *(info.get()), | 264 new DownloadItemImpl(&delegate_, next_download_id_++, *(info.get()), |
264 net::NetLogWithSource()); | 265 net::NetLogWithSource()); |
265 allocated_downloads_.insert(download); | 266 allocated_downloads_[download] = base::WrapUnique(download); |
266 return download; | 267 return download; |
267 } | 268 } |
268 | 269 |
269 // This class keeps ownership of the created download item; it will | 270 // This class keeps ownership of the created download item; it will |
270 // be torn down at the end of the test unless DestroyDownloadItem is | 271 // be torn down at the end of the test unless DestroyDownloadItem is |
271 // called. | 272 // called. |
272 DownloadItemImpl* CreateDownloadItem() { | 273 DownloadItemImpl* CreateDownloadItem() { |
273 create_info_->download_id = ++next_download_id_; | 274 create_info_->download_id = ++next_download_id_; |
274 DownloadItemImpl* download = | 275 DownloadItemImpl* download = |
275 new DownloadItemImpl(&delegate_, create_info_->download_id, | 276 new DownloadItemImpl(&delegate_, create_info_->download_id, |
276 *create_info_, net::NetLogWithSource()); | 277 *create_info_, net::NetLogWithSource()); |
277 allocated_downloads_.insert(download); | 278 allocated_downloads_[download] = base::WrapUnique(download); |
278 return download; | 279 return download; |
279 } | 280 } |
280 | 281 |
281 // Add DownloadFile to DownloadItem. Set |callback| to nullptr if a download | 282 // Add DownloadFile to DownloadItem. Set |callback| to nullptr if a download |
282 // target determination is not expected. | 283 // target determination is not expected. |
283 MockDownloadFile* CallDownloadItemStart( | 284 MockDownloadFile* CallDownloadItemStart( |
284 DownloadItemImpl* item, | 285 DownloadItemImpl* item, |
285 DownloadItemImplDelegate::DownloadTargetCallback* callback) { | 286 DownloadItemImplDelegate::DownloadTargetCallback* callback) { |
286 MockDownloadFile* mock_download_file = nullptr; | 287 MockDownloadFile* mock_download_file = nullptr; |
287 std::unique_ptr<DownloadFile> download_file; | 288 std::unique_ptr<DownloadFile> download_file; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 if (download_file) | 370 if (download_file) |
370 EXPECT_CALL(*download_file, Cancel()); | 371 EXPECT_CALL(*download_file, Cancel()); |
371 item->Cancel(true); | 372 item->Cancel(true); |
372 RunAllPendingInMessageLoops(); | 373 RunAllPendingInMessageLoops(); |
373 } | 374 } |
374 } | 375 } |
375 | 376 |
376 // Destroy a previously created download item. | 377 // Destroy a previously created download item. |
377 void DestroyDownloadItem(DownloadItem* item) { | 378 void DestroyDownloadItem(DownloadItem* item) { |
378 allocated_downloads_.erase(item); | 379 allocated_downloads_.erase(item); |
379 delete item; | |
380 } | 380 } |
381 | 381 |
382 void RunAllPendingInMessageLoops() { base::RunLoop().RunUntilIdle(); } | 382 void RunAllPendingInMessageLoops() { base::RunLoop().RunUntilIdle(); } |
383 | 383 |
384 MockDelegate* mock_delegate() { | 384 MockDelegate* mock_delegate() { |
385 return &delegate_; | 385 return &delegate_; |
386 } | 386 } |
387 | 387 |
388 void OnDownloadFileAcquired(base::FilePath* return_path, | 388 void OnDownloadFileAcquired(base::FilePath* return_path, |
389 const base::FilePath& path) { | 389 const base::FilePath& path) { |
390 *return_path = path; | 390 *return_path = path; |
391 } | 391 } |
392 | 392 |
393 DownloadCreateInfo* create_info() { return create_info_.get(); } | 393 DownloadCreateInfo* create_info() { return create_info_.get(); } |
394 | 394 |
395 BrowserContext* browser_context() { return &browser_context_; } | 395 BrowserContext* browser_context() { return &browser_context_; } |
396 | 396 |
397 private: | 397 private: |
398 TestBrowserThreadBundle thread_bundle_; | 398 TestBrowserThreadBundle thread_bundle_; |
399 StrictMock<MockDelegate> delegate_; | 399 StrictMock<MockDelegate> delegate_; |
400 std::set<DownloadItem*> allocated_downloads_; | 400 std::map<DownloadItem*, std::unique_ptr<DownloadItem>> allocated_downloads_; |
401 std::unique_ptr<DownloadCreateInfo> create_info_; | 401 std::unique_ptr<DownloadCreateInfo> create_info_; |
402 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1; | 402 uint32_t next_download_id_ = DownloadItem::kInvalidId + 1; |
403 TestBrowserContext browser_context_; | 403 TestBrowserContext browser_context_; |
404 }; | 404 }; |
405 | 405 |
406 // Tests to ensure calls that change a DownloadItem generate an update to | 406 // Tests to ensure calls that change a DownloadItem generate an update to |
407 // observers. | 407 // observers. |
408 // State changing functions not tested: | 408 // State changing functions not tested: |
409 // void OpenDownload(); | 409 // void OpenDownload(); |
410 // void ShowDownloadInShell(); | 410 // void ShowDownloadInShell(); |
(...skipping 1615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2026 | 2026 |
2027 item_->Cancel(true); | 2027 item_->Cancel(true); |
2028 RunAllPendingInMessageLoops(); | 2028 RunAllPendingInMessageLoops(); |
2029 } | 2029 } |
2030 | 2030 |
2031 TEST(MockDownloadItem, Compiles) { | 2031 TEST(MockDownloadItem, Compiles) { |
2032 MockDownloadItem mock_item; | 2032 MockDownloadItem mock_item; |
2033 } | 2033 } |
2034 | 2034 |
2035 } // namespace content | 2035 } // namespace content |
OLD | NEW |