| 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(DownloadStopped, void(DownloadItemImpl*)); | 54 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl*)); |
| 45 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl*)); | 55 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl*)); |
| 46 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 56 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
| 47 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 57 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
| 48 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); | 58 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
| 49 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 59 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
| 50 }; | 60 }; |
| 51 | 61 |
| 52 class MockRequestHandle : public DownloadRequestHandleInterface { | 62 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 53 public: | 63 public: |
| 54 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 64 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
| 55 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 65 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 56 MOCK_CONST_METHOD0(PauseRequest, void()); | 66 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 57 MOCK_CONST_METHOD0(ResumeRequest, void()); | 67 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 58 MOCK_CONST_METHOD0(CancelRequest, void()); | 68 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 69 MOCK_METHOD1(SetRequestId, void(int new_request_id)); |
| 70 MOCK_CONST_METHOD0(RequestId, int()); |
| 59 MOCK_CONST_METHOD0(DebugString, std::string()); | 71 MOCK_CONST_METHOD0(DebugString, std::string()); |
| 60 }; | 72 }; |
| 61 | 73 |
| 62 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on | 74 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on |
| 63 // the UI thread. Should only be used as the action for | 75 // the UI thread. Should only be used as the action for |
| 64 // MockDownloadFile::Rename as follows: | 76 // MockDownloadFile::Rename as follows: |
| 65 // EXPECT_CALL(download_file, Rename*(_,_)) | 77 // EXPECT_CALL(download_file, Rename*(_,_)) |
| 66 // .WillOnce(ScheduleRenameCallback(new_path)); | 78 // .WillOnce(ScheduleRenameCallback(new_path)); |
| 67 ACTION_P(ScheduleRenameCallback, new_path) { | 79 ACTION_P(ScheduleRenameCallback, new_path) { |
| 68 BrowserThread::PostTask( | 80 BrowserThread::PostTask( |
| 69 BrowserThread::UI, FROM_HERE, | 81 BrowserThread::UI, FROM_HERE, |
| 70 base::Bind(arg1, DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); | 82 base::Bind(arg1, DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); |
| 71 } | 83 } |
| 72 | 84 |
| 73 } // namespace | 85 } // namespace |
| 74 | 86 |
| 75 class DownloadItemTest : public testing::Test { | 87 class DownloadItemTest : public testing::Test { |
| 76 public: | 88 public: |
| 77 class MockObserver : public DownloadItem::Observer { | 89 class MockObserver : public DownloadItem::Observer { |
| 78 public: | 90 public: |
| 79 explicit MockObserver(DownloadItem* item) | 91 explicit MockObserver(DownloadItem* item) |
| 80 : item_(item), | 92 : item_(item), |
| 81 removed_(false), | 93 removed_(false), |
| 82 destroyed_(false), | 94 destroyed_(false), |
| 83 updated_(false) { | 95 updated_(false), |
| 96 interrupt_count_(0), |
| 97 resume_count_(0) { |
| 84 item_->AddObserver(this); | 98 item_->AddObserver(this); |
| 85 } | 99 } |
| 86 | 100 |
| 87 virtual ~MockObserver() { | 101 virtual ~MockObserver() { |
| 88 if (item_) item_->RemoveObserver(this); | 102 if (item_) item_->RemoveObserver(this); |
| 89 } | 103 } |
| 90 | 104 |
| 91 virtual void OnDownloadRemoved(DownloadItem* download) { | 105 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 106 DVLOG(20) << " " << __FUNCTION__ |
| 107 << " download = " << download->DebugString(false); |
| 92 removed_ = true; | 108 removed_ = true; |
| 93 } | 109 } |
| 94 | 110 |
| 95 virtual void OnDownloadUpdated(DownloadItem* download) { | 111 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 112 DVLOG(20) << " " << __FUNCTION__ |
| 113 << " download = " << download->DebugString(false); |
| 96 updated_ = true; | 114 updated_ = true; |
| 97 } | 115 } |
| 98 | 116 |
| 99 virtual void OnDownloadOpened(DownloadItem* download) { | 117 virtual void OnDownloadOpened(DownloadItem* download) { |
| 118 DVLOG(20) << " " << __FUNCTION__ |
| 119 << " download = " << download->DebugString(false); |
| 100 } | 120 } |
| 101 | 121 |
| 102 virtual void OnDownloadDestroyed(DownloadItem* download) { | 122 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 123 DVLOG(20) << " " << __FUNCTION__ |
| 124 << " download = " << download->DebugString(false); |
| 103 destroyed_ = true; | 125 destroyed_ = true; |
| 104 item_->RemoveObserver(this); | 126 item_->RemoveObserver(this); |
| 105 item_ = NULL; | 127 item_ = NULL; |
| 106 } | 128 } |
| 107 | 129 |
| 130 virtual void OnDownloadInterrupted(DownloadItem* download) { |
| 131 DVLOG(20) << " " << __FUNCTION__ |
| 132 << " download = " << download->DebugString(false); |
| 133 interrupt_count_++; |
| 134 } |
| 135 |
| 136 virtual void OnDownloadResumed(DownloadItem* download) { |
| 137 DVLOG(20) << " " << __FUNCTION__ |
| 138 << " download = " << download->DebugString(false); |
| 139 resume_count_++; |
| 140 } |
| 141 |
| 108 bool CheckRemoved() { | 142 bool CheckRemoved() { |
| 109 return removed_; | 143 return removed_; |
| 110 } | 144 } |
| 111 | 145 |
| 112 bool CheckDestroyed() { | 146 bool CheckDestroyed() { |
| 113 return destroyed_; | 147 return destroyed_; |
| 114 } | 148 } |
| 115 | 149 |
| 116 bool CheckUpdated() { | 150 bool CheckUpdated() { |
| 117 bool was_updated = updated_; | 151 bool was_updated = updated_; |
| 118 updated_ = false; | 152 updated_ = false; |
| 119 return was_updated; | 153 return was_updated; |
| 120 } | 154 } |
| 121 | 155 |
| 156 int CheckInterrupted() { |
| 157 return interrupt_count_; |
| 158 } |
| 159 |
| 160 int CheckResumed() { |
| 161 return resume_count_; |
| 162 } |
| 163 |
| 122 private: | 164 private: |
| 123 DownloadItem* item_; | 165 DownloadItem* item_; |
| 124 bool removed_; | 166 bool removed_; |
| 125 bool destroyed_; | 167 bool destroyed_; |
| 126 bool updated_; | 168 bool updated_; |
| 169 int interrupt_count_; |
| 170 int resume_count_; |
| 127 }; | 171 }; |
| 128 | 172 |
| 129 DownloadItemTest() | 173 DownloadItemTest() |
| 130 : ui_thread_(BrowserThread::UI, &loop_), | 174 : ui_thread_(BrowserThread::UI, &loop_), |
| 131 file_thread_(BrowserThread::FILE, &loop_), | 175 file_thread_(BrowserThread::FILE, &loop_), |
| 132 delegate_() { | 176 delegate_() { |
| 133 } | 177 } |
| 134 | 178 |
| 135 ~DownloadItemTest() { | 179 ~DownloadItemTest() { |
| 136 } | 180 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 153 // don't call the function that deletes it, so we do so ourselves. | 197 // don't call the function that deletes it, so we do so ourselves. |
| 154 scoped_ptr<DownloadCreateInfo> info_; | 198 scoped_ptr<DownloadCreateInfo> info_; |
| 155 | 199 |
| 156 info_.reset(new DownloadCreateInfo()); | 200 info_.reset(new DownloadCreateInfo()); |
| 157 static int next_id; | 201 static int next_id; |
| 158 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); | 202 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); |
| 159 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 203 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
| 160 info_->save_info->prompt_for_save_location = false; | 204 info_->save_info->prompt_for_save_location = false; |
| 161 info_->url_chain.push_back(GURL()); | 205 info_->url_chain.push_back(GURL()); |
| 162 | 206 |
| 163 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
| 164 new testing::NiceMock<MockRequestHandle>); | |
| 165 DownloadItemImpl* download = | 207 DownloadItemImpl* download = |
| 166 new DownloadItemImpl(&delegate_, *(info_.get()), | 208 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog()); |
| 167 request_handle.Pass(), net::BoundNetLog()); | |
| 168 allocated_downloads_.insert(download); | 209 allocated_downloads_.insert(download); |
| 169 return download; | 210 return download; |
| 170 } | 211 } |
| 171 | 212 |
| 172 // Add DownloadFile to DownloadItem | 213 // Add DownloadFile to DownloadItem |
| 173 MockDownloadFile* AddDownloadFileToDownloadItem( | 214 MockDownloadFile* AddDownloadFileToDownloadItem( |
| 174 DownloadItemImpl* item, | 215 DownloadItemImpl* item, |
| 175 DownloadItemImplDelegate::DownloadTargetCallback *callback) { | 216 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
| 176 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 217 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
| 177 scoped_ptr<DownloadFile> download_file(mock_download_file); | 218 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 178 EXPECT_CALL(*mock_download_file, Initialize(_)); | 219 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 179 if (callback) { | 220 if (callback) { |
| 180 // Save the callback. | 221 // Save the callback. |
| 181 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 222 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 182 .WillOnce(SaveArg<1>(callback)); | 223 .WillOnce(SaveArg<1>(callback)); |
| 183 } else { | 224 } else { |
| 184 // Drop it on the floor. | 225 // Drop it on the floor. |
| 185 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 226 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 186 } | 227 } |
| 187 | 228 |
| 188 item->Start(download_file.Pass()); | 229 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 230 new NiceMock<MockRequestHandle>); |
| 231 item->Start(download_file.Pass(), request_handle.Pass()); |
| 189 loop_.RunUntilIdle(); | 232 loop_.RunUntilIdle(); |
| 190 | 233 |
| 191 // So that we don't have a function writing to a stack variable | 234 // So that we don't have a function writing to a stack variable |
| 192 // lying around if the above failed. | 235 // lying around if the above failed. |
| 193 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 236 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 194 | 237 |
| 195 return mock_download_file; | 238 return mock_download_file; |
| 196 } | 239 } |
| 197 | 240 |
| 198 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 241 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
| 199 // The item must be in the IN_PROGRESS state. | 242 // The item must be in the expected state. |
| 200 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { | 243 void CleanupItem(DownloadItemImpl* item, |
| 201 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 244 MockDownloadFile* download_file, |
| 245 DownloadItem::DownloadState expected_state) { |
| 246 EXPECT_EQ(expected_state, item->GetState()); |
| 202 | 247 |
| 203 EXPECT_CALL(*download_file, Cancel()); | 248 EXPECT_CALL(*download_file, Cancel()); |
| 204 EXPECT_CALL(delegate_, DownloadStopped(item)); | 249 EXPECT_CALL(delegate_, DownloadStopped(item)); |
| 205 item->Cancel(true); | 250 item->Cancel(true); |
| 206 loop_.RunUntilIdle(); | 251 loop_.RunUntilIdle(); |
| 207 } | 252 } |
| 208 | 253 |
| 209 // Destroy a previously created download item. | 254 // Destroy a previously created download item. |
| 210 void DestroyDownloadItem(DownloadItem* item) { | 255 void DestroyDownloadItem(DownloadItem* item) { |
| 211 allocated_downloads_.erase(item); | 256 allocated_downloads_.erase(item); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 item->OnDownloadedFileRemoved(); | 337 item->OnDownloadedFileRemoved(); |
| 293 ASSERT_TRUE(observer.CheckUpdated()); | 338 ASSERT_TRUE(observer.CheckUpdated()); |
| 294 } | 339 } |
| 295 | 340 |
| 296 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 341 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
| 297 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 298 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 343 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 299 EXPECT_CALL(*download_file, Cancel()); | 344 EXPECT_CALL(*download_file, Cancel()); |
| 300 MockObserver observer(item); | 345 MockObserver observer(item); |
| 301 | 346 |
| 347 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) |
| 348 .Times(0); |
| 349 |
| 302 item->DestinationObserverAsWeakPtr()->DestinationError( | 350 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 303 DOWNLOAD_INTERRUPT_REASON_NONE); | 351 DOWNLOAD_INTERRUPT_REASON_NONE); |
| 304 ASSERT_TRUE(observer.CheckUpdated()); | 352 ASSERT_TRUE(observer.CheckUpdated()); |
| 305 } | 353 } |
| 306 | 354 |
| 307 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 355 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 308 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 356 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 309 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 357 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 310 EXPECT_CALL(*download_file, Cancel()); | 358 EXPECT_CALL(*download_file, Cancel()); |
| 311 MockObserver observer(item); | 359 MockObserver observer(item); |
| 312 | 360 |
| 313 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 361 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 314 ASSERT_TRUE(observer.CheckUpdated()); | 362 ASSERT_TRUE(observer.CheckUpdated()); |
| 315 } | 363 } |
| 316 | 364 |
| 317 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 365 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 318 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 366 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 319 MockObserver observer(item); | 367 MockObserver observer(item); |
| 320 | 368 |
| 321 DestroyDownloadItem(item); | 369 DestroyDownloadItem(item); |
| 322 ASSERT_TRUE(observer.CheckDestroyed()); | 370 ASSERT_TRUE(observer.CheckDestroyed()); |
| 323 } | 371 } |
| 324 | 372 |
| 373 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
| 374 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 375 MockObserver observer(item); |
| 376 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>); |
| 377 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 378 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 379 new NiceMock<MockRequestHandle>); |
| 380 |
| 381 item->Start(download_file.Pass(), request_handle.Pass()); |
| 382 |
| 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 |
| 393 mock_download_file = new NiceMock<MockDownloadFile>; |
| 394 download_file.reset(new MockDownloadFile); |
| 395 request_handle.reset(new NiceMock<MockRequestHandle>); |
| 396 |
| 397 item->Start(download_file.Pass(), request_handle.Pass()); |
| 398 ASSERT_EQ(1, observer.CheckResumed()); |
| 399 |
| 400 // Use a non-resumable interrupt. |
| 401 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 402 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 403 ASSERT_TRUE(observer.CheckUpdated()); |
| 404 // Should not try to auto-resume. |
| 405 ASSERT_EQ(2, observer.CheckInterrupted()); |
| 406 ASSERT_EQ(1, observer.CheckResumed()); |
| 407 |
| 408 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED); |
| 409 } |
| 410 |
| 411 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { |
| 412 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 413 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| 414 item->DestinationObserverAsWeakPtr()); |
| 415 MockObserver observer(item); |
| 416 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>); |
| 417 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 418 MockRequestHandle* mock_request_handle = new NiceMock<MockRequestHandle>; |
| 419 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 420 mock_request_handle); |
| 421 |
| 422 item->Start(download_file.Pass(), request_handle.Pass()); |
| 423 |
| 424 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 2); ++i) { |
| 425 DVLOG(20) << "Loop iteration " << i; |
| 426 |
| 427 // Use a continuable interrupt. |
| 428 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 429 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 430 |
| 431 mock_download_file = new NiceMock<MockDownloadFile>; |
| 432 download_file.reset(mock_download_file); |
| 433 mock_request_handle = new NiceMock<MockRequestHandle>; |
| 434 request_handle.reset(mock_request_handle); |
| 435 |
| 436 // It's too complicated to set up a WebContents instance that would cause |
| 437 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function |
| 438 // to be callled, so we simply verify that GetWebContents() is called. |
| 439 EXPECT_CALL(*mock_request_handle, GetWebContents()) |
| 440 .Times((i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) ? 1 : 0) |
| 441 .WillRepeatedly(Return(static_cast<WebContents*>(NULL))); |
| 442 |
| 443 // Should call delegate->ResumeInterruptedDownload() here, up to |
| 444 // kMaxAutoResumeAttempts times. |
| 445 item->Start(download_file.Pass(), request_handle.Pass()); |
| 446 |
| 447 ASSERT_EQ(i + 1, observer.CheckInterrupted()); |
| 448 ASSERT_EQ(i + 1, observer.CheckResumed()); |
| 449 } |
| 450 |
| 451 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 452 } |
| 453 |
| 325 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 454 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 326 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 455 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 327 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 456 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 328 EXPECT_CALL(*download_file, Cancel()); | 457 EXPECT_CALL(*download_file, Cancel()); |
| 329 MockObserver observer(item); | 458 MockObserver observer(item); |
| 330 | 459 |
| 331 item->Remove(); | 460 item->Remove(); |
| 332 ASSERT_TRUE(observer.CheckUpdated()); | 461 ASSERT_TRUE(observer.CheckUpdated()); |
| 333 ASSERT_TRUE(observer.CheckRemoved()); | 462 ASSERT_TRUE(observer.CheckRemoved()); |
| 334 } | 463 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 | 517 |
| 389 // Currently, a notification would be generated if the danger type is anything | 518 // Currently, a notification would be generated if the danger type is anything |
| 390 // other than NOT_DANGEROUS. | 519 // other than NOT_DANGEROUS. |
| 391 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 520 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 392 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 521 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 393 EXPECT_FALSE(observer.CheckUpdated()); | 522 EXPECT_FALSE(observer.CheckUpdated()); |
| 394 RunAllPendingInMessageLoops(); | 523 RunAllPendingInMessageLoops(); |
| 395 EXPECT_TRUE(observer.CheckUpdated()); | 524 EXPECT_TRUE(observer.CheckUpdated()); |
| 396 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 525 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 397 | 526 |
| 398 CleanupItem(item, download_file); | 527 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 399 } | 528 } |
| 400 | 529 |
| 401 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 530 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
| 402 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 531 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 403 MockObserver observer(item); | 532 MockObserver observer(item); |
| 533 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 534 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 535 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 536 new NiceMock<MockRequestHandle>); |
| 537 |
| 538 item->Start(download_file.Pass(), request_handle.Pass()); |
| 404 | 539 |
| 405 item->TogglePause(); | 540 item->TogglePause(); |
| 406 ASSERT_TRUE(observer.CheckUpdated()); | 541 ASSERT_TRUE(observer.CheckUpdated()); |
| 407 | 542 |
| 408 item->TogglePause(); | 543 item->TogglePause(); |
| 409 ASSERT_TRUE(observer.CheckUpdated()); | 544 ASSERT_TRUE(observer.CheckUpdated()); |
| 545 |
| 546 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 410 } | 547 } |
| 411 | 548 |
| 412 TEST_F(DownloadItemTest, DisplayName) { | 549 TEST_F(DownloadItemTest, DisplayName) { |
| 413 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 550 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 414 DownloadItemImplDelegate::DownloadTargetCallback callback; | 551 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 415 MockDownloadFile* download_file = | 552 MockDownloadFile* download_file = |
| 416 AddDownloadFileToDownloadItem(item, &callback); | 553 AddDownloadFileToDownloadItem(item, &callback); |
| 417 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 554 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 418 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 555 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 419 EXPECT_EQ(FILE_PATH_LITERAL(""), | 556 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 420 item->GetFileNameToReportUser().value()); | 557 item->GetFileNameToReportUser().value()); |
| 421 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 558 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
| 422 .WillOnce(ScheduleRenameCallback(intermediate_path)); | 559 .WillOnce(ScheduleRenameCallback(intermediate_path)); |
| 423 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 560 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 424 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 561 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 425 RunAllPendingInMessageLoops(); | 562 RunAllPendingInMessageLoops(); |
| 426 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 563 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 427 item->GetFileNameToReportUser().value()); | 564 item->GetFileNameToReportUser().value()); |
| 428 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 565 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 429 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 566 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 430 item->GetFileNameToReportUser().value()); | 567 item->GetFileNameToReportUser().value()); |
| 431 CleanupItem(item, download_file); | 568 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
| 432 } | 569 } |
| 433 | 570 |
| 434 // Test to make sure that Start method calls DF initialize properly. | 571 // Test to make sure that Start method calls DF initialize properly. |
| 435 TEST_F(DownloadItemTest, Start) { | 572 TEST_F(DownloadItemTest, Start) { |
| 436 MockDownloadFile* mock_download_file(new MockDownloadFile); | 573 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 437 scoped_ptr<DownloadFile> download_file(mock_download_file); | 574 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 438 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 575 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 439 EXPECT_CALL(*mock_download_file, Initialize(_)); | 576 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 440 item->Start(download_file.Pass()); | 577 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 578 new NiceMock<MockRequestHandle>); |
| 579 item->Start(download_file.Pass(), request_handle.Pass()); |
| 441 | 580 |
| 442 CleanupItem(item, mock_download_file); | 581 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
| 443 } | 582 } |
| 444 | 583 |
| 445 // Test that the delegate is invoked after the download file is renamed. | 584 // Test that the delegate is invoked after the download file is renamed. |
| 446 TEST_F(DownloadItemTest, CallbackAfterRename) { | 585 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 447 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 586 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 448 DownloadItemImplDelegate::DownloadTargetCallback callback; | 587 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 449 MockDownloadFile* download_file = | 588 MockDownloadFile* download_file = |
| 450 AddDownloadFileToDownloadItem(item, &callback); | 589 AddDownloadFileToDownloadItem(item, &callback); |
| 451 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 590 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 452 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 591 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 483 const DownloadInterruptReason reason( | 622 const DownloadInterruptReason reason( |
| 484 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 623 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 485 | 624 |
| 486 // Confirm interrupt sets state properly. | 625 // Confirm interrupt sets state properly. |
| 487 EXPECT_CALL(*download_file, Cancel()); | 626 EXPECT_CALL(*download_file, Cancel()); |
| 488 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 627 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
| 489 RunAllPendingInMessageLoops(); | 628 RunAllPendingInMessageLoops(); |
| 490 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 629 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 491 EXPECT_EQ(reason, item->GetLastReason()); | 630 EXPECT_EQ(reason, item->GetLastReason()); |
| 492 | 631 |
| 493 // Cancel should result in no change. | 632 // Cancel should kill it. |
| 494 item->Cancel(true); | 633 item->Cancel(true); |
| 495 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 634 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 496 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 635 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
| 497 } | 636 } |
| 498 | 637 |
| 499 TEST_F(DownloadItemTest, Canceled) { | 638 TEST_F(DownloadItemTest, Canceled) { |
| 500 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 639 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 501 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 640 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 502 | 641 |
| 503 // Confirm cancel sets state properly. | 642 // Confirm cancel sets state properly. |
| 504 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); | 643 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); |
| 505 EXPECT_CALL(*download_file, Cancel()); | 644 EXPECT_CALL(*download_file, Cancel()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 EXPECT_EQ("livebeef", item->GetHash()); | 732 EXPECT_EQ("livebeef", item->GetHash()); |
| 594 EXPECT_EQ("", item->GetHashState()); | 733 EXPECT_EQ("", item->GetHashState()); |
| 595 EXPECT_TRUE(item->AllDataSaved()); | 734 EXPECT_TRUE(item->AllDataSaved()); |
| 596 } | 735 } |
| 597 | 736 |
| 598 TEST(MockDownloadItem, Compiles) { | 737 TEST(MockDownloadItem, Compiles) { |
| 599 MockDownloadItem mock_item; | 738 MockDownloadItem mock_item; |
| 600 } | 739 } |
| 601 | 740 |
| 602 } // namespace content | 741 } // namespace content |
| OLD | NEW |