1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecmascript/base/builtins_base.h"
17#include "ecmascript/ecma_runtime_call_info.h"
18#include "ecmascript/ecma_vm.h"
19#include "ecmascript/global_env.h"
20#include "ecmascript/interpreter/interpreter.h"
21#include "ecmascript/js_function.h"
22#include "ecmascript/js_promise.h"
23#include "ecmascript/js_thread.h"
24#include "ecmascript/object_factory.h"
25#include "ecmascript/tests/test_helper.h"
26
27using namespace panda::ecmascript;
28using namespace panda::ecmascript::base;
29
30namespace panda::test {
31class JSPromiseTest : public BaseTestWithScope<false> {
32};
33
34HWTEST_F_L0(JSPromiseTest, CreateResolvingFunctions)
35{
36    EcmaVM *ecmaVM = thread->GetEcmaVM();
37    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
38    ObjectFactory *factory = ecmaVM->GetFactory();
39
40    JSHandle<JSTaggedValue> promiseFunc = env->GetPromiseFunction();
41    JSHandle<JSPromise> jsPromise =
42        JSHandle<JSPromise>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(promiseFunc), promiseFunc));
43    JSHandle<ResolvingFunctionsRecord> reactions = JSPromise::CreateResolvingFunctions(thread, jsPromise);
44    JSHandle<JSTaggedValue> resolve(thread, reactions->GetResolveFunction());
45    JSHandle<JSTaggedValue> reject(thread, reactions->GetRejectFunction());
46    EXPECT_EQ(resolve->IsCallable(), true);
47    EXPECT_EQ(reject->IsCallable(), true);
48}
49
50HWTEST_F_L0(JSPromiseTest, NewPromiseCapability)
51{
52    EcmaVM *ecmaVM = thread->GetEcmaVM();
53    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
54    JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
55
56    JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
57    JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
58    EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
59
60    JSHandle<JSPromiseReactionsFunction> resolve(thread, capbility->GetResolve());
61    JSHandle<JSPromiseReactionsFunction> reject(thread, capbility->GetReject());
62    EXPECT_EQ(resolve.GetTaggedValue().IsCallable(), true);
63    EXPECT_EQ(resolve.GetTaggedValue().IsCallable(), true);
64
65    JSHandle<JSPromise> resolvedPromise(thread, resolve->GetPromise());
66    JSHandle<JSPromise> rejectedPromise(thread, reject->GetPromise());
67    EXPECT_EQ(JSTaggedValue::SameValue(newPromise.GetTaggedValue(), resolvedPromise.GetTaggedValue()), true);
68    EXPECT_EQ(JSTaggedValue::SameValue(newPromise.GetTaggedValue(), rejectedPromise.GetTaggedValue()), true);
69}
70
71HWTEST_F_L0(JSPromiseTest, FullFillPromise)
72{
73    EcmaVM *ecmaVM = thread->GetEcmaVM();
74    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
75    JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
76    JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
77    JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
78    EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
79    EXPECT_EQ(newPromise->GetPromiseResult().IsUndefined(), true);
80
81    JSHandle<JSTaggedValue> resolve(thread, capbility->GetResolve());
82    JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
83    EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, undefined, undefined, 1);
84    info->SetCallArg(JSTaggedValue(33));
85    JSFunction::Call(info);
86    EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::FULFILLED);
87    EXPECT_EQ(JSTaggedValue::SameValue(newPromise->GetPromiseResult(), JSTaggedValue(33)), true);
88}
89
90HWTEST_F_L0(JSPromiseTest, RejectPromise)
91{
92    EcmaVM *ecmaVM = thread->GetEcmaVM();
93    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
94    JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
95    JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
96    JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
97    EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
98    EXPECT_EQ(newPromise->GetPromiseResult().IsUndefined(), true);
99
100    JSHandle<JSTaggedValue> reject(thread, capbility->GetReject());
101    JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
102    EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, reject, undefined, undefined, 1);
103    info->SetCallArg(JSTaggedValue(44));
104    JSFunction::Call(info);
105    EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::REJECTED);
106    EXPECT_EQ(JSTaggedValue::SameValue(newPromise->GetPromiseResult(), JSTaggedValue(44)), true);
107}
108}  // namespace panda::test
109