| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 return test_helper_.NewOperation(new MockDispatcher(this)); | 230 return test_helper_.NewOperation(new MockDispatcher(this)); |
| 217 } | 231 } |
| 218 | 232 |
| 219 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { | 233 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { |
| 220 GURL blob_url("blob:success"); | 234 GURL blob_url("blob:success"); |
| 221 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 235 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 222 blob_data->AppendData("Hello, world!\n"); | 236 blob_data->AppendData("Hello, world!\n"); |
| 223 | 237 |
| 224 scoped_refptr<TestURLRequestContext> url_request_context( | 238 scoped_refptr<TestURLRequestContext> url_request_context( |
| 225 new TestURLRequestContext()); | 239 new TestURLRequestContext()); |
| 226 url_request_context->blob_storage_controller()-> | 240 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 227 RegisterBlobUrl(blob_url, blob_data); | |
| 228 | 241 |
| 229 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 242 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, |
| 230 0); | 243 0); |
| 231 MessageLoop::current()->Run(); | 244 MessageLoop::current()->Run(); |
| 232 | 245 |
| 233 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 246 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 234 | 247 |
| 235 EXPECT_EQ(14, bytes_written()); | 248 EXPECT_EQ(14, bytes_written()); |
| 236 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 249 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 237 EXPECT_TRUE(complete()); | 250 EXPECT_TRUE(complete()); |
| 238 } | 251 } |
| 239 | 252 |
| 240 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { | 253 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { |
| 241 GURL blob_url("blob:zero"); | 254 GURL blob_url("blob:zero"); |
| 242 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 255 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 243 blob_data->AppendData(""); | 256 blob_data->AppendData(""); |
| 244 | 257 |
| 245 scoped_refptr<TestURLRequestContext> url_request_context( | 258 scoped_refptr<TestURLRequestContext> url_request_context( |
| 246 new TestURLRequestContext()); | 259 new TestURLRequestContext()); |
| 247 url_request_context->blob_storage_controller()-> | 260 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 248 RegisterBlobUrl(blob_url, blob_data); | |
| 249 | 261 |
| 250 operation()->Write(url_request_context, URLForPath(virtual_path_), | 262 operation()->Write(url_request_context, URLForPath(virtual_path_), |
| 251 blob_url, 0); | 263 blob_url, 0); |
| 252 MessageLoop::current()->Run(); | 264 MessageLoop::current()->Run(); |
| 253 | 265 |
| 254 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 266 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 255 | 267 |
| 256 EXPECT_EQ(0, bytes_written()); | 268 EXPECT_EQ(0, bytes_written()); |
| 257 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 269 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 258 EXPECT_TRUE(complete()); | 270 EXPECT_TRUE(complete()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 271 EXPECT_TRUE(complete()); | 283 EXPECT_TRUE(complete()); |
| 272 } | 284 } |
| 273 | 285 |
| 274 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { | 286 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 275 GURL blob_url("blob:writeinvalidfile"); | 287 GURL blob_url("blob:writeinvalidfile"); |
| 276 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 288 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 277 blob_data->AppendData("It\'ll not be written."); | 289 blob_data->AppendData("It\'ll not be written."); |
| 278 | 290 |
| 279 scoped_refptr<TestURLRequestContext> url_request_context( | 291 scoped_refptr<TestURLRequestContext> url_request_context( |
| 280 new TestURLRequestContext()); | 292 new TestURLRequestContext()); |
| 281 url_request_context->blob_storage_controller()-> | 293 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 282 RegisterBlobUrl(blob_url, blob_data); | |
| 283 | 294 |
| 284 operation()->Write(url_request_context, | 295 operation()->Write(url_request_context, |
| 285 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 296 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 286 blob_url, 0); | 297 blob_url, 0); |
| 287 MessageLoop::current()->Run(); | 298 MessageLoop::current()->Run(); |
| 288 | 299 |
| 289 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 300 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 290 | 301 |
| 291 EXPECT_EQ(0, bytes_written()); | 302 EXPECT_EQ(0, bytes_written()); |
| 292 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 303 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 293 EXPECT_TRUE(complete()); | 304 EXPECT_TRUE(complete()); |
| 294 } | 305 } |
| 295 | 306 |
| 296 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { | 307 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { |
| 297 FilePath subdir; | 308 FilePath subdir; |
| 298 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, | 309 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, |
| 299 FILE_PATH_LITERAL("d"), | 310 FILE_PATH_LITERAL("d"), |
| 300 &subdir)); | 311 &subdir)); |
| 301 FilePath virtual_subdir_path = subdir.BaseName(); | 312 FilePath virtual_subdir_path = subdir.BaseName(); |
| 302 | 313 |
| 303 GURL blob_url("blob:writedir"); | 314 GURL blob_url("blob:writedir"); |
| 304 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 315 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 305 blob_data->AppendData("It\'ll not be written, too."); | 316 blob_data->AppendData("It\'ll not be written, too."); |
| 306 | 317 |
| 307 scoped_refptr<TestURLRequestContext> url_request_context( | 318 scoped_refptr<TestURLRequestContext> url_request_context( |
| 308 new TestURLRequestContext()); | 319 new TestURLRequestContext()); |
| 309 url_request_context->blob_storage_controller()-> | 320 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 310 RegisterBlobUrl(blob_url, blob_data); | |
| 311 | 321 |
| 312 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), | 322 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), |
| 313 blob_url, 0); | 323 blob_url, 0); |
| 314 MessageLoop::current()->Run(); | 324 MessageLoop::current()->Run(); |
| 315 | 325 |
| 316 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 326 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 317 | 327 |
| 318 EXPECT_EQ(0, bytes_written()); | 328 EXPECT_EQ(0, bytes_written()); |
| 319 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); | 329 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); |
| 320 EXPECT_TRUE(complete()); | 330 EXPECT_TRUE(complete()); |
| 321 } | 331 } |
| 322 | 332 |
| 323 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { | 333 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { |
| 324 GURL blob_url("blob:success"); | 334 GURL blob_url("blob:success"); |
| 325 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 335 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 326 blob_data->AppendData("Hello, world!\n"); | 336 blob_data->AppendData("Hello, world!\n"); |
| 327 | 337 |
| 328 scoped_refptr<TestURLRequestContext> url_request_context( | 338 scoped_refptr<TestURLRequestContext> url_request_context( |
| 329 new TestURLRequestContext()); | 339 new TestURLRequestContext()); |
| 330 url_request_context->blob_storage_controller()-> | 340 url_request_context->RegisterBlobUrl(blob_url, blob_data); |
| 331 RegisterBlobUrl(blob_url, blob_data); | |
| 332 | 341 |
| 333 quota_manager_->set_quota(10); | 342 quota_manager_->set_quota(10); |
| 334 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 343 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, |
| 335 0); | 344 0); |
| 336 MessageLoop::current()->Run(); | 345 MessageLoop::current()->Run(); |
| 337 | 346 |
| 338 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); | 347 url_request_context->blob_storage_controller()->UnregisterBlobUrl(blob_url); |
| 339 | 348 |
| 340 EXPECT_EQ(10, bytes_written()); | 349 EXPECT_EQ(10, bytes_written()); |
| 341 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); | 350 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); |
| 342 EXPECT_TRUE(complete()); | 351 EXPECT_TRUE(complete()); |
| 343 } | 352 } |
| 344 | 353 |
| 345 // TODO(ericu,dmikurube): Add tests for Cancel. | 354 // TODO(ericu,dmikurube): Add tests for Cancel. |
| 346 | 355 |
| 347 } // namespace fileapi | 356 } // namespace fileapi |
| OLD | NEW |