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