Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / download_handler_unittest.cc
blob8cee158618aa149ed26f0b13de1103c4e44e6a43
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/browser/chromeos/drive/test_util.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 "testing/gtest/include/gtest/gtest.h"
16 namespace drive {
18 namespace {
20 // Test file system for verifying the behavior of DownloadHandler, by simulating
21 // various responses from FileSystem.
22 class DownloadHandlerTestFileSystem : public DummyFileSystem {
23 public:
24 DownloadHandlerTestFileSystem() : error_(FILE_ERROR_FAILED) {}
26 void set_error(FileError error) { error_ = error; }
28 // FileSystemInterface overrides.
29 virtual void GetResourceEntry(
30 const base::FilePath& file_path,
31 const GetResourceEntryCallback& callback) OVERRIDE {
32 callback.Run(error_, scoped_ptr<ResourceEntry>(
33 error_ == FILE_ERROR_OK ? new ResourceEntry : NULL));
36 virtual void CreateDirectory(
37 const base::FilePath& directory_path,
38 bool is_exclusive,
39 bool is_recursive,
40 const FileOperationCallback& callback) OVERRIDE {
41 callback.Run(error_);
44 private:
45 FileError error_;
48 } // namespace
50 class DownloadHandlerTest : public testing::Test {
51 public:
52 DownloadHandlerTest()
53 : download_manager_(new content::MockDownloadManager) {}
55 virtual 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());
66 protected:
67 base::ScopedTempDir temp_dir_;
68 content::TestBrowserThreadBundle thread_bundle_;
69 scoped_ptr<content::MockDownloadManager> download_manager_;
70 DownloadHandlerTestFileSystem test_file_system_;
71 scoped_ptr<DownloadHandler> download_handler_;
72 content::MockDownloadItem download_item_;
75 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
76 const base::FilePath non_drive_path(FILE_PATH_LITERAL("/foo/bar"));
77 ASSERT_FALSE(util::IsUnderDriveMountPoint(non_drive_path));
79 // Call SubstituteDriveDownloadPath()
80 base::FilePath substituted_path;
81 download_handler_->SubstituteDriveDownloadPath(
82 non_drive_path,
83 &download_item_,
84 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
85 test_util::RunBlockingPoolTask();
87 // Check the result.
88 EXPECT_EQ(non_drive_path, substituted_path);
89 EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
92 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPath) {
93 const base::FilePath drive_path =
94 util::GetDriveMountPointPath().AppendASCII("test.dat");
96 // Test the case that the download target directory already exists.
97 test_file_system_.set_error(FILE_ERROR_OK);
99 // Call SubstituteDriveDownloadPath()
100 base::FilePath substituted_path;
101 download_handler_->SubstituteDriveDownloadPath(
102 drive_path,
103 &download_item_,
104 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
105 test_util::RunBlockingPoolTask();
107 // Check the result.
108 EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
109 ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
110 EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
113 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathGetEntryFailure) {
114 const base::FilePath drive_path =
115 util::GetDriveMountPointPath().AppendASCII("test.dat");
117 // Test the case that access to the download target directory failed for some
118 // reason.
119 test_file_system_.set_error(FILE_ERROR_FAILED);
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 test_util::RunBlockingPoolTask();
129 // Check the result.
130 EXPECT_TRUE(substituted_path.empty());
133 // content::SavePackage calls SubstituteDriveDownloadPath before creating
134 // DownloadItem.
135 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathForSavePackage) {
136 const base::FilePath drive_path =
137 util::GetDriveMountPointPath().AppendASCII("test.dat");
138 test_file_system_.set_error(FILE_ERROR_OK);
140 // Call SubstituteDriveDownloadPath()
141 base::FilePath substituted_path;
142 download_handler_->SubstituteDriveDownloadPath(
143 drive_path,
144 NULL, // DownloadItem is not available at this moment.
145 google_apis::test_util::CreateCopyResultCallback(&substituted_path));
146 test_util::RunBlockingPoolTask();
148 // Check the result of SubstituteDriveDownloadPath().
149 EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
151 // |download_item_| is not a drive download yet.
152 EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
154 // Call SetDownloadParams().
155 download_handler_->SetDownloadParams(drive_path, &download_item_);
157 // |download_item_| is a drive download now.
158 ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
159 EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
162 TEST_F(DownloadHandlerTest, CheckForFileExistence) {
163 const base::FilePath drive_path =
164 util::GetDriveMountPointPath().AppendASCII("test.dat");
166 // Make |download_item_| a drive download.
167 download_handler_->SetDownloadParams(drive_path, &download_item_);
168 ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
169 EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
171 // Test for the case when the path exists.
172 test_file_system_.set_error(FILE_ERROR_OK);
174 // Call CheckForFileExistence.
175 bool file_exists = false;
176 download_handler_->CheckForFileExistence(
177 &download_item_,
178 google_apis::test_util::CreateCopyResultCallback(&file_exists));
179 test_util::RunBlockingPoolTask();
181 // Check the result.
182 EXPECT_TRUE(file_exists);
184 // Test for the case when the path does not exist.
185 test_file_system_.set_error(FILE_ERROR_NOT_FOUND);
187 // Call CheckForFileExistence again.
188 download_handler_->CheckForFileExistence(
189 &download_item_,
190 google_apis::test_util::CreateCopyResultCallback(&file_exists));
191 test_util::RunBlockingPoolTask();
193 // Check the result.
194 EXPECT_FALSE(file_exists);
197 } // namespace drive