Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / download_handler_unittest.cc
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/test_util.h"
11 #include "chrome/test/base/testing_profile.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/gtest/include/gtest/gtest.h"
16
17 namespace drive {
18
19 namespace {
20
21 // Test file system for verifying the behavior of DownloadHandler, by simulating
22 // various responses from FileSystem.
23 class DownloadHandlerTestFileSystem : public DummyFileSystem {
24  public:
25   DownloadHandlerTestFileSystem() : error_(FILE_ERROR_FAILED) {}
26
27   void set_error(FileError error) { error_ = error; }
28
29   // FileSystemInterface overrides.
30   virtual void GetResourceEntry(
31       const base::FilePath& file_path,
32       const GetResourceEntryCallback& callback) OVERRIDE {
33     callback.Run(error_, scoped_ptr<ResourceEntry>(
34         error_ == FILE_ERROR_OK ? new ResourceEntry : NULL));
35   }
36
37   virtual void CreateDirectory(
38       const base::FilePath& directory_path,
39       bool is_exclusive,
40       bool is_recursive,
41       const FileOperationCallback& callback) OVERRIDE {
42     callback.Run(error_);
43   }
44
45  private:
46   FileError error_;
47 };
48
49 }  // namespace
50
51 class DownloadHandlerTest : public testing::Test {
52  public:
53   DownloadHandlerTest()
54       : download_manager_(new content::MockDownloadManager) {}
55
56   virtual void SetUp() OVERRIDE {
57     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
58
59     // Set expectations for download item.
60     EXPECT_CALL(download_item_, GetState())
61         .WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
62
63     download_handler_.reset(new DownloadHandler(&test_file_system_));
64     download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
65   }
66
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_;
75 };
76
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));
80
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   test_util::RunBlockingPoolTask();
88
89   // Check the result.
90   EXPECT_EQ(non_drive_path, substituted_path);
91   EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
92 }
93
94 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPath) {
95   const base::FilePath drive_path =
96       util::GetDriveMountPointPath(&profile_).AppendASCII("test.dat");
97
98   // Test the case that the download target directory already exists.
99   test_file_system_.set_error(FILE_ERROR_OK);
100
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   test_util::RunBlockingPoolTask();
108
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_));
113 }
114
115 TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathGetEntryFailure) {
116   const base::FilePath drive_path =
117       util::GetDriveMountPointPath(&profile_).AppendASCII("test.dat");
118
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);
122
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   test_util::RunBlockingPoolTask();
130
131   // Check the result.
132   EXPECT_TRUE(substituted_path.empty());
133 }
134
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);
141
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   test_util::RunBlockingPoolTask();
149
150   // Check the result of SubstituteDriveDownloadPath().
151   EXPECT_TRUE(temp_dir_.path().IsParent(substituted_path));
152
153   // |download_item_| is not a drive download yet.
154   EXPECT_FALSE(download_handler_->IsDriveDownload(&download_item_));
155
156   // Call SetDownloadParams().
157   download_handler_->SetDownloadParams(drive_path, &download_item_);
158
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_));
162 }
163
164 TEST_F(DownloadHandlerTest, CheckForFileExistence) {
165   const base::FilePath drive_path =
166       util::GetDriveMountPointPath(&profile_).AppendASCII("test.dat");
167
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_));
172
173   // Test for the case when the path exists.
174   test_file_system_.set_error(FILE_ERROR_OK);
175
176   // Call CheckForFileExistence.
177   bool file_exists = false;
178   download_handler_->CheckForFileExistence(
179       &download_item_,
180       google_apis::test_util::CreateCopyResultCallback(&file_exists));
181   test_util::RunBlockingPoolTask();
182
183   // Check the result.
184   EXPECT_TRUE(file_exists);
185
186   // Test for the case when the path does not exist.
187   test_file_system_.set_error(FILE_ERROR_NOT_FOUND);
188
189   // Call CheckForFileExistence again.
190   download_handler_->CheckForFileExistence(
191       &download_item_,
192       google_apis::test_util::CreateCopyResultCallback(&file_exists));
193   test_util::RunBlockingPoolTask();
194
195   // Check the result.
196   EXPECT_FALSE(file_exists);
197 }
198
199 }  // namespace drive