1/* 2 * Copyright (C) 2022 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 commonEvent from '@ohos.commonEvent'; 16import thermal from "@ohos.thermal" 17 18export default function ThermalCommonEvent() { 19 describe('ThermalCommonEvent', function () { 20 console.log("*************Thermal commonEvent Test Begin*************"); 21 22 /** 23 * @tc.number ThermalCommonEventTest_001 24 * @tc.name subscribe thermal level changed common event 25 * @tc.desc Thermal acquisition kit 26 */ 27 it('ThermalCommonEventTest_001', 0, function (done) { 28 createSubscriber(); 29 done(); 30 }) 31 console.log("*************Thermal commonEvent Test End*************"); 32 }) 33 34 function createSubscriber() { 35 let commonEventSubscribeInfo = { 36 events: [commonEvent.Support.COMMON_EVENT_THERMAL_LEVEL_CHANGED], 37 }; 38 commonEvent.createSubscriber(commonEventSubscribeInfo) 39 .then(subscriber => { 40 console.info('createSubscriber success'); 41 let mySubscriber = subscriber; 42 console.log(subscriber); 43 44 if (subscriber == "" || subscriber == undefined || subscriber == null) { 45 console.info("createSubscriber failed"); 46 } 47 mySubscriber.getCode() 48 .then((data) => { 49 console.info('Subscriber getCode success : ' + JSON.stringify(data)); 50 }).catch((error) => { 51 console.error('Subscriber getCode error because: ' + JSON.stringify(error)); 52 }) 53 mySubscriber.getData() 54 .then((data) => { 55 console.info('Subscriber getData success : ' + JSON.stringify(data)); 56 }).catch((error) => { 57 console.error('Subscriber getData error because: ' + JSON.stringify(error)); 58 }) 59 console.info('subscribe begin '); 60 61 commonEvent.subscribe(mySubscriber, (error, commonEventData) => { 62 console.error('err code: ' + JSON.stringify(error)); 63 console.info('subscribe callback: ' + JSON.stringify(commonEventData)); 64 console.info("commonEventData event: " + commonEventData.event); 65 console.info("commonEventData bundleName: " + commonEventData.bundleName); 66 console.info("commonEventData data: " + commonEventData.data); 67 console.info("commonEventData parameter: " + commonEventData.parameters[0]); 68 let level = -1; 69 expect(level >= thermal.ThermalLevel.COOL && warm <= level.ThermalLevel.EMERGENCY).assertTrue(); 70 }); 71 commonEvent.unsubscribe(mySubscriber, (error) => { 72 if (error.code) { 73 console.error(`unsubscribe failed, code is ${err.code}`); 74 } else { 75 console.info("unsubscribe"); 76 } 77 }) 78 }).catch((error) => { 79 console.error('Operation failed. Cause: ' + JSON.stringify(error)); 80 }); 81 } 82} 83