• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 #ifndef PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H
17 #define PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H
18 
19 #include <gtest/gtest.h>
20 #include <gmock/gmock.h>
21 
22 #include "plugins/ets/runtime/napi/ets_scoped_objects_fix.h"
23 
24 namespace ark::ets::test {
25 
26 class EtsNapiTestBaseClass : public testing::Test {
27 public:
28     template <typename R, typename... Args>
CallEtsFunction(R * ret,std::string_view className,std::string_view methodName,Args &&...args)29     void CallEtsFunction(R *ret, std::string_view className, std::string_view methodName, Args &&...args)
30     {
31         std::string etsGlobalName(std::string(className) + "/ETSGLOBAL");
32         ets_class cls = env_->FindClass(etsGlobalName.data());
33         ASSERT_NE(cls, nullptr);
34         ets_method fn = env_->GetStaticp_method(cls, methodName.data(), nullptr);
35 
36         // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg)
37         if constexpr (std::is_same_v<R, ets_boolean>) {
38             *ret = env_->CallStaticBooleanMethod(cls, fn, args...);
39         } else if constexpr (std::is_same_v<R, ets_byte>) {
40             *ret = env_->CallStaticByteMethod(cls, fn, args...);
41         } else if constexpr (std::is_same_v<R, ets_char>) {
42             *ret = env_->CallStaticCharMethod(cls, fn, args...);
43         } else if constexpr (std::is_same_v<R, ets_short>) {
44             *ret = env_->CallStaticShortMethod(cls, fn, args...);
45         } else if constexpr (std::is_same_v<R, ets_int>) {
46             *ret = env_->CallStaticIntMethod(cls, fn, args...);
47         } else if constexpr (std::is_same_v<R, ets_long>) {
48             *ret = env_->CallStaticLongMethod(cls, fn, args...);
49         } else if constexpr (std::is_same_v<R, ets_float>) {
50             *ret = env_->CallStaticFloatMethod(cls, fn, args...);
51         } else if constexpr (std::is_same_v<R, ets_double>) {
52             *ret = env_->CallStaticDoubleMethod(cls, fn, args...);
53         } else if constexpr (std::is_same_v<R, ets_object> || std::is_same_v<R, ets_array> ||
54                              std::is_same_v<R, ets_arraybuffer>) {
55             *ret = static_cast<R>(env_->CallStaticObjectMethod(cls, fn, args...));
56         } else if constexpr (std::is_same_v<R, void>) {
57             // do nothing
58         } else {
59             enum { INCORRECT_TEMPLATE_TYPE = false };
60             static_assert(INCORRECT_TEMPLATE_TYPE, "Incorrect template type");
61         }
62         // NOLINTEND(cppcoreguidelines-pro-type-vararg)
63     }
64 
65 protected:
SetUp()66     void SetUp() override
67     {
68         const char *stdlib = std::getenv("ARK_ETS_STDLIB_PATH");
69         ASSERT_NE(stdlib, nullptr);
70 
71         // clang-format off
72         std::vector<EtsVMOption> optionsVector{
73             {EtsOptionType::ETS_GC_TYPE, "epsilon"},
74             {EtsOptionType::ETS_NO_JIT, nullptr},
75             {EtsOptionType::ETS_BOOT_FILE, stdlib}
76         };
77         // clang-format on
78 
79         abcPath_ = std::getenv("ANI_GTEST_ABC_PATH");
80         if (abcPath_ != nullptr) {
81             optionsVector.push_back({EtsOptionType::ETS_BOOT_FILE, abcPath_});
82         }
83 
84         EtsVMInitArgs vmArgs;
85         vmArgs.version = ETS_NAPI_VERSION_1_0;
86         vmArgs.options = optionsVector.data();
87         vmArgs.nOptions = static_cast<ets_int>(optionsVector.size());
88 
89         ASSERT_TRUE(ETS_CreateVM(&vm_, &env_, &vmArgs) == ETS_OK) << "Cannot create ETS VM";
90     }
91 
TearDown()92     void TearDown() override
93     {
94         ASSERT_TRUE(vm_->DestroyEtsVM() == ETS_OK) << "Cannot destroy ETS VM";
95     }
96 
97     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
98     const char *abcPath_ {nullptr};
99     EtsEnv *env_ {nullptr};
100     EtsVM *vm_ {nullptr};
101     // NOLINTEND(misc-non-private-member-variables-in-classes)
102 };
103 
104 }  // namespace ark::ets::test
105 
106 #endif  // PANDA_PLUGINS_ETS_TESTS_NATIVE_NATIVE_TEST_HELPER_H
107