| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "chrome/browser/download/download_ui_controller.h" | 9 #include "chrome/browser/download/download_ui_controller.h" |
| 10 #include "content/public/test/mock_download_item.h" | 10 #include "content/public/test/mock_download_item.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 class DownloadUIControllerTest : public testing::Test { | 48 class DownloadUIControllerTest : public testing::Test { |
| 49 public: | 49 public: |
| 50 DownloadUIControllerTest(); | 50 DownloadUIControllerTest(); |
| 51 | 51 |
| 52 protected: | 52 protected: |
| 53 // testing::Test | 53 // testing::Test |
| 54 virtual void SetUp() OVERRIDE; | 54 virtual void SetUp() OVERRIDE; |
| 55 | 55 |
| 56 // Returns a MockDownloadItem that has AddObserver and RemoveObserver | |
| 57 // expectations set up to store the observer in |item_observer_|. | |
| 58 scoped_ptr<MockDownloadItem> GetMockDownload(); | |
| 59 | |
| 60 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned | 56 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned |
| 61 // delegate results in the DownloadItem* being stored in |received_item_|. | 57 // delegate results in the DownloadItem* being stored in |received_item_|. |
| 62 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); | 58 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); |
| 63 | 59 |
| 64 MockDownloadManager* manager() { return manager_.get(); } | 60 MockDownloadManager* manager() { return manager_.get(); } |
| 65 content::DownloadManager::Observer* manager_observer() { | 61 content::DownloadManager::Observer* manager_observer() { |
| 66 return manager_observer_; | 62 return manager_observer_; |
| 67 } | 63 } |
| 68 content::DownloadItem::Observer* item_observer() { return item_observer_; } | |
| 69 content::DownloadItem* received_item() { return received_item_; } | 64 content::DownloadItem* received_item() { return received_item_; } |
| 70 | 65 |
| 71 private: | 66 private: |
| 72 scoped_ptr<MockDownloadManager> manager_; | 67 scoped_ptr<MockDownloadManager> manager_; |
| 73 content::DownloadManager::Observer* manager_observer_; | 68 content::DownloadManager::Observer* manager_observer_; |
| 74 content::DownloadItem::Observer* item_observer_; | |
| 75 content::DownloadItem* received_item_; | 69 content::DownloadItem* received_item_; |
| 76 | 70 |
| 77 base::WeakPtrFactory<content::DownloadItem*> receiver_factory_; | 71 base::WeakPtrFactory<content::DownloadItem*> receiver_factory_; |
| 78 }; | 72 }; |
| 79 | 73 |
| 80 DownloadUIControllerTest::DownloadUIControllerTest() | 74 DownloadUIControllerTest::DownloadUIControllerTest() |
| 81 : manager_observer_(NULL), | 75 : manager_observer_(NULL), |
| 82 item_observer_(NULL), | |
| 83 received_item_(NULL), | 76 received_item_(NULL), |
| 84 receiver_factory_(&received_item_) { | 77 receiver_factory_(&received_item_) { |
| 85 } | 78 } |
| 86 | 79 |
| 87 void DownloadUIControllerTest::SetUp() { | 80 void DownloadUIControllerTest::SetUp() { |
| 88 manager_.reset(new testing::StrictMock<MockDownloadManager>()); | 81 manager_.reset(new testing::StrictMock<MockDownloadManager>()); |
| 89 EXPECT_CALL(*manager_, AddObserver(_)) | 82 EXPECT_CALL(*manager_, AddObserver(_)) |
| 90 .WillOnce(SaveArg<0>(&manager_observer_)); | 83 .WillOnce(SaveArg<0>(&manager_observer_)); |
| 91 EXPECT_CALL(*manager_, RemoveObserver(_)) | 84 EXPECT_CALL(*manager_, RemoveObserver(_)) |
| 92 .WillOnce(Assign(&manager_observer_, | 85 .WillOnce(Assign(&manager_observer_, |
| 93 static_cast<content::DownloadManager::Observer*>(NULL))); | 86 static_cast<content::DownloadManager::Observer*>(NULL))); |
| 94 EXPECT_CALL(*manager_, GetAllDownloads(_)); | 87 EXPECT_CALL(*manager_, GetAllDownloads(_)); |
| 95 } | 88 } |
| 96 | 89 |
| 97 scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() { | |
| 98 scoped_ptr<MockDownloadItem> item( | |
| 99 new testing::StrictMock<MockDownloadItem>()); | |
| 100 EXPECT_CALL(*item, AddObserver(_)) | |
| 101 .WillOnce(SaveArg<0>(&item_observer_)); | |
| 102 EXPECT_CALL(*item, RemoveObserver(_)) | |
| 103 .WillOnce(Assign(&item_observer_, | |
| 104 static_cast<content::DownloadItem::Observer*>(NULL))); | |
| 105 return item.Pass(); | |
| 106 } | |
| 107 | |
| 108 scoped_ptr<DownloadUIController::Delegate> | 90 scoped_ptr<DownloadUIController::Delegate> |
| 109 DownloadUIControllerTest::GetTestDelegate() { | 91 DownloadUIControllerTest::GetTestDelegate() { |
| 110 scoped_ptr<DownloadUIController::Delegate> delegate( | 92 scoped_ptr<DownloadUIController::Delegate> delegate( |
| 111 new TestDelegate(receiver_factory_.GetWeakPtr())); | 93 new TestDelegate(receiver_factory_.GetWeakPtr())); |
| 112 return delegate.Pass(); | 94 return delegate.Pass(); |
| 113 } | 95 } |
| 114 | 96 |
| 115 // Normal downloads that are constructed in the IN_PROGRESS state should be | 97 // Normal downloads that are constructed in the IN_PROGRESS state should be |
| 116 // presented to the UI when GetTargetFilePath() returns a non-empty path. | 98 // presented to the UI when GetTargetFilePath() returns a non-empty path. |
| 117 // I.e. once the download target has been determined. | 99 // I.e. once the download target has been determined. |
| 118 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { | 100 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { |
| 119 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 101 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); |
| 120 DownloadUIController controller(manager(), GetTestDelegate()); | 102 DownloadUIController controller(manager(), GetTestDelegate()); |
| 121 EXPECT_CALL(*item, GetTargetFilePath()) | 103 EXPECT_CALL(*item, GetTargetFilePath()) |
| 122 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 104 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
| 123 EXPECT_CALL(*item, GetState()) | 105 EXPECT_CALL(*item, GetState()) |
| 124 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | 106 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); |
| 125 | 107 |
| 126 ASSERT_TRUE(manager_observer()); | 108 ASSERT_TRUE(manager_observer()); |
| 127 manager_observer()->OnDownloadCreated(manager(), item.get()); | 109 manager_observer()->OnDownloadCreated(manager(), item.get()); |
| 128 | 110 |
| 129 // The destination for the download hasn't been determined yet. It should not | 111 // The destination for the download hasn't been determined yet. It should not |
| 130 // be displayed. | 112 // be displayed. |
| 131 EXPECT_FALSE(received_item()); | 113 EXPECT_FALSE(received_item()); |
| 132 ASSERT_TRUE(item_observer()); | |
| 133 | 114 |
| 134 // Once the destination has been determined, then it should be displayed. | 115 // Once the destination has been determined, then it should be displayed. |
| 135 EXPECT_CALL(*item, GetTargetFilePath()) | 116 EXPECT_CALL(*item, GetTargetFilePath()) |
| 136 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | 117 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
| 137 item_observer()->OnDownloadUpdated(item.get()); | 118 item->NotifyObserversDownloadUpdated(); |
| 138 | 119 |
| 139 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 120 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); |
| 140 } | 121 } |
| 141 | 122 |
| 142 // Downloads that have a target path on creation and are in the IN_PROGRESS | 123 // Downloads that have a target path on creation and are in the IN_PROGRESS |
| 143 // state should be displayed in the UI immediately without requiring an | 124 // state should be displayed in the UI immediately without requiring an |
| 144 // additional OnDownloadUpdated() notification. | 125 // additional OnDownloadUpdated() notification. |
| 145 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { | 126 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { |
| 146 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 127 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); |
| 147 DownloadUIController controller(manager(), GetTestDelegate()); | 128 DownloadUIController controller(manager(), GetTestDelegate()); |
| 148 EXPECT_CALL(*item, GetTargetFilePath()) | 129 EXPECT_CALL(*item, GetTargetFilePath()) |
| 149 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | 130 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
| 150 EXPECT_CALL(*item, GetState()) | 131 EXPECT_CALL(*item, GetState()) |
| 151 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | 132 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); |
| 152 | 133 |
| 153 ASSERT_TRUE(manager_observer()); | 134 ASSERT_TRUE(manager_observer()); |
| 154 manager_observer()->OnDownloadCreated(manager(), item.get()); | 135 manager_observer()->OnDownloadCreated(manager(), item.get()); |
| 155 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 136 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); |
| 156 } | 137 } |
| 157 | 138 |
| 158 // History downloads (downloads that are not in IN_PROGRESS on create) should | 139 // History downloads (downloads that are not in IN_PROGRESS on create) should |
| 159 // not be displayed on the shelf. | 140 // not be displayed on the shelf. |
| 160 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { | 141 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { |
| 161 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 142 scoped_ptr<MockDownloadItem> item(new MockDownloadItem); |
| 162 DownloadUIController controller(manager(), GetTestDelegate()); | 143 DownloadUIController controller(manager(), GetTestDelegate()); |
| 163 EXPECT_CALL(*item, GetState()) | 144 EXPECT_CALL(*item, GetState()) |
| 164 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | 145 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
| 165 | 146 |
| 166 ASSERT_TRUE(manager_observer()); | 147 ASSERT_TRUE(manager_observer()); |
| 167 manager_observer()->OnDownloadCreated(manager(), item.get()); | 148 manager_observer()->OnDownloadCreated(manager(), item.get()); |
| 168 EXPECT_FALSE(received_item()); | 149 EXPECT_FALSE(received_item()); |
| 169 | 150 |
| 170 item_observer()->OnDownloadUpdated(item.get()); | 151 item->NotifyObserversDownloadUpdated(); |
| 171 EXPECT_FALSE(received_item()); | 152 EXPECT_FALSE(received_item()); |
| 172 } | 153 } |
| 173 | 154 |
| 174 } // namespace | 155 } // namespace |
| OLD | NEW |