Chromium Code Reviews| 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 "webkit/browser/fileapi/copy_or_move_operation_delegate.h" | |
|
kinuko
2013/09/21 10:44:36
afaik having this header at the top of unittest fi
hidehiko
2013/09/23 08:18:10
Ok, Done.
# But about half of unittest files in th
kinuko
2013/09/23 22:10:20
Yup, there was an era when we were told to do so,
| |
| 6 | |
| 5 #include <map> | 7 #include <map> |
| 6 #include <queue> | 8 #include <queue> |
| 7 | 9 |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/file_util.h" | |
| 10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 13 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webkit/browser/blob/file_stream_reader.h" | |
| 15 #include "webkit/browser/fileapi/async_file_test_helper.h" | 19 #include "webkit/browser/fileapi/async_file_test_helper.h" |
| 16 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 20 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 21 #include "webkit/browser/fileapi/file_stream_writer.h" | |
| 17 #include "webkit/browser/fileapi/file_system_backend.h" | 22 #include "webkit/browser/fileapi/file_system_backend.h" |
| 18 #include "webkit/browser/fileapi/file_system_context.h" | 23 #include "webkit/browser/fileapi/file_system_context.h" |
| 19 #include "webkit/browser/fileapi/file_system_operation.h" | 24 #include "webkit/browser/fileapi/file_system_operation.h" |
| 20 #include "webkit/browser/fileapi/file_system_url.h" | 25 #include "webkit/browser/fileapi/file_system_url.h" |
| 21 #include "webkit/browser/fileapi/mock_file_system_context.h" | 26 #include "webkit/browser/fileapi/mock_file_system_context.h" |
| 22 #include "webkit/browser/fileapi/test_file_set.h" | 27 #include "webkit/browser/fileapi/test_file_set.h" |
| 23 #include "webkit/browser/fileapi/test_file_system_backend.h" | 28 #include "webkit/browser/fileapi/test_file_system_backend.h" |
| 24 #include "webkit/browser/quota/mock_quota_manager.h" | 29 #include "webkit/browser/quota/mock_quota_manager.h" |
| 25 #include "webkit/browser/quota/quota_manager.h" | 30 #include "webkit/browser/quota/quota_manager.h" |
| 26 #include "webkit/common/fileapi/file_system_util.h" | 31 #include "webkit/common/fileapi/file_system_util.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 const FileSystemURL& dest_url, | 112 const FileSystemURL& dest_url, |
| 108 int64 size) { | 113 int64 size) { |
| 109 ProgressRecord record; | 114 ProgressRecord record; |
| 110 record.type = type; | 115 record.type = type; |
| 111 record.source_url = source_url; | 116 record.source_url = source_url; |
| 112 record.dest_url = dest_url; | 117 record.dest_url = dest_url; |
| 113 record.size = size; | 118 record.size = size; |
| 114 records->push_back(record); | 119 records->push_back(record); |
| 115 } | 120 } |
| 116 | 121 |
| 122 void RecordFileProgressCallback(std::vector<int64>* records, | |
| 123 int64 progress) { | |
| 124 records->push_back(progress); | |
| 125 } | |
| 126 | |
| 127 void AssignAndQuit(base::RunLoop* run_loop, | |
| 128 base::PlatformFileError* result_out, | |
| 129 base::PlatformFileError result) { | |
| 130 *result_out = result; | |
| 131 run_loop->Quit(); | |
| 132 } | |
| 133 | |
| 134 class ScopedThreadStopper { | |
| 135 public: | |
| 136 ScopedThreadStopper(base::Thread* thread) : thread_(thread) { | |
| 137 DCHECK(thread); | |
| 138 } | |
| 139 | |
| 140 ~ScopedThreadStopper() { | |
| 141 // Give another chance for deleted streams to perform Close. | |
| 142 base::RunLoop().RunUntilIdle(); | |
| 143 thread_->Stop(); | |
| 144 base::RunLoop().RunUntilIdle(); | |
|
kinuko
2013/09/21 10:44:36
Should we post one more task to the thread/runner
hidehiko
2013/09/23 08:18:10
Done.
| |
| 145 } | |
| 146 | |
| 147 private: | |
| 148 base::Thread* thread_; | |
| 149 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStopper); | |
| 150 }; | |
| 151 | |
| 117 } // namespace | 152 } // namespace |
| 118 | 153 |
| 119 class CopyOrMoveOperationTestHelper { | 154 class CopyOrMoveOperationTestHelper { |
| 120 public: | 155 public: |
| 121 CopyOrMoveOperationTestHelper( | 156 CopyOrMoveOperationTestHelper( |
| 122 const GURL& origin, | 157 const GURL& origin, |
| 123 FileSystemType src_type, | 158 FileSystemType src_type, |
| 124 FileSystemType dest_type) | 159 FileSystemType dest_type) |
| 125 : origin_(origin), | 160 : origin_(origin), |
| 126 src_type_(src_type), | 161 src_type_(src_type), |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 655 EXPECT_EQ(FileSystemOperation::PROGRESS, records[j].type); | 690 EXPECT_EQ(FileSystemOperation::PROGRESS, records[j].type); |
| 656 EXPECT_FALSE(records[j].dest_url.is_valid()); | 691 EXPECT_FALSE(records[j].dest_url.is_valid()); |
| 657 EXPECT_GE(records[j].size, current_size); | 692 EXPECT_GE(records[j].size, current_size); |
| 658 current_size = records[j].size; | 693 current_size = records[j].size; |
| 659 } | 694 } |
| 660 } | 695 } |
| 661 } | 696 } |
| 662 } | 697 } |
| 663 } | 698 } |
| 664 | 699 |
| 700 | |
| 701 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { | |
| 702 base::ScopedTempDir temp_dir; | |
| 703 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 704 base::FilePath source_path = temp_dir.path().AppendASCII("source"); | |
| 705 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
| 706 file_util::WriteFile(source_path, kTestData, | |
| 707 arraysize(kTestData) - 1); // Exclude trailing '\0'. | |
| 708 | |
| 709 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); | |
| 710 // LocalFileWriter requires the file is exists. So create an empty file here. | |
|
kinuko
2013/09/21 10:44:36
nit: is exists -> exists
hidehiko
2013/09/23 08:18:10
Done.
| |
| 711 file_util::WriteFile(dest_path, "", 0); | |
| 712 | |
| 713 base::MessageLoop message_loop; | |
| 714 base::Thread file_thread("file_thread"); | |
| 715 ASSERT_TRUE(file_thread.Start()); | |
| 716 ScopedThreadStopper thread_stopper(&file_thread); | |
| 717 | |
| 718 scoped_refptr<base::MessageLoopProxy> task_runner = | |
| 719 file_thread.message_loop_proxy(); | |
| 720 | |
| 721 scoped_ptr<webkit_blob::FileStreamReader> reader( | |
| 722 webkit_blob::FileStreamReader::CreateForLocalFile( | |
| 723 task_runner.get(), source_path, 0, base::Time())); | |
| 724 | |
| 725 scoped_ptr<FileStreamWriter> writer( | |
| 726 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); | |
| 727 | |
| 728 std::vector<int64> progress; | |
| 729 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | |
| 730 reader.Pass(), writer.Pass(), | |
| 731 10, // buffer size | |
| 732 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | |
| 733 base::TimeDelta()); // For testing, we need all the progress. | |
| 734 | |
| 735 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 736 base::RunLoop run_loop; | |
| 737 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); | |
| 738 run_loop.Run(); | |
| 739 | |
| 740 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 741 ASSERT_EQ(5U, progress.size()); | |
| 742 EXPECT_EQ(0, progress[0]); | |
| 743 EXPECT_EQ(10, progress[1]); | |
| 744 EXPECT_EQ(20, progress[2]); | |
| 745 EXPECT_EQ(30, progress[3]); | |
| 746 EXPECT_EQ(36, progress[4]); | |
| 747 | |
| 748 std::string content; | |
| 749 ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); | |
| 750 EXPECT_EQ(kTestData, content); | |
| 751 } | |
| 752 | |
| 665 } // namespace fileapi | 753 } // namespace fileapi |
| OLD | NEW |