From da648aa5ca273c02a6dc313f1c4a71877d06f0c6 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Thu, 11 Apr 2024 16:12:47 +0800 Subject: [PATCH 1/2] feature: add javascriptApi jsProperty demo: napi get/set/has/delete element Signed-off-by: gou-jingjing --- .../entry/src/main/cpp/CMakeLists.txt | 4 + .../src/main/cpp/include/javascriptapi.h | 5 + .../src/main/cpp/javascriptapi/common.cpp | 31 +++++ .../jsproperty/jsPropertyInit.cpp | 4 + .../jsproperty/napideleteelement.cpp | 68 ++++++++++ .../jsproperty/napigetelement.cpp | 69 ++++++++++ .../jsproperty/napihaselement.cpp | 69 ++++++++++ .../jsproperty/napisetelement.cpp | 70 ++++++++++ .../src/main/cpp/types/libentry/index.d.ts | 4 + .../ets/pages/javascript/JavascriptApi.ets | 8 +- .../jsproperties/napideleteelement.ets | 107 +++++++++++++++ .../jsproperties/napigetelement.ets | 122 ++++++++++++++++++ .../jsproperties/napihaselement.ets | 107 +++++++++++++++ .../jsproperties/napisetelement.ets | 122 ++++++++++++++++++ .../main/resources/base/element/string.json | 16 +++ .../resources/base/profile/main_pages.json | 5 +- .../main/resources/en_US/element/string.json | 16 +++ .../main/resources/zh_CN/element/string.json | 16 +++ .../ets/test/JsProperty/JsProperty.test.ets | 86 +++++++++++- 19 files changed, 921 insertions(+), 8 deletions(-) create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napideleteelement.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetelement.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihaselement.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetelement.cpp create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteelement.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetelement.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihaselement.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetelement.ets diff --git a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt index 3963f545..694a7641 100644 --- a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt +++ b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt @@ -29,6 +29,10 @@ add_library(entry SHARED javascriptapi/jsproperty/napigetnamedproperty.cpp javascriptapi/jsproperty/napisetnamedproperty.cpp javascriptapi/jsproperty/napihasnamedproperty.cpp + javascriptapi/jsproperty/napisetelement.cpp + javascriptapi/jsproperty/napigetelement.cpp + javascriptapi/jsproperty/napihaselement.cpp + javascriptapi/jsproperty/napideleteelement.cpp javascriptapi/jsproperty/jsPropertyInit.cpp javascriptapi/common.cpp javascriptapi/jsabstractops/napicoercetobool.cpp diff --git a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h index 230e166d..d159636d 100644 --- a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h +++ b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h @@ -35,6 +35,11 @@ bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName napi_value testNapiSetNamedProperty(napi_env env, napi_callback_info info); napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info); napi_value testNapiHasNamedProperty(napi_env env, napi_callback_info info); +bool validateArrayObjProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag); +napi_value testNapiSetElement(napi_env env, napi_callback_info info); +napi_value testNapiGetElement(napi_env env, napi_callback_info info); +napi_value testNapiHasElement(napi_env env, napi_callback_info info); +napi_value testNapiDeleteElement(napi_env env, napi_callback_info info); napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info); napi_value testNapiCoerceToNumber(napi_env env, napi_callback_info info); diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp index 0263165d..7c9cfbae 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp @@ -63,3 +63,34 @@ bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName } return true; } + +bool validateArrayObjProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag) +{ + napi_status status; + napi_valuetype valuetype0; + napi_valuetype valuetype1; + const napi_extended_error_info *extended_error_info; + + // 确认第一个参数是个对象 + status = napi_typeof(env, obj, &valuetype0); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get obj type", tag); + return false; + } + if (valuetype0 != napi_object) { + napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); + return false; + } + + // 确认第二个参数是个数字 + status = napi_typeof(env, propName, &valuetype1); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get propName type", tag); + return false; + } + if (valuetype1 != napi_number) { + napi_throw_type_error(env, NULL, "Wrong argument type, expected a number"); + return false; + } + return true; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp index 1d521314..b1d6b61b 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp @@ -30,6 +30,10 @@ napi_value jsPropertyInit(napi_env env, napi_value exports) nullptr}, {"testNapiHasNamedProperty", nullptr, testNapiHasNamedProperty, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiSetElement", nullptr, testNapiSetElement, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiGetElement", nullptr, testNapiGetElement, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiHasElement", nullptr, testNapiHasElement, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiDeleteElement", nullptr, testNapiDeleteElement, nullptr, nullptr, nullptr, napi_default, nullptr}, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc); return exports; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napideleteelement.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napideleteelement.cpp new file mode 100644 index 00000000..2142fdaa --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napideleteelement.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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. + */ + +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiDeleteElement(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napihaselement + size_t argc = PARAM2; + napi_value argv[PARAM2]; + napi_status status; + napi_value arrayObj; + napi_value elementIndex; + const napi_extended_error_info *extended_error_info; + + // 解析传入的参数 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + + // 检查参数数量 + if (argc < PARAM2) { + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + arrayObj = argv[PARAM0]; + elementIndex = argv[PARAM1]; + + // 判断参数有效性 + bool resValid = validateArrayObjProperty(env, arrayObj, elementIndex, TAG); + if (resValid == false) { + return NULL; + } + + // 将第二个参数(索引)转换为本地 uint32_t + uint32_t index = 0; + status = napi_get_value_uint32(env, elementIndex, &index); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value uint32", TAG); + return NULL; + } + + bool delElement = false; + status = napi_delete_element(env, arrayObj, index, &delElement); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "has element", TAG); + return NULL; + } + + // 返回删除元素后的数组 + return arrayObj; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetelement.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetelement.cpp new file mode 100644 index 00000000..540e474c --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetelement.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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. + */ + +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiGetElement(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napisetelement + size_t argc = PARAM2; + napi_value argv[PARAM2]; + napi_status status; + napi_value arrayObj; + napi_value elementIndex; + const napi_extended_error_info *extended_error_info; + + // 解析传入的参数 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + + // 检查参数数量 + if (argc < PARAM2) { + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + arrayObj = argv[PARAM0]; + elementIndex = argv[PARAM1]; + + // 判断参数有效性 + bool resValid = validateArrayObjProperty(env, arrayObj, elementIndex, TAG); + if (resValid == false) { + return NULL; + } + + uint32_t index = 0; + // 将第二个参数(索引)转换为本地 uint32_t + status = napi_get_value_uint32(env, elementIndex, &index); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value uint32", TAG); + return NULL; + } + + napi_value result; + // 设置数组元素 + status = napi_get_element(env, arrayObj, index, &result); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get element", TAG); + return NULL; + } + + // 可以返回新设置对象 + return result; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihaselement.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihaselement.cpp new file mode 100644 index 00000000..a82e6633 --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihaselement.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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. + */ + +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiHasElement(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napihaselement + size_t argc = PARAM2; + napi_value argv[PARAM2]; + napi_status status; + napi_value arrayObj; + napi_value elementIndex; + const napi_extended_error_info *extended_error_info; + // 解析传入的参数 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + // 检查参数数量 + if (argc < PARAM2) { + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + arrayObj = argv[PARAM0]; + elementIndex = argv[PARAM1]; + // 判断参数有效性 + bool resValid = validateArrayObjProperty(env, arrayObj, elementIndex, TAG); + if (resValid == false) { + return NULL; + } + uint32_t index = 0; + // 将第二个参数(索引)转换为本地 uint32_t + status = napi_get_value_uint32(env, elementIndex, &index); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value uint32", TAG); + return NULL; + } + bool hasElement = false; + // 设置数组元素 + status = napi_has_element(env, arrayObj, index, &hasElement); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "has element", TAG); + return NULL; + } + // 返回属性是否存在的布尔值 + napi_value result; + status = napi_get_boolean(env, hasElement, &result); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get boolean", TAG); + return NULL; + } + return result; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetelement.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetelement.cpp new file mode 100644 index 00000000..e4482d5a --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetelement.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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. + */ + +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiSetElement(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napisetelement + size_t argc = PARAM3; + napi_value argv[PARAM3]; + napi_status status; + napi_value arrayObj; + napi_value elementIndex; + napi_value valueToSet; + const napi_extended_error_info *extended_error_info; + + // 解析传入的参数 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + + // 检查参数数量 + if (argc < PARAM3) { + napi_throw_error(env, NULL, "Expected 3 arguments"); + return NULL; + } + arrayObj = argv[PARAM0]; + elementIndex = argv[PARAM1]; + valueToSet = argv[PARAM2]; + + // 判断参数有效性 + bool resValid = validateArrayObjProperty(env, arrayObj, elementIndex, TAG); + if (resValid == false) { + return NULL; + } + + uint32_t index = 0; + // 将第二个参数(索引)转换为本地 uint32_t + status = napi_get_value_uint32(env, elementIndex, &index); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value uint32", TAG); + return NULL; + } + + // 设置数组元素 + status = napi_set_element(env, arrayObj, index, valueToSet); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "set element", TAG); + return NULL; + } + + // 可以返回新设置对象 + return arrayObj; +} diff --git a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts index 83b82d6b..5030e46b 100644 --- a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts +++ b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts @@ -40,6 +40,10 @@ export const testNapiDeleteProperty: (a: object, b: any) => string; export const testNapiGetNamedProperty: (a: object, b: string) => string; export const testNapiSetNamedProperty: (a: object, b: string, c: any) => string; export const testNapiHasNamedProperty: (a: object, b: string) => string; +export const testNapiSetElement: (a: object, b: number, c: any) => string; +export const testNapiGetElement: (a: object, b: number) => string; +export const testNapiHasElement: (a: object, b: number) => string; +export const testNapiDeleteElement: (a: object, b: number) => string; /* work_with_javascript_values_and_abstract_operations */ export const testNapiCoerceToBool: (a: any) => boolean; diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets index da489705..82d24bac 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets @@ -365,19 +365,19 @@ const WORK_WITH_JAVASCRIPT_PROPERTIES: ThirdLevelCategory = }, { title: $r('app.string.napi_set_element'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napisetelement' }, { title: $r('app.string.napi_get_element'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napigetelement' }, { title: $r('app.string.napi_has_element'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napihaselement' }, { title: $r('app.string.napi_delete_element'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napideleteelement' }, { title: $r('app.string.napi_define_properties'), diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteelement.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteelement.ets new file mode 100644 index 00000000..f4112bc9 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteelement.ets @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_delete_element'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_delete_element_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 测试 N-API napi_delete_element \n' + + ' let obj = [0, false, "zz"]; \n' + + ' const myData = testNapi.testNapiDeleteElement(obj, 2); \n' + + ' console.log(myData); // 输出自定义数据 \n' + + ' const myData2 = testNapi.testNapiDeleteElement(obj, 1); \n' + + ' console.log(myData2); // 输出自定义数据 \n'; + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_delete_element') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_delete_element'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_delete_element') + .onClick(() => { + let obj = [0, false, "zz"]; + let ret = testNapi.testNapiDeleteElement(obj, 1); + this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); + let ret2 = testNapi.testNapiDeleteElement(obj, 0); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetelement.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetelement.ets new file mode 100644 index 00000000..d7e26129 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetelement.ets @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_get_element'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_get_element_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 测试 N-API napi_get_element \n' + + ' let obj1 = [1,2,3]; \n' + + ' const myData1 = testNapi.testNapiGetElement(obj1, 1); \n' + + ' console.log(myData1); // 输出自定义数据 \n' + + ' let obj2 = ["a", "b", "c"]' + + ' const myData2 = testNapi.testNapiGetElement(obj2, 2); \n' + + ' console.log(myData2); // 输出自定义数据 \n' + + ' let obj3 = [true, false]; \n' + + ' const myData3 = testNapi.testNapiGetElement(obj3, 1); \n' + + ' console.log(myData3); // 输出自定义数据 \n' + + ' let obj4 = [0, false, "zz"]; \n' + + ' const myData4 = testNapi.testNapiGetElement(obj4, 2); \n' + + ' console.log(myData4); // 输出自定义数据 \n' + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_get_element') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_get_element'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_get_element') + .onClick(() => { + let obj1 = [1,2,3]; + let ret = testNapi.testNapiGetElement(obj1, 1); + this.testcont = this.testcont.replace('log(myData1)', 'log(## '+JSON.stringify(ret)+' ##)'); + let obj2 = ["a", "b", "c"]; + let ret2 = testNapi.testNapiGetElement(obj2, 2); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + let obj3 = [true, false]; + let ret3 = testNapi.testNapiGetElement(obj3, 1); + this.testcont = this.testcont.replace('log(myData3)', 'log(## '+JSON.stringify(ret3)+' ##)'); + let obj4 = [0, false, "zz"]; + let ret4 = testNapi.testNapiGetElement(obj4, 2); + this.testcont = this.testcont.replace('log(myData4)', 'log(## '+JSON.stringify(ret4)+' ##)'); + + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihaselement.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihaselement.ets new file mode 100644 index 00000000..6170be86 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihaselement.ets @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_has_element'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_has_element_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 测试 N-API napi_has_element \n' + + ' let obj = [0, false, "zz"]; \n' + + ' const myData = testNapi.testNapiHasElement(obj, 2); \n' + + ' console.log(myData); // 输出自定义数据 \n' + + ' const myData2 = testNapi.testNapiHasElement(obj, 4); \n' + + ' console.log(myData2); // 输出自定义数据 \n'; + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_has_element') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_has_element'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_has_element') + .onClick(() => { + let obj = [0, false, "zz"]; + let ret = testNapi.testNapiHasElement(obj, 2); + this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); + let ret2 = testNapi.testNapiHasElement(obj, 4); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetelement.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetelement.ets new file mode 100644 index 00000000..81381e99 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetelement.ets @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_set_element'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_set_element_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 测试 N-API napi_set_element \n' + + ' let obj1 = [1,2,3]; \n' + + ' const myData1 = testNapi.testNapiSetElement(obj1, 1, 5); \n' + + ' console.log(myData1); // 输出自定义数据 \n' + + ' let obj2 = ["a", "b", "c"]' + + ' const myData2 = testNapi.testNapiSetElement(obj2, 4, "d"); \n' + + ' console.log(myData2); // 输出自定义数据 \n' + + ' let obj3 = [true, false]; \n' + + ' const myData3 = testNapi.testNapiSetElement(obj3, 2, true); \n' + + ' console.log(myData3); // 输出自定义数据 \n' + + ' let obj4 = [0, false, "zz"]; \n' + + ' const myData4 = testNapi.testNapiSetElement(obj4, 1, "kk"); \n' + + ' console.log(myData4); // 输出自定义数据 \n' + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_set_element') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_set_element'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_set_element') + .onClick(() => { + let obj1 = [1,2,3]; + let ret = testNapi.testNapiSetElement(obj1, 1, 5); + this.testcont = this.testcont.replace('log(myData1)', 'log(## '+JSON.stringify(ret)+' ##)'); + let obj2 = ["a", "b", "c"]; + let ret2 = testNapi.testNapiSetElement(obj2, 4, "d"); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + let obj3 = [true, false]; + let ret3 = testNapi.testNapiSetElement(obj3, 2, true); + this.testcont = this.testcont.replace('log(myData3)', 'log(## '+JSON.stringify(ret3)+' ##)'); + let obj4 = [0, false, "zz"]; + let ret4 = testNapi.testNapiSetElement(obj4, 1, "kk"); + this.testcont = this.testcont.replace('log(myData4)', 'log(## '+JSON.stringify(ret4)+' ##)'); + + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/resources/base/element/string.json b/examples/napitutorials/entry/src/main/resources/base/element/string.json index 08ceb647..f1d2ce9a 100644 --- a/examples/napitutorials/entry/src/main/resources/base/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/base/element/string.json @@ -6615,6 +6615,22 @@ { "name": "napi_has_named_property_description", "value": "The napi_has_named_property function is used to check if a JavaScript object has a named property. It allows you to determine whether the given object has a specified property name. " + }, + { + "name": "napi_get_element_description", + "value": "napi_get_element is a function in Node.js N-API that allows native addons to retrieve an element from a JavaScript array by its index. The function provides a mechanism for native code to access array elements, enabling manipulation or inspection of JavaScript arrays directly from C or C++ code." + }, + { + "name": "napi_set_element_description", + "value": "napi_set_element is a function in the Node.js N-API that allows native addons to set an element in a JavaScript array at a specified index. This function enables native code, written in C or C++, to modify or assign values to elements within JavaScript arrays directly." + }, + { + "name": "napi_has_element_description", + "value": "napi_has_element is a function in Node.js N-API that checks if an element exists at a specified index in a JavaScript array. This function allows native code written in C or C++ to easily query JavaScript arrays to determine the presence of an element at a given index. It facilitates interaction between native plugins and JavaScript arrays, allowing native code to better integrate with and manipulate data within the JavaScript environment." + }, + { + "name": "napi_delete_element_description", + "value": "napi_delete_element is a function in the Node.js N-API that enables the removal of an element at a specified index within a JavaScript array. This function allows native code, written in C or C++, to directly manipulate JavaScript arrays by providing a straightforward mechanism to delete elements. " } ] } diff --git a/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json b/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json index 3d020290..16660703 100644 --- a/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json +++ b/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json @@ -65,7 +65,10 @@ "pages/javascript/jsproperties/napisetnamedproperty", "pages/javascript/jsproperties/napigetnamedproperty", "pages/javascript/jsproperties/napihasnamedproperty", - "pages/ncpp/cjsonfuncs/cjsonfuncs", + "pages/javascript/jsproperties/napisetelement", + "pages/javascript/jsproperties/napigetelement", + "pages/javascript/jsproperties/napihaselement", + "pages/javascript/jsproperties/napideleteelement", "pages/javascript/jsabstractops/napicoercetobool", "pages/javascript/jsabstractops/napicoercetonumber", "pages/ncpp/cjsonfuncs/cjsonfuncs", diff --git a/examples/napitutorials/entry/src/main/resources/en_US/element/string.json b/examples/napitutorials/entry/src/main/resources/en_US/element/string.json index 1b4558ac..e3e85efe 100644 --- a/examples/napitutorials/entry/src/main/resources/en_US/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/en_US/element/string.json @@ -6614,6 +6614,22 @@ { "name": "napi_has_named_property_description", "value": "The napi_has_named_property function is used to check if a JavaScript object has a named property. It allows you to determine whether the given object has a specified property name. " + }, + { + "name": "napi_get_element_description", + "value": "napi_get_element is a function in Node.js N-API that allows native addons to retrieve an element from a JavaScript array by its index. The function provides a mechanism for native code to access array elements, enabling manipulation or inspection of JavaScript arrays directly from C or C++ code." + }, + { + "name": "napi_set_element_description", + "value": "napi_set_element is a function in the Node.js N-API that allows native addons to set an element in a JavaScript array at a specified index. This function enables native code, written in C or C++, to modify or assign values to elements within JavaScript arrays directly." + }, + { + "name": "napi_has_element_description", + "value": "napi_has_element is a function in Node.js N-API that checks if an element exists at a specified index in a JavaScript array. This function allows native code written in C or C++ to easily query JavaScript arrays to determine the presence of an element at a given index. It facilitates interaction between native plugins and JavaScript arrays, allowing native code to better integrate with and manipulate data within the JavaScript environment." + }, + { + "name": "napi_delete_element_description", + "value": "napi_delete_element is a function in the Node.js N-API that enables the removal of an element at a specified index within a JavaScript array. This function allows native code, written in C or C++, to directly manipulate JavaScript arrays by providing a straightforward mechanism to delete elements. " } ] } \ No newline at end of file diff --git a/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json b/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json index 95e489e1..45bc83a0 100644 --- a/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json @@ -5599,6 +5599,22 @@ { "name": "napi_has_named_property_description", "value": "napi_has_named_property 函数用于检查 JavaScript 对象是否具有命名属性。它允许确定给定对象是否具有指定的属性名称。" + }, + { + "name": "napi_get_element_description", + "value": "napi_get_element是Node.js N-API中的一个函数,它允许本地插件通过索引从JavaScript数组中检索元素。此函数为本地代码提供了一种访问数组元素的机制,使得可以直接从C或C++代码操作或检查JavaScript数组。" + }, + { + "name": "napi_set_element_description", + "value": "napi_set_element 是 Node.js N-API 中的一个函数,它允许本地插件在指定索引处设置 JavaScript 数组的元素。通过这个函数,C 或 C++ 编写的本地代码可以直接修改或为 JavaScript 数组中的元素赋值。" + }, + { + "name": "napi_has_element_description", + "value": "napi_has_element 是 Node.js N-API 中的一个函数,用于检查 JavaScript 数组中是否存在指定索引的元素。这个函数使得 C 或 C++ 编写的本地代码能够轻松查询 JavaScript 数组,以判断数组中是否包含特定索引位置的元素。这为本地插件与 JavaScript 数组的交互提供了便利,允许本地代码更好地集成并操作 JavaScript 环境中的数据。" + }, + { + "name": "napi_delete_element_description", + "value": "napi_delete_element 是 Node.js N-API 中的一个函数,用于删除 JavaScript 数组中指定索引位置的元素。这个函数使得 C 或 C++ 编写的本地代码能够直接操作 JavaScript 数组,提供了一种移除数组元素的简便方法。" } ] } diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets index 26337f56..40ecf733 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets @@ -109,7 +109,7 @@ export default function abilityTestJsProperty() { hilog.info(0x0000, 'testTag', `napi_set_property(obj.key1) = ${JSON.stringify(result)}`); hilog.info(0x0000, 'testTag', `napi_set_property(obj.0) = ${JSON.stringify(result2)}`); - hilog.info(0x0000, 'testTag', `napi_set_property(obj.true) = ${JSON.stringify(result3}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.true) = ${JSON.stringify(result3)}`); hilog.info(0x0000, 'testTag', `napi_set_property(obj.key4) = ${JSON.stringify(result4)}`); hilog.info(0x0000, 'testTag', `napi_set_property(obj.key5) = ${JSON.stringify(typeof result5.key5)}`); hilog.info(0x0000, 'testTag', `napi_set_property(obj.key6) = ${JSON.stringify(result6)}`); @@ -179,7 +179,7 @@ export default function abilityTestJsProperty() { expect(JSON.stringify(result2)).assertContain('{"0":false,"key4":["a","b","c"],"key6":{"key":"value"}}'); let result3 = testNapi.testNapiDeleteProperty(obj, 0); - hilog.info(0x0000, 'testTag', `napi_delete_property(obj.true) = ${JSON.stringify(result3}`); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.true) = ${JSON.stringify(result3)}`); expect(JSON.stringify(result3)).assertContain('{"key4":["a","b","c"],"key6":{"key":"value"}}'); let result4 = testNapi.testNapiDeleteProperty(obj, "key4"); @@ -216,7 +216,7 @@ export default function abilityTestJsProperty() { hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key1) = ${JSON.stringify(result)}`); hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key2) = ${JSON.stringify(result2)}`); - hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key3) = ${JSON.stringify(result3}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key3) = ${JSON.stringify(result3)}`); hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key4) = ${JSON.stringify(result4)}`); hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key5) = ${JSON.stringify(typeof result5.key5)}`); hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key6) = ${JSON.stringify(result6)}`); @@ -299,5 +299,85 @@ export default function abilityTestJsProperty() { expect(result6.toString()).assertContain('true'); expect(result7.toString()).assertContain('false'); }) + + it('testNapiSetElement', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiSetElement begin'); + let obj1 = [1,2,3]; + let result = testNapi.testNapiSetElement(obj1, 1, 5); + let obj2 = ["a", "b", "c"]; + let result2 = testNapi.testNapiSetElement(obj2, 4, "d"); + let obj3 = [true, false]; + let result3 = testNapi.testNapiSetElement(obj3, 2, true); + let obj4 = [0, false, "zz"]; + let result4 = testNapi.testNapiSetElement(obj4, 1, "kk"); + + hilog.info(0x0000, 'testTag', `napi_set_element(obj, 1, 5) = ${JSON.stringify(result)}`); + hilog.info(0x0000, 'testTag', `napi_set_element(obj, 4, "d") = ${JSON.stringify(result2)}`); + hilog.info(0x0000, 'testTag', `napi_set_element(obj, 2, true) = ${JSON.stringify(result3)}`); + hilog.info(0x0000, 'testTag', `napi_set_element(obj, 1, "kk") = ${JSON.stringify(result4)}`); + + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(result.toString()).assertContain('1,5,3'); + expect(result2.toString()).assertContain('a,b,c,,d'); + expect(result3.toString()).assertContain('true,false,true'); + expect(result4.toString()).assertContain('0,kk,zz'); + }) + + it('testNapiGetElement', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetElement begin'); + let obj1 = [1,2,3]; + let result = testNapi.testNapiGetElement(obj1, 1); + let obj2 = ["a", "b", "c"]; + let result2 = testNapi.testNapiGetElement(obj2, 2); + let obj3 = [true, false]; + let result3 = testNapi.testNapiGetElement(obj3, 1); + let obj4 = [0, false, "zz"]; + let result4 = testNapi.testNapiGetElement(obj4, 2); + + hilog.info(0x0000, 'testTag', `napi_get_element(obj, 1) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_get_element(obj, 2) = ${result2}`); + hilog.info(0x0000, 'testTag', `napi_get_element(obj, 1) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_get_element(obj, 2) = ${result4}`); + + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(result.toString()).assertContain('2'); + expect(result2.toString()).assertContain('c'); + expect(result3.toString()).assertContain('false'); + expect(result4.toString()).assertContain('zz'); + }) + + it('testNapiHasElement', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiHasElement begin'); + let obj = [0, false, "zz"]; + let result = testNapi.testNapiHasElement(obj, 2); + let result2 = testNapi.testNapiHasElement(obj, 4); + + hilog.info(0x0000, 'testTag', `napi_has_element(obj, 2) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_has_element(obj, 4) = ${result2}`); + + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(result.toString()).assertContain('true'); + expect(result2.toString()).assertContain('false'); + }) + + it('testNapiDeleteElement', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiDeleteElement begin'); + let obj = [0, false, "zz"]; + let result = testNapi.testNapiDeleteElement(obj, 1); + hilog.info(0x0000, 'testTag', `napi_delete_element(obj, 1) = ${JSON.stringify(result)}`); + expect(result.toString()).assertContain('0,,zz'); + let result2 = testNapi.testNapiDeleteElement(obj, 0); + hilog.info(0x0000, 'testTag', `napi_delete_element(obj, 0) = ${JSON.stringify(result2)}`); + expect(result2.toString()).assertContain(',,zz'); + let result3 = testNapi.testNapiDeleteElement(obj, 2); + hilog.info(0x0000, 'testTag', `napi_delete_element(obj, 2) = ${JSON.stringify(result3)}`); + expect(result3.toString()).assertContain(',,'); + }) + + }) } -- Gitee From 98be90f4f04579e8fe9c1a7921bd7c127069e69f Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Thu, 11 Apr 2024 17:10:49 +0800 Subject: [PATCH 2/2] jsProperty ets update to api10 Signed-off-by: gou-jingjing --- .../src/main/cpp/types/libentry/index.d.ts | 4 +- .../jsproperties/napideleteproperty.ets | 27 ++++- .../jsproperties/napigetnamedproperty.ets | 19 ++- .../jsproperties/napigetproperty.ets | 27 ++++- .../jsproperties/napigetpropertynames.ets | 14 ++- .../jsproperties/napihasnamedproperty.ets | 19 ++- .../jsproperties/napihasproperty.ets | 27 ++++- .../jsproperties/napisetnamedproperty.ets | 23 +++- .../jsproperties/napisetproperty.ets | 22 +++- .../ets/test/JsProperty/JsProperty.test.ets | 109 +++++++++++------- 10 files changed, 214 insertions(+), 77 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts index 5030e46b..52238c85 100644 --- a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts +++ b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts @@ -33,12 +33,12 @@ export const instance; export const cjson_version: () => string; export const testNapiGetPropertyNames: (a: object) => string; -export const testNapiSetProperty: (a: object, b: any, c: any) => string; +export const testNapiSetProperty: (a: object, b: any, c: any) => object; export const testNapiGetProperty: (a: object, b: any) => string; export const testNapiHasProperty: (a: object, b: any) => boolean; export const testNapiDeleteProperty: (a: object, b: any) => string; export const testNapiGetNamedProperty: (a: object, b: string) => string; -export const testNapiSetNamedProperty: (a: object, b: string, c: any) => string; +export const testNapiSetNamedProperty: (a: object, b: string, c: any) => object; export const testNapiHasNamedProperty: (a: object, b: string) => string; export const testNapiSetElement: (a: object, b: number, c: any) => string; export const testNapiGetElement: (a: object, b: number) => string; diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteproperty.ets index bbdea9e4..b3bca46f 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napideleteproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_delete_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1: string; + true: number; + key3: boolean; + key4: Array; + key5: Function; + key6: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -31,12 +44,12 @@ struct napiGetPropertyNames { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State testcont: string = ' // 测试 N-API napi_delete_property \n' - + 'let obj = {key1: "value",true: 1,0: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + + 'let obj = {key1: "value",true: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + ' const myData = testNapi.testNapiDeleteProperty(obj, "key1"); \n' + ' console.log(myData); // 输出自定义数据 \n' + ' const myData2 = testNapi.testNapiDeleteProperty(obj, true); \n' + ' console.log(myData2); // 输出自定义数据 \n' - + ' const myData3 = testNapi.testNapiDeleteProperty(obj, 0); \n' + + ' const myData3 = testNapi.testNapiDeleteProperty(obj, "key3"); \n' + ' console.log(myData3); // 输出自定义数据 \n' + ' const myData4 = testNapi.testNapiDeleteProperty(obj, "key4"); \n' + ' console.log(myData4); // 输出自定义数据 \n' @@ -93,19 +106,21 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_delete_property') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let ret = testNapi.testNapiDeleteProperty(obj, "key1"); this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); let ret2 = testNapi.testNapiDeleteProperty(obj, true); this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); - let ret3 = testNapi.testNapiDeleteProperty(obj, 0); + let ret3 = testNapi.testNapiDeleteProperty(obj, "key3"); this.testcont = this.testcont.replace('log(myData3)', 'log(## '+JSON.stringify(ret3)+' ##)'); let ret4 = testNapi.testNapiDeleteProperty(obj, "key4"); this.testcont = this.testcont.replace('log(myData4)', 'log(## '+JSON.stringify(ret4)+' ##)'); diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets index a2f91615..562bc91a 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_get_named_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1: string; + key2: number; + key3: boolean; + key4: Array; + key5: Function; + key6: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -96,13 +109,13 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_get_named_property') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { - return "function" + key5: () => { + return "function"; }, key6: { key: "value" } } diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetproperty.ets index 206d5d64..2824c61a 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_get_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1: string; + true: number; + key3: boolean; + key4: Array; + key5: Function; + key6: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -31,13 +44,13 @@ struct napiGetPropertyNames { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State testcont: string = ' // 测试 N-API napi_get_property \n' - + 'let obj = {key1: "value",true: 1,0: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + + 'let obj = {key1: "value",true: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + ' const myData = testNapi.testNapiGetProperty(obj, "key1"); \n' + ' // 使用获取的自定义数据 \n' + ' console.log(myData); // 输出自定义数据 \n' + ' const myData2 = testNapi.testNapiGetProperty(obj, true); \n' + ' console.log(myData2); // 输出自定义数据 \n' - + ' const myData3 = testNapi.testNapiGetProperty(obj, 0); \n' + + ' const myData3 = testNapi.testNapiGetProperty(obj, key3); \n' + ' console.log(myData3); // 输出自定义数据 \n' + ' const myData4 = testNapi.testNapiGetProperty(obj, "key4"); \n' + ' console.log(myData4); // 输出自定义数据 \n' @@ -95,19 +108,21 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_get_property') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let ret = testNapi.testNapiGetProperty(obj, "key1"); this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)'); let ret2 = testNapi.testNapiGetProperty(obj, true); this.testcont = this.testcont.replace('log(myData2)', 'log(## '+ret2+' ##)'); - let ret3 = testNapi.testNapiGetProperty(obj, 0); + let ret3 = testNapi.testNapiGetProperty(obj, "key3"); this.testcont = this.testcont.replace('log(myData3)', 'log(## '+ret3+' ##)'); let ret4 = testNapi.testNapiGetProperty(obj, "key4"); this.testcont = this.testcont.replace('log(myData4)', 'log(## '+ret4+' ##)'); diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetpropertynames.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetpropertynames.ets index ff46b946..397f5826 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetpropertynames.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetpropertynames.ets @@ -22,6 +22,14 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_get_property_names'; +interface myObj { + key1: string; + key2: number; + key3: boolean; + key4: Array; + key5: Function; +} + @Entry @Component struct napiGetPropertyNames { @@ -85,12 +93,14 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_get_property_names') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { console.info("This is a function") } + key5: () => { + console.info("This is a function"); + } } let ret = testNapi.testNapiGetPropertyNames(obj); this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)'); diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets index 8c7e36af..cc4463cc 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_has_named_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1: string; + key2: number; + key3: boolean; + key4: Array; + key5: Function; + key6: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -99,13 +112,13 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_has_named_property') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { - return "function" + key5: () => { + return "function"; }, key6: { key: "value" } } diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasproperty.ets index 7704549d..efa66128 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_has_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1: string; + true: number; + key3: boolean; + key4: Array; + key5: Function; + key6: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -31,13 +44,13 @@ struct napiGetPropertyNames { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State testcont: string = ' // 测试 N-API napi_has_property \n' - + 'let obj = {key1: "value",true: 1,0: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + + 'let obj = {key1: "value",true: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + '包含key的情况测试:包含该属性,返回true \n' + ' const myData = testNapi.testNapiHasProperty(obj, "key1"); \n' + ' console.log(myData); // 输出自定义数据 \n' + ' const myData2 = testNapi.testNapiHasProperty(obj, true); \n' + ' console.log(myData2); // 输出自定义数据 \n' - + ' const myData3 = testNapi.testNapiHasProperty(obj, 0); \n' + + ' const myData3 = testNapi.testNapiHasProperty(obj, "key3"); \n' + ' console.log(myData3); // 输出自定义数据 \n' + ' const myData4 = testNapi.testNapiHasProperty(obj, "key4"); \n' + ' console.log(myData4); // 输出自定义数据 \n' @@ -98,19 +111,21 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_has_property') .onClick(() => { - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let ret = testNapi.testNapiHasProperty(obj, "key1"); this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)'); let ret2 = testNapi.testNapiHasProperty(obj, true); this.testcont = this.testcont.replace('log(myData2)', 'log(## '+ret2+' ##)'); - let ret3 = testNapi.testNapiHasProperty(obj, 0); + let ret3 = testNapi.testNapiHasProperty(obj, "key3"); this.testcont = this.testcont.replace('log(myData3)', 'log(## '+ret3+' ##)'); let ret4 = testNapi.testNapiHasProperty(obj, "key4"); this.testcont = this.testcont.replace('log(myData4)', 'log(## '+ret4+' ##)'); diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets index b15d7656..6b64fc4a 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets @@ -22,6 +22,19 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_set_named_property'; +interface myObjVal { + key: string +} + +interface myObj { + key1?: string; + key2?: number; + key3?: boolean; + key4?: Array; + key5?: Function; + key6?: myObjVal; +} + @Entry @Component struct napiGetPropertyNames { @@ -99,7 +112,7 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_set_named_property') .onClick(() => { - let obj = {}; + let obj: myObj = {}; let ret = testNapi.testNapiSetNamedProperty(obj, "key1", "value"); this.testcont = this.testcont.replace('log(myData)', 'log(## ' + JSON.stringify(ret) + ' ##)'); let ret2 = testNapi.testNapiSetNamedProperty(obj, "key2", false); @@ -108,12 +121,12 @@ struct napiGetPropertyNames { this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + JSON.stringify(ret3) + ' ##)'); let ret4 = testNapi.testNapiSetNamedProperty(obj, "key4", ["a", "b", "c"]); this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + JSON.stringify(ret4) + ' ##)'); - let func = function () { - return "this is a function" + let func = () => { + return "this is a function"; }; - let ret5 = testNapi.testNapiSetNamedProperty(obj, "key5", func); + let ret5: myObj = testNapi.testNapiSetNamedProperty(obj, "key5", func); this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + JSON.stringify(typeof ret5.key5) + ' ##)'); - let objVal = { key: "value" }; + let objVal: myObjVal = { key: "value" }; let ret6 = testNapi.testNapiSetNamedProperty(obj, "key6", objVal); this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + JSON.stringify(ret6) + ' ##)'); }) diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetproperty.ets index b2051a06..c83b2ec3 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetproperty.ets @@ -22,6 +22,18 @@ import hilog from '@ohos.hilog'; const TAG: string = 'napi_set_property'; +interface myObj { + key1?: string; + true?: number; + 0?: boolean; + key4?: Array; + key5?: Function; +} + +interface myObjVal { + key: string; +} + @Entry @Component struct napiGetPropertyNames { @@ -97,7 +109,7 @@ struct napiGetPropertyNames { .margin({ left: 24 }) .id('napi_set_property') .onClick(() => { - let obj = {}; + let obj: myObj = {}; let ret = testNapi.testNapiSetProperty(obj, "key1", "value"); this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); let ret2 = testNapi.testNapiSetProperty(obj, 0, false); @@ -106,10 +118,12 @@ struct napiGetPropertyNames { this.testcont = this.testcont.replace('log(myData3)', 'log(## '+JSON.stringify(ret3)+' ##)'); let ret4 = testNapi.testNapiSetProperty(obj, "key4", ["a","b","c"]); this.testcont = this.testcont.replace('log(myData4)', 'log(## '+JSON.stringify(ret4)+' ##)'); - let func = function () {return "this is a function"}; - let ret5 = testNapi.testNapiSetProperty(obj, "key5", func); + let func = () => { + return "this is a function"; + }; + let ret5: myObj = testNapi.testNapiSetProperty(obj, "key5", func); this.testcont = this.testcont.replace('log(myData5)', 'log(## '+JSON.stringify(typeof ret5.key5)+' ##)'); - let objVal = {key: "value"}; + let objVal: myObjVal = {key: "value"}; let ret6 = testNapi.testNapiSetProperty(obj, "key6", objVal); this.testcont = this.testcont.replace('log(myData6)', 'log(## '+JSON.stringify(ret6)+' ##)'); }) diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets index 40ecf733..320079cb 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets @@ -17,6 +17,19 @@ import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +interface myObjVal { + key: string +} + +interface myObj { + key1?: string; + key2?: number; + true?: number; + key3?: boolean; + key4?: Array; + key5?: Function; + key6?: myObjVal; +} export default function abilityTestJsProperty() { describe('ActsAbilityTestJsProperty', () => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. @@ -42,12 +55,14 @@ export default function abilityTestJsProperty() { it('testNapiGetPropertyNames', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetPropertyNames begin'); - let obj = { + let obj: myObj = { key1: "aa", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { console.info("This is a function") } + key5: () => { + console.info("This is a function"); + } } let result = testNapi.testNapiGetPropertyNames(obj); hilog.info(0x0000, 'testTag', `napi_get_property_names(${obj}) = ${result}`); @@ -58,17 +73,19 @@ export default function abilityTestJsProperty() { it('testNapiGetProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetProperty begin'); - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let result = testNapi.testNapiGetProperty(obj, "key1"); let result2 = testNapi.testNapiGetProperty(obj, true); - let result3 = testNapi.testNapiGetProperty(obj, 0); + let result3 = testNapi.testNapiGetProperty(obj, "key3"); let result4 = testNapi.testNapiGetProperty(obj, "key4"); let result5 = testNapi.testNapiGetProperty(obj, "key5"); let result6 = testNapi.testNapiGetProperty(obj, "key6"); @@ -92,19 +109,21 @@ export default function abilityTestJsProperty() { it('testNapiSetProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiSetProperty begin'); - let obj1 = {}; + let obj1: myObj = {}; let result = testNapi.testNapiSetProperty(obj1, "key1", "value"); - let obj2 = {}; + let obj2: myObj = {}; let result2 = testNapi.testNapiSetProperty(obj2, 0, false); - let obj3 = {}; + let obj3: myObj = {}; let result3 = testNapi.testNapiSetProperty(obj3, true, 1); - let obj4 = {}; + let obj4: myObj = {}; let result4 = testNapi.testNapiSetProperty(obj4, "key4", ["a","b","c"]); - let obj5 = {}; - let func = function () {return "this is a function"}; - let result5 = testNapi.testNapiSetProperty(obj5, "key5", func); - let obj6 = {}; - let objVal = {key: "value"}; + let obj5: myObj = {}; + let func = () => { + return "this is a function"; + }; + let result5: myObj = testNapi.testNapiSetProperty(obj5, "key5", func); + let obj6: myObj = {}; + let objVal: myObjVal = {key: "value"}; let result6 = testNapi.testNapiSetProperty(obj6, "key6", objVal); hilog.info(0x0000, 'testTag', `napi_set_property(obj.key1) = ${JSON.stringify(result)}`); @@ -126,17 +145,19 @@ export default function abilityTestJsProperty() { it('testNapiHasProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiHasProperty begin'); - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let result = testNapi.testNapiHasProperty(obj, "key1"); let result2 = testNapi.testNapiHasProperty(obj, true); - let result3 = testNapi.testNapiHasProperty(obj, 0); + let result3 = testNapi.testNapiHasProperty(obj, "key3"); let result4 = testNapi.testNapiHasProperty(obj, "key4"); let result5 = testNapi.testNapiHasProperty(obj, "key5"); let result6 = testNapi.testNapiHasProperty(obj, "key6"); @@ -144,7 +165,7 @@ export default function abilityTestJsProperty() { hilog.info(0x0000, 'testTag', `napi_has_property(obj.key1) = ${result}`); hilog.info(0x0000, 'testTag', `napi_has_property(obj.true) = ${result2}`); - hilog.info(0x0000, 'testTag', `napi_has_property(obj.0) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key3) = ${result3}`); hilog.info(0x0000, 'testTag', `napi_has_property(obj.key4) = ${result4}`); hilog.info(0x0000, 'testTag', `napi_has_property(obj.key5) = ${result5}`); hilog.info(0x0000, 'testTag', `napi_has_property(obj.key6) = ${result6}`); @@ -162,23 +183,25 @@ export default function abilityTestJsProperty() { it('testNapiDeleteProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiDeleteProperty begin'); - let obj = { + let obj: myObj = { key1: "value", true: 1, - 0: false, + key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let result = testNapi.testNapiDeleteProperty(obj, "key1"); hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key1) = ${JSON.stringify(result)}`); - expect(JSON.stringify(result)).assertContain('{"0":false,"true":1,"key4":["a","b","c"],"key6":{"key":"value"}}'); + expect(JSON.stringify(result)).assertContain('{"true":1,"key3":false,"key4":["a","b","c"],"key6":{"key":"value"}}'); let result2 = testNapi.testNapiDeleteProperty(obj, true); - hilog.info(0x0000, 'testTag', `napi_delete_property(obj.0) = ${JSON.stringify(result2)}`); - expect(JSON.stringify(result2)).assertContain('{"0":false,"key4":["a","b","c"],"key6":{"key":"value"}}'); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key3) = ${JSON.stringify(result2)}`); + expect(JSON.stringify(result2)).assertContain('{"key3":false,"key4":["a","b","c"],"key6":{"key":"value"}}'); - let result3 = testNapi.testNapiDeleteProperty(obj, 0); + let result3 = testNapi.testNapiDeleteProperty(obj, "key3"); hilog.info(0x0000, 'testTag', `napi_delete_property(obj.true) = ${JSON.stringify(result3)}`); expect(JSON.stringify(result3)).assertContain('{"key4":["a","b","c"],"key6":{"key":"value"}}'); @@ -199,19 +222,21 @@ export default function abilityTestJsProperty() { it('testNapiSetNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiSetNamedProperty begin'); - let obj1 = {}; + let obj1: myObj = {}; let result = testNapi.testNapiSetNamedProperty(obj1, "key1", "value"); - let obj2 = {}; + let obj2: myObj = {}; let result2 = testNapi.testNapiSetNamedProperty(obj2, "key2", false); - let obj3 = {}; + let obj3: myObj = {}; let result3 = testNapi.testNapiSetNamedProperty(obj3, "key3", 1); - let obj4 = {}; + let obj4: myObj = {}; let result4 = testNapi.testNapiSetNamedProperty(obj4, "key4", ["a","b","c"]); - let obj5 = {}; - let func = function () {return "this is a function"}; - let result5 = testNapi.testNapiSetNamedProperty(obj5, "key5", func); - let obj6 = {}; - let objVal = {key: "value"}; + let obj5: myObj = {}; + let func = () => { + return "this is a function"; + }; + let result5: myObj = testNapi.testNapiSetNamedProperty(obj5, "key5", func); + let obj6: myObj = {}; + let objVal: myObjVal = {key: "value"}; let result6 = testNapi.testNapiSetNamedProperty(obj6, "key6", objVal); hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key1) = ${JSON.stringify(result)}`); @@ -232,12 +257,14 @@ export default function abilityTestJsProperty() { it('testNapiGetNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetNamedProperty begin'); - let obj = { + let obj: myObj = { key1: "value", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let result = testNapi.testNapiGetNamedProperty(obj, "key1"); @@ -266,12 +293,14 @@ export default function abilityTestJsProperty() { it('testNapiHasNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiHasNamedProperty begin'); - let obj = { + let obj: myObj = { key1: "value", key2: 1, key3: false, key4: ["a", "b", "c"], - key5: function () { return "function" }, + key5: () => { + return "function"; + }, key6: {key: "value"} } let result = testNapi.testNapiHasNamedProperty(obj, "key1"); -- Gitee