• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::base;
29 
30 namespace panda::test {
31 class JSPromiseTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         GTEST_LOG_(INFO) << "SetUpTestCase";
36     }
37 
TearDownTestCase()38     static void TearDownTestCase()
39     {
40         GTEST_LOG_(INFO) << "TearDownCase";
41     }
42 
SetUp()43     void SetUp() override
44     {
45         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
46     }
47 
TearDown()48     void TearDown() override
49     {
50         TestHelper::DestroyEcmaVMWithScope(instance, scope);
51     }
52 
53     JSThread *thread {nullptr};
54     EcmaVM *instance {nullptr};
55     ecmascript::EcmaHandleScope *scope {nullptr};
56 };
57 
HWTEST_F_L0(JSPromiseTest,CreateResolvingFunctions)58 HWTEST_F_L0(JSPromiseTest, CreateResolvingFunctions)
59 {
60     EcmaVM *ecmaVM = thread->GetEcmaVM();
61     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
62     ObjectFactory *factory = ecmaVM->GetFactory();
63 
64     JSHandle<JSTaggedValue> promiseFunc = env->GetPromiseFunction();
65     JSHandle<JSPromise> jsPromise =
66         JSHandle<JSPromise>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(promiseFunc), promiseFunc));
67     JSHandle<ResolvingFunctionsRecord> reactions = JSPromise::CreateResolvingFunctions(thread, jsPromise);
68     JSHandle<JSTaggedValue> resolve(thread, reactions->GetResolveFunction());
69     JSHandle<JSTaggedValue> reject(thread, reactions->GetRejectFunction());
70     EXPECT_EQ(resolve->IsCallable(), true);
71     EXPECT_EQ(reject->IsCallable(), true);
72 }
73 
HWTEST_F_L0(JSPromiseTest,NewPromiseCapability)74 HWTEST_F_L0(JSPromiseTest, NewPromiseCapability)
75 {
76     EcmaVM *ecmaVM = thread->GetEcmaVM();
77     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
78     JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
79 
80     JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
81     JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
82     EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
83 
84     JSHandle<JSPromiseReactionsFunction> resolve(thread, capbility->GetResolve());
85     JSHandle<JSPromiseReactionsFunction> reject(thread, capbility->GetReject());
86     EXPECT_EQ(resolve.GetTaggedValue().IsCallable(), true);
87     EXPECT_EQ(resolve.GetTaggedValue().IsCallable(), true);
88 
89     JSHandle<JSPromise> resolvedPromise(thread, resolve->GetPromise());
90     JSHandle<JSPromise> rejectedPromise(thread, reject->GetPromise());
91     EXPECT_EQ(JSTaggedValue::SameValue(newPromise.GetTaggedValue(), resolvedPromise.GetTaggedValue()), true);
92     EXPECT_EQ(JSTaggedValue::SameValue(newPromise.GetTaggedValue(), rejectedPromise.GetTaggedValue()), true);
93 }
94 
HWTEST_F_L0(JSPromiseTest,FullFillPromise)95 HWTEST_F_L0(JSPromiseTest, FullFillPromise)
96 {
97     EcmaVM *ecmaVM = thread->GetEcmaVM();
98     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
99     JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
100     JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
101     JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
102     EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
103     EXPECT_EQ(newPromise->GetPromiseResult().IsUndefined(), true);
104 
105     JSHandle<JSTaggedValue> resolve(thread, capbility->GetResolve());
106     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
107     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, resolve, undefined, undefined, 1);
108     info->SetCallArg(JSTaggedValue(33));
109     JSFunction::Call(info);
110     EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::FULFILLED);
111     EXPECT_EQ(JSTaggedValue::SameValue(newPromise->GetPromiseResult(), JSTaggedValue(33)), true);
112 }
113 
HWTEST_F_L0(JSPromiseTest,RejectPromise)114 HWTEST_F_L0(JSPromiseTest, RejectPromise)
115 {
116     EcmaVM *ecmaVM = thread->GetEcmaVM();
117     JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
118     JSHandle<JSTaggedValue> promise = env->GetPromiseFunction();
119     JSHandle<PromiseCapability> capbility = JSPromise::NewPromiseCapability(thread, promise);
120     JSHandle<JSPromise> newPromise(thread, capbility->GetPromise());
121     EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::PENDING);
122     EXPECT_EQ(newPromise->GetPromiseResult().IsUndefined(), true);
123 
124     JSHandle<JSTaggedValue> reject(thread, capbility->GetReject());
125     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
126     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, reject, undefined, undefined, 1);
127     info->SetCallArg(JSTaggedValue(44));
128     JSFunction::Call(info);
129     EXPECT_EQ(newPromise->GetPromiseState(), PromiseState::REJECTED);
130     EXPECT_EQ(JSTaggedValue::SameValue(newPromise->GetPromiseResult(), JSTaggedValue(44)), true);
131 }
132 }  // namespace panda::test
133