TEST_F(ReadableStreamTest, CancelWhenLocked) { ScriptState::Scope scope(scriptState()); ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate()); String onFulfilled, onRejected; StringStream* stream = construct(); ReadableStreamReader* reader = stream->getReader(scriptState()->executionContext(), exceptionState); EXPECT_TRUE(reader->isActive()); EXPECT_FALSE(exceptionState.hadException()); EXPECT_EQ(ReadableStream::Readable, stream->stateInternal()); EXPECT_FALSE(stream->isDisturbed()); stream->cancel(scriptState(), ScriptValue(scriptState(), v8::Undefined(isolate()))).then(createCaptor(&onFulfilled), createCaptor(&onRejected)); EXPECT_FALSE(stream->isDisturbed()); EXPECT_TRUE(onFulfilled.isNull()); EXPECT_TRUE(onRejected.isNull()); isolate()->RunMicrotasks(); EXPECT_TRUE(onFulfilled.isNull()); EXPECT_EQ("TypeError: this stream is locked to a ReadableStreamReader", onRejected); EXPECT_TRUE(reader->isActive()); EXPECT_EQ(ReadableStream::Readable, stream->stateInternal()); }
TEST_F(ReadableStreamTest, CancelWhenReadable) { ScriptState::Scope scope(scriptState()); ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate()); StringStream* stream = construct(); String onFulfilled, onRejected; String onCancelFulfilled, onCancelRejected; ScriptValue reason(scriptState(), v8String(scriptState()->isolate(), "reason")); ScriptPromise promise = ScriptPromise::cast(scriptState(), v8String(scriptState()->isolate(), "hello")); { InSequence s; EXPECT_CALL(*m_underlyingSource, cancelSource(scriptState(), reason)).WillOnce(ReturnPointee(&promise)); } stream->enqueue("hello"); EXPECT_EQ(ReadableStream::Readable, stream->stateInternal()); EXPECT_FALSE(stream->isDisturbed()); ScriptPromise cancelResult = stream->cancel(scriptState(), reason); EXPECT_TRUE(stream->isDisturbed()); cancelResult.then(createCaptor(&onCancelFulfilled), createCaptor(&onCancelRejected)); EXPECT_NE(promise, cancelResult); EXPECT_EQ(ReadableStream::Closed, stream->stateInternal()); EXPECT_TRUE(onCancelFulfilled.isNull()); EXPECT_TRUE(onCancelRejected.isNull()); isolate()->RunMicrotasks(); EXPECT_EQ("undefined", onCancelFulfilled); EXPECT_TRUE(onCancelRejected.isNull()); }
TEST_F(ReadableStreamTest, ReadQueue) { ScriptState::Scope scope(scriptState()); ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate()); StringStream* stream = construct(); Checkpoint checkpoint; { InSequence s; EXPECT_CALL(checkpoint, Call(0)); EXPECT_CALL(*m_underlyingSource, pullSource()).Times(1); EXPECT_CALL(checkpoint, Call(1)); } Deque<std::pair<String, size_t>> queue; EXPECT_TRUE(stream->enqueue("hello")); EXPECT_TRUE(stream->enqueue("bye")); EXPECT_EQ(ReadableStream::Readable, stream->stateInternal()); EXPECT_FALSE(stream->isPulling()); checkpoint.Call(0); EXPECT_FALSE(stream->isDisturbed()); stream->readInternal(queue); EXPECT_TRUE(stream->isDisturbed()); checkpoint.Call(1); ASSERT_EQ(2u, queue.size()); EXPECT_EQ(std::make_pair(String("hello"), static_cast<size_t>(5)), queue[0]); EXPECT_EQ(std::make_pair(String("bye"), static_cast<size_t>(3)), queue[1]); EXPECT_EQ(ReadableStream::Readable, stream->stateInternal()); EXPECT_TRUE(stream->isPulling()); EXPECT_FALSE(stream->isDraining()); }
TEST_F(ReadableStreamTest, Start) { ScriptState::Scope scope(scriptState()); ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate()); Checkpoint checkpoint; { InSequence s; EXPECT_CALL(checkpoint, Call(0)); EXPECT_CALL(*m_underlyingSource, pullSource()).Times(1); EXPECT_CALL(checkpoint, Call(1)); } StringStream* stream = new StringStream(m_underlyingSource); EXPECT_FALSE(exceptionState.hadException()); EXPECT_FALSE(stream->isStarted()); EXPECT_FALSE(stream->isDraining()); EXPECT_FALSE(stream->isPulling()); EXPECT_FALSE(stream->isDisturbed()); EXPECT_EQ(stream->stateInternal(), ReadableStream::Readable); checkpoint.Call(0); stream->didSourceStart(); checkpoint.Call(1); EXPECT_TRUE(stream->isStarted()); EXPECT_FALSE(stream->isDraining()); EXPECT_TRUE(stream->isPulling()); EXPECT_EQ(stream->stateInternal(), ReadableStream::Readable); // We need to call |error| in order to make // ActiveDOMObject::hasPendingActivity return false. stream->error(DOMException::create(AbortError, "done")); }
TEST_F(ReadableStreamTest, CancelWhenErrored) { ScriptState::Scope scope(scriptState()); ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate()); StringStream* stream = construct(); String onFulfilled, onRejected; stream->error(DOMException::create(NotFoundError, "error")); EXPECT_EQ(ReadableStream::Errored, stream->stateInternal()); EXPECT_FALSE(stream->isDisturbed()); ScriptPromise promise = stream->cancel(scriptState(), ScriptValue()); EXPECT_TRUE(stream->isDisturbed()); EXPECT_EQ(ReadableStream::Errored, stream->stateInternal()); promise.then(createCaptor(&onFulfilled), createCaptor(&onRejected)); EXPECT_TRUE(onFulfilled.isNull()); EXPECT_TRUE(onRejected.isNull()); isolate()->RunMicrotasks(); EXPECT_TRUE(onFulfilled.isNull()); EXPECT_EQ("NotFoundError: error", onRejected); }