| 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/message_loop.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.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/google_apis/fake_drive_service.h" | 20 #include "chrome/browser/google_apis/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.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
| 25 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 26 #include "net/base/test_completion_callback.h" | 26 #include "net/base/test_completion_callback.h" |
| 27 #include "net/http/http_byte_range.h" | 27 #include "net/http/http_byte_range.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 29 |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 | 31 |
| 32 namespace drive { | 32 namespace drive { |
| 33 namespace internal { | 33 namespace internal { |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // Increments the |num_called|, when this method is invoked. | 36 // Increments the |num_called|, when this method is invoked. |
| 37 void IncrementCallback(int* num_called) { | 37 void IncrementCallback(int* num_called) { |
| 38 DCHECK(num_called); | 38 DCHECK(num_called); |
| 39 ++*num_called; | 39 ++*num_called; |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 class LocalReaderProxyTest : public ::testing::Test { | 44 class LocalReaderProxyTest : public ::testing::Test { |
| 45 protected: | 45 protected: |
| 46 LocalReaderProxyTest() : io_thread_(BrowserThread::IO, &message_loop_) { | 46 LocalReaderProxyTest() : thread_bundle_(IO_MAINLOOP) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 virtual void SetUp() OVERRIDE { | 49 virtual void SetUp() OVERRIDE { |
| 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 50 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 51 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize( | 51 ASSERT_TRUE(google_apis::test_util::CreateFileOfSpecifiedSize( |
| 52 temp_dir_.path(), 1024, &file_path_, &file_content_)); | 52 temp_dir_.path(), 1024, &file_path_, &file_content_)); |
| 53 | 53 |
| 54 worker_thread_.reset(new base::Thread("ReaderProxyTest")); | 54 worker_thread_.reset(new base::Thread("ReaderProxyTest")); |
| 55 ASSERT_TRUE(worker_thread_->Start()); | 55 ASSERT_TRUE(worker_thread_->Start()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 virtual void TearDown() OVERRIDE { | 58 virtual void TearDown() OVERRIDE { |
| 59 worker_thread_.reset(); | 59 worker_thread_.reset(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 base::MessageLoopForIO message_loop_; | 62 content::TestBrowserThreadBundle thread_bundle_; |
| 63 content::TestBrowserThread io_thread_; | |
| 64 | 63 |
| 65 base::ScopedTempDir temp_dir_; | 64 base::ScopedTempDir temp_dir_; |
| 66 base::FilePath file_path_; | 65 base::FilePath file_path_; |
| 67 std::string file_content_; | 66 std::string file_content_; |
| 68 | 67 |
| 69 scoped_ptr<base::Thread> worker_thread_; | 68 scoped_ptr<base::Thread> worker_thread_; |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 TEST_F(LocalReaderProxyTest, Read) { | 71 TEST_F(LocalReaderProxyTest, Read) { |
| 73 // Open the file first. | 72 // Open the file first. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 102 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size()); | 101 LocalReaderProxy proxy(file_reader.Pass(), expected_content.size()); |
| 103 | 102 |
| 104 // Make sure the read contant is as same as the file. | 103 // Make sure the read contant is as same as the file. |
| 105 std::string content; | 104 std::string content; |
| 106 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); | 105 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); |
| 107 EXPECT_EQ(expected_content, content); | 106 EXPECT_EQ(expected_content, content); |
| 108 } | 107 } |
| 109 | 108 |
| 110 class NetworkReaderProxyTest : public ::testing::Test { | 109 class NetworkReaderProxyTest : public ::testing::Test { |
| 111 protected: | 110 protected: |
| 112 NetworkReaderProxyTest() : io_thread_(BrowserThread::IO, &message_loop_) { | 111 NetworkReaderProxyTest() : thread_bundle_(IO_MAINLOOP) { |
| 113 } | 112 } |
| 114 | 113 |
| 115 base::MessageLoopForIO message_loop_; | 114 content::TestBrowserThreadBundle thread_bundle_; |
| 116 content::TestBrowserThread io_thread_; | |
| 117 }; | 115 }; |
| 118 | 116 |
| 119 TEST_F(NetworkReaderProxyTest, EmptyFile) { | 117 TEST_F(NetworkReaderProxyTest, EmptyFile) { |
| 120 NetworkReaderProxy proxy(0, 0, base::Bind(&base::DoNothing)); | 118 NetworkReaderProxy proxy(0, 0, base::Bind(&base::DoNothing)); |
| 121 | 119 |
| 122 net::TestCompletionCallback callback; | 120 net::TestCompletionCallback callback; |
| 123 const int kBufferSize = 10; | 121 const int kBufferSize = 10; |
| 124 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 122 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 125 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 123 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 126 | 124 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Destroy the instance before the network operation is completed. | 279 // Destroy the instance before the network operation is completed. |
| 282 // The cancelling callback should be called. | 280 // The cancelling callback should be called. |
| 283 } | 281 } |
| 284 EXPECT_EQ(1, num_called); | 282 EXPECT_EQ(1, num_called); |
| 285 } | 283 } |
| 286 | 284 |
| 287 } // namespace internal | 285 } // namespace internal |
| 288 | 286 |
| 289 class DriveFileStreamReaderTest : public ::testing::Test { | 287 class DriveFileStreamReaderTest : public ::testing::Test { |
| 290 protected: | 288 protected: |
| 291 DriveFileStreamReaderTest() | 289 DriveFileStreamReaderTest() : thread_bundle_(IO_MAINLOOP) { |
| 292 : ui_thread_(BrowserThread::UI), | |
| 293 io_thread_(BrowserThread::IO, &message_loop_) { | |
| 294 } | 290 } |
| 295 | 291 |
| 296 virtual void SetUp() OVERRIDE { | 292 virtual void SetUp() OVERRIDE { |
| 297 ui_thread_.Start(); | |
| 298 | |
| 299 worker_thread_.reset(new base::Thread("DriveFileStreamReaderTest")); | 293 worker_thread_.reset(new base::Thread("DriveFileStreamReaderTest")); |
| 300 ASSERT_TRUE(worker_thread_->Start()); | 294 ASSERT_TRUE(worker_thread_->Start()); |
| 301 | 295 |
| 302 BrowserThread::PostTaskAndReply( | |
| 303 BrowserThread::UI, | |
| 304 FROM_HERE, | |
| 305 base::Bind(&DriveFileStreamReaderTest::SetUpOnUIThread, | |
| 306 base::Unretained(this)), | |
| 307 base::MessageLoop::QuitClosure()); | |
| 308 message_loop_.Run(); | |
| 309 } | |
| 310 | |
| 311 virtual void TearDown() OVERRIDE { | |
| 312 BrowserThread::PostTaskAndReply( | |
| 313 BrowserThread::UI, | |
| 314 FROM_HERE, | |
| 315 base::Bind(&DriveFileStreamReaderTest::TearDownOnUIThread, | |
| 316 base::Unretained(this)), | |
| 317 base::MessageLoop::QuitClosure()); | |
| 318 message_loop_.Run(); | |
| 319 | |
| 320 worker_thread_.reset(); | |
| 321 } | |
| 322 | |
| 323 void SetUpOnUIThread() { | |
| 324 // Initialize FakeDriveService. | 296 // Initialize FakeDriveService. |
| 325 fake_drive_service_.reset(new google_apis::FakeDriveService); | 297 fake_drive_service_.reset(new google_apis::FakeDriveService); |
| 326 fake_drive_service_->LoadResourceListForWapi( | 298 fake_drive_service_->LoadResourceListForWapi( |
| 327 "chromeos/gdata/root_feed.json"); | 299 "chromeos/gdata/root_feed.json"); |
| 328 fake_drive_service_->LoadAccountMetadataForWapi( | 300 fake_drive_service_->LoadAccountMetadataForWapi( |
| 329 "chromeos/gdata/account_metadata.json"); | 301 "chromeos/gdata/account_metadata.json"); |
| 330 | 302 |
| 331 // Create a testee instance. | 303 // Create a testee instance. |
| 332 fake_file_system_.reset( | 304 fake_file_system_.reset( |
| 333 new test_util::FakeFileSystem(fake_drive_service_.get())); | 305 new test_util::FakeFileSystem(fake_drive_service_.get())); |
| 334 fake_file_system_->Initialize(); | 306 fake_file_system_->Initialize(); |
| 335 } | 307 } |
| 336 | 308 |
| 337 void TearDownOnUIThread() { | 309 virtual void TearDown() OVERRIDE { |
| 338 fake_file_system_.reset(); | 310 fake_file_system_.reset(); |
| 339 fake_drive_service_.reset(); | 311 fake_drive_service_.reset(); |
| 312 |
| 313 worker_thread_.reset(); |
| 340 } | 314 } |
| 341 | 315 |
| 342 FileSystemInterface* GetFileSystem() { | 316 FileSystemInterface* GetFileSystem() { |
| 343 return fake_file_system_.get(); | 317 return fake_file_system_.get(); |
| 344 } | 318 } |
| 345 | 319 |
| 346 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { | 320 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { |
| 347 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem, | 321 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem, |
| 348 base::Unretained(this)); | 322 base::Unretained(this)); |
| 349 } | 323 } |
| 350 | 324 |
| 351 base::MessageLoopForIO message_loop_; | 325 content::TestBrowserThreadBundle thread_bundle_; |
| 352 content::TestBrowserThread ui_thread_; | |
| 353 content::TestBrowserThread io_thread_; | |
| 354 | 326 |
| 355 scoped_ptr<base::Thread> worker_thread_; | 327 scoped_ptr<base::Thread> worker_thread_; |
| 356 | 328 |
| 357 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 329 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
| 358 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 330 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; |
| 359 }; | 331 }; |
| 360 | 332 |
| 361 TEST_F(DriveFileStreamReaderTest, Read) { | 333 TEST_F(DriveFileStreamReaderTest, Read) { |
| 362 const base::FilePath kDriveFile = | 334 const base::FilePath kDriveFile = |
| 363 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 335 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 google_apis::CreateComposedCallback( | 485 google_apis::CreateComposedCallback( |
| 514 base::Bind(&google_apis::test_util::RunAndQuit), | 486 base::Bind(&google_apis::test_util::RunAndQuit), |
| 515 google_apis::test_util::CreateCopyResultCallback( | 487 google_apis::test_util::CreateCopyResultCallback( |
| 516 &error, &entry))); | 488 &error, &entry))); |
| 517 message_loop_.Run(); | 489 message_loop_.Run(); |
| 518 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); | 490 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); |
| 519 EXPECT_FALSE(entry); | 491 EXPECT_FALSE(entry); |
| 520 } | 492 } |
| 521 | 493 |
| 522 } // namespace drive | 494 } // namespace drive |
| OLD | NEW |