OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/fileapi/file_system_operation.h" | 5 #include "webkit/fileapi/file_system_operation.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 virtual void DidReadDirectory( | 45 virtual void DidReadDirectory( |
46 const std::vector<base::file_util_proxy::Entry>& entries, | 46 const std::vector<base::file_util_proxy::Entry>& entries, |
47 bool /* has_more */) { | 47 bool /* has_more */) { |
48 entries_ = entries; | 48 entries_ = entries; |
49 } | 49 } |
50 | 50 |
51 virtual void DidOpenFileSystem(const std::string&, const FilePath&) { | 51 virtual void DidOpenFileSystem(const std::string&, const FilePath&) { |
52 NOTREACHED(); | 52 NOTREACHED(); |
53 } | 53 } |
54 | 54 |
| 55 virtual void DidWrite(int64 bytes, bool complete) { |
| 56 NOTREACHED(); |
| 57 } |
| 58 |
55 // Helpers for testing. | 59 // Helpers for testing. |
56 int status() const { return status_; } | 60 int status() const { return status_; } |
57 int request_id() const { return request_id_; } | 61 int request_id() const { return request_id_; } |
58 const base::PlatformFileInfo& info() const { return info_; } | 62 const base::PlatformFileInfo& info() const { return info_; } |
59 const std::vector<base::file_util_proxy::Entry>& entries() const { | 63 const std::vector<base::file_util_proxy::Entry>& entries() const { |
60 return entries_; | 64 return entries_; |
61 } | 65 } |
62 | 66 |
63 private: | 67 private: |
64 int status_; | 68 int status_; |
(...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 ScopedTempDir empty_dir; | 550 ScopedTempDir empty_dir; |
547 ASSERT_TRUE(empty_dir.CreateUniqueTempDir()); | 551 ASSERT_TRUE(empty_dir.CreateUniqueTempDir()); |
548 EXPECT_TRUE(file_util::DirectoryExists(empty_dir.path())); | 552 EXPECT_TRUE(file_util::DirectoryExists(empty_dir.path())); |
549 | 553 |
550 operation()->Remove(empty_dir.path()); | 554 operation()->Remove(empty_dir.path()); |
551 MessageLoop::current()->RunAllPending(); | 555 MessageLoop::current()->RunAllPending(); |
552 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 556 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
553 EXPECT_FALSE(file_util::DirectoryExists(empty_dir.path())); | 557 EXPECT_FALSE(file_util::DirectoryExists(empty_dir.path())); |
554 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 558 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
555 } | 559 } |
| 560 |
| 561 TEST_F(FileSystemOperationTest, TestTruncate) { |
| 562 ScopedTempDir dir; |
| 563 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 564 FilePath file; |
| 565 file_util::CreateTemporaryFileInDir(dir.path(), &file); |
| 566 |
| 567 char test_data[] = "test data"; |
| 568 EXPECT_EQ(sizeof(test_data), |
| 569 file_util::WriteFile(file, test_data, sizeof(test_data))); |
| 570 |
| 571 // Check that its length is the size of the data written. |
| 572 operation()->GetMetadata(file); |
| 573 MessageLoop::current()->RunAllPending(); |
| 574 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 575 EXPECT_FALSE(mock_dispatcher_->info().is_directory); |
| 576 EXPECT_EQ(sizeof(test_data), mock_dispatcher_->info().size); |
| 577 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 578 |
| 579 // Extend the file by truncating it. |
| 580 int length = 17; |
| 581 operation()->Truncate(file, length); |
| 582 MessageLoop::current()->RunAllPending(); |
| 583 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 584 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 585 |
| 586 // Check that its length is now 17 and that it's all zeroes after the test |
| 587 // data. |
| 588 base::PlatformFileInfo info; |
| 589 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); |
| 590 EXPECT_EQ(length, info.size); |
| 591 char data[100]; |
| 592 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); |
| 593 for (int i = 0; i < length; ++i) { |
| 594 if (i < sizeof(test_data)) |
| 595 EXPECT_EQ(test_data[i], data[i]); |
| 596 else |
| 597 EXPECT_EQ(0, data[i]); |
| 598 } |
| 599 |
| 600 // Shorten the file by truncating it. |
| 601 length = 3; |
| 602 operation()->Truncate(file, length); |
| 603 MessageLoop::current()->RunAllPending(); |
| 604 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 605 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 606 |
| 607 // Check that its length is now 3 and that it contains only bits of test data. |
| 608 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); |
| 609 EXPECT_EQ(length, info.size); |
| 610 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); |
| 611 for (int i = 0; i < length; ++i) |
| 612 EXPECT_EQ(test_data[i], data[i]); |
| 613 } |
OLD | NEW |