• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2025 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, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType, Size, Level } from '@ohos/hypium';
19
20export default function SystemBatteryTest() {
21  describe('SystemBatteryTest', () => {
22    // Defines a test suite. Two parameters are supported: test suite name and test suite function.
23    beforeAll(() => {
24      // Presets an action, which is performed only once before all test cases of the test suite start.
25      // This API supports only one parameter: preset action function.
26    })
27    beforeEach(() => {
28      // Presets an action, which is performed before each unit test case starts.
29      // The number of execution times is the same as the number of test cases defined by **it**.
30      // This API supports only one parameter: preset action function.
31    })
32    afterEach(() => {
33      // Presets a clear action, which is performed after each unit test case ends.
34      // The number of execution times is the same as the number of test cases defined by **it**.
35      // This API supports only one parameter: clear action function.
36    })
37    afterAll(() => {
38      // Presets a clear action, which is performed after all test cases of the test suite end.
39      // This API supports only one parameter: clear action function.
40    })
41    /**
42     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0010
43     * @tc.name testget_status_success_test
44     * @tc.desc Battery acquisition kit
45     * @tc.level: Level 1
46     * @tc.type: Function
47     * @tc.size: MediumTest
48     */
49    it('get_status_success_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
50      let TAG = 'get_status_success_test';
51      battery.getStatus({
52        success: (batteryResponse) => {
53          console.info(`${TAG} batteryResponse.charging:${batteryResponse.charging}`);
54          console.info(`${TAG} batteryResponse.charging type:${typeof batteryResponse.charging}`);
55          expect(typeof batteryResponse.charging).assertEqual('number');
56          console.info(`${TAG} batteryResponse.level:${batteryResponse.level}`);
57          console.info(`${TAG} batteryInfo.batterySOC:${batteryInfo.batterySOC}`);
58          expect(batteryResponse.level * 100).assertClose(batteryInfo.batterySOC, 1);
59          done();
60        },
61        fail: (msg, code) => {
62          console.error(`${TAG} fail msg:${msg}`);
63          console.error(`${TAG} fail code:${code}`);
64          expect().assertFail();
65          done();
66        },
67        complete: () => {
68          console.info(`${TAG} getStatus complete`);
69        }
70      });
71    });
72
73    /**
74     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0020
75     * @tc.name testget_status_success_null_test
76     * @tc.desc Battery acquisition kit
77     * @tc.level: Level 3
78     * @tc.type: Function
79     * @tc.size: MediumTest
80     */
81    it('get_status_success_null_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
82      let TAG = 'get_status_success_null_test';
83      battery.getStatus({
84        success: null,
85        fail: (msg, code) => {
86          console.error(`${TAG} fail msg:${msg}`);
87          console.error(`${TAG} fail code:${code}`);
88          expect().assertFail();
89          done();
90        },
91        complete: () => {
92          console.info(`${TAG} getStatus complete`);
93          done();
94        }
95      });
96    });
97
98    /**
99     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0030
100     * @tc.name testget_status_success_empty_test
101     * @tc.desc Battery acquisition kit
102     * @tc.level: Level 3
103     * @tc.type: Function
104     * @tc.size: MediumTest
105     */
106    it('get_status_success_empty_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
107      let TAG = 'get_status_success_empty_test';
108      battery.getStatus({
109        fail: (msg, code) => {
110          console.error(`${TAG} fail msg:${msg}`);
111          console.error(`${TAG} fail code:${code}`);
112          expect().assertFail();
113          done();
114        },
115        complete: () => {
116          console.info(`${TAG} getStatus complete`);
117          done();
118        }
119      });
120    });
121
122    /**
123     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0040
124     * @tc.name testget_status_fail_null_test
125     * @tc.desc Battery acquisition kit
126     * @tc.level: Level 3
127     * @tc.type: Function
128     * @tc.size: MediumTest
129     */
130    it('get_status_fail_null_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
131      let TAG = 'get_status_fail_null_test';
132      battery.getStatus({
133        success: (batteryResponse) => {
134          console.info(`${TAG} batteryResponse.charging:${batteryResponse.charging}`);
135          console.info(`${TAG} batteryResponse.charging type:${typeof batteryResponse.charging}`);
136          expect(typeof batteryResponse.charging).assertEqual('number');
137          console.info(`${TAG} batteryResponse.level:${batteryResponse.level}`);
138          console.info(`${TAG} batteryInfo.batterySOC:${batteryInfo.batterySOC}`);
139          expect(batteryResponse.level * 100).assertClose(batteryInfo.batterySOC, 1);
140          done();
141        },
142        fail: null,
143        complete: () => {
144          console.info(`${TAG} getStatus complete`);
145        }
146      });
147    });
148
149    /**
150     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0050
151     * @tc.name testget_status_fail_empty_test
152     * @tc.desc Battery acquisition kit
153     * @tc.level: Level 3
154     * @tc.type: Function
155     * @tc.size: MediumTest
156     */
157    it('get_status_fail_empty_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
158      let TAG = 'get_status_fail_empty_test';
159      battery.getStatus({
160        success: (batteryResponse) => {
161          console.info(`${TAG} batteryResponse.charging:${batteryResponse.charging}`);
162          console.info(`${TAG} batteryResponse.charging type:${typeof batteryResponse.charging}`);
163          expect(typeof batteryResponse.charging).assertEqual('number');
164          console.info(`${TAG} batteryResponse.level:${batteryResponse.level}`);
165          console.info(`${TAG} batteryInfo.batterySOC:${batteryInfo.batterySOC}`);
166          expect(batteryResponse.level * 100).assertClose(batteryInfo.batterySOC, 1);
167          done();
168        },
169        complete: () => {
170          console.info(`${TAG} getStatus complete`);
171        }
172      });
173    });
174
175    /**
176     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0060
177     * @tc.name testget_status_complete_null_test
178     * @tc.desc Battery acquisition kit
179     * @tc.level: Level 3
180     * @tc.type: Function
181     * @tc.size: MediumTest
182     */
183    it('get_status_complete_null_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
184      let TAG = 'get_status_complete_null_test';
185      battery.getStatus({
186        success: (batteryResponse) => {
187          console.info(`${TAG} batteryResponse.charging:${batteryResponse.charging}`);
188          console.info(`${TAG} batteryResponse.charging type:${typeof batteryResponse.charging}`);
189          expect(typeof batteryResponse.charging).assertEqual('number');
190          console.info(`${TAG} batteryResponse.level:${batteryResponse.level}`);
191          console.info(`${TAG} batteryInfo.batterySOC:${batteryInfo.batterySOC}`);
192          expect(batteryResponse.level * 100).assertClose(batteryInfo.batterySOC, 1);
193          done();
194        },
195        fail: (msg, code) => {
196          console.error(`${TAG} fail msg:${msg}`);
197          console.error(`${TAG} fail code:${code}`);
198          expect().assertFail();
199          done();
200        },
201        complete: null
202      });
203    });
204
205    /**
206     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0070
207     * @tc.name testget_status_complete_empty_test
208     * @tc.desc Battery acquisition kit
209     * @tc.level: Level 3
210     * @tc.type: Function
211     * @tc.size: MediumTest
212     */
213    it('get_status_complete_empty_test', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, async (done) => {
214      let TAG = 'get_status_complete_empty_test';
215      battery.getStatus({
216        success: (batteryResponse) => {
217          console.info(`${TAG} batteryResponse.charging:${batteryResponse.charging}`);
218          console.info(`${TAG} batteryResponse.charging type:${typeof batteryResponse.charging}`);
219          expect(typeof batteryResponse.charging).assertEqual('number');
220          console.info(`${TAG} batteryResponse.level:${batteryResponse.level}`);
221          console.info(`${TAG} batteryInfo.batterySOC:${batteryInfo.batterySOC}`);
222          expect(batteryResponse.level * 100).assertClose(batteryInfo.batterySOC, 1);
223          done();
224        },
225        fail: (msg, code) => {
226          console.error(`${TAG} fail msg:${msg}`);
227          console.error(`${TAG} fail code:${code}`);
228          expect().assertFail();
229          done();
230        },
231      });
232    });
233
234    /**
235     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0080
236     * @tc.name testget_status_all_null
237     * @tc.desc Battery acquisition kit
238     * @tc.level: Level 3
239     * @tc.type: Function
240     * @tc.size: MediumTest
241     */
242    it('get_status_all_null', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, () => {
243      let TAG = 'get_status_all_null';
244      battery.getStatus({
245        success: null,
246        fail: null,
247        complete: null,
248      });
249      console.info(`${TAG} all null`);
250      expect(TAG).assertContain('null');
251    });
252
253    /**
254     * @tc.number SUB_PowerSystem_SystemBattery_JSTest_0090
255     * @tc.name testget_status_all_empty
256     * @tc.desc Battery acquisition kit
257     * @tc.level: Level 3
258     * @tc.type: Function
259     * @tc.size: MediumTest
260     */
261    it('get_status_all_empty', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL3, () => {
262      let TAG = 'get_status_all_empty';
263      battery.getStatus();
264      console.info(`${TAG} all empty`);
265      expect(TAG).assertContain('empty');
266    });
267  })
268}