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 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/file_util_proxy.h" | |
17 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
18 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
20 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
21 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
22 #include "net/url_request/url_request_job.h" | 21 #include "net/url_request/url_request_job.h" |
23 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
24 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
25 #include "webkit/fileapi/file_system_context.h" | 24 #include "webkit/fileapi/file_system_context.h" |
26 #include "webkit/fileapi/file_system_operation.h" | 25 #include "webkit/fileapi/file_system_operation.h" |
27 #include "webkit/fileapi/file_system_operation_context.h" | |
28 #include "webkit/fileapi/file_system_test_helper.h" | 26 #include "webkit/fileapi/file_system_test_helper.h" |
29 #include "webkit/fileapi/file_system_usage_cache.h" | |
30 #include "webkit/fileapi/file_writer_delegate.h" | 27 #include "webkit/fileapi/file_writer_delegate.h" |
31 #include "webkit/quota/quota_manager.h" | 28 #include "webkit/fileapi/sandbox_file_writer.h" |
32 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
33 | 29 |
34 namespace fileapi { | 30 namespace fileapi { |
35 | 31 |
36 namespace { | 32 namespace { |
37 | 33 |
38 class Result { | 34 class Result { |
39 public: | 35 public: |
40 Result() | 36 Result() |
41 : status_(base::PLATFORM_FILE_OK), | 37 : status_(base::PLATFORM_FILE_OK), |
42 bytes_written_(0), | 38 bytes_written_(0), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 70 |
75 const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; | 71 const char kData[] = "The quick brown fox jumps over the lazy dog.\n"; |
76 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; | 72 const int kDataSize = ARRAYSIZE_UNSAFE(kData) - 1; |
77 | 73 |
78 } // namespace (anonymous) | 74 } // namespace (anonymous) |
79 | 75 |
80 class FileWriterDelegateTest : public PlatformTest { | 76 class FileWriterDelegateTest : public PlatformTest { |
81 public: | 77 public: |
82 FileWriterDelegateTest() | 78 FileWriterDelegateTest() |
83 : loop_(MessageLoop::TYPE_IO), | 79 : loop_(MessageLoop::TYPE_IO), |
84 test_helper_(GURL("http://example.com"), kFileSystemTypeTest), | 80 test_helper_(GURL("http://example.com"), kFileSystemTypeTest) {} |
85 file_(base::kInvalidPlatformFileValue) {} | |
86 | 81 |
87 protected: | 82 protected: |
88 virtual void SetUp(); | 83 virtual void SetUp() OVERRIDE; |
89 virtual void TearDown(); | 84 virtual void TearDown() OVERRIDE; |
90 | 85 |
91 virtual void SetUpTestHelper(const FilePath& base_dir) { | 86 FileSystemFileUtil* file_util() { |
92 test_helper_.SetUp(base_dir, NULL); | 87 return test_helper_.file_util(); |
93 } | 88 } |
94 | 89 |
95 int64 ComputeCurrentOriginUsage() { | 90 int64 ComputeCurrentOriginUsage() { |
96 base::FlushPlatformFile(file_); | |
97 return test_helper_.ComputeCurrentOriginUsage(); | 91 return test_helper_.ComputeCurrentOriginUsage(); |
98 } | 92 } |
99 | 93 |
100 // Creates and sets up a FileWriterDelegate for writing the given |blob_url| | 94 FileSystemPath GetFileSystemPath(const char* file_name) const { |
101 // to a file (file_) from |offset| with |allowed_growth| quota setting. | 95 return test_helper_.CreatePathFromUTF8(file_name); |
| 96 } |
| 97 |
| 98 GURL GetFileSystemURL(const char* file_name) const { |
| 99 return test_helper_.GetURLForPath(FilePath().AppendASCII(file_name)); |
| 100 } |
| 101 |
| 102 FileWriterDelegate* CreateWriterDelegate( |
| 103 const char* test_file_path, |
| 104 int64 offset, |
| 105 int64 allowed_growth, |
| 106 Result* result) { |
| 107 SandboxFileWriter* writer = new SandboxFileWriter( |
| 108 test_helper_.file_system_context(), |
| 109 GetFileSystemURL(test_file_path), |
| 110 offset); |
| 111 writer->set_default_quota(allowed_growth); |
| 112 return new FileWriterDelegate( |
| 113 CreateNewOperation(result), |
| 114 scoped_ptr<FileWriter>(writer)); |
| 115 } |
| 116 |
| 117 // Creates and sets up a FileWriterDelegate for writing the given |blob_url|, |
| 118 // and creates a new FileWriterDelegate for the file. |
102 void PrepareForWrite(const GURL& blob_url, | 119 void PrepareForWrite(const GURL& blob_url, |
103 int64 offset, | 120 int64 offset, |
104 int64 allowed_growth) { | 121 int64 allowed_growth) { |
105 bool created; | |
106 base::PlatformFileError error_code; | |
107 file_ = base::CreatePlatformFile( | |
108 file_path_, | |
109 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
110 base::PLATFORM_FILE_ASYNC, | |
111 &created, &error_code); | |
112 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | |
113 | |
114 result_.reset(new Result()); | 122 result_.reset(new Result()); |
115 file_writer_delegate_.reset(new FileWriterDelegate( | 123 file_writer_delegate_.reset( |
116 CreateNewOperation(result_.get(), allowed_growth), | 124 CreateWriterDelegate("test", offset, allowed_growth, result_.get())); |
117 test_helper_.CreatePath(file_path_), | |
118 offset)); | |
119 request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); | 125 request_.reset(new net::URLRequest(blob_url, file_writer_delegate_.get())); |
120 } | 126 } |
121 | 127 |
122 FileSystemOperation* CreateNewOperation(Result* result, int64 quota); | 128 FileSystemOperation* CreateNewOperation(Result* result) { |
| 129 FileSystemOperation* operation = test_helper_.NewOperation(); |
| 130 operation->set_write_callback(base::Bind(&Result::DidWrite, |
| 131 base::Unretained(result))); |
| 132 return operation; |
| 133 } |
123 | 134 |
124 static net::URLRequest::ProtocolFactory Factory; | 135 static net::URLRequest::ProtocolFactory Factory; |
125 | 136 |
126 // This should be alive until the very end of this instance. | 137 // This should be alive until the very end of this instance. |
127 MessageLoop loop_; | 138 MessageLoop loop_; |
128 | 139 |
129 scoped_ptr<FileWriterDelegate> file_writer_delegate_; | 140 scoped_ptr<FileWriterDelegate> file_writer_delegate_; |
130 scoped_ptr<net::URLRequest> request_; | 141 scoped_ptr<net::URLRequest> request_; |
131 scoped_ptr<Result> result_; | 142 scoped_ptr<Result> result_; |
132 FileSystemTestOriginHelper test_helper_; | 143 FileSystemTestOriginHelper test_helper_; |
133 | 144 |
134 ScopedTempDir dir_; | 145 ScopedTempDir dir_; |
135 FilePath file_path_; | |
136 PlatformFile file_; | |
137 | 146 |
138 static const char* content_; | 147 static const char* content_; |
139 }; | 148 }; |
| 149 |
140 const char* FileWriterDelegateTest::content_ = NULL; | 150 const char* FileWriterDelegateTest::content_ = NULL; |
141 | 151 |
142 namespace { | 152 namespace { |
143 | 153 |
144 static std::string g_content; | 154 static std::string g_content; |
145 | 155 |
146 class FileWriterDelegateTestJob : public net::URLRequestJob { | 156 class FileWriterDelegateTestJob : public net::URLRequestJob { |
147 public: | 157 public: |
148 FileWriterDelegateTestJob(net::URLRequest* request, | 158 FileWriterDelegateTestJob(net::URLRequest* request, |
149 const std::string& content) | 159 const std::string& content) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 net::URLRequestJob* FileWriterDelegateTest::Factory( | 203 net::URLRequestJob* FileWriterDelegateTest::Factory( |
194 net::URLRequest* request, | 204 net::URLRequest* request, |
195 const std::string& scheme) { | 205 const std::string& scheme) { |
196 return new FileWriterDelegateTestJob( | 206 return new FileWriterDelegateTestJob( |
197 request, FileWriterDelegateTest::content_); | 207 request, FileWriterDelegateTest::content_); |
198 } | 208 } |
199 | 209 |
200 void FileWriterDelegateTest::SetUp() { | 210 void FileWriterDelegateTest::SetUp() { |
201 ASSERT_TRUE(dir_.CreateUniqueTempDir()); | 211 ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
202 FilePath base_dir = dir_.path().AppendASCII("filesystem"); | 212 FilePath base_dir = dir_.path().AppendASCII("filesystem"); |
203 SetUpTestHelper(base_dir); | 213 test_helper_.SetUp(base_dir, NULL); |
204 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( | 214 |
205 test_helper_.GetOriginRootPath(), &file_path_)); | 215 scoped_ptr<FileSystemOperationContext> context( |
| 216 test_helper_.NewOperationContext()); |
| 217 context->set_allowed_bytes_growth(kint64max); |
| 218 bool created = false; |
| 219 base::PlatformFileError error = file_util()->EnsureFileExists( |
| 220 context.get(), |
| 221 GetFileSystemPath("test"), |
| 222 &created); |
| 223 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 224 ASSERT_TRUE(created); |
206 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); | 225 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); |
207 } | 226 } |
208 | 227 |
209 void FileWriterDelegateTest::TearDown() { | 228 void FileWriterDelegateTest::TearDown() { |
210 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); | 229 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); |
211 base::ClosePlatformFile(file_); | |
212 test_helper_.TearDown(); | 230 test_helper_.TearDown(); |
213 } | 231 } |
214 | 232 |
215 FileSystemOperation* FileWriterDelegateTest::CreateNewOperation( | |
216 Result* result, int64 quota) { | |
217 FileSystemOperation* operation = test_helper_.NewOperation(); | |
218 operation->set_write_callback(base::Bind(&Result::DidWrite, | |
219 base::Unretained(result))); | |
220 FileSystemOperationContext* context = | |
221 operation->file_system_operation_context(); | |
222 context->set_allowed_bytes_growth(quota); | |
223 return operation; | |
224 } | |
225 | |
226 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { | 233 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) { |
227 const GURL kBlobURL("blob:nolimit"); | 234 const GURL kBlobURL("blob:nolimit"); |
228 content_ = kData; | 235 content_ = kData; |
229 | 236 |
230 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); | 237 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); |
231 | 238 |
232 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 239 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
233 file_writer_delegate_->Start(file_, request_.Pass()); | 240 file_writer_delegate_->Start(request_.Pass()); |
234 MessageLoop::current()->Run(); | 241 MessageLoop::current()->Run(); |
235 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 242 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
236 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 243 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
237 | 244 |
238 EXPECT_EQ(kDataSize, result_->bytes_written()); | 245 EXPECT_EQ(kDataSize, result_->bytes_written()); |
239 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 246 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
240 EXPECT_TRUE(result_->complete()); | 247 EXPECT_TRUE(result_->complete()); |
241 | 248 |
242 file_writer_delegate_.reset(); | 249 file_writer_delegate_.reset(); |
243 } | 250 } |
244 | 251 |
245 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { | 252 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { |
246 const GURL kBlobURL("blob:just"); | 253 const GURL kBlobURL("blob:just"); |
247 content_ = kData; | 254 content_ = kData; |
248 const int64 kAllowedGrowth = kDataSize; | 255 const int64 kAllowedGrowth = kDataSize; |
249 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 256 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
250 | 257 |
251 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 258 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
252 file_writer_delegate_->Start(file_, request_.Pass()); | 259 file_writer_delegate_->Start(request_.Pass()); |
253 MessageLoop::current()->Run(); | 260 MessageLoop::current()->Run(); |
254 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 261 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
255 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 262 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
256 | 263 |
257 file_writer_delegate_.reset(); | 264 file_writer_delegate_.reset(); |
258 | 265 |
259 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 266 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
260 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 267 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
261 EXPECT_TRUE(result_->complete()); | 268 EXPECT_TRUE(result_->complete()); |
262 } | 269 } |
263 | 270 |
264 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { | 271 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { |
265 const GURL kBlobURL("blob:failure"); | 272 const GURL kBlobURL("blob:failure"); |
266 content_ = kData; | 273 content_ = kData; |
267 const int64 kAllowedGrowth = kDataSize - 1; | 274 const int64 kAllowedGrowth = kDataSize - 1; |
268 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 275 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
269 | 276 |
270 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 277 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
271 file_writer_delegate_->Start(file_, request_.Pass()); | 278 file_writer_delegate_->Start(request_.Pass()); |
272 MessageLoop::current()->Run(); | 279 MessageLoop::current()->Run(); |
273 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 280 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
274 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 281 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
275 | 282 |
276 file_writer_delegate_.reset(); | 283 file_writer_delegate_.reset(); |
277 | 284 |
278 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 285 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
279 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); | 286 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); |
280 EXPECT_TRUE(result_->complete()); | 287 EXPECT_TRUE(result_->complete()); |
281 } | 288 } |
282 | 289 |
283 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { | 290 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { |
284 const GURL kBlobURL("blob:zero"); | 291 const GURL kBlobURL("blob:zero"); |
285 content_ = ""; | 292 content_ = ""; |
286 int64 kAllowedGrowth = 0; | 293 int64 kAllowedGrowth = 0; |
287 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); | 294 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); |
288 | 295 |
289 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 296 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
290 file_writer_delegate_->Start(file_, request_.Pass()); | 297 file_writer_delegate_->Start(request_.Pass()); |
291 MessageLoop::current()->Run(); | 298 MessageLoop::current()->Run(); |
292 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); | 299 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); |
293 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 300 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
294 | 301 |
295 file_writer_delegate_.reset(); | 302 file_writer_delegate_.reset(); |
296 | 303 |
297 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); | 304 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); |
298 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 305 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
299 EXPECT_TRUE(result_->complete()); | 306 EXPECT_TRUE(result_->complete()); |
300 } | 307 } |
301 | 308 |
302 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) { | 309 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) { |
303 scoped_ptr<FileWriterDelegate> file_writer_delegate2; | 310 scoped_ptr<FileWriterDelegate> file_writer_delegate2; |
304 scoped_ptr<net::URLRequest> request2; | 311 scoped_ptr<net::URLRequest> request2; |
305 scoped_ptr<Result> result2; | 312 scoped_ptr<Result> result2; |
306 | 313 |
307 FilePath file_path2; | 314 scoped_ptr<FileSystemOperationContext> context( |
308 PlatformFile file2; | 315 test_helper_.NewOperationContext()); |
309 bool created; | 316 bool created = false; |
310 base::PlatformFileError error_code; | 317 file_util()->EnsureFileExists(context.get(), |
311 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( | 318 GetFileSystemPath("test2"), |
312 test_helper_.GetOriginRootPath(), &file_path2)); | 319 &created); |
313 file2 = base::CreatePlatformFile( | 320 ASSERT_TRUE(created); |
314 file_path2, | |
315 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | |
316 base::PLATFORM_FILE_ASYNC, | |
317 &created, &error_code); | |
318 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | |
319 | 321 |
320 const GURL kBlobURL("blob:nolimitconcurrent"); | 322 const GURL kBlobURL("blob:nolimitconcurrent"); |
321 const GURL kBlobURL2("blob:nolimitconcurrent2"); | 323 const GURL kBlobURL2("blob:nolimitconcurrent2"); |
322 content_ = kData; | 324 content_ = kData; |
323 | 325 |
324 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); | 326 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); |
325 | 327 |
326 // Credate another FileWriterDelegate for concurrent write. | 328 // Credate another FileWriterDelegate for concurrent write. |
327 result2.reset(new Result()); | 329 result2.reset(new Result()); |
328 file_writer_delegate2.reset(new FileWriterDelegate( | 330 file_writer_delegate2.reset(CreateWriterDelegate( |
329 CreateNewOperation(result2.get(), quota::QuotaManager::kNoLimit), | 331 "test2", 0, quota::QuotaManager::kNoLimit, result2.get())); |
330 test_helper_.CreatePath(file_path2), 0)); | |
331 request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get())); | 332 request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get())); |
332 | 333 |
333 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 334 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
334 file_writer_delegate_->Start(file_, request_.Pass()); | 335 file_writer_delegate_->Start(request_.Pass()); |
335 file_writer_delegate2->Start(file2, request2.Pass()); | 336 file_writer_delegate2->Start(request2.Pass()); |
336 MessageLoop::current()->Run(); | 337 MessageLoop::current()->Run(); |
337 if (!result_->complete() || !result2->complete()) | 338 if (!result_->complete() || !result2->complete()) |
338 MessageLoop::current()->Run(); | 339 MessageLoop::current()->Run(); |
339 | 340 |
340 ASSERT_EQ(kDataSize * 2, test_helper_.GetCachedOriginUsage()); | 341 ASSERT_EQ(kDataSize * 2, test_helper_.GetCachedOriginUsage()); |
341 base::FlushPlatformFile(file2); | |
342 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 342 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
343 | 343 |
344 file_writer_delegate_.reset(); | 344 file_writer_delegate_.reset(); |
345 | 345 |
346 EXPECT_EQ(kDataSize, result_->bytes_written()); | 346 EXPECT_EQ(kDataSize, result_->bytes_written()); |
347 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 347 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
348 EXPECT_TRUE(result_->complete()); | 348 EXPECT_TRUE(result_->complete()); |
349 EXPECT_EQ(kDataSize, result2->bytes_written()); | 349 EXPECT_EQ(kDataSize, result2->bytes_written()); |
350 EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status()); | 350 EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status()); |
351 EXPECT_TRUE(result2->complete()); | 351 EXPECT_TRUE(result2->complete()); |
352 | |
353 base::ClosePlatformFile(file2); | |
354 } | 352 } |
355 | 353 |
356 TEST_F(FileWriterDelegateTest, WritesWithQuotaAndOffset) { | 354 TEST_F(FileWriterDelegateTest, WritesWithQuotaAndOffset) { |
357 const GURL kBlobURL("blob:failure-with-updated-quota"); | 355 const GURL kBlobURL("blob:failure-with-updated-quota"); |
358 content_ = kData; | 356 content_ = kData; |
359 | 357 |
360 // Writing kDataSize (=45) bytes data while allowed_growth is 100. | 358 // Writing kDataSize (=45) bytes data while allowed_growth is 100. |
361 int64 offset = 0; | 359 int64 offset = 0; |
362 int64 allowed_growth = 100; | 360 int64 allowed_growth = 100; |
363 ASSERT_LT(kDataSize, allowed_growth); | 361 ASSERT_LT(kDataSize, allowed_growth); |
364 PrepareForWrite(kBlobURL, offset, allowed_growth); | 362 PrepareForWrite(kBlobURL, offset, allowed_growth); |
365 | 363 |
366 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); | 364 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); |
367 file_writer_delegate_->Start(file_, request_.Pass()); | 365 file_writer_delegate_->Start(request_.Pass()); |
368 MessageLoop::current()->Run(); | 366 MessageLoop::current()->Run(); |
369 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 367 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
370 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 368 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
371 EXPECT_EQ(kDataSize, result_->bytes_written()); | 369 EXPECT_EQ(kDataSize, result_->bytes_written()); |
372 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 370 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
373 EXPECT_TRUE(result_->complete()); | 371 EXPECT_TRUE(result_->complete()); |
374 | 372 |
375 // Trying to overwrite kDataSize bytes data while allowed_growth is 20. | 373 // Trying to overwrite kDataSize bytes data while allowed_growth is 20. |
376 offset = 0; | 374 offset = 0; |
377 allowed_growth = 20; | 375 allowed_growth = 20; |
378 PrepareForWrite(kBlobURL, offset, allowed_growth); | 376 PrepareForWrite(kBlobURL, offset, allowed_growth); |
379 | 377 |
380 file_writer_delegate_->Start(file_, request_.Pass()); | 378 file_writer_delegate_->Start(request_.Pass()); |
381 MessageLoop::current()->Run(); | 379 MessageLoop::current()->Run(); |
382 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); | 380 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); |
383 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 381 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
384 EXPECT_EQ(kDataSize, result_->bytes_written()); | 382 EXPECT_EQ(kDataSize, result_->bytes_written()); |
385 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 383 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
386 EXPECT_TRUE(result_->complete()); | 384 EXPECT_TRUE(result_->complete()); |
387 | 385 |
388 // Trying to write kDataSize bytes data from offset 25 while | 386 // Trying to write kDataSize bytes data from offset 25 while |
389 // allowed_growth is 55. | 387 // allowed_growth is 55. |
390 offset = 25; | 388 offset = 25; |
391 allowed_growth = 55; | 389 allowed_growth = 55; |
392 PrepareForWrite(kBlobURL, offset, allowed_growth); | 390 PrepareForWrite(kBlobURL, offset, allowed_growth); |
393 | 391 |
394 file_writer_delegate_->Start(file_, request_.Pass()); | 392 file_writer_delegate_->Start(request_.Pass()); |
395 MessageLoop::current()->Run(); | 393 MessageLoop::current()->Run(); |
396 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); | 394 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); |
397 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 395 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
398 EXPECT_EQ(kDataSize, result_->bytes_written()); | 396 EXPECT_EQ(kDataSize, result_->bytes_written()); |
399 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 397 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
400 EXPECT_TRUE(result_->complete()); | 398 EXPECT_TRUE(result_->complete()); |
401 | 399 |
402 // Trying to overwrite 45 bytes data while allowed_growth is -20. | 400 // Trying to overwrite 45 bytes data while allowed_growth is -20. |
403 offset = 0; | 401 offset = 0; |
404 allowed_growth = -20; | 402 allowed_growth = -20; |
405 PrepareForWrite(kBlobURL, offset, allowed_growth); | 403 PrepareForWrite(kBlobURL, offset, allowed_growth); |
406 | 404 |
407 int64 pre_write_usage = ComputeCurrentOriginUsage(); | 405 int64 pre_write_usage = ComputeCurrentOriginUsage(); |
408 file_writer_delegate_->Start(file_, request_.Pass()); | 406 file_writer_delegate_->Start(request_.Pass()); |
409 MessageLoop::current()->Run(); | 407 MessageLoop::current()->Run(); |
410 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); | 408 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); |
411 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 409 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
412 EXPECT_EQ(kDataSize, result_->bytes_written()); | 410 EXPECT_EQ(kDataSize, result_->bytes_written()); |
413 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); | 411 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); |
414 EXPECT_TRUE(result_->complete()); | 412 EXPECT_TRUE(result_->complete()); |
415 | 413 |
416 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, | 414 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, |
417 // while allowed_growth is 10. | 415 // while allowed_growth is 10. |
418 const int kOverlap = 20; | 416 const int kOverlap = 20; |
419 offset = pre_write_usage - kOverlap; | 417 offset = pre_write_usage - kOverlap; |
420 allowed_growth = 10; | 418 allowed_growth = 10; |
421 PrepareForWrite(kBlobURL, offset, allowed_growth); | 419 PrepareForWrite(kBlobURL, offset, allowed_growth); |
422 | 420 |
423 file_writer_delegate_->Start(file_, request_.Pass()); | 421 file_writer_delegate_->Start(request_.Pass()); |
424 MessageLoop::current()->Run(); | 422 MessageLoop::current()->Run(); |
425 EXPECT_EQ(pre_write_usage + allowed_growth, | 423 EXPECT_EQ(pre_write_usage + allowed_growth, |
426 test_helper_.GetCachedOriginUsage()); | 424 test_helper_.GetCachedOriginUsage()); |
427 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); | 425 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); |
428 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); | 426 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); |
429 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); | 427 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); |
430 EXPECT_TRUE(result_->complete()); | 428 EXPECT_TRUE(result_->complete()); |
431 } | 429 } |
432 | 430 |
433 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest { | |
434 protected: | |
435 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE; | |
436 }; | |
437 | |
438 } // namespace fileapi | 431 } // namespace fileapi |
OLD | NEW |