OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/drive/drive_uploader.h" | 5 #include "chrome/browser/drive/drive_uploader.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
(...skipping 27 matching lines...) Expand all Loading... |
44 const char kTestDocumentTitle[] = "Hello world"; | 45 const char kTestDocumentTitle[] = "Hello world"; |
45 const char kTestInitiateUploadParentResourceId[] = "parent_resource_id"; | 46 const char kTestInitiateUploadParentResourceId[] = "parent_resource_id"; |
46 const char kTestInitiateUploadResourceId[] = "resource_id"; | 47 const char kTestInitiateUploadResourceId[] = "resource_id"; |
47 const char kTestMimeType[] = "text/plain"; | 48 const char kTestMimeType[] = "text/plain"; |
48 const char kTestUploadNewFileURL[] = "http://test/upload_location/new_file"; | 49 const char kTestUploadNewFileURL[] = "http://test/upload_location/new_file"; |
49 const char kTestUploadExistingFileURL[] = | 50 const char kTestUploadExistingFileURL[] = |
50 "http://test/upload_location/existing_file"; | 51 "http://test/upload_location/existing_file"; |
51 const int64 kUploadChunkSize = 1024 * 1024 * 1024; | 52 const int64 kUploadChunkSize = 1024 * 1024 * 1024; |
52 const char kTestETag[] = "test_etag"; | 53 const char kTestETag[] = "test_etag"; |
53 | 54 |
| 55 CancelCallback SendMultipartUploadResult( |
| 56 DriveApiErrorCode response_code, |
| 57 int64 content_length, |
| 58 const google_apis::FileResourceCallback& callback, |
| 59 const google_apis::ProgressCallback& progress_callback) { |
| 60 // Callback progress |
| 61 if (!progress_callback.is_null()) { |
| 62 // For the testing purpose, it always notifies the progress at the end of |
| 63 // whole file uploading. |
| 64 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 65 FROM_HERE, |
| 66 base::Bind(progress_callback, content_length, content_length)); |
| 67 } |
| 68 |
| 69 // MultipartUploadXXXFile is an asynchronous function, so don't callback |
| 70 // directly. |
| 71 scoped_ptr<FileResource> entry; |
| 72 entry.reset(new FileResource); |
| 73 entry->set_md5_checksum(kTestDummyMd5); |
| 74 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 75 FROM_HERE, base::Bind(callback, response_code, base::Passed(&entry))); |
| 76 return CancelCallback(); |
| 77 } |
| 78 |
54 // Mock DriveService that verifies if the uploaded content matches the preset | 79 // Mock DriveService that verifies if the uploaded content matches the preset |
55 // expectation. | 80 // expectation. |
56 class MockDriveServiceWithUploadExpectation : public DummyDriveService { | 81 class MockDriveServiceWithUploadExpectation : public DummyDriveService { |
57 public: | 82 public: |
58 // Sets up an expected upload content. InitiateUpload and ResumeUpload will | 83 // Sets up an expected upload content. InitiateUpload and ResumeUpload will |
59 // verify that the specified data is correctly uploaded. | 84 // verify that the specified data is correctly uploaded. |
60 MockDriveServiceWithUploadExpectation( | 85 MockDriveServiceWithUploadExpectation( |
61 const base::FilePath& expected_upload_file, | 86 const base::FilePath& expected_upload_file, |
62 int64 expected_content_length) | 87 int64 expected_content_length) |
63 : expected_upload_file_(expected_upload_file), | 88 : expected_upload_file_(expected_upload_file), |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 const base::FilePath& local_file_path, | 234 const base::FilePath& local_file_path, |
210 const UploadNewFileOptions& options, | 235 const UploadNewFileOptions& options, |
211 const google_apis::FileResourceCallback& callback, | 236 const google_apis::FileResourceCallback& callback, |
212 const google_apis::ProgressCallback& progress_callback) override { | 237 const google_apis::ProgressCallback& progress_callback) override { |
213 EXPECT_EQ(kTestMimeType, content_type); | 238 EXPECT_EQ(kTestMimeType, content_type); |
214 EXPECT_EQ(expected_content_length_, content_length); | 239 EXPECT_EQ(expected_content_length_, content_length); |
215 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); | 240 EXPECT_EQ(kTestInitiateUploadParentResourceId, parent_resource_id); |
216 EXPECT_EQ(kTestDocumentTitle, title); | 241 EXPECT_EQ(kTestDocumentTitle, title); |
217 EXPECT_EQ(expected_upload_file_, local_file_path); | 242 EXPECT_EQ(expected_upload_file_, local_file_path); |
218 | 243 |
| 244 received_bytes_ = content_length; |
| 245 multipart_upload_call_count_++; |
219 return SendMultipartUploadResult(HTTP_CREATED, content_length, callback, | 246 return SendMultipartUploadResult(HTTP_CREATED, content_length, callback, |
220 progress_callback); | 247 progress_callback); |
221 } | 248 } |
222 | 249 |
223 CancelCallback MultipartUploadExistingFile( | 250 CancelCallback MultipartUploadExistingFile( |
224 const std::string& content_type, | 251 const std::string& content_type, |
225 int64 content_length, | 252 int64 content_length, |
226 const std::string& resource_id, | 253 const std::string& resource_id, |
227 const base::FilePath& local_file_path, | 254 const base::FilePath& local_file_path, |
228 const UploadExistingFileOptions& options, | 255 const UploadExistingFileOptions& options, |
229 const google_apis::FileResourceCallback& callback, | 256 const google_apis::FileResourceCallback& callback, |
230 const google_apis::ProgressCallback& progress_callback) override { | 257 const google_apis::ProgressCallback& progress_callback) override { |
231 EXPECT_EQ(kTestMimeType, content_type); | 258 EXPECT_EQ(kTestMimeType, content_type); |
232 EXPECT_EQ(expected_content_length_, content_length); | 259 EXPECT_EQ(expected_content_length_, content_length); |
233 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); | 260 EXPECT_EQ(kTestInitiateUploadResourceId, resource_id); |
234 EXPECT_EQ(expected_upload_file_, local_file_path); | 261 EXPECT_EQ(expected_upload_file_, local_file_path); |
235 | 262 |
236 if (!options.etag.empty() && options.etag != kTestETag) { | 263 if (!options.etag.empty() && options.etag != kTestETag) { |
237 base::ThreadTaskRunnerHandle::Get()->PostTask( | 264 base::ThreadTaskRunnerHandle::Get()->PostTask( |
238 FROM_HERE, | 265 FROM_HERE, |
239 base::Bind(callback, HTTP_PRECONDITION, | 266 base::Bind(callback, HTTP_PRECONDITION, |
240 base::Passed(make_scoped_ptr<FileResource>(NULL)))); | 267 base::Passed(make_scoped_ptr<FileResource>(NULL)))); |
241 return CancelCallback(); | 268 return CancelCallback(); |
242 } | 269 } |
243 | 270 |
| 271 received_bytes_ = content_length; |
| 272 multipart_upload_call_count_++; |
244 return SendMultipartUploadResult(HTTP_SUCCESS, content_length, callback, | 273 return SendMultipartUploadResult(HTTP_SUCCESS, content_length, callback, |
245 progress_callback); | 274 progress_callback); |
246 } | 275 } |
247 | 276 |
248 CancelCallback SendMultipartUploadResult( | |
249 DriveApiErrorCode response_code, | |
250 int64 content_length, | |
251 const google_apis::FileResourceCallback& callback, | |
252 const google_apis::ProgressCallback& progress_callback) { | |
253 received_bytes_ = content_length; | |
254 multipart_upload_call_count_++; | |
255 | |
256 // Callback progress | |
257 if (!progress_callback.is_null()) { | |
258 // For the testing purpose, it always notifies the progress at the end of | |
259 // whole file uploading. | |
260 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
261 FROM_HERE, | |
262 base::Bind(progress_callback, content_length, content_length)); | |
263 } | |
264 | |
265 // MultipartUploadXXXFile is an asynchronous function, so don't callback | |
266 // directly. | |
267 scoped_ptr<FileResource> entry; | |
268 entry.reset(new FileResource); | |
269 entry->set_md5_checksum(kTestDummyMd5); | |
270 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
271 FROM_HERE, base::Bind(callback, response_code, base::Passed(&entry))); | |
272 return CancelCallback(); | |
273 } | |
274 | |
275 const base::FilePath expected_upload_file_; | 277 const base::FilePath expected_upload_file_; |
276 const int64 expected_content_length_; | 278 const int64 expected_content_length_; |
277 int64 received_bytes_; | 279 int64 received_bytes_; |
278 int64 resume_upload_call_count_; | 280 int64 resume_upload_call_count_; |
279 int64 multipart_upload_call_count_; | 281 int64 multipart_upload_call_count_; |
280 }; | 282 }; |
281 | 283 |
282 // Mock DriveService that returns a failure at InitiateUpload(). | 284 // Mock DriveService that returns a failure at InitiateUpload(). |
283 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { | 285 class MockDriveServiceNoConnectionAtInitiate : public DummyDriveService { |
284 // Returns error. | 286 // Returns error. |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 EXPECT_EQ(1024 * 1024, mock_service.received_bytes()); | 759 EXPECT_EQ(1024 * 1024, mock_service.received_bytes()); |
758 EXPECT_EQ(HTTP_SUCCESS, error); | 760 EXPECT_EQ(HTTP_SUCCESS, error); |
759 EXPECT_TRUE(upload_location.is_empty()); | 761 EXPECT_TRUE(upload_location.is_empty()); |
760 ASSERT_TRUE(entry); | 762 ASSERT_TRUE(entry); |
761 EXPECT_EQ(kTestDummyMd5, entry->md5_checksum()); | 763 EXPECT_EQ(kTestDummyMd5, entry->md5_checksum()); |
762 ASSERT_EQ(1U, upload_progress_values.size()); | 764 ASSERT_EQ(1U, upload_progress_values.size()); |
763 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), | 765 EXPECT_EQ(test_util::ProgressInfo(1024 * 1024, 1024 * 1024), |
764 upload_progress_values[0]); | 766 upload_progress_values[0]); |
765 } | 767 } |
766 | 768 |
| 769 class MockDriveServiceForBatchProcessing : public DummyDriveService { |
| 770 public: |
| 771 struct UploadFileInfo { |
| 772 enum { NEW_FILE, EXISTING_FILE } type; |
| 773 std::string content_type; |
| 774 uint64 content_length; |
| 775 std::string parent_resource_id; |
| 776 std::string resource_id; |
| 777 std::string title; |
| 778 base::FilePath local_file_path; |
| 779 google_apis::FileResourceCallback callback; |
| 780 google_apis::ProgressCallback progress_callback; |
| 781 }; |
| 782 |
| 783 class BatchRequestConfigurator : public BatchRequestConfiguratorInterface { |
| 784 public: |
| 785 explicit BatchRequestConfigurator( |
| 786 MockDriveServiceForBatchProcessing* service) |
| 787 : service(service) {} |
| 788 |
| 789 CancelCallback MultipartUploadNewFile( |
| 790 const std::string& content_type, |
| 791 int64 content_length, |
| 792 const std::string& parent_resource_id, |
| 793 const std::string& title, |
| 794 const base::FilePath& local_file_path, |
| 795 const UploadNewFileOptions& options, |
| 796 const google_apis::FileResourceCallback& callback, |
| 797 const google_apis::ProgressCallback& progress_callback) override { |
| 798 UploadFileInfo info; |
| 799 info.type = UploadFileInfo::NEW_FILE; |
| 800 info.content_type = content_type; |
| 801 info.content_length = content_length; |
| 802 info.parent_resource_id = parent_resource_id; |
| 803 info.title = title; |
| 804 info.local_file_path = local_file_path; |
| 805 info.callback = callback; |
| 806 info.progress_callback = progress_callback; |
| 807 service->files.push_back(info); |
| 808 return CancelCallback(); |
| 809 } |
| 810 |
| 811 CancelCallback MultipartUploadExistingFile( |
| 812 const std::string& content_type, |
| 813 int64 content_length, |
| 814 const std::string& resource_id, |
| 815 const base::FilePath& local_file_path, |
| 816 const UploadExistingFileOptions& options, |
| 817 const google_apis::FileResourceCallback& callback, |
| 818 const google_apis::ProgressCallback& progress_callback) override { |
| 819 UploadFileInfo info; |
| 820 info.type = UploadFileInfo::EXISTING_FILE; |
| 821 info.content_type = content_type; |
| 822 info.content_length = content_length; |
| 823 info.resource_id = resource_id; |
| 824 info.local_file_path = local_file_path; |
| 825 info.callback = callback; |
| 826 info.progress_callback = progress_callback; |
| 827 service->files.push_back(info); |
| 828 return CancelCallback(); |
| 829 } |
| 830 |
| 831 void Commit() override { |
| 832 ASSERT_FALSE(service->committed); |
| 833 service->committed = true; |
| 834 for (const auto& file : service->files) { |
| 835 SendMultipartUploadResult(HTTP_SUCCESS, file.content_length, |
| 836 file.callback, file.progress_callback); |
| 837 } |
| 838 } |
| 839 |
| 840 private: |
| 841 MockDriveServiceForBatchProcessing* service; |
| 842 }; |
| 843 |
| 844 public: |
| 845 scoped_ptr<BatchRequestConfiguratorInterface> StartBatchRequest() override { |
| 846 committed = false; |
| 847 return scoped_ptr<BatchRequestConfiguratorInterface>( |
| 848 new BatchRequestConfigurator(this)); |
| 849 } |
| 850 |
| 851 std::vector<UploadFileInfo> files; |
| 852 bool committed; |
| 853 }; |
| 854 |
| 855 TEST_F(DriveUploaderTest, BatchProcessing) { |
| 856 // Preapre test file. |
| 857 const size_t kTestFileSize = 1024 * 512; |
| 858 base::FilePath local_path; |
| 859 std::string data; |
| 860 ASSERT_TRUE(test_util::CreateFileOfSpecifiedSize( |
| 861 temp_dir_.path(), kTestFileSize, &local_path, &data)); |
| 862 |
| 863 // Prepare test target. |
| 864 MockDriveServiceForBatchProcessing service; |
| 865 DriveUploader uploader(&service, base::ThreadTaskRunnerHandle::Get().get()); |
| 866 |
| 867 struct { |
| 868 DriveApiErrorCode error; |
| 869 GURL resume_url; |
| 870 scoped_ptr<FileResource> file; |
| 871 UploadCompletionCallback callback() { |
| 872 return test_util::CreateCopyResultCallback(&error, &resume_url, &file); |
| 873 } |
| 874 } results[2]; |
| 875 |
| 876 uploader.StartBatchProcessing(); |
| 877 uploader.UploadNewFile("parent_resource_id", local_path, "title", |
| 878 kTestMimeType, UploadNewFileOptions(), |
| 879 results[0].callback(), |
| 880 google_apis::ProgressCallback()); |
| 881 uploader.UploadExistingFile( |
| 882 "resource_id", local_path, kTestMimeType, UploadExistingFileOptions(), |
| 883 results[1].callback(), google_apis::ProgressCallback()); |
| 884 uploader.StopBatchProcessing(); |
| 885 base::RunLoop().RunUntilIdle(); |
| 886 |
| 887 ASSERT_EQ(2u, service.files.size()); |
| 888 EXPECT_TRUE(service.committed); |
| 889 |
| 890 EXPECT_EQ(MockDriveServiceForBatchProcessing::UploadFileInfo::NEW_FILE, |
| 891 service.files[0].type); |
| 892 EXPECT_EQ(kTestMimeType, service.files[0].content_type); |
| 893 EXPECT_EQ(kTestFileSize, service.files[0].content_length); |
| 894 EXPECT_EQ("parent_resource_id", service.files[0].parent_resource_id); |
| 895 EXPECT_EQ("", service.files[0].resource_id); |
| 896 EXPECT_EQ("title", service.files[0].title); |
| 897 EXPECT_EQ(local_path.value(), service.files[0].local_file_path.value()); |
| 898 |
| 899 EXPECT_EQ(MockDriveServiceForBatchProcessing::UploadFileInfo::EXISTING_FILE, |
| 900 service.files[1].type); |
| 901 EXPECT_EQ(kTestMimeType, service.files[1].content_type); |
| 902 EXPECT_EQ(kTestFileSize, service.files[1].content_length); |
| 903 EXPECT_EQ("", service.files[1].parent_resource_id); |
| 904 EXPECT_EQ("resource_id", service.files[1].resource_id); |
| 905 EXPECT_EQ("", service.files[1].title); |
| 906 EXPECT_EQ(local_path.value(), service.files[1].local_file_path.value()); |
| 907 |
| 908 EXPECT_EQ(HTTP_SUCCESS, results[0].error); |
| 909 EXPECT_TRUE(results[0].resume_url.is_empty()); |
| 910 EXPECT_TRUE(results[0].file); |
| 911 |
| 912 EXPECT_EQ(HTTP_SUCCESS, results[1].error); |
| 913 EXPECT_TRUE(results[1].resume_url.is_empty()); |
| 914 EXPECT_TRUE(results[1].file); |
| 915 } |
| 916 |
| 917 TEST_F(DriveUploaderTest, BatchProcessingWithError) { |
| 918 // Prepare test target. |
| 919 MockDriveServiceForBatchProcessing service; |
| 920 DriveUploader uploader(&service, base::ThreadTaskRunnerHandle::Get().get()); |
| 921 |
| 922 struct { |
| 923 DriveApiErrorCode error; |
| 924 GURL resume_url; |
| 925 scoped_ptr<FileResource> file; |
| 926 UploadCompletionCallback callback() { |
| 927 return test_util::CreateCopyResultCallback(&error, &resume_url, &file); |
| 928 } |
| 929 } results[2]; |
| 930 |
| 931 uploader.StartBatchProcessing(); |
| 932 uploader.UploadNewFile("parent_resource_id", |
| 933 base::FilePath(FILE_PATH_LITERAL("/path/non_exists")), |
| 934 "title", kTestMimeType, UploadNewFileOptions(), |
| 935 results[0].callback(), |
| 936 google_apis::ProgressCallback()); |
| 937 uploader.UploadExistingFile( |
| 938 "resource_id", base::FilePath(FILE_PATH_LITERAL("/path/non_exists")), |
| 939 kTestMimeType, UploadExistingFileOptions(), results[1].callback(), |
| 940 google_apis::ProgressCallback()); |
| 941 uploader.StopBatchProcessing(); |
| 942 base::RunLoop().RunUntilIdle(); |
| 943 |
| 944 EXPECT_EQ(0u, service.files.size()); |
| 945 EXPECT_TRUE(service.committed); |
| 946 |
| 947 EXPECT_EQ(HTTP_NOT_FOUND, results[0].error); |
| 948 EXPECT_TRUE(results[0].resume_url.is_empty()); |
| 949 EXPECT_FALSE(results[0].file); |
| 950 |
| 951 EXPECT_EQ(HTTP_NOT_FOUND, results[1].error); |
| 952 EXPECT_TRUE(results[1].resume_url.is_empty()); |
| 953 EXPECT_FALSE(results[1].file); |
| 954 } |
767 } // namespace drive | 955 } // namespace drive |
OLD | NEW |