1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/drive/download_handler.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "chrome/browser/chromeos/drive/dummy_file_system.h"
9 #include "chrome/browser/chromeos/drive/file_system_util.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "content/public/test/mock_download_item.h"
12 #include "content/public/test/mock_download_manager.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "google_apis/drive/test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
22 // Test file system for verifying the behavior of DownloadHandler, by simulating
23 // various responses from FileSystem.
24 class DownloadHandlerTestFileSystem
: public DummyFileSystem
{
26 DownloadHandlerTestFileSystem() : error_(FILE_ERROR_FAILED
) {}
28 void set_error(FileError error
) { error_
= error
; }
30 // FileSystemInterface overrides.
31 void GetResourceEntry(const base::FilePath
& file_path
,
32 const GetResourceEntryCallback
& callback
) override
{
33 callback
.Run(error_
, scoped_ptr
<ResourceEntry
>(
34 error_
== FILE_ERROR_OK
? new ResourceEntry
: NULL
));
37 void CreateDirectory(const base::FilePath
& directory_path
,
40 const FileOperationCallback
& callback
) override
{
50 class DownloadHandlerTest
: public testing::Test
{
53 : download_manager_(new content::MockDownloadManager
) {}
55 void SetUp() override
{
56 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
58 // Set expectations for download item.
59 EXPECT_CALL(download_item_
, GetState())
60 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS
));
62 download_handler_
.reset(new DownloadHandler(&test_file_system_
));
63 download_handler_
->Initialize(download_manager_
.get(), temp_dir_
.path());
67 base::ScopedTempDir temp_dir_
;
68 content::TestBrowserThreadBundle thread_bundle_
;
69 TestingProfile profile_
;
70 scoped_ptr
<content::MockDownloadManager
> download_manager_
;
71 DownloadHandlerTestFileSystem test_file_system_
;
72 scoped_ptr
<DownloadHandler
> download_handler_
;
73 content::MockDownloadItem download_item_
;
76 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathNonDrivePath
) {
77 const base::FilePath
non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
78 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path
));
80 // Call SubstituteDriveDownloadPath()
81 base::FilePath substituted_path
;
82 download_handler_
->SubstituteDriveDownloadPath(
85 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
86 content::RunAllBlockingPoolTasksUntilIdle();
89 EXPECT_EQ(non_drive_path
, substituted_path
);
90 EXPECT_FALSE(download_handler_
->IsDriveDownload(&download_item_
));
93 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPath
) {
94 const base::FilePath drive_path
=
95 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
97 // Test the case that the download target directory already exists.
98 test_file_system_
.set_error(FILE_ERROR_OK
);
100 // Call SubstituteDriveDownloadPath()
101 base::FilePath substituted_path
;
102 download_handler_
->SubstituteDriveDownloadPath(
105 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
106 content::RunAllBlockingPoolTasksUntilIdle();
109 EXPECT_TRUE(temp_dir_
.path().IsParent(substituted_path
));
110 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
111 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
114 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathGetEntryFailure
) {
115 const base::FilePath drive_path
=
116 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
118 // Test the case that access to the download target directory failed for some
120 test_file_system_
.set_error(FILE_ERROR_FAILED
);
122 // Call SubstituteDriveDownloadPath()
123 base::FilePath substituted_path
;
124 download_handler_
->SubstituteDriveDownloadPath(
127 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
128 content::RunAllBlockingPoolTasksUntilIdle();
131 EXPECT_TRUE(substituted_path
.empty());
134 // content::SavePackage calls SubstituteDriveDownloadPath before creating
136 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathForSavePackage
) {
137 const base::FilePath drive_path
=
138 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
139 test_file_system_
.set_error(FILE_ERROR_OK
);
141 // Call SubstituteDriveDownloadPath()
142 base::FilePath substituted_path
;
143 download_handler_
->SubstituteDriveDownloadPath(
145 NULL
, // DownloadItem is not available at this moment.
146 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
147 content::RunAllBlockingPoolTasksUntilIdle();
149 // Check the result of SubstituteDriveDownloadPath().
150 EXPECT_TRUE(temp_dir_
.path().IsParent(substituted_path
));
152 // |download_item_| is not a drive download yet.
153 EXPECT_FALSE(download_handler_
->IsDriveDownload(&download_item_
));
155 // Call SetDownloadParams().
156 download_handler_
->SetDownloadParams(drive_path
, &download_item_
);
158 // |download_item_| is a drive download now.
159 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
160 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
163 TEST_F(DownloadHandlerTest
, CheckForFileExistence
) {
164 const base::FilePath drive_path
=
165 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
167 // Make |download_item_| a drive download.
168 download_handler_
->SetDownloadParams(drive_path
, &download_item_
);
169 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
170 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
172 // Test for the case when the path exists.
173 test_file_system_
.set_error(FILE_ERROR_OK
);
175 // Call CheckForFileExistence.
176 bool file_exists
= false;
177 download_handler_
->CheckForFileExistence(
179 google_apis::test_util::CreateCopyResultCallback(&file_exists
));
180 content::RunAllBlockingPoolTasksUntilIdle();
183 EXPECT_TRUE(file_exists
);
185 // Test for the case when the path does not exist.
186 test_file_system_
.set_error(FILE_ERROR_NOT_FOUND
);
188 // Call CheckForFileExistence again.
189 download_handler_
->CheckForFileExistence(
191 google_apis::test_util::CreateCopyResultCallback(&file_exists
));
192 content::RunAllBlockingPoolTasksUntilIdle();
195 EXPECT_FALSE(file_exists
);