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 "webkit/fileapi/file_system_operation.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/scoped_callback_factory.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/scoped_temp_dir.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/platform_file.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "webkit/fileapi/file_system_callback_dispatcher.h" | |
16 #include "webkit/fileapi/file_system_context.h" | |
17 #include "webkit/fileapi/file_system_file_util.h" | |
18 #include "webkit/fileapi/file_system_mount_point_provider.h" | |
19 #include "webkit/fileapi/file_system_operation.h" | |
20 #include "webkit/fileapi/file_system_path_manager.h" | |
21 #include "webkit/fileapi/file_system_usage_cache.h" | |
22 #include "webkit/fileapi/file_system_util.h" | |
23 #include "webkit/fileapi/local_file_system_file_util.h" | |
24 #include "webkit/fileapi/quota_file_util.h" | |
25 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
26 #include "webkit/quota/quota_manager.h" | |
27 | |
28 namespace fileapi { | |
29 | |
30 const int kFileOperationStatusNotSet = 1; | |
31 | |
32 class FileSystemQuotaTest : public testing::Test { | |
33 public: | |
34 FileSystemQuotaTest() | |
35 : status_(kFileOperationStatusNotSet), | |
36 quota_status_(quota::kQuotaStatusUnknown), | |
37 usage_(-1), | |
38 quota_(-1), | |
39 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | |
40 | |
41 FileSystemOperation* operation(); | |
42 | |
43 void set_local_path(const FilePath& path) { local_path_ = path; } | |
44 const FilePath& local_path() const { return local_path_; } | |
45 void set_status(int status) { status_ = status; } | |
46 int status() const { return status_; } | |
47 void set_info(const base::PlatformFileInfo& info) { info_ = info; } | |
48 const base::PlatformFileInfo& info() const { return info_; } | |
49 void set_path(const FilePath& path) { path_ = path; } | |
50 const FilePath& path() const { return path_; } | |
51 void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) { | |
52 entries_ = entries; | |
53 } | |
54 const std::vector<base::FileUtilProxy::Entry>& entries() const { | |
55 return entries_; | |
56 } | |
57 | |
58 virtual void SetUp(); | |
59 virtual void TearDown(); | |
60 | |
61 void OnGetUsageAndQuota( | |
62 quota::QuotaStatusCode status, int64 usage, int64 quota); | |
63 | |
64 protected: | |
65 base::PlatformFile OpenFile(const FilePath& virtual_path); | |
66 void PrepareFileSet(const FilePath& virtual_path); | |
67 | |
68 GURL URLForPath(const FilePath& path) const { | |
69 // Only the path will actually get used. | |
70 return GURL(GetFileSystemRootURI(GURL("http://www.example.com/"), | |
71 kFileSystemTypeTemporary).spec() + path.MaybeAsASCII()); | |
72 } | |
73 | |
74 FilePath PlatformPath(FilePath virtual_path) { | |
75 return root_dir_.Append(virtual_path); | |
76 } | |
77 | |
78 bool VirtualFileExists(FilePath virtual_path) { | |
79 return file_util::PathExists(PlatformPath(virtual_path)) && | |
80 !file_util::DirectoryExists(PlatformPath(virtual_path)); | |
81 } | |
82 | |
83 bool VirtualDirectoryExists(FilePath virtual_path) { | |
84 return file_util::DirectoryExists(PlatformPath(virtual_path)); | |
85 } | |
86 | |
87 FilePath CreateVirtualDirectory(const char* virtual_path_string) { | |
88 FilePath virtual_path(virtual_path_string); | |
89 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
90 return virtual_path; | |
91 } | |
92 | |
93 FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string, | |
94 const FilePath& virtual_dir_path) { | |
95 FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string)); | |
96 file_util::CreateDirectory(PlatformPath(virtual_path)); | |
97 return virtual_path; | |
98 } | |
99 | |
100 FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { | |
101 FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); | |
102 FilePath absolute_file_path; | |
103 if (file_util::CreateTemporaryFileInDir(absolute_dir_path, | |
104 &absolute_file_path)) | |
105 return virtual_dir_path.Append(absolute_file_path.BaseName()); | |
106 else | |
107 return FilePath(); | |
108 } | |
109 | |
110 FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path, | |
111 const FilePath::StringType& prefix) { | |
112 FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); | |
113 FilePath absolute_child_dir_path; | |
114 if (file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, prefix, | |
115 &absolute_child_dir_path)) | |
116 return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); | |
117 else | |
118 return FilePath(); | |
119 } | |
120 | |
121 FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) { | |
122 return CreateVirtualTemporaryDirInDir(FilePath(), prefix); | |
123 } | |
124 | |
125 ScopedTempDir base_dir_; | |
126 scoped_refptr<FileSystemContext> file_system_context_; | |
127 scoped_refptr<quota::QuotaManager> quota_manager_; | |
128 scoped_ptr<FileSystemPathManager> path_manager_; | |
kinuko
2011/05/09 15:20:38
path_manager is owned by file_system_context, so y
Dai Mikurube (NOT FULLTIME)
2011/05/10 08:58:23
Oh, I see. Changed it to a simple pointer.
| |
129 | |
130 FilePath child_dir_path_; | |
131 FilePath child_file1_path_; | |
132 FilePath child_file2_path_; | |
133 FilePath grandchild_file1_path_; | |
134 FilePath grandchild_file2_path_; | |
135 base::PlatformFile child_file1_; | |
136 base::PlatformFile child_file2_; | |
137 base::PlatformFile grandchild_file1_; | |
138 base::PlatformFile grandchild_file2_; | |
139 | |
140 // For post-operation status. | |
141 int status_; | |
142 quota::QuotaStatusCode quota_status_; | |
143 int64 usage_; | |
144 int64 quota_; | |
145 base::PlatformFileInfo info_; | |
146 FilePath path_; | |
147 FilePath local_path_; | |
148 FilePath filesystem_dir_; | |
149 FilePath root_dir_; | |
150 std::vector<base::FileUtilProxy::Entry> entries_; | |
151 | |
152 base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; | |
153 | |
154 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); | |
155 }; | |
156 | |
157 namespace { | |
158 | |
159 class MockDispatcher : public FileSystemCallbackDispatcher { | |
160 public: | |
161 MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } | |
162 | |
163 virtual void DidFail(base::PlatformFileError status) { | |
164 test_->set_status(status); | |
165 } | |
166 | |
167 virtual void DidSucceed() { | |
168 test_->set_status(base::PLATFORM_FILE_OK); | |
169 } | |
170 | |
171 virtual void DidGetLocalPath(const FilePath& local_path) { | |
172 test_->set_local_path(local_path); | |
173 test_->set_status(base::PLATFORM_FILE_OK); | |
174 } | |
175 | |
176 virtual void DidReadMetadata( | |
177 const base::PlatformFileInfo& info, | |
178 const FilePath& platform_path) { | |
179 test_->set_info(info); | |
180 test_->set_path(platform_path); | |
181 test_->set_status(base::PLATFORM_FILE_OK); | |
182 } | |
183 | |
184 virtual void DidReadDirectory( | |
185 const std::vector<base::FileUtilProxy::Entry>& entries, | |
186 bool /* has_more */) { | |
187 test_->set_entries(entries); | |
188 } | |
189 | |
190 virtual void DidOpenFileSystem(const std::string&, const GURL&) { | |
191 NOTREACHED(); | |
192 } | |
193 | |
194 virtual void DidWrite(int64 bytes, bool complete) { | |
195 NOTREACHED(); | |
196 } | |
197 | |
198 private: | |
199 FileSystemQuotaTest* test_; | |
200 }; | |
201 | |
202 } // namespace (anonymous) | |
203 | |
204 void FileSystemQuotaTest::SetUp() { | |
205 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | |
206 filesystem_dir_ = base_dir_.path().AppendASCII("filesystem"); | |
207 file_util::CreateDirectory(filesystem_dir_); | |
208 | |
209 path_manager_.reset(new FileSystemPathManager( | |
210 base::MessageLoopProxy::CreateForCurrentThread(), | |
211 filesystem_dir_, NULL, false, true)); | |
212 | |
213 quota_manager_ = new quota::QuotaManager( | |
214 false /* is_incognito */, | |
215 filesystem_dir_, | |
216 base::MessageLoopProxy::CreateForCurrentThread(), | |
217 base::MessageLoopProxy::CreateForCurrentThread()); | |
218 | |
219 file_system_context_ = new FileSystemContext( | |
220 base::MessageLoopProxy::CreateForCurrentThread(), | |
221 base::MessageLoopProxy::CreateForCurrentThread(), | |
222 NULL, quota_manager_->proxy(), filesystem_dir_, | |
223 false /* is_incognito */, true, false, | |
224 path_manager_.get()); | |
kinuko
2011/05/09 15:20:38
The FSContext takes over the path manager's owners
| |
225 | |
226 // Creates directory including "chrome-*". | |
227 root_dir_ = path_manager_->sandbox_provider()-> | |
228 ValidateFileSystemRootAndGetPathOnFileThread( | |
229 GURL("http://www.example.com"), kFileSystemTypeTemporary, | |
230 FilePath(), true); | |
231 } | |
232 | |
233 void FileSystemQuotaTest::TearDown() { | |
234 file_system_context_ = NULL; | |
235 quota_manager_ = NULL; | |
236 path_manager_.reset(NULL); | |
kinuko
2011/05/09 15:20:38
No need to reset here.
Instead please call Messag
Dai Mikurube (NOT FULLTIME)
2011/05/10 08:58:23
It works. Thanks!
| |
237 } | |
238 | |
239 FileSystemOperation* FileSystemQuotaTest::operation() { | |
240 FileSystemOperation* operation = new FileSystemOperation( | |
241 new MockDispatcher(this), | |
242 base::MessageLoopProxy::CreateForCurrentThread(), | |
243 file_system_context_.get(), | |
244 LocalFileSystemFileUtil::GetInstance()); | |
245 operation->file_system_operation_context()->set_src_type( | |
246 kFileSystemTypeTemporary); | |
247 operation->file_system_operation_context()->set_dest_type( | |
248 kFileSystemTypeTemporary); | |
249 GURL origin_url("http://www.example.com/"); | |
250 operation->file_system_operation_context()->set_src_origin_url(origin_url); | |
251 operation->file_system_operation_context()->set_dest_origin_url(origin_url); | |
252 | |
253 return operation; | |
254 } | |
255 | |
256 base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) { | |
257 base::PlatformFile file; | |
258 bool created; | |
259 base::PlatformFileError error_code; | |
260 file = base::CreatePlatformFile( | |
261 PlatformPath(virtual_path), | |
262 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
263 base::PLATFORM_FILE_ASYNC, &created, &error_code); | |
264 //ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | |
265 return file; | |
266 } | |
267 | |
268 void FileSystemQuotaTest::OnGetUsageAndQuota( | |
269 quota::QuotaStatusCode status, int64 usage, int64 quota) { | |
270 quota_status_ = status; | |
271 usage_ = usage; | |
272 quota_ = quota; | |
273 } | |
274 | |
275 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { | |
276 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix"); | |
277 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
278 child_file1_ = OpenFile(child_file1_path_); | |
279 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); | |
280 child_file2_ = OpenFile(child_file2_path_); | |
281 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
282 grandchild_file1_ = OpenFile(grandchild_file1_path_); | |
283 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); | |
284 grandchild_file2_ = OpenFile(grandchild_file2_path_); | |
285 } | |
286 | |
287 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { | |
288 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
289 PrepareFileSet(src_dir_path); | |
290 FilePath dest_dir_path(CreateVirtualTemporaryDir("")); | |
291 | |
292 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 1013)); | |
293 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 129)); | |
294 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 94)); | |
295 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 517)); | |
296 | |
297 EXPECT_EQ(1013+129+94+517, | |
298 file_util::ComputeDirectorySize(PlatformPath(src_dir_path))); | |
299 | |
300 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
301 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
302 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
303 MessageLoop::current()->RunAllPending(); | |
304 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); | |
305 | |
306 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
307 MessageLoop::current()->RunAllPending(); | |
308 | |
309 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
310 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
311 child_dir_path_.BaseName()))); | |
312 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
313 child_dir_path_.BaseName()).Append( | |
314 grandchild_file1_path_.BaseName()))); | |
315 | |
316 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
317 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
318 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
319 MessageLoop::current()->RunAllPending(); | |
320 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); | |
321 } | |
322 | |
323 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive2) { | |
324 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
325 PrepareFileSet(src_dir_path); | |
326 FilePath dest_dir_path(CreateVirtualTemporaryDir("")); | |
327 | |
328 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 1013)); | |
329 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 129)); | |
330 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 94)); | |
331 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 517)); | |
332 | |
333 EXPECT_EQ(1013+129+94+517, | |
334 file_util::ComputeDirectorySize(PlatformPath(src_dir_path))); | |
335 | |
336 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
337 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
338 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
339 MessageLoop::current()->RunAllPending(); | |
340 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); | |
341 | |
342 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
343 MessageLoop::current()->RunAllPending(); | |
344 | |
345 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
346 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
347 child_dir_path_.BaseName()))); | |
348 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
349 child_dir_path_.BaseName()).Append( | |
350 grandchild_file1_path_.BaseName()))); | |
351 | |
352 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
353 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
354 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
355 MessageLoop::current()->RunAllPending(); | |
356 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); | |
357 } | |
358 | |
359 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { | |
360 FilePath src_dir_path(CreateVirtualTemporaryDir("")); | |
361 PrepareFileSet(src_dir_path); | |
362 FilePath dest_dir_path(CreateVirtualTemporaryDir("")); | |
363 | |
364 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 12)); | |
365 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 23)); | |
366 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 34)); | |
367 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 45)); | |
368 | |
369 EXPECT_EQ(12+23+34+45, | |
370 file_util::ComputeDirectorySize(PlatformPath(src_dir_path))); | |
371 | |
372 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
373 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
374 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
375 MessageLoop::current()->RunAllPending(); | |
376 EXPECT_EQ(12+23+34+45 + FileSystemUsageCache::kUsageFileSize, usage_); | |
377 | |
378 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir_path)); | |
379 MessageLoop::current()->RunAllPending(); | |
380 | |
381 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
382 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( | |
383 child_dir_path_.BaseName()))); | |
384 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( | |
385 child_dir_path_.BaseName()).Append( | |
386 grandchild_file1_path_.BaseName()))); | |
387 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
388 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( | |
389 child_dir_path_.BaseName()))); | |
390 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( | |
391 child_dir_path_.BaseName()).Append( | |
392 grandchild_file1_path_.BaseName()))); | |
393 | |
394 // TODO(dmikurube): These checks still fail... Notifying is required. | |
395 // we have to check : | |
396 // 1. The actual size of files on disk | |
397 // 2. The described size in .usage | |
398 // 3. The result of QuotaManager::GetUsageAndQuota | |
399 // are the same number. | |
400 EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize, | |
401 file_util::ComputeDirectorySize(PlatformPath(FilePath()))); | |
402 | |
403 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
404 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
405 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
406 MessageLoop::current()->RunAllPending(); | |
407 EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize, usage_); | |
408 | |
409 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir_path)); | |
410 MessageLoop::current()->RunAllPending(); | |
411 | |
412 quota_manager_.get()->proxy()->GetUsageAndQuota( | |
413 GURL("http://www.example.com"), quota::kStorageTypeTemporary, | |
414 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); | |
415 MessageLoop::current()->RunAllPending(); | |
416 EXPECT_EQ(2*(12+23)+3*(34+45) + FileSystemUsageCache::kUsageFileSize, usage_); | |
417 } | |
418 | |
419 #if 0 | |
420 TEST_F(FileSystemQuotaTest, TestRemoveSuccess) { | |
421 FilePath empty_dir_path(CreateVirtualTemporaryDir("")); | |
422 EXPECT_TRUE(VirtualDirectoryExists(empty_dir_path)); | |
423 | |
424 operation()->Remove(URLForPath(empty_dir_path), false /* recursive */); | |
425 MessageLoop::current()->RunAllPending(); | |
426 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
427 EXPECT_FALSE(VirtualDirectoryExists(empty_dir_path)); | |
428 | |
429 // Removing a non-empty directory with recursive flag == true should be ok. | |
430 // parent_dir | |
431 // | | | |
432 // child_dir child_file | |
433 // Verify deleting parent_dir. | |
434 FilePath parent_dir_path(CreateVirtualTemporaryDir("")); | |
435 FilePath child_file_path(CreateVirtualTemporaryFileInDir(parent_dir_path)); | |
436 FilePath child_dir_path(CreateVirtualTemporaryDirInDir(parent_dir_path, | |
437 "child_dir")); | |
438 ASSERT_FALSE(child_dir_path.empty()); | |
439 | |
440 operation()->Remove(URLForPath(parent_dir_path), true /* recursive */); | |
441 MessageLoop::current()->RunAllPending(); | |
442 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
443 EXPECT_FALSE(VirtualDirectoryExists(parent_dir_path)); | |
444 } | |
445 | |
446 TEST_F(FileSystemQuotaTest, TestTruncate) { | |
447 FilePath dir_path(CreateVirtualTemporaryDir("")); | |
448 FilePath file_path(CreateVirtualTemporaryFileInDir(dir_path)); | |
449 | |
450 char test_data[] = "test data"; | |
451 int data_size = static_cast<int>(sizeof(test_data)); | |
452 EXPECT_EQ(data_size, | |
453 file_util::WriteFile(PlatformPath(file_path), | |
454 test_data, data_size)); | |
455 | |
456 // Check that its length is the size of the data written. | |
457 operation()->GetMetadata(URLForPath(file_path)); | |
458 MessageLoop::current()->RunAllPending(); | |
459 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
460 EXPECT_FALSE(info().is_directory); | |
461 EXPECT_EQ(data_size, info().size); | |
462 | |
463 // Extend the file by truncating it. | |
464 int length = 17; | |
465 operation()->Truncate(URLForPath(file_path), length); | |
466 MessageLoop::current()->RunAllPending(); | |
467 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
468 | |
469 // Check that its length is now 17 and that it's all zeroes after the test | |
470 // data. | |
471 base::PlatformFileInfo info; | |
472 | |
473 EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info)); | |
474 EXPECT_EQ(length, info.size); | |
475 char data[100]; | |
476 EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length)); | |
477 for (int i = 0; i < length; ++i) { | |
478 if (i < static_cast<int>(sizeof(test_data))) | |
479 EXPECT_EQ(test_data[i], data[i]); | |
480 else | |
481 EXPECT_EQ(0, data[i]); | |
482 } | |
483 | |
484 // Shorten the file by truncating it. | |
485 length = 3; | |
486 operation()->Truncate(URLForPath(file_path), length); | |
487 MessageLoop::current()->RunAllPending(); | |
488 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | |
489 | |
490 // Check that its length is now 3 and that it contains only bits of test data. | |
491 EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info)); | |
492 EXPECT_EQ(length, info.size); | |
493 EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length)); | |
494 for (int i = 0; i < length; ++i) | |
495 EXPECT_EQ(test_data[i], data[i]); | |
496 } | |
497 #endif | |
498 | |
499 } // namespace fileapi | |
OLD | NEW |