OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/test/test_timeouts.h" | 16 #include "base/test/test_timeouts.h" |
17 #include "net/base/capturing_net_log.h" | 17 #include "net/base/capturing_net_log.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
20 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
23 | 23 |
| 24 #if defined(OS_ANDROID) |
| 25 #include "base/test/test_file_util.h" |
| 26 #endif |
| 27 |
24 namespace net { | 28 namespace net { |
25 | 29 |
26 namespace { | 30 namespace { |
27 | 31 |
28 const char kTestData[] = "0123456789"; | 32 const char kTestData[] = "0123456789"; |
29 const int kTestDataSize = arraysize(kTestData) - 1; | 33 const int kTestDataSize = arraysize(kTestData) - 1; |
30 | 34 |
31 // Creates an IOBufferWithSize that contains the kTestDataSize. | 35 // Creates an IOBufferWithSize that contains the kTestDataSize. |
32 IOBufferWithSize* CreateTestDataBuffer() { | 36 IOBufferWithSize* CreateTestDataBuffer() { |
33 IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize); | 37 IOBufferWithSize* buf = new IOBufferWithSize(kTestDataSize); |
(...skipping 1132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1166 scoped_refptr<IOBuffer> buf = new IOBuffer(1); | 1170 scoped_refptr<IOBuffer> buf = new IOBuffer(1); |
1167 TestCompletionCallback callback; | 1171 TestCompletionCallback callback; |
1168 int rv = stream->Read(buf.get(), 1, callback.callback()); | 1172 int rv = stream->Read(buf.get(), 1, callback.callback()); |
1169 if (rv == ERR_IO_PENDING) | 1173 if (rv == ERR_IO_PENDING) |
1170 rv = callback.WaitForResult(); | 1174 rv = callback.WaitForResult(); |
1171 EXPECT_LT(rv, 0); | 1175 EXPECT_LT(rv, 0); |
1172 | 1176 |
1173 base::ClosePlatformFile(file); | 1177 base::ClosePlatformFile(file); |
1174 } | 1178 } |
1175 | 1179 |
| 1180 #if defined(OS_ANDROID) |
| 1181 TEST_F(FileStreamTest, ContentUriAsyncRead) { |
| 1182 base::FilePath test_dir; |
| 1183 PathService::Get(base::DIR_SOURCE_ROOT, &test_dir); |
| 1184 test_dir = test_dir.AppendASCII("net"); |
| 1185 test_dir = test_dir.AppendASCII("data"); |
| 1186 test_dir = test_dir.AppendASCII("file_stream_unittest"); |
| 1187 ASSERT_TRUE(base::PathExists(test_dir)); |
| 1188 base::FilePath image_file = test_dir.Append(FILE_PATH_LITERAL("red.png")); |
| 1189 |
| 1190 // Insert the image into MediaStore. MediaStore will do some conversions, and |
| 1191 // return the content URI. |
| 1192 base::FilePath path = file_util::InsertImageIntoMediaStore(image_file); |
| 1193 EXPECT_TRUE(path.IsContentUri()); |
| 1194 EXPECT_TRUE(base::PathExists(path)); |
| 1195 int64 file_size; |
| 1196 EXPECT_TRUE(file_util::GetFileSize(path, &file_size)); |
| 1197 EXPECT_LT(0, file_size); |
| 1198 |
| 1199 FileStream stream(NULL, base::MessageLoopProxy::current()); |
| 1200 int flags = base::PLATFORM_FILE_OPEN | |
| 1201 base::PLATFORM_FILE_READ | |
| 1202 base::PLATFORM_FILE_ASYNC; |
| 1203 TestCompletionCallback callback; |
| 1204 int rv = stream.Open(path, flags, callback.callback()); |
| 1205 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1206 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1207 |
| 1208 int64 total_bytes_avail = stream.Available(); |
| 1209 EXPECT_EQ(file_size, total_bytes_avail); |
| 1210 |
| 1211 int total_bytes_read = 0; |
| 1212 |
| 1213 std::string data_read; |
| 1214 for (;;) { |
| 1215 scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
| 1216 rv = stream.Read(buf.get(), buf->size(), callback.callback()); |
| 1217 if (rv == ERR_IO_PENDING) |
| 1218 rv = callback.WaitForResult(); |
| 1219 EXPECT_LE(0, rv); |
| 1220 if (rv <= 0) |
| 1221 break; |
| 1222 total_bytes_read += rv; |
| 1223 data_read.append(buf->data(), rv); |
| 1224 } |
| 1225 EXPECT_EQ(file_size, total_bytes_read); |
| 1226 } |
| 1227 #endif |
| 1228 |
1176 } // namespace | 1229 } // namespace |
1177 | 1230 |
1178 } // namespace net | 1231 } // namespace net |
OLD | NEW |