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

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

Issue 2565323003: Move gif image decoder to SkCodec (Closed)
Patch Set: More code review comments 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..c94dd8af03a7876c3ab5f3b524e88a66c7ec8446
--- /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 "platform/wtf/PtrUtil.h"
+#include "testing/gtest/include/gtest/gtest.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
scroggo_chromium 2017/04/25 15:31:41 I'm not sure this comment is necessary. I guess it
cblume 2017/04/26 10:09:43 You are right. I didn't realize SharedBuffer copie
+struct ReadyToReadSegmentStream {
+ ReadyToReadSegmentStream() = default;
scroggo_chromium 2017/04/25 15:31:41 I think this would be clearer if you keep the defa
+ 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);
scroggo_chromium 2017/04/25 15:31:40 Many of the above methods are pretty clear about w
cblume 2017/04/26 10:09:46 Done.
+
+} // 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) {
scroggo_chromium 2017/04/25 15:31:41 "SetReaderShouldUnsetIsAtEnd" seems misleading - i
cblume 2017/04/26 10:09:49 I think the position being 0 doesn't matter. But y
+ 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);
+ ASSERT_TRUE(IsCleared(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, SetReaderShouldSetIsAtEndWhenSetToNull) {
scroggo_chromium 2017/04/25 15:31:40 Some of these tests seem like they could be combin
cblume 2017/04/26 10:09:44 I completely agree. Ideally, only one test fails w
cblume 2017/04/26 18:17:52 I began working on this and realized it is not wha
cblume 2017/04/27 03:07:41 I have cleaned this up as much as I think makes se
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+ ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream));
+
+ readable_stream.segment_stream.SetReader(nullptr);
+ 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);
scroggo_chromium 2017/04/25 15:31:41 I think the expected value goes first (isn't that
cblume 2017/04/26 10:09:49 Oops, yeah. I caused this test to fail to see the
+ const size_t pre_nulled_position =
+ readable_stream.segment_stream.getPosition();
+ ASSERT_EQ(amount_read, pre_nulled_position);
+
+ readable_stream.segment_stream.SetReader(nullptr);
+ 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);
+ ASSERT_TRUE(LengthIsZero(readable_stream.segment_stream));
+}
+
+TEST(SegmentStreamTest, ReadShouldConsumeBuffer) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition);
scroggo_chromium 2017/04/25 15:31:40 Why not put this on the stack? char read_buffer
cblume 2017/04/26 10:09:47 Done.
+ 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);
+ 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) {
scroggo_chromium 2017/04/25 15:31:40 Consume* (not Cconsume)
cblume 2017/04/26 10:09:49 Thank you
+ 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);
+ 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, SeekShouldNotTruncatePositionWhenPastEnd) {
+ 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(),
+ kPastEndOfBufferPosition);
+}
+
+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, MoveShouldNotTruncatePositionWhenPastEnd) {
+ ReadyToReadSegmentStream readable_stream;
+ CreateReadableSegmentStream(readable_stream);
+
+ readable_stream.segment_stream.move(kPastEndOfBufferPosition);
+ ASSERT_EQ(readable_stream.segment_stream.getPosition(),
+ kPastEndOfBufferPosition);
+}
+
+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);
scroggo_chromium 2017/04/25 15:31:40 As I mention above, you never use this buffer agai
cblume 2017/04/26 10:09:49 Done.
+
+ auto shared_buffer = blink::SharedBuffer::Create(readable_stream.buffer.get(),
scroggo_chromium 2017/04/25 15:31:41 I've been advised not to use auto here, since this
cblume 2017/04/26 10:09:49 Done.
+ kBufferAllocationSize);
+ auto segment_reader =
scroggo_chromium 2017/04/25 15:31:40 Same concern about auto here.
cblume 2017/04/26 10:09:48 Done.
+ blink::SegmentReader::CreateFromSharedBuffer(std::move(shared_buffer));
+ readable_stream.segment_stream.SetReader(segment_reader.Get());
scroggo_chromium 2017/04/25 15:31:40 This makes me wonder if SetReader should take a Pa
cblume 2017/04/26 10:09:48 Correct. I used a raw pointer because that is what
scroggo_chromium 2017/04/26 19:02:22 I'm not sure I follow. Don't we already make a Ref
cblume 2017/04/26 19:16:06 OHHHHH it is invasive! Okay yeah, in that case I a
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698