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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/download/download_status_updater.h" | 9 #include "chrome/browser/download/download_status_updater.h" |
10 #include "content/public/test/mock_download_item.h" | 10 #include "content/public/test/mock_download_item.h" |
11 #include "content/public/test/mock_download_manager.h" | 11 #include "content/public/test/mock_download_manager.h" |
12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 | |
17 using ::testing::AtLeast; | 16 using ::testing::AtLeast; |
18 using ::testing::Mock; | 17 using ::testing::Mock; |
19 using ::testing::Return; | 18 using ::testing::Return; |
20 using ::testing::SetArgPointee; | 19 using ::testing::SetArgPointee; |
21 using ::testing::StrictMock; | 20 using ::testing::StrictMock; |
22 using ::testing::_; | 21 using ::testing::_; |
23 | 22 |
24 class TestDownloadStatusUpdater : public DownloadStatusUpdater { | 23 class TestDownloadStatusUpdater : public DownloadStatusUpdater { |
| 24 public: |
| 25 TestDownloadStatusUpdater() : notification_count_(0), |
| 26 acceptable_notification_item_(NULL) { |
| 27 } |
| 28 void SetAcceptableNotificationItem(content::DownloadItem* item) { |
| 29 acceptable_notification_item_ = item; |
| 30 } |
| 31 size_t NotificationCount() { |
| 32 return notification_count_; |
| 33 } |
25 protected: | 34 protected: |
26 virtual void UpdateAppIconDownloadProgress() OVERRIDE { | 35 virtual void UpdateAppIconDownloadProgress( |
27 return; | 36 content::DownloadItem* download) OVERRIDE { |
| 37 ++notification_count_; |
| 38 if (acceptable_notification_item_) |
| 39 EXPECT_EQ(acceptable_notification_item_, download); |
28 } | 40 } |
| 41 private: |
| 42 size_t notification_count_; |
| 43 content::DownloadItem* acceptable_notification_item_; |
29 }; | 44 }; |
30 | 45 |
31 class DownloadStatusUpdaterTest : public testing::Test { | 46 class DownloadStatusUpdaterTest : public testing::Test { |
32 public: | 47 public: |
33 DownloadStatusUpdaterTest() | 48 DownloadStatusUpdaterTest() |
34 : updater_(new TestDownloadStatusUpdater()), | 49 : updater_(new TestDownloadStatusUpdater()), |
35 ui_thread_(content::BrowserThread::UI, &loop_) {} | 50 ui_thread_(content::BrowserThread::UI, &loop_) {} |
36 | 51 |
37 virtual ~DownloadStatusUpdaterTest() { | 52 virtual ~DownloadStatusUpdaterTest() { |
38 for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) { | 53 for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 content::MockDownloadItem* Item(int manager_index, int item_index) { | 128 content::MockDownloadItem* Item(int manager_index, int item_index) { |
114 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); | 129 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); |
115 DCHECK_GT(manager_items_[manager_index].size(), | 130 DCHECK_GT(manager_items_[manager_index].size(), |
116 static_cast<size_t>(item_index)); | 131 static_cast<size_t>(item_index)); |
117 // All DownloadItems in manager_items_ are MockDownloadItems. | 132 // All DownloadItems in manager_items_ are MockDownloadItems. |
118 return static_cast<content::MockDownloadItem*>( | 133 return static_cast<content::MockDownloadItem*>( |
119 manager_items_[manager_index][item_index]); | 134 manager_items_[manager_index][item_index]); |
120 } | 135 } |
121 | 136 |
122 // Set return values relevant to |DownloadStatusUpdater::GetProgress()| | 137 // Set return values relevant to |DownloadStatusUpdater::GetProgress()| |
123 // for the specified item | 138 // for the specified item. |
124 void SetItemValues(int manager_index, int item_index, | 139 void SetItemValues(int manager_index, int item_index, |
125 int received_bytes, int total_bytes) { | 140 int received_bytes, int total_bytes, bool notify) { |
126 EXPECT_CALL(*Item(manager_index, item_index), GetReceivedBytes()) | 141 content::MockDownloadItem* item(Item(manager_index, item_index)); |
| 142 EXPECT_CALL(*item, GetReceivedBytes()) |
127 .WillRepeatedly(Return(received_bytes)); | 143 .WillRepeatedly(Return(received_bytes)); |
128 EXPECT_CALL(*Item(manager_index, item_index), GetTotalBytes()) | 144 EXPECT_CALL(*item, GetTotalBytes()) |
129 .WillRepeatedly(Return(total_bytes)); | 145 .WillRepeatedly(Return(total_bytes)); |
| 146 if (notify) |
| 147 updater_->OnDownloadUpdated(item); |
130 } | 148 } |
131 | 149 |
132 // Transition specified item to completed. | 150 // Transition specified item to completed. |
133 void CompleteItem(int manager_index, int item_index) { | 151 void CompleteItem(int manager_index, int item_index) { |
134 content::MockDownloadItem* item(Item(manager_index, item_index)); | 152 content::MockDownloadItem* item(Item(manager_index, item_index)); |
135 EXPECT_CALL(*item, GetState()) | 153 EXPECT_CALL(*item, GetState()) |
136 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); | 154 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); |
137 EXPECT_CALL(*item, RemoveObserver(updater_)) | 155 EXPECT_CALL(*item, RemoveObserver(updater_)) |
138 .WillOnce(Return()); | 156 .WillOnce(Return()); |
139 updater_->OnDownloadUpdated(item); | 157 updater_->OnDownloadUpdated(item); |
(...skipping 10 matching lines...) Expand all Loading... |
150 Mock::VerifyAndClearExpectations(*sit); | 168 Mock::VerifyAndClearExpectations(*sit); |
151 } | 169 } |
152 | 170 |
153 std::vector<scoped_refptr<content::MockDownloadManager> > managers_; | 171 std::vector<scoped_refptr<content::MockDownloadManager> > managers_; |
154 // DownloadItem so that it can be assigned to the result of SearchDownloads. | 172 // DownloadItem so that it can be assigned to the result of SearchDownloads. |
155 typedef std::vector<content::DownloadItem*> Items; | 173 typedef std::vector<content::DownloadItem*> Items; |
156 std::vector<Items> manager_items_; | 174 std::vector<Items> manager_items_; |
157 | 175 |
158 // Pointer so we can verify that destruction triggers appropriate | 176 // Pointer so we can verify that destruction triggers appropriate |
159 // changes. | 177 // changes. |
160 DownloadStatusUpdater *updater_; | 178 TestDownloadStatusUpdater *updater_; |
161 | 179 |
162 // Thread so that the DownloadManager (which is a DeleteOnUIThread | 180 // Thread so that the DownloadManager (which is a DeleteOnUIThread |
163 // object) can be deleted. | 181 // object) can be deleted. |
164 // TODO(rdsmith): This can be removed when the DownloadManager | 182 // TODO(rdsmith): This can be removed when the DownloadManager |
165 // is no longer required to be deleted on the UI thread. | 183 // is no longer required to be deleted on the UI thread. |
166 MessageLoop loop_; | 184 MessageLoop loop_; |
167 content::TestBrowserThread ui_thread_; | 185 content::TestBrowserThread ui_thread_; |
168 }; | 186 }; |
169 | 187 |
170 // Test null updater. | 188 // Test null updater. |
(...skipping 20 matching lines...) Expand all Loading... |
191 } | 209 } |
192 | 210 |
193 // Test updater with non-null manager, including transition an item to | 211 // Test updater with non-null manager, including transition an item to |
194 // |content::DownloadItem::COMPLETE| and adding a new item. | 212 // |content::DownloadItem::COMPLETE| and adding a new item. |
195 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { | 213 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { |
196 SetupManagers(1); | 214 SetupManagers(1); |
197 AddItems(0, 3, 2); | 215 AddItems(0, 3, 2); |
198 LinkManager(0); | 216 LinkManager(0); |
199 | 217 |
200 // Prime items | 218 // Prime items |
201 SetItemValues(0, 0, 10, 20); | 219 SetItemValues(0, 0, 10, 20, false); |
202 SetItemValues(0, 1, 50, 60); | 220 SetItemValues(0, 1, 50, 60, false); |
203 SetItemValues(0, 2, 90, 90); | 221 SetItemValues(0, 2, 90, 90, false); |
204 | 222 |
205 float progress = -1; | 223 float progress = -1; |
206 int download_count = -1; | 224 int download_count = -1; |
207 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 225 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
208 EXPECT_FLOAT_EQ((10+50)/(20.0f+60), progress); | 226 EXPECT_FLOAT_EQ((10+50)/(20.0f+60), progress); |
209 EXPECT_EQ(2, download_count); | 227 EXPECT_EQ(2, download_count); |
210 | 228 |
211 // Transition one item to completed and confirm progress is updated | 229 // Transition one item to completed and confirm progress is updated |
212 // properly. | 230 // properly. |
213 CompleteItem(0, 0); | 231 CompleteItem(0, 0); |
214 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 232 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
215 EXPECT_FLOAT_EQ(50/60.0f, progress); | 233 EXPECT_FLOAT_EQ(50/60.0f, progress); |
216 EXPECT_EQ(1, download_count); | 234 EXPECT_EQ(1, download_count); |
217 | 235 |
218 // Add a new item to manager and confirm progress is updated properly. | 236 // Add a new item to manager and confirm progress is updated properly. |
219 AddItems(0, 1, 1); | 237 AddItems(0, 1, 1); |
220 updater_->ModelChanged(Manager(0)); | 238 updater_->ModelChanged(Manager(0)); |
221 SetItemValues(0, 3, 150, 200); | 239 SetItemValues(0, 3, 150, 200, false); |
222 | 240 |
223 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 241 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
224 EXPECT_FLOAT_EQ((50+150)/(60+200.0f), progress); | 242 EXPECT_FLOAT_EQ((50+150)/(60+200.0f), progress); |
225 EXPECT_EQ(2, download_count); | 243 EXPECT_EQ(2, download_count); |
226 } | 244 } |
227 | 245 |
| 246 // Test to ensure that the download progress notification is called correctly. |
| 247 TEST_F(DownloadStatusUpdaterTest, ProgressNotification) { |
| 248 size_t expected_notifications = updater_->NotificationCount(); |
| 249 SetupManagers(1); |
| 250 AddItems(0, 2, 2); |
| 251 LinkManager(0); |
| 252 |
| 253 // Expect two notifications, one for each item; which item will come first |
| 254 // isn't defined so it cannot be tested. |
| 255 expected_notifications += 2; |
| 256 ASSERT_EQ(expected_notifications, updater_->NotificationCount()); |
| 257 |
| 258 // Make progress on the first item. |
| 259 updater_->SetAcceptableNotificationItem(Item(0, 0)); |
| 260 SetItemValues(0, 0, 10, 20, true); |
| 261 ++expected_notifications; |
| 262 ASSERT_EQ(expected_notifications, updater_->NotificationCount()); |
| 263 |
| 264 // Second item completes! |
| 265 updater_->SetAcceptableNotificationItem(Item(0, 1)); |
| 266 CompleteItem(0, 1); |
| 267 ++expected_notifications; |
| 268 ASSERT_EQ(expected_notifications, updater_->NotificationCount()); |
| 269 |
| 270 // First item completes. |
| 271 updater_->SetAcceptableNotificationItem(Item(0, 0)); |
| 272 CompleteItem(0, 0); |
| 273 ++expected_notifications; |
| 274 ASSERT_EQ(expected_notifications, updater_->NotificationCount()); |
| 275 |
| 276 updater_->SetAcceptableNotificationItem(NULL); |
| 277 } |
| 278 |
228 // Confirm we recognize the situation where we have an unknown size. | 279 // Confirm we recognize the situation where we have an unknown size. |
229 TEST_F(DownloadStatusUpdaterTest, UnknownSize) { | 280 TEST_F(DownloadStatusUpdaterTest, UnknownSize) { |
230 SetupManagers(1); | 281 SetupManagers(1); |
231 AddItems(0, 2, 2); | 282 AddItems(0, 2, 2); |
232 LinkManager(0); | 283 LinkManager(0); |
233 | 284 |
234 // Prime items | 285 // Prime items |
235 SetItemValues(0, 0, 10, 20); | 286 SetItemValues(0, 0, 10, 20, false); |
236 SetItemValues(0, 1, 50, -1); | 287 SetItemValues(0, 1, 50, -1, false); |
237 | 288 |
238 float progress = -1; | 289 float progress = -1; |
239 int download_count = -1; | 290 int download_count = -1; |
240 EXPECT_FALSE(updater_->GetProgress(&progress, &download_count)); | 291 EXPECT_FALSE(updater_->GetProgress(&progress, &download_count)); |
241 } | 292 } |
242 | 293 |
243 // Test many null managers. | 294 // Test many null managers. |
244 TEST_F(DownloadStatusUpdaterTest, ManyManagersNoItems) { | 295 TEST_F(DownloadStatusUpdaterTest, ManyManagersNoItems) { |
245 SetupManagers(1); | 296 SetupManagers(1); |
246 AddItems(0, 0, 0); | 297 AddItems(0, 0, 0); |
(...skipping 22 matching lines...) Expand all Loading... |
269 } | 320 } |
270 | 321 |
271 // Test many managers with some non-complete items. | 322 // Test many managers with some non-complete items. |
272 TEST_F(DownloadStatusUpdaterTest, ManyManagersMixedItems) { | 323 TEST_F(DownloadStatusUpdaterTest, ManyManagersMixedItems) { |
273 SetupManagers(2); | 324 SetupManagers(2); |
274 AddItems(0, 3, 2); | 325 AddItems(0, 3, 2); |
275 LinkManager(0); | 326 LinkManager(0); |
276 AddItems(1, 3, 1); | 327 AddItems(1, 3, 1); |
277 LinkManager(1); | 328 LinkManager(1); |
278 | 329 |
279 SetItemValues(0, 0, 10, 20); | 330 SetItemValues(0, 0, 10, 20, false); |
280 SetItemValues(0, 1, 50, 60); | 331 SetItemValues(0, 1, 50, 60, false); |
281 SetItemValues(1, 0, 80, 90); | 332 SetItemValues(1, 0, 80, 90, false); |
282 | 333 |
283 float progress = -1; | 334 float progress = -1; |
284 int download_count = -1; | 335 int download_count = -1; |
285 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); | 336 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); |
286 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); | 337 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); |
287 EXPECT_EQ(3, download_count); | 338 EXPECT_EQ(3, download_count); |
288 } | 339 } |
OLD | NEW |