TEST_F(ScriptPromiseTest, castPromise) { ScriptPromise promise = ScriptPromiseResolver::create(scriptState())->promise(); ScriptPromise newPromise = ScriptPromise::cast(scriptState(), promise.v8Value()); ASSERT_FALSE(promise.isEmpty()); EXPECT_EQ(promise.v8Value(), newPromise.v8Value()); }
TEST_F(ScriptPromiseTest, rejectWithExceptionState) { String onFulfilled, onRejected; ScriptPromise promise = ScriptPromise::rejectWithDOMException(scriptState(), DOMException::create(SyntaxError, "some syntax error")); promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected)); ASSERT_FALSE(promise.isEmpty()); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ(String(), onRejected); isolate()->RunMicrotasks(); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ("SyntaxError: some syntax error", onRejected); }
TEST_F(ScriptPromiseTest, rejectThen) { RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState()); ScriptPromise promise = resolver->promise(); String onFulfilled, onRejected; resolver->reject("hello"); promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected)); ASSERT_FALSE(promise.isEmpty()); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ(String(), onRejected); isolate()->RunMicrotasks(); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ("hello", onRejected); }
TEST_F(ScriptPromiseTest, reject) { String onFulfilled, onRejected; ScriptValue value = ScriptValue(scriptState(), v8String(isolate(), "hello")); ScriptPromise promise = ScriptPromise::reject(scriptState(), ScriptValue(value)); promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected)); ASSERT_FALSE(promise.isEmpty()); ASSERT_TRUE(promise.v8Value()->IsPromise()); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ(String(), onRejected); isolate()->RunMicrotasks(); EXPECT_EQ(String(), onFulfilled); EXPECT_EQ("hello", onRejected); }
ScriptPromise Body::text(ScriptState* scriptState) { ScriptPromise promise = rejectInvalidConsumption(scriptState); if (!promise.isEmpty()) return promise; // See above comment. if (!scriptState->executionContext()) return ScriptPromise(); ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); promise = resolver->promise(); if (bodyBuffer()) { bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader::createLoaderAsString(), new BodyTextConsumer(resolver)); } else { resolver->resolve(String()); } return promise; }
ScriptPromise Body::json(ScriptState* scriptState) { ScriptPromise promise = rejectInvalidConsumption(scriptState); if (!promise.isEmpty()) return promise; // See above comment. if (!scriptState->executionContext()) return ScriptPromise(); ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); promise = resolver->promise(); if (bodyBuffer()) { bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader::createLoaderAsString(), new BodyJsonConsumer(resolver)); } else { resolver->reject(V8ThrowException::createSyntaxError(scriptState->isolate(), "Unexpected end of input")); } return promise; }
ScriptPromise Body::blob(ScriptState* scriptState) { ScriptPromise promise = rejectInvalidConsumption(scriptState); if (!promise.isEmpty()) return promise; // See above comment. if (!scriptState->executionContext()) return ScriptPromise(); ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); promise = resolver->promise(); if (bodyBuffer()) { bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader::createLoaderAsBlobHandle(mimeType()), new BodyBlobConsumer(resolver)); } else { OwnPtr<BlobData> blobData = BlobData::create(); blobData->setContentType(mimeType()); resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release(), 0))); } return promise; }
ScriptPromise Body::arrayBuffer(ScriptState* scriptState) { ScriptPromise promise = rejectInvalidConsumption(scriptState); if (!promise.isEmpty()) return promise; // When the main thread sends a V8::TerminateExecution() signal to a worker // thread, any V8 API on the worker thread starts returning an empty // handle. This can happen in Body::readAsync. To avoid the situation, we // first check the ExecutionContext and return immediately if it's already // gone (which means that the V8::TerminateExecution() signal has been sent // to this worker thread). if (!scriptState->executionContext()) return ScriptPromise(); ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); promise = resolver->promise(); if (bodyBuffer()) { bodyBuffer()->startLoading(scriptState->executionContext(), FetchDataLoader::createLoaderAsArrayBuffer(), new BodyArrayBufferConsumer(resolver)); } else { resolver->resolve(DOMArrayBuffer::create(0u, 1)); } return promise; }