TEST_F(ReadableStreamTest, BackpressureOnEnqueueing)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    auto strategy = MockStrategy::create();
    Checkpoint checkpoint;

    StringStream* stream = construct(strategy);
    EXPECT_EQ(ReadableStream::Readable, stream->stateInternal());

    {
        InSequence s;
        EXPECT_CALL(checkpoint, Call(0));
        EXPECT_CALL(*strategy, size(String("hello"), stream)).WillOnce(Return(1));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(1, stream)).WillOnce(Return(false));
        EXPECT_CALL(checkpoint, Call(1));
        EXPECT_CALL(checkpoint, Call(2));
        EXPECT_CALL(*strategy, size(String("world"), stream)).WillOnce(Return(2));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(3, stream)).WillOnce(Return(true));
        EXPECT_CALL(checkpoint, Call(3));
    }
    checkpoint.Call(0);
    bool result = stream->enqueue("hello");
    checkpoint.Call(1);
    EXPECT_TRUE(result);

    checkpoint.Call(2);
    result = stream->enqueue("world");
    checkpoint.Call(3);
    EXPECT_FALSE(result);

    stream->error(DOMException::create(AbortError, "done"));
}
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, StrictStrategy)
{
    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(checkpoint, Call(1));
        EXPECT_CALL(*m_underlyingSource, pullSource());
        EXPECT_CALL(checkpoint, Call(2));
        EXPECT_CALL(checkpoint, Call(3));
        EXPECT_CALL(*m_underlyingSource, pullSource());
        EXPECT_CALL(checkpoint, Call(4));
        EXPECT_CALL(checkpoint, Call(5));
        EXPECT_CALL(checkpoint, Call(6));
        EXPECT_CALL(checkpoint, Call(7));
        EXPECT_CALL(checkpoint, Call(8));
        EXPECT_CALL(checkpoint, Call(9));
        EXPECT_CALL(*m_underlyingSource, pullSource());
    }
    StringStream* stream = new StringStream(m_underlyingSource, new StringStream::StrictStrategy);
    ReadableStreamReader* reader = stream->getReader(scriptState()->executionContext(), exceptionState);

    checkpoint.Call(0);
    stream->didSourceStart();

    checkpoint.Call(1);
    EXPECT_FALSE(stream->isPulling());
    reader->read(scriptState());
    EXPECT_TRUE(stream->isPulling());
    checkpoint.Call(2);
    stream->enqueue("hello");
    EXPECT_FALSE(stream->isPulling());
    checkpoint.Call(3);
    reader->read(scriptState());
    EXPECT_TRUE(stream->isPulling());
    checkpoint.Call(4);
    reader->read(scriptState());
    EXPECT_TRUE(stream->isPulling());
    checkpoint.Call(5);
    stream->enqueue("hello");
    EXPECT_FALSE(stream->isPulling());
    checkpoint.Call(6);
    stream->enqueue("hello");
    EXPECT_FALSE(stream->isPulling());
    checkpoint.Call(7);
    stream->enqueue("hello");
    EXPECT_FALSE(stream->isPulling());
    checkpoint.Call(8);
    reader->read(scriptState());
    EXPECT_FALSE(stream->isPulling());
    checkpoint.Call(9);
    reader->read(scriptState());
    EXPECT_TRUE(stream->isPulling());

    stream->error(DOMException::create(AbortError, "done"));
}
TEST_F(ReadableStreamTest, CloseWhenErrored)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    StringStream* stream = construct();
    EXPECT_EQ(ReadableStream::Readable, stream->stateInternal());

    stream->error(DOMException::create(NotFoundError, "error"));
    stream->close();

    EXPECT_EQ(ReadableStream::Errored, stream->stateInternal());
}
TEST_F(ReadableStreamTest, ErrorAndEnqueue)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    StringStream* stream = construct();

    stream->error(DOMException::create(NotFoundError, "error"));
    EXPECT_EQ(ReadableStream::Errored, stream->stateInternal());

    bool result = stream->enqueue("hello");
    EXPECT_FALSE(result);
    EXPECT_EQ(ReadableStream::Errored, stream->stateInternal());
}
TEST_F(ReadableStreamTest, BackpressureOnReading)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    auto strategy = MockStrategy::create();
    Checkpoint checkpoint;

    StringStream* stream = construct(strategy);
    EXPECT_EQ(ReadableStream::Readable, stream->stateInternal());

    {
        InSequence s;
        EXPECT_CALL(*strategy, size(String("hello"), stream)).WillOnce(Return(2));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(2, stream)).WillOnce(Return(false));
        EXPECT_CALL(*strategy, size(String("world"), stream)).WillOnce(Return(3));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(5, stream)).WillOnce(Return(false));

        EXPECT_CALL(checkpoint, Call(0));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(3, stream)).WillOnce(Return(false));
        EXPECT_CALL(*m_underlyingSource, pullSource()).Times(1);
        EXPECT_CALL(checkpoint, Call(1));
        // shouldApplyBackpressure and pullSource are not called because the
        // stream is pulling.
        EXPECT_CALL(checkpoint, Call(2));
        EXPECT_CALL(*strategy, size(String("foo"), stream)).WillOnce(Return(4));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(4, stream)).WillOnce(Return(true));
        EXPECT_CALL(*strategy, size(String("bar"), stream)).WillOnce(Return(5));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(9, stream)).WillOnce(Return(true));
        EXPECT_CALL(checkpoint, Call(3));
        EXPECT_CALL(*strategy, shouldApplyBackpressure(5, stream)).WillOnce(Return(true));
        EXPECT_CALL(checkpoint, Call(4));
    }
    stream->enqueue("hello");
    stream->enqueue("world");

    checkpoint.Call(0);
    stream->read(scriptState());
    checkpoint.Call(1);
    stream->read(scriptState());
    checkpoint.Call(2);
    stream->enqueue("foo");
    stream->enqueue("bar");
    checkpoint.Call(3);
    stream->read(scriptState());
    checkpoint.Call(4);

    stream->error(DOMException::create(AbortError, "done"));
}
TEST_F(ReadableStreamTest, StartFail)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    StringStream* stream = new StringStream(m_underlyingSource);
    EXPECT_FALSE(exceptionState.hadException());
    EXPECT_FALSE(stream->isStarted());
    EXPECT_FALSE(stream->isDraining());
    EXPECT_FALSE(stream->isPulling());
    EXPECT_EQ(stream->stateInternal(), ReadableStream::Readable);

    stream->error(DOMException::create(NotFoundError));

    EXPECT_FALSE(stream->isStarted());
    EXPECT_FALSE(stream->isDraining());
    EXPECT_FALSE(stream->isPulling());
    EXPECT_EQ(stream->stateInternal(), ReadableStream::Errored);
}
TEST_F(ReadableStreamTest, GetErroredReader)
{
    ScriptState::Scope scope(scriptState());
    ExceptionState exceptionState(ExceptionState::ConstructionContext, "property", "interface", scriptState()->context()->Global(), isolate());
    StringStream* stream = construct();
    stream->error(DOMException::create(SyntaxError, "some error"));
    ReadableStreamReader* reader = stream->getReader(scriptState()->executionContext(), exceptionState);

    ASSERT_TRUE(reader);
    EXPECT_FALSE(exceptionState.hadException());

    String onFulfilled, onRejected;
    reader->closed(scriptState()).then(createCaptor(&onFulfilled), createCaptor(&onRejected));

    EXPECT_FALSE(reader->isActive());
    EXPECT_TRUE(onFulfilled.isNull());
    EXPECT_TRUE(onRejected.isNull());

    isolate()->RunMicrotasks();
    EXPECT_TRUE(onFulfilled.isNull());
    EXPECT_EQ("SyntaxError: some error", onRejected);
}
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);
}