| 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_manager.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/public/browser/download_id.h" | 15 #include "content/public/browser/download_id.h" |
| 16 #include "content/public/browser/download_destination_observer.h" |
| 15 #include "content/public/browser/download_interrupt_reasons.h" | 17 #include "content/public/browser/download_interrupt_reasons.h" |
| 16 #include "content/public/test/mock_download_item.h" | 18 #include "content/public/test/mock_download_item.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 using content::BrowserThread; | 23 using content::BrowserThread; |
| 22 using content::DownloadId; | 24 using content::DownloadId; |
| 23 using content::DownloadItem; | 25 using content::DownloadItem; |
| 24 using content::DownloadManager; | 26 using content::DownloadManager; |
| 25 using content::MockDownloadItem; | 27 using content::MockDownloadItem; |
| 26 using content::WebContents; | 28 using content::WebContents; |
| 27 using ::testing::_; | 29 using ::testing::_; |
| 28 using ::testing::AllOf; | 30 using ::testing::AllOf; |
| 29 using ::testing::Property; | 31 using ::testing::Property; |
| 30 using ::testing::Return; | 32 using ::testing::Return; |
| 33 using ::testing::SaveArg; |
| 34 using ::testing::StrictMock; |
| 31 | 35 |
| 32 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 36 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
| 33 | 37 |
| 34 namespace { | 38 namespace { |
| 35 class MockDelegate : public DownloadItemImplDelegate { | 39 class MockDelegate : public DownloadItemImplDelegate { |
| 36 public: | 40 public: |
| 37 MockDelegate(DownloadFileManager* file_manager) | 41 MOCK_METHOD2(DetermineDownloadTarget, void( |
| 38 : file_manager_(file_manager) { | 42 DownloadItemImpl* download, const DownloadTargetCallback& callback)); |
| 39 } | 43 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); |
| 40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); | 44 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); |
| 41 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); | |
| 42 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); | 45 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); |
| 43 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); | 46 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); |
| 44 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | 47 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); |
| 45 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); | 48 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); |
| 46 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); | 49 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); |
| 47 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); | 50 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); |
| 48 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); | 51 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); |
| 49 MOCK_METHOD1(DownloadRenamedToIntermediateName, | 52 MOCK_METHOD1(DownloadRenamedToIntermediateName, |
| 50 void(DownloadItemImpl* download)); | 53 void(DownloadItemImpl* download)); |
| 51 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); | 54 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); |
| 52 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); | 55 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); |
| 53 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { | 56 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { |
| 54 return file_manager_; | 57 return file_manager_; |
| 55 } | 58 } |
| 56 private: | 59 private: |
| 57 DownloadFileManager* file_manager_; | 60 DownloadFileManager* file_manager_; |
| 58 }; | 61 }; |
| 59 | 62 |
| 60 class MockRequestHandle : public DownloadRequestHandleInterface { | 63 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 61 public: | 64 public: |
| 62 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 65 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
| 63 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 66 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 64 MOCK_CONST_METHOD0(PauseRequest, void()); | 67 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 65 MOCK_CONST_METHOD0(ResumeRequest, void()); | 68 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 66 MOCK_CONST_METHOD0(CancelRequest, void()); | 69 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 67 MOCK_CONST_METHOD0(DebugString, std::string()); | 70 MOCK_CONST_METHOD0(DebugString, std::string()); |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 class MockDownloadFileFactory : public content::DownloadFileFactory { | |
| 71 public: | |
| 72 content::DownloadFile* CreateFile( | |
| 73 DownloadCreateInfo* info, | |
| 74 scoped_ptr<content::ByteStreamReader> stream_reader, | |
| 75 DownloadManager* mgr, | |
| 76 bool calculate_hash, | |
| 77 const net::BoundNetLog& bound_net_log) { | |
| 78 return MockCreateFile( | |
| 79 info, stream_reader.get(), info->request_handle, mgr, calculate_hash, | |
| 80 bound_net_log); | |
| 81 } | |
| 82 | |
| 83 MOCK_METHOD6(MockCreateFile, | |
| 84 content::DownloadFile*(DownloadCreateInfo*, | |
| 85 content::ByteStreamReader*, | |
| 86 const DownloadRequestHandle&, | |
| 87 DownloadManager*, | |
| 88 bool, | |
| 89 const net::BoundNetLog&)); | |
| 90 }; | |
| 91 | |
| 92 class MockDownloadFileManager : public DownloadFileManager { | |
| 93 public: | |
| 94 MockDownloadFileManager(); | |
| 95 MOCK_METHOD0(Shutdown, void()); | |
| 96 MOCK_METHOD1(CancelDownload, void(DownloadId)); | |
| 97 MOCK_METHOD2(CompleteDownload, void(DownloadId, const base::Closure&)); | |
| 98 MOCK_METHOD1(OnDownloadManagerShutdown, void(DownloadManager*)); | |
| 99 MOCK_METHOD4(RenameDownloadFile, void(DownloadId, const FilePath&, bool, | |
| 100 const RenameCompletionCallback&)); | |
| 101 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int()); | |
| 102 private: | |
| 103 ~MockDownloadFileManager() {} | |
| 104 }; | |
| 105 | |
| 106 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on | 73 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on |
| 107 // the UI thread. Should only be used as the action for | 74 // the UI thread. Should only be used as the action for |
| 108 // MockDownloadFileManager::Rename*DownloadFile as follows: | 75 // MockDownloadFile::Rename as follows: |
| 109 // EXPECT_CALL(mock_download_file_manager, | 76 // EXPECT_CALL(download_file, Rename(_,_,_)) |
| 110 // RenameDownloadFile(_,_,_,_)) | |
| 111 // .WillOnce(ScheduleRenameCallback(new_path)); | 77 // .WillOnce(ScheduleRenameCallback(new_path)); |
| 112 ACTION_P(ScheduleRenameCallback, new_path) { | 78 ACTION_P(ScheduleRenameCallback, new_path) { |
| 113 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 114 BrowserThread::UI, FROM_HERE, | 80 BrowserThread::UI, FROM_HERE, |
| 115 base::Bind(arg3, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); | 81 base::Bind(arg2, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); |
| 82 } |
| 83 |
| 84 // Schedules a task to invoke the input closure on |
| 85 // the UI thread. Should only be used as the action for |
| 86 // MockDownloadFile::Detach/Cancel as follows: |
| 87 // EXPECT_CALL(download_file, Detach(_)) |
| 88 // .WillOnce(ScheduleClosure())); |
| 89 ACTION(ScheduleClosure) { |
| 90 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, arg0); |
| 116 } | 91 } |
| 117 | 92 |
| 118 // Similarly for scheduling a completion callback. | 93 // Similarly for scheduling a completion callback. |
| 119 ACTION(ScheduleCompleteCallback) { | 94 ACTION(ScheduleCompleteCallback) { |
| 120 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1)); | 95 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1)); |
| 121 } | 96 } |
| 122 | 97 |
| 123 MockDownloadFileManager::MockDownloadFileManager() | |
| 124 : DownloadFileManager(new MockDownloadFileFactory) { | |
| 125 } | |
| 126 | |
| 127 } // namespace | 98 } // namespace |
| 128 | 99 |
| 129 class DownloadItemTest : public testing::Test { | 100 class DownloadItemTest : public testing::Test { |
| 130 public: | 101 public: |
| 131 class MockObserver : public DownloadItem::Observer { | 102 class MockObserver : public DownloadItem::Observer { |
| 132 public: | 103 public: |
| 133 explicit MockObserver(DownloadItem* item) | 104 explicit MockObserver(DownloadItem* item) |
| 134 : item_(item), | 105 : item_(item), |
| 135 removed_(false), | 106 removed_(false), |
| 136 destroyed_(false), | 107 destroyed_(false), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 private: | 147 private: |
| 177 DownloadItem* item_; | 148 DownloadItem* item_; |
| 178 bool removed_; | 149 bool removed_; |
| 179 bool destroyed_; | 150 bool destroyed_; |
| 180 bool updated_; | 151 bool updated_; |
| 181 }; | 152 }; |
| 182 | 153 |
| 183 DownloadItemTest() | 154 DownloadItemTest() |
| 184 : ui_thread_(BrowserThread::UI, &loop_), | 155 : ui_thread_(BrowserThread::UI, &loop_), |
| 185 file_thread_(BrowserThread::FILE, &loop_), | 156 file_thread_(BrowserThread::FILE, &loop_), |
| 186 file_manager_(new MockDownloadFileManager), | 157 delegate_() { |
| 187 delegate_(file_manager_.get()) { | |
| 188 } | 158 } |
| 189 | 159 |
| 190 ~DownloadItemTest() { | 160 ~DownloadItemTest() { |
| 191 } | 161 } |
| 192 | 162 |
| 193 virtual void SetUp() { | 163 virtual void SetUp() { |
| 194 } | 164 } |
| 195 | 165 |
| 196 virtual void TearDown() { | 166 virtual void TearDown() { |
| 197 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 167 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 218 | 188 |
| 219 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 189 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 220 new testing::NiceMock<MockRequestHandle>); | 190 new testing::NiceMock<MockRequestHandle>); |
| 221 DownloadItemImpl* download = | 191 DownloadItemImpl* download = |
| 222 new DownloadItemImpl(&delegate_, *(info_.get()), | 192 new DownloadItemImpl(&delegate_, *(info_.get()), |
| 223 request_handle.Pass(), net::BoundNetLog()); | 193 request_handle.Pass(), net::BoundNetLog()); |
| 224 allocated_downloads_.insert(download); | 194 allocated_downloads_.insert(download); |
| 225 return download; | 195 return download; |
| 226 } | 196 } |
| 227 | 197 |
| 198 // Add DownloadFile to DownloadItem |
| 199 MockDownloadFile* AddDownloadFileToDownloadItem( |
| 200 DownloadItemImpl* item, |
| 201 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
| 202 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
| 203 scoped_ptr<content::DownloadFile> download_file(mock_download_file); |
| 204 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 205 if (callback) { |
| 206 // Save the callback. |
| 207 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 208 .WillOnce(SaveArg<1>(callback)); |
| 209 } else { |
| 210 // Drop it on the floor. |
| 211 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 212 } |
| 213 |
| 214 item->Start(download_file.Pass()); |
| 215 loop_.RunAllPending(); |
| 216 |
| 217 // So that we don't have a function writing to a stack variable |
| 218 // lying around if the above failed. |
| 219 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 220 |
| 221 return mock_download_file; |
| 222 } |
| 223 |
| 224 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
| 225 // The item must be in the IN_PROGRESS state. |
| 226 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { |
| 227 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 228 |
| 229 EXPECT_CALL(*download_file, Cancel()); |
| 230 EXPECT_CALL(delegate_, DownloadStopped(item)); |
| 231 item->Cancel(true); |
| 232 loop_.RunAllPending(); |
| 233 } |
| 234 |
| 228 // Destroy a previously created download item. | 235 // Destroy a previously created download item. |
| 229 void DestroyDownloadItem(DownloadItem* item) { | 236 void DestroyDownloadItem(DownloadItem* item) { |
| 230 allocated_downloads_.erase(item); | 237 allocated_downloads_.erase(item); |
| 231 delete item; | 238 delete item; |
| 232 } | 239 } |
| 233 | 240 |
| 234 void RunAllPendingInMessageLoops() { | 241 void RunAllPendingInMessageLoops() { |
| 235 loop_.RunAllPending(); | 242 loop_.RunAllPending(); |
| 236 } | 243 } |
| 237 | 244 |
| 238 MockDelegate* mock_delegate() { | 245 MockDelegate* mock_delegate() { |
| 239 return &delegate_; | 246 return &delegate_; |
| 240 } | 247 } |
| 241 | 248 |
| 242 MockDownloadFileManager* mock_file_manager() { | |
| 243 return file_manager_.get(); | |
| 244 } | |
| 245 | |
| 246 private: | 249 private: |
| 247 MessageLoopForUI loop_; | 250 MessageLoopForUI loop_; |
| 248 content::TestBrowserThread ui_thread_; // UI thread | 251 content::TestBrowserThread ui_thread_; // UI thread |
| 249 content::TestBrowserThread file_thread_; // FILE thread | 252 content::TestBrowserThread file_thread_; // FILE thread |
| 250 scoped_refptr<MockDownloadFileManager> file_manager_; | |
| 251 testing::NiceMock<MockDelegate> delegate_; | 253 testing::NiceMock<MockDelegate> delegate_; |
| 252 std::set<DownloadItem*> allocated_downloads_; | 254 std::set<DownloadItem*> allocated_downloads_; |
| 253 }; | 255 }; |
| 254 | 256 |
| 255 namespace { | 257 namespace { |
| 256 | 258 |
| 257 const int kDownloadChunkSize = 1000; | 259 const int kDownloadChunkSize = 1000; |
| 258 const int kDownloadSpeed = 1000; | 260 const int kDownloadSpeed = 1000; |
| 259 const int kDummyDBHandle = 10; | 261 const int kDummyDBHandle = 10; |
| 260 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 262 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 274 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 276 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 275 MockObserver observer(item); | 277 MockObserver observer(item); |
| 276 | 278 |
| 277 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); | 279 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); |
| 278 ASSERT_TRUE(observer.CheckUpdated()); | 280 ASSERT_TRUE(observer.CheckUpdated()); |
| 279 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); | 281 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); |
| 280 } | 282 } |
| 281 | 283 |
| 282 TEST_F(DownloadItemTest, NotificationAfterCancel) { | 284 TEST_F(DownloadItemTest, NotificationAfterCancel) { |
| 283 DownloadItemImpl* user_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 285 DownloadItemImpl* user_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 286 MockDownloadFile* download_file = |
| 287 AddDownloadFileToDownloadItem(user_cancel, NULL); |
| 288 EXPECT_CALL(*download_file, Cancel()); |
| 284 MockObserver observer1(user_cancel); | 289 MockObserver observer1(user_cancel); |
| 285 | 290 |
| 286 user_cancel->Cancel(true); | 291 user_cancel->Cancel(true); |
| 287 ASSERT_TRUE(observer1.CheckUpdated()); | 292 ASSERT_TRUE(observer1.CheckUpdated()); |
| 288 | 293 |
| 289 DownloadItemImpl* system_cancel = | 294 DownloadItemImpl* system_cancel = |
| 290 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 295 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 296 download_file = AddDownloadFileToDownloadItem(system_cancel, NULL); |
| 297 EXPECT_CALL(*download_file, Cancel()); |
| 291 MockObserver observer2(system_cancel); | 298 MockObserver observer2(system_cancel); |
| 292 | 299 |
| 293 system_cancel->Cancel(false); | 300 system_cancel->Cancel(false); |
| 294 ASSERT_TRUE(observer2.CheckUpdated()); | 301 ASSERT_TRUE(observer2.CheckUpdated()); |
| 295 } | 302 } |
| 296 | 303 |
| 297 TEST_F(DownloadItemTest, NotificationAfterComplete) { | 304 TEST_F(DownloadItemTest, NotificationAfterComplete) { |
| 298 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 305 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 299 MockObserver observer(item); | 306 MockObserver observer(item); |
| 300 | 307 |
| 301 item->OnAllDataSaved(kDownloadChunkSize, DownloadItem::kEmptyFileHash); | 308 item->OnAllDataSaved(DownloadItem::kEmptyFileHash); |
| 302 ASSERT_TRUE(observer.CheckUpdated()); | 309 ASSERT_TRUE(observer.CheckUpdated()); |
| 303 | 310 |
| 304 item->MarkAsComplete(); | 311 item->MarkAsComplete(); |
| 305 ASSERT_TRUE(observer.CheckUpdated()); | 312 ASSERT_TRUE(observer.CheckUpdated()); |
| 306 } | 313 } |
| 307 | 314 |
| 308 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { | 315 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { |
| 309 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 316 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 310 MockObserver observer(item); | 317 MockObserver observer(item); |
| 311 | 318 |
| 312 item->OnDownloadedFileRemoved(); | 319 item->OnDownloadedFileRemoved(); |
| 313 ASSERT_TRUE(observer.CheckUpdated()); | 320 ASSERT_TRUE(observer.CheckUpdated()); |
| 314 } | 321 } |
| 315 | 322 |
| 316 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 323 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
| 317 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 324 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 325 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 326 EXPECT_CALL(*download_file, Cancel()); |
| 318 MockObserver observer(item); | 327 MockObserver observer(item); |
| 319 | 328 |
| 320 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE); | 329 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 321 ASSERT_TRUE(observer.CheckUpdated()); | 330 ASSERT_TRUE(observer.CheckUpdated()); |
| 322 } | 331 } |
| 323 | 332 |
| 324 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 333 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 325 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 334 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 335 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 336 EXPECT_CALL(*download_file, Cancel()); |
| 326 MockObserver observer(item); | 337 MockObserver observer(item); |
| 327 | 338 |
| 328 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 339 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 329 ASSERT_TRUE(observer.CheckUpdated()); | 340 ASSERT_TRUE(observer.CheckUpdated()); |
| 330 } | 341 } |
| 331 | 342 |
| 332 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 343 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 333 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 344 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 334 MockObserver observer(item); | 345 MockObserver observer(item); |
| 335 | 346 |
| 336 DestroyDownloadItem(item); | 347 DestroyDownloadItem(item); |
| 337 ASSERT_TRUE(observer.CheckDestroyed()); | 348 ASSERT_TRUE(observer.CheckDestroyed()); |
| 338 } | 349 } |
| 339 | 350 |
| 340 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 351 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 341 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 352 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 353 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 354 EXPECT_CALL(*download_file, Cancel()); |
| 342 MockObserver observer(item); | 355 MockObserver observer(item); |
| 343 | 356 |
| 344 item->Remove(); | 357 item->Remove(); |
| 345 ASSERT_TRUE(observer.CheckUpdated()); | 358 ASSERT_TRUE(observer.CheckUpdated()); |
| 346 ASSERT_TRUE(observer.CheckRemoved()); | 359 ASSERT_TRUE(observer.CheckRemoved()); |
| 347 } | 360 } |
| 348 | 361 |
| 349 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 362 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 350 // Setting to NOT_DANGEROUS does not trigger a notification. | 363 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 351 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 364 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 352 MockObserver safe_observer(safe_item); | 365 MockObserver safe_observer(safe_item); |
| 353 | 366 |
| 354 safe_item->OnAllDataSaved(1, ""); | 367 safe_item->OnAllDataSaved(""); |
| 355 EXPECT_TRUE(safe_observer.CheckUpdated()); | 368 EXPECT_TRUE(safe_observer.CheckUpdated()); |
| 356 safe_item->OnContentCheckCompleted( | 369 safe_item->OnContentCheckCompleted( |
| 357 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 370 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 358 EXPECT_TRUE(safe_observer.CheckUpdated()); | 371 EXPECT_TRUE(safe_observer.CheckUpdated()); |
| 359 | 372 |
| 360 // Setting to unsafe url or unsafe file should trigger a notification. | 373 // Setting to unsafe url or unsafe file should trigger a notification. |
| 361 DownloadItemImpl* unsafeurl_item = | 374 DownloadItemImpl* unsafeurl_item = |
| 362 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 375 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 363 MockObserver unsafeurl_observer(unsafeurl_item); | 376 MockObserver unsafeurl_observer(unsafeurl_item); |
| 364 | 377 |
| 365 unsafeurl_item->OnAllDataSaved(1, ""); | 378 unsafeurl_item->OnAllDataSaved(""); |
| 366 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 379 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 367 unsafeurl_item->OnContentCheckCompleted( | 380 unsafeurl_item->OnContentCheckCompleted( |
| 368 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 381 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); |
| 369 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 382 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 370 | 383 |
| 371 unsafeurl_item->DangerousDownloadValidated(); | 384 unsafeurl_item->DangerousDownloadValidated(); |
| 372 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 385 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 373 | 386 |
| 374 DownloadItemImpl* unsafefile_item = | 387 DownloadItemImpl* unsafefile_item = |
| 375 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 388 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 376 MockObserver unsafefile_observer(unsafefile_item); | 389 MockObserver unsafefile_observer(unsafefile_item); |
| 377 | 390 |
| 378 unsafefile_item->OnAllDataSaved(1, ""); | 391 unsafefile_item->OnAllDataSaved(""); |
| 379 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 392 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 380 unsafefile_item->OnContentCheckCompleted( | 393 unsafefile_item->OnContentCheckCompleted( |
| 381 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 394 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 382 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 395 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 383 | 396 |
| 384 unsafefile_item->DangerousDownloadValidated(); | 397 unsafefile_item->DangerousDownloadValidated(); |
| 385 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 398 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 386 } | 399 } |
| 387 | 400 |
| 388 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run | 401 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run |
| 389 // DownloadFileManager::RenameDownloadFile(). Once the rename | 402 // DownloadFile::Rename(). Once the rename |
| 390 // completes, DownloadItemImpl receives a notification with the new file | 403 // completes, DownloadItemImpl receives a notification with the new file |
| 391 // name. Check that observers are updated when the new filename is available and | 404 // name. Check that observers are updated when the new filename is available and |
| 392 // not before. | 405 // not before. |
| 393 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { | 406 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { |
| 394 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 407 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 408 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 409 MockDownloadFile* download_file = |
| 410 AddDownloadFileToDownloadItem(item, &callback); |
| 395 MockObserver observer(item); | 411 MockObserver observer(item); |
| 396 FilePath target_path(kDummyPath); | 412 FilePath target_path(kDummyPath); |
| 397 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 413 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 398 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); | 414 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
| 399 EXPECT_CALL(*mock_file_manager(), | 415 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) |
| 400 RenameDownloadFile(_,intermediate_path,false,_)) | |
| 401 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 416 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 402 | 417 |
| 403 // Currently, a notification would be generated if the danger type is anything | 418 // Currently, a notification would be generated if the danger type is anything |
| 404 // other than NOT_DANGEROUS. | 419 // other than NOT_DANGEROUS. |
| 405 item->OnDownloadTargetDetermined(target_path, | 420 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 406 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 421 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 407 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 408 intermediate_path); | |
| 409 EXPECT_FALSE(observer.CheckUpdated()); | 422 EXPECT_FALSE(observer.CheckUpdated()); |
| 410 RunAllPendingInMessageLoops(); | 423 RunAllPendingInMessageLoops(); |
| 411 EXPECT_TRUE(observer.CheckUpdated()); | 424 EXPECT_TRUE(observer.CheckUpdated()); |
| 412 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 425 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 426 |
| 427 CleanupItem(item, download_file); |
| 413 } | 428 } |
| 414 | 429 |
| 415 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 430 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
| 416 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 431 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 417 MockObserver observer(item); | 432 MockObserver observer(item); |
| 418 | 433 |
| 419 item->TogglePause(); | 434 item->TogglePause(); |
| 420 ASSERT_TRUE(observer.CheckUpdated()); | 435 ASSERT_TRUE(observer.CheckUpdated()); |
| 421 | 436 |
| 422 item->TogglePause(); | 437 item->TogglePause(); |
| 423 ASSERT_TRUE(observer.CheckUpdated()); | 438 ASSERT_TRUE(observer.CheckUpdated()); |
| 424 } | 439 } |
| 425 | 440 |
| 426 TEST_F(DownloadItemTest, DisplayName) { | 441 TEST_F(DownloadItemTest, DisplayName) { |
| 427 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 442 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 443 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 444 MockDownloadFile* download_file = |
| 445 AddDownloadFileToDownloadItem(item, &callback); |
| 428 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 446 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 429 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 447 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 430 EXPECT_EQ(FILE_PATH_LITERAL(""), | 448 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 431 item->GetFileNameToReportUser().value()); | 449 item->GetFileNameToReportUser().value()); |
| 432 EXPECT_CALL(*mock_file_manager(), | 450 EXPECT_CALL(*download_file, Rename(_, false, _)) |
| 433 RenameDownloadFile(_,_,false,_)) | |
| 434 .WillOnce(ScheduleRenameCallback(intermediate_path)); | 451 .WillOnce(ScheduleRenameCallback(intermediate_path)); |
| 435 item->OnDownloadTargetDetermined(target_path, | 452 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 436 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 453 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 437 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 438 intermediate_path); | |
| 439 RunAllPendingInMessageLoops(); | 454 RunAllPendingInMessageLoops(); |
| 440 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 455 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 441 item->GetFileNameToReportUser().value()); | 456 item->GetFileNameToReportUser().value()); |
| 442 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 457 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 443 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 458 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 444 item->GetFileNameToReportUser().value()); | 459 item->GetFileNameToReportUser().value()); |
| 460 CleanupItem(item, download_file); |
| 461 } |
| 462 |
| 463 // Test to make sure that Start method calls DF initialize properly. |
| 464 TEST_F(DownloadItemTest, Start) { |
| 465 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 466 scoped_ptr<content::DownloadFile> download_file(mock_download_file); |
| 467 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 468 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 469 item->Start(download_file.Pass()); |
| 470 |
| 471 CleanupItem(item, mock_download_file); |
| 445 } | 472 } |
| 446 | 473 |
| 447 // Test that the delegate is invoked after the download file is renamed. | 474 // Test that the delegate is invoked after the download file is renamed. |
| 448 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the | 475 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the |
| 449 // download is renamed to the intermediate name. | 476 // download is renamed to the intermediate name. |
| 450 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | 477 // Delegate::DownloadRenamedToFinalName() should be invoked after the final |
| 451 // rename. | 478 // rename. |
| 452 TEST_F(DownloadItemTest, CallbackAfterRename) { | 479 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 453 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 480 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 481 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 482 MockDownloadFile* download_file = |
| 483 AddDownloadFileToDownloadItem(item, &callback); |
| 454 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 484 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 455 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 485 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 456 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 486 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
| 457 EXPECT_CALL(*mock_file_manager(), | 487 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) |
| 458 RenameDownloadFile(item->GetGlobalId(), | |
| 459 intermediate_path, false, _)) | |
| 460 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 488 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 489 |
| 461 // DownloadItemImpl should invoke this callback on the delegate once the | 490 // DownloadItemImpl should invoke this callback on the delegate once the |
| 462 // download is renamed to the intermediate name. Also check that GetFullPath() | 491 // download is renamed to the intermediate name. Also check that GetFullPath() |
| 463 // returns the intermediate path at the time of the call. | 492 // returns the intermediate path at the time of the call. |
| 464 EXPECT_CALL(*mock_delegate(), | 493 EXPECT_CALL(*mock_delegate(), |
| 465 DownloadRenamedToIntermediateName( | 494 DownloadRenamedToIntermediateName( |
| 466 AllOf(item, | 495 AllOf(item, |
| 467 Property(&DownloadItem::GetFullPath, | 496 Property(&DownloadItem::GetFullPath, |
| 468 new_intermediate_path)))); | 497 new_intermediate_path)))); |
| 469 item->OnDownloadTargetDetermined(final_path, | 498 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 470 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 499 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 471 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 472 intermediate_path); | |
| 473 RunAllPendingInMessageLoops(); | 500 RunAllPendingInMessageLoops(); |
| 474 // All the callbacks should have happened by now. | 501 // All the callbacks should have happened by now. |
| 475 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); | 502 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 476 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 503 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 477 | 504 |
| 478 item->OnAllDataSaved(10, ""); | 505 item->OnAllDataSaved(""); |
| 479 EXPECT_CALL(*mock_file_manager(), | 506 EXPECT_CALL(*download_file, Rename(final_path, true, _)) |
| 480 RenameDownloadFile(item->GetGlobalId(), | |
| 481 final_path, true, _)) | |
| 482 .WillOnce(ScheduleRenameCallback(final_path)); | 507 .WillOnce(ScheduleRenameCallback(final_path)); |
| 483 EXPECT_CALL(*mock_file_manager(), | |
| 484 CompleteDownload(item->GetGlobalId(), _)) | |
| 485 .WillOnce(ScheduleCompleteCallback()); | |
| 486 // DownloadItemImpl should invoke this callback on the delegate after the | 508 // DownloadItemImpl should invoke this callback on the delegate after the |
| 487 // final rename has completed. Also check that GetFullPath() and | 509 // final rename has completed. Also check that GetFullPath() and |
| 488 // GetTargetFilePath() return the final path at the time of the call. | 510 // GetTargetFilePath() return the final path at the time of the call. |
| 489 EXPECT_CALL(*mock_delegate(), | 511 EXPECT_CALL(*mock_delegate(), |
| 490 DownloadRenamedToFinalName( | 512 DownloadRenamedToFinalName( |
| 491 AllOf(item, | 513 AllOf(item, |
| 492 Property(&DownloadItem::GetFullPath, final_path), | 514 Property(&DownloadItem::GetFullPath, final_path), |
| 493 Property(&DownloadItem::GetTargetFilePath, | 515 Property(&DownloadItem::GetTargetFilePath, |
| 494 final_path)))); | 516 final_path)))); |
| 495 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); | 517 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); |
| 496 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) | 518 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) |
| 497 .WillOnce(Return(true)); | 519 .WillOnce(Return(true)); |
| 520 EXPECT_CALL(*download_file, Detach(_)) |
| 521 .WillOnce(ScheduleClosure()); |
| 498 item->OnDownloadCompleting(); | 522 item->OnDownloadCompleting(); |
| 499 RunAllPendingInMessageLoops(); | 523 RunAllPendingInMessageLoops(); |
| 500 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); | 524 ::testing::Mock::VerifyAndClearExpectations(download_file); |
| 501 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 525 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 502 } | 526 } |
| 503 | 527 |
| 504 TEST_F(DownloadItemTest, Interrupted) { | 528 TEST_F(DownloadItemTest, Interrupted) { |
| 505 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 529 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 530 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 506 | 531 |
| 507 const content::DownloadInterruptReason reason( | 532 const content::DownloadInterruptReason reason( |
| 508 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 533 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 509 | 534 |
| 510 // Confirm interrupt sets state properly. | 535 // Confirm interrupt sets state properly. |
| 536 EXPECT_CALL(*download_file, Cancel()); |
| 511 item->Interrupt(reason); | 537 item->Interrupt(reason); |
| 538 RunAllPendingInMessageLoops(); |
| 512 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 539 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 513 EXPECT_EQ(reason, item->GetLastReason()); | 540 EXPECT_EQ(reason, item->GetLastReason()); |
| 514 | 541 |
| 515 // Cancel should result in no change. | 542 // Cancel should result in no change. |
| 516 item->Cancel(true); | 543 item->Cancel(true); |
| 517 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 544 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
| 518 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, | 545 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, |
| 519 item->GetLastReason()); | 546 item->GetLastReason()); |
| 520 } | 547 } |
| 521 | 548 |
| 522 TEST_F(DownloadItemTest, Canceled) { | 549 TEST_F(DownloadItemTest, Canceled) { |
| 523 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 550 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 551 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 524 | 552 |
| 525 // Confirm cancel sets state properly. | 553 // Confirm cancel sets state properly. |
| 526 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); | 554 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); |
| 555 EXPECT_CALL(*download_file, Cancel()); |
| 527 item->Cancel(true); | 556 item->Cancel(true); |
| 528 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 557 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
| 529 } | 558 } |
| 530 | 559 |
| 531 TEST_F(DownloadItemTest, FileRemoved) { | 560 TEST_F(DownloadItemTest, FileRemoved) { |
| 532 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 561 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 533 | 562 |
| 534 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 563 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
| 535 item->OnDownloadedFileRemoved(); | 564 item->OnDownloadedFileRemoved(); |
| 536 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 565 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
| 537 } | 566 } |
| 538 | 567 |
| 568 TEST_F(DownloadItemTest, DestinationUpdate) { |
| 569 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 570 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 571 item->DestinationObserverAsWeakPtr()); |
| 572 MockObserver observer(item); |
| 573 |
| 574 EXPECT_EQ(0l, item->CurrentSpeed()); |
| 575 EXPECT_EQ("", item->GetHashState()); |
| 576 EXPECT_EQ(0l, item->GetReceivedBytes()); |
| 577 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 578 EXPECT_FALSE(observer.CheckUpdated()); |
| 579 item->SetTotalBytes(100l); |
| 580 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 581 |
| 582 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
| 583 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 584 EXPECT_EQ("deadbeef", item->GetHashState()); |
| 585 EXPECT_EQ(10l, item->GetReceivedBytes()); |
| 586 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 587 EXPECT_TRUE(observer.CheckUpdated()); |
| 588 |
| 589 as_observer->DestinationUpdate(200, 20, "livebeef"); |
| 590 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 591 EXPECT_EQ("livebeef", item->GetHashState()); |
| 592 EXPECT_EQ(200l, item->GetReceivedBytes()); |
| 593 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 594 EXPECT_TRUE(observer.CheckUpdated()); |
| 595 } |
| 596 |
| 597 TEST_F(DownloadItemTest, DestinationError) { |
| 598 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 599 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 600 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 601 item->DestinationObserverAsWeakPtr()); |
| 602 MockObserver observer(item); |
| 603 |
| 604 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 605 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); |
| 606 EXPECT_FALSE(observer.CheckUpdated()); |
| 607 |
| 608 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); |
| 609 EXPECT_CALL(*download_file, Cancel()); |
| 610 as_observer->DestinationError( |
| 611 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 612 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 613 EXPECT_TRUE(observer.CheckUpdated()); |
| 614 EXPECT_EQ(content::DownloadItem::INTERRUPTED, item->GetState()); |
| 615 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 616 item->GetLastReason()); |
| 617 } |
| 618 |
| 619 TEST_F(DownloadItemTest, DestinationCompleted) { |
| 620 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 621 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 622 item->DestinationObserverAsWeakPtr()); |
| 623 MockObserver observer(item); |
| 624 |
| 625 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 626 EXPECT_EQ("", item->GetHash()); |
| 627 EXPECT_EQ("", item->GetHashState()); |
| 628 EXPECT_FALSE(item->AllDataSaved()); |
| 629 EXPECT_FALSE(observer.CheckUpdated()); |
| 630 |
| 631 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
| 632 EXPECT_TRUE(observer.CheckUpdated()); |
| 633 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. |
| 634 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 635 EXPECT_EQ("", item->GetHash()); |
| 636 EXPECT_EQ("deadbeef", item->GetHashState()); |
| 637 EXPECT_FALSE(item->AllDataSaved()); |
| 638 |
| 639 EXPECT_CALL(*mock_delegate(), MaybeCompleteDownload(item)); |
| 640 as_observer->DestinationCompleted("livebeef"); |
| 641 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 642 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 643 EXPECT_TRUE(observer.CheckUpdated()); |
| 644 EXPECT_EQ("livebeef", item->GetHash()); |
| 645 EXPECT_EQ("", item->GetHashState()); |
| 646 EXPECT_TRUE(item->AllDataSaved()); |
| 647 } |
| 648 |
| 539 TEST(MockDownloadItem, Compiles) { | 649 TEST(MockDownloadItem, Compiles) { |
| 540 MockDownloadItem mock_item; | 650 MockDownloadItem mock_item; |
| 541 } | 651 } |
| OLD | NEW |