Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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. | |
| 4 | |
| 5 #include "webkit/fileapi/external_mount_points.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/file_path.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 #define FPL FILE_PATH_LITERAL | |
| 14 | |
| 15 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | |
| 16 #define DRIVE FPL("C:") | |
| 17 #else | |
| 18 #define DRIVE | |
| 19 #endif | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 TEST(ExternalMountPointsTest, AddMountPoint) { | |
| 24 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 25 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 26 | |
| 27 struct TestCase { | |
| 28 // The mount point's name. | |
| 29 const char* const name; | |
| 30 // The mount point's path. | |
| 31 const FilePath::CharType* const path; | |
| 32 // Whether the mount point registration should succeed. | |
| 33 bool success; | |
| 34 // Path returned by GetRegisteredPath. NULL if the method is expected to | |
| 35 // fail. | |
| 36 const FilePath::CharType* const registered_path; | |
| 37 }; | |
| 38 | |
| 39 const TestCase kTestCases[] = { | |
| 40 // Valid mount point. | |
| 41 { "test", DRIVE FPL("/foo/test"), true, DRIVE FPL("/foo/test") }, | |
| 42 // Valid mount point with only one path component. | |
| 43 { "bbb", DRIVE FPL("/bbb"), true, DRIVE FPL("/bbb") }, | |
| 44 // Existing mount point path is substring of the mount points path. | |
| 45 { "test11", DRIVE FPL("/foo/test11"), true, DRIVE FPL("/foo/test11") }, | |
| 46 // Path substring of an existing path. | |
| 47 { "test1", DRIVE FPL("/foo/test1"), true, DRIVE FPL("/foo/test1") }, | |
| 48 // Empty mount point name and path. | |
| 49 { "", DRIVE FPL(""), false, NULL }, | |
| 50 // Empty mount point name. | |
| 51 { "", DRIVE FPL("/ddd"), false, NULL }, | |
| 52 // Empty mount point path. | |
| 53 { "empty_path", FPL(""), true, FPL("") }, | |
| 54 // Name different from path's base name. | |
| 55 { "not_base_name", DRIVE FPL("/x/y/z"), true, DRIVE FPL("/x/y/z") }, | |
| 56 // References parent. | |
| 57 { "invalid", DRIVE FPL("../foo/invalid"), false, NULL }, | |
| 58 // Relative path. | |
| 59 { "relative", DRIVE FPL("foo/relative"), false, NULL }, | |
| 60 // Existing mount point path. | |
| 61 { "path_exists", DRIVE FPL("/foo/test"), false, NULL }, | |
| 62 // Mount point with the same name exists. | |
| 63 { "test", DRIVE FPL("/foo/a/test_name_exists"), false, | |
| 64 DRIVE FPL("/foo/test") }, | |
| 65 // Child of an existing mount point. | |
| 66 { "a1", DRIVE FPL("/foo/test/a"), false, NULL }, | |
| 67 // Parent of an existing mount point. | |
| 68 { "foo1", DRIVE FPL("/foo"), false, NULL }, | |
| 69 // Bit bigger depth. | |
| 70 { "g", DRIVE FPL("/foo/a/b/c/d/e/f/g"), true, | |
| 71 DRIVE FPL("/foo/a/b/c/d/e/f/g") }, | |
| 72 // Sibling mount point (with similar name) exists. | |
| 73 { "ff", DRIVE FPL("/foo/a/b/c/d/e/ff"), true, | |
| 74 DRIVE FPL("/foo/a/b/c/d/e/ff") }, | |
| 75 // Lexicographically last among existing mount points. | |
| 76 { "yyy", DRIVE FPL("/zzz/yyy"), true, DRIVE FPL("/zzz/yyy") }, | |
| 77 // Parent of the lexicographically last mount point. | |
| 78 { "zzz1", DRIVE FPL("/zzz"), false, NULL }, | |
| 79 // Child of the lexicographically last mount point. | |
| 80 { "xxx1", DRIVE FPL("/zzz/yyy/xxx"), false, NULL }, | |
| 81 // Lexicographically first among existing mount points. | |
| 82 { "b", DRIVE FPL("/a/b"), true, DRIVE FPL("/a/b") }, | |
| 83 // Parent of lexicographically first mount point. | |
| 84 { "a1", DRIVE FPL("/a"), false, NULL }, | |
| 85 // Child of lexicographically last mount point. | |
| 86 { "c1", DRIVE FPL("/a/b/c"), false, NULL }, | |
| 87 // Path contains .. component. | |
| 88 { "funky", DRIVE FPL("/tt/fun/../funky"), false, NULL }, | |
| 89 // Windows separators. | |
| 90 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 91 { "win", DRIVE FPL("\\try\\separators\\win"), true, | |
| 92 DRIVE FPL("\\try\\separators\\win") }, | |
| 93 { "win1", DRIVE FPL("\\try/separators\\win1"), true, | |
| 94 DRIVE FPL("\\try/separators\\win1") }, | |
| 95 #else | |
| 96 { "win", DRIVE FPL("\\separators\\win"), false, NULL }, | |
| 97 { "win1", DRIVE FPL("\\try/separators\\win1"), false, NULL }, | |
| 98 #endif | |
| 99 // Win separators, but relative path. | |
| 100 { "win2", DRIVE FPL("try\\separators\\win2"), false, NULL }, | |
| 101 }; | |
| 102 | |
| 103 // Test adding mount points. | |
| 104 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | |
| 105 EXPECT_EQ(kTestCases[i].success, | |
| 106 mount_points->RegisterFileSystem( | |
| 107 kTestCases[i].name, | |
| 108 fileapi::kFileSystemTypeNativeLocal, | |
| 109 FilePath(kTestCases[i].path))) | |
| 110 << "Adding mount point: " << kTestCases[i].name << " with path " | |
| 111 << kTestCases[i].path; | |
| 112 } | |
| 113 | |
| 114 // Test that final mount point presence state is as expected. | |
| 115 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | |
| 116 FilePath found_path; | |
| 117 EXPECT_EQ(kTestCases[i].registered_path != NULL, | |
| 118 mount_points->GetRegisteredPath(kTestCases[i].name, &found_path)) | |
| 119 << "Test case: " << i; | |
| 120 if (kTestCases[i].registered_path) | |
| 121 EXPECT_EQ(FilePath(kTestCases[i].registered_path), found_path); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 | |
|
kinuko
2013/01/11 08:20:52
nit: extra empty line
tbarzic
2013/01/11 09:13:16
Done.
| |
| 126 TEST(ExternalMountPointsTest, GetVirtualPath) { | |
| 127 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | |
| 128 fileapi::ExternalMountPoints::CreateRefCounted()); | |
| 129 | |
| 130 // Vector of pairs <mount_name, mount_path>. | |
| 131 std::vector<std::pair<std::string, FilePath> > kTestMountPoints; | |
| 132 kTestMountPoints.push_back( | |
| 133 std::make_pair("c", FilePath(DRIVE FPL("/a/b/c")))); | |
| 134 kTestMountPoints.push_back( | |
| 135 std::make_pair("x", FilePath(DRIVE FPL("/z/y/x")))); | |
| 136 kTestMountPoints.push_back( | |
| 137 std::make_pair("o", FilePath(DRIVE FPL("/m/n/o")))); | |
| 138 // A mount point whose name does not match its path base name. | |
| 139 kTestMountPoints.push_back( | |
| 140 std::make_pair("mount", FilePath(DRIVE FPL("/root/foo")))); | |
| 141 // A mount point with an empty path. | |
| 142 kTestMountPoints.push_back( | |
| 143 std::make_pair("empty_path", FilePath(FPL("")))); | |
| 144 | |
| 145 for (size_t i = 0; i < kTestMountPoints.size(); i++) { | |
| 146 mount_points->RegisterFileSystem( | |
| 147 kTestMountPoints[i].first, | |
| 148 fileapi::kFileSystemTypeNativeLocal, | |
| 149 kTestMountPoints[i].second); | |
| 150 } | |
|
kinuko
2013/01/11 08:20:52
If you don't refer kTestMountPoints again isn't it
tbarzic
2013/01/11 09:13:16
Done.
| |
| 151 | |
| 152 struct TestCase { | |
| 153 const FilePath::CharType* const local_path; | |
| 154 bool success; | |
| 155 const FilePath::CharType* const virtual_path; | |
| 156 }; | |
| 157 | |
| 158 const TestCase kTestCases[] = { | |
| 159 // Empty path. | |
| 160 { FPL(""), false, FPL("") }, | |
| 161 // No registered mount point (but is parent to a mount point). | |
| 162 { DRIVE FPL("/a/b"), false, FPL("") }, | |
| 163 // No registered mount point (but is parent to a mount point). | |
| 164 { DRIVE FPL("/z/y"), false, FPL("") }, | |
| 165 // No registered mount point (but is parent to a mount point). | |
| 166 { DRIVE FPL("/m/n"), false, FPL("") }, | |
| 167 // No registered mount point. | |
| 168 { DRIVE FPL("/foo/mount"), false, FPL("") }, | |
| 169 // An existing mount point path is substring. | |
| 170 { DRIVE FPL("/a/b/c1"), false, FPL("") }, | |
| 171 // No leading /. | |
| 172 { DRIVE FPL("a/b/c"), false, FPL("") }, | |
| 173 // Sibling to a root path. | |
| 174 { DRIVE FPL("/a/b/d/e"), false, FPL("") }, | |
| 175 // Sibling to a root path. | |
| 176 { DRIVE FPL("/z/y/v/u"), false, FPL("") }, | |
| 177 // Sibling to a root path. | |
| 178 { DRIVE FPL("/m/n/p/q"), false, FPL("") }, | |
| 179 // Mount point root path. | |
| 180 { DRIVE FPL("/a/b/c"), true, FPL("c") }, | |
| 181 // Mount point root path. | |
| 182 { DRIVE FPL("/z/y/x"), true, FPL("x") }, | |
| 183 // Mount point root path. | |
| 184 { DRIVE FPL("/m/n/o"), true, FPL("o") }, | |
| 185 // Mount point child path. | |
| 186 { DRIVE FPL("/a/b/c/d/e"), true, FPL("c/d/e") }, | |
| 187 // Mount point child path. | |
| 188 { DRIVE FPL("/z/y/x/v/u"), true, FPL("x/v/u") }, | |
| 189 // Mount point child path. | |
| 190 { DRIVE FPL("/m/n/o/p/q"), true, FPL("o/p/q") }, | |
| 191 // Name doesn't match mount point path base name. | |
| 192 { DRIVE FPL("/root/foo/a/b/c"), true, FPL("mount/a/b/c") }, | |
| 193 { DRIVE FPL("/root/foo"), true, FPL("mount") }, | |
| 194 }; | |
| 195 | |
| 196 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | |
| 197 // Initialize virtual path with a value. | |
| 198 FilePath virtual_path(DRIVE FPL("/mount")); | |
| 199 FilePath local_path(kTestCases[i].local_path); | |
| 200 EXPECT_EQ(kTestCases[i].success, | |
| 201 mount_points->GetVirtualPath(local_path, &virtual_path)) | |
| 202 << "Resolving " << kTestCases[i].local_path; | |
| 203 | |
| 204 // There are no guarantees for |virtual_path| value if |GetVirtualPath| | |
| 205 // fails. | |
| 206 if (!kTestCases[i].success) | |
| 207 continue; | |
| 208 | |
| 209 FilePath expected_virtual_path(kTestCases[i].virtual_path); | |
| 210 EXPECT_EQ(expected_virtual_path, virtual_path) | |
| 211 << "Resolving " << kTestCases[i].local_path; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 } // namespace | |
| 216 | |
| OLD | NEW |