| 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 <set> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/message_loop.h" | |
| 12 #include "base/stringprintf.h" | |
| 13 #include "base/time.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "webkit/fileapi/external_mount_points.h" | |
| 16 #include "webkit/fileapi/file_system_context.h" | |
| 17 #include "webkit/fileapi/file_system_mount_point_provider.h" | |
| 18 #include "webkit/fileapi/file_system_operation.h" | |
| 19 #include "webkit/fileapi/file_system_task_runners.h" | |
| 20 #include "webkit/fileapi/file_system_url.h" | |
| 21 #include "webkit/fileapi/isolated_context.h" | |
| 22 #include "webkit/fileapi/media/native_media_file_util.h" | |
| 23 #include "webkit/fileapi/mock_file_system_options.h" | |
| 24 #include "webkit/fileapi/native_file_util.h" | |
| 25 #include "webkit/quota/mock_special_storage_policy.h" | |
| 26 | |
| 27 #define FPL(x) FILE_PATH_LITERAL(x) | |
| 28 | |
| 29 namespace fileapi { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 typedef FileSystemOperation::FileEntryList FileEntryList; | |
| 34 | |
| 35 struct FilteringTestCase { | |
| 36 const base::FilePath::CharType* path; | |
| 37 bool is_directory; | |
| 38 bool visible; | |
| 39 }; | |
| 40 | |
| 41 const FilteringTestCase kFilteringTestCases[] = { | |
| 42 // Directory should always be visible. | |
| 43 { FPL("hoge"), true, true }, | |
| 44 { FPL("fuga.jpg"), true, true }, | |
| 45 { FPL("piyo.txt"), true, true }, | |
| 46 { FPL("moga.cod"), true, true }, | |
| 47 | |
| 48 // File should be visible if it's a supported media file. | |
| 49 { FPL("foo"), false, false }, // File without extension. | |
| 50 { FPL("bar.jpg"), false, true }, // Supported media file. | |
| 51 { FPL("baz.txt"), false, false }, // Non-media file. | |
| 52 { FPL("foobar.cod"), false, false }, // Unsupported media file. | |
| 53 }; | |
| 54 | |
| 55 void ExpectEqHelper(const std::string& test_name, | |
| 56 base::PlatformFileError expected, | |
| 57 base::PlatformFileError actual) { | |
| 58 EXPECT_EQ(expected, actual) << test_name; | |
| 59 } | |
| 60 | |
| 61 void ExpectMetadataEqHelper(const std::string& test_name, | |
| 62 base::PlatformFileError expected, | |
| 63 bool expected_is_directory, | |
| 64 base::PlatformFileError actual, | |
| 65 const base::PlatformFileInfo& file_info, | |
| 66 const base::FilePath& /*platform_path*/) { | |
| 67 EXPECT_EQ(expected, actual) << test_name; | |
| 68 if (actual == base::PLATFORM_FILE_OK) | |
| 69 EXPECT_EQ(expected_is_directory, file_info.is_directory) << test_name; | |
| 70 } | |
| 71 | |
| 72 void DidReadDirectory(std::set<base::FilePath::StringType>* content, | |
| 73 bool* completed, | |
| 74 base::PlatformFileError error, | |
| 75 const FileEntryList& file_list, | |
| 76 bool has_more) { | |
| 77 EXPECT_TRUE(!*completed); | |
| 78 *completed = !has_more; | |
| 79 for (FileEntryList::const_iterator itr = file_list.begin(); | |
| 80 itr != file_list.end(); ++itr) | |
| 81 EXPECT_TRUE(content->insert(itr->name).second); | |
| 82 } | |
| 83 | |
| 84 void PopulateDirectoryWithTestCases(const base::FilePath& dir, | |
| 85 const FilteringTestCase* test_cases, | |
| 86 size_t n) { | |
| 87 for (size_t i = 0; i < n; ++i) { | |
| 88 base::FilePath path = dir.Append(test_cases[i].path); | |
| 89 if (test_cases[i].is_directory) { | |
| 90 ASSERT_TRUE(file_util::CreateDirectory(path)); | |
| 91 } else { | |
| 92 bool created = false; | |
| 93 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 94 NativeFileUtil::EnsureFileExists(path, &created)); | |
| 95 ASSERT_TRUE(created); | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 } // namespace | |
| 101 | |
| 102 class NativeMediaFileUtilTest : public testing::Test { | |
| 103 public: | |
| 104 NativeMediaFileUtilTest() | |
| 105 : file_util_(NULL) { | |
| 106 } | |
| 107 | |
| 108 virtual void SetUp() { | |
| 109 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 110 ASSERT_TRUE(file_util::CreateDirectory(root_path())); | |
| 111 | |
| 112 scoped_refptr<quota::SpecialStoragePolicy> storage_policy = | |
| 113 new quota::MockSpecialStoragePolicy(); | |
| 114 | |
| 115 file_system_context_ = | |
| 116 new FileSystemContext( | |
| 117 FileSystemTaskRunners::CreateMockTaskRunners(), | |
| 118 ExternalMountPoints::CreateRefCounted().get(), | |
| 119 storage_policy, | |
| 120 NULL, | |
| 121 ScopedVector<FileSystemMountPointProvider>(), | |
| 122 data_dir_.path(), | |
| 123 CreateAllowFileAccessOptions()); | |
| 124 | |
| 125 file_util_ = file_system_context_->GetFileUtil(kFileSystemTypeNativeMedia); | |
| 126 | |
| 127 filesystem_id_ = isolated_context()->RegisterFileSystemForPath( | |
| 128 kFileSystemTypeNativeMedia, root_path(), NULL); | |
| 129 | |
| 130 isolated_context()->AddReference(filesystem_id_); | |
| 131 } | |
| 132 | |
| 133 virtual void TearDown() { | |
| 134 isolated_context()->RemoveReference(filesystem_id_); | |
| 135 file_system_context_ = NULL; | |
| 136 } | |
| 137 | |
| 138 protected: | |
| 139 FileSystemContext* file_system_context() { | |
| 140 return file_system_context_.get(); | |
| 141 } | |
| 142 | |
| 143 FileSystemURL CreateURL(const base::FilePath::CharType* test_case_path) { | |
| 144 return file_system_context_->CreateCrackedFileSystemURL( | |
| 145 origin(), | |
| 146 fileapi::kFileSystemTypeIsolated, | |
| 147 GetVirtualPath(test_case_path)); | |
| 148 } | |
| 149 | |
| 150 IsolatedContext* isolated_context() { | |
| 151 return IsolatedContext::GetInstance(); | |
| 152 } | |
| 153 | |
| 154 base::FilePath root_path() { | |
| 155 return data_dir_.path().Append(FPL("Media Directory")); | |
| 156 } | |
| 157 | |
| 158 base::FilePath GetVirtualPath(const base::FilePath::CharType* test_case_path)
{ | |
| 159 return base::FilePath::FromUTF8Unsafe(filesystem_id_). | |
| 160 Append(FPL("Media Directory")). | |
| 161 Append(base::FilePath(test_case_path)); | |
| 162 } | |
| 163 | |
| 164 FileSystemFileUtil* file_util() { | |
| 165 return file_util_; | |
| 166 } | |
| 167 | |
| 168 GURL origin() { | |
| 169 return GURL("http://example.com"); | |
| 170 } | |
| 171 | |
| 172 fileapi::FileSystemType type() { | |
| 173 return kFileSystemTypeNativeMedia; | |
| 174 } | |
| 175 | |
| 176 FileSystemOperation* NewOperation(const FileSystemURL& url) { | |
| 177 return file_system_context_->CreateFileSystemOperation(url, NULL); | |
| 178 } | |
| 179 | |
| 180 private: | |
| 181 MessageLoop message_loop_; | |
| 182 | |
| 183 base::ScopedTempDir data_dir_; | |
| 184 scoped_refptr<FileSystemContext> file_system_context_; | |
| 185 | |
| 186 FileSystemFileUtil* file_util_; | |
| 187 std::string filesystem_id_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(NativeMediaFileUtilTest); | |
| 190 }; | |
| 191 | |
| 192 TEST_F(NativeMediaFileUtilTest, DirectoryExistsAndFileExistsFiltering) { | |
| 193 PopulateDirectoryWithTestCases(root_path(), | |
| 194 kFilteringTestCases, | |
| 195 arraysize(kFilteringTestCases)); | |
| 196 | |
| 197 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 198 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 199 FileSystemOperation* operation = NewOperation(url); | |
| 200 | |
| 201 base::PlatformFileError expectation = | |
| 202 kFilteringTestCases[i].visible ? | |
| 203 base::PLATFORM_FILE_OK : | |
| 204 base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 205 | |
| 206 std::string test_name = | |
| 207 base::StringPrintf("DirectoryExistsAndFileExistsFiltering %" PRIuS, i); | |
| 208 if (kFilteringTestCases[i].is_directory) { | |
| 209 operation->DirectoryExists( | |
| 210 url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 211 } else { | |
| 212 operation->FileExists( | |
| 213 url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 214 } | |
| 215 MessageLoop::current()->RunUntilIdle(); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 TEST_F(NativeMediaFileUtilTest, ReadDirectoryFiltering) { | |
| 220 PopulateDirectoryWithTestCases(root_path(), | |
| 221 kFilteringTestCases, | |
| 222 arraysize(kFilteringTestCases)); | |
| 223 | |
| 224 std::set<base::FilePath::StringType> content; | |
| 225 FileSystemURL url = CreateURL(FPL("")); | |
| 226 bool completed = false; | |
| 227 NewOperation(url)->ReadDirectory( | |
| 228 url, base::Bind(&DidReadDirectory, &content, &completed)); | |
| 229 MessageLoop::current()->RunUntilIdle(); | |
| 230 EXPECT_TRUE(completed); | |
| 231 EXPECT_EQ(5u, content.size()); | |
| 232 | |
| 233 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 234 base::FilePath::StringType name = | |
| 235 base::FilePath(kFilteringTestCases[i].path).BaseName().value(); | |
| 236 std::set<base::FilePath::StringType>::const_iterator found = content.find(na
me); | |
| 237 EXPECT_EQ(kFilteringTestCases[i].visible, found != content.end()); | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 TEST_F(NativeMediaFileUtilTest, CreateFileAndCreateDirectoryFiltering) { | |
| 242 // Run the loop twice. The second loop attempts to create files that are | |
| 243 // pre-existing. Though the result should be the same. | |
| 244 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 245 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 246 FileSystemURL root_url = CreateURL(FPL("")); | |
| 247 FileSystemOperation* operation = NewOperation(root_url); | |
| 248 | |
| 249 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 250 | |
| 251 std::string test_name = base::StringPrintf( | |
| 252 "CreateFileAndCreateDirectoryFiltering run %d, test %" PRIuS, | |
| 253 loop_count, i); | |
| 254 base::PlatformFileError expectation = | |
| 255 kFilteringTestCases[i].visible ? | |
| 256 base::PLATFORM_FILE_OK : | |
| 257 base::PLATFORM_FILE_ERROR_SECURITY; | |
| 258 if (kFilteringTestCases[i].is_directory) { | |
| 259 operation->CreateDirectory( | |
| 260 url, false, false, | |
| 261 base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 262 } else { | |
| 263 operation->CreateFile( | |
| 264 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 265 } | |
| 266 MessageLoop::current()->RunUntilIdle(); | |
| 267 } | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 TEST_F(NativeMediaFileUtilTest, CopySourceFiltering) { | |
| 272 base::FilePath dest_path = root_path().AppendASCII("dest"); | |
| 273 FileSystemURL dest_url = CreateURL(FPL("dest")); | |
| 274 | |
| 275 // Run the loop twice. The first run has no source files. The second run does. | |
| 276 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 277 if (loop_count == 1) { | |
| 278 PopulateDirectoryWithTestCases(root_path(), | |
| 279 kFilteringTestCases, | |
| 280 arraysize(kFilteringTestCases)); | |
| 281 } | |
| 282 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 283 // Always start with an empty destination directory. | |
| 284 // Copying to a non-empty destination directory is an invalid operation. | |
| 285 ASSERT_TRUE(file_util::Delete(dest_path, true)); | |
| 286 ASSERT_TRUE(file_util::CreateDirectory(dest_path)); | |
| 287 | |
| 288 FileSystemURL root_url = CreateURL(FPL("")); | |
| 289 FileSystemOperation* operation = NewOperation(root_url); | |
| 290 | |
| 291 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 292 | |
| 293 std::string test_name = base::StringPrintf( | |
| 294 "CopySourceFiltering run %d test %" PRIuS, loop_count, i); | |
| 295 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 296 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 297 // If the source does not exist or is not visible. | |
| 298 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 299 } else if (!kFilteringTestCases[i].is_directory) { | |
| 300 // Cannot copy a visible file to a directory. | |
| 301 expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 302 } | |
| 303 operation->Copy( | |
| 304 url, dest_url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 305 MessageLoop::current()->RunUntilIdle(); | |
| 306 } | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 TEST_F(NativeMediaFileUtilTest, CopyDestFiltering) { | |
| 311 // Run the loop twice. The first run has no destination files. | |
| 312 // The second run does. | |
| 313 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 314 if (loop_count == 1) { | |
| 315 // Reset the test directory between the two loops to remove old | |
| 316 // directories and create new ones that should pre-exist. | |
| 317 ASSERT_TRUE(file_util::Delete(root_path(), true)); | |
| 318 ASSERT_TRUE(file_util::CreateDirectory(root_path())); | |
| 319 PopulateDirectoryWithTestCases(root_path(), | |
| 320 kFilteringTestCases, | |
| 321 arraysize(kFilteringTestCases)); | |
| 322 } | |
| 323 | |
| 324 // Always create a dummy source data file. | |
| 325 base::FilePath src_path = root_path().AppendASCII("foo.jpg"); | |
| 326 FileSystemURL src_url = CreateURL(FPL("foo.jpg")); | |
| 327 static const char kDummyData[] = "dummy"; | |
| 328 ASSERT_TRUE(file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); | |
| 329 | |
| 330 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 331 if (loop_count == 0 && kFilteringTestCases[i].is_directory) { | |
| 332 // These directories do not exist in this case, so Copy() will not | |
| 333 // treat them as directories. Thus invalidating these test cases. | |
| 334 // Continue now to avoid creating a new |operation| below that goes | |
| 335 // unused. | |
| 336 continue; | |
| 337 } | |
| 338 FileSystemURL root_url = CreateURL(FPL("")); | |
| 339 FileSystemOperation* operation = NewOperation(root_url); | |
| 340 | |
| 341 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 342 | |
| 343 std::string test_name = base::StringPrintf( | |
| 344 "CopyDestFiltering run %d test %" PRIuS, loop_count, i); | |
| 345 base::PlatformFileError expectation; | |
| 346 if (loop_count == 0) { | |
| 347 // The destination path is a file here. The directory case has been | |
| 348 // handled above. | |
| 349 // If the destination path does not exist and is not visible, then | |
| 350 // creating it would be a security violation. | |
| 351 expectation = | |
| 352 kFilteringTestCases[i].visible ? | |
| 353 base::PLATFORM_FILE_OK : | |
| 354 base::PLATFORM_FILE_ERROR_SECURITY; | |
| 355 } else { | |
| 356 if (!kFilteringTestCases[i].visible) { | |
| 357 // If the destination path exist and is not visible, then to the copy | |
| 358 // operation, it looks like the file needs to be created, which is a | |
| 359 // security violation. | |
| 360 expectation = base::PLATFORM_FILE_ERROR_SECURITY; | |
| 361 } else if (kFilteringTestCases[i].is_directory) { | |
| 362 // Cannot copy a file to a directory. | |
| 363 expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 364 } else { | |
| 365 // Copying from a file to a visible file that exists is ok. | |
| 366 expectation = base::PLATFORM_FILE_OK; | |
| 367 } | |
| 368 } | |
| 369 operation->Copy( | |
| 370 src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 371 MessageLoop::current()->RunUntilIdle(); | |
| 372 } | |
| 373 } | |
| 374 } | |
| 375 | |
| 376 TEST_F(NativeMediaFileUtilTest, MoveSourceFiltering) { | |
| 377 base::FilePath dest_path = root_path().AppendASCII("dest"); | |
| 378 FileSystemURL dest_url = CreateURL(FPL("dest")); | |
| 379 | |
| 380 // Run the loop twice. The first run has no source files. The second run does. | |
| 381 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 382 if (loop_count == 1) { | |
| 383 PopulateDirectoryWithTestCases(root_path(), | |
| 384 kFilteringTestCases, | |
| 385 arraysize(kFilteringTestCases)); | |
| 386 } | |
| 387 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 388 // Always start with an empty destination directory. | |
| 389 // Moving to a non-empty destination directory is an invalid operation. | |
| 390 ASSERT_TRUE(file_util::Delete(dest_path, true)); | |
| 391 ASSERT_TRUE(file_util::CreateDirectory(dest_path)); | |
| 392 | |
| 393 FileSystemURL root_url = CreateURL(FPL("")); | |
| 394 FileSystemOperation* operation = NewOperation(root_url); | |
| 395 | |
| 396 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 397 | |
| 398 std::string test_name = base::StringPrintf( | |
| 399 "MoveSourceFiltering run %d test %" PRIuS, loop_count, i); | |
| 400 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 401 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 402 // If the source does not exist or is not visible. | |
| 403 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 404 } else if (!kFilteringTestCases[i].is_directory) { | |
| 405 // Cannot move a visible file to a directory. | |
| 406 expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 407 } | |
| 408 operation->Move( | |
| 409 url, dest_url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 410 MessageLoop::current()->RunUntilIdle(); | |
| 411 } | |
| 412 } | |
| 413 } | |
| 414 | |
| 415 TEST_F(NativeMediaFileUtilTest, MoveDestFiltering) { | |
| 416 // Run the loop twice. The first run has no destination files. | |
| 417 // The second run does. | |
| 418 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 419 if (loop_count == 1) { | |
| 420 // Reset the test directory between the two loops to remove old | |
| 421 // directories and create new ones that should pre-exist. | |
| 422 ASSERT_TRUE(file_util::Delete(root_path(), true)); | |
| 423 ASSERT_TRUE(file_util::CreateDirectory(root_path())); | |
| 424 PopulateDirectoryWithTestCases(root_path(), | |
| 425 kFilteringTestCases, | |
| 426 arraysize(kFilteringTestCases)); | |
| 427 } | |
| 428 | |
| 429 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 430 if (loop_count == 0 && kFilteringTestCases[i].is_directory) { | |
| 431 // These directories do not exist in this case, so Copy() will not | |
| 432 // treat them as directories. Thus invalidating these test cases. | |
| 433 // Continue now to avoid creating a new |operation| below that goes | |
| 434 // unused. | |
| 435 continue; | |
| 436 } | |
| 437 | |
| 438 // Create the source file for every test case because it might get moved. | |
| 439 base::FilePath src_path = root_path().AppendASCII("foo.jpg"); | |
| 440 FileSystemURL src_url = CreateURL(FPL("foo.jpg")); | |
| 441 static const char kDummyData[] = "dummy"; | |
| 442 ASSERT_TRUE( | |
| 443 file_util::WriteFile(src_path, kDummyData, strlen(kDummyData))); | |
| 444 | |
| 445 FileSystemURL root_url = CreateURL(FPL("")); | |
| 446 FileSystemOperation* operation = NewOperation(root_url); | |
| 447 | |
| 448 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 449 | |
| 450 std::string test_name = base::StringPrintf( | |
| 451 "MoveDestFiltering run %d test %" PRIuS, loop_count, i); | |
| 452 base::PlatformFileError expectation; | |
| 453 if (loop_count == 0) { | |
| 454 // The destination path is a file here. The directory case has been | |
| 455 // handled above. | |
| 456 // If the destination path does not exist and is not visible, then | |
| 457 // creating it would be a security violation. | |
| 458 expectation = | |
| 459 kFilteringTestCases[i].visible ? | |
| 460 base::PLATFORM_FILE_OK : | |
| 461 base::PLATFORM_FILE_ERROR_SECURITY; | |
| 462 } else { | |
| 463 if (!kFilteringTestCases[i].visible) { | |
| 464 // If the destination path exist and is not visible, then to the move | |
| 465 // operation, it looks like the file needs to be created, which is a | |
| 466 // security violation. | |
| 467 expectation = base::PLATFORM_FILE_ERROR_SECURITY; | |
| 468 } else if (kFilteringTestCases[i].is_directory) { | |
| 469 // Cannot move a file to a directory. | |
| 470 expectation = base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 471 } else { | |
| 472 // Moving from a file to a visible file that exists is ok. | |
| 473 expectation = base::PLATFORM_FILE_OK; | |
| 474 } | |
| 475 } | |
| 476 operation->Move( | |
| 477 src_url, url, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 478 MessageLoop::current()->RunUntilIdle(); | |
| 479 } | |
| 480 } | |
| 481 } | |
| 482 | |
| 483 TEST_F(NativeMediaFileUtilTest, GetMetadataFiltering) { | |
| 484 // Run the loop twice. The first run has no files. The second run does. | |
| 485 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 486 if (loop_count == 1) { | |
| 487 PopulateDirectoryWithTestCases(root_path(), | |
| 488 kFilteringTestCases, | |
| 489 arraysize(kFilteringTestCases)); | |
| 490 } | |
| 491 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 492 FileSystemURL root_url = CreateURL(FPL("")); | |
| 493 FileSystemOperation* operation = NewOperation(root_url); | |
| 494 | |
| 495 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 496 | |
| 497 std::string test_name = base::StringPrintf( | |
| 498 "GetMetadataFiltering run %d test %" PRIuS, loop_count, i); | |
| 499 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 500 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 501 // Cannot get metadata from files that do not exist or are not visible. | |
| 502 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 503 } | |
| 504 operation->GetMetadata(url, | |
| 505 base::Bind(&ExpectMetadataEqHelper, | |
| 506 test_name, | |
| 507 expectation, | |
| 508 kFilteringTestCases[i].is_directory)); | |
| 509 MessageLoop::current()->RunUntilIdle(); | |
| 510 } | |
| 511 } | |
| 512 } | |
| 513 | |
| 514 TEST_F(NativeMediaFileUtilTest, RemoveFiltering) { | |
| 515 // Run the loop twice. The first run has no files. The second run does. | |
| 516 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 517 if (loop_count == 1) { | |
| 518 PopulateDirectoryWithTestCases(root_path(), | |
| 519 kFilteringTestCases, | |
| 520 arraysize(kFilteringTestCases)); | |
| 521 } | |
| 522 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 523 FileSystemURL root_url = CreateURL(FPL("")); | |
| 524 FileSystemOperation* operation = NewOperation(root_url); | |
| 525 | |
| 526 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 527 | |
| 528 std::string test_name = base::StringPrintf( | |
| 529 "RemoveFiltering run %d test %" PRIuS, loop_count, i); | |
| 530 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 531 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 532 // Cannot remove files that do not exist or are not visible. | |
| 533 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 534 } | |
| 535 operation->Remove( | |
| 536 url, false, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 537 MessageLoop::current()->RunUntilIdle(); | |
| 538 } | |
| 539 } | |
| 540 } | |
| 541 | |
| 542 TEST_F(NativeMediaFileUtilTest, TruncateFiltering) { | |
| 543 // Run the loop twice. The first run has no files. The second run does. | |
| 544 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 545 if (loop_count == 1) { | |
| 546 PopulateDirectoryWithTestCases(root_path(), | |
| 547 kFilteringTestCases, | |
| 548 arraysize(kFilteringTestCases)); | |
| 549 } | |
| 550 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 551 FileSystemURL root_url = CreateURL(FPL("")); | |
| 552 FileSystemOperation* operation = NewOperation(root_url); | |
| 553 | |
| 554 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 555 | |
| 556 std::string test_name = base::StringPrintf( | |
| 557 "TruncateFiltering run %d test %" PRIuS, loop_count, i); | |
| 558 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 559 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 560 // Cannot truncate files that do not exist or are not visible. | |
| 561 expectation = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 562 } else if (kFilteringTestCases[i].is_directory) { | |
| 563 // Cannot truncate directories. | |
| 564 expectation = base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | |
| 565 } | |
| 566 operation->Truncate( | |
| 567 url, 0, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 568 MessageLoop::current()->RunUntilIdle(); | |
| 569 } | |
| 570 } | |
| 571 } | |
| 572 | |
| 573 TEST_F(NativeMediaFileUtilTest, TouchFileFiltering) { | |
| 574 base::Time time = base::Time::Now(); | |
| 575 | |
| 576 // Run the loop twice. The first run has no files. The second run does. | |
| 577 for (int loop_count = 0; loop_count < 2; ++loop_count) { | |
| 578 if (loop_count == 1) { | |
| 579 PopulateDirectoryWithTestCases(root_path(), | |
| 580 kFilteringTestCases, | |
| 581 arraysize(kFilteringTestCases)); | |
| 582 } | |
| 583 for (size_t i = 0; i < arraysize(kFilteringTestCases); ++i) { | |
| 584 FileSystemURL root_url = CreateURL(FPL("")); | |
| 585 FileSystemOperation* operation = NewOperation(root_url); | |
| 586 | |
| 587 FileSystemURL url = CreateURL(kFilteringTestCases[i].path); | |
| 588 | |
| 589 std::string test_name = base::StringPrintf( | |
| 590 "TouchFileFiltering run %d test %" PRIuS, loop_count, i); | |
| 591 base::PlatformFileError expectation = base::PLATFORM_FILE_OK; | |
| 592 if (loop_count == 0 || !kFilteringTestCases[i].visible) { | |
| 593 // Files do not exists. Touch fails. | |
| 594 expectation = base::PLATFORM_FILE_ERROR_FAILED; | |
| 595 } | |
| 596 operation->TouchFile( | |
| 597 url, time, time, base::Bind(&ExpectEqHelper, test_name, expectation)); | |
| 598 MessageLoop::current()->RunUntilIdle(); | |
| 599 } | |
| 600 } | |
| 601 } | |
| 602 | |
| 603 } // namespace fileapi | |
| OLD | NEW |