Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Unified Diff: third_party/WebKit/Source/platform/image-decoders/SegmentStreamTest.cpp

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: Adding SegmentStreamTest.cpp, more null checks, and updating m_hasReadAllContents more. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..629d6ed61e2f4030a75bccafa30d0576be03e32a
--- /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:
+// - 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, defaultConstructor_setsIsCleared) {
+ SegmentStream segment_stream;
cblume 2017/04/08 01:19:20 A lot of variables like this one use the new Blink
+
+ ASSERT_TRUE(IsCleared(segment_stream));
+}
+
+TEST(SegmentStreamTest, defaultConstructor_setsIsAtEnd) {
+ SegmentStream segment_stream;
+
+ ASSERT_TRUE(IsAtEnd(segment_stream));
+}
+
+TEST(SegmentStreamTest, defaultContructor_clearsPosition) {
+ SegmentStream segment_stream;
+
+ ASSERT_TRUE(PositionIsZero(segment_stream));
+}
+
+TEST(SegmentStreamTest, defaultConstructor_hasZeroLength) {
+ SegmentStream segment_stream;
+
+ ASSERT_TRUE(LengthIsZero(segment_stream));
+}
+
+TEST(SegmentStreamTest, setReader_unsetsIsCleared) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ ASSERT_FALSE(IsCleared(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, setReader_unsetsIsAtEnd) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, setReader_doesNotChangePosition) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, setReader_updatesLength) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, setReader_setsIsCleared_whenSetToNull) {
+ 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, setReader_setsIsAtEnd_whenSetToNull) {
+ 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, setReader_doesNotChangePosition_whenSetToNull) {
+ 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, setReader_clearsLength_whenSetToNull) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.setReader(nullptr, true);
+ ASSERT_TRUE(LengthIsZero(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, read_consumesBuffer) {
+ 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, read_doesNotClear) {
+ 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, read_updatesIsAtEnd) {
+ 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, read_updatesPosition) {
+ 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, read_DoesNotChangeLength) {
+ 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, read_doesNotConsumeBuffer_whenCleared) {
+ 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, read_consumesBuffer_withoutGoingPastTheEnd) {
+ 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, read_setIsAtEnd_whenPastEnd) {
+ 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, read_truncatesPosition_whenPastEnd) {
+ 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, peek_consumesBuffer) {
+ 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, peek_doesNotClear) {
+ 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, peek_doesNotUpdateIsAtEnd) {
+ 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, peek_doesNotUpdatePosition) {
+ 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, peek_DoesNotChangeLength) {
+ 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, peek_doesNotConsumeBuffer_whenCleared) {
+ 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, peek_consumesBuffer_withoutGoingPastTheEnd) {
+ 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, peek_doesNotSetIsAtEnd_whenPastEnd) {
+ 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, peek_doesNotTruncatesPosition_whenPastEnd) {
+ 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, rewind_doesNotClear) {
+ 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, rewind_doesNotSetAtEnd) {
+ 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, rewind_resetsPosition) {
+ 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, rewind_doesNotChangeLength) {
+ 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, hasPosition_supported) {
+ blink::SegmentStream segment_stream;
+ ASSERT_TRUE(segment_stream.hasPosition());
+}
+
+TEST(SegmentStreamTest, seek_doesNotSetIsCleared) {
+ 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, seek_doesNotSetIsAtEnd_whenSeekingInsideTheBuffer) {
+ 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, seek_setsIsAtEnd_whenSeekingToTheEndOfTheBuffer) {
+ 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, seek_updatesPosition) {
+ 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, seek_truncatesPosition_whenPastEnd) {
+ 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, seek_doesNotUpdateLength) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.seek(kInsideBufferPosition);
+ ASSERT_EQ(readable_stream.segment_stream.getLength(), kBufferAllocationSize);
+}
+
+TEST(SegmentStreamTest, move_doesNotSetCleared) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.move(kInsideBufferPosition);
+ ASSERT_FALSE(IsCleared(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, move_updatesPosition) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.move(kInsideBufferPosition);
+ ASSERT_EQ(readable_stream.segment_stream.getPosition(),
+ kInsideBufferPosition);
+}
+
+TEST(SegmentStreamTest, move_truncatesPosition_whenPastEnd) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.move(kPastEndOfBufferPosition);
+ ASSERT_EQ(readable_stream.segment_stream.getPosition(),
+ kBufferAllocationSize);
+}
+
+TEST(SegmentStreamTest, move_clampsPositionToZero_whenGoingNegative) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ // Move the position off of 0
+ const auto read_buffer = WTF::makeUnique<char[]>(kInsideBufferPosition);
+ readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition);
+ ASSERT_FALSE(PositionIsZero(readable_stream.segment_stream));
+
+ readable_stream.segment_stream.move(-(kInsideBufferPosition + 1));
+ ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, move_doesNotChangeLength) {
+ 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, hasLength_supported) {
+ blink::SegmentStream segment_stream;
+ ASSERT_TRUE(segment_stream.hasLength());
+}
+
+} // namespace blink
+
+namespace {
+
+testing::AssertionResult IsCleared(const blink::SegmentStream& segmentStream) {
+ if (segmentStream.isCleared())
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure() << "SegmentStream is not clear";
+}
+
+testing::AssertionResult IsAtEnd(const blink::SegmentStream& segmentStream) {
+ if (segmentStream.isAtEnd())
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure() << "SegmentStream is not at the end";
+}
+
+testing::AssertionResult PositionIsZero(
+ const blink::SegmentStream& segmentStream) {
+ if (segmentStream.getPosition() == 0ul)
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure() << "SegmentStream position is not 0";
+}
+
+testing::AssertionResult PositionIsInsideBuffer(
+ const blink::SegmentStream& segmentStream) {
+ if (segmentStream.getPosition() == kInsideBufferPosition)
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure()
+ << "SegmentStream position is not inside the buffer";
+}
+
+testing::AssertionResult PositionIsAtEndOfBuffer(
+ const blink::SegmentStream& segmentStream) {
+ if (segmentStream.getPosition() == kBufferAllocationSize)
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure()
+ << "SegmentStream position is not at the end of the buffer";
+}
+
+testing::AssertionResult LengthIsZero(
+ const blink::SegmentStream& segmentStream) {
+ if (segmentStream.getLength() == 0ul)
+ return testing::AssertionSuccess();
+
+ return testing::AssertionFailure() << "SegmentStream length is not 0";
+}
+
+testing::AssertionResult LengthIsAllocationSize(
+ const blink::SegmentStream& segmentStream) {
+ if (segmentStream.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

Powered by Google App Engine
This is Rietveld 408576698