• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2021 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 sensor from '@ohos.sensor'
16
17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
18
19describe("SensorJsTest", function () {
20    function callback(data) {
21        console.info("callback" + JSON.stringify(data));
22        expect(typeof(data.intensity)).assertEqual("number");
23    }
24
25    function callback2(data) {
26        console.info("callback2" + JSON.stringify(data));
27        expect(typeof(data.intensity)).assertEqual("number");
28    }
29
30    beforeAll(function(data) {
31        /*
32         * @tc.setup: setup invoked before all testcases
33         */
34         console.info('beforeAll caled')
35    })
36
37    afterAll(function() {
38        /*
39         * @tc.teardown: teardown invoked after all testcases
40         */
41         console.info('afterAll caled')
42    })
43
44    beforeEach(function() {
45        /*
46         * @tc.setup: setup invoked before each testcases
47         */
48         console.info('beforeEach caled')
49    })
50
51    afterEach(function() {
52        /*
53         * @tc.teardown: teardown invoked after each testcases
54         */
55         console.info('afterEach caled')
56    })
57
58    /*
59     * @tc.name:SensorJsTest001
60     * @tc.desc:verify app info is not null
61     * @tc.type: FUNC
62     * @tc.require: Issue Number
63     */
64    it("SensorJsTest001", 0, async function (done) {
65        console.info('----------------------SensorJsTest001---------------------------');
66        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
67        setTimeout(()=>{
68            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT);
69            done();
70        }, 500);
71    })
72
73    /*
74     * @tc.name:SensorJsTest002
75     * @tc.desc:verify app info is not null
76     * @tc.type: FUNC
77     * @tc.require: Issue Number
78     */
79    it("SensorJsTest002", 0, async function (done) {
80        console.info('----------------------SensorJsTest002---------------------------');
81        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000});
82        setTimeout(()=>{
83            console.info('----------------------SensorJsTest002 off in---------------------------');
84            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT);
85            console.info('----------------------SensorJsTest002 off end---------------------------');
86            done();
87        }, 500);
88    })
89
90    /*
91     * @tc.name:SensorJsTest003
92     * @tc.desc:verify app info is not null
93     * @tc.type: FUNC
94     * @tc.require: Issue Number
95     */
96    it("SensorJsTest003", 0, function (done) {
97        console.info('----------------------SensorJsTest003---------------------------');
98        function onSensorCallback(data) {
99            console.info('SensorJsTest003  on error');
100            expect(false).assertTrue();
101            done();
102        }
103        try {
104            sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback, {'interval': 100000000}, 5);
105        } catch (error) {
106            console.info(error);
107            expect(true).assertTrue();
108            done();
109        }
110    })
111
112    /*
113     * @tc.name:SensorJsTest004
114     * @tc.desc:verify app info is not null
115     * @tc.type: FUNC
116     * @tc.require: Issue Number
117     */
118    it("SensorJsTest004", 0, async function (done) {
119        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
120        setTimeout(()=>{
121            expect(true).assertTrue();
122            done();
123        }, 500);
124    })
125
126    /*
127     * @tc.name:SensorJsTest005
128     * @tc.desc:verify app info is not null
129     * @tc.type: FUNC
130     * @tc.require: Issue Number
131     */
132    it("SensorJsTest005", 0, function (done) {
133        function onceSensorCallback(data) {
134            console.info('SensorJsTest005  on error');
135            expect(false).assertTrue();
136            done();
137        }
138        try{
139            sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onceSensorCallback, 5);
140        } catch (error) {
141            console.info(error);
142            expect(true).assertTrue();
143            done();
144        }
145    })
146
147    /*
148     * @tc.name:SensorJsTest006
149     * @tc.desc:verify app info is not null
150     * @tc.type: FUNC
151     * @tc.require: Issue Number
152     */
153    it("SensorJsTest006", 0, async function (done) {
154        try {
155            sensor.off(string, "");
156        } catch (error) {
157            console.info(error);
158            expect(true).assertTrue();
159            done();
160        }
161    })
162
163    /*
164     * @tc.name:SensorJsTest007
165     * @tc.desc:verify app info is not null
166     * @tc.type: FUNC
167     * @tc.require: Issue Number
168     */
169    it("SensorJsTest007", 0, async function (done) {
170        function onSensorCallback(data) {
171            console.info('SensorJsTest007  on error');
172            expect(false).assertTrue();
173            done();
174        }
175        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback);
176        sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, onSensorCallback);
177        setTimeout(()=>{
178            expect(true).assertTrue();
179            done();
180        }, 500);
181    })
182
183    /*
184     * @tc.name:SensorJsTest008
185     * @tc.desc:verify app info is not null
186     * @tc.type: FUNC
187     * @tc.require: Issue Number
188     */
189    it("SensorJsTest008", 0, async function (done) {
190        function onSensorCallback(data) {
191            console.info('SensorJsTest008  on error');
192            expect(false).assertTrue();
193            done();
194        }
195        try {
196            sensor.off(1000000, onSensorCallback);
197        } catch (error) {
198            console.info(error);
199            expect(true).assertTrue();
200            done();
201        }
202    })
203
204    /*
205     * @tc.name:SensorJsTest009
206     * @tc.desc:verify app info is not null
207     * @tc.type: FUNC
208     * @tc.require: Issue Number
209     */
210    it("SensorJsTest009", 0, async function (done) {
211        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
212        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2);
213        setTimeout(()=>{
214            console.info('----------------------SensorJsTest009 off in---------------------------');
215            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT);
216            console.info('----------------------SensorJsTest009 off end---------------------------');
217            done();
218        }, 1000);
219    })
220
221    /*
222     * @tc.name:SensorJsTest010
223     * @tc.desc:verify app info is not null
224     * @tc.type: FUNC
225     * @tc.require: Issue Number
226     */
227    it("SensorJsTest010", 0, async function (done) {
228        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
229        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2);
230        setTimeout(()=>{
231            console.info('----------------------SensorJsTest010 off in---------------------------');
232            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
233            console.info('----------------------SensorJsTest010 off end---------------------------');
234        }, 500);
235        setTimeout(()=>{
236            console.info('----------------------SensorJsTest010 off in---------------------------');
237            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2);
238            console.info('----------------------SensorJsTest010 off end---------------------------');
239            done();
240        }, 1000);
241    })
242
243    /*
244     * @tc.name:SensorJsTest011
245     * @tc.desc:verify app info is not null
246     * @tc.type: FUNC
247     * @tc.require: Issue Number
248     */
249    it("SensorJsTest011", 0, async function (done) {
250        console.info('----------------------SensorJsTest011---------------------------');
251        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000});
252        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2);
253        setTimeout(()=>{
254            console.info('----------------------SensorJsTest011 off in---------------------------');
255            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT);
256            console.info('----------------------SensorJsTest011 off end---------------------------');
257            done();
258        }, 1000);
259    })
260
261    /*
262     * @tc.name:SensorJsTest012
263     * @tc.desc:verify app info is not null
264     * @tc.type: FUNC
265     * @tc.require: Issue Number
266     */
267    it("SensorJsTest012", 0, async function (done) {
268        console.info('----------------------SensorJsTest012---------------------------');
269        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000});
270        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2, {'interval': 100000000});
271        setTimeout(()=>{
272            console.info('----------------------SensorJsTest012 off in---------------------------');
273            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback);
274            console.info('----------------------SensorJsTest012 off end---------------------------');
275        }, 500);
276        setTimeout(()=>{
277            console.info('----------------------SensorJsTest012 off in---------------------------');
278            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2);
279            console.info('----------------------SensorJsTest012 off end---------------------------');
280            done();
281        }, 1000);
282    })
283
284    /*
285     * @tc.name:SensorJsTest013
286     * @tc.desc:verify app info is not null
287     * @tc.type: FUNC
288     * @tc.require: Issue Number
289     */
290    it("SensorJsTest013", 0, async function (done) {
291        console.info('----------------------SensorJsTest013---------------------------');
292        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback, {'interval': 100000000});
293        sensor.on(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT, callback2, {'interval': 100000000});
294        setTimeout(()=>{
295            console.info('----------------------SensorJsTest013 off in---------------------------');
296            sensor.off(sensor.SensorType.SENSOR_TYPE_ID_AMBIENT_LIGHT);
297            console.info('----------------------SensorJsTest013 off end---------------------------');
298            done();
299        }, 1000);
300    })
301})