download_handler_unittest.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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.
4
5#include "chrome/browser/chromeos/drive/download_handler.h"
6
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/browser/chromeos/drive/file_write_helper.h"
11#include "chrome/browser/google_apis/test_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 "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace drive {
19
20namespace {
21
22// Flags to control the state of the test file system.
23enum DownloadPathState {
24  // Simulates the state that requested path just simply exists.
25  PATH_EXISTS,
26  // Simulates the state that requested path fails to be accessed.
27  PATH_INVALID,
28  // Simulates the state that the requested path does not exist.
29  PATH_NOT_EXIST,
30  // Simulates the state that the path does not exist nor be able to be created.
31  PATH_NOT_EXIST_AND_CREATE_FAIL,
32};
33
34// Test file system for verifying the behavior of DownloadHandler, by simulating
35// various responses from FileSystem.
36class DownloadHandlerTestFileSystem : public DummyFileSystem {
37 public:
38  DownloadHandlerTestFileSystem() : state_(PATH_INVALID) {}
39
40  void set_download_path_state(DownloadPathState state) { state_ = state; }
41
42  // FileSystemInterface overrides.
43  virtual void GetResourceEntryByPath(
44      const base::FilePath& file_path,
45      const GetResourceEntryCallback& callback) OVERRIDE {
46    if (state_ == PATH_EXISTS) {
47      callback.Run(FILE_ERROR_OK, make_scoped_ptr(new ResourceEntry));
48      return;
49    }
50    callback.Run(
51        state_ == PATH_INVALID ? FILE_ERROR_FAILED : FILE_ERROR_NOT_FOUND,
52        scoped_ptr<ResourceEntry>());
53 }
54
55 virtual void CreateDirectory(
56     const base::FilePath& directory_path,
57     bool is_exclusive,
58     bool is_recursive,
59     const FileOperationCallback& callback) OVERRIDE {
60   callback.Run(state_ == PATH_NOT_EXIST ? FILE_ERROR_OK : FILE_ERROR_FAILED);
61 }
62
63 private:
64  DownloadPathState state_;
65};
66
67}  // namespace
68
69class DownloadHandlerTest : public testing::Test {
70 public:
71  DownloadHandlerTest()
72      : download_manager_(new content::MockDownloadManager) {}
73
74  virtual void SetUp() OVERRIDE {
75    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
76
77    // Set expectations for download item.
78    EXPECT_CALL(download_item_, GetState())
79        .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
80
81    file_write_helper_.reset(new FileWriteHelper(&test_file_system_));
82    download_handler_.reset(
83        new DownloadHandler(file_write_helper_.get(), &test_file_system_));
84    download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
85  }
86
87 protected:
88  base::ScopedTempDir temp_dir_;
89  content::TestBrowserThreadBundle thread_bundle_;
90  scoped_ptr<content::MockDownloadManager> download_manager_;
91  DownloadHandlerTestFileSystem test_file_system_;
92  scoped_ptr<FileWriteHelper> file_write_helper_;
93  scoped_ptr<DownloadHandler> download_handler_;
94  content::MockDownloadItem download_item_;
95};
96
97TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
98  const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
99  ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path));
100
101  // Call SubstituteDriveDownloadPath()
102  base::FilePath substituted_path;
103  download_handler_->SubstituteDriveDownloadPath(
104      non_drive_path,
105      &download_item_,
106      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
107  google_apis::test_util::RunBlockingPoolTask();
108
109  // Check the result.
110  EXPECT_EQ(non_drive_path, substituted_path);
111  EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
112}
113
114TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPath) {
115  const base::FilePath drive_path =
116      util::GetDriveMountPointPath().AppendASCII("test.dat");
117
118  // Test the case that the download target directory already exists.
119  test_file_system_.set_download_path_state(PATH_EXISTS);
120
121  // Call SubstituteDriveDownloadPath()
122  base::FilePath substituted_path;
123  download_handler_->SubstituteDriveDownloadPath(
124      drive_path,
125      &download_item_,
126      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
127  google_apis::test_util::RunBlockingPoolTask();
128
129  // Check the result.
130  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
131  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
132  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
133}
134
135TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathGetEntryFailure) {
136  const base::FilePath drive_path =
137      util::GetDriveMountPointPath().AppendASCII("test.dat");
138
139  // Test the case that access to the download target directory failed for some
140  // reason.
141  test_file_system_.set_download_path_state(PATH_INVALID);
142
143  // Call SubstituteDriveDownloadPath()
144  base::FilePath substituted_path;
145  download_handler_->SubstituteDriveDownloadPath(
146      drive_path,
147      &download_item_,
148      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
149  google_apis::test_util::RunBlockingPoolTask();
150
151  // Check the result.
152  EXPECT_TRUE(substituted_path.empty());
153}
154
155TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathCreateDirectory) {
156  const base::FilePath drive_path =
157      util::GetDriveMountPointPath().AppendASCII("test.dat");
158
159  // Test the case that access to the download target directory does not exist,
160  // and thus will be created in DownloadHandler.
161  test_file_system_.set_download_path_state(PATH_NOT_EXIST);
162
163  // Call SubstituteDriveDownloadPath()
164  base::FilePath substituted_path;
165  download_handler_->SubstituteDriveDownloadPath(
166      drive_path,
167      &download_item_,
168      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
169  google_apis::test_util::RunBlockingPoolTask();
170
171  // Check the result.
172  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
173  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
174  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
175}
176
177TEST_F(DownloadHandlerTest,
178       SubstituteDriveDownloadPathCreateDirectoryFailure) {
179  const base::FilePath drive_path =
180      util::GetDriveMountPointPath().AppendASCII("test.dat");
181
182  // Test the case that access to the download target directory does not exist,
183  // and creation fails for some reason.
184  test_file_system_.set_download_path_state(PATH_NOT_EXIST_AND_CREATE_FAIL);
185
186  // Call SubstituteDriveDownloadPath()
187  base::FilePath substituted_path;
188  download_handler_->SubstituteDriveDownloadPath(
189      drive_path,
190      &download_item_,
191      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
192  google_apis::test_util::RunBlockingPoolTask();
193
194  // Check the result.
195  EXPECT_TRUE(substituted_path.empty());
196}
197
198// content::SavePackage calls SubstituteDriveDownloadPath before creating
199// DownloadItem.
200TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathForSavePackage) {
201  const base::FilePath drive_path =
202      util::GetDriveMountPointPath().AppendASCII("test.dat");
203  test_file_system_.set_download_path_state(PATH_EXISTS);
204
205  // Call SubstituteDriveDownloadPath()
206  base::FilePath substituted_path;
207  download_handler_->SubstituteDriveDownloadPath(
208      drive_path,
209      NULL,  // DownloadItem is not available at this moment.
210      google_apis::test_util::CreateCopyResultCallback(&substituted_path));
211  google_apis::test_util::RunBlockingPoolTask();
212
213  // Check the result of SubstituteDriveDownloadPath().
214  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
215
216  // |download_item_| is not a drive download yet.
217  EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
218
219  // Call SetDownloadParams().
220  download_handler_->SetDownloadParams(drive_path, &download_item_);
221
222  // |download_item_| is a drive download now.
223  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
224  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
225}
226
227TEST_F(DownloadHandlerTest, CheckForFileExistence) {
228  const base::FilePath drive_path =
229      util::GetDriveMountPointPath().AppendASCII("test.dat");
230
231  // Make |download_item_| a drive download.
232  download_handler_->SetDownloadParams(drive_path, &download_item_);
233  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
234  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
235
236  // Test for the case when the path exists.
237  test_file_system_.set_download_path_state(PATH_EXISTS);
238
239  // Call CheckForFileExistence.
240  bool file_exists = false;
241  download_handler_->CheckForFileExistence(
242      &download_item_,
243      google_apis::test_util::CreateCopyResultCallback(&file_exists));
244  google_apis::test_util::RunBlockingPoolTask();
245
246  // Check the result.
247  EXPECT_TRUE(file_exists);
248
249  // Test for the case when the path does not exist.
250  test_file_system_.set_download_path_state(PATH_NOT_EXIST);
251
252  // Call CheckForFileExistence again.
253  download_handler_->CheckForFileExistence(
254      &download_item_,
255      google_apis::test_util::CreateCopyResultCallback(&file_exists));
256  google_apis::test_util::RunBlockingPoolTask();
257
258  // Check the result.
259  EXPECT_FALSE(file_exists);
260}
261
262}  // namespace drive
263