fake_drive_uploader.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chrome/browser/sync_file_system/drive_backend/fake_drive_uploader.h" 6 7#include "base/bind.h" 8#include "base/location.h" 9#include "base/logging.h" 10#include "base/message_loop/message_loop.h" 11#include "base/message_loop/message_loop_proxy.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14using drive::FakeDriveService; 15using drive::UploadCompletionCallback; 16using google_apis::CancelCallback; 17using google_apis::GDataErrorCode; 18using google_apis::GetResourceEntryCallback; 19using google_apis::ProgressCallback; 20using google_apis::ResourceEntry; 21using google_apis::ResourceList; 22 23namespace sync_file_system { 24namespace drive_backend { 25 26namespace { 27 28void DidAddFileOrDirectoryForMakingConflict(GDataErrorCode error, 29 scoped_ptr<ResourceEntry> entry) { 30 ASSERT_EQ(google_apis::HTTP_CREATED, error); 31 ASSERT_TRUE(entry); 32} 33 34void DidAddFileForUploadNew( 35 const UploadCompletionCallback& callback, 36 GDataErrorCode error, 37 scoped_ptr<ResourceEntry> entry) { 38 ASSERT_EQ(google_apis::HTTP_CREATED, error); 39 ASSERT_TRUE(entry); 40 base::MessageLoopProxy::current()->PostTask( 41 FROM_HERE, 42 base::Bind(callback, 43 google_apis::HTTP_SUCCESS, 44 GURL(), 45 base::Passed(&entry))); 46} 47 48void DidGetResourceEntryForUploadExisting( 49 const UploadCompletionCallback& callback, 50 GDataErrorCode error, 51 scoped_ptr<ResourceEntry> entry) { 52 base::MessageLoopProxy::current()->PostTask( 53 FROM_HERE, 54 base::Bind(callback, 55 error, 56 GURL(), 57 base::Passed(&entry))); 58} 59 60} // namespace 61 62FakeDriveServiceWrapper::FakeDriveServiceWrapper() 63 : make_directory_conflict_(false) {} 64 65FakeDriveServiceWrapper::~FakeDriveServiceWrapper() {} 66 67CancelCallback FakeDriveServiceWrapper::AddNewDirectory( 68 const std::string& parent_resource_id, 69 const std::string& directory_name, 70 const AddNewDirectoryOptions& options, 71 const GetResourceEntryCallback& callback) { 72 if (make_directory_conflict_) { 73 FakeDriveService::AddNewDirectory( 74 parent_resource_id, 75 directory_name, 76 options, 77 base::Bind(&DidAddFileOrDirectoryForMakingConflict)); 78 } 79 return FakeDriveService::AddNewDirectory( 80 parent_resource_id, directory_name, options, callback); 81} 82 83FakeDriveUploader::FakeDriveUploader( 84 FakeDriveServiceWrapper* fake_drive_service) 85 : fake_drive_service_(fake_drive_service), 86 make_file_conflict_(false) {} 87 88FakeDriveUploader::~FakeDriveUploader() {} 89 90CancelCallback FakeDriveUploader::UploadNewFile( 91 const std::string& parent_resource_id, 92 const base::FilePath& local_file_path, 93 const std::string& title, 94 const std::string& content_type, 95 const UploadNewFileOptions& options, 96 const UploadCompletionCallback& callback, 97 const ProgressCallback& progress_callback) { 98 DCHECK(!callback.is_null()); 99 const std::string kFileContent = "test content"; 100 101 if (make_file_conflict_) { 102 fake_drive_service_->AddNewFile( 103 content_type, 104 kFileContent, 105 parent_resource_id, 106 title, 107 false, // shared_with_me 108 base::Bind(&DidAddFileOrDirectoryForMakingConflict)); 109 } 110 111 fake_drive_service_->AddNewFile( 112 content_type, 113 kFileContent, 114 parent_resource_id, 115 title, 116 false, // shared_with_me 117 base::Bind(&DidAddFileForUploadNew, callback)); 118 base::MessageLoop::current()->RunUntilIdle(); 119 120 return CancelCallback(); 121} 122 123CancelCallback FakeDriveUploader::UploadExistingFile( 124 const std::string& resource_id, 125 const base::FilePath& local_file_path, 126 const std::string& content_type, 127 const UploadExistingFileOptions& options, 128 const UploadCompletionCallback& callback, 129 const ProgressCallback& progress_callback) { 130 DCHECK(!callback.is_null()); 131 return fake_drive_service_->GetResourceEntry( 132 resource_id, 133 base::Bind(&DidGetResourceEntryForUploadExisting, callback)); 134} 135 136CancelCallback FakeDriveUploader::ResumeUploadFile( 137 const GURL& upload_location, 138 const base::FilePath& local_file_path, 139 const std::string& content_type, 140 const UploadCompletionCallback& callback, 141 const ProgressCallback& progress_callback) { 142 // At the moment, sync file system doesn't support resuming of the uploading. 143 // So this method shouldn't be reached. 144 NOTREACHED(); 145 return CancelCallback(); 146} 147 148} // namespace drive_backend 149} // namespace sync_file_system 150