1 // Copyright 2013 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/sync_file_system/syncable_file_system_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
12 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webkit/browser/fileapi/external_mount_points.h"
15 #include "webkit/common/fileapi/file_system_types.h"
17 using fileapi::ExternalMountPoints
;
18 using fileapi::FileSystemURL
;
20 namespace sync_file_system
{
24 const char kSyncableFileSystemRootURI
[] =
25 "filesystem:http://www.example.com/external/syncfs/";
26 const char kNonRegisteredFileSystemRootURI
[] =
27 "filesystem:http://www.example.com/external/non_registered/";
28 const char kNonSyncableFileSystemRootURI
[] =
29 "filesystem:http://www.example.com/temporary/";
31 const char kOrigin
[] = "http://www.example.com/";
32 const base::FilePath::CharType kPath
[] = FILE_PATH_LITERAL("dir/file");
34 FileSystemURL
CreateFileSystemURL(const std::string
& url
) {
35 return ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(url
));
38 base::FilePath
CreateNormalizedFilePath(const base::FilePath::CharType
* path
) {
39 return base::FilePath(path
).NormalizePathSeparators();
44 TEST(SyncableFileSystemUtilTest
, GetSyncableFileSystemRootURI
) {
45 const GURL root
= GetSyncableFileSystemRootURI(GURL(kOrigin
));
46 EXPECT_TRUE(root
.is_valid());
47 EXPECT_EQ(GURL(kSyncableFileSystemRootURI
), root
);
50 TEST(SyncableFileSystemUtilTest
, CreateSyncableFileSystemURL
) {
51 RegisterSyncableFileSystem();
53 const base::FilePath
path(kPath
);
54 const FileSystemURL expected_url
=
55 CreateFileSystemURL(kSyncableFileSystemRootURI
+ path
.AsUTF8Unsafe());
56 const FileSystemURL url
= CreateSyncableFileSystemURL(GURL(kOrigin
), path
);
58 EXPECT_TRUE(url
.is_valid());
59 EXPECT_EQ(expected_url
, url
);
61 RevokeSyncableFileSystem();
64 TEST(SyncableFileSystemUtilTest
,
65 SerializeAndDesirializeSyncableFileSystemURL
) {
66 RegisterSyncableFileSystem();
68 const std::string expected_url_str
= kSyncableFileSystemRootURI
+
69 CreateNormalizedFilePath(kPath
).AsUTF8Unsafe();
70 const FileSystemURL expected_url
= CreateFileSystemURL(expected_url_str
);
71 const FileSystemURL url
= CreateSyncableFileSystemURL(
72 GURL(kOrigin
), base::FilePath(kPath
));
74 std::string serialized
;
75 EXPECT_TRUE(SerializeSyncableFileSystemURL(url
, &serialized
));
76 EXPECT_EQ(expected_url_str
, serialized
);
78 FileSystemURL deserialized
;
79 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized
, &deserialized
));
80 EXPECT_TRUE(deserialized
.is_valid());
81 EXPECT_EQ(expected_url
, deserialized
);
83 RevokeSyncableFileSystem();
86 TEST(SyncableFileSystemUtilTest
,
87 FailInSerializingAndDeserializingSyncableFileSystemURL
) {
88 RegisterSyncableFileSystem();
90 const base::FilePath normalized_path
= CreateNormalizedFilePath(kPath
);
91 const std::string non_registered_url
=
92 kNonRegisteredFileSystemRootURI
+ normalized_path
.AsUTF8Unsafe();
93 const std::string non_syncable_url
=
94 kNonSyncableFileSystemRootURI
+ normalized_path
.AsUTF8Unsafe();
96 // Expected to fail in serializing URLs of non-registered filesystem and
97 // non-syncable filesystem.
98 std::string serialized
;
99 EXPECT_FALSE(SerializeSyncableFileSystemURL(
100 CreateFileSystemURL(non_registered_url
), &serialized
));
101 EXPECT_FALSE(SerializeSyncableFileSystemURL(
102 CreateFileSystemURL(non_syncable_url
), &serialized
));
104 // Expected to fail in deserializing a string that represents URLs of
105 // non-registered filesystem and non-syncable filesystem.
106 FileSystemURL deserialized
;
107 EXPECT_FALSE(DeserializeSyncableFileSystemURL(
108 non_registered_url
, &deserialized
));
109 EXPECT_FALSE(DeserializeSyncableFileSystemURL(
110 non_syncable_url
, &deserialized
));
112 RevokeSyncableFileSystem();
115 TEST(SyncableFileSystemUtilTest
, SyncableFileSystemURL_IsParent
) {
116 RegisterSyncableFileSystem();
118 const std::string root1
= sync_file_system::GetSyncableFileSystemRootURI(
119 GURL("http://foo.com")).spec();
120 const std::string root2
= sync_file_system::GetSyncableFileSystemRootURI(
121 GURL("http://bar.com")).spec();
123 const std::string
parent("dir");
124 const std::string
child("dir/child");
127 EXPECT_TRUE(CreateFileSystemURL(root1
+ parent
).IsParent(
128 CreateFileSystemURL(root1
+ child
)));
129 EXPECT_TRUE(CreateFileSystemURL(root2
+ parent
).IsParent(
130 CreateFileSystemURL(root2
+ child
)));
132 // False case: different origin.
133 EXPECT_FALSE(CreateFileSystemURL(root1
+ parent
).IsParent(
134 CreateFileSystemURL(root2
+ child
)));
135 EXPECT_FALSE(CreateFileSystemURL(root2
+ parent
).IsParent(
136 CreateFileSystemURL(root1
+ child
)));
138 RevokeSyncableFileSystem();
141 } // namespace sync_file_system