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

Side by Side Diff: chrome/browser/download/download_manager_unittest.cc

Issue 6990044: Fixed memory leaks in download manager unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 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
« no previous file with comments | « no previous file | tools/heapcheck/suppressions.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <string> 5 #include <string>
6 #include <set> 6 #include <set>
7 7
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 info->download_id = static_cast<int>(i); 306 info->download_id = static_cast<int>(i);
307 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 307 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
308 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 308 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
309 info->mime_type = kStartDownloadCases[i].mime_type; 309 info->mime_type = kStartDownloadCases[i].mime_type;
310 download_manager_->CreateDownloadItem(info); 310 download_manager_->CreateDownloadItem(info);
311 311
312 DownloadFile* download_file(new DownloadFile(info, download_manager_)); 312 DownloadFile* download_file(new DownloadFile(info, download_manager_));
313 AddDownloadToFileManager(info->download_id, download_file); 313 AddDownloadToFileManager(info->download_id, download_file);
314 download_file->Initialize(false); 314 download_file->Initialize(false);
315 download_manager_->StartDownload(info->download_id); 315 download_manager_->StartDownload(info->download_id);
316 delete info;
Randy Smith (Not in Mondays) 2011/05/23 22:30:41 If you're in a position where you don't need to pa
316 message_loop_.RunAllPending(); 317 message_loop_.RunAllPending();
317 318
318 // NOTE: At this point, |ContinueDownloadWithPath| will have been run if 319 // SelectFileObserver will have recorded any attempt to open the
319 // we don't need to prompt the user, so |info| could have been destructed.
320 // This means that we can't check any of its values.
321 // However, SelectFileObserver will have recorded any attempt to open the
322 // select file dialog. 320 // select file dialog.
321 // Note that DownloadManager::FileSelectionCanceled() is never called.
323 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, 322 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
324 observer.ShowedFileDialogForId(i)); 323 observer.ShowedFileDialogForId(i));
325
326 // If the Save As dialog pops up, it never reached
327 // DownloadManager::ContinueDownloadWithPath(), and never deleted info or
328 // completed. This cleans up info.
329 // Note that DownloadManager::FileSelectionCanceled() is never called.
330 if (observer.ShowedFileDialogForId(i)) {
331 delete info;
332 }
333 } 324 }
334 } 325 }
335 326
336 TEST_F(DownloadManagerTest, DownloadRenameTest) { 327 TEST_F(DownloadManagerTest, DownloadRenameTest) {
337 using ::testing::_; 328 using ::testing::_;
338 using ::testing::CreateFunctor; 329 using ::testing::CreateFunctor;
339 using ::testing::Invoke; 330 using ::testing::Invoke;
340 using ::testing::Return; 331 using ::testing::Return;
341 332
342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { 333 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) {
(...skipping 21 matching lines...) Expand all
364 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); 355 FilePath crdownload(download_util::GetCrDownloadPath(new_path));
365 EXPECT_CALL(*download_file, Rename(_)) 356 EXPECT_CALL(*download_file, Rename(_))
366 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 357 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
367 download_file, &MockDownloadFile::TestMultipleRename, 358 download_file, &MockDownloadFile::TestMultipleRename,
368 1, crdownload)))) 359 1, crdownload))))
369 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 360 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
370 download_file, &MockDownloadFile::TestMultipleRename, 361 download_file, &MockDownloadFile::TestMultipleRename,
371 2, new_path)))); 362 2, new_path))));
372 } 363 }
373 download_manager_->CreateDownloadItem(info); 364 download_manager_->CreateDownloadItem(info);
365 delete info;
374 366
375 int32* id_ptr = new int32; 367 int32* id_ptr = new int32;
376 *id_ptr = i; // Deleted in FileSelected(). 368 *id_ptr = i; // Deleted in FileSelected().
377 if (kDownloadRenameCases[i].finish_before_rename) { 369 if (kDownloadRenameCases[i].finish_before_rename) {
378 OnAllDataSaved(i, 1024, std::string("fake_hash")); 370 OnAllDataSaved(i, 1024, std::string("fake_hash"));
379 message_loop_.RunAllPending(); 371 message_loop_.RunAllPending();
380 FileSelected(new_path, i, id_ptr); 372 FileSelected(new_path, i, id_ptr);
381 } else { 373 } else {
382 FileSelected(new_path, i, id_ptr); 374 FileSelected(new_path, i, id_ptr);
383 message_loop_.RunAllPending(); 375 message_loop_.RunAllPending();
(...skipping 27 matching lines...) Expand all
411 new MockDownloadFile(info, download_manager_)); 403 new MockDownloadFile(info, download_manager_));
412 AddDownloadToFileManager(info->download_id, download_file); 404 AddDownloadToFileManager(info->download_id, download_file);
413 405
414 // |download_file| is owned by DownloadFileManager. 406 // |download_file| is owned by DownloadFileManager.
415 ::testing::Mock::AllowLeak(download_file); 407 ::testing::Mock::AllowLeak(download_file);
416 EXPECT_CALL(*download_file, Destructed()).Times(1); 408 EXPECT_CALL(*download_file, Destructed()).Times(1);
417 409
418 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); 410 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true));
419 411
420 download_manager_->CreateDownloadItem(info); 412 download_manager_->CreateDownloadItem(info);
413 delete info;
421 414
422 DownloadItem* download = GetActiveDownloadItem(0); 415 DownloadItem* download = GetActiveDownloadItem(0);
423 ASSERT_TRUE(download != NULL); 416 ASSERT_TRUE(download != NULL);
424 417
425 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 418 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
426 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 419 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
427 420
428 download_file->AppendDataToFile(kTestData, kTestDataLen); 421 download_file->AppendDataToFile(kTestData, kTestDataLen);
429 422
430 ContinueDownloadWithPath(download, new_path); 423 ContinueDownloadWithPath(download, new_path);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 new MockDownloadFile(info, download_manager_)); 469 new MockDownloadFile(info, download_manager_));
477 AddDownloadToFileManager(info->download_id, download_file); 470 AddDownloadToFileManager(info->download_id, download_file);
478 471
479 // |download_file| is owned by DownloadFileManager. 472 // |download_file| is owned by DownloadFileManager.
480 ::testing::Mock::AllowLeak(download_file); 473 ::testing::Mock::AllowLeak(download_file);
481 EXPECT_CALL(*download_file, Destructed()).Times(1); 474 EXPECT_CALL(*download_file, Destructed()).Times(1);
482 475
483 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); 476 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true));
484 477
485 download_manager_->CreateDownloadItem(info); 478 download_manager_->CreateDownloadItem(info);
479 delete info;
486 480
487 DownloadItem* download = GetActiveDownloadItem(0); 481 DownloadItem* download = GetActiveDownloadItem(0);
488 ASSERT_TRUE(download != NULL); 482 ASSERT_TRUE(download != NULL);
489 483
490 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 484 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
491 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 485 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
492 486
493 ContinueDownloadWithPath(download, new_path); 487 ContinueDownloadWithPath(download, new_path);
494 message_loop_.RunAllPending(); 488 message_loop_.RunAllPending();
495 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 489 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 536
543 // |info| will be destroyed in download_manager_. 537 // |info| will be destroyed in download_manager_.
544 DownloadCreateInfo* info(new DownloadCreateInfo); 538 DownloadCreateInfo* info(new DownloadCreateInfo);
545 info->download_id = static_cast<int>(0); 539 info->download_id = static_cast<int>(0);
546 info->prompt_user_for_save_location = true; 540 info->prompt_user_for_save_location = true;
547 info->url_chain.push_back(GURL()); 541 info->url_chain.push_back(GURL());
548 info->is_dangerous_file = false; 542 info->is_dangerous_file = false;
549 info->is_dangerous_url = false; 543 info->is_dangerous_url = false;
550 544
551 download_manager_->CreateDownloadItem(info); 545 download_manager_->CreateDownloadItem(info);
546 delete info;
552 547
553 DownloadItem* download = GetActiveDownloadItem(0); 548 DownloadItem* download = GetActiveDownloadItem(0);
554 ASSERT_TRUE(download != NULL); 549 ASSERT_TRUE(download != NULL);
555 550
556 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 551 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
557 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 552 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
558 553
559 // Create and initialize the download file. We're bypassing the first part 554 // Create and initialize the download file. We're bypassing the first part
560 // of the download process and skipping to the part after the final file 555 // of the download process and skipping to the part after the final file
561 // name has been chosen, so we need to initialize the download file 556 // name has been chosen, so we need to initialize the download file
562 // properly. 557 // properly.
563 DownloadFile* download_file( 558 DownloadFile* download_file(
564 new DownloadFile(info, download_manager_)); 559 new DownloadFile(info, download_manager_));
565 download_file->Rename(cr_path); 560 download_file->Rename(cr_path);
566 // This creates the .crdownload version of the file. 561 // This creates the .crdownload version of the file.
567 download_file->Initialize(false); 562 download_file->Initialize(false);
568 // |download_file| is owned by DownloadFileManager. 563 // |download_file| is owned by DownloadFileManager.
569 AddDownloadToFileManager(info->download_id, download_file); 564 AddDownloadToFileManager(info->download_id, download_file);
565 delete info;
570 566
571 ContinueDownloadWithPath(download, new_path); 567 ContinueDownloadWithPath(download, new_path);
572 message_loop_.RunAllPending(); 568 message_loop_.RunAllPending();
573 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 569 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
574 570
575 download_file->AppendDataToFile(kTestData, kTestDataLen); 571 download_file->AppendDataToFile(kTestData, kTestDataLen);
576 572
577 // Finish the download. 573 // Finish the download.
578 OnAllDataSaved(0, kTestDataLen, ""); 574 OnAllDataSaved(0, kTestDataLen, "");
579 message_loop_.RunAllPending(); 575 message_loop_.RunAllPending();
580 576
581 // Download is complete. 577 // Download is complete.
582 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); 578 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
583 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); 579 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS));
584 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); 580 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED));
585 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); 581 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED));
586 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); 582 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE));
587 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); 583 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING));
588 EXPECT_TRUE(observer->was_updated()); 584 EXPECT_TRUE(observer->was_updated());
589 EXPECT_FALSE(observer->was_opened()); 585 EXPECT_FALSE(observer->was_opened());
590 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); 586 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
591 587
592 EXPECT_TRUE(file_util::PathExists(new_path)); 588 EXPECT_TRUE(file_util::PathExists(new_path));
593 EXPECT_FALSE(file_util::PathExists(cr_path)); 589 EXPECT_FALSE(file_util::PathExists(cr_path));
594 EXPECT_FALSE(file_util::PathExists(unique_new_path)); 590 EXPECT_FALSE(file_util::PathExists(unique_new_path));
595 std::string file_contents; 591 std::string file_contents;
596 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); 592 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents));
597 EXPECT_EQ(std::string(kTestData), file_contents); 593 EXPECT_EQ(std::string(kTestData), file_contents);
598 } 594 }
OLDNEW
« no previous file with comments | « no previous file | tools/heapcheck/suppressions.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698