OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_util.h" | 5 #include "webkit/fileapi/file_system_util.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/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/fileapi/file_system_types.h" | 10 #include "webkit/fileapi/file_system_types.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 GURL origin_url_; | 22 GURL origin_url_; |
23 FileSystemType type_; | 23 FileSystemType type_; |
24 FilePath file_path_; | 24 FilePath file_path_; |
25 }; | 25 }; |
26 | 26 |
27 TEST_F(FileSystemUtilTest, ParsePersistent) { | 27 TEST_F(FileSystemUtilTest, ParsePersistent) { |
28 ASSERT_TRUE(CrackFileSystemURL( | 28 ASSERT_TRUE(CrackFileSystemURL( |
29 "filesystem:http://chromium.org/persistent/directory/file")); | 29 "filesystem:http://chromium.org/persistent/directory/file")); |
30 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); | 30 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); |
31 EXPECT_EQ(kFileSystemTypePersistent, type_); | 31 EXPECT_EQ(kFileSystemTypePersistent, type_); |
32 EXPECT_EQ(FILE_PATH_LITERAL("file"), file_path_.BaseName().value()); | 32 EXPECT_EQ(FILE_PATH_LITERAL("file"), |
| 33 VirtualPath::BaseName(file_path_).value()); |
33 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); | 34 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); |
34 } | 35 } |
35 | 36 |
36 TEST_F(FileSystemUtilTest, ParseTemporary) { | 37 TEST_F(FileSystemUtilTest, ParseTemporary) { |
37 ASSERT_TRUE(CrackFileSystemURL( | 38 ASSERT_TRUE(CrackFileSystemURL( |
38 "filesystem:http://chromium.org/temporary/directory/file")); | 39 "filesystem:http://chromium.org/temporary/directory/file")); |
39 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); | 40 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); |
40 EXPECT_EQ(kFileSystemTypeTemporary, type_); | 41 EXPECT_EQ(kFileSystemTypeTemporary, type_); |
41 EXPECT_EQ(FILE_PATH_LITERAL("file"), file_path_.BaseName().value()); | 42 EXPECT_EQ(FILE_PATH_LITERAL("file"), |
| 43 VirtualPath::BaseName(file_path_).value()); |
42 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); | 44 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); |
43 } | 45 } |
44 | 46 |
45 TEST_F(FileSystemUtilTest, EnsureFilePathIsRelative) { | 47 TEST_F(FileSystemUtilTest, EnsureFilePathIsRelative) { |
46 ASSERT_TRUE(CrackFileSystemURL( | 48 ASSERT_TRUE(CrackFileSystemURL( |
47 "filesystem:http://chromium.org/temporary/////directory/file")); | 49 "filesystem:http://chromium.org/temporary/////directory/file")); |
48 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); | 50 EXPECT_EQ("http://chromium.org/", origin_url_.spec()); |
49 EXPECT_EQ(kFileSystemTypeTemporary, type_); | 51 EXPECT_EQ(kFileSystemTypeTemporary, type_); |
50 EXPECT_EQ(FILE_PATH_LITERAL("file"), file_path_.BaseName().value()); | 52 EXPECT_EQ(FILE_PATH_LITERAL("file"), |
| 53 VirtualPath::BaseName(file_path_).value()); |
51 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); | 54 EXPECT_EQ(FILE_PATH_LITERAL("directory"), file_path_.DirName().value()); |
52 EXPECT_FALSE(file_path_.IsAbsolute()); | 55 EXPECT_FALSE(file_path_.IsAbsolute()); |
53 } | 56 } |
54 | 57 |
55 TEST_F(FileSystemUtilTest, RejectBadSchemes) { | 58 TEST_F(FileSystemUtilTest, RejectBadSchemes) { |
56 EXPECT_FALSE(CrackFileSystemURL("http://chromium.org/")); | 59 EXPECT_FALSE(CrackFileSystemURL("http://chromium.org/")); |
57 EXPECT_FALSE(CrackFileSystemURL("https://chromium.org/")); | 60 EXPECT_FALSE(CrackFileSystemURL("https://chromium.org/")); |
58 EXPECT_FALSE(CrackFileSystemURL("file:///foo/bar")); | 61 EXPECT_FALSE(CrackFileSystemURL("file:///foo/bar")); |
59 EXPECT_FALSE(CrackFileSystemURL("foobar:///foo/bar")); | 62 EXPECT_FALSE(CrackFileSystemURL("foobar:///foo/bar")); |
60 } | 63 } |
61 | 64 |
62 TEST_F(FileSystemUtilTest, UnescapePath) { | 65 TEST_F(FileSystemUtilTest, UnescapePath) { |
63 ASSERT_TRUE(CrackFileSystemURL( | 66 ASSERT_TRUE(CrackFileSystemURL( |
64 "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar")); | 67 "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar")); |
65 EXPECT_EQ(FILE_PATH_LITERAL("space bar"), file_path_.BaseName().value()); | 68 EXPECT_EQ(FILE_PATH_LITERAL("space bar"), |
| 69 VirtualPath::BaseName(file_path_).value()); |
66 EXPECT_EQ(FILE_PATH_LITERAL("~chromium"), file_path_.DirName().value()); | 70 EXPECT_EQ(FILE_PATH_LITERAL("~chromium"), file_path_.DirName().value()); |
67 } | 71 } |
68 | 72 |
69 TEST_F(FileSystemUtilTest, RejectBadType) { | 73 TEST_F(FileSystemUtilTest, RejectBadType) { |
70 EXPECT_FALSE(CrackFileSystemURL("filesystem:http://c.org/foobar/file")); | 74 EXPECT_FALSE(CrackFileSystemURL("filesystem:http://c.org/foobar/file")); |
71 } | 75 } |
72 | 76 |
73 TEST_F(FileSystemUtilTest, RejectMalformedURL) { | 77 TEST_F(FileSystemUtilTest, RejectMalformedURL) { |
74 EXPECT_FALSE(CrackFileSystemURL("filesystem:///foobar/file")); | 78 EXPECT_FALSE(CrackFileSystemURL("filesystem:///foobar/file")); |
75 EXPECT_FALSE(CrackFileSystemURL("filesystem:foobar/file")); | 79 EXPECT_FALSE(CrackFileSystemURL("filesystem:foobar/file")); |
76 } | 80 } |
77 | 81 |
78 TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) { | 82 TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) { |
79 GURL origin_url("http://chromium.org"); | 83 GURL origin_url("http://chromium.org"); |
80 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; | 84 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary; |
81 GURL uri = GURL("filesystem:http://chromium.org/temporary/"); | 85 GURL uri = GURL("filesystem:http://chromium.org/temporary/"); |
82 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type)); | 86 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type)); |
83 } | 87 } |
84 | 88 |
85 TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) { | 89 TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) { |
86 GURL origin_url("http://chromium.org"); | 90 GURL origin_url("http://chromium.org"); |
87 fileapi::FileSystemType type = fileapi::kFileSystemTypePersistent; | 91 fileapi::FileSystemType type = fileapi::kFileSystemTypePersistent; |
88 GURL uri = GURL("filesystem:http://chromium.org/persistent/"); | 92 GURL uri = GURL("filesystem:http://chromium.org/persistent/"); |
89 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type)); | 93 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type)); |
90 } | 94 } |
91 | 95 |
| 96 TEST_F(FileSystemUtilTest, VirtualPathBaseName) { |
| 97 struct test_data { |
| 98 const FilePath::StringType path; |
| 99 const FilePath::StringType base_name; |
| 100 } test_cases[] = { |
| 101 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("bar") }, |
| 102 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("b:bar") }, |
| 103 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("") }, |
| 104 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") }, |
| 105 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("bar") }, |
| 106 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("bar") }, |
| 107 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("bar") }, |
| 108 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("bar") }, |
| 109 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL("bar") }, |
| 110 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL("bar") }, |
| 111 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("bar") }, |
| 112 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("bar") }, |
| 113 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL("bar") } |
| 114 }; |
| 115 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 116 FilePath input = FilePath(test_cases[i].path); |
| 117 FilePath base_name = VirtualPath::BaseName(input); |
| 118 EXPECT_EQ(test_cases[i].base_name, base_name.value()); |
| 119 } |
| 120 } |
| 121 |
| 122 TEST_F(FileSystemUtilTest, VirtualPathGetComponents) { |
| 123 struct test_data { |
| 124 const FilePath::StringType path; |
| 125 size_t count; |
| 126 const FilePath::StringType components[3]; |
| 127 } test_cases[] = { |
| 128 { FILE_PATH_LITERAL("foo/bar"), |
| 129 2, |
| 130 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } }, |
| 131 { FILE_PATH_LITERAL("foo"), |
| 132 1, |
| 133 { FILE_PATH_LITERAL("foo") } }, |
| 134 { FILE_PATH_LITERAL("foo////bar"), |
| 135 2, |
| 136 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } }, |
| 137 { FILE_PATH_LITERAL("foo/c:bar"), |
| 138 2, |
| 139 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("c:bar") } }, |
| 140 { FILE_PATH_LITERAL("c:foo/bar"), |
| 141 2, |
| 142 { FILE_PATH_LITERAL("c:foo"), FILE_PATH_LITERAL("bar") } }, |
| 143 { FILE_PATH_LITERAL("foo/bar"), |
| 144 2, |
| 145 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } }, |
| 146 { FILE_PATH_LITERAL("/foo/bar"), |
| 147 2, |
| 148 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } }, |
| 149 }; |
| 150 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 151 FilePath input = FilePath(test_cases[i].path); |
| 152 std::vector<FilePath::StringType> components; |
| 153 VirtualPath::GetComponents(input, &components); |
| 154 EXPECT_EQ(test_cases[i].count, components.size()); |
| 155 for (size_t j = 0; j < components.size(); ++j) |
| 156 EXPECT_EQ(test_cases[i].components[j], components[j]); |
| 157 } |
| 158 } |
| 159 |
92 } // namespace (anonymous) | 160 } // namespace (anonymous) |
93 } // namespace fileapi | 161 } // namespace fileapi |
OLD | NEW |