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:
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/. */
10 #include "jsapi-tests/tests.h"
14 static bool executor_called
= false;
16 static bool PromiseExecutor(JSContext
* cx
, unsigned argc
, Value
* vp
) {
18 CallArgs args
= CallArgsFromVp(argc
, vp
);
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;
28 static JSObject
* CreatePromise(JSContext
* cx
) {
29 RootedFunction
executor(
30 cx
, JS_NewFunction(cx
, PromiseExecutor
, 2, 0, "executor"));
34 return JS::NewPromiseObject(cx
, executor
);
37 BEGIN_TEST(testPromise_NewPromise
) {
38 RootedObject
promise(cx
, CreatePromise(cx
));
40 CHECK(executor_called
);
44 END_TEST(testPromise_NewPromise
)
46 BEGIN_TEST(testPromise_GetPromiseState
) {
47 RootedObject
promise(cx
, CreatePromise(cx
));
52 CHECK(JS::GetPromiseState(promise
) == JS::PromiseState::Pending
);
56 END_TEST(testPromise_GetPromiseState
)
58 BEGIN_TEST(testPromise_ResolvePromise
) {
59 RootedObject
promise(cx
, CreatePromise(cx
));
64 RootedValue
result(cx
);
66 JS::ResolvePromise(cx
, promise
, result
);
68 CHECK(JS::GetPromiseState(promise
) == JS::PromiseState::Fulfilled
);
72 END_TEST(testPromise_ResolvePromise
)
74 BEGIN_TEST(testPromise_RejectPromise
) {
75 RootedObject
promise(cx
, CreatePromise(cx
));
80 RootedValue
result(cx
);
82 JS::RejectPromise(cx
, promise
, result
);
84 CHECK(JS::GetPromiseState(promise
) == JS::PromiseState::Rejected
);
88 END_TEST(testPromise_RejectPromise
)
90 static bool thenHandler_called
= false;
92 static bool PromiseThenHandler(JSContext
* cx
, unsigned argc
, Value
* vp
) {
94 CallArgs args
= CallArgsFromVp(argc
, vp
);
96 MOZ_ASSERT(args
.length() == 1);
98 thenHandler_called
= true;
102 static bool catchHandler_called
= false;
104 static bool PromiseCatchHandler(JSContext
* cx
, unsigned argc
, Value
* vp
) {
106 CallArgs args
= CallArgsFromVp(argc
, vp
);
108 MOZ_ASSERT(args
.length() == 1);
110 catchHandler_called
= true;
114 BEGIN_TEST(testPromise_PromiseThen
) {
115 RootedObject
promise(cx
, CreatePromise(cx
));
120 RootedFunction
thenHandler(
121 cx
, JS_NewFunction(cx
, PromiseThenHandler
, 1, 0, "thenHandler"));
125 RootedFunction
catchHandler(
126 cx
, JS_NewFunction(cx
, PromiseCatchHandler
, 1, 0, "catchHandler"));
130 JS::AddPromiseReactions(cx
, promise
, thenHandler
, catchHandler
);
132 RootedValue
result(cx
);
134 JS::ResolvePromise(cx
, promise
, result
);
137 CHECK(thenHandler_called
);
141 END_TEST(testPromise_PromiseThen
)
143 BEGIN_TEST(testPromise_PromiseCatch
) {
144 RootedObject
promise(cx
, CreatePromise(cx
));
149 RootedFunction
thenHandler(
150 cx
, JS_NewFunction(cx
, PromiseThenHandler
, 1, 0, "thenHandler"));
154 RootedFunction
catchHandler(
155 cx
, JS_NewFunction(cx
, PromiseCatchHandler
, 1, 0, "catchHandler"));
159 JS::AddPromiseReactions(cx
, promise
, thenHandler
, catchHandler
);
161 RootedValue
result(cx
);
163 JS::RejectPromise(cx
, promise
, result
);
166 CHECK(catchHandler_called
);
170 END_TEST(testPromise_PromiseCatch
)