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 */ 15 16import battery from '@system.battery'; 17import batteryInfo from '@ohos.batteryInfo'; 18import { describe, it, expect } from '@ohos/hypium'; 19 20function successFunc(data, tag) { 21 console.log(tag + ": level: " + data.level + ", charging: " + data.charging); 22 let soc = (batteryInfo.batterySOC * 0.01); 23 expect(fabs(soc - data.level) <= 1e-9).assertTrue(); 24 if (batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.ENABLE || 25 batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.FULL) { 26 expect(data.charging).assertTrue(); 27 } else { 28 expect(data.charging).assertFalse(); 29 } 30} 31 32function failFunc(data, code, tag) { 33 console.log(tag + ": data: " + data + ", code: " + code); 34 expect().assertFail(); 35} 36 37function completeFunc(tag) { 38 console.log(tag + ": The device information is obtained successfully."); 39} 40 41export default function SystemBatteryTest() { 42 describe('SystemBatteryTest', function () { 43 console.log("*************System Battery Unit Test Begin*************"); 44 45 /** 46 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0010 47 * @tc.name get_status_success_test 48 * @tc.desc Battery acquisition kit 49 */ 50 it('get_status_success_test', 0, function () { 51 battery.getStatus({ 52 success: (data) => { 53 successFunc(data, successTest); 54 }, 55 fail: (data, code) => { 56 failFunc(data, code, successTest); 57 }, 58 complete: () => { 59 completeFunc(successTest); 60 } 61 }); 62 }); 63 64 /** 65 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0020 66 * @tc.name get_status_success_null_test 67 * @tc.desc Battery acquisition kit 68 */ 69 it('get_status_success_null_test', 0, function () { 70 battery.getStatus({ 71 success: null, 72 fail: (data, code) => { 73 failFunc(data, code, successNullTest); 74 }, 75 complete: () => { 76 completeFunc(successNullTest); 77 } 78 }); 79 }); 80 81 /** 82 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0030 83 * @tc.name get_status_success_empty_test 84 * @tc.desc Battery acquisition kit 85 */ 86 it('get_status_success_empty_test', 0, function () { 87 battery.getStatus({ 88 fail: (data, code) => { 89 failFunc(data, code, successEmptyTest); 90 }, 91 complete: () => { 92 completeFunc(successEmptyTest); 93 } 94 }); 95 }); 96 97 /** 98 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0040 99 * @tc.name get_status_fail_null_test 100 * @tc.desc Battery acquisition kit 101 */ 102 it('get_status_fail_null_test', 0, function () { 103 battery.getStatus({ 104 success: (data) => { 105 successFunc(data, failNullTest); 106 }, 107 fail: null, 108 complete: () => { 109 completeFunc(failNullTest); 110 } 111 }); 112 }); 113 114 /** 115 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0050 116 * @tc.name get_status_fail_empty_test 117 * @tc.desc Battery acquisition kit 118 */ 119 it('get_status_fail_empty_test', 0, function () { 120 battery.getStatus({ 121 success: () => { 122 successFunc(data, failEmptyTest); 123 }, 124 complete: () => { 125 completeFunc(failEmptyTest); 126 } 127 }); 128 }); 129 130 /** 131 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0060 132 * @tc.name get_status_complete_null_test 133 * @tc.desc Battery acquisition kit 134 */ 135 it('get_status_complete_null_test', 0, function () { 136 battery.getStatus({ 137 success: (data) => { 138 successFunc(data, completeNullTest); 139 }, 140 fail: (data, code) => { 141 failFunc(data, code, completeNullTest); 142 }, 143 complete: null 144 }); 145 }); 146 147 /** 148 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0070 149 * @tc.name get_status_complete_empty_test 150 * @tc.desc Battery acquisition kit 151 */ 152 it('get_status_complete_empty_test', 0, function () { 153 battery.getStatus({ 154 success: (data) => { 155 successFunc(data, completeEmptyTest); 156 }, 157 fail: (data, code) => { 158 failFunc(data, code, completeEmptyTest); 159 } 160 }); 161 }); 162 163 /** 164 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0080 165 * @tc.name get_status_all_null 166 * @tc.desc Battery acquisition kit 167 */ 168 it('get_status_all_null', 0, function () { 169 let allNull = false; 170 battery.getStatus({ 171 success: null, 172 fail: null, 173 complete: null, 174 }); 175 expect(!allNull).assertTrue(); 176 }); 177 178 /** 179 * @tc.number SUB_PowerSystem_BatteryManager_JSTest_0090 180 * @tc.name get_status_all_empty 181 * @tc.desc Battery acquisition kit 182 */ 183 it('get_status_all_empty', 0, function () { 184 let allNull = false; 185 battery.getStatus(); 186 expect(!allNull).assertTrue(); 187 }); 188 }) 189}