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 #include "webkit/fileapi/file_system_file_stream_reader.h" | 5 #include "webkit/fileapi/file_system_file_stream_reader.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 size_t size, | 37 size_t size, |
38 int* result) { | 38 int* result) { |
39 ASSERT_TRUE(reader != NULL); | 39 ASSERT_TRUE(reader != NULL); |
40 ASSERT_TRUE(result != NULL); | 40 ASSERT_TRUE(result != NULL); |
41 *result = net::OK; | 41 *result = net::OK; |
42 net::TestCompletionCallback callback; | 42 net::TestCompletionCallback callback; |
43 size_t total_bytes_read = 0; | 43 size_t total_bytes_read = 0; |
44 while (total_bytes_read < size) { | 44 while (total_bytes_read < size) { |
45 scoped_refptr<net::IOBufferWithSize> buf( | 45 scoped_refptr<net::IOBufferWithSize> buf( |
46 new net::IOBufferWithSize(size - total_bytes_read)); | 46 new net::IOBufferWithSize(size - total_bytes_read)); |
47 int rv = reader->Read(buf, buf->size(), callback.callback()); | 47 int rv = reader->Read(buf.get(), buf->size(), callback.callback()); |
48 if (rv == net::ERR_IO_PENDING) | 48 if (rv == net::ERR_IO_PENDING) |
49 rv = callback.WaitForResult(); | 49 rv = callback.WaitForResult(); |
50 if (rv < 0) | 50 if (rv < 0) |
51 *result = rv; | 51 *result = rv; |
52 if (rv <= 0) | 52 if (rv <= 0) |
53 break; | 53 break; |
54 total_bytes_read += rv; | 54 total_bytes_read += rv; |
55 data->append(buf->data(), rv); | 55 data->append(buf->data(), rv); |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 void NeverCalled(int unused) { ADD_FAILURE(); } | 59 void NeverCalled(int unused) { ADD_FAILURE(); } |
60 | 60 |
61 } // namespace | 61 } // namespace |
62 | 62 |
63 class FileSystemFileStreamReaderTest : public testing::Test { | 63 class FileSystemFileStreamReaderTest : public testing::Test { |
64 public: | 64 public: |
65 FileSystemFileStreamReaderTest() | 65 FileSystemFileStreamReaderTest() |
66 : message_loop_(MessageLoop::TYPE_IO) {} | 66 : message_loop_(MessageLoop::TYPE_IO) {} |
67 | 67 |
68 virtual void SetUp() OVERRIDE { | 68 virtual void SetUp() OVERRIDE { |
69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
70 | 70 |
71 special_storage_policy_ = new quota::MockSpecialStoragePolicy; | 71 special_storage_policy_ = new quota::MockSpecialStoragePolicy; |
72 file_system_context_ = | 72 file_system_context_ = |
73 new FileSystemContext( | 73 new FileSystemContext( |
74 FileSystemTaskRunners::CreateMockTaskRunners(), | 74 FileSystemTaskRunners::CreateMockTaskRunners(), |
75 special_storage_policy_, | 75 special_storage_policy_.get(), |
76 NULL, | 76 NULL, |
77 temp_dir_.path(), | 77 temp_dir_.path(), |
78 CreateDisallowFileAccessOptions()); | 78 CreateDisallowFileAccessOptions()); |
79 | 79 |
80 file_system_context_->sandbox_provider()->ValidateFileSystemRoot( | 80 file_system_context_->sandbox_provider()->ValidateFileSystemRoot( |
81 GURL(kURLOrigin), kFileSystemTypeTemporary, true, // create | 81 GURL(kURLOrigin), kFileSystemTypeTemporary, true, // create |
82 base::Bind(&OnValidateFileSystem)); | 82 base::Bind(&OnValidateFileSystem)); |
83 MessageLoop::current()->RunAllPending(); | 83 MessageLoop::current()->RunAllPending(); |
84 | 84 |
85 WriteFile(kTestFileName, kTestData, kTestDataSize, | 85 WriteFile(kTestFileName, kTestData, kTestDataSize, |
86 &test_file_modification_time_); | 86 &test_file_modification_time_); |
87 } | 87 } |
88 | 88 |
89 virtual void TearDown() OVERRIDE { | 89 virtual void TearDown() OVERRIDE { |
90 MessageLoop::current()->RunAllPending(); | 90 MessageLoop::current()->RunAllPending(); |
91 } | 91 } |
92 | 92 |
93 protected: | 93 protected: |
94 FileSystemFileStreamReader* CreateFileReader( | 94 FileSystemFileStreamReader* CreateFileReader( |
95 const std::string& file_name, | 95 const std::string& file_name, |
96 int64 initial_offset, | 96 int64 initial_offset, |
97 const base::Time& expected_modification_time) { | 97 const base::Time& expected_modification_time) { |
98 return new FileSystemFileStreamReader(file_system_context_, | 98 return new FileSystemFileStreamReader(file_system_context_.get(), |
99 GetFileSystemURL(file_name), | 99 GetFileSystemURL(file_name), |
100 initial_offset, | 100 initial_offset, |
101 expected_modification_time); | 101 expected_modification_time); |
102 } | 102 } |
103 | 103 |
104 base::Time test_file_modification_time() const { | 104 base::Time test_file_modification_time() const { |
105 return test_file_modification_time_; | 105 return test_file_modification_time_; |
106 } | 106 } |
107 | 107 |
108 void WriteFile(const std::string& file_name, | 108 void WriteFile(const std::string& file_name, |
109 const char* buf, | 109 const char* buf, |
110 int buf_size, | 110 int buf_size, |
111 base::Time* modification_time) { | 111 base::Time* modification_time) { |
112 FileSystemFileUtil* file_util = file_system_context_-> | 112 FileSystemFileUtil* file_util = file_system_context_-> |
113 sandbox_provider()->GetFileUtil(kFileSystemTypeTemporary); | 113 sandbox_provider()->GetFileUtil(kFileSystemTypeTemporary); |
114 FileSystemURL url = GetFileSystemURL(file_name); | 114 FileSystemURL url = GetFileSystemURL(file_name); |
115 | 115 |
116 FileSystemOperationContext context(file_system_context_); | 116 FileSystemOperationContext context(file_system_context_.get()); |
117 context.set_allowed_bytes_growth(1024); | 117 context.set_allowed_bytes_growth(1024); |
118 | 118 |
119 base::PlatformFile handle = base::kInvalidPlatformFileValue; | 119 base::PlatformFile handle = base::kInvalidPlatformFileValue; |
120 bool created = false; | 120 bool created = false; |
121 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateOrOpen( | 121 ASSERT_EQ(base::PLATFORM_FILE_OK, file_util->CreateOrOpen( |
122 &context, | 122 &context, |
123 url, | 123 url, |
124 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, | 124 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE, |
125 &handle, | 125 &handle, |
126 &created)); | 126 &created)); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 ASSERT_EQ(&kTestData[3], data); | 270 ASSERT_EQ(&kTestData[3], data); |
271 } | 271 } |
272 | 272 |
273 TEST_F(FileSystemFileStreamReaderTest, DeleteWithUnfinishedRead) { | 273 TEST_F(FileSystemFileStreamReaderTest, DeleteWithUnfinishedRead) { |
274 scoped_ptr<FileSystemFileStreamReader> reader( | 274 scoped_ptr<FileSystemFileStreamReader> reader( |
275 CreateFileReader(kTestFileName, 0, base::Time())); | 275 CreateFileReader(kTestFileName, 0, base::Time())); |
276 | 276 |
277 net::TestCompletionCallback callback; | 277 net::TestCompletionCallback callback; |
278 scoped_refptr<net::IOBufferWithSize> buf( | 278 scoped_refptr<net::IOBufferWithSize> buf( |
279 new net::IOBufferWithSize(kTestDataSize)); | 279 new net::IOBufferWithSize(kTestDataSize)); |
280 int rv = reader->Read(buf, buf->size(), base::Bind(&NeverCalled)); | 280 int rv = reader->Read(buf.get(), buf->size(), base::Bind(&NeverCalled)); |
281 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); | 281 ASSERT_TRUE(rv == net::ERR_IO_PENDING || rv >= 0); |
282 | 282 |
283 // Delete immediately. | 283 // Delete immediately. |
284 // Should not crash; nor should NeverCalled be callback. | 284 // Should not crash; nor should NeverCalled be callback. |
285 reader.reset(); | 285 reader.reset(); |
286 } | 286 } |
287 | 287 |
288 } // namespace fileapi | 288 } // namespace fileapi |
OLD | NEW |