OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/memory/scoped_callback_factory.h" | |
9 #include "base/message_loop_proxy.h" | |
10 #include "base/platform_file.h" | |
11 #include "base/scoped_temp_dir.h" | |
12 #include "base/sys_string_conversions.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "webkit/fileapi/file_system_context.h" | |
16 #include "webkit/fileapi/file_system_file_util.h" | |
17 #include "webkit/fileapi/file_system_operation_context.h" | |
18 #include "webkit/fileapi/file_system_path_manager.h" | |
19 #include "webkit/fileapi/file_system_test_helper.h" | |
20 #include "webkit/fileapi/file_system_types.h" | |
21 #include "webkit/fileapi/local_file_system_file_util.h" | |
22 | |
23 namespace fileapi { | |
24 | |
25 // TODO(dmikurube): Cover all public methods in LocalFileSystemFileUtil. | |
26 class LocalFileSystemFileUtilTest : public testing::Test { | |
27 public: | |
28 LocalFileSystemFileUtilTest() | |
29 : local_file_util_( | |
30 new LocalFileSystemFileUtil(new FileSystemFileUtil())), | |
31 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
32 } | |
33 | |
34 void SetUp() { | |
35 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
36 test_helper_.SetUp(data_dir_.path(), FileUtil()); | |
37 } | |
38 | |
39 void TearDown() { | |
40 test_helper_.TearDown(); | |
41 } | |
42 | |
43 protected: | |
44 FileSystemOperationContext* NewContext() { | |
45 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | |
46 return context; | |
47 } | |
48 | |
49 LocalFileSystemFileUtil* FileUtil() { | |
50 return local_file_util_.get(); | |
51 } | |
52 | |
53 static FilePath Path(const std::string& file_name) { | |
54 return FilePath().AppendASCII(file_name); | |
55 } | |
56 | |
57 FilePath LocalPath(const char *file_name) { | |
58 return test_helper_.GetLocalPathFromASCII(file_name); | |
59 } | |
60 | |
61 bool FileExists(const char *file_name) { | |
62 return file_util::PathExists(LocalPath(file_name)) && | |
63 !file_util::DirectoryExists(LocalPath(file_name)); | |
64 } | |
65 | |
66 bool DirectoryExists(const char *file_name) { | |
67 return file_util::DirectoryExists(LocalPath(file_name)); | |
68 } | |
69 | |
70 int64 GetSize(const char *file_name) { | |
71 base::PlatformFileInfo info; | |
72 file_util::GetFileInfo(LocalPath(file_name), &info); | |
73 return info.size; | |
74 } | |
75 | |
76 base::PlatformFileError CreateFile(const char* file_name, | |
77 base::PlatformFile* file_handle, bool* created) { | |
78 int file_flags = base::PLATFORM_FILE_CREATE | | |
79 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; | |
80 | |
81 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
82 return FileUtil()->CreateOrOpen( | |
83 context.get(), | |
84 Path(file_name), | |
85 file_flags, file_handle, created); | |
86 } | |
87 | |
88 base::PlatformFileError EnsureFileExists(const char* file_name, | |
89 bool* created) { | |
90 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
91 return FileUtil()->EnsureFileExists( | |
92 context.get(), | |
93 Path(file_name), created); | |
94 } | |
95 | |
96 private: | |
97 scoped_ptr<LocalFileSystemFileUtil> local_file_util_; | |
98 ScopedTempDir data_dir_; | |
99 FileSystemTestOriginHelper test_helper_; | |
100 | |
101 base::ScopedCallbackFactory<LocalFileSystemFileUtilTest> callback_factory_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemFileUtilTest); | |
104 }; | |
105 | |
106 TEST_F(LocalFileSystemFileUtilTest, CreateAndClose) { | |
107 const char *file_name = "test_file"; | |
108 base::PlatformFile file_handle; | |
109 bool created; | |
110 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
111 CreateFile(file_name, &file_handle, &created)); | |
112 ASSERT_TRUE(created); | |
113 | |
114 EXPECT_TRUE(FileExists(file_name)); | |
115 EXPECT_EQ(0, GetSize(file_name)); | |
116 | |
117 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
118 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
119 FileUtil()->Close(context.get(), file_handle)); | |
120 } | |
121 | |
122 TEST_F(LocalFileSystemFileUtilTest, EnsureFileExists) { | |
123 const char *file_name = "foobar"; | |
124 bool created; | |
125 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
126 ASSERT_TRUE(created); | |
127 | |
128 EXPECT_TRUE(FileExists(file_name)); | |
129 EXPECT_EQ(0, GetSize(file_name)); | |
130 | |
131 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
132 EXPECT_FALSE(created); | |
133 } | |
134 | |
135 TEST_F(LocalFileSystemFileUtilTest, Truncate) { | |
136 const char *file_name = "truncated"; | |
137 bool created; | |
138 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
139 ASSERT_TRUE(created); | |
140 | |
141 scoped_ptr<FileSystemOperationContext> context; | |
142 | |
143 context.reset(NewContext()); | |
144 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
145 FileUtil()->Truncate(context.get(), Path(file_name), 1020)); | |
146 | |
147 EXPECT_TRUE(FileExists(file_name)); | |
148 EXPECT_EQ(1020, GetSize(file_name)); | |
149 } | |
150 | |
151 TEST_F(LocalFileSystemFileUtilTest, CopyFile) { | |
152 const char *from_file = "fromfile"; | |
153 const char *to_file1 = "tofile1"; | |
154 const char *to_file2 = "tofile2"; | |
155 bool created; | |
156 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
157 ASSERT_TRUE(created); | |
158 scoped_ptr<FileSystemOperationContext> context; | |
159 | |
160 context.reset(NewContext()); | |
161 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
162 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
163 | |
164 EXPECT_TRUE(FileExists(from_file)); | |
165 EXPECT_EQ(1020, GetSize(from_file)); | |
166 | |
167 context.reset(NewContext()); | |
168 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
169 FileUtil()->Copy(context.get(), Path(from_file), Path(to_file1))); | |
170 | |
171 context.reset(NewContext()); | |
172 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
173 FileUtil()->Copy(context.get(), Path(from_file), Path(to_file2))); | |
174 | |
175 EXPECT_TRUE(FileExists(from_file)); | |
176 EXPECT_EQ(1020, GetSize(from_file)); | |
177 EXPECT_TRUE(FileExists(to_file1)); | |
178 EXPECT_EQ(1020, GetSize(to_file1)); | |
179 EXPECT_TRUE(FileExists(to_file2)); | |
180 EXPECT_EQ(1020, GetSize(to_file2)); | |
181 } | |
182 | |
183 TEST_F(LocalFileSystemFileUtilTest, CopyDirectory) { | |
184 const char *from_dir = "fromdir"; | |
185 const char *from_file = "fromdir/fromfile"; | |
186 const char *to_dir = "todir"; | |
187 const char *to_file = "todir/fromfile"; | |
188 bool created; | |
189 scoped_ptr<FileSystemOperationContext> context; | |
190 | |
191 context.reset(NewContext()); | |
192 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
193 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | |
194 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
195 ASSERT_TRUE(created); | |
196 | |
197 context.reset(NewContext()); | |
198 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
199 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
200 | |
201 EXPECT_TRUE(DirectoryExists(from_dir)); | |
202 EXPECT_TRUE(FileExists(from_file)); | |
203 EXPECT_EQ(1020, GetSize(from_file)); | |
204 EXPECT_FALSE(DirectoryExists(to_dir)); | |
205 | |
206 context.reset(NewContext()); | |
207 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
208 FileUtil()->Copy(context.get(), Path(from_dir), Path(to_dir))); | |
209 | |
210 EXPECT_TRUE(DirectoryExists(from_dir)); | |
211 EXPECT_TRUE(FileExists(from_file)); | |
212 EXPECT_EQ(1020, GetSize(from_file)); | |
213 EXPECT_TRUE(DirectoryExists(to_dir)); | |
214 EXPECT_TRUE(FileExists(to_file)); | |
215 EXPECT_EQ(1020, GetSize(to_file)); | |
216 } | |
217 | |
218 TEST_F(LocalFileSystemFileUtilTest, MoveFile) { | |
219 const char *from_file = "fromfile"; | |
220 const char *to_file = "tofile"; | |
221 bool created; | |
222 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
223 ASSERT_TRUE(created); | |
224 scoped_ptr<FileSystemOperationContext> context; | |
225 | |
226 context.reset(NewContext()); | |
227 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
228 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
229 | |
230 EXPECT_TRUE(FileExists(from_file)); | |
231 EXPECT_EQ(1020, GetSize(from_file)); | |
232 | |
233 context.reset(NewContext()); | |
234 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
235 FileUtil()->Move(context.get(), Path(from_file), Path(to_file))); | |
236 | |
237 EXPECT_FALSE(FileExists(from_file)); | |
238 EXPECT_TRUE(FileExists(to_file)); | |
239 EXPECT_EQ(1020, GetSize(to_file)); | |
240 } | |
241 | |
242 TEST_F(LocalFileSystemFileUtilTest, MoveDirectory) { | |
243 const char *from_dir = "fromdir"; | |
244 const char *from_file = "fromdir/fromfile"; | |
245 const char *to_dir = "todir"; | |
246 const char *to_file = "todir/fromfile"; | |
247 bool created; | |
248 scoped_ptr<FileSystemOperationContext> context; | |
249 | |
250 context.reset(NewContext()); | |
251 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
252 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | |
253 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
254 ASSERT_TRUE(created); | |
255 | |
256 context.reset(NewContext()); | |
257 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
258 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
259 | |
260 EXPECT_TRUE(DirectoryExists(from_dir)); | |
261 EXPECT_TRUE(FileExists(from_file)); | |
262 EXPECT_EQ(1020, GetSize(from_file)); | |
263 EXPECT_FALSE(DirectoryExists(to_dir)); | |
264 | |
265 context.reset(NewContext()); | |
266 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
267 FileUtil()->Move(context.get(), Path(from_dir), Path(to_dir))); | |
268 | |
269 EXPECT_FALSE(DirectoryExists(from_dir)); | |
270 EXPECT_TRUE(DirectoryExists(to_dir)); | |
271 EXPECT_TRUE(FileExists(to_file)); | |
272 EXPECT_EQ(1020, GetSize(to_file)); | |
273 } | |
274 | |
275 } // namespace fileapi | |
OLD | NEW |