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_file_stream_reader.h" | 5 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/message_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/rand_util.h" | |
14 #include "base/sequenced_task_runner.h" | |
15 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
16 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 14 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
17 #include "chrome/browser/chromeos/drive/file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/file_system_util.h" |
18 #include "chrome/browser/chromeos/drive/local_file_reader.h" | 16 #include "chrome/browser/chromeos/drive/local_file_reader.h" |
19 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
20 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
21 #include "chrome/browser/google_apis/task_util.h" | |
22 #include "chrome/browser/google_apis/test_util.h" | 19 #include "chrome/browser/google_apis/test_util.h" |
23 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
24 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
25 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
26 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
27 #include "net/http/http_byte_range.h" | 24 #include "net/http/http_byte_range.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
29 | 26 |
30 namespace drive { | 27 namespace drive { |
31 namespace internal { | 28 namespace internal { |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 333 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
337 // Create the reader, and initialize it. | 334 // Create the reader, and initialize it. |
338 // In this case, the file is not yet locally cached. | 335 // In this case, the file is not yet locally cached. |
339 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 336 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
340 GetFileSystemGetter(), | 337 GetFileSystemGetter(), |
341 worker_thread_->message_loop_proxy())); | 338 worker_thread_->message_loop_proxy())); |
342 EXPECT_FALSE(reader->IsInitialized()); | 339 EXPECT_FALSE(reader->IsInitialized()); |
343 | 340 |
344 int error = net::ERR_FAILED; | 341 int error = net::ERR_FAILED; |
345 scoped_ptr<ResourceEntry> entry; | 342 scoped_ptr<ResourceEntry> entry; |
346 reader->Initialize( | 343 { |
347 kDriveFile, | 344 base::RunLoop run_loop; |
348 net::HttpByteRange(), | 345 reader->Initialize( |
349 google_apis::CreateComposedCallback( | 346 kDriveFile, |
350 base::Bind(&google_apis::test_util::RunAndQuit), | 347 net::HttpByteRange(), |
351 google_apis::test_util::CreateCopyResultCallback( | 348 google_apis::test_util::CreateQuitCallback( |
352 &error, &entry))); | 349 &run_loop, |
353 base::MessageLoop::current()->Run(); | 350 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 351 run_loop.Run(); |
| 352 } |
354 EXPECT_EQ(net::OK, error); | 353 EXPECT_EQ(net::OK, error); |
355 ASSERT_TRUE(entry); | 354 ASSERT_TRUE(entry); |
356 EXPECT_TRUE(reader->IsInitialized()); | 355 EXPECT_TRUE(reader->IsInitialized()); |
357 size_t content_size = entry->file_info().size(); | 356 size_t content_size = entry->file_info().size(); |
358 | 357 |
359 // Read data from the reader. | 358 // Read data from the reader. |
360 std::string first_content; | 359 std::string first_content; |
361 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); | 360 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); |
362 EXPECT_EQ(content_size, first_content.size()); | 361 EXPECT_EQ(content_size, first_content.size()); |
363 | 362 |
364 // Create second instance and initialize it. | 363 // Create second instance and initialize it. |
365 // In this case, the file should be cached one. | 364 // In this case, the file should be cached one. |
366 reader.reset( | 365 reader.reset( |
367 new DriveFileStreamReader(GetFileSystemGetter(), | 366 new DriveFileStreamReader(GetFileSystemGetter(), |
368 worker_thread_->message_loop_proxy())); | 367 worker_thread_->message_loop_proxy())); |
369 EXPECT_FALSE(reader->IsInitialized()); | 368 EXPECT_FALSE(reader->IsInitialized()); |
370 | 369 |
371 error = net::ERR_FAILED; | 370 error = net::ERR_FAILED; |
372 entry.reset(); | 371 entry.reset(); |
373 reader->Initialize( | 372 { |
374 kDriveFile, | 373 base::RunLoop run_loop; |
375 net::HttpByteRange(), | 374 reader->Initialize( |
376 google_apis::CreateComposedCallback( | 375 kDriveFile, |
377 base::Bind(&google_apis::test_util::RunAndQuit), | 376 net::HttpByteRange(), |
378 google_apis::test_util::CreateCopyResultCallback( | 377 google_apis::test_util::CreateQuitCallback( |
379 &error, &entry))); | 378 &run_loop, |
380 base::MessageLoop::current()->Run(); | 379 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 380 run_loop.Run(); |
| 381 } |
381 EXPECT_EQ(net::OK, error); | 382 EXPECT_EQ(net::OK, error); |
382 ASSERT_TRUE(entry); | 383 ASSERT_TRUE(entry); |
383 EXPECT_TRUE(reader->IsInitialized()); | 384 EXPECT_TRUE(reader->IsInitialized()); |
384 | 385 |
385 // The size should be same. | 386 // The size should be same. |
386 EXPECT_EQ(content_size, static_cast<size_t>(entry->file_info().size())); | 387 EXPECT_EQ(content_size, static_cast<size_t>(entry->file_info().size())); |
387 | 388 |
388 // Read data from the reader, again. | 389 // Read data from the reader, again. |
389 std::string second_content; | 390 std::string second_content; |
390 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); | 391 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); |
(...skipping 15 matching lines...) Expand all Loading... |
406 GetFileSystemGetter(), | 407 GetFileSystemGetter(), |
407 worker_thread_->message_loop_proxy())); | 408 worker_thread_->message_loop_proxy())); |
408 EXPECT_FALSE(reader->IsInitialized()); | 409 EXPECT_FALSE(reader->IsInitialized()); |
409 | 410 |
410 int error = net::ERR_FAILED; | 411 int error = net::ERR_FAILED; |
411 scoped_ptr<ResourceEntry> entry; | 412 scoped_ptr<ResourceEntry> entry; |
412 net::HttpByteRange byte_range; | 413 net::HttpByteRange byte_range; |
413 byte_range.set_first_byte_position(kRangeOffset); | 414 byte_range.set_first_byte_position(kRangeOffset); |
414 // Last byte position is inclusive. | 415 // Last byte position is inclusive. |
415 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 416 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
416 reader->Initialize( | 417 { |
417 kDriveFile, | 418 base::RunLoop run_loop; |
418 byte_range, | 419 reader->Initialize( |
419 google_apis::CreateComposedCallback( | 420 kDriveFile, |
420 base::Bind(&google_apis::test_util::RunAndQuit), | 421 byte_range, |
421 google_apis::test_util::CreateCopyResultCallback( | 422 google_apis::test_util::CreateQuitCallback( |
422 &error, &entry))); | 423 &run_loop, |
423 base::MessageLoop::current()->Run(); | 424 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 425 run_loop.Run(); |
| 426 } |
424 EXPECT_EQ(net::OK, error); | 427 EXPECT_EQ(net::OK, error); |
425 ASSERT_TRUE(entry); | 428 ASSERT_TRUE(entry); |
426 EXPECT_TRUE(reader->IsInitialized()); | 429 EXPECT_TRUE(reader->IsInitialized()); |
427 | 430 |
428 // Read data from the reader. | 431 // Read data from the reader. |
429 std::string first_content; | 432 std::string first_content; |
430 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); | 433 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); |
431 | 434 |
432 // The length should be equal to range length. | 435 // The length should be equal to range length. |
433 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size())); | 436 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size())); |
434 | 437 |
435 // Create second instance and initialize it. | 438 // Create second instance and initialize it. |
436 // In this case, the file should be cached one. | 439 // In this case, the file should be cached one. |
437 reader.reset( | 440 reader.reset( |
438 new DriveFileStreamReader(GetFileSystemGetter(), | 441 new DriveFileStreamReader(GetFileSystemGetter(), |
439 worker_thread_->message_loop_proxy())); | 442 worker_thread_->message_loop_proxy())); |
440 EXPECT_FALSE(reader->IsInitialized()); | 443 EXPECT_FALSE(reader->IsInitialized()); |
441 | 444 |
442 error = net::ERR_FAILED; | 445 error = net::ERR_FAILED; |
443 entry.reset(); | 446 entry.reset(); |
444 reader->Initialize( | 447 { |
445 kDriveFile, | 448 base::RunLoop run_loop; |
446 byte_range, | 449 reader->Initialize( |
447 google_apis::CreateComposedCallback( | 450 kDriveFile, |
448 base::Bind(&google_apis::test_util::RunAndQuit), | 451 byte_range, |
449 google_apis::test_util::CreateCopyResultCallback( | 452 google_apis::test_util::CreateQuitCallback( |
450 &error, &entry))); | 453 &run_loop, |
451 base::MessageLoop::current()->Run(); | 454 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 455 run_loop.Run(); |
| 456 } |
452 EXPECT_EQ(net::OK, error); | 457 EXPECT_EQ(net::OK, error); |
453 ASSERT_TRUE(entry); | 458 ASSERT_TRUE(entry); |
454 EXPECT_TRUE(reader->IsInitialized()); | 459 EXPECT_TRUE(reader->IsInitialized()); |
455 | 460 |
456 // Read data from the reader, again. | 461 // Read data from the reader, again. |
457 std::string second_content; | 462 std::string second_content; |
458 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); | 463 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); |
459 | 464 |
460 // The same content is expected. | 465 // The same content is expected. |
461 EXPECT_EQ(first_content, second_content); | 466 EXPECT_EQ(first_content, second_content); |
(...skipping 11 matching lines...) Expand all Loading... |
473 GetFileSystemGetter(), | 478 GetFileSystemGetter(), |
474 worker_thread_->message_loop_proxy())); | 479 worker_thread_->message_loop_proxy())); |
475 EXPECT_FALSE(reader->IsInitialized()); | 480 EXPECT_FALSE(reader->IsInitialized()); |
476 | 481 |
477 int error = net::ERR_FAILED; | 482 int error = net::ERR_FAILED; |
478 scoped_ptr<ResourceEntry> entry; | 483 scoped_ptr<ResourceEntry> entry; |
479 net::HttpByteRange byte_range; | 484 net::HttpByteRange byte_range; |
480 byte_range.set_first_byte_position(kRangeOffset); | 485 byte_range.set_first_byte_position(kRangeOffset); |
481 // Last byte position is inclusive. | 486 // Last byte position is inclusive. |
482 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 487 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
483 reader->Initialize( | 488 { |
484 kDriveFile, | 489 base::RunLoop run_loop; |
485 byte_range, | 490 reader->Initialize( |
486 google_apis::CreateComposedCallback( | 491 kDriveFile, |
487 base::Bind(&google_apis::test_util::RunAndQuit), | 492 byte_range, |
488 google_apis::test_util::CreateCopyResultCallback( | 493 google_apis::test_util::CreateQuitCallback( |
489 &error, &entry))); | 494 &run_loop, |
490 base::MessageLoop::current()->Run(); | 495 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 496 run_loop.Run(); |
| 497 } |
491 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); | 498 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); |
492 EXPECT_FALSE(entry); | 499 EXPECT_FALSE(entry); |
493 } | 500 } |
494 | 501 |
495 } // namespace drive | 502 } // namespace drive |
OLD | NEW |