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...) Expand 10 before | Expand all | Expand 10 after 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 DCHECK(thread); | |
kinuko
2013/09/23 22:10:20
nit: ASSERT is preferred over DCHECK in tests (so
hidehiko
2013/09/24 02:06:15
Done.
Note that ASSERT family doesn't work here, b
| |
137 } | |
138 | |
139 ~ScopedThreadStopper() { | |
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 private: | |
149 base::Thread* thread_; | |
150 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStopper); | |
151 }; | |
152 | |
117 } // namespace | 153 } // namespace |
118 | 154 |
119 class CopyOrMoveOperationTestHelper { | 155 class CopyOrMoveOperationTestHelper { |
120 public: | 156 public: |
121 CopyOrMoveOperationTestHelper( | 157 CopyOrMoveOperationTestHelper( |
122 const GURL& origin, | 158 const GURL& origin, |
123 FileSystemType src_type, | 159 FileSystemType src_type, |
124 FileSystemType dest_type) | 160 FileSystemType dest_type) |
125 : origin_(origin), | 161 : origin_(origin), |
126 src_type_(src_type), | 162 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); | 691 EXPECT_EQ(FileSystemOperation::PROGRESS, records[j].type); |
656 EXPECT_FALSE(records[j].dest_url.is_valid()); | 692 EXPECT_FALSE(records[j].dest_url.is_valid()); |
657 EXPECT_GE(records[j].size, current_size); | 693 EXPECT_GE(records[j].size, current_size); |
658 current_size = records[j].size; | 694 current_size = records[j].size; |
659 } | 695 } |
660 } | 696 } |
661 } | 697 } |
662 } | 698 } |
663 } | 699 } |
664 | 700 |
701 | |
702 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { | |
703 base::ScopedTempDir temp_dir; | |
704 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
705 base::FilePath source_path = temp_dir.path().AppendASCII("source"); | |
706 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
707 file_util::WriteFile(source_path, kTestData, | |
708 arraysize(kTestData) - 1); // Exclude trailing '\0'. | |
709 | |
710 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); | |
711 // LocalFileWriter requires the file exists. So create an empty file here. | |
712 file_util::WriteFile(dest_path, "", 0); | |
713 | |
714 base::MessageLoop message_loop; | |
715 base::Thread file_thread("file_thread"); | |
716 ASSERT_TRUE(file_thread.Start()); | |
717 ScopedThreadStopper thread_stopper(&file_thread); | |
718 | |
719 scoped_refptr<base::MessageLoopProxy> task_runner = | |
720 file_thread.message_loop_proxy(); | |
721 | |
722 scoped_ptr<webkit_blob::FileStreamReader> reader( | |
723 webkit_blob::FileStreamReader::CreateForLocalFile( | |
724 task_runner.get(), source_path, 0, base::Time())); | |
725 | |
726 scoped_ptr<FileStreamWriter> writer( | |
727 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); | |
728 | |
729 std::vector<int64> progress; | |
730 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | |
731 reader.Pass(), writer.Pass(), | |
732 10, // buffer size | |
733 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | |
734 base::TimeDelta()); // For testing, we need all the progress. | |
735 | |
736 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
737 base::RunLoop run_loop; | |
738 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); | |
739 run_loop.Run(); | |
740 | |
741 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
742 ASSERT_EQ(5U, progress.size()); | |
743 EXPECT_EQ(0, progress[0]); | |
744 EXPECT_EQ(10, progress[1]); | |
745 EXPECT_EQ(20, progress[2]); | |
746 EXPECT_EQ(30, progress[3]); | |
747 EXPECT_EQ(36, progress[4]); | |
748 | |
749 std::string content; | |
750 ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); | |
751 EXPECT_EQ(kTestData, content); | |
752 } | |
753 | |
665 } // namespace fileapi | 754 } // namespace fileapi |
OLD | NEW |