• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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/js_async_function.h"
17 #include "ecmascript/js_generator_object.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/jobs/micro_job_queue.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda;
23 using namespace panda::ecmascript;
24 
25 namespace panda::test {
26 class JSAsyncFunctionTest : public BaseTestWithScope<false> {
27 };
28 
29 /**
30  * @tc.name: SetAsyncContext
31  * @tc.desc: Call "NewJSAsyncAwaitStatusFunction" function to construct class JSAsyncAwaitStatusFunction object, calling
32  *           "SetAsyncContext" function to set AsyncContext and check the AsyncContext is within expectations by calling
33  *           "GetAsyncContext" function.
34  * @tc.type: FUNC
35  * @tc.require:
36  */
HWTEST_F_L0(JSAsyncFunctionTest,SetAsyncContext)37 HWTEST_F_L0(JSAsyncFunctionTest, SetAsyncContext)
38 {
39     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
40     JSHandle<JSAsyncFuncObject> asyncFuncObj = factory->NewJSAsyncFuncObject();
41     JSHandle<JSTaggedValue> asyncFuncObjVal(thread, asyncFuncObj->GetGeneratorContext(thread));
42 
43     JSHandle<JSAsyncAwaitStatusFunction> fulFunc =
44         factory->NewJSAsyncAwaitStatusFunction(MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED);
45     fulFunc->SetAsyncContext(thread, asyncFuncObjVal);
46     EXPECT_EQ(JSTaggedValue::SameValue(thread, fulFunc->GetAsyncContext(thread), asyncFuncObjVal.GetTaggedValue()),
47               true);
48 
49     JSHandle<JSAsyncAwaitStatusFunction> rejFunc =
50         factory->NewJSAsyncAwaitStatusFunction(MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED);
51     rejFunc->SetAsyncContext(thread, asyncFuncObj->GetGeneratorContext(thread));
52     EXPECT_EQ(JSTaggedValue::SameValue(thread, rejFunc->GetAsyncContext(thread), asyncFuncObjVal.GetTaggedValue()),
53               true);
54 }
55 
56 /**
57  * @tc.name: AsyncFunctionAwait
58  * @tc.desc: Call "NewJSAsyncFuncObject" function to construct class JSAsyncFuncObject object, calling
59  *           "AsyncFunctionAwait" function remove asyncContext and run execution context,but context is
60  *           undefined so cannot execute, the promise is undefined.
61  * @tc.type: FUNC
62  * @tc.require:
63  */
HWTEST_F_L0(JSAsyncFunctionTest,AsyncFunctionAwait)64 HWTEST_F_L0(JSAsyncFunctionTest, AsyncFunctionAwait)
65 {
66     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67     JSHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue(33));
68     // asyncContext is undefined
69     JSHandle<JSAsyncFuncObject> asyncFuncObj = factory->NewJSAsyncFuncObject();
70     JSAsyncFunction::AsyncFunctionAwait(thread, asyncFuncObj, valueHandle);
71 
72     // check AsyncPromise queue and queue cannot execute
73     auto microJobQueue = thread->GetEcmaVM()->GetMicroJobQueue();
74     EXPECT_FALSE(microJobQueue->GetPromiseJobQueue(thread).IsUndefined());
75     // promiese is undefined.
76     EXPECT_TRUE(asyncFuncObj->GetPromise(thread).IsUndefined());
77 }
78 }  // namespace panda::test
79