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 77fd66c871f783cd6bb66fe4b01bfec0c33fab68..65d3e24a17506ec5d4657ddbf5023f7bd4aa1c7c 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 @@ -15,6 +15,7 @@ #include "common/native_common.h" #include "napi/native_api.h" +#include "securec.h" #include #include #include @@ -27,6 +28,7 @@ static napi_ref test_reference = NULL; const int TAG_NUMBER = 666; const int NUMBER_FIVE = 5; +static int g_delCount = 0; static void add_returned_status(napi_env env, const char* key, @@ -2197,10 +2199,445 @@ static napi_value ThreadSafeTest(napi_env env, napi_callback_info info) { return _value; } +static void NoopDeleter(napi_env env, void* data, void* finalizeHint) +{ + (void)finalizeHint; + g_delCount++; +} + +static const char TEST_STR[] = + "Where there is a will, there is a way."; + +static void DelTest(napi_env env, void* data, void* finalizeHint) +{ + (void)finalizeHint; + free(data); + g_delCount++; +} + +static napi_value CreateBuffer(napi_env env, napi_callback_info info) +{ + const unsigned int bufferSize = sizeof(TEST_STR); + char* copyPtr; + napi_value napiBuffer; + napi_status status = napi_create_buffer(env, bufferSize, (void**)(©Ptr), &napiBuffer); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to create buffer"); + return nullptr; + } + NAPI_ASSERT(env, copyPtr, "Unable to duplicate static text for CreateBuffer."); + int ret = memcpy_s(copyPtr, bufferSize, TEST_STR, strlen(TEST_STR) + 1); + NAPI_ASSERT(env, ret == 0, "memcpy_s failed"); + return napiBuffer; +} + +static napi_value CreateExternalBuffer(napi_env env, napi_callback_info info) +{ + char* copyPtr = strdup(TEST_STR); + napi_value napiBuffer; + const unsigned int bufferSize = sizeof(TEST_STR); + + NAPI_ASSERT(env, copyPtr, "Unable to duplicate static text for CreateExternalBuffer."); + NAPI_CALL(env, + napi_create_external_buffer(env, + bufferSize, + copyPtr, + DelTest, + nullptr /* finalizeHint */, + &napiBuffer)); + return napiBuffer; +} + +static napi_value BufferCopy(napi_env env, napi_callback_info info) +{ + const unsigned int bufferSize = sizeof(TEST_STR); + napi_value napiBuffer; + void* dataPtr = nullptr; + napi_status status = napi_create_buffer_copy(env, bufferSize, TEST_STR, &dataPtr, &napiBuffer); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to create buffer"); + return nullptr; + } + return napiBuffer; +} + +static napi_value IsBuffer(napi_env env, napi_callback_info info) +{ + napi_value args[1]; + size_t argc = 1; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + NAPI_ASSERT(env, argc == 1, "The number of arguments provided is incorrect."); + napi_value napiBuffer = args[0]; + bool result; + napi_status status = napi_is_buffer(env, napiBuffer, &result); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "The parameter instance is not of type buffer."); + } + napi_value returnValue; + NAPI_CALL(env, napi_get_boolean(env, result, &returnValue)); + return returnValue; +} + +static napi_value GetBufferInfo(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + const unsigned int bufferSize = sizeof(TEST_STR); + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + NAPI_ASSERT(env, argc == 1, "Incorrect number of parameters."); + napi_value napiBuffer = args[0]; + char *bufferData; + napi_value returnValue; + size_t bufferLength; + if (napi_get_buffer_info(env, napiBuffer, (void**)(&bufferData), &bufferLength) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to get buffer info."); + return nullptr; + } + NAPI_CALL(env, napi_get_boolean(env, + !strcmp(bufferData, TEST_STR) && bufferLength == bufferSize, + &returnValue)); + return returnValue; +} + +static napi_value StaticBuffer(napi_env env, napi_callback_info info) +{ + napi_value napiBuffer; + const unsigned int bufferSize = sizeof(TEST_STR); + NAPI_CALL(env, + napi_create_external_buffer(env, + bufferSize, + (void*)TEST_STR, + NoopDeleter, + nullptr /* finalizeHint */, + &napiBuffer)); + return napiBuffer; +} + +static napi_value GetSymbolNames(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + if (argc < 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + napi_valuetype valueType0; + NAPI_CALL(env, napi_typeof(env, args[0], &valueType0)); + if (valueType0 != napi_object) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an object as first argument."); + return nullptr; + } + napi_value output; + NAPI_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_include_prototypes, + napi_key_skip_strings, + napi_key_numbers_to_strings, + &output)); + return output; +} + +static napi_value GetEnumerableWritableNames(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + if (argc < 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + napi_valuetype valueType0; + if (napi_typeof(env, args[0], &valueType0) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to get argument type"); + return nullptr; + } + if (valueType0 != napi_object) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an object as first argument."); + return nullptr; + } + napi_value output; + NAPI_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_include_prototypes, + static_cast(napi_key_writable | napi_key_enumerable), + napi_key_numbers_to_strings, + &output)); + return output; +} + +static napi_value GetOwnWritableNames(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + if (argc < 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + napi_valuetype valueType0; + if (napi_typeof(env, args[0], &valueType0) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to get argument type"); + return nullptr; + } + if (valueType0 != napi_object) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an object as first argument."); + return nullptr; + } + napi_value output; + NAPI_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_own_only, + napi_key_writable, + napi_key_numbers_to_strings, + &output)); + return output; +} + +static napi_value GetEnumerableConfigurableNames(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + if (argc < 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + napi_valuetype valueType0; + if (napi_typeof(env, args[0], &valueType0) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to get argument type"); + return nullptr; + } + if (valueType0 != napi_object) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an object as first argument."); + return nullptr; + } + napi_value output; + NAPI_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_include_prototypes, + static_cast(napi_key_enumerable | napi_key_configurable), + napi_key_numbers_to_strings, + &output)); + return output; +} + +static napi_value GetOwnConfigurableNames(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to parse arguments"); + return nullptr; + } + if (argc < 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments"); + return nullptr; + } + napi_valuetype valueType0; + if (napi_typeof(env, args[0], &valueType0) != napi_ok) { + napi_throw_error(env, nullptr, "Failed to get argument type"); + return nullptr; + } + if (valueType0 != napi_object) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an object as first argument."); + return nullptr; + } + napi_value output; + NAPI_CALL(env, + napi_get_all_property_names(env, + args[0], + napi_key_own_only, + napi_key_configurable, + napi_key_numbers_to_strings, + &output)); + return output; +} + +static napi_value GetAllPropertyNames(napi_env env, napi_callback_info info) +{ + napi_value returnValue, props; + NAPI_CALL(env, napi_create_object(env, &returnValue)); + add_returned_status(env, + "envIsNull", + returnValue, + "Invalid argument", + napi_invalid_arg, + napi_get_all_property_names(nullptr, + returnValue, + napi_key_own_only, + napi_key_writable, + napi_key_keep_numbers, + &props)); + napi_get_all_property_names(env, + nullptr, + napi_key_own_only, + napi_key_writable, + napi_key_keep_numbers, + &props); + add_last_status(env, "objectIsNull", returnValue); + napi_get_all_property_names(env, + returnValue, + napi_key_own_only, + napi_key_writable, + napi_key_keep_numbers, + nullptr); + add_last_status(env, "valueIsNull", returnValue); + return returnValue; +} + +static napi_value FreezeTest(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)); + + // Check if argument is an object + napi_value objectConstructor; + napi_status status = napi_get_named_property(env, args[0], "Object", &objectConstructor); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Argument must be an object"); + } + // Freeze the object + napi_value object = args[0]; + NAPI_CALL(env, napi_object_freeze(env, object)); + return object; +} + +static napi_value SealTest(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)); + + // Check if argument is an object + napi_value objectConstructor; + napi_status status = napi_get_named_property(env, args[0], "Object", &objectConstructor); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Argument must be an object"); + } + // Seal the object + napi_value object = args[0]; + NAPI_CALL(env, napi_object_seal(env, object)); + + return object; +} + +void FinalizeCallback(napi_env env, void* finalizeData, void* finalizeHint) +{ + free(finalizeData); +} + +static napi_value External(napi_env env, napi_callback_info info) +{ + const uint8_t parraySize = 3; + void* externalData = malloc(parraySize * sizeof(int8_t)); + + //Sets the three elements of the array that are used to create an ArrayBuffer object + ((int8_t*)externalData)[0] = 0; // 0 means that the first value of the created array is 0 + ((int8_t*)externalData)[1] = 1; // 1 means that the second value of the created array is 1 + ((int8_t*)externalData)[2] = 2; // 2 means that the third value of the created array is 2 + + napi_value outputBuffer; + napi_status status = napi_create_external_arraybuffer(env, + externalData, + parraySize * sizeof(int8_t), + FinalizeCallback, + nullptr, // finalizeHint + &outputBuffer); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to create external arraybuffer"); + return nullptr; + } + napi_value outputArray; + status = napi_create_typedarray(env, + napi_int8_array, + parraySize, + outputBuffer, + 0, + &outputArray); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to create typedarray"); + return nullptr; + } + return outputArray; +} + +static napi_value DetachTest(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, "The number of arguments provided is incorrect."); + void* data; + size_t length; + napi_typedarray_type type; + napi_value arrayBuffer; + NAPI_CALL(env, napi_get_typedarray_info(env, args[0], &type, &length, &data, &arrayBuffer, nullptr)); + + NAPI_CALL(env, napi_detach_arraybuffer(env, arrayBuffer)); + return nullptr; +} + +napi_value IsDetachedTest(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1]; + napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (status != napi_ok || argc != 1) { + napi_throw_error(env, nullptr, "Wrong number of arguments."); + return nullptr; + } + bool isArraybuffer; + status = napi_is_arraybuffer(env, args[0], &isArraybuffer); + if (status != napi_ok || !isArraybuffer) { + napi_throw_error(env, nullptr, "Wrong type of arguments. Expects an array buffer as first argument."); + return nullptr; + } + bool isDetached; + status = napi_is_detached_arraybuffer(env, args[0], &isDetached); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to check if array buffer is detached."); + return nullptr; + } + napi_value result; + status = napi_get_boolean(env, isDetached, &result); + if (status != napi_ok) { + napi_throw_error(env, nullptr, "Failed to create boolean result."); + return nullptr; + } + return result; +} + EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { + napi_value theValue; + NAPI_CALL(env, napi_create_string_utf8(env, TEST_STR, sizeof(TEST_STR), &theValue)); + NAPI_CALL(env, napi_set_named_property(env, exports, "testStr", theValue)); napi_property_descriptor properties[] = { DECLARE_NAPI_FUNCTION("getLastErrorInfo", getLastErrorInfo), DECLARE_NAPI_FUNCTION("cleanUpErrorInfo", cleanUpErrorInfo), @@ -2298,6 +2735,23 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getGlobal", getGlobal), DECLARE_NAPI_FUNCTION("callFunction", callFunction), DECLARE_NAPI_FUNCTION("ThreadSafeTest", ThreadSafeTest), + DECLARE_NAPI_FUNCTION("CreateBuffer", CreateBuffer), + DECLARE_NAPI_FUNCTION("CreateExternalBuffer", CreateExternalBuffer), + DECLARE_NAPI_FUNCTION("BufferCopy", BufferCopy), + DECLARE_NAPI_FUNCTION("IsBuffer", IsBuffer), + DECLARE_NAPI_FUNCTION("GetBufferInfo", GetBufferInfo), + DECLARE_NAPI_FUNCTION("GetAllPropertyNames", GetAllPropertyNames), + DECLARE_NAPI_FUNCTION("GetSymbolNames", GetSymbolNames), + DECLARE_NAPI_FUNCTION("GetEnumerableWritableNames", GetEnumerableWritableNames), + DECLARE_NAPI_FUNCTION("GetOwnWritableNames", GetOwnWritableNames), + DECLARE_NAPI_FUNCTION("GetEnumerableConfigurableNames", GetEnumerableConfigurableNames), + DECLARE_NAPI_FUNCTION("GetOwnConfigurableNames", GetOwnConfigurableNames), + DECLARE_NAPI_FUNCTION("FreezeTest", FreezeTest), + DECLARE_NAPI_FUNCTION("SealTest", SealTest), + DECLARE_NAPI_FUNCTION("StaticBuffer", StaticBuffer), + DECLARE_NAPI_FUNCTION("External", External), + DECLARE_NAPI_FUNCTION("DetachTest", DetachTest), + DECLARE_NAPI_FUNCTION("IsDetachedTest", IsDetachedTest), }; 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/List.test.ets b/arkui/ace_napi_test/entry/src/main/ets/test/List.test.ets index 43dca6e7d2672a3e8733b52002b4e22020060609..c66e6fc7f6bc638fab09002c833ff5677520843d 100644 --- a/arkui/ace_napi_test/entry/src/main/ets/test/List.test.ets +++ b/arkui/ace_napi_test/entry/src/main/ets/test/List.test.ets @@ -13,7 +13,14 @@ * limitations under the License. */ import napiStringTest from './NativeApiStringTest'; +import napiObjectTest from './NativeApiObjectTest'; +import napiArrayBufferTest from './NativeApiArrayBufferTest'; +import napiBufferTest from './NativeApiBufferTest'; + export default function testsuite() { - napiStringTest() -} \ No newline at end of file + napiStringTest(); + napiArrayBufferTest(); + napiBufferTest(); + napiObjectTest(); +} diff --git a/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiArrayBufferTest.ets b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiArrayBufferTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..84479465ccc50ddf4af3628b84dfc3126227a13b --- /dev/null +++ b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiArrayBufferTest.ets @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from "hypium/index" + +// @ts-ignore +import napitest from 'libnapitest.so' + +export default function napiArrayBufferTest() { + + describe('napiArrayBufferTest', function () { + + /** + * run after testcase + */ + afterEach(async function () { + console.info('[napiArrayBufferTest] after each called') + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0001 + * @tc.name napiArrayBufferTest001 + * @tc.desc aceNapiEtsTest + */ + it('napiArrayBufferTest001', 0, function () { + const buffer = napitest.External(); + expect(!napitest.IsDetachedTest(buffer.buffer)).assertTrue(); + }) + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0002 + * @tc.name napiArrayBufferTest002 + * @tc.desc aceNapiEtsTest + */ + it('napiArrayBufferTest002', 0, function () { + const buffer = new ArrayBuffer(128); + expect(!napitest.IsDetachedTest(buffer)).assertTrue(); + }) + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0003 + * @tc.name napiArrayBufferTest003 + * @tc.desc aceNapiEtsTest + */ + it('napiArrayBufferTest003', 0, function () { + const buffer = napitest.External(); + expect(!napitest.IsDetachedTest(buffer.buffer)).assertTrue(); + napitest.DetachTest(buffer); + expect(napitest.IsDetachedTest(buffer.buffer)).assertTrue(); + }) + }) +} \ No newline at end of file diff --git a/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiBufferTest.ets b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiBufferTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..082472401eead4fd7c5e78afeddc0700390d2b67 --- /dev/null +++ b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiBufferTest.ets @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from "hypium/index" + +// @ts-ignore +import napitest from 'libnapitest.so' + +export default function napiBufferTest() { + + describe('napiBufferTest', function () { + + /** + * run after testcase + */ + afterEach(async function () { + console.info('[napiBufferTest] after each called') + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0001 + * @tc.name napiBufferTest001 + * @tc.desc aceNapiEtsTest + */ + it('napiBufferTest001', 0, function () { + expect(napitest.CreateBuffer().toString()).assertDeepEquals(napitest.testStr); + expect(napitest.CreateExternalBuffer().toString()).assertDeepEquals(napitest.testStr); + }) + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0001 + * @tc.name napiBufferTest001 + * @tc.desc aceNapiEtsTest + */ + it('napiBufferTest002', 0, function () { + let buffer = napitest.CreateBuffer(); + expect(napitest.IsBuffer(buffer)).assertTrue(); + expect(napitest.GetBufferInfo(buffer)).assertTrue(); + }) + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0001 + * @tc.name napiBufferTest001 + * @tc.desc aceNapiEtsTest + */ + it('napiBufferTest003', 0, function () { + expect(napitest.BufferCopy().toString()).assertDeepEquals(napitest.testStr); + }) + }) +} \ No newline at end of file diff --git a/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiObjectTest.ets b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiObjectTest.ets new file mode 100644 index 0000000000000000000000000000000000000000..66696aaa71662209620b11b6bfa9c5f9a9523da9 --- /dev/null +++ b/arkui/ace_napi_test/entry/src/main/ets/test/NativeApiObjectTest.ets @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from "hypium/index" + +// @ts-ignore +import napitest from 'libnapitest.so' + +function deepStrictEqualOfArray(obj1, obj2) { + expect(obj1).assertInstanceOf('Array'); + expect(obj2).assertInstanceOf('Array'); + expect(obj1.length).assertEqual(obj2.length); + for (let i = 0; i < obj1.length; i++) { + if ((obj1[i] instanceof Symbol) && (obj2[i] instanceof Symbol)) { + expect(obj1[i].toString()).assertEqual(obj2[i].toString()); + } else { + expect(obj1[i] === obj2[i]).assertTrue(); + } + } +} + +function deepStrictEqualOfObject(obj1, obj2) { + deepStrictEqualOfArray(Object.keys(obj1), Object.keys(obj2)); + for(let key in obj1){ + expect(obj1[key]).assertEqual(obj2[key]); + } +} + +function addSeal() { + const obj = { x: 'a', y: 'b', z: 'c' }; + napitest.SealTest(obj); + // @ts-ignore + obj.w = 'd'; +} + +function deleteSeal() { + const obj = { x: 'a', y: 'b', z: 'c' }; + napitest.SealTest(obj); + // @ts-ignore + delete obj.x; +} + +function assignFreeze() { + const obj = { x: 10, y: 10, z: 10 }; + napitest.FreezeTest(obj); + // @ts-ignore + obj.x = 10; +} + +function addFreeze() { + const obj = { x: 10, y: 10, z: 10 }; + napitest.FreezeTest(obj); + // @ts-ignore + obj.w = 15; +} + +function deleteFreeze() { + const obj = { x: 10, y: 10, z: 10 }; + napitest.FreezeTest(obj); + // @ts-ignore + delete obj.x; +} + +const fooSymbol = Symbol('foo'); + +function createObject() { + const object = { __proto__: { + inherited: 1, + } + }; + // @ts-ignore + object.normal = 2; + object[fooSymbol] = 3; + Object.defineProperty(object, + 'unenumerable', + { + value: 4, + enumerable: false, + writable: true, + configurable: true, + }); + Object.defineProperty(object, + 'writable', + { + value: 4, + enumerable: true, + writable: true, + configurable: false, + }); + Object.defineProperty(object, + 'configurable', + { + value: 4, + enumerable: true, + writable: false, + configurable: true, + }); + object[5] = 5; + return object; +} + +export default function npiObjectTest() { + + describe('npiObjectTest', function () { + + /** + * run after testcase + */ + afterEach(async function () { + console.info('[npiObjectTest] after each called') + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0001 + * @tc.name napiObjectTest001 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest001', 0, function() { + const testObject = createObject(); + deepStrictEqualOfArray(napitest.GetSymbolNames(testObject),[fooSymbol]); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0002 + * @tc.name napiObjectTest002 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest002', 0, function() { + const testObject = createObject(); + deepStrictEqualOfArray(napitest.GetEnumerableWritableNames(testObject), + ['5', + 'normal', + 'writable', + fooSymbol, + 'inherited']); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0003 + * @tc.name napiObjectTest003 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest003', 0, function() { + const testObject = createObject(); + deepStrictEqualOfArray(napitest.GetOwnWritableNames(testObject), + ['5', + 'normal', + 'unenumerable', + 'writable', + fooSymbol]); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0004 + * @tc.name napiObjectTest004 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest004', 0, function() { + const testObject = createObject(); + deepStrictEqualOfArray(napitest.GetEnumerableConfigurableNames(testObject), + ['5', + 'normal', + 'configurable', + fooSymbol, + 'inherited']); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0005 + * @tc.name napiObjectTest005 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest005', 0, function() { + const testObject = createObject(); + deepStrictEqualOfArray(napitest.GetOwnConfigurableNames(testObject), + ['5', + 'normal', + 'unenumerable', + 'configurable', + fooSymbol]); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0006 + * @tc.name napiObjectTest006 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest006', 0, function() { + const expectedForElement = { + envIsNull: 'Invalid argument', + objectIsNull: 'Invalid parameter', + valueIsNull: 'Invalid parameter', + }; + deepStrictEqualOfObject(napitest.GetAllPropertyNames(), expectedForElement); + }); + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0007 + * @tc.name napiObjectTest007 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest007', 0, function () { + const obj = { + x: 10, + y: 10, + z: 10, + }; + napitest.FreezeTest(obj); + expect(Object.isFrozen(obj)).assertDeepEquals(true); + expect(assignFreeze).assertThrowError("Cannot set readonly property"); + expect(addFreeze).assertThrowError("Cannot add property in prevent extensions "); + expect(deleteFreeze).assertThrowError("Cannot delete property"); + }) + + /** + * @tc.number SUB_ACE_BASIC_ETS_NAPI_0008 + * @tc.name napiObjectTest008 + * @tc.desc aceNapiEtsTest + */ + it('napiObjectTest008', 0, function() { + const obj = { + x: 'a', + y: 'b', + z: 'c', + }; + napitest.SealTest(obj); + expect(Object.isSealed(obj)).assertDeepEquals(true); + expect(addSeal).assertThrowError("Cannot add property in prevent extensions "); + expect(deleteSeal).assertThrowError("Cannot delete property"); + obj.x = 'd'; + }) + }) +} \ No newline at end of file