OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/chromeos/drive/fileapi/webkit_file_stream_reader_impl.h
" | 5 #include "chrome/browser/chromeos/drive/fileapi/webkit_file_stream_reader_impl.h
" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "components/drive/drive_test_util.h" | 18 #include "components/drive/drive_test_util.h" |
19 #include "components/drive/fake_file_system.h" | 19 #include "components/drive/fake_file_system.h" |
20 #include "components/drive/file_system_core_util.h" | 20 #include "components/drive/file_system_core_util.h" |
21 #include "components/drive/file_system_interface.h" | 21 #include "components/drive/file_system_interface.h" |
22 #include "components/drive/service/fake_drive_service.h" | 22 #include "components/drive/service/fake_drive_service.h" |
23 #include "components/drive/service/test_util.h" | 23 #include "components/drive/service/test_util.h" |
24 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 return fake_file_system_.get(); | 57 return fake_file_system_.get(); |
58 } | 58 } |
59 | 59 |
60 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { | 60 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { |
61 return base::Bind(&WebkitFileStreamReaderImplTest::GetFileSystem, | 61 return base::Bind(&WebkitFileStreamReaderImplTest::GetFileSystem, |
62 base::Unretained(this)); | 62 base::Unretained(this)); |
63 } | 63 } |
64 | 64 |
65 content::TestBrowserThreadBundle thread_bundle_; | 65 content::TestBrowserThreadBundle thread_bundle_; |
66 | 66 |
67 scoped_ptr<base::Thread> worker_thread_; | 67 std::unique_ptr<base::Thread> worker_thread_; |
68 | 68 |
69 scoped_ptr<FakeDriveService> fake_drive_service_; | 69 std::unique_ptr<FakeDriveService> fake_drive_service_; |
70 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 70 std::unique_ptr<test_util::FakeFileSystem> fake_file_system_; |
71 }; | 71 }; |
72 | 72 |
73 TEST_F(WebkitFileStreamReaderImplTest, ReadThenGetLength) { | 73 TEST_F(WebkitFileStreamReaderImplTest, ReadThenGetLength) { |
74 const base::FilePath kDriveFile = | 74 const base::FilePath kDriveFile = |
75 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 75 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
76 | 76 |
77 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 77 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
78 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 78 new WebkitFileStreamReaderImpl( |
79 0, // offset | 79 GetFileSystemGetter(), worker_thread_->task_runner().get(), |
80 base::Time())); // expected modification time | 80 kDriveFile, |
| 81 0, // offset |
| 82 base::Time())); // expected modification time |
81 | 83 |
82 std::string content; | 84 std::string content; |
83 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | 85 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); |
84 | 86 |
85 net::TestInt64CompletionCallback callback; | 87 net::TestInt64CompletionCallback callback; |
86 int64_t length = reader->GetLength(callback.callback()); | 88 int64_t length = reader->GetLength(callback.callback()); |
87 length = callback.GetResult(length); | 89 length = callback.GetResult(length); |
88 EXPECT_EQ(content.size(), static_cast<size_t>(length)); | 90 EXPECT_EQ(content.size(), static_cast<size_t>(length)); |
89 } | 91 } |
90 | 92 |
91 TEST_F(WebkitFileStreamReaderImplTest, GetLengthThenRead) { | 93 TEST_F(WebkitFileStreamReaderImplTest, GetLengthThenRead) { |
92 const base::FilePath kDriveFile = | 94 const base::FilePath kDriveFile = |
93 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 95 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
94 | 96 |
95 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 97 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
96 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 98 new WebkitFileStreamReaderImpl( |
97 0, // offset | 99 GetFileSystemGetter(), worker_thread_->task_runner().get(), |
98 base::Time())); // expected modification time | 100 kDriveFile, |
| 101 0, // offset |
| 102 base::Time())); // expected modification time |
99 | 103 |
100 net::TestInt64CompletionCallback callback; | 104 net::TestInt64CompletionCallback callback; |
101 int64_t length = reader->GetLength(callback.callback()); | 105 int64_t length = reader->GetLength(callback.callback()); |
102 length = callback.GetResult(length); | 106 length = callback.GetResult(length); |
103 | 107 |
104 std::string content; | 108 std::string content; |
105 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | 109 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); |
106 EXPECT_EQ(content.size(), static_cast<size_t>(length)); | 110 EXPECT_EQ(content.size(), static_cast<size_t>(length)); |
107 } | 111 } |
108 | 112 |
109 TEST_F(WebkitFileStreamReaderImplTest, ReadWithOffset) { | 113 TEST_F(WebkitFileStreamReaderImplTest, ReadWithOffset) { |
110 const base::FilePath kDriveFile = | 114 const base::FilePath kDriveFile = |
111 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 115 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
112 const int kOffset = 5; | 116 const int kOffset = 5; |
113 | 117 |
114 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 118 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
115 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 119 new WebkitFileStreamReaderImpl( |
116 kOffset, | 120 GetFileSystemGetter(), worker_thread_->task_runner().get(), |
117 base::Time())); // expected modification time | 121 kDriveFile, kOffset, |
| 122 base::Time())); // expected modification time |
118 | 123 |
119 std::string content; | 124 std::string content; |
120 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | 125 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); |
121 | 126 |
122 net::TestInt64CompletionCallback callback; | 127 net::TestInt64CompletionCallback callback; |
123 int64_t length = reader->GetLength(callback.callback()); | 128 int64_t length = reader->GetLength(callback.callback()); |
124 length = callback.GetResult(length); | 129 length = callback.GetResult(length); |
125 EXPECT_EQ(content.size() + kOffset, static_cast<size_t>(length)); | 130 EXPECT_EQ(content.size() + kOffset, static_cast<size_t>(length)); |
126 } | 131 } |
127 | 132 |
128 TEST_F(WebkitFileStreamReaderImplTest, ReadError) { | 133 TEST_F(WebkitFileStreamReaderImplTest, ReadError) { |
129 const base::FilePath kDriveFile = | 134 const base::FilePath kDriveFile = |
130 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); | 135 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); |
131 | 136 |
132 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 137 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
133 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 138 new WebkitFileStreamReaderImpl( |
134 0, // offset | 139 GetFileSystemGetter(), worker_thread_->task_runner().get(), |
135 base::Time())); // expected modification time | 140 kDriveFile, |
| 141 0, // offset |
| 142 base::Time())); // expected modification time |
136 | 143 |
137 const int kBufferSize = 10; | 144 const int kBufferSize = 10; |
138 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(kBufferSize)); | 145 scoped_refptr<net::IOBuffer> io_buffer(new net::IOBuffer(kBufferSize)); |
139 net::TestCompletionCallback callback; | 146 net::TestCompletionCallback callback; |
140 int result = reader->Read(io_buffer.get(), kBufferSize, callback.callback()); | 147 int result = reader->Read(io_buffer.get(), kBufferSize, callback.callback()); |
141 result = callback.GetResult(result); | 148 result = callback.GetResult(result); |
142 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); | 149 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); |
143 } | 150 } |
144 | 151 |
145 TEST_F(WebkitFileStreamReaderImplTest, GetLengthError) { | 152 TEST_F(WebkitFileStreamReaderImplTest, GetLengthError) { |
146 const base::FilePath kDriveFile = | 153 const base::FilePath kDriveFile = |
147 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); | 154 util::GetDriveMyDriveRootPath().AppendASCII("non-existing.txt"); |
148 | 155 |
149 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 156 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
150 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 157 new WebkitFileStreamReaderImpl( |
151 0, // offset | 158 GetFileSystemGetter(), worker_thread_->task_runner().get(), |
152 base::Time())); // expected modification time | 159 kDriveFile, |
| 160 0, // offset |
| 161 base::Time())); // expected modification time |
153 | 162 |
154 net::TestInt64CompletionCallback callback; | 163 net::TestInt64CompletionCallback callback; |
155 int64_t result = reader->GetLength(callback.callback()); | 164 int64_t result = reader->GetLength(callback.callback()); |
156 result = callback.GetResult(result); | 165 result = callback.GetResult(result); |
157 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); | 166 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result); |
158 } | 167 } |
159 | 168 |
160 TEST_F(WebkitFileStreamReaderImplTest, LastModification) { | 169 TEST_F(WebkitFileStreamReaderImplTest, LastModification) { |
161 const base::FilePath kDriveFile = | 170 const base::FilePath kDriveFile = |
162 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 171 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
163 | 172 |
164 base::Time expected_modification_time; | 173 base::Time expected_modification_time; |
165 ASSERT_TRUE(google_apis::util::GetTimeFromString( | 174 ASSERT_TRUE(google_apis::util::GetTimeFromString( |
166 "2011-12-14T00:40:47.330Z", &expected_modification_time)); | 175 "2011-12-14T00:40:47.330Z", &expected_modification_time)); |
167 | 176 |
168 FileError error = FILE_ERROR_FAILED; | 177 FileError error = FILE_ERROR_FAILED; |
169 scoped_ptr<ResourceEntry> entry; | 178 std::unique_ptr<ResourceEntry> entry; |
170 fake_file_system_->GetResourceEntry( | 179 fake_file_system_->GetResourceEntry( |
171 kDriveFile, | 180 kDriveFile, |
172 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 181 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
173 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
174 EXPECT_EQ(FILE_ERROR_OK, error); | 183 EXPECT_EQ(FILE_ERROR_OK, error); |
175 ASSERT_TRUE(entry); | 184 ASSERT_TRUE(entry); |
176 | 185 |
177 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR; | 186 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR; |
178 scoped_ptr<google_apis::FileResource> server_entry; | 187 std::unique_ptr<google_apis::FileResource> server_entry; |
179 fake_drive_service_->UpdateResource( | 188 fake_drive_service_->UpdateResource( |
180 entry->resource_id(), | 189 entry->resource_id(), |
181 std::string(), // parent_resource_id | 190 std::string(), // parent_resource_id |
182 std::string(), // title | 191 std::string(), // title |
183 expected_modification_time, base::Time(), | 192 expected_modification_time, base::Time(), |
184 google_apis::drive::Properties(), | 193 google_apis::drive::Properties(), |
185 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry)); | 194 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry)); |
186 base::RunLoop().RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
187 EXPECT_EQ(google_apis::HTTP_SUCCESS, status); | 196 EXPECT_EQ(google_apis::HTTP_SUCCESS, status); |
188 | 197 |
189 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 198 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
190 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 199 new WebkitFileStreamReaderImpl(GetFileSystemGetter(), |
191 0, // offset | 200 worker_thread_->task_runner().get(), |
192 expected_modification_time)); | 201 kDriveFile, |
| 202 0, // offset |
| 203 expected_modification_time)); |
193 | 204 |
194 net::TestInt64CompletionCallback callback; | 205 net::TestInt64CompletionCallback callback; |
195 int64_t result = reader->GetLength(callback.callback()); | 206 int64_t result = reader->GetLength(callback.callback()); |
196 result = callback.GetResult(result); | 207 result = callback.GetResult(result); |
197 | 208 |
198 std::string content; | 209 std::string content; |
199 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); | 210 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &content)); |
200 EXPECT_GE(content.size(), static_cast<size_t>(result)); | 211 EXPECT_GE(content.size(), static_cast<size_t>(result)); |
201 } | 212 } |
202 | 213 |
203 // TODO(hashimoto): Enable this test. crbug.com/346625 | 214 // TODO(hashimoto): Enable this test. crbug.com/346625 |
204 TEST_F(WebkitFileStreamReaderImplTest, DISABLED_LastModificationError) { | 215 TEST_F(WebkitFileStreamReaderImplTest, DISABLED_LastModificationError) { |
205 const base::FilePath kDriveFile = | 216 const base::FilePath kDriveFile = |
206 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 217 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
207 | 218 |
208 scoped_ptr<WebkitFileStreamReaderImpl> reader(new WebkitFileStreamReaderImpl( | 219 std::unique_ptr<WebkitFileStreamReaderImpl> reader( |
209 GetFileSystemGetter(), worker_thread_->task_runner().get(), kDriveFile, | 220 new WebkitFileStreamReaderImpl(GetFileSystemGetter(), |
210 0, // offset | 221 worker_thread_->task_runner().get(), |
211 base::Time::FromInternalValue(1))); | 222 kDriveFile, |
| 223 0, // offset |
| 224 base::Time::FromInternalValue(1))); |
212 | 225 |
213 net::TestInt64CompletionCallback callback; | 226 net::TestInt64CompletionCallback callback; |
214 int64_t result = reader->GetLength(callback.callback()); | 227 int64_t result = reader->GetLength(callback.callback()); |
215 result = callback.GetResult(result); | 228 result = callback.GetResult(result); |
216 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); | 229 EXPECT_EQ(net::ERR_UPLOAD_FILE_CHANGED, result); |
217 } | 230 } |
218 | 231 |
219 } // namespace internal | 232 } // namespace internal |
220 } // namespace drive | 233 } // namespace drive |
OLD | NEW |