1 // Copyright 2014 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 "base/path_service.h"
6 #include "chrome/browser/apps/app_browsertest_util.h"
7 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
8 #include "chrome/browser/chromeos/drive/file_system_interface.h"
9 #include "chrome/browser/chromeos/drive/file_system_util.h"
10 #include "chrome/browser/drive/fake_drive_service.h"
11 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
12 #include "chrome/browser/extensions/component_loader.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "content/public/test/test_utils.h"
15 #include "google_apis/drive/drive_api_parser.h"
16 #include "google_apis/drive/test_util.h"
18 namespace extensions {
20 // This class contains chrome.filesystem API test specific to Chrome OS, namely,
21 // the integrated Google Drive support.
22 class FileSystemApiTestForDrive : public PlatformAppBrowserTest {
24 FileSystemApiTestForDrive()
25 : fake_drive_service_(NULL),
26 integration_service_(NULL) {
29 // Sets up fake Drive service for tests (this has to be injected before the
30 // real DriveIntegrationService instance is created.)
31 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
32 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
33 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
35 ASSERT_TRUE(test_cache_root_.CreateUniqueTempDir());
37 create_drive_integration_service_ =
38 base::Bind(&FileSystemApiTestForDrive::CreateDriveIntegrationService,
39 base::Unretained(this));
40 service_factory_for_test_.reset(
41 new drive::DriveIntegrationServiceFactory::ScopedFactoryForTest(
42 &create_drive_integration_service_));
45 // Ensure the fake service's data is fetch in the local file system. This is
46 // necessary because the fetch starts lazily upon the first read operation.
47 virtual void SetUpOnMainThread() OVERRIDE {
48 PlatformAppBrowserTest::SetUpOnMainThread();
50 scoped_ptr<drive::ResourceEntry> entry;
51 drive::FileError error = drive::FILE_ERROR_FAILED;
52 integration_service_->file_system()->GetResourceEntry(
53 base::FilePath::FromUTF8Unsafe("drive/root"), // whatever
54 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
55 content::RunAllBlockingPoolTasksUntilIdle();
56 ASSERT_EQ(drive::FILE_ERROR_OK, error);
59 virtual void TearDown() OVERRIDE {
60 FileSystemChooseEntryFunction::StopSkippingPickerForTest();
61 PlatformAppBrowserTest::TearDown();
65 drive::DriveIntegrationService* CreateDriveIntegrationService(
67 fake_drive_service_ = new drive::FakeDriveService;
68 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json");
70 SetUpTestFileHierarchy();
72 integration_service_ = new drive::DriveIntegrationService(
73 profile, NULL, fake_drive_service_, std::string(),
74 test_cache_root_.path(), NULL);
75 return integration_service_;
78 void SetUpTestFileHierarchy() {
79 const std::string root = fake_drive_service_->GetRootResourceId();
80 ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", root));
81 ASSERT_TRUE(AddTestFile("open_existing1.txt", "Can you see me?", root));
82 ASSERT_TRUE(AddTestFile("open_existing2.txt", "Can you see me?", root));
83 ASSERT_TRUE(AddTestFile("save_existing.txt", "Can you see me?", root));
84 const std::string subdir = AddTestDirectory("subdir", root);
85 ASSERT_FALSE(subdir.empty());
86 ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", subdir));
89 bool AddTestFile(const std::string& title,
90 const std::string& data,
91 const std::string& parent_id) {
92 scoped_ptr<google_apis::FileResource> entry;
93 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
94 fake_drive_service_->AddNewFile(
95 "text/plain", data, parent_id, title, false,
96 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
97 content::RunAllPendingInMessageLoop();
98 return error == google_apis::HTTP_CREATED && entry;
101 std::string AddTestDirectory(const std::string& title,
102 const std::string& parent_id) {
103 scoped_ptr<google_apis::FileResource> entry;
104 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
105 fake_drive_service_->AddNewDirectory(
107 drive::DriveServiceInterface::AddNewDirectoryOptions(),
108 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
109 content::RunAllPendingInMessageLoop();
110 return error == google_apis::HTTP_CREATED && entry ? entry->file_id() : "";
113 base::ScopedTempDir test_cache_root_;
114 drive::FakeDriveService* fake_drive_service_;
115 drive::DriveIntegrationService* integration_service_;
116 drive::DriveIntegrationServiceFactory::FactoryCallback
117 create_drive_integration_service_;
118 scoped_ptr<drive::DriveIntegrationServiceFactory::ScopedFactoryForTest>
119 service_factory_for_test_;
122 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
123 FileSystemApiOpenExistingFileTest) {
124 base::FilePath test_file = drive::util::GetDriveMountPointPath(
125 browser()->profile()).AppendASCII("root/open_existing.txt");
126 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
128 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
132 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
133 FileSystemApiOpenExistingFileWithWriteTest) {
134 base::FilePath test_file = drive::util::GetDriveMountPointPath(
135 browser()->profile()).AppendASCII("root/open_existing.txt");
136 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
138 ASSERT_TRUE(RunPlatformAppTest(
139 "api_test/file_system/open_existing_with_write")) << message_;
142 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
143 FileSystemApiOpenMultipleSuggested) {
144 base::FilePath test_file = drive::util::GetDriveMountPointPath(
145 browser()->profile()).AppendASCII("root/open_existing.txt");
146 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
147 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), true, false));
148 FileSystemChooseEntryFunction::SkipPickerAndSelectSuggestedPathForTest();
149 ASSERT_TRUE(RunPlatformAppTest(
150 "api_test/file_system/open_multiple_with_suggested_name"))
154 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
155 FileSystemApiOpenMultipleExistingFilesTest) {
156 base::FilePath test_file1 = drive::util::GetDriveMountPointPath(
157 browser()->profile()).AppendASCII("root/open_existing1.txt");
158 base::FilePath test_file2 = drive::util::GetDriveMountPointPath(
159 browser()->profile()).AppendASCII("root/open_existing2.txt");
160 std::vector<base::FilePath> test_files;
161 test_files.push_back(test_file1);
162 test_files.push_back(test_file2);
163 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
165 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_multiple_existing"))
169 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
170 FileSystemApiOpenDirectoryTest) {
171 base::FilePath test_directory =
172 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
174 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
176 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
180 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
181 FileSystemApiOpenDirectoryWithWriteTest) {
182 base::FilePath test_directory =
183 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
185 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
188 RunPlatformAppTest("api_test/file_system/open_directory_with_write"))
192 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
193 FileSystemApiOpenDirectoryWithoutPermissionTest) {
194 base::FilePath test_directory =
195 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
197 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
199 ASSERT_TRUE(RunPlatformAppTest(
200 "api_test/file_system/open_directory_without_permission"))
204 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
205 FileSystemApiOpenDirectoryWithOnlyWritePermissionTest) {
206 base::FilePath test_directory =
207 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
209 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
211 ASSERT_TRUE(RunPlatformAppTest(
212 "api_test/file_system/open_directory_with_only_write"))
216 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
217 FileSystemApiSaveNewFileTest) {
218 base::FilePath test_file = drive::util::GetDriveMountPointPath(
219 browser()->profile()).AppendASCII("root/save_new.txt");
220 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
222 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new"))
226 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
227 FileSystemApiSaveExistingFileTest) {
228 base::FilePath test_file = drive::util::GetDriveMountPointPath(
229 browser()->profile()).AppendASCII("root/save_existing.txt");
230 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
232 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_existing"))
236 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
237 FileSystemApiSaveNewFileWithWriteTest) {
238 base::FilePath test_file = drive::util::GetDriveMountPointPath(
239 browser()->profile()).AppendASCII("root/save_new.txt");
240 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
242 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new_with_write"))
246 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
247 FileSystemApiSaveExistingFileWithWriteTest) {
248 base::FilePath test_file = drive::util::GetDriveMountPointPath(
249 browser()->profile()).AppendASCII("root/save_existing.txt");
250 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
252 ASSERT_TRUE(RunPlatformAppTest(
253 "api_test/file_system/save_existing_with_write")) << message_;
256 } // namespace extensions