1/* 2* Copyright (c) 2023 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 napitest from '@ohos.napitest'; 17 18let ns: napitest.NodeISayHello = new napitest.NodeISayHello(); 19class NodeISayHelloListenerImpl { 20 onSayHelloStart(info: object) { 21 AppStorage.SetOrCreate("textInfoStart", JSON.stringify(info)) 22 console.log('napiTestDemo ----onSayHelloStart', JSON.stringify(info)); 23 } 24 onSayHelloEnd(info: object) { 25 AppStorage.SetOrCreate("textInfoEnd", JSON.stringify(info)) 26 console.log('napiTestDemo ----onSayHelloEnd.', JSON.stringify(info)); 27 } 28} 29 30let listener: NodeISayHelloListenerImpl = new NodeISayHelloListenerImpl() 31 32// register注册的回调 33function onCallbackfunnm(wid: number) { 34 AppStorage.SetOrCreate("callBackNum", JSON.stringify(wid)) 35 console.info("napiTestDemo ----onCallbackfunnm wid = " + wid) 36 return "ocCallbackfuncnm"; 37} 38 39@Entry 40@Component 41struct Index { 42 @State promiseRes: string = '' 43 @State returnVal: string = '' 44 @StorageLink("textInfoStart")textInfoStart: string = "" 45 @StorageLink("textInfoEnd")textInfoEnd: string = "" 46 @StorageLink("callBackNum")callBackNum: string = "" 47 48 build() { 49 Row() { 50 Column() { 51 Button() { 52 Text('注册object回调后SayHello调用回调') 53 .fontSize(20) 54 .fontWeight(FontWeight.Bold) 55 } 56 .type(ButtonType.Capsule) 57 .margin({ 58 top: 10 59 }) 60 .backgroundColor('#0D9FFB') 61 .width('90%') 62 .height('5%') 63 .onClick(() => { 64 AppStorage.SetOrCreate("textInfoStart", "") 65 AppStorage.SetOrCreate("textInfoEnd", "") 66 // 注册回调 67 ns.addSayHelloListener(listener); 68 // 调用回调 69 ns.sayHello("js1", "native1", napitest.SayType.kInitiative); 70 }) 71 72 Button() { 73 Text('注销object回调后SayHello调用回调') 74 .fontSize(20) 75 .fontWeight(FontWeight.Bold) 76 } 77 .type(ButtonType.Capsule) 78 .margin({ 79 top: 10 80 }) 81 .backgroundColor('#0D9FFB') 82 .width('90%') 83 .height('5%') 84 .onClick(() => { 85 AppStorage.SetOrCreate("textInfoStart", "") 86 AppStorage.SetOrCreate("textInfoEnd", "") 87 // 注销回调 removeSayHelloListener 88 ns.removeSayHelloListener(listener); 89 // 调用回调 90 ns.sayHello("js2", "native2", napitest.SayType.kInitiative); 91 }) 92 93 // promise回调 94 Button() { 95 Text('Promise 回调') 96 .fontSize(20) 97 .fontWeight(FontWeight.Bold) 98 } 99 .type(ButtonType.Capsule) 100 .margin({ 101 top: 10 102 }) 103 .backgroundColor('#0D9FFB') 104 .width('90%') 105 .height('5%') 106 .onClick(async () => { 107 await ns.sayHelloWithResponse("response from", "response to", napitest.SayType.kResponse).then((ret: object) => { 108 this.promiseRes = JSON.stringify(ret); 109 console.info("napiTestDemo ----sayHelloWithResponse ret = " + JSON.stringify(ret)); 110 }); 111 }) 112 113 Button() { 114 Text('register回调后sayHi调用回调') 115 .fontSize(20) 116 .fontWeight(FontWeight.Bold) 117 } 118 .type(ButtonType.Capsule) 119 .margin({ 120 top: 10 121 }) 122 .backgroundColor('#0D9FFB') 123 .width('90%') 124 .height('5%') 125 .onClick( () => { 126 AppStorage.SetOrCreate("callBackNum", "") 127 // 注册回调 128 ns.registerCallbackfunc(onCallbackfunnm); 129 // 调用回调 130 ns.sayHi("js3", "native3", napitest.SayType.kResponse); 131 }) 132 133 Button() { 134 Text('unRegister回调后sayHi调用回调') 135 .fontSize(20) 136 .fontWeight(FontWeight.Bold) 137 } 138 .type(ButtonType.Capsule) 139 .margin({ 140 top: 10 141 }) 142 .backgroundColor('#0D9FFB') 143 .width('90%') 144 .height('5%') 145 .onClick( () => { 146 AppStorage.SetOrCreate("callBackNum", "") 147 // 注销回调 148 ns.unRegisterCallbackfunc(onCallbackfunnm); 149 // 调用回调 150 ns.sayHi("js4", "native4", napitest.SayType.kResponse); 151 }) 152 153 // 调用普通函数 154 Button() { 155 Text('调用funcTest方法') 156 .fontSize(20) 157 .fontWeight(FontWeight.Bold) 158 } 159 .type(ButtonType.Capsule) 160 .margin({ 161 top: 10 162 }) 163 .backgroundColor('#0D9FFB') 164 .width('90%') 165 .height('5%') 166 .onClick( () => { 167 this.returnVal = napitest.funcTest(false); 168 console.info("napiTestDemo ----funcTest returnVal = " + this.returnVal) 169 }) 170 171 Text('promise回调: promiseResult = ' + this.promiseRes).margin({ top: 10 }) 172 Text('sayHelloStart回调: info = ' + this.textInfoStart).margin({ top: 10 }) 173 Text('sayHelloEnd回调: info = ' + this.textInfoEnd).margin({ top: 10 }) 174 Text('register注册的回调: wid = ' + this.callBackNum).margin({ top: 10 }) 175 Text('普通方法funcTest返回值: returnVal = ' + this.returnVal).margin({ top: 10 }) 176 } 177 .width('100%') 178 } 179 .height('100%') 180 } 181} 182