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 // interface TestClass1 { 44 // on(type: string, callback: Callback<boolean>): void; 45 // } 46 let tc1 = new TestClass1(); 47 it('test TestClass1 on', function () { 48 ret = false; 49 tc1.on('OnEvent', onCallback); 50 tc1.off('OnEvent', onCallback); 51 // assert.strictEqual(ret, true); 52 }); 53 54 // interface ModelEvent{ 55 // topic: string; 56 // message: string; 57 // } 58 // interface TestClass2 { 59 // on(type: string, callback: Callback<ModelEvent>): void; // Callback为interface 60 // } 61 let tc2 = new TestClass2(); 62 it('test TestClass2 on', function () { 63 ret = false; 64 tc2.on('OnEvent', onCallback2); 65 tc2.off('OnEvent', onCallback2); 66 // assert.strictEqual(ret, true); 67 }); 68 69 // interface TestClass3 { 70 // on(type: string, callback: Callback<{topic:string,message:string}>): void; // Callback为匿名interface 71 // } 72 let tc3 = new TestClass3(); 73 it('test TestClass3 on', function () { 74 ret = false; 75 tc3.on('OnEvent', onCallbackTest3); 76 tc3.off('OnEvent', onCallbackTest3); 77 // assert.strictEqual(ret, true); 78 }); 79}); 80 81describe('test 2 on', function () { 82 let ret = false; 83 let inter = new ModelEvent({topic:'aa', message:'sit down'}); 84 function onCallback(val) { 85 ret = true; 86 console.info('onCallback err = ' + val) 87 console.info('onCallback ret = ' + ret) 88 } 89 90 function onCallback2(val, inter) { 91 ret = true; 92 console.info('onCallback2 val = ' + val) 93 console.info('onCallback2 inter.topic = ' + inter.topic) 94 console.info('onCallback2 inter.message = ' + inter.message) 95 } 96 97 // interface TestClass4 { 98 // on(type: "heartbeat", callback: (wid: boolean) => void): void; // 箭头函数支持 99 // } 100 let tc4 = new TestClass4(); 101 it('test TestClass4 on', function () { 102 ret = false; 103 tc4.on('heartbeat', onCallback); 104 tc4.off('heartbeat', onCallback); 105 // assert.strictEqual(ret, true); 106 }); 107 108 // interface TestClass5 { 109 // on(type: "inputStart", callback: (wid: boolean, modeEv: ModelEvent) => void): void // 回调函数参数个数大于1,支持 110 // } 111 let tc5 = new TestClass5(); 112 it('test TestClass5 on', function () { 113 ret = false; 114 tc5.on('inputStart', onCallback2); 115 tc5.off('inputStart', onCallback2); 116 // assert.strictEqual(ret, true); 117 }); 118 119 // namespace域中有多个on function 120 // function on(type: "onEvents", callback: (wid: number) => void): void; // 箭头函数支持 121 it('test function on', function () { 122 ret = false; 123 on('onEvents', onCallback); 124 off('onEvents'); 125 // assert.strictEqual(ret, true); 126 ret = false; 127 on('onEventFunc', onCallback2); 128 off('onEventFunc', onCallback2); 129 // assert.strictEqual(ret, true); 130 }); 131}); 132 133describe('test 3 on', function () { 134 let ret = false; 135 let inter = new ModelEvent({topic:'aa', message:'sit down'}); 136 function onCallback(val) { 137 ret = true; 138 console.info('onCallback val = ' + val) 139 console.info('onCallback ret = ' + ret) 140 } 141 142 function onCallback2(inter) { 143 ret = true; 144 console.info('onCallback2 inter.topic = ' + inter.topic) 145 console.info('onCallback2 inter.message = ' + inter.message) 146 } 147 148 function onCallback3(val, inter) { 149 ret = true; 150 console.info('onCallback3 val = ' + val) 151 console.info('onCallback3 inter.topic = ' + inter.topic) 152 console.info('onCallback3 inter.message = ' + inter.message) 153 } 154 155 // interface TestClass6 { 156 // on(type: string, asyncCallback: AsyncCallback<boolean>): void; 157 // } 158 let tc6 = new TestClass6(); 159 it('test TestClass6 on', function () { 160 ret = false; 161 tc6.on('test1', onCallback); 162 tc6.off('test1', onCallback); 163 // assert.strictEqual(ret, true); 164 }); 165 166 // interface TestClass7 { 167 // on(type: string, asyncCallback: AsyncCallback<ModelEvent>): void; // Callback为interface 168 // } 169 let tc7 = new TestClass7(); 170 it('test TestClass7 on', function () { 171 ret = false; 172 tc7.on('test2', onCallback2); 173 tc7.off('test2', onCallback2); 174 // assert.strictEqual(ret, true); 175 }); 176 177 // interface中有多个on 178 let tc10 = new TestClass10(); 179 it('test TestClass10 on', function () { 180 // on(type: "heartbeat", callback: Callback<boolean>): void; 181 ret = false; 182 tc10.on('heartbeat', onCallback); 183 tc10.off('heartbeat'); 184 // assert.strictEqual(ret, true); 185 186 // on(type: "enableChange", callback: Callback<ModelEvent>): void; 187 ret = false; 188 tc10.on('enableChange', onCallback2); 189 tc10.off('enableChange'); 190 // assert.strictEqual(ret, true); 191 192 // on(type: string, asyncCallback: AsyncCallback<string>): void; 193 ret = false; 194 tc10.on('test01', onCallback); 195 tc10.off('test01'); 196 // assert.strictEqual(ret, true); 197 198 // on(type: string, callback: (wid: number) => void): void; 199 ret = false; 200 tc10.on('test02', onCallback); 201 tc10.off('test02'); 202 // assert.strictEqual(ret, true); 203 204 // on(type: "inputStart", callback: (wid: boolean, modeEv: ModelEvent) => void): void 205 ret = false; 206 tc10.on('inputStart', onCallback3); 207 tc10.off('inputStart'); 208 // assert.strictEqual(ret, true); 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 242// interface TestClass11 { 243// registerTestfunc11(cb: Function); 244// unRegisterTestfunc11(cb: Function); 245 246// registerTestfunc12(cb : (wid: number) => string); 247// unRegisterTestfunc12(cb : (wid: number) => string); 248 249// registerTestfunc13(cb : Callback<boolean>); 250// unRegisterTestfunc13(cb : Callback<boolean>); 251// } 252 253function callbackTest14(ret1, ret2) { 254 console.info("SayInfo.from = " + ret1.from) 255 console.info("SayInfo.fromId = " + ret1.fromId) 256 console.info("SayInfo.content = " + ret1.content) 257 console.info("SayInfo.saidTime = " + ret1.saidTime) 258 console.info("SayInfo.isEnd = " + ret1.isEnd) 259 console.info("TestOptional.v1 = " + ret2.v1) 260 console.info("TestOptional.v2 = " + ret2.v2) 261 console.info("TestOptional.v3 = " + ret2.v3) 262 console.info("TestOptional.v4 = " + ret2.v4) 263 console.info("TestOptional.v5 = " + ret2.v5) 264} 265 266// interface TestClass12 { 267// registerTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void); 268// unRegisterTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void); 269// } 270// function registerNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void); 271// function unRegisterNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void); 272describe('test register/unRegister callback interface/type param is optional', function () { 273 let tc12 = new TestClass12(); 274 it('test TestClass12 registerTestfunc14', function () { 275 tc12.registerTestfunc14(callbackTest14); 276 }); 277 278 it('test TestClass12 unRegisterTestfunc14', function () { 279 tc12.unRegisterTestfunc14(callbackTest14); 280 }); 281 282 it('test function registerNamespacefunc23', function () { 283 testObj.registerNamespacefunc23(callbackTest14); 284 }); 285 286 it('test function unRegisterNamespacefunc23', function () { 287 testObj.unRegisterNamespacefunc23(callbackTest14); 288 }); 289 290}); 291 292class NodeISayHelloListenerImpl { 293 onSayHelloStart(info) { 294 console.log('----onSayHelloStart XXX', info); 295 } 296 onSayHelloEnd(info) { 297 console.log('----onSayHelloEnd abc.', info); 298 } 299} 300 301function onSayHelloStart(info) { 302 console.log('----aaa bbb ccc onSayHelloStart xxx', info); 303} 304 305describe('test Obj callback', function () { 306 let nis = new testObj.NodeISayHello(); 307 let lis = new NodeISayHelloListenerImpl(); 308 // 注册回调 309 it('test NodeISayHello addSayHelloListener', function () { 310 nis.addSayHelloListener(lis); 311 }); 312 313 it('test NodeISayHello sayHello', function () { 314 nis.sayHello("js", "native", testObj.SayType.kInitiative); 315 }); 316 317 it('test NodeISayHello removeSayHelloListener', function () { 318 nis.removeSayHelloListener(lis); 319 }); 320 321 it('test NodeISayHello sayHello not reg', function () { 322 nis.sayHello("js", "native", testObj.SayType.kInitiative); 323 }); 324}); 325 326