testPromise.cpp (4035B)
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/. */ 7 8 #include "jsapi.h" 9 10 #include "jsapi-tests/tests.h" 11 12 using namespace JS; 13 14 static bool executor_called = false; 15 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>()); 23 24 executor_called = true; 25 return true; 26 } 27 28 static JSObject* CreatePromise(JSContext* cx) { 29 RootedFunction executor( 30 cx, JS_NewFunction(cx, PromiseExecutor, 2, 0, "executor")); 31 if (!executor) { 32 return nullptr; 33 } 34 return JS::NewPromiseObject(cx, executor); 35 } 36 37 BEGIN_TEST(testPromise_NewPromise) { 38 RootedObject promise(cx, CreatePromise(cx)); 39 CHECK(promise); 40 CHECK(executor_called); 41 42 return true; 43 } 44 END_TEST(testPromise_NewPromise) 45 46 BEGIN_TEST(testPromise_GetPromiseState) { 47 RootedObject promise(cx, CreatePromise(cx)); 48 if (!promise) { 49 return false; 50 } 51 52 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Pending); 53 54 return true; 55 } 56 END_TEST(testPromise_GetPromiseState) 57 58 BEGIN_TEST(testPromise_ResolvePromise) { 59 RootedObject promise(cx, CreatePromise(cx)); 60 if (!promise) { 61 return false; 62 } 63 64 RootedValue result(cx); 65 result.setInt32(42); 66 JS::ResolvePromise(cx, promise, result); 67 68 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Fulfilled); 69 70 return true; 71 } 72 END_TEST(testPromise_ResolvePromise) 73 74 BEGIN_TEST(testPromise_RejectPromise) { 75 RootedObject promise(cx, CreatePromise(cx)); 76 if (!promise) { 77 return false; 78 } 79 80 RootedValue result(cx); 81 result.setInt32(42); 82 JS::RejectPromise(cx, promise, result); 83 84 CHECK(JS::GetPromiseState(promise) == JS::PromiseState::Rejected); 85 86 return true; 87 } 88 END_TEST(testPromise_RejectPromise) 89 90 static bool thenHandler_called = false; 91 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); 97 98 thenHandler_called = true; 99 return true; 100 } 101 102 static bool catchHandler_called = false; 103 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); 109 110 catchHandler_called = true; 111 return true; 112 } 113 114 BEGIN_TEST(testPromise_PromiseThen) { 115 RootedObject promise(cx, CreatePromise(cx)); 116 if (!promise) { 117 return false; 118 } 119 120 RootedFunction thenHandler( 121 cx, JS_NewFunction(cx, PromiseThenHandler, 1, 0, "thenHandler")); 122 if (!thenHandler) { 123 return false; 124 } 125 RootedFunction catchHandler( 126 cx, JS_NewFunction(cx, PromiseCatchHandler, 1, 0, "catchHandler")); 127 if (!catchHandler) { 128 return false; 129 } 130 JS::AddPromiseReactions(cx, promise, thenHandler, catchHandler); 131 132 RootedValue result(cx); 133 result.setInt32(42); 134 JS::ResolvePromise(cx, promise, result); 135 js::RunJobs(cx); 136 137 CHECK(thenHandler_called); 138 139 return true; 140 } 141 END_TEST(testPromise_PromiseThen) 142 143 BEGIN_TEST(testPromise_PromiseCatch) { 144 RootedObject promise(cx, CreatePromise(cx)); 145 if (!promise) { 146 return false; 147 } 148 149 RootedFunction thenHandler( 150 cx, JS_NewFunction(cx, PromiseThenHandler, 1, 0, "thenHandler")); 151 if (!thenHandler) { 152 return false; 153 } 154 RootedFunction catchHandler( 155 cx, JS_NewFunction(cx, PromiseCatchHandler, 1, 0, "catchHandler")); 156 if (!catchHandler) { 157 return false; 158 } 159 JS::AddPromiseReactions(cx, promise, thenHandler, catchHandler); 160 161 RootedValue result(cx); 162 result.setInt32(42); 163 JS::RejectPromise(cx, promise, result); 164 js::RunJobs(cx); 165 166 CHECK(catchHandler_called); 167 168 return true; 169 } 170 END_TEST(testPromise_PromiseCatch)