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