| 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 TEST(LocalReaderProxyTest, Read) { | 40 TEST(LocalReaderProxyTest, Read) { |
| 41 // Prepare the test content. | 41 // Prepare the test content. |
| 42 const base::FilePath kTestFile( | 42 const base::FilePath kTestFile( |
| 43 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json")); | 43 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json")); |
| 44 std::string expected_content; | 44 std::string expected_content; |
| 45 ASSERT_TRUE(file_util::ReadFileToString(kTestFile, &expected_content)); | 45 ASSERT_TRUE(file_util::ReadFileToString(kTestFile, &expected_content)); |
| 46 | 46 |
| 47 // The LocalReaderProxy should live on IO thread. | 47 // The LocalReaderProxy should live on IO thread. |
| 48 MessageLoopForIO io_loop; | 48 base::MessageLoopForIO io_loop; |
| 49 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 49 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 50 | 50 |
| 51 { | 51 { |
| 52 // Open the file first. | 52 // Open the file first. |
| 53 scoped_ptr<net::FileStream> file_stream(new net::FileStream(NULL)); | 53 scoped_ptr<net::FileStream> file_stream(new net::FileStream(NULL)); |
| 54 net::TestCompletionCallback callback; | 54 net::TestCompletionCallback callback; |
| 55 int result = file_stream->Open( | 55 int result = file_stream->Open( |
| 56 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json"), | 56 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json"), |
| 57 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | 57 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 58 base::PLATFORM_FILE_ASYNC, | 58 base::PLATFORM_FILE_ASYNC, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 83 EXPECT_EQ(expected_content, concatenated_content); | 83 EXPECT_EQ(expected_content, concatenated_content); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // For graceful shutdown, we wait for that the FileStream used above is | 86 // For graceful shutdown, we wait for that the FileStream used above is |
| 87 // actually closed. | 87 // actually closed. |
| 88 test_util::WaitForFileStreamClosed(); | 88 test_util::WaitForFileStreamClosed(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 TEST(NetworkReaderProxyTest, EmptyFile) { | 91 TEST(NetworkReaderProxyTest, EmptyFile) { |
| 92 // The NetworkReaderProxy should live on IO thread. | 92 // The NetworkReaderProxy should live on IO thread. |
| 93 MessageLoopForIO io_loop; | 93 base::MessageLoopForIO io_loop; |
| 94 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 94 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 95 | 95 |
| 96 NetworkReaderProxy proxy(0, base::Bind(&base::DoNothing)); | 96 NetworkReaderProxy proxy(0, base::Bind(&base::DoNothing)); |
| 97 | 97 |
| 98 net::TestCompletionCallback callback; | 98 net::TestCompletionCallback callback; |
| 99 const int kBufferSize = 10; | 99 const int kBufferSize = 10; |
| 100 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 100 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 101 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 101 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 102 | 102 |
| 103 // For empty file, Read() should return 0 immediately. | 103 // For empty file, Read() should return 0 immediately. |
| 104 EXPECT_EQ(0, result); | 104 EXPECT_EQ(0, result); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST(NetworkReaderProxyTest, Read) { | 107 TEST(NetworkReaderProxyTest, Read) { |
| 108 // The NetworkReaderProxy should live on IO thread. | 108 // The NetworkReaderProxy should live on IO thread. |
| 109 MessageLoopForIO io_loop; | 109 base::MessageLoopForIO io_loop; |
| 110 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 110 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 111 | 111 |
| 112 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); | 112 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); |
| 113 | 113 |
| 114 net::TestCompletionCallback callback; | 114 net::TestCompletionCallback callback; |
| 115 const int kBufferSize = 3; | 115 const int kBufferSize = 3; |
| 116 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 116 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 117 | 117 |
| 118 // If no data is available yet, ERR_IO_PENDING should be returned. | 118 // If no data is available yet, ERR_IO_PENDING should be returned. |
| 119 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 119 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 148 EXPECT_EQ(2, result); | 148 EXPECT_EQ(2, result); |
| 149 EXPECT_EQ("ij", std::string(buffer->data(), result)); | 149 EXPECT_EQ("ij", std::string(buffer->data(), result)); |
| 150 | 150 |
| 151 // The whole data is read, so Read() should return 0 immediately by then. | 151 // The whole data is read, so Read() should return 0 immediately by then. |
| 152 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 152 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 153 EXPECT_EQ(0, result); | 153 EXPECT_EQ(0, result); |
| 154 } | 154 } |
| 155 | 155 |
| 156 TEST(NetworkReaderProxyTest, ErrorWithPendingCallback) { | 156 TEST(NetworkReaderProxyTest, ErrorWithPendingCallback) { |
| 157 // The NetworkReaderProxy should live on IO thread. | 157 // The NetworkReaderProxy should live on IO thread. |
| 158 MessageLoopForIO io_loop; | 158 base::MessageLoopForIO io_loop; |
| 159 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 159 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 160 | 160 |
| 161 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); | 161 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); |
| 162 | 162 |
| 163 net::TestCompletionCallback callback; | 163 net::TestCompletionCallback callback; |
| 164 const int kBufferSize = 3; | 164 const int kBufferSize = 3; |
| 165 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 165 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 166 | 166 |
| 167 // Set pending callback. | 167 // Set pending callback. |
| 168 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 168 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 169 EXPECT_EQ(net::ERR_IO_PENDING, result); | 169 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 170 | 170 |
| 171 // Emulate that an error is found. The callback should be called internally. | 171 // Emulate that an error is found. The callback should be called internally. |
| 172 proxy.OnCompleted(FILE_ERROR_FAILED); | 172 proxy.OnCompleted(FILE_ERROR_FAILED); |
| 173 result = callback.GetResult(result); | 173 result = callback.GetResult(result); |
| 174 EXPECT_EQ(net::ERR_FAILED, result); | 174 EXPECT_EQ(net::ERR_FAILED, result); |
| 175 | 175 |
| 176 // The next Read call should also return the same error code. | 176 // The next Read call should also return the same error code. |
| 177 EXPECT_EQ(net::ERR_FAILED, | 177 EXPECT_EQ(net::ERR_FAILED, |
| 178 proxy.Read(buffer.get(), kBufferSize, callback.callback())); | 178 proxy.Read(buffer.get(), kBufferSize, callback.callback())); |
| 179 } | 179 } |
| 180 | 180 |
| 181 TEST(NetworkReaderProxyTest, ErrorWithPendingData) { | 181 TEST(NetworkReaderProxyTest, ErrorWithPendingData) { |
| 182 // The NetworkReaderProxy should live on IO thread. | 182 // The NetworkReaderProxy should live on IO thread. |
| 183 MessageLoopForIO io_loop; | 183 base::MessageLoopForIO io_loop; |
| 184 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 184 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 185 | 185 |
| 186 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); | 186 NetworkReaderProxy proxy(10, base::Bind(&base::DoNothing)); |
| 187 | 187 |
| 188 net::TestCompletionCallback callback; | 188 net::TestCompletionCallback callback; |
| 189 const int kBufferSize = 3; | 189 const int kBufferSize = 3; |
| 190 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 190 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 191 | 191 |
| 192 // Supply the data before an error. | 192 // Supply the data before an error. |
| 193 scoped_ptr<std::string> data(new std::string("abcde")); | 193 scoped_ptr<std::string> data(new std::string("abcde")); |
| 194 proxy.OnGetContent(data.Pass()); | 194 proxy.OnGetContent(data.Pass()); |
| 195 | 195 |
| 196 // Emulate that an error is found. | 196 // Emulate that an error is found. |
| 197 proxy.OnCompleted(FILE_ERROR_FAILED); | 197 proxy.OnCompleted(FILE_ERROR_FAILED); |
| 198 | 198 |
| 199 // The next Read call should return the error code, even if there is | 199 // The next Read call should return the error code, even if there is |
| 200 // pending data (the pending data should be released in OnCompleted. | 200 // pending data (the pending data should be released in OnCompleted. |
| 201 EXPECT_EQ(net::ERR_FAILED, | 201 EXPECT_EQ(net::ERR_FAILED, |
| 202 proxy.Read(buffer.get(), kBufferSize, callback.callback())); | 202 proxy.Read(buffer.get(), kBufferSize, callback.callback())); |
| 203 } | 203 } |
| 204 | 204 |
| 205 TEST(NetworkReaderProxyTest, CancelJob) { | 205 TEST(NetworkReaderProxyTest, CancelJob) { |
| 206 // The NetworkReaderProxy should live on IO thread. | 206 // The NetworkReaderProxy should live on IO thread. |
| 207 MessageLoopForIO io_loop; | 207 base::MessageLoopForIO io_loop; |
| 208 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | 208 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); |
| 209 | 209 |
| 210 int num_called = 0; | 210 int num_called = 0; |
| 211 { | 211 { |
| 212 NetworkReaderProxy proxy(0, base::Bind(&IncrementCallback, &num_called)); | 212 NetworkReaderProxy proxy(0, base::Bind(&IncrementCallback, &num_called)); |
| 213 proxy.OnCompleted(FILE_ERROR_OK); | 213 proxy.OnCompleted(FILE_ERROR_OK); |
| 214 // Destroy the instance after the network operation is completed. | 214 // Destroy the instance after the network operation is completed. |
| 215 // The cancelling callback shouldn't be called. | 215 // The cancelling callback shouldn't be called. |
| 216 } | 216 } |
| 217 EXPECT_EQ(0, num_called); | 217 EXPECT_EQ(0, num_called); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 | 278 |
| 279 DriveFileSystemInterface* GetDriveFileSystem() { | 279 DriveFileSystemInterface* GetDriveFileSystem() { |
| 280 return fake_drive_file_system_.get(); | 280 return fake_drive_file_system_.get(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 DriveFileStreamReader::DriveFileSystemGetter GetDriveFileSystemGetter() { | 283 DriveFileStreamReader::DriveFileSystemGetter GetDriveFileSystemGetter() { |
| 284 return base::Bind(&DriveFileStreamReaderTest::GetDriveFileSystem, | 284 return base::Bind(&DriveFileStreamReaderTest::GetDriveFileSystem, |
| 285 base::Unretained(this)); | 285 base::Unretained(this)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 MessageLoopForIO message_loop_; | 288 base::MessageLoopForIO message_loop_; |
| 289 content::TestBrowserThread ui_thread_; | 289 content::TestBrowserThread ui_thread_; |
| 290 content::TestBrowserThread io_thread_; | 290 content::TestBrowserThread io_thread_; |
| 291 | 291 |
| 292 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 292 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
| 293 scoped_ptr<test_util::FakeDriveFileSystem> fake_drive_file_system_; | 293 scoped_ptr<test_util::FakeDriveFileSystem> fake_drive_file_system_; |
| 294 }; | 294 }; |
| 295 | 295 |
| 296 TEST_F(DriveFileStreamReaderTest, Read) { | 296 TEST_F(DriveFileStreamReaderTest, Read) { |
| 297 const base::FilePath kDriveFile = | 297 const base::FilePath kDriveFile = |
| 298 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 298 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 result = callback.GetResult(result); | 362 result = callback.GetResult(result); |
| 363 ASSERT_GT(result, 0); | 363 ASSERT_GT(result, 0); |
| 364 second_content.append(buffer->data(), result); | 364 second_content.append(buffer->data(), result); |
| 365 } | 365 } |
| 366 | 366 |
| 367 // The same content is expected. | 367 // The same content is expected. |
| 368 EXPECT_EQ(first_content, second_content); | 368 EXPECT_EQ(first_content, second_content); |
| 369 } | 369 } |
| 370 | 370 |
| 371 } // namespace drive | 371 } // namespace drive |
| OLD | NEW |