| 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/fileapi/file_stream_write
r.h" | 5 #include "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_write
r.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <string> | 10 #include <string> |
| 8 #include <vector> | 11 #include <vector> |
| 9 | 12 |
| 10 #include "base/files/file.h" | 13 #include "base/files/file.h" |
| 11 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 15 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 16 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system
.h" | 19 #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system
.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 scoped_ptr<TestingProfileManager> profile_manager_; | 99 scoped_ptr<TestingProfileManager> profile_manager_; |
| 97 TestingProfile* profile_; // Owned by TestingProfileManager. | 100 TestingProfile* profile_; // Owned by TestingProfileManager. |
| 98 FakeProvidedFileSystem* provided_file_system_; // Owned by Service. | 101 FakeProvidedFileSystem* provided_file_system_; // Owned by Service. |
| 99 storage::FileSystemURL file_url_; | 102 storage::FileSystemURL file_url_; |
| 100 storage::FileSystemURL wrong_file_url_; | 103 storage::FileSystemURL wrong_file_url_; |
| 101 }; | 104 }; |
| 102 | 105 |
| 103 TEST_F(FileSystemProviderFileStreamWriter, Write) { | 106 TEST_F(FileSystemProviderFileStreamWriter, Write) { |
| 104 std::vector<int> write_log; | 107 std::vector<int> write_log; |
| 105 | 108 |
| 106 const int64 initial_offset = 0; | 109 const int64_t initial_offset = 0; |
| 107 FileStreamWriter writer(file_url_, initial_offset); | 110 FileStreamWriter writer(file_url_, initial_offset); |
| 108 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); | 111 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); |
| 109 | 112 |
| 110 { | 113 { |
| 111 const int result = writer.Write(io_buffer.get(), | 114 const int result = writer.Write(io_buffer.get(), |
| 112 sizeof(kTextToWrite) - 1, | 115 sizeof(kTextToWrite) - 1, |
| 113 base::Bind(&LogValue, &write_log)); | 116 base::Bind(&LogValue, &write_log)); |
| 114 EXPECT_EQ(net::ERR_IO_PENDING, result); | 117 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 115 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
| 116 | 119 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 147 const std::string expected_contents = | 150 const std::string expected_contents = |
| 148 std::string(kTextToWrite) + kTextToWrite; | 151 std::string(kTextToWrite) + kTextToWrite; |
| 149 EXPECT_EQ(expected_contents, | 152 EXPECT_EQ(expected_contents, |
| 150 entry->contents.substr(0, expected_contents.size())); | 153 entry->contents.substr(0, expected_contents.size())); |
| 151 } | 154 } |
| 152 } | 155 } |
| 153 | 156 |
| 154 TEST_F(FileSystemProviderFileStreamWriter, Cancel) { | 157 TEST_F(FileSystemProviderFileStreamWriter, Cancel) { |
| 155 std::vector<int> write_log; | 158 std::vector<int> write_log; |
| 156 | 159 |
| 157 const int64 initial_offset = 0; | 160 const int64_t initial_offset = 0; |
| 158 FileStreamWriter writer(file_url_, initial_offset); | 161 FileStreamWriter writer(file_url_, initial_offset); |
| 159 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); | 162 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); |
| 160 | 163 |
| 161 const int write_result = writer.Write(io_buffer.get(), | 164 const int write_result = writer.Write(io_buffer.get(), |
| 162 sizeof(kTextToWrite) - 1, | 165 sizeof(kTextToWrite) - 1, |
| 163 base::Bind(&LogValue, &write_log)); | 166 base::Bind(&LogValue, &write_log)); |
| 164 EXPECT_EQ(net::ERR_IO_PENDING, write_result); | 167 EXPECT_EQ(net::ERR_IO_PENDING, write_result); |
| 165 | 168 |
| 166 std::vector<int> cancel_log; | 169 std::vector<int> cancel_log; |
| 167 const int cancel_result = writer.Cancel(base::Bind(&LogValue, &cancel_log)); | 170 const int cancel_result = writer.Cancel(base::Bind(&LogValue, &cancel_log)); |
| 168 EXPECT_EQ(net::ERR_IO_PENDING, cancel_result); | 171 EXPECT_EQ(net::ERR_IO_PENDING, cancel_result); |
| 169 base::RunLoop().RunUntilIdle(); | 172 base::RunLoop().RunUntilIdle(); |
| 170 | 173 |
| 171 EXPECT_EQ(0u, write_log.size()); | 174 EXPECT_EQ(0u, write_log.size()); |
| 172 ASSERT_EQ(1u, cancel_log.size()); | 175 ASSERT_EQ(1u, cancel_log.size()); |
| 173 EXPECT_EQ(net::OK, cancel_log[0]); | 176 EXPECT_EQ(net::OK, cancel_log[0]); |
| 174 } | 177 } |
| 175 | 178 |
| 176 TEST_F(FileSystemProviderFileStreamWriter, Cancel_NotRunning) { | 179 TEST_F(FileSystemProviderFileStreamWriter, Cancel_NotRunning) { |
| 177 std::vector<int> write_log; | 180 std::vector<int> write_log; |
| 178 | 181 |
| 179 const int64 initial_offset = 0; | 182 const int64_t initial_offset = 0; |
| 180 FileStreamWriter writer(file_url_, initial_offset); | 183 FileStreamWriter writer(file_url_, initial_offset); |
| 181 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); | 184 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); |
| 182 | 185 |
| 183 std::vector<int> cancel_log; | 186 std::vector<int> cancel_log; |
| 184 const int cancel_result = writer.Cancel(base::Bind(&LogValue, &cancel_log)); | 187 const int cancel_result = writer.Cancel(base::Bind(&LogValue, &cancel_log)); |
| 185 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); | 188 EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); |
| 186 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 187 | 190 |
| 188 EXPECT_EQ(0u, write_log.size()); | 191 EXPECT_EQ(0u, write_log.size()); |
| 189 EXPECT_EQ(0u, cancel_log.size()); // Result returned synchronously. | 192 EXPECT_EQ(0u, cancel_log.size()); // Result returned synchronously. |
| 190 } | 193 } |
| 191 | 194 |
| 192 TEST_F(FileSystemProviderFileStreamWriter, Write_WrongFile) { | 195 TEST_F(FileSystemProviderFileStreamWriter, Write_WrongFile) { |
| 193 std::vector<int> write_log; | 196 std::vector<int> write_log; |
| 194 | 197 |
| 195 const int64 initial_offset = 0; | 198 const int64_t initial_offset = 0; |
| 196 FileStreamWriter writer(wrong_file_url_, initial_offset); | 199 FileStreamWriter writer(wrong_file_url_, initial_offset); |
| 197 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); | 200 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); |
| 198 | 201 |
| 199 const int result = writer.Write(io_buffer.get(), | 202 const int result = writer.Write(io_buffer.get(), |
| 200 sizeof(kTextToWrite) - 1, | 203 sizeof(kTextToWrite) - 1, |
| 201 base::Bind(&LogValue, &write_log)); | 204 base::Bind(&LogValue, &write_log)); |
| 202 EXPECT_EQ(net::ERR_IO_PENDING, result); | 205 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 203 base::RunLoop().RunUntilIdle(); | 206 base::RunLoop().RunUntilIdle(); |
| 204 | 207 |
| 205 ASSERT_EQ(1u, write_log.size()); | 208 ASSERT_EQ(1u, write_log.size()); |
| 206 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, write_log[0]); | 209 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, write_log[0]); |
| 207 } | 210 } |
| 208 | 211 |
| 209 TEST_F(FileSystemProviderFileStreamWriter, Write_Append) { | 212 TEST_F(FileSystemProviderFileStreamWriter, Write_Append) { |
| 210 std::vector<int> write_log; | 213 std::vector<int> write_log; |
| 211 | 214 |
| 212 const FakeEntry* const entry = | 215 const FakeEntry* const entry = |
| 213 provided_file_system_->GetEntry(base::FilePath(kFakeFilePath)); | 216 provided_file_system_->GetEntry(base::FilePath(kFakeFilePath)); |
| 214 ASSERT_TRUE(entry); | 217 ASSERT_TRUE(entry); |
| 215 | 218 |
| 216 const std::string original_contents = entry->contents; | 219 const std::string original_contents = entry->contents; |
| 217 const int64 initial_offset = *entry->metadata->size; | 220 const int64_t initial_offset = *entry->metadata->size; |
| 218 ASSERT_LT(0, initial_offset); | 221 ASSERT_LT(0, initial_offset); |
| 219 | 222 |
| 220 FileStreamWriter writer(file_url_, initial_offset); | 223 FileStreamWriter writer(file_url_, initial_offset); |
| 221 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); | 224 scoped_refptr<net::IOBuffer> io_buffer(new net::StringIOBuffer(kTextToWrite)); |
| 222 | 225 |
| 223 const int result = writer.Write(io_buffer.get(), | 226 const int result = writer.Write(io_buffer.get(), |
| 224 sizeof(kTextToWrite) - 1, | 227 sizeof(kTextToWrite) - 1, |
| 225 base::Bind(&LogValue, &write_log)); | 228 base::Bind(&LogValue, &write_log)); |
| 226 EXPECT_EQ(net::ERR_IO_PENDING, result); | 229 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 227 base::RunLoop().RunUntilIdle(); | 230 base::RunLoop().RunUntilIdle(); |
| 228 | 231 |
| 229 ASSERT_EQ(1u, write_log.size()); | 232 ASSERT_EQ(1u, write_log.size()); |
| 230 EXPECT_EQ(sizeof(kTextToWrite) - 1, static_cast<size_t>(write_log[0])); | 233 EXPECT_EQ(sizeof(kTextToWrite) - 1, static_cast<size_t>(write_log[0])); |
| 231 | 234 |
| 232 const std::string expected_contents = original_contents + kTextToWrite; | 235 const std::string expected_contents = original_contents + kTextToWrite; |
| 233 EXPECT_EQ(expected_contents, entry->contents); | 236 EXPECT_EQ(expected_contents, entry->contents); |
| 234 } | 237 } |
| 235 | 238 |
| 236 } // namespace file_system_provider | 239 } // namespace file_system_provider |
| 237 } // namespace chromeos | 240 } // namespace chromeos |
| OLD | NEW |