1 /*
2 * Copyright (c) 2021-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 "ark_interop_external.h"
17 #include "ark_interop_hitrace.h"
18 #include "ark_interop_internal.h"
19 #include "ark_interop_log.h"
20 #include "ark_interop_napi.h"
21 #include "gtest/gtest.h"
22 #include "uv_loop_handler.h"
23
24 #include <thread>
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 struct ARKTS_ModuleCallbacks {
30 ARKTS_Value (*exportModule)(ARKTS_Env env, const char* dllName, ARKTS_Value exports) = nullptr;
31 bool (*hasModuleHandle)(const char* dllName) = nullptr;
32 void (*throwJSError)(ARKTS_Env env, ARKTS_Value) = nullptr;
33 void (*throwNativeError)(const char*) = nullptr;
34 void (*deleteArrayBufferRawData)(void* buffer, int64_t lambdaId) = nullptr;
35 void (*deleteExternal)(int64_t id, ARKTS_Env env) = nullptr;
36 ARKTS_Value (*invokerLambda)(ARKTS_CallInfo, int64_t lambdaId) = nullptr;
37 void (*deleteLambda)(ARKTS_Env env, int64_t lambdaId) = nullptr;
38 void (*invokeAsyncLambda)(ARKTS_Env env, int64_t lambdaId) = nullptr;
39 void (*deleteJSContext)(ARKTS_Env env) = nullptr;
40 };
41
42 namespace {
43 ARKTS_Engine engine_ = nullptr;
44
45 } // namespace
46
47 class ArkInteropTest : public testing::Test {};
48
49 HWTEST_F(ArkInteropTest, ArkTSInteropNapiAsync001, TestSize.Level1)
50 {
51 ARKTS_Env env = ARKTS_GetContext(engine_);
52 ARKTS_CreateAsyncTask(env, 0);
53 ARKTS_CreateAsyncTask(nullptr, 0);
54 ARKTS_CreateAsyncTask(env, 0);
55 }
56
57 HWTEST_F(ArkInteropTest, ArkTSInteropNapi001, TestSize.Level1)
58 {
59 ARKTS_Env env = ARKTS_GetContext(engine_);
60 ARKTS_GetGlobalConstant(env);
61 ARKTS_InitEventHandle(env);
62 }
63
64 HWTEST_F(ArkInteropTest, ArkTSInteropNapi005, TestSize.Level1)
65 {
66 ARKTS_Env env = ARKTS_GetContext(engine_);
67 EXPECT_NE(ARKTS_GetNAPIEnv(engine_), nullptr);
68
69 auto scope = ARKTS_OpenScope(env);
70 auto subscope = ARKTS_OpenScope(env);
71 ARKTS_CloseScope(env, subscope);
72 auto val = ARKTS_CreateF64(12.34);
73 EXPECT_NE(ARKTS_Return(env, scope, val), nullptr);
74 }
75
76 HWTEST_F(ArkInteropTest, ArkTSInteropNapi006, TestSize.Level1)
77 {
78 ARKTS_Env env = ARKTS_GetContext(engine_);
79 ARKTS_Value objv = ARKTS_CreateObject(env);
80 char origeStr[] = "key01";
81 auto key = ARKTS_CreateUtf8(env, origeStr, strlen(origeStr));
82 ARKTS_Value get = ARKTS_CreateFunc(env, 0);
83 ARKTS_Value set = ARKTS_CreateFunc(env, 0);
84 ARKTS_DefineAccessors(env, objv, key, {get, set, ARKTS_PropertyFlag::N_ENUMERABLE});
85 }
86
87 HWTEST_F(ArkInteropTest, ArkTSInteropNapi007, TestSize.Level1)
88 {
89 ARKTS_Env env = ARKTS_GetContext(engine_);
90 ARKTS_GetValueType(env, ARKTS_CreateNull());
91 ARKTS_GetValueType(env, ARKTS_CreateUndefined());
92
93 char des[] = "symbol test";
94 auto symv = ARKTS_CreateSymbol(env, des, strlen(des));
95 ARKTS_GetValueType(env, symv);
96 ARKTS_StrictEqual(env, symv, symv);
97
98 auto boolv = ARKTS_CreateBool(false);
99 ARKTS_GetValueType(env, boolv);
100 ARKTS_StrictEqual(env, boolv, boolv);
101 }
102
103 HWTEST_F(ArkInteropTest, ArkTSInteropNapiExternal001, TestSize.Level1)
104 {
105 ARKTS_Env env = ARKTS_GetContext(engine_);
106 char dllName[] = "123";
107 ARKTS_LoadModule(env, dllName);
108 }
109
110 HWTEST_F(ArkInteropTest, ArkTSInteropNapiHitrace001, TestSize.Level1)
111 {
112 char name[] = "";
113 int32_t taskId = 0;
114 ARKTS_HiTraceStartTrace(name, taskId);
115 ARKTS_HiTraceFinishTrace(name, taskId);
116 ARKTS_HiTraceCountTrace(name, taskId);
117 }
118
119 HWTEST_F(ArkInteropTest, ArkTSInteropNapiLog001, TestSize.Level1)
120 {
121 LOGI("test LOGI");
122 LOGE("test LOGE");
123 }
124
125 HWTEST_F(ArkInteropTest, ArkTSInteropNapiCreateEngineNew, TestSize.Level1)
126 {
127 auto engine = ARKTS_CreateEngineWithNewThread();
128
129 auto curTid = ARKTS_GetPosixThreadId();
130 auto engineTid = ARKTS_GetThreadIdOfEngine(engine);
131
132 EXPECT_NE(curTid, engineTid);
133
134 ARKTS_DestroyEngine(engine);
135 }
136
main(int argc,char ** argv)137 int main(int argc, char** argv)
138 {
139 engine_ = ARKTS_CreateEngine();
140 LOGI("main in");
141 testing::GTEST_FLAG(output) = "xml:./";
142 testing::InitGoogleTest(&argc, argv);
143 int ret = testing::UnitTest::GetInstance()->Run();
144 std::thread t([] {
145 ARKTS_DestroyEngine(engine_);
146 engine_ = nullptr;
147 });
148 if (!ret) {
149 LOGE("run test failed. return %d", ret);
150 return ret;
151 }
152 LOGI("main out");
153 return ret;
154 }
155