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/chromeos/file_system_provider/operations/copy_entry.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "base/files/file.h" 11 #include "base/files/file_path.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_vector.h" 14 #include "chrome/browser/chromeos/file_system_provider/operations/test_util.h" 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h" 16 #include "chrome/common/extensions/api/file_system_provider.h" 17 #include "chrome/common/extensions/api/file_system_provider_internal.h" 18 #include "extensions/browser/event_router.h" 19 #include "storage/browser/fileapi/async_file_util.h" 20 #include "testing/gtest/include/gtest/gtest.h" 21 22 namespace chromeos { 23 namespace file_system_provider { 24 namespace operations { 25 namespace { 26 27 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; 28 const char kFileSystemId[] = "testing-file-system"; 29 const int kRequestId = 2; 30 const base::FilePath::CharType kSourcePath[] = "/bunny/and/bear/happy"; 31 const base::FilePath::CharType kTargetPath[] = "/kitty/and/puppy/happy"; 32 33 } // namespace 34 35 class FileSystemProviderOperationsCopyEntryTest : public testing::Test { 36 protected: 37 FileSystemProviderOperationsCopyEntryTest() {} 38 virtual ~FileSystemProviderOperationsCopyEntryTest() {} 39 40 virtual void SetUp() OVERRIDE { 41 file_system_info_ = 42 ProvidedFileSystemInfo(kExtensionId, 43 kFileSystemId, 44 "" /* file_system_name */, 45 true /* writable */, 46 base::FilePath() /* mount_path */); 47 } 48 49 ProvidedFileSystemInfo file_system_info_; 50 }; 51 52 TEST_F(FileSystemProviderOperationsCopyEntryTest, Execute) { 53 using extensions::api::file_system_provider::CopyEntryRequestedOptions; 54 55 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 56 util::StatusCallbackLog callback_log; 57 58 CopyEntry copy_entry(NULL, 59 file_system_info_, 60 base::FilePath::FromUTF8Unsafe(kSourcePath), 61 base::FilePath::FromUTF8Unsafe(kTargetPath), 62 base::Bind(&util::LogStatusCallback, &callback_log)); 63 copy_entry.SetDispatchEventImplForTesting( 64 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 65 base::Unretained(&dispatcher))); 66 67 EXPECT_TRUE(copy_entry.Execute(kRequestId)); 68 69 ASSERT_EQ(1u, dispatcher.events().size()); 70 extensions::Event* event = dispatcher.events()[0]; 71 EXPECT_EQ( 72 extensions::api::file_system_provider::OnCopyEntryRequested::kEventName, 73 event->event_name); 74 base::ListValue* event_args = event->event_args.get(); 75 ASSERT_EQ(1u, event_args->GetSize()); 76 77 const base::DictionaryValue* options_as_value = NULL; 78 ASSERT_TRUE(event_args->GetDictionary(0, &options_as_value)); 79 80 CopyEntryRequestedOptions options; 81 ASSERT_TRUE(CopyEntryRequestedOptions::Populate(*options_as_value, &options)); 82 EXPECT_EQ(kFileSystemId, options.file_system_id); 83 EXPECT_EQ(kRequestId, options.request_id); 84 EXPECT_EQ(kSourcePath, options.source_path); 85 EXPECT_EQ(kTargetPath, options.target_path); 86 } 87 88 TEST_F(FileSystemProviderOperationsCopyEntryTest, Execute_NoListener) { 89 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); 90 util::StatusCallbackLog callback_log; 91 92 CopyEntry copy_entry(NULL, 93 file_system_info_, 94 base::FilePath::FromUTF8Unsafe(kSourcePath), 95 base::FilePath::FromUTF8Unsafe(kTargetPath), 96 base::Bind(&util::LogStatusCallback, &callback_log)); 97 copy_entry.SetDispatchEventImplForTesting( 98 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 99 base::Unretained(&dispatcher))); 100 101 EXPECT_FALSE(copy_entry.Execute(kRequestId)); 102 } 103 104 TEST_F(FileSystemProviderOperationsCopyEntryTest, Execute_ReadOnly) { 105 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 106 util::StatusCallbackLog callback_log; 107 108 const ProvidedFileSystemInfo read_only_file_system_info( 109 kExtensionId, 110 kFileSystemId, 111 "" /* file_system_name */, 112 false /* writable */, 113 base::FilePath() /* mount_path */); 114 115 CopyEntry copy_entry(NULL, 116 read_only_file_system_info, 117 base::FilePath::FromUTF8Unsafe(kSourcePath), 118 base::FilePath::FromUTF8Unsafe(kTargetPath), 119 base::Bind(&util::LogStatusCallback, &callback_log)); 120 copy_entry.SetDispatchEventImplForTesting( 121 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 122 base::Unretained(&dispatcher))); 123 124 EXPECT_FALSE(copy_entry.Execute(kRequestId)); 125 } 126 127 TEST_F(FileSystemProviderOperationsCopyEntryTest, OnSuccess) { 128 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 129 util::StatusCallbackLog callback_log; 130 131 CopyEntry copy_entry(NULL, 132 file_system_info_, 133 base::FilePath::FromUTF8Unsafe(kSourcePath), 134 base::FilePath::FromUTF8Unsafe(kTargetPath), 135 base::Bind(&util::LogStatusCallback, &callback_log)); 136 copy_entry.SetDispatchEventImplForTesting( 137 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 138 base::Unretained(&dispatcher))); 139 140 EXPECT_TRUE(copy_entry.Execute(kRequestId)); 141 142 copy_entry.OnSuccess(kRequestId, 143 scoped_ptr<RequestValue>(new RequestValue()), 144 false /* has_more */); 145 ASSERT_EQ(1u, callback_log.size()); 146 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); 147 } 148 149 TEST_F(FileSystemProviderOperationsCopyEntryTest, OnError) { 150 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); 151 util::StatusCallbackLog callback_log; 152 153 CopyEntry copy_entry(NULL, 154 file_system_info_, 155 base::FilePath::FromUTF8Unsafe(kSourcePath), 156 base::FilePath::FromUTF8Unsafe(kTargetPath), 157 base::Bind(&util::LogStatusCallback, &callback_log)); 158 copy_entry.SetDispatchEventImplForTesting( 159 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, 160 base::Unretained(&dispatcher))); 161 162 EXPECT_TRUE(copy_entry.Execute(kRequestId)); 163 164 copy_entry.OnError(kRequestId, 165 scoped_ptr<RequestValue>(new RequestValue()), 166 base::File::FILE_ERROR_TOO_MANY_OPENED); 167 ASSERT_EQ(1u, callback_log.size()); 168 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); 169 } 170 171 } // namespace operations 172 } // namespace file_system_provider 173 } // namespace chromeos 174