| 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> | 10 #include <map> |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 // void CompleteDelayedDownload(); | 411 // void CompleteDelayedDownload(); |
| 412 // set_* mutators | 412 // set_* mutators |
| 413 | 413 |
| 414 TEST_F(DownloadItemTest, NotificationAfterUpdate) { | 414 TEST_F(DownloadItemTest, NotificationAfterUpdate) { |
| 415 DownloadItemImpl* item = CreateDownloadItem(); | 415 DownloadItemImpl* item = CreateDownloadItem(); |
| 416 MockDownloadFile* file = | 416 MockDownloadFile* file = |
| 417 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 417 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 418 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 418 ASSERT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 419 TestDownloadItemObserver observer(item); | 419 TestDownloadItemObserver observer(item); |
| 420 | 420 |
| 421 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed); | 421 item->DestinationUpdate(kDownloadChunkSize, kDownloadSpeed, |
| 422 std::vector<DownloadItem::ReceivedSlice>()); |
| 422 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); | 423 ASSERT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 423 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); | 424 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); |
| 424 CleanupItem(item, file, DownloadItem::IN_PROGRESS); | 425 CleanupItem(item, file, DownloadItem::IN_PROGRESS); |
| 425 } | 426 } |
| 426 | 427 |
| 427 TEST_F(DownloadItemTest, NotificationAfterCancel) { | 428 TEST_F(DownloadItemTest, NotificationAfterCancel) { |
| 428 DownloadItemImpl* user_cancel = CreateDownloadItem(); | 429 DownloadItemImpl* user_cancel = CreateDownloadItem(); |
| 429 DownloadItemImplDelegate::DownloadTargetCallback target_callback; | 430 DownloadItemImplDelegate::DownloadTargetCallback target_callback; |
| 430 MockDownloadFile* download_file = | 431 MockDownloadFile* download_file = |
| 431 CallDownloadItemStart(user_cancel, &target_callback); | 432 CallDownloadItemStart(user_cancel, &target_callback); |
| (...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 item->DestinationObserverAsWeakPtr()); | 1223 item->DestinationObserverAsWeakPtr()); |
| 1223 TestDownloadItemObserver observer(item); | 1224 TestDownloadItemObserver observer(item); |
| 1224 | 1225 |
| 1225 EXPECT_EQ(0l, item->CurrentSpeed()); | 1226 EXPECT_EQ(0l, item->CurrentSpeed()); |
| 1226 EXPECT_EQ(0l, item->GetReceivedBytes()); | 1227 EXPECT_EQ(0l, item->GetReceivedBytes()); |
| 1227 EXPECT_EQ(0l, item->GetTotalBytes()); | 1228 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 1228 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); | 1229 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); |
| 1229 item->SetTotalBytes(100l); | 1230 item->SetTotalBytes(100l); |
| 1230 EXPECT_EQ(100l, item->GetTotalBytes()); | 1231 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 1231 | 1232 |
| 1232 as_observer->DestinationUpdate(10, 20); | 1233 std::vector<DownloadItem::ReceivedSlice> received_slices; |
| 1234 received_slices.emplace_back(0, 10); |
| 1235 as_observer->DestinationUpdate(10, 20, received_slices); |
| 1233 EXPECT_EQ(20l, item->CurrentSpeed()); | 1236 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 1234 EXPECT_EQ(10l, item->GetReceivedBytes()); | 1237 EXPECT_EQ(10l, item->GetReceivedBytes()); |
| 1235 EXPECT_EQ(100l, item->GetTotalBytes()); | 1238 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 1239 EXPECT_EQ(received_slices, item->GetReceivedSlices()); |
| 1236 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1240 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 1237 | 1241 |
| 1238 as_observer->DestinationUpdate(200, 20); | 1242 received_slices.emplace_back(200, 100); |
| 1243 as_observer->DestinationUpdate(200, 20, received_slices); |
| 1239 EXPECT_EQ(20l, item->CurrentSpeed()); | 1244 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 1240 EXPECT_EQ(200l, item->GetReceivedBytes()); | 1245 EXPECT_EQ(200l, item->GetReceivedBytes()); |
| 1241 EXPECT_EQ(0l, item->GetTotalBytes()); | 1246 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 1247 EXPECT_EQ(received_slices, item->GetReceivedSlices()); |
| 1242 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1248 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 1243 | 1249 |
| 1244 CleanupItem(item, file, DownloadItem::IN_PROGRESS); | 1250 CleanupItem(item, file, DownloadItem::IN_PROGRESS); |
| 1245 } | 1251 } |
| 1246 | 1252 |
| 1247 TEST_F(DownloadItemTest, DestinationError_NoRestartRequired) { | 1253 TEST_F(DownloadItemTest, DestinationError_NoRestartRequired) { |
| 1248 DownloadItemImpl* item = CreateDownloadItem(); | 1254 DownloadItemImpl* item = CreateDownloadItem(); |
| 1249 MockDownloadFile* download_file = | 1255 MockDownloadFile* download_file = |
| 1250 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1256 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1251 base::WeakPtr<DownloadDestinationObserver> as_observer( | 1257 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1303 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 1309 DoIntermediateRename(item, DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 1304 base::WeakPtr<DownloadDestinationObserver> as_observer( | 1310 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| 1305 item->DestinationObserverAsWeakPtr()); | 1311 item->DestinationObserverAsWeakPtr()); |
| 1306 TestDownloadItemObserver observer(item); | 1312 TestDownloadItemObserver observer(item); |
| 1307 | 1313 |
| 1308 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1314 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1309 EXPECT_EQ("", item->GetHash()); | 1315 EXPECT_EQ("", item->GetHash()); |
| 1310 EXPECT_FALSE(item->AllDataSaved()); | 1316 EXPECT_FALSE(item->AllDataSaved()); |
| 1311 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); | 1317 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); |
| 1312 | 1318 |
| 1313 as_observer->DestinationUpdate(10, 20); | 1319 as_observer->DestinationUpdate( |
| 1320 10, 20, std::vector<DownloadItem::ReceivedSlice>()); |
| 1314 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); | 1321 EXPECT_TRUE(observer.CheckAndResetDownloadUpdated()); |
| 1315 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. | 1322 EXPECT_FALSE(observer.CheckAndResetDownloadUpdated()); // Confirm reset. |
| 1316 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 1323 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
| 1317 EXPECT_EQ("", item->GetHash()); | 1324 EXPECT_EQ("", item->GetHash()); |
| 1318 EXPECT_FALSE(item->AllDataSaved()); | 1325 EXPECT_FALSE(item->AllDataSaved()); |
| 1319 | 1326 |
| 1320 std::unique_ptr<crypto::SecureHash> hash( | 1327 std::unique_ptr<crypto::SecureHash> hash( |
| 1321 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 1328 crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 1322 hash->Update(kTestData1, sizeof(kTestData1)); | 1329 hash->Update(kTestData1, sizeof(kTestData1)); |
| 1323 | 1330 |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1758 // DownloadDestinationObserver. If std::bind was allowed along with | 1765 // DownloadDestinationObserver. If std::bind was allowed along with |
| 1759 // std::placeholders, it is possible to avoid these functions, but currently | 1766 // std::placeholders, it is possible to avoid these functions, but currently |
| 1760 // Chromium doesn't allow using std::bind for good reasons. | 1767 // Chromium doesn't allow using std::bind for good reasons. |
| 1761 void DestinationUpdateInvoker( | 1768 void DestinationUpdateInvoker( |
| 1762 int64_t bytes_so_far, | 1769 int64_t bytes_so_far, |
| 1763 int64_t bytes_per_sec, | 1770 int64_t bytes_per_sec, |
| 1764 base::WeakPtr<DownloadDestinationObserver> observer) { | 1771 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 1765 DVLOG(20) << "DestinationUpdate(bytes_so_far:" << bytes_so_far | 1772 DVLOG(20) << "DestinationUpdate(bytes_so_far:" << bytes_so_far |
| 1766 << ", bytes_per_sec:" << bytes_per_sec | 1773 << ", bytes_per_sec:" << bytes_per_sec |
| 1767 << ") observer:" << !!observer; | 1774 << ") observer:" << !!observer; |
| 1768 if (observer) | 1775 if (observer) { |
| 1769 observer->DestinationUpdate(bytes_so_far, bytes_per_sec); | 1776 observer->DestinationUpdate(bytes_so_far, bytes_per_sec, |
| 1777 std::vector<DownloadItem::ReceivedSlice>()); |
| 1778 } |
| 1770 } | 1779 } |
| 1771 | 1780 |
| 1772 void DestinationErrorInvoker( | 1781 void DestinationErrorInvoker( |
| 1773 DownloadInterruptReason reason, | 1782 DownloadInterruptReason reason, |
| 1774 int64_t bytes_so_far, | 1783 int64_t bytes_so_far, |
| 1775 base::WeakPtr<DownloadDestinationObserver> observer) { | 1784 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 1776 DVLOG(20) << "DestinationError(reason:" | 1785 DVLOG(20) << "DestinationError(reason:" |
| 1777 << DownloadInterruptReasonToString(reason) | 1786 << DownloadInterruptReasonToString(reason) |
| 1778 << ", bytes_so_far:" << bytes_so_far << ") observer:" << !!observer; | 1787 << ", bytes_so_far:" << bytes_so_far << ") observer:" << !!observer; |
| 1779 if (observer) | 1788 if (observer) |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2107 | 2116 |
| 2108 item_->Cancel(true); | 2117 item_->Cancel(true); |
| 2109 RunAllPendingInMessageLoops(); | 2118 RunAllPendingInMessageLoops(); |
| 2110 } | 2119 } |
| 2111 | 2120 |
| 2112 TEST(MockDownloadItem, Compiles) { | 2121 TEST(MockDownloadItem, Compiles) { |
| 2113 MockDownloadItem mock_item; | 2122 MockDownloadItem mock_item; |
| 2114 } | 2123 } |
| 2115 | 2124 |
| 2116 } // namespace content | 2125 } // namespace content |
| OLD | NEW |