• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41     }
42 
TearDown()43     void TearDown() override
44     {
45         TestHelper::DestroyEcmaVMWithScope(instance, scope);
46     }
47 
48     EcmaVM *instance {nullptr};
49     ecmascript::EcmaHandleScope *scope {nullptr};
50     JSThread *thread {nullptr};
51 };
52 
53 /**
54  * @tc.name: SetAsyncContext
55  * @tc.desc: Call "NewJSAsyncAwaitStatusFunction" function to construct class JSAsyncAwaitStatusFunction object, calling
56  *           "SetAsyncContext" function to set AsyncContext and check the AsyncContext is within expectations by calling
57  *           "GetAsyncContext" function.
58  * @tc.type: FUNC
59  * @tc.require:
60  */
HWTEST_F_L0(JSAsyncFunctionTest,SetAsyncContext)61 HWTEST_F_L0(JSAsyncFunctionTest, SetAsyncContext)
62 {
63     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64     JSHandle<JSAsyncFuncObject> asyncFuncObj = factory->NewJSAsyncFuncObject();
65     JSHandle<JSTaggedValue> asyncFuncObjVal(thread, asyncFuncObj->GetGeneratorContext());
66 
67     JSHandle<JSAsyncAwaitStatusFunction> fulFunc =
68         factory->NewJSAsyncAwaitStatusFunction(MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED);
69     fulFunc->SetAsyncContext(thread, asyncFuncObjVal);
70     EXPECT_EQ(JSTaggedValue::SameValue(fulFunc->GetAsyncContext(), asyncFuncObjVal.GetTaggedValue()), true);
71 
72     JSHandle<JSAsyncAwaitStatusFunction> rejFunc =
73         factory->NewJSAsyncAwaitStatusFunction(MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED);
74     rejFunc->SetAsyncContext(thread, asyncFuncObj->GetGeneratorContext());
75     EXPECT_EQ(JSTaggedValue::SameValue(rejFunc->GetAsyncContext(), asyncFuncObjVal.GetTaggedValue()), true);
76 }
77 
78 /**
79  * @tc.name: AsyncFunctionAwait
80  * @tc.desc: Call "NewJSAsyncFuncObject" function to construct class JSAsyncFuncObject object, calling
81  *           "AsyncFunctionAwait" function remove asyncContext and run execution context,but context is
82  *           undefined so cannot execute, the promise is undefined.
83  * @tc.type: FUNC
84  * @tc.require:
85  */
HWTEST_F_L0(JSAsyncFunctionTest,AsyncFunctionAwait)86 HWTEST_F_L0(JSAsyncFunctionTest, AsyncFunctionAwait)
87 {
88     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
89     JSHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue(33));
90     // asyncContext is undefined
91     JSHandle<JSAsyncFuncObject> asyncFuncObj = factory->NewJSAsyncFuncObject();
92     JSAsyncFunction::AsyncFunctionAwait(thread, asyncFuncObj, valueHandle);
93 
94     // check AsyncPromise queue and queue cannot execute
95     auto microJobQueue = thread->GetCurrentEcmaContext()->GetMicroJobQueue();
96     EXPECT_FALSE(microJobQueue->GetPromiseJobQueue().IsUndefined());
97     // promiese is undefined.
98     EXPECT_TRUE(asyncFuncObj->GetPromise().IsUndefined());
99 }
100 }  // namespace panda::test