SensorOnOffTest.test_Gyroscope_Uncalibrated.js 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import sensor from '@ohos.sensor'

import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'

describe("SensorJsTest", function () {
    beforeAll(function () {

        /*
         * @tc.setup: setup invoked before all testcases
         */
        console.info('beforeAll caled')
    })

    afterAll(function () {

        /*
         * @tc.teardown: teardown invoked after all testcases
         */
        console.info('afterAll caled')
    })

    beforeEach(function () {

        /*
         * @tc.setup: setup invoked before each testcases
         */
        console.info('beforeEach caled')
    })

    afterEach(function () {

        /*
         * @tc.teardown: teardown invoked after each testcases
         */
        console.info('afterEach caled')
    })

    /*
53 54
     * @tc.number: SUB_SensorsSystem_Gyroscope_Uncalibrated_JSTest_0010
     * @tc.name: SensorGyroscopeUncalibratedJSTest001
55 56
     * @tc.desc:verify app info is not null
     */
57 58
    it("SUB_SensorsSystem_Gyroscope_Uncalibrated_JSTest_0010", 0, async function (done) {
        console.info('-----------------SUB_SensorsSystem_Gyroscope_Uncalibrated_JSTest_0010----------------------');
59 60 61 62
        function offPromise() {
            return new Promise((resolve, reject) => {
                sensor.off(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, (error) => {
                    if (error) {
63
                        console.info('SensorGyroscopeUncalibratedJSTest001  off error');
64 65 66 67 68 69
                        expect(false).assertTrue();
                        console.info('setTimeout ..start')
                        setTimeout((err) => {
                            reject(err);
                        }, 500);
                    } else {
70
                        console.info('SensorGyroscopeUncalibratedJSTest001  off success');
71 72 73 74 75 76 77 78 79 80 81 82
                        expect(true).assertTrue();
                        setTimeout(() => {
                            resolve();
                        }, 500);
                    }
                }, 1000)
            })
        }

        let promise = new Promise((resolve, reject) => {
            sensor.on(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, function (error, data) {
                if (error) {
83
                    console.info('SensorGyroscopeUncalibratedJSTest001  on error');
84 85 86 87 88
                    expect(false).assertTrue();
                    setTimeout((err) => {
                        reject(err);
                    }, 500);
                } else {
89 90
                    console.info('SensorGyroscopeUncalibratedJSTest001  on  success, x: ' + data.x + "y: " 
                        + data.y + "z: " 
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                        + data.z + "biasX:" + data.biasX + "biasY: " + data.biasY + "biasZ: " + data.biasZ);
                    expect(typeof (data.x)).assertEqual("number");
                    expect(typeof (data.y)).assertEqual("number");
                    expect(typeof (data.z)).assertEqual("number");
                    expect(typeof (data.biasX)).assertEqual("number");
                    expect(typeof (data.biasY)).assertEqual("number");
                    expect(typeof (data.biasZ)).assertEqual("number");
                    setTimeout(() => {
                        resolve();
                    }, 500);
                }
            });
        })

        await promise.then(() => {
            return offPromise();
        }, () => {
108
            console.info("SensorGyroscopeUncalibratedJSTest001 reject");
109 110 111 112 113
        })
        done();
    })

    /*
114 115
     * @tc.number: SUB_SensorsSystem_Gyroscope_Uncalibrated_JSTest_0050
     * @tc.name: SensorGyroscopeUncalibratedJSTest005
116 117
     * @tc.desc:verify app info is not null
     */
118
    it("SUB_SensorsSystem_Gyroscope_Uncalibrated_JSTest_0050", 0, async function (done) {
119 120
        function onceSensorCallback(error, data) {
            if (error) {
121
                console.info('SensorGyroscopeUncalibratedJSTest005  once error');
122 123
                expect(false).assertTrue();
            } else {
124
                console.info('SensorGyroscopeUncalibratedJSTest005  on  success, x: ' + data.x + "y: " + data.y + "z: " 
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                    + data.z + "biasX:" + data.biasX + "biasY: " + data.biasY + "biasZ: " + data.biasZ);
                expect(typeof (data.x)).assertEqual("number");
                expect(typeof (data.y)).assertEqual("number");
                expect(typeof (data.z)).assertEqual("number");
                expect(typeof (data.biasX)).assertEqual("number");
                expect(typeof (data.biasY)).assertEqual("number");
                expect(typeof (data.biasZ)).assertEqual("number");
            }
            setTimeout(() => {
                done();
            }, 500);
        }
        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED, onceSensorCallback);
    })
})