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 <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/browser/blob/file_stream_reader.h" |
15 #include "webkit/browser/fileapi/async_file_test_helper.h" | 17 #include "webkit/browser/fileapi/async_file_test_helper.h" |
16 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | 18 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
| 19 #include "webkit/browser/fileapi/copy_or_move_operation_delegate.h" |
| 20 #include "webkit/browser/fileapi/file_stream_writer.h" |
17 #include "webkit/browser/fileapi/file_system_backend.h" | 21 #include "webkit/browser/fileapi/file_system_backend.h" |
18 #include "webkit/browser/fileapi/file_system_context.h" | 22 #include "webkit/browser/fileapi/file_system_context.h" |
19 #include "webkit/browser/fileapi/file_system_operation.h" | 23 #include "webkit/browser/fileapi/file_system_operation.h" |
20 #include "webkit/browser/fileapi/file_system_url.h" | 24 #include "webkit/browser/fileapi/file_system_url.h" |
21 #include "webkit/browser/fileapi/mock_file_system_context.h" | 25 #include "webkit/browser/fileapi/mock_file_system_context.h" |
22 #include "webkit/browser/fileapi/test_file_set.h" | 26 #include "webkit/browser/fileapi/test_file_set.h" |
23 #include "webkit/browser/fileapi/test_file_system_backend.h" | 27 #include "webkit/browser/fileapi/test_file_system_backend.h" |
24 #include "webkit/browser/quota/mock_quota_manager.h" | 28 #include "webkit/browser/quota/mock_quota_manager.h" |
25 #include "webkit/browser/quota/quota_manager.h" | 29 #include "webkit/browser/quota/quota_manager.h" |
26 #include "webkit/common/fileapi/file_system_util.h" | 30 #include "webkit/common/fileapi/file_system_util.h" |
(...skipping 80 matching lines...) Loading... |
107 const FileSystemURL& dest_url, | 111 const FileSystemURL& dest_url, |
108 int64 size) { | 112 int64 size) { |
109 ProgressRecord record; | 113 ProgressRecord record; |
110 record.type = type; | 114 record.type = type; |
111 record.source_url = source_url; | 115 record.source_url = source_url; |
112 record.dest_url = dest_url; | 116 record.dest_url = dest_url; |
113 record.size = size; | 117 record.size = size; |
114 records->push_back(record); | 118 records->push_back(record); |
115 } | 119 } |
116 | 120 |
| 121 void RecordFileProgressCallback(std::vector<int64>* records, |
| 122 int64 progress) { |
| 123 records->push_back(progress); |
| 124 } |
| 125 |
| 126 void AssignAndQuit(base::RunLoop* run_loop, |
| 127 base::PlatformFileError* result_out, |
| 128 base::PlatformFileError result) { |
| 129 *result_out = result; |
| 130 run_loop->Quit(); |
| 131 } |
| 132 |
| 133 class ScopedThreadStopper { |
| 134 public: |
| 135 ScopedThreadStopper(base::Thread* thread) : thread_(thread) { |
| 136 } |
| 137 |
| 138 ~ScopedThreadStopper() { |
| 139 if (thread_) { |
| 140 // Give another chance for deleted streams to perform Close. |
| 141 base::RunLoop run_loop; |
| 142 thread_->message_loop_proxy()->PostTaskAndReply( |
| 143 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()); |
| 144 run_loop.Run(); |
| 145 thread_->Stop(); |
| 146 } |
| 147 } |
| 148 |
| 149 bool is_valid() const { return thread_; } |
| 150 |
| 151 private: |
| 152 base::Thread* thread_; |
| 153 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStopper); |
| 154 }; |
| 155 |
117 } // namespace | 156 } // namespace |
118 | 157 |
119 class CopyOrMoveOperationTestHelper { | 158 class CopyOrMoveOperationTestHelper { |
120 public: | 159 public: |
121 CopyOrMoveOperationTestHelper( | 160 CopyOrMoveOperationTestHelper( |
122 const GURL& origin, | 161 const GURL& origin, |
123 FileSystemType src_type, | 162 FileSystemType src_type, |
124 FileSystemType dest_type) | 163 FileSystemType dest_type) |
125 : origin_(origin), | 164 : origin_(origin), |
126 src_type_(src_type), | 165 src_type_(src_type), |
127 dest_type_(dest_type) {} | 166 dest_type_(dest_type), |
| 167 message_loop_(base::MessageLoop::TYPE_IO) {} |
128 | 168 |
129 ~CopyOrMoveOperationTestHelper() { | 169 ~CopyOrMoveOperationTestHelper() { |
130 file_system_context_ = NULL; | 170 file_system_context_ = NULL; |
131 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 171 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
132 quota_manager_ = NULL; | 172 quota_manager_ = NULL; |
133 quota_manager_proxy_ = NULL; | 173 quota_manager_proxy_ = NULL; |
134 base::RunLoop().RunUntilIdle(); | 174 base::RunLoop().RunUntilIdle(); |
135 } | 175 } |
136 | 176 |
137 void SetUp() { | 177 void SetUp() { |
(...skipping 517 matching lines...) Loading... |
655 EXPECT_EQ(FileSystemOperation::PROGRESS, records[j].type); | 695 EXPECT_EQ(FileSystemOperation::PROGRESS, records[j].type); |
656 EXPECT_FALSE(records[j].dest_url.is_valid()); | 696 EXPECT_FALSE(records[j].dest_url.is_valid()); |
657 EXPECT_GE(records[j].size, current_size); | 697 EXPECT_GE(records[j].size, current_size); |
658 current_size = records[j].size; | 698 current_size = records[j].size; |
659 } | 699 } |
660 } | 700 } |
661 } | 701 } |
662 } | 702 } |
663 } | 703 } |
664 | 704 |
| 705 |
| 706 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { |
| 707 base::ScopedTempDir temp_dir; |
| 708 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 709 base::FilePath source_path = temp_dir.path().AppendASCII("source"); |
| 710 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 711 file_util::WriteFile(source_path, kTestData, |
| 712 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
| 713 |
| 714 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); |
| 715 // LocalFileWriter requires the file exists. So create an empty file here. |
| 716 file_util::WriteFile(dest_path, "", 0); |
| 717 |
| 718 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 719 base::Thread file_thread("file_thread"); |
| 720 ASSERT_TRUE(file_thread.Start()); |
| 721 ScopedThreadStopper thread_stopper(&file_thread); |
| 722 ASSERT_TRUE(thread_stopper.is_valid()); |
| 723 |
| 724 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 725 file_thread.message_loop_proxy(); |
| 726 |
| 727 scoped_ptr<webkit_blob::FileStreamReader> reader( |
| 728 webkit_blob::FileStreamReader::CreateForLocalFile( |
| 729 task_runner.get(), source_path, 0, base::Time())); |
| 730 |
| 731 scoped_ptr<FileStreamWriter> writer( |
| 732 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); |
| 733 |
| 734 std::vector<int64> progress; |
| 735 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
| 736 reader.Pass(), writer.Pass(), |
| 737 10, // buffer size |
| 738 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
| 739 base::TimeDelta()); // For testing, we need all the progress. |
| 740 |
| 741 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; |
| 742 base::RunLoop run_loop; |
| 743 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); |
| 744 run_loop.Run(); |
| 745 |
| 746 EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
| 747 ASSERT_EQ(5U, progress.size()); |
| 748 EXPECT_EQ(0, progress[0]); |
| 749 EXPECT_EQ(10, progress[1]); |
| 750 EXPECT_EQ(20, progress[2]); |
| 751 EXPECT_EQ(30, progress[3]); |
| 752 EXPECT_EQ(36, progress[4]); |
| 753 |
| 754 std::string content; |
| 755 ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); |
| 756 EXPECT_EQ(kTestData, content); |
| 757 } |
| 758 |
665 } // namespace fileapi | 759 } // namespace fileapi |
OLD | NEW |