OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/file_system_url.h" | 5 #include "webkit/fileapi/file_system_url.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "webkit/browser/fileapi/external_mount_points.h" | |
11 #include "webkit/browser/fileapi/isolated_context.h" | |
12 #include "webkit/fileapi/file_system_types.h" | 10 #include "webkit/fileapi/file_system_types.h" |
13 #include "webkit/fileapi/file_system_util.h" | 11 #include "webkit/fileapi/file_system_util.h" |
14 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 12 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
15 | 13 |
16 #define FPL FILE_PATH_LITERAL | 14 #define FPL FILE_PATH_LITERAL |
17 | 15 |
18 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 16 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
19 #define DRIVE FPL("C:") | 17 #define DRIVE FPL("C:") |
20 #else | 18 #else |
21 #define DRIVE FPL("/a/") | 19 #define DRIVE FPL("/a/") |
22 #endif | 20 #endif |
23 | 21 |
24 namespace fileapi { | 22 namespace fileapi { |
25 | 23 |
26 namespace { | 24 namespace { |
27 | 25 |
28 FileSystemURL CreateFileSystemURL(const std::string& url_string) { | 26 FileSystemURL CreateFileSystemURL(const std::string& url_string) { |
29 FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string)); | 27 FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string)); |
30 switch (url.type()) { | 28 EXPECT_TRUE(url.type() != kFileSystemTypeExternal && |
31 case kFileSystemTypeExternal: | 29 url.type() != kFileSystemTypeIsolated); |
32 return ExternalMountPoints::GetSystemInstance()-> | 30 return url; |
33 CreateCrackedFileSystemURL(url.origin(), url.type(), url.path()); | |
34 case kFileSystemTypeIsolated: | |
35 return IsolatedContext::GetInstance()->CreateCrackedFileSystemURL( | |
36 url.origin(), url.type(), url.path()); | |
37 default: | |
38 return url; | |
39 } | |
40 } | |
41 | |
42 FileSystemURL CreateExternalFileSystemURL(const GURL& origin, | |
43 FileSystemType type, | |
44 const base::FilePath& path) { | |
45 return ExternalMountPoints::GetSystemInstance()->CreateCrackedFileSystemURL( | |
46 origin, type, path); | |
47 } | 31 } |
48 | 32 |
49 std::string NormalizedUTF8Path(const base::FilePath& path) { | 33 std::string NormalizedUTF8Path(const base::FilePath& path) { |
50 return path.NormalizePathSeparators().AsUTF8Unsafe(); | 34 return path.NormalizePathSeparators().AsUTF8Unsafe(); |
51 } | 35 } |
52 | 36 |
53 } // namespace | 37 } // namespace |
54 | 38 |
55 TEST(FileSystemURLTest, ParsePersistent) { | 39 TEST(FileSystemURLTest, ParsePersistent) { |
56 FileSystemURL url = CreateFileSystemURL( | 40 FileSystemURL url = CreateFileSystemURL( |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 116 |
133 const FileSystemURL a = CreateFileSystemURL( | 117 const FileSystemURL a = CreateFileSystemURL( |
134 "filesystem:http://chromium.org/temporary/dir a/file a"); | 118 "filesystem:http://chromium.org/temporary/dir a/file a"); |
135 const FileSystemURL b = CreateFileSystemURL( | 119 const FileSystemURL b = CreateFileSystemURL( |
136 "filesystem:http://chromium.org/persistent/dir a/file a"); | 120 "filesystem:http://chromium.org/persistent/dir a/file a"); |
137 EXPECT_EQ(a.type() < b.type(), compare(a, b)); | 121 EXPECT_EQ(a.type() < b.type(), compare(a, b)); |
138 EXPECT_EQ(b.type() < a.type(), compare(b, a)); | 122 EXPECT_EQ(b.type() < a.type(), compare(b, a)); |
139 } | 123 } |
140 | 124 |
141 TEST(FileSystemURLTest, IsParent) { | 125 TEST(FileSystemURLTest, IsParent) { |
142 ScopedExternalFileSystem scoped1("foo", kFileSystemTypeSyncable, | |
143 base::FilePath()); | |
144 ScopedExternalFileSystem scoped2("bar", kFileSystemTypeSyncable, | |
145 base::FilePath()); | |
146 | |
147 const std::string root1 = GetFileSystemRootURI( | 126 const std::string root1 = GetFileSystemRootURI( |
148 GURL("http://example.com"), kFileSystemTypeTemporary).spec(); | 127 GURL("http://example.com"), kFileSystemTypeTemporary).spec(); |
149 const std::string root2 = sync_file_system::GetSyncableFileSystemRootURI( | 128 const std::string root2 = GetFileSystemRootURI( |
150 GURL("http://example.com"), "foo").spec(); | 129 GURL("http://example.com"), kFileSystemTypePersistent).spec(); |
151 const std::string root3 = sync_file_system::GetSyncableFileSystemRootURI( | 130 const std::string root3 = GetFileSystemRootURI( |
152 GURL("http://example.com"), "bar").spec(); | |
153 const std::string root4 = GetFileSystemRootURI( | |
154 GURL("http://chromium.org"), kFileSystemTypeTemporary).spec(); | 131 GURL("http://chromium.org"), kFileSystemTypeTemporary).spec(); |
155 | 132 |
156 const std::string parent("dir"); | 133 const std::string parent("dir"); |
157 const std::string child("dir/child"); | 134 const std::string child("dir/child"); |
158 const std::string other("other"); | 135 const std::string other("other"); |
159 | 136 |
160 // True cases. | 137 // True cases. |
161 EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent( | 138 EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent( |
162 CreateFileSystemURL(root1 + child))); | 139 CreateFileSystemURL(root1 + child))); |
163 EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent( | 140 EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent( |
164 CreateFileSystemURL(root2 + child))); | 141 CreateFileSystemURL(root2 + child))); |
165 | 142 |
166 // False cases: the path is not a child. | 143 // False cases: the path is not a child. |
167 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | 144 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( |
168 CreateFileSystemURL(root1 + other))); | 145 CreateFileSystemURL(root1 + other))); |
169 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | 146 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( |
170 CreateFileSystemURL(root1 + parent))); | 147 CreateFileSystemURL(root1 + parent))); |
171 EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent( | 148 EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent( |
172 CreateFileSystemURL(root1 + parent))); | 149 CreateFileSystemURL(root1 + parent))); |
173 | 150 |
174 // False case: different types. | 151 // False case: different types. |
175 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | 152 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( |
176 CreateFileSystemURL(root2 + child))); | 153 CreateFileSystemURL(root2 + child))); |
177 | 154 |
178 // False case: different filesystem ID. | 155 // False case: different origins. |
179 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | 156 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( |
180 CreateFileSystemURL(root3 + child))); | 157 CreateFileSystemURL(root3 + child))); |
181 | |
182 // False case: different origins. | |
183 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
184 CreateFileSystemURL(root4 + child))); | |
185 } | 158 } |
186 | 159 |
187 TEST(FileSystemURLTest, DebugString) { | 160 TEST(FileSystemURLTest, DebugString) { |
188 const GURL kOrigin("http://example.com"); | 161 const GURL kOrigin("http://example.com"); |
189 const base::FilePath kPath(FPL("dir/file")); | 162 const base::FilePath kPath(FPL("dir/file")); |
190 | 163 |
191 const FileSystemURL kURL1 = FileSystemURL::CreateForTest( | 164 const FileSystemURL kURL1 = FileSystemURL::CreateForTest( |
192 kOrigin, kFileSystemTypeTemporary, kPath); | 165 kOrigin, kFileSystemTypeTemporary, kPath); |
193 EXPECT_EQ("filesystem:http://example.com/temporary/" + | 166 EXPECT_EQ("filesystem:http://example.com/temporary/" + |
194 NormalizedUTF8Path(kPath), | 167 NormalizedUTF8Path(kPath), |
195 kURL1.DebugString()); | 168 kURL1.DebugString()); |
196 | |
197 const base::FilePath kRoot(DRIVE FPL("/root")); | |
198 ScopedExternalFileSystem scoped_fs("foo", | |
199 kFileSystemTypeNativeLocal, | |
200 kRoot.NormalizePathSeparators()); | |
201 const FileSystemURL kURL2(CreateExternalFileSystemURL( | |
202 kOrigin, | |
203 kFileSystemTypeExternal, | |
204 scoped_fs.GetVirtualRootPath().Append(kPath))); | |
205 EXPECT_EQ("filesystem:http://example.com/external/" + | |
206 NormalizedUTF8Path(scoped_fs.GetVirtualRootPath().Append(kPath)) + | |
207 " (NativeLocal@foo:" + | |
208 NormalizedUTF8Path(kRoot.Append(kPath)) + ")", | |
209 kURL2.DebugString()); | |
210 } | 169 } |
211 | 170 |
212 } // namespace fileapi | 171 } // namespace fileapi |
OLD | NEW |