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: |
| 51 MockDelegate() : DownloadItemImplDelegate() { |
| 52 SetDefaultExpectations(); |
| 53 } |
| 54 |
47 MOCK_METHOD2(DetermineDownloadTarget, void( | 55 MOCK_METHOD2(DetermineDownloadTarget, void( |
48 DownloadItemImpl*, const DownloadTargetCallback&)); | 56 DownloadItemImpl*, const DownloadTargetCallback&)); |
49 MOCK_METHOD2(ShouldOpenDownload, | 57 MOCK_METHOD2(ShouldOpenDownload, |
50 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); | 58 bool(DownloadItemImpl*, const ShouldOpenDownloadCallback&)); |
51 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); | 59 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath&)); |
52 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); | 60 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl*)); |
| 61 |
| 62 virtual void ResumeInterruptedDownload( |
| 63 scoped_ptr<DownloadUrlParameters> params, DownloadId id) OVERRIDE { |
| 64 MockResumeInterruptedDownload(params.get(), id); |
| 65 } |
| 66 MOCK_METHOD2(MockResumeInterruptedDownload, |
| 67 void(DownloadUrlParameters* params, DownloadId id)); |
| 68 |
53 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); | 69 MOCK_CONST_METHOD0(GetBrowserContext, BrowserContext*()); |
54 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); | 70 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl*)); |
55 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); | 71 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl*)); |
56 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); | 72 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl*)); |
57 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); | 73 MOCK_METHOD1(ShowDownloadInBrowser, void(DownloadItemImpl*)); |
58 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); | 74 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl*)); |
| 75 |
| 76 void VerifyAndClearExpectations() { |
| 77 ::testing::Mock::VerifyAndClearExpectations(this); |
| 78 SetDefaultExpectations(); |
| 79 } |
| 80 |
| 81 private: |
| 82 void SetDefaultExpectations() { |
| 83 EXPECT_CALL(*this, AssertStateConsistent(_)) |
| 84 .WillRepeatedly(Return()); |
| 85 EXPECT_CALL(*this, ShouldOpenFileBasedOnExtension(_)) |
| 86 .WillRepeatedly(Return(false)); |
| 87 EXPECT_CALL(*this, ShouldOpenDownload(_, _)) |
| 88 .WillRepeatedly(Return(true)); |
| 89 } |
59 }; | 90 }; |
60 | 91 |
61 class MockRequestHandle : public DownloadRequestHandleInterface { | 92 class MockRequestHandle : public DownloadRequestHandleInterface { |
62 public: | 93 public: |
63 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 94 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
64 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 95 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
65 MOCK_CONST_METHOD0(PauseRequest, void()); | 96 MOCK_CONST_METHOD0(PauseRequest, void()); |
66 MOCK_CONST_METHOD0(ResumeRequest, void()); | 97 MOCK_CONST_METHOD0(ResumeRequest, void()); |
67 MOCK_CONST_METHOD0(CancelRequest, void()); | 98 MOCK_CONST_METHOD0(CancelRequest, void()); |
68 MOCK_CONST_METHOD0(DebugString, std::string()); | 99 MOCK_CONST_METHOD0(DebugString, std::string()); |
(...skipping 12 matching lines...) Expand all Loading... |
81 } | 112 } |
82 | 113 |
83 } // namespace | 114 } // namespace |
84 | 115 |
85 class DownloadItemTest : public testing::Test { | 116 class DownloadItemTest : public testing::Test { |
86 public: | 117 public: |
87 class MockObserver : public DownloadItem::Observer { | 118 class MockObserver : public DownloadItem::Observer { |
88 public: | 119 public: |
89 explicit MockObserver(DownloadItem* item) | 120 explicit MockObserver(DownloadItem* item) |
90 : item_(item), | 121 : item_(item), |
| 122 last_state_(item->GetState()), |
91 removed_(false), | 123 removed_(false), |
92 destroyed_(false), | 124 destroyed_(false), |
93 updated_(false) { | 125 updated_(false), |
| 126 interrupt_count_(0), |
| 127 resume_count_(0) { |
94 item_->AddObserver(this); | 128 item_->AddObserver(this); |
95 } | 129 } |
96 | 130 |
97 virtual ~MockObserver() { | 131 virtual ~MockObserver() { |
98 if (item_) item_->RemoveObserver(this); | 132 if (item_) item_->RemoveObserver(this); |
99 } | 133 } |
100 | 134 |
101 virtual void OnDownloadRemoved(DownloadItem* download) { | 135 virtual void OnDownloadRemoved(DownloadItem* download) { |
| 136 DVLOG(20) << " " << __FUNCTION__ |
| 137 << " download = " << download->DebugString(false); |
102 removed_ = true; | 138 removed_ = true; |
103 } | 139 } |
104 | 140 |
105 virtual void OnDownloadUpdated(DownloadItem* download) { | 141 virtual void OnDownloadUpdated(DownloadItem* download) { |
| 142 DVLOG(20) << " " << __FUNCTION__ |
| 143 << " download = " << download->DebugString(false); |
106 updated_ = true; | 144 updated_ = true; |
| 145 DownloadItem::DownloadState new_state = download->GetState(); |
| 146 if (last_state_ == DownloadItem::IN_PROGRESS && |
| 147 new_state == DownloadItem::INTERRUPTED) { |
| 148 interrupt_count_++; |
| 149 } |
| 150 if (last_state_ == DownloadItem::INTERRUPTED && |
| 151 new_state == DownloadItem::IN_PROGRESS) { |
| 152 resume_count_++; |
| 153 } |
| 154 last_state_ = new_state; |
107 } | 155 } |
108 | 156 |
109 virtual void OnDownloadOpened(DownloadItem* download) { | 157 virtual void OnDownloadOpened(DownloadItem* download) { |
| 158 DVLOG(20) << " " << __FUNCTION__ |
| 159 << " download = " << download->DebugString(false); |
110 } | 160 } |
111 | 161 |
112 virtual void OnDownloadDestroyed(DownloadItem* download) { | 162 virtual void OnDownloadDestroyed(DownloadItem* download) { |
| 163 DVLOG(20) << " " << __FUNCTION__ |
| 164 << " download = " << download->DebugString(false); |
113 destroyed_ = true; | 165 destroyed_ = true; |
114 item_->RemoveObserver(this); | 166 item_->RemoveObserver(this); |
115 item_ = NULL; | 167 item_ = NULL; |
116 } | 168 } |
117 | 169 |
118 bool CheckRemoved() { | 170 bool CheckRemoved() { |
119 return removed_; | 171 return removed_; |
120 } | 172 } |
121 | 173 |
122 bool CheckDestroyed() { | 174 bool CheckDestroyed() { |
123 return destroyed_; | 175 return destroyed_; |
124 } | 176 } |
125 | 177 |
126 bool CheckUpdated() { | 178 bool CheckUpdated() { |
127 bool was_updated = updated_; | 179 bool was_updated = updated_; |
128 updated_ = false; | 180 updated_ = false; |
129 return was_updated; | 181 return was_updated; |
130 } | 182 } |
131 | 183 |
| 184 int GetInterruptCount() { |
| 185 return interrupt_count_; |
| 186 } |
| 187 |
| 188 int GetResumeCount() { |
| 189 return resume_count_; |
| 190 } |
| 191 |
132 private: | 192 private: |
133 DownloadItem* item_; | 193 DownloadItem* item_; |
| 194 DownloadItem::DownloadState last_state_; |
134 bool removed_; | 195 bool removed_; |
135 bool destroyed_; | 196 bool destroyed_; |
136 bool updated_; | 197 bool updated_; |
| 198 int interrupt_count_; |
| 199 int resume_count_; |
137 }; | 200 }; |
138 | 201 |
139 DownloadItemTest() | 202 DownloadItemTest() |
140 : ui_thread_(BrowserThread::UI, &loop_), | 203 : ui_thread_(BrowserThread::UI, &loop_), |
141 file_thread_(BrowserThread::FILE, &loop_), | 204 file_thread_(BrowserThread::FILE, &loop_), |
142 delegate_() { | 205 delegate_() { |
143 } | 206 } |
144 | 207 |
145 ~DownloadItemTest() { | 208 ~DownloadItemTest() { |
146 } | 209 } |
(...skipping 16 matching lines...) Expand all Loading... |
163 // don't call the function that deletes it, so we do so ourselves. | 226 // don't call the function that deletes it, so we do so ourselves. |
164 scoped_ptr<DownloadCreateInfo> info_; | 227 scoped_ptr<DownloadCreateInfo> info_; |
165 | 228 |
166 info_.reset(new DownloadCreateInfo()); | 229 info_.reset(new DownloadCreateInfo()); |
167 static int next_id; | 230 static int next_id; |
168 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); | 231 info_->download_id = DownloadId(kValidDownloadItemIdDomain, ++next_id); |
169 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); | 232 info_->save_info = scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()); |
170 info_->save_info->prompt_for_save_location = false; | 233 info_->save_info->prompt_for_save_location = false; |
171 info_->url_chain.push_back(GURL()); | 234 info_->url_chain.push_back(GURL()); |
172 | 235 |
173 scoped_ptr<DownloadRequestHandleInterface> request_handle( | |
174 new testing::NiceMock<MockRequestHandle>); | |
175 DownloadItemImpl* download = | 236 DownloadItemImpl* download = |
176 new DownloadItemImpl(&delegate_, *(info_.get()), | 237 new DownloadItemImpl(&delegate_, *(info_.get()), net::BoundNetLog()); |
177 request_handle.Pass(), net::BoundNetLog()); | |
178 allocated_downloads_.insert(download); | 238 allocated_downloads_.insert(download); |
179 return download; | 239 return download; |
180 } | 240 } |
181 | 241 |
182 // Add DownloadFile to DownloadItem | 242 // Add DownloadFile to DownloadItem |
183 MockDownloadFile* AddDownloadFileToDownloadItem( | 243 MockDownloadFile* AddDownloadFileToDownloadItem( |
184 DownloadItemImpl* item, | 244 DownloadItemImpl* item, |
185 DownloadItemImplDelegate::DownloadTargetCallback *callback) { | 245 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
186 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); | 246 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
187 scoped_ptr<DownloadFile> download_file(mock_download_file); | 247 scoped_ptr<DownloadFile> download_file(mock_download_file); |
188 EXPECT_CALL(*mock_download_file, Initialize(_)); | 248 EXPECT_CALL(*mock_download_file, Initialize(_)); |
189 if (callback) { | 249 if (callback) { |
190 // Save the callback. | 250 // Save the callback. |
191 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) | 251 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
192 .WillOnce(SaveArg<1>(callback)); | 252 .WillOnce(SaveArg<1>(callback)); |
193 } else { | 253 } else { |
194 // Drop it on the floor. | 254 // Drop it on the floor. |
195 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); | 255 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
196 } | 256 } |
197 | 257 |
198 item->Start(download_file.Pass()); | 258 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 259 new NiceMock<MockRequestHandle>); |
| 260 item->Start(download_file.Pass(), request_handle.Pass()); |
199 loop_.RunUntilIdle(); | 261 loop_.RunUntilIdle(); |
200 | 262 |
201 // So that we don't have a function writing to a stack variable | 263 // So that we don't have a function writing to a stack variable |
202 // lying around if the above failed. | 264 // lying around if the above failed. |
203 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 265 mock_delegate()->VerifyAndClearExpectations(); |
| 266 EXPECT_CALL(*mock_delegate(), AssertStateConsistent(_)) |
| 267 .WillRepeatedly(Return()); |
| 268 EXPECT_CALL(*mock_delegate(), ShouldOpenFileBasedOnExtension(_)) |
| 269 .WillRepeatedly(Return(false)); |
| 270 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(_, _)) |
| 271 .WillRepeatedly(Return(true)); |
204 | 272 |
205 return mock_download_file; | 273 return mock_download_file; |
206 } | 274 } |
207 | 275 |
208 // Perform the intermediate rename for |item|. The target path for the | 276 // Perform the intermediate rename for |item|. The target path for the |
209 // download will be set to kDummyPath. Returns the MockDownloadFile* that was | 277 // download will be set to kDummyPath. Returns the MockDownloadFile* that was |
210 // added to the DownloadItem. | 278 // added to the DownloadItem. |
211 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item) { | 279 MockDownloadFile* DoIntermediateRename(DownloadItemImpl* item) { |
212 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 280 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
213 EXPECT_TRUE(item->GetTargetFilePath().empty()); | 281 EXPECT_TRUE(item->GetTargetFilePath().empty()); |
214 DownloadItemImplDelegate::DownloadTargetCallback callback; | 282 DownloadItemImplDelegate::DownloadTargetCallback callback; |
215 MockDownloadFile* download_file = | 283 MockDownloadFile* download_file = |
216 AddDownloadFileToDownloadItem(item, &callback); | 284 AddDownloadFileToDownloadItem(item, &callback); |
217 FilePath target_path(kDummyPath); | 285 FilePath target_path(kDummyPath); |
218 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 286 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
219 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 287 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
220 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 288 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
221 intermediate_path)); | 289 intermediate_path)); |
| 290 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
222 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 291 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
223 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 292 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
224 RunAllPendingInMessageLoops(); | 293 RunAllPendingInMessageLoops(); |
225 return download_file; | 294 return download_file; |
226 } | 295 } |
227 | 296 |
228 // Cleanup a download item (specifically get rid of the DownloadFile on it). | 297 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
229 // The item must be in the IN_PROGRESS state. | 298 // The item must be in the expected state. |
230 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { | 299 void CleanupItem(DownloadItemImpl* item, |
231 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 300 MockDownloadFile* download_file, |
| 301 DownloadItem::DownloadState expected_state) { |
| 302 EXPECT_EQ(expected_state, item->GetState()); |
232 | 303 |
233 EXPECT_CALL(*download_file, Cancel()); | 304 if (expected_state == DownloadItem::IN_PROGRESS) { |
234 item->Cancel(true); | 305 EXPECT_CALL(*download_file, Cancel()); |
235 loop_.RunUntilIdle(); | 306 item->Cancel(true); |
| 307 loop_.RunUntilIdle(); |
| 308 } |
236 } | 309 } |
237 | 310 |
238 // Destroy a previously created download item. | 311 // Destroy a previously created download item. |
239 void DestroyDownloadItem(DownloadItem* item) { | 312 void DestroyDownloadItem(DownloadItem* item) { |
240 allocated_downloads_.erase(item); | 313 allocated_downloads_.erase(item); |
241 delete item; | 314 delete item; |
242 } | 315 } |
243 | 316 |
244 void RunAllPendingInMessageLoops() { | 317 void RunAllPendingInMessageLoops() { |
245 loop_.RunUntilIdle(); | 318 loop_.RunUntilIdle(); |
246 } | 319 } |
247 | 320 |
248 MockDelegate* mock_delegate() { | 321 MockDelegate* mock_delegate() { |
249 return &delegate_; | 322 return &delegate_; |
250 } | 323 } |
251 | 324 |
252 private: | 325 private: |
253 MessageLoopForUI loop_; | 326 MessageLoopForUI loop_; |
254 TestBrowserThread ui_thread_; // UI thread | 327 TestBrowserThread ui_thread_; // UI thread |
255 TestBrowserThread file_thread_; // FILE thread | 328 TestBrowserThread file_thread_; // FILE thread |
256 testing::NiceMock<MockDelegate> delegate_; | 329 StrictMock<MockDelegate> delegate_; |
257 std::set<DownloadItem*> allocated_downloads_; | 330 std::set<DownloadItem*> allocated_downloads_; |
258 }; | 331 }; |
259 | 332 |
260 // Tests to ensure calls that change a DownloadItem generate an update to | 333 // Tests to ensure calls that change a DownloadItem generate an update to |
261 // observers. | 334 // observers. |
262 // State changing functions not tested: | 335 // State changing functions not tested: |
263 // void OpenDownload(); | 336 // void OpenDownload(); |
264 // void ShowDownloadInShell(); | 337 // void ShowDownloadInShell(); |
265 // void CompleteDelayedDownload(); | 338 // void CompleteDelayedDownload(); |
266 // set_* mutators | 339 // set_* mutators |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 item->OnDownloadedFileRemoved(); | 384 item->OnDownloadedFileRemoved(); |
312 ASSERT_TRUE(observer.CheckUpdated()); | 385 ASSERT_TRUE(observer.CheckUpdated()); |
313 } | 386 } |
314 | 387 |
315 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 388 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
316 DownloadItemImpl* item = CreateDownloadItem(); | 389 DownloadItemImpl* item = CreateDownloadItem(); |
317 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 390 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
318 EXPECT_CALL(*download_file, Cancel()); | 391 EXPECT_CALL(*download_file, Cancel()); |
319 MockObserver observer(item); | 392 MockObserver observer(item); |
320 | 393 |
| 394 EXPECT_CALL(*mock_delegate(), MockResumeInterruptedDownload(_,_)) |
| 395 .Times(0); |
| 396 |
321 item->DestinationObserverAsWeakPtr()->DestinationError( | 397 item->DestinationObserverAsWeakPtr()->DestinationError( |
322 DOWNLOAD_INTERRUPT_REASON_NONE); | 398 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
323 ASSERT_TRUE(observer.CheckUpdated()); | 399 ASSERT_TRUE(observer.CheckUpdated()); |
324 } | 400 } |
325 | 401 |
326 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 402 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
327 DownloadItemImpl* item = CreateDownloadItem(); | 403 DownloadItemImpl* item = CreateDownloadItem(); |
328 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 404 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
329 EXPECT_CALL(*download_file, Cancel()); | 405 EXPECT_CALL(*download_file, Cancel()); |
| 406 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
330 MockObserver observer(item); | 407 MockObserver observer(item); |
331 | 408 |
332 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 409 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
333 ASSERT_TRUE(observer.CheckUpdated()); | 410 ASSERT_TRUE(observer.CheckUpdated()); |
334 } | 411 } |
335 | 412 |
336 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 413 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
337 DownloadItemImpl* item = CreateDownloadItem(); | 414 DownloadItemImpl* item = CreateDownloadItem(); |
338 MockObserver observer(item); | 415 MockObserver observer(item); |
339 | 416 |
340 DestroyDownloadItem(item); | 417 DestroyDownloadItem(item); |
341 ASSERT_TRUE(observer.CheckDestroyed()); | 418 ASSERT_TRUE(observer.CheckDestroyed()); |
342 } | 419 } |
343 | 420 |
| 421 TEST_F(DownloadItemTest, ContinueAfterInterrupted) { |
| 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 continuable interrupt. |
| 428 EXPECT_CALL(*download_file, Detach()); |
| 429 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 430 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 431 ASSERT_TRUE(observer.CheckUpdated()); |
| 432 // Should attempt to auto-resume. Because we don't have a mock WebContents, |
| 433 // ResumeInterruptedDownload() will abort early, with another interrupt, |
| 434 // which will be ignored. |
| 435 ASSERT_EQ(1, observer.GetInterruptCount()); |
| 436 ASSERT_EQ(0, observer.GetResumeCount()); |
| 437 RunAllPendingInMessageLoops(); |
| 438 |
| 439 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); |
| 440 } |
| 441 |
| 442 // Same as above, but with a non-continuable interrupt. |
| 443 TEST_F(DownloadItemTest, RestartAfterInterrupted) { |
| 444 DownloadItemImpl* item = CreateDownloadItem(); |
| 445 MockObserver observer(item); |
| 446 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 447 MockDownloadFile* download_file = DoIntermediateRename(item); |
| 448 |
| 449 // Interrupt the download, using a restartable interrupt. |
| 450 EXPECT_CALL(*download_file, Cancel()); |
| 451 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 452 DOWNLOAD_INTERRUPT_REASON_FILE_FAILED); |
| 453 ASSERT_TRUE(observer.CheckUpdated()); |
| 454 // Should not try to auto-resume. |
| 455 ASSERT_EQ(1, observer.GetInterruptCount()); |
| 456 ASSERT_EQ(0, observer.GetResumeCount()); |
| 457 RunAllPendingInMessageLoops(); |
| 458 |
| 459 CleanupItem(item, download_file, DownloadItem::INTERRUPTED); |
| 460 } |
| 461 |
| 462 TEST_F(DownloadItemTest, LimitRestartsAfterInterrupted) { |
| 463 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 464 switches::kEnableDownloadResumption); |
| 465 |
| 466 DownloadItemImpl* item = CreateDownloadItem(); |
| 467 base::WeakPtr<DownloadDestinationObserver> as_observer( |
| 468 item->DestinationObserverAsWeakPtr()); |
| 469 MockObserver observer(item); |
| 470 MockDownloadFile* mock_download_file(NULL); |
| 471 scoped_ptr<DownloadFile> download_file; |
| 472 MockRequestHandle* mock_request_handle(NULL); |
| 473 scoped_ptr<DownloadRequestHandleInterface> request_handle; |
| 474 |
| 475 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 476 .WillRepeatedly(Return()); |
| 477 for (int i = 0; i < (DownloadItemImpl::kMaxAutoResumeAttempts + 1); ++i) { |
| 478 DVLOG(20) << "Loop iteration " << i; |
| 479 |
| 480 mock_download_file = new NiceMock<MockDownloadFile>; |
| 481 download_file.reset(mock_download_file); |
| 482 mock_request_handle = new NiceMock<MockRequestHandle>; |
| 483 request_handle.reset(mock_request_handle); |
| 484 |
| 485 // It's too complicated to set up a WebContents instance that would cause |
| 486 // the MockDownloadItemDelegate's ResumeInterruptedDownload() function |
| 487 // to be callled, so we simply verify that GetWebContents() is called. |
| 488 if (i < (DownloadItemImpl::kMaxAutoResumeAttempts - 1)) { |
| 489 EXPECT_CALL(*mock_request_handle, GetWebContents()) |
| 490 .WillOnce(Return(static_cast<WebContents*>(NULL))); |
| 491 } |
| 492 |
| 493 item->Start(download_file.Pass(), request_handle.Pass()); |
| 494 |
| 495 ASSERT_EQ(i, observer.GetResumeCount()); |
| 496 |
| 497 // Use a continuable interrupt. |
| 498 item->DestinationObserverAsWeakPtr()->DestinationError( |
| 499 DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR); |
| 500 |
| 501 ASSERT_EQ(i + 1, observer.GetInterruptCount()); |
| 502 } |
| 503 |
| 504 CleanupItem(item, mock_download_file, DownloadItem::INTERRUPTED); |
| 505 } |
| 506 |
344 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 507 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
345 DownloadItemImpl* item = CreateDownloadItem(); | 508 DownloadItemImpl* item = CreateDownloadItem(); |
346 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 509 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
347 EXPECT_CALL(*download_file, Cancel()); | 510 EXPECT_CALL(*download_file, Cancel()); |
| 511 EXPECT_CALL(*mock_delegate(), DownloadRemoved(_)); |
348 MockObserver observer(item); | 512 MockObserver observer(item); |
349 | 513 |
350 item->Remove(); | 514 item->Remove(); |
351 ASSERT_TRUE(observer.CheckUpdated()); | 515 ASSERT_TRUE(observer.CheckUpdated()); |
352 ASSERT_TRUE(observer.CheckRemoved()); | 516 ASSERT_TRUE(observer.CheckRemoved()); |
353 } | 517 } |
354 | 518 |
355 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 519 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
356 // Setting to NOT_DANGEROUS does not trigger a notification. | 520 // Setting to NOT_DANGEROUS does not trigger a notification. |
357 DownloadItemImpl* safe_item = CreateDownloadItem(); | 521 DownloadItemImpl* safe_item = CreateDownloadItem(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 DownloadItemImplDelegate::DownloadTargetCallback callback; | 562 DownloadItemImplDelegate::DownloadTargetCallback callback; |
399 MockDownloadFile* download_file = | 563 MockDownloadFile* download_file = |
400 AddDownloadFileToDownloadItem(item, &callback); | 564 AddDownloadFileToDownloadItem(item, &callback); |
401 MockObserver observer(item); | 565 MockObserver observer(item); |
402 FilePath target_path(kDummyPath); | 566 FilePath target_path(kDummyPath); |
403 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 567 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
404 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); | 568 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
405 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 569 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
406 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 570 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
407 new_intermediate_path)); | 571 new_intermediate_path)); |
| 572 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
408 | 573 |
409 // Currently, a notification would be generated if the danger type is anything | 574 // Currently, a notification would be generated if the danger type is anything |
410 // other than NOT_DANGEROUS. | 575 // other than NOT_DANGEROUS. |
411 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 576 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
412 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 577 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
413 EXPECT_FALSE(observer.CheckUpdated()); | 578 EXPECT_FALSE(observer.CheckUpdated()); |
414 RunAllPendingInMessageLoops(); | 579 RunAllPendingInMessageLoops(); |
415 EXPECT_TRUE(observer.CheckUpdated()); | 580 EXPECT_TRUE(observer.CheckUpdated()); |
416 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 581 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
417 | 582 |
418 CleanupItem(item, download_file); | 583 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
419 } | 584 } |
420 | 585 |
421 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 586 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
422 DownloadItemImpl* item = CreateDownloadItem(); | 587 DownloadItemImpl* item = CreateDownloadItem(); |
423 MockObserver observer(item); | 588 MockObserver observer(item); |
| 589 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 590 scoped_ptr<DownloadFile> download_file(mock_download_file); |
| 591 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 592 new NiceMock<MockRequestHandle>); |
| 593 |
| 594 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 595 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(_, _)); |
| 596 item->Start(download_file.Pass(), request_handle.Pass()); |
424 | 597 |
425 item->TogglePause(); | 598 item->TogglePause(); |
426 ASSERT_TRUE(observer.CheckUpdated()); | 599 ASSERT_TRUE(observer.CheckUpdated()); |
427 | 600 |
428 item->TogglePause(); | 601 item->TogglePause(); |
429 ASSERT_TRUE(observer.CheckUpdated()); | 602 ASSERT_TRUE(observer.CheckUpdated()); |
| 603 |
| 604 RunAllPendingInMessageLoops(); |
| 605 |
| 606 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
430 } | 607 } |
431 | 608 |
432 TEST_F(DownloadItemTest, DisplayName) { | 609 TEST_F(DownloadItemTest, DisplayName) { |
433 DownloadItemImpl* item = CreateDownloadItem(); | 610 DownloadItemImpl* item = CreateDownloadItem(); |
434 DownloadItemImplDelegate::DownloadTargetCallback callback; | 611 DownloadItemImplDelegate::DownloadTargetCallback callback; |
435 MockDownloadFile* download_file = | 612 MockDownloadFile* download_file = |
436 AddDownloadFileToDownloadItem(item, &callback); | 613 AddDownloadFileToDownloadItem(item, &callback); |
437 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 614 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
438 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 615 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
439 EXPECT_EQ(FILE_PATH_LITERAL(""), | 616 EXPECT_EQ(FILE_PATH_LITERAL(""), |
440 item->GetFileNameToReportUser().value()); | 617 item->GetFileNameToReportUser().value()); |
441 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) | 618 EXPECT_CALL(*download_file, RenameAndUniquify(_, _)) |
442 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 619 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
443 intermediate_path)); | 620 intermediate_path)); |
444 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 621 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
445 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 622 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
| 623 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(_)); |
446 RunAllPendingInMessageLoops(); | 624 RunAllPendingInMessageLoops(); |
447 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 625 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
448 item->GetFileNameToReportUser().value()); | 626 item->GetFileNameToReportUser().value()); |
449 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 627 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
450 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 628 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
451 item->GetFileNameToReportUser().value()); | 629 item->GetFileNameToReportUser().value()); |
452 CleanupItem(item, download_file); | 630 CleanupItem(item, download_file, DownloadItem::IN_PROGRESS); |
453 } | 631 } |
454 | 632 |
455 // Test to make sure that Start method calls DF initialize properly. | 633 // Test to make sure that Start method calls DF initialize properly. |
456 TEST_F(DownloadItemTest, Start) { | 634 TEST_F(DownloadItemTest, Start) { |
457 MockDownloadFile* mock_download_file(new MockDownloadFile); | 635 MockDownloadFile* mock_download_file(new MockDownloadFile); |
458 scoped_ptr<DownloadFile> download_file(mock_download_file); | 636 scoped_ptr<DownloadFile> download_file(mock_download_file); |
459 DownloadItemImpl* item = CreateDownloadItem(); | 637 DownloadItemImpl* item = CreateDownloadItem(); |
460 EXPECT_CALL(*mock_download_file, Initialize(_)); | 638 EXPECT_CALL(*mock_download_file, Initialize(_)); |
461 item->Start(download_file.Pass()); | 639 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
| 640 new NiceMock<MockRequestHandle>); |
| 641 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 642 item->Start(download_file.Pass(), request_handle.Pass()); |
462 | 643 |
463 CleanupItem(item, mock_download_file); | 644 CleanupItem(item, mock_download_file, DownloadItem::IN_PROGRESS); |
464 } | 645 } |
465 | 646 |
466 // Test that the delegate is invoked after the download file is renamed. | 647 // Test that the delegate is invoked after the download file is renamed. |
467 TEST_F(DownloadItemTest, CallbackAfterRename) { | 648 TEST_F(DownloadItemTest, CallbackAfterRename) { |
468 DownloadItemImpl* item = CreateDownloadItem(); | 649 DownloadItemImpl* item = CreateDownloadItem(); |
469 DownloadItemImplDelegate::DownloadTargetCallback callback; | 650 DownloadItemImplDelegate::DownloadTargetCallback callback; |
470 MockDownloadFile* download_file = | 651 MockDownloadFile* download_file = |
471 AddDownloadFileToDownloadItem(item, &callback); | 652 AddDownloadFileToDownloadItem(item, &callback); |
472 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 653 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
473 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 654 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
474 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 655 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
475 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 656 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
476 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 657 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
477 new_intermediate_path)); | 658 new_intermediate_path)); |
478 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) | 659 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) |
479 .Times(1); | 660 .Times(1); |
480 | 661 |
481 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 662 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
482 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 663 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
483 RunAllPendingInMessageLoops(); | 664 RunAllPendingInMessageLoops(); |
484 // All the callbacks should have happened by now. | 665 // All the callbacks should have happened by now. |
485 ::testing::Mock::VerifyAndClearExpectations(download_file); | 666 ::testing::Mock::VerifyAndClearExpectations(download_file); |
486 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 667 mock_delegate()->VerifyAndClearExpectations(); |
487 | 668 |
488 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) | 669 EXPECT_CALL(*download_file, RenameAndAnnotate(final_path, _)) |
489 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 670 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
490 final_path)); | 671 final_path)); |
491 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
492 .WillOnce(Return(true)); | |
493 EXPECT_CALL(*download_file, Detach()); | 672 EXPECT_CALL(*download_file, Detach()); |
494 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 673 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
495 RunAllPendingInMessageLoops(); | 674 RunAllPendingInMessageLoops(); |
496 ::testing::Mock::VerifyAndClearExpectations(download_file); | 675 ::testing::Mock::VerifyAndClearExpectations(download_file); |
497 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 676 mock_delegate()->VerifyAndClearExpectations(); |
498 } | 677 } |
499 | 678 |
500 // Test that the delegate is invoked after the download file is renamed and the | 679 // Test that the delegate is invoked after the download file is renamed and the |
501 // download item is in an interrupted state. | 680 // download item is in an interrupted state. |
502 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { | 681 TEST_F(DownloadItemTest, CallbackAfterInterruptedRename) { |
503 DownloadItemImpl* item = CreateDownloadItem(); | 682 DownloadItemImpl* item = CreateDownloadItem(); |
504 DownloadItemImplDelegate::DownloadTargetCallback callback; | 683 DownloadItemImplDelegate::DownloadTargetCallback callback; |
505 MockDownloadFile* download_file = | 684 MockDownloadFile* download_file = |
506 AddDownloadFileToDownloadItem(item, &callback); | 685 AddDownloadFileToDownloadItem(item, &callback); |
507 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 686 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
508 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 687 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
509 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 688 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
510 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) | 689 EXPECT_CALL(*download_file, RenameAndUniquify(intermediate_path, _)) |
511 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, | 690 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_FILE_FAILED, |
512 new_intermediate_path)); | 691 new_intermediate_path)); |
513 EXPECT_CALL(*download_file, Cancel()) | 692 EXPECT_CALL(*download_file, Cancel()) |
514 .Times(1); | 693 .Times(1); |
515 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) | 694 EXPECT_CALL(*mock_delegate(), ShowDownloadInBrowser(item)) |
516 .Times(1); | 695 .Times(1); |
517 | 696 |
518 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 697 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
519 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); | 698 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
520 RunAllPendingInMessageLoops(); | 699 RunAllPendingInMessageLoops(); |
521 // All the callbacks should have happened by now. | 700 // All the callbacks should have happened by now. |
522 ::testing::Mock::VerifyAndClearExpectations(download_file); | 701 ::testing::Mock::VerifyAndClearExpectations(download_file); |
523 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 702 mock_delegate()->VerifyAndClearExpectations(); |
524 } | 703 } |
525 | 704 |
526 TEST_F(DownloadItemTest, Interrupted) { | 705 TEST_F(DownloadItemTest, Interrupted) { |
527 DownloadItemImpl* item = CreateDownloadItem(); | 706 DownloadItemImpl* item = CreateDownloadItem(); |
528 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 707 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
529 | 708 |
530 const DownloadInterruptReason reason( | 709 const DownloadInterruptReason reason( |
531 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 710 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
532 | 711 |
533 // Confirm interrupt sets state properly. | 712 // Confirm interrupt sets state properly. |
534 EXPECT_CALL(*download_file, Cancel()); | 713 EXPECT_CALL(*download_file, Cancel()); |
535 item->DestinationObserverAsWeakPtr()->DestinationError(reason); | 714 item->DestinationObserverAsWeakPtr()->DestinationError(reason); |
536 RunAllPendingInMessageLoops(); | 715 RunAllPendingInMessageLoops(); |
537 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 716 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
538 EXPECT_EQ(reason, item->GetLastReason()); | 717 EXPECT_EQ(reason, item->GetLastReason()); |
539 | 718 |
540 // Cancel should result in no change. | 719 // Cancel should kill it. |
541 item->Cancel(true); | 720 item->Cancel(true); |
542 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 721 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
543 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); | 722 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, item->GetLastReason()); |
544 } | 723 } |
545 | 724 |
546 TEST_F(DownloadItemTest, Canceled) { | 725 TEST_F(DownloadItemTest, Canceled) { |
547 DownloadItemImpl* item = CreateDownloadItem(); | 726 DownloadItemImpl* item = CreateDownloadItem(); |
548 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); | 727 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
549 | 728 |
550 // Confirm cancel sets state properly. | 729 // Confirm cancel sets state properly. |
551 EXPECT_CALL(*download_file, Cancel()); | 730 EXPECT_CALL(*download_file, Cancel()); |
552 item->Cancel(true); | 731 item->Cancel(true); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 item->DestinationObserverAsWeakPtr()); | 776 item->DestinationObserverAsWeakPtr()); |
598 MockObserver observer(item); | 777 MockObserver observer(item); |
599 | 778 |
600 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 779 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
601 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); | 780 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); |
602 EXPECT_FALSE(observer.CheckUpdated()); | 781 EXPECT_FALSE(observer.CheckUpdated()); |
603 | 782 |
604 EXPECT_CALL(*download_file, Cancel()); | 783 EXPECT_CALL(*download_file, Cancel()); |
605 as_observer->DestinationError( | 784 as_observer->DestinationError( |
606 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 785 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
607 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 786 mock_delegate()->VerifyAndClearExpectations(); |
608 EXPECT_TRUE(observer.CheckUpdated()); | 787 EXPECT_TRUE(observer.CheckUpdated()); |
609 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 788 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
610 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, | 789 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
611 item->GetLastReason()); | 790 item->GetLastReason()); |
612 } | 791 } |
613 | 792 |
614 TEST_F(DownloadItemTest, DestinationCompleted) { | 793 TEST_F(DownloadItemTest, DestinationCompleted) { |
615 DownloadItemImpl* item = CreateDownloadItem(); | 794 DownloadItemImpl* item = CreateDownloadItem(); |
616 base::WeakPtr<DownloadDestinationObserver> as_observer( | 795 base::WeakPtr<DownloadDestinationObserver> as_observer( |
617 item->DestinationObserverAsWeakPtr()); | 796 item->DestinationObserverAsWeakPtr()); |
618 MockObserver observer(item); | 797 MockObserver observer(item); |
619 | 798 |
620 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 799 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
621 EXPECT_EQ("", item->GetHash()); | 800 EXPECT_EQ("", item->GetHash()); |
622 EXPECT_EQ("", item->GetHashState()); | 801 EXPECT_EQ("", item->GetHashState()); |
623 EXPECT_FALSE(item->AllDataSaved()); | 802 EXPECT_FALSE(item->AllDataSaved()); |
624 EXPECT_FALSE(observer.CheckUpdated()); | 803 EXPECT_FALSE(observer.CheckUpdated()); |
625 | 804 |
626 as_observer->DestinationUpdate(10, 20, "deadbeef"); | 805 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
627 EXPECT_TRUE(observer.CheckUpdated()); | 806 EXPECT_TRUE(observer.CheckUpdated()); |
628 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. | 807 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. |
629 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 808 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
630 EXPECT_EQ("", item->GetHash()); | 809 EXPECT_EQ("", item->GetHash()); |
631 EXPECT_EQ("deadbeef", item->GetHashState()); | 810 EXPECT_EQ("deadbeef", item->GetHashState()); |
632 EXPECT_FALSE(item->AllDataSaved()); | 811 EXPECT_FALSE(item->AllDataSaved()); |
633 | 812 |
634 as_observer->DestinationCompleted("livebeef"); | 813 as_observer->DestinationCompleted("livebeef"); |
635 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 814 mock_delegate()->VerifyAndClearExpectations(); |
636 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); | 815 EXPECT_EQ(DownloadItem::IN_PROGRESS, item->GetState()); |
637 EXPECT_TRUE(observer.CheckUpdated()); | 816 EXPECT_TRUE(observer.CheckUpdated()); |
638 EXPECT_EQ("livebeef", item->GetHash()); | 817 EXPECT_EQ("livebeef", item->GetHash()); |
639 EXPECT_EQ("", item->GetHashState()); | 818 EXPECT_EQ("", item->GetHashState()); |
640 EXPECT_TRUE(item->AllDataSaved()); | 819 EXPECT_TRUE(item->AllDataSaved()); |
641 } | 820 } |
642 | 821 |
643 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { | 822 TEST_F(DownloadItemTest, EnabledActionsForNormalDownload) { |
644 DownloadItemImpl* item = CreateDownloadItem(); | 823 DownloadItemImpl* item = CreateDownloadItem(); |
645 MockDownloadFile* download_file = DoIntermediateRename(item); | 824 MockDownloadFile* download_file = DoIntermediateRename(item); |
646 | 825 |
647 // InProgress | 826 // InProgress |
648 ASSERT_TRUE(item->IsInProgress()); | 827 ASSERT_TRUE(item->IsInProgress()); |
649 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 828 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
650 EXPECT_TRUE(item->CanShowInFolder()); | 829 EXPECT_TRUE(item->CanShowInFolder()); |
651 EXPECT_TRUE(item->CanOpenDownload()); | 830 EXPECT_TRUE(item->CanOpenDownload()); |
652 | 831 |
653 // Complete | 832 // Complete |
654 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 833 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
655 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 834 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
656 FilePath(kDummyPath))); | 835 FilePath(kDummyPath))); |
657 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
658 .WillOnce(Return(true)); | |
659 EXPECT_CALL(*download_file, Detach()); | 836 EXPECT_CALL(*download_file, Detach()); |
660 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 837 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
661 RunAllPendingInMessageLoops(); | 838 RunAllPendingInMessageLoops(); |
662 | 839 |
663 ASSERT_TRUE(item->IsComplete()); | 840 ASSERT_TRUE(item->IsComplete()); |
664 EXPECT_TRUE(item->CanShowInFolder()); | 841 EXPECT_TRUE(item->CanShowInFolder()); |
665 EXPECT_TRUE(item->CanOpenDownload()); | 842 EXPECT_TRUE(item->CanOpenDownload()); |
666 } | 843 } |
667 | 844 |
668 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { | 845 TEST_F(DownloadItemTest, EnabledActionsForTemporaryDownload) { |
669 DownloadItemImpl* item = CreateDownloadItem(); | 846 DownloadItemImpl* item = CreateDownloadItem(); |
670 MockDownloadFile* download_file = DoIntermediateRename(item); | 847 MockDownloadFile* download_file = DoIntermediateRename(item); |
671 item->SetIsTemporary(true); | 848 item->SetIsTemporary(true); |
672 | 849 |
673 // InProgress Temporary | 850 // InProgress Temporary |
674 ASSERT_TRUE(item->IsInProgress()); | 851 ASSERT_TRUE(item->IsInProgress()); |
675 ASSERT_FALSE(item->GetTargetFilePath().empty()); | 852 ASSERT_FALSE(item->GetTargetFilePath().empty()); |
676 ASSERT_TRUE(item->IsTemporary()); | 853 ASSERT_TRUE(item->IsTemporary()); |
677 EXPECT_FALSE(item->CanShowInFolder()); | 854 EXPECT_FALSE(item->CanShowInFolder()); |
678 EXPECT_FALSE(item->CanOpenDownload()); | 855 EXPECT_FALSE(item->CanOpenDownload()); |
679 | 856 |
680 // Complete Temporary | 857 // Complete Temporary |
681 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) | 858 EXPECT_CALL(*download_file, RenameAndAnnotate(_, _)) |
682 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, | 859 .WillOnce(ScheduleRenameCallback(DOWNLOAD_INTERRUPT_REASON_NONE, |
683 FilePath(kDummyPath))); | 860 FilePath(kDummyPath))); |
684 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item, _)) | |
685 .WillOnce(Return(true)); | |
686 EXPECT_CALL(*download_file, Detach()); | 861 EXPECT_CALL(*download_file, Detach()); |
687 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); | 862 item->DestinationObserverAsWeakPtr()->DestinationCompleted(""); |
688 RunAllPendingInMessageLoops(); | 863 RunAllPendingInMessageLoops(); |
689 | 864 |
690 ASSERT_TRUE(item->IsComplete()); | 865 ASSERT_TRUE(item->IsComplete()); |
691 EXPECT_FALSE(item->CanShowInFolder()); | 866 EXPECT_FALSE(item->CanShowInFolder()); |
692 EXPECT_FALSE(item->CanOpenDownload()); | 867 EXPECT_FALSE(item->CanOpenDownload()); |
693 } | 868 } |
694 | 869 |
695 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { | 870 TEST_F(DownloadItemTest, EnabledActionsForInterruptedDownload) { |
(...skipping 22 matching lines...) Expand all Loading... |
718 ASSERT_TRUE(item->IsCancelled()); | 893 ASSERT_TRUE(item->IsCancelled()); |
719 EXPECT_FALSE(item->CanShowInFolder()); | 894 EXPECT_FALSE(item->CanShowInFolder()); |
720 EXPECT_FALSE(item->CanOpenDownload()); | 895 EXPECT_FALSE(item->CanOpenDownload()); |
721 } | 896 } |
722 | 897 |
723 TEST(MockDownloadItem, Compiles) { | 898 TEST(MockDownloadItem, Compiles) { |
724 MockDownloadItem mock_item; | 899 MockDownloadItem mock_item; |
725 } | 900 } |
726 | 901 |
727 } // namespace content | 902 } // namespace content |
OLD | NEW |