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 dbdad2cbbebfac8bdde3089dcbf8f68ca59114d1..3b5af69d2fe768a9b505467479b8f3e8dab8dd4e 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 @@ -669,6 +669,33 @@ static napi_value createAndGetStringUtf8(napi_env env, napi_callback_info info) return output; } +static napi_value StringUtf8OfLengthLeZero(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + NAPI_ASSERT(env, argc == 1, "Expects one argument."); + + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, args[0], &valueType)); + + NAPI_ASSERT(env, valueType == napi_string, + "Wrong type of argment. Expects a string."); + + char buffer[128]; + size_t bufferSize = 128; + size_t copied = 0; + + NAPI_CALL(env, + napi_get_value_string_utf8(env, args[0], buffer, bufferSize, &copied)); + + napi_value output; + NAPI_CALL(env, napi_create_string_utf8(env, buffer, -1, &output)); + + return output; +} + static napi_value CreateAndGetStringUtf16(napi_env env, napi_callback_info info) { size_t argc = 1; @@ -684,7 +711,7 @@ static napi_value CreateAndGetStringUtf16(napi_env env, napi_callback_info info) char16_t buffer[128]; // 128: char16_t type of element size size_t bufferSize = 128; // 128: char16_t type of element size - size_t copied = 0; + size_t copied = 0; NAPI_CALL(env, napi_get_value_string_utf16(env, args[0], buffer, bufferSize, &copied)); @@ -720,6 +747,52 @@ static napi_value StringUtf16OfCase(napi_env env, napi_callback_info info) return result; } +static napi_value StringUtf16OfLengthLeZero(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + NAPI_ASSERT(env, argc == 1, "Expects one argument."); + + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, args[0], &valueType)); + + NAPI_ASSERT(env, valueType == napi_string, "Expects a string."); + + char16_t buffer[128]; + size_t bufferSize = 128; + size_t copied = 0; + + NAPI_CALL(env, + napi_get_value_string_utf16(env, args[0], buffer, bufferSize, &copied)); + + napi_value result; + NAPI_CALL(env, napi_create_string_utf16(env, buffer, -1, &result)); + + return result; +} + +static napi_value StringUtf16OfLengthEqOne(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + NAPI_ASSERT(env, argc == 1, "Expects one argument."); + + char16_t buffer[128]; + size_t bufferSize = 1; + size_t copied = 1; + + NAPI_CALL(env, + napi_get_value_string_utf16(env, args[0], buffer, bufferSize, &copied)); + + napi_value value; + NAPI_CALL(env, napi_create_int32(env, copied, &value)); + return value; +} + static const napi_type_tag typeTags[NUMBER_FIVE] = { {0xdaf987b3cc62481a, 0xb745b0497f299531}, {0xbb7936c374084d9b, 0xa9548d0762eeedb9}, @@ -825,7 +898,7 @@ static napi_value getTypedArrayInfo(napi_env env, napi_callback_info info) size_t byteOffset = -1; NAPI_CALL(env, napi_get_typedarray_info(env, result, &type, &length, &data, &retArrayBuffer, &byteOffset)); NAPI_ASSERT(env, type == napi_int32_array, "napi_get_typedarray_info success 0"); - NAPI_ASSERT(env, length == arrayBufferSize, "napi_get_typedarray_info success 1"); + NAPI_ASSERT(env, length == typedArrayLength, "napi_get_typedarray_info success 1"); NAPI_ASSERT(env, data == arrayBufferPtr, "napi_get_dataview_info success 2"); bool retIsArrayBuffer = false; @@ -1556,6 +1629,21 @@ static napi_value MyConstructor(napi_env env, napi_callback_info info) return thisVar; } +static napi_value NewTargetTest(napi_env env, napi_callback_info info) +{ + bool isConstructor = true; + napi_value constructor = nullptr; + napi_get_new_target(env, info, &constructor); + if (constructor == nullptr) { + napi_throw_error(env, nullptr, "is not new instance"); + isConstructor = false; + } + napi_value value; + NAPI_CALL(env, napi_get_boolean(env, isConstructor, &value)); + + return value; +} + static napi_value wrap(napi_env env, napi_callback_info info) { napi_value testClass = nullptr; @@ -1749,6 +1837,60 @@ static napi_value TestLatin1(napi_env env, napi_callback_info info) return output; } + +static napi_value TestUtf8(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + NAPI_ASSERT(env, argc == 1, "Expects one argument."); + + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); + + NAPI_ASSERT(env, valuetype == napi_string, + "Wrong type of argment. Expects a string."); + + char buffer[128]; + size_t bufferSize = 128; + size_t copied; + + NAPI_CALL(env, + napi_get_value_string_utf8(env, args[0], buffer, bufferSize, &copied)); + + napi_value output; + NAPI_CALL(env, napi_create_string_utf8(env, buffer, copied, &output)); + + return output; +} + +static napi_value TestUtf16(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + + NAPI_ASSERT(env, argc == 1, "Expects one argument."); + + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); + + NAPI_ASSERT(env, valuetype == napi_string, + "Wrong type of argment. Expects a string."); + + char16_t buffer[128]; + size_t bufferSize = 128; + size_t copied; + + NAPI_CALL(env, + napi_get_value_string_utf16(env, args[0], buffer, bufferSize, &copied)); + + napi_value output; + NAPI_CALL(env, napi_create_string_utf16(env, buffer, copied, &output)); + + return output; +} // test the napi function static napi_value napCreateArrayBuffer(napi_env env, napi_callback_info info) { @@ -2028,6 +2170,24 @@ static napi_value napiCreateFunction(napi_env env, napi_callback_info info) return funcValue; } +static napi_value NapiCreateFunctionTwo(napi_env env, napi_callback_info info) +{ + napi_value resultValue = nullptr; + napi_status status = napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, SayHello, nullptr, &resultValue); + if (resultValue == nullptr) { + napi_throw_error(env, nullptr, "napi_create_function fail"); + return nullptr; + } + if (status != napi_ok) { + napi_throw_error(env, nullptr, "napi_create_function fail"); + return nullptr; + } + napi_value value; + NAPI_CALL(env, napi_create_int32(env, 1, &value)); + + return resultValue; +} + static napi_value napiRefthreadSafeFunction(napi_env env, napi_callback_info info) { napi_threadsafe_function tsFunc = nullptr; @@ -2706,6 +2866,8 @@ 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("TestUtf16", TestUtf16), + DECLARE_NAPI_FUNCTION("TestUtf8", TestUtf8), DECLARE_NAPI_FUNCTION("runScript", runScript), DECLARE_NAPI_FUNCTION("getUvEventLoop", getUvEventLoop), DECLARE_NAPI_FUNCTION("napCreateArrayBuffer", napCreateArrayBuffer), @@ -2729,6 +2891,7 @@ static napi_value Init(napi_env env, napi_value exports) { "testAsyncWork", nullptr, testAsyncWork, nullptr, nullptr, nullptr, napi_default, nullptr }, { "testPromise", nullptr, testPromise, nullptr, nullptr, nullptr, napi_default, nullptr }, { "napiCreateFunction", nullptr, napiCreateFunction, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "NapiCreateFunctionTwo", nullptr, NapiCreateFunctionTwo, nullptr, nullptr, nullptr, napi_default, nullptr }, DECLARE_NAPI_FUNCTION("getGlobal", getGlobal), DECLARE_NAPI_FUNCTION("callFunction", callFunction), DECLARE_NAPI_FUNCTION("ThreadSafeTest", ThreadSafeTest), @@ -2749,6 +2912,10 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("External", External), DECLARE_NAPI_FUNCTION("DetachTest", DetachTest), DECLARE_NAPI_FUNCTION("IsDetachedTest", IsDetachedTest), + DECLARE_NAPI_FUNCTION("stringUtf16OfLengthLeZero", StringUtf16OfLengthLeZero), + DECLARE_NAPI_FUNCTION("stringUtf16OfLengthEqOne", StringUtf16OfLengthEqOne), + DECLARE_NAPI_FUNCTION("stringUtf8OfLengthLeZero", StringUtf8OfLengthLeZero), + DECLARE_NAPI_FUNCTION("NewTargetTest", NewTargetTest), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); 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 d2e374f0c51ef1a98d26e0b051dc30888ae13a23..7b0cc2b6da36705a4d4b3042834b90e44b2bd968 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 @@ -670,9 +670,10 @@ export default function nativeApiStringJsunit() { */ it('aceNapiTest047', 0, async function (done) { console.info('aceNapiTest047 START'); - value = napitest.coerceToString(123); + let number = 123; + value = napitest.coerceToString(number); console.info('aceNapiTest047 testString result is: ' + value + "--" + JSON.stringify(value)); - expect(value).assertEqual('123'); + expect(value).assertEqual(number.toString()); done(); }); @@ -1135,9 +1136,9 @@ export default function nativeApiStringJsunit() { expect(result < 0).assertTrue(); done(); } - ); + ); }); - + /** * @tc.number SUB_ACE_BASIC_ETS_NAPI_0080 * @tc.name aceNapiTest080 @@ -1151,7 +1152,7 @@ export default function nativeApiStringJsunit() { expect(value === 1).assertTrue(); done(); }); - + /** * @tc.number SUB_ACE_BASIC_ETS_NAPI_0081 * @tc.name aceNapiTest081 @@ -1365,7 +1366,10 @@ export default function nativeApiStringJsunit() { console.info('aceNapiTest094 START'); let valueeEmpty = napitest.createAndGetStringUtf16(' '); console.info('aceNapiTest094 testString result is: ' + valueeEmpty + "--" + JSON.stringify(valueeEmpty)); + let value = napitest.createAndGetStringUtf16('中文'); + console.info('aceNapiTest094 testString result is: ' + value + "--" + JSON.stringify(value)); expect(valueeEmpty).assertEqual(' '); + expect(value).assertEqual('中文'); done(); }); @@ -1423,5 +1427,247 @@ export default function nativeApiStringJsunit() { expect(isTag4).assertFalse(); done(); }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0097 + * @tc.name aceNapiTest097 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest097', 0, async function (done) { + console.info('aceNapiTest097 START'); + value = napitest.coerceToString('String Test'); + console.info('aceNapiTest097 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('String Test'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0098 + * @tc.name aceNapiTest098 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest098', 0, async function (done) { + console.info('aceNapiTest098 START'); + value = napitest.coerceToString(false); + console.info('aceNapiTest098 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('false'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0099 + * @tc.name aceNapiTest099 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest099', 0, async function (done) { + console.info('aceNapiTest099 START'); + value = napitest.coerceToString(undefined); + console.info('aceNapiTest099 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('undefined'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0100 + * @tc.name aceNapiTest100 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest100', 0, async function (done) { + console.info('aceNapiTest100 START'); + value = napitest.coerceToString(null); + console.info('aceNapiTest100 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('null'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0101 + * @tc.name aceNapiTest101 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest101', 0, async function (done) { + console.info('aceNapiTest101 START'); + value = napitest.coerceToString(true); + console.info('aceNapiTest101 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('true'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0102 + * @tc.name aceNapiTest102 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest102', 0, async function (done) { + console.info('aceNapiTest102 START'); + let bigintNum = napitest.napiCreateBigintInt64(); + value = napitest.coerceToString(bigintNum); + console.info('aceNapiTest102 testString result is: ' + JSON.stringify(bigintNum)); + console.info('aceNapiTest102 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual(bigintNum.toString()); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0103 + * @tc.name aceNapiTest103 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest103', 0, async function (done) { + console.info('aceNapiTest103 START'); + const arr = [1, 2, 3]; + value = napitest.coerceToString(arr); + console.info('aceNapiTest103 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual(arr.toString()); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0104 + * @tc.name aceNapiTest104 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest104', 0, async function (done) { + console.info('aceNapiTest104 START'); + let value = napitest.stringUtf16OfLengthLeZero('string utf16 test'); + console.info('aceNapiTest104 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('string utf16 test'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0105 + * @tc.name aceNapiTest105 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest105', 0, async function (done) { + console.info('aceNapiTest105 START'); + let copied = napitest.stringUtf16OfLengthEqOne('string utf16 test1'); + console.info('aceNapiTest105 testString result is: ' + copied + "--" + JSON.stringify(copied)); + expect(copied).assertEqual(0); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0106 + * @tc.name aceNapiTest106 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest106', 0, async function (done) { + console.info('aceNapiTest106 START'); + let value = napitest.stringUtf8OfLengthLeZero('string utf8 test'); + console.info('aceNapiTest106 testString result is: ' + value + "--" + JSON.stringify(value)); + expect(value).assertEqual('string utf8 test'); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0107 + * @tc.name aceNapiTest107 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest107', 0, async function (done) { + console.info('aceNapiTest107 START'); + function InputString() { + const boolInput = true; + napitest.TestUtf8(boolInput); + } + expect(InputString).assertThrowError( + "assertion (valuetype == napi_string) failed: Wrong type of argment. Expects a string."); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0108 + * @tc.name aceNapiTest108 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest108', 0, async function (done) { + console.info('aceNapiTest108 START'); + function InputString() { + const numberInput = 123; + napitest.TestUtf16(numberInput); + } + expect(InputString).assertThrowError( + "assertion (valuetype == napi_string) failed: Wrong type of argment. Expects a string."); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0109 + * @tc.name aceNapiTest109 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest109', 0, async function (done) { + console.info('aceNapiTest109 START'); + function InputString() { + const numberInput = 123; + napitest.TestLatin1(numberInput); + } + expect(InputString).assertThrowError( + "assertion (valuetype == napi_string) failed: Wrong type of argment. Expects a string."); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0110 + * @tc.name aceNapiTest110 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest110', 0, async function (done) { + console.info('aceNapiTest110 START'); + let func = napitest.NapiCreateFunctionTwo(); + value = func(); + console.info('aceNapiTest111 testString result is: ' + JSON.stringify(value)); + expect(value).assertEqual(666); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0111 + * @tc.name aceNapiTest111 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest111', 0, async function (done) { + console.info('aceNapiTest111 START'); + function testFunc() { + napitest.NewTargetTest(); + } + expect(testFunc).assertThrowError("InstanceOf error when type of target is not Object"); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0112 + * @tc.name aceNapiTest112 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest112', 0, async function (done) { + console.info('aceNapiTest112 START'); + function testFunc() { + throw new Error('error'); + } + function testCall() { + napitest.callFunction(testFunc); + } + expect(testCall).assertThrowError("error"); + done(); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0113 + * @tc.name aceNapiTest113 + * @tc.desc aceNapiEtsTest + */ + it('aceNapiTest113', 0, async function (done) { + console.info('aceNapiTest113 START'); + function testFunc() { + return 'hello'; + } + value = napitest.callFunction(testFunc); + console.info('aceNapiTest114 testString result is: ' + JSON.stringify(value)); + expect(value.toString() === 'hello').assertTrue(); + done(); + }); }) }