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 thermal from "@ohos.thermal"
17import { describe, it, expect } from '@ohos/hypium'
18const MSEC_1000 = 1000;
19
20export default function ThermalTest() {
21    describe('ThermalTest', function () {
22        console.log("*************Thermal API Test Begin*************");
23
24        /* @tc.number SUB_PowerSystem_ThermalManager_JSTest_0010
25         * @tc.name testGet_Thermal_Level_JSTest0010
26         * @tc.desc Thermal acquisition kit
27         * @tc.level: Level 1
28         * @tc.type: Functiontion
29         * @tc.size: MediumTest
30         */
31        it('Get_Thermal_Level_JSTest0010', 0, async function (done) {
32            console.info("enter");
33            await new Promise((resolve, reject) => {
34                setTimeout(() => {
35                    let level = thermal.getThermalLevel();
36                    console.info("level is: " + level);
37                    expect(level >= 0 && level <= 7).assertTrue();
38                    resolve();
39                    done();
40                }, MSEC_1000 * 4);
41            })
42        })
43
44        /* @tc.number SUB_PowerSystem_ThermalManager_JSTest_0020
45         * @tc.name testSubscribeAndUnsubscribe_Thermal_Level_JSTest0020
46         * @tc.desc Thermal acquisition kit
47         * @tc.level: Level 1
48         * @tc.type: Functiontion
49         * @tc.size: MediumTest
50         */
51        it('SubscribeAndUnsubscribe_Thermal_Level_JSTest0020', 0, async function (done) {
52            thermal.subscribeThermalLevel((level) => {
53                console.info("level is: " + level);
54                expect(level >= 0 && level <= 7).assertTrue();
55                done();
56            })
57            await new Promise((resolve, reject) => {
58                setTimeout(() => {
59                    thermal.unsubscribeThermalLevel(() => {
60                        console.info("unsubscribe successfully!");
61                    });
62                    resolve();
63                }, MSEC_1000 * 4);
64            })
65        })
66
67        /**
68         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0030
69         * @tc.name testGet_Thermal_Level_Cool_JSTest0030
70         * @tc.desc Get device thermalLevel COOL
71         * @tc.level: Level 1
72         * @tc.type: Functiontion
73         * @tc.size: MediumTest
74         */
75        it('Get_Thermal_Level_Cool_JSTest0030', 0, function () {
76            let thermalLevel = thermal.ThermalLevel.COOL;
77            console.info('ThermalLevel.COOL = ' + thermalLevel);
78            expect(thermalLevel === 0).assertTrue();
79        })
80
81        /**
82         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0040
83         * @tc.name testGet_Thermal_Level_Normal_JSTest0040
84         * @tc.desc Get device thermalLevel NORMAL
85         * @tc.level: Level 1
86         * @tc.type: Functiontion
87         * @tc.size: MediumTest
88         */
89        it('Get_Thermal_Level_Normal_JSTest0040', 0, function () {
90            let thermalLevel = thermal.ThermalLevel.NORMAL;
91            console.info('ThermalLevel.NORMAL = ' + thermalLevel);
92            expect(thermalLevel === 1).assertTrue();
93        })
94
95        /**
96         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0050
97         * @tc.name testGet_Thermal_Level_Warm_JSTest0050
98         * @tc.desc Get device thermalLevel WARM
99         * @tc.level: Level 3
100         * @tc.type: Functiontion
101         * @tc.size: MediumTest
102         */
103        it('Get_Thermal_Level_Warm_JSTest0050', 0, function () {
104            let thermalLevel = thermal.ThermalLevel.WARM;
105            console.info('ThermalLevel.WARM = ' + thermalLevel);
106            expect(thermalLevel === 2).assertTrue();
107        })
108
109        /**
110         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0060
111         * @tc.name testGet_Thermal_Level_Hot_JSTest0060
112         * @tc.desc Get device thermalLevel HOT
113         * @tc.level: Level 3
114         * @tc.type: Functiontion
115         * @tc.size: MediumTest
116         */
117        it('Get_Thermal_Level_Hot_JSTest0060', 0, function () {
118            let thermalLevel = thermal.ThermalLevel.HOT;
119            console.info('ThermalLevel.HOT = ' + thermalLevel);
120            expect(thermalLevel === 3).assertTrue();
121        })
122
123        /**
124         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0070
125         * @tc.name testGet_Thermal_Level_OverHeated_JSTest0070
126         * @tc.desc Get device thermalLevel OVERHEATED
127         * @tc.level: Level 3
128         * @tc.type: Functiontion
129         * @tc.size: MediumTest
130         */
131        it('Get_Thermal_Level_OverHeated_JSTest0070', 0, function () {
132            let thermalLevel = thermal.ThermalLevel.OVERHEATED;
133            console.info('ThermalLevel.OVERHEATED = ' + thermalLevel);
134            expect(thermalLevel === 4).assertTrue();
135        })
136
137        /**
138         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0080
139         * @tc.name testGet_Thermal_Level_Warning_JSTest0080
140         * @tc.desc Get device thermalLevel WARNING
141         * @tc.level: Level 3
142         * @tc.type: Functiontion
143         * @tc.size: MediumTest
144         */
145        it('Get_Thermal_Level_Warning_JSTest0080', 0, function () {
146            let thermalLevel = thermal.ThermalLevel.WARNING;
147            console.info('ThermalLevel.WARNING = ' + thermalLevel);
148            expect(thermalLevel === 5).assertTrue();
149        })
150
151        /**
152         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0090
153         * @tc.name testGet_Thermal_Level_Emergency_JSTest0090
154         * @tc.desc Get device thermalLevel EMERGENCY
155         * @tc.level: Level 3
156         * @tc.type: Functiontion
157         * @tc.size: MediumTest
158         */
159        it('Get_Thermal_Level_Emergency_JSTest0090', 0, function () {
160            let thermalLevel = thermal.ThermalLevel.EMERGENCY;
161            console.info('ThermalLevel.EMERGENCY = ' + thermalLevel);
162            expect(thermalLevel === 6).assertTrue();
163        })
164
165        /**
166         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0100
167         * @tc.name testGet_Thermal_Level_Escape_JSTest0100
168         * @tc.desc Get device thermalLevel ESCAPE
169         * @tc.level: Level 3
170         * @tc.type: Functiontion
171         * @tc.size: MediumTest
172         */
173        it('Get_Thermal_Level_Escape_JSTest0100', 0, function () {
174            let thermalLevel = thermal.ThermalLevel.ESCAPE;
175            console.info('ThermalLevel.ESCAPE = ' + thermalLevel);
176            expect(thermalLevel === 7).assertTrue();
177        })
178
179        /**
180         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0110
181         * @tc.name testGet_Level_JSTest0110
182         * @tc.desc Thermal acquisition kit
183         * @tc.level: Level 3
184         * @tc.type: Functiontion
185         * @tc.size: MediumTest
186         */
187        it('Get_Level_JSTest0110', 0, async function (done) {
188            let level = thermal.getLevel();
189            console.info("Get_Level_JSTest0110 level is: " + level);
190            expect(level >= thermal.ThermalLevel.COOL && level <= thermal.ThermalLevel.ESCAPE).assertTrue();
191            done();
192        })
193
194        /**
195         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0120
196         * @tc.name testRegister_Thermal_Level_Callback_JSTest0120
197         * @tc.desc Thermal acquisition kit
198         * @tc.level: Level 3
199         * @tc.type: Functiontion
200         * @tc.size: MediumTest
201         */
202        it('Register_Thermal_Level_Callback_JSTest0120', 0, async function (done) {
203            try {
204                thermal.registerThermalLevelCallback((value) => {
205                    console.info("Register_Thermal_Level_Callback_JSTest0120 level is: " + value);
206                    let level = thermal.getLevel();
207                    expect(level === value).assertTrue();
208                })
209                done();
210            } catch (error) {
211                console.info('Register_Thermal_Level_Callback_JSTest0120:' + error);
212                expect().assertFail();
213                done();
214            }
215        })
216
217        /**
218         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0130
219         * @tc.name testRegister_Thermal_Level_Callback_JSTest0130
220         * @tc.desc Thermal acquisition kit
221         * @tc.level: Level 3
222         * @tc.type: Functiontion
223         * @tc.size: MediumTest
224         */
225        it('Register_Thermal_Level_Callback_JSTest0130', 0, async function (done) {
226            try {
227                thermal.registerThermalLevelCallback('')
228                done();
229            } catch (error) {
230                console.info('Register_Thermal_Level_Callback_JSTest0130 error:' + error);
231                // 401: Invalid input parameter
232                expect(error.code === 401).assertTrue();
233                done();
234            }
235        })
236
237        /**
238         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0140
239         * @tc.name testUnRegister_Thermal_Level_Callback_JSTest0140
240         * @tc.desc Thermal acquisition kit
241         * @tc.level: Level 3
242         * @tc.type: Functiontion
243         * @tc.size: MediumTest
244         */
245        it('UnRegister_Thermal_Level_Callback_JSTest0140', 0, async function (done) {
246            try {
247                thermal.unregisterThermalLevelCallback('')
248                done();
249            } catch (error) {
250                console.info('UnRegister_Thermal_Level_Callback_JSTest0140 error:' + error);
251                // 401: Invalid input parameter
252                expect(error.code === 401).assertTrue();
253                done();
254            }
255        })
256
257        /**
258         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0150
259         * @tc.name testUnRegister_Thermal_Level_Callback_JSTest0150
260         * @tc.desc Thermal acquisition kit
261         * @tc.level: Level 3
262         * @tc.type: Functiontion
263         * @tc.size: MediumTest
264         */
265        it('UnRegister_Thermal_Level_Callback_JSTest0150', 0, async function (done) {
266            try {
267                thermal.unregisterThermalLevelCallback()
268                done();
269            } catch (error) {
270                console.info('UnRegister_Thermal_Level_Callback_JSTest0150:' + error);
271                expect().assertFail();
272                done();
273            }
274        })
275
276        /**
277         * @tc.number SUB_PowerSystem_ThermalManager_JSTest_0160
278         * @tc.name testUnRegister_Thermal_Level_Callback_JSTest0160
279         * @tc.desc Thermal acquisition kit
280         * @tc.level: Level 3
281         * @tc.type: Functiontion
282         * @tc.size: MediumTest
283         */
284        it('UnRegister_Thermal_Level_Callback_JSTest0160', 0, async function (done) {
285            try {
286                thermal.unregisterThermalLevelCallback(() => {
287                    expect(true).assertTrue();
288                    done();
289                })
290            } catch (error) {
291                console.info('UnRegister_Thermal_Level_Callback_JSTest0160:' + error);
292                expect().assertFail();
293                done();
294            }
295        })
296    })
297}
298