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/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/stl_util.h" | 6 #include "base/stl_util.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "content/browser/download/byte_stream.h" | 8 #include "content/browser/download/byte_stream.h" |
9 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
10 #include "content/browser/download/download_file_manager.h" | 10 #include "content/browser/download/download_file_factory.h" |
11 #include "content/browser/download/download_item_impl.h" | 11 #include "content/browser/download/download_item_impl.h" |
12 #include "content/browser/download/download_item_impl_delegate.h" | 12 #include "content/browser/download/download_item_impl_delegate.h" |
13 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 14 #include "content/browser/download/mock_download_file.h" |
14 #include "content/public/browser/download_id.h" | 15 #include "content/public/browser/download_id.h" |
| 16 #include "content/public/browser/download_destination_observer.h" |
15 #include "content/public/browser/download_interrupt_reasons.h" | 17 #include "content/public/browser/download_interrupt_reasons.h" |
16 #include "content/public/test/mock_download_item.h" | 18 #include "content/public/test/mock_download_item.h" |
17 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 using content::BrowserThread; | 23 using content::BrowserThread; |
22 using content::DownloadId; | 24 using content::DownloadId; |
23 using content::DownloadItem; | 25 using content::DownloadItem; |
24 using content::DownloadManager; | 26 using content::DownloadManager; |
25 using content::MockDownloadItem; | 27 using content::MockDownloadItem; |
26 using content::WebContents; | 28 using content::WebContents; |
27 using ::testing::_; | 29 using ::testing::_; |
28 using ::testing::AllOf; | 30 using ::testing::AllOf; |
29 using ::testing::Property; | 31 using ::testing::Property; |
30 using ::testing::Return; | 32 using ::testing::Return; |
| 33 using ::testing::SaveArg; |
| 34 using ::testing::StrictMock; |
31 | 35 |
32 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; | 36 DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain"; |
33 | 37 |
34 namespace { | 38 namespace { |
35 class MockDelegate : public DownloadItemImplDelegate { | 39 class MockDelegate : public DownloadItemImplDelegate { |
36 public: | 40 public: |
37 MockDelegate(DownloadFileManager* file_manager) | 41 MOCK_METHOD2(DetermineDownloadTarget, void( |
38 : file_manager_(file_manager) { | 42 DownloadItemImpl* download, const DownloadTargetCallback& callback)); |
39 } | 43 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); |
40 MOCK_METHOD2(ReadyForDownloadCompletion, | |
41 void(DownloadItemImpl*, const base::Closure&)); | |
42 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); | 44 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); |
43 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download)); | |
44 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); | 45 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); |
45 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); | 46 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); |
46 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl* download)); | 47 MOCK_METHOD1(UpdatePersistence, void(DownloadItemImpl* download)); |
47 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); | 48 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); |
48 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); | 49 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); |
49 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); | 50 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); |
50 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); | 51 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); |
51 MOCK_METHOD1(DownloadRenamedToIntermediateName, | 52 MOCK_METHOD1(DownloadRenamedToIntermediateName, |
52 void(DownloadItemImpl* download)); | 53 void(DownloadItemImpl* download)); |
53 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); | 54 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); |
54 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); | 55 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); |
55 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { | 56 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE { |
56 return file_manager_; | 57 return file_manager_; |
57 } | 58 } |
58 private: | 59 private: |
59 DownloadFileManager* file_manager_; | 60 DownloadFileManager* file_manager_; |
60 }; | 61 }; |
61 | 62 |
62 class MockRequestHandle : public DownloadRequestHandleInterface { | 63 class MockRequestHandle : public DownloadRequestHandleInterface { |
63 public: | 64 public: |
64 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); | 65 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); |
65 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); | 66 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); |
66 MOCK_CONST_METHOD0(PauseRequest, void()); | 67 MOCK_CONST_METHOD0(PauseRequest, void()); |
67 MOCK_CONST_METHOD0(ResumeRequest, void()); | 68 MOCK_CONST_METHOD0(ResumeRequest, void()); |
68 MOCK_CONST_METHOD0(CancelRequest, void()); | 69 MOCK_CONST_METHOD0(CancelRequest, void()); |
69 MOCK_CONST_METHOD0(DebugString, std::string()); | 70 MOCK_CONST_METHOD0(DebugString, std::string()); |
70 }; | 71 }; |
71 | 72 |
72 class MockDownloadFileFactory : public content::DownloadFileFactory { | |
73 public: | |
74 content::DownloadFile* CreateFile( | |
75 scoped_ptr<DownloadCreateInfo> info, | |
76 scoped_ptr<content::ByteStreamReader> stream_reader, | |
77 DownloadManager* mgr, | |
78 bool calculate_hash, | |
79 const net::BoundNetLog& bound_net_log) { | |
80 return MockCreateFile( | |
81 info.get(), stream_reader.get(), info->request_handle, mgr, | |
82 calculate_hash, bound_net_log); | |
83 } | |
84 | |
85 MOCK_METHOD6(MockCreateFile, | |
86 content::DownloadFile*(DownloadCreateInfo*, | |
87 content::ByteStreamReader*, | |
88 const DownloadRequestHandle&, | |
89 DownloadManager*, | |
90 bool, | |
91 const net::BoundNetLog&)); | |
92 }; | |
93 | |
94 class MockDownloadFileManager : public DownloadFileManager { | |
95 public: | |
96 MockDownloadFileManager(); | |
97 MOCK_METHOD0(Shutdown, void()); | |
98 MOCK_METHOD1(CancelDownload, void(DownloadId)); | |
99 MOCK_METHOD2(CompleteDownload, void(DownloadId, const base::Closure&)); | |
100 MOCK_METHOD1(OnDownloadManagerShutdown, void(DownloadManager*)); | |
101 MOCK_METHOD4(RenameDownloadFile, void(DownloadId, const FilePath&, bool, | |
102 const RenameCompletionCallback&)); | |
103 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int()); | |
104 private: | |
105 ~MockDownloadFileManager() {} | |
106 }; | |
107 | |
108 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on | 73 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on |
109 // the UI thread. Should only be used as the action for | 74 // the UI thread. Should only be used as the action for |
110 // MockDownloadFileManager::Rename*DownloadFile as follows: | 75 // MockDownloadFile::Rename as follows: |
111 // EXPECT_CALL(mock_download_file_manager, | 76 // EXPECT_CALL(download_file, Rename(_,_,_)) |
112 // RenameDownloadFile(_,_,_,_)) | |
113 // .WillOnce(ScheduleRenameCallback(new_path)); | 77 // .WillOnce(ScheduleRenameCallback(new_path)); |
114 ACTION_P(ScheduleRenameCallback, new_path) { | 78 ACTION_P(ScheduleRenameCallback, new_path) { |
115 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
116 BrowserThread::UI, FROM_HERE, | 80 BrowserThread::UI, FROM_HERE, |
117 base::Bind(arg3, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); | 81 base::Bind(arg2, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); |
| 82 } |
| 83 |
| 84 // Schedules a task to invoke the input closure on |
| 85 // the UI thread. Should only be used as the action for |
| 86 // MockDownloadFile::Detach/Cancel as follows: |
| 87 // EXPECT_CALL(download_file, Detach(_)) |
| 88 // .WillOnce(ScheduleClosure())); |
| 89 ACTION(ScheduleClosure) { |
| 90 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, arg0); |
118 } | 91 } |
119 | 92 |
120 // Similarly for scheduling a completion callback. | 93 // Similarly for scheduling a completion callback. |
121 ACTION(ScheduleCompleteCallback) { | 94 ACTION(ScheduleCompleteCallback) { |
122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1)); | 95 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1)); |
123 } | 96 } |
124 | 97 |
125 // Just run the callback in arg1 | |
126 ACTION(RunArg1Callback) { | |
127 arg1.Run(); | |
128 } | |
129 | |
130 MockDownloadFileManager::MockDownloadFileManager() | |
131 : DownloadFileManager(new MockDownloadFileFactory) { | |
132 } | |
133 | |
134 } // namespace | 98 } // namespace |
135 | 99 |
136 class DownloadItemTest : public testing::Test { | 100 class DownloadItemTest : public testing::Test { |
137 public: | 101 public: |
138 class MockObserver : public DownloadItem::Observer { | 102 class MockObserver : public DownloadItem::Observer { |
139 public: | 103 public: |
140 explicit MockObserver(DownloadItem* item) | 104 explicit MockObserver(DownloadItem* item) |
141 : item_(item), | 105 : item_(item), |
142 removed_(false), | 106 removed_(false), |
143 destroyed_(false), | 107 destroyed_(false), |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 private: | 147 private: |
184 DownloadItem* item_; | 148 DownloadItem* item_; |
185 bool removed_; | 149 bool removed_; |
186 bool destroyed_; | 150 bool destroyed_; |
187 bool updated_; | 151 bool updated_; |
188 }; | 152 }; |
189 | 153 |
190 DownloadItemTest() | 154 DownloadItemTest() |
191 : ui_thread_(BrowserThread::UI, &loop_), | 155 : ui_thread_(BrowserThread::UI, &loop_), |
192 file_thread_(BrowserThread::FILE, &loop_), | 156 file_thread_(BrowserThread::FILE, &loop_), |
193 file_manager_(new MockDownloadFileManager), | 157 delegate_() { |
194 delegate_(file_manager_.get()) { | |
195 } | 158 } |
196 | 159 |
197 ~DownloadItemTest() { | 160 ~DownloadItemTest() { |
198 } | 161 } |
199 | 162 |
200 virtual void SetUp() { | 163 virtual void SetUp() { |
201 } | 164 } |
202 | 165 |
203 virtual void TearDown() { | 166 virtual void TearDown() { |
204 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); | 167 ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending(); |
(...skipping 20 matching lines...) Expand all Loading... |
225 | 188 |
226 scoped_ptr<DownloadRequestHandleInterface> request_handle( | 189 scoped_ptr<DownloadRequestHandleInterface> request_handle( |
227 new testing::NiceMock<MockRequestHandle>); | 190 new testing::NiceMock<MockRequestHandle>); |
228 DownloadItemImpl* download = | 191 DownloadItemImpl* download = |
229 new DownloadItemImpl(&delegate_, *(info_.get()), | 192 new DownloadItemImpl(&delegate_, *(info_.get()), |
230 request_handle.Pass(), net::BoundNetLog()); | 193 request_handle.Pass(), net::BoundNetLog()); |
231 allocated_downloads_.insert(download); | 194 allocated_downloads_.insert(download); |
232 return download; | 195 return download; |
233 } | 196 } |
234 | 197 |
| 198 // Add DownloadFile to DownloadItem |
| 199 MockDownloadFile* AddDownloadFileToDownloadItem( |
| 200 DownloadItemImpl* item, |
| 201 DownloadItemImplDelegate::DownloadTargetCallback *callback) { |
| 202 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>); |
| 203 scoped_ptr<content::DownloadFile> download_file(mock_download_file); |
| 204 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 205 if (callback) { |
| 206 // Save the callback. |
| 207 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)) |
| 208 .WillOnce(SaveArg<1>(callback)); |
| 209 } else { |
| 210 // Drop it on the floor. |
| 211 EXPECT_CALL(*mock_delegate(), DetermineDownloadTarget(item, _)); |
| 212 } |
| 213 |
| 214 item->Start(download_file.Pass()); |
| 215 loop_.RunAllPending(); |
| 216 |
| 217 // So that we don't have a function writing to a stack variable |
| 218 // lying around if the above failed. |
| 219 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 220 |
| 221 return mock_download_file; |
| 222 } |
| 223 |
| 224 // Cleanup a download item (specifically get rid of the DownloadFile on it). |
| 225 // The item must be in the IN_PROGRESS state. |
| 226 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) { |
| 227 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 228 |
| 229 EXPECT_CALL(*download_file, Cancel()); |
| 230 EXPECT_CALL(delegate_, DownloadStopped(item)); |
| 231 item->Cancel(true); |
| 232 loop_.RunAllPending(); |
| 233 } |
| 234 |
235 // Destroy a previously created download item. | 235 // Destroy a previously created download item. |
236 void DestroyDownloadItem(DownloadItem* item) { | 236 void DestroyDownloadItem(DownloadItem* item) { |
237 allocated_downloads_.erase(item); | 237 allocated_downloads_.erase(item); |
238 delete item; | 238 delete item; |
239 } | 239 } |
240 | 240 |
241 void RunAllPendingInMessageLoops() { | 241 void RunAllPendingInMessageLoops() { |
242 loop_.RunAllPending(); | 242 loop_.RunAllPending(); |
243 } | 243 } |
244 | 244 |
245 MockDelegate* mock_delegate() { | 245 MockDelegate* mock_delegate() { |
246 return &delegate_; | 246 return &delegate_; |
247 } | 247 } |
248 | 248 |
249 MockDownloadFileManager* mock_file_manager() { | |
250 return file_manager_.get(); | |
251 } | |
252 | |
253 private: | 249 private: |
254 MessageLoopForUI loop_; | 250 MessageLoopForUI loop_; |
255 content::TestBrowserThread ui_thread_; // UI thread | 251 content::TestBrowserThread ui_thread_; // UI thread |
256 content::TestBrowserThread file_thread_; // FILE thread | 252 content::TestBrowserThread file_thread_; // FILE thread |
257 scoped_refptr<MockDownloadFileManager> file_manager_; | |
258 testing::NiceMock<MockDelegate> delegate_; | 253 testing::NiceMock<MockDelegate> delegate_; |
259 std::set<DownloadItem*> allocated_downloads_; | 254 std::set<DownloadItem*> allocated_downloads_; |
260 }; | 255 }; |
261 | 256 |
262 namespace { | 257 namespace { |
263 | 258 |
264 const int kDownloadChunkSize = 1000; | 259 const int kDownloadChunkSize = 1000; |
265 const int kDownloadSpeed = 1000; | 260 const int kDownloadSpeed = 1000; |
266 const int kDummyDBHandle = 10; | 261 const int kDummyDBHandle = 10; |
267 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); | 262 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); |
(...skipping 12 matching lines...) Expand all Loading... |
280 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 275 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
281 MockObserver observer(item); | 276 MockObserver observer(item); |
282 | 277 |
283 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); | 278 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); |
284 ASSERT_TRUE(observer.CheckUpdated()); | 279 ASSERT_TRUE(observer.CheckUpdated()); |
285 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); | 280 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); |
286 } | 281 } |
287 | 282 |
288 TEST_F(DownloadItemTest, NotificationAfterCancel) { | 283 TEST_F(DownloadItemTest, NotificationAfterCancel) { |
289 DownloadItemImpl* user_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 284 DownloadItemImpl* user_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 285 MockDownloadFile* download_file = |
| 286 AddDownloadFileToDownloadItem(user_cancel, NULL); |
| 287 EXPECT_CALL(*download_file, Cancel()); |
290 MockObserver observer1(user_cancel); | 288 MockObserver observer1(user_cancel); |
291 | 289 |
292 user_cancel->Cancel(true); | 290 user_cancel->Cancel(true); |
293 ASSERT_TRUE(observer1.CheckUpdated()); | 291 ASSERT_TRUE(observer1.CheckUpdated()); |
294 | 292 |
295 DownloadItemImpl* system_cancel = | 293 DownloadItemImpl* system_cancel = |
296 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 294 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 295 download_file = AddDownloadFileToDownloadItem(system_cancel, NULL); |
| 296 EXPECT_CALL(*download_file, Cancel()); |
297 MockObserver observer2(system_cancel); | 297 MockObserver observer2(system_cancel); |
298 | 298 |
299 system_cancel->Cancel(false); | 299 system_cancel->Cancel(false); |
300 ASSERT_TRUE(observer2.CheckUpdated()); | 300 ASSERT_TRUE(observer2.CheckUpdated()); |
301 } | 301 } |
302 | 302 |
303 TEST_F(DownloadItemTest, NotificationAfterComplete) { | 303 TEST_F(DownloadItemTest, NotificationAfterComplete) { |
304 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 304 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
305 MockObserver observer(item); | 305 MockObserver observer(item); |
306 | 306 |
307 item->OnAllDataSaved(kDownloadChunkSize, DownloadItem::kEmptyFileHash); | 307 item->OnAllDataSaved(DownloadItem::kEmptyFileHash); |
308 ASSERT_TRUE(observer.CheckUpdated()); | 308 ASSERT_TRUE(observer.CheckUpdated()); |
309 | 309 |
310 item->MarkAsComplete(); | 310 item->MarkAsComplete(); |
311 ASSERT_TRUE(observer.CheckUpdated()); | 311 ASSERT_TRUE(observer.CheckUpdated()); |
312 } | 312 } |
313 | 313 |
314 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { | 314 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { |
315 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 315 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
316 MockObserver observer(item); | 316 MockObserver observer(item); |
317 | 317 |
318 item->OnDownloadedFileRemoved(); | 318 item->OnDownloadedFileRemoved(); |
319 ASSERT_TRUE(observer.CheckUpdated()); | 319 ASSERT_TRUE(observer.CheckUpdated()); |
320 } | 320 } |
321 | 321 |
322 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { | 322 TEST_F(DownloadItemTest, NotificationAfterInterrupted) { |
323 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 323 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 324 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 325 EXPECT_CALL(*download_file, Cancel()); |
324 MockObserver observer(item); | 326 MockObserver observer(item); |
325 | 327 |
326 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE); | 328 item->Interrupt(content::DOWNLOAD_INTERRUPT_REASON_NONE); |
327 ASSERT_TRUE(observer.CheckUpdated()); | 329 ASSERT_TRUE(observer.CheckUpdated()); |
328 } | 330 } |
329 | 331 |
330 TEST_F(DownloadItemTest, NotificationAfterDelete) { | 332 TEST_F(DownloadItemTest, NotificationAfterDelete) { |
331 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 333 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 334 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 335 EXPECT_CALL(*download_file, Cancel()); |
332 MockObserver observer(item); | 336 MockObserver observer(item); |
333 | 337 |
334 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 338 item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
335 ASSERT_TRUE(observer.CheckUpdated()); | 339 ASSERT_TRUE(observer.CheckUpdated()); |
336 } | 340 } |
337 | 341 |
338 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { | 342 TEST_F(DownloadItemTest, NotificationAfterDestroyed) { |
339 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 343 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
340 MockObserver observer(item); | 344 MockObserver observer(item); |
341 | 345 |
342 DestroyDownloadItem(item); | 346 DestroyDownloadItem(item); |
343 ASSERT_TRUE(observer.CheckDestroyed()); | 347 ASSERT_TRUE(observer.CheckDestroyed()); |
344 } | 348 } |
345 | 349 |
346 TEST_F(DownloadItemTest, NotificationAfterRemove) { | 350 TEST_F(DownloadItemTest, NotificationAfterRemove) { |
347 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 351 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 352 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 353 EXPECT_CALL(*download_file, Cancel()); |
348 MockObserver observer(item); | 354 MockObserver observer(item); |
349 | 355 |
350 item->Remove(); | 356 item->Remove(); |
351 ASSERT_TRUE(observer.CheckUpdated()); | 357 ASSERT_TRUE(observer.CheckUpdated()); |
352 ASSERT_TRUE(observer.CheckRemoved()); | 358 ASSERT_TRUE(observer.CheckRemoved()); |
353 } | 359 } |
354 | 360 |
355 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { | 361 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { |
356 // Setting to NOT_DANGEROUS does not trigger a notification. | 362 // Setting to NOT_DANGEROUS does not trigger a notification. |
357 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 363 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
358 MockObserver safe_observer(safe_item); | 364 MockObserver safe_observer(safe_item); |
359 | 365 |
360 safe_item->OnAllDataSaved(1, ""); | 366 safe_item->OnAllDataSaved(""); |
361 EXPECT_TRUE(safe_observer.CheckUpdated()); | 367 EXPECT_TRUE(safe_observer.CheckUpdated()); |
362 safe_item->OnContentCheckCompleted( | 368 safe_item->OnContentCheckCompleted( |
363 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); | 369 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
364 EXPECT_TRUE(safe_observer.CheckUpdated()); | 370 EXPECT_TRUE(safe_observer.CheckUpdated()); |
365 | 371 |
366 // Setting to unsafe url or unsafe file should trigger a notification. | 372 // Setting to unsafe url or unsafe file should trigger a notification. |
367 DownloadItemImpl* unsafeurl_item = | 373 DownloadItemImpl* unsafeurl_item = |
368 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 374 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
369 MockObserver unsafeurl_observer(unsafeurl_item); | 375 MockObserver unsafeurl_observer(unsafeurl_item); |
370 | 376 |
371 unsafeurl_item->OnAllDataSaved(1, ""); | 377 unsafeurl_item->OnAllDataSaved(""); |
372 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 378 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
373 unsafeurl_item->OnContentCheckCompleted( | 379 unsafeurl_item->OnContentCheckCompleted( |
374 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); | 380 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); |
375 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 381 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
376 | 382 |
377 unsafeurl_item->DangerousDownloadValidated(); | 383 unsafeurl_item->DangerousDownloadValidated(); |
378 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); | 384 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); |
379 | 385 |
380 DownloadItemImpl* unsafefile_item = | 386 DownloadItemImpl* unsafefile_item = |
381 CreateDownloadItem(DownloadItem::IN_PROGRESS); | 387 CreateDownloadItem(DownloadItem::IN_PROGRESS); |
382 MockObserver unsafefile_observer(unsafefile_item); | 388 MockObserver unsafefile_observer(unsafefile_item); |
383 | 389 |
384 unsafefile_item->OnAllDataSaved(1, ""); | 390 unsafefile_item->OnAllDataSaved(""); |
385 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 391 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
386 unsafefile_item->OnContentCheckCompleted( | 392 unsafefile_item->OnContentCheckCompleted( |
387 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); | 393 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); |
388 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 394 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
389 | 395 |
390 unsafefile_item->DangerousDownloadValidated(); | 396 unsafefile_item->DangerousDownloadValidated(); |
391 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); | 397 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); |
392 } | 398 } |
393 | 399 |
394 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run | 400 // DownloadItemImpl::OnDownloadTargetDetermined will schedule a task to run |
395 // DownloadFileManager::RenameDownloadFile(). Once the rename | 401 // DownloadFile::Rename(). Once the rename |
396 // completes, DownloadItemImpl receives a notification with the new file | 402 // completes, DownloadItemImpl receives a notification with the new file |
397 // name. Check that observers are updated when the new filename is available and | 403 // name. Check that observers are updated when the new filename is available and |
398 // not before. | 404 // not before. |
399 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { | 405 TEST_F(DownloadItemTest, NotificationAfterOnDownloadTargetDetermined) { |
400 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 406 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 407 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 408 MockDownloadFile* download_file = |
| 409 AddDownloadFileToDownloadItem(item, &callback); |
401 MockObserver observer(item); | 410 MockObserver observer(item); |
402 FilePath target_path(kDummyPath); | 411 FilePath target_path(kDummyPath); |
403 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 412 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
404 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); | 413 FilePath new_intermediate_path(target_path.InsertBeforeExtensionASCII("y")); |
405 EXPECT_CALL(*mock_file_manager(), | 414 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) |
406 RenameDownloadFile(_,intermediate_path,false,_)) | |
407 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 415 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
408 | 416 |
409 // Currently, a notification would be generated if the danger type is anything | 417 // Currently, a notification would be generated if the danger type is anything |
410 // other than NOT_DANGEROUS. | 418 // other than NOT_DANGEROUS. |
411 item->OnDownloadTargetDetermined(target_path, | 419 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
412 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 420 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
413 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
414 intermediate_path); | |
415 EXPECT_FALSE(observer.CheckUpdated()); | 421 EXPECT_FALSE(observer.CheckUpdated()); |
416 RunAllPendingInMessageLoops(); | 422 RunAllPendingInMessageLoops(); |
417 EXPECT_TRUE(observer.CheckUpdated()); | 423 EXPECT_TRUE(observer.CheckUpdated()); |
418 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); | 424 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); |
| 425 |
| 426 CleanupItem(item, download_file); |
419 } | 427 } |
420 | 428 |
421 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { | 429 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { |
422 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 430 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
423 MockObserver observer(item); | 431 MockObserver observer(item); |
424 | 432 |
425 item->TogglePause(); | 433 item->TogglePause(); |
426 ASSERT_TRUE(observer.CheckUpdated()); | 434 ASSERT_TRUE(observer.CheckUpdated()); |
427 | 435 |
428 item->TogglePause(); | 436 item->TogglePause(); |
429 ASSERT_TRUE(observer.CheckUpdated()); | 437 ASSERT_TRUE(observer.CheckUpdated()); |
430 } | 438 } |
431 | 439 |
432 TEST_F(DownloadItemTest, DisplayName) { | 440 TEST_F(DownloadItemTest, DisplayName) { |
433 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 441 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 442 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 443 MockDownloadFile* download_file = |
| 444 AddDownloadFileToDownloadItem(item, &callback); |
434 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 445 FilePath target_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
435 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); | 446 FilePath intermediate_path(target_path.InsertBeforeExtensionASCII("x")); |
436 EXPECT_EQ(FILE_PATH_LITERAL(""), | 447 EXPECT_EQ(FILE_PATH_LITERAL(""), |
437 item->GetFileNameToReportUser().value()); | 448 item->GetFileNameToReportUser().value()); |
438 EXPECT_CALL(*mock_file_manager(), | 449 EXPECT_CALL(*download_file, Rename(_, false, _)) |
439 RenameDownloadFile(_,_,false,_)) | |
440 .WillOnce(ScheduleRenameCallback(intermediate_path)); | 450 .WillOnce(ScheduleRenameCallback(intermediate_path)); |
441 item->OnDownloadTargetDetermined(target_path, | 451 callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
442 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 452 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
443 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
444 intermediate_path); | |
445 RunAllPendingInMessageLoops(); | 453 RunAllPendingInMessageLoops(); |
446 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), | 454 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), |
447 item->GetFileNameToReportUser().value()); | 455 item->GetFileNameToReportUser().value()); |
448 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); | 456 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); |
449 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), | 457 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), |
450 item->GetFileNameToReportUser().value()); | 458 item->GetFileNameToReportUser().value()); |
| 459 CleanupItem(item, download_file); |
| 460 } |
| 461 |
| 462 // Test to make sure that Start method calls DF initialize properly. |
| 463 TEST_F(DownloadItemTest, Start) { |
| 464 MockDownloadFile* mock_download_file(new MockDownloadFile); |
| 465 scoped_ptr<content::DownloadFile> download_file(mock_download_file); |
| 466 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 467 EXPECT_CALL(*mock_download_file, Initialize(_)); |
| 468 item->Start(download_file.Pass()); |
| 469 |
| 470 CleanupItem(item, mock_download_file); |
451 } | 471 } |
452 | 472 |
453 // Test that the delegate is invoked after the download file is renamed. | 473 // Test that the delegate is invoked after the download file is renamed. |
454 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the | 474 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the |
455 // download is renamed to the intermediate name. | 475 // download is renamed to the intermediate name. |
456 // Delegate::DownloadRenamedToFinalName() should be invoked after the final | 476 // Delegate::DownloadRenamedToFinalName() should be invoked after the final |
457 // rename. | 477 // rename. |
458 TEST_F(DownloadItemTest, CallbackAfterRename) { | 478 TEST_F(DownloadItemTest, CallbackAfterRename) { |
459 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 479 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 480 DownloadItemImplDelegate::DownloadTargetCallback callback; |
| 481 MockDownloadFile* download_file = |
| 482 AddDownloadFileToDownloadItem(item, &callback); |
460 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); | 483 FilePath final_path(FilePath(kDummyPath).AppendASCII("foo.bar")); |
461 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); | 484 FilePath intermediate_path(final_path.InsertBeforeExtensionASCII("x")); |
462 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); | 485 FilePath new_intermediate_path(final_path.InsertBeforeExtensionASCII("y")); |
463 EXPECT_CALL(*mock_file_manager(), | 486 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _)) |
464 RenameDownloadFile(item->GetGlobalId(), | |
465 intermediate_path, false, _)) | |
466 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); | 487 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); |
| 488 |
467 // DownloadItemImpl should invoke this callback on the delegate once the | 489 // DownloadItemImpl should invoke this callback on the delegate once the |
468 // download is renamed to the intermediate name. Also check that GetFullPath() | 490 // download is renamed to the intermediate name. Also check that GetFullPath() |
469 // returns the intermediate path at the time of the call. | 491 // returns the intermediate path at the time of the call. |
470 EXPECT_CALL(*mock_delegate(), | 492 EXPECT_CALL(*mock_delegate(), |
471 DownloadRenamedToIntermediateName( | 493 DownloadRenamedToIntermediateName( |
472 AllOf(item, | 494 AllOf(item, |
473 Property(&DownloadItem::GetFullPath, | 495 Property(&DownloadItem::GetFullPath, |
474 new_intermediate_path)))); | 496 new_intermediate_path)))); |
475 item->OnDownloadTargetDetermined(final_path, | 497 callback.Run(final_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
476 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | 498 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, intermediate_path); |
477 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
478 intermediate_path); | |
479 RunAllPendingInMessageLoops(); | 499 RunAllPendingInMessageLoops(); |
480 // All the callbacks should have happened by now. | 500 // All the callbacks should have happened by now. |
481 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); | 501 ::testing::Mock::VerifyAndClearExpectations(download_file); |
482 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 502 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
483 | 503 |
484 item->OnAllDataSaved(10, ""); | 504 item->OnAllDataSaved(""); |
485 EXPECT_CALL(*mock_delegate(), ReadyForDownloadCompletion(item, _)) | 505 EXPECT_CALL(*download_file, Rename(final_path, true, _)) |
486 .WillOnce(RunArg1Callback()); | |
487 EXPECT_CALL(*mock_delegate(), UpdatePersistence(item)); | |
488 EXPECT_CALL(*mock_file_manager(), | |
489 RenameDownloadFile(item->GetGlobalId(), | |
490 final_path, true, _)) | |
491 .WillOnce(ScheduleRenameCallback(final_path)); | 506 .WillOnce(ScheduleRenameCallback(final_path)); |
492 EXPECT_CALL(*mock_file_manager(), | |
493 CompleteDownload(item->GetGlobalId(), _)) | |
494 .WillOnce(ScheduleCompleteCallback()); | |
495 // DownloadItemImpl should invoke this callback on the delegate after the | 507 // DownloadItemImpl should invoke this callback on the delegate after the |
496 // final rename has completed. Also check that GetFullPath() and | 508 // final rename has completed. Also check that GetFullPath() and |
497 // GetTargetFilePath() return the final path at the time of the call. | 509 // GetTargetFilePath() return the final path at the time of the call. |
498 EXPECT_CALL(*mock_delegate(), | 510 EXPECT_CALL(*mock_delegate(), |
499 DownloadRenamedToFinalName( | 511 DownloadRenamedToFinalName( |
500 AllOf(item, | 512 AllOf(item, |
501 Property(&DownloadItem::GetFullPath, final_path), | 513 Property(&DownloadItem::GetFullPath, final_path), |
502 Property(&DownloadItem::GetTargetFilePath, | 514 Property(&DownloadItem::GetTargetFilePath, |
503 final_path)))); | 515 final_path)))); |
504 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); | 516 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); |
505 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) | 517 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) |
506 .WillOnce(Return(true)); | 518 .WillOnce(Return(true)); |
| 519 EXPECT_CALL(*download_file, Detach(_)) |
| 520 .WillOnce(ScheduleClosure()); |
507 item->SetIsPersisted(); | 521 item->SetIsPersisted(); |
508 item->MaybeCompleteDownload(); | 522 item->MaybeCompleteDownload(); |
509 RunAllPendingInMessageLoops(); | 523 RunAllPendingInMessageLoops(); |
510 ::testing::Mock::VerifyAndClearExpectations(mock_file_manager()); | 524 ::testing::Mock::VerifyAndClearExpectations(download_file); |
511 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); | 525 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
512 } | 526 } |
513 | 527 |
514 TEST_F(DownloadItemTest, Interrupted) { | 528 TEST_F(DownloadItemTest, Interrupted) { |
515 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 529 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 530 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
516 | 531 |
517 const content::DownloadInterruptReason reason( | 532 const content::DownloadInterruptReason reason( |
518 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 533 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
519 | 534 |
520 // Confirm interrupt sets state properly. | 535 // Confirm interrupt sets state properly. |
| 536 EXPECT_CALL(*download_file, Cancel()); |
521 item->Interrupt(reason); | 537 item->Interrupt(reason); |
| 538 RunAllPendingInMessageLoops(); |
522 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 539 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
523 EXPECT_EQ(reason, item->GetLastReason()); | 540 EXPECT_EQ(reason, item->GetLastReason()); |
524 | 541 |
525 // Cancel should result in no change. | 542 // Cancel should result in no change. |
526 item->Cancel(true); | 543 item->Cancel(true); |
527 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); | 544 EXPECT_EQ(DownloadItem::INTERRUPTED, item->GetState()); |
528 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, | 545 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED, |
529 item->GetLastReason()); | 546 item->GetLastReason()); |
530 } | 547 } |
531 | 548 |
532 TEST_F(DownloadItemTest, Canceled) { | 549 TEST_F(DownloadItemTest, Canceled) { |
533 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 550 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 551 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
534 | 552 |
535 // Confirm cancel sets state properly. | 553 // Confirm cancel sets state properly. |
536 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); | 554 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); |
| 555 EXPECT_CALL(*download_file, Cancel()); |
537 item->Cancel(true); | 556 item->Cancel(true); |
538 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); | 557 EXPECT_EQ(DownloadItem::CANCELLED, item->GetState()); |
539 } | 558 } |
540 | 559 |
541 TEST_F(DownloadItemTest, FileRemoved) { | 560 TEST_F(DownloadItemTest, FileRemoved) { |
542 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); | 561 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
543 | 562 |
544 EXPECT_FALSE(item->GetFileExternallyRemoved()); | 563 EXPECT_FALSE(item->GetFileExternallyRemoved()); |
545 item->OnDownloadedFileRemoved(); | 564 item->OnDownloadedFileRemoved(); |
546 EXPECT_TRUE(item->GetFileExternallyRemoved()); | 565 EXPECT_TRUE(item->GetFileExternallyRemoved()); |
547 } | 566 } |
548 | 567 |
| 568 TEST_F(DownloadItemTest, DestinationUpdate) { |
| 569 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 570 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 571 item->DestinationObserverAsWeakPtr()); |
| 572 MockObserver observer(item); |
| 573 |
| 574 EXPECT_EQ(0l, item->CurrentSpeed()); |
| 575 EXPECT_EQ("", item->GetHashState()); |
| 576 EXPECT_EQ(0l, item->GetReceivedBytes()); |
| 577 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 578 EXPECT_FALSE(observer.CheckUpdated()); |
| 579 item->SetTotalBytes(100l); |
| 580 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 581 |
| 582 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
| 583 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 584 EXPECT_EQ("deadbeef", item->GetHashState()); |
| 585 EXPECT_EQ(10l, item->GetReceivedBytes()); |
| 586 EXPECT_EQ(100l, item->GetTotalBytes()); |
| 587 EXPECT_TRUE(observer.CheckUpdated()); |
| 588 |
| 589 as_observer->DestinationUpdate(200, 20, "livebeef"); |
| 590 EXPECT_EQ(20l, item->CurrentSpeed()); |
| 591 EXPECT_EQ("livebeef", item->GetHashState()); |
| 592 EXPECT_EQ(200l, item->GetReceivedBytes()); |
| 593 EXPECT_EQ(0l, item->GetTotalBytes()); |
| 594 EXPECT_TRUE(observer.CheckUpdated()); |
| 595 } |
| 596 |
| 597 TEST_F(DownloadItemTest, DestinationError) { |
| 598 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 599 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item, NULL); |
| 600 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 601 item->DestinationObserverAsWeakPtr()); |
| 602 MockObserver observer(item); |
| 603 |
| 604 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 605 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason()); |
| 606 EXPECT_FALSE(observer.CheckUpdated()); |
| 607 |
| 608 EXPECT_CALL(*mock_delegate(), DownloadStopped(item)); |
| 609 EXPECT_CALL(*download_file, Cancel()); |
| 610 as_observer->DestinationError( |
| 611 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 612 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 613 EXPECT_TRUE(observer.CheckUpdated()); |
| 614 EXPECT_EQ(content::DownloadItem::INTERRUPTED, item->GetState()); |
| 615 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED, |
| 616 item->GetLastReason()); |
| 617 } |
| 618 |
| 619 TEST_F(DownloadItemTest, DestinationCompleted) { |
| 620 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); |
| 621 base::WeakPtr<content::DownloadDestinationObserver> as_observer( |
| 622 item->DestinationObserverAsWeakPtr()); |
| 623 MockObserver observer(item); |
| 624 |
| 625 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 626 EXPECT_EQ("", item->GetHash()); |
| 627 EXPECT_EQ("", item->GetHashState()); |
| 628 EXPECT_FALSE(item->AllDataSaved()); |
| 629 EXPECT_FALSE(observer.CheckUpdated()); |
| 630 |
| 631 as_observer->DestinationUpdate(10, 20, "deadbeef"); |
| 632 EXPECT_TRUE(observer.CheckUpdated()); |
| 633 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset. |
| 634 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 635 EXPECT_EQ("", item->GetHash()); |
| 636 EXPECT_EQ("deadbeef", item->GetHashState()); |
| 637 EXPECT_FALSE(item->AllDataSaved()); |
| 638 |
| 639 as_observer->DestinationCompleted("livebeef"); |
| 640 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); |
| 641 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState()); |
| 642 EXPECT_TRUE(observer.CheckUpdated()); |
| 643 EXPECT_EQ("livebeef", item->GetHash()); |
| 644 EXPECT_EQ("", item->GetHashState()); |
| 645 EXPECT_TRUE(item->AllDataSaved()); |
| 646 } |
| 647 |
549 TEST(MockDownloadItem, Compiles) { | 648 TEST(MockDownloadItem, Compiles) { |
550 MockDownloadItem mock_item; | 649 MockDownloadItem mock_item; |
551 } | 650 } |
OLD | NEW |