OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" |
10 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
11 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
13 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
14 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 15 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
15 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
16 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
17 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
18 #include "chrome/browser/google_apis/task_util.h" | |
19 #include "chrome/browser/google_apis/test_util.h" | 19 #include "chrome/browser/google_apis/test_util.h" |
20 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
24 #include "net/base/test_completion_callback.h" | 24 #include "net/base/test_completion_callback.h" |
25 #include "net/http/http_byte_range.h" | 25 #include "net/http/http_byte_range.h" |
26 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 new base::Thread("ReadDriveFileSync")); | 149 new base::Thread("ReadDriveFileSync")); |
150 if (!worker_thread->Start()) | 150 if (!worker_thread->Start()) |
151 return false; | 151 return false; |
152 | 152 |
153 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 153 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
154 base::Bind(&DriveURLRequestJobTest::GetFileSystem, | 154 base::Bind(&DriveURLRequestJobTest::GetFileSystem, |
155 base::Unretained(this)), | 155 base::Unretained(this)), |
156 worker_thread->message_loop_proxy())); | 156 worker_thread->message_loop_proxy())); |
157 int error = net::ERR_FAILED; | 157 int error = net::ERR_FAILED; |
158 scoped_ptr<ResourceEntry> entry; | 158 scoped_ptr<ResourceEntry> entry; |
159 reader->Initialize( | 159 { |
160 file_path, | 160 base::RunLoop run_loop; |
161 net::HttpByteRange(), | 161 reader->Initialize( |
162 google_apis::CreateComposedCallback( | 162 file_path, |
163 base::Bind(&google_apis::test_util::RunAndQuit), | 163 net::HttpByteRange(), |
164 google_apis::test_util::CreateCopyResultCallback( | 164 google_apis::test_util::CreateQuitCallback( |
165 &error, &entry))); | 165 &run_loop, |
166 base::MessageLoop::current()->Run(); | 166 google_apis::test_util::CreateCopyResultCallback( |
| 167 &error, &entry))); |
| 168 run_loop.Run(); |
| 169 } |
167 if (error != net::OK || !entry) | 170 if (error != net::OK || !entry) |
168 return false; | 171 return false; |
169 | 172 |
170 // Read data from the reader. | 173 // Read data from the reader. |
171 std::string content; | 174 std::string content; |
172 if (test_util::ReadAllData(reader.get(), &content) != net::OK) | 175 if (test_util::ReadAllData(reader.get(), &content) != net::OK) |
173 return false; | 176 return false; |
174 | 177 |
175 if (static_cast<size_t>(entry->file_info().size()) != content.size()) | 178 if (static_cast<size_t>(entry->file_info().size()) != content.size()) |
176 return false; | 179 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
190 scoped_ptr<TestDelegate> test_delegate_; | 193 scoped_ptr<TestDelegate> test_delegate_; |
191 }; | 194 }; |
192 | 195 |
193 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 196 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
194 net::URLRequest request( | 197 net::URLRequest request( |
195 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 198 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
196 url_request_context_.get(), test_network_delegate_.get()); | 199 url_request_context_.get(), test_network_delegate_.get()); |
197 request.set_method("POST"); // Set non "GET" method. | 200 request.set_method("POST"); // Set non "GET" method. |
198 request.Start(); | 201 request.Start(); |
199 | 202 |
200 base::MessageLoop::current()->Run(); | 203 base::RunLoop().Run(); |
201 | 204 |
202 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 205 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
203 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); | 206 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); |
204 } | 207 } |
205 | 208 |
206 TEST_F(DriveURLRequestJobTest, RegularFile) { | 209 TEST_F(DriveURLRequestJobTest, RegularFile) { |
207 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 210 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
208 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 211 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
209 | 212 |
210 // For the first time, the file should be fetched from the server. | 213 // For the first time, the file should be fetched from the server. |
211 { | 214 { |
212 net::URLRequest request( | 215 net::URLRequest request( |
213 kTestUrl, test_delegate_.get(), | 216 kTestUrl, test_delegate_.get(), |
214 url_request_context_.get(), test_network_delegate_.get()); | 217 url_request_context_.get(), test_network_delegate_.get()); |
215 request.Start(); | 218 request.Start(); |
216 | 219 |
217 base::MessageLoop::current()->Run(); | 220 base::RunLoop().Run(); |
218 | 221 |
219 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 222 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
220 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" | 223 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" |
221 // on the server. | 224 // on the server. |
222 std::string mime_type; | 225 std::string mime_type; |
223 request.GetMimeType(&mime_type); | 226 request.GetMimeType(&mime_type); |
224 EXPECT_EQ("audio/mpeg", mime_type); | 227 EXPECT_EQ("audio/mpeg", mime_type); |
225 | 228 |
226 // Reading file must be done after |request| runs, otherwise | 229 // Reading file must be done after |request| runs, otherwise |
227 // it'll create a local cache file, and we cannot test correctly. | 230 // it'll create a local cache file, and we cannot test correctly. |
228 std::string expected_data; | 231 std::string expected_data; |
229 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 232 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
230 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 233 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
231 } | 234 } |
232 | 235 |
233 // For the second time, the locally cached file should be used. | 236 // For the second time, the locally cached file should be used. |
234 // The caching emulation is done by FakeFileSystem. | 237 // The caching emulation is done by FakeFileSystem. |
235 { | 238 { |
236 test_delegate_.reset(new TestDelegate); | 239 test_delegate_.reset(new TestDelegate); |
237 net::URLRequest request( | 240 net::URLRequest request( |
238 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 241 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
239 url_request_context_.get(), test_network_delegate_.get()); | 242 url_request_context_.get(), test_network_delegate_.get()); |
240 request.Start(); | 243 request.Start(); |
241 | 244 |
242 base::MessageLoop::current()->Run(); | 245 base::RunLoop().Run(); |
243 | 246 |
244 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 247 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
245 std::string mime_type; | 248 std::string mime_type; |
246 request.GetMimeType(&mime_type); | 249 request.GetMimeType(&mime_type); |
247 EXPECT_EQ("audio/mpeg", mime_type); | 250 EXPECT_EQ("audio/mpeg", mime_type); |
248 | 251 |
249 std::string expected_data; | 252 std::string expected_data; |
250 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 253 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
251 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 254 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
252 } | 255 } |
253 } | 256 } |
254 | 257 |
255 TEST_F(DriveURLRequestJobTest, HostedDocument) { | 258 TEST_F(DriveURLRequestJobTest, HostedDocument) { |
256 // Open a gdoc file. | 259 // Open a gdoc file. |
257 test_delegate_->set_quit_on_redirect(true); | 260 test_delegate_->set_quit_on_redirect(true); |
258 net::URLRequest request( | 261 net::URLRequest request( |
259 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), | 262 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), |
260 test_delegate_.get(), | 263 test_delegate_.get(), |
261 url_request_context_.get(), test_network_delegate_.get()); | 264 url_request_context_.get(), test_network_delegate_.get()); |
262 request.Start(); | 265 request.Start(); |
263 | 266 |
264 base::MessageLoop::current()->Run(); | 267 base::RunLoop().Run(); |
265 | 268 |
266 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 269 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
267 // Make sure that a hosted document triggers redirection. | 270 // Make sure that a hosted document triggers redirection. |
268 EXPECT_TRUE(request.is_redirecting()); | 271 EXPECT_TRUE(request.is_redirecting()); |
269 EXPECT_EQ(GURL("https://3_document_alternate_link"), | 272 EXPECT_EQ(GURL("https://3_document_alternate_link"), |
270 test_delegate_->redirect_url()); | 273 test_delegate_->redirect_url()); |
271 } | 274 } |
272 | 275 |
273 TEST_F(DriveURLRequestJobTest, RootDirectory) { | 276 TEST_F(DriveURLRequestJobTest, RootDirectory) { |
274 net::URLRequest request( | 277 net::URLRequest request( |
275 GURL("drive:drive/root"), test_delegate_.get(), | 278 GURL("drive:drive/root"), test_delegate_.get(), |
276 url_request_context_.get(), test_network_delegate_.get()); | 279 url_request_context_.get(), test_network_delegate_.get()); |
277 request.Start(); | 280 request.Start(); |
278 | 281 |
279 base::MessageLoop::current()->Run(); | 282 base::RunLoop().Run(); |
280 | 283 |
281 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 284 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
282 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 285 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
283 } | 286 } |
284 | 287 |
285 TEST_F(DriveURLRequestJobTest, Directory) { | 288 TEST_F(DriveURLRequestJobTest, Directory) { |
286 net::URLRequest request( | 289 net::URLRequest request( |
287 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), | 290 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), |
288 url_request_context_.get(), test_network_delegate_.get()); | 291 url_request_context_.get(), test_network_delegate_.get()); |
289 request.Start(); | 292 request.Start(); |
290 | 293 |
291 base::MessageLoop::current()->Run(); | 294 base::RunLoop().Run(); |
292 | 295 |
293 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 296 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
294 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 297 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
295 } | 298 } |
296 | 299 |
297 TEST_F(DriveURLRequestJobTest, NonExistingFile) { | 300 TEST_F(DriveURLRequestJobTest, NonExistingFile) { |
298 net::URLRequest request( | 301 net::URLRequest request( |
299 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), | 302 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), |
300 url_request_context_.get(), test_network_delegate_.get()); | 303 url_request_context_.get(), test_network_delegate_.get()); |
301 request.Start(); | 304 request.Start(); |
302 | 305 |
303 base::MessageLoop::current()->Run(); | 306 base::RunLoop().Run(); |
304 | 307 |
305 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 308 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
306 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); | 309 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); |
307 } | 310 } |
308 | 311 |
309 TEST_F(DriveURLRequestJobTest, WrongFormat) { | 312 TEST_F(DriveURLRequestJobTest, WrongFormat) { |
310 net::URLRequest request( | 313 net::URLRequest request( |
311 GURL("drive:"), test_delegate_.get(), | 314 GURL("drive:"), test_delegate_.get(), |
312 url_request_context_.get(), test_network_delegate_.get()); | 315 url_request_context_.get(), test_network_delegate_.get()); |
313 request.Start(); | 316 request.Start(); |
314 | 317 |
315 base::MessageLoop::current()->Run(); | 318 base::RunLoop().Run(); |
316 | 319 |
317 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 320 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
318 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); | 321 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); |
319 } | 322 } |
320 | 323 |
321 TEST_F(DriveURLRequestJobTest, Cancel) { | 324 TEST_F(DriveURLRequestJobTest, Cancel) { |
322 net::URLRequest request( | 325 net::URLRequest request( |
323 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 326 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
324 url_request_context_.get(), test_network_delegate_.get()); | 327 url_request_context_.get(), test_network_delegate_.get()); |
325 | 328 |
326 // Start the request, and cancel it immediately after it. | 329 // Start the request, and cancel it immediately after it. |
327 request.Start(); | 330 request.Start(); |
328 request.Cancel(); | 331 request.Cancel(); |
329 | 332 |
330 base::MessageLoop::current()->Run(); | 333 base::RunLoop().Run(); |
331 | 334 |
332 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 335 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); |
333 } | 336 } |
334 | 337 |
335 TEST_F(DriveURLRequestJobTest, RangeHeader) { | 338 TEST_F(DriveURLRequestJobTest, RangeHeader) { |
336 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 339 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
337 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 340 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
338 | 341 |
339 net::URLRequest request( | 342 net::URLRequest request( |
340 kTestUrl, test_delegate_.get(), | 343 kTestUrl, test_delegate_.get(), |
341 url_request_context_.get(), test_network_delegate_.get()); | 344 url_request_context_.get(), test_network_delegate_.get()); |
342 | 345 |
343 // Set range header. | 346 // Set range header. |
344 request.SetExtraRequestHeaderByName( | 347 request.SetExtraRequestHeaderByName( |
345 "Range", "bytes=3-5", false /* overwrite */); | 348 "Range", "bytes=3-5", false /* overwrite */); |
346 request.Start(); | 349 request.Start(); |
347 | 350 |
348 base::MessageLoop::current()->Run(); | 351 base::RunLoop().Run(); |
349 | 352 |
350 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 353 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
351 | 354 |
352 // Reading file must be done after |request| runs, otherwise | 355 // Reading file must be done after |request| runs, otherwise |
353 // it'll create a local cache file, and we cannot test correctly. | 356 // it'll create a local cache file, and we cannot test correctly. |
354 std::string expected_data; | 357 std::string expected_data; |
355 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 358 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
356 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); | 359 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); |
357 } | 360 } |
358 | 361 |
359 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { | 362 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { |
360 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 363 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
361 | 364 |
362 net::URLRequest request( | 365 net::URLRequest request( |
363 kTestUrl, test_delegate_.get(), | 366 kTestUrl, test_delegate_.get(), |
364 url_request_context_.get(), test_network_delegate_.get()); | 367 url_request_context_.get(), test_network_delegate_.get()); |
365 | 368 |
366 // Set range header. | 369 // Set range header. |
367 request.SetExtraRequestHeaderByName( | 370 request.SetExtraRequestHeaderByName( |
368 "Range", "Wrong Range Header Value", false /* overwrite */); | 371 "Range", "Wrong Range Header Value", false /* overwrite */); |
369 request.Start(); | 372 request.Start(); |
370 | 373 |
371 base::MessageLoop::current()->Run(); | 374 base::RunLoop().Run(); |
372 | 375 |
373 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 376 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
374 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); | 377 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); |
375 } | 378 } |
376 | 379 |
377 } // namespace drive | 380 } // namespace drive |
OLD | NEW |