| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "content/public/test/mock_blob_url_request_context.h" | |
| 17 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_job.h" | 18 #include "net/url_request/url_request_job.h" |
| 20 #include "net/url_request/url_request_job_factory_impl.h" | 19 #include "net/url_request/url_request_job_factory_impl.h" |
| 21 #include "storage/browser/blob/blob_storage_context.h" | 20 #include "storage/browser/blob/blob_storage_context.h" |
| 22 #include "storage/browser/blob/blob_url_request_job.h" | 21 #include "storage/browser/blob/blob_url_request_job.h" |
| 23 #include "storage/browser/fileapi/file_system_context.h" | 22 #include "storage/browser/fileapi/file_system_context.h" |
| 24 #include "storage/browser/fileapi/file_system_file_util.h" | 23 #include "storage/browser/fileapi/file_system_file_util.h" |
| 25 #include "storage/browser/fileapi/file_system_operation_context.h" | 24 #include "storage/browser/fileapi/file_system_operation_context.h" |
| 26 #include "storage/browser/fileapi/file_system_operation_runner.h" | 25 #include "storage/browser/fileapi/file_system_operation_runner.h" |
| 27 #include "storage/browser/fileapi/local_file_util.h" | 26 #include "storage/browser/fileapi/local_file_util.h" |
| 27 #include "storage/browser/test/mock_blob_url_request_context.h" |
| 28 #include "storage/browser/test/mock_file_change_observer.h" | 28 #include "storage/browser/test/mock_file_change_observer.h" |
| 29 #include "storage/browser/test/mock_quota_manager.h" | 29 #include "storage/browser/test/mock_quota_manager.h" |
| 30 #include "storage/browser/test/test_file_system_backend.h" | 30 #include "storage/browser/test/test_file_system_backend.h" |
| 31 #include "storage/browser/test/test_file_system_context.h" | 31 #include "storage/browser/test/test_file_system_context.h" |
| 32 #include "storage/common/fileapi/file_system_util.h" | 32 #include "storage/common/fileapi/file_system_util.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 35 | 35 |
| 36 using storage::FileSystemOperation; | 36 using storage::FileSystemOperation; |
| 37 using storage::FileSystemOperationRunner; | 37 using storage::FileSystemOperationRunner; |
| 38 using storage::FileSystemURL; | 38 using storage::FileSystemURL; |
| 39 using content::MockBlobURLRequestContext; | 39 using content::MockBlobURLRequestContext; |
| 40 using content::ScopedTextBlob; | 40 using content::ScopedTextBlob; |
| 41 | 41 |
| 42 namespace content { | 42 namespace content { |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 const GURL kOrigin("http://example.com"); | 46 const GURL kOrigin("http://example.com"); |
| 47 const storage::FileSystemType kFileSystemType = storage::kFileSystemTypeTest; | 47 const storage::FileSystemType kFileSystemType = storage::kFileSystemTypeTest; |
| 48 | 48 |
| 49 void AssertStatusEq(base::File::Error expected, | 49 void AssertStatusEq(base::File::Error expected, base::File::Error actual) { |
| 50 base::File::Error actual) { | |
| 51 ASSERT_EQ(expected, actual); | 50 ASSERT_EQ(expected, actual); |
| 52 } | 51 } |
| 53 | 52 |
| 54 } // namespace | 53 } // namespace |
| 55 | 54 |
| 56 class FileSystemOperationImplWriteTest | 55 class FileSystemOperationImplWriteTest : public testing::Test { |
| 57 : public testing::Test { | |
| 58 public: | 56 public: |
| 59 FileSystemOperationImplWriteTest() | 57 FileSystemOperationImplWriteTest() |
| 60 : status_(base::File::FILE_OK), | 58 : status_(base::File::FILE_OK), |
| 61 cancel_status_(base::File::FILE_ERROR_FAILED), | 59 cancel_status_(base::File::FILE_ERROR_FAILED), |
| 62 bytes_written_(0), | 60 bytes_written_(0), |
| 63 complete_(false), | 61 complete_(false), |
| 64 weak_factory_(this) { | 62 weak_factory_(this) { |
| 65 change_observers_ = | 63 change_observers_ = |
| 66 storage::MockFileChangeObserver::CreateList(&change_observer_); | 64 storage::MockFileChangeObserver::CreateList(&change_observer_); |
| 67 } | 65 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } else { | 137 } else { |
| 140 EXPECT_FALSE(complete_); | 138 EXPECT_FALSE(complete_); |
| 141 EXPECT_EQ(status_, base::File::FILE_OK); | 139 EXPECT_EQ(status_, base::File::FILE_OK); |
| 142 complete_ = true; | 140 complete_ = true; |
| 143 status_ = status; | 141 status_ = status; |
| 144 if (base::MessageLoop::current()->is_running()) | 142 if (base::MessageLoop::current()->is_running()) |
| 145 base::MessageLoop::current()->QuitWhenIdle(); | 143 base::MessageLoop::current()->QuitWhenIdle(); |
| 146 } | 144 } |
| 147 } | 145 } |
| 148 | 146 |
| 149 void DidCancel(base::File::Error status) { | 147 void DidCancel(base::File::Error status) { cancel_status_ = status; } |
| 150 cancel_status_ = status; | |
| 151 } | |
| 152 | 148 |
| 153 const MockBlobURLRequestContext& url_request_context() const { | 149 const MockBlobURLRequestContext& url_request_context() const { |
| 154 return *url_request_context_; | 150 return *url_request_context_; |
| 155 } | 151 } |
| 156 | 152 |
| 157 scoped_refptr<storage::FileSystemContext> file_system_context_; | 153 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 158 scoped_refptr<MockQuotaManager> quota_manager_; | 154 scoped_refptr<MockQuotaManager> quota_manager_; |
| 159 | 155 |
| 160 base::MessageLoopForIO loop_; | 156 base::MessageLoopForIO loop_; |
| 161 | 157 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 172 | 168 |
| 173 storage::MockFileChangeObserver change_observer_; | 169 storage::MockFileChangeObserver change_observer_; |
| 174 storage::ChangeObserverList change_observers_; | 170 storage::ChangeObserverList change_observers_; |
| 175 | 171 |
| 176 base::WeakPtrFactory<FileSystemOperationImplWriteTest> weak_factory_; | 172 base::WeakPtrFactory<FileSystemOperationImplWriteTest> weak_factory_; |
| 177 | 173 |
| 178 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationImplWriteTest); | 174 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationImplWriteTest); |
| 179 }; | 175 }; |
| 180 | 176 |
| 181 TEST_F(FileSystemOperationImplWriteTest, TestWriteSuccess) { | 177 TEST_F(FileSystemOperationImplWriteTest, TestWriteSuccess) { |
| 182 ScopedTextBlob blob(url_request_context(), | 178 ScopedTextBlob blob(url_request_context(), "blob-id:success", |
| 183 "blob-id:success", | |
| 184 "Hello, world!\n"); | 179 "Hello, world!\n"); |
| 185 file_system_context_->operation_runner()->Write( | 180 file_system_context_->operation_runner()->Write( |
| 186 &url_request_context(), URLForPath(virtual_path_), | 181 &url_request_context(), URLForPath(virtual_path_), |
| 187 blob.GetBlobDataHandle(), | 182 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
| 188 0, RecordWriteCallback()); | |
| 189 base::RunLoop().Run(); | 183 base::RunLoop().Run(); |
| 190 | 184 |
| 191 EXPECT_EQ(14, bytes_written()); | 185 EXPECT_EQ(14, bytes_written()); |
| 192 EXPECT_EQ(base::File::FILE_OK, status()); | 186 EXPECT_EQ(base::File::FILE_OK, status()); |
| 193 EXPECT_TRUE(complete()); | 187 EXPECT_TRUE(complete()); |
| 194 | 188 |
| 195 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 189 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 196 } | 190 } |
| 197 | 191 |
| 198 TEST_F(FileSystemOperationImplWriteTest, TestWriteZero) { | 192 TEST_F(FileSystemOperationImplWriteTest, TestWriteZero) { |
| 199 ScopedTextBlob blob(url_request_context(), "blob_id:zero", ""); | 193 ScopedTextBlob blob(url_request_context(), "blob_id:zero", ""); |
| 200 file_system_context_->operation_runner()->Write( | 194 file_system_context_->operation_runner()->Write( |
| 201 &url_request_context(), URLForPath(virtual_path_), | 195 &url_request_context(), URLForPath(virtual_path_), |
| 202 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); | 196 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
| 203 base::RunLoop().Run(); | 197 base::RunLoop().Run(); |
| 204 | 198 |
| 205 EXPECT_EQ(0, bytes_written()); | 199 EXPECT_EQ(0, bytes_written()); |
| 206 EXPECT_EQ(base::File::FILE_OK, status()); | 200 EXPECT_EQ(base::File::FILE_OK, status()); |
| 207 EXPECT_TRUE(complete()); | 201 EXPECT_TRUE(complete()); |
| 208 | 202 |
| 209 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 203 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 210 } | 204 } |
| 211 | 205 |
| 212 | |
| 213 TEST_F(FileSystemOperationImplWriteTest, TestWriteInvalidBlobUrl) { | 206 TEST_F(FileSystemOperationImplWriteTest, TestWriteInvalidBlobUrl) { |
| 214 std::unique_ptr<storage::BlobDataHandle> null_handle; | 207 std::unique_ptr<storage::BlobDataHandle> null_handle; |
| 215 file_system_context_->operation_runner()->Write( | 208 file_system_context_->operation_runner()->Write( |
| 216 &url_request_context(), URLForPath(virtual_path_), std::move(null_handle), | 209 &url_request_context(), URLForPath(virtual_path_), std::move(null_handle), |
| 217 0, RecordWriteCallback()); | 210 0, RecordWriteCallback()); |
| 218 base::RunLoop().Run(); | 211 base::RunLoop().Run(); |
| 219 | 212 |
| 220 EXPECT_EQ(0, bytes_written()); | 213 EXPECT_EQ(0, bytes_written()); |
| 221 EXPECT_EQ(base::File::FILE_ERROR_FAILED, status()); | 214 EXPECT_EQ(base::File::FILE_ERROR_FAILED, status()); |
| 222 EXPECT_TRUE(complete()); | 215 EXPECT_TRUE(complete()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 236 EXPECT_EQ(0, bytes_written()); | 229 EXPECT_EQ(0, bytes_written()); |
| 237 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, status()); | 230 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, status()); |
| 238 EXPECT_TRUE(complete()); | 231 EXPECT_TRUE(complete()); |
| 239 | 232 |
| 240 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 233 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 241 } | 234 } |
| 242 | 235 |
| 243 TEST_F(FileSystemOperationImplWriteTest, TestWriteDir) { | 236 TEST_F(FileSystemOperationImplWriteTest, TestWriteDir) { |
| 244 base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); | 237 base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); |
| 245 file_system_context_->operation_runner()->CreateDirectory( | 238 file_system_context_->operation_runner()->CreateDirectory( |
| 246 URLForPath(virtual_dir_path), | 239 URLForPath(virtual_dir_path), true /* exclusive */, false /* recursive */, |
| 247 true /* exclusive */, false /* recursive */, | |
| 248 base::Bind(&AssertStatusEq, base::File::FILE_OK)); | 240 base::Bind(&AssertStatusEq, base::File::FILE_OK)); |
| 249 | 241 |
| 250 ScopedTextBlob blob(url_request_context(), "blob:writedir", | 242 ScopedTextBlob blob(url_request_context(), "blob:writedir", |
| 251 "It\'ll not be written, too."); | 243 "It\'ll not be written, too."); |
| 252 file_system_context_->operation_runner()->Write( | 244 file_system_context_->operation_runner()->Write( |
| 253 &url_request_context(), URLForPath(virtual_dir_path), | 245 &url_request_context(), URLForPath(virtual_dir_path), |
| 254 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); | 246 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
| 255 base::RunLoop().Run(); | 247 base::RunLoop().Run(); |
| 256 | 248 |
| 257 EXPECT_EQ(0, bytes_written()); | 249 EXPECT_EQ(0, bytes_written()); |
| 258 // TODO(kinuko): This error code is platform- or fileutil- dependent | 250 // TODO(kinuko): This error code is platform- or fileutil- dependent |
| 259 // right now. Make it return File::FILE_ERROR_NOT_A_FILE in every case. | 251 // right now. Make it return File::FILE_ERROR_NOT_A_FILE in every case. |
| 260 EXPECT_TRUE(status() == base::File::FILE_ERROR_NOT_A_FILE || | 252 EXPECT_TRUE(status() == base::File::FILE_ERROR_NOT_A_FILE || |
| 261 status() == base::File::FILE_ERROR_ACCESS_DENIED || | 253 status() == base::File::FILE_ERROR_ACCESS_DENIED || |
| 262 status() == base::File::FILE_ERROR_FAILED); | 254 status() == base::File::FILE_ERROR_FAILED); |
| 263 EXPECT_TRUE(complete()); | 255 EXPECT_TRUE(complete()); |
| 264 | 256 |
| 265 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 257 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 266 } | 258 } |
| 267 | 259 |
| 268 TEST_F(FileSystemOperationImplWriteTest, TestWriteFailureByQuota) { | 260 TEST_F(FileSystemOperationImplWriteTest, TestWriteFailureByQuota) { |
| 269 ScopedTextBlob blob(url_request_context(), "blob:success", | 261 ScopedTextBlob blob(url_request_context(), "blob:success", "Hello, world!\n"); |
| 270 "Hello, world!\n"); | |
| 271 quota_manager_->SetQuota( | 262 quota_manager_->SetQuota( |
| 272 kOrigin, FileSystemTypeToQuotaStorageType(kFileSystemType), 10); | 263 kOrigin, FileSystemTypeToQuotaStorageType(kFileSystemType), 10); |
| 273 file_system_context_->operation_runner()->Write( | 264 file_system_context_->operation_runner()->Write( |
| 274 &url_request_context(), URLForPath(virtual_path_), | 265 &url_request_context(), URLForPath(virtual_path_), |
| 275 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); | 266 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
| 276 base::RunLoop().Run(); | 267 base::RunLoop().Run(); |
| 277 | 268 |
| 278 EXPECT_EQ(10, bytes_written()); | 269 EXPECT_EQ(10, bytes_written()); |
| 279 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, status()); | 270 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, status()); |
| 280 EXPECT_TRUE(complete()); | 271 EXPECT_TRUE(complete()); |
| 281 | 272 |
| 282 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 273 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 283 } | 274 } |
| 284 | 275 |
| 285 TEST_F(FileSystemOperationImplWriteTest, TestImmediateCancelSuccessfulWrite) { | 276 TEST_F(FileSystemOperationImplWriteTest, TestImmediateCancelSuccessfulWrite) { |
| 286 ScopedTextBlob blob(url_request_context(), "blob:success", | 277 ScopedTextBlob blob(url_request_context(), "blob:success", "Hello, world!\n"); |
| 287 "Hello, world!\n"); | |
| 288 FileSystemOperationRunner::OperationID id = | 278 FileSystemOperationRunner::OperationID id = |
| 289 file_system_context_->operation_runner()->Write( | 279 file_system_context_->operation_runner()->Write( |
| 290 &url_request_context(), URLForPath(virtual_path_), | 280 &url_request_context(), URLForPath(virtual_path_), |
| 291 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); | 281 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
| 292 file_system_context_->operation_runner()->Cancel(id, RecordCancelCallback()); | 282 file_system_context_->operation_runner()->Cancel(id, RecordCancelCallback()); |
| 293 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 283 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
| 294 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 284 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
| 295 // to run another write cycle. | 285 // to run another write cycle. |
| 296 base::RunLoop().RunUntilIdle(); | 286 base::RunLoop().RunUntilIdle(); |
| 297 | 287 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 325 EXPECT_EQ(base::File::FILE_ERROR_ABORT, status()); | 315 EXPECT_EQ(base::File::FILE_ERROR_ABORT, status()); |
| 326 EXPECT_EQ(base::File::FILE_OK, cancel_status()); | 316 EXPECT_EQ(base::File::FILE_OK, cancel_status()); |
| 327 EXPECT_TRUE(complete()); | 317 EXPECT_TRUE(complete()); |
| 328 | 318 |
| 329 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 319 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
| 330 } | 320 } |
| 331 | 321 |
| 332 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. | 322 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. |
| 333 | 323 |
| 334 } // namespace content | 324 } // namespace content |
| OLD | NEW |