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 "components/drive/file_system_core_util.h"
12 #include "content/public/test/mock_download_item.h"
13 #include "content/public/test/mock_download_manager.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_utils.h"
16 #include "google_apis/drive/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 // Test file system for verifying the behavior of DownloadHandler, by simulating
24 // various responses from FileSystem.
25 class DownloadHandlerTestFileSystem
: public DummyFileSystem
{
27 DownloadHandlerTestFileSystem() : error_(FILE_ERROR_FAILED
) {}
29 void set_error(FileError error
) { error_
= error
; }
31 // FileSystemInterface overrides.
32 void GetResourceEntry(const base::FilePath
& file_path
,
33 const GetResourceEntryCallback
& callback
) override
{
34 callback
.Run(error_
, scoped_ptr
<ResourceEntry
>(
35 error_
== FILE_ERROR_OK
? new ResourceEntry
: NULL
));
38 void CreateDirectory(const base::FilePath
& directory_path
,
41 const FileOperationCallback
& callback
) override
{
51 class DownloadHandlerTest
: public testing::Test
{
54 : download_manager_(new content::MockDownloadManager
) {}
56 void SetUp() override
{
57 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
59 // Set expectations for download item.
60 EXPECT_CALL(download_item_
, GetState())
61 .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS
));
63 download_handler_
.reset(new DownloadHandler(&test_file_system_
));
64 download_handler_
->Initialize(download_manager_
.get(), temp_dir_
.path());
68 base::ScopedTempDir temp_dir_
;
69 content::TestBrowserThreadBundle thread_bundle_
;
70 TestingProfile profile_
;
71 scoped_ptr
<content::MockDownloadManager
> download_manager_
;
72 DownloadHandlerTestFileSystem test_file_system_
;
73 scoped_ptr
<DownloadHandler
> download_handler_
;
74 content::MockDownloadItem download_item_
;
77 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathNonDrivePath
) {
78 const base::FilePath
non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
79 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path
));
81 // Call SubstituteDriveDownloadPath()
82 base::FilePath substituted_path
;
83 download_handler_
->SubstituteDriveDownloadPath(
86 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
87 content::RunAllBlockingPoolTasksUntilIdle();
90 EXPECT_EQ(non_drive_path
, substituted_path
);
91 EXPECT_FALSE(download_handler_
->IsDriveDownload(&download_item_
));
94 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPath
) {
95 const base::FilePath drive_path
=
96 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
98 // Test the case that the download target directory already exists.
99 test_file_system_
.set_error(FILE_ERROR_OK
);
101 // Call SubstituteDriveDownloadPath()
102 base::FilePath substituted_path
;
103 download_handler_
->SubstituteDriveDownloadPath(
106 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
107 content::RunAllBlockingPoolTasksUntilIdle();
110 EXPECT_TRUE(temp_dir_
.path().IsParent(substituted_path
));
111 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
112 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
115 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathGetEntryFailure
) {
116 const base::FilePath drive_path
=
117 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
119 // Test the case that access to the download target directory failed for some
121 test_file_system_
.set_error(FILE_ERROR_FAILED
);
123 // Call SubstituteDriveDownloadPath()
124 base::FilePath substituted_path
;
125 download_handler_
->SubstituteDriveDownloadPath(
128 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
129 content::RunAllBlockingPoolTasksUntilIdle();
132 EXPECT_TRUE(substituted_path
.empty());
135 // content::SavePackage calls SubstituteDriveDownloadPath before creating
137 TEST_F(DownloadHandlerTest
, SubstituteDriveDownloadPathForSavePackage
) {
138 const base::FilePath drive_path
=
139 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
140 test_file_system_
.set_error(FILE_ERROR_OK
);
142 // Call SubstituteDriveDownloadPath()
143 base::FilePath substituted_path
;
144 download_handler_
->SubstituteDriveDownloadPath(
146 NULL
, // DownloadItem is not available at this moment.
147 google_apis::test_util::CreateCopyResultCallback(&substituted_path
));
148 content::RunAllBlockingPoolTasksUntilIdle();
150 // Check the result of SubstituteDriveDownloadPath().
151 EXPECT_TRUE(temp_dir_
.path().IsParent(substituted_path
));
153 // |download_item_| is not a drive download yet.
154 EXPECT_FALSE(download_handler_
->IsDriveDownload(&download_item_
));
156 // Call SetDownloadParams().
157 download_handler_
->SetDownloadParams(drive_path
, &download_item_
);
159 // |download_item_| is a drive download now.
160 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
161 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
164 TEST_F(DownloadHandlerTest
, CheckForFileExistence
) {
165 const base::FilePath drive_path
=
166 util::GetDriveMountPointPath(&profile_
).AppendASCII("test.dat");
168 // Make |download_item_| a drive download.
169 download_handler_
->SetDownloadParams(drive_path
, &download_item_
);
170 ASSERT_TRUE(download_handler_
->IsDriveDownload(&download_item_
));
171 EXPECT_EQ(drive_path
, download_handler_
->GetTargetPath(&download_item_
));
173 // Test for the case when the path exists.
174 test_file_system_
.set_error(FILE_ERROR_OK
);
176 // Call CheckForFileExistence.
177 bool file_exists
= false;
178 download_handler_
->CheckForFileExistence(
180 google_apis::test_util::CreateCopyResultCallback(&file_exists
));
181 content::RunAllBlockingPoolTasksUntilIdle();
184 EXPECT_TRUE(file_exists
);
186 // Test for the case when the path does not exist.
187 test_file_system_
.set_error(FILE_ERROR_NOT_FOUND
);
189 // Call CheckForFileExistence again.
190 download_handler_
->CheckForFileExistence(
192 google_apis::test_util::CreateCopyResultCallback(&file_exists
));
193 content::RunAllBlockingPoolTasksUntilIdle();
196 EXPECT_FALSE(file_exists
);