| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/file_util.h" |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 } | 711 } |
| 712 } | 712 } |
| 713 } | 713 } |
| 714 } | 714 } |
| 715 } | 715 } |
| 716 | 716 |
| 717 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { | 717 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper) { |
| 718 base::ScopedTempDir temp_dir; | 718 base::ScopedTempDir temp_dir; |
| 719 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 719 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 720 base::FilePath source_path = temp_dir.path().AppendASCII("source"); | 720 base::FilePath source_path = temp_dir.path().AppendASCII("source"); |
| 721 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); |
| 721 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | 722 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 722 base::WriteFile(source_path, kTestData, | 723 base::WriteFile(source_path, kTestData, |
| 723 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 724 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
| 724 | 725 |
| 725 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); | |
| 726 // LocalFileWriter requires the file exists. So create an empty file here. | |
| 727 base::WriteFile(dest_path, "", 0); | |
| 728 | |
| 729 base::MessageLoopForIO message_loop; | 726 base::MessageLoopForIO message_loop; |
| 730 base::Thread file_thread("file_thread"); | 727 base::Thread file_thread("file_thread"); |
| 731 ASSERT_TRUE(file_thread.Start()); | 728 ASSERT_TRUE(file_thread.Start()); |
| 732 ScopedThreadStopper thread_stopper(&file_thread); | 729 ScopedThreadStopper thread_stopper(&file_thread); |
| 733 ASSERT_TRUE(thread_stopper.is_valid()); | 730 ASSERT_TRUE(thread_stopper.is_valid()); |
| 734 | 731 |
| 735 scoped_refptr<base::MessageLoopProxy> task_runner = | 732 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 736 file_thread.message_loop_proxy(); | 733 file_thread.message_loop_proxy(); |
| 737 | 734 |
| 738 scoped_ptr<webkit_blob::FileStreamReader> reader( | 735 scoped_ptr<webkit_blob::FileStreamReader> reader( |
| 739 webkit_blob::FileStreamReader::CreateForLocalFile( | 736 webkit_blob::FileStreamReader::CreateForLocalFile( |
| 740 task_runner.get(), source_path, 0, base::Time())); | 737 task_runner.get(), source_path, 0, base::Time())); |
| 741 | 738 |
| 742 scoped_ptr<FileStreamWriter> writer( | 739 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
| 743 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); | 740 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
| 744 | 741 |
| 745 std::vector<int64> progress; | 742 std::vector<int64> progress; |
| 746 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 743 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
| 747 reader.Pass(), writer.Pass(), | 744 reader.Pass(), writer.Pass(), |
| 748 false, // don't need flush | 745 false, // don't need flush |
| 749 10, // buffer size | 746 10, // buffer size |
| 750 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | 747 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
| 751 base::TimeDelta()); // For testing, we need all the progress. | 748 base::TimeDelta()); // For testing, we need all the progress. |
| 752 | 749 |
| 753 base::File::Error error = base::File::FILE_ERROR_FAILED; | 750 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 769 } | 766 } |
| 770 | 767 |
| 771 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelperWithFlush) { | 768 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelperWithFlush) { |
| 772 // Testing the same configuration as StreamCopyHelper, but with |need_flush| | 769 // Testing the same configuration as StreamCopyHelper, but with |need_flush| |
| 773 // parameter set to true. Since it is hard to test that the flush is indeed | 770 // parameter set to true. Since it is hard to test that the flush is indeed |
| 774 // taking place, this test just only verifies that the file is correctly | 771 // taking place, this test just only verifies that the file is correctly |
| 775 // written with or without the flag. | 772 // written with or without the flag. |
| 776 base::ScopedTempDir temp_dir; | 773 base::ScopedTempDir temp_dir; |
| 777 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 774 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 778 base::FilePath source_path = temp_dir.path().AppendASCII("source"); | 775 base::FilePath source_path = temp_dir.path().AppendASCII("source"); |
| 776 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); |
| 779 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | 777 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 780 base::WriteFile(source_path, kTestData, | 778 base::WriteFile(source_path, kTestData, |
| 781 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 779 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
| 782 | 780 |
| 783 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); | |
| 784 // LocalFileWriter requires the file exists. So create an empty file here. | |
| 785 base::WriteFile(dest_path, "", 0); | |
| 786 | 781 |
| 787 base::MessageLoopForIO message_loop; | 782 base::MessageLoopForIO message_loop; |
| 788 base::Thread file_thread("file_thread"); | 783 base::Thread file_thread("file_thread"); |
| 789 ASSERT_TRUE(file_thread.Start()); | 784 ASSERT_TRUE(file_thread.Start()); |
| 790 ScopedThreadStopper thread_stopper(&file_thread); | 785 ScopedThreadStopper thread_stopper(&file_thread); |
| 791 ASSERT_TRUE(thread_stopper.is_valid()); | 786 ASSERT_TRUE(thread_stopper.is_valid()); |
| 792 | 787 |
| 793 scoped_refptr<base::MessageLoopProxy> task_runner = | 788 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 794 file_thread.message_loop_proxy(); | 789 file_thread.message_loop_proxy(); |
| 795 | 790 |
| 796 scoped_ptr<webkit_blob::FileStreamReader> reader( | 791 scoped_ptr<webkit_blob::FileStreamReader> reader( |
| 797 webkit_blob::FileStreamReader::CreateForLocalFile( | 792 webkit_blob::FileStreamReader::CreateForLocalFile( |
| 798 task_runner.get(), source_path, 0, base::Time())); | 793 task_runner.get(), source_path, 0, base::Time())); |
| 799 | 794 |
| 800 scoped_ptr<FileStreamWriter> writer( | 795 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
| 801 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); | 796 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
| 802 | 797 |
| 803 std::vector<int64> progress; | 798 std::vector<int64> progress; |
| 804 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 799 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
| 805 reader.Pass(), writer.Pass(), | 800 reader.Pass(), writer.Pass(), |
| 806 true, // need flush | 801 true, // need flush |
| 807 10, // buffer size | 802 10, // buffer size |
| 808 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | 803 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
| 809 base::TimeDelta()); // For testing, we need all the progress. | 804 base::TimeDelta()); // For testing, we need all the progress. |
| 810 | 805 |
| 811 base::File::Error error = base::File::FILE_ERROR_FAILED; | 806 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 823 | 818 |
| 824 std::string content; | 819 std::string content; |
| 825 ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); | 820 ASSERT_TRUE(base::ReadFileToString(dest_path, &content)); |
| 826 EXPECT_EQ(kTestData, content); | 821 EXPECT_EQ(kTestData, content); |
| 827 } | 822 } |
| 828 | 823 |
| 829 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper_Cancel) { | 824 TEST(LocalFileSystemCopyOrMoveOperationTest, StreamCopyHelper_Cancel) { |
| 830 base::ScopedTempDir temp_dir; | 825 base::ScopedTempDir temp_dir; |
| 831 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 826 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 832 base::FilePath source_path = temp_dir.path().AppendASCII("source"); | 827 base::FilePath source_path = temp_dir.path().AppendASCII("source"); |
| 828 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); |
| 833 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; | 829 const char kTestData[] = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 834 base::WriteFile(source_path, kTestData, | 830 base::WriteFile(source_path, kTestData, |
| 835 arraysize(kTestData) - 1); // Exclude trailing '\0'. | 831 arraysize(kTestData) - 1); // Exclude trailing '\0'. |
| 836 | 832 |
| 837 base::FilePath dest_path = temp_dir.path().AppendASCII("dest"); | |
| 838 // LocalFileWriter requires the file exists. So create an empty file here. | |
| 839 base::WriteFile(dest_path, "", 0); | |
| 840 | |
| 841 base::MessageLoopForIO message_loop; | 833 base::MessageLoopForIO message_loop; |
| 842 base::Thread file_thread("file_thread"); | 834 base::Thread file_thread("file_thread"); |
| 843 ASSERT_TRUE(file_thread.Start()); | 835 ASSERT_TRUE(file_thread.Start()); |
| 844 ScopedThreadStopper thread_stopper(&file_thread); | 836 ScopedThreadStopper thread_stopper(&file_thread); |
| 845 ASSERT_TRUE(thread_stopper.is_valid()); | 837 ASSERT_TRUE(thread_stopper.is_valid()); |
| 846 | 838 |
| 847 scoped_refptr<base::MessageLoopProxy> task_runner = | 839 scoped_refptr<base::MessageLoopProxy> task_runner = |
| 848 file_thread.message_loop_proxy(); | 840 file_thread.message_loop_proxy(); |
| 849 | 841 |
| 850 scoped_ptr<webkit_blob::FileStreamReader> reader( | 842 scoped_ptr<webkit_blob::FileStreamReader> reader( |
| 851 webkit_blob::FileStreamReader::CreateForLocalFile( | 843 webkit_blob::FileStreamReader::CreateForLocalFile( |
| 852 task_runner.get(), source_path, 0, base::Time())); | 844 task_runner.get(), source_path, 0, base::Time())); |
| 853 | 845 |
| 854 scoped_ptr<FileStreamWriter> writer( | 846 scoped_ptr<FileStreamWriter> writer(FileStreamWriter::CreateForLocalFile( |
| 855 FileStreamWriter::CreateForLocalFile(task_runner.get(), dest_path, 0)); | 847 task_runner.get(), dest_path, 0, FileStreamWriter::CREATE_NEW_FILE)); |
| 856 | 848 |
| 857 std::vector<int64> progress; | 849 std::vector<int64> progress; |
| 858 CopyOrMoveOperationDelegate::StreamCopyHelper helper( | 850 CopyOrMoveOperationDelegate::StreamCopyHelper helper( |
| 859 reader.Pass(), writer.Pass(), | 851 reader.Pass(), writer.Pass(), |
| 860 false, // need_flush | 852 false, // need_flush |
| 861 10, // buffer size | 853 10, // buffer size |
| 862 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), | 854 base::Bind(&RecordFileProgressCallback, base::Unretained(&progress)), |
| 863 base::TimeDelta()); // For testing, we need all the progress. | 855 base::TimeDelta()); // For testing, we need all the progress. |
| 864 | 856 |
| 865 // Call Cancel() later. | 857 // Call Cancel() later. |
| 866 base::MessageLoopProxy::current()->PostTask( | 858 base::MessageLoopProxy::current()->PostTask( |
| 867 FROM_HERE, | 859 FROM_HERE, |
| 868 base::Bind(&CopyOrMoveOperationDelegate::StreamCopyHelper::Cancel, | 860 base::Bind(&CopyOrMoveOperationDelegate::StreamCopyHelper::Cancel, |
| 869 base::Unretained(&helper))); | 861 base::Unretained(&helper))); |
| 870 | 862 |
| 871 base::File::Error error = base::File::FILE_ERROR_FAILED; | 863 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 872 base::RunLoop run_loop; | 864 base::RunLoop run_loop; |
| 873 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); | 865 helper.Run(base::Bind(&AssignAndQuit, &run_loop, &error)); |
| 874 run_loop.Run(); | 866 run_loop.Run(); |
| 875 | 867 |
| 876 EXPECT_EQ(base::File::FILE_ERROR_ABORT, error); | 868 EXPECT_EQ(base::File::FILE_ERROR_ABORT, error); |
| 877 } | 869 } |
| 878 | 870 |
| 879 } // namespace content | 871 } // namespace content |
| OLD | NEW |