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/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 { | 34 namespace { |
31 | 35 |
32 const int kDownloadChunkSize = 1000; | 36 const int kDownloadChunkSize = 1000; |
33 const int kDownloadSpeed = 1000; | 37 const int kDownloadSpeed = 1000; |
34 const int kDummyDBHandle = 10; | 38 const int kDummyDBHandle = 10; |
35 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 39 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
36 | 40 |
37 } // namespace | 41 } // namespace |
38 | 42 |
39 namespace content { | 43 namespace content { |
40 | 44 |
41 namespace { | 45 namespace { |
42 | 46 |
43 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 47 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
44 | 48 |
45 class MockDelegate : public DownloadItemImplDelegate { | 49 class MockDelegate : public DownloadItemImplDelegate { |
46 public: | 50 public: |
47 MOCK_METHOD2(DetermineDownloadTarget, void( | 51 MOCK_METHOD2(DetermineDownloadTarget, void( |
48 DownloadItemImpl*, const DownloadTargetCallback&)); | 52 DownloadItemImpl*, const DownloadTargetCallback&)); |
49 MOCK_METHOD2(ShouldOpenDownload, | 53 MOCK_METHOD2(ShouldOpenDownload, |
50 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); | 54 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); |
51 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); | 55 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
52 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 56 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
57 | |
58 virtual void ResumeInterruptedDownload( | |
59 scoped_ptr<DownloadUrlParameters> params, DownloadId id) OVERRIDE { | |
60 MockResumeInterruptedDownload(params.get(), id); | |
61 } | |
62 MOCK_METHOD2(MockResumeInterruptedDownload, | |
63 void(DownloadUrlParameters* params, DownloadId id)); | |
64 | |
53 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 65 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
54 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); | 66 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); |
55 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 67 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
56 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 68 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
57 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); | 69 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
58 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 70 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
59 }; | 71 }; |
60 | 72 |
61 class MockRequestHandle : public DownloadRequestHandleInterface { | 73 class MockRequestHandle : public DownloadRequestHandleInterface { |
62 public: | 74 public: |
(...skipping 10 matching lines...) Expand all Loading... | |
73 // MockDownloadFile::Rename as follows: | 85 // MockDownloadFile::Rename as follows: |
74 // EXPECT_CALL(download_file, Rename*(_,_)) | 86 // EXPECT_CALL(download_file, Rename*(_,_)) |
75 // .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 87 // .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
76 // new_path)); | 88 // new_path)); |
77 ACTION_P2(ScheduleRenameCallback, interrupt_reason, new_path) { | 89 ACTION_P2(ScheduleRenameCallback, interrupt_reason, new_path) { |
78 BrowserThread::PostTask( | 90 BrowserThread::PostTask( |
79 BrowserThread::UI, FROM_HERE, | 91 BrowserThread::UI, FROM_HERE, |
80 base::Bind(arg1, interrupt_reason, new_path)); | 92 base::Bind(arg1, interrupt_reason, new_path)); |
81 } | 93 } |
82 | 94 |
95 const int kDownloadChunkSize = 1000; | |
96 const int kDownloadSpeed = 1000; | |
97 const int kDummyDBHandle = 10; | |
98 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | |
asanka
2013/01/04 22:54:16
Duplicate?
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
Sure looks it. Removed.
| |
99 | |
83 } // namespace | 100 } // namespace |
84 | 101 |
85 class DownloadItemTest : public testing::Test { | 102 class DownloadItemTest : public testing::Test { |
86 public: | 103 public: |
87 class MockObserver : public DownloadItem::Observer { | 104 class MockObserver : public DownloadItem::Observer { |
88 public: | 105 public: |
89 explicit MockObserver(DownloadItem* item) | 106 explicit MockObserver(DownloadItem* item) |
90 : item_(item), | 107 : item_(item), |
108 last_state_(item->GetState()), | |
91 removed_(false), | 109 removed_(false), |
92 destroyed_(false), | 110 destroyed_(false), |
93 updated_(false) { | 111 updated_(false), |
112 interrupt_count_(0), | |
113 resume_count_(0) { | |
94 item_->AddObserver(this); | 114 item_->AddObserver(this); |
95 } | 115 } |
96 | 116 |
97 virtual ~MockObserver() { | 117 virtual ~MockObserver() { |
98 if (item_) item_->RemoveObserver(this); | 118 if (item_) item_->RemoveObserver(this); |
99 } | 119 } |
100 | 120 |
101 virtual void OnDownloadRemoved(DownloadItem* download) { | 121 virtual void OnDownloadRemoved(DownloadItem* download) { |
122 DVLOG(20) << " " << __FUNCTION__ | |
123 << " download = " << download->DebugString(false); | |
102 removed_ = true; | 124 removed_ = true; |
103 } | 125 } |
104 | 126 |
105 virtual void OnDownloadUpdated(DownloadItem* download) { | 127 virtual void OnDownloadUpdated(DownloadItem* download) { |
128 DVLOG(20) << " " << __FUNCTION__ | |
129 << " download = " << download->DebugString(false); | |
106 updated_ = true; | 130 updated_ = true; |
131 DownloadItem::DownloadState new_state = download->GetState(); | |
132 if (last_state_ == DownloadItem::IN_PROGRESS && | |
133 new_state == DownloadItem::INTERRUPTED) { | |
134 interrupt_count_++; | |
135 } | |
136 if (last_state_ == DownloadItem::INTERRUPTED && | |
137 new_state == DownloadItem::IN_PROGRESS) { | |
138 resume_count_++; | |
139 } | |
140 last_state_ = new_state; | |
107 } | 141 } |
108 | 142 |
109 virtual void OnDownloadOpened(DownloadItem* download) { | 143 virtual void OnDownloadOpened(DownloadItem* download) { |
144 DVLOG(20) << " " << __FUNCTION__ | |
145 << " download = " << download->DebugString(false); | |
110 } | 146 } |
111 | 147 |
112 virtual void OnDownloadDestroyed(DownloadItem* download) { | 148 virtual void OnDownloadDestroyed(DownloadItem* download) { |
149 DVLOG(20) << " " << __FUNCTION__ | |
150 << " download = " << download->DebugString(false); | |
113 destroyed_ = true; | 151 destroyed_ = true; |
114 item_->RemoveObserver(this); | 152 item_->RemoveObserver(this); |
115 item_ = NULL; | 153 item_ = NULL; |
116 } | 154 } |
117 | 155 |
118 bool CheckRemoved() { | 156 bool CheckRemoved() { |
119 return removed_; | 157 return removed_; |
120 } | 158 } |
121 | 159 |
122 bool CheckDestroyed() { | 160 bool CheckDestroyed() { |
123 return destroyed_; | 161 return destroyed_; |
124 } | 162 } |
125 | 163 |
126 bool CheckUpdated() { | 164 bool CheckUpdated() { |
127 bool was_updated = updated_; | 165 bool was_updated = updated_; |
128 updated_ = false; | 166 updated_ = false; |
129 return was_updated; | 167 return was_updated; |
130 } | 168 } |
131 | 169 |
170 int CheckInterrupted() { | |
asanka
2013/01/04 22:54:16
Nit: Why not GetInterruptCount()?
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
Done.
| |
171 return interrupt_count_; | |
172 } | |
173 | |
174 int CheckResumed() { | |
asanka
2013/01/04 22:54:16
Nit: Ditto.
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
Done.
| |
175 return resume_count_; | |
176 } | |
177 | |
132 private: | 178 private: |
133 DownloadItem* item_; | 179 DownloadItem* item_; |
180 DownloadItem::DownloadState last_state_; | |
134 bool removed_; | 181 bool removed_; |
135 bool destroyed_; | 182 bool destroyed_; |
136 bool updated_; | 183 bool updated_; |
184 int interrupt_count_; | |
185 int resume_count_; | |
137 }; | 186 }; |
138 | 187 |
139 DownloadItemTest() | 188 DownloadItemTest() |
140 : ui_thread_(BrowserThread::UI, &loop_), | 189 : ui_thread_(BrowserThread::UI, &loop_), |
141 file_thread_(BrowserThread::FILE, &loop_), | 190 file_thread_(BrowserThread::FILE, &loop_), |
142 delegate_() { | 191 delegate_() { |
143 } | 192 } |
144 | 193 |
145 ~DownloadItemTest() { | 194 ~DownloadItemTest() { |
146 } | 195 } |
(...skipping 16 matching lines...) Expand all Loading... | |
163 // don't call the function that deletes it, so we do so ourselves. | 212 // don't call the function that deletes it, so we do so ourselves. |
164 scoped_ptr<DownloadCreateInfo> info_; | 213 scoped_ptr<DownloadCreateInfo> info_; |
165 | 214 |
166 info_.reset(new DownloadCreateInfo()); | 215 info_.reset(new DownloadCreateInfo()); |
167 static int next_id; | 216 static int next_id; |
168 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); | 217 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); |
169 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 218 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
170 info_->save_info->prompt_for_save_location = false; | 219 info_->save_info->prompt_for_save_location = false; |
171 info_->url_chain.push_back(GURL()); | 220 info_->url_chain.push_back(GURL()); |
172 | 221 |
173 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
174 new testing::NiceMock<MockRequestHandle>); | |
175 DownloadItemImpl* download = | 222 DownloadItemImpl* download = |
176 new DownloadItemImpl(&delegate_, *(info_.get()), | 223 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog()); |
177 request_handle.Pass(), net::BoundNetLog()); | |
178 allocated_downloads_.insert(download); | 224 allocated_downloads_.insert(download); |
179 return download; | 225 return download; |
180 } | 226 } |
181 | 227 |
182 // Add DownloadFile to DownloadItem | 228 // Add DownloadFile to DownloadItem |
183 MockDownloadFile* AddDownloadFileToDownloadItem( | 229 MockDownloadFile* AddDownloadFileToDownloadItem( |
184 DownloadItemImpl* item, | 230 DownloadItemImpl* item, |
185 DownloadItemImplDelegate::DownloadTargetCallback *callback) { | 231 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
186 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 232 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
187 scoped_ptr<DownloadFile> download_file(mock_download_file); | 233 scoped_ptr<DownloadFile> download_file(mock_download_file); |
188 EXPECT_CALL(*mock_download_file, Initialize(_)); | 234 EXPECT_CALL(*mock_download_file, Initialize(_)); |
189 if (callback) { | 235 if (callback) { |
190 // Save the callback. | 236 // Save the callback. |
191 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 237 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
192 .WillOnce(SaveArg<1>(callback)); | 238 .WillOnce(SaveArg<1>(callback)); |
193 } else { | 239 } else { |
194 // Drop it on the floor. | 240 // Drop it on the floor. |
195 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 241 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
196 } | 242 } |
197 | 243 |
198 item->Start(download_file.Pass()); | 244 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
245 new NiceMock<MockRequestHandle>); | |
246 item->Start(download_file.Pass(), request_handle.Pass()); | |
199 loop_.RunUntilIdle(); | 247 loop_.RunUntilIdle(); |
200 | 248 |
201 // So that we don't have a function writing to a stack variable | 249 // So that we don't have a function writing to a stack variable |
202 // lying around if the above failed. | 250 // lying around if the above failed. |
203 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 251 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
204 | 252 |
205 return mock_download_file; | 253 return mock_download_file; |
206 } | 254 } |
207 | 255 |
208 // Perform the intermediate rename for |item|. The target path for the | 256 // Perform the intermediate rename for |item|. The target path for the |
(...skipping 10 matching lines...) Expand all Loading... | |
219 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 267 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
220 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 268 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
221 intermediate_path)); | 269 intermediate_path)); |
222 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 270 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
223 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 271 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
224 RunAllPendingInMessageLoops(); | 272 RunAllPendingInMessageLoops(); |
225 return download_file; | 273 return download_file; |
226 } | 274 } |
227 | 275 |
228 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 276 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
229 // The item must be in the IN_PROGRESS state. | 277 // The item must be in the expected state. |
230 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { | 278 void CleanupItem(DownloadItemImpl* item, |
231 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 279 MockDownloadFile* download_file, |
280 DownloadItem::DownloadState expected_state) { | |
281 EXPECT_EQ(expected_state, item->GetState()); | |
232 | 282 |
233 EXPECT_CALL(*download_file, Cancel()); | 283 if (expected_state == DownloadItem::IN_PROGRESS) { |
234 item->Cancel(true); | 284 EXPECT_CALL(*download_file, Cancel()); |
235 loop_.RunUntilIdle(); | 285 item->Cancel(true); |
286 loop_.RunUntilIdle(); | |
287 } | |
236 } | 288 } |
237 | 289 |
238 // Destroy a previously created download item. | 290 // Destroy a previously created download item. |
239 void DestroyDownloadItem(DownloadItem* item) { | 291 void DestroyDownloadItem(DownloadItem* item) { |
240 allocated_downloads_.erase(item); | 292 allocated_downloads_.erase(item); |
241 delete item; | 293 delete item; |
242 } | 294 } |
243 | 295 |
244 void RunAllPendingInMessageLoops() { | 296 void RunAllPendingInMessageLoops() { |
245 loop_.RunUntilIdle(); | 297 loop_.RunUntilIdle(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 item->OnDownloadedFileRemoved(); | 363 item->OnDownloadedFileRemoved(); |
312 ASSERT_TRUE(observer.CheckUpdated()); | 364 ASSERT_TRUE(observer.CheckUpdated()); |
313 } | 365 } |
314 | 366 |
315 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 367 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
316 DownloadItemImpl* item = CreateDownloadItem(); | 368 DownloadItemImpl* item = CreateDownloadItem(); |
317 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 369 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
318 EXPECT_CALL(*download_file, Cancel()); | 370 EXPECT_CALL(*download_file, Cancel()); |
319 MockObserver observer(item); | 371 MockObserver observer(item); |
320 | 372 |
373 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) | |
374 .Times(0); | |
375 | |
321 item->DestinationObserverAsWeakPtr()->DestinationError( | 376 item->DestinationObserverAsWeakPtr()->DestinationError( |
322 DOWNLOAD_INTERRUPT_REASON_NONE); | 377 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
323 ASSERT_TRUE(observer.CheckUpdated()); | 378 ASSERT_TRUE(observer.CheckUpdated()); |
324 } | 379 } |
325 | 380 |
326 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 381 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
327 DownloadItemImpl* item = CreateDownloadItem(); | 382 DownloadItemImpl* item = CreateDownloadItem(); |
328 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 383 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
329 EXPECT_CALL(*download_file, Cancel()); | 384 EXPECT_CALL(*download_file, Cancel()); |
330 MockObserver observer(item); | 385 MockObserver observer(item); |
331 | 386 |
332 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 387 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
333 ASSERT_TRUE(observer.CheckUpdated()); | 388 ASSERT_TRUE(observer.CheckUpdated()); |
334 } | 389 } |
335 | 390 |
336 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 391 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
337 DownloadItemImpl* item = CreateDownloadItem(); | 392 DownloadItemImpl* item = CreateDownloadItem(); |
338 MockObserver observer(item); | 393 MockObserver observer(item); |
339 | 394 |
340 DestroyDownloadItem(item); | 395 DestroyDownloadItem(item); |
341 ASSERT_TRUE(observer.CheckDestroyed()); | 396 ASSERT_TRUE(observer.CheckDestroyed()); |
342 } | 397 } |
343 | 398 |
399 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { | |
400 DownloadItemImpl* item = CreateDownloadItem(); | |
401 MockObserver observer(item); | |
402 DownloadItemImplDelegate::DownloadTargetCallback callback; | |
403 MockDownloadFile* download_file = DoIntermediateRename(item); | |
404 | |
405 // Interrupt the download, using a continuable interrupt. | |
406 EXPECT_CALL(*download_file, Detach()); | |
407 item->DestinationObserverAsWeakPtr()->DestinationError( | |
408 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | |
409 ASSERT_TRUE(observer.CheckUpdated()); | |
410 // Should attempt to auto-resume. Because we don't have a mock WebContents, | |
411 // ResumeInterruptedDownload() will abort early, with another interrupt, | |
412 // which will be ignored. | |
413 ASSERT_EQ(1, observer.CheckInterrupted()); | |
414 ASSERT_EQ(0, observer.CheckResumed()); | |
415 RunAllPendingInMessageLoops(); | |
416 | |
417 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); | |
418 } | |
419 | |
420 // Same as above, but with a non-continuable interrupt. | |
421 TEST_F(DownloadItemTest, RestartAfterInterrupted) { | |
422 DownloadItemImpl* item = CreateDownloadItem(); | |
423 MockObserver observer(item); | |
424 DownloadItemImplDelegate::DownloadTargetCallback callback; | |
425 MockDownloadFile* download_file = DoIntermediateRename(item); | |
426 | |
427 // Interrupt the download, using a restartable interrupt. | |
428 EXPECT_CALL(*download_file, Cancel()); | |
429 item->DestinationObserverAsWeakPtr()->DestinationError( | |
430 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); | |
431 ASSERT_TRUE(observer.CheckUpdated()); | |
432 // Should not try to auto-resume. | |
433 ASSERT_EQ(1, observer.CheckInterrupted()); | |
434 ASSERT_EQ(0, observer.CheckResumed()); | |
435 RunAllPendingInMessageLoops(); | |
436 | |
437 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); | |
438 } | |
439 | |
440 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { | |
441 CommandLine::ForCurrentProcess()->AppendSwitch( | |
442 switches::kEnableDownloadResumption); | |
443 | |
444 DownloadItemImpl* item = CreateDownloadItem(); | |
445 base::WeakPtr<DownloadDestinationObserver> as_observer( | |
446 item->DestinationObserverAsWeakPtr()); | |
447 MockObserver observer(item); | |
448 MockDownloadFile* mock_download_file(new NiceMock<MockDownloadFile>); | |
449 scoped_ptr<DownloadFile> download_file(mock_download_file); | |
asanka
2013/01/04 22:54:16
Nit: You can use PassAs<DownloadFile>() and just k
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
I don't *think* that will work because of ownershi
| |
450 MockRequestHandle* mock_request_handle = new NiceMock<MockRequestHandle>; | |
451 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
452 mock_request_handle); | |
453 | |
454 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 2); ++i) { | |
asanka
2013/01/04 22:54:16
Can you explain the +2 here? You have an ASSERT_EQ
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
I think it was to make sure the test (to avoid aut
| |
455 DVLOG(20) << "Loop iteration " << i; | |
456 | |
457 // It's too complicated to set up a WebContents instance that would cause | |
458 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function | |
459 // to be callled, so we simply verify that GetWebContents() is called. | |
460 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { | |
461 EXPECT_CALL(*mock_request_handle, GetWebContents()) | |
462 .WillOnce(Return(static_cast<WebContents*>(NULL))); | |
asanka
2013/01/04 22:54:16
Since mock_request_handle is a NiceMock, would it
Randy Smith (Not in Mondays)
2013/01/07 20:54:10
The original code had Times(0)/Times(1), but I've
| |
463 } | |
464 | |
465 item->Start(download_file.Pass(), request_handle.Pass()); | |
466 | |
467 ASSERT_EQ(i, observer.CheckResumed()); | |
468 | |
469 // Use a continuable interrupt. | |
470 item->DestinationObserverAsWeakPtr()->DestinationError( | |
471 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); | |
472 | |
473 mock_download_file = new NiceMock<MockDownloadFile>; | |
474 download_file.reset(mock_download_file); | |
475 mock_request_handle = new NiceMock<MockRequestHandle>; | |
476 request_handle.reset(mock_request_handle); | |
477 | |
478 ASSERT_EQ(i + 1, observer.CheckInterrupted()); | |
479 } | |
480 | |
481 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED); | |
482 } | |
483 | |
344 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 484 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
345 DownloadItemImpl* item = CreateDownloadItem(); | 485 DownloadItemImpl* item = CreateDownloadItem(); |
346 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 486 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
347 EXPECT_CALL(*download_file, Cancel()); | 487 EXPECT_CALL(*download_file, Cancel()); |
348 MockObserver observer(item); | 488 MockObserver observer(item); |
349 | 489 |
350 item->Remove(); | 490 item->Remove(); |
351 ASSERT_TRUE(observer.CheckUpdated()); | 491 ASSERT_TRUE(observer.CheckUpdated()); |
352 ASSERT_TRUE(observer.CheckRemoved()); | 492 ASSERT_TRUE(observer.CheckRemoved()); |
353 } | 493 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 | 548 |
409 // Currently, a notification would be generated if the danger type is anything | 549 // Currently, a notification would be generated if the danger type is anything |
410 // other than NOT_DANGEROUS. | 550 // other than NOT_DANGEROUS. |
411 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 551 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
412 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 552 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
413 EXPECT_FALSE(observer.CheckUpdated()); | 553 EXPECT_FALSE(observer.CheckUpdated()); |
414 RunAllPendingInMessageLoops(); | 554 RunAllPendingInMessageLoops(); |
415 EXPECT_TRUE(observer.CheckUpdated()); | 555 EXPECT_TRUE(observer.CheckUpdated()); |
416 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 556 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
417 | 557 |
418 CleanupItem(item, download_file); | 558 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
419 } | 559 } |
420 | 560 |
421 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 561 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
422 DownloadItemImpl* item = CreateDownloadItem(); | 562 DownloadItemImpl* item = CreateDownloadItem(); |
423 MockObserver observer(item); | 563 MockObserver observer(item); |
564 MockDownloadFile* mock_download_file(new MockDownloadFile); | |
565 scoped_ptr<DownloadFile> download_file(mock_download_file); | |
566 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
567 new NiceMock<MockRequestHandle>); | |
568 | |
569 EXPECT_CALL(*mock_download_file, Initialize(_)); | |
570 item->Start(download_file.Pass(), request_handle.Pass()); | |
424 | 571 |
425 item->TogglePause(); | 572 item->TogglePause(); |
426 ASSERT_TRUE(observer.CheckUpdated()); | 573 ASSERT_TRUE(observer.CheckUpdated()); |
427 | 574 |
428 item->TogglePause(); | 575 item->TogglePause(); |
429 ASSERT_TRUE(observer.CheckUpdated()); | 576 ASSERT_TRUE(observer.CheckUpdated()); |
577 RunAllPendingInMessageLoops(); | |
578 | |
579 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); | |
430 } | 580 } |
431 | 581 |
432 TEST_F(DownloadItemTest, DisplayName) { | 582 TEST_F(DownloadItemTest, DisplayName) { |
433 DownloadItemImpl* item = CreateDownloadItem(); | 583 DownloadItemImpl* item = CreateDownloadItem(); |
434 DownloadItemImplDelegate::DownloadTargetCallback callback; | 584 DownloadItemImplDelegate::DownloadTargetCallback callback; |
435 MockDownloadFile* download_file = | 585 MockDownloadFile* download_file = |
436 AddDownloadFileToDownloadItem(item, &callback); | 586 AddDownloadFileToDownloadItem(item, &callback); |
437 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 587 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
438 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 588 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
439 EXPECT_EQ(FILE_PATH_LITERAL(""), | 589 EXPECT_EQ(FILE_PATH_LITERAL(""), |
440 item->GetFileNameToReportUser().value()); | 590 item->GetFileNameToReportUser().value()); |
441 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 591 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
442 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 592 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
443 intermediate_path)); | 593 intermediate_path)); |
444 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 594 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
445 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 595 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
446 RunAllPendingInMessageLoops(); | 596 RunAllPendingInMessageLoops(); |
447 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 597 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
448 item->GetFileNameToReportUser().value()); | 598 item->GetFileNameToReportUser().value()); |
449 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 599 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
450 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 600 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
451 item->GetFileNameToReportUser().value()); | 601 item->GetFileNameToReportUser().value()); |
452 CleanupItem(item, download_file); | 602 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
453 } | 603 } |
454 | 604 |
455 // Test to make sure that Start method calls DF initialize properly. | 605 // Test to make sure that Start method calls DF initialize properly. |
456 TEST_F(DownloadItemTest, Start) { | 606 TEST_F(DownloadItemTest, Start) { |
457 MockDownloadFile* mock_download_file(new MockDownloadFile); | 607 MockDownloadFile* mock_download_file(new MockDownloadFile); |
458 scoped_ptr<DownloadFile> download_file(mock_download_file); | 608 scoped_ptr<DownloadFile> download_file(mock_download_file); |
459 DownloadItemImpl* item = CreateDownloadItem(); | 609 DownloadItemImpl* item = CreateDownloadItem(); |
460 EXPECT_CALL(*mock_download_file, Initialize(_)); | 610 EXPECT_CALL(*mock_download_file, Initialize(_)); |
461 item->Start(download_file.Pass()); | 611 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
612 new NiceMock<MockRequestHandle>); | |
613 item->Start(download_file.Pass(), request_handle.Pass()); | |
462 | 614 |
463 CleanupItem(item, mock_download_file); | 615 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
464 } | 616 } |
465 | 617 |
466 // Test that the delegate is invoked after the download file is renamed. | 618 // Test that the delegate is invoked after the download file is renamed. |
467 TEST_F(DownloadItemTest, CallbackAfterRename) { | 619 TEST_F(DownloadItemTest, CallbackAfterRename) { |
468 DownloadItemImpl* item = CreateDownloadItem(); | 620 DownloadItemImpl* item = CreateDownloadItem(); |
469 DownloadItemImplDelegate::DownloadTargetCallback callback; | 621 DownloadItemImplDelegate::DownloadTargetCallback callback; |
470 MockDownloadFile* download_file = | 622 MockDownloadFile* download_file = |
471 AddDownloadFileToDownloadItem(item, &callback); | 623 AddDownloadFileToDownloadItem(item, &callback); |
472 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 624 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
473 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 625 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
530 const DownloadInterruptReason reason( | 682 const DownloadInterruptReason reason( |
531 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 683 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
532 | 684 |
533 // Confirm interrupt sets state properly. | 685 // Confirm interrupt sets state properly. |
534 EXPECT_CALL(*download_file, Cancel()); | 686 EXPECT_CALL(*download_file, Cancel()); |
535 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 687 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
536 RunAllPendingInMessageLoops(); | 688 RunAllPendingInMessageLoops(); |
537 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 689 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
538 EXPECT_EQ(reason, item->GetLastReason()); | 690 EXPECT_EQ(reason, item->GetLastReason()); |
539 | 691 |
540 // Cancel should result in no change. | 692 // Cancel should kill it. |
541 item->Cancel(true); | 693 item->Cancel(true); |
542 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 694 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
543 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 695 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
544 } | 696 } |
545 | 697 |
546 TEST_F(DownloadItemTest, Canceled) { | 698 TEST_F(DownloadItemTest, Canceled) { |
547 DownloadItemImpl* item = CreateDownloadItem(); | 699 DownloadItemImpl* item = CreateDownloadItem(); |
548 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 700 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
549 | 701 |
550 // Confirm cancel sets state properly. | 702 // Confirm cancel sets state properly. |
551 EXPECT_CALL(*download_file, Cancel()); | 703 EXPECT_CALL(*download_file, Cancel()); |
552 item->Cancel(true); | 704 item->Cancel(true); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 ASSERT_TRUE(item->IsCancelled()); | 870 ASSERT_TRUE(item->IsCancelled()); |
719 EXPECT_FALSE(item->CanShowInFolder()); | 871 EXPECT_FALSE(item->CanShowInFolder()); |
720 EXPECT_FALSE(item->CanOpenDownload()); | 872 EXPECT_FALSE(item->CanOpenDownload()); |
721 } | 873 } |
722 | 874 |
723 TEST(MockDownloadItem, Compiles) { | 875 TEST(MockDownloadItem, Compiles) { |
724 MockDownloadItem mock_item; | 876 MockDownloadItem mock_item; |
725 } | 877 } |
726 | 878 |
727 } // namespace content | 879 } // namespace content |
OLD | NEW |