1/* 2* Copyright (c) 2022 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*/ 15const { TestClass1, TestClass2, ModelEvent, TestClass3, on, off, TestClass4, TestClass5, 16 TestClass6, TestClass7, TestClass10, TestClass11, TestClass12} = require("./out/build/Release/napitest") 17const testObj = require("./out/build/Release/napitest") 18var assert = require("assert"); 19 20describe('test 1 on', function () { 21 let ret = false; 22 let inter = new ModelEvent({topic:'aa', message:'sit down'}); 23 function onCallback(val) { 24 ret = true; 25 console.info('onAsyncCallback val = ' + val) 26 console.info('onAsyncCallback ret = ' + ret) 27 } 28 29 function onCallback2(inter) { 30 ret = true; 31 console.info('onCallback inter.topic = ' + inter.topic) 32 console.info('onCallback inter.message = ' + inter.message) 33 } 34 35 let topic = 'hh'; 36 let message = 'jjj'; 37 function onCallbackTest3({topic, message}) { 38 ret = true; 39 console.info('onCallback topic = ' + topic) 40 console.info('onCallback message = ' + message) 41 } 42 43 /* 测试 44 interface TestClass1 { 45 on(type: string, callback: Callback<boolean>): void; 46 } 47 */ 48 let tc1 = new TestClass1(); 49 it('test TestClass1 on', function () { 50 ret = false; 51 tc1.on('OnEvent', onCallback); 52 tc1.off('OnEvent', onCallback); 53 }); 54 55 /* 测试 56 interface ModelEvent{ 57 topic: string; 58 message: string; 59 } 60 interface TestClass2 { 61 on(type: string, callback: Callback<ModelEvent>): void; // Callback为interface 62 } 63 */ 64 let tc2 = new TestClass2(); 65 it('test TestClass2 on', function () { 66 ret = false; 67 tc2.on('OnEvent', onCallback2); 68 tc2.off('OnEvent', onCallback2); 69 }); 70 71 /* 测试 72 interface TestClass3 { 73 on(type: string, callback: Callback<{topic:string,message:string}>): void; // Callback为匿名interface 74 } 75 */ 76 let tc3 = new TestClass3(); 77 it('test TestClass3 on', function () { 78 ret = false; 79 tc3.on('OnEvent', onCallbackTest3); 80 tc3.off('OnEvent', onCallbackTest3); 81 }); 82}); 83 84describe('test 2 on', function () { 85 let ret = false; 86 let inter = new ModelEvent({topic:'aa', message:'sit down'}); 87 function onCallback(val) { 88 ret = true; 89 console.info('onCallback err = ' + val) 90 console.info('onCallback ret = ' + ret) 91 } 92 93 function onCallback2(val, inter) { 94 ret = true; 95 console.info('onCallback2 val = ' + val) 96 console.info('onCallback2 inter.topic = ' + inter.topic) 97 console.info('onCallback2 inter.message = ' + inter.message) 98 } 99 100 /* 测试 101 interface TestClass4 { 102 on(type: "heartbeat", callback: (wid: boolean) => void): void; // 箭头函数支持 103 } 104 */ 105 let tc4 = new TestClass4(); 106 it('test TestClass4 on', function () { 107 ret = false; 108 tc4.on('heartbeat', onCallback); 109 tc4.off('heartbeat', onCallback); 110 }); 111 112 /* 测试 113 interface TestClass5 { 114 on(type: "inputStart", callback: (wid: boolean, modeEv: ModelEvent) => void): void // 回调函数参数个数大于1,支持 115 } 116 */ 117 let tc5 = new TestClass5(); 118 it('test TestClass5 on', function () { 119 ret = false; 120 tc5.on('inputStart', onCallback2); 121 tc5.off('inputStart', onCallback2); 122 }); 123 124 // namespace域中有多个on function 125 // 测试:function on(type: "onEvents", callback: (wid: number) => void): void; // 箭头函数支持 126 it('test function on', function () { 127 ret = false; 128 on('onEvents', onCallback); 129 off('onEvents'); 130 ret = false; 131 on('onEventFunc', onCallback2); 132 off('onEventFunc', onCallback2); 133 }); 134}); 135 136describe('test 3 on', function () { 137 let ret = false; 138 let inter = new ModelEvent({topic:'aa', message:'sit down'}); 139 function onCallback(val) { 140 ret = true; 141 console.info('onCallback val = ' + val) 142 console.info('onCallback ret = ' + ret) 143 } 144 145 function onCallback2(inter) { 146 ret = true; 147 console.info('onCallback2 inter.topic = ' + inter.topic) 148 console.info('onCallback2 inter.message = ' + inter.message) 149 } 150 151 function onCallback3(val, inter) { 152 ret = true; 153 console.info('onCallback3 val = ' + val) 154 console.info('onCallback3 inter.topic = ' + inter.topic) 155 console.info('onCallback3 inter.message = ' + inter.message) 156 } 157 158 /* 测试 159 interface TestClass6 { 160 on(type: string, asyncCallback: AsyncCallback<boolean>): void; 161 } 162 */ 163 let tc6 = new TestClass6(); 164 it('test TestClass6 on', function () { 165 ret = false; 166 tc6.on('test1', onCallback); 167 tc6.off('test1', onCallback); 168 }); 169 170 /* 测试 171 interface TestClass7 { 172 on(type: string, asyncCallback: AsyncCallback<ModelEvent>): void; // Callback为interface 173 } 174 */ 175 let tc7 = new TestClass7(); 176 it('test TestClass7 on', function () { 177 ret = false; 178 tc7.on('test2', onCallback2); 179 tc7.off('test2', onCallback2); 180 }); 181 182 // interface中有多个on 183 let tc10 = new TestClass10(); 184 it('test TestClass10 on', function () { 185 // 测试:on(type: "heartbeat", callback: Callback<boolean>): void; 186 ret = false; 187 tc10.on('heartbeat', onCallback); 188 tc10.off('heartbeat'); 189 190 // 测试:on(type: "enableChange", callback: Callback<ModelEvent>): void; 191 ret = false; 192 tc10.on('enableChange', onCallback2); 193 tc10.off('enableChange'); 194 195 // 测试:on(type: string, asyncCallback: AsyncCallback<string>): void; 196 ret = false; 197 tc10.on('test01', onCallback); 198 tc10.off('test01'); 199 200 // 测试:on(type: string, callback: (wid: number) => void): void; 201 ret = false; 202 tc10.on('test02', onCallback); 203 tc10.off('test02'); 204 205 // 测试:on(type: "inputStart", callback: (wid: boolean, modeEv: ModelEvent) => void): void 206 ret = false; 207 tc10.on('inputStart', onCallback3); 208 tc10.off('inputStart'); 209 }); 210}); 211 212describe('test register/unRegister', function () { 213 // 测试:function registerNamespacefunc20(cb: Function); 214 // 测试:function unRegisterNamespacefunc20(cb: Function); 215 216 // 测试:function registerNamespacefunc21(cb : (wid: number) => string); 217 // 测试:function unRegisterNamespacefunc21(cb : (wid: number) => string); 218 219 // 测试:function registerNamespacefunc22(cb : Callback<boolean>); 220 // 测试:function unRegisterNamespacefunc22(cb : Callback<boolean>); 221 222 // cb: (wid: number) => string 223 function onCallbackfun10nm(wid) { 224 return 'fun10nm' 225 } 226 227 it('test registerNamespacefunc21', function () { 228 testObj.registerNamespacefunc21(onCallbackfun10nm); 229 }); 230 231 it('test unRegisterNamespacefunc21', function () { 232 testObj.unRegisterNamespacefunc21(onCallbackfun10nm); 233 }); 234 235 it('test TestClass11 registerTestfunc12', function () { 236 let tc11 = new TestClass11(); 237 tc11.registerTestfunc12(onCallbackfun10nm); 238 }); 239 240}); 241 242function callbackTest14(ret1, ret2) { 243 console.info("SayInfo.from = " + ret1.from) 244 console.info("SayInfo.fromId = " + ret1.fromId) 245 console.info("SayInfo.content = " + ret1.content) 246 console.info("SayInfo.saidTime = " + ret1.saidTime) 247 console.info("SayInfo.isEnd = " + ret1.isEnd) 248 console.info("TestOptional.v1 = " + ret2.v1) 249 console.info("TestOptional.v2 = " + ret2.v2) 250 console.info("TestOptional.v3 = " + ret2.v3) 251 console.info("TestOptional.v4 = " + ret2.v4) 252 console.info("TestOptional.v5 = " + ret2.v5) 253} 254 255/* 测试 256interface TestClass12 { 257 registerTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void); 258 unRegisterTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void); 259 } 260 function registerNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void); 261 function unRegisterNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void); 262*/ 263describe('test register/unRegister callback interface/type param is optional', function () { 264 let tc12 = new TestClass12(); 265 it('test TestClass12 registerTestfunc14', function () { 266 tc12.registerTestfunc14(callbackTest14); 267 }); 268 269 it('test TestClass12 unRegisterTestfunc14', function () { 270 tc12.unRegisterTestfunc14(callbackTest14); 271 }); 272 273 it('test function registerNamespacefunc23', function () { 274 testObj.registerNamespacefunc23(callbackTest14); 275 }); 276 277 it('test function unRegisterNamespacefunc23', function () { 278 testObj.unRegisterNamespacefunc23(callbackTest14); 279 }); 280 281}); 282 283class NodeISayHelloListenerImpl { 284 onSayHelloStart(info) { 285 console.log('----onSayHelloStart XXX', info); 286 } 287 onSayHelloEnd(info) { 288 console.log('----onSayHelloEnd abc.', info); 289 } 290} 291 292function onSayHelloStart(info) { 293 console.log('----aaa bbb ccc onSayHelloStart xxx', info); 294} 295 296describe('test Obj callback', function () { 297 let nis = new testObj.NodeISayHello(); 298 let lis = new NodeISayHelloListenerImpl(); 299 // 注册回调 300 it('test NodeISayHello addSayHelloListener', function () { 301 nis.addSayHelloListener(lis); 302 }); 303 304 it('test NodeISayHello sayHello', function () { 305 nis.sayHello("js", "native", testObj.SayType.kInitiative); 306 }); 307 308 it('test NodeISayHello removeSayHelloListener', function () { 309 nis.removeSayHelloListener(lis); 310 }); 311 312 it('test NodeISayHello sayHello not reg', function () { 313 nis.sayHello("js", "native", testObj.SayType.kInitiative); 314 }); 315}); 316 317