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 "chrome/browser/apps/app_browsertest_util.h"
6 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
7 #include "chrome/browser/chromeos/drive/file_system_interface.h"
8 #include "chrome/browser/chromeos/drive/file_system_util.h"
9 #include "chrome/browser/chromeos/drive/test_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 "content/public/test/test_utils.h"
13 #include "google_apis/drive/test_util.h"
15 namespace extensions {
17 // This class contains chrome.filesystem API test specific to Chrome OS, namely,
18 // the integrated Google Drive support.
19 class FileSystemApiTestForDrive : public PlatformAppBrowserTest {
21 FileSystemApiTestForDrive()
22 : fake_drive_service_(NULL),
23 integration_service_(NULL) {
26 // Sets up fake Drive service for tests (this has to be injected before the
27 // real DriveIntegrationService instance is created.)
28 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
29 PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
31 ASSERT_TRUE(test_cache_root_.CreateUniqueTempDir());
33 create_drive_integration_service_ =
34 base::Bind(&FileSystemApiTestForDrive::CreateDriveIntegrationService,
35 base::Unretained(this));
36 service_factory_for_test_.reset(
37 new drive::DriveIntegrationServiceFactory::ScopedFactoryForTest(
38 &create_drive_integration_service_));
41 // Ensure the fake service's data is fetch in the local file system. This is
42 // necessary because the fetch starts lazily upon the first read operation.
43 virtual void SetUpOnMainThread() OVERRIDE {
44 PlatformAppBrowserTest::SetUpOnMainThread();
46 scoped_ptr<drive::ResourceEntry> entry;
47 drive::FileError error = drive::FILE_ERROR_FAILED;
48 integration_service_->file_system()->GetResourceEntry(
49 base::FilePath::FromUTF8Unsafe("drive/root"), // whatever
50 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
51 drive::test_util::RunBlockingPoolTask();
52 ASSERT_EQ(drive::FILE_ERROR_OK, error);
55 virtual void TearDown() OVERRIDE {
56 FileSystemChooseEntryFunction::StopSkippingPickerForTest();
57 PlatformAppBrowserTest::TearDown();
61 drive::DriveIntegrationService* CreateDriveIntegrationService(
63 fake_drive_service_ = new drive::FakeDriveService;
64 fake_drive_service_->LoadResourceListForWapi(
65 "gdata/empty_feed.json");
66 fake_drive_service_->LoadAccountMetadataForWapi(
67 "gdata/account_metadata.json");
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 const std::string subdir = AddTestDirectory("subdir", root);
82 ASSERT_FALSE(subdir.empty());
83 ASSERT_TRUE(AddTestFile("open_existing.txt", "Can you see me?", subdir));
86 bool AddTestFile(const std::string& title,
87 const std::string& data,
88 const std::string& parent_id) {
89 scoped_ptr<google_apis::ResourceEntry> resource_entry;
90 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
91 fake_drive_service_->AddNewFile(
92 "text/plain", data, parent_id, title, false,
93 google_apis::test_util::CreateCopyResultCallback(&error,
95 content::RunAllPendingInMessageLoop();
96 return error == google_apis::HTTP_CREATED && resource_entry;
99 std::string AddTestDirectory(const std::string& title,
100 const std::string& parent_id) {
101 scoped_ptr<google_apis::ResourceEntry> resource_entry;
102 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
103 fake_drive_service_->AddNewDirectory(
105 drive::DriveServiceInterface::AddNewDirectoryOptions(),
106 google_apis::test_util::CreateCopyResultCallback(&error,
108 content::RunAllPendingInMessageLoop();
109 return error == google_apis::HTTP_CREATED && resource_entry ?
110 resource_entry->resource_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 FileSystemApiOpenDirectoryTest) {
144 base::FilePath test_directory =
145 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
147 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
149 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
153 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
154 FileSystemApiOpenDirectoryWithWriteTest) {
155 base::FilePath test_directory =
156 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
158 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
161 RunPlatformAppTest("api_test/file_system/open_directory_with_write"))
165 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
166 FileSystemApiOpenDirectoryWithoutPermissionTest) {
167 base::FilePath test_directory =
168 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
170 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
172 ASSERT_TRUE(RunPlatformAppTest(
173 "api_test/file_system/open_directory_without_permission"))
177 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
178 FileSystemApiOpenDirectoryWithOnlyWritePermissionTest) {
179 base::FilePath test_directory =
180 drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
182 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
184 ASSERT_TRUE(RunPlatformAppTest(
185 "api_test/file_system/open_directory_with_only_write"))
189 } // namespace extensions