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 "gtest/gtest.h"
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 #include "native_utils.h"
20 #define private public
21 #define protected public
22 #include "test.h"
23 #undef private
24 #include "test_common.h"
25 #include "utils/log.h"
26
27 constexpr const char TEST_ERROR_CODE[] = "500";
28 constexpr const char TEST_ERROR_MESSAGE[] = "Common error";
29 constexpr const char SENDABLE_CLASS_NAME[] = "MY_CLASS";
30 constexpr const char SENDABLE_FUNCTION_NAME[] = "func";
31 constexpr const char STRING_TEST[] = "a";
32 constexpr const char TEST_WRAP_STRING[] = "testWrapStr";
33 constexpr const int32_t LENGTH = 1024;
34 constexpr const char TEST_STR[] = "test";
35 constexpr const char TEST_CHAR_ASYNCWORK[] = "AsyncWorkTest";
36 constexpr const char TEST_KEY[] = "testKey";
37 constexpr const char TEST_VALUE[] = "testValue";
38 constexpr const char ENUMERABLE[] = "enumerable";
39 constexpr const char WRITEABLE[] = "writable";
40 constexpr const char CONFIGURABLE[] = "configurable";
41 constexpr const char TEST_CLASS[] = "TestClass";
42 constexpr const uint32_t REFERENCE_SIZE = 2;
43 constexpr const char MESSAGE[] = "message";
44 constexpr const char TEST_FUNC[] = "testFunc";
45 constexpr const char CONTEXT_NEW_PROP[] = "contextNewProp";
46 constexpr const char STR_ATTRIBUTE[] = "strAttribute";
47 constexpr const char STR_MODULE_PATH[] = "@ohos:xxxx";
48 constexpr const char TEST_STR_UTF8[] = "ut.latin1test.napi.!@#%^&*()6666";
49 constexpr const char16_t TEST_STR_UTF16[] = u"中文,English,123456,!@#$%$#^%&12345";
50 constexpr const char BUFFER_STR[] = "hello world";
51 constexpr const char PROPERTY_NAME[] = "toString";
52 constexpr const char MOD_NAME[] = "entry";
53 constexpr const char CONST_STRING_NUMBER[] = "number";
54 constexpr const char CONST_STRING_VALUE[] = "value";
55 constexpr const char CONST_STRING_DONE[] = "done";
56 constexpr const char CONST_STRING_PROP[] = "prop";
57
58 static constexpr int INT_ZERO = 0;
59 static constexpr int INT_ONE = 1;
60 static constexpr int INT_TWO = 2;
61 static constexpr int INT_THREE = 3;
62 static constexpr double TEST_DOUBLE = 1.1;
63 constexpr size_t TEST_ARRAY_LENGTH = 10;
64 constexpr uint32_t CONST_U32_1K = 1000;
65 constexpr uint32_t CONST_U32_2K = 2000;
66
67 static constexpr int32_t NAPI_BUFFER_SIZE = 5;
68 static constexpr int32_t TYPE_TAGS_SIZE = 5;
69 static constexpr size_t ARRAYBUFFER_SIZE = 1024;
70
71 static int g_hookTag = 0;
72 static int g_hookArgOne = 1;
73 static int g_hookArgTwo = 2;
74
SendableFunc(napi_env env,napi_callback_info info)75 static napi_value SendableFunc(napi_env env, napi_callback_info info)
76 {
77 return nullptr;
78 }
79
80 static const napi_type_tag typeTags[TYPE_TAGS_SIZE] = { // 5:array element size is 5.
81 {0xdaf987b3cc62481a, 0xb745b0497f299531},
82 {0xbb7936c374084d9b, 0xa9548d0762eeedb9},
83 {0xa5ed9ce2e4c00c38, 0},
84 {0, 0},
85 {0xa5ed9ce2e4c00c34, 0xdaf987b3cc62481a},
86 };
87
88 class NapiContextTest : public NativeEngineTest {
89 public:
NapiContextTest()90 NapiContextTest() : NativeEngineTest()
91 {
92 napi_create_ark_context(reinterpret_cast<napi_env>(engine_), reinterpret_cast<napi_env*>(&multiContextEngine_));
93 }
94
~NapiContextTest()95 ~NapiContextTest() override
96 {
97 napi_switch_ark_context(reinterpret_cast<napi_env>(engine_));
98 if (multiContextEngine_ != nullptr) {
99 napi_destroy_ark_context(reinterpret_cast<napi_env>(multiContextEngine_));
100 multiContextEngine_ = nullptr;
101 }
102 }
103
SetUpTestCase()104 static void SetUpTestCase()
105 {
106 GTEST_LOG_(INFO) << "NapiContextTest SetUpTestCase";
107 }
108
TearDownTestCase()109 static void TearDownTestCase()
110 {
111 GTEST_LOG_(INFO) << "NapiContextTest TearDownTestCase";
112 }
113
SetUp()114 void SetUp() override
115 {
116 napi_env env = reinterpret_cast<napi_env>(engine_);
117 napi_open_handle_scope(env, &scope_);
118 }
119
TearDown()120 void TearDown() override
121 {
122 napi_env env = reinterpret_cast<napi_env>(engine_);
123 napi_value exception = nullptr;
124 napi_get_and_clear_last_exception(env, &exception);
125 napi_close_handle_scope(env, scope_);
126 }
127
SetMainEnvContext(bool isMainContext)128 inline void SetMainEnvContext(bool isMainContext)
129 {
130 auto arkNativeEngine = reinterpret_cast<ArkNativeEngine* >(engine_);
131 arkNativeEngine->isMainEnvContext_ = isMainContext;
132 ASSERT_EQ(arkNativeEngine->IsMainEnvContext(), isMainContext);
133 }
134
CheckContextEnv()135 inline void CheckContextEnv()
136 {
137 ASSERT_NE(multiContextEngine_, nullptr);
138 bool isMainContextEnv = multiContextEngine_->IsMainEnvContext();
139 ASSERT_FALSE(isMainContextEnv);
140 }
141
142 private:
143 napi_handle_scope scope_ = nullptr;
144 };
145
146 /**
147 * @tc.name: NapiCreateContextTest001
148 * @tc.desc: Test napi_create_ark_context when the input argument env is nullptr.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(NapiContextTest, NapiCreateContextTest001, testing::ext::TestSize.Level1)
152 {
153 napi_env newEnv = nullptr;
154 napi_status res = napi_create_ark_context(nullptr, &newEnv);
155 EXPECT_EQ(res, napi_invalid_arg);
156 }
157
158 /**
159 * @tc.name: NapiCreateContextTest002
160 * @tc.desc: Test napi_create_ark_context when some exception exists.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(NapiContextTest, NapiCreateContextTest002, testing::ext::TestSize.Level1)
164 {
165 ASSERT_NE(engine_, nullptr);
166 napi_env env = reinterpret_cast<napi_env>(engine_);
167
168 // mock exception
169 napi_value value = nullptr;
170 ASSERT_CHECK_CALL(napi_get_undefined(env, &value));
171 Local<panda::JSValueRef> nativeValue = LocalValueFromJsValue(value);
172 engine_->lastException_ = Local<panda::ObjectRef>(nativeValue);
173
174 napi_env newEnv = nullptr;
175 napi_status status = napi_create_ark_context(env, &newEnv);
176 EXPECT_EQ(status, napi_pending_exception);
177
178 //clear exception
179 engine_->lastException_.Empty();
180 }
181
182 /**
183 * @tc.name: NapiCreateContextTest003
184 * @tc.desc: Test napi_create_ark_context when some exception exists.
185 * @tc.type: FUNC
186 */
187 HWTEST_F(NapiContextTest, NapiCreateContextTest003, testing::ext::TestSize.Level1)
188 {
189 ASSERT_NE(engine_, nullptr);
190 napi_env env = reinterpret_cast<napi_env>(engine_);
191
192 // mock exception
193 NAPI_THROW_ERROR(env, TEST_ERROR_CODE, TEST_ERROR_MESSAGE);
194
195 napi_env newEnv = nullptr;
196 EXPECT_EQ(napi_create_ark_context(env, &newEnv), napi_pending_exception);
197 //clear exception
198 napi_value error = nullptr;
199 EXPECT_EQ(napi_get_and_clear_last_exception(env, &error), napi_ok);
200 }
201
202 /**
203 * @tc.name: NapiCreateContextTest004
204 * @tc.desc: Test napi_create_ark_context when current env is not main env context.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(NapiContextTest, NapiCreateContextTest004, testing::ext::TestSize.Level1)
208 {
209 ASSERT_NE(engine_, nullptr);
210 napi_env env = reinterpret_cast<napi_env>(engine_);
211 auto arkNativeEngine = reinterpret_cast<ArkNativeEngine* >(engine_);
212 EXPECT_TRUE(arkNativeEngine->IsMainEnvContext());
213 arkNativeEngine->isMainEnvContext_ = false;
214 EXPECT_FALSE(arkNativeEngine->IsMainEnvContext());
215
216 napi_env newEnv = nullptr;
217 napi_status status = napi_create_ark_context(env, &newEnv);
218 EXPECT_EQ(status, napi_invalid_arg);
219
220 arkNativeEngine->isMainEnvContext_ = true;
221 EXPECT_TRUE(arkNativeEngine->IsMainEnvContext());
222 }
223
224 /**
225 * @tc.name: NapiCreateContextTest005
226 * @tc.desc: Test napi_create_ark_context when context is not empty.
227 * @tc.type: FUNC
228 */
229 HWTEST_F(NapiContextTest, NapiCreateContextTest005, testing::ext::TestSize.Level1)
230 {
231 ASSERT_NE(engine_, nullptr);
232 napi_env env = reinterpret_cast<napi_env>(engine_);
233
234 napi_env newEnv = nullptr;
235
236 napi_status status = napi_create_ark_context(env, &newEnv);
237 EXPECT_EQ(status, napi_ok);
238 EXPECT_EQ(napi_destroy_ark_context(newEnv), napi_ok);
239 }
240
241 /**
242 * @tc.name: NapiCreateContextTest006
243 * @tc.desc: Test napi_create_ark_context successfully.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(NapiContextTest, NapiCreateContextTest006, testing::ext::TestSize.Level1)
247 {
248 ASSERT_NE(engine_, nullptr);
__anon9b81ebcd0102() 249 std::thread t([]() {
250 NativeEngineProxy engine;
251 napi_env env = napi_env(engine);
252
253 NativeEngineProxy newEngine;
254 napi_env newEnv = napi_env(newEngine);
255 auto context = newEngine->context_;
256 (newEngine->context_).Empty();
257 napi_status status = napi_create_ark_context(env, &newEnv);
258 EXPECT_EQ(status, napi_ok);
259 newEngine->context_ = context;
260 });
261 t.join();
262 }
263
264 /**
265 * @tc.name: NapiCreateContextTest007
266 * @tc.desc: Test napi_create_ark_context with nullptr out parameter.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(NapiContextTest, NapiCreateContextTest007, testing::ext::TestSize.Level1)
270 {
271 ASSERT_NE(engine_, nullptr);
272 ASSERT_EQ(napi_create_ark_context(reinterpret_cast<napi_env>(engine_), nullptr), napi_invalid_arg);
273 }
274
275 /**
276 * @tc.name: NapiSwitchContextTest001
277 * @tc.desc: Test napi_switch_ark_context when the input argument env is nullptr.
278 * @tc.type: FUNC
279 */
280 HWTEST_F(NapiContextTest, NapiSwitchContextTest001, testing::ext::TestSize.Level1)
281 {
282 napi_status status = napi_switch_ark_context(nullptr);
283 EXPECT_EQ(status, napi_invalid_arg);
284 }
285
286 /**
287 * @tc.name: NapiSwitchContextTest002
288 * @tc.desc: Test napi_switch_ark_context when some exception exists.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(NapiContextTest, NapiSwitchContextTest002, testing::ext::TestSize.Level1)
292 {
293 ASSERT_NE(engine_, nullptr);
294 napi_env env = reinterpret_cast<napi_env>(engine_);
295
296 // mock exception
297 napi_value value = nullptr;
298 ASSERT_CHECK_CALL(napi_get_undefined(env, &value));
299 Local<panda::JSValueRef> nativeValue = LocalValueFromJsValue(value);
300 engine_->lastException_ = Local<panda::ObjectRef>(nativeValue);
301
302 napi_status status = napi_switch_ark_context(env);
303 EXPECT_EQ(status, napi_pending_exception);
304
305 //clear exception
306 engine_->lastException_.Empty();
307 }
308
309 /**
310 * @tc.name: NapiSwitchContextTest003
311 * @tc.desc: Test napi_switch_ark_context when some exception exists.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(NapiContextTest, NapiSwitchContextTest003, testing::ext::TestSize.Level1)
315 {
316 ASSERT_NE(engine_, nullptr);
317 napi_env env = reinterpret_cast<napi_env>(engine_);
318
319 NAPI_THROW_ERROR(env, TEST_ERROR_CODE, TEST_ERROR_MESSAGE);
320
321 EXPECT_EQ(napi_switch_ark_context(env), napi_pending_exception);
322
323 //clear exception
324 napi_value error = nullptr;
325 EXPECT_EQ(napi_get_and_clear_last_exception(env, &error), napi_ok);
326 }
327
328 /**
329 * @tc.name: NapiSwitchContextTest004
330 * @tc.desc: Test napi_switch_ark_context successfully.
331 * @tc.type: FUNC
332 */
333 HWTEST_F(NapiContextTest, NapiSwitchContextTest004, testing::ext::TestSize.Level1)
334 {
335 ASSERT_NE(engine_, nullptr);
336 napi_env env = reinterpret_cast<napi_env>(engine_);
337 napi_status status = napi_switch_ark_context(env);
338 EXPECT_EQ(status, napi_ok);
339 }
340
341 /**
342 * @tc.name: NapiDestroyContextTest001
343 * @tc.desc: Test napi_destroy_ark_context when the input argument env is nullptr.
344 * @tc.type: FUNC
345 */
346 HWTEST_F(NapiContextTest, NapiDestroyContextTest001, testing::ext::TestSize.Level1)
347 {
348 napi_status status = napi_destroy_ark_context(nullptr);
349 EXPECT_EQ(status, napi_invalid_arg);
350 }
351
352
353 /**
354 * @tc.name: NapiDestroyContextTest002
355 * @tc.desc: Test napi_destroy_ark_context when current env is using.
356 * @tc.type: FUNC
357 */
358 HWTEST_F(NapiContextTest, NapiDestroyContextTest002, testing::ext::TestSize.Level1)
359 {
360 NativeEngineProxy engine(engine_);
361 napi_env env = napi_env(engine);
362 EXPECT_EQ(napi_switch_ark_context(env), napi_ok);
363 napi_status status = napi_destroy_ark_context(env);
364 EXPECT_EQ(status, napi_invalid_arg);
365 EXPECT_EQ(napi_switch_ark_context(reinterpret_cast<napi_env>(engine_)), napi_ok);
366 }
367
368 /**
369 * @tc.name: NapiDestroyContextTest003
370 * @tc.desc: Test napi_destroy_ark_context when some exception exists.
371 * @tc.type: FUNC
372 */
373 HWTEST_F(NapiContextTest, NapiDestroyContextTest003, testing::ext::TestSize.Level1)
374 {
375 ASSERT_NE(engine_, nullptr);
376 napi_env env = reinterpret_cast<napi_env>(engine_);
377
378 // mock exception
379 napi_value value = nullptr;
380 ASSERT_CHECK_CALL(napi_get_undefined(env, &value));
381 Local<panda::JSValueRef> nativeValue = LocalValueFromJsValue(value);
382 engine_->lastException_ = Local<panda::ObjectRef>(nativeValue);
383
384 napi_status status = napi_destroy_ark_context(env);
385 EXPECT_EQ(status, napi_pending_exception);
386
387 //clear exception
388 engine_->lastException_.Empty();
389 }
390
391 /**
392 * @tc.name: NapiDestroyContextTest004
393 * @tc.desc: Test napi_create_ark_context when current env is main env context.
394 * @tc.type: FUNC
395 */
396 HWTEST_F(NapiContextTest, NapiDestroyContextTest004, testing::ext::TestSize.Level1)
397 {
398 ASSERT_NE(engine_, nullptr);
399 napi_env env = reinterpret_cast<napi_env>(engine_);
400 EXPECT_TRUE(engine_->IsMainEnvContext());
401 napi_status status = napi_destroy_ark_context(env);
402 EXPECT_EQ(status, napi_invalid_arg);
403 }
404
405 /**
406 * @tc.name: SetModuleValidateCallbackTest001
407 * @tc.desc: Test napi_set_module_validate_callback on context env.
408 * @tc.type: FUNC
409 */
410 HWTEST_F(NapiContextTest, SetModuleValidateCallbackTest001, testing::ext::TestSize.Level1)
411 {
412 ArkNativeEngine::moduleValidateCallback_ = nullptr;
413 ASSERT_EQ(napi_set_module_validate_callback(nullptr), napi_invalid_arg);
414 ArkNativeEngine::moduleValidateCallback_ = nullptr;
415 }
416
417 /**
418 * @tc.name: SetModuleValidateCallbackTest002
419 * @tc.desc: Test napi_set_module_validate_callback on context env.
420 * @tc.type: FUNC
421 */
422 HWTEST_F(NapiContextTest, SetModuleValidateCallbackTest002, testing::ext::TestSize.Level1)
423 {
424 ArkNativeEngine::moduleValidateCallback_ = nullptr;
__anon9b81ebcd0202(const char*) 425 ASSERT_CHECK_CALL(napi_set_module_validate_callback([](const char*) { return false; }));
426 ArkNativeEngine::moduleValidateCallback_ = nullptr;
427 }
428
429 /**
430 * @tc.name: SetModuleValidateCallbackTest003
431 * @tc.desc: Test napi_set_module_validate_callback on context env.
432 * @tc.type: FUNC
433 */
434 HWTEST_F(NapiContextTest, SetModuleValidateCallbackTest003, testing::ext::TestSize.Level1)
435 {
436 ArkNativeEngine::moduleValidateCallback_ = nullptr;
__anon9b81ebcd0302(const char*) 437 ASSERT_CHECK_CALL(napi_set_module_validate_callback([](const char*) { return false; }));
__anon9b81ebcd0402(const char*) 438 ASSERT_EQ(napi_set_module_validate_callback([](const char*) { return false; }), napi_generic_failure);
439 ArkNativeEngine::moduleValidateCallback_ = nullptr;
440 }
441
442 /**
443 * @tc.name: NapiGetUvEventLoopWithMultiContextTest001
444 * @tc.desc: Test napi_get_uv_event_loop when designated env is generated by napi_create_ark_context interface.
445 * @tc.type: FUNC
446 */
447 HWTEST_F(NapiContextTest, NapiGetUvEventLoopWithMultiContextTest001, testing::ext::TestSize.Level1)
448 {
449 ASSERT_NE(engine_, nullptr);
450 napi_env env = reinterpret_cast<napi_env>(engine_);
451 SetMainEnvContext(false);
452 struct uv_loop_s** loop = nullptr;
453 napi_status status = napi_get_uv_event_loop(env, loop);
454 EXPECT_EQ(status, napi_invalid_arg);
455 SetMainEnvContext(true);
456 }
457
458 /**
459 * @tc.name: RequireMethodTest001
460 * @tc.desc: Test globalThis.requireNapi method.
461 * @tc.type: FUNC
462 */
463 HWTEST_F(NapiContextTest, RequireMethodTest001, testing::ext::TestSize.Level1)
464 {
465 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
466 const char requireName[] = "requireInternal";
467 napi_value require = nullptr;
468 ASSERT_CHECK_CALL(napi_create_string_utf8(env, requireName, NAPI_AUTO_LENGTH, &require));
469
470 napi_value global = nullptr;
471 ASSERT_CHECK_CALL(napi_get_global(env, &global));
472
473 bool hasProp = false;
474 ASSERT_CHECK_CALL(napi_has_property(env, global, require, &hasProp));
475 ASSERT_TRUE(hasProp);
476
477 napi_value requireFunc = nullptr;
478 ASSERT_CHECK_CALL(napi_get_property(env, global, require, &requireFunc));
479 ASSERT_CHECK_VALUE_TYPE(env, requireFunc, napi_function);
480 }
481
482 /**
483 * @tc.name: RequireMethodTest002
484 * @tc.desc: Test globalThis.requireNapi method.
485 * @tc.type: FUNC
486 */
487 HWTEST_F(NapiContextTest, RequireMethodTest002, testing::ext::TestSize.Level1)
488 {
489 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
490 const char requireName[] = "requireNapi";
491 napi_value require = nullptr;
492 ASSERT_CHECK_CALL(napi_create_string_utf8(env, requireName, NAPI_AUTO_LENGTH, &require));
493
494 napi_value global = nullptr;
495 ASSERT_CHECK_CALL(napi_get_global(env, &global));
496
497 bool hasProp = false;
498 ASSERT_CHECK_CALL(napi_has_property(env, global, require, &hasProp));
499 ASSERT_TRUE(hasProp);
500
501 napi_value requireFunc = nullptr;
502 ASSERT_CHECK_CALL(napi_get_property(env, global, require, &requireFunc));
503 ASSERT_CHECK_VALUE_TYPE(env, requireFunc, napi_function);
504 }
505
506 /**
507 * @tc.name: CreateSendableArrayWithMultiContext001
508 * @tc.desc: Test napi_create_sendable_array when designated env is generated by napi_create_ark_context interface.
509 * @tc.type: FUNC
510 */
511 HWTEST_F(NapiContextTest, CreateSendableArrayWithMultiContext001, testing::ext::TestSize.Level1)
512 {
513 ASSERT_NE(engine_, nullptr);
514 napi_env env = reinterpret_cast<napi_env>(engine_);
515
516 SetMainEnvContext(false);
517 napi_value result = nullptr;
518 napi_status res = napi_ok;
519 res = napi_create_sendable_array(env, &result);
520 EXPECT_EQ(res, napi_invalid_arg);
521 SetMainEnvContext(true);
522 }
523
524 /**
525 * @tc.name: CreateSendableArrayWithLengthWithMultiContext001
526 * @tc.desc: Test napi_create_sendable_array_with_length when designated env is generated by napi_create_ark_context
527 * interface.
528 * @tc.type: FUNC
529 */
530 HWTEST_F(NapiContextTest, CreateSendableArrayWithLengthWithMultiContext001, testing::ext::TestSize.Level1)
531 {
532 ASSERT_NE(engine_, nullptr);
533 napi_env env = reinterpret_cast<napi_env>(engine_);
534
535 SetMainEnvContext(false);
536 napi_value result = nullptr;
537 napi_status res = napi_ok;
538 res = napi_create_sendable_array_with_length(env, 0, &result);
539 EXPECT_EQ(res, napi_invalid_arg);
540 SetMainEnvContext(true);
541 }
542
543 /**
544 * @tc.name: IsSendableWithMultiContext001
545 * @tc.desc: Test napi_is_sendable when designated env is generated by napi_create_ark_context interface.
546 * @tc.type: FUNC
547 */
548 HWTEST_F(NapiContextTest, IsSendableWithMultiContext001, testing::ext::TestSize.Level1)
549 {
550 ASSERT_NE(engine_, nullptr);
551 napi_env env = reinterpret_cast<napi_env>(engine_);
552 napi_status res = napi_ok;
553
554 napi_value result = nullptr;
555 res = napi_create_sendable_array_with_length(env, LENGTH, &result);
556 EXPECT_EQ(res, napi_ok);
557
558 SetMainEnvContext(false);
559 bool isShared = false;
560 res = napi_is_sendable(env, result, &isShared);
561 EXPECT_EQ(res, napi_invalid_arg);
562 SetMainEnvContext(true);
563 }
564
565 /**
566 * @tc.name: SendableClassWithMultiContextTest001
567 * @tc.desc: Test napi_define_sendable_class when designated env is generated by napi_create_ark_context interface.
568 * @tc.type: FUNC
569 */
570 HWTEST_F(NapiContextTest, SendableClassWithMultiContextTest001, testing::ext::TestSize.Level1)
571 {
572 napi_env env = reinterpret_cast<napi_env>(engine_);
573 SetMainEnvContext(false);
574 napi_value myClass = nullptr;
575 napi_status status = napi_define_sendable_class(
576 env, SENDABLE_CLASS_NAME, NAPI_AUTO_LENGTH,
__anon9b81ebcd0502(napi_env env, napi_callback_info info) 577 [](napi_env env, napi_callback_info info) -> napi_value {
578 napi_value thisVar = nullptr;
579 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
580 return thisVar;
581 },
582 nullptr, 0, nullptr, nullptr, &myClass);
583 EXPECT_EQ(status, napi_invalid_arg);
584 SetMainEnvContext(true);
585 }
586
587 /**
588 * @tc.name: CreateSendableObjectWithPropertiesWithMultiContextTest001
589 * @tc.desc: Test napi_create_sendable_object_with_properties when designated env is generated by
590 * napi_create_ark_context interface.
591 * @tc.type: FUNC
592 */
593 HWTEST_F(NapiContextTest, CreateSendableObjectWithPropertiesWithMultiContextTest001, testing::ext::TestSize.Level1)
594 {
595 napi_env env = reinterpret_cast<napi_env>(engine_);
596 napi_value exception = nullptr;
597 ASSERT_CHECK_CALL(napi_get_and_clear_last_exception(env, &exception));
598
599 napi_value obj1 = nullptr;
600 napi_property_descriptor desc1[] = {
601 DECLARE_NAPI_FUNCTION(SENDABLE_FUNCTION_NAME, SendableFunc),
602 };
603
604 SetMainEnvContext(false);
605 napi_status status = napi_create_sendable_object_with_properties(env, 1, desc1, &obj1);
606 EXPECT_EQ(status, napi_invalid_arg);
607 SetMainEnvContext(true);
608 }
609
610 /**
611 * @tc.name: CreateSendableMapWithMultiContext001
612 * @tc.desc: Test napi_create_sendable_map when designated env is generated by napi_create_ark_context interface.
613 * @tc.type: FUNC
614 */
615 HWTEST_F(NapiContextTest, CreateSendableMapWithMultiContext001, testing::ext::TestSize.Level1)
616 {
617 ASSERT_NE(engine_, nullptr);
618 napi_env env = reinterpret_cast<napi_env>(engine_);
619 napi_status res = napi_ok;
620
621 SetMainEnvContext(false);
622 napi_value result = nullptr;
623 res = napi_create_sendable_map(env, &result);
624 EXPECT_EQ(res, napi_invalid_arg);
625 SetMainEnvContext(true);
626 }
627
628 /**
629 * @tc.name: WrapSendableWithMultiContextTest001
630 * @tc.desc: Test napi_wrap_sendable when designated env is generated by napi_create_ark_context interface.
631 * @tc.type: FUNC
632 */
633 HWTEST_F(NapiContextTest, WrapSendableWithMultiContextTest001, testing::ext::TestSize.Level1)
634 {
635 napi_env env = (napi_env)engine_;
636 napi_value exception = nullptr;
637 ASSERT_CHECK_CALL(napi_get_and_clear_last_exception(env, &exception));
638
639 napi_value obj1 = nullptr;
640 napi_property_descriptor desc1[] = {
641 DECLARE_NAPI_FUNCTION(SENDABLE_FUNCTION_NAME, SendableFunc),
642 };
643 ASSERT_CHECK_CALL(napi_create_sendable_object_with_properties(env, 1, desc1, &obj1));
644 bool isShared = false;
645 napi_is_sendable(env, obj1, &isShared);
646 EXPECT_EQ(isShared, true);
647
648 SetMainEnvContext(false);
649 napi_status status = napi_wrap_sendable(env, obj1, (void*)STRING_TEST, nullptr, nullptr);
650 EXPECT_EQ(status, napi_invalid_arg);
651 SetMainEnvContext(true);
652 }
653
654 /**
655 * @tc.name: WrapSendableWithMultiContextTest002
656 * @tc.desc: Test napi_wrap_sendable_with_size when designated env is generated by napi_create_ark_context interface.
657 * @tc.type: FUNC
658 */
659 HWTEST_F(NapiContextTest, WrapSendableWithMultiContextTest002, testing::ext::TestSize.Level1)
660 {
661 napi_env env = (napi_env)engine_;
662
663 napi_value testWrapClass = nullptr;
664 napi_define_sendable_class(
665 env, SENDABLE_CLASS_NAME, NAPI_AUTO_LENGTH,
__anon9b81ebcd0602(napi_env env, napi_callback_info info) 666 [](napi_env env, napi_callback_info info) -> napi_value {
667 napi_value thisVar = nullptr;
668 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
669 return thisVar;
670 },
671 nullptr, 0, nullptr, nullptr, &testWrapClass);
672
673 napi_value instanceValue = nullptr;
674 napi_new_instance(env, testWrapClass, 0, nullptr, &instanceValue);
675
676 size_t size = sizeof(STRING_TEST) / sizeof(char);
677 SetMainEnvContext(false);
678 napi_status status = napi_wrap_sendable_with_size(
__anon9b81ebcd0702(napi_env env, void* data, void* hint) 679 env, instanceValue, (void*)STRING_TEST, [](napi_env env, void* data, void* hint) {}, nullptr, size);
680 EXPECT_EQ(status, napi_invalid_arg);
681 SetMainEnvContext(true);
682 }
683
684 /**
685 * @tc.name: WrapSendableWithMultiContextTest003
686 * @tc.desc: Test napi_unwrap_sendable when designated env is generated by napi_create_ark_context interface.
687 * @tc.type: FUNC
688 */
689 HWTEST_F(NapiContextTest, WrapSendableWithMultiContextTest003, testing::ext::TestSize.Level1)
690 {
691 napi_env env = (napi_env)engine_;
692
693 napi_value testWrapClass = nullptr;
694 napi_define_sendable_class(
695 env, SENDABLE_CLASS_NAME, NAPI_AUTO_LENGTH,
__anon9b81ebcd0802(napi_env env, napi_callback_info info) 696 [](napi_env env, napi_callback_info info) -> napi_value {
697 napi_value thisVar = nullptr;
698 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
699 return thisVar;
700 },
701 nullptr, 0, nullptr, nullptr, &testWrapClass);
702
703 napi_value instanceValue = nullptr;
704 napi_new_instance(env, testWrapClass, 0, nullptr, &instanceValue);
705
706 size_t size = sizeof(TEST_WRAP_STRING) / sizeof(char);
707 napi_wrap_sendable_with_size(
__anon9b81ebcd0902(napi_env env, void* data, void* hint) 708 env, instanceValue, (void*)TEST_WRAP_STRING, [](napi_env env, void* data, void* hint) {}, nullptr, size);
709
710 char* tempTestStr = nullptr;
711 SetMainEnvContext(false);
712 napi_status status = napi_unwrap_sendable(env, instanceValue, (void**)&tempTestStr);
713 EXPECT_EQ(status, napi_invalid_arg);
714 SetMainEnvContext(true);
715 }
716
717 /**
718 * @tc.name: WrapSendableWithMultiContextTest004
719 * @tc.desc: Test napi_remove_wrap_sendable when designated env is generated by napi_create_ark_context interface.
720 * @tc.type: FUNC
721 */
722 HWTEST_F(NapiContextTest, WrapSendableWithMultiContextTest004, testing::ext::TestSize.Level1)
723 {
724 napi_env env = (napi_env)engine_;
725 napi_status res = napi_ok;
726
727 napi_value testClass = nullptr;
728 res = napi_define_sendable_class(
729 env, SENDABLE_CLASS_NAME, NAPI_AUTO_LENGTH,
__anon9b81ebcd0a02(napi_env env, napi_callback_info info) 730 [](napi_env env, napi_callback_info info) -> napi_value {
731 napi_value thisVar = nullptr;
732 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
733 return thisVar;
734 },
735 nullptr, 0, nullptr, nullptr, &testClass);
736 ASSERT_EQ(res, napi_ok);
737
738 napi_value instanceValue = nullptr;
739 res = napi_new_instance(env, testClass, 0, nullptr, &instanceValue);
740 ASSERT_EQ(res, napi_ok);
741
742 res = napi_wrap_sendable(
__anon9b81ebcd0b02(napi_env env, void* data, void* hint) 743 env, instanceValue, (void*)STRING_TEST, [](napi_env env, void* data, void* hint) {}, nullptr);
744 ASSERT_EQ(res, napi_ok);
745
746 char* tmpTestStr = nullptr;
747 res = napi_unwrap_sendable(env, instanceValue, (void**)&tmpTestStr);
748 ASSERT_EQ(res, napi_ok);
749 ASSERT_STREQ(STRING_TEST, tmpTestStr);
750
751 char* tmpTestStr1 = nullptr;
752 SetMainEnvContext(false);
753 res = napi_remove_wrap_sendable(env, instanceValue, (void**)&tmpTestStr1);
754 EXPECT_EQ(res, napi_invalid_arg);
755 SetMainEnvContext(true);
756 }
757
758 /**
759 * @tc.name: CreateSendableArrayBuffer002
760 * @tc.desc: Test napi_create_sendable_arraybuffer when designated env is generated by napi_create_ark_context
761 * interface.
762 * @tc.type: FUNC
763 */
764 HWTEST_F(NapiContextTest, CreateSendableArrayBuffer002, testing::ext::TestSize.Level1)
765 {
766 ASSERT_NE(engine_, nullptr);
767 napi_env env = reinterpret_cast<napi_env>(engine_);
768 napi_status res = napi_ok;
769
770 void *data;
771 napi_value result = nullptr;
772 SetMainEnvContext(false);
773 res = napi_create_sendable_arraybuffer(env, LENGTH, &data, &result);
774 ASSERT_EQ(res, napi_invalid_arg);
775 SetMainEnvContext(true);
776 }
777
778 /**
779 * @tc.name: CreateSendableTypedArrayWithMultiContext001
780 * @tc.desc: Test napi_create_sendable_typedarray when designated env is generated by napi_create_ark_context
781 * interface.
782 * @tc.type: FUNC
783 */
784 HWTEST_F(NapiContextTest, CreateSendableTypedArrayWithMultiContext001, testing::ext::TestSize.Level1)
785 {
786 static size_t LENGTH = 1024;
787 ASSERT_NE(engine_, nullptr);
788 napi_env env = reinterpret_cast<napi_env>(engine_);
789 napi_status res = napi_ok;
790
791 void *data;
792 napi_value arraybuffer = nullptr;
793 napi_value result = nullptr;
794 res = napi_create_sendable_arraybuffer(env, LENGTH, &data, &arraybuffer);
795 ASSERT_EQ(res, napi_ok);
796
797 bool isShared = false;
798 res = napi_is_sendable(env, arraybuffer, &isShared);
799 ASSERT_EQ(res, napi_ok);
800 ASSERT_EQ(isShared, true);
801 SetMainEnvContext(false);
802 res = napi_create_sendable_typedarray(env, napi_uint8_clamped_array, LENGTH / 2, arraybuffer, 1, &result);
803 ASSERT_EQ(res, napi_invalid_arg);
804 SetMainEnvContext(true);
805 }
806
807 /**
808 * @tc.name: ExternalTestWithMultiContext001
809 * @tc.desc: Test external type.
810 * @tc.type: FUNC
811 */
812 HWTEST_F(NapiContextTest, ExternalTestWithMultiContext001, testing::ext::TestSize.Level1)
813 {
814 ASSERT_NE(engine_, nullptr);
815 napi_env env = (napi_env)engine_;
816 // mock not main env context case
817 EXPECT_TRUE(engine_->IsMainEnvContext());
818 auto arkNativeEngine = reinterpret_cast<ArkNativeEngine* >(engine_);
819 arkNativeEngine->isMainEnvContext_ = false;
820
821 napi_value external = nullptr;
822 napi_create_external(
823 env, (void*)TEST_STR,
__anon9b81ebcd0c02(napi_env env, void* data, void* hint) 824 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
825 (void*)TEST_STR, &external);
826
827 ASSERT_CHECK_VALUE_TYPE(env, external, napi_external);
828 void* tmpExternal = nullptr;
829 napi_get_value_external(env, external, &tmpExternal);
830 ASSERT_TRUE(tmpExternal);
831 ASSERT_EQ(tmpExternal, TEST_STR);
832 arkNativeEngine->isMainEnvContext_ = true;
833 }
834
835 /**
836 * @tc.name: CreateExternalWithSizeAndMultiContextTest001
837 * @tc.desc: Test create external with size.
838 * @tc.type: FUNC
839 */
840 HWTEST_F(NapiContextTest, CreateExternalWithSizeTest001, testing::ext::TestSize.Level1)
841 {
842 ASSERT_NE(engine_, nullptr);
843 napi_env env = (napi_env)engine_;
844 // mock not main env context case
845 EXPECT_TRUE(engine_->IsMainEnvContext());
846 auto arkNativeEngine = reinterpret_cast<ArkNativeEngine* >(engine_);
847 arkNativeEngine->isMainEnvContext_ = false;
848
849 size_t size = sizeof(TEST_STR) / sizeof(char);
850 napi_value external = nullptr;
851 napi_create_external_with_size(
852 env, (void*)TEST_STR,
__anon9b81ebcd0d02(napi_env env, void* data, void* hint) 853 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
854 (void*)TEST_STR, &external, size);
855
856 ASSERT_CHECK_VALUE_TYPE(env, external, napi_external);
857 void* tempExternal = nullptr;
858 napi_get_value_external(env, external, &tempExternal);
859 ASSERT_TRUE(tempExternal);
860 ASSERT_EQ(tempExternal, TEST_STR);
861 arkNativeEngine->isMainEnvContext_ = true;
862 }
863
864 /**
865 * @tc.name: NapiCreateExternalArraybufferWithMultiContextTest001
866 * @tc.desc: Test interface of napi_create_external_arraybuffer
867 * @tc.type: FUNC
868 */
869 HWTEST_F(NapiContextTest, NapiCreateExternalArraybufferWithMultiContextTest001, testing::ext::TestSize.Level1)
870 {
871 ASSERT_NE(engine_, nullptr);
872 napi_env env = reinterpret_cast<napi_env>(engine_);
873 // mock not main env context case
874 EXPECT_TRUE(engine_->IsMainEnvContext());
875 auto arkNativeEngine = reinterpret_cast<ArkNativeEngine* >(engine_);
876 arkNativeEngine->isMainEnvContext_ = false;
877
878 napi_value external = nullptr;
879 auto res = napi_create_external_arraybuffer(
880 env, (void*)TEST_STR, strlen(TEST_STR),
__anon9b81ebcd0e02(napi_env env, void* data, void* hint) 881 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
882 (void*)TEST_STR, &external);
883 ASSERT_EQ(res, napi_ok);
884
885 arkNativeEngine->isMainEnvContext_ = true;
886 }
887
888 /**
889 * @tc.name: NapiCreateExternalBufferWithMultiContextTest001
890 * @tc.desc: Test interface of napi_create_external_buffer
891 * @tc.type: FUNC
892 */
893 HWTEST_F(NapiContextTest, NapiCreateExternalBufferWithMultiContextTest001, testing::ext::TestSize.Level1)
894 {
895 ASSERT_NE(engine_, nullptr);
896 napi_env env = (napi_env)engine_;
897
898 napi_value buffer = nullptr;
899 size_t bufferSize = NAPI_BUFFER_SIZE;
900 napi_status status = napi_create_external_buffer(
__anon9b81ebcd0f02(napi_env env, void* data, void* hint) 901 env, bufferSize, (void*)TEST_STR, [](napi_env env, void* data, void* hint) {
902 ASSERT_STREQ((const char*)data, (const char*)hint);
903 }, (void*)TEST_STR, &buffer);
904 ASSERT_EQ(status, napi_ok);
905 void* tmpBufferPtr = nullptr;
906 size_t bufferLength = 0;
907 status = napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
908 ASSERT_EQ(status, napi_ok);
909 bool isBuffer = false;
910 status = napi_is_buffer(env, buffer, &isBuffer);
911 ASSERT_EQ(status, napi_ok);
912 ASSERT_EQ(bufferSize, bufferLength);
913 }
914
915 /**
916 * @tc.name: AsyncInitWithMultiContext001
917 * @tc.desc: Test napi_async_init when context is sub context.
918 * interface.
919 * @tc.type: FUNC
920 */
921 HWTEST_F(NapiContextTest, AsyncInitWithMultiContext001, testing::ext::TestSize.Level1)
922 {
923 CheckContextEnv();
924 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
925
926 napi_value name;
927 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR, NAPI_AUTO_LENGTH, &name));
928
929 napi_async_context context = nullptr;
930 ASSERT_CHECK_CALL(napi_async_init(env, nullptr, name, &context));
931 }
932
933 /**
934 * @tc.name: CreateBufferWithMultiContext001
935 * @tc.desc: Test napi_create_buffer when context is sub context.
936 * interface.
937 * @tc.type: FUNC
938 */
939 HWTEST_F(NapiContextTest, CreateBufferWithMultiContext001, testing::ext::TestSize.Level1)
940 {
941 CheckContextEnv();
942 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
943
944 napi_value buffer = nullptr;
945 void* bufferPtr = nullptr;
946 size_t bufferSize = NAPI_BUFFER_SIZE;
947 ASSERT_CHECK_CALL(napi_create_buffer(env, bufferSize, &bufferPtr, &buffer));
948 }
949
950 /**
951 * @tc.name: CreateExternalBufferWithMultiContext001
952 * @tc.desc: Test napi_create_external_buffer when context is sub context.
953 * interface.
954 * @tc.type: FUNC
955 */
956 HWTEST_F(NapiContextTest, CreateExternalBufferWithMultiContext001, testing::ext::TestSize.Level1)
957 {
958 CheckContextEnv();
959 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
960
961 const unsigned int bufferSize = NAPI_BUFFER_SIZE;
962 char* copyPtr = strdup(TEST_STR);
963 napi_value result;
964 ASSERT_CHECK_CALL(napi_create_external_buffer(env, bufferSize, copyPtr,
__anon9b81ebcd1002(napi_env env, void* data, void* hint) 965 [](napi_env env, void* data, void* hint) {
966 free(reinterpret_cast<char*>(data));
967 },
968 nullptr, &result));
969 }
970
971 /**
972 * @tc.name: CreateBufferCopyWithMultiContext001
973 * @tc.desc: Test napi_create_buffer_copy when context is sub context.
974 * interface.
975 * @tc.type: FUNC
976 */
977 HWTEST_F(NapiContextTest, CreateBufferCopyWithMultiContext001, testing::ext::TestSize.Level1)
978 {
979 CheckContextEnv();
980 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
981
982 napi_value Buffer = nullptr;
983 void* resultData = nullptr;
984 size_t BufferSize = sizeof(TEST_STR);
985 ASSERT_CHECK_CALL(napi_create_buffer_copy(env, BufferSize, TEST_STR, &resultData, &Buffer));
986 }
987
988 /**
989 * @tc.name: CreateAsyncWorkWithMultiContext001
990 * @tc.desc: Test napi_create_buffer_copy when context is sub context.
991 * interface.
992 * @tc.type: FUNC
993 */
994 HWTEST_F(NapiContextTest, CreateAsyncWorkWithMultiContext001, testing::ext::TestSize.Level1)
995 {
996 CheckContextEnv();
997 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
998
999 struct AsyncWorkContext {
1000 napi_async_work work = nullptr;
1001 };
1002 std::unique_ptr<AsyncWorkContext> asyncWorkContext = std::make_unique<AsyncWorkContext>();
1003 napi_value resourceName = nullptr;
1004 napi_create_string_utf8(env, TEST_CHAR_ASYNCWORK, NAPI_AUTO_LENGTH, &resourceName);
1005 napi_status status = napi_create_async_work(
__anon9b81ebcd1102(napi_env env, void* data) 1006 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anon9b81ebcd1202(napi_env env, napi_status status, void* data) 1007 [](napi_env env, napi_status status, void* data) {},
1008 nullptr, &asyncWorkContext->work);
1009 ASSERT_EQ(status, napi_ok);
1010 }
1011
1012 /**
1013 * @tc.name: GetGlobalWithMultiContext001
1014 * @tc.desc: Test napi_get_global when context is sub context.
1015 * interface.
1016 * @tc.type: FUNC
1017 */
1018 HWTEST_F(NapiContextTest, GetGlobalWithMultiContext001, testing::ext::TestSize.Level1)
1019 {
1020 CheckContextEnv();
1021 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1022 ASSERT_NE(engine_, nullptr);
1023 napi_env oldEnv = reinterpret_cast<napi_env>(engine_);
1024
1025 napi_value global = nullptr;
1026 napi_value undefined = nullptr;
1027 ASSERT_CHECK_CALL(napi_get_global(env, &global));
1028 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined));
1029 ASSERT_CHECK_CALL(napi_set_named_property(env, global, CONTEXT_NEW_PROP, undefined));
1030 bool hasProp = false;
1031 ASSERT_CHECK_CALL(napi_has_named_property(env, global, CONTEXT_NEW_PROP, &hasProp));
1032 ASSERT_EQ(hasProp, true);
1033
1034 hasProp = false;
1035 napi_value globalOld = nullptr;
1036 ASSERT_CHECK_CALL(napi_get_global(oldEnv, &globalOld));
1037 ASSERT_CHECK_CALL(napi_has_named_property(oldEnv, globalOld, CONTEXT_NEW_PROP, &hasProp));
1038 ASSERT_EQ(hasProp, false);
1039 }
1040
1041 /**
1042 * @tc.name: CreateFunctionWithMultiContext001
1043 * @tc.desc: Test napi_create_function when context is sub context.
1044 * interface.
1045 * @tc.type: FUNC
1046 */
1047 HWTEST_F(NapiContextTest, CreateFunctionWithMultiContext001, testing::ext::TestSize.Level1)
1048 {
1049 CheckContextEnv();
1050 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1051
__anon9b81ebcd1302(napi_env env, napi_callback_info info) 1052 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
1053 return nullptr;
1054 };
1055 napi_value fn;
1056 const char data[] = "data";
1057 ASSERT_CHECK_CALL(napi_create_function(env, nullptr, 0, func, (void*)data, &fn));
1058 }
1059
1060 /**
1061 * @tc.name: CreateErrorWithMultiContext001
1062 * @tc.desc: Test napi_create_error when context is sub context.
1063 * interface.
1064 * @tc.type: FUNC
1065 */
1066 HWTEST_F(NapiContextTest, CreateErrorWithMultiContext001, testing::ext::TestSize.Level1)
1067 {
1068 CheckContextEnv();
1069 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1070
1071 napi_value code = nullptr;
1072 napi_value message = nullptr;
1073 napi_create_string_latin1(env, TEST_ERROR_CODE, NAPI_AUTO_LENGTH, &code);
1074 napi_create_string_latin1(env, TEST_ERROR_MESSAGE, NAPI_AUTO_LENGTH, &message);
1075
1076 napi_value error = nullptr;
1077 ASSERT_CHECK_CALL(napi_create_error(env, code, message, &error));
1078 }
1079
1080 /**
1081 * @tc.name: CreateTypeErrorWithMultiContext001
1082 * @tc.desc: Test napi_create_type_error when context is sub context.
1083 * interface.
1084 * @tc.type: FUNC
1085 */
1086 HWTEST_F(NapiContextTest, CreateTypeErrorWithMultiContext001, testing::ext::TestSize.Level1)
1087 {
1088 CheckContextEnv();
1089 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1090
1091 napi_value code = nullptr;
1092 napi_value message = nullptr;
1093 napi_create_string_latin1(env, TEST_ERROR_CODE, NAPI_AUTO_LENGTH, &code);
1094 napi_create_string_latin1(env, TEST_ERROR_MESSAGE, NAPI_AUTO_LENGTH, &message);
1095
1096 napi_value error = nullptr;
1097 ASSERT_CHECK_CALL(napi_create_type_error(env, code, message, &error));
1098 }
1099
1100 /**
1101 * @tc.name: CreateRangeErrorWithMultiContext001
1102 * @tc.desc: Test napi_create_range_error when context is sub context.
1103 * interface.
1104 * @tc.type: FUNC
1105 */
1106 HWTEST_F(NapiContextTest, CreateRangeErrorWithMultiContext001, testing::ext::TestSize.Level1)
1107 {
1108 CheckContextEnv();
1109 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1110
1111 napi_value code = nullptr;
1112 napi_value message = nullptr;
1113 napi_create_string_latin1(env, TEST_ERROR_CODE, NAPI_AUTO_LENGTH, &code);
1114 napi_create_string_latin1(env, TEST_ERROR_MESSAGE, NAPI_AUTO_LENGTH, &message);
1115
1116 napi_value error = nullptr;
1117 ASSERT_CHECK_CALL(napi_create_range_error(env, code, message, &error));
1118 }
1119
1120 /**
1121 * @tc.name: CoerceToNumberWithMultiContext001
1122 * @tc.desc: Test napi_coerce_to_number when context is sub context.
1123 * interface.
1124 * @tc.type: FUNC
1125 */
1126 HWTEST_F(NapiContextTest, CoerceToNumberWithMultiContext001, testing::ext::TestSize.Level1)
1127 {
1128 CheckContextEnv();
1129 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1130
1131 napi_value value;
1132 napi_value result;
1133 napi_status status = napi_create_string_utf8(env, "", 0, &value);
1134 status = napi_coerce_to_number(env, value, &result);
1135 ASSERT_EQ(status, napi_ok);
1136 }
1137
1138 /**
1139 * @tc.name: CoerceToObjectWithMultiContext001
1140 * @tc.desc: Test napi_coerce_to_object when context is sub context.
1141 * interface.
1142 * @tc.type: FUNC
1143 */
1144 HWTEST_F(NapiContextTest, CoerceToObjectWithMultiContext001, testing::ext::TestSize.Level1)
1145 {
1146 CheckContextEnv();
1147 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1148
1149 napi_value value;
1150 napi_value result;
1151 napi_status status = napi_get_undefined(env, &value);
1152 status = napi_coerce_to_object(env, value, &result);
1153 ASSERT_EQ(status, napi_ok);
1154 }
1155
1156 /**
1157 * @tc.name: CoerceToStringWithMultiContext001
1158 * @tc.desc: Test napi_coerce_to_string when context is sub context.
1159 * interface.
1160 * @tc.type: FUNC
1161 */
1162 HWTEST_F(NapiContextTest, CoerceToStringWithMultiContext001, testing::ext::TestSize.Level1)
1163 {
1164 CheckContextEnv();
1165 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1166
1167 napi_value argument;
1168 napi_get_boolean(env, true, &argument);
1169 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_boolean);
1170
1171 napi_value result;
1172 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
1173 }
1174
1175 /**
1176 * @tc.name: NapiEnvReferenceManagerTest001
1177 * @tc.desc: Test code of engine constructor
1178 * @tc.type: FUNC
1179 */
1180 HWTEST_F(NapiContextTest, NapiEnvReferenceManagerTest001, testing::ext::TestSize.Level1)
1181 {
1182 ASSERT_NE(multiContextEngine_->GetReferenceManager(), nullptr);
1183 }
1184
1185 /**
1186 * @tc.name: NapiEnvReferenceManagerTest002
1187 * @tc.desc: Test code of engine constructor
1188 * @tc.type: FUNC
1189 */
1190 HWTEST_F(NapiContextTest, NapiEnvReferenceManagerTest002, testing::ext::TestSize.Level1)
1191 {
1192 ASSERT_NE(engine_->GetReferenceManager(), nullptr);
1193 }
1194
1195 /**
1196 * @tc.name: NapiEnvCallbackScopeManagerTest001
1197 * @tc.desc: Test code of engine constructor
1198 * @tc.type: FUNC
1199 */
1200 HWTEST_F(NapiContextTest, NapiEnvCallbackScopeManagerTest001, testing::ext::TestSize.Level1)
1201 {
1202 ASSERT_NE(multiContextEngine_->GetCallbackScopeManager(), nullptr);
1203 }
1204
1205 /**
1206 * @tc.name: NapiEnvCallbackScopeManagerTest002
1207 * @tc.desc: Test code of engine constructor
1208 * @tc.type: FUNC
1209 */
1210 HWTEST_F(NapiContextTest, NapiEnvCallbackScopeManagerTest002, testing::ext::TestSize.Level1)
1211 {
1212 ASSERT_NE(engine_->GetCallbackScopeManager(), nullptr);
1213 }
1214
EmptyNapiCallback(napi_env,napi_callback_info)1215 napi_value EmptyNapiCallback(napi_env, napi_callback_info)
1216 {
1217 return nullptr;
1218 }
1219
1220 /**
1221 * @tc.name: CreateObjWithPropsWithinMultiContext001
1222 * @tc.desc: Test napi_create_object_with_properties when context is sub context.
1223 * @tc.type: FUNC
1224 */
1225 HWTEST_F(NapiContextTest, CreateObjWithPropsWithinMultiContext001, testing::ext::TestSize.Level1)
1226 {
1227 ASSERT_NE(multiContextEngine_, nullptr);
1228 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1229
1230 napi_value propJsName = nullptr;
1231 ASSERT_CHECK_CALL(napi_create_string_utf8(env, CONST_STRING_PROP, NAPI_AUTO_LENGTH, &propJsName));
1232 napi_value result = nullptr;
1233 napi_property_descriptor desc[] = { {
1234 CONST_STRING_PROP,
1235 nullptr,
1236 EmptyNapiCallback,
1237 nullptr,
1238 nullptr,
1239 nullptr,
1240 napi_default,
1241 nullptr,
1242 } };
1243 ASSERT_CHECK_CALL(napi_create_object_with_properties(env, &result, sizeof(desc) / sizeof(desc[0]), desc));
1244 napi_value val = nullptr;
1245 ASSERT_CHECK_CALL(napi_get_property(env, result, propJsName, &val));
1246 ASSERT_CHECK_VALUE_TYPE(env, val, napi_function);
1247 }
1248
1249 /**
1250 * @tc.name: CreateObjWithPropsWithinMultiContext002
1251 * @tc.desc: Test napi_create_object_with_properties when context is sub context.
1252 * @tc.type: FUNC
1253 */
1254 HWTEST_F(NapiContextTest, CreateObjWithPropsWithinMultiContext002, testing::ext::TestSize.Level1)
1255 {
1256 ASSERT_NE(multiContextEngine_, nullptr);
1257 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1258
1259 napi_value propJsName = nullptr;
1260 ASSERT_CHECK_CALL(napi_create_string_utf8(env, CONST_STRING_PROP, NAPI_AUTO_LENGTH, &propJsName));
1261 napi_value result = nullptr;
1262 napi_property_descriptor desc[] = { {
1263 CONST_STRING_PROP,
1264 nullptr,
1265 nullptr,
1266 EmptyNapiCallback,
1267 nullptr,
1268 nullptr,
1269 napi_default,
1270 nullptr,
1271 } };
1272 ASSERT_CHECK_CALL(napi_create_object_with_properties(env, &result, sizeof(desc) / sizeof(desc[0]), desc));
1273 napi_value val = nullptr;
1274 ASSERT_CHECK_CALL(napi_get_property(env, result, propJsName, &val));
1275 ASSERT_CHECK_VALUE_TYPE(env, val, napi_undefined);
1276 bool hasProp = false;
1277 ASSERT_CHECK_CALL(napi_has_property(env, result, propJsName, &hasProp));
1278 ASSERT_TRUE(hasProp);
1279 }
1280
1281 /**
1282 * @tc.name: CreateObjWithPropsWithinMultiContext003
1283 * @tc.desc: Test napi_create_object_with_properties when context is sub context.
1284 * @tc.type: FUNC
1285 */
1286 HWTEST_F(NapiContextTest, CreateObjWithPropsWithinMultiContext003, testing::ext::TestSize.Level1)
1287 {
1288 ASSERT_NE(multiContextEngine_, nullptr);
1289 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1290
1291 napi_value propJsName = nullptr;
1292 ASSERT_CHECK_CALL(napi_create_string_utf8(env, CONST_STRING_PROP, NAPI_AUTO_LENGTH, &propJsName));
1293 napi_value result = nullptr;
1294 napi_property_descriptor desc[] = { {
1295 CONST_STRING_PROP,
1296 nullptr,
1297 nullptr,
1298 nullptr,
1299 nullptr,
1300 propJsName,
1301 napi_default,
1302 nullptr,
1303 } };
1304 ASSERT_CHECK_CALL(napi_create_object_with_properties(env, &result, sizeof(desc) / sizeof(desc[0]), desc));
1305 napi_value val = nullptr;
1306 ASSERT_CHECK_CALL(napi_get_property(env, result, propJsName, &val));
1307 ASSERT_CHECK_VALUE_TYPE(env, val, napi_string);
1308 }
1309
1310 /**
1311 * @tc.name: CreateObjWithNamedPropsWithinMultiContext001
1312 * @tc.desc: Test napi_create_object_with_named_properties when context is sub context.
1313 * @tc.type: FUNC
1314 */
1315 HWTEST_F(NapiContextTest, CreateObjWithNamedPropsWithinMultiContext001, testing::ext::TestSize.Level1)
1316 {
1317 ASSERT_NE(multiContextEngine_, nullptr);
1318 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1319
1320 const char* names[] = { CONST_STRING_PROP };
1321 napi_value propJsName = nullptr;
1322 ASSERT_CHECK_CALL(napi_create_string_utf8(env, CONST_STRING_PROP, NAPI_AUTO_LENGTH, &propJsName));
1323
1324 napi_value result = nullptr;
1325 ASSERT_CHECK_CALL(napi_create_object_with_named_properties(env, &result, 1, names, &propJsName));
1326 napi_value val = nullptr;
1327 ASSERT_CHECK_CALL(napi_get_property(env, result, propJsName, &val));
1328 ASSERT_CHECK_VALUE_TYPE(env, val, napi_string);
1329 }
1330
1331 /**
1332 * @tc.name: CoerceToNativeBindingObjectWithMultiContext001
1333 * @tc.desc: Test napi_coerce_to_native_binding_object when context is sub context.
1334 * @tc.type: FUNC
1335 */
1336 HWTEST_F(NapiContextTest, CoerceToNativeBindingObjectWithMultiContext001, testing::ext::TestSize.Level1)
1337 {
1338 ASSERT_NE(multiContextEngine_, nullptr);
1339 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1340
__anon9b81ebcd1402null1341 bool* testResult = new bool[] { false, false }; // detached, attached
1342 ASSERT_FALSE(testResult[0]);
1343 ASSERT_FALSE(testResult[1]);
1344
1345 napi_value object = nullptr;
1346 ASSERT_CHECK_CALL(napi_create_object(env, &object));
1347 ASSERT_CHECK_CALL(napi_coerce_to_native_binding_object(
1348 env, object,
__anon9b81ebcd1502(napi_env, void* data, void*) 1349 [](napi_env, void* data, void*) -> void* {
1350 reinterpret_cast<bool*>(data)[0] = true;
1351 return data;
1352 },
__anon9b81ebcd1602(napi_env, void* data, void*) 1353 [](napi_env, void* data, void*) -> napi_value {
1354 reinterpret_cast<bool*>(data)[1] = true;
1355 return nullptr;
1356 },
1357 testResult, nullptr));
1358 napi_value undefined = nullptr;
1359 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined))
1360 void* serializeData = nullptr;
1361 // trigger detach callback
1362 ASSERT_CHECK_CALL(napi_serialize(env, object, undefined, undefined, &serializeData));
1363 ASSERT_TRUE(testResult[0]);
1364 napi_value result = nullptr;
1365 // trigger attach callback
1366 ASSERT_CHECK_CALL(napi_deserialize(env, serializeData, &result));
1367 ASSERT_TRUE(testResult[1]);
1368 // release serialize data
1369 ASSERT_CHECK_CALL(napi_delete_serialization_data(env, serializeData));
1370 serializeData = nullptr;
1371 delete [] testResult;
1372 }
1373
1374 /**
1375 * @tc.name: AddDetachedFinalizerWithMultiContext001
1376 * @tc.desc: Test napi_add_detached_finalizer when context is sub context.
1377 * @tc.type: FUNC
1378 */
1379 HWTEST_F(NapiContextTest, AddDetachedFinalizerWithMultiContext001, testing::ext::TestSize.Level1)
1380 {
1381 ASSERT_NE(multiContextEngine_, nullptr);
1382 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1383
1384 bool* detachFinalizerRan = new bool(false);
1385 napi_value object = nullptr;
1386 ASSERT_CHECK_CALL(napi_create_object(env, &object));
1387 ASSERT_CHECK_CALL(napi_coerce_to_native_binding_object(
1388 env, object,
__anon9b81ebcd1702(napi_env, void* data, void*) 1389 [](napi_env, void* data, void*) -> void* {
1390 reinterpret_cast<bool*>(data)[0] = true;
1391 return data;
1392 },
__anon9b81ebcd1802(napi_env, void* data, void*) 1393 [](napi_env, void* data, void*) -> napi_value {
1394 reinterpret_cast<bool*>(data)[1] = true;
1395 return nullptr;
1396 },
1397 detachFinalizerRan, nullptr));
1398 ASSERT_CHECK_CALL(napi_add_detached_finalizer(
__anon9b81ebcd1902(void* data, void*) 1399 env, object, [](void* data, void*) { *reinterpret_cast<bool*>(data) = true; }, nullptr));
1400
1401 napi_value undefined = nullptr;
1402 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined))
1403 void* serializeData = nullptr;
1404 // make serialize data
1405 ASSERT_CHECK_CALL(napi_serialize(env, object, undefined, undefined, &serializeData));
1406 // trigger detach finalize callback
1407 ASSERT_CHECK_CALL(napi_delete_serialization_data(env, serializeData));
1408 ASSERT_TRUE(*detachFinalizerRan);
1409 delete detachFinalizerRan;
1410 }
1411
1412 /**
1413 * @tc.name: RunEventLoopWithMultiContext001
1414 * @tc.desc: Test napi_run_event_loop when context is sub context.
1415 * @tc.type: FUNC
1416 */
1417 HWTEST_F(NapiContextTest, RunEventLoopWithMultiContext001, testing::ext::TestSize.Level1)
1418 {
1419 ASSERT_NE(multiContextEngine_, nullptr);
1420 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1421
1422 ASSERT_EQ(napi_run_event_loop(env, napi_event_mode_default), napi_invalid_arg);
1423 }
1424
1425 /**
1426 * @tc.name: RunEventLoopWithMultiContext002
1427 * @tc.desc: Test napi_run_event_loop when context is sub context.
1428 * @tc.type: FUNC
1429 */
1430 HWTEST_F(NapiContextTest, RunEventLoopWithMultiContext002, testing::ext::TestSize.Level1)
1431 {
1432 ASSERT_NE(multiContextEngine_, nullptr);
1433 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1434
1435 ASSERT_EQ(napi_run_event_loop(env, napi_event_mode_default), napi_invalid_arg);
1436 }
1437
1438 /**
1439 * @tc.name: StopEventLoopWithMultiContext001
1440 * @tc.desc: Test napi_stop_event_loop when context is sub context.
1441 * @tc.type: FUNC
1442 */
1443 HWTEST_F(NapiContextTest, StopEventLoopWithMultiContext001, testing::ext::TestSize.Level1)
1444 {
1445 ASSERT_NE(multiContextEngine_, nullptr);
1446 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1447
1448 ASSERT_EQ(napi_stop_event_loop(env), napi_invalid_arg);
1449 }
1450
1451 /**
1452 * @tc.name: DeserializeWithMultiContext001
1453 * @tc.desc: Test napi_deserialize when context is sub context.
1454 * @tc.type: FUNC
1455 */
1456 HWTEST_F(NapiContextTest, DeserializeWithMultiContext001, testing::ext::TestSize.Level1)
1457 {
1458 ASSERT_NE(multiContextEngine_, nullptr);
1459 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1460
1461 napi_value object = nullptr;
1462 ASSERT_CHECK_CALL(napi_create_object(env, &object));
1463
1464 napi_value undefined = nullptr;
1465 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined));
1466 void* serializeData = nullptr;
1467 // make serialize data
1468 ASSERT_CHECK_CALL(napi_serialize(env, object, undefined, undefined, &serializeData));
1469 napi_value result = nullptr;
1470 ASSERT_CHECK_CALL(napi_deserialize(env, serializeData, &result));
1471 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
1472 ASSERT_CHECK_CALL(napi_delete_serialization_data(env, serializeData));
1473 }
1474
1475 /**
1476 * @tc.name: DeleteDeserializeDataWithMultiContext001
1477 * @tc.desc: Test napi_delete_serialization_data when context is sub context.
1478 * @tc.type: FUNC
1479 */
1480 HWTEST_F(NapiContextTest, DeleteDeserializeDataWithMultiContext001, testing::ext::TestSize.Level1)
1481 {
1482 ASSERT_NE(multiContextEngine_, nullptr);
1483 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1484
1485 napi_value object = nullptr;
1486 ASSERT_CHECK_CALL(napi_create_object(env, &object));
1487
1488 napi_value undefined = nullptr;
1489 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined));
1490 void* serializeData = nullptr;
1491 // make serialize data
1492 ASSERT_CHECK_CALL(napi_serialize(env, object, undefined, undefined, &serializeData));
1493 ASSERT_CHECK_CALL(napi_delete_serialization_data(env, serializeData));
1494 }
1495
1496 /**
1497 * @tc.name: IsConcurrentFunctionWithMultiContext001
1498 * @tc.desc: Test napi_is_concurrent_function when context is sub context.
1499 * @tc.type: FUNC
1500 */
1501 HWTEST_F(NapiContextTest, IsConcurrentFunctionWithMultiContext001, testing::ext::TestSize.Level1)
1502 {
1503 ASSERT_NE(multiContextEngine_, nullptr);
1504 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1505
1506 napi_value callback = nullptr;
1507 ASSERT_CHECK_CALL(napi_create_function(env, __func__, NAPI_AUTO_LENGTH, EmptyNapiCallback, nullptr, &callback));
1508 bool isConcurrent = true;
1509 ASSERT_CHECK_CALL(napi_is_concurrent_function(env, callback, &isConcurrent));
1510 ASSERT_FALSE(isConcurrent);
1511 }
1512
1513 /**
1514 * @tc.name: CreateMapWithMultiContext001
1515 * @tc.desc: Test napi_create_map when context is sub context.
1516 * @tc.type: FUNC
1517 */
1518 HWTEST_F(NapiContextTest, CreateMapWithMultiContext001, testing::ext::TestSize.Level1)
1519 {
1520 ASSERT_NE(multiContextEngine_, nullptr);
1521 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1522
1523 napi_value map = nullptr;
1524 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1525 bool isMap = false;
1526 ASSERT_CHECK_CALL(napi_is_map(env, map, &isMap));
1527 ASSERT_TRUE(isMap);
1528 }
1529
1530 /**
1531 * @tc.name: MapSetPropertyWithMultiContext001
1532 * @tc.desc: Test napi_map_set_property when context is sub context.
1533 * @tc.type: FUNC
1534 */
1535 HWTEST_F(NapiContextTest, MapSetPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1536 {
1537 ASSERT_NE(multiContextEngine_, nullptr);
1538 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1539
1540 napi_value map = nullptr;
1541 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1542 napi_value str = nullptr;
1543 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1544 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1545 bool hasProp = false;
1546 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &hasProp));
1547 ASSERT_TRUE(hasProp);
1548 }
1549
1550 /**
1551 * @tc.name: MapSetNamedPropertyWithMultiContext001
1552 * @tc.desc: Test napi_map_set_named_property when context is sub context.
1553 * @tc.type: FUNC
1554 */
1555 HWTEST_F(NapiContextTest, MapSetNamedPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1556 {
1557 ASSERT_NE(multiContextEngine_, nullptr);
1558 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1559
1560 napi_value map = nullptr;
1561 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1562 napi_value str = nullptr;
1563 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1564 ASSERT_CHECK_CALL(napi_map_set_named_property(env, map, __func__, str));
1565 bool hasProp = false;
1566 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &hasProp));
1567 ASSERT_TRUE(hasProp);
1568 }
1569
1570 /**
1571 * @tc.name: MapGetPropertyWithMultiContext001
1572 * @tc.desc: Test napi_map_get_property when context is sub context.
1573 * @tc.type: FUNC
1574 */
1575 HWTEST_F(NapiContextTest, MapGetPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1576 {
1577 ASSERT_NE(multiContextEngine_, nullptr);
1578 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1579
1580 napi_value map = nullptr;
1581 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1582 napi_value str = nullptr;
1583 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1584 napi_value result = nullptr;
1585 ASSERT_CHECK_CALL(napi_map_get_property(env, map, str, &result));
1586 ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
1587 }
1588
1589 /**
1590 * @tc.name: MapGetPropertyWithMultiContext002
1591 * @tc.desc: Test napi_map_get_property when context is sub context.
1592 * @tc.type: FUNC
1593 */
1594 HWTEST_F(NapiContextTest, MapGetPropertyWithMultiContext002, testing::ext::TestSize.Level1)
1595 {
1596 ASSERT_NE(multiContextEngine_, nullptr);
1597 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1598
1599 napi_value map = nullptr;
1600 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1601 napi_value str = nullptr;
1602 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1603 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1604 napi_value result = nullptr;
1605 ASSERT_CHECK_CALL(napi_map_get_property(env, map, str, &result));
1606 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
1607 }
1608
1609 /*
1610 * @tc.name: MapGetNamedPropertyWithMultiContext001
1611 * @tc.desc: Test napi_map_get_named_property when context is sub context.
1612 * @tc.type: FUNC
1613 */
1614 HWTEST_F(NapiContextTest, MapGetNamedPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1615 {
1616 ASSERT_NE(multiContextEngine_, nullptr);
1617 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1618
1619 napi_value map = nullptr;
1620 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1621 napi_value str = nullptr;
1622 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1623 napi_value result = nullptr;
1624 ASSERT_CHECK_CALL(napi_map_get_named_property(env, map, __func__, &result));
1625 ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
1626 }
1627
1628 /**
1629 * @tc.name: MapGetNamedPropertyWithMultiContext002
1630 * @tc.desc: Test napi_map_get_named_property when context is sub context.
1631 * @tc.type: FUNC
1632 */
1633 HWTEST_F(NapiContextTest, MapGetNamedPropertyWithMultiContext002, testing::ext::TestSize.Level1)
1634 {
1635 ASSERT_NE(multiContextEngine_, nullptr);
1636 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1637
1638 napi_value map = nullptr;
1639 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1640 napi_value str = nullptr;
1641 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1642 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1643 napi_value result = nullptr;
1644 ASSERT_CHECK_CALL(napi_map_get_named_property(env, map, __func__, &result));
1645 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
1646 }
1647
1648
1649 /**
1650 * @tc.name: MapHasPropertyWithMultiContext001
1651 * @tc.desc: Test napi_map_get_property when context is sub context.
1652 * @tc.type: FUNC
1653 */
1654 HWTEST_F(NapiContextTest, MapHasPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1655 {
1656 ASSERT_NE(multiContextEngine_, nullptr);
1657 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1658
1659 napi_value map = nullptr;
1660 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1661 napi_value str = nullptr;
1662 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1663 bool result = true;
1664 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &result));
1665 ASSERT_FALSE(result);
1666 }
1667
1668 /**
1669 * @tc.name: MapHasPropertyWithMultiContext002
1670 * @tc.desc: Test napi_map_get_property when context is sub context.
1671 * @tc.type: FUNC
1672 */
1673 HWTEST_F(NapiContextTest, MapHasPropertyWithMultiContext002, testing::ext::TestSize.Level1)
1674 {
1675 ASSERT_NE(multiContextEngine_, nullptr);
1676 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1677
1678 napi_value map = nullptr;
1679 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1680 napi_value str = nullptr;
1681 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1682 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1683 bool result = false;
1684 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &result));
1685 ASSERT_TRUE(result);
1686 }
1687
1688 /**
1689 * @tc.name: MapHasNamedPropertyWithMultiContext001
1690 * @tc.desc: Test napi_map_has_named_property when context is sub context.
1691 * @tc.type: FUNC
1692 */
1693 HWTEST_F(NapiContextTest, MapHasNamedPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1694 {
1695 ASSERT_NE(multiContextEngine_, nullptr);
1696 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1697
1698 napi_value map = nullptr;
1699 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1700 napi_value str = nullptr;
1701 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1702 bool result = true;
1703 ASSERT_CHECK_CALL(napi_map_has_named_property(env, map, __func__, &result));
1704 ASSERT_FALSE(result);
1705 }
1706
1707 /**
1708 * @tc.name: MapHasNamedPropertyWithMultiContext002
1709 * @tc.desc: Test napi_map_has_named_property when context is sub context.
1710 * @tc.type: FUNC
1711 */
1712 HWTEST_F(NapiContextTest, MapHasNamedPropertyWithMultiContext002, testing::ext::TestSize.Level1)
1713 {
1714 ASSERT_NE(multiContextEngine_, nullptr);
1715 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1716
1717 napi_value map = nullptr;
1718 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1719 napi_value str = nullptr;
1720 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1721 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1722 bool result = false;
1723 ASSERT_CHECK_CALL(napi_map_has_named_property(env, map, __func__, &result));
1724 ASSERT_TRUE(result);
1725 }
1726
1727 /**
1728 * @tc.name: MapDeleteNamedPropertyWithMultiContext001
1729 * @tc.desc: Test napi_map_delete_property when context is sub context.
1730 * @tc.type: FUNC
1731 */
1732 HWTEST_F(NapiContextTest, MapDeleteNamedPropertyWithMultiContext001, testing::ext::TestSize.Level1)
1733 {
1734 ASSERT_NE(multiContextEngine_, nullptr);
1735 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1736
1737 napi_value map = nullptr;
1738 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1739 napi_value str = nullptr;
1740 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1741 ASSERT_CHECK_CALL(napi_map_delete_property(env, map, str));
1742 }
1743
1744 /**
1745 * @tc.name: MapDeleteNamedPropertyWithMultiContext002
1746 * @tc.desc: Test napi_map_delete_property when context is sub context.
1747 * @tc.type: FUNC
1748 */
1749 HWTEST_F(NapiContextTest, MapDeleteNamedPropertyWithMultiContext002, testing::ext::TestSize.Level1)
1750 {
1751 ASSERT_NE(multiContextEngine_, nullptr);
1752 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1753
1754 napi_value map = nullptr;
1755 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1756 napi_value str = nullptr;
1757 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1758 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1759 bool hasProp = false;
1760 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &hasProp));
1761 ASSERT_TRUE(hasProp);
1762 ASSERT_CHECK_CALL(napi_map_delete_property(env, map, str));
1763 hasProp = true;
1764 ASSERT_CHECK_CALL(napi_map_has_property(env, map, str, &hasProp));
1765 ASSERT_FALSE(hasProp);
1766 }
1767
1768 /**
1769 * @tc.name: MapClearWithMultiContext001
1770 * @tc.desc: Test napi_map_clear when context is sub context.
1771 * @tc.type: FUNC
1772 */
1773 HWTEST_F(NapiContextTest, MapClearWithMultiContext001, testing::ext::TestSize.Level1)
1774 {
1775 ASSERT_NE(multiContextEngine_, nullptr);
1776 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1777
1778 napi_value map = nullptr;
1779 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1780 ASSERT_CHECK_CALL(napi_map_clear(env, map));
1781 }
1782
1783 /**
1784 * @tc.name: MapClearWithMultiContext002
1785 * @tc.desc: Test napi_map_clear when context is sub context.
1786 * @tc.type: FUNC
1787 */
1788 HWTEST_F(NapiContextTest, MapClearWithMultiContext002, testing::ext::TestSize.Level1)
1789 {
1790 ASSERT_NE(multiContextEngine_, nullptr);
1791 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1792
1793 napi_value map = nullptr;
1794 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1795 napi_value str = nullptr;
1796 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1797 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1798 uint32_t size = 0;
1799 ASSERT_CHECK_CALL(napi_map_get_size(env, map, &size));
1800 ASSERT_GT(size, 0);
1801 ASSERT_CHECK_CALL(napi_map_clear(env, map));
1802 ASSERT_CHECK_CALL(napi_map_get_size(env, map, &size));
1803 ASSERT_EQ(size, 0);
1804 }
1805
1806 /**
1807 * @tc.name: MapGetSizeWithMultiContext001
1808 * @tc.desc: Test napi_map_get_size when context is sub context.
1809 * @tc.type: FUNC
1810 */
1811 HWTEST_F(NapiContextTest, MapGetSizeWithMultiContext001, testing::ext::TestSize.Level1)
1812 {
1813 ASSERT_NE(multiContextEngine_, nullptr);
1814 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1815
1816 napi_value map = nullptr;
1817 ASSERT_CHECK_CALL(napi_create_map(env, &map));;
1818 uint32_t size = 0;
1819 ASSERT_CHECK_CALL(napi_map_get_size(env, map, &size));
1820 ASSERT_EQ(size, 0);
1821 }
1822
1823 /**
1824 * @tc.name: MapGetSizeWithMultiContext002
1825 * @tc.desc: Test napi_map_get_size when context is sub context.
1826 * @tc.type: FUNC
1827 */
1828 HWTEST_F(NapiContextTest, MapGetSizeWithMultiContext002, testing::ext::TestSize.Level1)
1829 {
1830 ASSERT_NE(multiContextEngine_, nullptr);
1831 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1832
1833 napi_value map = nullptr;
1834 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1835 napi_value str = nullptr;
1836 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &str));
1837 ASSERT_CHECK_CALL(napi_map_set_property(env, map, str, str));
1838 uint32_t size = 0;
1839 ASSERT_CHECK_CALL(napi_map_get_size(env, map, &size));
1840 ASSERT_EQ(size, 1);
1841 }
1842
1843 /**
1844 * @tc.name: MapGetEntriesWithMultiContext001
1845 * @tc.desc: Test napi_map_get_entries when context is sub context.
1846 * @tc.type: FUNC
1847 */
1848 HWTEST_F(NapiContextTest, MapGetEntriesWithMultiContext001, testing::ext::TestSize.Level1)
1849 {
1850 ASSERT_NE(multiContextEngine_, nullptr);
1851 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1852
1853 napi_value map = nullptr;
1854 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1855
1856 napi_value zero = nullptr;
1857 ASSERT_CHECK_CALL(napi_create_int32(env, 0, &zero));
1858 napi_value one = nullptr;
1859 ASSERT_CHECK_CALL(napi_create_int32(env, 1, &one));
1860
1861 ASSERT_CHECK_CALL(napi_map_set_property(env, map, zero, one));
1862
1863 napi_value entries;
1864 ASSERT_CHECK_CALL(napi_map_get_entries(env, map, &entries));
1865
1866 napi_value entries0;
1867 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, entries, &entries0));
1868 napi_value entries0Value = nullptr;
1869 ASSERT_CHECK_CALL(napi_get_named_property(env, entries0, CONST_STRING_VALUE, &entries0Value));
1870 napi_value key = nullptr;
1871 ASSERT_CHECK_CALL(napi_get_element(env, entries0Value, 0, &key));
1872 int32_t nativeKey;
1873 ASSERT_CHECK_CALL(napi_get_value_int32(env, key, &nativeKey));
1874 ASSERT_EQ(nativeKey, 0);
1875 napi_value value = nullptr;
1876 ASSERT_CHECK_CALL(napi_get_element(env, entries0Value, 1, &value));
1877 int32_t nativeValue;
1878 ASSERT_CHECK_CALL(napi_get_value_int32(env, value, &nativeValue));
1879 ASSERT_EQ(nativeValue, 1);
1880
1881 napi_value end;
1882 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, entries, &end));
1883 napi_value done = nullptr;
1884 ASSERT_CHECK_CALL(napi_get_named_property(env, end, CONST_STRING_DONE, &done));
1885 bool isDone;
1886 ASSERT_CHECK_CALL(napi_get_value_bool(env, done, &isDone));
1887 ASSERT_TRUE(isDone);
1888 }
1889
1890 /**
1891 * @tc.name: MapGetKeysWithMultiContext001
1892 * @tc.desc: Test napi_map_get_keys when context is sub context.
1893 * @tc.type: FUNC
1894 */
1895 HWTEST_F(NapiContextTest, MapGetKeysWithMultiContext001, testing::ext::TestSize.Level1)
1896 {
1897 ASSERT_NE(multiContextEngine_, nullptr);
1898 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1899
1900 napi_value map = nullptr;
1901 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1902
1903 napi_value zero = nullptr;
1904 ASSERT_CHECK_CALL(napi_create_int32(env, 0, &zero));
1905 napi_value one = nullptr;
1906 ASSERT_CHECK_CALL(napi_create_int32(env, 1, &one));
1907
1908 ASSERT_CHECK_CALL(napi_map_set_property(env, map, zero, one));
1909
1910 napi_value keys;
1911 ASSERT_CHECK_CALL(napi_map_get_keys(env, map, &keys));
1912
1913 napi_value keys0;
1914 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, keys, &keys0));
1915 napi_value key = nullptr;
1916 ASSERT_CHECK_CALL(napi_get_named_property(env, keys0, CONST_STRING_VALUE, &key));
1917 int32_t nativeKey;
1918 ASSERT_CHECK_CALL(napi_get_value_int32(env, key, &nativeKey));
1919 ASSERT_EQ(nativeKey, 0);
1920
1921 napi_value end;
1922 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, keys, &end));
1923 napi_value done = nullptr;
1924 ASSERT_CHECK_CALL(napi_get_named_property(env, end, CONST_STRING_DONE, &done));
1925 bool isDone;
1926 ASSERT_CHECK_CALL(napi_get_value_bool(env, done, &isDone));
1927 ASSERT_TRUE(isDone);
1928 }
1929
1930 /**
1931 * @tc.name: MapGetValuesWithMultiContext001
1932 * @tc.desc: Test napi_map_get_values when context is sub context.
1933 * @tc.type: FUNC
1934 */
1935 HWTEST_F(NapiContextTest, MapGetValuesWithMultiContext001, testing::ext::TestSize.Level1)
1936 {
1937 ASSERT_NE(multiContextEngine_, nullptr);
1938 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
1939
1940 napi_value map = nullptr;
1941 ASSERT_CHECK_CALL(napi_create_map(env, &map));
1942
1943 napi_value zero = nullptr;
1944 ASSERT_CHECK_CALL(napi_create_int32(env, 0, &zero));
1945 napi_value one = nullptr;
1946 ASSERT_CHECK_CALL(napi_create_int32(env, 1, &one));
1947
1948 ASSERT_CHECK_CALL(napi_map_set_property(env, map, zero, one));
1949
1950 napi_value values;
1951 ASSERT_CHECK_CALL(napi_map_get_values(env, map, &values));
1952
1953 napi_value values0;
1954 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, values, &values0));
1955 napi_value value = nullptr;
1956 ASSERT_CHECK_CALL(napi_get_named_property(env, values0, CONST_STRING_VALUE, &value));
1957 int32_t nativeValue;
1958 ASSERT_CHECK_CALL(napi_get_value_int32(env, value, &nativeValue));
1959 ASSERT_EQ(nativeValue, 1);
1960
1961 napi_value end;
1962 ASSERT_CHECK_CALL(napi_map_iterator_get_next(env, values, &end));
1963 napi_value done = nullptr;
1964 ASSERT_CHECK_CALL(napi_get_named_property(env, end, CONST_STRING_DONE, &done));
1965 bool isDone;
1966 ASSERT_CHECK_CALL(napi_get_value_bool(env, done, &isDone));
1967 ASSERT_TRUE(isDone);
1968 }
1969
1970 /**
1971 * @tc.name: CreateLimitRuntimeWithMultiContext001
1972 * @tc.desc: Test napi_create_limit_runtime when context is sub context.
1973 * @tc.type: FUNC
1974 */
1975 HWTEST_F(NapiContextTest, CreateLimitRuntimeWithMultiContext001, testing::ext::TestSize.Level1)
1976 {
__anon9b81ebcd1a02() 1977 std::thread t([]() {
1978 static bool executed = false;
1979 BasicDeathTest(
1980 []() {
1981 NativeEngineProxy rootEngine;
1982 NativeEngineProxy contextEngine(*rootEngine);
1983 napi_env limitEnv = nullptr;
1984 napi_create_limit_runtime(contextEngine, &limitEnv);
1985 },
1986 [](std::string, std::string err) {
1987 executed = true;
1988 ASSERT_NE(err.find("(napi_create_limit_runtime)] multi-context does not support this interface"),
1989 std::string::npos);
1990 })
1991 .Run();
1992 ASSERT_TRUE(executed);
1993 });
1994 t.join();
1995 }
1996
1997 /**
1998 * @tc.name: RegisterModWithJsWithinMultiContext001
1999 * @tc.desc: Test napi_module_with_js_register when context is sub context.
2000 * @tc.type: FUNC
2001 */
2002 HWTEST_F(NapiContextTest, RegisterModWithJsWithinMultiContext001, testing::ext::TestSize.Level1)
2003 {
2004 napi_module_with_js mixedModule = {
2005 .nm_version = 0,
2006 .nm_flags = 0,
2007 .nm_filename = __func__,
__anon9b81ebcd1d02() 2008 .nm_register_func = [](napi_env, napi_value) -> napi_value { return nullptr; },
2009 .nm_modname = __func__,
2010 .nm_priv = nullptr,
2011 .nm_get_abc_code = nullptr,
2012 .nm_get_js_code = nullptr,
2013 };
2014 // Fixme: Add LoggerCollector to watch logs, make sure module is registered succeed.
2015 napi_module_with_js_register(&mixedModule);
2016 }
2017
2018 /**
2019 * @tc.name: IsCallableWithMultiContext001
2020 * @tc.desc: Test napi_is_callable when context is sub context.
2021 * @tc.type: FUNC
2022 */
2023 HWTEST_F(NapiContextTest, IsCallableWithMultiContext001, testing::ext::TestSize.Level1)
2024 {
2025 ASSERT_NE(multiContextEngine_, nullptr);
2026 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2027
2028 napi_value undefined = nullptr;
2029 ASSERT_CHECK_CALL(napi_get_undefined(env, &undefined));
2030 bool isCallable = false;
2031 ASSERT_CHECK_CALL(napi_is_callable(env, undefined, &isCallable));
2032 ASSERT_FALSE(isCallable);
2033 }
2034
2035 /**
2036 * @tc.name: IsCallableWithMultiContext002
2037 * @tc.desc: Test napi_is_callable when context is sub context.
2038 * @tc.type: FUNC
2039 */
2040 HWTEST_F(NapiContextTest, IsCallableWithMultiContext002, testing::ext::TestSize.Level1)
2041 {
2042 ASSERT_NE(multiContextEngine_, nullptr);
2043 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2044
2045 napi_value func = nullptr;
2046 ASSERT_CHECK_CALL(napi_create_function(env, __func__, NAPI_AUTO_LENGTH, EmptyNapiCallback, nullptr, &func));
2047 bool isCallable = false;
2048 ASSERT_CHECK_CALL(napi_is_callable(env, func, &isCallable));
2049 ASSERT_TRUE(isCallable);
2050 }
2051
2052 static bool g_finalizer = false;
TestDetachCallback(napi_env env,void * nativeObject,void * hint)2053 static void* TestDetachCallback(napi_env env, void* nativeObject, void* hint)
2054 {
2055 return nativeObject;
2056 }
2057
TestDetachFinalizer(void * detachedObject,void * finalizeHint)2058 static void TestDetachFinalizer(void* detachedObject, void* finalizeHint)
2059 {
2060 ASSERT_FALSE(g_finalizer);
2061 g_finalizer = true;
2062 }
2063
TestAttachCallback(napi_env env,void * nativeObject,void * hint)2064 static napi_value TestAttachCallback(napi_env env, void* nativeObject, void* hint)
2065 {
2066 napi_value object = nullptr;
2067 napi_value number = nullptr;
2068 uint32_t data = 0;
2069 if (hint != nullptr) {
2070 object = reinterpret_cast<napi_value>(nativeObject);
2071 data = CONST_U32_2K;
2072 napi_create_uint32(env, data, &number);
2073 } else {
2074 napi_create_object(env, &object);
2075 data = CONST_U32_1K;
2076 napi_create_uint32(env, data, &number);
2077 }
2078 napi_set_named_property(env, object, CONST_STRING_NUMBER, number);
2079 return object;
2080 }
2081 /**
2082 * @tc.name: SerializeInnerWithMultiContext001
2083 * @tc.desc: Test napi_serialize_inner when context is sub context.
2084 * @tc.type: FUNC
2085 */
2086 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext001, testing::ext::TestSize.Level1)
2087 {
2088 ASSERT_NE(multiContextEngine_, nullptr);
2089 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2090
2091 napi_value object = nullptr;
2092 napi_create_object(env, &object);
2093 napi_value object1 = nullptr;
2094 napi_create_object(env, &object1);
2095 napi_status status = napi_coerce_to_native_binding_object(
2096 env, object, TestDetachCallback, TestAttachCallback, reinterpret_cast<void*>(object1), nullptr);
2097 ASSERT_EQ(status, napi_status::napi_ok);
2098 status = napi_add_detached_finalizer(env, object, TestDetachFinalizer, nullptr);
2099 ASSERT_EQ(status, napi_status::napi_ok);
2100 napi_value undefined = nullptr;
2101 napi_get_undefined(env, &undefined);
2102 void* data = nullptr;
2103 napi_serialize_inner(env, object, undefined, undefined, false, true, &data);
2104 ASSERT_NE(data, nullptr);
2105 napi_value result = nullptr;
2106 napi_deserialize(env, data, &result);
2107 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
2108 g_finalizer = false;
2109 napi_delete_serialization_data(env, data);
2110 ASSERT_TRUE(g_finalizer);
2111 napi_value number = nullptr;
2112 napi_get_named_property(env, result, CONST_STRING_NUMBER, &number);
2113 ASSERT_CHECK_VALUE_TYPE(env, number, napi_number);
2114 uint32_t numData = 0;
2115 napi_get_value_uint32(env, number, &numData);
2116 ASSERT_EQ(numData, CONST_U32_1K);
2117 }
2118
2119 /**
2120 * @tc.name: SerializeInnerWithMultiContext002
2121 * @tc.desc: Test napi_serialize_inner when context is sub context.
2122 * @tc.type: FUNC
2123 */
2124 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext002, testing::ext::TestSize.Level1)
2125 {
2126 ASSERT_NE(multiContextEngine_, nullptr);
2127 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2128
2129 napi_value object = nullptr;
2130 napi_create_object(env, &object);
2131 napi_value hint = nullptr;
2132 napi_create_object(env, &hint);
2133 napi_value object1 = nullptr;
2134 napi_create_object(env, &object1);
2135 napi_status status = napi_coerce_to_native_binding_object(env, object,
2136 TestDetachCallback, TestAttachCallback, reinterpret_cast<void*>(object1), reinterpret_cast<void*>(hint));
2137 ASSERT_EQ(status, napi_status::napi_ok);
2138 status = napi_add_detached_finalizer(env, object, TestDetachFinalizer, nullptr);
2139 ASSERT_EQ(status, napi_status::napi_ok);
2140 napi_value undefined = nullptr;
2141 napi_get_undefined(env, &undefined);
2142 void* data = nullptr;
2143 napi_serialize_inner(env, object, undefined, undefined, false, true, &data);
2144 ASSERT_NE(data, nullptr);
2145 napi_value result = nullptr;
2146 napi_deserialize(env, data, &result);
2147 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
2148 g_finalizer = false;
2149 napi_delete_serialization_data(env, data);
2150 ASSERT_TRUE(g_finalizer);
2151 napi_value number = nullptr;
2152 napi_get_named_property(env, result, CONST_STRING_NUMBER, &number);
2153 ASSERT_CHECK_VALUE_TYPE(env, number, napi_number);
2154 uint32_t numData = 0;
2155 napi_get_value_uint32(env, number, &numData);
2156 ASSERT_EQ(numData, CONST_U32_2K);
2157 }
2158
2159 /**
2160 * @tc.name: SerializeInnerWithMultiContext003
2161 * @tc.desc: Test napi_serialize_inner when context is sub context.
2162 * @tc.type: FUNC
2163 */
2164 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext003, testing::ext::TestSize.Level1)
2165 {
2166 ASSERT_NE(multiContextEngine_, nullptr);
2167 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2168
2169 napi_value undefined = nullptr;
2170 napi_get_undefined(env, &undefined);
2171
2172 napi_value num = nullptr;
2173 napi_create_uint32(env, CONST_U32_1K, &num);
2174 void* data = nullptr;
2175 napi_serialize_inner(env, num, undefined, undefined, false, true, &data);
2176 ASSERT_NE(data, nullptr);
2177
2178 napi_value result = nullptr;
2179 napi_deserialize(env, data, &result);
2180 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
2181 napi_delete_serialization_data(env, data);
2182 int32_t resultData = 0;
2183 napi_get_value_int32(env, result, &resultData);
2184 ASSERT_EQ(resultData, CONST_U32_1K);
2185 }
2186
2187 /**
2188 * @tc.name: SerializeInnerWithMultiContext004
2189 * @tc.desc: Test napi_serialize_inner when context is sub context.
2190 * @tc.type: FUNC
2191 */
2192 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext004, testing::ext::TestSize.Level1)
2193 {
2194 ASSERT_NE(multiContextEngine_, nullptr);
2195 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2196
2197
2198 napi_value undefined = nullptr;
2199 napi_get_undefined(env, &undefined);
2200
2201 napi_value num = nullptr;
2202 napi_create_uint32(env, CONST_U32_1K, &num);
2203 void* data = nullptr;
2204 napi_serialize_inner(env, num, undefined, undefined, false, true, &data);
2205 ASSERT_NE(data, nullptr);
2206
2207 napi_value result1 = nullptr;
2208 napi_deserialize(env, data, &result1);
2209 ASSERT_CHECK_VALUE_TYPE(env, result1, napi_number);
2210 int32_t resultData1 = 0;
2211 napi_get_value_int32(env, result1, &resultData1);
2212 ASSERT_EQ(resultData1, CONST_U32_1K);
2213
2214 napi_value result2 = nullptr;
2215 napi_deserialize(env, data, &result2);
2216 ASSERT_CHECK_VALUE_TYPE(env, result2, napi_number);
2217 int32_t resultData2 = 0;
2218 napi_get_value_int32(env, result2, &resultData2);
2219 ASSERT_EQ(resultData2, CONST_U32_1K);
2220
2221 napi_delete_serialization_data(env, data);
2222 }
2223
2224 /**
2225 * @tc.name: SerializeInnerWithMultiContext005
2226 * @tc.desc: Test napi_serialize_inner when context is sub context.
2227 * @tc.type: FUNC
2228 */
2229 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext005, testing::ext::TestSize.Level1)
2230 {
2231 ASSERT_NE(multiContextEngine_, nullptr);
2232 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2233
2234 napi_value object = nullptr;
2235 napi_create_object(env, &object);
2236 napi_value hint = nullptr;
2237 napi_create_object(env, &hint);
2238 napi_value object1 = nullptr;
2239 napi_create_object(env, &object1);
2240 napi_status status = napi_coerce_to_native_binding_object(env, object,
2241 TestDetachCallback, TestAttachCallback, reinterpret_cast<void*>(object1), reinterpret_cast<void*>(hint));
2242 ASSERT_EQ(status, napi_status::napi_ok);
2243 napi_value undefined = nullptr;
2244 napi_get_undefined(env, &undefined);
2245 void* data = nullptr;
2246 napi_serialize_inner(env, object, undefined, undefined, false, true, &data);
2247 ASSERT_NE(data, nullptr);
2248
2249 napi_value result1 = nullptr;
2250 napi_deserialize(env, data, &result1);
2251 ASSERT_CHECK_VALUE_TYPE(env, result1, napi_object);
2252 napi_value number1 = nullptr;
2253 napi_get_named_property(env, result1, CONST_STRING_NUMBER, &number1);
2254 ASSERT_CHECK_VALUE_TYPE(env, number1, napi_number);
2255 uint32_t numData1 = 0;
2256 napi_get_value_uint32(env, number1, &numData1);
2257 ASSERT_EQ(numData1, CONST_U32_2K);
2258
2259 napi_value result2 = nullptr;
2260 napi_deserialize(env, data, &result2);
2261 ASSERT_CHECK_VALUE_TYPE(env, result2, napi_object);
2262 napi_value number2 = nullptr;
2263 napi_get_named_property(env, result2, CONST_STRING_NUMBER, &number2);
2264 ASSERT_CHECK_VALUE_TYPE(env, number2, napi_number);
2265 uint32_t numData2 = 0;
2266 napi_get_value_uint32(env, number2, &numData2);
2267 ASSERT_EQ(numData2, CONST_U32_2K);
2268
2269 napi_delete_serialization_data(env, data);
2270 }
2271
2272 /**
2273 * @tc.name: SerializeInnerWithMultiContext006
2274 * @tc.desc: Test napi_serialize_inner when context is sub context.
2275 * @tc.type: FUNC
2276 */
2277 HWTEST_F(NapiContextTest, SerializeInnerWithMultiContext006, testing::ext::TestSize.Level1)
2278 {
2279 ASSERT_NE(multiContextEngine_, nullptr);
2280 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2281
2282 const char objKey[] = "objKey";
2283 const char numKey[] = "numKey";
2284
2285 napi_value object = nullptr;
2286 napi_create_object(env, &object);
2287 napi_value num = nullptr;
2288 napi_create_uint32(env, CONST_U32_1K, &num);
2289 napi_set_named_property(env, object, numKey, num);
2290 napi_value obj = nullptr;
2291 napi_create_object(env, &obj);
2292 napi_set_named_property(env, object, objKey, obj);
2293
2294 napi_value undefined = nullptr;
2295 napi_get_undefined(env, &undefined);
2296 void* data = nullptr;
2297 napi_serialize_inner(env, object, undefined, undefined, false, true, &data);
2298 ASSERT_NE(data, nullptr);
2299
2300 napi_value result1 = nullptr;
2301 napi_deserialize(env, data, &result1);
2302 ASSERT_CHECK_VALUE_TYPE(env, result1, napi_object);
2303 napi_value obj1 = nullptr;
2304 napi_get_named_property(env, result1, objKey, &obj1);
2305 ASSERT_CHECK_VALUE_TYPE(env, obj1, napi_object);
2306
2307 napi_value result2 = nullptr;
2308 napi_deserialize(env, data, &result2);
2309 ASSERT_CHECK_VALUE_TYPE(env, result2, napi_object);
2310 napi_value num1 = nullptr;
2311 napi_get_named_property(env, result2, numKey, &num1);
2312 uint32_t value1 = 0;
2313 napi_get_value_uint32(env, num1, &value1);
2314 ASSERT_EQ(value1, CONST_U32_1K);
2315
2316 napi_delete_serialization_data(env, data);
2317 }
2318
2319 /**
2320 * @tc.name: RunActorWithMultiContext001
2321 * @tc.desc: Test napi_run_actor when context is sub context.
2322 * @tc.type: FUNC
2323 */
2324 HWTEST_F(NapiContextTest, RunActorWithMultiContext001, testing::ext::TestSize.Level1)
2325 {
__anon9b81ebcd1e02() 2326 std::thread t([]() {
2327 static bool executed = false;
2328 BasicDeathTest(
2329 []() {
2330 NativeEngineProxy rootEngine;
2331 NativeEngineProxy contextEngine(*rootEngine);
2332 napi_value result = nullptr;
2333 char buf[16] { 0 };
2334 napi_run_actor(contextEngine, "", buf, &result);
2335 },
2336 [](std::string, std::string err) {
2337 executed = true;
2338 ASSERT_NE(err.find("(napi_run_actor)] multi-context does not support this interface"),
2339 std::string::npos);
2340 })
2341 .Run();
2342 ASSERT_TRUE(executed);
2343 });
2344 t.join();
2345 }
2346
2347 /**
2348 * @tc.name: WrapAsyncFinalizerWithinMultiContext001
2349 * @tc.desc: Test napi_wrap_with_size when context is sub context.
2350 * @tc.type: FUNC
2351 */
2352 HWTEST_F(NapiContextTest, WrapAsyncFinalizerWithinMultiContext001, testing::ext::TestSize.Level1)
2353 {
2354 ASSERT_NE(multiContextEngine_, nullptr);
2355 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2356
2357 napi_value object = nullptr;
2358 pid_t callbackTid = 0;
2359 const EcmaVM* vm = reinterpret_cast<ArkNativeEngine*>(engine_)->GetEcmaVm();
2360 {
2361 panda::LocalScope scope(vm);
2362 ASSERT_CHECK_CALL(napi_create_object(env, &object));
2363 ASSERT_CHECK_CALL(napi_wrap_async_finalizer(
2364 env, object, reinterpret_cast<void*>(&callbackTid),
__anon9b81ebcd2102(napi_env, void* data, void* hint) 2365 [](napi_env, void* data, void* hint) {
2366 std::cout << "finalize callback is executing at thread: " << gettid() << std::endl;
2367 *reinterpret_cast<pid_t*>(data) = gettid();
2368 STOP_EVENT_LOOP(reinterpret_cast<napi_env>(hint));
2369 }, engine_, nullptr, 0));
2370 }
2371 panda::JSNApi::TriggerGC(vm, panda::ecmascript::GCReason::OTHER, panda::JSNApi::TRIGGER_GC_TYPE::FULL_GC);
2372 RUN_EVENT_LOOP(reinterpret_cast<napi_env>(engine_));
2373 ASSERT_NE(callbackTid, 0);
2374 ASSERT_NE(callbackTid, gettid());
2375 }
2376
2377 /**
2378 * @tc.name: NapiHasOwnPropertyTest001
2379 * @tc.desc: Test napi_has_own_property when env is a context env.
2380 * @tc.type: FUNC
2381 */
2382 HWTEST_F(NapiContextTest, NapiHasOwnPropertyTest001, testing::ext::TestSize.Level1)
2383 {
2384 CheckContextEnv();
2385 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2386
2387 napi_value obj = nullptr;
2388 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
2389 napi_value key = nullptr;
2390 ASSERT_CHECK_CALL(napi_create_int32(env, 1, &key));
2391 napi_value value = nullptr;
2392 ASSERT_CHECK_CALL(napi_create_int32(env, 1, &value));
2393
2394 ASSERT_CHECK_CALL(napi_set_property(env, obj, key, value));
2395 bool result = false;
2396 ASSERT_CHECK_CALL(napi_has_own_property(env, obj, key, &result));
2397 ASSERT_TRUE(result);
2398 }
2399
2400 /**
2401 * @tc.name: NapiSetNamedPropertyTest001
2402 * @tc.desc: Test napi_set_named_property when env is a context env.
2403 * @tc.type: FUNC
2404 */
2405 HWTEST_F(NapiContextTest, NapiSetNamedPropertyTest001, testing::ext::TestSize.Level1)
2406 {
2407 CheckContextEnv();
2408 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2409 napi_value obj = nullptr;
2410 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
2411 napi_value key = nullptr;
2412 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_KEY, NAPI_AUTO_LENGTH, &key));
2413 napi_value value = nullptr;
2414 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_VALUE, NAPI_AUTO_LENGTH, &value));
2415 ASSERT_CHECK_CALL(napi_set_named_property(env, obj, TEST_KEY, value));
2416 bool result = false;
2417 ASSERT_CHECK_CALL(napi_has_own_property(env, obj, key, &result));
2418 ASSERT_TRUE(result);
2419 }
2420
2421 /**
2422 * @tc.name: NapiHasNamedPropertyTest001
2423 * @tc.desc: Test napi_has_named_property when env is a context env.
2424 * @tc.type: FUNC
2425 */
2426 HWTEST_F(NapiContextTest, NapiHasNamedPropertyTest001, testing::ext::TestSize.Level1)
2427 {
2428 CheckContextEnv();
2429 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2430 napi_value obj;
2431 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
2432 napi_value value = nullptr;
2433 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_VALUE, NAPI_AUTO_LENGTH, &value));
2434 ASSERT_CHECK_CALL(napi_set_named_property(env, obj, TEST_KEY, value));
2435 bool result = false;
2436 ASSERT_CHECK_CALL(napi_has_named_property(env, obj, TEST_KEY, &result));
2437 ASSERT_TRUE(result);
2438 }
2439
2440 /**
2441 * @tc.name: NapiGetNamedPropertyTest001
2442 * @tc.desc: Test napi_get_named_property when env is a context env.
2443 * @tc.type: FUNC
2444 */
2445 HWTEST_F(NapiContextTest, NapiGetNamedPropertyTest001, testing::ext::TestSize.Level1)
2446 {
2447 CheckContextEnv();
2448 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2449 napi_value obj;
2450 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
2451 napi_value value = nullptr;
2452 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_VALUE, NAPI_AUTO_LENGTH, &value));
2453 ASSERT_CHECK_CALL(napi_set_named_property(env, obj, TEST_KEY, value));
2454 napi_value result = nullptr;
2455 ASSERT_CHECK_CALL(napi_get_named_property(env, obj, TEST_KEY, &result));
2456
2457 char* buffer = nullptr;
2458 size_t bufferSize = 0;
2459 size_t strLength = 0;
2460 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize));
2461 ASSERT_GT(bufferSize, static_cast<size_t>(0));
__anon9b81ebcd2202null2462 buffer = new char[bufferSize + 1]{ 0 };
2463 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength));
2464 ASSERT_STREQ(TEST_VALUE, buffer);
2465 ASSERT_EQ(strlen(TEST_VALUE), strLength);
2466 delete []buffer;
2467 }
2468
2469 /**
2470 * @tc.name: NapiObjectElementTest001
2471 * @tc.desc: Test object element setting and getting capability when env is a context env.
2472 * @tc.type: FUNC
2473 */
2474 HWTEST_F(NapiContextTest, NapiObjectElementTest001, testing::ext::TestSize.Level1)
2475 {
2476 CheckContextEnv();
2477 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2478 napi_value array = nullptr;
2479 ASSERT_CHECK_CALL(napi_create_array(env, &array));
2480 ASSERT_NE(array, nullptr);
2481 bool isArray = false;
2482 ASSERT_CHECK_CALL(napi_is_array(env, array, &isArray));
2483 ASSERT_TRUE(isArray);
2484
2485 for (size_t i = 0; i < TEST_ARRAY_LENGTH; i++) {
2486 napi_value num = nullptr;
2487 ASSERT_CHECK_CALL(napi_create_uint32(env, i, &num));
2488 ASSERT_CHECK_CALL(napi_set_element(env, array, i, num));
2489 }
2490
2491 uint32_t arrayLength = 0;
2492 ASSERT_CHECK_CALL(napi_get_array_length(env, array, &arrayLength));
2493 ASSERT_EQ(arrayLength, static_cast<uint32_t>(TEST_ARRAY_LENGTH));
2494
2495 for (size_t i = 0; i < arrayLength; i++) {
2496 bool hasIndex = false;
2497 ASSERT_CHECK_CALL(napi_has_element(env, array, i, &hasIndex));
2498 ASSERT_TRUE(hasIndex);
2499 napi_value value = nullptr;
2500 ASSERT_CHECK_CALL(napi_get_element(env, array, i, &value));
2501 uint32_t number = 0;
2502 napi_get_value_uint32(env, value, &number);
2503 ASSERT_EQ(number, i);
2504 }
2505
2506 for (size_t i = 0; i < arrayLength; i++) {
2507 bool isDelete = false;
2508 napi_delete_element(env, array, i, &isDelete);
2509 ASSERT_TRUE(isDelete);
2510 }
2511 }
2512
2513 /**
2514 * @tc.name: NapiDefinePropertiesTest001
2515 * @tc.desc: Test napi_define_properties when env is a context env.
2516 * @tc.type: FUNC
2517 */
2518 HWTEST_F(NapiContextTest, NapiDefinePropertiesTest001, testing::ext::TestSize.Level1)
2519 {
2520 CheckContextEnv();
2521 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2522
2523 napi_value result = nullptr;
2524 // Create napi_values for 123, 456 and 789
2525 napi_value enumerable = nullptr;
2526 napi_value writAble = nullptr;
2527 napi_value configurAble = nullptr;
2528 napi_create_int32(env, 123, &enumerable);
2529 napi_create_int32(env, 456, &writAble);
2530 napi_create_int32(env, 789, &configurAble);
2531
2532 napi_property_descriptor descriptors[] = {
2533 {ENUMERABLE, nullptr, nullptr, nullptr, nullptr, enumerable, napi_default_method, nullptr},
2534 {WRITEABLE, nullptr, nullptr, nullptr, nullptr, writAble,
2535 static_cast<napi_property_attributes>(napi_enumerable | napi_writable), nullptr},
2536 {CONFIGURABLE, nullptr, nullptr, nullptr, nullptr, configurAble,
2537 static_cast<napi_property_attributes>(napi_enumerable | napi_configurable), nullptr}
2538 };
2539
2540 ASSERT_CHECK_CALL(napi_create_object(env, &result));
2541 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
2542 ASSERT_CHECK_CALL(napi_define_properties(env, result, sizeof(descriptors) / sizeof(descriptors[0]), descriptors));
2543
2544 bool ret = false;
2545 ASSERT_CHECK_CALL(napi_has_named_property(env, result, ENUMERABLE, &ret));
2546 ASSERT_TRUE(ret);
2547
2548 ret = false;
2549 ASSERT_CHECK_CALL(napi_has_named_property(env, result, WRITEABLE, &ret));
2550 ASSERT_TRUE(ret);
2551
2552 ret = false;
2553 ASSERT_CHECK_CALL(napi_has_named_property(env, result, CONFIGURABLE, &ret));
2554 ASSERT_TRUE(ret);
2555 }
2556
2557 /**
2558 * @tc.name: NapiIsArrayTest001
2559 * @tc.desc: Test array when env is a context env.
2560 * @tc.type: FUNC
2561 */
2562 HWTEST_F(NapiContextTest, NapiIsArrayTest001, testing::ext::TestSize.Level1)
2563 {
2564 CheckContextEnv();
2565 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2566 napi_value array = nullptr;
2567 ASSERT_CHECK_CALL(napi_create_array(env, &array));
2568 ASSERT_NE(array, nullptr);
2569 bool isArray = false;
2570 ASSERT_CHECK_CALL(napi_is_array(env, array, &isArray));
2571 ASSERT_TRUE(isArray);
2572 }
2573
2574 /**
2575 * @tc.name: NapiGetArrayLengthTest001
2576 * @tc.desc: Test napi_get_array_length when env is a context env.
2577 * @tc.type: FUNC
2578 */
2579 HWTEST_F(NapiContextTest, NapiGetArrayLengthTest001, testing::ext::TestSize.Level1)
2580 {
2581 CheckContextEnv();
2582 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2583 napi_value array = nullptr;
2584 ASSERT_CHECK_CALL(napi_create_array(env, &array));
2585 ASSERT_NE(array, nullptr);
2586
2587 uint32_t arrayLength = 0;
2588 ASSERT_CHECK_CALL(napi_get_array_length(env, array, &arrayLength));
2589 ASSERT_EQ(arrayLength, 0);
2590 }
2591
2592 /**
2593 * @tc.name: NapiStrictEqualsTest001
2594 * @tc.desc: Test napi_strict_equals when env is a context env.
2595 * @tc.type: FUNC
2596 */
2597 HWTEST_F(NapiContextTest, NapiStrictEqualsTest001, testing::ext::TestSize.Level1)
2598 {
2599 CheckContextEnv();
2600 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2601
2602 napi_value testString = nullptr;
2603 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR, NAPI_AUTO_LENGTH, &testString));
2604 bool isStrictEquals = false;
2605 ASSERT_CHECK_CALL(napi_strict_equals(env, testString, testString, &isStrictEquals));
2606 ASSERT_TRUE(isStrictEquals);
2607 }
2608
2609 /**
2610 * @tc.name: NapiCallFunctionTest001
2611 * @tc.desc: Test napi_call_function when env is a context env.
2612 * @tc.type: FUNC
2613 */
2614 HWTEST_F(NapiContextTest, NapiCallFunctionTest001, testing::ext::TestSize.Level1)
2615 {
2616 CheckContextEnv();
2617 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2618
__anon9b81ebcd2302(napi_env env, napi_callback_info info) 2619 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
2620 napi_value thisVar;
2621 napi_value* argv = nullptr;
2622 size_t argc = 0;
2623 void* innerData = nullptr;
2624
2625 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
2626 if (argc > 0) {
2627 argv = new napi_value[argc];
2628 }
2629 napi_get_cb_info(env, info, &argc, argv, &thisVar, &innerData);
2630 napi_value result = nullptr;
2631 if (argv) {
2632 result = argv[0];
2633 delete[] argv;
2634 }
2635 return result;
2636 };
2637
2638 napi_value fn = nullptr;
2639 napi_value funcResultValue = nullptr;
2640 napi_value recv = nullptr;
2641 napi_value jsNumber = nullptr;
2642 napi_status status = napi_create_function(env, nullptr, 0, func, (void*)TEST_STR, &fn);
2643 ASSERT_EQ(napi_ok, status);
2644
2645 const int32_t testNumber = 1;
2646 napi_create_int32(env, testNumber, &jsNumber);
2647 napi_value argv[] = { jsNumber };
2648 napi_get_undefined(env, &recv);
2649 status = napi_call_function(env, recv, fn, 1, argv, &funcResultValue);
2650 ASSERT_EQ(status, napi_ok);
2651
2652 int32_t cNumber;
2653 napi_get_value_int32(env, funcResultValue, &cNumber);
2654 ASSERT_EQ(cNumber, testNumber);
2655 }
2656
2657 /**
2658 * @tc.name: NapiNewInstanceTest001
2659 * @tc.desc: Test napi_new_instance when env is a context env.
2660 * @tc.type: FUNC
2661 */
2662 HWTEST_F(NapiContextTest, NapiNewInstanceTest001, testing::ext::TestSize.Level1)
2663 {
2664 CheckContextEnv();
2665 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2666
2667 napi_value testClass = nullptr;
2668 napi_define_class(env, TEST_CLASS, NAPI_AUTO_LENGTH,
__anon9b81ebcd2402(napi_env env, napi_callback_info info) 2669 [](napi_env env, napi_callback_info info) -> napi_value {
2670 napi_value thisVar = nullptr;
2671 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2672 return thisVar;
2673 }, nullptr, 0, nullptr, &testClass);
2674
2675 napi_value instanceValue = nullptr;
2676 ASSERT_CHECK_CALL(napi_new_instance(env, testClass, 0, nullptr, &instanceValue));
2677 }
2678
2679 /**
2680 * @tc.name: NapiGetNewTargetTest001
2681 * @tc.desc: Test napi_new_instance when env is a context env.
2682 * @tc.type: FUNC
2683 */
2684 HWTEST_F(NapiContextTest, NapiGetNewTargetTest001, testing::ext::TestSize.Level1)
2685 {
2686 CheckContextEnv();
2687 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2688
2689 napi_value testClass = nullptr;
2690 napi_define_class(env, TEST_CLASS, NAPI_AUTO_LENGTH,
__anon9b81ebcd2502(napi_env env, napi_callback_info info) 2691 [](napi_env env, napi_callback_info info) -> napi_value {
2692 napi_value thisVar = nullptr;
2693 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
2694 napi_value constructor = nullptr;
2695 napi_get_new_target(env, info, &constructor);
2696 if (constructor == nullptr) {
2697 napi_throw_error(env, nullptr, "is not new instance");
2698 }
2699 return thisVar;
2700 }, nullptr, 0, nullptr, &testClass);
2701
2702 napi_value instanceValue = nullptr;
2703 ASSERT_CHECK_CALL(napi_new_instance(env, testClass, 0, nullptr, &instanceValue));
2704 bool isInstanceOf = false;
2705 ASSERT_CHECK_CALL(napi_instanceof(env, instanceValue, testClass, &isInstanceOf));
2706 ASSERT_TRUE(isInstanceOf);
2707 }
2708
2709 /**
2710 * @tc.name: NapiWrapTest001
2711 * @tc.desc: Test napi_wrap when env is a context env.
2712 * @tc.type: FUNC
2713 */
2714 HWTEST_F(NapiContextTest, NapiWrapTest001, testing::ext::TestSize.Level1)
2715 {
2716 CheckContextEnv();
2717 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2718
2719 napi_value object = nullptr;
2720 ASSERT_CHECK_CALL(napi_create_object(env, &object));
__anon9b81ebcd2602(napi_env, void* data, void*) 2721 auto finalizer = [](napi_env, void* data, void*) {
2722 delete reinterpret_cast<uint8_t*>(data);
2723 };
2724 uint8_t* data0 = new uint8_t;
2725 ASSERT_CHECK_CALL(napi_wrap(env, object, data0, finalizer, nullptr, nullptr));
2726 }
2727
2728 /**
2729 * @tc.name: NapiUnwrapTest001
2730 * @tc.desc: Test napi_unwrap when env is a context env.
2731 * @tc.type: FUNC
2732 */
2733 HWTEST_F(NapiContextTest, NapiUnwrapTest001, testing::ext::TestSize.Level1)
2734 {
2735 CheckContextEnv();
2736 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2737
2738 napi_value object = nullptr;
2739 ASSERT_CHECK_CALL(napi_create_object(env, &object));
__anon9b81ebcd2702(napi_env, void* data, void*) 2740 auto finalizer = [](napi_env, void* data, void*) {
2741 delete reinterpret_cast<char*>(data);
2742 };
2743
2744 char *str = strdup(TEST_STR);
2745 ASSERT_CHECK_CALL(napi_wrap(env, object, (void*)str, finalizer, nullptr, nullptr));
2746 void* result = nullptr;
2747
2748 ASSERT_CHECK_CALL(napi_unwrap(env, object, &result));
2749 ASSERT_STREQ(reinterpret_cast<char*>(result), str);
2750 }
2751
2752 /**
2753 * @tc.name: NapiRemoveWrapTest001
2754 * @tc.desc: Test napi_remove_wrap when env is a context env.
2755 * @tc.type: FUNC
2756 */
2757 HWTEST_F(NapiContextTest, NapiRemoveWrapTest001, testing::ext::TestSize.Level1)
2758 {
2759 CheckContextEnv();
2760 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2761
2762 napi_value object = nullptr;
2763 ASSERT_CHECK_CALL(napi_create_object(env, &object));
__anon9b81ebcd2802(napi_env, void* data, void* hint) 2764 auto finalizer = [](napi_env, void* data, void* hint) {
2765 (void) data;
2766 };
2767 ASSERT_CHECK_CALL(napi_wrap(env, object, (void*)TEST_STR, finalizer, nullptr, nullptr));
2768 void* result = nullptr;
2769
2770 ASSERT_CHECK_CALL(napi_remove_wrap(env, object, &result));
2771 ASSERT_STREQ(reinterpret_cast<char*>(result), TEST_STR);
2772 }
2773
2774 /**
2775 * @tc.name: NapiCreateExternalTest001
2776 * @tc.desc: Test napi_create_external when env is a context env.
2777 * @tc.type: FUNC
2778 */
2779 HWTEST_F(NapiContextTest, NapiCreateExternalTest001, testing::ext::TestSize.Level1)
2780 {
2781 CheckContextEnv();
2782 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2783
2784 napi_value external = nullptr;
2785 ASSERT_CHECK_CALL(napi_create_external(env, (void*)TEST_STR,
__anon9b81ebcd2902(napi_env env, void* data, void* hint) 2786 [](napi_env env, void* data, void* hint) {
2787 ASSERT_STREQ((const char*)data, (const char*)hint);
2788 }, (void*)TEST_STR, &external));
2789
2790 ASSERT_CHECK_VALUE_TYPE(env, external, napi_external);
2791 void* tmpExternal = nullptr;
2792 ASSERT_CHECK_CALL(napi_get_value_external(env, external, &tmpExternal));
2793 ASSERT_TRUE(tmpExternal);
2794 ASSERT_EQ(tmpExternal, TEST_STR);
2795 }
2796
2797 /**
2798 * @tc.name: NapiCreateReferenceTest001
2799 * @tc.desc: Test napi_create_reference when env is a context env.
2800 * @tc.type: FUNC
2801 */
2802 HWTEST_F(NapiContextTest, NapiCreateReferenceTest001, testing::ext::TestSize.Level1)
2803 {
2804 CheckContextEnv();
2805 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2806
2807 napi_value result = nullptr;
2808 ASSERT_CHECK_CALL(napi_create_object(env, &result));
2809
2810 napi_ref resultRef = nullptr;
2811 ASSERT_CHECK_CALL(napi_create_reference(env, result, 1, &resultRef));
2812
2813 uint32_t resultRefCount = 0;
2814
2815 ASSERT_CHECK_CALL(napi_reference_ref(env, resultRef, &resultRefCount));
2816 ASSERT_EQ(resultRefCount, REFERENCE_SIZE);
2817
2818 ASSERT_CHECK_CALL(napi_reference_unref(env, resultRef, &resultRefCount));
2819 ASSERT_EQ(resultRefCount, 1);
2820
2821 napi_value refValue = nullptr;
2822 ASSERT_CHECK_CALL(napi_get_reference_value(env, resultRef, &refValue));
2823 ASSERT_NE(refValue, nullptr);
2824 bool isStrictEquals = false;
2825 ASSERT_CHECK_CALL(napi_strict_equals(env, result, refValue, &isStrictEquals));
2826 ASSERT_TRUE(isStrictEquals);
2827 ASSERT_CHECK_CALL(napi_delete_reference(env, resultRef));
2828 }
2829
2830 /**
2831 * @tc.name: NapiScopeTest001
2832 * @tc.desc: Test napi scope when env is a context env.
2833 * @tc.type: FUNC
2834 */
2835 HWTEST_F(NapiContextTest, NapiScopeTest001, testing::ext::TestSize.Level1)
2836 {
2837 CheckContextEnv();
2838 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2839
__anon9b81ebcd2a02(napi_env env, napi_callback_info info) 2840 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
2841 napi_value thisVar;
2842 napi_value* argv = nullptr;
2843 size_t argc = 0;
2844 void* data = nullptr;
2845 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
2846 if (argc > 0) {
2847 argv = new napi_value[argc];
2848 }
2849 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
2850 napi_value result = nullptr;
2851 napi_create_object(env, &result);
2852 napi_value messageKey = nullptr;
2853 napi_create_string_latin1(env, MESSAGE, NAPI_AUTO_LENGTH, &messageKey);
2854 napi_value messageValue = nullptr;
2855 napi_create_string_latin1(env, MESSAGE, NAPI_AUTO_LENGTH, &messageValue);
2856 napi_set_property(env, result, messageKey, messageValue);
2857 if (argv != nullptr) {
2858 delete []argv;
2859 }
2860 return result;
2861 };
2862
2863 napi_value recv = nullptr;
2864 napi_value funcValue = nullptr;
2865 napi_get_undefined(env, &recv);
2866 ASSERT_NE(recv, nullptr);
2867
2868 ASSERT_CHECK_CALL(napi_create_function(env, TEST_FUNC, NAPI_AUTO_LENGTH, func, nullptr, &funcValue));
2869 ASSERT_NE(funcValue, nullptr);
2870
2871 napi_handle_scope parentScope = nullptr;
2872 ASSERT_CHECK_CALL(napi_open_handle_scope(env, &parentScope));
2873 ASSERT_NE(parentScope, nullptr);
2874
2875 napi_escapable_handle_scope childScope = nullptr;
2876 ASSERT_CHECK_CALL(napi_open_escapable_handle_scope(env, &childScope));
2877 ASSERT_NE(childScope, nullptr);
2878
2879 napi_value funcResultValue = nullptr;
2880 napi_value newFuncResultValue = nullptr;
2881 ASSERT_CHECK_CALL(napi_call_function(env, recv, funcValue, 0, nullptr, &funcResultValue));
2882 ASSERT_NE(funcResultValue, nullptr);
2883
2884 ASSERT_CHECK_CALL(napi_escape_handle(env, childScope, funcResultValue, &newFuncResultValue));
2885 ASSERT_CHECK_CALL(napi_close_escapable_handle_scope(env, childScope));
2886 ASSERT_TRUE(newFuncResultValue != nullptr);
2887 ASSERT_CHECK_VALUE_TYPE(env, newFuncResultValue, napi_object);
2888 ASSERT_CHECK_CALL(napi_close_handle_scope(env, parentScope));
2889 }
2890
2891 /**
2892 * @tc.name: NapiThrowTest001
2893 * @tc.desc: Test napi_throw when env is a context env.
2894 * @tc.type: FUNC
2895 */
2896 HWTEST_F(NapiContextTest, NapiThrowTest001, testing::ext::TestSize.Level1)
2897 {
2898 CheckContextEnv();
2899 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2900
2901 napi_value code = nullptr;
2902 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_ERROR_CODE, NAPI_AUTO_LENGTH, &code));
2903
2904 napi_value message = nullptr;
2905 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_ERROR_MESSAGE, NAPI_AUTO_LENGTH, &message));
2906
2907 napi_value error = nullptr;
2908 ASSERT_CHECK_CALL(napi_create_error(env, code, message, &error));
2909 ASSERT_TRUE(error != nullptr);
2910
2911 bool isError = false;
2912 ASSERT_CHECK_CALL(napi_is_error(env, error, &isError));
2913 ASSERT_TRUE(isError);
2914 ASSERT_CHECK_CALL(napi_throw(env, error));
2915
2916 bool isExceptionPending = false;
2917 ASSERT_CHECK_CALL(napi_is_exception_pending(env, &isExceptionPending));
2918 ASSERT_TRUE(isExceptionPending);
2919
2920 napi_value exception = nullptr;
2921 ASSERT_CHECK_CALL(napi_get_and_clear_last_exception(env, &exception));
2922 ASSERT_CHECK_CALL(napi_is_exception_pending(env, &isExceptionPending));
2923 ASSERT_FALSE(isExceptionPending);
2924 }
2925
2926 /**
2927 * @tc.name: NapiThrowErrorTest001
2928 * @tc.desc: Test napi_throw_error when env is a context env.
2929 * @tc.type: FUNC
2930 */
2931 HWTEST_F(NapiContextTest, NapiThrowErrorTest001, testing::ext::TestSize.Level1)
2932 {
2933 CheckContextEnv();
2934 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2935
2936 ASSERT_CHECK_CALL(napi_throw_error(env, TEST_ERROR_CODE, TEST_ERROR_MESSAGE));
2937
2938 bool isExceptionPending = false;
2939 ASSERT_CHECK_CALL(napi_is_exception_pending(env, &isExceptionPending));
2940 ASSERT_TRUE(isExceptionPending);
2941 }
2942
2943 /**
2944 * @tc.name: NapiThrowTypeErrorTest001
2945 * @tc.desc: Test napi_throw_type_error when env is a context env.
2946 * @tc.type: FUNC
2947 */
2948 HWTEST_F(NapiContextTest, NapiThrowTypeErrorTest001, testing::ext::TestSize.Level1)
2949 {
2950 CheckContextEnv();
2951 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2952
2953 ASSERT_CHECK_CALL(napi_throw_type_error(env, TEST_ERROR_CODE, TEST_ERROR_MESSAGE));
2954
2955 bool isExceptionPending = false;
2956 ASSERT_CHECK_CALL(napi_is_exception_pending(env, &isExceptionPending));
2957 ASSERT_TRUE(isExceptionPending);
2958 }
2959
2960 /**
2961 * @tc.name: NapiThrowRangeErrorTest001
2962 * @tc.desc: Test napi_throw_range_error when env is a context env.
2963 * @tc.type: FUNC
2964 */
2965 HWTEST_F(NapiContextTest, NapiThrowRangeErrorTest001, testing::ext::TestSize.Level1)
2966 {
2967 CheckContextEnv();
2968 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2969
2970 ASSERT_CHECK_CALL(napi_throw_range_error(env, TEST_ERROR_CODE, TEST_ERROR_MESSAGE));
2971
2972 bool isExceptionPending = false;
2973 ASSERT_CHECK_CALL(napi_is_exception_pending(env, &isExceptionPending));
2974 ASSERT_TRUE(isExceptionPending);
2975 }
2976
2977 /**
2978 * @tc.name: NapiThrowRangeErrorTest001
2979 * @tc.desc: Test napi_throw_range_error when env is a context env.
2980 * @tc.type: FUNC
2981 */
2982 HWTEST_F(NapiContextTest, NapiArrayBufferTest001, testing::ext::TestSize.Level1)
2983 {
2984 CheckContextEnv();
2985 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
2986
2987 napi_value arrayBuffer = nullptr;
2988 void* arrayBufferPtr = nullptr;
2989 ASSERT_CHECK_CALL(napi_create_arraybuffer(env, LENGTH, &arrayBufferPtr, &arrayBuffer));
2990 ASSERT_NE(arrayBuffer, nullptr);
2991 ASSERT_NE(arrayBufferPtr, nullptr);
2992 bool isArrayBuffer = false;
2993 ASSERT_CHECK_CALL(napi_is_arraybuffer(env, arrayBuffer, &isArrayBuffer));
2994 ASSERT_TRUE(isArrayBuffer);
2995
2996 void* tmpArrayBufferPtr = nullptr;
2997 size_t arrayBufferLength = 0;
2998 ASSERT_CHECK_CALL(napi_get_arraybuffer_info(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength));
2999
3000 ASSERT_EQ(arrayBufferPtr, tmpArrayBufferPtr);
3001 ASSERT_EQ(LENGTH, arrayBufferLength);
3002 }
3003
3004 /**
3005 * @tc.name: NapiCreateExternalArrayBufferTest001
3006 * @tc.desc: Test napi_create_external_arraybuffer when env is a context env.
3007 * @tc.type: FUNC
3008 */
3009 HWTEST_F(NapiContextTest, NapiCreateExternalArrayBufferTest001, testing::ext::TestSize.Level1)
3010 {
3011 CheckContextEnv();
3012 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3013
3014 napi_value external = nullptr;
3015 auto res = napi_create_external_arraybuffer(
3016 env, (void*)TEST_STR, strlen(TEST_STR),
__anon9b81ebcd2b02(napi_env env, void* data, void* hint) 3017 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
3018 (void*)TEST_STR, &external);
3019 ASSERT_EQ(res, napi_ok);
3020
3021 void* tmpArrayBufferPtr = nullptr;
3022 size_t arrayBufferLength = 0;
3023 ASSERT_CHECK_CALL(napi_get_arraybuffer_info(env, external, &tmpArrayBufferPtr, &arrayBufferLength));
3024 ASSERT_EQ(TEST_STR, reinterpret_cast<char*>(tmpArrayBufferPtr));
3025 ASSERT_EQ(arrayBufferLength, strlen(TEST_STR));
3026 }
3027
3028 /**
3029 * @tc.name: NapiTypedArrayTest001
3030 * @tc.desc: Test napi_create_typedarray when env is a context env.
3031 * @tc.type: FUNC
3032 */
3033 HWTEST_F(NapiContextTest, NapiTypedArrayTest001, testing::ext::TestSize.Level1)
3034 {
3035 CheckContextEnv();
3036 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3037
3038 // create arraybuffer
3039 napi_value arrayBuffer = nullptr;
3040 void* arrayBufferPtr = nullptr;
3041 ASSERT_CHECK_CALL(napi_create_arraybuffer(env, LENGTH, &arrayBufferPtr, &arrayBuffer));
3042 ASSERT_NE(arrayBuffer, nullptr);
3043 ASSERT_NE(arrayBufferPtr, nullptr);
3044
3045 // using arraybuffer to generate typedArray
3046 napi_value typedArray = nullptr;
3047 size_t byteOffsetCreate = 0;
3048 ASSERT_CHECK_CALL(napi_create_typedarray(env, napi_int8_array, LENGTH, arrayBuffer, byteOffsetCreate,
3049 &typedArray));
3050 ASSERT_NE(typedArray, nullptr);
3051
3052 bool isTypedArray = false;
3053 ASSERT_CHECK_CALL(napi_is_typedarray(env, typedArray, &isTypedArray));
3054 ASSERT_TRUE(isTypedArray);
3055
3056 napi_typedarray_type typedarrayType;
3057 size_t typedarrayLength = 0;
3058 void* typedarrayBufferPtr = nullptr;
3059 napi_value tmpArrayBuffer = nullptr;
3060 size_t byteOffset = 0;
3061
3062 ASSERT_CHECK_CALL(napi_get_typedarray_info(env, typedArray, &typedarrayType, &typedarrayLength,
3063 &typedarrayBufferPtr, &tmpArrayBuffer, &byteOffset));
3064
3065 ASSERT_EQ(typedarrayType, napi_int8_array);
3066 ASSERT_EQ(typedarrayBufferPtr, arrayBufferPtr);
3067 ASSERT_EQ(LENGTH, typedarrayLength);
3068 ASSERT_EQ(byteOffset, byteOffsetCreate);
3069 }
3070
3071 /**
3072 * @tc.name: NapiDataViewTest001
3073 * @tc.desc: Test napi_create_dataview when env is a context env.
3074 * @tc.type: FUNC
3075 */
3076 HWTEST_F(NapiContextTest, NapiDataViewTest001, testing::ext::TestSize.Level1)
3077 {
3078 CheckContextEnv();
3079 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3080
3081 napi_value arrayBuffer = nullptr;
3082 void* arrayBufferPtr = nullptr;
3083 ASSERT_CHECK_CALL(napi_create_arraybuffer(env, LENGTH, &arrayBufferPtr, &arrayBuffer));
3084 ASSERT_NE(arrayBuffer, nullptr);
3085 ASSERT_NE(arrayBufferPtr, nullptr);
3086 bool isArrayBuffer = false;
3087 napi_is_arraybuffer(env, arrayBuffer, &isArrayBuffer);
3088 ASSERT_TRUE(isArrayBuffer);
3089
3090 napi_value result = nullptr;
3091 ASSERT_CHECK_CALL(napi_create_dataview(env, LENGTH, arrayBuffer, 0, &result));
3092
3093 bool isDataView = false;
3094 ASSERT_CHECK_CALL(napi_is_dataview(env, result, &isDataView));
3095
3096 napi_value retArrayBuffer = nullptr;
3097 void* data = nullptr;
3098 size_t byteLength = 0;
3099 size_t byteOffset = 0;
3100 ASSERT_CHECK_CALL(napi_get_dataview_info(env, result, &byteLength, &data, &retArrayBuffer, &byteOffset));
3101
3102 bool retIsArrayBuffer = false;
3103 ASSERT_CHECK_CALL(napi_is_arraybuffer(env, retArrayBuffer, &retIsArrayBuffer));
3104 ASSERT_TRUE(retIsArrayBuffer);
3105 ASSERT_EQ(arrayBufferPtr, data);
3106 ASSERT_EQ(LENGTH, byteLength);
3107 ASSERT_EQ(static_cast<size_t>(0), byteOffset);
3108 }
3109
3110 /**
3111 * @tc.name: NapiGetVersionTest001
3112 * @tc.desc: Test napi_get_version when env is a context env.
3113 * @tc.type: FUNC
3114 */
3115 HWTEST_F(NapiContextTest, NapiGetVersionTest001, testing::ext::TestSize.Level1)
3116 {
3117 CheckContextEnv();
3118 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3119
3120 uint32_t result = 0;
3121 napi_status status = napi_get_version(env, &result);
3122 ASSERT_EQ(status, napi_ok);
3123 }
3124
3125 HWTEST_F(NapiContextTest, NapiPromiseTest001, testing::ext::TestSize.Level1)
3126 {
3127 CheckContextEnv();
3128 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3129
3130 {
3131 napi_deferred deferred = nullptr;
3132 napi_value promise = nullptr;
3133 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
3134 ASSERT_NE(deferred, nullptr);
3135 ASSERT_NE(promise, nullptr);
3136
3137 bool isPromise = false;
3138 ASSERT_CHECK_CALL(napi_is_promise(env, promise, &isPromise));
3139 ASSERT_TRUE(isPromise);
3140
3141 napi_value undefined = nullptr;
3142 napi_get_undefined(env, &undefined);
3143 ASSERT_CHECK_CALL(napi_resolve_deferred(env, deferred, undefined));
3144 }
3145 {
3146 napi_deferred deferred = nullptr;
3147 napi_value promise = nullptr;
3148 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
3149 ASSERT_NE(deferred, nullptr);
3150 ASSERT_NE(promise, nullptr);
3151
3152 bool isPromise = false;
3153 ASSERT_CHECK_CALL(napi_is_promise(env, promise, &isPromise));
3154 ASSERT_TRUE(isPromise);
3155
3156 napi_value undefined = nullptr;
3157 napi_get_undefined(env, &undefined);
3158 ASSERT_CHECK_CALL(napi_reject_deferred(env, deferred, undefined));
3159 }
3160 }
3161
3162 /**
3163 * @tc.name: NapiRunScriptTest001
3164 * @tc.desc: Test napi_run_script when env is a context env.
3165 * @tc.type: FUNC
3166 */
3167 HWTEST_F(NapiContextTest, NapiRunScriptTest001, testing::ext::TestSize.Level1)
3168 {
3169 CheckContextEnv();
3170 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3171
3172 napi_value script = nullptr;
3173 napi_value res = nullptr;
3174 napi_status status = napi_run_script(env, script, &res);
3175 ASSERT_EQ(status, napi_invalid_arg);
3176 }
3177
3178 /**
3179 * @tc.name: NapiDateTest001
3180 * @tc.desc: Test napi_create_date when env is a context env.
3181 * @tc.type: FUNC
3182 */
3183 HWTEST_F(NapiContextTest, NapiDateTest001, testing::ext::TestSize.Level1)
3184 {
3185 CheckContextEnv();
3186 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3187
3188 const double time = 202110181203150;
3189 napi_value date;
3190 napi_status status = napi_create_date(env, time, &date);
3191 ASSERT_EQ(status, napi_ok);
3192
3193 bool isDate = false;
3194 napi_is_date(env, date, &isDate);
3195 ASSERT_TRUE(isDate);
3196
3197 double result;
3198 status = napi_get_date_value(env, date, &result);
3199 ASSERT_EQ(status, napi_ok);
3200 ASSERT_EQ(time, result);
3201 }
3202
3203 /**
3204 * @tc.name: NapiAddFinalizerTest001
3205 * @tc.desc: Test napi_add_finalizer when env is a context env.
3206 * @tc.type: FUNC
3207 */
3208 HWTEST_F(NapiContextTest, NapiAddFinalizerTest001, testing::ext::TestSize.Level1)
3209 {
3210 CheckContextEnv();
3211 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3212
3213 napi_value object;
3214 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &object));
3215
3216 static bool testValue = false;
3217 napi_ref ref = nullptr;
3218 napi_add_finalizer(
__anon9b81ebcd2c02(napi_env env, void* data, void* hint) 3219 env, object, (void*)TEST_STR, [](napi_env env, void* data, void* hint) {
3220 testValue = true;
3221 }, nullptr, &ref);
3222
3223 napi_delete_reference(env, ref);
3224 ASSERT_TRUE(testValue);
3225 }
3226
3227 /**
3228 * @tc.name: NapiBigIntTest001
3229 * @tc.desc: Test napi bigint when env is a context env.
3230 * @tc.type: FUNC
3231 */
3232 HWTEST_F(NapiContextTest, NapiBigIntTest001, testing::ext::TestSize.Level1)
3233 {
3234 CheckContextEnv();
3235 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3236 // int64
3237 {
3238 int64_t testValue = INT64_MAX;
3239 napi_value result = nullptr;
3240 ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &result));
3241 ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
3242
3243 int64_t resultValue = 0;
3244 bool flag = false;
3245 ASSERT_CHECK_CALL(napi_get_value_bigint_int64(env, result, &resultValue, &flag));
3246 ASSERT_EQ(resultValue, INT64_MAX);
3247 ASSERT_TRUE(flag);
3248 }
3249
3250 // uint64
3251 {
3252 uint64_t testValue = UINT64_MAX;
3253 napi_value result = nullptr;
3254 ASSERT_CHECK_CALL(napi_create_bigint_uint64(env, testValue, &result));
3255 ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
3256
3257 bool flag = false;
3258 uint64_t resultValue = 0;
3259 ASSERT_CHECK_CALL(napi_get_value_bigint_uint64(env, result, &resultValue, &flag));
3260 ASSERT_EQ(resultValue, UINT64_MAX);
3261 ASSERT_TRUE(flag);
3262 }
3263
3264 // word
3265 {
3266 int signBit = 0;
3267 size_t wordCount = 4;
3268 uint64_t words[] = { 0xFFFFFFFFFFFFFFFF, 34ULL, 56ULL, 0xFFFFFFFFFFFFFFFF };
3269 napi_value result = nullptr;
3270 ASSERT_CHECK_CALL(napi_create_bigint_words(env, signBit, wordCount, words, &result));
3271
3272 uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL };
3273 ASSERT_CHECK_CALL(napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut));
3274
3275 ASSERT_EQ(signBit, 0);
3276 ASSERT_EQ(wordCount, 4);
3277 for (auto index = 0; index < wordCount; ++index)
3278 {
3279 ASSERT_EQ(words[index], wordsOut[index]);
3280 }
3281 }
3282 }
3283
3284 /**
3285 * @tc.name: NapiGetAllPropertyNamesTest001
3286 * @tc.desc: Test napi_get_all_property_names when env is a context env.
3287 * @tc.type: FUNC
3288 */
3289 HWTEST_F(NapiContextTest, NapiGetAllPropertyNamesTest001, testing::ext::TestSize.Level1)
3290 {
3291 CheckContextEnv();
3292 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3293
3294 napi_key_collection_mode keyMode = napi_key_own_only;
3295 napi_key_filter keyFilter = napi_key_all_properties;
3296 napi_key_conversion keyConversion = napi_key_keep_numbers;
3297 napi_value result = nullptr;
3298 napi_value propNames = nullptr;
3299
3300 ASSERT_CHECK_CALL(napi_create_object(env, &result));
3301 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
3302
3303 napi_value strAttribute = nullptr;
3304 napi_create_string_utf8(env, TEST_STR, NAPI_AUTO_LENGTH, &strAttribute);
3305 napi_set_named_property(env, result, STR_ATTRIBUTE, strAttribute);
3306
3307 int32_t testNumber = 1;
3308 napi_value numberAttribute = nullptr;
3309 napi_create_int32(env, testNumber, &numberAttribute);
3310 napi_set_named_property(env, result, STR_ATTRIBUTE, numberAttribute);
3311
3312 ASSERT_CHECK_CALL(napi_get_all_property_names(env, result, keyMode, keyFilter, keyConversion, &propNames));
3313
3314 ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
3315 bool isArray = false;
3316 ASSERT_CHECK_CALL(napi_is_array(env, propNames, &isArray));
3317 ASSERT_TRUE(isArray);
3318 }
3319
3320 typedef struct {
3321 size_t value;
3322 bool print;
3323 napi_ref jsCallback;
3324 } AddonData;
3325
DeleteAddonData(napi_env env,void * rawData,void * hint)3326 static void DeleteAddonData(napi_env env, void* rawData, void* hint)
3327 {
3328 AddonData* data = (AddonData*)rawData;
3329 if (data->print) {
3330 printf("deleting addon data\n");
3331 }
3332 if (data->jsCallback != NULL) {
3333 NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, data->jsCallback));
3334 }
3335 delete data;
3336 }
3337
SetPrintOnDelete(napi_env env,napi_callback_info info)3338 static napi_value SetPrintOnDelete(napi_env env, napi_callback_info info)
3339 {
3340 AddonData* data;
3341 NAPI_CALL(env, napi_get_instance_data(env, (void**)&data));
3342 data->print = true;
3343 return NULL;
3344 }
3345
TestFinalizer(napi_env env,void * rawData,void * hint)3346 static void TestFinalizer(napi_env env, void* rawData, void* hint)
3347 {
3348 (void)rawData;
3349 (void)hint;
3350
3351 AddonData* data;
3352 napi_value jsResult;
3353 NAPI_CALL_RETURN_VOID(env, napi_get_instance_data(env, (void**)&data));
3354 napi_value js_cb = nullptr;
3355 napi_value undefined = nullptr;
3356 NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, data->jsCallback, &js_cb));
3357 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined));
3358 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, js_cb, 0, NULL, &jsResult));
3359
3360 NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, data->jsCallback));
3361 data->jsCallback = NULL;
3362 }
3363
ObjectWithFinalizer(napi_env env,napi_callback_info info)3364 static napi_value ObjectWithFinalizer(napi_env env, napi_callback_info info)
3365 {
3366 AddonData* data;
3367
3368 napi_value value;
3369 napi_value jsCb;
3370 size_t argc = 1;
3371
3372 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
3373 return nullptr;
3374 };
3375
3376 napi_create_function(env, TEST_FUNC, NAPI_AUTO_LENGTH, func, nullptr, &jsCb);
3377
3378 NAPI_CALL(env, napi_get_instance_data(env, (void**)&data));
3379 NAPI_ASSERT(env, data->jsCallback == nullptr, "reference must be nullptr");
3380 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &jsCb, nullptr, nullptr));
3381 NAPI_CALL(env, napi_create_object(env, &value));
3382 NAPI_CALL(env, napi_add_finalizer(env, value, nullptr, TestFinalizer, nullptr, nullptr));
3383 NAPI_CALL(env, napi_create_reference(env, jsCb, 1, &data->jsCallback));
3384 return nullptr;
3385 }
3386
3387 /**
3388 * @tc.name: NapiInstanceDataTest001
3389 * @tc.desc: Test instance data when env is a context env.
3390 * @tc.type: FUNC
3391 */
3392 HWTEST_F(NapiContextTest, NapiInstanceDataTest001, testing::ext::TestSize.Level1)
3393 {
3394 CheckContextEnv();
3395 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3396
3397 // Set instance data
3398 AddonData* data = new AddonData();
3399 data->value = 41;
3400 data->print = false;
3401 data->jsCallback = NULL;
3402 ASSERT_CHECK_CALL(napi_set_instance_data(env, data, DeleteAddonData, NULL));
3403
3404 // Test get instance data
3405 AddonData* addonData = nullptr;
3406 ASSERT_CHECK_CALL(napi_get_instance_data(env, (void**)&addonData));
3407 ++addonData->value;
3408 const size_t expectValue = 42;
3409 ASSERT_EQ(addonData->value, expectValue);
3410
3411 // Test finalizer
3412 SetPrintOnDelete(env, nullptr);
3413 ObjectWithFinalizer(env, nullptr);
3414 }
3415
3416 /**
3417 * @tc.name: NapiDetachArrayBufferTest001
3418 * @tc.desc: Test napi_is_detached_arraybuffer when env is a context env.
3419 * @tc.type: FUNC
3420 */
3421 HWTEST_F(NapiContextTest, NapiDetachArrayBufferTest001, testing::ext::TestSize.Level1)
3422 {
3423 CheckContextEnv();
3424 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3425
3426 static constexpr size_t arrayBufferSize = 1024;
3427 napi_value arrayBuffer = nullptr;
3428 void* arrayBufferPtr = nullptr;
3429 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
3430
3431 bool result = false;
3432 ASSERT_CHECK_CALL(napi_is_detached_arraybuffer(env, arrayBuffer, &result));
3433 ASSERT_FALSE(result);
3434
3435 napi_status status = napi_detach_arraybuffer(env, arrayBuffer);
3436 if (status == napi_ok) {
3437 arrayBufferPtr = nullptr;
3438 }
3439 ASSERT_EQ(status, napi_ok);
3440
3441 result = false;
3442 ASSERT_CHECK_CALL(napi_is_detached_arraybuffer(env, arrayBuffer, &result));
3443 ASSERT_TRUE(result);
3444 }
3445
3446 /**
3447 * @tc.name: NapiTypeTagObjectTest001
3448 * @tc.desc: Test napi_check_object_type_tag when env is a context env.
3449 * @tc.type: FUNC
3450 */
3451 HWTEST_F(NapiContextTest, NapiTypeTagObjectTest001, testing::ext::TestSize.Level1)
3452 {
3453 CheckContextEnv();
3454 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3455
3456 napi_value instance = nullptr;
3457 bool result;
3458 for (size_t i = 0; i < TYPE_TAGS_SIZE; i++) {
3459 napi_create_object(env, &instance);
3460 napi_type_tag_object(env, instance, &typeTags[i]);
3461 napi_check_object_type_tag(env, instance, &typeTags[i], &result);
3462 ASSERT_TRUE(result);
3463 }
3464 }
3465
3466 /**
3467 * @tc.name: NapiObjectFreezeTest001
3468 * @tc.desc: Test napi_object_freeze when env is a context env.
3469 * @tc.type: FUNC
3470 */
3471 HWTEST_F(NapiContextTest, NapiObjectFreezeTest001, testing::ext::TestSize.Level1)
3472 {
3473 CheckContextEnv();
3474 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3475
3476 napi_value object = nullptr;
3477 ASSERT_CHECK_CALL(napi_create_object(env, &object));
3478 int32_t testNumber = 0;
3479 napi_value numberAttribute = nullptr;
3480 ASSERT_CHECK_CALL(napi_create_int32(env, testNumber, &numberAttribute));
3481
3482 ASSERT_CHECK_CALL(napi_object_freeze(env, object));
3483 // Set property after freezed will throw 'Cannot add property in prevent extensions'.
3484 napi_status status = napi_set_named_property(env, object, TEST_STR, numberAttribute);
3485 ASSERT_EQ(status, napi_pending_exception);
3486 }
3487
3488 /**
3489 * @tc.name: NapiObjectSealTest001
3490 * @tc.desc: Test napi_object_seal when env is a context env.
3491 * @tc.type: FUNC
3492 */
3493 HWTEST_F(NapiContextTest, NapiObjectSealTest001, testing::ext::TestSize.Level1)
3494 {
3495 CheckContextEnv();
3496 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3497
3498 napi_value object = nullptr;
3499 napi_create_object(env, &object);
3500 napi_value strAttribute = nullptr;
3501 napi_create_string_utf8(env, TEST_VALUE, strlen(TEST_VALUE), &strAttribute);
3502 napi_set_named_property(env, object, TEST_KEY, strAttribute);
3503
3504 ASSERT_CHECK_CALL(napi_object_seal(env, object));
3505
3506 bool testDeleted = false;
3507 napi_value key = nullptr;
3508 napi_create_string_utf8(env, TEST_KEY, NAPI_AUTO_LENGTH, &key);
3509 ASSERT_CHECK_CALL(napi_delete_property(env, object, key, &testDeleted));
3510 ASSERT_FALSE(testDeleted);
3511 }
3512
3513 /**
3514 * @tc.name: NapiObjectSealTest001
3515 * @tc.desc: Test napi_object_seal when env is a context env.
3516 * @tc.type: FUNC
3517 */
3518 HWTEST_F(NapiContextTest, NapiRunScriptPathTest001, testing::ext::TestSize.Level1)
3519 {
3520 CheckContextEnv();
3521 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3522
3523 napi_value result = nullptr;
3524 auto res = napi_run_script_path(env, TEST_STR, &result);
3525 ASSERT_EQ(res, napi_ok);
3526 }
3527
3528 /**
3529 * @tc.name: NapiLoadModuleTest001
3530 * @tc.desc: Test napi_load_module when env is a context env.
3531 * @tc.type: FUNC
3532 */
3533 HWTEST_F(NapiContextTest, NapiLoadModuleTest001, testing::ext::TestSize.Level1)
3534 {
3535 CheckContextEnv();
3536 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3537
3538 napi_value result = nullptr;
3539 auto res = napi_load_module(env, nullptr, &result);
3540 ASSERT_EQ(res, napi_ok);
3541 }
3542
3543 /**
3544 * @tc.name: NapiLoadModuleWithInfoTest001
3545 * @tc.desc: Test napi_load_module_with_info when env is a context env.
3546 * @tc.type: FUNC
3547 */
3548 HWTEST_F(NapiContextTest, NapiLoadModuleWithInfoTest001, testing::ext::TestSize.Level1)
3549 {
3550 CheckContextEnv();
3551 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3552
3553 napi_value result = nullptr;
3554 napi_status res = napi_load_module_with_info(env, STR_MODULE_PATH, nullptr, &result);
3555 ASSERT_EQ(res, napi_generic_failure);
3556 const napi_extended_error_info* result2;
3557 napi_get_last_error_info(env, &result2);
3558 ASSERT_EQ(result2->error_code, napi_generic_failure);
3559 }
3560
TestCallFunc(napi_env env,napi_callback_info info)3561 static napi_value TestCallFunc(napi_env env, napi_callback_info info)
3562 {
3563 return nullptr;
3564 }
3565
3566 /**
3567 * @tc.name: MakeCallbackWithMultiContext001
3568 * @tc.desc: Test napi_make_callback when context is sub context.
3569 * interface.
3570 * @tc.type: FUNC
3571 */
3572 HWTEST_F(NapiContextTest, MakeCallbackWithMultiContext001, testing::ext::TestSize.Level1)
3573 {
3574 ASSERT_NE(multiContextEngine_, nullptr);
3575 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3576
3577 napi_value global;
3578 napi_get_global(env, &global);
3579 size_t argc = 0;
3580 napi_value result = nullptr;
3581 napi_value funcValue = nullptr;
3582 napi_create_function(env, TEST_STR, NAPI_AUTO_LENGTH, TestCallFunc, nullptr, &funcValue);
3583 ASSERT_CHECK_CALL(napi_make_callback(env, nullptr, global, funcValue, argc, nullptr, &result));
3584 }
3585
3586 /**
3587 * @tc.name: GetPrototypeWithMultiContext001
3588 * @tc.desc: Test napi_get_prototype when context is sub context.
3589 * interface.
3590 * @tc.type: FUNC
3591 */
3592 HWTEST_F(NapiContextTest, GetPrototypeWithMultiContext001, testing::ext::TestSize.Level1)
3593 {
3594 ASSERT_NE(multiContextEngine_, nullptr);
3595 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3596
3597 napi_value obj = nullptr;
3598 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3599
3600 napi_value prototype = nullptr;
3601 ASSERT_CHECK_CALL(napi_get_prototype(env, obj, &prototype));
3602 ASSERT_CHECK_VALUE_TYPE(env, prototype, napi_object);
3603 }
3604
3605 /**
3606 * @tc.name: GetPropertyNamesWithMultiContext001
3607 * @tc.desc: Test napi_get_property_names when context is sub context.
3608 * interface.
3609 * @tc.type: FUNC
3610 */
3611 HWTEST_F(NapiContextTest, GetPropertyNamesWithMultiContext001, testing::ext::TestSize.Level1)
3612 {
3613 ASSERT_NE(multiContextEngine_, nullptr);
3614 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3615
3616 napi_value obj = nullptr;
3617 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3618
3619 napi_value propNames = nullptr;
3620 ASSERT_CHECK_CALL(napi_get_property_names(env, obj, &propNames));
3621 ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
3622 bool isArray = false;
3623 ASSERT_CHECK_CALL(napi_is_array(env, propNames, &isArray));
3624 ASSERT_TRUE(isArray);
3625 }
3626
3627 /**
3628 * @tc.name: SetPropertyWithMultiContext001
3629 * @tc.desc: Test napi_set_property when context is sub context.
3630 * interface.
3631 * @tc.type: FUNC
3632 */
3633 HWTEST_F(NapiContextTest, SetPropertyWithMultiContext001, testing::ext::TestSize.Level1)
3634 {
3635 ASSERT_NE(multiContextEngine_, nullptr);
3636 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3637
3638 napi_value obj = nullptr;
3639 napi_value key = nullptr;
3640 napi_value value = nullptr;
3641 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3642 ASSERT_CHECK_CALL(napi_create_int32(env, INT_ZERO, &key));
3643 ASSERT_CHECK_CALL(napi_create_int32(env, INT_THREE, &value));
3644 ASSERT_CHECK_CALL(napi_set_property(env, obj, key, value));
3645
3646 napi_value propVal = nullptr;
3647 int32_t resultValue = 0;
3648 ASSERT_CHECK_CALL(napi_get_property(env, obj, key, &propVal));
3649 ASSERT_CHECK_CALL(napi_get_value_int32(env, propVal, &resultValue));
3650 ASSERT_EQ(resultValue, INT_THREE);
3651 }
3652
3653 /**
3654 * @tc.name: HasPropertyWithMultiContext001
3655 * @tc.desc: Test napi_has_property when context is sub context.
3656 * interface.
3657 * @tc.type: FUNC
3658 */
3659 HWTEST_F(NapiContextTest, HasPropertyWithMultiContext001, testing::ext::TestSize.Level1)
3660 {
3661 ASSERT_NE(multiContextEngine_, nullptr);
3662 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3663
3664 napi_value obj = nullptr;
3665 napi_value key = nullptr;
3666 bool result = false;
3667 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3668 ASSERT_CHECK_CALL(napi_create_int32(env, INT_ZERO, &key));
3669 ASSERT_CHECK_CALL(napi_has_property(env, obj, key, &result));
3670 ASSERT_FALSE(result);
3671 }
3672
3673 /**
3674 * @tc.name: GetPropertyWithMultiContext001
3675 * @tc.desc: Test napi_get_property when context is sub context.
3676 * interface.
3677 * @tc.type: FUNC
3678 */
3679 HWTEST_F(NapiContextTest, GetPropertyWithMultiContext001, testing::ext::TestSize.Level1)
3680 {
3681 ASSERT_NE(multiContextEngine_, nullptr);
3682 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3683
3684 napi_value obj = nullptr;
3685 napi_value key = nullptr;
3686 napi_value result = nullptr;
3687 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3688 ASSERT_CHECK_CALL(napi_create_string_utf8(env, PROPERTY_NAME, NAPI_AUTO_LENGTH, &key));
3689 ASSERT_CHECK_CALL(napi_get_property(env, obj, key, &result));
3690 ASSERT_CHECK_VALUE_TYPE(env, result, napi_function);
3691 }
3692
3693 /**
3694 * @tc.name: DeletePropertyWithMultiContext001
3695 * @tc.desc: Test napi_delete_property when context is sub context.
3696 * interface.
3697 * @tc.type: FUNC
3698 */
3699 HWTEST_F(NapiContextTest, DeletePropertyWithMultiContext001, testing::ext::TestSize.Level1)
3700 {
3701 ASSERT_NE(multiContextEngine_, nullptr);
3702 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3703
3704 napi_value obj = nullptr;
3705 napi_value key = nullptr;
3706 napi_value value = nullptr;
3707 bool result;
3708
3709 ASSERT_CHECK_CALL(napi_create_object(env, &obj));
3710 ASSERT_CHECK_CALL(napi_create_int32(env, INT_ZERO, &key));
3711 ASSERT_CHECK_CALL(napi_create_int32(env, INT_TWO, &value));
3712 ASSERT_CHECK_CALL(napi_set_property(env, obj, key, value));
3713 ASSERT_CHECK_CALL(napi_delete_property(env, obj, key, &result));
3714 ASSERT_TRUE(result);
3715 }
3716
3717 /**
3718 * @tc.name: WrapEnhanceWithMultiContext001
3719 * @tc.desc: Test napi_wrap_enhance when context is sub context.
3720 * interface.
3721 * @tc.type: FUNC
3722 */
3723 HWTEST_F(NapiContextTest, WrapEnhanceWithMultiContext001, testing::ext::TestSize.Level1)
3724 {
3725 ASSERT_NE(multiContextEngine_, nullptr);
3726 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3727
3728 napi_value testWrapClass = nullptr;
3729 napi_define_class(
3730 env, SENDABLE_CLASS_NAME, NAPI_AUTO_LENGTH,
__anon9b81ebcd2f02(napi_env env, napi_callback_info info) 3731 [](napi_env env, napi_callback_info info) -> napi_value {
3732 napi_value thisVar = nullptr;
3733 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
3734
3735 return thisVar;
3736 },
3737 nullptr, 0, nullptr, &testWrapClass);
3738
3739 napi_value instanceValue = nullptr;
3740 napi_new_instance(env, testWrapClass, 0, nullptr, &instanceValue);
3741
3742 size_t size = sizeof(TEST_WRAP_STRING) / sizeof(char);
3743 napi_wrap_enhance(
__anon9b81ebcd3002(napi_env env, void* data, void* hint) 3744 env, instanceValue, (void*)TEST_WRAP_STRING, [](napi_env env, void* data, void* hint) {}, true, nullptr, size,
3745 nullptr);
3746
3747 char* tempTestStr = nullptr;
3748 napi_unwrap(env, instanceValue, (void**)&tempTestStr);
3749 ASSERT_STREQ(TEST_WRAP_STRING, tempTestStr);
3750
3751 char* tempTestStr1 = nullptr;
3752 napi_remove_wrap(env, instanceValue, (void**)&tempTestStr1);
3753 ASSERT_STREQ(TEST_WRAP_STRING, tempTestStr1);
3754 }
3755
3756 /**
3757 * @tc.name: ModuleRegisterWithMultiContext001
3758 * @tc.desc: Test napi_module_register when context is sub context.
3759 * interface.
3760 * @tc.type: FUNC
3761 */
3762 HWTEST_F(NapiContextTest, ModuleRegisterWithMultiContext001, testing::ext::TestSize.Level1)
3763 {
3764 ASSERT_NE(multiContextEngine_, nullptr);
3765 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3766
3767 static napi_module demoModule = {
3768 .nm_version = 1,
3769 .nm_flags = 0,
3770 .nm_filename = nullptr,
3771 .nm_register_func = nullptr,
3772 .nm_modname = MOD_NAME,
3773 .nm_priv = ((void*)0),
3774 .reserved = { 0 },
3775 };
3776 napi_module_register(&demoModule);
3777 const napi_extended_error_info* errorInfo;
3778 ASSERT_CHECK_CALL(napi_get_last_error_info(env, &errorInfo));
3779 ASSERT_EQ(errorInfo->error_code, napi_ok);
3780 }
3781
3782 /**
3783 * @tc.name: AsyncDestroyWithMultiContext001
3784 * @tc.desc: Test napi_async_destroy when context is sub context.
3785 * interface.
3786 * @tc.type: FUNC
3787 */
3788 HWTEST_F(NapiContextTest, AsyncDestroyWithMultiContext001, testing::ext::TestSize.Level1)
3789 {
3790 ASSERT_NE(multiContextEngine_, nullptr);
3791 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3792
3793 void* arrayBufferPtr = nullptr;
3794 napi_value arrayBuffer = nullptr;
3795 size_t arrayBufferSize = ARRAYBUFFER_SIZE;
3796 napi_value asyncResourceName = nullptr;
3797 napi_async_context result = nullptr;
3798
3799 ASSERT_CHECK_CALL(napi_create_string_utf8(env, __func__, NAPI_AUTO_LENGTH, &asyncResourceName));
3800 ASSERT_CHECK_CALL(napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer));
3801
3802 ASSERT_CHECK_CALL(napi_async_init(env, arrayBuffer, asyncResourceName, &result));
3803 EXPECT_NE(result, nullptr);
3804
3805 ASSERT_CHECK_CALL(napi_async_destroy(env, result));
3806 }
3807
3808 /**
3809 * @tc.name: IsBufferWithMultiContext001
3810 * @tc.desc: Test napi_is_buffer when context is sub context.
3811 * interface.
3812 * @tc.type: FUNC
3813 */
3814 HWTEST_F(NapiContextTest, IsBufferWithMultiContext001, testing::ext::TestSize.Level1)
3815 {
3816 ASSERT_NE(multiContextEngine_, nullptr);
3817 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3818
3819 napi_value buffer = nullptr;
3820 void* bufferPtr = nullptr;
3821 size_t bufferSize = ARRAYBUFFER_SIZE;
3822
3823 ASSERT_CHECK_CALL(napi_create_buffer(env, bufferSize, &bufferPtr, &buffer));
3824 bool isBuffer = false;
3825 ASSERT_CHECK_CALL(napi_is_buffer(env, buffer, &isBuffer));
3826 EXPECT_TRUE(isBuffer);
3827 }
3828
3829 /**
3830 * @tc.name: GetBufferInfoWithMultiContext001
3831 * @tc.desc: Test napi_get_buffer_info when context is sub context.
3832 * interface.
3833 * @tc.type: FUNC
3834 */
3835 HWTEST_F(NapiContextTest, GetBufferInfoWithMultiContext001, testing::ext::TestSize.Level1)
3836 {
3837 ASSERT_NE(multiContextEngine_, nullptr);
3838 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3839
3840 napi_value buffer = nullptr;
3841 void* resultData = nullptr;
3842 size_t bufferSize = ARRAYBUFFER_SIZE;
3843
3844 ASSERT_CHECK_CALL(napi_create_buffer_copy(env, bufferSize, BUFFER_STR, &resultData, &buffer));
3845 void* tmpBufferPtr = nullptr;
3846 size_t bufferLength = 0;
3847 ASSERT_CHECK_CALL(napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength));
3848 EXPECT_EQ(strcmp((char*)tmpBufferPtr, BUFFER_STR), 0);
3849 EXPECT_NE(tmpBufferPtr, nullptr);
3850 EXPECT_EQ(bufferLength, ARRAYBUFFER_SIZE);
3851 }
3852
3853 /**
3854 * @tc.name: DeleteAsyncWorkWithMultiContext001
3855 * @tc.desc: Test napi_delete_async_work when context is sub context.
3856 * interface.
3857 * @tc.type: FUNC
3858 */
3859 HWTEST_F(NapiContextTest, DeleteAsyncWorkWithMultiContext001, testing::ext::TestSize.Level1)
3860 {
3861 ASSERT_NE(multiContextEngine_, nullptr);
3862 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3863
3864 struct AsyncWorkContext {
3865 napi_async_work work = nullptr;
3866 };
3867 std::unique_ptr<AsyncWorkContext> asyncWorkContext = std::make_unique<AsyncWorkContext>();
3868 napi_value resourceName = nullptr;
3869 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_CHAR_ASYNCWORK, NAPI_AUTO_LENGTH, &resourceName));
3870 ASSERT_CHECK_CALL(napi_create_async_work(
__anon9b81ebcd3102(napi_env env, void* data) 3871 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anon9b81ebcd3202(napi_env env, napi_status status, void* data) 3872 [](napi_env env, napi_status status, void* data) {},
3873 nullptr, &asyncWorkContext->work));
3874 ASSERT_CHECK_CALL(napi_delete_async_work(env, asyncWorkContext->work));
3875 }
3876
3877 /**
3878 * @tc.name: GetNodeVersionWithMultiContext001
3879 * @tc.desc: Test napi_get_node_version when context is sub context.
3880 * interface.
3881 * @tc.type: FUNC
3882 */
3883 HWTEST_F(NapiContextTest, GetNodeVersionWithMultiContext001, testing::ext::TestSize.Level1)
3884 {
3885 ASSERT_NE(multiContextEngine_, nullptr);
3886 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3887
3888 const napi_node_version* version = nullptr;
3889 ASSERT_CHECK_CALL(napi_get_node_version(env, &version));
3890 ASSERT_EQ(version, nullptr);
3891 }
3892
3893 /**
3894 * @tc.name: GetUvEventLoopWithMultiContext001
3895 * @tc.desc: Test napi_get_uv_event_loop when context is sub context.
3896 * interface.
3897 * @tc.type: FUNC
3898 */
3899 HWTEST_F(NapiContextTest, GetUvEventLoopWithMultiContext001, testing::ext::TestSize.Level1)
3900 {
3901 ASSERT_NE(multiContextEngine_, nullptr);
3902 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3903
3904 struct uv_loop_s* loop = nullptr;
3905 napi_status status = napi_get_uv_event_loop(env, &loop);
3906 ASSERT_EQ(status, napi_invalid_arg);
3907 ASSERT_EQ(loop, nullptr);
3908 }
3909
TestFatalException(napi_env env,napi_callback_info info)3910 static napi_value TestFatalException(napi_env env, napi_callback_info info)
3911 {
3912 napi_value err;
3913 size_t argc = 1;
3914
3915 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &err, nullptr, nullptr));
3916 NAPI_CALL(env, napi_fatal_exception(env, err));
3917 return nullptr;
3918 }
3919
3920 /**
3921 * @tc.name: FatalExceptionWithMultiContext001
3922 * @tc.desc: Test napi_fatal_exception when context is sub context.
3923 * interface.
3924 * @tc.type: FUNC
3925 */
3926 HWTEST_F(NapiContextTest, FatalExceptionWithMultiContext001, testing::ext::TestSize.Level1)
3927 {
3928 ASSERT_NE(multiContextEngine_, nullptr);
3929 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3930
3931 ASSERT_EQ(TestFatalException(env, nullptr), nullptr);
3932 }
3933
Cleanup(void * arg)3934 static void Cleanup(void* arg)
3935 {
3936 g_hookTag += INT_ONE;
3937 if (arg != nullptr) {
3938 }
3939 }
3940 /**
3941 * @tc.name: AddEnvCleanupHookWithMultiContext001
3942 * @tc.desc: Test napi_add_env_cleanup_hook when context is sub context.
3943 * interface.
3944 * @tc.type: FUNC
3945 */
3946 HWTEST_F(NapiContextTest, AddEnvCleanupHookWithMultiContext001, testing::ext::TestSize.Level1)
3947 {
3948 napi_env newEnv = nullptr;
3949 napi_create_ark_context(reinterpret_cast<napi_env>(engine_), &newEnv);
3950 g_hookTag = INT_ZERO;
3951 ASSERT_CHECK_CALL(napi_add_env_cleanup_hook(newEnv, Cleanup, nullptr));
3952 napi_destroy_ark_context(newEnv);
3953 ASSERT_EQ(g_hookTag, INT_ONE);
3954 }
3955
3956 /**
3957 * @tc.name: RemoveEnvCleanupHookWithMultiContext001
3958 * @tc.desc: Test napi_remove_env_cleanup_hook when context is sub context.
3959 * interface.
3960 * @tc.type: FUNC
3961 */
3962 HWTEST_F(NapiContextTest, RemoveEnvCleanupHookWithMultiContext001, testing::ext::TestSize.Level1)
3963 {
3964 napi_env newEnv = nullptr;
3965 napi_create_ark_context(reinterpret_cast<napi_env>(engine_), &newEnv);
3966 g_hookTag = INT_ZERO;
3967 ASSERT_CHECK_CALL(napi_add_env_cleanup_hook(newEnv, Cleanup, &g_hookArgOne));
3968 ASSERT_CHECK_CALL(napi_add_env_cleanup_hook(newEnv, Cleanup, &g_hookArgTwo));
3969 ASSERT_CHECK_CALL(napi_remove_env_cleanup_hook(newEnv, Cleanup, &g_hookArgTwo));
3970 napi_destroy_ark_context(newEnv);
3971 ASSERT_EQ(g_hookTag, INT_ONE);
3972 }
3973
3974 /**
3975 * @tc.name: OpenCallbackScopeWithMultiContext001
3976 * @tc.desc: Test napi_open_callback_scope when context is sub context.
3977 * interface.
3978 * @tc.type: FUNC
3979 */
3980 HWTEST_F(NapiContextTest, OpenCallbackScopeWithMultiContext001, testing::ext::TestSize.Level1)
3981 {
3982 ASSERT_NE(multiContextEngine_, nullptr);
3983 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
3984
3985 napi_value obj = nullptr;
3986 napi_async_context context = nullptr;
3987 napi_value name;
3988 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR, NAPI_AUTO_LENGTH, &name));
3989 ASSERT_CHECK_CALL(napi_async_init(env, nullptr, name, &context));
3990 EXPECT_NE(context, nullptr);
3991 napi_callback_scope result = nullptr;
3992 ASSERT_CHECK_CALL(napi_open_callback_scope(env, obj, context, &result));
3993 EXPECT_NE(result, nullptr);
3994 }
3995
3996 /**
3997 * @tc.name: CloseCallbackScopeWithMultiContext001
3998 * @tc.desc: Test napi_close_callback_scope when context is sub context.
3999 * interface.
4000 * @tc.type: FUNC
4001 */
4002 HWTEST_F(NapiContextTest, CloseCallbackScopeWithMultiContext001, testing::ext::TestSize.Level1)
4003 {
4004 ASSERT_NE(multiContextEngine_, nullptr);
4005 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4006
4007 napi_value obj = nullptr;
4008 napi_async_context context = nullptr;
4009 napi_value name;
4010 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR, NAPI_AUTO_LENGTH, &name));
4011 ASSERT_CHECK_CALL(napi_async_init(env, nullptr, name, &context));
4012 EXPECT_NE(context, nullptr);
4013 napi_callback_scope result = nullptr;
4014 ASSERT_CHECK_CALL(napi_open_callback_scope(env, obj, context, &result));
4015 EXPECT_NE(result, nullptr);
4016 ASSERT_CHECK_CALL(napi_close_callback_scope(env, result));
4017 }
4018
4019 /**
4020 * @tc.name: GetThreadsafeFunctionContextWithMultiContext001
4021 * @tc.desc: Test napi_get_threadsafe_function_context when context is sub context.
4022 * interface.
4023 * @tc.type: FUNC
4024 */
4025 HWTEST_F(NapiContextTest, GetThreadsafeFunctionContextWithMultiContext001, testing::ext::TestSize.Level1)
4026 {
4027 ASSERT_NE(multiContextEngine_, nullptr);
4028 UVLoopRunner runner(multiContextEngine_);
4029 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4030
4031 napi_value resourceName;
4032 ASSERT_CHECK_CALL(napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
4033 napi_threadsafe_function tsfn;
4034 int32_t data = INT_ONE;
4035 ASSERT_CHECK_CALL(napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, 0, 1, &data,
__anon9b81ebcd3302(napi_env env, void* context, void*) 4036 [](napi_env env, void* context, void*) {}, &data,
__anon9b81ebcd3402(napi_env env, napi_value jsCb, void *context, void *data) 4037 [](napi_env env, napi_value jsCb, void *context, void *data) {}, &tsfn));
4038 void* context = nullptr;
4039 ASSERT_CHECK_CALL(napi_get_threadsafe_function_context(tsfn, &context));
4040 ASSERT_EQ(*reinterpret_cast<int32_t*>(context), INT_ONE);
4041 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_release));
4042 runner.Run();
4043 }
4044
4045 /**
4046 * @tc.name: AcquireThreadsafeFunctionWithMultiContext001
4047 * @tc.desc: Test napi_acquire_threadsafe_function when context is sub context.
4048 * interface.
4049 * @tc.type: FUNC
4050 */
4051 HWTEST_F(NapiContextTest, AcquireThreadsafeFunctionWithMultiContext001, testing::ext::TestSize.Level1)
4052 {
4053 ASSERT_NE(multiContextEngine_, nullptr);
4054 UVLoopRunner runner(multiContextEngine_);
4055 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4056
4057 napi_value resourceName;
4058 ASSERT_CHECK_CALL(napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
4059 napi_threadsafe_function tsfn;
4060 ASSERT_CHECK_CALL(napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, 0, 1, nullptr,
__anon9b81ebcd3502(napi_env env, void* context, void*) 4061 [](napi_env env, void* context, void*) {}, nullptr,
__anon9b81ebcd3602(napi_env env, napi_value jsCb, void *context, void *data) 4062 [](napi_env env, napi_value jsCb, void *context, void *data) {}, &tsfn));
4063 ASSERT_CHECK_CALL(napi_acquire_threadsafe_function(tsfn));
4064 ASSERT_EQ(reinterpret_cast<NativeSafeAsyncWork*>(tsfn)->threadCount_, INT_TWO);
4065 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_abort));
4066 runner.Run();
4067 }
4068
4069 /**
4070 * @tc.name: ReleaseThreadsafeFunctionWithMultiContext001
4071 * @tc.desc: Test napi_release_threadsafe_function when context is sub context.
4072 * interface.
4073 * @tc.type: FUNC
4074 */
4075 HWTEST_F(NapiContextTest, ReleaseThreadsafeFunctionWithMultiContext001, testing::ext::TestSize.Level1)
4076 {
4077 ASSERT_NE(multiContextEngine_, nullptr);
4078 UVLoopRunner runner(multiContextEngine_);
4079 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4080
4081 napi_value resourceName;
4082 ASSERT_CHECK_CALL(napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
4083 napi_threadsafe_function tsfn;
4084 ASSERT_CHECK_CALL(napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, 0, 1, nullptr,
__anon9b81ebcd3702(napi_env env, void* context, void*) 4085 [](napi_env env, void* context, void*) {}, nullptr,
__anon9b81ebcd3802(napi_env env, napi_value jsCb, void *context, void *data) 4086 [](napi_env env, napi_value jsCb, void *context, void *data) {}, &tsfn));
4087 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_abort));
4088 runner.Run();
4089 }
4090
4091 /**
4092 * @tc.name: UnrefThreadsafeFunctionWithMultiContext001
4093 * @tc.desc: Test napi_unref_threadsafe_function when context is sub context.
4094 * interface.
4095 * @tc.type: FUNC
4096 */
4097 HWTEST_F(NapiContextTest, UnrefThreadsafeFunctionWithMultiContext001, testing::ext::TestSize.Level1)
4098 {
4099 ASSERT_NE(multiContextEngine_, nullptr);
4100 UVLoopRunner runner(multiContextEngine_);
4101 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4102
4103 napi_value resourceName;
4104 ASSERT_CHECK_CALL(napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
4105 napi_threadsafe_function tsfn;
4106 ASSERT_CHECK_CALL(napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, 0, 1, nullptr,
__anon9b81ebcd3902(napi_env env, void* context, void*) 4107 [](napi_env env, void* context, void*) {}, nullptr,
__anon9b81ebcd3a02(napi_env env, napi_value jsCb, void *context, void *data) 4108 [](napi_env env, napi_value jsCb, void *context, void *data) {}, &tsfn));
4109 ASSERT_CHECK_CALL(napi_unref_threadsafe_function(env, tsfn));
4110 ASSERT_CHECK_CALL(napi_ref_threadsafe_function(env, tsfn));
4111 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_abort));
4112 runner.Run();
4113 }
4114
4115 /**
4116 * @tc.name: RefThreadsafeFunctionWithMultiContext001
4117 * @tc.desc: Test napi_ref_threadsafe_function when context is sub context.
4118 * interface.
4119 * @tc.type: FUNC
4120 */
4121 HWTEST_F(NapiContextTest, RefThreadsafeFunctionWithMultiContext001, testing::ext::TestSize.Level1)
4122 {
4123 ASSERT_NE(multiContextEngine_, nullptr);
4124 UVLoopRunner runner(multiContextEngine_);
4125 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4126
4127 napi_value resourceName;
4128 ASSERT_CHECK_CALL(napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
4129 napi_threadsafe_function tsfn;
4130 ASSERT_CHECK_CALL(napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, 0, 1, nullptr,
__anon9b81ebcd3b02(napi_env env, void* context, void*) 4131 [](napi_env env, void* context, void*) {}, nullptr,
__anon9b81ebcd3c02(napi_env env, napi_value jsCb, void *context, void *data) 4132 [](napi_env env, napi_value jsCb, void *context, void *data) {}, &tsfn));
4133 ASSERT_CHECK_CALL(napi_ref_threadsafe_function(env, tsfn));
4134 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_abort));
4135 runner.Run();
4136 }
4137
4138 /**
4139 * @tc.name: AddAsyncCleanupHookWithMultiContext001
4140 * @tc.desc: Test napi_add_async_cleanup_hook when context is sub context.
4141 * interface.
4142 * @tc.type: FUNC
4143 */
4144 HWTEST_F(NapiContextTest, AddAsyncCleanupHookWithMultiContext001, testing::ext::TestSize.Level1)
4145 {
4146 ASSERT_NE(multiContextEngine_, nullptr);
4147 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4148
4149 napi_async_cleanup_hook_handle handle;
__anon9b81ebcd3d02(napi_async_cleanup_hook_handle handle, void* arg) 4150 napi_status res = napi_add_async_cleanup_hook(env, [](napi_async_cleanup_hook_handle handle, void* arg) {},
4151 nullptr, &handle);
4152 EXPECT_EQ(res, napi_ok);
4153 ASSERT_CHECK_CALL(napi_remove_async_cleanup_hook(handle));
4154 }
4155
4156 /**
4157 * @tc.name: RemoveAsyncCleanupHookWithMultiContext001
4158 * @tc.desc: Test napi_add_async_cleanup_hook when context is sub context.
4159 * interface.
4160 * @tc.type: FUNC
4161 */
4162 HWTEST_F(NapiContextTest, RemoveAsyncCleanupHookWithMultiContext001, testing::ext::TestSize.Level1)
4163 {
4164 ASSERT_NE(multiContextEngine_, nullptr);
4165 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4166
4167 napi_async_cleanup_hook_handle handle = nullptr;
__anon9b81ebcd3e02(napi_async_cleanup_hook_handle handle, void* arg) 4168 ASSERT_CHECK_CALL(napi_add_async_cleanup_hook(env, [](napi_async_cleanup_hook_handle handle, void* arg) {},
4169 nullptr, &handle));
4170 EXPECT_NE(handle, nullptr);
4171 ASSERT_CHECK_CALL(napi_remove_async_cleanup_hook(handle));
4172 }
4173
4174 /**
4175 * @tc.name: ApiGetModuleFileNameWithMultiContext001
4176 * @tc.desc: Test node_api_get_module_file_name when context is sub context.
4177 * interface.
4178 * @tc.type: FUNC
4179 */
4180 HWTEST_F(NapiContextTest, ApiGetModuleFileNameWithMultiContext001, testing::ext::TestSize.Level1)
4181 {
4182 ASSERT_NE(multiContextEngine_, nullptr);
4183 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4184
4185 const char* result = nullptr;
4186 ASSERT_CHECK_CALL(node_api_get_module_file_name(env, &result));
4187 ASSERT_NE(result, nullptr);
4188 }
4189
4190 /**
4191 * @tc.name: GetLastErrorInfoWithMultiContext001
4192 * @tc.desc: Test napi_get_last_error_info when context is sub context.
4193 * interface.
4194 * @tc.type: FUNC
4195 */
4196 HWTEST_F(NapiContextTest, GetLastErrorInfoWithMultiContext001, testing::ext::TestSize.Level1)
4197 {
4198 ASSERT_NE(multiContextEngine_, nullptr);
4199 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4200
4201 const napi_extended_error_info* result;
4202 ASSERT_CHECK_CALL(napi_get_last_error_info(env, &result));
4203 }
4204
4205 /**
4206 * @tc.name: GetUndefinedWithMultiContext001
4207 * @tc.desc: Test napi_get_undefined when context is sub context.
4208 * interface.
4209 * @tc.type: FUNC
4210 */
4211 HWTEST_F(NapiContextTest, GetUndefinedWithMultiContext001, testing::ext::TestSize.Level1)
4212 {
4213 ASSERT_NE(multiContextEngine_, nullptr);
4214 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4215
4216 napi_value result = nullptr;
4217 ASSERT_CHECK_CALL(napi_get_undefined(env, &result));
4218 ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
4219 }
4220
4221 /**
4222 * @tc.name: GetNullWithMultiContext001
4223 * @tc.desc: Test napi_get_null when context is sub context.
4224 * interface.
4225 * @tc.type: FUNC
4226 */
4227 HWTEST_F(NapiContextTest, GetNullWithMultiContext001, testing::ext::TestSize.Level1)
4228 {
4229 ASSERT_NE(multiContextEngine_, nullptr);
4230 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4231
4232 napi_value result = nullptr;
4233 ASSERT_CHECK_CALL(napi_get_null(env, &result));
4234 ASSERT_CHECK_VALUE_TYPE(env, result, napi_null);
4235 }
4236
4237 /**
4238 * @tc.name: GetBooleanWithMultiContext001
4239 * @tc.desc: Test napi_get_boolean when context is sub context.
4240 * interface.
4241 * @tc.type: FUNC
4242 */
4243 HWTEST_F(NapiContextTest, GetBooleanWithMultiContext001, testing::ext::TestSize.Level1)
4244 {
4245 ASSERT_NE(multiContextEngine_, nullptr);
4246 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4247
4248 napi_value result = nullptr;
4249 ASSERT_CHECK_CALL(napi_get_boolean(env, true, &result));
4250 ASSERT_CHECK_VALUE_TYPE(env, result, napi_boolean);
4251 }
4252
4253 /**
4254 * @tc.name: CreateObjectWithMultiContext001
4255 * @tc.desc: Test napi_create_object when context is sub context.
4256 * interface.
4257 * @tc.type: FUNC
4258 */
4259 HWTEST_F(NapiContextTest, CreateObjectWithMultiContext001, testing::ext::TestSize.Level1)
4260 {
4261 ASSERT_NE(multiContextEngine_, nullptr);
4262 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4263
4264 napi_value result = nullptr;
4265 ASSERT_CHECK_CALL(napi_create_object(env, &result));
4266 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
4267 }
4268
4269 /**
4270 * @tc.name: CreateArraybufferWithMultiContext001
4271 * @tc.desc: Test napi_create_arraybuffer when context is sub context.
4272 * interface.
4273 * @tc.type: FUNC
4274 */
4275 HWTEST_F(NapiContextTest, CreateArraybufferWithMultiContext001, testing::ext::TestSize.Level1)
4276 {
4277 ASSERT_NE(multiContextEngine_, nullptr);
4278 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4279
4280 napi_value arrayBuffer = nullptr;
4281 void* arrayBufferPtr = nullptr;
4282 size_t arrayBufferSize = ARRAYBUFFER_SIZE;
4283 ASSERT_CHECK_CALL(napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer));
4284
4285 void* tmpArrayBufferPtr = nullptr;
4286 size_t arrayBufferLength = 0;
4287 napi_get_arraybuffer_info(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength);
4288
4289 ASSERT_EQ(arrayBufferPtr, tmpArrayBufferPtr);
4290 ASSERT_EQ(arrayBufferSize, arrayBufferLength);
4291 }
4292
4293 /**
4294 * @tc.name: CreateArraybufferWithLengthWithMultiContext001
4295 * @tc.desc: Test napi_create_array_with_length when context is sub context.
4296 * interface.
4297 * @tc.type: FUNC
4298 */
4299 HWTEST_F(NapiContextTest, CreateArraybufferWithLengthWithMultiContext001, testing::ext::TestSize.Level1)
4300 {
4301 ASSERT_NE(multiContextEngine_, nullptr);
4302 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4303
4304 napi_value array = nullptr;
4305 ASSERT_CHECK_CALL(napi_create_array_with_length(env, INT_THREE, &array));
4306 uint32_t result = 0;
4307 napi_get_array_length(env, array, &result);
4308 ASSERT_EQ(result, INT_THREE);
4309 }
4310
4311 /**
4312 * @tc.name: CreateDoubleWithMultiContext001
4313 * @tc.desc: Test napi_create_double when context is sub context.
4314 * interface.
4315 * @tc.type: FUNC
4316 */
4317 HWTEST_F(NapiContextTest, CreateDoubleWithMultiContext001, testing::ext::TestSize.Level1)
4318 {
4319 ASSERT_NE(multiContextEngine_, nullptr);
4320 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4321
4322 napi_value value = nullptr;
4323 ASSERT_CHECK_CALL(napi_create_double(env, TEST_DOUBLE, &value));
4324 }
4325
4326 /**
4327 * @tc.name: CreateInt32WithMultiContext001
4328 * @tc.desc: Test napi_create_int32 when context is sub context.
4329 * interface.
4330 * @tc.type: FUNC
4331 */
4332 HWTEST_F(NapiContextTest, CreateInt32WithMultiContext001, testing::ext::TestSize.Level1)
4333 {
4334 ASSERT_NE(multiContextEngine_, nullptr);
4335 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4336
4337 napi_value value = nullptr;
4338 ASSERT_CHECK_CALL(napi_create_int32(env, INT_ONE, &value));
4339 }
4340
4341 /**
4342 * @tc.name: CreateUint32WithMultiContext001
4343 * @tc.desc: Test napi_create_uint32 when context is sub context.
4344 * interface.
4345 * @tc.type: FUNC
4346 */
4347 HWTEST_F(NapiContextTest, CreateUint32WithMultiContext001, testing::ext::TestSize.Level1)
4348 {
4349 ASSERT_NE(multiContextEngine_, nullptr);
4350 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4351
4352 napi_value value = nullptr;
4353 ASSERT_CHECK_CALL(napi_create_uint32(env, INT_ONE, &value));
4354 }
4355
4356 /**
4357 * @tc.name: CreateInt64WithMultiContext001
4358 * @tc.desc: Test napi_create_int64 when context is sub context.
4359 * interface.
4360 * @tc.type: FUNC
4361 */
4362 HWTEST_F(NapiContextTest, CreateInt64WithMultiContext001, testing::ext::TestSize.Level1)
4363 {
4364 ASSERT_NE(multiContextEngine_, nullptr);
4365 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4366
4367 napi_value value = nullptr;
4368 ASSERT_CHECK_CALL(napi_create_int64(env, INT_ONE, &value));
4369 }
4370
4371 /**
4372 * @tc.name: CreateStringLatin1WithMultiContext001
4373 * @tc.desc: Test napi_create_string_latin1 when context is sub context.
4374 * interface.
4375 * @tc.type: FUNC
4376 */
4377 HWTEST_F(NapiContextTest, CreateStringLatin1WithMultiContext001, testing::ext::TestSize.Level1)
4378 {
4379 ASSERT_NE(multiContextEngine_, nullptr);
4380 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4381
4382 napi_value result = nullptr;
4383 ASSERT_CHECK_CALL(napi_create_string_latin1(env, TEST_STR_UTF8, NAPI_AUTO_LENGTH, &result));
4384 }
4385
4386 /**
4387 * @tc.name: CreateStringUtf8WithMultiContext001
4388 * @tc.desc: Test napi_create_string_utf8 when context is sub context.
4389 * interface.
4390 * @tc.type: FUNC
4391 */
4392 HWTEST_F(NapiContextTest, CreateStringUtf8WithMultiContext001, testing::ext::TestSize.Level1)
4393 {
4394 ASSERT_NE(multiContextEngine_, nullptr);
4395 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4396
4397 napi_value result = nullptr;
4398 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR_UTF8, NAPI_AUTO_LENGTH, &result));
4399 }
4400
4401 /**
4402 * @tc.name: CreateStringUtf16WithMultiContext001
4403 * @tc.desc: Test napi_create_string_utf16 when context is sub context.
4404 * interface.
4405 * @tc.type: FUNC
4406 */
4407 HWTEST_F(NapiContextTest, CreateStringUtf16WithMultiContext001, testing::ext::TestSize.Level1)
4408 {
4409 ASSERT_NE(multiContextEngine_, nullptr);
4410 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4411
4412 napi_value result = nullptr;
4413 ASSERT_CHECK_CALL(napi_create_string_utf16(env, TEST_STR_UTF16, NAPI_AUTO_LENGTH, &result));
4414 }
4415
4416 /**
4417 * @tc.name: CreateSymbolWithMultiContext001
4418 * @tc.desc: Test napi_create_symbol when context is sub context.
4419 * interface.
4420 * @tc.type: FUNC
4421 */
4422 HWTEST_F(NapiContextTest, CreateSymbolWithMultiContext001, testing::ext::TestSize.Level1)
4423 {
4424 ASSERT_NE(multiContextEngine_, nullptr);
4425 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4426
4427 napi_value result = nullptr;
4428 napi_create_string_latin1(env, TEST_STR, NAPI_AUTO_LENGTH, &result);
4429
4430 napi_value symbolVal = nullptr;
4431 ASSERT_CHECK_CALL(napi_create_symbol(env, result, &symbolVal));
4432 ASSERT_CHECK_VALUE_TYPE(env, symbolVal, napi_symbol);
4433 }
4434
4435 /**
4436 * @tc.name: TypeofWithMultiContext001
4437 * @tc.desc: Test napi_typeof when context is sub context.
4438 * interface.
4439 * @tc.type: FUNC
4440 */
4441 HWTEST_F(NapiContextTest, TypeofWithMultiContext001, testing::ext::TestSize.Level1)
4442 {
4443 ASSERT_NE(multiContextEngine_, nullptr);
4444 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4445
4446 napi_value value = nullptr;
4447 napi_valuetype result;
4448 ASSERT_CHECK_CALL(napi_create_double(env, TEST_DOUBLE, &value));
4449 ASSERT_CHECK_CALL(napi_typeof(env, value, &result));
4450 ASSERT_EQ(result, napi_number);
4451 }
4452
4453 /**
4454 * @tc.name: GetValueDoubleWithMultiContext001
4455 * @tc.desc: Test napi_get_value_double when context is sub context.
4456 * interface.
4457 * @tc.type: FUNC
4458 */
4459 HWTEST_F(NapiContextTest, GetValueDoubleWithMultiContext001, testing::ext::TestSize.Level1)
4460 {
4461 ASSERT_NE(multiContextEngine_, nullptr);
4462 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4463
4464 napi_value value = nullptr;
4465 ASSERT_CHECK_CALL(napi_create_double(env, TEST_DOUBLE, &value));
4466 double number;
4467 ASSERT_CHECK_CALL(napi_get_value_double(env, value, &number));
4468 ASSERT_EQ(number, TEST_DOUBLE);
4469 }
4470
4471 /**
4472 * @tc.name: GetValueInt32WithMultiContext001
4473 * @tc.desc: Test napi_get_value_int32 when context is sub context.
4474 * interface.
4475 * @tc.type: FUNC
4476 */
4477 HWTEST_F(NapiContextTest, GetValueInt32WithMultiContext001, testing::ext::TestSize.Level1)
4478 {
4479 ASSERT_NE(multiContextEngine_, nullptr);
4480 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4481
4482 napi_value value = nullptr;
4483 ASSERT_CHECK_CALL(napi_create_int32(env, INT_ONE, &value));
4484 int32_t number;
4485 ASSERT_CHECK_CALL(napi_get_value_int32(env, value, &number));
4486 ASSERT_EQ(number, INT_ONE);
4487 }
4488
4489 /**
4490 * @tc.name: GetValueUint32WithMultiContext001
4491 * @tc.desc: Test napi_get_value_uint32 when context is sub context.
4492 * interface.
4493 * @tc.type: FUNC
4494 */
4495 HWTEST_F(NapiContextTest, GetValueUint32WithMultiContext001, testing::ext::TestSize.Level1)
4496 {
4497 ASSERT_NE(multiContextEngine_, nullptr);
4498 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4499
4500 napi_value value = nullptr;
4501 ASSERT_CHECK_CALL(napi_create_uint32(env, INT_ONE, &value));
4502 uint32_t number;
4503 ASSERT_CHECK_CALL(napi_get_value_uint32(env, value, &number));
4504 ASSERT_EQ(number, INT_ONE);
4505 }
4506
4507 /**
4508 * @tc.name: GetValueInt64WithMultiContext001
4509 * @tc.desc: Test napi_get_value_int64 when context is sub context.
4510 * interface.
4511 * @tc.type: FUNC
4512 */
4513 HWTEST_F(NapiContextTest, GetValueInt64WithMultiContext001, testing::ext::TestSize.Level1)
4514 {
4515 ASSERT_NE(multiContextEngine_, nullptr);
4516 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4517
4518 napi_value value = nullptr;
4519 ASSERT_CHECK_CALL(napi_create_int64(env, INT_ONE, &value));
4520 int64_t number;
4521 ASSERT_CHECK_CALL(napi_get_value_int64(env, value, &number));
4522 ASSERT_EQ(number, INT_ONE);
4523 }
4524
4525 /**
4526 * @tc.name: GetValueBoolWithMultiContext001
4527 * @tc.desc: Test napi_get_value_bool when context is sub context.
4528 * interface.
4529 * @tc.type: FUNC
4530 */
4531 HWTEST_F(NapiContextTest, GetValueBoolWithMultiContext001, testing::ext::TestSize.Level1)
4532 {
4533 ASSERT_NE(multiContextEngine_, nullptr);
4534 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4535
4536 napi_value value = nullptr;
4537 ASSERT_CHECK_CALL(napi_get_boolean(env, true, &value));
4538 bool boolValue;
4539 ASSERT_CHECK_CALL(napi_get_value_bool(env, value, &boolValue));
4540 ASSERT_EQ(boolValue, true);
4541 }
4542
4543 /**
4544 * @tc.name: GetValueStringLatin1WithMultiContext001
4545 * @tc.desc: Test napi_get_value_string_latin1 when context is sub context.
4546 * interface.
4547 * @tc.type: FUNC
4548 */
4549 HWTEST_F(NapiContextTest, GetValueStringLatin1WithMultiContext001, testing::ext::TestSize.Level1)
4550 {
4551 ASSERT_NE(multiContextEngine_, nullptr);
4552 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4553
4554 size_t testStrLength = strlen(TEST_STR_UTF8);
4555 napi_value result = nullptr;
4556 ASSERT_CHECK_CALL(napi_create_string_latin1(env, TEST_STR_UTF8, testStrLength, &result));
4557 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
4558
4559 char* buffer = nullptr;
4560 size_t bufferSize = 0;
4561 size_t strLength = 0;
4562 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, nullptr, 0, &bufferSize));
4563 ASSERT_GT(bufferSize, static_cast<size_t>(0));
__anon9b81ebcd3f02null4564 buffer = new char[bufferSize + 1]{ 0 };
4565 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, buffer, bufferSize + 1, &strLength));
4566 ASSERT_STREQ(TEST_STR_UTF8, buffer);
4567 ASSERT_EQ(testStrLength, strLength);
4568 delete []buffer;
4569 buffer = nullptr;
4570 }
4571
4572 /**
4573 * @tc.name: GetValueStringUtf8WithMultiContext001
4574 * @tc.desc: Test napi_get_value_string_utf8 when context is sub context.
4575 * interface.
4576 * @tc.type: FUNC
4577 */
4578 HWTEST_F(NapiContextTest, GetValueStringUtf8WithMultiContext001, testing::ext::TestSize.Level1)
4579 {
4580 ASSERT_NE(multiContextEngine_, nullptr);
4581 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4582
4583 size_t testStrLength = strlen(TEST_STR_UTF8);
4584 napi_value result = nullptr;
4585 ASSERT_CHECK_CALL(napi_create_string_utf8(env, TEST_STR_UTF8, testStrLength, &result));
4586 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
4587
4588 char* buffer = nullptr;
4589 size_t bufferSize = 0;
4590 size_t strLength = 0;
4591 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize));
4592 ASSERT_GT(bufferSize, static_cast<size_t>(0));
__anon9b81ebcd4002null4593 buffer = new char[bufferSize + 1]{ 0 };
4594 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength));
4595 ASSERT_STREQ(TEST_STR_UTF8, buffer);
4596 ASSERT_EQ(testStrLength, strLength);
4597 delete []buffer;
4598 buffer = nullptr;
4599 }
4600
4601 /**
4602 * @tc.name: GetValuStringUtf16WithMultiContext001
4603 * @tc.desc: Test napi_get_value_string_utf16 when context is sub context.
4604 * interface.
4605 * @tc.type: FUNC
4606 */
4607 HWTEST_F(NapiContextTest, GetValuStringUtf16WithMultiContext001, testing::ext::TestSize.Level1)
4608 {
4609 ASSERT_NE(multiContextEngine_, nullptr);
4610 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4611
4612 size_t testStrLength = static_cast<size_t>(std::char_traits<char16_t>::length(TEST_STR_UTF16));
4613 napi_value result = nullptr;
4614 ASSERT_CHECK_CALL(napi_create_string_utf16(env, TEST_STR_UTF16, testStrLength, &result));
4615 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
4616
4617 char16_t* buffer = nullptr;
4618 size_t bufferSize = 0;
4619 size_t strLength = 0;
4620 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, nullptr, 0, &bufferSize));
4621 ASSERT_GT(bufferSize, static_cast<size_t>(0));
__anon9b81ebcd4102null4622 buffer = new char16_t[bufferSize + 1]{ 0 };
4623 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, buffer, bufferSize + 1, &strLength));
4624 for (int i = 0; i < testStrLength; i++) {
4625 ASSERT_EQ(TEST_STR_UTF16[i], buffer[i]);
4626 }
4627 ASSERT_EQ(testStrLength, strLength);
4628 delete []buffer;
4629 buffer = nullptr;
4630 }
4631
4632 /**
4633 * @tc.name: CoerceToBoolWithMultiContext001
4634 * @tc.desc: Test napi_coerce_to_bool when context is sub context.
4635 * interface.
4636 * @tc.type: FUNC
4637 */
4638 HWTEST_F(NapiContextTest, CoerceToBoolWithMultiContext001, testing::ext::TestSize.Level1)
4639 {
4640 ASSERT_NE(multiContextEngine_, nullptr);
4641 napi_env env = reinterpret_cast<napi_env>(multiContextEngine_);
4642
4643 napi_value value;
4644 napi_value result;
4645 ASSERT_CHECK_CALL(napi_create_double(env, TEST_DOUBLE, &value));
4646 ASSERT_CHECK_CALL(napi_coerce_to_bool(env, value, &result));
4647 bool ret = false;
4648 napi_get_value_bool(env, result, &ret);
4649 ASSERT_EQ(ret, true);
4650 }
4651
4652 class ContextAsyncWorkTestData final : public NapiAsyncWorkTestData {
4653 public:
ContextAsyncWorkTestData(napi_env env,const char * name)4654 ContextAsyncWorkTestData(napi_env env, const char* name) : NapiAsyncWorkTestData(env, name) {}
~ContextAsyncWorkTestData()4655 ~ContextAsyncWorkTestData() {};
4656
Execute(napi_env env)4657 void Execute(napi_env env) override
4658 {
4659 executeSucc_ = env == env_;
4660 }
Complete(napi_env env,napi_status status)4661 void Complete(napi_env env, napi_status status) override
4662 {
4663 completeSucc_ = env == env_;
4664 }
4665
4666 bool executeSucc_ { false };
4667 bool completeSucc_ { false };
4668 };
4669
4670 /**
4671 * @tc.name: NapiAsyncWorkWithContextEnv001
4672 * @tc.desc: Test interface of napi_queue_async_work
4673 * @tc.type: FUNC
4674 */
4675 HWTEST_F(NapiContextTest, NapiAsyncWorkWithContextEnv001, testing::ext::TestSize.Level1)
4676 {
4677 UVLoopRunner runner(engine_);
4678 NativeEngineProxy contextEngine(engine_);
4679 ContextAsyncWorkTestData* work = new ContextAsyncWorkTestData(contextEngine, GetTestCaseName());
4680 ASSERT_CHECK_CALL(work->Queue());
4681 runner.Run();
4682 ASSERT_TRUE(work->executeSucc_);
4683 ASSERT_TRUE(work->completeSucc_);
4684 delete work;
4685 }
4686
4687 /**
4688 * @tc.name: NapiAsyncWorkWithContextEnv002
4689 * @tc.desc: Test interface of napi_queue_async_work_with_qos
4690 * @tc.type: FUNC
4691 */
4692 HWTEST_F(NapiContextTest, NapiAsyncWorkWithContextEnv002, testing::ext::TestSize.Level1)
4693 {
4694 UVLoopRunner runner(engine_);
4695 NativeEngineProxy contextEngine(engine_);
4696 ContextAsyncWorkTestData* work = new ContextAsyncWorkTestData(contextEngine, GetTestCaseName());
4697 ASSERT_CHECK_CALL(work->Queue(napi_qos_background));
4698 runner.Run();
4699 ASSERT_TRUE(work->executeSucc_);
4700 ASSERT_TRUE(work->completeSucc_);
4701 delete work;
4702 }
4703
4704 /**
4705 * @tc.name: NapiAsyncWorkWithContextEnv003
4706 * @tc.desc: Test interface of napi_queue_async_work_with_qos
4707 * @tc.type: FUNC
4708 */
4709 HWTEST_F(NapiContextTest, NapiAsyncWorkWithContextEnv003, testing::ext::TestSize.Level1)
4710 {
4711 UVLoopRunner runner(engine_);
4712 NativeEngineProxy contextEngine(engine_);
4713 ContextAsyncWorkTestData* work = new ContextAsyncWorkTestData(contextEngine, GetTestCaseName());
4714 ASSERT_CHECK_CALL(work->Queue(napi_qos_utility));
4715 runner.Run();
4716 ASSERT_TRUE(work->executeSucc_);
4717 ASSERT_TRUE(work->completeSucc_);
4718 delete work;
4719 }
4720
4721 /**
4722 * @tc.name: NapiAsyncWorkWithContextEnv004
4723 * @tc.desc: Test interface of napi_queue_async_work_with_qos
4724 * @tc.type: FUNC
4725 */
4726 HWTEST_F(NapiContextTest, NapiAsyncWorkWithContextEnv004, testing::ext::TestSize.Level1)
4727 {
4728 UVLoopRunner runner(engine_);
4729 NativeEngineProxy contextEngine(engine_);
4730 ContextAsyncWorkTestData* work = new ContextAsyncWorkTestData(contextEngine, GetTestCaseName());
4731 ASSERT_CHECK_CALL(work->Queue(napi_qos_default));
4732 runner.Run();
4733 ASSERT_TRUE(work->executeSucc_);
4734 ASSERT_TRUE(work->completeSucc_);
4735 delete work;
4736 }
4737
4738 /**
4739 * @tc.name: NapiCreateTSFNWithContextEnv005
4740 * @tc.desc: Test interface of napi_queue_async_work_with_qos
4741 * @tc.type: FUNC
4742 */
4743 HWTEST_F(NapiContextTest, NapiCreateTSFNWithContextEnv005, testing::ext::TestSize.Level1)
4744 {
4745 UVLoopRunner runner(engine_);
4746 NativeEngineProxy contextEngine(engine_);
4747 napi_threadsafe_function tsfn = nullptr;
4748 bool* executed = new bool { false };
4749 ASSERT_CHECK_CALL(napi_create_threadsafe_function(
4750 contextEngine, nullptr, nullptr, GetNapiTCName(contextEngine), 0, 1, nullptr, nullptr, nullptr,
__anon9b81ebcd4202(napi_env, napi_value, void*, void* data) 4751 [](napi_env, napi_value, void*, void* data) { *reinterpret_cast<bool*>(data) = true; }, &tsfn));
4752 ASSERT_CHECK_CALL(napi_call_threadsafe_function(tsfn, executed, napi_tsfn_nonblocking));
4753 runner.Run(UV_RUN_ONCE);
4754 ASSERT_TRUE(*executed);
4755 ASSERT_CHECK_CALL(napi_release_threadsafe_function(tsfn, napi_tsfn_release));
4756 runner.Run(UV_RUN_ONCE);
4757 delete executed;
4758 }
4759
4760