| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | |
| 6 | |
| 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/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" | |
| 14 | |
| 15 using fileapi::ExternalMountPoints; | |
| 16 using fileapi::FileSystemURL; | |
| 17 using fileapi::ScopedExternalFileSystem; | |
| 18 | |
| 19 namespace sync_file_system { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 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/"; | |
| 29 | |
| 30 const char kOrigin[] = "http://www.example.com/"; | |
| 31 const char kServiceName[] = "service"; | |
| 32 const base::FilePath::CharType kPath[] = FILE_PATH_LITERAL("dir/file"); | |
| 33 | |
| 34 FileSystemURL CreateFileSystemURL(const std::string& url) { | |
| 35 return ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(url)); | |
| 36 } | |
| 37 | |
| 38 base::FilePath CreateNormalizedFilePath(const base::FilePath::CharType* path) { | |
| 39 return base::FilePath(path).NormalizePathSeparators(); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 TEST(SyncableFileSystemUtilTest, GetSyncableFileSystemRootURI) { | |
| 45 const GURL root = GetSyncableFileSystemRootURI(GURL(kOrigin), kServiceName); | |
| 46 EXPECT_TRUE(root.is_valid()); | |
| 47 EXPECT_EQ(GURL(kSyncableFileSystemRootURI), root); | |
| 48 } | |
| 49 | |
| 50 TEST(SyncableFileSystemUtilTest, CreateSyncableFileSystemURL) { | |
| 51 ScopedExternalFileSystem scoped_fs( | |
| 52 kServiceName, fileapi::kFileSystemTypeSyncable, base::FilePath()); | |
| 53 | |
| 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); | |
| 59 | |
| 60 EXPECT_TRUE(url.is_valid()); | |
| 61 EXPECT_EQ(expected_url, url); | |
| 62 } | |
| 63 | |
| 64 TEST(SyncableFileSystemUtilTest, | |
| 65 SerializeAndDesirializeSyncableFileSystemURL) { | |
| 66 ScopedExternalFileSystem scoped_fs( | |
| 67 kServiceName, fileapi::kFileSystemTypeSyncable, base::FilePath()); | |
| 68 | |
| 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)); | |
| 74 | |
| 75 std::string serialized; | |
| 76 EXPECT_TRUE(SerializeSyncableFileSystemURL(url, &serialized)); | |
| 77 EXPECT_EQ(expected_url_str, serialized); | |
| 78 | |
| 79 FileSystemURL deserialized; | |
| 80 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized, &deserialized)); | |
| 81 EXPECT_TRUE(deserialized.is_valid()); | |
| 82 EXPECT_EQ(expected_url, deserialized); | |
| 83 } | |
| 84 | |
| 85 TEST(SyncableFileSystemUtilTest, | |
| 86 FailInSerializingAndDeserializingSyncableFileSystemURL) { | |
| 87 ScopedExternalFileSystem scoped_fs( | |
| 88 kServiceName, fileapi::kFileSystemTypeSyncable, base::FilePath()); | |
| 89 | |
| 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(); | |
| 95 | |
| 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)); | |
| 103 | |
| 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)); | |
| 111 } | |
| 112 | |
| 113 TEST(SyncableFileSystemUtilTest, SerializeBeforeOpenFileSystem) { | |
| 114 const std::string serialized = kSyncableFileSystemRootURI + | |
| 115 CreateNormalizedFilePath(kPath).AsUTF8Unsafe(); | |
| 116 FileSystemURL deserialized; | |
| 117 base::MessageLoop message_loop; | |
| 118 | |
| 119 // Setting up a full syncable filesystem environment. | |
| 120 CannedSyncableFileSystem file_system(GURL(kOrigin), kServiceName, | |
| 121 base::MessageLoopProxy::current(), | |
| 122 base::MessageLoopProxy::current()); | |
| 123 file_system.SetUp(); | |
| 124 scoped_refptr<LocalFileSyncContext> sync_context = | |
| 125 new LocalFileSyncContext(base::MessageLoopProxy::current(), | |
| 126 base::MessageLoopProxy::current()); | |
| 127 | |
| 128 // Before calling initialization we would not be able to get a valid | |
| 129 // deserialized URL. | |
| 130 EXPECT_FALSE(DeserializeSyncableFileSystemURL(serialized, &deserialized)); | |
| 131 EXPECT_FALSE(deserialized.is_valid()); | |
| 132 | |
| 133 ASSERT_EQ(sync_file_system::SYNC_STATUS_OK, | |
| 134 file_system.MaybeInitializeFileSystemContext(sync_context)); | |
| 135 | |
| 136 // After initialization this should be ok (even before opening the file | |
| 137 // system). | |
| 138 EXPECT_TRUE(DeserializeSyncableFileSystemURL(serialized, &deserialized)); | |
| 139 EXPECT_TRUE(deserialized.is_valid()); | |
| 140 | |
| 141 // Shutting down. | |
| 142 file_system.TearDown(); | |
| 143 RevokeSyncableFileSystem(kServiceName); | |
| 144 sync_context->ShutdownOnUIThread(); | |
| 145 sync_context = NULL; | |
| 146 base::MessageLoop::current()->RunUntilIdle(); | |
| 147 } | |
| 148 | |
| 149 } // namespace sync_file_system | |
| OLD | NEW |