Chromium Code Reviews| 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 "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "wtf/PtrUtil.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, defaultConstructor_setsIsCleared) { | |
| 63 SegmentStream segment_stream; | |
|
cblume
2017/04/08 01:19:20
A lot of variables like this one use the new Blink
| |
| 64 | |
| 65 ASSERT_TRUE(IsCleared(segment_stream)); | |
| 66 } | |
| 67 | |
| 68 TEST(SegmentStreamTest, defaultConstructor_setsIsAtEnd) { | |
| 69 SegmentStream segment_stream; | |
| 70 | |
| 71 ASSERT_TRUE(IsAtEnd(segment_stream)); | |
| 72 } | |
| 73 | |
| 74 TEST(SegmentStreamTest, defaultContructor_clearsPosition) { | |
| 75 SegmentStream segment_stream; | |
| 76 | |
| 77 ASSERT_TRUE(PositionIsZero(segment_stream)); | |
| 78 } | |
| 79 | |
| 80 TEST(SegmentStreamTest, defaultConstructor_hasZeroLength) { | |
| 81 SegmentStream segment_stream; | |
| 82 | |
| 83 ASSERT_TRUE(LengthIsZero(segment_stream)); | |
| 84 } | |
| 85 | |
| 86 TEST(SegmentStreamTest, setReader_unsetsIsCleared) { | |
| 87 ReadyToReadSegmentStream readable_stream; | |
| 88 CreateReadableSegmentStream(readable_stream); | |
| 89 | |
| 90 ASSERT_FALSE(IsCleared(readable_stream.segment_stream)); | |
| 91 } | |
| 92 | |
| 93 TEST(SegmentStreamTest, setReader_unsetsIsAtEnd) { | |
| 94 ReadyToReadSegmentStream readable_stream; | |
| 95 CreateReadableSegmentStream(readable_stream); | |
| 96 | |
| 97 ASSERT_FALSE(IsAtEnd(readable_stream.segment_stream)); | |
| 98 } | |
| 99 | |
| 100 TEST(SegmentStreamTest, setReader_doesNotChangePosition) { | |
| 101 ReadyToReadSegmentStream readable_stream; | |
| 102 CreateReadableSegmentStream(readable_stream); | |
| 103 | |
| 104 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); | |
| 105 } | |
| 106 | |
| 107 TEST(SegmentStreamTest, setReader_updatesLength) { | |
| 108 ReadyToReadSegmentStream readable_stream; | |
| 109 CreateReadableSegmentStream(readable_stream); | |
| 110 | |
| 111 ASSERT_TRUE(LengthIsAllocationSize(readable_stream.segment_stream)); | |
| 112 } | |
| 113 | |
| 114 TEST(SegmentStreamTest, setReader_setsIsCleared_whenSetToNull) { | |
| 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, setReader_setsIsAtEnd_whenSetToNull) { | |
| 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, setReader_doesNotChangePosition_whenSetToNull) { | |
| 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, setReader_clearsLength_whenSetToNull) { | |
| 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, read_consumesBuffer) { | |
| 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, read_doesNotClear) { | |
| 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, read_updatesIsAtEnd) { | |
| 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, read_updatesPosition) { | |
| 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, read_DoesNotChangeLength) { | |
| 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, read_doesNotConsumeBuffer_whenCleared) { | |
| 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, read_consumesBuffer_withoutGoingPastTheEnd) { | |
| 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, read_setIsAtEnd_whenPastEnd) { | |
| 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, read_truncatesPosition_whenPastEnd) { | |
| 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, peek_consumesBuffer) { | |
| 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, peek_doesNotClear) { | |
| 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, peek_doesNotUpdateIsAtEnd) { | |
| 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, peek_doesNotUpdatePosition) { | |
| 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, peek_DoesNotChangeLength) { | |
| 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, peek_doesNotConsumeBuffer_whenCleared) { | |
| 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, peek_consumesBuffer_withoutGoingPastTheEnd) { | |
| 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, peek_doesNotSetIsAtEnd_whenPastEnd) { | |
| 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, peek_doesNotTruncatesPosition_whenPastEnd) { | |
| 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, rewind_doesNotClear) { | |
| 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, rewind_doesNotSetAtEnd) { | |
| 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, rewind_resetsPosition) { | |
| 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, rewind_doesNotChangeLength) { | |
| 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, hasPosition_supported) { | |
| 404 blink::SegmentStream segment_stream; | |
| 405 ASSERT_TRUE(segment_stream.hasPosition()); | |
| 406 } | |
| 407 | |
| 408 TEST(SegmentStreamTest, seek_doesNotSetIsCleared) { | |
| 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, seek_doesNotSetIsAtEnd_whenSeekingInsideTheBuffer) { | |
| 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, seek_setsIsAtEnd_whenSeekingToTheEndOfTheBuffer) { | |
| 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, seek_updatesPosition) { | |
| 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, seek_truncatesPosition_whenPastEnd) { | |
| 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, seek_doesNotUpdateLength) { | |
| 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, move_doesNotSetCleared) { | |
| 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, move_updatesPosition) { | |
| 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, move_truncatesPosition_whenPastEnd) { | |
| 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, move_clampsPositionToZero_whenGoingNegative) { | |
| 490 ReadyToReadSegmentStream readable_stream; | |
| 491 CreateReadableSegmentStream(readable_stream); | |
| 492 | |
| 493 // Move the position off of 0 | |
| 494 const auto read_buffer = WTF::makeUnique<char[]>(kInsideBufferPosition); | |
| 495 readable_stream.segment_stream.read(read_buffer.get(), kInsideBufferPosition); | |
| 496 ASSERT_FALSE(PositionIsZero(readable_stream.segment_stream)); | |
| 497 | |
| 498 readable_stream.segment_stream.move(-(kInsideBufferPosition + 1)); | |
| 499 ASSERT_TRUE(PositionIsZero(readable_stream.segment_stream)); | |
| 500 } | |
| 501 | |
| 502 TEST(SegmentStreamTest, move_doesNotChangeLength) { | |
| 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, hasLength_supported) { | |
| 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& segmentStream) { | |
| 521 if (segmentStream.isCleared()) | |
| 522 return testing::AssertionSuccess(); | |
| 523 | |
| 524 return testing::AssertionFailure() << "SegmentStream is not clear"; | |
| 525 } | |
| 526 | |
| 527 testing::AssertionResult IsAtEnd(const blink::SegmentStream& segmentStream) { | |
| 528 if (segmentStream.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& segmentStream) { | |
| 536 if (segmentStream.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& segmentStream) { | |
| 544 if (segmentStream.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& segmentStream) { | |
| 553 if (segmentStream.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& segmentStream) { | |
| 562 if (segmentStream.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& segmentStream) { | |
| 570 if (segmentStream.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 |