Chromium Code Reviews| Index: third_party/WebKit/Source/platform/image-decoders/SegmentStreamTest.cpp |
| diff --git a/third_party/WebKit/Source/platform/image-decoders/SegmentStreamTest.cpp b/third_party/WebKit/Source/platform/image-decoders/SegmentStreamTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..402cb53d17aad65e3a315d4f0985c5ab87401881 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/image-decoders/SegmentStreamTest.cpp |
| @@ -0,0 +1,589 @@ |
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "platform/image-decoders/SegmentStream.h" |
| + |
| +#include <memory> |
| +#include "platform/SharedBuffer.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "wtf/PtrUtil.h" |
| + |
| +// SegmentStream has 4 accessors which do not alter state: |
|
scroggo_chromium
2017/04/17 20:04:56
I have not yet reviewed this file.
|
| +// - isCleared() |
| +// - isAtEnd() |
| +// - getPosition() |
| +// - getLength() |
| +// |
| +// For every operation which changes state we can test: |
| +// - the operation completed as expected, |
| +// - the accessors did not change, and/or |
| +// - the accessors changed in the way we expected. |
| +// |
| +// There are actually 2 more accessors: |
| +// - hasPosition() |
| +// - hasLength() |
| +// but these should always return true to indicate that we can call getLength() |
| +// for example. So let's not add them to every state changing operation and add |
| +// needless complexity. |
| + |
| +namespace { |
| + |
| +constexpr size_t kBufferAllocationSize = 20; |
| +constexpr size_t kInsideBufferPosition = 10; |
| +constexpr size_t kPastEndOfBufferPosition = 30; |
| + |
| +testing::AssertionResult IsCleared(const blink::SegmentStream&); |
| +testing::AssertionResult IsAtEnd(const blink::SegmentStream&); |
| +testing::AssertionResult PositionIsZero(const blink::SegmentStream&); |
| +testing::AssertionResult PositionIsInsideBuffer(const blink::SegmentStream&); |
| +testing::AssertionResult PositionIsAtEndOfBuffer(const blink::SegmentStream&); |
| +testing::AssertionResult LengthIsZero(const blink::SegmentStream&); |
| +testing::AssertionResult LengthIsAllocationSize(const blink::SegmentStream&); |
| + |
| +// We want to keep the buffer alive for the same duration as the SegmentStream |
| +struct ReadyToReadSegmentStream { |
| + ReadyToReadSegmentStream() = default; |
| + ReadyToReadSegmentStream(const ReadyToReadSegmentStream&) = delete; |
| + ReadyToReadSegmentStream(ReadyToReadSegmentStream&&) = default; |
| + ReadyToReadSegmentStream& operator=(const ReadyToReadSegmentStream&) = delete; |
| + ReadyToReadSegmentStream& operator=(ReadyToReadSegmentStream&&) = default; |
| + |
| + std::unique_ptr<char[]> buffer; |
| + blink::SegmentStream segment_stream; |
| +}; |
| + |
| +void CreateReadableSegmentStream(ReadyToReadSegmentStream& readable_stream); |
| + |
| +} // namespace |
| + |
| +namespace blink { |
| + |
| +TEST(SegmentStreamTest, DefaultConstructorShouldSetIsCleared) { |
| + SegmentStream segment_stream; |
| + |
| + ASSERT_TRUE(IsCleared(segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, DefaultConstructorShouldSetIsAtEnd) { |
| + SegmentStream segment_stream; |
| + |
| + ASSERT_TRUE(IsAtEnd(segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, DefaultContructorShouldClearPosition) { |
| + SegmentStream segment_stream; |
| + |
| + ASSERT_TRUE(PositionIsZero(segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, DefaultConstructorShouldHaveZeroLength) { |
| + SegmentStream segment_stream; |
| + |
| + ASSERT_TRUE(LengthIsZero(segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldUnsetIsCleared) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldUnsetIsAtEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldNotChangePosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldUpdateLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldSetIsClearedWhenSetToNull) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldSetIsAtEndWhenSetToNull) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldNotChangePositionWhenSetToNull) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(amount_read, kInsideBufferPosition); |
| + const size_t pre_nulled_position = |
| + readable_stream.segment_stream.getPosition(); |
| + ASSERT_EQ(amount_read, pre_nulled_position); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), pre_nulled_position); |
| +} |
| + |
| +TEST(SegmentStreamTest, SetReaderShouldClearLengthWhenSetToNull) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_TRUE(LengthIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldConsumeBuffer) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(amount_read, kInsideBufferPosition); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldNotClear) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldUpdateIsAtEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kBufferAllocationSize); |
| + ASSERT_EQ(amount_read, kBufferAllocationSize); |
| + ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldUpdatePosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), amount_read); |
| + ASSERT_TRUE(PositionIsInsideBuffer(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldNotChangeLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const size_t length = readable_stream.segment_stream.getLength(); |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getLength(), length); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldNotConsumeBufferWhenCleared) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const auto read_buffer_as_void_ptr = |
| + reinterpret_cast<void*>(read_buffer.get()); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| + |
| + ASSERT_EQ(0u, readable_stream.segment_stream.read(read_buffer_as_void_ptr, |
| + kInsideBufferPosition)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldConsumeBufferWithoutGoingPastTheEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kPastEndOfBufferPosition); |
| + ASSERT_EQ(amount_read, readable_stream.segment_stream.getLength()); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldSetIsAtEndWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), |
| + kPastEndOfBufferPosition); |
| + ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, ReadShouldTruncatePositionWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + const size_t amount_read = readable_stream.segment_stream.read( |
| + read_buffer.get(), kPastEndOfBufferPosition); |
| + ASSERT_EQ(amount_read, readable_stream.segment_stream.getPosition()); |
| + ASSERT_TRUE(PositionIsAtEndOfBuffer(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldCconsumeBuffer) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const size_t amount_peeked = readable_stream.segment_stream.peek( |
| + peek_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(amount_peeked, kInsideBufferPosition); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotClear) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotUpdateIsAtEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| + const size_t amount_peeked = readable_stream.segment_stream.peek( |
| + peek_buffer.get(), kBufferAllocationSize); |
| + ASSERT_EQ(amount_peeked, kBufferAllocationSize); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotUpdatePosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotChangeLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const size_t length = readable_stream.segment_stream.getLength(); |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getLength(), length); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotConsumeBufferWhenCleared) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + const auto peek_buffer_as_void_ptr = |
| + reinterpret_cast<void*>(peek_buffer.get()); |
| + |
| + readable_stream.segment_stream.SetReader(nullptr, true); |
| + ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| + |
| + ASSERT_EQ(0u, readable_stream.segment_stream.peek(peek_buffer_as_void_ptr, |
| + kInsideBufferPosition)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldConsumeBufferWithoutGoingPastTheEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + const size_t amount_peeked = readable_stream.segment_stream.peek( |
| + peek_buffer.get(), kPastEndOfBufferPosition); |
| + ASSERT_EQ(amount_peeked, readable_stream.segment_stream.getLength()); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotSetIsAtEndWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + readable_stream.segment_stream.peek(peek_buffer.get(), |
| + kPastEndOfBufferPosition); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, PeekShouldNotTruncatePositionWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| + readable_stream.segment_stream.peek(peek_buffer.get(), |
| + kPastEndOfBufferPosition); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, RewindShouldNotClear) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.rewind(); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, RewindShouldNotSetAtEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.rewind(); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, RewindShouldResetPosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + ASSERT_TRUE(PositionIsInsideBuffer(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.rewind(); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, RewindShouldNotChangeLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| + readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| + const size_t pre_rewind_length = readable_stream.segment_stream.getLength(); |
| + |
| + readable_stream.segment_stream.rewind(); |
| + ASSERT_EQ(readable_stream.segment_stream.getLength(), pre_rewind_length); |
| +} |
| + |
| +TEST(SegmentStreamTest, HasPositionShouldBeSupported) { |
| + blink::SegmentStream segment_stream; |
| + ASSERT_TRUE(segment_stream.hasPosition()); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldNotSetIsCleared) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.seek(kInsideBufferPosition); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldNotSetIsAtEndWhenSeekingInsideTheBuffer) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.seek(kInsideBufferPosition); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldSetIsAtEndWhenSeekingToTheEndOfTheBuffer) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.seek(kBufferAllocationSize); |
| + ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldUpdatePosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.seek(kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| + kInsideBufferPosition); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldTruncatePositionWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.seek(kPastEndOfBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| + kBufferAllocationSize); |
| +} |
| + |
| +TEST(SegmentStreamTest, SeekShouldNotUpdateLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + readable_stream.segment_stream.seek(kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getLength(), kBufferAllocationSize); |
| +} |
| + |
| +TEST(SegmentStreamTest, MoveShouldNotSetCleared) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + readable_stream.segment_stream.move(kInsideBufferPosition); |
| + ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, MoveShouldUpdatePosition) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + readable_stream.segment_stream.move(kInsideBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| + kInsideBufferPosition); |
| +} |
| + |
| +TEST(SegmentStreamTest, MoveShouldTruncatePositionWhenPastEnd) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + readable_stream.segment_stream.move(kPastEndOfBufferPosition); |
| + ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| + kBufferAllocationSize); |
| +} |
| + |
| +TEST(SegmentStreamTest, MoveShouldClampPositionToZeroWhenGoingNegative) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + |
| + // Move the position off of 0 |
| + const auto read_buffer = WTF::MakeUnique<char[]>(size_t{1}); |
| + readable_stream.segment_stream.read(read_buffer.get(), 1); |
| + ASSERT_FALSE(PositionIsZero(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.move(-2); |
| + ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, MoveShouldNotChangeLength) { |
| + ReadyToReadSegmentStream readable_stream; |
| + CreateReadableSegmentStream(readable_stream); |
| + ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| + |
| + readable_stream.segment_stream.move(kInsideBufferPosition); |
| + ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| +} |
| + |
| +TEST(SegmentStreamTest, HasLengthShouldBeSupported) { |
| + blink::SegmentStream segment_stream; |
| + ASSERT_TRUE(segment_stream.hasLength()); |
| +} |
| + |
| +} // namespace blink |
| + |
| +namespace { |
| + |
| +testing::AssertionResult IsCleared(const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.IsCleared()) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() << "SegmentStream is not clear"; |
| +} |
| + |
| +testing::AssertionResult IsAtEnd(const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.isAtEnd()) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() << "SegmentStream is not at the end"; |
| +} |
| + |
| +testing::AssertionResult PositionIsZero( |
| + const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.getPosition() == 0ul) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() << "SegmentStream position is not 0"; |
| +} |
| + |
| +testing::AssertionResult PositionIsInsideBuffer( |
| + const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.getPosition() == kInsideBufferPosition) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() |
| + << "SegmentStream position is not inside the buffer"; |
| +} |
| + |
| +testing::AssertionResult PositionIsAtEndOfBuffer( |
| + const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.getPosition() == kBufferAllocationSize) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() |
| + << "SegmentStream position is not at the end of the buffer"; |
| +} |
| + |
| +testing::AssertionResult LengthIsZero( |
| + const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.getLength() == 0ul) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() << "SegmentStream length is not 0"; |
| +} |
| + |
| +testing::AssertionResult LengthIsAllocationSize( |
| + const blink::SegmentStream& segment_stream) { |
| + if (segment_stream.getLength() == kBufferAllocationSize) |
| + return testing::AssertionSuccess(); |
| + |
| + return testing::AssertionFailure() |
| + << "SegmentStream length is not the allocation size"; |
| +} |
| + |
| +// We want to keep the buffer alive for the same duration as the SegmentStream |
| + |
| +void CreateReadableSegmentStream(ReadyToReadSegmentStream& readable_stream) { |
| + readable_stream.buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| + |
| + auto shared_buffer = blink::SharedBuffer::Create(readable_stream.buffer.get(), |
| + kBufferAllocationSize); |
| + auto segment_reader = |
| + blink::SegmentReader::CreateFromSharedBuffer(std::move(shared_buffer)); |
| + readable_stream.segment_stream.SetReader(segment_reader.Get(), true); |
| +} |
| + |
| +} // namespace |