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