| 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
| 7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
| 8 #include "content/browser/download/byte_stream.h" | 8 #include "content/browser/download/byte_stream.h" |
| 9 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
| 10 #include "content/browser/download/download_file_factory.h" | 10 #include "content/browser/download/download_file_factory.h" |
| 11 #include "content/browser/download/download_item_impl.h" | 11 #include "content/browser/download/download_item_impl.h" |
| 12 #include "content/browser/download/download_item_impl_delegate.h" | 12 #include "content/browser/download/download_item_impl_delegate.h" |
| 13 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 14 #include "content/browser/download/mock_download_file.h" | 14 #include "content/browser/download/mock_download_file.h" |
| 15 #include "content/public/browser/download_id.h" | 15 #include "content/public/browser/download_id.h" |
| 16 #include "content/public/browser/download_destination_observer.h" | 16 #include "content/public/browser/download_destination_observer.h" |
| 17 #include "content/public/browser/download_interrupt_reasons.h" | 17 #include "content/public/browser/download_interrupt_reasons.h" |
| 18 #include "content/public/browser/download_url_parameters.h" |
| 18 #include "content/public/test/mock_download_item.h" | 19 #include "content/public/test/mock_download_item.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using ::testing::_; | 24 using ::testing::_; |
| 24 using ::testing::AllOf; | 25 using ::testing::AllOf; |
| 26 using ::testing::NiceMock; |
| 25 using ::testing::Property; | 27 using ::testing::Property; |
| 26 using ::testing::Return; | 28 using ::testing::Return; |
| 27 using ::testing::SaveArg; | 29 using ::testing::SaveArg; |
| 28 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
| 29 | 31 |
| 30 namespace content { | 32 namespace content { |
| 31 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 33 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| 34 class MockDelegate : public DownloadItemImplDelegate { | 36 class MockDelegate : public DownloadItemImplDelegate { |
| 35 public: | 37 public: |
| 36 MOCK_METHOD2(DetermineDownloadTarget, void( | 38 MOCK_METHOD2(DetermineDownloadTarget, void( |
| 37 DownloadItemImpl*, const DownloadTargetCallback&)); | 39 DownloadItemImpl*, const DownloadTargetCallback&)); |
| 38 MOCK_METHOD2(ShouldOpenDownload, | 40 MOCK_METHOD2(ShouldOpenDownload, |
| 39 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); | 41 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); |
| 40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); | 42 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
| 41 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 43 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
| 44 |
| 45 virtual void ResumeInterruptedDownload( |
| 46 scoped_ptr<DownloadUrlParameters> params, DownloadId id) OVERRIDE { |
| 47 MockResumeInterruptedDownload(params.get(), id); |
| 48 } |
| 49 MOCK_METHOD2(MockResumeInterruptedDownload, |
| 50 void(DownloadUrlParameters* params, DownloadId id)); |
| 51 |
| 42 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 52 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
| 43 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); | 53 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); |
| 44 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 54 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
| 45 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 55 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
| 46 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); | 56 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
| 47 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 57 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
| 48 }; | 58 }; |
| 49 | 59 |
| 50 class MockRequestHandle : public DownloadRequestHandleInterface { | 60 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 51 public: | 61 public: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 70 } | 80 } |
| 71 | 81 |
| 72 } // namespace | 82 } // namespace |
| 73 | 83 |
| 74 class DownloadItemTest : public testing::Test { | 84 class DownloadItemTest : public testing::Test { |
| 75 public: | 85 public: |
| 76 class MockObserver : public DownloadItem::Observer { | 86 class MockObserver : public DownloadItem::Observer { |
| 77 public: | 87 public: |
| 78 explicit MockObserver(DownloadItem* item) | 88 explicit MockObserver(DownloadItem* item) |
| 79 : item_(item), | 89 : item_(item), |
| 90 last_state_(item->GetState()), |
| 80 removed_(false), | 91 removed_(false), |
| 81 destroyed_(false), | 92 destroyed_(false), |
| 82 updated_(false) { | 93 updated_(false), |
| 94 interrupt_count_(0), |
| 95 resume_count_(0) { |
| 83 item_->AddObserver(this); | 96 item_->AddObserver(this); |
| 84 } | 97 } |
| 85 | 98 |
| 86 virtual ~MockObserver() { | 99 virtual ~MockObserver() { |
| 87 if (item_) item_->RemoveObserver(this); | 100 if (item_) item_->RemoveObserver(this); |
| 88 } | 101 } |
| 89 | 102 |
| 90 virtual void OnDownloadRemoved(DownloadItem* download) { | 103 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 104 DVLOG(20) << " " << __FUNCTION__ |
| 105 << " download = " << download->DebugString(false); |
| 91 removed_ = true; | 106 removed_ = true; |
| 92 } | 107 } |
| 93 | 108 |
| 94 virtual void OnDownloadUpdated(DownloadItem* download) { | 109 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 110 DVLOG(20) << " " << __FUNCTION__ |
| 111 << " download = " << download->DebugString(false); |
| 95 updated_ = true; | 112 updated_ = true; |
| 113 DownloadItem::DownloadState new_state = download->GetState(); |
| 114 if (last_state_ == DownloadItem::IN_PROGRESS && |
| 115 new_state == DownloadItem::INTERRUPTED) { |
| 116 interrupt_count_++; |
| 117 } |
| 118 if (last_state_ == DownloadItem::INTERRUPTED && |
| 119 new_state == DownloadItem::IN_PROGRESS) { |
| 120 resume_count_++; |
| 121 } |
| 122 last_state_ = new_state; |
| 96 } | 123 } |
| 97 | 124 |
| 98 virtual void OnDownloadOpened(DownloadItem* download) { | 125 virtual void OnDownloadOpened(DownloadItem* download) { |
| 126 DVLOG(20) << " " << __FUNCTION__ |
| 127 << " download = " << download->DebugString(false); |
| 99 } | 128 } |
| 100 | 129 |
| 101 virtual void OnDownloadDestroyed(DownloadItem* download) { | 130 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 131 DVLOG(20) << " " << __FUNCTION__ |
| 132 << " download = " << download->DebugString(false); |
| 102 destroyed_ = true; | 133 destroyed_ = true; |
| 103 item_->RemoveObserver(this); | 134 item_->RemoveObserver(this); |
| 104 item_ = NULL; | 135 item_ = NULL; |
| 105 } | 136 } |
| 106 | 137 |
| 107 bool CheckRemoved() { | 138 bool CheckRemoved() { |
| 108 return removed_; | 139 return removed_; |
| 109 } | 140 } |
| 110 | 141 |
| 111 bool CheckDestroyed() { | 142 bool CheckDestroyed() { |
| 112 return destroyed_; | 143 return destroyed_; |
| 113 } | 144 } |
| 114 | 145 |
| 115 bool CheckUpdated() { | 146 bool CheckUpdated() { |
| 116 bool was_updated = updated_; | 147 bool was_updated = updated_; |
| 117 updated_ = false; | 148 updated_ = false; |
| 118 return was_updated; | 149 return was_updated; |
| 119 } | 150 } |
| 120 | 151 |
| 152 int CheckInterrupted() { |
| 153 return interrupt_count_; |
| 154 } |
| 155 |
| 156 int CheckResumed() { |
| 157 return resume_count_; |
| 158 } |
| 159 |
| 121 private: | 160 private: |
| 122 DownloadItem* item_; | 161 DownloadItem* item_; |
| 162 DownloadItem::DownloadState last_state_; |
| 123 bool removed_; | 163 bool removed_; |
| 124 bool destroyed_; | 164 bool destroyed_; |
| 125 bool updated_; | 165 bool updated_; |
| 166 int interrupt_count_; |
| 167 int resume_count_; |
| 126 }; | 168 }; |
| 127 | 169 |
| 128 DownloadItemTest() | 170 DownloadItemTest() |
| 129 : ui_thread_(BrowserThread::UI, &loop_), | 171 : ui_thread_(BrowserThread::UI, &loop_), |
| 130 file_thread_(BrowserThread::FILE, &loop_), | 172 file_thread_(BrowserThread::FILE, &loop_), |
| 131 delegate_() { | 173 delegate_() { |
| 132 } | 174 } |
| 133 | 175 |
| 134 ~DownloadItemTest() { | 176 ~DownloadItemTest() { |
| 135 } | 177 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 152 // don't call the function that deletes it, so we do so ourselves. | 194 // don't call the function that deletes it, so we do so ourselves. |
| 153 scoped_ptr<DownloadCreateInfo> info_; | 195 scoped_ptr<DownloadCreateInfo> info_; |
| 154 | 196 |
| 155 info_.reset(new DownloadCreateInfo()); | 197 info_.reset(new DownloadCreateInfo()); |
| 156 static int next_id; | 198 static int next_id; |
| 157 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); | 199 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); |
| 158 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 200 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
| 159 info_->save_info->prompt_for_save_location = false; | 201 info_->save_info->prompt_for_save_location = false; |
| 160 info_->url_chain.push_back(GURL()); | 202 info_->url_chain.push_back(GURL()); |
| 161 | 203 |
| 162 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
| 163 new testing::NiceMock<MockRequestHandle>); | |
| 164 DownloadItemImpl* download = | 204 DownloadItemImpl* download = |
| 165 new DownloadItemImpl(&delegate_, *(info_.get()), | 205 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog()); |
| 166 request_handle.Pass(), net::BoundNetLog()); | |
| 167 allocated_downloads_.insert(download); | 206 allocated_downloads_.insert(download); |
| 168 return download; | 207 return download; |
| 169 } | 208 } |
| 170 | 209 |
| 171 // Add DownloadFile to DownloadItem | 210 // Add DownloadFile to DownloadItem |
| 172 MockDownloadFile* AddDownloadFileToDownloadItem( | 211 MockDownloadFile* AddDownloadFileToDownloadItem( |
| 173 DownloadItemImpl* item, | 212 DownloadItemImpl* item, |
| 174 DownloadItemImplDelegate::DownloadTargetCallback *callback) { | 213 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
| 175 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 214 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
| 176 scoped_ptr<DownloadFile> download_file(mock_download_file); | 215 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 177 EXPECT_CALL(*mock_download_file, Initialize(_)); | 216 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 178 if (callback) { | 217 if (callback) { |
| 179 // Save the callback. | 218 // Save the callback. |
| 180 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 219 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 181 .WillOnce(SaveArg<1>(callback)); | 220 .WillOnce(SaveArg<1>(callback)); |
| 182 } else { | 221 } else { |
| 183 // Drop it on the floor. | 222 // Drop it on the floor. |
| 184 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 223 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 185 } | 224 } |
| 186 | 225 |
| 187 item->Start(download_file.Pass()); | 226 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 227 new NiceMock<MockRequestHandle>); |
| 228 item->Start(download_file.Pass(), request_handle.Pass()); |
| 188 loop_.RunUntilIdle(); | 229 loop_.RunUntilIdle(); |
| 189 | 230 |
| 190 // So that we don't have a function writing to a stack variable | 231 // So that we don't have a function writing to a stack variable |
| 191 // lying around if the above failed. | 232 // lying around if the above failed. |
| 192 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 233 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 193 | 234 |
| 194 return mock_download_file; | 235 return mock_download_file; |
| 195 } | 236 } |
| 196 | 237 |
| 197 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 238 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
| 198 // The item must be in the IN_PROGRESS state. | 239 // The item must be in the expected state. |
| 199 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { | 240 void CleanupItem(DownloadItemImpl* item, |
| 200 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 241 MockDownloadFile* download_file, |
| 242 DownloadItem::DownloadState expected_state) { |
| 243 EXPECT_EQ(expected_state, item->GetState()); |
| 201 | 244 |
| 202 EXPECT_CALL(*download_file, Cancel()); | 245 if (expected_state == DownloadItem::IN_PROGRESS) { |
| 203 item->Cancel(true); | 246 EXPECT_CALL(*download_file, Cancel()); |
| 204 loop_.RunUntilIdle(); | 247 item->Cancel(true); |
| 248 loop_.RunUntilIdle(); |
| 249 } |
| 205 } | 250 } |
| 206 | 251 |
| 207 // Destroy a previously created download item. | 252 // Destroy a previously created download item. |
| 208 void DestroyDownloadItem(DownloadItem* item) { | 253 void DestroyDownloadItem(DownloadItem* item) { |
| 209 allocated_downloads_.erase(item); | 254 allocated_downloads_.erase(item); |
| 210 delete item; | 255 delete item; |
| 211 } | 256 } |
| 212 | 257 |
| 213 void RunAllPendingInMessageLoops() { | 258 void RunAllPendingInMessageLoops() { |
| 214 loop_.RunUntilIdle(); | 259 loop_.RunUntilIdle(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 item->OnDownloadedFileRemoved(); | 335 item->OnDownloadedFileRemoved(); |
| 291 ASSERT_TRUE(observer.CheckUpdated()); | 336 ASSERT_TRUE(observer.CheckUpdated()); |
| 292 } | 337 } |
| 293 | 338 |
| 294 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 339 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
| 295 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 340 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 296 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 341 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 297 EXPECT_CALL(*download_file, Cancel()); | 342 EXPECT_CALL(*download_file, Cancel()); |
| 298 MockObserver observer(item); | 343 MockObserver observer(item); |
| 299 | 344 |
| 345 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) |
| 346 .Times(0); |
| 347 |
| 300 item->DestinationObserverAsWeakPtr()->DestinationError( | 348 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 301 DOWNLOAD_INTERRUPT_REASON_NONE); | 349 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 302 ASSERT_TRUE(observer.CheckUpdated()); | 350 ASSERT_TRUE(observer.CheckUpdated()); |
| 303 } | 351 } |
| 304 | 352 |
| 305 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 353 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 354 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 307 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 355 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 308 EXPECT_CALL(*download_file, Cancel()); | 356 EXPECT_CALL(*download_file, Cancel()); |
| 309 MockObserver observer(item); | 357 MockObserver observer(item); |
| 310 | 358 |
| 311 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 359 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 312 ASSERT_TRUE(observer.CheckUpdated()); | 360 ASSERT_TRUE(observer.CheckUpdated()); |
| 313 } | 361 } |
| 314 | 362 |
| 315 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 363 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 316 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 364 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 317 MockObserver observer(item); | 365 MockObserver observer(item); |
| 318 | 366 |
| 319 DestroyDownloadItem(item); | 367 DestroyDownloadItem(item); |
| 320 ASSERT_TRUE(observer.CheckDestroyed()); | 368 ASSERT_TRUE(observer.CheckDestroyed()); |
| 321 } | 369 } |
| 322 | 370 |
| 371 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
| 372 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 373 MockObserver observer(item); |
| 374 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>); |
| 375 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 376 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 377 new NiceMock<MockRequestHandle>); |
| 378 |
| 379 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 380 item->Start(download_file.Pass(), request_handle.Pass()); |
| 381 |
| 382 EXPECT_CALL(*mock_download_file, Detach()); |
| 383 // Use a continuable interrupt. |
| 384 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 385 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 386 ASSERT_TRUE(observer.CheckUpdated()); |
| 387 // Should attempt to auto-resume. Because we don't have a mock WebContents, |
| 388 // ResumeInterruptedDownload() will abort early, with another interrupt, |
| 389 // which will be ignored. |
| 390 ASSERT_EQ(1, observer.CheckInterrupted()); |
| 391 ASSERT_EQ(0, observer.CheckResumed()); |
| 392 RunAllPendingInMessageLoops(); |
| 393 |
| 394 mock_download_file = new NiceMock<MockDownloadFile>; |
| 395 download_file.reset(mock_download_file); |
| 396 request_handle.reset(new NiceMock<MockRequestHandle>); |
| 397 |
| 398 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 399 item->Start(download_file.Pass(), request_handle.Pass()); |
| 400 ASSERT_EQ(1, observer.CheckResumed()); |
| 401 |
| 402 // Use a non-resumable interrupt. |
| 403 EXPECT_CALL(*mock_download_file, Cancel()); |
| 404 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 405 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 406 ASSERT_TRUE(observer.CheckUpdated()); |
| 407 // Should not try to auto-resume. |
| 408 ASSERT_EQ(2, observer.CheckInterrupted()); |
| 409 ASSERT_EQ(1, observer.CheckResumed()); |
| 410 RunAllPendingInMessageLoops(); |
| 411 |
| 412 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED); |
| 413 } |
| 414 |
| 415 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { |
| 416 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 417 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| 418 item->DestinationObserverAsWeakPtr()); |
| 419 MockObserver observer(item); |
| 420 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>); |
| 421 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 422 MockRequestHandle* mock_request_handle = new NiceMock<MockRequestHandle>; |
| 423 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 424 mock_request_handle); |
| 425 |
| 426 item->Start(download_file.Pass(), request_handle.Pass()); |
| 427 |
| 428 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 2); ++i) { |
| 429 DVLOG(20) << "Loop iteration " << i; |
| 430 |
| 431 // Use a continuable interrupt. |
| 432 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 433 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 434 |
| 435 mock_download_file = new NiceMock<MockDownloadFile>; |
| 436 download_file.reset(mock_download_file); |
| 437 mock_request_handle = new NiceMock<MockRequestHandle>; |
| 438 request_handle.reset(mock_request_handle); |
| 439 |
| 440 // It's too complicated to set up a WebContents instance that would cause |
| 441 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function |
| 442 // to be callled, so we simply verify that GetWebContents() is called. |
| 443 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { |
| 444 EXPECT_CALL(*mock_request_handle, GetWebContents()) |
| 445 .WillOnce(Return(static_cast<WebContents*>(NULL))); |
| 446 } |
| 447 |
| 448 // Should call delegate->ResumeInterruptedDownload() here, up to |
| 449 // kMaxAutoResumeAttempts times. |
| 450 item->Start(download_file.Pass(), request_handle.Pass()); |
| 451 |
| 452 ASSERT_EQ(i + 1, observer.CheckInterrupted()); |
| 453 ASSERT_EQ(i + 1, observer.CheckResumed()); |
| 454 } |
| 455 |
| 456 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 457 } |
| 458 |
| 323 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 459 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 324 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 460 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 325 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 461 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 326 EXPECT_CALL(*download_file, Cancel()); | 462 EXPECT_CALL(*download_file, Cancel()); |
| 327 MockObserver observer(item); | 463 MockObserver observer(item); |
| 328 | 464 |
| 329 item->Remove(); | 465 item->Remove(); |
| 330 ASSERT_TRUE(observer.CheckUpdated()); | 466 ASSERT_TRUE(observer.CheckUpdated()); |
| 331 ASSERT_TRUE(observer.CheckRemoved()); | 467 ASSERT_TRUE(observer.CheckRemoved()); |
| 332 } | 468 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 523 |
| 388 // Currently, a notification would be generated if the danger type is anything | 524 // Currently, a notification would be generated if the danger type is anything |
| 389 // other than NOT_DANGEROUS. | 525 // other than NOT_DANGEROUS. |
| 390 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 526 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 391 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 527 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 392 EXPECT_FALSE(observer.CheckUpdated()); | 528 EXPECT_FALSE(observer.CheckUpdated()); |
| 393 RunAllPendingInMessageLoops(); | 529 RunAllPendingInMessageLoops(); |
| 394 EXPECT_TRUE(observer.CheckUpdated()); | 530 EXPECT_TRUE(observer.CheckUpdated()); |
| 395 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 531 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 396 | 532 |
| 397 CleanupItem(item, download_file); | 533 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 398 } | 534 } |
| 399 | 535 |
| 400 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 536 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
| 401 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 537 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 402 MockObserver observer(item); | 538 MockObserver observer(item); |
| 539 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 540 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 541 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 542 new NiceMock<MockRequestHandle>); |
| 543 |
| 544 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 545 item->Start(download_file.Pass(), request_handle.Pass()); |
| 403 | 546 |
| 404 item->TogglePause(); | 547 item->TogglePause(); |
| 405 ASSERT_TRUE(observer.CheckUpdated()); | 548 ASSERT_TRUE(observer.CheckUpdated()); |
| 406 | 549 |
| 407 item->TogglePause(); | 550 item->TogglePause(); |
| 408 ASSERT_TRUE(observer.CheckUpdated()); | 551 ASSERT_TRUE(observer.CheckUpdated()); |
| 552 RunAllPendingInMessageLoops(); |
| 553 |
| 554 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 409 } | 555 } |
| 410 | 556 |
| 411 TEST_F(DownloadItemTest, DisplayName) { | 557 TEST_F(DownloadItemTest, DisplayName) { |
| 412 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 558 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 413 DownloadItemImplDelegate::DownloadTargetCallback callback; | 559 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 414 MockDownloadFile* download_file = | 560 MockDownloadFile* download_file = |
| 415 AddDownloadFileToDownloadItem(item, &callback); | 561 AddDownloadFileToDownloadItem(item, &callback); |
| 416 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 562 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 417 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 563 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 418 EXPECT_EQ(FILE_PATH_LITERAL(""), | 564 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 419 item->GetFileNameToReportUser().value()); | 565 item->GetFileNameToReportUser().value()); |
| 420 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 566 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
| 421 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 567 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
| 422 intermediate_path)); | 568 intermediate_path)); |
| 423 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 569 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 424 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 570 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 425 RunAllPendingInMessageLoops(); | 571 RunAllPendingInMessageLoops(); |
| 426 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 572 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 427 item->GetFileNameToReportUser().value()); | 573 item->GetFileNameToReportUser().value()); |
| 428 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 574 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 429 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 575 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 430 item->GetFileNameToReportUser().value()); | 576 item->GetFileNameToReportUser().value()); |
| 431 CleanupItem(item, download_file); | 577 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 432 } | 578 } |
| 433 | 579 |
| 434 // Test to make sure that Start method calls DF initialize properly. | 580 // Test to make sure that Start method calls DF initialize properly. |
| 435 TEST_F(DownloadItemTest, Start) { | 581 TEST_F(DownloadItemTest, Start) { |
| 436 MockDownloadFile* mock_download_file(new MockDownloadFile); | 582 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 437 scoped_ptr<DownloadFile> download_file(mock_download_file); | 583 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 438 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 584 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 439 EXPECT_CALL(*mock_download_file, Initialize(_)); | 585 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 440 item->Start(download_file.Pass()); | 586 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 587 new NiceMock<MockRequestHandle>); |
| 588 item->Start(download_file.Pass(), request_handle.Pass()); |
| 441 | 589 |
| 442 CleanupItem(item, mock_download_file); | 590 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 443 } | 591 } |
| 444 | 592 |
| 445 // Test that the delegate is invoked after the download file is renamed. | 593 // Test that the delegate is invoked after the download file is renamed. |
| 446 TEST_F(DownloadItemTest, CallbackAfterRename) { | 594 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 447 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 595 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 448 DownloadItemImplDelegate::DownloadTargetCallback callback; | 596 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 449 MockDownloadFile* download_file = | 597 MockDownloadFile* download_file = |
| 450 AddDownloadFileToDownloadItem(item, &callback); | 598 AddDownloadFileToDownloadItem(item, &callback); |
| 451 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 599 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 452 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 600 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 const DownloadInterruptReason reason( | 657 const DownloadInterruptReason reason( |
| 510 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 658 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 511 | 659 |
| 512 // Confirm interrupt sets state properly. | 660 // Confirm interrupt sets state properly. |
| 513 EXPECT_CALL(*download_file, Cancel()); | 661 EXPECT_CALL(*download_file, Cancel()); |
| 514 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 662 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
| 515 RunAllPendingInMessageLoops(); | 663 RunAllPendingInMessageLoops(); |
| 516 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 664 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 517 EXPECT_EQ(reason, item->GetLastReason()); | 665 EXPECT_EQ(reason, item->GetLastReason()); |
| 518 | 666 |
| 519 // Cancel should result in no change. | 667 // Cancel should kill it. |
| 520 item->Cancel(true); | 668 item->Cancel(true); |
| 521 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 669 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 522 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 670 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
| 523 } | 671 } |
| 524 | 672 |
| 525 TEST_F(DownloadItemTest, Canceled) { | 673 TEST_F(DownloadItemTest, Canceled) { |
| 526 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 674 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 527 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 675 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 528 | 676 |
| 529 // Confirm cancel sets state properly. | 677 // Confirm cancel sets state properly. |
| 530 EXPECT_CALL(*download_file, Cancel()); | 678 EXPECT_CALL(*download_file, Cancel()); |
| 531 item->Cancel(true); | 679 item->Cancel(true); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 617 EXPECT_EQ("livebeef", item->GetHash()); | 765 EXPECT_EQ("livebeef", item->GetHash()); |
| 618 EXPECT_EQ("", item->GetHashState()); | 766 EXPECT_EQ("", item->GetHashState()); |
| 619 EXPECT_TRUE(item->AllDataSaved()); | 767 EXPECT_TRUE(item->AllDataSaved()); |
| 620 } | 768 } |
| 621 | 769 |
| 622 TEST(MockDownloadItem, Compiles) { | 770 TEST(MockDownloadItem, Compiles) { |
| 623 MockDownloadItem mock_item; | 771 MockDownloadItem mock_item; |
| 624 } | 772 } |
| 625 | 773 |
| 626 } // namespace content | 774 } // namespace content |
| OLD | NEW |