| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/memory/scoped_callback_factory.h" | 8 #include "base/memory/scoped_callback_factory.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webkit/fileapi/file_system_context.h" | 15 #include "webkit/fileapi/file_system_context.h" |
| 16 #include "webkit/fileapi/file_system_file_util.h" | 16 #include "webkit/fileapi/file_system_file_util.h" |
| 17 #include "webkit/fileapi/file_system_operation_context.h" | 17 #include "webkit/fileapi/file_system_operation_context.h" |
| 18 #include "webkit/fileapi/file_system_path_manager.h" | 18 #include "webkit/fileapi/file_system_path_manager.h" |
| 19 #include "webkit/fileapi/file_system_test_helper.h" | 19 #include "webkit/fileapi/file_system_test_helper.h" |
| 20 #include "webkit/fileapi/file_system_types.h" | 20 #include "webkit/fileapi/file_system_types.h" |
| 21 #include "webkit/fileapi/local_file_system_file_util.h" | 21 #include "webkit/fileapi/local_file_util.h" |
| 22 #include "webkit/fileapi/native_file_util.h" |
| 22 | 23 |
| 23 namespace fileapi { | 24 namespace fileapi { |
| 24 | 25 |
| 25 // TODO(dmikurube): Cover all public methods in LocalFileSystemFileUtil. | 26 // TODO(dmikurube): Cover all public methods in LocalFileUtil. |
| 26 class LocalFileSystemFileUtilTest : public testing::Test { | 27 class LocalFileUtilTest : public testing::Test { |
| 27 public: | 28 public: |
| 28 LocalFileSystemFileUtilTest() | 29 LocalFileUtilTest() |
| 29 : local_file_util_( | 30 : local_file_util_(new LocalFileUtil(new NativeFileUtil())), |
| 30 new LocalFileSystemFileUtil(new FileSystemFileUtil())), | |
| 31 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 31 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 32 } | 32 } |
| 33 | 33 |
| 34 void SetUp() { | 34 void SetUp() { |
| 35 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 35 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 36 test_helper_.SetUp(data_dir_.path(), FileUtil()); | 36 test_helper_.SetUp(data_dir_.path(), FileUtil()); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void TearDown() { | 39 void TearDown() { |
| 40 test_helper_.TearDown(); | 40 test_helper_.TearDown(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 protected: | 43 protected: |
| 44 FileSystemOperationContext* NewContext() { | 44 FileSystemOperationContext* NewContext() { |
| 45 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | 45 FileSystemOperationContext* context = test_helper_.NewOperationContext(); |
| 46 return context; | 46 return context; |
| 47 } | 47 } |
| 48 | 48 |
| 49 LocalFileSystemFileUtil* FileUtil() { | 49 LocalFileUtil* FileUtil() { |
| 50 return local_file_util_.get(); | 50 return local_file_util_.get(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 static FilePath Path(const std::string& file_name) { | 53 static FilePath Path(const std::string& file_name) { |
| 54 return FilePath().AppendASCII(file_name); | 54 return FilePath().AppendASCII(file_name); |
| 55 } | 55 } |
| 56 | 56 |
| 57 FilePath LocalPath(const char *file_name) { | 57 FilePath LocalPath(const char *file_name) { |
| 58 return test_helper_.GetLocalPathFromASCII(file_name); | 58 return test_helper_.GetLocalPathFromASCII(file_name); |
| 59 } | 59 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 87 | 87 |
| 88 base::PlatformFileError EnsureFileExists(const char* file_name, | 88 base::PlatformFileError EnsureFileExists(const char* file_name, |
| 89 bool* created) { | 89 bool* created) { |
| 90 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 90 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 91 return FileUtil()->EnsureFileExists( | 91 return FileUtil()->EnsureFileExists( |
| 92 context.get(), | 92 context.get(), |
| 93 Path(file_name), created); | 93 Path(file_name), created); |
| 94 } | 94 } |
| 95 | 95 |
| 96 private: | 96 private: |
| 97 scoped_ptr<LocalFileSystemFileUtil> local_file_util_; | 97 scoped_ptr<LocalFileUtil> local_file_util_; |
| 98 ScopedTempDir data_dir_; | 98 ScopedTempDir data_dir_; |
| 99 FileSystemTestOriginHelper test_helper_; | 99 FileSystemTestOriginHelper test_helper_; |
| 100 | 100 |
| 101 base::ScopedCallbackFactory<LocalFileSystemFileUtilTest> callback_factory_; | 101 base::ScopedCallbackFactory<LocalFileUtilTest> callback_factory_; |
| 102 | 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemFileUtilTest); | 103 DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest); |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 TEST_F(LocalFileSystemFileUtilTest, CreateAndClose) { | 106 TEST_F(LocalFileUtilTest, CreateAndClose) { |
| 107 const char *file_name = "test_file"; | 107 const char *file_name = "test_file"; |
| 108 base::PlatformFile file_handle; | 108 base::PlatformFile file_handle; |
| 109 bool created; | 109 bool created; |
| 110 ASSERT_EQ(base::PLATFORM_FILE_OK, | 110 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 111 CreateFile(file_name, &file_handle, &created)); | 111 CreateFile(file_name, &file_handle, &created)); |
| 112 ASSERT_TRUE(created); | 112 ASSERT_TRUE(created); |
| 113 | 113 |
| 114 EXPECT_TRUE(FileExists(file_name)); | 114 EXPECT_TRUE(FileExists(file_name)); |
| 115 EXPECT_EQ(0, GetSize(file_name)); | 115 EXPECT_EQ(0, GetSize(file_name)); |
| 116 | 116 |
| 117 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 117 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 118 EXPECT_EQ(base::PLATFORM_FILE_OK, | 118 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 119 FileUtil()->Close(context.get(), file_handle)); | 119 FileUtil()->Close(context.get(), file_handle)); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(LocalFileSystemFileUtilTest, EnsureFileExists) { | 122 TEST_F(LocalFileUtilTest, EnsureFileExists) { |
| 123 const char *file_name = "foobar"; | 123 const char *file_name = "foobar"; |
| 124 bool created; | 124 bool created; |
| 125 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | 125 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 126 ASSERT_TRUE(created); | 126 ASSERT_TRUE(created); |
| 127 | 127 |
| 128 EXPECT_TRUE(FileExists(file_name)); | 128 EXPECT_TRUE(FileExists(file_name)); |
| 129 EXPECT_EQ(0, GetSize(file_name)); | 129 EXPECT_EQ(0, GetSize(file_name)); |
| 130 | 130 |
| 131 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | 131 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 132 EXPECT_FALSE(created); | 132 EXPECT_FALSE(created); |
| 133 } | 133 } |
| 134 | 134 |
| 135 TEST_F(LocalFileSystemFileUtilTest, Truncate) { | 135 TEST_F(LocalFileUtilTest, Truncate) { |
| 136 const char *file_name = "truncated"; | 136 const char *file_name = "truncated"; |
| 137 bool created; | 137 bool created; |
| 138 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | 138 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 139 ASSERT_TRUE(created); | 139 ASSERT_TRUE(created); |
| 140 | 140 |
| 141 scoped_ptr<FileSystemOperationContext> context; | 141 scoped_ptr<FileSystemOperationContext> context; |
| 142 | 142 |
| 143 context.reset(NewContext()); | 143 context.reset(NewContext()); |
| 144 ASSERT_EQ(base::PLATFORM_FILE_OK, | 144 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 145 FileUtil()->Truncate(context.get(), Path(file_name), 1020)); | 145 FileUtil()->Truncate(context.get(), Path(file_name), 1020)); |
| 146 | 146 |
| 147 EXPECT_TRUE(FileExists(file_name)); | 147 EXPECT_TRUE(FileExists(file_name)); |
| 148 EXPECT_EQ(1020, GetSize(file_name)); | 148 EXPECT_EQ(1020, GetSize(file_name)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 TEST_F(LocalFileSystemFileUtilTest, CopyFile) { | 151 TEST_F(LocalFileUtilTest, CopyFile) { |
| 152 const char *from_file = "fromfile"; | 152 const char *from_file = "fromfile"; |
| 153 const char *to_file1 = "tofile1"; | 153 const char *to_file1 = "tofile1"; |
| 154 const char *to_file2 = "tofile2"; | 154 const char *to_file2 = "tofile2"; |
| 155 bool created; | 155 bool created; |
| 156 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | 156 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 157 ASSERT_TRUE(created); | 157 ASSERT_TRUE(created); |
| 158 scoped_ptr<FileSystemOperationContext> context; | 158 scoped_ptr<FileSystemOperationContext> context; |
| 159 | 159 |
| 160 context.reset(NewContext()); | 160 context.reset(NewContext()); |
| 161 ASSERT_EQ(base::PLATFORM_FILE_OK, | 161 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 173 FileUtil()->Copy(context.get(), Path(from_file), Path(to_file2))); | 173 FileUtil()->Copy(context.get(), Path(from_file), Path(to_file2))); |
| 174 | 174 |
| 175 EXPECT_TRUE(FileExists(from_file)); | 175 EXPECT_TRUE(FileExists(from_file)); |
| 176 EXPECT_EQ(1020, GetSize(from_file)); | 176 EXPECT_EQ(1020, GetSize(from_file)); |
| 177 EXPECT_TRUE(FileExists(to_file1)); | 177 EXPECT_TRUE(FileExists(to_file1)); |
| 178 EXPECT_EQ(1020, GetSize(to_file1)); | 178 EXPECT_EQ(1020, GetSize(to_file1)); |
| 179 EXPECT_TRUE(FileExists(to_file2)); | 179 EXPECT_TRUE(FileExists(to_file2)); |
| 180 EXPECT_EQ(1020, GetSize(to_file2)); | 180 EXPECT_EQ(1020, GetSize(to_file2)); |
| 181 } | 181 } |
| 182 | 182 |
| 183 TEST_F(LocalFileSystemFileUtilTest, CopyDirectory) { | 183 TEST_F(LocalFileUtilTest, CopyDirectory) { |
| 184 const char *from_dir = "fromdir"; | 184 const char *from_dir = "fromdir"; |
| 185 const char *from_file = "fromdir/fromfile"; | 185 const char *from_file = "fromdir/fromfile"; |
| 186 const char *to_dir = "todir"; | 186 const char *to_dir = "todir"; |
| 187 const char *to_file = "todir/fromfile"; | 187 const char *to_file = "todir/fromfile"; |
| 188 bool created; | 188 bool created; |
| 189 scoped_ptr<FileSystemOperationContext> context; | 189 scoped_ptr<FileSystemOperationContext> context; |
| 190 | 190 |
| 191 context.reset(NewContext()); | 191 context.reset(NewContext()); |
| 192 ASSERT_EQ(base::PLATFORM_FILE_OK, | 192 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 193 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | 193 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 208 FileUtil()->Copy(context.get(), Path(from_dir), Path(to_dir))); | 208 FileUtil()->Copy(context.get(), Path(from_dir), Path(to_dir))); |
| 209 | 209 |
| 210 EXPECT_TRUE(DirectoryExists(from_dir)); | 210 EXPECT_TRUE(DirectoryExists(from_dir)); |
| 211 EXPECT_TRUE(FileExists(from_file)); | 211 EXPECT_TRUE(FileExists(from_file)); |
| 212 EXPECT_EQ(1020, GetSize(from_file)); | 212 EXPECT_EQ(1020, GetSize(from_file)); |
| 213 EXPECT_TRUE(DirectoryExists(to_dir)); | 213 EXPECT_TRUE(DirectoryExists(to_dir)); |
| 214 EXPECT_TRUE(FileExists(to_file)); | 214 EXPECT_TRUE(FileExists(to_file)); |
| 215 EXPECT_EQ(1020, GetSize(to_file)); | 215 EXPECT_EQ(1020, GetSize(to_file)); |
| 216 } | 216 } |
| 217 | 217 |
| 218 TEST_F(LocalFileSystemFileUtilTest, MoveFile) { | 218 TEST_F(LocalFileUtilTest, MoveFile) { |
| 219 const char *from_file = "fromfile"; | 219 const char *from_file = "fromfile"; |
| 220 const char *to_file = "tofile"; | 220 const char *to_file = "tofile"; |
| 221 bool created; | 221 bool created; |
| 222 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | 222 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 223 ASSERT_TRUE(created); | 223 ASSERT_TRUE(created); |
| 224 scoped_ptr<FileSystemOperationContext> context; | 224 scoped_ptr<FileSystemOperationContext> context; |
| 225 | 225 |
| 226 context.reset(NewContext()); | 226 context.reset(NewContext()); |
| 227 ASSERT_EQ(base::PLATFORM_FILE_OK, | 227 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 228 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | 228 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); |
| 229 | 229 |
| 230 EXPECT_TRUE(FileExists(from_file)); | 230 EXPECT_TRUE(FileExists(from_file)); |
| 231 EXPECT_EQ(1020, GetSize(from_file)); | 231 EXPECT_EQ(1020, GetSize(from_file)); |
| 232 | 232 |
| 233 context.reset(NewContext()); | 233 context.reset(NewContext()); |
| 234 ASSERT_EQ(base::PLATFORM_FILE_OK, | 234 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 235 FileUtil()->Move(context.get(), Path(from_file), Path(to_file))); | 235 FileUtil()->Move(context.get(), Path(from_file), Path(to_file))); |
| 236 | 236 |
| 237 EXPECT_FALSE(FileExists(from_file)); | 237 EXPECT_FALSE(FileExists(from_file)); |
| 238 EXPECT_TRUE(FileExists(to_file)); | 238 EXPECT_TRUE(FileExists(to_file)); |
| 239 EXPECT_EQ(1020, GetSize(to_file)); | 239 EXPECT_EQ(1020, GetSize(to_file)); |
| 240 } | 240 } |
| 241 | 241 |
| 242 TEST_F(LocalFileSystemFileUtilTest, MoveDirectory) { | 242 TEST_F(LocalFileUtilTest, MoveDirectory) { |
| 243 const char *from_dir = "fromdir"; | 243 const char *from_dir = "fromdir"; |
| 244 const char *from_file = "fromdir/fromfile"; | 244 const char *from_file = "fromdir/fromfile"; |
| 245 const char *to_dir = "todir"; | 245 const char *to_dir = "todir"; |
| 246 const char *to_file = "todir/fromfile"; | 246 const char *to_file = "todir/fromfile"; |
| 247 bool created; | 247 bool created; |
| 248 scoped_ptr<FileSystemOperationContext> context; | 248 scoped_ptr<FileSystemOperationContext> context; |
| 249 | 249 |
| 250 context.reset(NewContext()); | 250 context.reset(NewContext()); |
| 251 ASSERT_EQ(base::PLATFORM_FILE_OK, | 251 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 252 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | 252 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 266 ASSERT_EQ(base::PLATFORM_FILE_OK, | 266 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 267 FileUtil()->Move(context.get(), Path(from_dir), Path(to_dir))); | 267 FileUtil()->Move(context.get(), Path(from_dir), Path(to_dir))); |
| 268 | 268 |
| 269 EXPECT_FALSE(DirectoryExists(from_dir)); | 269 EXPECT_FALSE(DirectoryExists(from_dir)); |
| 270 EXPECT_TRUE(DirectoryExists(to_dir)); | 270 EXPECT_TRUE(DirectoryExists(to_dir)); |
| 271 EXPECT_TRUE(FileExists(to_file)); | 271 EXPECT_TRUE(FileExists(to_file)); |
| 272 EXPECT_EQ(1020, GetSize(to_file)); | 272 EXPECT_EQ(1020, GetSize(to_file)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 } // namespace fileapi | 275 } // namespace fileapi |
| OLD | NEW |