Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system_util_unittest.cc
blob4d288341531568f637bea6ae3793a5f0f451efe5
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/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/chromeos/profiles/profile_helper.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/testing_profile_manager.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "content/public/test/test_file_system_options.h"
21 #include "google_apis/drive/test_util.h"
22 #include "storage/browser/fileapi/external_mount_points.h"
23 #include "storage/browser/fileapi/file_system_backend.h"
24 #include "storage/browser/fileapi/file_system_context.h"
25 #include "storage/browser/fileapi/file_system_url.h"
26 #include "storage/browser/fileapi/isolated_context.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 namespace drive {
30 namespace util {
32 namespace {
34 // Sets up ProfileManager for testing and marks the current thread as UI by
35 // TestBrowserThreadBundle. We need the thread since Profile objects must be
36 // touched from UI and hence has CHECK/DCHECKs for it.
37 class ProfileRelatedFileSystemUtilTest : public testing::Test {
38 protected:
39 ProfileRelatedFileSystemUtilTest()
40 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()) {}
42 void SetUp() override { ASSERT_TRUE(testing_profile_manager_.SetUp()); }
44 TestingProfileManager& testing_profile_manager() {
45 return testing_profile_manager_;
48 private:
49 content::TestBrowserThreadBundle thread_bundle_;
50 TestingProfileManager testing_profile_manager_;
53 } // namespace
55 TEST_F(ProfileRelatedFileSystemUtilTest, GetDriveMountPointPath) {
56 Profile* profile = testing_profile_manager().CreateTestingProfile("user1");
57 const std::string user_id_hash =
58 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
59 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("/special/drive-" + user_id_hash),
60 GetDriveMountPointPath(profile));
63 TEST_F(ProfileRelatedFileSystemUtilTest, IsUnderDriveMountPoint) {
64 EXPECT_FALSE(IsUnderDriveMountPoint(
65 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
66 EXPECT_FALSE(IsUnderDriveMountPoint(
67 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
68 EXPECT_FALSE(IsUnderDriveMountPoint(
69 base::FilePath::FromUTF8Unsafe("special/drive/foo.txt")));
71 EXPECT_TRUE(
72 IsUnderDriveMountPoint(base::FilePath::FromUTF8Unsafe("/special/drive")));
73 EXPECT_TRUE(IsUnderDriveMountPoint(
74 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
75 EXPECT_TRUE(IsUnderDriveMountPoint(
76 base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt")));
77 EXPECT_TRUE(IsUnderDriveMountPoint(
78 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
81 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractDrivePath) {
82 EXPECT_EQ(
83 base::FilePath(),
84 ExtractDrivePath(base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
85 EXPECT_EQ(
86 base::FilePath(),
87 ExtractDrivePath(base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
89 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"),
90 ExtractDrivePath(base::FilePath::FromUTF8Unsafe("/special/drive")));
91 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
92 ExtractDrivePath(
93 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
94 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"),
95 ExtractDrivePath(base::FilePath::FromUTF8Unsafe(
96 "/special/drive/subdir/foo.txt")));
97 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
98 ExtractDrivePath(
99 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
102 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractProfileFromPath) {
103 Profile* profile1 = testing_profile_manager().CreateTestingProfile("user1");
104 Profile* profile2 = testing_profile_manager().CreateTestingProfile("user2");
105 const std::string user1_id_hash =
106 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
107 const std::string user2_id_hash =
108 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2");
109 EXPECT_EQ(profile1, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
110 "/special/drive-" + user1_id_hash)));
111 EXPECT_EQ(profile2, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
112 "/special/drive-" + user2_id_hash + "/root/xxx")));
113 EXPECT_EQ(NULL, ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
114 "/special/non-drive-path")));
117 TEST_F(ProfileRelatedFileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
118 TestingProfile profile;
120 // Set up file system context for testing.
121 base::ScopedTempDir temp_dir_;
122 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
124 scoped_refptr<storage::ExternalMountPoints> mount_points =
125 storage::ExternalMountPoints::CreateRefCounted();
126 scoped_refptr<storage::FileSystemContext> context(
127 new storage::FileSystemContext(
128 base::ThreadTaskRunnerHandle::Get().get(),
129 base::ThreadTaskRunnerHandle::Get().get(), mount_points.get(),
130 NULL, // special_storage_policy
131 NULL, // quota_manager_proxy,
132 ScopedVector<storage::FileSystemBackend>(),
133 std::vector<storage::URLRequestAutoMountHandler>(),
134 temp_dir_.path(), // partition_path
135 content::CreateAllowFileAccessOptions()));
137 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
138 const std::string& drive_mount_name =
139 GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe();
140 mount_points->RegisterFileSystem(
141 drive_mount_name, storage::kFileSystemTypeDrive,
142 storage::FileSystemMountOption(), GetDriveMountPointPath(&profile));
143 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
144 ExtractDrivePathFromFileSystemUrl(context->CrackURL(
145 GURL("filesystem:chrome-extension://dummy-id/external/" +
146 drive_mount_name + "/foo/bar"))));
148 // Virtual mount name should not affect the extracted path.
149 mount_points->RevokeFileSystem(drive_mount_name);
150 mount_points->RegisterFileSystem("drive2", storage::kFileSystemTypeDrive,
151 storage::FileSystemMountOption(),
152 GetDriveMountPointPath(&profile));
153 EXPECT_EQ(
154 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
155 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
156 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
158 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path.
159 mount_points->RegisterFileSystem(
160 "Downloads", storage::kFileSystemTypeNativeLocal,
161 storage::FileSystemMountOption(), temp_dir_.path());
162 EXPECT_EQ(
163 base::FilePath(),
164 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
165 "filesystem:chrome-extension://dummy-id/external/Downloads/foo"))));
167 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path.
168 std::string isolated_name;
169 std::string isolated_id =
170 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
171 storage::kFileSystemTypeNativeForPlatformApp, std::string(),
172 GetDriveMountPointPath(&profile).AppendASCII("bar/buz"),
173 &isolated_name);
174 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/bar/buz"),
175 ExtractDrivePathFromFileSystemUrl(context->CrackURL(
176 GURL("filesystem:chrome-extension://dummy-id/isolated/" +
177 isolated_id + "/" + isolated_name))));
180 TEST_F(ProfileRelatedFileSystemUtilTest, GetCacheRootPath) {
181 TestingProfile profile;
182 base::FilePath profile_path = profile.GetPath();
183 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"),
184 util::GetCacheRootPath(&profile));
187 } // namespace util
188 } // namespace drive