From 71d272418dd1734e2e8237e65c7708ee8a606af7 Mon Sep 17 00:00:00 2001 From: hekun Date: Tue, 15 Nov 2022 18:00:39 +0800 Subject: [PATCH] add napi test case Signed-off-by: hekun --- .../entry/src/main/cpp/napi/napi_test.cpp | 298 +++++++++++++++++- .../src/main/ets/test/NativeApiStringTest.ets | 199 +++++++++++- 2 files changed, 493 insertions(+), 4 deletions(-) diff --git a/arkui/ace_napi_test/entry/src/main/cpp/napi/napi_test.cpp b/arkui/ace_napi_test/entry/src/main/cpp/napi/napi_test.cpp index 652fece9b..b349fdf72 100644 --- a/arkui/ace_napi_test/entry/src/main/cpp/napi/napi_test.cpp +++ b/arkui/ace_napi_test/entry/src/main/cpp/napi/napi_test.cpp @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include static bool exceptionWasPending = false; static napi_ref test_reference = NULL; @@ -1320,11 +1323,282 @@ static napi_value TestLatin1(napi_env env, napi_callback_info info) { return output; } +// test the napi function +static napi_value napiCreateArrayBuffer(napi_env env, napi_callback_info info) +{ + napi_value arrayBuffer = nullptr; + void* arrayBufferPtr = nullptr; + size_t arrayBufferSize = 1024; + napi_status status = napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); + NAPI_ASSERT(env, status == napi_ok, "success to napi_create_arraybuffer"); + NAPI_ASSERT(env, arrayBuffer != nullptr, "success create_arrayBuffer"); + + return arrayBuffer; +} + +static napi_value naiGetArrayBufferInfo(napi_env env, napi_callback_info info) +{ + // the value to return + napi_value arrayBuffer; + napi_status status; + void* yourPointer = nullptr; + size_t arrayBufferSize = 1024; + status = napi_create_arraybuffer(env, arrayBufferSize, &yourPointer, &arrayBuffer); + NAPI_ASSERT(env, status == napi_ok, "success to napi_create_arraybuffer"); + + void* tmpArrayBufferPtr = nullptr; + size_t arrayBufferLength = 0; + status = napi_get_arraybuffer_info(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength); + NAPI_ASSERT(env, status == napi_ok, "success to napi_get_arraybuffer_info"); + + napi_value arrayLength; + // return the length of array js type int + NAPI_CALL(env, napi_create_int32(env, arrayBufferLength, &arrayLength)); + return arrayLength; + } + +static napi_value napiNewInstance(napi_env env, napi_callback_info info) +{ + // the value to return + napi_value global, constructor, arg, value; + napi_status status = napi_get_global(env, &global); + NAPI_ASSERT(env, status == napi_ok,"napi_get_global success"); + status = napi_get_named_property(env, global, "MyObject", &constructor); + NAPI_ASSERT(env, status == napi_ok,"napi_get_named_property success"); + status = napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &arg); + napi_value* argv = &arg; + size_t argc = 1; + napi_status _status = napi_new_instance(env, constructor, argc, argv, &value); + NAPI_ASSERT(env, _status != napi_ok, "fail to napi_new_instance"); + return value; +} + +static napi_value napiDefineClass(napi_env env, napi_callback_info info) +{ + napi_value testWrapClass = nullptr; + napi_define_class( + env, "TestWrapClass", NAPI_AUTO_LENGTH, + [](napi_env env, napi_callback_info info) -> napi_value { + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + + return thisVar; + }, + nullptr, 0, nullptr, &testWrapClass); + + napi_value instanceValue = nullptr; + napi_new_instance(env, testWrapClass, 0, nullptr, &instanceValue); + NAPI_ASSERT(env, instanceValue != nullptr, "success napiDefineClass"); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, 0, &value)); + return value; +} + +static napi_value napiRunScriptPath(napi_env env, napi_callback_info info) +{ + napi_value value; + char const *path = "/index/name"; + napi_status status = napi_run_script_path(env, path, &value); + NAPI_ASSERT(env, status == napi_ok,"napi_run_script_path ok"); + napi_value _value; + NAPI_CALL(env, napi_create_int32(env, 0, &_value)); + return _value; +} + +static napi_value napiGetNodeVersion(napi_env env, napi_callback_info info) +{ + napi_value value; + const napi_node_version* version ; + napi_get_node_version(env,&version); + const char* release = version->release; + napi_status status = napi_create_string_utf8(env, release, strlen(release), &value); + NAPI_ASSERT(env, status == napi_ok,"napi_create_string_utf8 ok"); + napi_value _value; + NAPI_CALL(env, napi_create_int32(env, 0, &_value)); + return _value; +} + +static napi_value napiCallThreadsafeFunction(napi_env env, napi_callback_info info) +{ + void *data = nullptr; + napi_threadsafe_function func = (napi_threadsafe_function)data; + napi_threadsafe_function_call_mode blockMode = napi_tsfn_nonblocking; + void* context = nullptr; + napi_status status = napi_get_threadsafe_function_context(func, &context); + NAPI_ASSERT(env, status != napi_ok,"napi_get_threadsafe_function_context fail"); + static int32_t g_sendData = 0; + napi_call_threadsafe_function(func, &g_sendData, blockMode); + status = napi_call_threadsafe_function(func, &g_sendData, blockMode); + NAPI_ASSERT(env, status != napi_ok,"napi_call_threadsafe_function fail"); + napi_release_threadsafe_function(func, napi_tsfn_release); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, 0, &value)); + return value; +} + +static void TsFuncFinalTotalFour(napi_env env, void* finalizeData, void* hint) +{ + static uv_thread_t g_uvThreadTest7; + uv_thread_join(&g_uvThreadTest7); +} + +static void TsFuncCallJsFour(napi_env env, napi_value tsfn_cb, void* context, void* data) +{ + int* pData = (int32_t*)data; + printf("TsFuncCallJsFour is %p \n", pData); +} + +static napi_value napiCreateThreadsafeFunction(napi_env env, napi_callback_info info) +{ + + napi_threadsafe_function tsFunc = nullptr; + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + int32_t CALL_JS_CB_DATA_TEST_ID = 101; + int32_t FINAL_CB_DATA_TEST_ID = 1001; + napi_status status = napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, + 0, 1, &CALL_JS_CB_DATA_TEST_ID, TsFuncFinalTotalFour, &FINAL_CB_DATA_TEST_ID , TsFuncCallJsFour, &tsFunc); + NAPI_ASSERT(env, status == napi_ok,"napi_create_threadsafe_function"); + napi_acquire_threadsafe_function(tsFunc); + status = napi_unref_threadsafe_function(env, tsFunc); + NAPI_ASSERT(env, status == napi_ok, "napi_unref_threadsafe_function"); + napi_value _value; + NAPI_CALL(env, napi_create_int32(env, 0, &_value)); + return _value; +} + +static napi_value napiCancelAsyncWork(napi_env env, napi_callback_info info) +{ + napi_async_work work = nullptr; + napi_value resourceName = nullptr; + napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, nullptr, resourceName, [](napi_env value, void* data) {}, + [](napi_env env, napi_status status, void* data) { + napi_async_work workData = (napi_async_work)data; + napi_delete_async_work(env, workData); + },work, &work); + napi_queue_async_work(env, work); + napi_cancel_async_work(env, work); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, 0, &value)); + return value; +} + +static napi_value SayHello(napi_env env, napi_callback_info info) { + printf("Hello\n"); + return NULL; +} + +static napi_value napiCreateFunction(napi_env env, napi_callback_info info) +{ + napi_value funcValue = nullptr; + napi_status status = napi_create_function(env, NULL, 0, SayHello, NULL, &funcValue); + NAPI_ASSERT(env, status != napi_ok,"napi_create_function fail"); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, 1, &value)); + return value; +} + + +static napi_value napiRefthreadSafeFunction(napi_env env, napi_callback_info info) +{ + napi_threadsafe_function tsFunc = nullptr; + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + int32_t CALL_JS_CB_DATA_TEST_ID = 101; + int32_t FINAL_CB_DATA_TEST_ID = 1001; + napi_status status = napi_create_threadsafe_function(env, nullptr, nullptr, resourceName, + 0, 1, &CALL_JS_CB_DATA_TEST_ID, TsFuncFinalTotalFour, &FINAL_CB_DATA_TEST_ID , TsFuncCallJsFour, &tsFunc); + NAPI_ASSERT(env, status == napi_ok,"napi_create_threadsafe_function"); + status = napi_ref_threadsafe_function(env, tsFunc); + NAPI_ASSERT(env, status == napi_ok,"napi_ref_threadsafe_function"); + napi_value _value; + NAPI_CALL(env, napi_create_int32(env, 0, &_value)); + return _value; +} + +static napi_value napiCreateDate(napi_env env, napi_callback_info info) +{ + napi_value createResult = nullptr; + double time = 202110181203150; + napi_status status = napi_create_date(env, time, &createResult); + NAPI_ASSERT(env, status == napi_ok, "napi_create_date success"); + double getTime = false; + napi_get_date_value(env,createResult, &getTime); + bool result = false; + if (time == getTime) { + result = true; + } + napi_value value; + NAPI_CALL(env, napi_create_int32(env, result, &value)); + return value; +} + +static napi_value napiCreateBigintUint64(napi_env env, napi_callback_info info) +{ + uint64_t testValue = UINT64_MAX; + napi_value result = nullptr; + napi_create_bigint_uint64(env, testValue, &result); + + uint64_t resultValue = 0; + bool flag = false; + napi_get_value_bigint_uint64(env, result, &resultValue, &flag); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, flag, &value)); + return value; +} + +static napi_value napiCreateBigintInt64(napi_env env, napi_callback_info info) +{ + int64_t testValue = INT64_MAX; + napi_value result = nullptr; + napi_create_bigint_int64(env, testValue, &result); + int64_t resultValue = 0; + bool flag = false; + napi_get_value_bigint_int64(env, result, &resultValue, &flag); + napi_value value; + NAPI_CALL(env, napi_create_int32(env, flag, &value)); + return value; +} + +static napi_value napiCreateBigintWords(napi_env env, napi_callback_info info) +{ + int signBit = 0; + size_t wordCount = 4; + uint64_t words[] = { 0xFFFFFFFFFFFFFFFF, 34ULL, 56ULL, 0xFFFFFFFFFFFFFFFF }; + uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL }; + napi_value result = nullptr; + NAPI_CALL(env,napi_create_bigint_words(env, signBit, wordCount, words, &result)); + NAPI_CALL(env,napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut)); + bool testResult = false; + if (signBit == 0 && wordCount == 2 && words[0] == wordsOut[0] && words[1] == wordsOut[1]) { + testResult = true; + } + napi_value value; + NAPI_CALL(env, napi_create_int32(env, testResult, &value)); + return value; +} + +static napi_value napiFatalerror(napi_env env, napi_callback_info info) +{ + void *data = nullptr; + napi_threadsafe_function tsfun = static_cast(data); + if (napi_release_threadsafe_function(tsfun, napi_tsfn_release) == napi_ok) { + napi_fatal_error("ReleaseThreadsafeFunction", NAPI_AUTO_LENGTH, "napi_release_threadsafe_function failed", NAPI_AUTO_LENGTH); + } + napi_value _value; + NAPI_CALL(env, napi_create_int32(env, 0, &_value)); + return _value; +} + + + EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("getLastErrorInfo", getLastErrorInfo), DECLARE_NAPI_FUNCTION("cleanUpErrorInfo", cleanUpErrorInfo), DECLARE_NAPI_FUNCTION("throwExistingError", throwExistingError), @@ -1396,9 +1670,27 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION("resolveAndRejectDeferred", resolveAndRejectDeferred), DECLARE_NAPI_FUNCTION("isPromise", isPromise), DECLARE_NAPI_FUNCTION("TestLatin1", TestLatin1), - DECLARE_NAPI_FUNCTION("runScript", runScript), }; - - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(*properties), properties)); + DECLARE_NAPI_FUNCTION("runScript", runScript), + DECLARE_NAPI_FUNCTION("napiCreateArrayBuffer", napiCreateArrayBuffer), + DECLARE_NAPI_FUNCTION("naiGetArrayBufferInfo", naiGetArrayBufferInfo), + DECLARE_NAPI_FUNCTION("napiNewInstance", napiNewInstance), + DECLARE_NAPI_FUNCTION("napiDefineClass", napiDefineClass), + DECLARE_NAPI_FUNCTION("napiRunScriptPath", napiRunScriptPath), + DECLARE_NAPI_FUNCTION("napiGetNodeVersion", napiGetNodeVersion), + DECLARE_NAPI_FUNCTION("napiCallThreadsafeFunction", napiCallThreadsafeFunction), + DECLARE_NAPI_FUNCTION("napiCreateThreadsafeFunction", napiCreateThreadsafeFunction), + DECLARE_NAPI_FUNCTION("napiRefthreadSafeFunction", napiRefthreadSafeFunction), + DECLARE_NAPI_FUNCTION("napiCreateDate", napiCreateDate), + DECLARE_NAPI_FUNCTION("napiCreateBigintUint64", napiCreateBigintUint64), + DECLARE_NAPI_FUNCTION("napiCreateBigintInt64", napiCreateBigintInt64), + DECLARE_NAPI_FUNCTION("napiCreateBigintWords", napiCreateBigintWords), + { "napiCancelAsyncWork", nullptr, napiCancelAsyncWork, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "napiCreateFunction", nullptr, napiCreateFunction, nullptr, nullptr, nullptr, napi_default, nullptr }, + + DECLARE_NAPI_FUNCTION("napiFatalerror", napiFatalerror), }; + + + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); return exports; } EXTERN_C_END diff --git a/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiStringTest.ets b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiStringTest.ets index acc725ecc..2490cc657 100644 --- a/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiStringTest.ets +++ b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiStringTest.ets @@ -22,7 +22,7 @@ import napitest from 'libnapitest.so' export default function nativeApiStringJsunit() { describe('napiStringTest', function () { - const empty = ''; + const empty = 'hh'; var value; async function sleep(time) { @@ -56,5 +56,202 @@ export default function nativeApiStringJsunit() { done(); }); + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0200 + * @tc.name napiCreateArrayBuffer + * @tc.desc aceNapiEtsTest + */ + it('napiCreateArrayBuffer002', 0, async function (done) { + console.info('napiCreateArrayBuffer002 START'); + value = napitest.napiCreateArrayBuffer(); + console.info('apiCreateArrayBuffer testString result is: ' + JSON.stringify(value)); + expect(value instanceof ArrayBuffer).assertTrue(); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0300 + * @tc.name naiGetArrayBufferInfo + * @tc.desc aceNapiEtsTest + */ + it('naiGetArrayBufferInfo003', 0, async function (done) { + console.info('naiGetArrayBufferInfo003 START'); + value = napitest.naiGetArrayBufferInfo(); + console.info('naiGetArrayBufferInfo testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(1024); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0400 + * @tc.name napiCreateFunction + * @tc.desc aceNapiEtsTest + */ + it('napiCreateFunction004', 0, async function (done) { + console.info('napiCreateFunction004 START'); + value = napitest.napiCreateFunction(); + console.info('napiCreateFunction testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(1); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0500 + * @tc.name napiNewInstance + * @tc.desc aceNapiEtsTest + */ + + it('napiNewInstance005', 0, async function (done) { + console.info(' napiNewInstance005 START'); + value = napitest.napiNewInstance('hello'); + console.info('napiNewInstance testString result is: ' + JSON.stringify(value)); + expect(value == undefined).assertTrue(); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0600 + * @tc.name napiDefineClass + * @tc.desc aceNapiEtsTest + */ + + it('napiDefineClass006', 0, async function (done) { + console.info('napiDefineClass006 START'); + value = napitest.napiDefineClass(); + console.info('napiDefineClass testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0700 + * @tc.name napiCancelAsyncWork + * @tc.desc aceNapiEtsTest + */ + it('napiCancelAsyncWork007', 0, async function (done) { + console.info('napiCancelAsyncWork007 START'); + value = napitest.napiCancelAsyncWork(); + console.info('napiCancelAsyncWork testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0900 + * @tc.name napiRunScriptPath + * @tc.desc aceNapiEtsTest + */ + it('napiRunScriptPath009', 0, async function (done) { + console.info('napiRunScriptPath START'); + value = napitest.napiRunScriptPath(); + console.info('napiRunScriptPath testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0100 + * @tc.name napiCallThreadsafeFunction + * @tc.desc aceNapiEtsTest + */ + it('napiCallThreadsafeFunction010', 0, async function (done) { + console.info('napiCallThreadsafeFunction START'); + value = napitest.napiCallThreadsafeFunction(); + console.info('napiCallThreadsafeFunction testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0011 + * @tc.name napiCreateThreadsafeFunction + * @tc.desc aceNapiEtsTest + */ + it('napiCreateThreadsafeFunction011', 0, async function (done) { + console.info('napiCreateThreadsafeFunction START'); + value = napitest.napiCreateThreadsafeFunction(); + console.info('napiCreateThreadsafeFunction testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0012 + * @tc.name napiRefthreadSafeFunction + * @tc.desc aceNapiEtsTest + */ + it('napiRefthreadSafeFunction012', 0, async function (done) { + console.info('napiRefthreadSafeFunction START'); + value = napitest.napiRefthreadSafeFunction(); + console.info('napiRefthreadSafeFunction testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0013 + * @tc.name napiCreateDate + * @tc.desc aceNapiEtsTest + */ + it('napiCreateDate013', 0, async function (done) { + console.info('napiCreateDate START'); + value = napitest.napiCreateDate(); + console.info('napiCreateDate testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(1) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0014 + * @tc.name napiCreateBigintUint64 + * @tc.desc aceNapiEtsTest + */ + it('napiCreateBigintUint64014', 0, async function (done) { + console.info('napiCreateBigintUint64 START'); + value = napitest.napiCreateBigintUint64(); + console.info('napiCreateBigintUint64 testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(1) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0015 + * @tc.name napiCreateBigintInt64 + * @tc.desc aceNapiEtsTest + */ + it('napiCreateBigintInt64015', 0, async function (done) { + console.info('napiCreateBigintInt64 START'); + value = napitest.napiCreateBigintInt64(); + console.info('napiCreateBigintInt64 testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(1) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0016 + * @tc.name napiCreateBigintWords + * @tc.desc aceNapiEtsTest + */ + it('napiCreateBigintWords016', 0, async function (done) { + console.info('napiCreateBigintWords START'); + value = napitest.napiCreateBigintWords(); + console.info('napiCreateBigintWords testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0017 + * @tc.name napiFatalerror + * @tc.desc aceNapiEtsTest + */ + it('napiFatalerror017', 0, async function (done) { + console.info('napiFatalerror START'); + value = napitest.napiFatalerror(); + console.info('napiFatalerror testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(0) + done(); + }); + }) } -- GitLab