| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/file_system_provider/operations/write_file.h" | 5 #include "chrome/browser/chromeos/file_system_provider/operations/write_file.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace chromeos { | 23 namespace chromeos { |
| 24 namespace file_system_provider { | 24 namespace file_system_provider { |
| 25 namespace operations { | 25 namespace operations { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; | 28 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 29 const char kFileSystemId[] = "testing-file-system"; | 29 const char kFileSystemId[] = "testing-file-system"; |
| 30 const int kRequestId = 2; | 30 const int kRequestId = 2; |
| 31 const int kFileHandle = 3; | 31 const int kFileHandle = 3; |
| 32 const char kWriteData[] = "Welcome to my world!"; |
| 32 const int kOffset = 10; | 33 const int kOffset = 10; |
| 33 const int kLength = 5; | |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 class FileSystemProviderOperationsWriteFileTest : public testing::Test { | 37 class FileSystemProviderOperationsWriteFileTest : public testing::Test { |
| 38 protected: | 38 protected: |
| 39 FileSystemProviderOperationsWriteFileTest() {} | 39 FileSystemProviderOperationsWriteFileTest() {} |
| 40 virtual ~FileSystemProviderOperationsWriteFileTest() {} | 40 virtual ~FileSystemProviderOperationsWriteFileTest() {} |
| 41 | 41 |
| 42 virtual void SetUp() OVERRIDE { | 42 virtual void SetUp() OVERRIDE { |
| 43 file_system_info_ = | 43 file_system_info_ = |
| 44 ProvidedFileSystemInfo(kExtensionId, | 44 ProvidedFileSystemInfo(kExtensionId, |
| 45 kFileSystemId, | 45 kFileSystemId, |
| 46 "" /* display_name */, | 46 "" /* display_name */, |
| 47 true /* writable */, | 47 true /* writable */, |
| 48 base::FilePath() /* mount_path */); | 48 base::FilePath() /* mount_path */); |
| 49 io_buffer_ = make_scoped_refptr(new net::IOBuffer(kOffset + kLength)); | 49 io_buffer_ = make_scoped_refptr(new net::StringIOBuffer(kWriteData)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 ProvidedFileSystemInfo file_system_info_; | 52 ProvidedFileSystemInfo file_system_info_; |
| 53 scoped_refptr<net::IOBuffer> io_buffer_; | 53 scoped_refptr<net::StringIOBuffer> io_buffer_; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute) { | 56 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute) { |
| 57 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 57 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 58 util::StatusCallbackLog callback_log; | 58 util::StatusCallbackLog callback_log; |
| 59 | 59 |
| 60 WriteFile write_file(NULL, | 60 WriteFile write_file(NULL, |
| 61 file_system_info_, | 61 file_system_info_, |
| 62 kFileHandle, | 62 kFileHandle, |
| 63 io_buffer_.get(), | 63 io_buffer_.get(), |
| 64 kOffset, | 64 kOffset, |
| 65 kLength, | 65 io_buffer_->size(), |
| 66 base::Bind(&util::LogStatusCallback, &callback_log)); | 66 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 67 write_file.SetDispatchEventImplForTesting( | 67 write_file.SetDispatchEventImplForTesting( |
| 68 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 68 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 69 base::Unretained(&dispatcher))); | 69 base::Unretained(&dispatcher))); |
| 70 | 70 |
| 71 EXPECT_TRUE(write_file.Execute(kRequestId)); | 71 EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 72 | 72 |
| 73 ASSERT_EQ(1u, dispatcher.events().size()); | 73 ASSERT_EQ(1u, dispatcher.events().size()); |
| 74 extensions::Event* event = dispatcher.events()[0]; | 74 extensions::Event* event = dispatcher.events()[0]; |
| 75 EXPECT_EQ( | 75 EXPECT_EQ( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 90 EXPECT_EQ(kRequestId, event_request_id); | 90 EXPECT_EQ(kRequestId, event_request_id); |
| 91 | 91 |
| 92 int event_file_handle = -1; | 92 int event_file_handle = -1; |
| 93 EXPECT_TRUE(options->GetInteger("openRequestId", &event_file_handle)); | 93 EXPECT_TRUE(options->GetInteger("openRequestId", &event_file_handle)); |
| 94 EXPECT_EQ(kFileHandle, event_file_handle); | 94 EXPECT_EQ(kFileHandle, event_file_handle); |
| 95 | 95 |
| 96 double event_offset = -1; | 96 double event_offset = -1; |
| 97 EXPECT_TRUE(options->GetDouble("offset", &event_offset)); | 97 EXPECT_TRUE(options->GetDouble("offset", &event_offset)); |
| 98 EXPECT_EQ(kOffset, static_cast<double>(event_offset)); | 98 EXPECT_EQ(kOffset, static_cast<double>(event_offset)); |
| 99 | 99 |
| 100 int event_length = -1; | 100 base::BinaryValue* event_data = NULL; |
| 101 EXPECT_TRUE(options->GetInteger("length", &event_length)); | 101 ASSERT_TRUE(options->GetBinary("data", &event_data)); |
| 102 EXPECT_EQ(kLength, event_length); | 102 EXPECT_EQ(static_cast<size_t>(io_buffer_->size()), event_data->GetSize()); |
| 103 char* const event_data_buffer = event_data->GetBuffer(); |
| 104 ASSERT_TRUE(event_data_buffer); |
| 105 EXPECT_EQ(std::string(kWriteData), |
| 106 std::string(event_data_buffer, event_data->GetSize())); |
| 103 } | 107 } |
| 104 | 108 |
| 105 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_NoListener) { | 109 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_NoListener) { |
| 106 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); | 110 util::LoggingDispatchEventImpl dispatcher(false /* dispatch_reply */); |
| 107 util::StatusCallbackLog callback_log; | 111 util::StatusCallbackLog callback_log; |
| 108 | 112 |
| 109 WriteFile write_file(NULL, | 113 WriteFile write_file(NULL, |
| 110 file_system_info_, | 114 file_system_info_, |
| 111 kFileHandle, | 115 kFileHandle, |
| 112 io_buffer_.get(), | 116 io_buffer_.get(), |
| 113 kOffset, | 117 kOffset, |
| 114 kLength, | 118 io_buffer_->size(), |
| 115 base::Bind(&util::LogStatusCallback, &callback_log)); | 119 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 116 write_file.SetDispatchEventImplForTesting( | 120 write_file.SetDispatchEventImplForTesting( |
| 117 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 121 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 118 base::Unretained(&dispatcher))); | 122 base::Unretained(&dispatcher))); |
| 119 | 123 |
| 120 EXPECT_FALSE(write_file.Execute(kRequestId)); | 124 EXPECT_FALSE(write_file.Execute(kRequestId)); |
| 121 } | 125 } |
| 122 | 126 |
| 123 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_ReadOnly) { | 127 TEST_F(FileSystemProviderOperationsWriteFileTest, Execute_ReadOnly) { |
| 124 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 128 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 125 util::StatusCallbackLog callback_log; | 129 util::StatusCallbackLog callback_log; |
| 126 | 130 |
| 127 const ProvidedFileSystemInfo read_only_file_system_info( | 131 const ProvidedFileSystemInfo read_only_file_system_info( |
| 128 kExtensionId, | 132 kExtensionId, |
| 129 kFileSystemId, | 133 kFileSystemId, |
| 130 "" /* file_system_name */, | 134 "" /* file_system_name */, |
| 131 false /* writable */, | 135 false /* writable */, |
| 132 base::FilePath() /* mount_path */); | 136 base::FilePath() /* mount_path */); |
| 133 | 137 |
| 134 WriteFile write_file(NULL, | 138 WriteFile write_file(NULL, |
| 135 read_only_file_system_info, | 139 read_only_file_system_info, |
| 136 kFileHandle, | 140 kFileHandle, |
| 137 io_buffer_.get(), | 141 io_buffer_.get(), |
| 138 kOffset, | 142 kOffset, |
| 139 kLength, | 143 io_buffer_->size(), |
| 140 base::Bind(&util::LogStatusCallback, &callback_log)); | 144 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 141 write_file.SetDispatchEventImplForTesting( | 145 write_file.SetDispatchEventImplForTesting( |
| 142 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 146 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 143 base::Unretained(&dispatcher))); | 147 base::Unretained(&dispatcher))); |
| 144 | 148 |
| 145 EXPECT_FALSE(write_file.Execute(kRequestId)); | 149 EXPECT_FALSE(write_file.Execute(kRequestId)); |
| 146 } | 150 } |
| 147 | 151 |
| 148 TEST_F(FileSystemProviderOperationsWriteFileTest, OnSuccess) { | 152 TEST_F(FileSystemProviderOperationsWriteFileTest, OnSuccess) { |
| 149 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 153 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 150 util::StatusCallbackLog callback_log; | 154 util::StatusCallbackLog callback_log; |
| 151 | 155 |
| 152 WriteFile write_file(NULL, | 156 WriteFile write_file(NULL, |
| 153 file_system_info_, | 157 file_system_info_, |
| 154 kFileHandle, | 158 kFileHandle, |
| 155 io_buffer_.get(), | 159 io_buffer_.get(), |
| 156 kOffset, | 160 kOffset, |
| 157 kLength, | 161 io_buffer_->size(), |
| 158 base::Bind(&util::LogStatusCallback, &callback_log)); | 162 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 159 write_file.SetDispatchEventImplForTesting( | 163 write_file.SetDispatchEventImplForTesting( |
| 160 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 164 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 161 base::Unretained(&dispatcher))); | 165 base::Unretained(&dispatcher))); |
| 162 | 166 |
| 163 EXPECT_TRUE(write_file.Execute(kRequestId)); | 167 EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 164 | 168 |
| 165 write_file.OnSuccess(kRequestId, | 169 write_file.OnSuccess(kRequestId, |
| 166 scoped_ptr<RequestValue>(new RequestValue()), | 170 scoped_ptr<RequestValue>(new RequestValue()), |
| 167 false /* has_more */); | 171 false /* has_more */); |
| 168 ASSERT_EQ(1u, callback_log.size()); | 172 ASSERT_EQ(1u, callback_log.size()); |
| 169 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); | 173 EXPECT_EQ(base::File::FILE_OK, callback_log[0]); |
| 170 } | 174 } |
| 171 | 175 |
| 172 TEST_F(FileSystemProviderOperationsWriteFileTest, OnError) { | 176 TEST_F(FileSystemProviderOperationsWriteFileTest, OnError) { |
| 173 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); | 177 util::LoggingDispatchEventImpl dispatcher(true /* dispatch_reply */); |
| 174 util::StatusCallbackLog callback_log; | 178 util::StatusCallbackLog callback_log; |
| 175 | 179 |
| 176 WriteFile write_file(NULL, | 180 WriteFile write_file(NULL, |
| 177 file_system_info_, | 181 file_system_info_, |
| 178 kFileHandle, | 182 kFileHandle, |
| 179 io_buffer_.get(), | 183 io_buffer_.get(), |
| 180 kOffset, | 184 kOffset, |
| 181 kLength, | 185 io_buffer_->size(), |
| 182 base::Bind(&util::LogStatusCallback, &callback_log)); | 186 base::Bind(&util::LogStatusCallback, &callback_log)); |
| 183 write_file.SetDispatchEventImplForTesting( | 187 write_file.SetDispatchEventImplForTesting( |
| 184 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, | 188 base::Bind(&util::LoggingDispatchEventImpl::OnDispatchEventImpl, |
| 185 base::Unretained(&dispatcher))); | 189 base::Unretained(&dispatcher))); |
| 186 | 190 |
| 187 EXPECT_TRUE(write_file.Execute(kRequestId)); | 191 EXPECT_TRUE(write_file.Execute(kRequestId)); |
| 188 | 192 |
| 189 write_file.OnError(kRequestId, | 193 write_file.OnError(kRequestId, |
| 190 scoped_ptr<RequestValue>(new RequestValue()), | 194 scoped_ptr<RequestValue>(new RequestValue()), |
| 191 base::File::FILE_ERROR_TOO_MANY_OPENED); | 195 base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 192 | 196 |
| 193 ASSERT_EQ(1u, callback_log.size()); | 197 ASSERT_EQ(1u, callback_log.size()); |
| 194 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); | 198 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, callback_log[0]); |
| 195 } | 199 } |
| 196 | 200 |
| 197 } // namespace operations | 201 } // namespace operations |
| 198 } // namespace file_system_provider | 202 } // namespace file_system_provider |
| 199 } // namespace chromeos | 203 } // namespace chromeos |
| OLD | NEW |