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