Ensure tests have an active task runner
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system_util_unittest.cc
blob7bbbe0a0de5bad20292263779fb23087dc84fe98
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/file_system_util.h"
7 #include <vector>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chromeos/profiles/profile_helper.h"
14 #include "chrome/test/base/testing_browser_process.h"
15 #include "chrome/test/base/testing_profile.h"
16 #include "chrome/test/base/testing_profile_manager.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/test_file_system_options.h"
19 #include "google_apis/drive/test_util.h"
20 #include "storage/browser/fileapi/external_mount_points.h"
21 #include "storage/browser/fileapi/file_system_backend.h"
22 #include "storage/browser/fileapi/file_system_context.h"
23 #include "storage/browser/fileapi/file_system_url.h"
24 #include "storage/browser/fileapi/isolated_context.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace drive {
28 namespace util {
30 namespace {
32 // Sets up ProfileManager for testing and marks the current thread as UI by
33 // TestBrowserThreadBundle. We need the thread since Profile objects must be
34 // touched from UI and hence has CHECK/DCHECKs for it.
35 class ProfileRelatedFileSystemUtilTest : public testing::Test {
36 protected:
37 ProfileRelatedFileSystemUtilTest()
38 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {
41 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
43 TestingProfileManager& testing_profile_manager() {
44 return testing_profile_manager_;
47 private:
48 content::TestBrowserThreadBundle thread_bundle_;
49 TestingProfileManager testing_profile_manager_;
52 } // namespace
54 TEST_F(ProfileRelatedFileSystemUtilTest, GetDriveMountPointPath) {
55 Profile* profile = testing_profile_manager().CreateTestingProfile("user1");
56 const std::string user_id_hash =
57 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
58 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-" + user_id_hash),
59 GetDriveMountPointPath(profile));
62 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractProfileFromPath) {
63 Profile* profile1 = testing_profile_manager().CreateTestingProfile("user1");
64 Profile* profile2 = testing_profile_manager().CreateTestingProfile("user2");
65 const std::string user1_id_hash =
66 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
67 const std::string user2_id_hash =
68 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2");
69 EXPECT_EQ(profile1,
70 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
71 "/special/drive-" + user1_id_hash)));
72 EXPECT_EQ(profile2,
73 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
74 "/special/drive-" + user2_id_hash + "/root/xxx")));
75 EXPECT_EQ(NULL, ExtractProfileFromPath(
76 base::FilePath::FromUTF8Unsafe("/special/non-drive-path")));
79 class FileSystemUtilTest : public testing::Test {
80 content::TestBrowserThreadBundle thread_bundle_;
83 TEST_F(FileSystemUtilTest, IsUnderDriveMountPoint) {
84 EXPECT_FALSE(IsUnderDriveMountPoint(
85 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
86 EXPECT_FALSE(IsUnderDriveMountPoint(
87 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
88 EXPECT_FALSE(IsUnderDriveMountPoint(
89 base::FilePath::FromUTF8Unsafe("special/drive/foo.txt")));
91 EXPECT_TRUE(IsUnderDriveMountPoint(
92 base::FilePath::FromUTF8Unsafe("/special/drive")));
93 EXPECT_TRUE(IsUnderDriveMountPoint(
94 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
95 EXPECT_TRUE(IsUnderDriveMountPoint(
96 base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt")));
97 EXPECT_TRUE(IsUnderDriveMountPoint(
98 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
101 TEST_F(FileSystemUtilTest, ExtractDrivePath) {
102 EXPECT_EQ(base::FilePath(),
103 ExtractDrivePath(
104 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
105 EXPECT_EQ(base::FilePath(),
106 ExtractDrivePath(
107 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
109 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"),
110 ExtractDrivePath(
111 base::FilePath::FromUTF8Unsafe("/special/drive")));
112 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
113 ExtractDrivePath(
114 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
115 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"),
116 ExtractDrivePath(base::FilePath::FromUTF8Unsafe(
117 "/special/drive/subdir/foo.txt")));
118 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
119 ExtractDrivePath(
120 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
123 TEST_F(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
124 TestingProfile profile;
126 // Set up file system context for testing.
127 base::ScopedTempDir temp_dir_;
128 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
130 scoped_refptr<storage::ExternalMountPoints> mount_points =
131 storage::ExternalMountPoints::CreateRefCounted();
132 scoped_refptr<storage::FileSystemContext> context(
133 new storage::FileSystemContext(
134 base::MessageLoopProxy::current().get(),
135 base::MessageLoopProxy::current().get(),
136 mount_points.get(),
137 NULL, // special_storage_policy
138 NULL, // quota_manager_proxy,
139 ScopedVector<storage::FileSystemBackend>(),
140 std::vector<storage::URLRequestAutoMountHandler>(),
141 temp_dir_.path(), // partition_path
142 content::CreateAllowFileAccessOptions()));
144 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
145 const std::string& drive_mount_name =
146 GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe();
147 mount_points->RegisterFileSystem(drive_mount_name,
148 storage::kFileSystemTypeDrive,
149 storage::FileSystemMountOption(),
150 GetDriveMountPointPath(&profile));
151 EXPECT_EQ(
152 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
153 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
154 "filesystem:chrome-extension://dummy-id/external/" +
155 drive_mount_name + "/foo/bar"))));
157 // Virtual mount name should not affect the extracted path.
158 mount_points->RevokeFileSystem(drive_mount_name);
159 mount_points->RegisterFileSystem("drive2",
160 storage::kFileSystemTypeDrive,
161 storage::FileSystemMountOption(),
162 GetDriveMountPointPath(&profile));
163 EXPECT_EQ(
164 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
165 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
166 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
168 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path.
169 mount_points->RegisterFileSystem("Downloads",
170 storage::kFileSystemTypeNativeLocal,
171 storage::FileSystemMountOption(),
172 temp_dir_.path());
173 EXPECT_EQ(
174 base::FilePath(),
175 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
176 "filesystem:chrome-extension://dummy-id/external/Downloads/foo"))));
178 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path.
179 std::string isolated_name;
180 std::string isolated_id =
181 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
182 storage::kFileSystemTypeNativeForPlatformApp,
183 std::string(),
184 GetDriveMountPointPath(&profile).AppendASCII("bar/buz"),
185 &isolated_name);
186 EXPECT_EQ(
187 base::FilePath::FromUTF8Unsafe("drive/bar/buz"),
188 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
189 "filesystem:chrome-extension://dummy-id/isolated/" +
190 isolated_id + "/" + isolated_name))));
193 TEST_F(FileSystemUtilTest, EscapeUnescapeCacheFileName) {
194 const std::string kUnescapedFileName(
195 "tmp:`~!@#$%^&*()-_=+[{|]}\\\\;\',<.>/?");
196 const std::string kEscapedFileName(
197 "tmp:`~!@#$%25^&*()-_=+[{|]}\\\\;\',<%2E>%2F?");
198 EXPECT_EQ(kEscapedFileName, EscapeCacheFileName(kUnescapedFileName));
199 EXPECT_EQ(kUnescapedFileName, UnescapeCacheFileName(kEscapedFileName));
202 TEST_F(FileSystemUtilTest, NormalizeFileName) {
203 EXPECT_EQ("", NormalizeFileName(""));
204 EXPECT_EQ("foo", NormalizeFileName("foo"));
205 // Slash
206 EXPECT_EQ("foo_zzz", NormalizeFileName("foo/zzz"));
207 EXPECT_EQ("___", NormalizeFileName("///"));
208 // Japanese hiragana "hi" + semi-voiced-mark is normalized to "pi".
209 EXPECT_EQ("\xE3\x81\xB4", NormalizeFileName("\xE3\x81\xB2\xE3\x82\x9A"));
210 // Dot
211 EXPECT_EQ("_", NormalizeFileName("."));
212 EXPECT_EQ("_", NormalizeFileName(".."));
213 EXPECT_EQ("_", NormalizeFileName("..."));
214 EXPECT_EQ(".bashrc", NormalizeFileName(".bashrc"));
215 EXPECT_EQ("._", NormalizeFileName("./"));
218 TEST_F(FileSystemUtilTest, GetCacheRootPath) {
219 TestingProfile profile;
220 base::FilePath profile_path = profile.GetPath();
221 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"),
222 util::GetCacheRootPath(&profile));
225 TEST_F(FileSystemUtilTest, GDocFile) {
226 base::ScopedTempDir temp_dir;
227 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
229 GURL url("https://docs.google.com/document/d/"
230 "1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug/edit");
231 std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");
233 // Read and write gdoc.
234 base::FilePath file = temp_dir.path().AppendASCII("test.gdoc");
235 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
236 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
237 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
239 // Read and write gsheet.
240 file = temp_dir.path().AppendASCII("test.gsheet");
241 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
242 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
243 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
245 // Read and write gslides.
246 file = temp_dir.path().AppendASCII("test.gslides");
247 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
248 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
249 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
251 // Read and write gdraw.
252 file = temp_dir.path().AppendASCII("test.gdraw");
253 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
254 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
255 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
257 // Read and write gtable.
258 file = temp_dir.path().AppendASCII("test.gtable");
259 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
260 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
261 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
263 // Non GDoc file.
264 file = temp_dir.path().AppendASCII("test.txt");
265 std::string data = "Hello world!";
266 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(file, data));
267 EXPECT_TRUE(ReadUrlFromGDocFile(file).is_empty());
268 EXPECT_TRUE(ReadResourceIdFromGDocFile(file).empty());
271 } // namespace util
272 } // namespace drive