Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: content/browser/download/download_item_impl_unittest.cc

Issue 10799005: Replace the DownloadFileManager with direct ownership (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated comments. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:
40 MOCK_METHOD1(DelegateStart, void(DownloadItemImpl* download));
41 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download));
37 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path)); 42 MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path));
38 MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItemImpl* download));
39 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download)); 43 MOCK_METHOD1(CheckForFileRemoval, void(DownloadItemImpl* download));
40 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download)); 44 MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItemImpl* download));
41 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); 45 MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
42 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download)); 46 MOCK_METHOD1(DownloadStopped, void(DownloadItemImpl* download));
43 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download)); 47 MOCK_METHOD1(DownloadCompleted, void(DownloadItemImpl* download));
44 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download)); 48 MOCK_METHOD1(DownloadOpened, void(DownloadItemImpl* download));
45 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download)); 49 MOCK_METHOD1(DownloadRemoved, void(DownloadItemImpl* download));
46 MOCK_METHOD1(DownloadRenamedToIntermediateName, 50 MOCK_METHOD1(DownloadRenamedToIntermediateName,
47 void(DownloadItemImpl* download)); 51 void(DownloadItemImpl* download));
48 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download)); 52 MOCK_METHOD1(DownloadRenamedToFinalName, void(DownloadItemImpl* download));
49 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download)); 53 MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItemImpl* download));
50 }; 54 };
51 55
52 class MockRequestHandle : public DownloadRequestHandleInterface { 56 class MockRequestHandle : public DownloadRequestHandleInterface {
53 public: 57 public:
54 MOCK_CONST_METHOD0(GetWebContents, WebContents*()); 58 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
55 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*()); 59 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
56 MOCK_CONST_METHOD0(PauseRequest, void()); 60 MOCK_CONST_METHOD0(PauseRequest, void());
57 MOCK_CONST_METHOD0(ResumeRequest, void()); 61 MOCK_CONST_METHOD0(ResumeRequest, void());
58 MOCK_CONST_METHOD0(CancelRequest, void()); 62 MOCK_CONST_METHOD0(CancelRequest, void());
59 MOCK_CONST_METHOD0(DebugString, std::string()); 63 MOCK_CONST_METHOD0(DebugString, std::string());
60 }; 64 };
61 65
62 class MockDownloadFileFactory
63 : public DownloadFileManager::DownloadFileFactory {
64 public:
65 content::DownloadFile* CreateFile(
66 DownloadCreateInfo* info,
67 scoped_ptr<content::ByteStreamReader> stream_reader,
68 DownloadManager* mgr,
69 bool calculate_hash,
70 const net::BoundNetLog& bound_net_log) {
71 return MockCreateFile(
72 info, stream_reader.get(), info->request_handle, mgr, calculate_hash,
73 bound_net_log);
74 }
75
76 MOCK_METHOD6(MockCreateFile,
77 content::DownloadFile*(DownloadCreateInfo*,
78 content::ByteStreamReader*,
79 const DownloadRequestHandle&,
80 DownloadManager*,
81 bool,
82 const net::BoundNetLog&));
83 };
84
85 class MockDownloadFileManager : public DownloadFileManager {
86 public:
87 MockDownloadFileManager();
88 MOCK_METHOD0(Shutdown, void());
89 MOCK_METHOD1(CancelDownload, void(DownloadId));
90 MOCK_METHOD2(CompleteDownload, void(DownloadId, const base::Closure&));
91 MOCK_METHOD1(OnDownloadManagerShutdown, void(DownloadManager*));
92 MOCK_METHOD4(RenameDownloadFile, void(DownloadId, const FilePath&, bool,
93 const RenameCompletionCallback&));
94 MOCK_CONST_METHOD0(NumberOfActiveDownloads, int());
95 private:
96 ~MockDownloadFileManager() {}
97 };
98
99 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on 66 // Schedules a task to invoke the RenameCompletionCallback with |new_path| on
100 // the UI thread. Should only be used as the action for 67 // the UI thread. Should only be used as the action for
101 // MockDownloadFileManager::Rename*DownloadFile as follows: 68 // MockDownloadFile::Rename as follows:
102 // EXPECT_CALL(mock_download_file_manager, 69 // EXPECT_CALL(download_file, Rename(_,_,_))
103 // RenameDownloadFile(_,_,_,_))
104 // .WillOnce(ScheduleRenameCallback(new_path)); 70 // .WillOnce(ScheduleRenameCallback(new_path));
105 ACTION_P(ScheduleRenameCallback, new_path) { 71 ACTION_P(ScheduleRenameCallback, new_path) {
106 BrowserThread::PostTask( 72 BrowserThread::PostTask(
107 BrowserThread::UI, FROM_HERE, 73 BrowserThread::UI, FROM_HERE,
108 base::Bind(arg3, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path)); 74 base::Bind(arg2, content::DOWNLOAD_INTERRUPT_REASON_NONE, new_path));
109 } 75 }
110 76
111 // Similarly for scheduling a completion callback. 77 // Similarly for scheduling a completion callback.
112 ACTION(ScheduleCompleteCallback) { 78 ACTION(ScheduleCompleteCallback) {
113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1)); 79 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(arg1));
114 } 80 }
115 81
116 MockDownloadFileManager::MockDownloadFileManager()
117 : DownloadFileManager(new MockDownloadFileFactory) {
118 }
119
120 } // namespace 82 } // namespace
121 83
122 class DownloadItemTest : public testing::Test { 84 class DownloadItemTest : public testing::Test {
123 public: 85 public:
124 class MockObserver : public DownloadItem::Observer { 86 class MockObserver : public DownloadItem::Observer {
125 public: 87 public:
126 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) { 88 explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) {
127 item_->AddObserver(this); 89 item_->AddObserver(this);
128 } 90 }
129 ~MockObserver() { item_->RemoveObserver(this); } 91 ~MockObserver() { item_->RemoveObserver(this); }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 143
182 scoped_ptr<DownloadRequestHandleInterface> request_handle( 144 scoped_ptr<DownloadRequestHandleInterface> request_handle(
183 new testing::NiceMock<MockRequestHandle>); 145 new testing::NiceMock<MockRequestHandle>);
184 DownloadItemImpl* download = 146 DownloadItemImpl* download =
185 new DownloadItemImpl(&delegate_, *(info_.get()), 147 new DownloadItemImpl(&delegate_, *(info_.get()),
186 request_handle.Pass(), false, net::BoundNetLog()); 148 request_handle.Pass(), false, net::BoundNetLog());
187 allocated_downloads_.insert(download); 149 allocated_downloads_.insert(download);
188 return download; 150 return download;
189 } 151 }
190 152
153 // Add DownloadFile to DownloadItem
154 MockDownloadFile* AddDownloadFileToDownloadItem(DownloadItemImpl* item) {
155 MockDownloadFile* mock_download_file(new StrictMock<MockDownloadFile>);
156 scoped_ptr<content::DownloadFile> download_file(mock_download_file);
157 EXPECT_CALL(*mock_download_file, Initialize(_));
158 item->Start(download_file.Pass());
159 loop_.RunAllPending();
160 return mock_download_file;
161 }
162
163 // Cleanup a download item (specifically get rid of the DownloadFile on it).
164 void CleanupItem(DownloadItemImpl* item, MockDownloadFile* download_file) {
165 EXPECT_CALL(*download_file, Cancel());
166 item->OffThreadCancel();
167 }
168
191 // Destroy a previously created download item. 169 // Destroy a previously created download item.
192 void DestroyDownloadItem(DownloadItem* item) { 170 void DestroyDownloadItem(DownloadItem* item) {
193 allocated_downloads_.erase(item); 171 allocated_downloads_.erase(item);
194 delete item; 172 delete item;
195 } 173 }
196 174
197 void RunAllPendingInMessageLoops() { 175 void RunAllPendingInMessageLoops() {
198 loop_.RunAllPending(); 176 loop_.RunAllPending();
199 } 177 }
200 178
(...skipping 17 matching lines...) Expand all
218 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath"); 196 const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath");
219 197
220 } // namespace 198 } // namespace
221 199
222 // Tests to ensure calls that change a DownloadItem generate an update to 200 // Tests to ensure calls that change a DownloadItem generate an update to
223 // observers. 201 // observers.
224 // State changing functions not tested: 202 // State changing functions not tested:
225 // void OpenDownload(); 203 // void OpenDownload();
226 // void ShowDownloadInShell(); 204 // void ShowDownloadInShell();
227 // void CompleteDelayedDownload(); 205 // void CompleteDelayedDownload();
228 // void OnDownloadCompleting(DownloadFileManager* file_manager); 206 // void OnDownloadCompleting();
229 // set_* mutators 207 // set_* mutators
230 208
231 TEST_F(DownloadItemTest, NotificationAfterUpdate) { 209 TEST_F(DownloadItemTest, NotificationAfterUpdate) {
232 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 210 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
233 MockObserver observer(item); 211 MockObserver observer(item);
234 212
235 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, ""); 213 item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, "");
236 ASSERT_TRUE(observer.CheckUpdated()); 214 ASSERT_TRUE(observer.CheckUpdated());
237 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed()); 215 EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed());
238 } 216 }
(...skipping 10 matching lines...) Expand all
249 MockObserver observer2(system_cancel); 227 MockObserver observer2(system_cancel);
250 228
251 system_cancel->Cancel(false); 229 system_cancel->Cancel(false);
252 ASSERT_TRUE(observer2.CheckUpdated()); 230 ASSERT_TRUE(observer2.CheckUpdated());
253 } 231 }
254 232
255 TEST_F(DownloadItemTest, NotificationAfterComplete) { 233 TEST_F(DownloadItemTest, NotificationAfterComplete) {
256 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 234 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
257 MockObserver observer(item); 235 MockObserver observer(item);
258 236
259 item->OnAllDataSaved(kDownloadChunkSize, DownloadItem::kEmptyFileHash); 237 item->OnAllDataSaved(DownloadItem::kEmptyFileHash);
260 ASSERT_TRUE(observer.CheckUpdated()); 238 ASSERT_TRUE(observer.CheckUpdated());
261 239
262 item->MarkAsComplete(); 240 item->MarkAsComplete();
263 ASSERT_TRUE(observer.CheckUpdated()); 241 ASSERT_TRUE(observer.CheckUpdated());
264 } 242 }
265 243
266 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) { 244 TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) {
267 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 245 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
268 MockObserver observer(item); 246 MockObserver observer(item);
269 247
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 309
332 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) { 310 TEST_F(DownloadItemTest, NotificationAfterOnContentCheckCompleted) {
333 // Setting to NOT_DANGEROUS does not trigger a notification. 311 // Setting to NOT_DANGEROUS does not trigger a notification.
334 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 312 DownloadItemImpl* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
335 MockObserver safe_observer(safe_item); 313 MockObserver safe_observer(safe_item);
336 314
337 safe_item->OnTargetPathDetermined( 315 safe_item->OnTargetPathDetermined(
338 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, 316 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
339 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 317 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
340 EXPECT_FALSE(safe_observer.CheckUpdated()); 318 EXPECT_FALSE(safe_observer.CheckUpdated());
341 safe_item->OnAllDataSaved(1, ""); 319 safe_item->OnAllDataSaved("");
342 EXPECT_TRUE(safe_observer.CheckUpdated()); 320 EXPECT_TRUE(safe_observer.CheckUpdated());
343 safe_item->OnContentCheckCompleted( 321 safe_item->OnContentCheckCompleted(
344 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 322 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
345 EXPECT_FALSE(safe_observer.CheckUpdated()); 323 EXPECT_FALSE(safe_observer.CheckUpdated());
346 324
347 // Setting to unsafe url or unsafe file should trigger a notification. 325 // Setting to unsafe url or unsafe file should trigger a notification.
348 DownloadItemImpl* unsafeurl_item = 326 DownloadItemImpl* unsafeurl_item =
349 CreateDownloadItem(DownloadItem::IN_PROGRESS); 327 CreateDownloadItem(DownloadItem::IN_PROGRESS);
350 MockObserver unsafeurl_observer(unsafeurl_item); 328 MockObserver unsafeurl_observer(unsafeurl_item);
351 329
352 unsafeurl_item->OnTargetPathDetermined( 330 unsafeurl_item->OnTargetPathDetermined(
353 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, 331 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
354 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 332 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
355 EXPECT_FALSE(unsafeurl_observer.CheckUpdated()); 333 EXPECT_FALSE(unsafeurl_observer.CheckUpdated());
356 unsafeurl_item->OnAllDataSaved(1, ""); 334 unsafeurl_item->OnAllDataSaved("");
357 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 335 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
358 unsafeurl_item->OnContentCheckCompleted( 336 unsafeurl_item->OnContentCheckCompleted(
359 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL); 337 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL);
360 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 338 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
361 339
362 unsafeurl_item->DangerousDownloadValidated(); 340 unsafeurl_item->DangerousDownloadValidated();
363 EXPECT_TRUE(unsafeurl_observer.CheckUpdated()); 341 EXPECT_TRUE(unsafeurl_observer.CheckUpdated());
364 342
365 DownloadItemImpl* unsafefile_item = 343 DownloadItemImpl* unsafefile_item =
366 CreateDownloadItem(DownloadItem::IN_PROGRESS); 344 CreateDownloadItem(DownloadItem::IN_PROGRESS);
367 MockObserver unsafefile_observer(unsafefile_item); 345 MockObserver unsafefile_observer(unsafefile_item);
368 346
369 unsafefile_item->OnTargetPathDetermined( 347 unsafefile_item->OnTargetPathDetermined(
370 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE, 348 FilePath(kDummyPath), DownloadItem::TARGET_DISPOSITION_OVERWRITE,
371 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 349 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
372 EXPECT_FALSE(unsafefile_observer.CheckUpdated()); 350 EXPECT_FALSE(unsafefile_observer.CheckUpdated());
373 unsafefile_item->OnAllDataSaved(1, ""); 351 unsafefile_item->OnAllDataSaved("");
374 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 352 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
375 unsafefile_item->OnContentCheckCompleted( 353 unsafefile_item->OnContentCheckCompleted(
376 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); 354 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
377 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 355 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
378 356
379 unsafefile_item->DangerousDownloadValidated(); 357 unsafefile_item->DangerousDownloadValidated();
380 EXPECT_TRUE(unsafefile_observer.CheckUpdated()); 358 EXPECT_TRUE(unsafefile_observer.CheckUpdated());
381 } 359 }
382 360
383 // DownloadItemImpl::OnIntermediatePathDetermined will schedule a task to run 361 // DownloadItemImpl::OnIntermediatePathDetermined will schedule a task to run
384 // DownloadFileManager::RenameDownloadFile(). Once the rename 362 // DownloadFile::Rename(). Once the rename
385 // completes, DownloadItemImpl receives a notification with the new file 363 // completes, DownloadItemImpl receives a notification with the new file
386 // name. Check that observers are updated when the new filename is available and 364 // name. Check that observers are updated when the new filename is available and
387 // not before. 365 // not before.
388 TEST_F(DownloadItemTest, NotificationAfterOnIntermediatePathDetermined) { 366 TEST_F(DownloadItemTest, NotificationAfterOnIntermediatePathDetermined) {
389 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 367 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
368 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item);
390 MockObserver observer(item); 369 MockObserver observer(item);
391 FilePath intermediate_path(kDummyPath); 370 FilePath intermediate_path(kDummyPath);
392 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo")); 371 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo"));
393 scoped_refptr<MockDownloadFileManager> file_manager( 372 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _))
394 new MockDownloadFileManager);
395 EXPECT_CALL(*file_manager.get(),
396 RenameDownloadFile(_,intermediate_path,false,_))
397 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); 373 .WillOnce(ScheduleRenameCallback(new_intermediate_path));
398 374
399 item->OnIntermediatePathDetermined(file_manager.get(), intermediate_path); 375 item->OnIntermediatePathDetermined(intermediate_path);
400 EXPECT_FALSE(observer.CheckUpdated()); 376 EXPECT_FALSE(observer.CheckUpdated());
401 RunAllPendingInMessageLoops(); 377 RunAllPendingInMessageLoops();
402 EXPECT_TRUE(observer.CheckUpdated()); 378 EXPECT_TRUE(observer.CheckUpdated());
403 EXPECT_EQ(new_intermediate_path, item->GetFullPath()); 379 EXPECT_EQ(new_intermediate_path, item->GetFullPath());
380
381 CleanupItem(item, download_file);
382 RunAllPendingInMessageLoops();
404 } 383 }
405 384
406 TEST_F(DownloadItemTest, NotificationAfterTogglePause) { 385 TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
407 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 386 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
408 MockObserver observer(item); 387 MockObserver observer(item);
409 388
410 item->TogglePause(); 389 item->TogglePause();
411 ASSERT_TRUE(observer.CheckUpdated()); 390 ASSERT_TRUE(observer.CheckUpdated());
412 391
413 item->TogglePause(); 392 item->TogglePause();
414 ASSERT_TRUE(observer.CheckUpdated()); 393 ASSERT_TRUE(observer.CheckUpdated());
415 } 394 }
416 395
417 TEST_F(DownloadItemTest, DisplayName) { 396 TEST_F(DownloadItemTest, DisplayName) {
418 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 397 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
419 item->OnTargetPathDetermined(FilePath(kDummyPath).AppendASCII("foo.bar"), 398 item->OnTargetPathDetermined(FilePath(kDummyPath).AppendASCII("foo.bar"),
420 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 399 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
421 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 400 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
422 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"), 401 EXPECT_EQ(FILE_PATH_LITERAL("foo.bar"),
423 item->GetFileNameToReportUser().value()); 402 item->GetFileNameToReportUser().value());
424 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name"))); 403 item->SetDisplayName(FilePath(FILE_PATH_LITERAL("new.name")));
425 EXPECT_EQ(FILE_PATH_LITERAL("new.name"), 404 EXPECT_EQ(FILE_PATH_LITERAL("new.name"),
426 item->GetFileNameToReportUser().value()); 405 item->GetFileNameToReportUser().value());
427 } 406 }
428 407
408 // Test to make sure that Start method calls DF initialize properly.
409 TEST_F(DownloadItemTest, Start) {
410 MockDownloadFile* mock_download_file(new MockDownloadFile);
411 scoped_ptr<content::DownloadFile> download_file(mock_download_file);
412 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
413 EXPECT_CALL(*mock_download_file, Initialize(_));
414 item->Start(download_file.Pass());
415
416 CleanupItem(item, mock_download_file);
417 }
418
429 static char external_data_test_string[] = "External data test"; 419 static char external_data_test_string[] = "External data test";
430 static int destructor_called = 0; 420 static int destructor_called = 0;
431 421
432 class TestExternalData : public DownloadItem::ExternalData { 422 class TestExternalData : public DownloadItem::ExternalData {
433 public: 423 public:
434 int value; 424 int value;
435 virtual ~TestExternalData() { 425 virtual ~TestExternalData() {
436 destructor_called++; 426 destructor_called++;
437 } 427 }
438 }; 428 };
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 EXPECT_EQ(3, destructor_called); 483 EXPECT_EQ(3, destructor_called);
494 } 484 }
495 485
496 // Test that the delegate is invoked after the download file is renamed. 486 // Test that the delegate is invoked after the download file is renamed.
497 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the 487 // Delegate::DownloadRenamedToIntermediateName() should be invoked when the
498 // download is renamed to the intermediate name. 488 // download is renamed to the intermediate name.
499 // Delegate::DownloadRenamedToFinalName() should be invoked after the final 489 // Delegate::DownloadRenamedToFinalName() should be invoked after the final
500 // rename. 490 // rename.
501 TEST_F(DownloadItemTest, CallbackAfterRename) { 491 TEST_F(DownloadItemTest, CallbackAfterRename) {
502 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 492 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
493 MockDownloadFile* download_file = AddDownloadFileToDownloadItem(item);
503 FilePath intermediate_path(kDummyPath); 494 FilePath intermediate_path(kDummyPath);
504 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo")); 495 FilePath new_intermediate_path(intermediate_path.AppendASCII("foo"));
505 FilePath final_path(intermediate_path.AppendASCII("bar")); 496 FilePath final_path(intermediate_path.AppendASCII("bar"));
506 scoped_refptr<MockDownloadFileManager> file_manager( 497 EXPECT_CALL(*download_file, Rename(intermediate_path, false, _))
507 new MockDownloadFileManager);
508 EXPECT_CALL(*file_manager.get(),
509 RenameDownloadFile(item->GetGlobalId(),
510 intermediate_path, false, _))
511 .WillOnce(ScheduleRenameCallback(new_intermediate_path)); 498 .WillOnce(ScheduleRenameCallback(new_intermediate_path));
499
512 // DownloadItemImpl should invoke this callback on the delegate once the 500 // DownloadItemImpl should invoke this callback on the delegate once the
513 // download is renamed to the intermediate name. Also check that GetFullPath() 501 // download is renamed to the intermediate name. Also check that GetFullPath()
514 // returns the intermediate path at the time of the call. 502 // returns the intermediate path at the time of the call.
515 EXPECT_CALL(*mock_delegate(), 503 EXPECT_CALL(*mock_delegate(),
516 DownloadRenamedToIntermediateName( 504 DownloadRenamedToIntermediateName(
517 AllOf(item, 505 AllOf(item,
518 Property(&DownloadItem::GetFullPath, 506 Property(&DownloadItem::GetFullPath,
519 new_intermediate_path)))); 507 new_intermediate_path))));
520 item->OnTargetPathDetermined(final_path, 508 item->OnTargetPathDetermined(final_path,
521 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 509 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
522 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); 510 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
523 item->OnIntermediatePathDetermined(file_manager.get(), intermediate_path); 511 item->OnIntermediatePathDetermined(intermediate_path);
524 RunAllPendingInMessageLoops(); 512 RunAllPendingInMessageLoops();
525 // All the callbacks should have happened by now. 513 // All the callbacks should have happened by now.
526 ::testing::Mock::VerifyAndClearExpectations(file_manager.get()); 514 ::testing::Mock::VerifyAndClearExpectations(download_file);
527 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); 515 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
528 516
529 item->OnAllDataSaved(10, ""); 517 item->OnAllDataSaved("");
530 EXPECT_CALL(*file_manager.get(), 518 EXPECT_CALL(*download_file, Rename(final_path, true, _))
531 RenameDownloadFile(item->GetGlobalId(),
532 final_path, true, _))
533 .WillOnce(ScheduleRenameCallback(final_path)); 519 .WillOnce(ScheduleRenameCallback(final_path));
534 EXPECT_CALL(*file_manager.get(), 520
535 CompleteDownload(item->GetGlobalId(), _))
536 .WillOnce(ScheduleCompleteCallback());
537 // DownloadItemImpl should invoke this callback on the delegate after the 521 // DownloadItemImpl should invoke this callback on the delegate after the
538 // final rename has completed. Also check that GetFullPath() and 522 // final rename has completed. Also check that GetFullPath() and
539 // GetTargetFilePath() return the final path at the time of the call. 523 // GetTargetFilePath() return the final path at the time of the call.
540 EXPECT_CALL(*mock_delegate(), 524 EXPECT_CALL(*mock_delegate(),
541 DownloadRenamedToFinalName( 525 DownloadRenamedToFinalName(
542 AllOf(item, 526 AllOf(item,
543 Property(&DownloadItem::GetFullPath, final_path), 527 Property(&DownloadItem::GetFullPath, final_path),
544 Property(&DownloadItem::GetTargetFilePath, 528 Property(&DownloadItem::GetTargetFilePath,
545 final_path)))); 529 final_path))));
546 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item)); 530 EXPECT_CALL(*mock_delegate(), DownloadCompleted(item));
547 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item)) 531 EXPECT_CALL(*mock_delegate(), ShouldOpenDownload(item))
548 .WillOnce(Return(true)); 532 .WillOnce(Return(true));
549 item->OnDownloadCompleting(file_manager.get()); 533 EXPECT_CALL(*download_file, Detach());
534 item->OnDownloadCompleting();
550 RunAllPendingInMessageLoops(); 535 RunAllPendingInMessageLoops();
551 ::testing::Mock::VerifyAndClearExpectations(file_manager.get()); 536 ::testing::Mock::VerifyAndClearExpectations(download_file);
552 ::testing::Mock::VerifyAndClearExpectations(mock_delegate()); 537 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
553 } 538 }
554 539
555 TEST_F(DownloadItemTest, Interrupted) { 540 TEST_F(DownloadItemTest, Interrupted) {
556 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 541 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
557 542
558 const content::DownloadInterruptReason reason( 543 const content::DownloadInterruptReason reason(
559 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); 544 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
560 545
561 // Confirm interrupt sets state properly. 546 // Confirm interrupt sets state properly.
(...skipping 18 matching lines...) Expand all
580 } 565 }
581 566
582 TEST_F(DownloadItemTest, FileRemoved) { 567 TEST_F(DownloadItemTest, FileRemoved) {
583 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS); 568 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
584 569
585 EXPECT_FALSE(item->GetFileExternallyRemoved()); 570 EXPECT_FALSE(item->GetFileExternallyRemoved());
586 item->OnDownloadedFileRemoved(); 571 item->OnDownloadedFileRemoved();
587 EXPECT_TRUE(item->GetFileExternallyRemoved()); 572 EXPECT_TRUE(item->GetFileExternallyRemoved());
588 } 573 }
589 574
575 TEST_F(DownloadItemTest, DestinationUpdate) {
576 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
577 base::WeakPtr<content::DownloadDestinationObserver> as_observer(
578 item->DestinationObserverAsWeakPtr());
579 MockObserver observer(item);
580
581 EXPECT_EQ(0l, item->CurrentSpeed());
582 EXPECT_EQ("", item->GetHashState());
583 EXPECT_EQ(0l, item->GetReceivedBytes());
584 EXPECT_EQ(0l, item->GetTotalBytes());
585 EXPECT_FALSE(observer.CheckUpdated());
586 item->SetTotalBytes(100l);
587 EXPECT_EQ(100l, item->GetTotalBytes());
588
589 as_observer->DestinationUpdate(10, 20, "deadbeef");
590 EXPECT_EQ(20l, item->CurrentSpeed());
591 EXPECT_EQ("deadbeef", item->GetHashState());
592 EXPECT_EQ(10l, item->GetReceivedBytes());
593 EXPECT_EQ(100l, item->GetTotalBytes());
594 EXPECT_TRUE(observer.CheckUpdated());
595
596 as_observer->DestinationUpdate(200, 20, "livebeef");
597 EXPECT_EQ(20l, item->CurrentSpeed());
598 EXPECT_EQ("livebeef", item->GetHashState());
599 EXPECT_EQ(200l, item->GetReceivedBytes());
600 EXPECT_EQ(0l, item->GetTotalBytes());
601 EXPECT_TRUE(observer.CheckUpdated());
602 }
603
604 TEST_F(DownloadItemTest, DestinationError) {
605 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
606 base::WeakPtr<content::DownloadDestinationObserver> as_observer(
607 item->DestinationObserverAsWeakPtr());
608 MockObserver observer(item);
609
610 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState());
611 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, item->GetLastReason());
612 EXPECT_FALSE(observer.CheckUpdated());
613
614 EXPECT_CALL(*mock_delegate(), DownloadStopped(item));
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 }
623
624 TEST_F(DownloadItemTest, DestinationCompleted) {
625 DownloadItemImpl* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
626 base::WeakPtr<content::DownloadDestinationObserver> as_observer(
627 item->DestinationObserverAsWeakPtr());
628 MockObserver observer(item);
629
630 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState());
631 EXPECT_EQ("", item->GetHash());
632 EXPECT_EQ("", item->GetHashState());
633 EXPECT_FALSE(item->AllDataSaved());
634 EXPECT_FALSE(observer.CheckUpdated());
635
636 as_observer->DestinationUpdate(10, 20, "deadbeef");
637 EXPECT_TRUE(observer.CheckUpdated());
638 EXPECT_FALSE(observer.CheckUpdated()); // Confirm reset.
639 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState());
640 EXPECT_EQ("", item->GetHash());
641 EXPECT_EQ("deadbeef", item->GetHashState());
642 EXPECT_FALSE(item->AllDataSaved());
643
644 EXPECT_CALL(*mock_delegate(), MaybeCompleteDownload(item));
645 as_observer->DestinationCompleted("livebeef");
646 ::testing::Mock::VerifyAndClearExpectations(mock_delegate());
647 EXPECT_EQ(content::DownloadItem::IN_PROGRESS, item->GetState());
648 EXPECT_TRUE(observer.CheckUpdated());
649 EXPECT_EQ("livebeef", item->GetHash());
650 EXPECT_EQ("", item->GetHashState());
651 EXPECT_TRUE(item->AllDataSaved());
652 }
653
590 TEST(MockDownloadItem, Compiles) { 654 TEST(MockDownloadItem, Compiles) {
591 MockDownloadItem mock_item; 655 MockDownloadItem mock_item;
592 } 656 }
657
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698