Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1533)

Side by Side Diff: webkit/fileapi/file_writer_delegate_unittest.cc

Issue 10387054: Implement SandboxFileWriter and rewrite FileWriterDelegate to use it (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test fix Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/fileapi/file_writer_delegate.cc ('k') | webkit/fileapi/sandbox_file_writer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 base::Bind(&Result::DidWrite, base::Unretained(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);
123
124 static net::URLRequest::ProtocolFactory Factory; 128 static net::URLRequest::ProtocolFactory Factory;
125 129
126 // This should be alive until the very end of this instance. 130 // This should be alive until the very end of this instance.
127 MessageLoop loop_; 131 MessageLoop loop_;
128 132
129 scoped_ptr<FileWriterDelegate> file_writer_delegate_; 133 scoped_ptr<FileWriterDelegate> file_writer_delegate_;
130 scoped_ptr<net::URLRequest> request_; 134 scoped_ptr<net::URLRequest> request_;
131 scoped_ptr<Result> result_; 135 scoped_ptr<Result> result_;
132 FileSystemTestOriginHelper test_helper_; 136 FileSystemTestOriginHelper test_helper_;
133 137
134 ScopedTempDir dir_; 138 ScopedTempDir dir_;
135 FilePath file_path_;
136 PlatformFile file_;
137 139
138 static const char* content_; 140 static const char* content_;
139 }; 141 };
142
140 const char* FileWriterDelegateTest::content_ = NULL; 143 const char* FileWriterDelegateTest::content_ = NULL;
141 144
142 namespace { 145 namespace {
143 146
144 static std::string g_content; 147 static std::string g_content;
145 148
146 class FileWriterDelegateTestJob : public net::URLRequestJob { 149 class FileWriterDelegateTestJob : public net::URLRequestJob {
147 public: 150 public:
148 FileWriterDelegateTestJob(net::URLRequest* request, 151 FileWriterDelegateTestJob(net::URLRequest* request,
149 const std::string& content) 152 const std::string& content)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 net::URLRequestJob* FileWriterDelegateTest::Factory( 196 net::URLRequestJob* FileWriterDelegateTest::Factory(
194 net::URLRequest* request, 197 net::URLRequest* request,
195 const std::string& scheme) { 198 const std::string& scheme) {
196 return new FileWriterDelegateTestJob( 199 return new FileWriterDelegateTestJob(
197 request, FileWriterDelegateTest::content_); 200 request, FileWriterDelegateTest::content_);
198 } 201 }
199 202
200 void FileWriterDelegateTest::SetUp() { 203 void FileWriterDelegateTest::SetUp() {
201 ASSERT_TRUE(dir_.CreateUniqueTempDir()); 204 ASSERT_TRUE(dir_.CreateUniqueTempDir());
202 FilePath base_dir = dir_.path().AppendASCII("filesystem"); 205 FilePath base_dir = dir_.path().AppendASCII("filesystem");
203 SetUpTestHelper(base_dir); 206 test_helper_.SetUp(base_dir, NULL);
204 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( 207
205 test_helper_.GetOriginRootPath(), &file_path_)); 208 scoped_ptr<FileSystemOperationContext> context(
209 test_helper_.NewOperationContext());
210 context->set_allowed_bytes_growth(kint64max);
211 bool created = false;
212 base::PlatformFileError error = file_util()->EnsureFileExists(
213 context.get(),
214 GetFileSystemPath("test"),
215 &created);
216 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
217 ASSERT_TRUE(created);
206 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory); 218 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", &Factory);
207 } 219 }
208 220
209 void FileWriterDelegateTest::TearDown() { 221 void FileWriterDelegateTest::TearDown() {
210 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL); 222 net::URLRequest::Deprecated::RegisterProtocolFactory("blob", NULL);
211 base::ClosePlatformFile(file_);
212 test_helper_.TearDown(); 223 test_helper_.TearDown();
213 } 224 }
214 225
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) { 226 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimit) {
227 const GURL kBlobURL("blob:nolimit"); 227 const GURL kBlobURL("blob:nolimit");
228 content_ = kData; 228 content_ = kData;
229 229
230 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); 230 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit);
231 231
232 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 232 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
233 file_writer_delegate_->Start(file_, request_.Pass()); 233 file_writer_delegate_->Start(request_.Pass());
234 MessageLoop::current()->Run(); 234 MessageLoop::current()->Run();
235
236 ASSERT_TRUE(result_->complete());
237 file_writer_delegate_.reset();
238
235 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); 239 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage());
236 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 240 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
237
238 EXPECT_EQ(kDataSize, result_->bytes_written()); 241 EXPECT_EQ(kDataSize, result_->bytes_written());
239 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 242 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
240 EXPECT_TRUE(result_->complete());
241
242 file_writer_delegate_.reset();
243 } 243 }
244 244
245 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) { 245 TEST_F(FileWriterDelegateTest, WriteSuccessWithJustQuota) {
246 const GURL kBlobURL("blob:just"); 246 const GURL kBlobURL("blob:just");
247 content_ = kData; 247 content_ = kData;
248 const int64 kAllowedGrowth = kDataSize; 248 const int64 kAllowedGrowth = kDataSize;
249 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); 249 PrepareForWrite(kBlobURL, 0, kAllowedGrowth);
250 250
251 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 251 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
252 file_writer_delegate_->Start(file_, request_.Pass()); 252 file_writer_delegate_->Start(request_.Pass());
253 MessageLoop::current()->Run(); 253 MessageLoop::current()->Run();
254 ASSERT_TRUE(result_->complete());
255 file_writer_delegate_.reset();
256
254 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); 257 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage());
255 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 258 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
256 259
257 file_writer_delegate_.reset();
258
259 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); 260 EXPECT_EQ(kAllowedGrowth, result_->bytes_written());
260 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 261 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
261 EXPECT_TRUE(result_->complete());
262 } 262 }
263 263
264 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) { 264 TEST_F(FileWriterDelegateTest, WriteFailureByQuota) {
265 const GURL kBlobURL("blob:failure"); 265 const GURL kBlobURL("blob:failure");
266 content_ = kData; 266 content_ = kData;
267 const int64 kAllowedGrowth = kDataSize - 1; 267 const int64 kAllowedGrowth = kDataSize - 1;
268 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); 268 PrepareForWrite(kBlobURL, 0, kAllowedGrowth);
269 269
270 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 270 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
271 file_writer_delegate_->Start(file_, request_.Pass()); 271 file_writer_delegate_->Start(request_.Pass());
272 MessageLoop::current()->Run(); 272 MessageLoop::current()->Run();
273 ASSERT_TRUE(result_->complete());
274 file_writer_delegate_.reset();
275
273 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); 276 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage());
274 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 277 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
275 278
276 file_writer_delegate_.reset();
277
278 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); 279 EXPECT_EQ(kAllowedGrowth, result_->bytes_written());
279 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); 280 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status());
280 EXPECT_TRUE(result_->complete()); 281 EXPECT_TRUE(result_->complete());
281 } 282 }
282 283
283 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) { 284 TEST_F(FileWriterDelegateTest, WriteZeroBytesSuccessfullyWithZeroQuota) {
284 const GURL kBlobURL("blob:zero"); 285 const GURL kBlobURL("blob:zero");
285 content_ = ""; 286 content_ = "";
286 int64 kAllowedGrowth = 0; 287 int64 kAllowedGrowth = 0;
287 PrepareForWrite(kBlobURL, 0, kAllowedGrowth); 288 PrepareForWrite(kBlobURL, 0, kAllowedGrowth);
288 289
289 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 290 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
290 file_writer_delegate_->Start(file_, request_.Pass()); 291 file_writer_delegate_->Start(request_.Pass());
291 MessageLoop::current()->Run(); 292 MessageLoop::current()->Run();
293 ASSERT_TRUE(result_->complete());
294 file_writer_delegate_.reset();
295
292 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage()); 296 ASSERT_EQ(kAllowedGrowth, test_helper_.GetCachedOriginUsage());
293 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 297 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
294 298
295 file_writer_delegate_.reset();
296
297 EXPECT_EQ(kAllowedGrowth, result_->bytes_written()); 299 EXPECT_EQ(kAllowedGrowth, result_->bytes_written());
298 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 300 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
299 EXPECT_TRUE(result_->complete()); 301 EXPECT_TRUE(result_->complete());
300 } 302 }
301 303
302 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) { 304 TEST_F(FileWriterDelegateTest, WriteSuccessWithoutQuotaLimitConcurrent) {
303 scoped_ptr<FileWriterDelegate> file_writer_delegate2; 305 scoped_ptr<FileWriterDelegate> file_writer_delegate2;
304 scoped_ptr<net::URLRequest> request2; 306 scoped_ptr<net::URLRequest> request2;
305 scoped_ptr<Result> result2; 307 scoped_ptr<Result> result2;
306 308
307 FilePath file_path2; 309 scoped_ptr<FileSystemOperationContext> context(
308 PlatformFile file2; 310 test_helper_.NewOperationContext());
309 bool created; 311 bool created = false;
310 base::PlatformFileError error_code; 312 file_util()->EnsureFileExists(context.get(),
311 ASSERT_TRUE(file_util::CreateTemporaryFileInDir( 313 GetFileSystemPath("test2"),
312 test_helper_.GetOriginRootPath(), &file_path2)); 314 &created);
313 file2 = base::CreatePlatformFile( 315 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 316
320 const GURL kBlobURL("blob:nolimitconcurrent"); 317 const GURL kBlobURL("blob:nolimitconcurrent");
321 const GURL kBlobURL2("blob:nolimitconcurrent2"); 318 const GURL kBlobURL2("blob:nolimitconcurrent2");
322 content_ = kData; 319 content_ = kData;
323 320
324 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit); 321 PrepareForWrite(kBlobURL, 0, quota::QuotaManager::kNoLimit);
325 322
326 // Credate another FileWriterDelegate for concurrent write. 323 // Credate another FileWriterDelegate for concurrent write.
327 result2.reset(new Result()); 324 result2.reset(new Result());
328 file_writer_delegate2.reset(new FileWriterDelegate( 325 file_writer_delegate2.reset(CreateWriterDelegate(
329 CreateNewOperation(result2.get(), quota::QuotaManager::kNoLimit), 326 "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())); 327 request2.reset(new net::URLRequest(kBlobURL2, file_writer_delegate2.get()));
332 328
333 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 329 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
334 file_writer_delegate_->Start(file_, request_.Pass()); 330 file_writer_delegate_->Start(request_.Pass());
335 file_writer_delegate2->Start(file2, request2.Pass()); 331 file_writer_delegate2->Start(request2.Pass());
336 MessageLoop::current()->Run(); 332 MessageLoop::current()->Run();
337 if (!result_->complete() || !result2->complete()) 333 if (!result_->complete() || !result2->complete())
338 MessageLoop::current()->Run(); 334 MessageLoop::current()->Run();
339 335
336 ASSERT_TRUE(result_->complete());
337 ASSERT_TRUE(result2->complete());
338 file_writer_delegate_.reset();
339 file_writer_delegate2.reset();
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();
345
346 EXPECT_EQ(kDataSize, result_->bytes_written()); 344 EXPECT_EQ(kDataSize, result_->bytes_written());
347 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 345 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
348 EXPECT_TRUE(result_->complete());
349 EXPECT_EQ(kDataSize, result2->bytes_written()); 346 EXPECT_EQ(kDataSize, result2->bytes_written());
350 EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status()); 347 EXPECT_EQ(base::PLATFORM_FILE_OK, result2->status());
351 EXPECT_TRUE(result2->complete());
352
353 base::ClosePlatformFile(file2);
354 } 348 }
355 349
356 TEST_F(FileWriterDelegateTest, WritesWithQuotaAndOffset) { 350 TEST_F(FileWriterDelegateTest, WritesWithQuotaAndOffset) {
357 const GURL kBlobURL("blob:failure-with-updated-quota"); 351 const GURL kBlobURL("blob:failure-with-updated-quota");
358 content_ = kData; 352 content_ = kData;
359 353
360 // Writing kDataSize (=45) bytes data while allowed_growth is 100. 354 // Writing kDataSize (=45) bytes data while allowed_growth is 100.
361 int64 offset = 0; 355 int64 offset = 0;
362 int64 allowed_growth = 100; 356 int64 allowed_growth = 100;
363 ASSERT_LT(kDataSize, allowed_growth); 357 ASSERT_LT(kDataSize, allowed_growth);
364 PrepareForWrite(kBlobURL, offset, allowed_growth); 358 PrepareForWrite(kBlobURL, offset, allowed_growth);
365 359
366 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage()); 360 ASSERT_EQ(0, test_helper_.GetCachedOriginUsage());
367 file_writer_delegate_->Start(file_, request_.Pass()); 361 file_writer_delegate_->Start(request_.Pass());
368 MessageLoop::current()->Run(); 362 MessageLoop::current()->Run();
363 ASSERT_TRUE(result_->complete());
364 file_writer_delegate_.reset();
365
369 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); 366 ASSERT_EQ(kDataSize, test_helper_.GetCachedOriginUsage());
370 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 367 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
371 EXPECT_EQ(kDataSize, result_->bytes_written()); 368 EXPECT_EQ(kDataSize, result_->bytes_written());
372 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 369 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
373 EXPECT_TRUE(result_->complete());
374 370
375 // Trying to overwrite kDataSize bytes data while allowed_growth is 20. 371 // Trying to overwrite kDataSize bytes data while allowed_growth is 20.
376 offset = 0; 372 offset = 0;
377 allowed_growth = 20; 373 allowed_growth = 20;
378 PrepareForWrite(kBlobURL, offset, allowed_growth); 374 PrepareForWrite(kBlobURL, offset, allowed_growth);
379 375
380 file_writer_delegate_->Start(file_, request_.Pass()); 376 file_writer_delegate_->Start(request_.Pass());
381 MessageLoop::current()->Run(); 377 MessageLoop::current()->Run();
382 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage()); 378 EXPECT_EQ(kDataSize, test_helper_.GetCachedOriginUsage());
383 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 379 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
384 EXPECT_EQ(kDataSize, result_->bytes_written()); 380 EXPECT_EQ(kDataSize, result_->bytes_written());
385 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 381 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
386 EXPECT_TRUE(result_->complete()); 382 EXPECT_TRUE(result_->complete());
387 383
388 // Trying to write kDataSize bytes data from offset 25 while 384 // Trying to write kDataSize bytes data from offset 25 while
389 // allowed_growth is 55. 385 // allowed_growth is 55.
390 offset = 25; 386 offset = 25;
391 allowed_growth = 55; 387 allowed_growth = 55;
392 PrepareForWrite(kBlobURL, offset, allowed_growth); 388 PrepareForWrite(kBlobURL, offset, allowed_growth);
393 389
394 file_writer_delegate_->Start(file_, request_.Pass()); 390 file_writer_delegate_->Start(request_.Pass());
395 MessageLoop::current()->Run(); 391 MessageLoop::current()->Run();
392 ASSERT_TRUE(result_->complete());
393 file_writer_delegate_.reset();
394
396 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage()); 395 EXPECT_EQ(offset + kDataSize, test_helper_.GetCachedOriginUsage());
397 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 396 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
398 EXPECT_EQ(kDataSize, result_->bytes_written()); 397 EXPECT_EQ(kDataSize, result_->bytes_written());
399 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 398 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
400 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();
408 ASSERT_TRUE(result_->complete());
409 file_writer_delegate_.reset();
410
410 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage()); 411 EXPECT_EQ(pre_write_usage, test_helper_.GetCachedOriginUsage());
411 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 412 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
412 EXPECT_EQ(kDataSize, result_->bytes_written()); 413 EXPECT_EQ(kDataSize, result_->bytes_written());
413 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status()); 414 EXPECT_EQ(base::PLATFORM_FILE_OK, result_->status());
414 EXPECT_TRUE(result_->complete());
415 415
416 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20, 416 // Trying to overwrite 45 bytes data with offset pre_write_usage - 20,
417 // while allowed_growth is 10. 417 // while allowed_growth is 10.
418 const int kOverlap = 20; 418 const int kOverlap = 20;
419 offset = pre_write_usage - kOverlap; 419 offset = pre_write_usage - kOverlap;
420 allowed_growth = 10; 420 allowed_growth = 10;
421 PrepareForWrite(kBlobURL, offset, allowed_growth); 421 PrepareForWrite(kBlobURL, offset, allowed_growth);
422 422
423 file_writer_delegate_->Start(file_, request_.Pass()); 423 file_writer_delegate_->Start(request_.Pass());
424 MessageLoop::current()->Run(); 424 MessageLoop::current()->Run();
425 ASSERT_TRUE(result_->complete());
426 file_writer_delegate_.reset();
427
425 EXPECT_EQ(pre_write_usage + allowed_growth, 428 EXPECT_EQ(pre_write_usage + allowed_growth,
426 test_helper_.GetCachedOriginUsage()); 429 test_helper_.GetCachedOriginUsage());
427 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage()); 430 EXPECT_EQ(ComputeCurrentOriginUsage(), test_helper_.GetCachedOriginUsage());
428 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written()); 431 EXPECT_EQ(kOverlap + allowed_growth, result_->bytes_written());
429 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status()); 432 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_->status());
430 EXPECT_TRUE(result_->complete());
431 } 433 }
432 434
433 class FileWriterDelegateUnlimitedTest : public FileWriterDelegateTest {
434 protected:
435 virtual void SetUpTestHelper(const FilePath& path) OVERRIDE;
436 };
437
438 } // namespace fileapi 435 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_writer_delegate.cc ('k') | webkit/fileapi/sandbox_file_writer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698