Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / download_handler_unittest.cc
blob52bde9123f12669dcd1911528fab184285e241ac
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/file_system_util.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "components/drive/dummy_file_system.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"
19 namespace drive {
21 namespace {
23 // Test file system for verifying the behavior of DownloadHandler, by simulating
24 // various responses from FileSystem.
25 class DownloadHandlerTestFileSystem : public DummyFileSystem {
26 public:
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,
39 bool is_exclusive,
40 bool is_recursive,
41 const FileOperationCallback& callback) override {
42 callback.Run(error_);
45 private:
46 FileError error_;
49 } // namespace
51 class DownloadHandlerTest : public testing::Test {
52 public:
53 DownloadHandlerTest()
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());
67 protected:
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(
84 non_drive_path,
85 &download_item_,
86 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
87 content::RunAllBlockingPoolTasksUntilIdle();
89 // Check the result.
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(
104 drive_path,
105 &download_item_,
106 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
107 content::RunAllBlockingPoolTasksUntilIdle();
109 // Check the result.
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
120 // reason.
121 test_file_system_.set_error(FILE_ERROR_FAILED);
123 // Call SubstituteDriveDownloadPath()
124 base::FilePath substituted_path;
125 download_handler_->SubstituteDriveDownloadPath(
126 drive_path,
127 &download_item_,
128 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
129 content::RunAllBlockingPoolTasksUntilIdle();
131 // Check the result.
132 EXPECT_TRUE(substituted_path.empty());
135 // content::SavePackage calls SubstituteDriveDownloadPath before creating
136 // DownloadItem.
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(
145 drive_path,
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(
179 &download_item_,
180 google_apis::test_util::CreateCopyResultCallback(&file_exists));
181 content::RunAllBlockingPoolTasksUntilIdle();
183 // Check the result.
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(
191 &download_item_,
192 google_apis::test_util::CreateCopyResultCallback(&file_exists));
193 content::RunAllBlockingPoolTasksUntilIdle();
195 // Check the result.
196 EXPECT_FALSE(file_exists);
199 } // namespace drive