133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_COMMON_H
1733eb0b6dSopenharmony_ci#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_COMMON_H
1833eb0b6dSopenharmony_ci#include "gtest/gtest.h"
1933eb0b6dSopenharmony_ci#include "utils/log.h"
2033eb0b6dSopenharmony_ci#define ASSERT_CHECK_CALL(call)   \
2133eb0b6dSopenharmony_ci    {                             \
2233eb0b6dSopenharmony_ci        ASSERT_EQ(call, napi_ok); \
2333eb0b6dSopenharmony_ci    }
2433eb0b6dSopenharmony_ci
2533eb0b6dSopenharmony_ci#define ASSERT_CHECK_VALUE_TYPE(env, value, type)                   \
2633eb0b6dSopenharmony_ci    {                                                               \
2733eb0b6dSopenharmony_ci        do {                                                        \
2833eb0b6dSopenharmony_ci            napi_valuetype valueType = napi_undefined;              \
2933eb0b6dSopenharmony_ci            ASSERT_TRUE((value) != nullptr);                        \
3033eb0b6dSopenharmony_ci            ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
3133eb0b6dSopenharmony_ci            ASSERT_EQ(valueType, type);                             \
3233eb0b6dSopenharmony_ci        } while (0);                                                \
3333eb0b6dSopenharmony_ci    }
3433eb0b6dSopenharmony_ci
3533eb0b6dSopenharmony_ci#define RUN_EVENT_LOOP_WITH_MODE(env, mode)                      \
3633eb0b6dSopenharmony_ci    do {                                                         \
3733eb0b6dSopenharmony_ci        uv_loop_t* loop = nullptr;                               \
3833eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_uv_event_loop((env), &loop)); \
3933eb0b6dSopenharmony_ci        ASSERT_NE(loop, nullptr);                                \
4033eb0b6dSopenharmony_ci        uv_run(loop, (mode));                                    \
4133eb0b6dSopenharmony_ci    } while (0)
4233eb0b6dSopenharmony_ci
4333eb0b6dSopenharmony_ci#define RUN_EVENT_LOOP(env) RUN_EVENT_LOOP_WITH_MODE((env), UV_RUN_DEFAULT)
4433eb0b6dSopenharmony_ci
4533eb0b6dSopenharmony_ci#define STOP_EVENT_LOOP(env)                                     \
4633eb0b6dSopenharmony_ci    do {                                                         \
4733eb0b6dSopenharmony_ci        uv_loop_t* loop = nullptr;                               \
4833eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_uv_event_loop((env), &loop)); \
4933eb0b6dSopenharmony_ci        ASSERT_NE(loop, nullptr);                                \
5033eb0b6dSopenharmony_ci        uv_stop(loop);                                           \
5133eb0b6dSopenharmony_ci    } while (0)
5233eb0b6dSopenharmony_ci
5333eb0b6dSopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_COMMON_H */
54