SensorOnOffTest.test_Linear_Acceleration.js 4.7 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_Linear_Acceleration_JSTest_0010
     * @tc.name: SensorLinearAccelerationJSTest001
55 56
     * @tc.desc:verify app info is not null
     */
57 58
    it("SUB_SensorsSystem_Linear_Acceleration_JSTest_0010", 0, async function (done) {
        console.info('---------------SUB_SensorsSystem_Linear_Acceleration_JSTest_0010-----------------');
59 60 61 62
        function offPromise() {
            return new Promise((resolve, reject) => {
                sensor.off(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, (error) => {
                    if (error) {
63
                        console.info('SensorLinearAccelerationJSTest001  off error');
64 65 66 67 68 69
                        expect(false).assertTrue();
                        console.info('setTimeout ..start')
                        setTimeout((err) => {
                            reject(err);
                        }, 500);
                    } else {
70
                        console.info('SensorLinearAccelerationJSTest001  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_LINEAR_ACCELERATION, function (error, data) {
                if (error) {
83
                    console.info('SensorLinearAccelerationJSTest001  on error');
84 85 86 87 88
                    expect(false).assertTrue();
                    setTimeout((err) => {
                        reject(err);
                    }, 500);
                } else {
89 90
                    console.info('SensorLinearAccelerationJSTest001  on  success, x: ' + data.x + "y: " 
                        + data.y + "z: " + data.z);
91 92 93 94 95 96 97 98 99 100 101 102 103
                    expect(typeof (data.x)).assertEqual("number");
                    expect(typeof (data.y)).assertEqual("number");
                    expect(typeof (data.z)).assertEqual("number");
                    setTimeout(() => {
                        resolve();
                    }, 500);
                }
            });
        })

        await promise.then(() => {
            return offPromise();
        }, () => {
104
            console.info("SensorLinearAccelerationJSTest001 reject");
105 106 107 108 109
        })
        done();
    })

    /*
110 111
     * @tc.number: SUB_SensorsSystem_Linear_Acceleration_JSTest_0050
     * @tc.name: SensorLinearAccelerationJSTest005
112 113
     * @tc.desc:verify app info is not null
     */
114
    it("SUB_SensorsSystem_Linear_Acceleration_JSTest_0050", 0, async function (done) {
115 116
        function onceSensorCallback(error, data) {
            if (error) {
117
                console.info('SensorLinearAccelerationJSTest005  once error');
118 119
                expect(false).assertTrue();
            } else {
120 121
                console.info('SensorLinearAccelerationJSTest005  on  success, x: ' + data.x + "y: " 
                    + data.y + "z: " + data.z);
122 123 124 125 126 127 128 129 130 131 132
                expect(typeof (data.x)).assertEqual("number");
                expect(typeof (data.y)).assertEqual("number");
                expect(typeof (data.z)).assertEqual("number");
            }
            setTimeout(() => {
                done();
            }, 500);
        }
        sensor.once(sensor.SensorType.SENSOR_TYPE_ID_LINEAR_ACCELERATION, onceSensorCallback);
    })
})