| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/platform_file.h" | 8 #include "base/platform_file.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "webkit/browser/fileapi/native_file_util.h" | 10 #include "webkit/browser/fileapi/native_file_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 base::FilePath Path() { | 23 base::FilePath Path() { |
| 24 return data_dir_.path(); | 24 return data_dir_.path(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 base::FilePath Path(const char* file_name) { | 27 base::FilePath Path(const char* file_name) { |
| 28 return data_dir_.path().AppendASCII(file_name); | 28 return data_dir_.path().AppendASCII(file_name); |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool FileExists(const base::FilePath& path) { | 31 bool FileExists(const base::FilePath& path) { |
| 32 return base::PathExists(path) && | 32 return base::PathExists(path) && |
| 33 !file_util::DirectoryExists(path); | 33 !base::DirectoryExists(path); |
| 34 } | 34 } |
| 35 | 35 |
| 36 int64 GetSize(const base::FilePath& path) { | 36 int64 GetSize(const base::FilePath& path) { |
| 37 base::PlatformFileInfo info; | 37 base::PlatformFileInfo info; |
| 38 file_util::GetFileInfo(path, &info); | 38 file_util::GetFileInfo(path, &info); |
| 39 return info.size; | 39 return info.size; |
| 40 } | 40 } |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 base::ScopedTempDir data_dir_; | 43 base::ScopedTempDir data_dir_; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST_F(NativeFileUtilTest, CreateAndDeleteDirectory) { | 94 TEST_F(NativeFileUtilTest, CreateAndDeleteDirectory) { |
| 95 base::FilePath dir_name = Path("test_dir"); | 95 base::FilePath dir_name = Path("test_dir"); |
| 96 ASSERT_EQ(base::PLATFORM_FILE_OK, | 96 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 97 NativeFileUtil::CreateDirectory(dir_name, | 97 NativeFileUtil::CreateDirectory(dir_name, |
| 98 false /* exclusive */, | 98 false /* exclusive */, |
| 99 false /* recursive */)); | 99 false /* recursive */)); |
| 100 | 100 |
| 101 EXPECT_TRUE(NativeFileUtil::DirectoryExists(dir_name)); | 101 EXPECT_TRUE(NativeFileUtil::DirectoryExists(dir_name)); |
| 102 EXPECT_TRUE(file_util::DirectoryExists(dir_name)); | 102 EXPECT_TRUE(base::DirectoryExists(dir_name)); |
| 103 | 103 |
| 104 ASSERT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, | 104 ASSERT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, |
| 105 NativeFileUtil::CreateDirectory(dir_name, | 105 NativeFileUtil::CreateDirectory(dir_name, |
| 106 true /* exclusive */, | 106 true /* exclusive */, |
| 107 false /* recursive */)); | 107 false /* recursive */)); |
| 108 | 108 |
| 109 ASSERT_EQ(base::PLATFORM_FILE_OK, | 109 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 110 NativeFileUtil::DeleteDirectory(dir_name)); | 110 NativeFileUtil::DeleteDirectory(dir_name)); |
| 111 EXPECT_FALSE(file_util::DirectoryExists(dir_name)); | 111 EXPECT_FALSE(base::DirectoryExists(dir_name)); |
| 112 EXPECT_FALSE(NativeFileUtil::DirectoryExists(dir_name)); | 112 EXPECT_FALSE(NativeFileUtil::DirectoryExists(dir_name)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 TEST_F(NativeFileUtilTest, TouchFileAndGetFileInfo) { | 115 TEST_F(NativeFileUtilTest, TouchFileAndGetFileInfo) { |
| 116 base::FilePath file_name = Path("test_file"); | 116 base::FilePath file_name = Path("test_file"); |
| 117 base::PlatformFileInfo native_info; | 117 base::PlatformFileInfo native_info; |
| 118 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 118 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 119 NativeFileUtil::GetFileInfo(file_name, &native_info)); | 119 NativeFileUtil::GetFileInfo(file_name, &native_info)); |
| 120 | 120 |
| 121 bool created = false; | 121 bool created = false; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 EXPECT_TRUE(FileExists(from_file)); | 233 EXPECT_TRUE(FileExists(from_file)); |
| 234 EXPECT_EQ(1020, GetSize(from_file)); | 234 EXPECT_EQ(1020, GetSize(from_file)); |
| 235 EXPECT_TRUE(FileExists(to_file1)); | 235 EXPECT_TRUE(FileExists(to_file1)); |
| 236 EXPECT_EQ(1020, GetSize(to_file1)); | 236 EXPECT_EQ(1020, GetSize(to_file1)); |
| 237 EXPECT_TRUE(FileExists(to_file2)); | 237 EXPECT_TRUE(FileExists(to_file2)); |
| 238 EXPECT_EQ(1020, GetSize(to_file2)); | 238 EXPECT_EQ(1020, GetSize(to_file2)); |
| 239 | 239 |
| 240 base::FilePath dir = Path("dir"); | 240 base::FilePath dir = Path("dir"); |
| 241 ASSERT_EQ(base::PLATFORM_FILE_OK, | 241 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 242 NativeFileUtil::CreateDirectory(dir, false, false)); | 242 NativeFileUtil::CreateDirectory(dir, false, false)); |
| 243 ASSERT_TRUE(file_util::DirectoryExists(dir)); | 243 ASSERT_TRUE(base::DirectoryExists(dir)); |
| 244 base::FilePath to_dir_file = dir.AppendASCII("file"); | 244 base::FilePath to_dir_file = dir.AppendASCII("file"); |
| 245 ASSERT_EQ(base::PLATFORM_FILE_OK, | 245 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 246 NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, true)); | 246 NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, true)); |
| 247 EXPECT_TRUE(FileExists(to_dir_file)); | 247 EXPECT_TRUE(FileExists(to_dir_file)); |
| 248 EXPECT_EQ(1020, GetSize(to_dir_file)); | 248 EXPECT_EQ(1020, GetSize(to_dir_file)); |
| 249 | 249 |
| 250 // Following tests are error checking. | 250 // Following tests are error checking. |
| 251 // Source doesn't exist. | 251 // Source doesn't exist. |
| 252 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 252 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 253 NativeFileUtil::CopyOrMoveFile(Path("nonexists"), Path("file"), | 253 NativeFileUtil::CopyOrMoveFile(Path("nonexists"), Path("file"), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 EXPECT_EQ(1020, GetSize(to_file)); | 292 EXPECT_EQ(1020, GetSize(to_file)); |
| 293 | 293 |
| 294 ASSERT_EQ(base::PLATFORM_FILE_OK, | 294 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 295 NativeFileUtil::EnsureFileExists(from_file, &created)); | 295 NativeFileUtil::EnsureFileExists(from_file, &created)); |
| 296 ASSERT_TRUE(FileExists(from_file)); | 296 ASSERT_TRUE(FileExists(from_file)); |
| 297 ASSERT_EQ(base::PLATFORM_FILE_OK, NativeFileUtil::Truncate(from_file, 1020)); | 297 ASSERT_EQ(base::PLATFORM_FILE_OK, NativeFileUtil::Truncate(from_file, 1020)); |
| 298 | 298 |
| 299 base::FilePath dir = Path("dir"); | 299 base::FilePath dir = Path("dir"); |
| 300 ASSERT_EQ(base::PLATFORM_FILE_OK, | 300 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 301 NativeFileUtil::CreateDirectory(dir, false, false)); | 301 NativeFileUtil::CreateDirectory(dir, false, false)); |
| 302 ASSERT_TRUE(file_util::DirectoryExists(dir)); | 302 ASSERT_TRUE(base::DirectoryExists(dir)); |
| 303 base::FilePath to_dir_file = dir.AppendASCII("file"); | 303 base::FilePath to_dir_file = dir.AppendASCII("file"); |
| 304 ASSERT_EQ(base::PLATFORM_FILE_OK, | 304 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 305 NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, false)); | 305 NativeFileUtil::CopyOrMoveFile(from_file, to_dir_file, false)); |
| 306 EXPECT_FALSE(FileExists(from_file)); | 306 EXPECT_FALSE(FileExists(from_file)); |
| 307 EXPECT_TRUE(FileExists(to_dir_file)); | 307 EXPECT_TRUE(FileExists(to_dir_file)); |
| 308 EXPECT_EQ(1020, GetSize(to_dir_file)); | 308 EXPECT_EQ(1020, GetSize(to_dir_file)); |
| 309 | 309 |
| 310 // Following is error checking. | 310 // Following is error checking. |
| 311 // Source doesn't exist. | 311 // Source doesn't exist. |
| 312 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 312 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 332 Path("nodir").AppendASCII("file"), | 332 Path("nodir").AppendASCII("file"), |
| 333 false)); | 333 false)); |
| 334 // Destination's parent is a file. | 334 // Destination's parent is a file. |
| 335 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 335 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 336 NativeFileUtil::CopyOrMoveFile(from_file, | 336 NativeFileUtil::CopyOrMoveFile(from_file, |
| 337 Path("tofile1").AppendASCII("file"), | 337 Path("tofile1").AppendASCII("file"), |
| 338 false)); | 338 false)); |
| 339 } | 339 } |
| 340 | 340 |
| 341 } // namespace fileapi | 341 } // namespace fileapi |
| OLD | NEW |