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