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