Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / file_system / file_system_apitest_chromeos.cc
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.
4
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"
14
15 namespace extensions {
16
17 // This class contains chrome.filesystem API test specific to Chrome OS, namely,
18 // the integrated Google Drive support.
19 class FileSystemApiTestForDrive : public PlatformAppBrowserTest {
20  public:
21   FileSystemApiTestForDrive()
22       : fake_drive_service_(NULL),
23         integration_service_(NULL) {
24   }
25
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();
30
31     ASSERT_TRUE(test_cache_root_.CreateUniqueTempDir());
32
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_));
39   }
40
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();
45
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);
53   }
54
55   virtual void TearDown() OVERRIDE {
56     FileSystemChooseEntryFunction::StopSkippingPickerForTest();
57     PlatformAppBrowserTest::TearDown();
58   };
59
60  private:
61   drive::DriveIntegrationService* CreateDriveIntegrationService(
62       Profile* profile) {
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");
69
70     SetUpTestFileHierarchy();
71
72     integration_service_ = new drive::DriveIntegrationService(
73         profile, NULL, fake_drive_service_, std::string(),
74         test_cache_root_.path(), NULL);
75     return integration_service_;
76   }
77
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));
84   }
85
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,
94                                                          &resource_entry));
95     content::RunAllPendingInMessageLoop();
96     return error == google_apis::HTTP_CREATED && resource_entry;
97   }
98
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(
104         parent_id, title,
105         drive::DriveServiceInterface::AddNewDirectoryOptions(),
106         google_apis::test_util::CreateCopyResultCallback(&error,
107                                                          &resource_entry));
108     content::RunAllPendingInMessageLoop();
109     return error == google_apis::HTTP_CREATED && resource_entry ?
110         resource_entry->resource_id() : "";
111   }
112
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_;
120 };
121
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(
127       &test_file);
128   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
129       << message_;
130 }
131
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(
137       &test_file);
138   ASSERT_TRUE(RunPlatformAppTest(
139       "api_test/file_system/open_existing_with_write")) << message_;
140 }
141
142 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
143                        FileSystemApiOpenDirectoryTest) {
144   base::FilePath test_directory =
145       drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
146           "root/subdir");
147   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
148       &test_directory);
149   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
150       << message_;
151 }
152
153 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
154                        FileSystemApiOpenDirectoryWithWriteTest) {
155   base::FilePath test_directory =
156       drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
157           "root/subdir");
158   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
159       &test_directory);
160   ASSERT_TRUE(
161       RunPlatformAppTest("api_test/file_system/open_directory_with_write"))
162       << message_;
163 }
164
165 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
166                        FileSystemApiOpenDirectoryWithoutPermissionTest) {
167   base::FilePath test_directory =
168       drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
169           "root/subdir");
170   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
171       &test_directory);
172   ASSERT_TRUE(RunPlatformAppTest(
173       "api_test/file_system/open_directory_without_permission"))
174       << message_;
175 }
176
177 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForDrive,
178                        FileSystemApiOpenDirectoryWithOnlyWritePermissionTest) {
179   base::FilePath test_directory =
180       drive::util::GetDriveMountPointPath(browser()->profile()).AppendASCII(
181           "root/subdir");
182   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
183       &test_directory);
184   ASSERT_TRUE(RunPlatformAppTest(
185       "api_test/file_system/open_directory_with_only_write"))
186       << message_;
187 }
188
189 }  // namespace extensions