diff options
author | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 22:03:49 +0000 |
---|---|---|
committer | dcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-13 22:03:49 +0000 |
commit | acc4eb8c1e9474ee6e72bedbc5f2b8a32c76036f (patch) | |
tree | 40979202882a06030611f7a4de7fdb9ef95a17c2 | |
parent | e6b4e33f818937c4691703cb768aaf463cb01e80 (diff) | |
download | chromium_src-acc4eb8c1e9474ee6e72bedbc5f2b8a32c76036f.zip chromium_src-acc4eb8c1e9474ee6e72bedbc5f2b8a32c76036f.tar.gz chromium_src-acc4eb8c1e9474ee6e72bedbc5f2b8a32c76036f.tar.bz2 |
Revert 117696 - Created DownloadFileManager unit test.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/8978001
TBR=ahendrickson@chromium.org
Review URL: http://codereview.chromium.org/9112041
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117706 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/download/download_manager_unittest.cc | 109 | ||||
-rw-r--r-- | content/browser/download/download_file_manager.cc | 4 | ||||
-rw-r--r-- | content/browser/download/download_file_manager.h | 3 | ||||
-rw-r--r-- | content/browser/download/download_file_manager_unittest.cc | 672 | ||||
-rw-r--r-- | content/browser/download/download_file_unittest.cc | 10 | ||||
-rw-r--r-- | content/browser/download/download_id_unittest.cc | 8 | ||||
-rw-r--r-- | content/browser/download/download_manager_impl.cc | 10 | ||||
-rw-r--r-- | content/browser/download/download_manager_impl.h | 7 | ||||
-rw-r--r-- | content/browser/download/mock_download_file.cc | 142 | ||||
-rw-r--r-- | content/browser/download/mock_download_file.h | 112 | ||||
-rw-r--r-- | content/browser/download/mock_download_item.h | 3 | ||||
-rw-r--r-- | content/browser/download/mock_download_manager.cc | 190 | ||||
-rw-r--r-- | content/browser/download/mock_download_manager.h | 148 | ||||
-rw-r--r-- | content/content_tests.gypi | 3 | ||||
-rw-r--r-- | content/public/browser/download_manager.h | 8 |
15 files changed, 565 insertions, 864 deletions
diff --git a/chrome/browser/download/download_manager_unittest.cc b/chrome/browser/download/download_manager_unittest.cc index 2ab9057..3f2c669 100644 --- a/chrome/browser/download/download_manager_unittest.cc +++ b/chrome/browser/download/download_manager_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -60,8 +60,6 @@ using content::BrowserThread; using content::DownloadFile; using content::DownloadItem; using content::DownloadManager; -using ::testing::ReturnRef; -using ::testing::Return; using content::WebContents; namespace { @@ -195,12 +193,6 @@ class DownloadManagerTest : public testing::Test { download_file; } - void AddMockDownloadToFileManager(int id, MockDownloadFile* download_file) { - AddDownloadToFileManager(id, download_file); - ON_CALL(*download_file, GetDownloadManager()) - .WillByDefault(Return(download_manager_)); - } - void OnResponseCompleted(int32 download_id, int64 size, const std::string& hash) { download_manager_->OnResponseCompleted(download_id, size, hash); @@ -493,8 +485,7 @@ TEST_F(DownloadManagerTest, MAYBE_StartDownload) { // responsible for deleting it. In these unit tests, however, we // don't call the function that deletes it, so we do so ourselves. scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); - const DownloadId id = DownloadId(kValidIdDomain, static_cast<int>(i)); - info->download_id = id; + info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); info->mime_type = kStartDownloadCases[i].mime_type; @@ -513,8 +504,6 @@ TEST_F(DownloadManagerTest, MAYBE_StartDownload) { // Note that DownloadManager::FileSelectionCanceled() is never called. EXPECT_EQ(kStartDownloadCases[i].expected_save_as, observer.ShowedFileDialogForId(i)); - - file_manager()->CancelDownload(id); } } @@ -771,7 +760,9 @@ TEST_F(DownloadManagerTest, DownloadFilenameTest) { info->download_id = DownloadId(kValidIdDomain, i); info->url_chain.push_back(GURL()); - MockDownloadFile* download_file(new MockDownloadFile()); + MockDownloadFile::StatisticsRecorder recorder; + MockDownloadFile* download_file(new MockDownloadFile( + info.get(), DownloadRequestHandle(), download_manager_, &recorder)); FilePath suggested_path(ExpandFilenameTestPath( kDownloadFilenameTestCases[i].suggested_path, downloads_dir, alternate_dir)); @@ -784,18 +775,15 @@ TEST_F(DownloadManagerTest, DownloadFilenameTest) { FilePath final_path(ExpandFilenameTestPath( kDownloadFilenameTestCases[i].final_path, downloads_dir, alternate_dir)); + // If |final_path| is empty, its a signal that the download doesn't + // complete. Therefore it will only go through a single rename. + int expected_rename_count = (final_path.empty() ? 1 : 2); - AddMockDownloadToFileManager(info->download_id.local(), download_file); - - EXPECT_CALL(*download_file, Rename(intermediate_path)) - .WillOnce(Return(net::OK)); + AddDownloadToFileManager(info->download_id.local(), download_file); - if (!final_path.empty()) { - // If |final_path| is empty, its a signal that the download doesn't - // complete. Therefore it will only go through a single rename. - EXPECT_CALL(*download_file, Rename(final_path)) - .WillOnce(Return(net::OK)); - } + download_file->SetExpectedPath(0, intermediate_path); + if (!final_path.empty()) + download_file->SetExpectedPath(1, final_path); download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); DownloadItem* download = GetActiveDownloadItem(i); @@ -830,6 +818,11 @@ TEST_F(DownloadManagerTest, DownloadFilenameTest) { message_loop_.RunAllPending(); // |download| might be deleted when we get here. } + + EXPECT_EQ( + expected_rename_count, + recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)) + << "For test run " << i; } } @@ -849,27 +842,22 @@ TEST_F(DownloadManagerTest, DownloadRenameTest) { info->url_chain.push_back(GURL()); const FilePath new_path(kDownloadRenameCases[i].suggested_path); - MockDownloadFile* download_file(new MockDownloadFile()); - const DownloadId id = info->download_id; - ON_CALL(*download_file, GlobalId()) - .WillByDefault(ReturnRef(id)); - - AddMockDownloadToFileManager(info->download_id.local(), download_file); + MockDownloadFile::StatisticsRecorder recorder; + MockDownloadFile* download_file( + new MockDownloadFile(info.get(), + DownloadRequestHandle(), + download_manager_, + &recorder)); + AddDownloadToFileManager(info->download_id.local(), download_file); // |download_file| is owned by DownloadFileManager. if (kDownloadRenameCases[i].expected_rename_count == 1) { - EXPECT_CALL(*download_file, Rename(new_path)) - .Times(1) - .WillOnce(Return(net::OK)); + download_file->SetExpectedPath(0, new_path); } else { ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); FilePath crdownload(download_util::GetCrDownloadPath(new_path)); - EXPECT_CALL(*download_file, Rename(crdownload)) - .Times(1) - .WillOnce(Return(net::OK)); - EXPECT_CALL(*download_file, Rename(new_path)) - .Times(1) - .WillOnce(Return(net::OK)); + download_file->SetExpectedPath(0, crdownload); + download_file->SetExpectedPath(1, new_path); } download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); DownloadItem* download = GetActiveDownloadItem(i); @@ -889,10 +877,10 @@ TEST_F(DownloadManagerTest, DownloadRenameTest) { message_loop_.RunAllPending(); OnResponseCompleted(i, 1024, std::string("fake_hash")); } - // Validating the download item, so it will complete. - if (state.danger == DownloadStateInfo::DANGEROUS_FILE) - download->DangerousDownloadValidated(); message_loop_.RunAllPending(); + EXPECT_EQ( + kDownloadRenameCases[i].expected_rename_count, + recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); } } @@ -913,16 +901,16 @@ TEST_F(DownloadManagerTest, DownloadInterruptTest) { const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); - MockDownloadFile* download_file(new MockDownloadFile()); - ON_CALL(*download_file, AppendDataToFile(_, _)) - .WillByDefault(Return(net::OK)); + MockDownloadFile::StatisticsRecorder recorder; + MockDownloadFile* download_file( + new MockDownloadFile(info.get(), + DownloadRequestHandle(), + download_manager_, + &recorder)); + AddDownloadToFileManager(info->download_id.local(), download_file); // |download_file| is owned by DownloadFileManager. - AddMockDownloadToFileManager(info->download_id.local(), download_file); - - EXPECT_CALL(*download_file, Rename(cr_path)) - .Times(1) - .WillOnce(Return(net::OK)); + download_file->SetExpectedPath(0, cr_path); download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); @@ -938,6 +926,8 @@ TEST_F(DownloadManagerTest, DownloadInterruptTest) { ContinueDownloadWithPath(download, new_path); message_loop_.RunAllPending(); + EXPECT_EQ(1, + recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); int64 error_size = 3; @@ -1078,22 +1068,21 @@ TEST_F(DownloadManagerTest, DownloadCancelTest) { // responsible for deleting it. In these unit tests, however, we // don't call the function that deletes it, so we do so ourselves. scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); - DownloadId id = DownloadId(kValidIdDomain, 0); - info->download_id = id; + info->download_id = DownloadId(kValidIdDomain, 0); info->prompt_user_for_save_location = false; info->url_chain.push_back(GURL()); const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); - MockDownloadFile* download_file(new MockDownloadFile()); - ON_CALL(*download_file, AppendDataToFile(_, _)) - .WillByDefault(Return(net::OK)); - AddMockDownloadToFileManager(info->download_id.local(), download_file); + MockDownloadFile* download_file( + new MockDownloadFile(info.get(), + DownloadRequestHandle(), + download_manager_, + NULL)); + AddDownloadToFileManager(info->download_id.local(), download_file); // |download_file| is owned by DownloadFileManager. - EXPECT_CALL(*download_file, Rename(cr_path)) - .Times(1) - .WillOnce(Return(net::OK)); + download_file->SetExpectedPath(0, cr_path); download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); @@ -1127,8 +1116,6 @@ TEST_F(DownloadManagerTest, DownloadCancelTest) { EXPECT_EQ(download_item_model->GetStatusText(), l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED)); - file_manager()->CancelDownload(id); - EXPECT_FALSE(file_util::PathExists(new_path)); EXPECT_FALSE(file_util::PathExists(cr_path)); } diff --git a/content/browser/download/download_file_manager.cc b/content/browser/download/download_file_manager.cc index 4a1f771..45fea51 100644 --- a/content/browser/download/download_file_manager.cc +++ b/content/browser/download/download_file_manager.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -167,7 +167,7 @@ void DownloadFileManager::StartDownload( } manager->CreateDownloadItem(info, request_handle); - bool hash_needed = manager->GenerateFileHash(); + bool hash_needed = manager->delegate()->GenerateFileHash(); BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(&DownloadFileManager::CreateDownloadFile, this, diff --git a/content/browser/download/download_file_manager.h b/content/browser/download/download_file_manager.h index 4688835..fdce6af 100644 --- a/content/browser/download/download_file_manager.h +++ b/content/browser/download/download_file_manager.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // @@ -139,7 +139,6 @@ class CONTENT_EXPORT DownloadFileManager private: friend class base::RefCountedThreadSafe<DownloadFileManager>; - friend class DownloadFileManagerTest; friend class DownloadManagerTest; FRIEND_TEST_ALL_PREFIXES(DownloadManagerTest, StartDownload); diff --git a/content/browser/download/download_file_manager_unittest.cc b/content/browser/download/download_file_manager_unittest.cc deleted file mode 100644 index bf49ddf..0000000 --- a/content/browser/download/download_file_manager_unittest.cc +++ /dev/null @@ -1,672 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/download/download_file_manager.h" - -#include "base/file_path.h" -#include "base/file_util.h" -#include "base/message_loop.h" -#include "base/string_number_conversions.h" -#include "content/browser/browser_thread_impl.h" -#include "content/browser/download/download_buffer.h" -#include "content/browser/download/download_create_info.h" -#include "content/browser/download/download_id.h" -#include "content/browser/download/download_id_factory.h" -#include "content/browser/download/download_request_handle.h" -#include "content/browser/download/download_status_updater.h" -#include "content/browser/download/mock_download_file.h" -#include "content/browser/download/mock_download_manager.h" -#include "content/browser/download/mock_download_manager_delegate.h" -#include "net/base/io_buffer.h" -#include "net/base/net_errors.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" - -using content::BrowserThread; -using content::BrowserThreadImpl; - -using ::testing::_; -using ::testing::AtLeast; -using ::testing::Mock; -using ::testing::Return; -using ::testing::StrEq; - -namespace { - -DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; - -class MockDownloadFileFactory : - public DownloadFileManager::DownloadFileFactory { - - public: - MockDownloadFileFactory() {} - virtual ~MockDownloadFileFactory() {} - - virtual content::DownloadFile* CreateFile( - DownloadCreateInfo* info, - const DownloadRequestHandle& request_handle, - content::DownloadManager* download_manager, - bool calculate_hash) OVERRIDE; - - MockDownloadFile* GetExistingFile(const DownloadId& id); - - private: - std::map<DownloadId, MockDownloadFile*> files_; -}; - -content::DownloadFile* MockDownloadFileFactory::CreateFile( - DownloadCreateInfo* info, - const DownloadRequestHandle& request_handle, - content::DownloadManager* download_manager, - bool calculate_hash) { - DCHECK(files_.end() == files_.find(info->download_id)); - MockDownloadFile* created_file = new MockDownloadFile(); - files_[info->download_id] = created_file; - - ON_CALL(*created_file, GetDownloadManager()) - .WillByDefault(Return(download_manager)); - - return created_file; -} - -MockDownloadFile* MockDownloadFileFactory::GetExistingFile( - const DownloadId& id) { - DCHECK(files_.find(id) != files_.end()); - return files_[id]; -} - -class MockDownloadRequestHandle : public DownloadRequestHandle { - public: - MockDownloadRequestHandle(content::DownloadManager* manager) - : manager_(manager) {} - - virtual content::DownloadManager* GetDownloadManager() const OVERRIDE; - - private: - - content::DownloadManager* manager_; -}; - -content::DownloadManager* MockDownloadRequestHandle::GetDownloadManager() - const { - return manager_; -} - -} // namespace - -class DownloadFileManagerTest : public testing::Test { - public: - static const char* kTestData1; - static const char* kTestData2; - static const char* kTestData3; - static const char* kTestData4; - static const char* kTestData5; - static const char* kTestData6; - static const int32 kDummyDownloadId; - static const int32 kDummyDownloadId2; - static const int kDummyChildId; - static const int kDummyRequestId; - - // We need a UI |BrowserThread| in order to destruct |download_manager_|, - // which has trait |BrowserThread::DeleteOnUIThread|. Without this, - // calling Release() on |download_manager_| won't ever result in its - // destructor being called and we get a leak. - DownloadFileManagerTest() - : download_buffer_(new content::DownloadBuffer()), - id_factory_(new DownloadIdFactory(kValidIdDomain)), - ui_thread_(BrowserThread::UI, &loop_), - file_thread_(BrowserThread::FILE, &loop_) { - } - - ~DownloadFileManagerTest() { - } - - virtual void SetUp() { - download_manager_ = new MockDownloadManager(); - request_handle_.reset(new MockDownloadRequestHandle(download_manager_)); - download_file_factory_ = new MockDownloadFileFactory; - download_file_manager_ = - new DownloadFileManager(NULL, download_file_factory_); - } - - virtual void TearDown() { - // When a DownloadManager's reference count drops to 0, it is not - // deleted immediately. Instead, a task is posted to the UI thread's - // message loop to delete it. - // So, drop the reference count to 0 and run the message loop once - // to ensure that all resources are cleaned up before the test exits. - download_manager_ = NULL; - ui_thread_.message_loop()->RunAllPending(); - } - - void ProcessAllPendingMessages() { - loop_.RunAllPending(); - } - - // Clears all gmock expectations for the download file |id| and the manager. - void ClearExpectations(DownloadId id) { - MockDownloadFile* file = download_file_factory_->GetExistingFile(id); - Mock::VerifyAndClearExpectations(file); - Mock::VerifyAndClearExpectations(download_manager_); - } - - // Start a download. - // |info| is the information needed to create a new download file. - // |id| is the download ID of the new download file. - void StartDownload(DownloadCreateInfo* info, const DownloadId& id) { - // Expected call sequence: - // StartDownload - // DownloadManager::CreateDownloadItem - // DownloadManagerDelegate::GenerateFileHash - // Process one message in the message loop - // CreateDownloadFile - // new MockDownloadFile, add to downloads_[id] - // DownloadRequestHandle::ResumeRequest - // StartUpdateTimer - // DownloadCreateInfo is destroyed - // Process one message in the message loop - // DownloadManager::StartDownload - info->download_id = id; - - // Set expectations and return values. - EXPECT_CALL(*download_manager_, CreateDownloadItem(info, _)) - .Times(1); - EXPECT_CALL(*download_manager_, GenerateFileHash()) - .Times(AtLeast(1)) - .WillRepeatedly(Return(false)); - EXPECT_CALL(*download_manager_, StartDownload(id.local())); - - download_file_manager_->StartDownload(info, *request_handle_); - ProcessAllPendingMessages(); - - ClearExpectations(id); - } - - // Creates a new |net::IOBuffer| of size |length| with |data|, and attaches - // it to the download buffer. - bool UpdateBuffer(const char* data, size_t length) { - // We are passing ownership of this buffer to the download file manager. - // NOTE: we are padding io_buffer with one extra character so that the - // mock testing framework can treat it as a NULL-delimited string. - net::IOBuffer* io_buffer = new net::IOBuffer(length + 1); - // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. - io_buffer->AddRef(); - memcpy(io_buffer->data(), data, length); - io_buffer->data()[length] = 0; - - download_buffer_->AddData(io_buffer, length); - - return true; - } - - // Updates the download file. - // |id| is the download ID of the download file. - // |data| is the buffer containing the data. - // |length| is the length of the data buffer. - // |error_to_insert| is the error to simulate. For no error, use net::OK. - void UpdateDownload(const DownloadId& id, - const char* data, - size_t length, - net::Error error_to_insert) { - // Expected call sequence: - // Create DownloadBuffer - // UpdateDownload - // GetDownloadFile - // DownloadFile::AppendDataToFile - // - // On error: - // DownloadFile::GetDownloadManager - // DownloadFile::BytesSoFar - // CancelDownload - // Process one message in the message loop - // DownloadManager::OnDownloadInterrupted - EXPECT_TRUE(UpdateBuffer(data, length)); - MockDownloadFile* file = download_file_factory_->GetExistingFile(id); - ASSERT_TRUE(file != NULL); - byte_count_[id] += length; - - // Make sure our comparison string is NULL-terminated. - char* expected_data = new char[length + 1]; - memcpy(expected_data, data, length); - expected_data[length] = 0; - - EXPECT_CALL(*file, AppendDataToFile(StrEq(expected_data), length)) - .Times(1) - .WillOnce(Return(error_to_insert)); - - if (error_to_insert != net::OK) { - EXPECT_CALL(*file, BytesSoFar()) - .Times(AtLeast(1)) - .WillRepeatedly(Return(byte_count_[id])); - } - - download_file_manager_->UpdateDownload(id, download_buffer_.get()); - - if (error_to_insert != net::OK) { - EXPECT_CALL(*download_manager_, - OnDownloadInterrupted( - id.local(), - byte_count_[id], - "", - ConvertNetErrorToInterruptReason(error_to_insert, - DOWNLOAD_INTERRUPT_FROM_DISK))); - - ProcessAllPendingMessages(); - ++error_count_[id]; - } - - ClearExpectations(id); - delete [] expected_data; - } - - // Renames the download file. - // |id| is the download ID of the download file. - // |new_path| is the new file name. - // |error_to_insert| is the error to inject. For no error, use net::OK. - // |is_complete| indicates whether the rename occurs after the download is - // complete. - // |replace| indicates whether to replace or uniquify the file. Only used - // if |is_complete| true. - void RenameFile(const DownloadId& id, - const FilePath& new_path, - net::Error error_to_insert, - bool is_complete, - bool replace) { - // Expected call sequence: - // RenameInProgressDownloadFile/RenameCompletingDownloadFile - // GetDownloadFile - // DownloadFile::Rename - // - // On Error: - // CancelDownloadOnRename - // GetDownloadFile - // DownloadFile::GetDownloadManager - // No Manager: - // DownloadFile::CancelDownloadRequest/return - // Has Manager: - // DownloadFile::BytesSoFar - // Process one message in the message loop - // DownloadManager::OnDownloadInterrupted - // - // On Success, if final rename: - // Process one message in the message loop - // DownloadManager::OnDownloadRenamedToFinalName - MockDownloadFile* file = download_file_factory_->GetExistingFile(id); - ASSERT_TRUE(file != NULL); - - FilePath unique_path = new_path; - int uniquifier = 0; - - if (is_complete && !replace) { - uniquifier = content::DownloadFile::GetUniquePathNumber(new_path); - if (uniquifier > 0) - content::DownloadFile::AppendNumberToPath(&unique_path, uniquifier); - } - - EXPECT_CALL(*file, Rename(unique_path)) - .Times(1) - .WillOnce(Return(error_to_insert)); - - if (error_to_insert != net::OK) { - EXPECT_CALL(*file, GetDownloadManager()); - - EXPECT_CALL(*file, BytesSoFar()) - .Times(AtLeast(1)) - .WillRepeatedly(Return(byte_count_[id])); - } - - if (!is_complete) { - download_file_manager_->RenameInProgressDownloadFile(id, new_path); - } else { - download_file_manager_->RenameCompletingDownloadFile(id, - new_path, - replace); - } - - if (error_to_insert != net::OK) { - EXPECT_CALL(*download_manager_, - OnDownloadInterrupted( - id.local(), - byte_count_[id], - "", - ConvertNetErrorToInterruptReason( - error_to_insert, DOWNLOAD_INTERRUPT_FROM_DISK))); - - ProcessAllPendingMessages(); - ++error_count_[id]; - } else if (is_complete) { - EXPECT_CALL(*download_manager_, OnDownloadRenamedToFinalName( - id.local(), unique_path, uniquifier)); - ProcessAllPendingMessages(); - } - - } - - // Finish the download operation. - // |id| is the download ID of the download file. - // |reason| is the interrupt reason to inject. For no interrupt, use - // DOWNLOAD_INTERRUPT_REASON_NONE. - // |security_string| is the extra security information, if interrupted. - void OnResponseCompleted(const DownloadId& id, - InterruptReason reason, - const std::string& security_string) { - // OnResponseCompleted - // GetDownloadFile - // DownloadFile::Finish - // DownloadFile::GetDownloadManager - // DownloadFile::BytesSoFar - // Process one message in the message loop - // - // OK: - // DownloadFile::GetHash - // DownloadManager::OnResponseCompleted - // - // On Error: - // DownloadManager::OnDownloadInterrupted - MockDownloadFile* file = download_file_factory_->GetExistingFile(id); - ASSERT_TRUE(file != NULL); - - EXPECT_CALL(*file, Finish()); - if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { - EXPECT_CALL(*file, GetHash(_)) - .WillOnce(Return(false)); - } - EXPECT_CALL(*file, BytesSoFar()) - .Times(AtLeast(1)) - .WillRepeatedly(Return(byte_count_[id])); - - download_file_manager_->OnResponseCompleted(id, reason, security_string); - - if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { - EXPECT_CALL(*download_manager_, - OnResponseCompleted(id.local(), byte_count_[id], "")) - .Times(1); - } else { - EXPECT_EQ(0, error_count_[id]); - - EXPECT_CALL(*download_manager_, - OnDownloadInterrupted(id.local(), - byte_count_[id], - "", - reason)) - .Times(1); - } - - ProcessAllPendingMessages(); - - if (reason != DOWNLOAD_INTERRUPT_REASON_NONE) - ++error_count_[id]; - - ClearExpectations(id); - } - - void CleanUp(DownloadId id) { - // Expected calls: - // DownloadFileManager::CancelDownload - // DownloadFile::Cancel - // DownloadFileManager::EraseDownload - // if no more downloads: - // DownloadFileManager::StopUpdateTimer - MockDownloadFile* file = download_file_factory_->GetExistingFile(id); - ASSERT_TRUE(file != NULL); - - EXPECT_CALL(*file, Cancel()); - - download_file_manager_->CancelDownload(id); - - EXPECT_TRUE(NULL == download_file_manager_->GetDownloadFile(id)); - } - - protected: - scoped_refptr<MockDownloadManager> download_manager_; - scoped_ptr<MockDownloadRequestHandle> request_handle_; - MockDownloadFileFactory* download_file_factory_; - scoped_refptr<DownloadFileManager> download_file_manager_; - scoped_refptr<content::DownloadBuffer> download_buffer_; - - // Per-download statistics. - std::map<DownloadId, int64> byte_count_; - std::map<DownloadId, int> error_count_; - - private: - MessageLoop loop_; - scoped_refptr<DownloadIdFactory> id_factory_; - - // UI thread. - BrowserThreadImpl ui_thread_; - - // File thread to satisfy debug checks in DownloadFile. - BrowserThreadImpl file_thread_; -}; - -const char* DownloadFileManagerTest::kTestData1 = - "Let's write some data to the file!\n"; -const char* DownloadFileManagerTest::kTestData2 = "Writing more data.\n"; -const char* DownloadFileManagerTest::kTestData3 = "Final line."; -const char* DownloadFileManagerTest::kTestData4 = "Writing, writing, writing\n"; -const char* DownloadFileManagerTest::kTestData5 = "All we do is writing,\n"; -const char* DownloadFileManagerTest::kTestData6 = "Rawhide!"; - -const int32 DownloadFileManagerTest::kDummyDownloadId = 23; -const int32 DownloadFileManagerTest::kDummyDownloadId2 = 77; -const int DownloadFileManagerTest::kDummyChildId = 3; -const int DownloadFileManagerTest::kDummyRequestId = 67; - -TEST_F(DownloadFileManagerTest, CancelAtStart) { - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, CancelBeforeFinished) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, DownloadWithError) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), - net::ERR_FILE_NO_SPACE); -} - -TEST_F(DownloadFileManagerTest, CompleteDownload) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, CompleteDownloadWithError) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, - DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED, - ""); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, RenameInProgress) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::OK, false, false); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, RenameInProgressWithError) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::ERR_FILE_PATH_TOO_LONG, false, false); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, RenameCompleting) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::OK, false, false); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, RenameCompletingWithError) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::ERR_FILE_PATH_TOO_LONG, false, false); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, RenameTwice) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - FilePath crfoo(FILE_PATH_LITERAL("foo.txt.crdownload")); - RenameFile(dummy_id, crfoo, net::OK, false, false); - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::OK, false, false); - - CleanUp(dummy_id); -} - -TEST_F(DownloadFileManagerTest, TwoDownloads) { - // Same as StartDownload, at first. - DownloadCreateInfo* info = new DownloadCreateInfo; - DownloadCreateInfo* info2 = new DownloadCreateInfo; - DownloadId dummy_id(download_manager_.get(), kDummyDownloadId); - DownloadId dummy_id2(download_manager_.get(), kDummyDownloadId2); - - StartDownload(info, dummy_id); - - UpdateDownload(dummy_id, kTestData1, strlen(kTestData1), net::OK); - - StartDownload(info2, dummy_id2); - - UpdateDownload(dummy_id, kTestData2, strlen(kTestData2), net::OK); - - UpdateDownload(dummy_id2, kTestData1, strlen(kTestData4), net::OK); - FilePath crbar(FILE_PATH_LITERAL("bar.txt.crdownload")); - RenameFile(dummy_id2, crbar, net::OK, false, false); - - FilePath crfoo(FILE_PATH_LITERAL("foo.txt.crdownload")); - RenameFile(dummy_id, crfoo, net::OK, false, false); - - UpdateDownload(dummy_id, kTestData3, strlen(kTestData3), net::OK); - - UpdateDownload(dummy_id2, kTestData2, strlen(kTestData5), net::OK); - UpdateDownload(dummy_id2, kTestData3, strlen(kTestData6), net::OK); - - OnResponseCompleted(dummy_id2, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - OnResponseCompleted(dummy_id, DOWNLOAD_INTERRUPT_REASON_NONE, ""); - - FilePath bar(FILE_PATH_LITERAL("bar.txt")); - RenameFile(dummy_id2, bar, net::OK, false, false); - - CleanUp(dummy_id2); - - FilePath foo(FILE_PATH_LITERAL("foo.txt")); - RenameFile(dummy_id, foo, net::OK, false, false); - - CleanUp(dummy_id); -} - - -// TODO(ahendrickson) -- A test for updating progress. -// Expected call sequence: -// UpdateInProgressDownloads -// DownloadFile::GetDownloadFile -// Process one message in the message loop -// DownloadManager::UpdateDownload - -// TODO(ahendrickson) -- A test for download manager shutdown. -// Expected call sequence: -// OnDownloadManagerShutdown -// DownloadFile::GetDownloadManager -// DownloadFile::CancelDownloadRequest -// DownloadFile::~DownloadFile diff --git a/content/browser/download/download_file_unittest.cc b/content/browser/download/download_file_unittest.cc index 4855d44..4bc6ec354 100644 --- a/content/browser/download/download_file_unittest.cc +++ b/content/browser/download/download_file_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -51,7 +51,11 @@ class DownloadFileTest : public testing::Test { } virtual void SetUp() { - download_manager_ = new MockDownloadManager; + download_manager_delegate_.reset(new MockDownloadManagerDelegate()); + download_manager_ = new MockDownloadManager( + download_manager_delegate_.get(), + id_factory_, + &download_status_updater_); } virtual void TearDown() { @@ -104,6 +108,8 @@ class DownloadFileTest : public testing::Test { } protected: + DownloadStatusUpdater download_status_updater_; + scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; scoped_refptr<DownloadManager> download_manager_; linked_ptr<net::FileStream> file_stream_; diff --git a/content/browser/download/download_id_unittest.cc b/content/browser/download/download_id_unittest.cc index 18f157f..cc5272b 100644 --- a/content/browser/download/download_id_unittest.cc +++ b/content/browser/download/download_id_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -22,7 +22,8 @@ using content::DownloadManager; class DownloadIdTest : public testing::Test { public: DownloadIdTest() - : ui_thread_(BrowserThread::UI, &message_loop_) { + : download_manager_delegate_(new MockDownloadManagerDelegate), + ui_thread_(BrowserThread::UI, &message_loop_) { num_managers_ = ARRAYSIZE_UNSAFE(download_managers_); std::vector<MockDownloadManager*> managers; managers.resize(num_managers_); @@ -30,7 +31,7 @@ class DownloadIdTest : public testing::Test { // Create the download managers. for (i = 0; i < num_managers_; ++i) { managers[i] = - new MockDownloadManager(); + new MockDownloadManager(download_manager_delegate_.get(), NULL, NULL); } // Sort by pointer value. std::sort(managers.begin(), managers.end()); @@ -47,6 +48,7 @@ class DownloadIdTest : public testing::Test { } protected: + scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; scoped_refptr<DownloadManager> download_managers_[2]; MessageLoopForUI message_loop_; // Necessary to delete |DownloadManager|s. diff --git a/content/browser/download/download_manager_impl.cc b/content/browser/download/download_manager_impl.cc index 55d7cf6..c6b7de5 100644 --- a/content/browser/download/download_manager_impl.cc +++ b/content/browser/download/download_manager_impl.cc @@ -693,10 +693,6 @@ void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) { } } -bool DownloadManagerImpl::GenerateFileHash() { - return delegate_->GenerateFileHash(); -} - content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const { return delegate_; } @@ -748,8 +744,8 @@ void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) { DCHECK_EQ(1, downloads_count); } -int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, - base::Time remove_end) { +int DownloadManagerImpl::RemoveDownloadsBetween(const base::Time remove_begin, + const base::Time remove_end) { delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); // All downloads visible to the user will be in the history, @@ -769,7 +765,7 @@ int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, return RemoveDownloadItems(pending_deletes); } -int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { +int DownloadManagerImpl::RemoveDownloads(const base::Time remove_begin) { return RemoveDownloadsBetween(remove_begin, base::Time()); } diff --git a/content/browser/download/download_manager_impl.h b/content/browser/download/download_manager_impl.h index f323856..50fdf58 100644 --- a/content/browser/download/download_manager_impl.h +++ b/content/browser/download/download_manager_impl.h @@ -58,9 +58,9 @@ class CONTENT_EXPORT DownloadManagerImpl virtual void OnDownloadRenamedToFinalName(int download_id, const FilePath& full_path, int uniquifier) OVERRIDE; - virtual int RemoveDownloadsBetween(base::Time remove_begin, - base::Time remove_end) OVERRIDE; - virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; + virtual int RemoveDownloadsBetween(const base::Time remove_begin, + const base::Time remove_end) OVERRIDE; + virtual int RemoveDownloads(const base::Time remove_begin) OVERRIDE; virtual int RemoveAllDownloads() OVERRIDE; virtual void DownloadUrl(const GURL& url, const GURL& referrer, @@ -97,7 +97,6 @@ class CONTENT_EXPORT DownloadManagerImpl virtual void SavePageDownloadFinished( content::DownloadItem* download) OVERRIDE; virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; - virtual bool GenerateFileHash() OVERRIDE; virtual content::DownloadManagerDelegate* delegate() const OVERRIDE; virtual void SetDownloadManagerDelegate( content::DownloadManagerDelegate* delegate) OVERRIDE; diff --git a/content/browser/download/mock_download_file.cc b/content/browser/download/mock_download_file.cc index 2c5f8c3..885def1 100644 --- a/content/browser/download/mock_download_file.cc +++ b/content/browser/download/mock_download_file.cc @@ -1,18 +1,144 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "content/browser/download/mock_download_file.h" -using ::testing::_; -using ::testing::Return; +#include "content/browser/download/download_create_info.h" +#include "testing/gtest/include/gtest/gtest.h" -MockDownloadFile::MockDownloadFile() { - // This is here because |Initialize()| is normally called right after - // construction. - ON_CALL(*this, Initialize()) - .WillByDefault(Return(net::OK)); +using content::DownloadManager; + +MockDownloadFile::StatisticsRecorder::StatisticsRecorder() { +} + +MockDownloadFile::StatisticsRecorder::~StatisticsRecorder() { +} + +void MockDownloadFile::StatisticsRecorder::Record(StatisticsIndex index) { + Add(index, 1); +} + +void MockDownloadFile::StatisticsRecorder::Add(StatisticsIndex index, + int count) { + map_[index] = map_[index] + count; +} + +int MockDownloadFile::StatisticsRecorder::Count(StatisticsIndex index) { + if (map_.find(index) == map_.end()) + return 0; + return map_[index]; +} + +MockDownloadFile::MockDownloadFile( + const DownloadCreateInfo* info, + const DownloadRequestHandle& request_handle, + DownloadManager* download_manager, + StatisticsRecorder* recorder) + : id_(info->download_id), + request_handle_(request_handle), + download_manager_(download_manager), + recorder_(recorder), + rename_count_(0), + in_progress_(true) { } MockDownloadFile::~MockDownloadFile() { } + +net::Error MockDownloadFile::Initialize() { + in_progress_ = true; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_INITIALIZE); + return net::OK; +} + +net::Error MockDownloadFile::AppendDataToFile( + const char* data, size_t data_len) { + data_.append(data, data_len); + if (recorder_) { + recorder_->Record(StatisticsRecorder::STAT_APPEND); + recorder_->Add(StatisticsRecorder::STAT_BYTES, data_len); + } + return net::OK; +} + +net::Error MockDownloadFile::Rename(const FilePath& full_path) { + EXPECT_LT(rename_count_, expected_rename_path_list_.size()); + EXPECT_STREQ(expected_rename_path_list_[rename_count_].value().c_str(), + full_path.value().c_str()); + ++rename_count_; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_RENAME); + return net::OK; +} + +void MockDownloadFile::Detach() { + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_DETACH); +} + +void MockDownloadFile::Cancel() { + in_progress_ = false; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_CANCEL); +} + +void MockDownloadFile::Finish() { + in_progress_ = false; + if (recorder_) + recorder_->Record(StatisticsRecorder::STAT_FINISH); +} + +void MockDownloadFile::AnnotateWithSourceInformation() { +} + +FilePath MockDownloadFile::FullPath() const { + return FilePath(); +} + +bool MockDownloadFile::InProgress() const { + return in_progress_; +} + +int64 MockDownloadFile::BytesSoFar() const { + return data_.length(); +} + +int64 MockDownloadFile::CurrentSpeed() const { + return 0; +} + +bool MockDownloadFile::GetHash(std::string* hash) { + return false; +} + +std::string MockDownloadFile::GetHashState() { + return ""; +} + +// DownloadFileInterface implementation. +void MockDownloadFile::CancelDownloadRequest() { +} + +int MockDownloadFile::Id() const { + return id_.local(); +} + +DownloadManager* MockDownloadFile::GetDownloadManager() { + return download_manager_; +} + +const DownloadId& MockDownloadFile::GlobalId() const { + return id_; +} + +std::string MockDownloadFile::DebugString() const { + return ""; +} + +void MockDownloadFile::SetExpectedPath(size_t index, const FilePath& path) { + if (expected_rename_path_list_.size() < index + 1) + expected_rename_path_list_.resize(index + 1); + expected_rename_path_list_[index] = path; +} diff --git a/content/browser/download/mock_download_file.h b/content/browser/download/mock_download_file.h index 2e97b04..4a2a5ef 100644 --- a/content/browser/download/mock_download_file.h +++ b/content/browser/download/mock_download_file.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -16,35 +16,103 @@ #include "content/public/browser/download_file.h" #include "content/public/browser/download_manager.h" #include "net/base/net_errors.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" struct DownloadCreateInfo; class MockDownloadFile : virtual public content::DownloadFile { public: - MockDownloadFile(); + // Class to extract statistics from the usage of |MockDownloadFile|. + class StatisticsRecorder { + public: + enum StatisticsIndex { + STAT_INITIALIZE, + STAT_APPEND, + STAT_RENAME, + STAT_DETACH, + STAT_CANCEL, + STAT_FINISH, + STAT_BYTES + }; + + StatisticsRecorder(); + ~StatisticsRecorder(); + + // Records the statistic. + // |index| indicates what statistic to use. + void Record(StatisticsIndex index); + + // Adds to the statistic. + // |index| indicates what statistic to use. + void Add(StatisticsIndex index, int count); + + // Returns the statistic value. + // |index| indicates what statistic to use. + int Count(StatisticsIndex index); + + private: + typedef std::map<StatisticsIndex, int> StatisticsMap; + + StatisticsMap map_; + }; + + MockDownloadFile(const DownloadCreateInfo* info, + const DownloadRequestHandle& request_handle, + content::DownloadManager* download_manager, + StatisticsRecorder* recorder); virtual ~MockDownloadFile(); // DownloadFile functions. - MOCK_METHOD0(Initialize, net::Error()); - MOCK_METHOD2(AppendDataToFile, net::Error(const char* data, size_t data_len)); - MOCK_METHOD1(Rename, net::Error(const FilePath& full_path)); - MOCK_METHOD0(Detach, void()); - MOCK_METHOD0(Cancel, void()); - MOCK_METHOD0(Finish, void()); - MOCK_METHOD0(AnnotateWithSourceInformation, void()); - MOCK_CONST_METHOD0(FullPath, FilePath()); - MOCK_CONST_METHOD0(InProgress, bool()); - MOCK_CONST_METHOD0(BytesSoFar, int64()); - MOCK_CONST_METHOD0(CurrentSpeed, int64()); - MOCK_METHOD1(GetHash, bool(std::string* hash)); - MOCK_METHOD0(GetHashState, std::string()); - MOCK_METHOD0(CancelDownloadRequest, void()); - MOCK_CONST_METHOD0(Id, int()); - MOCK_METHOD0(GetDownloadManager, content::DownloadManager*()); - MOCK_CONST_METHOD0(GlobalId, const DownloadId&()); - MOCK_CONST_METHOD0(DebugString, std::string()); + virtual net::Error Initialize() OVERRIDE; + virtual net::Error AppendDataToFile(const char* data, + size_t data_len) OVERRIDE; + virtual net::Error Rename(const FilePath& full_path) OVERRIDE; + virtual void Detach() OVERRIDE; + virtual void Cancel() OVERRIDE; + virtual void Finish() OVERRIDE; + virtual void AnnotateWithSourceInformation() OVERRIDE; + virtual FilePath FullPath() const OVERRIDE; + virtual bool InProgress() const OVERRIDE; + virtual int64 BytesSoFar() const OVERRIDE; + virtual int64 CurrentSpeed() const OVERRIDE; + virtual bool GetHash(std::string* hash) OVERRIDE; + virtual std::string GetHashState() OVERRIDE; + virtual void CancelDownloadRequest() OVERRIDE; + virtual int Id() const OVERRIDE; + virtual content::DownloadManager* GetDownloadManager() OVERRIDE; + virtual const DownloadId& GlobalId() const OVERRIDE; + virtual std::string DebugString() const OVERRIDE; + + // Functions relating to setting and checking expectations. + size_t rename_count() const { return rename_count_; } + void SetExpectedPath(size_t index, const FilePath& path); + + private: + // The unique identifier for this download, assigned at creation by + // the DownloadFileManager for its internal record keeping. + DownloadId id_; + + // The handle to the request information. Used for operations outside the + // download system, specifically canceling a download. + DownloadRequestHandle request_handle_; + + // DownloadManager this download belongs to. + scoped_refptr<content::DownloadManager> download_manager_; + + // Records usage statistics. Not owned by this class (survives destruction). + StatisticsRecorder* recorder_; + + // The number of times |Rename()| has been called. + // Used instead of checking |recorder_| because the latter can be NULL. + size_t rename_count_; + + // A vector of paths that we expect to call |Rename()| with. + std::vector<FilePath> expected_rename_path_list_; + + // A buffer to hold the data we write. + std::string data_; + + // Dummy in-progress flag. + bool in_progress_; }; #endif // CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ diff --git a/content/browser/download/mock_download_item.h b/content/browser/download/mock_download_item.h index 7b3f429..2ffb07b 100644 --- a/content/browser/download/mock_download_item.h +++ b/content/browser/download/mock_download_item.h @@ -1,4 +1,4 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -10,7 +10,6 @@ #include "content/browser/download/download_id.h" #include "content/browser/download/download_persistent_store_info.h" -#include "content/browser/download/interrupt_reasons.h" #include "content/public/browser/download_item.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/content/browser/download/mock_download_manager.cc b/content/browser/download/mock_download_manager.cc index 78400e3..74f3e84 100644 --- a/content/browser/download/mock_download_manager.cc +++ b/content/browser/download/mock_download_manager.cc @@ -4,8 +4,196 @@ #include "content/browser/download/mock_download_manager.h" -MockDownloadManager::MockDownloadManager() { +#include "content/browser/download/download_create_info.h" +#include "content/browser/download/download_item_impl.h" + +using content::DownloadItem; +using content::WebContents; + +MockDownloadManager::MockDownloadManager( + content::DownloadManagerDelegate* delegate, + DownloadIdFactory* id_factory, + DownloadStatusUpdater* updater) + : delegate_(delegate), id_factory_(id_factory), updater_(updater), + file_manager_(NULL) { } MockDownloadManager::~MockDownloadManager() { + for (std::map<int32, DownloadItem*>::iterator it = item_map_.begin(); + it != item_map_.end(); + ++it) { + delete it->second; + } + for (std::map<int32, DownloadItem*>::iterator it = inactive_item_map_.begin(); + it != inactive_item_map_.end(); + ++it) { + delete it->second; + } +} + +void MockDownloadManager::Shutdown() { +} + +void MockDownloadManager::GetTemporaryDownloads(const FilePath& dir_path, + DownloadVector* result) { +} + +void MockDownloadManager::GetAllDownloads(const FilePath& dir_path, + DownloadVector* result) { +} + +void MockDownloadManager::SearchDownloads(const string16& query, + DownloadVector* result) { +} + +bool MockDownloadManager::Init(content::BrowserContext* browser_context) { + return true; +} + +void MockDownloadManager::StartDownload(int32 id) { +} + +void MockDownloadManager::UpdateDownload(int32 download_id, + int64 bytes_so_far, + int64 bytes_per_sec, + const std::string& hash_state) { +} + +void MockDownloadManager::OnResponseCompleted(int32 download_id, int64 size, + const std::string& hash) { +} + +void MockDownloadManager::CancelDownload(int32 download_id) { +} + +void MockDownloadManager::OnDownloadInterrupted(int32 download_id, + int64 size, + const std::string& hash_state, + InterruptReason reason) { +} + +void MockDownloadManager::OnDownloadRenamedToFinalName( + int download_id, + const FilePath& full_path, + int uniquifier) { +} + +int MockDownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, + const base::Time remove_end) { + return 0; +} + +int MockDownloadManager::RemoveDownloads(const base::Time remove_begin) { + return 0; +} + +int MockDownloadManager::RemoveAllDownloads() { + return 1; +} + +void MockDownloadManager::DownloadUrl(const GURL& url, + const GURL& referrer, + const std::string& referrer_encoding, + WebContents* web_contents) { +} + +void MockDownloadManager::DownloadUrlToFile( + const GURL& url, + const GURL& referrer, + const std::string& referrer_encoding, + const DownloadSaveInfo& save_info, + WebContents* web_contents) { +} + +void MockDownloadManager::AddObserver(Observer* observer) { +} + +void MockDownloadManager::RemoveObserver(Observer* observer) { +} + +void MockDownloadManager::OnPersistentStoreQueryComplete( + std::vector<DownloadPersistentStoreInfo>* entries) { +} + +void MockDownloadManager::OnItemAddedToPersistentStore(int32 download_id, + int64 db_handle) { +} + +int MockDownloadManager::InProgressCount() const { + return 1; +} + +content::BrowserContext* MockDownloadManager::GetBrowserContext() const { + return NULL; +} + +FilePath MockDownloadManager::LastDownloadPath() { + return FilePath(); +} + +void MockDownloadManager::CreateDownloadItem( + DownloadCreateInfo* info, + const DownloadRequestHandle& request_handle) { + NOTREACHED(); // Not yet implemented. + return; +} + +DownloadItem* MockDownloadManager::CreateSavePackageDownloadItem( + const FilePath& main_file_path, + const GURL& page_url, + bool is_otr, + DownloadItem::Observer* observer) { + NOTREACHED(); // Not yet implemented. + return NULL; +} + +void MockDownloadManager::ClearLastDownloadPath() { +} + +void MockDownloadManager::FileSelected(const FilePath& path, void* params) { +} + +void MockDownloadManager::FileSelectionCanceled(void* params) { +} + +void MockDownloadManager::RestartDownload(int32 download_id) { +} + +void MockDownloadManager::CheckForHistoryFilesRemoval() { +} + +DownloadItem* MockDownloadManager::GetDownloadItem(int id) { + std::map<int32, DownloadItem*>::iterator it = item_map_.find(id); + if (it == item_map_.end()) + return NULL; + return it->second; +} + +void MockDownloadManager::SavePageDownloadFinished(DownloadItem* download) { +} + +DownloadItem* MockDownloadManager::GetActiveDownloadItem(int id) { + return GetDownloadItem(id); +} + +content::DownloadManagerDelegate* MockDownloadManager::delegate() const { + return delegate_; +} + +void MockDownloadManager::SetDownloadManagerDelegate( + content::DownloadManagerDelegate* delegate) { +} + +void MockDownloadManager::ContinueDownloadWithPath( + DownloadItem* download, + const FilePath& chosen_file) { + download->Rename(chosen_file); +} + +DownloadItem* MockDownloadManager::GetActiveDownload(int32 download_id) { + return GetDownloadItem(download_id); +} + +void MockDownloadManager::SetFileManager(DownloadFileManager* file_manager) { + file_manager_ = file_manager; } diff --git a/content/browser/download/mock_download_manager.h b/content/browser/download/mock_download_manager.h index b8138a4..3172d6e 100644 --- a/content/browser/download/mock_download_manager.h +++ b/content/browser/download/mock_download_manager.h @@ -6,92 +6,98 @@ #define CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_MANAGER_H_ #pragma once +#include "content/browser/download/download_item_impl.h" #include "content/browser/download/download_id.h" #include "content/browser/download/download_id_factory.h" -#include "content/browser/download/download_request_handle.h" -#include "content/browser/download/download_types.h" -#include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" -#include "googleurl/src/gurl.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" + +class DownloadStatusUpdater; class MockDownloadManager : public content::DownloadManager { public: - MockDownloadManager(); + explicit MockDownloadManager(content::DownloadManagerDelegate* delegate, + DownloadIdFactory* id_factory, + DownloadStatusUpdater* updater); virtual ~MockDownloadManager(); // DownloadManager: - MOCK_METHOD0(Shutdown, void()); - MOCK_METHOD2(GetTemporaryDownloads, void(const FilePath& dir_path, - DownloadVector* result)); - MOCK_METHOD2(GetAllDownloads, void(const FilePath& dir_path, - DownloadVector* result)); - MOCK_METHOD2(SearchDownloads, void(const string16& query, - DownloadVector* result)); - MOCK_METHOD1(Init, bool(content::BrowserContext* browser_context)); - MOCK_METHOD1(StartDownload, void(int32 id)); - MOCK_METHOD4(UpdateDownload, void(int32 download_id, - int64 bytes_so_far, - int64 bytes_per_sec, - const std::string& hash_state)); - MOCK_METHOD3(OnResponseCompleted, void(int32 download_id, - int64 size, - const std::string& hash)); - MOCK_METHOD1(CancelDownload, void(int32 download_id)); - MOCK_METHOD4(OnDownloadInterrupted, void(int32 download_id, - int64 size, - const std::string& hash_state, - InterruptReason reason)); - MOCK_METHOD3(OnDownloadRenamedToFinalName, void(int download_id, - const FilePath& full_path, - int uniquifier)); - MOCK_METHOD2(RemoveDownloadsBetween, int(base::Time remove_begin, - base::Time remove_end)); - MOCK_METHOD1(RemoveDownloads, int(base::Time remove_begin)); - MOCK_METHOD0(RemoveAllDownloads, int()); - MOCK_METHOD4(DownloadUrl, void(const GURL& url, + virtual void Shutdown() OVERRIDE; + virtual void GetTemporaryDownloads(const FilePath& dir_path, + DownloadVector* result) OVERRIDE; + virtual void GetAllDownloads(const FilePath& dir_path, + DownloadVector* result) OVERRIDE; + virtual void SearchDownloads(const string16& query, + DownloadVector* result) OVERRIDE; + virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; + virtual void StartDownload(int32 id) OVERRIDE; + virtual void UpdateDownload(int32 download_id, + int64 bytes_so_far, + int64 bytes_per_sec, + const std::string& hash_state) OVERRIDE; + virtual void OnResponseCompleted(int32 download_id, int64 size, + const std::string& hash) OVERRIDE; + virtual void CancelDownload(int32 download_id) OVERRIDE; + virtual void OnDownloadInterrupted(int32 download_id, + int64 size, + const std::string& hash_state, + InterruptReason reason) OVERRIDE; + virtual void OnDownloadRenamedToFinalName(int download_id, + const FilePath& full_path, + int uniquifier) OVERRIDE; + virtual int RemoveDownloadsBetween(const base::Time remove_begin, + const base::Time remove_end) OVERRIDE; + virtual int RemoveDownloads(const base::Time remove_begin) OVERRIDE; + virtual int RemoveAllDownloads() OVERRIDE; + virtual void DownloadUrl(const GURL& url, + const GURL& referrer, + const std::string& referrer_encoding, + content::WebContents* web_contents) OVERRIDE; + virtual void DownloadUrlToFile(const GURL& url, const GURL& referrer, const std::string& referrer_encoding, - content::WebContents* web_contents)); - MOCK_METHOD5(DownloadUrlToFile, void(const GURL& url, - const GURL& referrer, - const std::string& referrer_encoding, - const DownloadSaveInfo& save_info, - content::WebContents* web_contents)); - MOCK_METHOD1(AddObserver, void(Observer* observer)); - MOCK_METHOD1(RemoveObserver, void(Observer* observer)); - MOCK_METHOD1(OnPersistentStoreQueryComplete, void( - std::vector<DownloadPersistentStoreInfo>* entries)); - MOCK_METHOD2(OnItemAddedToPersistentStore, void(int32 download_id, - int64 db_handle)); - MOCK_CONST_METHOD0(InProgressCount, int()); - MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*()); - MOCK_METHOD0(LastDownloadPath, FilePath()); - MOCK_METHOD2(CreateDownloadItem, void( + const DownloadSaveInfo& save_info, + content::WebContents* web_contents) OVERRIDE; + virtual void AddObserver(Observer* observer) OVERRIDE; + virtual void RemoveObserver(Observer* observer) OVERRIDE; + virtual void OnPersistentStoreQueryComplete( + std::vector<DownloadPersistentStoreInfo>* entries) OVERRIDE; + virtual void OnItemAddedToPersistentStore(int32 download_id, + int64 db_handle) OVERRIDE; + virtual int InProgressCount() const OVERRIDE; + virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; + virtual FilePath LastDownloadPath() OVERRIDE; + virtual void CreateDownloadItem( DownloadCreateInfo* info, - const DownloadRequestHandle& request_handle)); - MOCK_METHOD4(CreateSavePackageDownloadItem, content::DownloadItem*( + const DownloadRequestHandle& request_handle) OVERRIDE; + virtual content::DownloadItem* CreateSavePackageDownloadItem( const FilePath& main_file_path, const GURL& page_url, bool is_otr, - content::DownloadItem::Observer* observer)); - MOCK_METHOD0(ClearLastDownloadPath, void()); - MOCK_METHOD2(FileSelected, void(const FilePath& path, void* params)); - MOCK_METHOD1(FileSelectionCanceled, void(void* params)); - MOCK_METHOD1(RestartDownload, void(int32 download_id)); - MOCK_METHOD0(CheckForHistoryFilesRemoval, void()); - MOCK_METHOD1(GetDownloadItem, content::DownloadItem*(int id)); - MOCK_METHOD1(SavePageDownloadFinished, void(content::DownloadItem* download)); - MOCK_METHOD1(GetActiveDownloadItem, content::DownloadItem*(int id)); - MOCK_METHOD0(GenerateFileHash, bool()); - MOCK_CONST_METHOD0(delegate, content::DownloadManagerDelegate*()); - MOCK_METHOD1(SetDownloadManagerDelegate, void( - content::DownloadManagerDelegate* delegate)); - MOCK_METHOD2(ContinueDownloadWithPath, void(content::DownloadItem* download, - const FilePath& chosen_file)); - MOCK_METHOD1(GetActiveDownload, content::DownloadItem*(int32 download_id)); - MOCK_METHOD1(SetFileManager, void(DownloadFileManager* file_manager)); + content::DownloadItem::Observer* observer) OVERRIDE; + virtual void ClearLastDownloadPath() OVERRIDE; + virtual void FileSelected(const FilePath& path, void* params) OVERRIDE; + virtual void FileSelectionCanceled(void* params) OVERRIDE; + virtual void RestartDownload(int32 download_id) OVERRIDE; + virtual void CheckForHistoryFilesRemoval() OVERRIDE; + virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE; + virtual void SavePageDownloadFinished( + content::DownloadItem* download) OVERRIDE; + virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; + virtual content::DownloadManagerDelegate* delegate() const OVERRIDE; + virtual void SetDownloadManagerDelegate( + content::DownloadManagerDelegate* delegate) OVERRIDE; + virtual void ContinueDownloadWithPath(content::DownloadItem* download, + const FilePath& chosen_file) OVERRIDE; + virtual content::DownloadItem* GetActiveDownload(int32 download_id) OVERRIDE; + virtual void SetFileManager(DownloadFileManager* file_manager) OVERRIDE; + + private: + content::DownloadManagerDelegate* delegate_; + DownloadIdFactory* id_factory_; + DownloadStatusUpdater* updater_; + DownloadFileManager* file_manager_; + std::map<int32, content::DownloadItem*> item_map_; + std::map<int32, content::DownloadItem*> inactive_item_map_; }; #endif // CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_MANAGER_H_ diff --git a/content/content_tests.gypi b/content/content_tests.gypi index 1cea1f5..78e8a96 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -1,4 +1,4 @@ -# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Copyright (c) 2011 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -198,7 +198,6 @@ 'browser/device_orientation/provider_unittest.cc', 'browser/download/base_file_unittest.cc', 'browser/download/download_buffer_unittest.cc', - 'browser/download/download_file_manager_unittest.cc', 'browser/download/download_file_unittest.cc', 'browser/download/download_id_unittest.cc', 'browser/download/download_query_unittest.cc', diff --git a/content/public/browser/download_manager.h b/content/public/browser/download_manager.h index 4d7cfb8..13e717a5 100644 --- a/content/public/browser/download_manager.h +++ b/content/public/browser/download_manager.h @@ -153,13 +153,13 @@ class CONTENT_EXPORT DownloadManager // Remove downloads after remove_begin (inclusive) and before remove_end // (exclusive). You may pass in null Time values to do an unbounded delete // in either direction. - virtual int RemoveDownloadsBetween(base::Time remove_begin, - base::Time remove_end) = 0; + virtual int RemoveDownloadsBetween(const base::Time remove_begin, + const base::Time remove_end) = 0; // Remove downloads will delete all downloads that have a timestamp that is // the same or more recent than |remove_begin|. The number of downloads // deleted is returned back to the caller. - virtual int RemoveDownloads(base::Time remove_begin) = 0; + virtual int RemoveDownloads(const base::Time remove_begin) = 0; // Remove all downloads will delete all downloads. The number of downloads // deleted is returned back to the caller. @@ -244,8 +244,6 @@ class CONTENT_EXPORT DownloadManager // yet in the history map. virtual DownloadItem* GetActiveDownloadItem(int id) = 0; - virtual bool GenerateFileHash() = 0; - virtual content::DownloadManagerDelegate* delegate() const = 0; // For testing only. May be called from tests indirectly (through |