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/bind.h" |
5 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
6 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
9 #include "chrome/browser/download/download_ui_controller.h" | 10 #include "chrome/browser/download/download_ui_controller.h" |
10 #include "content/public/test/mock_download_item.h" | 11 #include "content/public/test/mock_download_item.h" |
11 #include "content/public/test/mock_download_manager.h" | 12 #include "content/public/test/mock_download_manager.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 virtual void SetUp() OVERRIDE; | 55 virtual void SetUp() OVERRIDE; |
55 | 56 |
56 // Returns a MockDownloadItem that has AddObserver and RemoveObserver | 57 // Returns a MockDownloadItem that has AddObserver and RemoveObserver |
57 // expectations set up to store the observer in |item_observer_|. | 58 // expectations set up to store the observer in |item_observer_|. |
58 scoped_ptr<MockDownloadItem> GetMockDownload(); | 59 scoped_ptr<MockDownloadItem> GetMockDownload(); |
59 | 60 |
60 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned | 61 // Returns a TestDelegate. Invoking NotifyDownloadStarting on the returned |
61 // delegate results in the DownloadItem* being stored in |received_item_|. | 62 // delegate results in the DownloadItem* being stored in |received_item_|. |
62 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); | 63 scoped_ptr<DownloadUIController::Delegate> GetTestDelegate(); |
63 | 64 |
| 65 DownloadUIController::DownloadFilter NewDownloadPredicate(bool value); |
| 66 |
64 MockDownloadManager* manager() { return manager_.get(); } | 67 MockDownloadManager* manager() { return manager_.get(); } |
65 content::DownloadManager::Observer* manager_observer() { | 68 content::DownloadManager::Observer* manager_observer() { |
66 return manager_observer_; | 69 return manager_observer_; |
67 } | 70 } |
68 content::DownloadItem::Observer* item_observer() { return item_observer_; } | 71 content::DownloadItem::Observer* item_observer() { return item_observer_; } |
69 content::DownloadItem* received_item() { return received_item_; } | 72 content::DownloadItem* received_item() { return received_item_; } |
70 | 73 |
71 private: | 74 private: |
72 scoped_ptr<MockDownloadManager> manager_; | 75 scoped_ptr<MockDownloadManager> manager_; |
73 content::DownloadManager::Observer* manager_observer_; | 76 content::DownloadManager::Observer* manager_observer_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 return item.Pass(); | 108 return item.Pass(); |
106 } | 109 } |
107 | 110 |
108 scoped_ptr<DownloadUIController::Delegate> | 111 scoped_ptr<DownloadUIController::Delegate> |
109 DownloadUIControllerTest::GetTestDelegate() { | 112 DownloadUIControllerTest::GetTestDelegate() { |
110 scoped_ptr<DownloadUIController::Delegate> delegate( | 113 scoped_ptr<DownloadUIController::Delegate> delegate( |
111 new TestDelegate(receiver_factory_.GetWeakPtr())); | 114 new TestDelegate(receiver_factory_.GetWeakPtr())); |
112 return delegate.Pass(); | 115 return delegate.Pass(); |
113 } | 116 } |
114 | 117 |
115 // Normal downloads that are constructed in the IN_PROGRESS state should be | 118 bool StaticDownloadItemPredicate(bool value, const content::DownloadItem*) { |
116 // presented to the UI when GetTargetFilePath() returns a non-empty path. | 119 return value; |
117 // I.e. once the download target has been determined. | 120 } |
| 121 |
| 122 DownloadUIController::DownloadFilter |
| 123 DownloadUIControllerTest::NewDownloadPredicate(bool value) { |
| 124 return base::Bind(&StaticDownloadItemPredicate, value); |
| 125 } |
| 126 |
| 127 // New downloads should be presented to the UI when GetTargetFilePath() returns |
| 128 // a non-empty path. I.e. once the download target has been determined. |
118 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { | 129 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) { |
119 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 130 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
120 DownloadUIController controller(manager(), GetTestDelegate()); | 131 DownloadUIController controller( |
| 132 manager(), NewDownloadPredicate(true), GetTestDelegate()); |
121 EXPECT_CALL(*item, GetTargetFilePath()) | 133 EXPECT_CALL(*item, GetTargetFilePath()) |
122 .WillOnce(ReturnRefOfCopy(base::FilePath())); | 134 .WillOnce(ReturnRefOfCopy(base::FilePath())); |
123 EXPECT_CALL(*item, GetState()) | 135 EXPECT_CALL(*item, GetState()) |
124 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | 136 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); |
125 | 137 |
126 ASSERT_TRUE(manager_observer()); | 138 ASSERT_TRUE(manager_observer()); |
127 manager_observer()->OnDownloadCreated(manager(), item.get()); | 139 manager_observer()->OnDownloadCreated(manager(), item.get()); |
128 | 140 |
129 // The destination for the download hasn't been determined yet. It should not | 141 // The destination for the download hasn't been determined yet. It should not |
130 // be displayed. | 142 // be displayed. |
131 EXPECT_FALSE(received_item()); | 143 EXPECT_FALSE(received_item()); |
132 ASSERT_TRUE(item_observer()); | 144 ASSERT_TRUE(item_observer()); |
133 | 145 |
134 // Once the destination has been determined, then it should be displayed. | 146 // Once the destination has been determined, then it should be displayed. |
135 EXPECT_CALL(*item, GetTargetFilePath()) | 147 EXPECT_CALL(*item, GetTargetFilePath()) |
136 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | 148 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
137 item_observer()->OnDownloadUpdated(item.get()); | 149 item_observer()->OnDownloadUpdated(item.get()); |
138 | 150 |
139 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 151 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); |
140 } | 152 } |
141 | 153 |
| 154 // A download that's created in an interrupted state should also be displayed if |
| 155 // the new download filter flags it as a new download. |
| 156 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic_Interrupted) { |
| 157 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
| 158 DownloadUIController controller( |
| 159 manager(), NewDownloadPredicate(true), GetTestDelegate()); |
| 160 EXPECT_CALL(*item, GetTargetFilePath()) |
| 161 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
| 162 EXPECT_CALL(*item, GetState()) |
| 163 .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); |
| 164 |
| 165 ASSERT_TRUE(manager_observer()); |
| 166 manager_observer()->OnDownloadCreated(manager(), item.get()); |
| 167 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); |
| 168 } |
| 169 |
142 // Downloads that have a target path on creation and are in the IN_PROGRESS | 170 // 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 | 171 // state should be displayed in the UI immediately without requiring an |
144 // additional OnDownloadUpdated() notification. | 172 // additional OnDownloadUpdated() notification. |
145 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { | 173 TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) { |
146 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 174 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
147 DownloadUIController controller(manager(), GetTestDelegate()); | 175 DownloadUIController controller( |
| 176 manager(), NewDownloadPredicate(true), GetTestDelegate()); |
148 EXPECT_CALL(*item, GetTargetFilePath()) | 177 EXPECT_CALL(*item, GetTargetFilePath()) |
149 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); | 178 .WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo")))); |
150 EXPECT_CALL(*item, GetState()) | 179 EXPECT_CALL(*item, GetState()) |
151 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); | 180 .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS)); |
152 | 181 |
153 ASSERT_TRUE(manager_observer()); | 182 ASSERT_TRUE(manager_observer()); |
154 manager_observer()->OnDownloadCreated(manager(), item.get()); | 183 manager_observer()->OnDownloadCreated(manager(), item.get()); |
155 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); | 184 EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item()); |
156 } | 185 } |
157 | 186 |
158 // History downloads (downloads that are not in IN_PROGRESS on create) should | 187 // Downloads that are flagged by the new download filter as not being a new |
159 // not be displayed on the shelf. | 188 // download at the time OnDownloadCreated is called should not be displayed in |
| 189 // the UI. |
160 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { | 190 TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) { |
161 scoped_ptr<MockDownloadItem> item = GetMockDownload(); | 191 scoped_ptr<MockDownloadItem> item = GetMockDownload(); |
162 DownloadUIController controller(manager(), GetTestDelegate()); | 192 DownloadUIController controller( |
| 193 manager(), NewDownloadPredicate(false), GetTestDelegate()); |
163 EXPECT_CALL(*item, GetState()) | 194 EXPECT_CALL(*item, GetState()) |
164 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | 195 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
165 | 196 |
166 ASSERT_TRUE(manager_observer()); | 197 ASSERT_TRUE(manager_observer()); |
167 manager_observer()->OnDownloadCreated(manager(), item.get()); | 198 manager_observer()->OnDownloadCreated(manager(), item.get()); |
168 EXPECT_FALSE(received_item()); | 199 EXPECT_FALSE(received_item()); |
169 | 200 |
170 item_observer()->OnDownloadUpdated(item.get()); | 201 item_observer()->OnDownloadUpdated(item.get()); |
171 EXPECT_FALSE(received_item()); | 202 EXPECT_FALSE(received_item()); |
172 } | 203 } |
173 | 204 |
174 } // namespace | 205 } // namespace |
OLD | NEW |