BRSwitch.test.js 8.0 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2022 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 bluetooth from '@ohos.bluetooth';
17 18 19 20 21
import geolocation from '@ohos.geolocation';
import geolocationm from '@ohos.geoLocationManager';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import bundle from '@ohos.bundle';
import osaccount from '@ohos.account.osAccount';
Q
quanli 已提交
22
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
23 24 25 26 27 28 29 30

let BluetoothState = {
    STATE_OFF: 0,STATE_TURNING_ON: 1,
    STATE_ON: 2,STATE_TURNING_OFF: 3,
    STATE_BLE_TURNING_ON: 4, STATE_BLE_ON: 5,
    STATE_BLE_ON: 6
};

Q
quanli 已提交
31 32 33 34 35 36 37 38 39 40
export default function bluetoothhostTest() {
describe('bluetoothhostTest', function() {
    function sleep(delay) {
        return new Promise(resovle => setTimeout(resovle, delay))
    }
    async function tryToEnableBt() {
        let sta = bluetooth.getState();
        switch(sta){
            case 0:
                bluetooth.enableBluetooth();
41
                await sleep(10000);
Q
quanli 已提交
42 43 44 45 46 47 48 49 50 51 52 53
                let sta1 = bluetooth.getState();
                console.info('[bluetooth_js] Reacquire bt state:'+ JSON.stringify(sta1));
                break;
            case 1:
                console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta));
                await sleep(3000);
                break;
            case 2:
                console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta));
                break;
            case 3:
                bluetooth.enableBluetooth();
54
                await sleep(10000);
Q
quanli 已提交
55 56 57 58 59 60 61
                let sta2 = bluetooth.getState();
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta2));
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	async function applyPermission() {
		let osAccountManager = osaccount.getAccountManager();
		console.info("=== getAccountManager finish");
		let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
		console.info("LocalId is :" + localId);
		let appInfo = await bundle.getApplicationInfo('ohos.acts.location.geolocation.function', 0, localId);
		let atManager = abilityAccessCtrl.createAtManager();
		if (atManager != null) {
        let tokenID = appInfo.accessTokenId;
        console.info('[permission] case accessTokenID is ' + tokenID);
        let permissionName1 = 'ohos.permission.LOCATION';
        let permissionName2 = 'ohos.permission.LOCATION_IN_BACKGROUND';
        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
        await atManager.grantUserGrantedPermission(tokenID, permissionName2, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
		} else {
        console.info('[permission] case apply permission failed, createAtManager failed');
		}
	}
Q
quanli 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    beforeAll(function () {
        console.info('beforeAll called')
    })
    beforeEach(async function(done) {
        console.info('beforeEach called')
        await tryToEnableBt()
        done()
    })
    afterEach(function () {
        console.info('afterEach called')
    })
    afterAll(function () {
        console.info('afterAll called')
    })

    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0100
     * @tc.name testEnableBluetooth and getState
     * @tc.desc Test EnableBluetooth api by promise.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 0
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0100', 0, async function (done) {
Q
quanli 已提交
113 114 115 116
        function onReceiveEvent(data) {
            console.info('bluetooth state001 ='+ JSON.stringify(data));
        }
        await bluetooth.on('stateChange', onReceiveEvent);
Q
quanli 已提交
117
        let state = bluetooth.getState();
Q
quanli 已提交
118
        console.info('[bluetooth_js] get bluetooth state001'+ JSON.stringify(state));
119
        if(state!=bluetooth.BluetoothState.STATE_ON)
Q
quanli 已提交
120 121
        {
            let enable = bluetooth.enableBluetooth();
122
            await sleep(10000);
Q
quanli 已提交
123
            console.info('[bluetooth_js] bluetooth enable001'+JSON.stringify(enable));
Q
quanli 已提交
124 125
            expect(enable).assertTrue();
            let state1 = bluetooth.getState();
Q
quanli 已提交
126
            console.info('[bluetooth_js] enable state001 '+ JSON.stringify(state1));
127
            expect(state1).assertEqual(bluetooth.BluetoothState.STATE_ON);
Q
quanli 已提交
128 129 130
        }
        let disable = bluetooth.disableBluetooth();
        await sleep(3000);
Q
quanli 已提交
131
        console.info('[bluetooth_js] bluetooth disable001'+JSON.stringify(disable));
Q
quanli 已提交
132 133
        expect(disable).assertTrue();
        let state2 = bluetooth.getState();
Q
quanli 已提交
134
        console.info('[bluetooth_js] disable state001 '+ JSON.stringify(state2));
135
        expect(state2).assertEqual(bluetooth.BluetoothState.STATE_OFF);
Q
quanli 已提交
136
        await bluetooth.off('stateChange', onReceiveEvent);
Q
quanli 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
        done()   
    })

    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0300
     * @tc.name testEnableBluetooth
     * @tc.desc Test EnableBluetooth api by promise.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0300', 0, async function (done) {
150
        let enable = bluetooth.enableBluetooth();
Q
quanli 已提交
151 152
        let state = bluetooth.getState();
        console.info('[bluetooth_js] bt open state1 = '+ JSON.stringify(state));
153 154
        expect(state).assertEqual(bluetooth.BluetoothState.STATE_ON);
        if(state==bluetooth.BluetoothState.STATE_ON) {
Q
quanli 已提交
155
            let enable1=bluetooth.enableBluetooth();
156
            await sleep(10000);
Q
quanli 已提交
157 158 159 160
            console.info('[bluetooth_js]enable1 :'+ JSON.stringify(enable1));
            expect(enable1).assertFalse();
            let state1 = bluetooth.getState();
            console.info('[bluetooth_js] bt open state2='+ JSON.stringify(state1));
161
            expect(state1).assertEqual(bluetooth.BluetoothState.STATE_ON);
Q
quanli 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        }else{
            console.info('[bluetooth_js]bluetooth open fail');
        } 
        done();
    })

    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0400
     * @tc.name testEnableBluetooth
     * @tc.desc Test EnableBluetooth api by promise.
     * @tc.size MEDIUM
     * @ since 7
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_Switch_0400', 0, async function (done) {
        let result1= bluetooth.disableBluetooth();
        await sleep(3000);
        console.info('[bluetooth_js] disable result'+ JSON.stringify(result1));
        expect(result1).assertTrue();
        let state = bluetooth.getState();
        console.info('[bluetooth_js] getState1 off = '+ JSON.stringify(state));
184 185
        expect(state).assertEqual(bluetooth.BluetoothState.STATE_OFF);
        if(state == bluetooth.BluetoothState.STATE_OFF) {
Q
quanli 已提交
186 187 188 189 190
            let result2= bluetooth.disableBluetooth();
            console.info('[bluetooth_js]disable result1'+ JSON.stringify(result2));
            expect(result2).assertFalse();
            let state1 = bluetooth.getState();
            console.info('[bluetooth_js] getState4 off = '+ JSON.stringify(state1));
191
            expect(state1).assertEqual(bluetooth.BluetoothState.STATE_OFF);
Q
quanli 已提交
192 193 194 195 196 197
        }
        done();
    })
})
}

198