download_handler_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 "base/message_loop.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/chromeos/drive/mock_file_system.h"
12#include "chrome/browser/google_apis/test_util.h"
13#include "content/public/test/mock_download_item.h"
14#include "content/public/test/mock_download_manager.h"
15#include "content/public/test/test_browser_thread.h"
16#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19using ::testing::Return;
20using ::testing::SaveArg;
21using ::testing::_;
22
23namespace drive {
24
25namespace {
26
27// Copies |file_path| to |out_file_path|, is used as a
28// SubstituteDriveDownloadPathCallback.
29void CopySubstituteDriveDownloadPathResult(base::FilePath* out_file_path,
30                                           const base::FilePath& file_path) {
31  *out_file_path = file_path;
32}
33
34// Copies |value| to |out|, is used as a content::CheckForFileExistenceCallback.
35void CopyCheckForFileExistenceResult(bool* out, bool value) {
36  *out = value;
37}
38
39}  // namespace
40
41class DownloadHandlerTest : public testing::Test {
42 public:
43  DownloadHandlerTest()
44      : ui_thread_(content::BrowserThread::UI, &message_loop_),
45        download_manager_(new content::MockDownloadManager) {}
46
47  virtual void SetUp() OVERRIDE {
48    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49
50    // Set expectations for download item.
51    EXPECT_CALL(download_item_, GetState())
52        .WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
53
54    // Set expectations for file system to save argument callbacks.
55    EXPECT_CALL(file_system_, GetEntryInfoByPath(_, _))
56        .WillRepeatedly(SaveArg<1>(&get_entry_info_callback_));
57    EXPECT_CALL(file_system_, CreateDirectory(_, _, _, _))
58        .WillRepeatedly(SaveArg<3>(&create_directory_callback_));
59
60    file_write_helper_.reset(new FileWriteHelper(&file_system_));
61    download_handler_.reset(
62        new DownloadHandler(file_write_helper_.get(), &file_system_));
63    download_handler_->Initialize(download_manager_, temp_dir_.path());
64  }
65
66  virtual void TearDown() OVERRIDE {
67  }
68
69 protected:
70  base::ScopedTempDir temp_dir_;
71  MessageLoopForUI message_loop_;
72  content::TestBrowserThread ui_thread_;
73  scoped_refptr<content::MockDownloadManager> download_manager_;
74  MockFileSystem file_system_;
75  scoped_ptr<FileWriteHelper> file_write_helper_;
76  scoped_ptr<DownloadHandler> download_handler_;
77  content::MockDownloadItem download_item_;
78
79  // Argument callbacks passed to the file system.
80  GetEntryInfoCallback get_entry_info_callback_;
81  FileOperationCallback create_directory_callback_;
82};
83
84TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
85  const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
86  ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path));
87
88  // Call SubstituteDriveDownloadPath()
89  base::FilePath substituted_path;
90  download_handler_->SubstituteDriveDownloadPath(
91      non_drive_path,
92      &download_item_,
93      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
94  google_apis::test_util::RunBlockingPoolTask();
95
96  // Check the result.
97  EXPECT_EQ(non_drive_path, substituted_path);
98  EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
99}
100
101TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPath) {
102  const base::FilePath drive_path =
103      util::GetDriveMountPointPath().AppendASCII("test.dat");
104
105  // Call SubstituteDriveDownloadPath()
106  base::FilePath substituted_path;
107  download_handler_->SubstituteDriveDownloadPath(
108      drive_path,
109      &download_item_,
110      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
111  google_apis::test_util::RunBlockingPoolTask();
112
113  // Return result of GetEntryInfoByPath(), destination directory found.
114  scoped_ptr<ResourceEntry> entry(new ResourceEntry);
115  ASSERT_FALSE(get_entry_info_callback_.is_null());
116  get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
117  google_apis::test_util::RunBlockingPoolTask();
118
119  // Check the result.
120  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
121  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
122  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
123}
124
125TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathGetEntryFailure) {
126  const base::FilePath drive_path =
127      util::GetDriveMountPointPath().AppendASCII("test.dat");
128
129  // Call SubstituteDriveDownloadPath()
130  base::FilePath substituted_path;
131  download_handler_->SubstituteDriveDownloadPath(
132      drive_path,
133      &download_item_,
134      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
135  google_apis::test_util::RunBlockingPoolTask();
136
137  // Return result of GetEntryInfoByPath(), failing for some reason.
138  ASSERT_FALSE(get_entry_info_callback_.is_null());
139  get_entry_info_callback_.Run(FILE_ERROR_FAILED,
140                              scoped_ptr<ResourceEntry>());
141  google_apis::test_util::RunBlockingPoolTask();
142
143  // Check the result.
144  EXPECT_TRUE(substituted_path.empty());
145}
146
147TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathCreateDirectory) {
148  const base::FilePath drive_path =
149      util::GetDriveMountPointPath().AppendASCII("test.dat");
150
151  // Call SubstituteDriveDownloadPath()
152  base::FilePath substituted_path;
153  download_handler_->SubstituteDriveDownloadPath(
154      drive_path,
155      &download_item_,
156      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
157  google_apis::test_util::RunBlockingPoolTask();
158
159  // Return result of GetEntryInfoByPath(), destination directory not found.
160  ASSERT_FALSE(get_entry_info_callback_.is_null());
161  get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
162                              scoped_ptr<ResourceEntry>());
163  google_apis::test_util::RunBlockingPoolTask();
164
165  // Return result of CreateDirecotry().
166  ASSERT_FALSE(create_directory_callback_.is_null());
167  create_directory_callback_.Run(FILE_ERROR_OK);
168  google_apis::test_util::RunBlockingPoolTask();
169
170  // Check the result.
171  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
172  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
173  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
174}
175
176TEST_F(DownloadHandlerTest,
177       SubstituteDriveDownloadPathCreateDirectoryFailure) {
178  const base::FilePath drive_path =
179      util::GetDriveMountPointPath().AppendASCII("test.dat");
180
181  // Call SubstituteDriveDownloadPath()
182  base::FilePath substituted_path;
183  download_handler_->SubstituteDriveDownloadPath(
184      drive_path,
185      &download_item_,
186      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
187  google_apis::test_util::RunBlockingPoolTask();
188
189  // Return result of GetEntryInfoByPath(), destination directory not found.
190  ASSERT_FALSE(get_entry_info_callback_.is_null());
191  get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
192                              scoped_ptr<ResourceEntry>());
193  google_apis::test_util::RunBlockingPoolTask();
194
195  // Return result of CreateDirecotry().
196  ASSERT_FALSE(create_directory_callback_.is_null());
197  create_directory_callback_.Run(FILE_ERROR_FAILED);
198  google_apis::test_util::RunBlockingPoolTask();
199
200  // Check the result.
201  EXPECT_TRUE(substituted_path.empty());
202}
203
204// content::SavePackage calls SubstituteDriveDownloadPath before creating
205// DownloadItem.
206TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathForSavePackage) {
207  const base::FilePath drive_path =
208      util::GetDriveMountPointPath().AppendASCII("test.dat");
209
210  // Call SubstituteDriveDownloadPath()
211  base::FilePath substituted_path;
212  download_handler_->SubstituteDriveDownloadPath(
213      drive_path,
214      NULL,  // DownloadItem is not available at this moment.
215      base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
216  google_apis::test_util::RunBlockingPoolTask();
217
218  // Return result of GetEntryInfoByPath(), destination directory found.
219  scoped_ptr<ResourceEntry> entry(new ResourceEntry);
220  ASSERT_FALSE(get_entry_info_callback_.is_null());
221  get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
222  google_apis::test_util::RunBlockingPoolTask();
223
224  // Check the result of SubstituteDriveDownloadPath().
225  EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
226
227  // |download_item_| is not a drive download yet.
228  EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
229
230  // Call SetDownloadParams().
231  download_handler_->SetDownloadParams(drive_path, &download_item_);
232
233  // |download_item_| is a drive download now.
234  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
235  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
236}
237
238TEST_F(DownloadHandlerTest, CheckForFileExistence) {
239  const base::FilePath drive_path =
240      util::GetDriveMountPointPath().AppendASCII("test.dat");
241
242  // Make |download_item_| a drive download.
243  download_handler_->SetDownloadParams(drive_path, &download_item_);
244  ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
245  EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
246
247  // Call CheckForFileExistence.
248  bool file_exists = false;
249  download_handler_->CheckForFileExistence(
250      &download_item_,
251      base::Bind(&CopyCheckForFileExistenceResult, &file_exists));
252  google_apis::test_util::RunBlockingPoolTask();
253
254  // Return result of GetEntryInfoByPath(), file exists.
255  {
256    scoped_ptr<ResourceEntry> entry(new ResourceEntry);
257    ASSERT_FALSE(get_entry_info_callback_.is_null());
258    get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
259  }
260  google_apis::test_util::RunBlockingPoolTask();
261
262  // Check the result.
263  EXPECT_TRUE(file_exists);
264
265  // Reset callback to call CheckForFileExistence again.
266  get_entry_info_callback_.Reset();
267
268  // Call CheckForFileExistence again.
269  download_handler_->CheckForFileExistence(
270      &download_item_,
271      base::Bind(&CopyCheckForFileExistenceResult, &file_exists));
272  google_apis::test_util::RunBlockingPoolTask();
273
274  // Return result of GetEntryInfoByPath(), file does not exist.
275  ASSERT_FALSE(get_entry_info_callback_.is_null());
276  get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
277                               scoped_ptr<ResourceEntry>());
278  google_apis::test_util::RunBlockingPoolTask();
279
280  // Check the result.
281  EXPECT_FALSE(file_exists);
282}
283
284}  // namespace drive
285