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