| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 void AssertStatusEq(base::PlatformFileError expected, | 39 void AssertStatusEq(base::PlatformFileError expected, |
| 40 base::PlatformFileError actual) { | 40 base::PlatformFileError actual) { |
| 41 ASSERT_EQ(expected, actual); | 41 ASSERT_EQ(expected, actual); |
| 42 } | 42 } |
| 43 | 43 |
| 44 class MockQuotaManager : public QuotaManager { | 44 class MockQuotaManager : public QuotaManager { |
| 45 public: | 45 public: |
| 46 MockQuotaManager(const FilePath& base_dir, int64 quota) | 46 MockQuotaManager(const base::FilePath& base_dir, int64 quota) |
| 47 : QuotaManager(false /* is_incognito */, base_dir, | 47 : QuotaManager(false /* is_incognito */, base_dir, |
| 48 base::MessageLoopProxy::current(), | 48 base::MessageLoopProxy::current(), |
| 49 base::MessageLoopProxy::current(), | 49 base::MessageLoopProxy::current(), |
| 50 NULL /* special_storage_policy */), | 50 NULL /* special_storage_policy */), |
| 51 usage_(0), | 51 usage_(0), |
| 52 quota_(quota) {} | 52 quota_(quota) {} |
| 53 | 53 |
| 54 virtual void GetUsageAndQuota( | 54 virtual void GetUsageAndQuota( |
| 55 const GURL& origin, quota::StorageType type, | 55 const GURL& origin, quota::StorageType type, |
| 56 const GetUsageAndQuotaCallback& callback) OVERRIDE { | 56 const GetUsageAndQuotaCallback& callback) OVERRIDE { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 protected: | 103 protected: |
| 104 const ChangeObserverList& change_observers() const { | 104 const ChangeObserverList& change_observers() const { |
| 105 return change_observers_; | 105 return change_observers_; |
| 106 } | 106 } |
| 107 | 107 |
| 108 MockFileChangeObserver* change_observer() { | 108 MockFileChangeObserver* change_observer() { |
| 109 return &change_observer_; | 109 return &change_observer_; |
| 110 } | 110 } |
| 111 | 111 |
| 112 FileSystemURL URLForPath(const FilePath& path) const { | 112 FileSystemURL URLForPath(const base::FilePath& path) const { |
| 113 return test_helper_.CreateURL(path); | 113 return test_helper_.CreateURL(path); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Callback function for recording test results. | 116 // Callback function for recording test results. |
| 117 FileSystemOperation::WriteCallback RecordWriteCallback() { | 117 FileSystemOperation::WriteCallback RecordWriteCallback() { |
| 118 return base::Bind(&LocalFileSystemOperationWriteTest::DidWrite, | 118 return base::Bind(&LocalFileSystemOperationWriteTest::DidWrite, |
| 119 AsWeakPtr()); | 119 AsWeakPtr()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 FileSystemOperation::StatusCallback RecordCancelCallback() { | 122 FileSystemOperation::StatusCallback RecordCancelCallback() { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 146 FileSystemFileUtil* file_util() { | 146 FileSystemFileUtil* file_util() { |
| 147 return test_helper_.file_util(); | 147 return test_helper_.file_util(); |
| 148 } | 148 } |
| 149 | 149 |
| 150 scoped_refptr<MockQuotaManager> quota_manager_; | 150 scoped_refptr<MockQuotaManager> quota_manager_; |
| 151 LocalFileSystemTestOriginHelper test_helper_; | 151 LocalFileSystemTestOriginHelper test_helper_; |
| 152 | 152 |
| 153 MessageLoop loop_; | 153 MessageLoop loop_; |
| 154 | 154 |
| 155 base::ScopedTempDir dir_; | 155 base::ScopedTempDir dir_; |
| 156 FilePath virtual_path_; | 156 base::FilePath virtual_path_; |
| 157 | 157 |
| 158 // For post-operation status. | 158 // For post-operation status. |
| 159 base::PlatformFileError status_; | 159 base::PlatformFileError status_; |
| 160 base::PlatformFileError cancel_status_; | 160 base::PlatformFileError cancel_status_; |
| 161 int64 bytes_written_; | 161 int64 bytes_written_; |
| 162 bool complete_; | 162 bool complete_; |
| 163 | 163 |
| 164 MockBlobURLRequestContext url_request_context_; | 164 MockBlobURLRequestContext url_request_context_; |
| 165 | 165 |
| 166 private: | 166 private: |
| 167 MockFileChangeObserver change_observer_; | 167 MockFileChangeObserver change_observer_; |
| 168 ChangeObserverList change_observers_; | 168 ChangeObserverList change_observers_; |
| 169 | 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationWriteTest); | 170 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationWriteTest); |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 void LocalFileSystemOperationWriteTest::SetUp() { | 173 void LocalFileSystemOperationWriteTest::SetUp() { |
| 174 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 174 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
| 175 FilePath base_dir = dir_.path().AppendASCII("filesystem"); | 175 base::FilePath base_dir = dir_.path().AppendASCII("filesystem"); |
| 176 | 176 |
| 177 quota_manager_ = new MockQuotaManager(base_dir, 1024); | 177 quota_manager_ = new MockQuotaManager(base_dir, 1024); |
| 178 test_helper_.SetUp(base_dir, | 178 test_helper_.SetUp(base_dir, |
| 179 false /* unlimited quota */, | 179 false /* unlimited quota */, |
| 180 quota_manager_->proxy()); | 180 quota_manager_->proxy()); |
| 181 virtual_path_ = FilePath(FILE_PATH_LITERAL("temporary file")); | 181 virtual_path_ = base::FilePath(FILE_PATH_LITERAL("temporary file")); |
| 182 | 182 |
| 183 operation()->CreateFile( | 183 operation()->CreateFile( |
| 184 URLForPath(virtual_path_), true /* exclusive */, | 184 URLForPath(virtual_path_), true /* exclusive */, |
| 185 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); | 185 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void LocalFileSystemOperationWriteTest::TearDown() { | 188 void LocalFileSystemOperationWriteTest::TearDown() { |
| 189 quota_manager_ = NULL; | 189 quota_manager_ = NULL; |
| 190 test_helper_.TearDown(); | 190 test_helper_.TearDown(); |
| 191 } | 191 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 EXPECT_TRUE(complete()); | 241 EXPECT_TRUE(complete()); |
| 242 | 242 |
| 243 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 243 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
| 244 } | 244 } |
| 245 | 245 |
| 246 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidFile) { | 246 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 247 GURL blob_url("blob:writeinvalidfile"); | 247 GURL blob_url("blob:writeinvalidfile"); |
| 248 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); | 248 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); |
| 249 | 249 |
| 250 operation()->Write(&url_request_context_, | 250 operation()->Write(&url_request_context_, |
| 251 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 251 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 252 blob_url, 0, RecordWriteCallback()); | 252 blob_url, 0, RecordWriteCallback()); |
| 253 MessageLoop::current()->Run(); | 253 MessageLoop::current()->Run(); |
| 254 | 254 |
| 255 EXPECT_EQ(0, bytes_written()); | 255 EXPECT_EQ(0, bytes_written()); |
| 256 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 256 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 257 EXPECT_TRUE(complete()); | 257 EXPECT_TRUE(complete()); |
| 258 | 258 |
| 259 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 259 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
| 260 } | 260 } |
| 261 | 261 |
| 262 TEST_F(LocalFileSystemOperationWriteTest, TestWriteDir) { | 262 TEST_F(LocalFileSystemOperationWriteTest, TestWriteDir) { |
| 263 FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); | 263 base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); |
| 264 operation()->CreateDirectory( | 264 operation()->CreateDirectory( |
| 265 URLForPath(virtual_dir_path), | 265 URLForPath(virtual_dir_path), |
| 266 true /* exclusive */, false /* recursive */, | 266 true /* exclusive */, false /* recursive */, |
| 267 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); | 267 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); |
| 268 | 268 |
| 269 GURL blob_url("blob:writedir"); | 269 GURL blob_url("blob:writedir"); |
| 270 ScopedTextBlob blob(url_request_context_, blob_url, | 270 ScopedTextBlob blob(url_request_context_, blob_url, |
| 271 "It\'ll not be written, too."); | 271 "It\'ll not be written, too."); |
| 272 | 272 |
| 273 operation()->Write(&url_request_context_, URLForPath(virtual_dir_path), | 273 operation()->Write(&url_request_context_, URLForPath(virtual_dir_path), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 323 |
| 324 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 324 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
| 325 } | 325 } |
| 326 | 326 |
| 327 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { | 327 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { |
| 328 GURL blob_url("blob:writeinvalidfile"); | 328 GURL blob_url("blob:writeinvalidfile"); |
| 329 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); | 329 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); |
| 330 | 330 |
| 331 FileSystemOperation* write_operation = operation(); | 331 FileSystemOperation* write_operation = operation(); |
| 332 write_operation->Write(&url_request_context_, | 332 write_operation->Write(&url_request_context_, |
| 333 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 333 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist")
)), |
| 334 blob_url, 0, RecordWriteCallback()); | 334 blob_url, 0, RecordWriteCallback()); |
| 335 write_operation->Cancel(RecordCancelCallback()); | 335 write_operation->Cancel(RecordCancelCallback()); |
| 336 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 336 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
| 337 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 337 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
| 338 // to run another write cycle. | 338 // to run another write cycle. |
| 339 MessageLoop::current()->RunUntilIdle(); | 339 MessageLoop::current()->RunUntilIdle(); |
| 340 | 340 |
| 341 // Issued Cancel() before receiving any response from Write(), | 341 // Issued Cancel() before receiving any response from Write(), |
| 342 // so nothing should have happen. | 342 // so nothing should have happen. |
| 343 EXPECT_EQ(0, bytes_written()); | 343 EXPECT_EQ(0, bytes_written()); |
| 344 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); | 344 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); |
| 345 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); | 345 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); |
| 346 EXPECT_TRUE(complete()); | 346 EXPECT_TRUE(complete()); |
| 347 | 347 |
| 348 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 348 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
| 349 } | 349 } |
| 350 | 350 |
| 351 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. | 351 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. |
| 352 | 352 |
| 353 } // namespace fileapi | 353 } // namespace fileapi |
| OLD | NEW |