| 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 // 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 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationWriteTest); | 147 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationWriteTest); |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 namespace { | 150 namespace { |
| 151 | 151 |
| 152 class TestURLRequestContext : public net::URLRequestContext { | 152 class TestURLRequestContext : public net::URLRequestContext { |
| 153 public: | 153 public: |
| 154 TestURLRequestContext() | 154 TestURLRequestContext() |
| 155 : blob_storage_controller_(new webkit_blob::BlobStorageController) {} | 155 : blob_storage_controller_(new webkit_blob::BlobStorageController) {} |
| 156 | 156 |
| 157 virtual ~TestURLRequestContext() {} |
| 158 |
| 157 webkit_blob::BlobStorageController* blob_storage_controller() const { | 159 webkit_blob::BlobStorageController* blob_storage_controller() const { |
| 158 return blob_storage_controller_.get(); | 160 return blob_storage_controller_.get(); |
| 159 } | 161 } |
| 160 | 162 |
| 161 protected: | |
| 162 virtual ~TestURLRequestContext() {} | |
| 163 | |
| 164 private: | 163 private: |
| 165 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; | 164 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; |
| 166 }; | 165 }; |
| 167 | 166 |
| 168 static net::URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request, | 167 static net::URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request, |
| 169 const std::string& scheme) { | 168 const std::string& scheme) { |
| 170 webkit_blob::BlobStorageController* blob_storage_controller = | 169 webkit_blob::BlobStorageController* blob_storage_controller = |
| 171 static_cast<const TestURLRequestContext*>(request->context())-> | 170 static_cast<const TestURLRequestContext*>(request->context())-> |
| 172 blob_storage_controller(); | 171 blob_storage_controller(); |
| 173 return new webkit_blob::BlobURLRequestJob( | 172 return new webkit_blob::BlobURLRequestJob( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 204 | 203 |
| 205 FileSystemOperation* FileSystemOperationWriteTest::operation() { | 204 FileSystemOperation* FileSystemOperationWriteTest::operation() { |
| 206 return test_helper_.NewOperation(); | 205 return test_helper_.NewOperation(); |
| 207 } | 206 } |
| 208 | 207 |
| 209 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { | 208 TEST_F(FileSystemOperationWriteTest, TestWriteSuccess) { |
| 210 GURL blob_url("blob:success"); | 209 GURL blob_url("blob:success"); |
| 211 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 210 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 212 blob_data->AppendData("Hello, world!\n"); | 211 blob_data->AppendData("Hello, world!\n"); |
| 213 | 212 |
| 214 scoped_refptr<TestURLRequestContext> url_request_context( | 213 TestURLRequestContext url_request_context; |
| 215 new TestURLRequestContext()); | 214 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 216 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 217 blob_url, blob_data); | 215 blob_url, blob_data); |
| 218 | 216 |
| 219 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 217 operation()->Write(&url_request_context, URLForPath(virtual_path_), blob_url, |
| 220 0, RecordWriteCallback()); | 218 0, RecordWriteCallback()); |
| 221 MessageLoop::current()->Run(); | 219 MessageLoop::current()->Run(); |
| 222 | 220 |
| 223 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 221 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 224 | 222 |
| 225 EXPECT_EQ(14, bytes_written()); | 223 EXPECT_EQ(14, bytes_written()); |
| 226 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 224 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 227 EXPECT_TRUE(complete()); | 225 EXPECT_TRUE(complete()); |
| 228 } | 226 } |
| 229 | 227 |
| 230 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { | 228 TEST_F(FileSystemOperationWriteTest, TestWriteZero) { |
| 231 GURL blob_url("blob:zero"); | 229 GURL blob_url("blob:zero"); |
| 232 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 230 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 233 blob_data->AppendData(""); | 231 blob_data->AppendData(""); |
| 234 | 232 |
| 235 scoped_refptr<TestURLRequestContext> url_request_context( | 233 TestURLRequestContext url_request_context; |
| 236 new TestURLRequestContext()); | 234 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 237 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 238 blob_url, blob_data); | 235 blob_url, blob_data); |
| 239 | 236 |
| 240 operation()->Write(url_request_context, URLForPath(virtual_path_), | 237 operation()->Write(&url_request_context, URLForPath(virtual_path_), |
| 241 blob_url, 0, RecordWriteCallback()); | 238 blob_url, 0, RecordWriteCallback()); |
| 242 MessageLoop::current()->Run(); | 239 MessageLoop::current()->Run(); |
| 243 | 240 |
| 244 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 241 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 245 | 242 |
| 246 EXPECT_EQ(0, bytes_written()); | 243 EXPECT_EQ(0, bytes_written()); |
| 247 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 244 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| 248 EXPECT_TRUE(complete()); | 245 EXPECT_TRUE(complete()); |
| 249 } | 246 } |
| 250 | 247 |
| 251 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { | 248 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { |
| 252 scoped_refptr<TestURLRequestContext> url_request_context( | 249 TestURLRequestContext url_request_context; |
| 253 new TestURLRequestContext()); | |
| 254 | 250 |
| 255 operation()->Write(url_request_context, URLForPath(virtual_path_), | 251 operation()->Write(&url_request_context, URLForPath(virtual_path_), |
| 256 GURL("blob:invalid"), 0, RecordWriteCallback()); | 252 GURL("blob:invalid"), 0, RecordWriteCallback()); |
| 257 MessageLoop::current()->Run(); | 253 MessageLoop::current()->Run(); |
| 258 | 254 |
| 259 EXPECT_EQ(0, bytes_written()); | 255 EXPECT_EQ(0, bytes_written()); |
| 260 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status()); | 256 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status()); |
| 261 EXPECT_TRUE(complete()); | 257 EXPECT_TRUE(complete()); |
| 262 } | 258 } |
| 263 | 259 |
| 264 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { | 260 TEST_F(FileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 265 GURL blob_url("blob:writeinvalidfile"); | 261 GURL blob_url("blob:writeinvalidfile"); |
| 266 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 262 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 267 blob_data->AppendData("It\'ll not be written."); | 263 blob_data->AppendData("It\'ll not be written."); |
| 268 | 264 |
| 269 scoped_refptr<TestURLRequestContext> url_request_context( | 265 TestURLRequestContext url_request_context; |
| 270 new TestURLRequestContext()); | 266 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 271 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 272 blob_url, blob_data); | 267 blob_url, blob_data); |
| 273 | 268 |
| 274 operation()->Write(url_request_context, | 269 operation()->Write(&url_request_context, |
| 275 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 270 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 276 blob_url, 0, RecordWriteCallback()); | 271 blob_url, 0, RecordWriteCallback()); |
| 277 MessageLoop::current()->Run(); | 272 MessageLoop::current()->Run(); |
| 278 | 273 |
| 279 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 274 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 280 | 275 |
| 281 EXPECT_EQ(0, bytes_written()); | 276 EXPECT_EQ(0, bytes_written()); |
| 282 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 277 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 283 EXPECT_TRUE(complete()); | 278 EXPECT_TRUE(complete()); |
| 284 } | 279 } |
| 285 | 280 |
| 286 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { | 281 TEST_F(FileSystemOperationWriteTest, TestWriteDir) { |
| 287 FilePath subdir; | 282 FilePath subdir; |
| 288 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, | 283 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(filesystem_dir_, |
| 289 FILE_PATH_LITERAL("d"), | 284 FILE_PATH_LITERAL("d"), |
| 290 &subdir)); | 285 &subdir)); |
| 291 FilePath virtual_subdir_path = subdir.BaseName(); | 286 FilePath virtual_subdir_path = subdir.BaseName(); |
| 292 | 287 |
| 293 GURL blob_url("blob:writedir"); | 288 GURL blob_url("blob:writedir"); |
| 294 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 289 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 295 blob_data->AppendData("It\'ll not be written, too."); | 290 blob_data->AppendData("It\'ll not be written, too."); |
| 296 | 291 |
| 297 scoped_refptr<TestURLRequestContext> url_request_context( | 292 TestURLRequestContext url_request_context; |
| 298 new TestURLRequestContext()); | 293 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 299 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 300 blob_url, blob_data); | 294 blob_url, blob_data); |
| 301 | 295 |
| 302 operation()->Write(url_request_context, URLForPath(virtual_subdir_path), | 296 operation()->Write(&url_request_context, URLForPath(virtual_subdir_path), |
| 303 blob_url, 0, RecordWriteCallback()); | 297 blob_url, 0, RecordWriteCallback()); |
| 304 MessageLoop::current()->Run(); | 298 MessageLoop::current()->Run(); |
| 305 | 299 |
| 306 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 300 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 307 | 301 |
| 308 EXPECT_EQ(0, bytes_written()); | 302 EXPECT_EQ(0, bytes_written()); |
| 309 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); | 303 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ACCESS_DENIED, status()); |
| 310 EXPECT_TRUE(complete()); | 304 EXPECT_TRUE(complete()); |
| 311 } | 305 } |
| 312 | 306 |
| 313 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { | 307 TEST_F(FileSystemOperationWriteTest, TestWriteFailureByQuota) { |
| 314 GURL blob_url("blob:success"); | 308 GURL blob_url("blob:success"); |
| 315 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 309 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 316 blob_data->AppendData("Hello, world!\n"); | 310 blob_data->AppendData("Hello, world!\n"); |
| 317 | 311 |
| 318 scoped_refptr<TestURLRequestContext> url_request_context( | 312 TestURLRequestContext url_request_context; |
| 319 new TestURLRequestContext()); | 313 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 320 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 321 blob_url, blob_data); | 314 blob_url, blob_data); |
| 322 | 315 |
| 323 quota_manager_->set_quota(10); | 316 quota_manager_->set_quota(10); |
| 324 operation()->Write(url_request_context, URLForPath(virtual_path_), blob_url, | 317 operation()->Write(&url_request_context, URLForPath(virtual_path_), blob_url, |
| 325 0, RecordWriteCallback()); | 318 0, RecordWriteCallback()); |
| 326 MessageLoop::current()->Run(); | 319 MessageLoop::current()->Run(); |
| 327 | 320 |
| 328 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 321 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 329 | 322 |
| 330 EXPECT_EQ(10, bytes_written()); | 323 EXPECT_EQ(10, bytes_written()); |
| 331 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); | 324 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); |
| 332 EXPECT_TRUE(complete()); | 325 EXPECT_TRUE(complete()); |
| 333 } | 326 } |
| 334 | 327 |
| 335 TEST_F(FileSystemOperationWriteTest, TestImmediateCancelSuccessfulWrite) { | 328 TEST_F(FileSystemOperationWriteTest, TestImmediateCancelSuccessfulWrite) { |
| 336 GURL blob_url("blob:success"); | 329 GURL blob_url("blob:success"); |
| 337 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 330 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 338 blob_data->AppendData("Hello, world!\n"); | 331 blob_data->AppendData("Hello, world!\n"); |
| 339 | 332 |
| 340 scoped_refptr<TestURLRequestContext> url_request_context( | 333 TestURLRequestContext url_request_context; |
| 341 new TestURLRequestContext()); | 334 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 342 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 343 blob_url, blob_data); | 335 blob_url, blob_data); |
| 344 | 336 |
| 345 FileSystemOperationInterface* write_operation = operation(); | 337 FileSystemOperationInterface* write_operation = operation(); |
| 346 write_operation->Write(url_request_context, URLForPath(virtual_path_), | 338 write_operation->Write(&url_request_context, URLForPath(virtual_path_), |
| 347 blob_url, 0, RecordWriteCallback()); | 339 blob_url, 0, RecordWriteCallback()); |
| 348 write_operation->Cancel(RecordCancelCallback()); | 340 write_operation->Cancel(RecordCancelCallback()); |
| 349 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 341 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
| 350 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 342 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
| 351 // to run another write cycle. | 343 // to run another write cycle. |
| 352 MessageLoop::current()->RunAllPending(); | 344 MessageLoop::current()->RunAllPending(); |
| 353 | 345 |
| 354 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 346 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 355 | 347 |
| 356 // Issued Cancel() before receiving any response from Write(), | 348 // Issued Cancel() before receiving any response from Write(), |
| 357 // so nothing should have happen. | 349 // so nothing should have happen. |
| 358 EXPECT_EQ(0, bytes_written()); | 350 EXPECT_EQ(0, bytes_written()); |
| 359 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); | 351 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); |
| 360 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); | 352 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); |
| 361 EXPECT_TRUE(complete()); | 353 EXPECT_TRUE(complete()); |
| 362 } | 354 } |
| 363 | 355 |
| 364 TEST_F(FileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { | 356 TEST_F(FileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { |
| 365 GURL blob_url("blob:writeinvalidfile"); | 357 GURL blob_url("blob:writeinvalidfile"); |
| 366 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | 358 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); |
| 367 blob_data->AppendData("It\'ll not be written."); | 359 blob_data->AppendData("It\'ll not be written."); |
| 368 | 360 |
| 369 scoped_refptr<TestURLRequestContext> url_request_context( | 361 TestURLRequestContext url_request_context; |
| 370 new TestURLRequestContext()); | 362 url_request_context.blob_storage_controller()->AddFinishedBlob( |
| 371 url_request_context->blob_storage_controller()->AddFinishedBlob( | |
| 372 blob_url, blob_data); | 363 blob_url, blob_data); |
| 373 | 364 |
| 374 FileSystemOperationInterface* write_operation = operation(); | 365 FileSystemOperationInterface* write_operation = operation(); |
| 375 write_operation->Write(url_request_context, | 366 write_operation->Write(&url_request_context, |
| 376 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), | 367 URLForPath(FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 377 blob_url, 0, RecordWriteCallback()); | 368 blob_url, 0, RecordWriteCallback()); |
| 378 write_operation->Cancel(RecordCancelCallback()); | 369 write_operation->Cancel(RecordCancelCallback()); |
| 379 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 370 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
| 380 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 371 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
| 381 // to run another write cycle. | 372 // to run another write cycle. |
| 382 MessageLoop::current()->RunAllPending(); | 373 MessageLoop::current()->RunAllPending(); |
| 383 | 374 |
| 384 url_request_context->blob_storage_controller()->RemoveBlob(blob_url); | 375 url_request_context.blob_storage_controller()->RemoveBlob(blob_url); |
| 385 | 376 |
| 386 // Issued Cancel() before receiving any response from Write(), | 377 // Issued Cancel() before receiving any response from Write(), |
| 387 // so nothing should have happen. | 378 // so nothing should have happen. |
| 388 EXPECT_EQ(0, bytes_written()); | 379 EXPECT_EQ(0, bytes_written()); |
| 389 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); | 380 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); |
| 390 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); | 381 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); |
| 391 EXPECT_TRUE(complete()); | 382 EXPECT_TRUE(complete()); |
| 392 } | 383 } |
| 393 | 384 |
| 394 // TODO(ericu,dmikurube): Add more tests for Cancel. | 385 // TODO(ericu,dmikurube): Add more tests for Cancel. |
| 395 | 386 |
| 396 } // namespace fileapi | 387 } // namespace fileapi |
| OLD | NEW |