| 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_manager.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using ::testing::_; | 27 using ::testing::_; |
| 28 using ::testing::AllOf; | 28 using ::testing::AllOf; |
| 29 using ::testing::Property; | 29 using ::testing::Property; |
| 30 using ::testing::Return; | 30 using ::testing::Return; |
| 31 | 31 |
| 32 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 32 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 class MockDelegate : public DownloadItemImplDelegate { | 35 class MockDelegate : public DownloadItemImplDelegate { |
| 36 public: | 36 public: |
| 37 MockDelegate(DownloadFileManager* file_manager) |
| 38 : file_manager_(file_manager) { |
| 39 } |
| 37 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); | 40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); |
| 38 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); | 41 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); |
| 39 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); | 42 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); |
| 40 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); | 43 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); |
| 41 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | 44 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); |
| 42 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); | 45 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); |
| 43 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); | 46 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); |
| 44 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); | 47 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); |
| 45 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); | 48 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); |
| 46 MOCK_METHOD1(DownloadRenamedToIntermediateName, | 49 MOCK_METHOD1(DownloadRenamedToIntermediateName, |
| 47 void(DownloadItemImpl* download)); | 50 void(DownloadItemImpl* download)); |
| 48 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); | 51 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); |
| 49 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); | 52 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); |
| 53 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { |
| 54 return file_manager_; |
| 55 } |
| 56 private: |
| 57 DownloadFileManager* file_manager_; |
| 50 }; | 58 }; |
| 51 | 59 |
| 52 class MockRequestHandle : public DownloadRequestHandleInterface { | 60 class MockRequestHandle : public DownloadRequestHandleInterface { |
| 53 public: | 61 public: |
| 54 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 62 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
| 55 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 63 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
| 56 MOCK_CONST_METHOD0(PauseRequest, void()); | 64 MOCK_CONST_METHOD0(PauseRequest, void()); |
| 57 MOCK_CONST_METHOD0(ResumeRequest, void()); | 65 MOCK_CONST_METHOD0(ResumeRequest, void()); |
| 58 MOCK_CONST_METHOD0(CancelRequest, void()); | 66 MOCK_CONST_METHOD0(CancelRequest, void()); |
| 59 MOCK_CONST_METHOD0(DebugString, std::string()); | 67 MOCK_CONST_METHOD0(DebugString, std::string()); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 return was_updated; | 148 return was_updated; |
| 141 } | 149 } |
| 142 | 150 |
| 143 private: | 151 private: |
| 144 DownloadItem* item_; | 152 DownloadItem* item_; |
| 145 bool updated_; | 153 bool updated_; |
| 146 }; | 154 }; |
| 147 | 155 |
| 148 DownloadItemTest() | 156 DownloadItemTest() |
| 149 : ui_thread_(BrowserThread::UI, &loop_), | 157 : ui_thread_(BrowserThread::UI, &loop_), |
| 150 file_thread_(BrowserThread::FILE, &loop_) { | 158 file_thread_(BrowserThread::FILE, &loop_), |
| 159 file_manager_(new MockDownloadFileManager), |
| 160 delegate_(file_manager_.get()) { |
| 151 } | 161 } |
| 152 | 162 |
| 153 ~DownloadItemTest() { | 163 ~DownloadItemTest() { |
| 154 } | 164 } |
| 155 | 165 |
| 156 virtual void SetUp() { | 166 virtual void SetUp() { |
| 157 } | 167 } |
| 158 | 168 |
| 159 virtual void TearDown() { | 169 virtual void TearDown() { |
| 160 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 170 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 } | 205 } |
| 196 | 206 |
| 197 void RunAllPendingInMessageLoops() { | 207 void RunAllPendingInMessageLoops() { |
| 198 loop_.RunAllPending(); | 208 loop_.RunAllPending(); |
| 199 } | 209 } |
| 200 | 210 |
| 201 MockDelegate* mock_delegate() { | 211 MockDelegate* mock_delegate() { |
| 202 return &delegate_; | 212 return &delegate_; |
| 203 } | 213 } |
| 204 | 214 |
| 215 MockDownloadFileManager* mock_file_manager() { |
| 216 return file_manager_.get(); |
| 217 } |
| 218 |
| 205 private: | 219 private: |
| 206 MessageLoopForUI loop_; | 220 MessageLoopForUI loop_; |
| 207 content::TestBrowserThread ui_thread_; // UI thread | 221 content::TestBrowserThread ui_thread_; // UI thread |
| 208 content::TestBrowserThread file_thread_; // FILE thread | 222 content::TestBrowserThread file_thread_; // FILE thread |
| 223 scoped_refptr<MockDownloadFileManager> file_manager_; |
| 209 testing::NiceMock<MockDelegate> delegate_; | 224 testing::NiceMock<MockDelegate> delegate_; |
| 210 std::set<DownloadItem*> allocated_downloads_; | 225 std::set<DownloadItem*> allocated_downloads_; |
| 211 }; | 226 }; |
| 212 | 227 |
| 213 namespace { | 228 namespace { |
| 214 | 229 |
| 215 const int kDownloadChunkSize = 1000; | 230 const int kDownloadChunkSize = 1000; |
| 216 const int kDownloadSpeed = 1000; | 231 const int kDownloadSpeed = 1000; |
| 217 const int kDummyDBHandle = 10; | 232 const int kDummyDBHandle = 10; |
| 218 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 233 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
| 219 | 234 |
| 220 } // namespace | 235 } // namespace |
| 221 | 236 |
| 222 // Tests to ensure calls that change a DownloadItem generate an update to | 237 // Tests to ensure calls that change a DownloadItem generate an update to |
| 223 // observers. | 238 // observers. |
| 224 // State changing functions not tested: | 239 // State changing functions not tested: |
| 225 // void OpenDownload(); | 240 // void OpenDownload(); |
| 226 // void ShowDownloadInShell(); | 241 // void ShowDownloadInShell(); |
| 227 // void CompleteDelayedDownload(); | 242 // void CompleteDelayedDownload(); |
| 228 // void OnDownloadCompleting(DownloadFileManager* file_manager); | 243 // void OnDownloadCompleting(); |
| 229 // set_* mutators | 244 // set_* mutators |
| 230 | 245 |
| 231 TEST_F(DownloadItemTest, NotificationAfterUpdate) { | 246 TEST_F(DownloadItemTest, NotificationAfterUpdate) { |
| 232 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 247 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 233 MockObserver observer(item); | 248 MockObserver observer(item); |
| 234 | 249 |
| 235 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); | 250 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); |
| 236 ASSERT_TRUE(observer.CheckUpdated()); | 251 ASSERT_TRUE(observer.CheckUpdated()); |
| 237 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); | 252 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); |
| 238 } | 253 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 303 } |
| 289 | 304 |
| 290 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 305 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 291 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 292 MockObserver observer(item); | 307 MockObserver observer(item); |
| 293 | 308 |
| 294 item->Remove(); | 309 item->Remove(); |
| 295 ASSERT_TRUE(observer.CheckUpdated()); | 310 ASSERT_TRUE(observer.CheckUpdated()); |
| 296 } | 311 } |
| 297 | 312 |
| 298 TEST_F(DownloadItemTest, NotificationAfterOnTargetPathDetermined) { | |
| 299 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | |
| 300 MockObserver safe_observer(safe_item); | |
| 301 | |
| 302 // Calling OnTargetPathDetermined does not trigger notification if danger type | |
| 303 // is NOT_DANGEROUS. | |
| 304 safe_item->OnTargetPathDetermined( | |
| 305 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 306 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
| 307 EXPECT_FALSE(safe_observer.CheckUpdated()); | |
| 308 | |
| 309 DownloadItemImpl* dangerous_item = | |
| 310 CreateDownloadItem(DownloadItem::IN_PROGRESS); | |
| 311 MockObserver dangerous_observer(dangerous_item); | |
| 312 | |
| 313 // Calling OnTargetPathDetermined does trigger notification if danger type | |
| 314 // anything other than NOT_DANGEROUS. | |
| 315 dangerous_item->OnTargetPathDetermined( | |
| 316 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 317 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | |
| 318 EXPECT_TRUE(dangerous_observer.CheckUpdated()); | |
| 319 } | |
| 320 | |
| 321 TEST_F(DownloadItemTest, NotificationAfterOnTargetPathSelected) { | |
| 322 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | |
| 323 MockObserver observer(item); | |
| 324 | |
| 325 item->OnTargetPathDetermined( | |
| 326 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_PROMPT, | |
| 327 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
| 328 item->OnTargetPathSelected(FilePath(kDummyPath)); | |
| 329 EXPECT_FALSE(observer.CheckUpdated()); | |
| 330 } | |
| 331 | |
| 332 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 313 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 333 // Setting to NOT_DANGEROUS does not trigger a notification. | 314 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 334 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 315 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 335 MockObserver safe_observer(safe_item); | 316 MockObserver safe_observer(safe_item); |
| 336 | 317 |
| 337 safe_item->OnTargetPathDetermined( | |
| 338 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 339 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
| 340 EXPECT_FALSE(safe_observer.CheckUpdated()); | |
| 341 safe_item->OnAllDataSaved(1, ""); | 318 safe_item->OnAllDataSaved(1, ""); |
| 342 EXPECT_TRUE(safe_observer.CheckUpdated()); | 319 EXPECT_TRUE(safe_observer.CheckUpdated()); |
| 343 safe_item->OnContentCheckCompleted( | 320 safe_item->OnContentCheckCompleted( |
| 344 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 321 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 345 EXPECT_FALSE(safe_observer.CheckUpdated()); | 322 EXPECT_FALSE(safe_observer.CheckUpdated()); |
| 346 | 323 |
| 347 // Setting to unsafe url or unsafe file should trigger a notification. | 324 // Setting to unsafe url or unsafe file should trigger a notification. |
| 348 DownloadItemImpl* unsafeurl_item = | 325 DownloadItemImpl* unsafeurl_item = |
| 349 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 326 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 350 MockObserver unsafeurl_observer(unsafeurl_item); | 327 MockObserver unsafeurl_observer(unsafeurl_item); |
| 351 | 328 |
| 352 unsafeurl_item->OnTargetPathDetermined( | |
| 353 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 354 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
| 355 EXPECT_FALSE(unsafeurl_observer.CheckUpdated()); | |
| 356 unsafeurl_item->OnAllDataSaved(1, ""); | 329 unsafeurl_item->OnAllDataSaved(1, ""); |
| 357 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 330 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 358 unsafeurl_item->OnContentCheckCompleted( | 331 unsafeurl_item->OnContentCheckCompleted( |
| 359 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 332 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); |
| 360 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 333 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 361 | 334 |
| 362 unsafeurl_item->DangerousDownloadValidated(); | 335 unsafeurl_item->DangerousDownloadValidated(); |
| 363 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 336 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
| 364 | 337 |
| 365 DownloadItemImpl* unsafefile_item = | 338 DownloadItemImpl* unsafefile_item = |
| 366 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 339 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 367 MockObserver unsafefile_observer(unsafefile_item); | 340 MockObserver unsafefile_observer(unsafefile_item); |
| 368 | 341 |
| 369 unsafefile_item->OnTargetPathDetermined( | |
| 370 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
| 371 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | |
| 372 EXPECT_FALSE(unsafefile_observer.CheckUpdated()); | |
| 373 unsafefile_item->OnAllDataSaved(1, ""); | 342 unsafefile_item->OnAllDataSaved(1, ""); |
| 374 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 343 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 375 unsafefile_item->OnContentCheckCompleted( | 344 unsafefile_item->OnContentCheckCompleted( |
| 376 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 345 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
| 377 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 346 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 378 | 347 |
| 379 unsafefile_item->DangerousDownloadValidated(); | 348 unsafefile_item->DangerousDownloadValidated(); |
| 380 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 349 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
| 381 } | 350 } |
| 382 | 351 |
| 383 // DownloadItemImpl::OnIntermediatePathDetermined will schedule a task to run | 352 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run |
| 384 // DownloadFileManager::RenameDownloadFile(). Once the rename | 353 // DownloadFileManager::RenameDownloadFile(). Once the rename |
| 385 // completes, DownloadItemImpl receives a notification with the new file | 354 // completes, DownloadItemImpl receives a notification with the new file |
| 386 // name. Check that observers are updated when the new filename is available and | 355 // name. Check that observers are updated when the new filename is available and |
| 387 // not before. | 356 // not before. |
| 388 TEST_F(DownloadItemTest, NotificationAfterOnIntermediatePathDetermined) { | 357 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { |
| 389 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 358 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 390 MockObserver observer(item); | 359 MockObserver observer(item); |
| 391 FilePath intermediate_path(kDummyPath); | 360 FilePath target_path(kDummyPath); |
| 392 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo")); | 361 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 393 scoped_refptr<MockDownloadFileManager> file_manager( | 362 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
| 394 new MockDownloadFileManager); | 363 EXPECT_CALL(*mock_file_manager(), |
| 395 EXPECT_CALL(*file_manager.get(), | |
| 396 RenameDownloadFile(_,intermediate_path,false,_)) | 364 RenameDownloadFile(_,intermediate_path,false,_)) |
| 397 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 365 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 398 | 366 |
| 399 item->OnIntermediatePathDetermined(file_manager.get(), intermediate_path); | 367 // Currently, a notification would be generated if the danger type is anything |
| 368 // other than NOT_DANGEROUS. |
| 369 item->OnDownloadTargetDetermined(target_path, |
| 370 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 371 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 372 intermediate_path); |
| 400 EXPECT_FALSE(observer.CheckUpdated()); | 373 EXPECT_FALSE(observer.CheckUpdated()); |
| 401 RunAllPendingInMessageLoops(); | 374 RunAllPendingInMessageLoops(); |
| 402 EXPECT_TRUE(observer.CheckUpdated()); | 375 EXPECT_TRUE(observer.CheckUpdated()); |
| 403 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 376 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 404 } | 377 } |
| 405 | 378 |
| 406 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 379 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
| 407 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 380 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 408 MockObserver observer(item); | 381 MockObserver observer(item); |
| 409 | 382 |
| 410 item->TogglePause(); | 383 item->TogglePause(); |
| 411 ASSERT_TRUE(observer.CheckUpdated()); | 384 ASSERT_TRUE(observer.CheckUpdated()); |
| 412 | 385 |
| 413 item->TogglePause(); | 386 item->TogglePause(); |
| 414 ASSERT_TRUE(observer.CheckUpdated()); | 387 ASSERT_TRUE(observer.CheckUpdated()); |
| 415 } | 388 } |
| 416 | 389 |
| 417 TEST_F(DownloadItemTest, DisplayName) { | 390 TEST_F(DownloadItemTest, DisplayName) { |
| 418 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 391 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 419 item->OnTargetPathDetermined(FilePath(kDummyPath).AppendASCII("foo.bar"), | 392 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 420 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 393 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
| 421 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 394 EXPECT_EQ(FILE_PATH_LITERAL(""), |
| 395 item->GetFileNameToReportUser().value()); |
| 396 EXPECT_CALL(*mock_file_manager(), |
| 397 RenameDownloadFile(_,_,false,_)) |
| 398 .WillOnce(ScheduleRenameCallback(intermediate_path)); |
| 399 item->OnDownloadTargetDetermined(target_path, |
| 400 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 401 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 402 intermediate_path); |
| 403 RunAllPendingInMessageLoops(); |
| 422 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 404 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 423 item->GetFileNameToReportUser().value()); | 405 item->GetFileNameToReportUser().value()); |
| 424 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 406 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 425 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 407 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 426 item->GetFileNameToReportUser().value()); | 408 item->GetFileNameToReportUser().value()); |
| 427 } | 409 } |
| 428 | 410 |
| 429 static char external_data_test_string[] = "External data test"; | 411 static char external_data_test_string[] = "External data test"; |
| 430 static int destructor_called = 0; | 412 static int destructor_called = 0; |
| 431 | 413 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 EXPECT_EQ(3, destructor_called); | 475 EXPECT_EQ(3, destructor_called); |
| 494 } | 476 } |
| 495 | 477 |
| 496 // Test that the delegate is invoked after the download file is renamed. | 478 // Test that the delegate is invoked after the download file is renamed. |
| 497 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the | 479 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the |
| 498 // download is renamed to the intermediate name. | 480 // download is renamed to the intermediate name. |
| 499 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | 481 // Delegate::DownloadRenamedToFinalName() should be invoked after the final |
| 500 // rename. | 482 // rename. |
| 501 TEST_F(DownloadItemTest, CallbackAfterRename) { | 483 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 502 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 484 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 503 FilePath intermediate_path(kDummyPath); | 485 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 504 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo")); | 486 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 505 FilePath final_path(intermediate_path.AppendASCII("bar")); | 487 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
| 506 scoped_refptr<MockDownloadFileManager> file_manager( | 488 EXPECT_CALL(*mock_file_manager(), |
| 507 new MockDownloadFileManager); | |
| 508 EXPECT_CALL(*file_manager.get(), | |
| 509 RenameDownloadFile(item->GetGlobalId(), | 489 RenameDownloadFile(item->GetGlobalId(), |
| 510 intermediate_path, false, _)) | 490 intermediate_path, false, _)) |
| 511 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 491 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 512 // DownloadItemImpl should invoke this callback on the delegate once the | 492 // DownloadItemImpl should invoke this callback on the delegate once the |
| 513 // download is renamed to the intermediate name. Also check that GetFullPath() | 493 // download is renamed to the intermediate name. Also check that GetFullPath() |
| 514 // returns the intermediate path at the time of the call. | 494 // returns the intermediate path at the time of the call. |
| 515 EXPECT_CALL(*mock_delegate(), | 495 EXPECT_CALL(*mock_delegate(), |
| 516 DownloadRenamedToIntermediateName( | 496 DownloadRenamedToIntermediateName( |
| 517 AllOf(item, | 497 AllOf(item, |
| 518 Property(&DownloadItem::GetFullPath, | 498 Property(&DownloadItem::GetFullPath, |
| 519 new_intermediate_path)))); | 499 new_intermediate_path)))); |
| 520 item->OnTargetPathDetermined(final_path, | 500 item->OnDownloadTargetDetermined(final_path, |
| 521 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 501 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 522 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 502 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 523 item->OnIntermediatePathDetermined(file_manager.get(), intermediate_path); | 503 intermediate_path); |
| 524 RunAllPendingInMessageLoops(); | 504 RunAllPendingInMessageLoops(); |
| 525 // All the callbacks should have happened by now. | 505 // All the callbacks should have happened by now. |
| 526 ::testing::Mock::VerifyAndClearExpectations(file_manager.get()); | 506 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); |
| 527 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 507 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 528 | 508 |
| 529 item->OnAllDataSaved(10, ""); | 509 item->OnAllDataSaved(10, ""); |
| 530 EXPECT_CALL(*file_manager.get(), | 510 EXPECT_CALL(*mock_file_manager(), |
| 531 RenameDownloadFile(item->GetGlobalId(), | 511 RenameDownloadFile(item->GetGlobalId(), |
| 532 final_path, true, _)) | 512 final_path, true, _)) |
| 533 .WillOnce(ScheduleRenameCallback(final_path)); | 513 .WillOnce(ScheduleRenameCallback(final_path)); |
| 534 EXPECT_CALL(*file_manager.get(), | 514 EXPECT_CALL(*mock_file_manager(), |
| 535 CompleteDownload(item->GetGlobalId(), _)) | 515 CompleteDownload(item->GetGlobalId(), _)) |
| 536 .WillOnce(ScheduleCompleteCallback()); | 516 .WillOnce(ScheduleCompleteCallback()); |
| 537 // DownloadItemImpl should invoke this callback on the delegate after the | 517 // DownloadItemImpl should invoke this callback on the delegate after the |
| 538 // final rename has completed. Also check that GetFullPath() and | 518 // final rename has completed. Also check that GetFullPath() and |
| 539 // GetTargetFilePath() return the final path at the time of the call. | 519 // GetTargetFilePath() return the final path at the time of the call. |
| 540 EXPECT_CALL(*mock_delegate(), | 520 EXPECT_CALL(*mock_delegate(), |
| 541 DownloadRenamedToFinalName( | 521 DownloadRenamedToFinalName( |
| 542 AllOf(item, | 522 AllOf(item, |
| 543 Property(&DownloadItem::GetFullPath, final_path), | 523 Property(&DownloadItem::GetFullPath, final_path), |
| 544 Property(&DownloadItem::GetTargetFilePath, | 524 Property(&DownloadItem::GetTargetFilePath, |
| 545 final_path)))); | 525 final_path)))); |
| 546 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); | 526 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); |
| 547 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) | 527 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) |
| 548 .WillOnce(Return(true)); | 528 .WillOnce(Return(true)); |
| 549 item->OnDownloadCompleting(file_manager.get()); | 529 item->OnDownloadCompleting(); |
| 550 RunAllPendingInMessageLoops(); | 530 RunAllPendingInMessageLoops(); |
| 551 ::testing::Mock::VerifyAndClearExpectations(file_manager.get()); | 531 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); |
| 552 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 532 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 553 } | 533 } |
| 554 | 534 |
| 555 TEST_F(DownloadItemTest, Interrupted) { | 535 TEST_F(DownloadItemTest, Interrupted) { |
| 556 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 536 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 557 | 537 |
| 558 const content::DownloadInterruptReason reason( | 538 const content::DownloadInterruptReason reason( |
| 559 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 539 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 560 | 540 |
| 561 // Confirm interrupt sets state properly. | 541 // Confirm interrupt sets state properly. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 583 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 563 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 584 | 564 |
| 585 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 565 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
| 586 item->OnDownloadedFileRemoved(); | 566 item->OnDownloadedFileRemoved(); |
| 587 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 567 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
| 588 } | 568 } |
| 589 | 569 |
| 590 TEST(MockDownloadItem, Compiles) { | 570 TEST(MockDownloadItem, Compiles) { |
| 591 MockDownloadItem mock_item; | 571 MockDownloadItem mock_item; |
| 592 } | 572 } |
| OLD | NEW |