• 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_property';
24
25interface myObjVal {
26  key: string
27}
28
29interface myObj {
30  key1: string;
31  true: 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_property_description');
44  @State isSetInstance: Boolean = false;
45  @State imagePixelMap: PixelMap | undefined = undefined;
46  @State testcont: string = ' // 测试 N-API napi_has_property \n'
47    + 'let obj = {key1: "value",true: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n'
48    + '包含key的情况测试:包含该属性,返回true \n'
49    + ' const myData = testNapi.testNapiHasProperty(obj, "key1"); \n'
50    + ' console.log(myData); // 输出自定义数据 \n'
51    + ' const myData2 = testNapi.testNapiHasProperty(obj, true); \n'
52    + ' console.log(myData2); // 输出自定义数据 \n'
53    + ' const myData3 = testNapi.testNapiHasProperty(obj, "key3"); \n'
54    + ' console.log(myData3); // 输出自定义数据 \n'
55    + ' const myData4 = testNapi.testNapiHasProperty(obj, "key4"); \n'
56    + ' console.log(myData4); // 输出自定义数据 \n'
57    + ' const myData5 = testNapi.testNapiHasProperty(obj, "key5"); \n'
58    + ' console.log(myData5); // 输出自定义数据 \n'
59    + ' const myData6 = testNapi.testNapiHasProperty(obj, "key6"); \n'
60    + ' console.log(myData6); // 输出自定义数据 \n'
61    + '不包含key的情况测试:不包含该属性,返回false \n'
62    + ' const myExcData = testNapi.testNapiGetProperty(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_property') })
71
72      Column() {
73        Column() {
74          TextArea({
75            text: this.textcont,
76            placeholder: '',
77          })
78            .placeholderFont({ size: 16, weight: 400 })
79            .width('90%')
80            .margin(10)
81            .fontSize(16)
82            .fontColor($r('app.color.sub_title_color'))
83            .backgroundColor($r('app.color.white'))
84            .enabled(false)
85
86          TextArea({
87            text: this.testcont,
88            placeholder: '',
89          })
90            .placeholderFont({ size: 16, weight: 400 })
91            .width('90%')
92            .margin(10)
93            .fontSize(16)
94            .fontColor($r('app.color.textarea_font_color'))
95            .backgroundColor($r('app.color.textarea_background_color'))
96            .enabled(false)
97        }
98        .width('100%')
99        .alignItems(HorizontalAlign.Center)
100        .justifyContent(FlexAlign.Start)
101
102        Row() {
103
104          Button($r('app.string.napi_has_property'), { type: ButtonType.Capsule })
105            .backgroundColor(Color.Blue)
106            .width('80%')
107            .height(48)
108            .fontSize(16)
109            .fontWeight(500)
110            .fontColor(this.btnFontColor)
111            .margin({ left: 24 })
112            .id('napi_has_property')
113            .onClick(() => {
114              let obj: myObj = {
115                key1: "value",
116                true: 1,
117                key3: false,
118                key4: ["a", "b", "c"],
119                key5: () => {
120                  return "function";
121                },
122                key6: {key: "value"}
123              }
124              let ret = testNapi.testNapiHasProperty(obj, "key1");
125              this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret+' ##)');
126              let ret2 = testNapi.testNapiHasProperty(obj, true);
127              this.testcont = this.testcont.replace('log(myData2)', 'log(## '+ret2+' ##)');
128              let ret3 = testNapi.testNapiHasProperty(obj, "key3");
129              this.testcont = this.testcont.replace('log(myData3)', 'log(## '+ret3+' ##)');
130              let ret4 = testNapi.testNapiHasProperty(obj, "key4");
131              this.testcont = this.testcont.replace('log(myData4)', 'log(## '+ret4+' ##)');
132              let ret5 = testNapi.testNapiHasProperty(obj, "key5");
133              this.testcont = this.testcont.replace('log(myData5)', 'log(## '+ret5+' ##)');
134              let ret6 = testNapi.testNapiHasProperty(obj, "key6");
135              this.testcont = this.testcont.replace('log(myData6)', 'log(## '+ret6+' ##)');
136              let ret7 = testNapi.testNapiHasProperty(obj, "key7");
137              this.testcont = this.testcont.replace('log(myExcData)', 'log(## '+ret7+' ##)');
138            })
139        }
140        .width('100%')
141        .height(48)
142        .alignItems(VerticalAlign.Center)
143        .justifyContent(FlexAlign.SpaceBetween)
144      }
145      .width('100%')
146    }
147    .height('100%')
148    .width('100%')
149    .backgroundColor($r('app.color.background_shallow_grey'))
150  }
151}
152