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" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 base::ScopedTempDir temp_dir_; | 56 base::ScopedTempDir temp_dir_; |
57 base::FilePath file_path_; | 57 base::FilePath file_path_; |
58 std::string file_content_; | 58 std::string file_content_; |
59 | 59 |
60 scoped_ptr<base::Thread> worker_thread_; | 60 scoped_ptr<base::Thread> worker_thread_; |
61 }; | 61 }; |
62 | 62 |
63 TEST_F(LocalReaderProxyTest, Read) { | 63 TEST_F(LocalReaderProxyTest, Read) { |
64 // Open the file first. | 64 // Open the file first. |
65 scoped_ptr<util::LocalFileReader> file_reader( | 65 scoped_ptr<util::LocalFileReader> file_reader( |
66 new util::LocalFileReader(worker_thread_->message_loop_proxy())); | 66 new util::LocalFileReader(worker_thread_->message_loop_proxy().get())); |
67 net::TestCompletionCallback callback; | 67 net::TestCompletionCallback callback; |
68 file_reader->Open(file_path_, 0, callback.callback()); | 68 file_reader->Open(file_path_, 0, callback.callback()); |
69 ASSERT_EQ(net::OK, callback.WaitForResult()); | 69 ASSERT_EQ(net::OK, callback.WaitForResult()); |
70 | 70 |
71 // Test instance. | 71 // Test instance. |
72 LocalReaderProxy proxy(file_reader.Pass(), file_content_.size()); | 72 LocalReaderProxy proxy(file_reader.Pass(), file_content_.size()); |
73 | 73 |
74 // Make sure the read content is as same as the file. | 74 // Make sure the read content is as same as the file. |
75 std::string content; | 75 std::string content; |
76 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); | 76 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); |
77 EXPECT_EQ(file_content_, content); | 77 EXPECT_EQ(file_content_, content); |
78 } | 78 } |
79 | 79 |
80 TEST_F(LocalReaderProxyTest, ReadWithLimit) { | 80 TEST_F(LocalReaderProxyTest, ReadWithLimit) { |
81 // This test case, we only read first half of the file. | 81 // This test case, we only read first half of the file. |
82 const std::string expected_content = | 82 const std::string expected_content = |
83 file_content_.substr(0, file_content_.size() / 2); | 83 file_content_.substr(0, file_content_.size() / 2); |
84 | 84 |
85 // Open the file first. | 85 // Open the file first. |
86 scoped_ptr<util::LocalFileReader> file_reader( | 86 scoped_ptr<util::LocalFileReader> file_reader( |
87 new util::LocalFileReader(worker_thread_->message_loop_proxy())); | 87 new util::LocalFileReader(worker_thread_->message_loop_proxy().get())); |
88 net::TestCompletionCallback callback; | 88 net::TestCompletionCallback callback; |
89 file_reader->Open(file_path_, 0, callback.callback()); | 89 file_reader->Open(file_path_, 0, callback.callback()); |
90 ASSERT_EQ(net::OK, callback.WaitForResult()); | 90 ASSERT_EQ(net::OK, callback.WaitForResult()); |
91 | 91 |
92 // Test instance. | 92 // Test instance. |
93 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size()); | 93 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size()); |
94 | 94 |
95 // Make sure the read content is as same as the file. | 95 // Make sure the read content is as same as the file. |
96 std::string content; | 96 std::string content; |
97 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); | 97 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 scoped_ptr<FakeDriveService> fake_drive_service_; | 323 scoped_ptr<FakeDriveService> fake_drive_service_; |
324 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 324 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; |
325 }; | 325 }; |
326 | 326 |
327 TEST_F(DriveFileStreamReaderTest, Read) { | 327 TEST_F(DriveFileStreamReaderTest, Read) { |
328 const base::FilePath kDriveFile = | 328 const base::FilePath kDriveFile = |
329 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 329 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
330 // Create the reader, and initialize it. | 330 // Create the reader, and initialize it. |
331 // In this case, the file is not yet locally cached. | 331 // In this case, the file is not yet locally cached. |
332 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 332 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
333 GetFileSystemGetter(), | 333 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
334 worker_thread_->message_loop_proxy())); | |
335 EXPECT_FALSE(reader->IsInitialized()); | 334 EXPECT_FALSE(reader->IsInitialized()); |
336 | 335 |
337 int error = net::ERR_FAILED; | 336 int error = net::ERR_FAILED; |
338 scoped_ptr<ResourceEntry> entry; | 337 scoped_ptr<ResourceEntry> entry; |
339 { | 338 { |
340 base::RunLoop run_loop; | 339 base::RunLoop run_loop; |
341 reader->Initialize( | 340 reader->Initialize( |
342 kDriveFile, | 341 kDriveFile, |
343 net::HttpByteRange(), | 342 net::HttpByteRange(), |
344 google_apis::test_util::CreateQuitCallback( | 343 google_apis::test_util::CreateQuitCallback( |
345 &run_loop, | 344 &run_loop, |
346 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 345 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
347 run_loop.Run(); | 346 run_loop.Run(); |
348 } | 347 } |
349 EXPECT_EQ(net::OK, error); | 348 EXPECT_EQ(net::OK, error); |
350 ASSERT_TRUE(entry); | 349 ASSERT_TRUE(entry); |
351 EXPECT_TRUE(reader->IsInitialized()); | 350 EXPECT_TRUE(reader->IsInitialized()); |
352 size_t content_size = entry->file_info().size(); | 351 size_t content_size = entry->file_info().size(); |
353 | 352 |
354 // Read data from the reader. | 353 // Read data from the reader. |
355 std::string first_content; | 354 std::string first_content; |
356 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); | 355 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); |
357 EXPECT_EQ(content_size, first_content.size()); | 356 EXPECT_EQ(content_size, first_content.size()); |
358 | 357 |
359 // Create second instance and initialize it. | 358 // Create second instance and initialize it. |
360 // In this case, the file should be cached one. | 359 // In this case, the file should be cached one. |
361 reader.reset( | 360 reader.reset(new DriveFileStreamReader( |
362 new DriveFileStreamReader(GetFileSystemGetter(), | 361 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
363 worker_thread_->message_loop_proxy())); | |
364 EXPECT_FALSE(reader->IsInitialized()); | 362 EXPECT_FALSE(reader->IsInitialized()); |
365 | 363 |
366 error = net::ERR_FAILED; | 364 error = net::ERR_FAILED; |
367 entry.reset(); | 365 entry.reset(); |
368 { | 366 { |
369 base::RunLoop run_loop; | 367 base::RunLoop run_loop; |
370 reader->Initialize( | 368 reader->Initialize( |
371 kDriveFile, | 369 kDriveFile, |
372 net::HttpByteRange(), | 370 net::HttpByteRange(), |
373 google_apis::test_util::CreateQuitCallback( | 371 google_apis::test_util::CreateQuitCallback( |
(...skipping 19 matching lines...) Expand all Loading... |
393 TEST_F(DriveFileStreamReaderTest, ReadRange) { | 391 TEST_F(DriveFileStreamReaderTest, ReadRange) { |
394 // In this test case, we just confirm that the part of file is read. | 392 // In this test case, we just confirm that the part of file is read. |
395 const int64 kRangeOffset = 3; | 393 const int64 kRangeOffset = 3; |
396 const int64 kRangeLength = 4; | 394 const int64 kRangeLength = 4; |
397 | 395 |
398 const base::FilePath kDriveFile = | 396 const base::FilePath kDriveFile = |
399 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 397 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
400 // Create the reader, and initialize it. | 398 // Create the reader, and initialize it. |
401 // In this case, the file is not yet locally cached. | 399 // In this case, the file is not yet locally cached. |
402 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 400 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
403 GetFileSystemGetter(), | 401 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
404 worker_thread_->message_loop_proxy())); | |
405 EXPECT_FALSE(reader->IsInitialized()); | 402 EXPECT_FALSE(reader->IsInitialized()); |
406 | 403 |
407 int error = net::ERR_FAILED; | 404 int error = net::ERR_FAILED; |
408 scoped_ptr<ResourceEntry> entry; | 405 scoped_ptr<ResourceEntry> entry; |
409 net::HttpByteRange byte_range; | 406 net::HttpByteRange byte_range; |
410 byte_range.set_first_byte_position(kRangeOffset); | 407 byte_range.set_first_byte_position(kRangeOffset); |
411 // Last byte position is inclusive. | 408 // Last byte position is inclusive. |
412 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 409 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
413 { | 410 { |
414 base::RunLoop run_loop; | 411 base::RunLoop run_loop; |
(...skipping 11 matching lines...) Expand all Loading... |
426 | 423 |
427 // Read data from the reader. | 424 // Read data from the reader. |
428 std::string first_content; | 425 std::string first_content; |
429 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); | 426 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); |
430 | 427 |
431 // The length should be equal to range length. | 428 // The length should be equal to range length. |
432 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size())); | 429 EXPECT_EQ(kRangeLength, static_cast<int64>(first_content.size())); |
433 | 430 |
434 // Create second instance and initialize it. | 431 // Create second instance and initialize it. |
435 // In this case, the file should be cached one. | 432 // In this case, the file should be cached one. |
436 reader.reset( | 433 reader.reset(new DriveFileStreamReader( |
437 new DriveFileStreamReader(GetFileSystemGetter(), | 434 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
438 worker_thread_->message_loop_proxy())); | |
439 EXPECT_FALSE(reader->IsInitialized()); | 435 EXPECT_FALSE(reader->IsInitialized()); |
440 | 436 |
441 error = net::ERR_FAILED; | 437 error = net::ERR_FAILED; |
442 entry.reset(); | 438 entry.reset(); |
443 { | 439 { |
444 base::RunLoop run_loop; | 440 base::RunLoop run_loop; |
445 reader->Initialize( | 441 reader->Initialize( |
446 kDriveFile, | 442 kDriveFile, |
447 byte_range, | 443 byte_range, |
448 google_apis::test_util::CreateQuitCallback( | 444 google_apis::test_util::CreateQuitCallback( |
(...skipping 15 matching lines...) Expand all Loading... |
464 | 460 |
465 TEST_F(DriveFileStreamReaderTest, OutOfRangeError) { | 461 TEST_F(DriveFileStreamReaderTest, OutOfRangeError) { |
466 const int64 kRangeOffset = 1000000; // Out of range. | 462 const int64 kRangeOffset = 1000000; // Out of range. |
467 const int64 kRangeLength = 4; | 463 const int64 kRangeLength = 4; |
468 | 464 |
469 const base::FilePath kDriveFile = | 465 const base::FilePath kDriveFile = |
470 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 466 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
471 // Create the reader, and initialize it. | 467 // Create the reader, and initialize it. |
472 // In this case, the file is not yet locally cached. | 468 // In this case, the file is not yet locally cached. |
473 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 469 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
474 GetFileSystemGetter(), | 470 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
475 worker_thread_->message_loop_proxy())); | |
476 EXPECT_FALSE(reader->IsInitialized()); | 471 EXPECT_FALSE(reader->IsInitialized()); |
477 | 472 |
478 int error = net::ERR_FAILED; | 473 int error = net::ERR_FAILED; |
479 scoped_ptr<ResourceEntry> entry; | 474 scoped_ptr<ResourceEntry> entry; |
480 net::HttpByteRange byte_range; | 475 net::HttpByteRange byte_range; |
481 byte_range.set_first_byte_position(kRangeOffset); | 476 byte_range.set_first_byte_position(kRangeOffset); |
482 // Last byte position is inclusive. | 477 // Last byte position is inclusive. |
483 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 478 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
484 { | 479 { |
485 base::RunLoop run_loop; | 480 base::RunLoop run_loop; |
486 reader->Initialize( | 481 reader->Initialize( |
487 kDriveFile, | 482 kDriveFile, |
488 byte_range, | 483 byte_range, |
489 google_apis::test_util::CreateQuitCallback( | 484 google_apis::test_util::CreateQuitCallback( |
490 &run_loop, | 485 &run_loop, |
491 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 486 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
492 run_loop.Run(); | 487 run_loop.Run(); |
493 } | 488 } |
494 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); | 489 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); |
495 EXPECT_FALSE(entry); | 490 EXPECT_FALSE(entry); |
496 } | 491 } |
497 | 492 |
498 } // namespace drive | 493 } // namespace drive |
OLD | NEW |