1/* 2 * Copyright (c) 2021 Huawei Device 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 */ 15import emitter from '@ohos.events.emitter' 16import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 17 18export default function EmitterTest() { 19 describe('SUB_NOTIFICATION_CES_EMITTER_TEST', function () { 20 const TAG = 'SUB_NOTIFICATION_CES_EMITTER_TEST ===> ' 21 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST START`) 22 23 let innerEvent = { 24 eventId: undefined, 25 priority: undefined 26 } 27 28 let eventData = { 29 data: { 30 'id': undefined, 31 'content': undefined 32 } 33 } 34 35 /* EmitterCallbackDummy should not be called */ 36 function EmitterCallbackDummy(eventData) { 37 console.info(TAG + 'eventData.id: ' + JSON.stringify(eventData.data.id)); 38 console.info(TAG + 'eventData.content: ' + JSON.stringify(eventData.data.content)); 39 expect(eventData.data.content).asertFalse(); 40 } 41 42 function EmitterCallback(eventData) { 43 console.info(TAG + 'eventData.id: ' + JSON.stringify(eventData.data.id)); 44 console.info(TAG + 'eventData.content: ' + JSON.stringify(eventData.data.content)); 45 if (eventData.data.id == 0) { 46 expect(eventData.data.content).assertEqual('message_0'); 47 } else if (eventData.data.id == 1) { 48 expect(eventData.data.content).assertEqual('message_1'); 49 } else if (eventData.data.id == 2) { 50 expect(eventData.data.content).assertEqual('message_2'); 51 } else if (eventData.data.id == 3) { 52 expect(eventData.data.content).assertEqual('message_3'); 53 } else if (eventData.data.id == 4) { 54 expect(eventData.data.content).assertEqual('message_4'); 55 } else if (eventData.data.id == 5) { 56 expect(eventData.data.content).assertEqual('message_5'); 57 } else if (eventData.data.id == 6) { 58 expect(eventData.data.content).assertEqual('message_6'); 59 } else if (eventData.data.id == 7) { 60 expect(eventData.data.content).assertFalse(); 61 } else if (eventData.data.id == 8) { 62 expect(eventData.data.content).assertEqual('message_8'); 63 } else if (eventData.data.id == 9) { 64 expect(eventData.data.content).assertFalse(); 65 } else if (eventData.data.id == 10) { 66 expect(eventData.data.content).assertFalse(); 67 } else if (eventData.data.id == 11) { 68 expect(eventData.data.content).assertFalse(); 69 } else if (eventData.data.id == 12) { 70 expect(eventData.data.content).assertEqual('message_12'); 71 } else if (eventData.data.id == 13) { 72 expect(eventData.data.content).assertEqual('message_13'); 73 } 74 } 75 76 /* 77 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0100 78 * @tc.name : verify on : on(event: InnerEvent, callback: Callback<EventData>): void 79 * @tc.desc : emitter.EventPriority.IDLE 80 */ 81 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0100', 0, async function (done) { 82 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0100 START`) 83 innerEvent.eventId = 1 84 innerEvent.priority = emitter.EventPriority.IDLE 85 86 eventData.data.id = 0 87 eventData.data.content = 'message_0' 88 89 emitter.on(innerEvent, EmitterCallback) 90 emitter.emit(innerEvent, eventData) 91 emitter.off(1) 92 93 innerEvent.priority = emitter.EventPriority.HIGH 94 innerEvent.eventId = 1 95 eventData.data.id = 1 96 eventData.data.content = 'message_1' 97 98 emitter.on(innerEvent, EmitterCallback) 99 emitter.emit(innerEvent, eventData) 100 emitter.off(1) 101 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0100 END`) 102 done() 103 }) 104 105 /* 106 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0200 107 * @tc.name : verify on : once(event: InnerEvent, callback: Callback<EventData>): void 108 * @tc.desc : emitter.EventPriority.LOW 109 */ 110 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0200', 0, async function (done) { 111 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0200 START`) 112 innerEvent.eventId = 2 113 innerEvent.priority = emitter.EventPriority.LOW 114 115 eventData.data.id = 2 116 eventData.data.content = 'message_2' 117 118 emitter.once(innerEvent, EmitterCallback) 119 emitter.emit(innerEvent, eventData) 120 emitter.off(2) 121 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0200 END`) 122 done() 123 }) 124 125 /* 126 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0300 127 * @tc.name : verify on : emit(event: InnerEvent, data?: EventData): void 128 * @tc.desc : emitter.EventPriority.HIGH 129 */ 130 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0300', 0, async function (done) { 131 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0300 START`) 132 innerEvent.eventId = 3 133 innerEvent.priority = emitter.EventPriority.HIGH 134 135 eventData.data.id = 3 136 eventData.data.content = 'message_3' 137 138 emitter.once(innerEvent, EmitterCallback) 139 emitter.emit(innerEvent, eventData) 140 emitter.off(3) 141 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0300 END`) 142 done() 143 }) 144 145 /* 146 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0400 147 * @tc.name : verify on : emit(event: InnerEvent, data?: EventData): void 148 * @tc.desc : emitter.EventPriority.IMMEDIATE 149 */ 150 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0400', 0, async function (done) { 151 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0400 START`) 152 innerEvent.eventId = 4 153 innerEvent.priority = emitter.EventPriority.IMMEDIATE 154 155 eventData.data.id = 4 156 eventData.data.content = 'message_4' 157 158 emitter.once(innerEvent, EmitterCallback) 159 emitter.emit(innerEvent, eventData) 160 emitter.off(4) 161 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0400 END`) 162 done() 163 }) 164 165 /* 166 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0500 167 * @tc.name : verify on : off(eventId: number): void 168 * @tc.desc : emitter.EventPriority.IMMEDIATE 169 */ 170 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0500', 0, async function (done) { 171 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0500 START`) 172 innerEvent.eventId = 5 173 innerEvent.priority = emitter.EventPriority.IMMEDIATE 174 175 eventData.data.id = 5 176 eventData.data.content = 'message_5' 177 178 emitter.once(innerEvent, EmitterCallback) 179 emitter.emit(innerEvent, eventData) 180 emitter.off(5) 181 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0500 END`) 182 done() 183 }) 184 185 /* 186 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0600 187 * @tc.name : verify on : off(eventId: number, callback: Callback<EventData>): void 188 * @tc.desc : emitter.EventPriority.IMMEDIATE 189 */ 190 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0600', 0, async function (done) { 191 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0600 START`) 192 innerEvent.eventId = 6 193 innerEvent.priority = emitter.EventPriority.IMMEDIATE 194 195 eventData.data.id = 6 196 eventData.data.content = 'message_6' 197 emitter.on(innerEvent, EmitterCallback) 198 199 emitter.off(6, EmitterCallbackDummy) 200 emitter.emit(innerEvent, eventData) 201 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0600 END`) 202 done() 203 }) 204 205 /* 206 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0700 207 * @tc.name : verify on : off(eventId: number, callback: Callback<EventData>): void 208 * @tc.desc : emitter.EventPriority.IMMEDIATE 209 */ 210 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0700', 0, async function (done) { 211 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0700 START`) 212 innerEvent.eventId = 7 213 innerEvent.priority = emitter.EventPriority.IMMEDIATE 214 215 eventData.data.id = 7 216 eventData.data.content = 'message_7' 217 emitter.on(innerEvent, EmitterCallback) 218 219 emitter.off(7, EmitterCallback) 220 emitter.emit(innerEvent, eventData) 221 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0700 END`) 222 done() 223 }) 224 225 /* 226 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0800 227 * @tc.name : verify on: off(eventId: number, callback: Callback<EventData>): void 228 * @tc.desc : emitter.EventPriority.IMMEDIATE 229 */ 230 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0800', 0, async function (done) { 231 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0800 START`) 232 innerEvent.eventId = 8 233 innerEvent.priority = emitter.EventPriority.IMMEDIATE 234 235 eventData.data.id = 8 236 eventData.data.content = 'message_8' 237 emitter.once(innerEvent, EmitterCallback) 238 239 emitter.off(8, EmitterCallbackDummy) 240 emitter.emit(innerEvent, eventData) 241 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0800 END`) 242 done() 243 }) 244 245 /* 246 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_0900 247 * @tc.name : verify on : off(eventId: number, callback: Callback<EventData>): void 248 * @tc.desc : emitter.EventPriority.IMMEDIATE 249 */ 250 it('SUB_NOTIFICATION_CES_EMITTER_TEST_0900', 0, async function (done) { 251 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0900 START`) 252 innerEvent.eventId = 9 253 innerEvent.priority = emitter.EventPriority.IMMEDIATE 254 255 eventData.data.id = 9 256 eventData.data.content = 'message_9' 257 emitter.once(innerEvent, EmitterCallback) 258 259 emitter.off(9, EmitterCallback) 260 emitter.emit(innerEvent, eventData) 261 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_0900 END`) 262 done() 263 }) 264 265 /* 266 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_1000 267 * @tc.name : verify on : off(eventId: number): void 268 * @tc.desc : emitter.EventPriority.IMMEDIATE 269 */ 270 it('SUB_NOTIFICATION_CES_EMITTER_TEST_1000', 0, async function (done) { 271 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1000 START`) 272 innerEvent.eventId = 10 273 innerEvent.priority = emitter.EventPriority.IMMEDIATE 274 275 eventData.data.id = 10 276 eventData.data.content = 'message_10' 277 emitter.on(innerEvent, EmitterCallback) 278 279 emitter.off(10) 280 emitter.emit(innerEvent, eventData) 281 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1000 END`) 282 done() 283 }) 284 285 /* 286 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_1100 287 * @tc.name : verify on : off(eventId: number): void 288 * @tc.desc : emitter.EventPriority.IMMEDIATE 289 */ 290 it('SUB_NOTIFICATION_CES_EMITTER_TEST_1100', 0, async function (done) { 291 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1100 START`) 292 innerEvent.eventId = 11 293 innerEvent.priority = emitter.EventPriority.IMMEDIATE 294 295 eventData.data.id = 11 296 eventData.data.content = 'message_11' 297 emitter.once(innerEvent, EmitterCallback) 298 299 emitter.off(11) 300 emitter.emit(innerEvent, eventData) 301 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1100 END`) 302 done() 303 }) 304 305 /* 306 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_1200 307 * @tc.name : verify on : off(eventId: number): void 308 * @tc.desc : emitter.EventPriority.IMMEDIATE 309 */ 310 it('SUB_NOTIFICATION_CES_EMITTER_TEST_1200', 0, async function (done) { 311 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1200 START`) 312 innerEvent.eventId = 12 313 innerEvent.priority = emitter.EventPriority.IMMEDIATE 314 315 eventData.data.id = 12 316 eventData.data.content = 'message_12' 317 emitter.on(innerEvent, EmitterCallback) 318 319 emitter.off(1000) 320 emitter.emit(innerEvent, eventData) 321 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1200 END`) 322 done() 323 }) 324 325 /* 326 * @tc.number : SUB_NOTIFICATION_CES_EMITTER_TEST_1300 327 * @tc.name : verify on : off(eventId: number): void 328 * @tc.desc : emitter.EventPriority.IMMEDIATE 329 */ 330 it('SUB_NOTIFICATION_CES_EMITTER_TEST_1300', 0, async function (done) { 331 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1300 START`) 332 innerEvent.eventId = 13 333 innerEvent.priority = emitter.EventPriority.IMMEDIATE 334 335 eventData.data.id = 13 336 eventData.data.content = 'message_13' 337 emitter.on(innerEvent, EmitterCallback) 338 339 emitter.off(1000) 340 emitter.emit(innerEvent, eventData) 341 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST_1300 END`) 342 done() 343 }) 344 345 console.info(`${TAG} SUB_NOTIFICATION_CES_EMITTER_TEST END`) 346 }) 347} 348