| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 CreateNewOperation(result_.get(), allowed_growth), | 116 CreateNewOperation(result_.get(), allowed_growth), |
| 117 test_helper_.CreatePath(file_path_), | 117 test_helper_.CreatePath(file_path_), |
| 118 offset)); | 118 offset)); |
| 119 request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); | 119 request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); |
| 120 } | 120 } |
| 121 | 121 |
| 122 FileSystemOperation* CreateNewOperation(Result* result, int64 quota); | 122 FileSystemOperation* CreateNewOperation(Result* result, int64 quota); |
| 123 | 123 |
| 124 static net::URLRequest::ProtocolFactory Factory; | 124 static net::URLRequest::ProtocolFactory Factory; |
| 125 | 125 |
| 126 // This should be alive until the very end of this instance. |
| 127 MessageLoop loop_; |
| 128 |
| 126 scoped_ptr<QuotaFileUtil> quota_file_util_; | 129 scoped_ptr<QuotaFileUtil> quota_file_util_; |
| 127 scoped_ptr<FileWriterDelegate> file_writer_delegate_; | 130 scoped_ptr<FileWriterDelegate> file_writer_delegate_; |
| 128 scoped_ptr<net::URLRequest> request_; | 131 scoped_ptr<net::URLRequest> request_; |
| 129 scoped_ptr<Result> result_; | 132 scoped_ptr<Result> result_; |
| 130 FileSystemTestOriginHelper test_helper_; | 133 FileSystemTestOriginHelper test_helper_; |
| 131 | 134 |
| 132 MessageLoop loop_; | |
| 133 ScopedTempDir dir_; | 135 ScopedTempDir dir_; |
| 134 FilePath file_path_; | 136 FilePath file_path_; |
| 135 PlatformFile file_; | 137 PlatformFile file_; |
| 136 | 138 |
| 137 static const char* content_; | 139 static const char* content_; |
| 138 }; | 140 }; |
| 139 const char* FileWriterDelegateTest::content_ = NULL; | 141 const char* FileWriterDelegateTest::content_ = NULL; |
| 140 | 142 |
| 141 namespace { | 143 namespace { |
| 142 | 144 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 return operation; | 224 return operation; |
| 223 } | 225 } |
| 224 | 226 |
| 225 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { | 227 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { |
| 226 const GURL kBlobURL("blob:nolimit"); | 228 const GURL kBlobURL("blob:nolimit"); |
| 227 content_ = kData; | 229 content_ = kData; |
| 228 | 230 |
| 229 PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); | 231 PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); |
| 230 | 232 |
| 231 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 233 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 232 file_writer_delegate_->Start(file_, request_.get()); | 234 file_writer_delegate_->Start(file_, request_.Pass()); |
| 233 MessageLoop::current()->Run(); | 235 MessageLoop::current()->Run(); |
| 234 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 236 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
| 235 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 237 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 236 | 238 |
| 237 EXPECT_EQ(kDataSize, result_->bytes_written()); | 239 EXPECT_EQ(kDataSize, result_->bytes_written()); |
| 238 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 240 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 239 EXPECT_TRUE(result_->complete()); | 241 EXPECT_TRUE(result_->complete()); |
| 240 | 242 |
| 241 file_writer_delegate_.reset(); | 243 file_writer_delegate_.reset(); |
| 242 } | 244 } |
| 243 | 245 |
| 244 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { | 246 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { |
| 245 const GURL kBlobURL("blob:just"); | 247 const GURL kBlobURL("blob:just"); |
| 246 content_ = kData; | 248 content_ = kData; |
| 247 const int64 kAllowedGrowth = kDataSize; | 249 const int64 kAllowedGrowth = kDataSize; |
| 248 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 250 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
| 249 | 251 |
| 250 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 252 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 251 file_writer_delegate_->Start(file_, request_.get()); | 253 file_writer_delegate_->Start(file_, request_.Pass()); |
| 252 MessageLoop::current()->Run(); | 254 MessageLoop::current()->Run(); |
| 253 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 255 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
| 254 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 256 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 255 | 257 |
| 256 file_writer_delegate_.reset(); | 258 file_writer_delegate_.reset(); |
| 257 | 259 |
| 258 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 260 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
| 259 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 261 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 260 EXPECT_TRUE(result_->complete()); | 262 EXPECT_TRUE(result_->complete()); |
| 261 } | 263 } |
| 262 | 264 |
| 263 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { | 265 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { |
| 264 const GURL kBlobURL("blob:failure"); | 266 const GURL kBlobURL("blob:failure"); |
| 265 content_ = kData; | 267 content_ = kData; |
| 266 const int64 kAllowedGrowth = kDataSize - 1; | 268 const int64 kAllowedGrowth = kDataSize - 1; |
| 267 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 269 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
| 268 | 270 |
| 269 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 271 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 270 file_writer_delegate_->Start(file_, request_.get()); | 272 file_writer_delegate_->Start(file_, request_.Pass()); |
| 271 MessageLoop::current()->Run(); | 273 MessageLoop::current()->Run(); |
| 272 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 274 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
| 273 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 275 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 274 | 276 |
| 275 file_writer_delegate_.reset(); | 277 file_writer_delegate_.reset(); |
| 276 | 278 |
| 277 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 279 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
| 278 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); | 280 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); |
| 279 EXPECT_TRUE(result_->complete()); | 281 EXPECT_TRUE(result_->complete()); |
| 280 } | 282 } |
| 281 | 283 |
| 282 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { | 284 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { |
| 283 const GURL kBlobURL("blob:zero"); | 285 const GURL kBlobURL("blob:zero"); |
| 284 content_ = ""; | 286 content_ = ""; |
| 285 int64 kAllowedGrowth = 0; | 287 int64 kAllowedGrowth = 0; |
| 286 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 288 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
| 287 | 289 |
| 288 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 290 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 289 file_writer_delegate_->Start(file_, request_.get()); | 291 file_writer_delegate_->Start(file_, request_.Pass()); |
| 290 MessageLoop::current()->Run(); | 292 MessageLoop::current()->Run(); |
| 291 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 293 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
| 292 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 294 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 293 | 295 |
| 294 file_writer_delegate_.reset(); | 296 file_writer_delegate_.reset(); |
| 295 | 297 |
| 296 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 298 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
| 297 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 299 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 298 EXPECT_TRUE(result_->complete()); | 300 EXPECT_TRUE(result_->complete()); |
| 299 } | 301 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 323 PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); | 325 PrepareForWrite(kBlobURL, 0, QuotaFileUtil::kNoLimit); |
| 324 | 326 |
| 325 // Credate another FileWriterDelegate for concurrent write. | 327 // Credate another FileWriterDelegate for concurrent write. |
| 326 result2.reset(new Result()); | 328 result2.reset(new Result()); |
| 327 file_writer_delegate2.reset(new FileWriterDelegate( | 329 file_writer_delegate2.reset(new FileWriterDelegate( |
| 328 CreateNewOperation(result2.get(), QuotaFileUtil::kNoLimit), | 330 CreateNewOperation(result2.get(), QuotaFileUtil::kNoLimit), |
| 329 test_helper_.CreatePath(file_path2), 0)); | 331 test_helper_.CreatePath(file_path2), 0)); |
| 330 request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get())); | 332 request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get())); |
| 331 | 333 |
| 332 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 334 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 333 file_writer_delegate_->Start(file_, request_.get()); | 335 file_writer_delegate_->Start(file_, request_.Pass()); |
| 334 file_writer_delegate2->Start(file2, request2.get()); | 336 file_writer_delegate2->Start(file2, request2.Pass()); |
| 335 MessageLoop::current()->Run(); | 337 MessageLoop::current()->Run(); |
| 336 if (!result_->complete() || !result2->complete()) | 338 if (!result_->complete() || !result2->complete()) |
| 337 MessageLoop::current()->Run(); | 339 MessageLoop::current()->Run(); |
| 338 | 340 |
| 339 ASSERT_EQ(kDataSize * 2, test_helper_.GetCachedOriginUsage()); | 341 ASSERT_EQ(kDataSize * 2, test_helper_.GetCachedOriginUsage()); |
| 340 base::FlushPlatformFile(file2); | 342 base::FlushPlatformFile(file2); |
| 341 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 343 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 342 | 344 |
| 343 file_writer_delegate_.reset(); | 345 file_writer_delegate_.reset(); |
| 344 | 346 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 356 const GURL kBlobURL("blob:failure-with-updated-quota"); | 358 const GURL kBlobURL("blob:failure-with-updated-quota"); |
| 357 content_ = kData; | 359 content_ = kData; |
| 358 | 360 |
| 359 // Writing kDataSize (=45) bytes data while allowed_growth is 100. | 361 // Writing kDataSize (=45) bytes data while allowed_growth is 100. |
| 360 int64 offset = 0; | 362 int64 offset = 0; |
| 361 int64 allowed_growth = 100; | 363 int64 allowed_growth = 100; |
| 362 ASSERT_LT(kDataSize, allowed_growth); | 364 ASSERT_LT(kDataSize, allowed_growth); |
| 363 PrepareForWrite(kBlobURL, offset, allowed_growth); | 365 PrepareForWrite(kBlobURL, offset, allowed_growth); |
| 364 | 366 |
| 365 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 367 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
| 366 file_writer_delegate_->Start(file_, request_.get()); | 368 file_writer_delegate_->Start(file_, request_.Pass()); |
| 367 MessageLoop::current()->Run(); | 369 MessageLoop::current()->Run(); |
| 368 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 370 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
| 369 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 371 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 370 EXPECT_EQ(kDataSize, result_->bytes_written()); | 372 EXPECT_EQ(kDataSize, result_->bytes_written()); |
| 371 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 373 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 372 EXPECT_TRUE(result_->complete()); | 374 EXPECT_TRUE(result_->complete()); |
| 373 | 375 |
| 374 // Trying to overwrite kDataSize bytes data while allowed_growth is 20. | 376 // Trying to overwrite kDataSize bytes data while allowed_growth is 20. |
| 375 offset = 0; | 377 offset = 0; |
| 376 allowed_growth = 20; | 378 allowed_growth = 20; |
| 377 PrepareForWrite(kBlobURL, offset, allowed_growth); | 379 PrepareForWrite(kBlobURL, offset, allowed_growth); |
| 378 | 380 |
| 379 file_writer_delegate_->Start(file_, request_.get()); | 381 file_writer_delegate_->Start(file_, request_.Pass()); |
| 380 MessageLoop::current()->Run(); | 382 MessageLoop::current()->Run(); |
| 381 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 383 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
| 382 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 384 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 383 EXPECT_EQ(kDataSize, result_->bytes_written()); | 385 EXPECT_EQ(kDataSize, result_->bytes_written()); |
| 384 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 386 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 385 EXPECT_TRUE(result_->complete()); | 387 EXPECT_TRUE(result_->complete()); |
| 386 | 388 |
| 387 // Trying to write kDataSize bytes data from offset 25 while | 389 // Trying to write kDataSize bytes data from offset 25 while |
| 388 // allowed_growth is 55. | 390 // allowed_growth is 55. |
| 389 offset = 25; | 391 offset = 25; |
| 390 allowed_growth = 55; | 392 allowed_growth = 55; |
| 391 PrepareForWrite(kBlobURL, offset, allowed_growth); | 393 PrepareForWrite(kBlobURL, offset, allowed_growth); |
| 392 | 394 |
| 393 file_writer_delegate_->Start(file_, request_.get()); | 395 file_writer_delegate_->Start(file_, request_.Pass()); |
| 394 MessageLoop::current()->Run(); | 396 MessageLoop::current()->Run(); |
| 395 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); | 397 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); |
| 396 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 398 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 397 EXPECT_EQ(kDataSize, result_->bytes_written()); | 399 EXPECT_EQ(kDataSize, result_->bytes_written()); |
| 398 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 400 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 399 EXPECT_TRUE(result_->complete()); | 401 EXPECT_TRUE(result_->complete()); |
| 400 | 402 |
| 401 // Trying to overwrite 45 bytes data while allowed_growth is -20. | 403 // Trying to overwrite 45 bytes data while allowed_growth is -20. |
| 402 offset = 0; | 404 offset = 0; |
| 403 allowed_growth = -20; | 405 allowed_growth = -20; |
| 404 PrepareForWrite(kBlobURL, offset, allowed_growth); | 406 PrepareForWrite(kBlobURL, offset, allowed_growth); |
| 405 | 407 |
| 406 int64 pre_write_usage = ComputeCurrentOriginUsage(); | 408 int64 pre_write_usage = ComputeCurrentOriginUsage(); |
| 407 file_writer_delegate_->Start(file_, request_.get()); | 409 file_writer_delegate_->Start(file_, request_.Pass()); |
| 408 MessageLoop::current()->Run(); | 410 MessageLoop::current()->Run(); |
| 409 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); | 411 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); |
| 410 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 412 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 411 EXPECT_EQ(kDataSize, result_->bytes_written()); | 413 EXPECT_EQ(kDataSize, result_->bytes_written()); |
| 412 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 414 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
| 413 EXPECT_TRUE(result_->complete()); | 415 EXPECT_TRUE(result_->complete()); |
| 414 | 416 |
| 415 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, | 417 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, |
| 416 // while allowed_growth is 10. | 418 // while allowed_growth is 10. |
| 417 const int kOverlap = 20; | 419 const int kOverlap = 20; |
| 418 offset = pre_write_usage - kOverlap; | 420 offset = pre_write_usage - kOverlap; |
| 419 allowed_growth = 10; | 421 allowed_growth = 10; |
| 420 PrepareForWrite(kBlobURL, offset, allowed_growth); | 422 PrepareForWrite(kBlobURL, offset, allowed_growth); |
| 421 | 423 |
| 422 file_writer_delegate_->Start(file_, request_.get()); | 424 file_writer_delegate_->Start(file_, request_.Pass()); |
| 423 MessageLoop::current()->Run(); | 425 MessageLoop::current()->Run(); |
| 424 EXPECT_EQ(pre_write_usage + allowed_growth, | 426 EXPECT_EQ(pre_write_usage + allowed_growth, |
| 425 test_helper_.GetCachedOriginUsage()); | 427 test_helper_.GetCachedOriginUsage()); |
| 426 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 428 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
| 427 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); | 429 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); |
| 428 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); | 430 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); |
| 429 EXPECT_TRUE(result_->complete()); | 431 EXPECT_TRUE(result_->complete()); |
| 430 } | 432 } |
| 431 | 433 |
| 432 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { | 434 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { |
| 433 protected: | 435 protected: |
| 434 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; | 436 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; |
| 435 }; | 437 }; |
| 436 | 438 |
| 437 } // namespace fileapi | 439 } // namespace fileapi |
| OLD | NEW |