OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/image-decoders/SegmentStream.h" |
| 6 |
| 7 #include <memory> |
| 8 #include "platform/SharedBuffer.h" |
| 9 #include "platform/wtf/PtrUtil.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 // SegmentStream has 4 accessors which do not alter state: |
| 13 // - isCleared() |
| 14 // - isAtEnd() |
| 15 // - getPosition() |
| 16 // - getLength() |
| 17 // |
| 18 // For every operation which changes state we can test: |
| 19 // - the operation completed as expected, |
| 20 // - the accessors did not change, and/or |
| 21 // - the accessors changed in the way we expected. |
| 22 // |
| 23 // There are actually 2 more accessors: |
| 24 // - hasPosition() |
| 25 // - hasLength() |
| 26 // but these should always return true to indicate that we can call getLength() |
| 27 // for example. So let's not add them to every state changing operation and add |
| 28 // needless complexity. |
| 29 |
| 30 namespace { |
| 31 |
| 32 constexpr size_t kBufferAllocationSize = 20; |
| 33 constexpr size_t kInsideBufferPosition = 10; |
| 34 constexpr size_t kPastEndOfBufferPosition = 30; |
| 35 |
| 36 testing::AssertionResult IsCleared(const blink::SegmentStream&); |
| 37 testing::AssertionResult IsAtEnd(const blink::SegmentStream&); |
| 38 testing::AssertionResult PositionIsZero(const blink::SegmentStream&); |
| 39 testing::AssertionResult PositionIsInsideBuffer(const blink::SegmentStream&); |
| 40 testing::AssertionResult PositionIsAtEndOfBuffer(const blink::SegmentStream&); |
| 41 testing::AssertionResult LengthIsZero(const blink::SegmentStream&); |
| 42 testing::AssertionResult LengthIsAllocationSize(const blink::SegmentStream&); |
| 43 |
| 44 // We want to keep the buffer alive for the same duration as the SegmentStream |
| 45 struct ReadyToReadSegmentStream { |
| 46 ReadyToReadSegmentStream() = default; |
| 47 ReadyToReadSegmentStream(const ReadyToReadSegmentStream&) = delete; |
| 48 ReadyToReadSegmentStream(ReadyToReadSegmentStream&&) = default; |
| 49 ReadyToReadSegmentStream& operator=(const ReadyToReadSegmentStream&) = delete; |
| 50 ReadyToReadSegmentStream& operator=(ReadyToReadSegmentStream&&) = default; |
| 51 |
| 52 std::unique_ptr<char[]> buffer; |
| 53 blink::SegmentStream segment_stream; |
| 54 }; |
| 55 |
| 56 void CreateReadableSegmentStream(ReadyToReadSegmentStream& readable_stream); |
| 57 |
| 58 } // namespace |
| 59 |
| 60 namespace blink { |
| 61 |
| 62 TEST(SegmentStreamTest, DefaultConstructorShouldSetIsCleared) { |
| 63 SegmentStream segment_stream; |
| 64 |
| 65 ASSERT_TRUE(IsCleared(segment_stream)); |
| 66 } |
| 67 |
| 68 TEST(SegmentStreamTest, DefaultConstructorShouldSetIsAtEnd) { |
| 69 SegmentStream segment_stream; |
| 70 |
| 71 ASSERT_TRUE(IsAtEnd(segment_stream)); |
| 72 } |
| 73 |
| 74 TEST(SegmentStreamTest, DefaultContructorShouldClearPosition) { |
| 75 SegmentStream segment_stream; |
| 76 |
| 77 ASSERT_TRUE(PositionIsZero(segment_stream)); |
| 78 } |
| 79 |
| 80 TEST(SegmentStreamTest, DefaultConstructorShouldHaveZeroLength) { |
| 81 SegmentStream segment_stream; |
| 82 |
| 83 ASSERT_TRUE(LengthIsZero(segment_stream)); |
| 84 } |
| 85 |
| 86 TEST(SegmentStreamTest, SetReaderShouldUnsetIsCleared) { |
| 87 ReadyToReadSegmentStream readable_stream; |
| 88 CreateReadableSegmentStream(readable_stream); |
| 89 |
| 90 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 91 } |
| 92 |
| 93 TEST(SegmentStreamTest, SetReaderShouldUnsetIsAtEnd) { |
| 94 ReadyToReadSegmentStream readable_stream; |
| 95 CreateReadableSegmentStream(readable_stream); |
| 96 |
| 97 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 98 } |
| 99 |
| 100 TEST(SegmentStreamTest, SetReaderShouldNotChangePosition) { |
| 101 ReadyToReadSegmentStream readable_stream; |
| 102 CreateReadableSegmentStream(readable_stream); |
| 103 |
| 104 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 105 } |
| 106 |
| 107 TEST(SegmentStreamTest, SetReaderShouldUpdateLength) { |
| 108 ReadyToReadSegmentStream readable_stream; |
| 109 CreateReadableSegmentStream(readable_stream); |
| 110 |
| 111 ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| 112 } |
| 113 |
| 114 TEST(SegmentStreamTest, SetReaderShouldSetIsClearedWhenSetToNull) { |
| 115 ReadyToReadSegmentStream readable_stream; |
| 116 CreateReadableSegmentStream(readable_stream); |
| 117 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 118 |
| 119 readable_stream.segment_stream.SetReader(nullptr, true); |
| 120 ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| 121 } |
| 122 |
| 123 TEST(SegmentStreamTest, SetReaderShouldSetIsAtEndWhenSetToNull) { |
| 124 ReadyToReadSegmentStream readable_stream; |
| 125 CreateReadableSegmentStream(readable_stream); |
| 126 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 127 |
| 128 readable_stream.segment_stream.SetReader(nullptr, true); |
| 129 ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| 130 } |
| 131 |
| 132 TEST(SegmentStreamTest, SetReaderShouldNotChangePositionWhenSetToNull) { |
| 133 ReadyToReadSegmentStream readable_stream; |
| 134 CreateReadableSegmentStream(readable_stream); |
| 135 |
| 136 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 137 const size_t amount_read = readable_stream.segment_stream.read( |
| 138 read_buffer.get(), kInsideBufferPosition); |
| 139 ASSERT_EQ(amount_read, kInsideBufferPosition); |
| 140 const size_t pre_nulled_position = |
| 141 readable_stream.segment_stream.getPosition(); |
| 142 ASSERT_EQ(amount_read, pre_nulled_position); |
| 143 |
| 144 readable_stream.segment_stream.SetReader(nullptr, true); |
| 145 ASSERT_EQ(readable_stream.segment_stream.getPosition(), pre_nulled_position); |
| 146 } |
| 147 |
| 148 TEST(SegmentStreamTest, SetReaderShouldClearLengthWhenSetToNull) { |
| 149 ReadyToReadSegmentStream readable_stream; |
| 150 CreateReadableSegmentStream(readable_stream); |
| 151 |
| 152 readable_stream.segment_stream.SetReader(nullptr, true); |
| 153 ASSERT_TRUE(LengthIsZero(readable_stream.segment_stream)); |
| 154 } |
| 155 |
| 156 TEST(SegmentStreamTest, ReadShouldConsumeBuffer) { |
| 157 ReadyToReadSegmentStream readable_stream; |
| 158 CreateReadableSegmentStream(readable_stream); |
| 159 |
| 160 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 161 const size_t amount_read = readable_stream.segment_stream.read( |
| 162 read_buffer.get(), kInsideBufferPosition); |
| 163 ASSERT_EQ(amount_read, kInsideBufferPosition); |
| 164 } |
| 165 |
| 166 TEST(SegmentStreamTest, ReadShouldNotClear) { |
| 167 ReadyToReadSegmentStream readable_stream; |
| 168 CreateReadableSegmentStream(readable_stream); |
| 169 |
| 170 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 171 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 172 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 173 } |
| 174 |
| 175 TEST(SegmentStreamTest, ReadShouldUpdateIsAtEnd) { |
| 176 ReadyToReadSegmentStream readable_stream; |
| 177 CreateReadableSegmentStream(readable_stream); |
| 178 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 179 |
| 180 const auto read_buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| 181 const size_t amount_read = readable_stream.segment_stream.read( |
| 182 read_buffer.get(), kBufferAllocationSize); |
| 183 ASSERT_EQ(amount_read, kBufferAllocationSize); |
| 184 ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| 185 } |
| 186 |
| 187 TEST(SegmentStreamTest, ReadShouldUpdatePosition) { |
| 188 ReadyToReadSegmentStream readable_stream; |
| 189 CreateReadableSegmentStream(readable_stream); |
| 190 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 191 |
| 192 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 193 const size_t amount_read = readable_stream.segment_stream.read( |
| 194 read_buffer.get(), kInsideBufferPosition); |
| 195 ASSERT_EQ(readable_stream.segment_stream.getPosition(), amount_read); |
| 196 ASSERT_TRUE(PositionIsInsideBuffer(readable_stream.segment_stream)); |
| 197 } |
| 198 |
| 199 TEST(SegmentStreamTest, ReadShouldNotChangeLength) { |
| 200 ReadyToReadSegmentStream readable_stream; |
| 201 CreateReadableSegmentStream(readable_stream); |
| 202 |
| 203 const size_t length = readable_stream.segment_stream.getLength(); |
| 204 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 205 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 206 ASSERT_EQ(readable_stream.segment_stream.getLength(), length); |
| 207 } |
| 208 |
| 209 TEST(SegmentStreamTest, ReadShouldNotConsumeBufferWhenCleared) { |
| 210 ReadyToReadSegmentStream readable_stream; |
| 211 CreateReadableSegmentStream(readable_stream); |
| 212 |
| 213 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 214 const auto read_buffer_as_void_ptr = |
| 215 reinterpret_cast<void*>(read_buffer.get()); |
| 216 |
| 217 readable_stream.segment_stream.SetReader(nullptr, true); |
| 218 ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| 219 |
| 220 ASSERT_EQ(0u, readable_stream.segment_stream.read(read_buffer_as_void_ptr, |
| 221 kInsideBufferPosition)); |
| 222 } |
| 223 |
| 224 TEST(SegmentStreamTest, ReadShouldConsumeBufferWithoutGoingPastTheEnd) { |
| 225 ReadyToReadSegmentStream readable_stream; |
| 226 CreateReadableSegmentStream(readable_stream); |
| 227 |
| 228 const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 229 const size_t amount_read = readable_stream.segment_stream.read( |
| 230 read_buffer.get(), kPastEndOfBufferPosition); |
| 231 ASSERT_EQ(amount_read, readable_stream.segment_stream.getLength()); |
| 232 } |
| 233 |
| 234 TEST(SegmentStreamTest, ReadShouldSetIsAtEndWhenPastEnd) { |
| 235 ReadyToReadSegmentStream readable_stream; |
| 236 CreateReadableSegmentStream(readable_stream); |
| 237 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 238 |
| 239 const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 240 readable_stream.segment_stream.read(read_buffer.get(), |
| 241 kPastEndOfBufferPosition); |
| 242 ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| 243 } |
| 244 |
| 245 TEST(SegmentStreamTest, ReadShouldTruncatePositionWhenPastEnd) { |
| 246 ReadyToReadSegmentStream readable_stream; |
| 247 CreateReadableSegmentStream(readable_stream); |
| 248 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 249 |
| 250 const auto read_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 251 const size_t amount_read = readable_stream.segment_stream.read( |
| 252 read_buffer.get(), kPastEndOfBufferPosition); |
| 253 ASSERT_EQ(amount_read, readable_stream.segment_stream.getPosition()); |
| 254 ASSERT_TRUE(PositionIsAtEndOfBuffer(readable_stream.segment_stream)); |
| 255 } |
| 256 |
| 257 TEST(SegmentStreamTest, PeekShouldCconsumeBuffer) { |
| 258 ReadyToReadSegmentStream readable_stream; |
| 259 CreateReadableSegmentStream(readable_stream); |
| 260 |
| 261 const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 262 const size_t amount_peeked = readable_stream.segment_stream.peek( |
| 263 peek_buffer.get(), kInsideBufferPosition); |
| 264 ASSERT_EQ(amount_peeked, kInsideBufferPosition); |
| 265 } |
| 266 |
| 267 TEST(SegmentStreamTest, PeekShouldNotClear) { |
| 268 ReadyToReadSegmentStream readable_stream; |
| 269 CreateReadableSegmentStream(readable_stream); |
| 270 |
| 271 const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 272 readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| 273 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 274 } |
| 275 |
| 276 TEST(SegmentStreamTest, PeekShouldNotUpdateIsAtEnd) { |
| 277 ReadyToReadSegmentStream readable_stream; |
| 278 CreateReadableSegmentStream(readable_stream); |
| 279 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 280 |
| 281 const auto peek_buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| 282 const size_t amount_peeked = readable_stream.segment_stream.peek( |
| 283 peek_buffer.get(), kBufferAllocationSize); |
| 284 ASSERT_EQ(amount_peeked, kBufferAllocationSize); |
| 285 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 286 } |
| 287 |
| 288 TEST(SegmentStreamTest, PeekShouldNotUpdatePosition) { |
| 289 ReadyToReadSegmentStream readable_stream; |
| 290 CreateReadableSegmentStream(readable_stream); |
| 291 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 292 |
| 293 const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 294 readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| 295 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 296 } |
| 297 |
| 298 TEST(SegmentStreamTest, PeekShouldNotChangeLength) { |
| 299 ReadyToReadSegmentStream readable_stream; |
| 300 CreateReadableSegmentStream(readable_stream); |
| 301 |
| 302 const size_t length = readable_stream.segment_stream.getLength(); |
| 303 const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 304 readable_stream.segment_stream.peek(peek_buffer.get(), kInsideBufferPosition); |
| 305 ASSERT_EQ(readable_stream.segment_stream.getLength(), length); |
| 306 } |
| 307 |
| 308 TEST(SegmentStreamTest, PeekShouldNotConsumeBufferWhenCleared) { |
| 309 ReadyToReadSegmentStream readable_stream; |
| 310 CreateReadableSegmentStream(readable_stream); |
| 311 |
| 312 const auto peek_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 313 const auto peek_buffer_as_void_ptr = |
| 314 reinterpret_cast<void*>(peek_buffer.get()); |
| 315 |
| 316 readable_stream.segment_stream.SetReader(nullptr, true); |
| 317 ASSERT_TRUE(IsCleared(readable_stream.segment_stream)); |
| 318 |
| 319 ASSERT_EQ(0u, readable_stream.segment_stream.peek(peek_buffer_as_void_ptr, |
| 320 kInsideBufferPosition)); |
| 321 } |
| 322 |
| 323 TEST(SegmentStreamTest, PeekShouldConsumeBufferWithoutGoingPastTheEnd) { |
| 324 ReadyToReadSegmentStream readable_stream; |
| 325 CreateReadableSegmentStream(readable_stream); |
| 326 |
| 327 const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 328 const size_t amount_peeked = readable_stream.segment_stream.peek( |
| 329 peek_buffer.get(), kPastEndOfBufferPosition); |
| 330 ASSERT_EQ(amount_peeked, readable_stream.segment_stream.getLength()); |
| 331 } |
| 332 |
| 333 TEST(SegmentStreamTest, PeekShouldNotSetIsAtEndWhenPastEnd) { |
| 334 ReadyToReadSegmentStream readable_stream; |
| 335 CreateReadableSegmentStream(readable_stream); |
| 336 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 337 |
| 338 const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 339 readable_stream.segment_stream.peek(peek_buffer.get(), |
| 340 kPastEndOfBufferPosition); |
| 341 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 342 } |
| 343 |
| 344 TEST(SegmentStreamTest, PeekShouldNotTruncatePositionWhenPastEnd) { |
| 345 ReadyToReadSegmentStream readable_stream; |
| 346 CreateReadableSegmentStream(readable_stream); |
| 347 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 348 |
| 349 const auto peek_buffer = WTF::MakeUnique<char[]>(kPastEndOfBufferPosition); |
| 350 readable_stream.segment_stream.peek(peek_buffer.get(), |
| 351 kPastEndOfBufferPosition); |
| 352 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 353 } |
| 354 |
| 355 TEST(SegmentStreamTest, RewindShouldNotClear) { |
| 356 ReadyToReadSegmentStream readable_stream; |
| 357 CreateReadableSegmentStream(readable_stream); |
| 358 |
| 359 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 360 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 361 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 362 |
| 363 readable_stream.segment_stream.rewind(); |
| 364 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 365 } |
| 366 |
| 367 TEST(SegmentStreamTest, RewindShouldNotSetAtEnd) { |
| 368 ReadyToReadSegmentStream readable_stream; |
| 369 CreateReadableSegmentStream(readable_stream); |
| 370 |
| 371 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 372 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 373 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 374 |
| 375 readable_stream.segment_stream.rewind(); |
| 376 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 377 } |
| 378 |
| 379 TEST(SegmentStreamTest, RewindShouldResetPosition) { |
| 380 ReadyToReadSegmentStream readable_stream; |
| 381 CreateReadableSegmentStream(readable_stream); |
| 382 |
| 383 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 384 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 385 ASSERT_TRUE(PositionIsInsideBuffer(readable_stream.segment_stream)); |
| 386 |
| 387 readable_stream.segment_stream.rewind(); |
| 388 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 389 } |
| 390 |
| 391 TEST(SegmentStreamTest, RewindShouldNotChangeLength) { |
| 392 ReadyToReadSegmentStream readable_stream; |
| 393 CreateReadableSegmentStream(readable_stream); |
| 394 |
| 395 const auto read_buffer = WTF::MakeUnique<char[]>(kInsideBufferPosition); |
| 396 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); |
| 397 const size_t pre_rewind_length = readable_stream.segment_stream.getLength(); |
| 398 |
| 399 readable_stream.segment_stream.rewind(); |
| 400 ASSERT_EQ(readable_stream.segment_stream.getLength(), pre_rewind_length); |
| 401 } |
| 402 |
| 403 TEST(SegmentStreamTest, HasPositionShouldBeSupported) { |
| 404 blink::SegmentStream segment_stream; |
| 405 ASSERT_TRUE(segment_stream.hasPosition()); |
| 406 } |
| 407 |
| 408 TEST(SegmentStreamTest, SeekShouldNotSetIsCleared) { |
| 409 ReadyToReadSegmentStream readable_stream; |
| 410 CreateReadableSegmentStream(readable_stream); |
| 411 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 412 |
| 413 readable_stream.segment_stream.seek(kInsideBufferPosition); |
| 414 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 415 } |
| 416 |
| 417 TEST(SegmentStreamTest, SeekShouldNotSetIsAtEndWhenSeekingInsideTheBuffer) { |
| 418 ReadyToReadSegmentStream readable_stream; |
| 419 CreateReadableSegmentStream(readable_stream); |
| 420 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 421 |
| 422 readable_stream.segment_stream.seek(kInsideBufferPosition); |
| 423 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 424 } |
| 425 |
| 426 TEST(SegmentStreamTest, SeekShouldSetIsAtEndWhenSeekingToTheEndOfTheBuffer) { |
| 427 ReadyToReadSegmentStream readable_stream; |
| 428 CreateReadableSegmentStream(readable_stream); |
| 429 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); |
| 430 |
| 431 readable_stream.segment_stream.seek(kBufferAllocationSize); |
| 432 ASSERT_TRUE(IsAtEnd(readable_stream.segment_stream)); |
| 433 } |
| 434 |
| 435 TEST(SegmentStreamTest, SeekShouldUpdatePosition) { |
| 436 ReadyToReadSegmentStream readable_stream; |
| 437 CreateReadableSegmentStream(readable_stream); |
| 438 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 439 |
| 440 readable_stream.segment_stream.seek(kInsideBufferPosition); |
| 441 ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| 442 kInsideBufferPosition); |
| 443 } |
| 444 |
| 445 TEST(SegmentStreamTest, SeekShouldTruncatePositionWhenPastEnd) { |
| 446 ReadyToReadSegmentStream readable_stream; |
| 447 CreateReadableSegmentStream(readable_stream); |
| 448 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 449 |
| 450 readable_stream.segment_stream.seek(kPastEndOfBufferPosition); |
| 451 ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| 452 kBufferAllocationSize); |
| 453 } |
| 454 |
| 455 TEST(SegmentStreamTest, SeekShouldNotUpdateLength) { |
| 456 ReadyToReadSegmentStream readable_stream; |
| 457 CreateReadableSegmentStream(readable_stream); |
| 458 |
| 459 readable_stream.segment_stream.seek(kInsideBufferPosition); |
| 460 ASSERT_EQ(readable_stream.segment_stream.getLength(), kBufferAllocationSize); |
| 461 } |
| 462 |
| 463 TEST(SegmentStreamTest, MoveShouldNotSetCleared) { |
| 464 ReadyToReadSegmentStream readable_stream; |
| 465 CreateReadableSegmentStream(readable_stream); |
| 466 |
| 467 readable_stream.segment_stream.move(kInsideBufferPosition); |
| 468 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); |
| 469 } |
| 470 |
| 471 TEST(SegmentStreamTest, MoveShouldUpdatePosition) { |
| 472 ReadyToReadSegmentStream readable_stream; |
| 473 CreateReadableSegmentStream(readable_stream); |
| 474 |
| 475 readable_stream.segment_stream.move(kInsideBufferPosition); |
| 476 ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| 477 kInsideBufferPosition); |
| 478 } |
| 479 |
| 480 TEST(SegmentStreamTest, MoveShouldTruncatePositionWhenPastEnd) { |
| 481 ReadyToReadSegmentStream readable_stream; |
| 482 CreateReadableSegmentStream(readable_stream); |
| 483 |
| 484 readable_stream.segment_stream.move(kPastEndOfBufferPosition); |
| 485 ASSERT_EQ(readable_stream.segment_stream.getPosition(), |
| 486 kBufferAllocationSize); |
| 487 } |
| 488 |
| 489 TEST(SegmentStreamTest, MoveShouldClampPositionToZeroWhenGoingNegative) { |
| 490 ReadyToReadSegmentStream readable_stream; |
| 491 CreateReadableSegmentStream(readable_stream); |
| 492 |
| 493 // Move the position off of 0 |
| 494 const auto read_buffer = WTF::MakeUnique<char[]>(size_t{1}); |
| 495 readable_stream.segment_stream.read(read_buffer.get(), 1); |
| 496 ASSERT_FALSE(PositionIsZero(readable_stream.segment_stream)); |
| 497 |
| 498 readable_stream.segment_stream.move(-2); |
| 499 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); |
| 500 } |
| 501 |
| 502 TEST(SegmentStreamTest, MoveShouldNotChangeLength) { |
| 503 ReadyToReadSegmentStream readable_stream; |
| 504 CreateReadableSegmentStream(readable_stream); |
| 505 ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| 506 |
| 507 readable_stream.segment_stream.move(kInsideBufferPosition); |
| 508 ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); |
| 509 } |
| 510 |
| 511 TEST(SegmentStreamTest, HasLengthShouldBeSupported) { |
| 512 blink::SegmentStream segment_stream; |
| 513 ASSERT_TRUE(segment_stream.hasLength()); |
| 514 } |
| 515 |
| 516 } // namespace blink |
| 517 |
| 518 namespace { |
| 519 |
| 520 testing::AssertionResult IsCleared(const blink::SegmentStream& segment_stream) { |
| 521 if (segment_stream.IsCleared()) |
| 522 return testing::AssertionSuccess(); |
| 523 |
| 524 return testing::AssertionFailure() << "SegmentStream is not clear"; |
| 525 } |
| 526 |
| 527 testing::AssertionResult IsAtEnd(const blink::SegmentStream& segment_stream) { |
| 528 if (segment_stream.isAtEnd()) |
| 529 return testing::AssertionSuccess(); |
| 530 |
| 531 return testing::AssertionFailure() << "SegmentStream is not at the end"; |
| 532 } |
| 533 |
| 534 testing::AssertionResult PositionIsZero( |
| 535 const blink::SegmentStream& segment_stream) { |
| 536 if (segment_stream.getPosition() == 0ul) |
| 537 return testing::AssertionSuccess(); |
| 538 |
| 539 return testing::AssertionFailure() << "SegmentStream position is not 0"; |
| 540 } |
| 541 |
| 542 testing::AssertionResult PositionIsInsideBuffer( |
| 543 const blink::SegmentStream& segment_stream) { |
| 544 if (segment_stream.getPosition() == kInsideBufferPosition) |
| 545 return testing::AssertionSuccess(); |
| 546 |
| 547 return testing::AssertionFailure() |
| 548 << "SegmentStream position is not inside the buffer"; |
| 549 } |
| 550 |
| 551 testing::AssertionResult PositionIsAtEndOfBuffer( |
| 552 const blink::SegmentStream& segment_stream) { |
| 553 if (segment_stream.getPosition() == kBufferAllocationSize) |
| 554 return testing::AssertionSuccess(); |
| 555 |
| 556 return testing::AssertionFailure() |
| 557 << "SegmentStream position is not at the end of the buffer"; |
| 558 } |
| 559 |
| 560 testing::AssertionResult LengthIsZero( |
| 561 const blink::SegmentStream& segment_stream) { |
| 562 if (segment_stream.getLength() == 0ul) |
| 563 return testing::AssertionSuccess(); |
| 564 |
| 565 return testing::AssertionFailure() << "SegmentStream length is not 0"; |
| 566 } |
| 567 |
| 568 testing::AssertionResult LengthIsAllocationSize( |
| 569 const blink::SegmentStream& segment_stream) { |
| 570 if (segment_stream.getLength() == kBufferAllocationSize) |
| 571 return testing::AssertionSuccess(); |
| 572 |
| 573 return testing::AssertionFailure() |
| 574 << "SegmentStream length is not the allocation size"; |
| 575 } |
| 576 |
| 577 // We want to keep the buffer alive for the same duration as the SegmentStream |
| 578 |
| 579 void CreateReadableSegmentStream(ReadyToReadSegmentStream& readable_stream) { |
| 580 readable_stream.buffer = WTF::MakeUnique<char[]>(kBufferAllocationSize); |
| 581 |
| 582 auto shared_buffer = blink::SharedBuffer::Create(readable_stream.buffer.get(), |
| 583 kBufferAllocationSize); |
| 584 auto segment_reader = |
| 585 blink::SegmentReader::CreateFromSharedBuffer(std::move(shared_buffer)); |
| 586 readable_stream.segment_stream.SetReader(segment_reader.Get(), true); |
| 587 } |
| 588 |
| 589 } // namespace |
OLD | NEW |