| 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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 rv = reader->Read(&buffer, sizeof(buffer), WebDataConsumerHandle::kFlagNone, | 302 rv = reader->Read(&buffer, sizeof(buffer), WebDataConsumerHandle::kFlagNone, |
| 303 &read_size); | 303 &read_size); |
| 304 EXPECT_EQ(WebDataConsumerHandle::Result::kOk, rv); | 304 EXPECT_EQ(WebDataConsumerHandle::Result::kOk, rv); |
| 305 EXPECT_EQ(data_size, read_size); | 305 EXPECT_EQ(data_size, read_size); |
| 306 EXPECT_EQ(expected, std::string(buffer, read_size)); | 306 EXPECT_EQ(expected, std::string(buffer, read_size)); |
| 307 | 307 |
| 308 rv = reader->Read(nullptr, 0, WebDataConsumerHandle::kFlagNone, &read_size); | 308 rv = reader->Read(nullptr, 0, WebDataConsumerHandle::kFlagNone, &read_size); |
| 309 EXPECT_EQ(WebDataConsumerHandle::Result::kDone, rv); | 309 EXPECT_EQ(WebDataConsumerHandle::Result::kDone, rv); |
| 310 } | 310 } |
| 311 | 311 |
| 312 class CountDidGetReadableClient : public blink::WebDataConsumerHandle::Client { |
| 313 public: |
| 314 ~CountDidGetReadableClient() override {} |
| 315 void DidGetReadable() override { num_did_get_readable_called_++; } |
| 316 int num_did_get_readable_called() { return num_did_get_readable_called_; } |
| 317 |
| 318 private: |
| 319 int num_did_get_readable_called_ = 0; |
| 320 }; |
| 321 |
| 322 TEST_F(WebDataConsumerHandleImplTest, DidGetReadable) { |
| 323 static constexpr size_t kBlockSize = kDataPipeCapacity / 3; |
| 324 static constexpr size_t kTotalSize = kBlockSize * 3; |
| 325 |
| 326 std::unique_ptr<CountDidGetReadableClient> client = |
| 327 base::MakeUnique<CountDidGetReadableClient>(); |
| 328 std::unique_ptr<WebDataConsumerHandleImpl> handle( |
| 329 new WebDataConsumerHandleImpl(std::move(consumer_))); |
| 330 std::unique_ptr<WebDataConsumerHandle::Reader> reader( |
| 331 handle->ObtainReader(client.get())); |
| 332 base::RunLoop().RunUntilIdle(); |
| 333 EXPECT_EQ(0, client->num_did_get_readable_called()); |
| 334 |
| 335 // Push three blocks. |
| 336 { |
| 337 std::string expected; |
| 338 int index = 0; |
| 339 for (size_t i = 0; i < kTotalSize; ++i) { |
| 340 expected += static_cast<char>(index + 'a'); |
| 341 index = (37 * index + 11) % 26; |
| 342 } |
| 343 uint32_t size = expected.size(); |
| 344 MojoResult rv = mojo::WriteDataRaw(producer_.get(), expected.data(), &size, |
| 345 MOJO_WRITE_DATA_FLAG_NONE); |
| 346 EXPECT_EQ(MOJO_RESULT_OK, rv); |
| 347 EXPECT_EQ(kTotalSize, size); |
| 348 } |
| 349 base::RunLoop().RunUntilIdle(); |
| 350 // |client| is notified the pipe gets ready. |
| 351 EXPECT_EQ(1, client->num_did_get_readable_called()); |
| 352 |
| 353 // Read a block. |
| 354 { |
| 355 char buffer[kBlockSize]; |
| 356 size_t size = 0; |
| 357 Result rv = reader->Read(&buffer, sizeof(buffer), |
| 358 WebDataConsumerHandle::kFlagNone, &size); |
| 359 EXPECT_EQ(Result::kOk, rv); |
| 360 EXPECT_EQ(sizeof(buffer), size); |
| 361 } |
| 362 base::RunLoop().RunUntilIdle(); |
| 363 // |client| is notified the pipe is still ready. |
| 364 EXPECT_EQ(2, client->num_did_get_readable_called()); |
| 365 |
| 366 // Read one more block. |
| 367 { |
| 368 const void* buffer = nullptr; |
| 369 size_t size = sizeof(buffer); |
| 370 Result rv = |
| 371 reader->BeginRead(&buffer, WebDataConsumerHandle::kFlagNone, &size); |
| 372 EXPECT_EQ(Result::kOk, rv); |
| 373 EXPECT_TRUE(buffer); |
| 374 EXPECT_EQ(kTotalSize - kBlockSize, size); |
| 375 base::RunLoop().RunUntilIdle(); |
| 376 // |client| is NOT notified until EndRead is called. |
| 377 EXPECT_EQ(2, client->num_did_get_readable_called()); |
| 378 |
| 379 rv = reader->EndRead(kBlockSize); |
| 380 EXPECT_EQ(Result::kOk, rv); |
| 381 } |
| 382 base::RunLoop().RunUntilIdle(); |
| 383 // |client| is notified the pipe is still ready. |
| 384 EXPECT_EQ(3, client->num_did_get_readable_called()); |
| 385 |
| 386 // Read the final block. |
| 387 { |
| 388 char buffer[kBlockSize]; |
| 389 size_t size = 0; |
| 390 Result rv = reader->Read(&buffer, sizeof(buffer), |
| 391 WebDataConsumerHandle::kFlagNone, &size); |
| 392 EXPECT_EQ(Result::kOk, rv); |
| 393 EXPECT_EQ(sizeof(buffer), size); |
| 394 } |
| 395 base::RunLoop().RunUntilIdle(); |
| 396 // |client| is NOT notified because the pipe doesn't have any data. |
| 397 EXPECT_EQ(3, client->num_did_get_readable_called()); |
| 398 } |
| 399 |
| 312 } // namespace | 400 } // namespace |
| 313 | 401 |
| 314 } // namespace content | 402 } // namespace content |
| OLD | NEW |