Backed out changeset 8f976ed899d7 (bug 1847231) for causing bc failures on browser_se...
[gecko.git] / js / src / jsapi-tests / testPromise.cpp
blob2f9cf030486bf4f0f3deb81ed6fdcd3e04d99ed3
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jsapi.h"
10 #include "jsapi-tests/tests.h"
12 using namespace JS;
14 static bool executor_called = false;
16 static bool PromiseExecutor(JSContext* cx, unsigned argc, Value* vp) {
17 #ifdef DEBUG
18 CallArgs args = CallArgsFromVp(argc, vp);
19 #endif // DEBUG
20 MOZ_ASSERT(args.length() == 2);
21 MOZ_ASSERT(args[0].toObject().is<JSFunction>());
22 MOZ_ASSERT(args[1].toObject().is<JSFunction>());
24 executor_called = true;
25 return true;
28 static JSObject* CreatePromise(JSContext* cx) {
29 RootedFunction executor(
30 cx, JS_NewFunction(cx, PromiseExecutor, 2, 0, "executor"));
31 if (!executor) {
32 return nullptr;
34 return JS::NewPromiseObject(cx, executor);
37 BEGIN_TEST(testPromise_NewPromise) {
38 RootedObject promise(cx, CreatePromise(cx));
39 CHECK(promise);
40 CHECK(executor_called);
42 return true;
44 END_TEST(testPromise_NewPromise)
46 BEGIN_TEST(testPromise_GetPromiseState) {
47 RootedObject promise(cx, CreatePromise(cx));
48 if (!promise) {
49 return false;
52 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Pending);
54 return true;
56 END_TEST(testPromise_GetPromiseState)
58 BEGIN_TEST(testPromise_ResolvePromise) {
59 RootedObject promise(cx, CreatePromise(cx));
60 if (!promise) {
61 return false;
64 RootedValue result(cx);
65 result.setInt32(42);
66 JS::ResolvePromise(cx, promise, result);
68 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Fulfilled);
70 return true;
72 END_TEST(testPromise_ResolvePromise)
74 BEGIN_TEST(testPromise_RejectPromise) {
75 RootedObject promise(cx, CreatePromise(cx));
76 if (!promise) {
77 return false;
80 RootedValue result(cx);
81 result.setInt32(42);
82 JS::RejectPromise(cx, promise, result);
84 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Rejected);
86 return true;
88 END_TEST(testPromise_RejectPromise)
90 static bool thenHandler_called = false;
92 static bool PromiseThenHandler(JSContext* cx, unsigned argc, Value* vp) {
93 #ifdef DEBUG
94 CallArgs args = CallArgsFromVp(argc, vp);
95 #endif // DEBUG
96 MOZ_ASSERT(args.length() == 1);
98 thenHandler_called = true;
99 return true;
102 static bool catchHandler_called = false;
104 static bool PromiseCatchHandler(JSContext* cx, unsigned argc, Value* vp) {
105 #ifdef DEBUG
106 CallArgs args = CallArgsFromVp(argc, vp);
107 #endif // DEBUG
108 MOZ_ASSERT(args.length() == 1);
110 catchHandler_called = true;
111 return true;
114 BEGIN_TEST(testPromise_PromiseThen) {
115 RootedObject promise(cx, CreatePromise(cx));
116 if (!promise) {
117 return false;
120 RootedFunction thenHandler(
121 cx, JS_NewFunction(cx, PromiseThenHandler, 1, 0, "thenHandler"));
122 if (!thenHandler) {
123 return false;
125 RootedFunction catchHandler(
126 cx, JS_NewFunction(cx, PromiseCatchHandler, 1, 0, "catchHandler"));
127 if (!catchHandler) {
128 return false;
130 JS::AddPromiseReactions(cx, promise, thenHandler, catchHandler);
132 RootedValue result(cx);
133 result.setInt32(42);
134 JS::ResolvePromise(cx, promise, result);
135 js::RunJobs(cx);
137 CHECK(thenHandler_called);
139 return true;
141 END_TEST(testPromise_PromiseThen)
143 BEGIN_TEST(testPromise_PromiseCatch) {
144 RootedObject promise(cx, CreatePromise(cx));
145 if (!promise) {
146 return false;
149 RootedFunction thenHandler(
150 cx, JS_NewFunction(cx, PromiseThenHandler, 1, 0, "thenHandler"));
151 if (!thenHandler) {
152 return false;
154 RootedFunction catchHandler(
155 cx, JS_NewFunction(cx, PromiseCatchHandler, 1, 0, "catchHandler"));
156 if (!catchHandler) {
157 return false;
159 JS::AddPromiseReactions(cx, promise, thenHandler, catchHandler);
161 RootedValue result(cx);
162 result.setInt32(42);
163 JS::RejectPromise(cx, promise, result);
164 js::RunJobs(cx);
166 CHECK(catchHandler_called);
168 return true;
170 END_TEST(testPromise_PromiseCatch)