| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) | 5 // NOTE: These tests are run as part of "unit_tests" (in chrome/test/unit) |
| 6 // rather than as part of test_shell_tests because they rely on being able | 6 // rather than as part of test_shell_tests because they rely on being able |
| 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses | 7 // to instantiate a MessageLoop of type TYPE_IO. test_shell_tests uses |
| 8 // TYPE_UI, which URLRequest doesn't allow. | 8 // TYPE_UI, which URLRequest doesn't allow. |
| 9 // | 9 // |
| 10 | 10 |
| 11 #include <vector> |
| 12 |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 13 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 14 #include "base/message_loop_proxy.h" | 16 #include "base/message_loop_proxy.h" |
| 15 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
| 16 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 17 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_job.h" | 21 #include "net/url_request/url_request_job.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webkit/blob/blob_data.h" | 23 #include "webkit/blob/blob_data.h" |
| 22 #include "webkit/blob/blob_storage_controller.h" | 24 #include "webkit/blob/blob_storage_controller.h" |
| 23 #include "webkit/blob/blob_url_request_job.h" | 25 #include "webkit/blob/blob_url_request_job.h" |
| 24 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 26 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 25 #include "webkit/fileapi/file_system_context.h" | 27 #include "webkit/fileapi/file_system_context.h" |
| 26 #include "webkit/fileapi/file_system_file_util.h" | 28 #include "webkit/fileapi/file_system_file_util.h" |
| 27 #include "webkit/fileapi/file_system_operation.h" | 29 #include "webkit/fileapi/file_system_operation.h" |
| 28 #include "webkit/fileapi/file_system_path_manager.h" | 30 #include "webkit/fileapi/file_system_path_manager.h" |
| 29 #include "webkit/fileapi/file_system_test_helper.h" | 31 #include "webkit/fileapi/file_system_test_helper.h" |
| 30 #include "webkit/fileapi/file_system_util.h" | 32 #include "webkit/fileapi/file_system_util.h" |
| 31 #include "webkit/fileapi/local_file_util.h" | 33 #include "webkit/fileapi/local_file_util.h" |
| 32 #include "webkit/fileapi/quota_file_util.h" | 34 #include "webkit/fileapi/quota_file_util.h" |
| 33 #include "webkit/quota/quota_manager.h" | 35 #include "webkit/quota/quota_manager.h" |
| 34 | 36 |
| 35 using quota::QuotaManager; | 37 using quota::QuotaManager; |
| 38 using webkit_blob::BlobData; |
| 36 | 39 |
| 37 namespace fileapi { | 40 namespace fileapi { |
| 38 | 41 |
| 39 namespace { | 42 namespace { |
| 40 | 43 |
| 41 class MockQuotaManager : public QuotaManager { | 44 class MockQuotaManager : public QuotaManager { |
| 42 public: | 45 public: |
| 43 MockQuotaManager(const FilePath& base_dir, int64 quota) | 46 MockQuotaManager(const FilePath& base_dir, int64 quota) |
| 44 : QuotaManager(false /* is_incognito */, base_dir, | 47 : QuotaManager(false /* is_incognito */, base_dir, |
| 45 base::MessageLoopProxy::current(), | 48 base::MessageLoopProxy::current(), |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 public: | 127 public: |
| 125 TestURLRequestContext() | 128 TestURLRequestContext() |
| 126 : blob_storage_controller_(new webkit_blob::BlobStorageController) {} | 129 : blob_storage_controller_(new webkit_blob::BlobStorageController) {} |
| 127 | 130 |
| 128 virtual ~TestURLRequestContext() {} | 131 virtual ~TestURLRequestContext() {} |
| 129 | 132 |
| 130 webkit_blob::BlobStorageController* blob_storage_controller() const { | 133 webkit_blob::BlobStorageController* blob_storage_controller() const { |
| 131 return blob_storage_controller_.get(); | 134 return blob_storage_controller_.get(); |
| 132 } | 135 } |
| 133 | 136 |
| 137 void RegisterBlobUrl(const GURL& url, const BlobData* data) { |
| 138 blob_storage_controller_->RegisterUnfinalizedBlobUrl(url); |
| 139 for (std::vector<BlobData::Item>::const_iterator iter = |
| 140 data->items().begin(); |
| 141 iter != data->items().end(); ++iter) { |
| 142 blob_storage_controller_->AppendBlobDataItem(url, *iter); |
| 143 } |
| 144 blob_storage_controller_->FinalizeBlob(url, data->content_type()); |
| 145 } |
| 146 |
| 147 |
| 134 private: | 148 private: |
| 135 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; | 149 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; |
| 136 }; | 150 }; |
| 137 | 151 |
| 138 static net::URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request, | 152 static net::URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request, |
| 139 const std::string& scheme) { | 153 const std::string& scheme) { |
| 140 webkit_blob::BlobStorageController* blob_storage_controller = | 154 webkit_blob::BlobStorageController* blob_storage_controller = |
| 141 static_cast<const TestURLRequestContext*>(request->context())-> | 155 static_cast<const TestURLRequestContext*>(request->context())-> |
| 142 blob_storage_controller(); | 156 blob_storage_controller(); |
| 143 return new webkit_blob::BlobURLRequestJob( | 157 return new webkit_blob::BlobURLRequestJob( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 EXPECT_TRUE(complete()); | 251 EXPECT_TRUE(complete()); |
| 238 } | 252 } |
| 239 | 253 |
| 240 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { | 254 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { |
| 241 GURL blob_url("blob:zero"); | 255 GURL blob_url("blob:zero"); |
| 242 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 256 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 243 blob_data->AppendData(""); | 257 blob_data->AppendData(""); |
| 244 | 258 |
| 245 scoped_refptr<TestURLRequestContext> url_request_context( | 259 scoped_refptr<TestURLRequestContext> url_request_context( |
| 246 new TestURLRequestContext()); | 260 new TestURLRequestContext()); |
| 247 url_request_context->blob_storage_controller()-> | 261 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 248 RegisterBlobUrl(blob_url, blob_data); | |
| 249 | 262 |
| 250 operation()->Write(url_request_context, URLForPath(virtual_path_), | 263 operation()->Write(url_request_context, URLForPath(virtual_path_), |
| 251 blob_url, 0); | 264 blob_url, 0); |
| 252 MessageLoop::current()->Run(); | 265 MessageLoop::current()->Run(); |
| 253 | 266 |
| 254 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 267 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 255 | 268 |
| 256 EXPECT_EQ(0, bytes_written()); | 269 EXPECT_EQ(0, bytes_written()); |
| 257 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 270 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 258 EXPECT_TRUE(complete()); | 271 EXPECT_TRUE(complete()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 271 EXPECT_TRUE(complete()); | 284 EXPECT_TRUE(complete()); |
| 272 } | 285 } |
| 273 | 286 |
| 274 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { | 287 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 275 GURL blob_url("blob:writeinvalidfile"); | 288 GURL blob_url("blob:writeinvalidfile"); |
| 276 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 289 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 277 blob_data->AppendData("It\'ll not be written."); | 290 blob_data->AppendData("It\'ll not be written."); |
| 278 | 291 |
| 279 scoped_refptr<TestURLRequestContext> url_request_context( | 292 scoped_refptr<TestURLRequestContext> url_request_context( |
| 280 new TestURLRequestContext()); | 293 new TestURLRequestContext()); |
| 281 url_request_context->blob_storage_controller()-> | 294 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 282 RegisterBlobUrl(blob_url, blob_data); | |
| 283 | 295 |
| 284 operation()->Write(url_request_context, | 296 operation()->Write(url_request_context, |
| 285 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 297 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 286 blob_url, 0); | 298 blob_url, 0); |
| 287 MessageLoop::current()->Run(); | 299 MessageLoop::current()->Run(); |
| 288 | 300 |
| 289 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 301 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 290 | 302 |
| 291 EXPECT_EQ(0, bytes_written()); | 303 EXPECT_EQ(0, bytes_written()); |
| 292 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 304 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 293 EXPECT_TRUE(complete()); | 305 EXPECT_TRUE(complete()); |
| 294 } | 306 } |
| 295 | 307 |
| 296 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { | 308 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { |
| 297 FilePath subdir; | 309 FilePath subdir; |
| 298 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, | 310 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, |
| 299 FILE_PATH_LITERAL("d"), | 311 FILE_PATH_LITERAL("d"), |
| 300 &subdir)); | 312 &subdir)); |
| 301 FilePath virtual_subdir_path = subdir.BaseName(); | 313 FilePath virtual_subdir_path = subdir.BaseName(); |
| 302 | 314 |
| 303 GURL blob_url("blob:writedir"); | 315 GURL blob_url("blob:writedir"); |
| 304 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 316 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 305 blob_data->AppendData("It\'ll not be written, too."); | 317 blob_data->AppendData("It\'ll not be written, too."); |
| 306 | 318 |
| 307 scoped_refptr<TestURLRequestContext> url_request_context( | 319 scoped_refptr<TestURLRequestContext> url_request_context( |
| 308 new TestURLRequestContext()); | 320 new TestURLRequestContext()); |
| 309 url_request_context->blob_storage_controller()-> | 321 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 310 RegisterBlobUrl(blob_url, blob_data); | |
| 311 | 322 |
| 312 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), | 323 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), |
| 313 blob_url, 0); | 324 blob_url, 0); |
| 314 MessageLoop::current()->Run(); | 325 MessageLoop::current()->Run(); |
| 315 | 326 |
| 316 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 327 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 317 | 328 |
| 318 EXPECT_EQ(0, bytes_written()); | 329 EXPECT_EQ(0, bytes_written()); |
| 319 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); | 330 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); |
| 320 EXPECT_TRUE(complete()); | 331 EXPECT_TRUE(complete()); |
| 321 } | 332 } |
| 322 | 333 |
| 323 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { | 334 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { |
| 324 GURL blob_url("blob:success"); | 335 GURL blob_url("blob:success"); |
| 325 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 336 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 326 blob_data->AppendData("Hello, world!\n"); | 337 blob_data->AppendData("Hello, world!\n"); |
| 327 | 338 |
| 328 scoped_refptr<TestURLRequestContext> url_request_context( | 339 scoped_refptr<TestURLRequestContext> url_request_context( |
| 329 new TestURLRequestContext()); | 340 new TestURLRequestContext()); |
| 330 url_request_context->blob_storage_controller()-> | 341 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 331 RegisterBlobUrl(blob_url, blob_data); | |
| 332 | 342 |
| 333 quota_manager_->set_quota(10); | 343 quota_manager_->set_quota(10); |
| 334 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 344 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, |
| 335 0); | 345 0); |
| 336 MessageLoop::current()->Run(); | 346 MessageLoop::current()->Run(); |
| 337 | 347 |
| 338 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 348 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 339 | 349 |
| 340 EXPECT_EQ(10, bytes_written()); | 350 EXPECT_EQ(10, bytes_written()); |
| 341 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); | 351 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); |
| 342 EXPECT_TRUE(complete()); | 352 EXPECT_TRUE(complete()); |
| 343 } | 353 } |
| 344 | 354 |
| 345 // TODO(ericu,dmikurube): Add tests for Cancel. | 355 // TODO(ericu,dmikurube): Add tests for Cancel. |
| 346 | 356 |
| 347 } // namespace fileapi | 357 } // namespace fileapi |
| OLD | NEW |