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