Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_manager / external_filesystem_apitest.cc
blobfd2d19e186429bb7cf02beb2a1a008ea120bef00
1 // Copyright (c) 2012 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/bind.h"
6 #include "base/files/file_path.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
12 #include "chrome/browser/chromeos/drive/file_system_util.h"
13 #include "chrome/browser/chromeos/file_manager/mount_test_util.h"
14 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "components/drive/service/fake_drive_service.h"
23 #include "components/user_manager/user_manager.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/test/test_utils.h"
27 #include "extensions/browser/notification_types.h"
28 #include "extensions/test/result_catcher.h"
29 #include "google_apis/drive/drive_api_parser.h"
30 #include "google_apis/drive/test_util.h"
31 #include "google_apis/drive/time_util.h"
32 #include "storage/browser/fileapi/external_mount_points.h"
33 #include "ui/shell_dialogs/select_file_dialog_factory.h"
35 // Tests for access to external file systems (as defined in
36 // storage/common/fileapi/file_system_types.h) from extensions with
37 // fileManagerPrivate and fileBrowserHandler extension permissions.
38 // The tests cover following external file system types:
39 // - local (kFileSystemTypeLocalNative): a local file system on which files are
40 // accessed using native local path.
41 // - restricted (kFileSystemTypeRestrictedLocalNative): a *read-only* local file
42 // system which can only be accessed by extensions that have full access to
43 // external file systems (i.e. extensions with fileManagerPrivate permission).
44 // - drive (kFileSystemTypeDrive): a file system that provides access to Google
45 // Drive.
47 // The tests cover following scenarios:
48 // - Performing file system operations on external file systems from an
49 // app with fileManagerPrivate permission (i.e. Files.app).
50 // - Performing read/write operations from file handler extensions. These
51 // extensions need a file browser extension to give them permissions to access
52 // files. This also includes file handler extensions in filesystem API.
53 // - Observing directory changes from a file browser extension (using
54 // fileManagerPrivate API).
55 // - Doing searches on drive file system from file browser extension (using
56 // fileManagerPrivate API).
58 using drive::DriveIntegrationServiceFactory;
59 using extensions::Extension;
61 namespace file_manager {
62 namespace {
64 // Root dirs for file systems expected by the test extensions.
65 // NOTE: Root dir for drive file system is set by Chrome's drive implementation,
66 // but the test will have to make sure the mount point is added before
67 // starting a test extension using WaitUntilDriveMountPointIsAdded().
68 const char kLocalMountPointName[] = "local";
69 const char kRestrictedMountPointName[] = "restricted";
71 // Default file content for the test files.
72 const char kTestFileContent[] = "This is some test content.";
74 // User account email and directory hash for secondary account for multi-profile
75 // sensitive test cases.
76 const char kSecondProfileAccount[] = "profile2@test.com";
77 const char kSecondProfileHash[] = "fileBrowserApiTestProfile2";
79 class FakeSelectFileDialog : public ui::SelectFileDialog {
80 public:
81 FakeSelectFileDialog(ui::SelectFileDialog::Listener* listener,
82 ui::SelectFilePolicy* policy)
83 : ui::SelectFileDialog(listener, policy) {}
85 void SelectFileImpl(Type type,
86 const base::string16& title,
87 const base::FilePath& default_path,
88 const FileTypeInfo* file_types,
89 int file_type_index,
90 const base::FilePath::StringType& default_extension,
91 gfx::NativeWindow owning_window,
92 void* params) override {
93 listener_->FileSelected(
94 base::FilePath("/special/drive-user/root/test_dir"), 0, NULL);
97 bool IsRunning(gfx::NativeWindow owning_window) const override {
98 return false;
101 void ListenerDestroyed() override {}
103 bool HasMultipleFileTypeChoicesImpl() override { return false; }
105 private:
106 ~FakeSelectFileDialog() override {}
109 class FakeSelectFileDialogFactory : public ui::SelectFileDialogFactory {
110 private:
111 ui::SelectFileDialog* Create(ui::SelectFileDialog::Listener* listener,
112 ui::SelectFilePolicy* policy) override {
113 return new FakeSelectFileDialog(listener, policy);
117 // Sets up the initial file system state for native local and restricted native
118 // local file systems. The hierarchy is the same as for the drive file system.
119 // The directory is created at unique_temp_dir/|mount_point_name| path.
120 bool InitializeLocalFileSystem(std::string mount_point_name,
121 base::ScopedTempDir* tmp_dir,
122 base::FilePath* mount_point_dir) {
123 if (!tmp_dir->CreateUniqueTempDir())
124 return false;
126 *mount_point_dir = tmp_dir->path().AppendASCII(mount_point_name);
127 // Create the mount point.
128 if (!base::CreateDirectory(*mount_point_dir))
129 return false;
131 base::FilePath test_dir = mount_point_dir->AppendASCII("test_dir");
132 if (!base::CreateDirectory(test_dir))
133 return false;
135 base::FilePath test_subdir = test_dir.AppendASCII("empty_test_dir");
136 if (!base::CreateDirectory(test_subdir))
137 return false;
139 test_subdir = test_dir.AppendASCII("subdir");
140 if (!base::CreateDirectory(test_subdir))
141 return false;
143 base::FilePath test_file = test_dir.AppendASCII("test_file.xul");
144 if (!google_apis::test_util::WriteStringToFile(test_file, kTestFileContent))
145 return false;
147 test_file = test_dir.AppendASCII("test_file.xul.foo");
148 if (!google_apis::test_util::WriteStringToFile(test_file, kTestFileContent))
149 return false;
151 test_file = test_dir.AppendASCII("test_file.tiff");
152 if (!google_apis::test_util::WriteStringToFile(test_file, kTestFileContent))
153 return false;
155 test_file = test_dir.AppendASCII("test_file.tiff.foo");
156 if (!google_apis::test_util::WriteStringToFile(test_file, kTestFileContent))
157 return false;
159 test_file = test_dir.AppendASCII("empty_test_file.foo");
160 if (!google_apis::test_util::WriteStringToFile(test_file, ""))
161 return false;
163 return true;
166 scoped_ptr<google_apis::FileResource> UpdateDriveEntryTime(
167 drive::FakeDriveService* fake_drive_service,
168 const std::string& resource_id,
169 const std::string& last_modified,
170 const std::string& last_viewed_by_me) {
171 base::Time last_modified_time, last_viewed_by_me_time;
172 if (!google_apis::util::GetTimeFromString(last_modified,
173 &last_modified_time) ||
174 !google_apis::util::GetTimeFromString(last_viewed_by_me,
175 &last_viewed_by_me_time))
176 return scoped_ptr<google_apis::FileResource>();
178 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
179 scoped_ptr<google_apis::FileResource> entry;
180 fake_drive_service->UpdateResource(
181 resource_id,
182 std::string(), // parent_resource_id
183 std::string(), // title
184 last_modified_time, last_viewed_by_me_time,
185 google_apis::drive::Properties(),
186 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
187 base::RunLoop().RunUntilIdle();
188 if (error != google_apis::HTTP_SUCCESS)
189 return scoped_ptr<google_apis::FileResource>();
191 return entry.Pass();
194 scoped_ptr<google_apis::FileResource> AddFileToDriveService(
195 drive::FakeDriveService* fake_drive_service,
196 const std::string& mime_type,
197 const std::string& content,
198 const std::string& parent_resource_id,
199 const std::string& title,
200 const std::string& last_modified,
201 const std::string& last_viewed_by_me) {
202 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
203 scoped_ptr<google_apis::FileResource> entry;
204 fake_drive_service->AddNewFile(
205 mime_type,
206 content,
207 parent_resource_id,
208 title,
209 false, // shared_with_me
210 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
211 base::RunLoop().RunUntilIdle();
212 if (error != google_apis::HTTP_CREATED)
213 return scoped_ptr<google_apis::FileResource>();
215 return UpdateDriveEntryTime(fake_drive_service, entry->file_id(),
216 last_modified, last_viewed_by_me);
219 scoped_ptr<google_apis::FileResource> AddDirectoryToDriveService(
220 drive::FakeDriveService* fake_drive_service,
221 const std::string& parent_resource_id,
222 const std::string& title,
223 const std::string& last_modified,
224 const std::string& last_viewed_by_me) {
225 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
226 scoped_ptr<google_apis::FileResource> entry;
227 fake_drive_service->AddNewDirectory(
228 parent_resource_id, title, drive::AddNewDirectoryOptions(),
229 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
230 base::RunLoop().RunUntilIdle();
231 if (error != google_apis::HTTP_CREATED)
232 return scoped_ptr<google_apis::FileResource>();
234 return UpdateDriveEntryTime(fake_drive_service, entry->file_id(),
235 last_modified, last_viewed_by_me);
238 // Sets up the drive service state.
239 // The hierarchy is the same as for the local file system.
240 bool InitializeDriveService(
241 drive::FakeDriveService* fake_drive_service,
242 std::map<std::string, std::string>* out_resource_ids) {
243 scoped_ptr<google_apis::FileResource> entry;
245 entry = AddDirectoryToDriveService(fake_drive_service,
246 fake_drive_service->GetRootResourceId(),
247 "test_dir",
248 "2012-01-02T00:00:00.000Z",
249 "2012-01-02T00:00:01.000Z");
250 if (!entry)
251 return false;
252 (*out_resource_ids)[entry->title()] = entry->file_id();
254 entry = AddDirectoryToDriveService(fake_drive_service,
255 (*out_resource_ids)["test_dir"],
256 "empty_test_dir",
257 "2011-11-02T04:00:00.000Z",
258 "2011-11-02T04:00:00.000Z");
259 if (!entry)
260 return false;
261 (*out_resource_ids)[entry->title()] = entry->file_id();
263 entry = AddDirectoryToDriveService(fake_drive_service,
264 (*out_resource_ids)["test_dir"],
265 "subdir",
266 "2011-04-01T18:34:08.234Z",
267 "2012-01-02T00:00:01.000Z");
268 if (!entry)
269 return false;
270 (*out_resource_ids)[entry->title()] = entry->file_id();
272 entry = AddFileToDriveService(fake_drive_service,
273 "application/vnd.mozilla.xul+xml",
274 kTestFileContent,
275 (*out_resource_ids)["test_dir"],
276 "test_file.xul",
277 "2011-12-14T00:40:47.330Z",
278 "2012-01-02T00:00:00.000Z");
279 if (!entry)
280 return false;
281 (*out_resource_ids)[entry->title()] = entry->file_id();
283 entry = AddFileToDriveService(fake_drive_service,
284 "test/ro",
285 kTestFileContent,
286 (*out_resource_ids)["test_dir"],
287 "test_file.xul.foo",
288 "2012-01-01T10:00:30.000Z",
289 "2012-01-01T00:00:00.000Z");
290 if (!entry)
291 return false;
292 (*out_resource_ids)[entry->title()] = entry->file_id();
294 entry = AddFileToDriveService(fake_drive_service,
295 "image/tiff",
296 kTestFileContent,
297 (*out_resource_ids)["test_dir"],
298 "test_file.tiff",
299 "2011-04-03T11:11:10.000Z",
300 "2012-01-02T00:00:00.000Z");
301 if (!entry)
302 return false;
303 (*out_resource_ids)[entry->title()] = entry->file_id();
305 entry = AddFileToDriveService(fake_drive_service,
306 "test/rw",
307 kTestFileContent,
308 (*out_resource_ids)["test_dir"],
309 "test_file.tiff.foo",
310 "2011-12-14T00:40:47.330Z",
311 "2010-01-02T00:00:00.000Z");
312 if (!entry)
313 return false;
314 (*out_resource_ids)[entry->title()] = entry->file_id();
316 entry = AddFileToDriveService(fake_drive_service,
317 "test/rw",
319 (*out_resource_ids)["test_dir"],
320 "empty_test_file.foo",
321 "2011-12-14T00:40:47.330Z",
322 "2011-12-14T00:40:47.330Z");
323 if (!entry)
324 return false;
325 (*out_resource_ids)[entry->title()] = entry->file_id();
327 return true;
330 // Helper class to wait for a background page to load or close again.
331 class BackgroundObserver {
332 public:
333 BackgroundObserver()
334 : page_created_(extensions::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
335 content::NotificationService::AllSources()),
336 page_closed_(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
337 content::NotificationService::AllSources()) {}
339 void WaitUntilLoaded() {
340 page_created_.Wait();
343 void WaitUntilClosed() {
344 page_closed_.Wait();
347 private:
348 content::WindowedNotificationObserver page_created_;
349 content::WindowedNotificationObserver page_closed_;
352 // Base class for FileSystemExtensionApi tests.
353 class FileSystemExtensionApiTestBase : public ExtensionApiTest {
354 public:
355 enum Flags {
356 FLAGS_NONE = 0,
357 FLAGS_USE_FILE_HANDLER = 1 << 1,
358 FLAGS_LAZY_FILE_HANDLER = 1 << 2
361 FileSystemExtensionApiTestBase() {}
362 ~FileSystemExtensionApiTestBase() override {}
364 void SetUp() override {
365 InitTestFileSystem();
366 ExtensionApiTest::SetUp();
369 void SetUpOnMainThread() override {
370 AddTestMountPoint();
371 ExtensionApiTest::SetUpOnMainThread();
374 // Runs a file system extension API test.
375 // It loads test component extension at |filebrowser_path| with manifest
376 // at |filebrowser_manifest|. The |filebrowser_manifest| should be a path
377 // relative to |filebrowser_path|. The method waits until the test extension
378 // sends test succeed or fail message. It returns true if the test succeeds.
379 // If |FLAGS_USE_FILE_HANDLER| flag is set, the file handler extension at path
380 // |filehandler_path| will be loaded before the file browser extension.
381 // If the flag FLAGS_LAZY_FILE_HANDLER is set, the file handler extension must
382 // not have persistent background page. The test will wait until the file
383 // handler's background page is closed after initial load before the file
384 // browser extension is loaded.
385 // If |RunFileSystemExtensionApiTest| fails, |message_| will contain a failure
386 // message.
387 bool RunFileSystemExtensionApiTest(
388 const std::string& filebrowser_path,
389 const base::FilePath::CharType* filebrowser_manifest,
390 const std::string& filehandler_path,
391 int flags) {
392 if (flags & FLAGS_USE_FILE_HANDLER) {
393 if (filehandler_path.empty()) {
394 message_ = "Missing file handler path.";
395 return false;
398 BackgroundObserver page_complete;
399 const Extension* file_handler =
400 LoadExtension(test_data_dir_.AppendASCII(filehandler_path));
401 if (!file_handler)
402 return false;
404 if (flags & FLAGS_LAZY_FILE_HANDLER) {
405 page_complete.WaitUntilClosed();
406 } else {
407 page_complete.WaitUntilLoaded();
411 extensions::ResultCatcher catcher;
413 const Extension* file_browser = LoadExtensionAsComponentWithManifest(
414 test_data_dir_.AppendASCII(filebrowser_path),
415 filebrowser_manifest);
416 if (!file_browser)
417 return false;
419 if (!catcher.GetNextResult()) {
420 message_ = catcher.message();
421 return false;
424 return true;
427 protected:
428 // Sets up initial test file system hierarchy.
429 virtual void InitTestFileSystem() = 0;
430 // Registers mount point used in the test.
431 virtual void AddTestMountPoint() = 0;
434 // Tests for a native local file system.
435 class LocalFileSystemExtensionApiTest : public FileSystemExtensionApiTestBase {
436 public:
437 LocalFileSystemExtensionApiTest() {}
438 ~LocalFileSystemExtensionApiTest() override {}
440 // FileSystemExtensionApiTestBase override.
441 void InitTestFileSystem() override {
442 ASSERT_TRUE(InitializeLocalFileSystem(
443 kLocalMountPointName, &tmp_dir_, &mount_point_dir_))
444 << "Failed to initialize file system.";
447 // FileSystemExtensionApiTestBase override.
448 void AddTestMountPoint() override {
449 EXPECT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile())
450 ->RegisterFileSystem(kLocalMountPointName,
451 storage::kFileSystemTypeNativeLocal,
452 storage::FileSystemMountOption(),
453 mount_point_dir_));
454 VolumeManager::Get(browser()->profile())
455 ->AddVolumeForTesting(mount_point_dir_, VOLUME_TYPE_TESTING,
456 chromeos::DEVICE_TYPE_UNKNOWN,
457 false /* read_only */);
460 private:
461 base::ScopedTempDir tmp_dir_;
462 base::FilePath mount_point_dir_;
465 // Tests for restricted native local file systems.
466 class RestrictedFileSystemExtensionApiTest
467 : public FileSystemExtensionApiTestBase {
468 public:
469 RestrictedFileSystemExtensionApiTest() {}
470 ~RestrictedFileSystemExtensionApiTest() override {}
472 // FileSystemExtensionApiTestBase override.
473 void InitTestFileSystem() override {
474 ASSERT_TRUE(InitializeLocalFileSystem(
475 kRestrictedMountPointName, &tmp_dir_, &mount_point_dir_))
476 << "Failed to initialize file system.";
479 // FileSystemExtensionApiTestBase override.
480 void AddTestMountPoint() override {
481 EXPECT_TRUE(
482 content::BrowserContext::GetMountPoints(browser()->profile())
483 ->RegisterFileSystem(kRestrictedMountPointName,
484 storage::kFileSystemTypeRestrictedNativeLocal,
485 storage::FileSystemMountOption(),
486 mount_point_dir_));
487 VolumeManager::Get(browser()->profile())
488 ->AddVolumeForTesting(mount_point_dir_, VOLUME_TYPE_TESTING,
489 chromeos::DEVICE_TYPE_UNKNOWN,
490 true /* read_only */);
493 private:
494 base::ScopedTempDir tmp_dir_;
495 base::FilePath mount_point_dir_;
498 // Tests for a drive file system.
499 class DriveFileSystemExtensionApiTest : public FileSystemExtensionApiTestBase {
500 public:
501 DriveFileSystemExtensionApiTest() : fake_drive_service_(NULL) {}
502 ~DriveFileSystemExtensionApiTest() override {}
504 // FileSystemExtensionApiTestBase override.
505 void InitTestFileSystem() override {
506 // Set up cache root to be used by DriveIntegrationService. This has to be
507 // done before the browser is created because the service instance is
508 // initialized by EventRouter.
509 ASSERT_TRUE(test_cache_root_.CreateUniqueTempDir());
511 // This callback will get called during Profile creation.
512 create_drive_integration_service_ = base::Bind(
513 &DriveFileSystemExtensionApiTest::CreateDriveIntegrationService,
514 base::Unretained(this));
515 service_factory_for_test_.reset(
516 new DriveIntegrationServiceFactory::ScopedFactoryForTest(
517 &create_drive_integration_service_));
520 // FileSystemExtensionApiTestBase override.
521 void AddTestMountPoint() override {
522 test_util::WaitUntilDriveMountPointIsAdded(browser()->profile());
525 // FileSystemExtensionApiTestBase override.
526 void TearDown() override {
527 FileSystemExtensionApiTestBase::TearDown();
528 ui::SelectFileDialog::SetFactory(NULL);
531 protected:
532 // DriveIntegrationService factory function for this test.
533 drive::DriveIntegrationService* CreateDriveIntegrationService(
534 Profile* profile) {
535 // Ignore signin profile.
536 if (profile->GetPath() == chromeos::ProfileHelper::GetSigninProfileDir())
537 return NULL;
539 // DriveFileSystemExtensionApiTest doesn't expect that several user profiles
540 // could exist simultaneously.
541 DCHECK(fake_drive_service_ == NULL);
542 fake_drive_service_ = new drive::FakeDriveService;
543 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json");
545 std::map<std::string, std::string> resource_ids;
546 EXPECT_TRUE(InitializeDriveService(fake_drive_service_, &resource_ids));
548 return new drive::DriveIntegrationService(
549 profile, NULL, fake_drive_service_, "", test_cache_root_.path(), NULL);
552 base::ScopedTempDir test_cache_root_;
553 drive::FakeDriveService* fake_drive_service_;
554 DriveIntegrationServiceFactory::FactoryCallback
555 create_drive_integration_service_;
556 scoped_ptr<DriveIntegrationServiceFactory::ScopedFactoryForTest>
557 service_factory_for_test_;
560 // Tests for Drive file systems in multi-profile setting.
561 class MultiProfileDriveFileSystemExtensionApiTest :
562 public FileSystemExtensionApiTestBase {
563 public:
564 MultiProfileDriveFileSystemExtensionApiTest() : second_profile(NULL) {}
566 void SetUpOnMainThread() override {
567 base::FilePath user_data_directory;
568 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
569 user_manager::UserManager::Get()->UserLoggedIn(
570 kSecondProfileAccount, kSecondProfileHash, false);
571 // Set up the secondary profile.
572 base::FilePath profile_dir =
573 user_data_directory.Append(
574 chromeos::ProfileHelper::GetUserProfileDir(
575 kSecondProfileHash).BaseName());
576 second_profile =
577 g_browser_process->profile_manager()->GetProfile(profile_dir);
579 FileSystemExtensionApiTestBase::SetUpOnMainThread();
582 void InitTestFileSystem() override {
583 // This callback will get called during Profile creation.
584 create_drive_integration_service_ = base::Bind(
585 &MultiProfileDriveFileSystemExtensionApiTest::
586 CreateDriveIntegrationService,
587 base::Unretained(this));
588 service_factory_for_test_.reset(
589 new DriveIntegrationServiceFactory::ScopedFactoryForTest(
590 &create_drive_integration_service_));
593 void AddTestMountPoint() override {
594 test_util::WaitUntilDriveMountPointIsAdded(browser()->profile());
595 test_util::WaitUntilDriveMountPointIsAdded(second_profile);
598 protected:
599 // DriveIntegrationService factory function for this test.
600 drive::DriveIntegrationService* CreateDriveIntegrationService(
601 Profile* profile) {
602 base::FilePath cache_dir;
603 base::CreateNewTempDirectory(base::FilePath::StringType(), &cache_dir);
605 drive::FakeDriveService* const fake_drive_service =
606 new drive::FakeDriveService;
607 fake_drive_service->LoadAppListForDriveApi("drive/applist.json");
608 EXPECT_TRUE(InitializeDriveService(fake_drive_service, &resource_ids_));
610 return new drive::DriveIntegrationService(
611 profile, NULL, fake_drive_service, std::string(), cache_dir, NULL);
614 bool AddTestHostedDocuments() {
615 const char kResourceId[] = "unique-id-for-multiprofile-copy-test";
616 drive::FakeDriveService* const main_service =
617 static_cast<drive::FakeDriveService*>(
618 drive::util::GetDriveServiceByProfile(browser()->profile()));
619 drive::FakeDriveService* const sub_service =
620 static_cast<drive::FakeDriveService*>(
621 drive::util::GetDriveServiceByProfile(second_profile));
623 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR;
624 scoped_ptr<google_apis::FileResource> entry;
626 // Place a hosted document under root/test_dir of the sub profile.
627 sub_service->AddNewFileWithResourceId(
628 kResourceId,
629 "application/vnd.google-apps.document", "",
630 resource_ids_["test_dir"], "hosted_doc", true,
631 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
632 content::RunAllBlockingPoolTasksUntilIdle();
633 if (error != google_apis::HTTP_CREATED)
634 return false;
636 // Place the hosted document with no parent in the main profile, for
637 // simulating the situation that the document is shared to the main profile.
638 error = google_apis::DRIVE_OTHER_ERROR;
639 main_service->AddNewFileWithResourceId(
640 kResourceId,
641 "application/vnd.google-apps.document", "", "", "hosted_doc", true,
642 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
643 content::RunAllBlockingPoolTasksUntilIdle();
644 return (error == google_apis::HTTP_CREATED);
647 DriveIntegrationServiceFactory::FactoryCallback
648 create_drive_integration_service_;
649 scoped_ptr<DriveIntegrationServiceFactory::ScopedFactoryForTest>
650 service_factory_for_test_;
651 Profile* second_profile;
652 std::map<std::string, std::string> resource_ids_;
655 class LocalAndDriveFileSystemExtensionApiTest
656 : public FileSystemExtensionApiTestBase {
657 public:
658 LocalAndDriveFileSystemExtensionApiTest() {}
659 ~LocalAndDriveFileSystemExtensionApiTest() override {}
661 // FileSystemExtensionApiTestBase override.
662 void InitTestFileSystem() override {
663 ASSERT_TRUE(InitializeLocalFileSystem(
664 kLocalMountPointName, &local_tmp_dir_, &local_mount_point_dir_))
665 << "Failed to initialize file system.";
667 // Set up cache root to be used by DriveIntegrationService. This has to be
668 // done before the browser is created because the service instance is
669 // initialized by EventRouter.
670 ASSERT_TRUE(test_cache_root_.CreateUniqueTempDir());
672 // This callback will get called during Profile creation.
673 create_drive_integration_service_ = base::Bind(
674 &LocalAndDriveFileSystemExtensionApiTest::CreateDriveIntegrationService,
675 base::Unretained(this));
676 service_factory_for_test_.reset(
677 new DriveIntegrationServiceFactory::ScopedFactoryForTest(
678 &create_drive_integration_service_));
681 // FileSystemExtensionApiTestBase override.
682 void AddTestMountPoint() override {
683 EXPECT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile())
684 ->RegisterFileSystem(kLocalMountPointName,
685 storage::kFileSystemTypeNativeLocal,
686 storage::FileSystemMountOption(),
687 local_mount_point_dir_));
688 VolumeManager::Get(browser()->profile())
689 ->AddVolumeForTesting(local_mount_point_dir_, VOLUME_TYPE_TESTING,
690 chromeos::DEVICE_TYPE_UNKNOWN,
691 false /* read_only */);
692 test_util::WaitUntilDriveMountPointIsAdded(browser()->profile());
695 protected:
696 // DriveIntegrationService factory function for this test.
697 drive::DriveIntegrationService* CreateDriveIntegrationService(
698 Profile* profile) {
699 fake_drive_service_ = new drive::FakeDriveService;
700 fake_drive_service_->LoadAppListForDriveApi("drive/applist.json");
702 std::map<std::string, std::string> resource_ids;
703 EXPECT_TRUE(InitializeDriveService(fake_drive_service_, &resource_ids));
705 return new drive::DriveIntegrationService(profile,
706 NULL,
707 fake_drive_service_,
708 "drive",
709 test_cache_root_.path(),
710 NULL);
713 private:
714 // For local volume.
715 base::ScopedTempDir local_tmp_dir_;
716 base::FilePath local_mount_point_dir_;
718 // For drive volume.
719 base::ScopedTempDir test_cache_root_;
720 drive::FakeDriveService* fake_drive_service_;
721 DriveIntegrationServiceFactory::FactoryCallback
722 create_drive_integration_service_;
723 scoped_ptr<DriveIntegrationServiceFactory::ScopedFactoryForTest>
724 service_factory_for_test_;
728 // LocalFileSystemExtensionApiTests.
731 IN_PROC_BROWSER_TEST_F(LocalFileSystemExtensionApiTest, FileSystemOperations) {
732 EXPECT_TRUE(RunFileSystemExtensionApiTest(
733 "file_browser/filesystem_operations_test",
734 FILE_PATH_LITERAL("manifest.json"),
736 FLAGS_NONE)) << message_;
739 IN_PROC_BROWSER_TEST_F(LocalFileSystemExtensionApiTest, FileWatch) {
740 EXPECT_TRUE(RunFileSystemExtensionApiTest(
741 "file_browser/file_watcher_test",
742 FILE_PATH_LITERAL("manifest.json"),
744 FLAGS_NONE)) << message_;
747 IN_PROC_BROWSER_TEST_F(LocalFileSystemExtensionApiTest, FileBrowserHandlers) {
748 EXPECT_TRUE(RunFileSystemExtensionApiTest(
749 "file_browser/handler_test_runner",
750 FILE_PATH_LITERAL("manifest.json"),
751 "file_browser/file_browser_handler",
752 FLAGS_USE_FILE_HANDLER)) << message_;
755 IN_PROC_BROWSER_TEST_F(LocalFileSystemExtensionApiTest,
756 FileBrowserHandlersLazy) {
757 EXPECT_TRUE(RunFileSystemExtensionApiTest(
758 "file_browser/handler_test_runner",
759 FILE_PATH_LITERAL("manifest.json"),
760 "file_browser/file_browser_handler_lazy",
761 FLAGS_USE_FILE_HANDLER | FLAGS_LAZY_FILE_HANDLER)) << message_;
764 IN_PROC_BROWSER_TEST_F(LocalFileSystemExtensionApiTest, AppFileHandler) {
765 EXPECT_TRUE(RunFileSystemExtensionApiTest(
766 "file_browser/handler_test_runner",
767 FILE_PATH_LITERAL("manifest.json"),
768 "file_browser/app_file_handler",
769 FLAGS_USE_FILE_HANDLER)) << message_;
773 // RestrictedFileSystemExtensionApiTests.
775 IN_PROC_BROWSER_TEST_F(RestrictedFileSystemExtensionApiTest,
776 FileSystemOperations) {
777 EXPECT_TRUE(RunFileSystemExtensionApiTest(
778 "file_browser/filesystem_operations_test",
779 FILE_PATH_LITERAL("manifest.json"),
781 FLAGS_NONE)) << message_;
785 // DriveFileSystemExtensionApiTests.
787 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, FileSystemOperations) {
788 EXPECT_TRUE(RunFileSystemExtensionApiTest(
789 "file_browser/filesystem_operations_test",
790 FILE_PATH_LITERAL("manifest.json"),
792 FLAGS_NONE)) << message_;
795 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, FileWatch) {
796 EXPECT_TRUE(RunFileSystemExtensionApiTest(
797 "file_browser/file_watcher_test",
798 FILE_PATH_LITERAL("manifest.json"),
800 FLAGS_NONE)) << message_;
803 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, FileBrowserHandlers) {
804 EXPECT_TRUE(RunFileSystemExtensionApiTest(
805 "file_browser/handler_test_runner",
806 FILE_PATH_LITERAL("manifest.json"),
807 "file_browser/file_browser_handler",
808 FLAGS_USE_FILE_HANDLER)) << message_;
811 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, Search) {
812 // Configure the drive service to return only one search result at a time
813 // to simulate paginated searches.
814 fake_drive_service_->set_default_max_results(1);
815 EXPECT_TRUE(RunFileSystemExtensionApiTest(
816 "file_browser/drive_search_test",
817 FILE_PATH_LITERAL("manifest.json"),
819 FLAGS_NONE)) << message_;
822 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, AppFileHandler) {
823 EXPECT_TRUE(RunFileSystemExtensionApiTest(
824 "file_browser/handler_test_runner",
825 FILE_PATH_LITERAL("manifest.json"),
826 "file_browser/app_file_handler",
827 FLAGS_USE_FILE_HANDLER)) << message_;
830 IN_PROC_BROWSER_TEST_F(DriveFileSystemExtensionApiTest, RetainEntry) {
831 ui::SelectFileDialog::SetFactory(new FakeSelectFileDialogFactory());
832 EXPECT_TRUE(RunFileSystemExtensionApiTest("file_browser/retain_entry",
833 FILE_PATH_LITERAL("manifest.json"),
835 FLAGS_NONE))
836 << message_;
839 IN_PROC_BROWSER_TEST_F(MultiProfileDriveFileSystemExtensionApiTest,
840 CrossProfileCopy) {
841 ASSERT_TRUE(AddTestHostedDocuments());
842 EXPECT_TRUE(RunFileSystemExtensionApiTest(
843 "file_browser/multi_profile_copy",
844 FILE_PATH_LITERAL("manifest.json"),
846 FLAGS_NONE)) << message_;
850 // LocalAndDriveFileSystemExtensionApiTests.
852 IN_PROC_BROWSER_TEST_F(LocalAndDriveFileSystemExtensionApiTest,
853 AppFileHandlerMulti) {
854 EXPECT_TRUE(
855 RunFileSystemExtensionApiTest("file_browser/app_file_handler_multi",
856 FILE_PATH_LITERAL("manifest.json"),
858 FLAGS_NONE))
859 << message_;
861 } // namespace
862 } // namespace file_manager