| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/child/web_data_consumer_handle_impl.h" | 5 #include "content/child/web_data_consumer_handle_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 186 |
| 187 class WebDataConsumerHandleImplTest : public ::testing::Test { | 187 class WebDataConsumerHandleImplTest : public ::testing::Test { |
| 188 public: | 188 public: |
| 189 typedef WebDataConsumerHandle::Result Result; | 189 typedef WebDataConsumerHandle::Result Result; |
| 190 | 190 |
| 191 void SetUp() override { | 191 void SetUp() override { |
| 192 MojoCreateDataPipeOptions options; | 192 MojoCreateDataPipeOptions options; |
| 193 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 193 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 194 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 194 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 195 options.element_num_bytes = 1; | 195 options.element_num_bytes = 1; |
| 196 options.capacity_num_bytes = 4; | 196 options.capacity_num_bytes = kDataPipeCapacity; |
| 197 | 197 |
| 198 MojoResult result = mojo::CreateDataPipe(&options, &producer_, &consumer_); | 198 MojoResult result = mojo::CreateDataPipe(&options, &producer_, &consumer_); |
| 199 ASSERT_EQ(MOJO_RESULT_OK, result); | 199 ASSERT_EQ(MOJO_RESULT_OK, result); |
| 200 } | 200 } |
| 201 | 201 |
| 202 protected: |
| 203 static constexpr int kDataPipeCapacity = 4; |
| 204 |
| 202 // This function can be blocked if the associated consumer doesn't consume | 205 // This function can be blocked if the associated consumer doesn't consume |
| 203 // the data. | 206 // the data. |
| 204 std::string ProduceData(size_t total_size) { | 207 std::string ProduceData(size_t total_size) { |
| 205 int index = 0; | 208 int index = 0; |
| 206 std::string expected; | 209 std::string expected; |
| 207 for (size_t i = 0; i < total_size; ++i) { | 210 for (size_t i = 0; i < total_size; ++i) { |
| 208 expected += static_cast<char>(index + 'a'); | 211 expected += static_cast<char>(index + 'a'); |
| 209 index = (37 * index + 11) % 26; | 212 index = (37 * index + 11) % 26; |
| 210 } | 213 } |
| 211 | 214 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 272 |
| 270 std::string expected = ProduceData(24 * 1024); | 273 std::string expected = ProduceData(24 * 1024); |
| 271 producer_.reset(); | 274 producer_.reset(); |
| 272 | 275 |
| 273 run_loop.Run(); | 276 run_loop.Run(); |
| 274 t.Stop(); | 277 t.Stop(); |
| 275 | 278 |
| 276 EXPECT_EQ(expected, operation->result()); | 279 EXPECT_EQ(expected, operation->result()); |
| 277 } | 280 } |
| 278 | 281 |
| 282 TEST_F(WebDataConsumerHandleImplTest, ZeroSizeRead) { |
| 283 ASSERT_GT(kDataPipeCapacity - 1, 0); |
| 284 constexpr size_t data_size = kDataPipeCapacity - 1; |
| 285 std::string expected = ProduceData(data_size); |
| 286 producer_.reset(); |
| 287 std::unique_ptr<WebDataConsumerHandleImpl> handle( |
| 288 new WebDataConsumerHandleImpl(std::move(consumer_))); |
| 289 std::unique_ptr<WebDataConsumerHandle::Reader> reader( |
| 290 handle->obtainReader(nullptr)); |
| 291 |
| 292 size_t read_size; |
| 293 WebDataConsumerHandle::Result rv = |
| 294 reader->read(nullptr, 0, WebDataConsumerHandle::FlagNone, &read_size); |
| 295 EXPECT_EQ(WebDataConsumerHandle::Result::Ok, rv); |
| 296 |
| 297 char buffer[16]; |
| 298 rv = reader->read(&buffer, sizeof(buffer), WebDataConsumerHandle::FlagNone, |
| 299 &read_size); |
| 300 EXPECT_EQ(WebDataConsumerHandle::Result::Ok, rv); |
| 301 EXPECT_EQ(data_size, read_size); |
| 302 EXPECT_EQ(expected, std::string(buffer, read_size)); |
| 303 } |
| 304 |
| 279 } // namespace | 305 } // namespace |
| 280 | 306 |
| 281 } // namespace content | 307 } // namespace content |
| OLD | NEW |