Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: chrome/browser/download/download_status_updater_unittest.cc

Issue 10827207: Mountain Lion: use the system download progress. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 16
17 using ::testing::AtLeast; 17 using ::testing::AtLeast;
18 using ::testing::Mock; 18 using ::testing::Mock;
19 using ::testing::Return; 19 using ::testing::Return;
20 using ::testing::SetArgPointee; 20 using ::testing::SetArgPointee;
21 using ::testing::StrictMock; 21 using ::testing::StrictMock;
22 using ::testing::_; 22 using ::testing::_;
23 23
24 class TestDownloadStatusUpdater : public DownloadStatusUpdater { 24 class TestDownloadStatusUpdater : public DownloadStatusUpdater {
25 public:
26 TestDownloadStatusUpdater() : started_count_(0),
27 progressed_count_(0),
28 completed_count_(0) {
29 }
30 size_t GetStartedCount() const { return started_count_; }
31 size_t GetProgressedCount() const { return progressed_count_; }
32 size_t GetCompletedCount() const { return completed_count_; }
25 protected: 33 protected:
26 virtual void UpdateAppIconDownloadProgress() OVERRIDE { 34 virtual void UpdateAppIconDownloadProgress() OVERRIDE {
27 return; 35 return;
28 } 36 }
37 virtual void UpdateDownloadProgressForItemStarted(
38 content::DownloadItem* download) OVERRIDE {
39 ++started_count_;
40 }
41 virtual void UpdateDownloadProgressForItemProgressed(
42 content::DownloadItem* download) OVERRIDE {
43 ++progressed_count_;
44 }
45 virtual void UpdateDownloadProgressForItemCompleted(
46 content::DownloadItem* download) OVERRIDE {
47 ++completed_count_;
48 }
49 private:
50 size_t started_count_;
51 size_t progressed_count_;
52 size_t completed_count_;
29 }; 53 };
30 54
31 class DownloadStatusUpdaterTest : public testing::Test { 55 class DownloadStatusUpdaterTest : public testing::Test {
32 public: 56 public:
33 DownloadStatusUpdaterTest() 57 DownloadStatusUpdaterTest()
34 : updater_(new TestDownloadStatusUpdater()), 58 : updater_(new TestDownloadStatusUpdater()),
35 ui_thread_(content::BrowserThread::UI, &loop_) {} 59 ui_thread_(content::BrowserThread::UI, &loop_) {}
36 60
37 virtual ~DownloadStatusUpdaterTest() { 61 virtual ~DownloadStatusUpdaterTest() {
38 for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) { 62 for (size_t mgr_idx = 0; mgr_idx < managers_.size(); ++mgr_idx) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 content::MockDownloadItem* Item(int manager_index, int item_index) { 137 content::MockDownloadItem* Item(int manager_index, int item_index) {
114 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index)); 138 DCHECK_GT(manager_items_.size(), static_cast<size_t>(manager_index));
115 DCHECK_GT(manager_items_[manager_index].size(), 139 DCHECK_GT(manager_items_[manager_index].size(),
116 static_cast<size_t>(item_index)); 140 static_cast<size_t>(item_index));
117 // All DownloadItems in manager_items_ are MockDownloadItems. 141 // All DownloadItems in manager_items_ are MockDownloadItems.
118 return static_cast<content::MockDownloadItem*>( 142 return static_cast<content::MockDownloadItem*>(
119 manager_items_[manager_index][item_index]); 143 manager_items_[manager_index][item_index]);
120 } 144 }
121 145
122 // Set return values relevant to |DownloadStatusUpdater::GetProgress()| 146 // Set return values relevant to |DownloadStatusUpdater::GetProgress()|
123 // for the specified item 147 // for the specified item.
124 void SetItemValues(int manager_index, int item_index, 148 void SetItemValues(int manager_index, int item_index,
125 int received_bytes, int total_bytes) { 149 int received_bytes, int total_bytes, bool notify) {
126 EXPECT_CALL(*Item(manager_index, item_index), GetReceivedBytes()) 150 content::MockDownloadItem* item(Item(manager_index, item_index));
151 EXPECT_CALL(*item, GetReceivedBytes())
127 .WillRepeatedly(Return(received_bytes)); 152 .WillRepeatedly(Return(received_bytes));
128 EXPECT_CALL(*Item(manager_index, item_index), GetTotalBytes()) 153 EXPECT_CALL(*item, GetTotalBytes())
129 .WillRepeatedly(Return(total_bytes)); 154 .WillRepeatedly(Return(total_bytes));
155 if (notify)
156 updater_->OnDownloadUpdated(item);
130 } 157 }
131 158
132 // Transition specified item to completed. 159 // Transition specified item to completed.
133 void CompleteItem(int manager_index, int item_index) { 160 void CompleteItem(int manager_index, int item_index) {
134 content::MockDownloadItem* item(Item(manager_index, item_index)); 161 content::MockDownloadItem* item(Item(manager_index, item_index));
135 EXPECT_CALL(*item, GetState()) 162 EXPECT_CALL(*item, GetState())
136 .WillRepeatedly(Return(content::DownloadItem::COMPLETE)); 163 .WillRepeatedly(Return(content::DownloadItem::COMPLETE));
137 EXPECT_CALL(*item, RemoveObserver(updater_)) 164 EXPECT_CALL(*item, RemoveObserver(updater_))
138 .WillOnce(Return()); 165 .WillOnce(Return());
139 updater_->OnDownloadUpdated(item); 166 updater_->OnDownloadUpdated(item);
(...skipping 10 matching lines...) Expand all
150 Mock::VerifyAndClearExpectations(*sit); 177 Mock::VerifyAndClearExpectations(*sit);
151 } 178 }
152 179
153 std::vector<scoped_refptr<content::MockDownloadManager> > managers_; 180 std::vector<scoped_refptr<content::MockDownloadManager> > managers_;
154 // DownloadItem so that it can be assigned to the result of SearchDownloads. 181 // DownloadItem so that it can be assigned to the result of SearchDownloads.
155 typedef std::vector<content::DownloadItem*> Items; 182 typedef std::vector<content::DownloadItem*> Items;
156 std::vector<Items> manager_items_; 183 std::vector<Items> manager_items_;
157 184
158 // Pointer so we can verify that destruction triggers appropriate 185 // Pointer so we can verify that destruction triggers appropriate
159 // changes. 186 // changes.
160 DownloadStatusUpdater *updater_; 187 TestDownloadStatusUpdater *updater_;
161 188
162 // Thread so that the DownloadManager (which is a DeleteOnUIThread 189 // Thread so that the DownloadManager (which is a DeleteOnUIThread
163 // object) can be deleted. 190 // object) can be deleted.
164 // TODO(rdsmith): This can be removed when the DownloadManager 191 // TODO(rdsmith): This can be removed when the DownloadManager
165 // is no longer required to be deleted on the UI thread. 192 // is no longer required to be deleted on the UI thread.
166 MessageLoop loop_; 193 MessageLoop loop_;
167 content::TestBrowserThread ui_thread_; 194 content::TestBrowserThread ui_thread_;
168 }; 195 };
169 196
170 // Test null updater. 197 // Test null updater.
(...skipping 20 matching lines...) Expand all
191 } 218 }
192 219
193 // Test updater with non-null manager, including transition an item to 220 // Test updater with non-null manager, including transition an item to
194 // |content::DownloadItem::COMPLETE| and adding a new item. 221 // |content::DownloadItem::COMPLETE| and adding a new item.
195 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) { 222 TEST_F(DownloadStatusUpdaterTest, OneManagerManyItems) {
196 SetupManagers(1); 223 SetupManagers(1);
197 AddItems(0, 3, 2); 224 AddItems(0, 3, 2);
198 LinkManager(0); 225 LinkManager(0);
199 226
200 // Prime items 227 // Prime items
201 SetItemValues(0, 0, 10, 20); 228 SetItemValues(0, 0, 10, 20, false);
202 SetItemValues(0, 1, 50, 60); 229 SetItemValues(0, 1, 50, 60, false);
203 SetItemValues(0, 2, 90, 90); 230 SetItemValues(0, 2, 90, 90, false);
204 231
205 float progress = -1; 232 float progress = -1;
206 int download_count = -1; 233 int download_count = -1;
207 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); 234 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count));
208 EXPECT_FLOAT_EQ((10+50)/(20.0f+60), progress); 235 EXPECT_FLOAT_EQ((10+50)/(20.0f+60), progress);
209 EXPECT_EQ(2, download_count); 236 EXPECT_EQ(2, download_count);
210 237
211 // Transition one item to completed and confirm progress is updated 238 // Transition one item to completed and confirm progress is updated
212 // properly. 239 // properly.
213 CompleteItem(0, 0); 240 CompleteItem(0, 0);
214 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); 241 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count));
215 EXPECT_FLOAT_EQ(50/60.0f, progress); 242 EXPECT_FLOAT_EQ(50/60.0f, progress);
216 EXPECT_EQ(1, download_count); 243 EXPECT_EQ(1, download_count);
217 244
218 // Add a new item to manager and confirm progress is updated properly. 245 // Add a new item to manager and confirm progress is updated properly.
219 AddItems(0, 1, 1); 246 AddItems(0, 1, 1);
220 updater_->ModelChanged(Manager(0)); 247 updater_->ModelChanged(Manager(0));
221 SetItemValues(0, 3, 150, 200); 248 SetItemValues(0, 3, 150, 200, false);
222 249
223 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); 250 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count));
224 EXPECT_FLOAT_EQ((50+150)/(60+200.0f), progress); 251 EXPECT_FLOAT_EQ((50+150)/(60+200.0f), progress);
225 EXPECT_EQ(2, download_count); 252 EXPECT_EQ(2, download_count);
226 } 253 }
227 254
255 // Test that the progress for items callbacks are called in a reasonable way.
256 TEST_F(DownloadStatusUpdaterTest, ProgressForItem) {
257 size_t orig_started = updater_->GetStartedCount();
258 size_t orig_progressed = updater_->GetProgressedCount();
259 size_t orig_completed = updater_->GetCompletedCount();
260 SetupManagers(1);
261 AddItems(0, 1, 1);
262 LinkManager(0);
263
264 EXPECT_GE(updater_->GetStartedCount(), orig_started);
Randy Smith (Not in Mondays) 2012/08/09 20:12:25 Why are these GE rather than GT (throughout this f
265 EXPECT_EQ(updater_->GetProgressedCount(), orig_progressed);
266 EXPECT_EQ(updater_->GetCompletedCount(), orig_completed);
267
268 // Make progress.
269 SetItemValues(0, 0, 10, 20, true);
270
271 EXPECT_GE(updater_->GetStartedCount(), orig_started);
272 EXPECT_GE(updater_->GetProgressedCount(), orig_progressed);
273 EXPECT_EQ(updater_->GetCompletedCount(), orig_completed);
274
275 // Transition one item to completed and confirm progress is updated
276 // properly.
277 CompleteItem(0, 0);
278
279 EXPECT_GE(updater_->GetStartedCount(), orig_started);
280 EXPECT_GE(updater_->GetProgressedCount(), orig_progressed);
281 EXPECT_GE(updater_->GetCompletedCount(), orig_completed);
282 }
283
228 // Confirm we recognize the situation where we have an unknown size. 284 // Confirm we recognize the situation where we have an unknown size.
229 TEST_F(DownloadStatusUpdaterTest, UnknownSize) { 285 TEST_F(DownloadStatusUpdaterTest, UnknownSize) {
230 SetupManagers(1); 286 SetupManagers(1);
231 AddItems(0, 2, 2); 287 AddItems(0, 2, 2);
232 LinkManager(0); 288 LinkManager(0);
233 289
234 // Prime items 290 // Prime items
235 SetItemValues(0, 0, 10, 20); 291 SetItemValues(0, 0, 10, 20, false);
236 SetItemValues(0, 1, 50, -1); 292 SetItemValues(0, 1, 50, -1, false);
237 293
238 float progress = -1; 294 float progress = -1;
239 int download_count = -1; 295 int download_count = -1;
240 EXPECT_FALSE(updater_->GetProgress(&progress, &download_count)); 296 EXPECT_FALSE(updater_->GetProgress(&progress, &download_count));
241 } 297 }
242 298
243 // Test many null managers. 299 // Test many null managers.
244 TEST_F(DownloadStatusUpdaterTest, ManyManagersNoItems) { 300 TEST_F(DownloadStatusUpdaterTest, ManyManagersNoItems) {
245 SetupManagers(1); 301 SetupManagers(1);
246 AddItems(0, 0, 0); 302 AddItems(0, 0, 0);
(...skipping 22 matching lines...) Expand all
269 } 325 }
270 326
271 // Test many managers with some non-complete items. 327 // Test many managers with some non-complete items.
272 TEST_F(DownloadStatusUpdaterTest, ManyManagersMixedItems) { 328 TEST_F(DownloadStatusUpdaterTest, ManyManagersMixedItems) {
273 SetupManagers(2); 329 SetupManagers(2);
274 AddItems(0, 3, 2); 330 AddItems(0, 3, 2);
275 LinkManager(0); 331 LinkManager(0);
276 AddItems(1, 3, 1); 332 AddItems(1, 3, 1);
277 LinkManager(1); 333 LinkManager(1);
278 334
279 SetItemValues(0, 0, 10, 20); 335 SetItemValues(0, 0, 10, 20, false);
280 SetItemValues(0, 1, 50, 60); 336 SetItemValues(0, 1, 50, 60, false);
281 SetItemValues(1, 0, 80, 90); 337 SetItemValues(1, 0, 80, 90, false);
282 338
283 float progress = -1; 339 float progress = -1;
284 int download_count = -1; 340 int download_count = -1;
285 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count)); 341 EXPECT_TRUE(updater_->GetProgress(&progress, &download_count));
286 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress); 342 EXPECT_FLOAT_EQ((10+50+80)/(20.0f+60+90), progress);
287 EXPECT_EQ(3, download_count); 343 EXPECT_EQ(3, download_count);
288 } 344 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_status_updater_mac.mm ('k') | chrome/browser/download/download_status_updater_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698