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

Side by Side Diff: content/browser/download/download_item_impl_unittest.cc

Issue 11571025: Initial CL for Downloads resumption. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix try bot problems. Created 7 years, 12 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/command_line.h"
5 #include "base/message_loop.h" 6 #include "base/message_loop.h"
6 #include "base/stl_util.h" 7 #include "base/stl_util.h"
7 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
8 #include "content/browser/download/byte_stream.h" 9 #include "content/browser/download/byte_stream.h"
9 #include "content/browser/download/download_create_info.h" 10 #include "content/browser/download/download_create_info.h"
10 #include "content/browser/download/download_file_factory.h" 11 #include "content/browser/download/download_file_factory.h"
11 #include "content/browser/download/download_item_impl.h" 12 #include "content/browser/download/download_item_impl.h"
12 #include "content/browser/download/download_item_impl_delegate.h" 13 #include "content/browser/download/download_item_impl_delegate.h"
13 #include "content/browser/download/download_request_handle.h" 14 #include "content/browser/download/download_request_handle.h"
14 #include "content/browser/download/mock_download_file.h" 15 #include "content/browser/download/mock_download_file.h"
15 #include "content/public/browser/download_id.h" 16 #include "content/public/browser/download_id.h"
16 #include "content/public/browser/download_destination_observer.h" 17 #include "content/public/browser/download_destination_observer.h"
17 #include "content/public/browser/download_interrupt_reasons.h" 18 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_url_parameters.h"
20 #include "content/public/common/content_switches.h"
18 #include "content/public/test/mock_download_item.h" 21 #include "content/public/test/mock_download_item.h"
19 #include "content/public/test/test_browser_thread.h" 22 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
22 25
23 using ::testing::_; 26 using ::testing::_;
24 using ::testing::AllOf; 27 using ::testing::AllOf;
28 using ::testing::NiceMock;
25 using ::testing::Property; 29 using ::testing::Property;
26 using ::testing::Return; 30 using ::testing::Return;
27 using ::testing::SaveArg; 31 using ::testing::SaveArg;
28 using ::testing::StrictMock; 32 using ::testing::StrictMock;
29 33
30 namespace content { 34 namespace content {
31 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; 35 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain";
32 36
33 namespace { 37 namespace {
34 class MockDelegate : public DownloadItemImplDelegate { 38 class MockDelegate : public DownloadItemImplDelegate {
35 public: 39 public:
36 MOCK_METHOD2(DetermineDownloadTarget, void( 40 MOCK_METHOD2(DetermineDownloadTarget, void(
37 DownloadItemImpl*, const DownloadTargetCallback&)); 41 DownloadItemImpl*, const DownloadTargetCallback&));
38 MOCK_METHOD2(ShouldOpenDownload, 42 MOCK_METHOD2(ShouldOpenDownload,
39 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); 43 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&));
40 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); 44 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&));
41 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); 45 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*));
46
47 virtual void ResumeInterruptedDownload(
48 scoped_ptr<DownloadUrlParameters> params, DownloadId id) OVERRIDE {
49 MockResumeInterruptedDownload(params.get(), id);
50 }
51 MOCK_METHOD2(MockResumeInterruptedDownload,
52 void(DownloadUrlParameters* params, DownloadId id));
53
42 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); 54 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*());
43 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); 55 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*));
44 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); 56 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*));
45 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); 57 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*));
46 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); 58 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*));
47 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); 59 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*));
48 }; 60 };
49 61
50 class MockRequestHandle : public DownloadRequestHandleInterface { 62 class MockRequestHandle : public DownloadRequestHandleInterface {
51 public: 63 public:
(...skipping 18 matching lines...) Expand all
70 } 82 }
71 83
72 } // namespace 84 } // namespace
73 85
74 class DownloadItemTest : public testing::Test { 86 class DownloadItemTest : public testing::Test {
75 public: 87 public:
76 class MockObserver : public DownloadItem::Observer { 88 class MockObserver : public DownloadItem::Observer {
77 public: 89 public:
78 explicit MockObserver(DownloadItem* item) 90 explicit MockObserver(DownloadItem* item)
79 : item_(item), 91 : item_(item),
92 last_state_(item->GetState()),
80 removed_(false), 93 removed_(false),
81 destroyed_(false), 94 destroyed_(false),
82 updated_(false) { 95 updated_(false),
96 interrupt_count_(0),
97 resume_count_(0) {
83 item_->AddObserver(this); 98 item_->AddObserver(this);
84 } 99 }
85 100
86 virtual ~MockObserver() { 101 virtual ~MockObserver() {
87 if (item_) item_->RemoveObserver(this); 102 if (item_) item_->RemoveObserver(this);
88 } 103 }
89 104
90 virtual void OnDownloadRemoved(DownloadItem* download) { 105 virtual void OnDownloadRemoved(DownloadItem* download) {
106 DVLOG(20) << " " << __FUNCTION__
107 << " download = " << download->DebugString(false);
91 removed_ = true; 108 removed_ = true;
92 } 109 }
93 110
94 virtual void OnDownloadUpdated(DownloadItem* download) { 111 virtual void OnDownloadUpdated(DownloadItem* download) {
112 DVLOG(20) << " " << __FUNCTION__
113 << " download = " << download->DebugString(false);
95 updated_ = true; 114 updated_ = true;
115 DownloadItem::DownloadState new_state = download->GetState();
116 if (last_state_ == DownloadItem::IN_PROGRESS &&
117 new_state == DownloadItem::INTERRUPTED) {
118 interrupt_count_++;
119 }
120 if (last_state_ == DownloadItem::INTERRUPTED &&
121 new_state == DownloadItem::IN_PROGRESS) {
122 resume_count_++;
123 }
124 last_state_ = new_state;
96 } 125 }
97 126
98 virtual void OnDownloadOpened(DownloadItem* download) { 127 virtual void OnDownloadOpened(DownloadItem* download) {
128 DVLOG(20) << " " << __FUNCTION__
129 << " download = " << download->DebugString(false);
99 } 130 }
100 131
101 virtual void OnDownloadDestroyed(DownloadItem* download) { 132 virtual void OnDownloadDestroyed(DownloadItem* download) {
133 DVLOG(20) << " " << __FUNCTION__
134 << " download = " << download->DebugString(false);
102 destroyed_ = true; 135 destroyed_ = true;
103 item_->RemoveObserver(this); 136 item_->RemoveObserver(this);
104 item_ = NULL; 137 item_ = NULL;
105 } 138 }
106 139
107 bool CheckRemoved() { 140 bool CheckRemoved() {
108 return removed_; 141 return removed_;
109 } 142 }
110 143
111 bool CheckDestroyed() { 144 bool CheckDestroyed() {
112 return destroyed_; 145 return destroyed_;
113 } 146 }
114 147
115 bool CheckUpdated() { 148 bool CheckUpdated() {
116 bool was_updated = updated_; 149 bool was_updated = updated_;
117 updated_ = false; 150 updated_ = false;
118 return was_updated; 151 return was_updated;
119 } 152 }
120 153
154 int CheckInterrupted() {
155 return interrupt_count_;
156 }
157
158 int CheckResumed() {
159 return resume_count_;
160 }
161
121 private: 162 private:
122 DownloadItem* item_; 163 DownloadItem* item_;
164 DownloadItem::DownloadState last_state_;
123 bool removed_; 165 bool removed_;
124 bool destroyed_; 166 bool destroyed_;
125 bool updated_; 167 bool updated_;
168 int interrupt_count_;
169 int resume_count_;
126 }; 170 };
127 171
128 DownloadItemTest() 172 DownloadItemTest()
129 : ui_thread_(BrowserThread::UI, &loop_), 173 : ui_thread_(BrowserThread::UI, &loop_),
130 file_thread_(BrowserThread::FILE, &loop_), 174 file_thread_(BrowserThread::FILE, &loop_),
131 delegate_() { 175 delegate_() {
132 } 176 }
133 177
134 ~DownloadItemTest() { 178 ~DownloadItemTest() {
135 } 179 }
(...skipping 16 matching lines...) Expand all
152 // don't call the function that deletes it, so we do so ourselves. 196 // don't call the function that deletes it, so we do so ourselves.
153 scoped_ptr<DownloadCreateInfo> info_; 197 scoped_ptr<DownloadCreateInfo> info_;
154 198
155 info_.reset(new DownloadCreateInfo()); 199 info_.reset(new DownloadCreateInfo());
156 static int next_id; 200 static int next_id;
157 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); 201 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id);
158 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); 202 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo());
159 info_->save_info->prompt_for_save_location = false; 203 info_->save_info->prompt_for_save_location = false;
160 info_->url_chain.push_back(GURL()); 204 info_->url_chain.push_back(GURL());
161 205
162 scoped_ptr<DownloadRequestHandleInterface> request_handle(
163 new testing::NiceMock<MockRequestHandle>);
164 DownloadItemImpl* download = 206 DownloadItemImpl* download =
165 new DownloadItemImpl(&delegate_, *(info_.get()), 207 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog());
166 request_handle.Pass(), net::BoundNetLog());
167 allocated_downloads_.insert(download); 208 allocated_downloads_.insert(download);
168 return download; 209 return download;
169 } 210 }
170 211
171 // Add DownloadFile to DownloadItem 212 // Add DownloadFile to DownloadItem
172 MockDownloadFile* AddDownloadFileToDownloadItem( 213 MockDownloadFile* AddDownloadFileToDownloadItem(
173 DownloadItemImpl* item, 214 DownloadItemImpl* item,
174 DownloadItemImplDelegate::DownloadTargetCallback *callback) { 215 DownloadItemImplDelegate::DownloadTargetCallback *callback) {
175 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); 216 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>);
176 scoped_ptr<DownloadFile> download_file(mock_download_file); 217 scoped_ptr<DownloadFile> download_file(mock_download_file);
177 EXPECT_CALL(*mock_download_file, Initialize(_)); 218 EXPECT_CALL(*mock_download_file, Initialize(_));
178 if (callback) { 219 if (callback) {
179 // Save the callback. 220 // Save the callback.
180 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) 221 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _))
181 .WillOnce(SaveArg<1>(callback)); 222 .WillOnce(SaveArg<1>(callback));
182 } else { 223 } else {
183 // Drop it on the floor. 224 // Drop it on the floor.
184 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); 225 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _));
185 } 226 }
186 227
187 item->Start(download_file.Pass()); 228 scoped_ptr<DownloadRequestHandleInterface> request_handle(
229 new NiceMock<MockRequestHandle>);
230 item->Start(download_file.Pass(), request_handle.Pass());
188 loop_.RunUntilIdle(); 231 loop_.RunUntilIdle();
189 232
190 // So that we don't have a function writing to a stack variable 233 // So that we don't have a function writing to a stack variable
191 // lying around if the above failed. 234 // lying around if the above failed.
192 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); 235 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
193 236
194 return mock_download_file; 237 return mock_download_file;
195 } 238 }
196 239
197 // Cleanup a download item (specifically get rid of the DownloadFile on it). 240 // Cleanup a download item (specifically get rid of the DownloadFile on it).
198 // The item must be in the IN_PROGRESS state. 241 // The item must be in the expected state.
199 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { 242 void CleanupItem(DownloadItemImpl* item,
200 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); 243 MockDownloadFile* download_file,
244 DownloadItem::DownloadState expected_state) {
245 EXPECT_EQ(expected_state, item->GetState());
201 246
202 EXPECT_CALL(*download_file, Cancel()); 247 if (expected_state == DownloadItem::IN_PROGRESS) {
203 item->Cancel(true); 248 EXPECT_CALL(*download_file, Cancel());
204 loop_.RunUntilIdle(); 249 item->Cancel(true);
250 loop_.RunUntilIdle();
251 }
205 } 252 }
206 253
207 // Destroy a previously created download item. 254 // Destroy a previously created download item.
208 void DestroyDownloadItem(DownloadItem* item) { 255 void DestroyDownloadItem(DownloadItem* item) {
209 allocated_downloads_.erase(item); 256 allocated_downloads_.erase(item);
210 delete item; 257 delete item;
211 } 258 }
212 259
213 void RunAllPendingInMessageLoops() { 260 void RunAllPendingInMessageLoops() {
214 loop_.RunUntilIdle(); 261 loop_.RunUntilIdle();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 item->OnDownloadedFileRemoved(); 337 item->OnDownloadedFileRemoved();
291 ASSERT_TRUE(observer.CheckUpdated()); 338 ASSERT_TRUE(observer.CheckUpdated());
292 } 339 }
293 340
294 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { 341 TEST_F(DownloadItemTest, NotificationAfterInterrupted) {
295 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 342 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
296 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 343 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
297 EXPECT_CALL(*download_file, Cancel()); 344 EXPECT_CALL(*download_file, Cancel());
298 MockObserver observer(item); 345 MockObserver observer(item);
299 346
347 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_))
348 .Times(0);
349
300 item->DestinationObserverAsWeakPtr()->DestinationError( 350 item->DestinationObserverAsWeakPtr()->DestinationError(
301 DOWNLOAD_INTERRUPT_REASON_NONE); 351 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
302 ASSERT_TRUE(observer.CheckUpdated()); 352 ASSERT_TRUE(observer.CheckUpdated());
303 } 353 }
304 354
305 TEST_F(DownloadItemTest, NotificationAfterDelete) { 355 TEST_F(DownloadItemTest, NotificationAfterDelete) {
306 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 356 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
307 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 357 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
308 EXPECT_CALL(*download_file, Cancel()); 358 EXPECT_CALL(*download_file, Cancel());
309 MockObserver observer(item); 359 MockObserver observer(item);
310 360
311 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); 361 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
312 ASSERT_TRUE(observer.CheckUpdated()); 362 ASSERT_TRUE(observer.CheckUpdated());
313 } 363 }
314 364
315 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { 365 TEST_F(DownloadItemTest, NotificationAfterDestroyed) {
316 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 366 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
317 MockObserver observer(item); 367 MockObserver observer(item);
318 368
319 DestroyDownloadItem(item); 369 DestroyDownloadItem(item);
320 ASSERT_TRUE(observer.CheckDestroyed()); 370 ASSERT_TRUE(observer.CheckDestroyed());
321 } 371 }
322 372
373 TEST_F(DownloadItemTest, RestartAfterInterrupted) {
374 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
375 MockObserver observer(item);
376 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>);
377 scoped_ptr<DownloadFile> download_file(mock_download_file);
378 scoped_ptr<DownloadRequestHandleInterface> request_handle(
379 new NiceMock<MockRequestHandle>);
380
381 EXPECT_CALL(*mock_download_file, Initialize(_));
382 item->Start(download_file.Pass(), request_handle.Pass());
383
384 EXPECT_CALL(*mock_download_file, Detach());
385 // Use a continuable interrupt.
386 item->DestinationObserverAsWeakPtr()->DestinationError(
387 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
388 ASSERT_TRUE(observer.CheckUpdated());
389 // Should attempt to auto-resume. Because we don't have a mock WebContents,
390 // ResumeInterruptedDownload() will abort early, with another interrupt,
391 // which will be ignored.
392 ASSERT_EQ(1, observer.CheckInterrupted());
393 ASSERT_EQ(0, observer.CheckResumed());
394 RunAllPendingInMessageLoops();
395
396 mock_download_file = new NiceMock<MockDownloadFile>;
397 download_file.reset(mock_download_file);
398 request_handle.reset(new NiceMock<MockRequestHandle>);
399
400 EXPECT_CALL(*mock_download_file, Initialize(_));
401 item->Start(download_file.Pass(), request_handle.Pass());
402 ASSERT_EQ(1, observer.CheckResumed());
403
404 // Use a non-resumable interrupt.
405 EXPECT_CALL(*mock_download_file, Cancel());
406 item->DestinationObserverAsWeakPtr()->DestinationError(
407 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED);
408 ASSERT_TRUE(observer.CheckUpdated());
409 // Should not try to auto-resume.
410 ASSERT_EQ(2, observer.CheckInterrupted());
411 ASSERT_EQ(1, observer.CheckResumed());
412 RunAllPendingInMessageLoops();
413
414 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED);
415 }
416
417 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) {
418 CommandLine::ForCurrentProcess()->AppendSwitch(
419 switches::kEnableDownloadResumption);
420
421 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
422 base::WeakPtr<DownloadDestinationObserver> as_observer(
423 item->DestinationObserverAsWeakPtr());
424 MockObserver observer(item);
425 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>);
426 scoped_ptr<DownloadFile> download_file(mock_download_file);
427 MockRequestHandle* mock_request_handle = new NiceMock<MockRequestHandle>;
428 scoped_ptr<DownloadRequestHandleInterface> request_handle(
429 mock_request_handle);
430
431 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 2); ++i) {
432 DVLOG(20) << "Loop iteration " << i;
433
434 // It's too complicated to set up a WebContents instance that would cause
435 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function
436 // to be callled, so we simply verify that GetWebContents() is called.
437 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) {
438 EXPECT_CALL(*mock_request_handle, GetWebContents())
439 .WillOnce(Return(static_cast<WebContents*>(NULL)));
440 }
441
442 item->Start(download_file.Pass(), request_handle.Pass());
443
444 ASSERT_EQ(i, observer.CheckResumed());
445
446 // Use a continuable interrupt.
447 item->DestinationObserverAsWeakPtr()->DestinationError(
448 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR);
449
450 mock_download_file = new NiceMock<MockDownloadFile>;
451 download_file.reset(mock_download_file);
452 mock_request_handle = new NiceMock<MockRequestHandle>;
453 request_handle.reset(mock_request_handle);
454
455 ASSERT_EQ(i + 1, observer.CheckInterrupted());
456 }
457
458 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED);
459 }
460
323 TEST_F(DownloadItemTest, NotificationAfterRemove) { 461 TEST_F(DownloadItemTest, NotificationAfterRemove) {
324 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 462 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
325 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 463 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
326 EXPECT_CALL(*download_file, Cancel()); 464 EXPECT_CALL(*download_file, Cancel());
327 MockObserver observer(item); 465 MockObserver observer(item);
328 466
329 item->Remove(); 467 item->Remove();
330 ASSERT_TRUE(observer.CheckUpdated()); 468 ASSERT_TRUE(observer.CheckUpdated());
331 ASSERT_TRUE(observer.CheckRemoved()); 469 ASSERT_TRUE(observer.CheckRemoved());
332 } 470 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 525
388 // Currently, a notification would be generated if the danger type is anything 526 // Currently, a notification would be generated if the danger type is anything
389 // other than NOT_DANGEROUS. 527 // other than NOT_DANGEROUS.
390 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 528 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
391 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 529 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
392 EXPECT_FALSE(observer.CheckUpdated()); 530 EXPECT_FALSE(observer.CheckUpdated());
393 RunAllPendingInMessageLoops(); 531 RunAllPendingInMessageLoops();
394 EXPECT_TRUE(observer.CheckUpdated()); 532 EXPECT_TRUE(observer.CheckUpdated());
395 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 533 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
396 534
397 CleanupItem(item, download_file); 535 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
398 } 536 }
399 537
400 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 538 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
401 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 539 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
402 MockObserver observer(item); 540 MockObserver observer(item);
541 MockDownloadFile* mock_download_file(new MockDownloadFile);
542 scoped_ptr<DownloadFile> download_file(mock_download_file);
543 scoped_ptr<DownloadRequestHandleInterface> request_handle(
544 new NiceMock<MockRequestHandle>);
545
546 EXPECT_CALL(*mock_download_file, Initialize(_));
547 item->Start(download_file.Pass(), request_handle.Pass());
403 548
404 item->TogglePause(); 549 item->TogglePause();
405 ASSERT_TRUE(observer.CheckUpdated()); 550 ASSERT_TRUE(observer.CheckUpdated());
406 551
407 item->TogglePause(); 552 item->TogglePause();
408 ASSERT_TRUE(observer.CheckUpdated()); 553 ASSERT_TRUE(observer.CheckUpdated());
554 RunAllPendingInMessageLoops();
555
556 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
409 } 557 }
410 558
411 TEST_F(DownloadItemTest, DisplayName) { 559 TEST_F(DownloadItemTest, DisplayName) {
412 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 560 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
413 DownloadItemImplDelegate::DownloadTargetCallback callback; 561 DownloadItemImplDelegate::DownloadTargetCallback callback;
414 MockDownloadFile* download_file = 562 MockDownloadFile* download_file =
415 AddDownloadFileToDownloadItem(item, &callback); 563 AddDownloadFileToDownloadItem(item, &callback);
416 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); 564 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar"));
417 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); 565 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x"));
418 EXPECT_EQ(FILE_PATH_LITERAL(""), 566 EXPECT_EQ(FILE_PATH_LITERAL(""),
419 item->GetFileNameToReportUser().value()); 567 item->GetFileNameToReportUser().value());
420 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) 568 EXPECT_CALL(*download_file, RenameAndUniquify(_, _))
421 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, 569 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE,
422 intermediate_path)); 570 intermediate_path));
423 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, 571 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE,
424 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); 572 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path);
425 RunAllPendingInMessageLoops(); 573 RunAllPendingInMessageLoops();
426 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), 574 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"),
427 item->GetFileNameToReportUser().value()); 575 item->GetFileNameToReportUser().value());
428 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); 576 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name")));
429 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 577 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
430 item->GetFileNameToReportUser().value()); 578 item->GetFileNameToReportUser().value());
431 CleanupItem(item, download_file); 579 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS);
432 } 580 }
433 581
434 // Test to make sure that Start method calls DF initialize properly. 582 // Test to make sure that Start method calls DF initialize properly.
435 TEST_F(DownloadItemTest, Start) { 583 TEST_F(DownloadItemTest, Start) {
436 MockDownloadFile* mock_download_file(new MockDownloadFile); 584 MockDownloadFile* mock_download_file(new MockDownloadFile);
437 scoped_ptr<DownloadFile> download_file(mock_download_file); 585 scoped_ptr<DownloadFile> download_file(mock_download_file);
438 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 586 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
439 EXPECT_CALL(*mock_download_file, Initialize(_)); 587 EXPECT_CALL(*mock_download_file, Initialize(_));
440 item->Start(download_file.Pass()); 588 scoped_ptr<DownloadRequestHandleInterface> request_handle(
589 new NiceMock<MockRequestHandle>);
590 item->Start(download_file.Pass(), request_handle.Pass());
441 591
442 CleanupItem(item, mock_download_file); 592 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS);
443 } 593 }
444 594
445 // Test that the delegate is invoked after the download file is renamed. 595 // Test that the delegate is invoked after the download file is renamed.
446 TEST_F(DownloadItemTest, CallbackAfterRename) { 596 TEST_F(DownloadItemTest, CallbackAfterRename) {
447 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 597 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
448 DownloadItemImplDelegate::DownloadTargetCallback callback; 598 DownloadItemImplDelegate::DownloadTargetCallback callback;
449 MockDownloadFile* download_file = 599 MockDownloadFile* download_file =
450 AddDownloadFileToDownloadItem(item, &callback); 600 AddDownloadFileToDownloadItem(item, &callback);
451 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); 601 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar"));
452 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); 602 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x"));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 const DownloadInterruptReason reason( 659 const DownloadInterruptReason reason(
510 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 660 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
511 661
512 // Confirm interrupt sets state properly. 662 // Confirm interrupt sets state properly.
513 EXPECT_CALL(*download_file, Cancel()); 663 EXPECT_CALL(*download_file, Cancel());
514 item->DestinationObserverAsWeakPtr()->DestinationError(reason); 664 item->DestinationObserverAsWeakPtr()->DestinationError(reason);
515 RunAllPendingInMessageLoops(); 665 RunAllPendingInMessageLoops();
516 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 666 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState());
517 EXPECT_EQ(reason, item->GetLastReason()); 667 EXPECT_EQ(reason, item->GetLastReason());
518 668
519 // Cancel should result in no change. 669 // Cancel should kill it.
520 item->Cancel(true); 670 item->Cancel(true);
521 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); 671 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState());
522 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); 672 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason());
523 } 673 }
524 674
525 TEST_F(DownloadItemTest, Canceled) { 675 TEST_F(DownloadItemTest, Canceled) {
526 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 676 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
527 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); 677 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL);
528 678
529 // Confirm cancel sets state properly. 679 // Confirm cancel sets state properly.
530 EXPECT_CALL(*download_file, Cancel()); 680 EXPECT_CALL(*download_file, Cancel());
531 item->Cancel(true); 681 item->Cancel(true);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 EXPECT_EQ("livebeef", item->GetHash()); 767 EXPECT_EQ("livebeef", item->GetHash());
618 EXPECT_EQ("", item->GetHashState()); 768 EXPECT_EQ("", item->GetHashState());
619 EXPECT_TRUE(item->AllDataSaved()); 769 EXPECT_TRUE(item->AllDataSaved());
620 } 770 }
621 771
622 TEST(MockDownloadItem, Compiles) { 772 TEST(MockDownloadItem, Compiles) {
623 MockDownloadItem mock_item; 773 MockDownloadItem mock_item;
624 } 774 }
625 775
626 } // namespace content 776 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698