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

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: Using scoped_ptr to manage lifetime of DownloadCreateInfo. 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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 PrefService* prefs = profile_->GetPrefs(); 295 PrefService* prefs = profile_->GetPrefs();
296 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath()); 296 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath());
297 download_manager_->download_prefs()->EnableAutoOpenBasedOnExtension( 297 download_manager_->download_prefs()->EnableAutoOpenBasedOnExtension(
298 FilePath(FILE_PATH_LITERAL("example.pdf"))); 298 FilePath(FILE_PATH_LITERAL("example.pdf")));
299 299
300 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) { 300 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) {
301 prefs->SetBoolean(prefs::kPromptForDownload, 301 prefs->SetBoolean(prefs::kPromptForDownload,
302 kStartDownloadCases[i].prompt_for_download); 302 kStartDownloadCases[i].prompt_for_download);
303 303
304 SelectFileObserver observer(download_manager_); 304 SelectFileObserver observer(download_manager_);
305 DownloadCreateInfo* info = new DownloadCreateInfo; 305 // Normally, the download system takes ownership of info, and is
306 // responsible for deleting it. In these unit tests, however, we
307 // don't call the function that deletes it, so we do so ourselves.
308 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
306 info->download_id = static_cast<int>(i); 309 info->download_id = static_cast<int>(i);
307 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; 310 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
308 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); 311 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
309 info->mime_type = kStartDownloadCases[i].mime_type; 312 info->mime_type = kStartDownloadCases[i].mime_type;
310 download_manager_->CreateDownloadItem(info); 313 download_manager_->CreateDownloadItem(info.get());
311 314
312 DownloadFile* download_file(new DownloadFile(info, download_manager_)); 315 DownloadFile* download_file(
316 new DownloadFile(info.get(), download_manager_));
313 AddDownloadToFileManager(info->download_id, download_file); 317 AddDownloadToFileManager(info->download_id, download_file);
314 download_file->Initialize(false); 318 download_file->Initialize(false);
315 download_manager_->StartDownload(info->download_id); 319 download_manager_->StartDownload(info->download_id);
316 message_loop_.RunAllPending(); 320 message_loop_.RunAllPending();
317 321
318 // NOTE: At this point, |ContinueDownloadWithPath| will have been run if 322 // 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. 323 // select file dialog.
324 // Note that DownloadManager::FileSelectionCanceled() is never called.
323 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, 325 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
324 observer.ShowedFileDialogForId(i)); 326 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 } 327 }
334 } 328 }
335 329
336 TEST_F(DownloadManagerTest, DownloadRenameTest) { 330 TEST_F(DownloadManagerTest, DownloadRenameTest) {
337 using ::testing::_; 331 using ::testing::_;
338 using ::testing::CreateFunctor; 332 using ::testing::CreateFunctor;
339 using ::testing::Invoke; 333 using ::testing::Invoke;
340 using ::testing::Return; 334 using ::testing::Return;
341 335
342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { 336 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) {
343 // |info| will be destroyed in download_manager_. 337 // Normally, the download system takes ownership of info, and is
344 DownloadCreateInfo* info(new DownloadCreateInfo); 338 // responsible for deleting it. In these unit tests, however, we
339 // don't call the function that deletes it, so we do so ourselves.
340 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
345 info->download_id = static_cast<int>(i); 341 info->download_id = static_cast<int>(i);
346 info->prompt_user_for_save_location = false; 342 info->prompt_user_for_save_location = false;
347 info->url_chain.push_back(GURL()); 343 info->url_chain.push_back(GURL());
348 info->is_dangerous_file = kDownloadRenameCases[i].is_dangerous_file; 344 info->is_dangerous_file = kDownloadRenameCases[i].is_dangerous_file;
349 info->is_dangerous_url = kDownloadRenameCases[i].is_dangerous_url; 345 info->is_dangerous_url = kDownloadRenameCases[i].is_dangerous_url;
350 const FilePath new_path(kDownloadRenameCases[i].suggested_path); 346 const FilePath new_path(kDownloadRenameCases[i].suggested_path);
351 347
352 MockDownloadFile* download_file( 348 MockDownloadFile* download_file(
353 new MockDownloadFile(info, download_manager_)); 349 new MockDownloadFile(info.get(), download_manager_));
354 AddDownloadToFileManager(info->download_id, download_file); 350 AddDownloadToFileManager(info->download_id, download_file);
355 351
356 // |download_file| is owned by DownloadFileManager. 352 // |download_file| is owned by DownloadFileManager.
357 ::testing::Mock::AllowLeak(download_file); 353 ::testing::Mock::AllowLeak(download_file);
358 EXPECT_CALL(*download_file, Destructed()).Times(1); 354 EXPECT_CALL(*download_file, Destructed()).Times(1);
359 355
360 if (kDownloadRenameCases[i].expected_rename_count == 1) { 356 if (kDownloadRenameCases[i].expected_rename_count == 1) {
361 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true)); 357 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true));
362 } else { 358 } else {
363 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); 359 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count);
364 FilePath crdownload(download_util::GetCrDownloadPath(new_path)); 360 FilePath crdownload(download_util::GetCrDownloadPath(new_path));
365 EXPECT_CALL(*download_file, Rename(_)) 361 EXPECT_CALL(*download_file, Rename(_))
366 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 362 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
367 download_file, &MockDownloadFile::TestMultipleRename, 363 download_file, &MockDownloadFile::TestMultipleRename,
368 1, crdownload)))) 364 1, crdownload))))
369 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor( 365 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
370 download_file, &MockDownloadFile::TestMultipleRename, 366 download_file, &MockDownloadFile::TestMultipleRename,
371 2, new_path)))); 367 2, new_path))));
372 } 368 }
373 download_manager_->CreateDownloadItem(info); 369 download_manager_->CreateDownloadItem(info.get());
374 370
375 int32* id_ptr = new int32; 371 int32* id_ptr = new int32;
376 *id_ptr = i; // Deleted in FileSelected(). 372 *id_ptr = i; // Deleted in FileSelected().
377 if (kDownloadRenameCases[i].finish_before_rename) { 373 if (kDownloadRenameCases[i].finish_before_rename) {
378 OnAllDataSaved(i, 1024, std::string("fake_hash")); 374 OnAllDataSaved(i, 1024, std::string("fake_hash"));
379 message_loop_.RunAllPending(); 375 message_loop_.RunAllPending();
380 FileSelected(new_path, i, id_ptr); 376 FileSelected(new_path, i, id_ptr);
381 } else { 377 } else {
382 FileSelected(new_path, i, id_ptr); 378 FileSelected(new_path, i, id_ptr);
383 message_loop_.RunAllPending(); 379 message_loop_.RunAllPending();
384 OnAllDataSaved(i, 1024, std::string("fake_hash")); 380 OnAllDataSaved(i, 1024, std::string("fake_hash"));
385 } 381 }
386 382
387 message_loop_.RunAllPending(); 383 message_loop_.RunAllPending();
388 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, 384 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file,
389 kDownloadRenameCases[i].is_dangerous_url, 385 kDownloadRenameCases[i].is_dangerous_url,
390 i)); 386 i));
391 } 387 }
392 } 388 }
393 389
394 TEST_F(DownloadManagerTest, DownloadInterruptTest) { 390 TEST_F(DownloadManagerTest, DownloadInterruptTest) {
395 using ::testing::_; 391 using ::testing::_;
396 using ::testing::CreateFunctor; 392 using ::testing::CreateFunctor;
397 using ::testing::Invoke; 393 using ::testing::Invoke;
398 using ::testing::Return; 394 using ::testing::Return;
399 395
400 // |info| will be destroyed in download_manager_. 396 // Normally, the download system takes ownership of info, and is
401 DownloadCreateInfo* info(new DownloadCreateInfo); 397 // responsible for deleting it. In these unit tests, however, we
398 // don't call the function that deletes it, so we do so ourselves.
399 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
402 info->download_id = static_cast<int>(0); 400 info->download_id = static_cast<int>(0);
403 info->prompt_user_for_save_location = false; 401 info->prompt_user_for_save_location = false;
404 info->url_chain.push_back(GURL()); 402 info->url_chain.push_back(GURL());
405 info->is_dangerous_file = false; 403 info->is_dangerous_file = false;
406 info->is_dangerous_url = false; 404 info->is_dangerous_url = false;
407 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 405 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
408 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); 406 const FilePath cr_path(download_util::GetCrDownloadPath(new_path));
409 407
410 MockDownloadFile* download_file( 408 MockDownloadFile* download_file(
411 new MockDownloadFile(info, download_manager_)); 409 new MockDownloadFile(info.get(), download_manager_));
412 AddDownloadToFileManager(info->download_id, download_file); 410 AddDownloadToFileManager(info->download_id, download_file);
413 411
414 // |download_file| is owned by DownloadFileManager. 412 // |download_file| is owned by DownloadFileManager.
415 ::testing::Mock::AllowLeak(download_file); 413 ::testing::Mock::AllowLeak(download_file);
416 EXPECT_CALL(*download_file, Destructed()).Times(1); 414 EXPECT_CALL(*download_file, Destructed()).Times(1);
417 415
418 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); 416 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true));
419 417
420 download_manager_->CreateDownloadItem(info); 418 download_manager_->CreateDownloadItem(info.get());
421 419
422 DownloadItem* download = GetActiveDownloadItem(0); 420 DownloadItem* download = GetActiveDownloadItem(0);
423 ASSERT_TRUE(download != NULL); 421 ASSERT_TRUE(download != NULL);
424 422
425 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 423 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
426 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 424 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
427 425
428 download_file->AppendDataToFile(kTestData, kTestDataLen); 426 download_file->AppendDataToFile(kTestData, kTestDataLen);
429 427
430 ContinueDownloadWithPath(download, new_path); 428 ContinueDownloadWithPath(download, new_path);
(...skipping 24 matching lines...) Expand all
455 EXPECT_TRUE(observer->was_updated()); 453 EXPECT_TRUE(observer->was_updated());
456 EXPECT_FALSE(observer->was_opened()); 454 EXPECT_FALSE(observer->was_opened());
457 } 455 }
458 456
459 TEST_F(DownloadManagerTest, DownloadCancelTest) { 457 TEST_F(DownloadManagerTest, DownloadCancelTest) {
460 using ::testing::_; 458 using ::testing::_;
461 using ::testing::CreateFunctor; 459 using ::testing::CreateFunctor;
462 using ::testing::Invoke; 460 using ::testing::Invoke;
463 using ::testing::Return; 461 using ::testing::Return;
464 462
465 // |info| will be destroyed in download_manager_. 463 // Normally, the download system takes ownership of info, and is
466 DownloadCreateInfo* info(new DownloadCreateInfo); 464 // responsible for deleting it. In these unit tests, however, we
465 // don't call the function that deletes it, so we do so ourselves.
466 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
467 info->download_id = static_cast<int>(0); 467 info->download_id = static_cast<int>(0);
468 info->prompt_user_for_save_location = false; 468 info->prompt_user_for_save_location = false;
469 info->url_chain.push_back(GURL()); 469 info->url_chain.push_back(GURL());
470 info->is_dangerous_file = false; 470 info->is_dangerous_file = false;
471 info->is_dangerous_url = false; 471 info->is_dangerous_url = false;
472 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); 472 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
473 const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); 473 const FilePath cr_path(download_util::GetCrDownloadPath(new_path));
474 474
475 MockDownloadFile* download_file( 475 MockDownloadFile* download_file(
476 new MockDownloadFile(info, download_manager_)); 476 new MockDownloadFile(info.get(), download_manager_));
477 AddDownloadToFileManager(info->download_id, download_file); 477 AddDownloadToFileManager(info->download_id, download_file);
478 478
479 // |download_file| is owned by DownloadFileManager. 479 // |download_file| is owned by DownloadFileManager.
480 ::testing::Mock::AllowLeak(download_file); 480 ::testing::Mock::AllowLeak(download_file);
481 EXPECT_CALL(*download_file, Destructed()).Times(1); 481 EXPECT_CALL(*download_file, Destructed()).Times(1);
482 482
483 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true)); 483 EXPECT_CALL(*download_file, Rename(cr_path)).WillOnce(Return(true));
484 484
485 download_manager_->CreateDownloadItem(info); 485 download_manager_->CreateDownloadItem(info.get());
486 486
487 DownloadItem* download = GetActiveDownloadItem(0); 487 DownloadItem* download = GetActiveDownloadItem(0);
488 ASSERT_TRUE(download != NULL); 488 ASSERT_TRUE(download != NULL);
489 489
490 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 490 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
491 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 491 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
492 492
493 ContinueDownloadWithPath(download, new_path); 493 ContinueDownloadWithPath(download, new_path);
494 message_loop_.RunAllPending(); 494 message_loop_.RunAllPending();
495 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 495 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 file_util::CloseFile(fp); 533 file_util::CloseFile(fp);
534 EXPECT_TRUE(file_util::PathExists(new_path)); 534 EXPECT_TRUE(file_util::PathExists(new_path));
535 535
536 // Construct the unique file name that normally would be created, but 536 // Construct the unique file name that normally would be created, but
537 // which we will override. 537 // which we will override.
538 int uniquifier = download_util::GetUniquePathNumber(new_path); 538 int uniquifier = download_util::GetUniquePathNumber(new_path);
539 FilePath unique_new_path = new_path; 539 FilePath unique_new_path = new_path;
540 EXPECT_NE(0, uniquifier); 540 EXPECT_NE(0, uniquifier);
541 download_util::AppendNumberToPath(&unique_new_path, uniquifier); 541 download_util::AppendNumberToPath(&unique_new_path, uniquifier);
542 542
543 // |info| will be destroyed in download_manager_. 543 // Normally, the download system takes ownership of info, and is
544 DownloadCreateInfo* info(new DownloadCreateInfo); 544 // responsible for deleting it. In these unit tests, however, we
545 // don't call the function that deletes it, so we do so ourselves.
546 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo);
545 info->download_id = static_cast<int>(0); 547 info->download_id = static_cast<int>(0);
546 info->prompt_user_for_save_location = true; 548 info->prompt_user_for_save_location = true;
547 info->url_chain.push_back(GURL()); 549 info->url_chain.push_back(GURL());
548 info->is_dangerous_file = false; 550 info->is_dangerous_file = false;
549 info->is_dangerous_url = false; 551 info->is_dangerous_url = false;
550 552
551 download_manager_->CreateDownloadItem(info); 553 download_manager_->CreateDownloadItem(info.get());
552 554
553 DownloadItem* download = GetActiveDownloadItem(0); 555 DownloadItem* download = GetActiveDownloadItem(0);
554 ASSERT_TRUE(download != NULL); 556 ASSERT_TRUE(download != NULL);
555 557
556 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); 558 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
557 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); 559 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
558 560
559 // Create and initialize the download file. We're bypassing the first part 561 // 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 562 // 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 563 // name has been chosen, so we need to initialize the download file
562 // properly. 564 // properly.
563 DownloadFile* download_file( 565 DownloadFile* download_file(
564 new DownloadFile(info, download_manager_)); 566 new DownloadFile(info.get(), download_manager_));
565 download_file->Rename(cr_path); 567 download_file->Rename(cr_path);
566 // This creates the .crdownload version of the file. 568 // This creates the .crdownload version of the file.
567 download_file->Initialize(false); 569 download_file->Initialize(false);
568 // |download_file| is owned by DownloadFileManager. 570 // |download_file| is owned by DownloadFileManager.
569 AddDownloadToFileManager(info->download_id, download_file); 571 AddDownloadToFileManager(info->download_id, download_file);
570 572
571 ContinueDownloadWithPath(download, new_path); 573 ContinueDownloadWithPath(download, new_path);
572 message_loop_.RunAllPending(); 574 message_loop_.RunAllPending();
573 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); 575 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
574 576
(...skipping 14 matching lines...) Expand all
589 EXPECT_FALSE(observer->was_opened()); 591 EXPECT_FALSE(observer->was_opened());
590 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); 592 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
591 593
592 EXPECT_TRUE(file_util::PathExists(new_path)); 594 EXPECT_TRUE(file_util::PathExists(new_path));
593 EXPECT_FALSE(file_util::PathExists(cr_path)); 595 EXPECT_FALSE(file_util::PathExists(cr_path));
594 EXPECT_FALSE(file_util::PathExists(unique_new_path)); 596 EXPECT_FALSE(file_util::PathExists(unique_new_path));
595 std::string file_contents; 597 std::string file_contents;
596 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); 598 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents));
597 EXPECT_EQ(std::string(kTestData), file_contents); 599 EXPECT_EQ(std::string(kTestData), file_contents);
598 } 600 }
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