| 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 MockDownloadFileManager::MockDownloadFileManager() | 124 MockDownloadFileManager::MockDownloadFileManager() |
| 125 : DownloadFileManager(new MockDownloadFileFactory) { | 125 : DownloadFileManager(new MockDownloadFileFactory) { |
| 126 } | 126 } |
| 127 | 127 |
| 128 } // namespace | 128 } // namespace |
| 129 | 129 |
| 130 class DownloadItemTest : public testing::Test { | 130 class DownloadItemTest : public testing::Test { |
| 131 public: | 131 public: |
| 132 class MockObserver : public DownloadItem::Observer { | 132 class MockObserver : public DownloadItem::Observer { |
| 133 public: | 133 public: |
| 134 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { | 134 explicit MockObserver(DownloadItem* item) |
| 135 : item_(item), |
| 136 removed_(false), |
| 137 destroyed_(false), |
| 138 updated_(false) { |
| 135 item_->AddObserver(this); | 139 item_->AddObserver(this); |
| 136 } | 140 } |
| 137 ~MockObserver() { item_->RemoveObserver(this); } | 141 |
| 142 virtual ~MockObserver() { |
| 143 if (item_) item_->RemoveObserver(this); |
| 144 } |
| 145 |
| 146 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 147 removed_ = true; |
| 148 } |
| 138 | 149 |
| 139 virtual void OnDownloadUpdated(DownloadItem* download) { | 150 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 140 updated_ = true; | 151 updated_ = true; |
| 141 } | 152 } |
| 142 | 153 |
| 143 virtual void OnDownloadOpened(DownloadItem* download) { } | 154 virtual void OnDownloadOpened(DownloadItem* download) { |
| 155 } |
| 156 |
| 157 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 158 destroyed_ = true; |
| 159 item_->RemoveObserver(this); |
| 160 item_ = NULL; |
| 161 } |
| 162 |
| 163 bool CheckRemoved() { |
| 164 return removed_; |
| 165 } |
| 166 |
| 167 bool CheckDestroyed() { |
| 168 return destroyed_; |
| 169 } |
| 144 | 170 |
| 145 bool CheckUpdated() { | 171 bool CheckUpdated() { |
| 146 bool was_updated = updated_; | 172 bool was_updated = updated_; |
| 147 updated_ = false; | 173 updated_ = false; |
| 148 return was_updated; | 174 return was_updated; |
| 149 } | 175 } |
| 150 | 176 |
| 151 private: | 177 private: |
| 152 DownloadItem* item_; | 178 DownloadItem* item_; |
| 179 bool removed_; |
| 180 bool destroyed_; |
| 153 bool updated_; | 181 bool updated_; |
| 154 }; | 182 }; |
| 155 | 183 |
| 156 DownloadItemTest() | 184 DownloadItemTest() |
| 157 : ui_thread_(BrowserThread::UI, &loop_), | 185 : ui_thread_(BrowserThread::UI, &loop_), |
| 158 file_thread_(BrowserThread::FILE, &loop_), | 186 file_thread_(BrowserThread::FILE, &loop_), |
| 159 file_manager_(new MockDownloadFileManager), | 187 file_manager_(new MockDownloadFileManager), |
| 160 delegate_(file_manager_.get()) { | 188 delegate_(file_manager_.get()) { |
| 161 } | 189 } |
| 162 | 190 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 323 } |
| 296 | 324 |
| 297 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 325 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
| 298 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 326 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 299 MockObserver observer(item); | 327 MockObserver observer(item); |
| 300 | 328 |
| 301 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 329 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| 302 ASSERT_TRUE(observer.CheckUpdated()); | 330 ASSERT_TRUE(observer.CheckUpdated()); |
| 303 } | 331 } |
| 304 | 332 |
| 333 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
| 334 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 335 MockObserver observer(item); |
| 336 |
| 337 DestroyDownloadItem(item); |
| 338 ASSERT_TRUE(observer.CheckDestroyed()); |
| 339 } |
| 340 |
| 305 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 341 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
| 306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 307 MockObserver observer(item); | 343 MockObserver observer(item); |
| 308 | 344 |
| 309 item->Remove(); | 345 item->Remove(); |
| 310 ASSERT_TRUE(observer.CheckUpdated()); | 346 ASSERT_TRUE(observer.CheckUpdated()); |
| 347 ASSERT_TRUE(observer.CheckRemoved()); |
| 311 } | 348 } |
| 312 | 349 |
| 313 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 350 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
| 314 // Setting to NOT_DANGEROUS does not trigger a notification. | 351 // Setting to NOT_DANGEROUS does not trigger a notification. |
| 315 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 352 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 316 MockObserver safe_observer(safe_item); | 353 MockObserver safe_observer(safe_item); |
| 317 | 354 |
| 318 safe_item->OnAllDataSaved(1, ""); | 355 safe_item->OnAllDataSaved(1, ""); |
| 319 EXPECT_TRUE(safe_observer.CheckUpdated()); | 356 EXPECT_TRUE(safe_observer.CheckUpdated()); |
| 320 safe_item->OnContentCheckCompleted( | 357 safe_item->OnContentCheckCompleted( |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 438 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 402 intermediate_path); | 439 intermediate_path); |
| 403 RunAllPendingInMessageLoops(); | 440 RunAllPendingInMessageLoops(); |
| 404 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 441 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
| 405 item->GetFileNameToReportUser().value()); | 442 item->GetFileNameToReportUser().value()); |
| 406 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 443 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
| 407 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 444 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
| 408 item->GetFileNameToReportUser().value()); | 445 item->GetFileNameToReportUser().value()); |
| 409 } | 446 } |
| 410 | 447 |
| 411 static char external_data_test_string[] = "External data test"; | |
| 412 static int destructor_called = 0; | |
| 413 | |
| 414 class TestExternalData : public DownloadItem::ExternalData { | |
| 415 public: | |
| 416 int value; | |
| 417 virtual ~TestExternalData() { | |
| 418 destructor_called++; | |
| 419 } | |
| 420 }; | |
| 421 | |
| 422 TEST_F(DownloadItemTest, ExternalData) { | |
| 423 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | |
| 424 const DownloadItemImpl* const_item = item; | |
| 425 | |
| 426 // Shouldn't be anything there before set. | |
| 427 EXPECT_EQ(NULL, item->GetExternalData(&external_data_test_string)); | |
| 428 EXPECT_EQ(NULL, const_item->GetExternalData(&external_data_test_string)); | |
| 429 | |
| 430 TestExternalData* test1(new TestExternalData()); | |
| 431 test1->value = 2; | |
| 432 | |
| 433 // Should be able to get back what you set. | |
| 434 item->SetExternalData(&external_data_test_string, test1); | |
| 435 TestExternalData* test_result = | |
| 436 static_cast<TestExternalData*>( | |
| 437 item->GetExternalData(&external_data_test_string)); | |
| 438 EXPECT_EQ(test1, test_result); | |
| 439 | |
| 440 // Ditto for const lookup. | |
| 441 const TestExternalData* test_const_result = | |
| 442 static_cast<const TestExternalData*>( | |
| 443 const_item->GetExternalData(&external_data_test_string)); | |
| 444 EXPECT_EQ(static_cast<const TestExternalData*>(test1), | |
| 445 test_const_result); | |
| 446 | |
| 447 // Destructor should be called if value overwritten. New value | |
| 448 // should then be retrievable. | |
| 449 TestExternalData* test2(new TestExternalData()); | |
| 450 test2->value = 3; | |
| 451 EXPECT_EQ(0, destructor_called); | |
| 452 item->SetExternalData(&external_data_test_string, test2); | |
| 453 EXPECT_EQ(1, destructor_called); | |
| 454 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2), | |
| 455 item->GetExternalData(&external_data_test_string)); | |
| 456 | |
| 457 // Overwriting with the same value shouldn't do anything. | |
| 458 EXPECT_EQ(1, destructor_called); | |
| 459 item->SetExternalData(&external_data_test_string, test2); | |
| 460 EXPECT_EQ(1, destructor_called); | |
| 461 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2), | |
| 462 item->GetExternalData(&external_data_test_string)); | |
| 463 | |
| 464 // Overwriting with NULL should result in destruction. | |
| 465 item->SetExternalData(&external_data_test_string, NULL); | |
| 466 EXPECT_EQ(2, destructor_called); | |
| 467 | |
| 468 // Destroying the download item should destroy the external data. | |
| 469 | |
| 470 TestExternalData* test3(new TestExternalData()); | |
| 471 item->SetExternalData(&external_data_test_string, test3); | |
| 472 EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test3), | |
| 473 item->GetExternalData(&external_data_test_string)); | |
| 474 DestroyDownloadItem(item); | |
| 475 EXPECT_EQ(3, destructor_called); | |
| 476 } | |
| 477 | |
| 478 // Test that the delegate is invoked after the download file is renamed. | 448 // Test that the delegate is invoked after the download file is renamed. |
| 479 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the | 449 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the |
| 480 // download is renamed to the intermediate name. | 450 // download is renamed to the intermediate name. |
| 481 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | 451 // Delegate::DownloadRenamedToFinalName() should be invoked after the final |
| 482 // rename. | 452 // rename. |
| 483 TEST_F(DownloadItemTest, CallbackAfterRename) { | 453 TEST_F(DownloadItemTest, CallbackAfterRename) { |
| 484 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 454 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 485 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 455 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
| 486 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 456 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
| 487 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 457 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 533 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 564 | 534 |
| 565 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 535 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
| 566 item->OnDownloadedFileRemoved(); | 536 item->OnDownloadedFileRemoved(); |
| 567 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 537 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
| 568 } | 538 } |
| 569 | 539 |
| 570 TEST(MockDownloadItem, Compiles) { | 540 TEST(MockDownloadItem, Compiles) { |
| 571 MockDownloadItem mock_item; | 541 MockDownloadItem mock_item; |
| 572 } | 542 } |
| OLD | NEW |