Index: webkit/browser/fileapi/copy_or_move_operation_delegate_unittest.cc |
diff --git a/webkit/browser/fileapi/copy_or_move_operation_delegate_unittest.cc b/webkit/browser/fileapi/copy_or_move_operation_delegate_unittest.cc |
index 322341dc40ff174fd5a72729c2eae834225072a1..288a7ee11f9de58397ca2d1ae4e13e5860c01637 100644 |
--- a/webkit/browser/fileapi/copy_or_move_operation_delegate_unittest.cc |
+++ b/webkit/browser/fileapi/copy_or_move_operation_delegate_unittest.cc |
@@ -2,18 +2,23 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#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,
|
+ |
#include <map> |
#include <queue> |
#include "base/basictypes.h" |
#include "base/bind.h" |
+#include "base/file_util.h" |
#include "base/files/scoped_temp_dir.h" |
#include "base/message_loop/message_loop.h" |
#include "base/run_loop.h" |
#include "base/stl_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "webkit/browser/blob/file_stream_reader.h" |
#include "webkit/browser/fileapi/async_file_test_helper.h" |
#include "webkit/browser/fileapi/copy_or_move_file_validator.h" |
+#include "webkit/browser/fileapi/file_stream_writer.h" |
#include "webkit/browser/fileapi/file_system_backend.h" |
#include "webkit/browser/fileapi/file_system_context.h" |
#include "webkit/browser/fileapi/file_system_operation.h" |
@@ -114,6 +119,36 @@ void RecordProgressCallback(std::vector<ProgressRecord>* records, |
records->push_back(record); |
} |
+void RecordFileProgressCallback(std::vector<int64>* records, |
+ int64 progress) { |
+ records->push_back(progress); |
+} |
+ |
+void AssignAndQuit(base::RunLoop* run_loop, |
+ base::PlatformFileError* result_out, |
+ base::PlatformFileError result) { |
+ *result_out = result; |
+ run_loop->Quit(); |
+} |
+ |
+class ScopedThreadStopper { |
+ public: |
+ ScopedThreadStopper(base::Thread* thread) : thread_(thread) { |
+ DCHECK(thread); |
+ } |
+ |
+ ~ScopedThreadStopper() { |
+ // Give another chance for deleted streams to perform Close. |
+ base::RunLoop().RunUntilIdle(); |
+ thread_->Stop(); |
+ 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.
|
+ } |
+ |
+ private: |
+ base::Thread* thread_; |
+ DISALLOW_COPY_AND_ASSIGN(ScopedThreadStopper); |
+}; |
+ |
} // namespace |
class CopyOrMoveOperationTestHelper { |
@@ -662,4 +697,57 @@ TEST(LocalFileSystemCopyOrMoveOperationTest, ProgressCallback) { |
} |
} |
+ |
+TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { |
+ base::ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ base::FilePath source_path = temp_dir.path().AppendASCII("source"); |
+ const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
+ file_util::WriteFile(source_path, kTestData, |
+ arraysize(kTestData) - 1); // Exclude trailing '\0'. |
+ |
+ base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); |
+ // 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.
|
+ file_util::WriteFile(dest_path, "", 0); |
+ |
+ base::MessageLoop message_loop; |
+ base::Thread file_thread("file_thread"); |
+ ASSERT_TRUE(file_thread.Start()); |
+ ScopedThreadStopper thread_stopper(&file_thread); |
+ |
+ scoped_refptr<base::MessageLoopProxy> task_runner = |
+ file_thread.message_loop_proxy(); |
+ |
+ scoped_ptr<webkit_blob::FileStreamReader> reader( |
+ webkit_blob::FileStreamReader::CreateForLocalFile( |
+ task_runner.get(), source_path, 0, base::Time())); |
+ |
+ scoped_ptr<FileStreamWriter> writer( |
+ FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); |
+ |
+ std::vector<int64> progress; |
+ CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
+ reader.Pass(), writer.Pass(), |
+ 10, // buffer size |
+ base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
+ base::TimeDelta()); // For testing, we need all the progress. |
+ |
+ base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; |
+ base::RunLoop run_loop; |
+ helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); |
+ run_loop.Run(); |
+ |
+ EXPECT_EQ(base::PLATFORM_FILE_OK, error); |
+ ASSERT_EQ(5U, progress.size()); |
+ EXPECT_EQ(0, progress[0]); |
+ EXPECT_EQ(10, progress[1]); |
+ EXPECT_EQ(20, progress[2]); |
+ EXPECT_EQ(30, progress[3]); |
+ EXPECT_EQ(36, progress[4]); |
+ |
+ std::string content; |
+ ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); |
+ EXPECT_EQ(kTestData, content); |
+} |
+ |
} // namespace fileapi |