• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import router from '@ohos.router';
17import image from '@ohos.multimedia.image';
18import Logger from '../../../util/Logger';
19import testNapi, { testNapiValue } from 'libentry.so';
20import { TitleBar } from '../../../common/TitleBar'
21import hilog from '@ohos.hilog';
22
23const TAG: string = 'napi_has_named_property';
24
25interface myObjVal {
26  key: string
27}
28
29interface myObj {
30  key1: string;
31  key2: number;
32  key3: boolean;
33  key4: Array<string>;
34  key5: Function;
35  key6: myObjVal;
36}
37
38@Entry
39@Component
40struct napiGetPropertyNames {
41  private btnFontColor: Resource = $r('app.color.white');
42  private pixelMapFormat: image.PixelMapFormat = 3;
43  private textcont: Resource = $r('app.string.napi_has_named_property_description');
44  @State isSetInstance: Boolean = false;
45  @State imagePixelMap: PixelMap | undefined = undefined;
46  @State testcont: string = ' // 测试 N-API napi_has_named_property \n'
47    + 'let obj = {key1: "value","key2": 1,"key3": false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n'
48    + '包含key的情况测试:包含该属性,返回true \n'
49    + ' const myData = testNapi.testNapiHasNamedProperty(obj, "key1"); \n'
50    + ' console.log(myData); // 输出自定义数据 \n'
51    + ' const myData2 = testNapi.testNapiHasNamedProperty(obj, "key2"); \n'
52    + ' console.log(myData2); // 输出自定义数据 \n'
53    + ' const myData3 = testNapi.testNapiHasNamedProperty(obj, "key3"); \n'
54    + ' console.log(myData3); // 输出自定义数据 \n'
55    + ' const myData4 = testNapi.testNapiHasNamedProperty(obj, "key4"); \n'
56    + ' console.log(myData4); // 输出自定义数据 \n'
57    + ' const myData5 = testNapi.testNapiHasNamedProperty(obj, "key5"); \n'
58    + ' console.log(myData5); // 输出自定义数据 \n'
59    + ' const myData6 = testNapi.testNapiHasNamedProperty(obj, "key6"); \n'
60    + ' console.log(myData6); // 输出自定义数据 \n'
61    + '不包含key的情况测试:不包含该属性,返回false \n'
62    + ' const myExcData = testNapi.testNapiHasNamedProperty(obj, "key7"); \n'
63    + ' console.log(myExcData); // 输出自定义数据 \n';
64
65  controller: TextAreaController = new TextAreaController()
66
67  build() {
68    Column() {
69      // 标题
70      TitleBar({ title: $r('app.string.napi_has_named_property') })
71
72      Scroll() {
73        Column() {
74          Column() {
75            TextArea({
76              text: this.textcont,
77              placeholder: '',
78            })
79              .placeholderFont({ size: 16, weight: 400 })
80              .width('90%')
81              .margin(10)
82              .fontSize(16)
83              .fontColor($r('app.color.sub_title_color'))
84              .backgroundColor($r('app.color.white'))
85              .enabled(false)
86
87            TextArea({
88              text: this.testcont,
89              placeholder: '',
90            })
91              .placeholderFont({ size: 16, weight: 400 })
92              .width('90%')
93              .margin(10)
94              .fontSize(16)
95              .fontColor($r('app.color.textarea_font_color'))
96              .backgroundColor($r('app.color.textarea_background_color'))
97              .enabled(false)
98          }
99          .width('100%')
100          .alignItems(HorizontalAlign.Center)
101          .justifyContent(FlexAlign.Start)
102
103          Row() {
104
105            Button($r('app.string.napi_has_named_property'), { type: ButtonType.Capsule })
106              .backgroundColor(Color.Blue)
107              .width('80%')
108              .height(48)
109              .fontSize(16)
110              .fontWeight(500)
111              .fontColor(this.btnFontColor)
112              .margin({ left: 24 })
113              .id('napi_has_named_property')
114              .onClick(() => {
115                let obj: myObj = {
116                  key1: "value",
117                  key2: 1,
118                  key3: false,
119                  key4: ["a", "b", "c"],
120                  key5: () => {
121                    return "function";
122                  },
123                  key6: { key: "value" }
124                }
125                let ret = testNapi.testNapiHasNamedProperty(obj, "key1");
126                this.testcont = this.testcont.replace('log(myData)', 'log(## ' + ret + ' ##)');
127                let ret2 = testNapi.testNapiHasNamedProperty(obj, "key2");
128                this.testcont = this.testcont.replace('log(myData2)', 'log(## ' + ret2 + ' ##)');
129                let ret3 = testNapi.testNapiHasNamedProperty(obj, "key3");
130                this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + ret3 + ' ##)');
131                let ret4 = testNapi.testNapiHasNamedProperty(obj, "key4");
132                this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + ret4 + ' ##)');
133                let ret5 = testNapi.testNapiHasNamedProperty(obj, "key5");
134                this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + ret5 + ' ##)');
135                let ret6 = testNapi.testNapiHasNamedProperty(obj, "key6");
136                this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + ret6 + ' ##)');
137                let ret7 = testNapi.testNapiHasNamedProperty(obj, "key7");
138                this.testcont = this.testcont.replace('log(myExcData)', 'log(## ' + ret7 + ' ##)');
139              })
140          }
141          .width('100%')
142          .height(48)
143          .alignItems(VerticalAlign.Center)
144          .justifyContent(FlexAlign.SpaceBetween)
145        }
146        .width('100%')
147      }
148    }
149    .height('100%')
150    .width('100%')
151    .backgroundColor($r('app.color.background_shallow_grey'))
152  }
153}
154