Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system_util_unittest.cc
blob500ac524c5fd9f756602bed1b48f31baf6813372
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/message_loop/message_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "chrome/test/base/testing_profile_manager.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "content/public/test/test_file_system_options.h"
20 #include "google_apis/drive/test_util.h"
21 #include "storage/browser/fileapi/external_mount_points.h"
22 #include "storage/browser/fileapi/file_system_backend.h"
23 #include "storage/browser/fileapi/file_system_context.h"
24 #include "storage/browser/fileapi/file_system_url.h"
25 #include "storage/browser/fileapi/isolated_context.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 namespace drive {
29 namespace util {
31 namespace {
33 // Sets up ProfileManager for testing and marks the current thread as UI by
34 // TestBrowserThreadBundle. We need the thread since Profile objects must be
35 // touched from UI and hence has CHECK/DCHECKs for it.
36 class ProfileRelatedFileSystemUtilTest : public testing::Test {
37 protected:
38 ProfileRelatedFileSystemUtilTest()
39 : 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, ExtractProfileFromPath) {
64 Profile* profile1 = testing_profile_manager().CreateTestingProfile("user1");
65 Profile* profile2 = testing_profile_manager().CreateTestingProfile("user2");
66 const std::string user1_id_hash =
67 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user1");
68 const std::string user2_id_hash =
69 chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting("user2");
70 EXPECT_EQ(profile1,
71 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
72 "/special/drive-" + user1_id_hash)));
73 EXPECT_EQ(profile2,
74 ExtractProfileFromPath(base::FilePath::FromUTF8Unsafe(
75 "/special/drive-" + user2_id_hash + "/root/xxx")));
76 EXPECT_EQ(NULL, ExtractProfileFromPath(
77 base::FilePath::FromUTF8Unsafe("/special/non-drive-path")));
80 TEST(FileSystemUtilTest, IsUnderDriveMountPoint) {
81 EXPECT_FALSE(IsUnderDriveMountPoint(
82 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
83 EXPECT_FALSE(IsUnderDriveMountPoint(
84 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
85 EXPECT_FALSE(IsUnderDriveMountPoint(
86 base::FilePath::FromUTF8Unsafe("special/drive/foo.txt")));
88 EXPECT_TRUE(IsUnderDriveMountPoint(
89 base::FilePath::FromUTF8Unsafe("/special/drive")));
90 EXPECT_TRUE(IsUnderDriveMountPoint(
91 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
92 EXPECT_TRUE(IsUnderDriveMountPoint(
93 base::FilePath::FromUTF8Unsafe("/special/drive/subdir/foo.txt")));
94 EXPECT_TRUE(IsUnderDriveMountPoint(
95 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
98 TEST(FileSystemUtilTest, ExtractDrivePath) {
99 EXPECT_EQ(base::FilePath(),
100 ExtractDrivePath(
101 base::FilePath::FromUTF8Unsafe("/wherever/foo.txt")));
102 EXPECT_EQ(base::FilePath(),
103 ExtractDrivePath(
104 base::FilePath::FromUTF8Unsafe("/special/foo.txt")));
106 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"),
107 ExtractDrivePath(
108 base::FilePath::FromUTF8Unsafe("/special/drive")));
109 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
110 ExtractDrivePath(
111 base::FilePath::FromUTF8Unsafe("/special/drive/foo.txt")));
112 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/subdir/foo.txt"),
113 ExtractDrivePath(base::FilePath::FromUTF8Unsafe(
114 "/special/drive/subdir/foo.txt")));
115 EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/foo.txt"),
116 ExtractDrivePath(
117 base::FilePath::FromUTF8Unsafe("/special/drive-xxx/foo.txt")));
120 TEST(FileSystemUtilTest, ExtractDrivePathFromFileSystemUrl) {
121 TestingProfile profile;
123 // Set up file system context for testing.
124 base::ScopedTempDir temp_dir_;
125 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
127 base::MessageLoop message_loop;
128 scoped_refptr<storage::ExternalMountPoints> mount_points =
129 storage::ExternalMountPoints::CreateRefCounted();
130 scoped_refptr<storage::FileSystemContext> context(
131 new storage::FileSystemContext(
132 base::MessageLoopProxy::current().get(),
133 base::MessageLoopProxy::current().get(),
134 mount_points.get(),
135 NULL, // special_storage_policy
136 NULL, // quota_manager_proxy,
137 ScopedVector<storage::FileSystemBackend>(),
138 std::vector<storage::URLRequestAutoMountHandler>(),
139 temp_dir_.path(), // partition_path
140 content::CreateAllowFileAccessOptions()));
142 // Type:"external" + virtual_path:"drive/foo/bar" resolves to "drive/foo/bar".
143 const std::string& drive_mount_name =
144 GetDriveMountPointPath(&profile).BaseName().AsUTF8Unsafe();
145 mount_points->RegisterFileSystem(drive_mount_name,
146 storage::kFileSystemTypeDrive,
147 storage::FileSystemMountOption(),
148 GetDriveMountPointPath(&profile));
149 EXPECT_EQ(
150 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
151 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
152 "filesystem:chrome-extension://dummy-id/external/" +
153 drive_mount_name + "/foo/bar"))));
155 // Virtual mount name should not affect the extracted path.
156 mount_points->RevokeFileSystem(drive_mount_name);
157 mount_points->RegisterFileSystem("drive2",
158 storage::kFileSystemTypeDrive,
159 storage::FileSystemMountOption(),
160 GetDriveMountPointPath(&profile));
161 EXPECT_EQ(
162 base::FilePath::FromUTF8Unsafe("drive/foo/bar"),
163 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
164 "filesystem:chrome-extension://dummy-id/external/drive2/foo/bar"))));
166 // Type:"external" + virtual_path:"Downloads/foo" is not a Drive path.
167 mount_points->RegisterFileSystem("Downloads",
168 storage::kFileSystemTypeNativeLocal,
169 storage::FileSystemMountOption(),
170 temp_dir_.path());
171 EXPECT_EQ(
172 base::FilePath(),
173 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
174 "filesystem:chrome-extension://dummy-id/external/Downloads/foo"))));
176 // Type:"isolated" + virtual_path:"isolated_id/name" mapped on a Drive path.
177 std::string isolated_name;
178 std::string isolated_id =
179 storage::IsolatedContext::GetInstance()->RegisterFileSystemForPath(
180 storage::kFileSystemTypeNativeForPlatformApp,
181 std::string(),
182 GetDriveMountPointPath(&profile).AppendASCII("bar/buz"),
183 &isolated_name);
184 EXPECT_EQ(
185 base::FilePath::FromUTF8Unsafe("drive/bar/buz"),
186 ExtractDrivePathFromFileSystemUrl(context->CrackURL(GURL(
187 "filesystem:chrome-extension://dummy-id/isolated/" +
188 isolated_id + "/" + isolated_name))));
191 TEST(FileSystemUtilTest, EscapeUnescapeCacheFileName) {
192 const std::string kUnescapedFileName(
193 "tmp:`~!@#$%^&*()-_=+[{|]}\\\\;\',<.>/?");
194 const std::string kEscapedFileName(
195 "tmp:`~!@#$%25^&*()-_=+[{|]}\\\\;\',<%2E>%2F?");
196 EXPECT_EQ(kEscapedFileName, EscapeCacheFileName(kUnescapedFileName));
197 EXPECT_EQ(kUnescapedFileName, UnescapeCacheFileName(kEscapedFileName));
200 TEST(FileSystemUtilTest, NormalizeFileName) {
201 EXPECT_EQ("", NormalizeFileName(""));
202 EXPECT_EQ("foo", NormalizeFileName("foo"));
203 // Slash
204 EXPECT_EQ("foo_zzz", NormalizeFileName("foo/zzz"));
205 EXPECT_EQ("___", NormalizeFileName("///"));
206 // Japanese hiragana "hi" + semi-voiced-mark is normalized to "pi".
207 EXPECT_EQ("\xE3\x81\xB4", NormalizeFileName("\xE3\x81\xB2\xE3\x82\x9A"));
208 // Dot
209 EXPECT_EQ("_", NormalizeFileName("."));
210 EXPECT_EQ("_", NormalizeFileName(".."));
211 EXPECT_EQ("_", NormalizeFileName("..."));
212 EXPECT_EQ(".bashrc", NormalizeFileName(".bashrc"));
213 EXPECT_EQ("._", NormalizeFileName("./"));
216 TEST(FileSystemUtilTest, GetCacheRootPath) {
217 TestingProfile profile;
218 base::FilePath profile_path = profile.GetPath();
219 EXPECT_EQ(profile_path.AppendASCII("GCache/v1"),
220 util::GetCacheRootPath(&profile));
223 TEST(FileSystemUtilTest, GDocFile) {
224 base::ScopedTempDir temp_dir;
225 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
227 GURL url("https://docs.google.com/document/d/"
228 "1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug/edit");
229 std::string resource_id("1YsCnrMxxgp7LDdtlFDt-WdtEIth89vA9inrILtvK-Ug");
231 // Read and write gdoc.
232 base::FilePath file = temp_dir.path().AppendASCII("test.gdoc");
233 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
234 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
235 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
237 // Read and write gsheet.
238 file = temp_dir.path().AppendASCII("test.gsheet");
239 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
240 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
241 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
243 // Read and write gslides.
244 file = temp_dir.path().AppendASCII("test.gslides");
245 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
246 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
247 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
249 // Read and write gdraw.
250 file = temp_dir.path().AppendASCII("test.gdraw");
251 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
252 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
253 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
255 // Read and write gtable.
256 file = temp_dir.path().AppendASCII("test.gtable");
257 EXPECT_TRUE(CreateGDocFile(file, url, resource_id));
258 EXPECT_EQ(url, ReadUrlFromGDocFile(file));
259 EXPECT_EQ(resource_id, ReadResourceIdFromGDocFile(file));
261 // Non GDoc file.
262 file = temp_dir.path().AppendASCII("test.txt");
263 std::string data = "Hello world!";
264 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(file, data));
265 EXPECT_TRUE(ReadUrlFromGDocFile(file).is_empty());
266 EXPECT_TRUE(ReadResourceIdFromGDocFile(file).empty());
269 } // namespace util
270 } // namespace drive