1 /**
2 * Copyright (c) 2021-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 "ets_interop_js_gtest.h"
17 #include <iostream>
18 #include <memory>
19
20 namespace ark::ets::interop::js::testing {
21
22 napi_env EtsInteropTest::jsEnv_ = {};
23
24 class JsEnvAccessor {
25 public:
SetJsEnv(napi_env env)26 static void SetJsEnv(napi_env env)
27 {
28 EtsInteropTest::jsEnv_ = env;
29 }
30
ResetJsEnv()31 static void ResetJsEnv()
32 {
33 EtsInteropTest::jsEnv_ = {};
34 }
35 };
36
Main(napi_env env,napi_callback_info info)37 static napi_value Main(napi_env env, napi_callback_info info)
38 {
39 size_t jsArgc = 0;
40 [[maybe_unused]] napi_status status = napi_get_cb_info(env, info, &jsArgc, nullptr, nullptr, nullptr);
41 ASSERT(status == napi_ok);
42
43 if (jsArgc != 1) {
44 std::cerr << "ETS_INTEROP_GTEST_PLUGIN: Incorrect argc, argc=" << jsArgc << std::endl;
45 std::abort();
46 }
47
48 std::vector<napi_value> jsArgv(jsArgc);
49
50 napi_value thisArg = nullptr;
51 void *data = nullptr;
52 status = napi_get_cb_info(env, info, &jsArgc, jsArgv.data(), &thisArg, &data);
53 ASSERT(status == napi_ok);
54
55 napi_value jsArray = jsArgv.front();
56 uint32_t jsArrayLength = 0;
57 status = napi_get_array_length(env, jsArray, &jsArrayLength);
58 ASSERT(status == napi_ok);
59
60 std::vector<std::string> argv;
61 for (uint32_t i = 0; i < jsArrayLength; ++i) {
62 napi_value jsArrayElement;
63 status = napi_get_element(env, jsArray, i, &jsArrayElement);
64 ASSERT(status == napi_ok);
65
66 size_t strLength = 0;
67 status = napi_get_value_string_utf8(env, jsArrayElement, nullptr, 0, &strLength);
68 ASSERT(status == napi_ok);
69
70 std::string utf8Str(++strLength, '\0');
71 size_t copied = 0;
72 status = napi_get_value_string_utf8(env, jsArrayElement, utf8Str.data(), utf8Str.size(), &copied);
73 ASSERT(status == napi_ok);
74
75 argv.emplace_back(std::move(utf8Str));
76 }
77
78 int argc = jsArrayLength;
79 std::vector<char *> argvChar;
80 argvChar.reserve(argv.size());
81
82 for (auto &arg : argv) {
83 argvChar.emplace_back(arg.data());
84 }
85
86 ASSERT(size_t(argc) == argvChar.size());
87 ::testing::InitGoogleTest(&argc, argvChar.data());
88
89 // Run tests
90 JsEnvAccessor::SetJsEnv(env);
91 int retValue = RUN_ALL_TESTS();
92 JsEnvAccessor::ResetJsEnv();
93
94 napi_value jsRetValue;
95 status = napi_create_int32(env, retValue, &jsRetValue);
96 ASSERT(status == napi_ok);
97 return jsRetValue;
98 }
99
Init(napi_env env,napi_value exports)100 static napi_value Init(napi_env env, napi_value exports)
101 {
102 const std::array desc = {
103 napi_property_descriptor {"main", 0, Main, 0, 0, 0, napi_enumerable, 0},
104 };
105
106 [[maybe_unused]] napi_status status = napi_define_properties(env, exports, desc.size(), desc.data());
107 ASSERT(status == napi_ok);
108
109 return exports;
110 }
111
112 } // namespace ark::ets::interop::js::testing
113
114 NAPI_MODULE(GTEST_ADDON, ark::ets::interop::js::testing::Init)
115