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/chromeos/drive/drive_integration_service.h"
6 #include "chrome/browser/chromeos/drive/fake_file_system.h"
7 #include "chrome/browser/chromeos/drive/test_util.h"
8 #include "chrome/browser/chromeos/file_manager/drive_test_util.cc"
9 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
10 #include "chrome/browser/drive/fake_drive_service.h"
11 #include "chrome/browser/drive/test_util.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/common/file_chooser_file_info.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/test_utils.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/shell_dialogs/selected_file_info.h"
22 namespace file_manager
{
26 // Passes the |result| to the |output| pointer.
27 void PassFileChooserFileInfoList(FileChooserFileInfoList
* output
,
28 const FileChooserFileInfoList
& result
) {
32 // Creates the drive integration service for the |profile|.
33 drive::DriveIntegrationService
* CreateDriveIntegrationService(
34 const base::FilePath temp_dir
,
36 drive::FakeDriveService
* const drive_service
= new drive::FakeDriveService
;
37 if (!drive::test_util::SetUpTestEntries(drive_service
))
40 return new drive::DriveIntegrationService(
44 /* default mount name */ "",
46 new drive::test_util::FakeFileSystem(drive_service
));
49 TEST(FileManagerFileAPIUtilTest
,
50 ConvertSelectedFileInfoListToFileChooserFileInfoList
) {
51 // Prepare the test drive environment.
52 content::TestBrowserThreadBundle threads
;
53 TestingProfileManager
profile_manager(TestingBrowserProcess::GetGlobal());
54 ASSERT_TRUE(profile_manager
.SetUp());
55 base::ScopedTempDir drive_cache_dir
;
56 ASSERT_TRUE(drive_cache_dir
.CreateUniqueTempDir());
57 drive::DriveIntegrationServiceFactory::FactoryCallback
factory_callback(
58 base::Bind(&CreateDriveIntegrationService
, drive_cache_dir
.path()));
59 drive::DriveIntegrationServiceFactory::ScopedFactoryForTest
60 integration_service_factory_scope(&factory_callback
);
62 // Prepare the test profile.
63 Profile
* const profile
= profile_manager
.CreateTestingProfile("test-user");
64 drive::DriveIntegrationService
* const service
=
65 drive::DriveIntegrationServiceFactory::GetForProfile(profile
);
66 service
->SetEnabled(true);
67 test_util::WaitUntilDriveMountPointIsAdded(profile
);
68 ASSERT_TRUE(service
->IsMounted());
70 // Obtain the file system context.
71 content::StoragePartition
* const partition
=
72 content::BrowserContext::GetStoragePartitionForSite(
73 profile
, GURL("http://example.com"));
74 ASSERT_TRUE(partition
);
75 storage::FileSystemContext
* const context
= partition
->GetFileSystemContext();
78 // Prepare the test input.
79 SelectedFileInfoList selected_info_list
;
83 ui::SelectedFileInfo info
;
84 info
.file_path
= base::FilePath(FILE_PATH_LITERAL("/native/File 1.txt"));
85 info
.local_path
= base::FilePath(FILE_PATH_LITERAL("/native/File 1.txt"));
86 info
.display_name
= "display_name";
87 selected_info_list
.push_back(info
);
90 // Non-native file with cache.
92 ui::SelectedFileInfo info
;
93 info
.file_path
= base::FilePath(
94 FILE_PATH_LITERAL("/special/drive-test-user-hash/root/File 1.txt"));
95 info
.local_path
= base::FilePath(FILE_PATH_LITERAL("/native/cache/xxx"));
96 info
.display_name
= "display_name";
97 selected_info_list
.push_back(info
);
100 // Non-native file without.
102 ui::SelectedFileInfo info
;
103 info
.file_path
= base::FilePath(
104 FILE_PATH_LITERAL("/special/drive-test-user-hash/root/File 1.txt"));
105 selected_info_list
.push_back(info
);
108 // Run the test target.
109 FileChooserFileInfoList result
;
110 ConvertSelectedFileInfoListToFileChooserFileInfoList(
112 GURL("http://example.com"),
114 base::Bind(&PassFileChooserFileInfoList
, &result
));
115 content::RunAllBlockingPoolTasksUntilIdle();
118 ASSERT_EQ(3u, result
.size());
120 EXPECT_EQ(FILE_PATH_LITERAL("/native/File 1.txt"),
121 result
[0].file_path
.value());
122 EXPECT_EQ("display_name", result
[0].display_name
);
123 EXPECT_FALSE(result
[0].file_system_url
.is_valid());
125 EXPECT_EQ(FILE_PATH_LITERAL("/native/cache/xxx"),
126 result
[1].file_path
.value());
127 EXPECT_EQ("display_name", result
[1].display_name
);
128 EXPECT_FALSE(result
[1].file_system_url
.is_valid());
130 EXPECT_EQ(FILE_PATH_LITERAL("/special/drive-test-user-hash/root/File 1.txt"),
131 result
[2].file_path
.value());
132 EXPECT_TRUE(result
[2].display_name
.empty());
133 EXPECT_TRUE(result
[2].file_system_url
.is_valid());
134 const storage::FileSystemURL url
=
135 context
->CrackURL(result
[2].file_system_url
);
136 EXPECT_EQ(GURL("http://example.com"), url
.origin());
137 EXPECT_EQ(storage::kFileSystemTypeIsolated
, url
.mount_type());
138 EXPECT_EQ(storage::kFileSystemTypeDrive
, url
.type());
139 EXPECT_EQ(26u, result
[2].length
);
144 } // namespace file_manager