1 // Copyright (c) 2012 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 "webkit/fileapi/syncable/syncable_file_system_util.h"
7 #include "base/logging.h"
8 #include "base/message_loop.h"
9 #include "base/message_loop_proxy.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/browser/fileapi/external_mount_points.h"
12 #include "webkit/fileapi/syncable/canned_syncable_file_system.h"
13 #include "webkit/fileapi/syncable/local_file_sync_context.h"
15 using fileapi::ExternalMountPoints
;
16 using fileapi::FileSystemURL
;
17 using fileapi::ScopedExternalFileSystem
;
19 namespace sync_file_system
{
23 const char kSyncableFileSystemRootURI
[] =
24 "filesystem:http://www.example.com/external/service/";
25 const char kNonRegisteredFileSystemRootURI
[] =
26 "filesystem:http://www.example.com/external/non_registered/";
27 const char kNonSyncableFileSystemRootURI
[] =
28 "filesystem:http://www.example.com/temporary/";
30 const char kOrigin
[] = "http://www.example.com/";
31 const char kServiceName
[] = "service";
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
), kServiceName
);
46 EXPECT_TRUE(root
.is_valid());
47 EXPECT_EQ(GURL(kSyncableFileSystemRootURI
), root
);
50 TEST(SyncableFileSystemUtilTest
, CreateSyncableFileSystemURL
) {
51 ScopedExternalFileSystem
scoped_fs(
52 kServiceName
, fileapi::kFileSystemTypeSyncable
, base::FilePath());
54 const base::FilePath
path(kPath
);
55 const FileSystemURL expected_url
=
56 CreateFileSystemURL(kSyncableFileSystemRootURI
+ path
.AsUTF8Unsafe());
57 const FileSystemURL url
=
58 CreateSyncableFileSystemURL(GURL(kOrigin
), kServiceName
, path
);
60 EXPECT_TRUE(url
.is_valid());
61 EXPECT_EQ(expected_url
, url
);
64 TEST(SyncableFileSystemUtilTest
,
65 SerializeAndDesirializeSyncableFileSystemURL
) {
66 ScopedExternalFileSystem
scoped_fs(
67 kServiceName
, fileapi::kFileSystemTypeSyncable
, base::FilePath());
69 const std::string expected_url_str
= kSyncableFileSystemRootURI
+
70 CreateNormalizedFilePath(kPath
).AsUTF8Unsafe();
71 const FileSystemURL expected_url
= CreateFileSystemURL(expected_url_str
);
72 const FileSystemURL url
= CreateSyncableFileSystemURL(
73 GURL(kOrigin
), kServiceName
, base::FilePath(kPath
));
75 std::string serialized
;
76 EXPECT_TRUE(SerializeSyncableFileSystemURL(url
, &serialized
));
77 EXPECT_EQ(expected_url_str
, serialized
);
79 FileSystemURL deserialized
;
80 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized
, &deserialized
));
81 EXPECT_TRUE(deserialized
.is_valid());
82 EXPECT_EQ(expected_url
, deserialized
);
85 TEST(SyncableFileSystemUtilTest
,
86 FailInSerializingAndDeserializingSyncableFileSystemURL
) {
87 ScopedExternalFileSystem
scoped_fs(
88 kServiceName
, fileapi::kFileSystemTypeSyncable
, base::FilePath());
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
));
113 TEST(SyncableFileSystemUtilTest
, SerializeBeforeOpenFileSystem
) {
114 const std::string serialized
= kSyncableFileSystemRootURI
+
115 CreateNormalizedFilePath(kPath
).AsUTF8Unsafe();
116 FileSystemURL deserialized
;
117 base::MessageLoop message_loop
;
119 // Setting up a full syncable filesystem environment.
120 CannedSyncableFileSystem
file_system(GURL(kOrigin
), kServiceName
,
121 base::MessageLoopProxy::current(),
122 base::MessageLoopProxy::current());
124 scoped_refptr
<LocalFileSyncContext
> sync_context
=
125 new LocalFileSyncContext(base::MessageLoopProxy::current(),
126 base::MessageLoopProxy::current());
128 // Before calling initialization we would not be able to get a valid
130 EXPECT_FALSE(DeserializeSyncableFileSystemURL(serialized
, &deserialized
));
131 EXPECT_FALSE(deserialized
.is_valid());
133 ASSERT_EQ(sync_file_system::SYNC_STATUS_OK
,
134 file_system
.MaybeInitializeFileSystemContext(sync_context
));
136 // After initialization this should be ok (even before opening the file
138 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized
, &deserialized
));
139 EXPECT_TRUE(deserialized
.is_valid());
142 file_system
.TearDown();
143 RevokeSyncableFileSystem(kServiceName
);
144 sync_context
->ShutdownOnUIThread();
146 base::MessageLoop::current()->RunUntilIdle();
149 } // namespace sync_file_system