BluetoothA2dp.test.js 10.5 KB
Newer Older
J
jiyong_sd 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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';
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'


let ProfileId = {
Q
quanli 已提交
21
    
J
jiyong_sd 已提交
22 23 24 25 26 27 28 29 30 31 32
    PROFILE_A2DP_SOURCE : 1,
    PROFILE_HANDS_FREE_AUDIO_GATEWAY : 4,
    PROFILE_HID_HOST : 6,
    PROFILE_PAN_NETWORK : 7
}

let PlayingState = {
        STATE_NOT_PLAYING : 0x0000 ,
        STATE_PLAYING : 0x0001,
    }

Q
quanli 已提交
33 34 35 36 37 38 39
let ProfileConnectionState=
    {
        STATE_CONNECTING : 1,
        STATE_CONNECTED : 2,
        STATE_DISCONNECTED : 0,
        STATE_DISCONNECTING : 3
    }
J
jiyong_sd 已提交
40 41 42 43 44 45 46 47 48 49 50 51

export default function bluetoothhostTest_host_1() {
describe('bluetoothhostTest_host_1', function () {
    function sleep(delay) {
        return new Promise(resovle => setTimeout(resovle, delay))
    }

    async function tryToEnableBt() {
        let sta = bluetooth.getState();
        switch(sta){
            case 0:
                bluetooth.enableBluetooth();
Q
quanli 已提交
52 53 54
                await sleep(5000);
                let sta1 = bluetooth.getState();
                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta1));
J
jiyong_sd 已提交
55 56 57 58 59 60 61 62 63 64 65
                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();
                await sleep(3000);
Q
quanli 已提交
66 67
                let sta2 = bluetooth.getState();
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta2));
J
jiyong_sd 已提交
68 69 70 71 72
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
Q
quanli 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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')
    })
J
jiyong_sd 已提交
87 88

    /**
Q
quanli 已提交
89 90 91 92 93
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0100
     * @tc.name test bluetooth Profile ConnectionState
     * @tc.desc Test getBtConnectionState api.
     * @tc.size MEDIUM
     * @ since 7
J
jiyong_sd 已提交
94
     * @tc.type Function
Q
quanli 已提交
95
     * @tc.level Level 2
J
jiyong_sd 已提交
96
     */
Q
quanli 已提交
97 98 99 100 101 102 103
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0100', 0, async function (done) {
        let connState = bluetooth.getBtConnectionState();
        console.info('[bluetooth_js] get bt connection state result' + JSON.stringify(connState));
        expect(connState).assertEqual(ProfileConnectionState.STATE_DISCONNECTED);
        expect(true).assertTrue(ProfileConnectionState.STATE_CONNECTING!= connState );
        expect(true).assertTrue(ProfileConnectionState.STATE_CONNECTED!= connState );
        expect(true).assertTrue(ProfileConnectionState.STATE_DISCONNECTING!= connState );
J
jiyong_sd 已提交
104 105 106 107
        done();
    })

    /**
Q
quanli 已提交
108 109 110 111 112
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0200
     * @tc.name test A2DP Connect
     * @tc.desc Test connect api.
     * @tc.size MEDIUM
     * @ since 8
J
jiyong_sd 已提交
113
     * @tc.type Function
Q
quanli 已提交
114
     * @tc.level Level 1
J
jiyong_sd 已提交
115
     */
Q
quanli 已提交
116 117 118 119 120 121 122 123 124 125
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0200', 0, async function (done) {
        function StateChangeParam(data) {
            console.info("[bluetooth_js] a2dp state " + JSON.stringify(data) +
            'deviceId: ' + data.deviceId + 'state:'+ data.state);
            expect(true).assertEqual(data !=null);
        }
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        a2dpSrc.on('connectionStateChange', StateChangeParam);
        let conn = a2dpSrc.connect('11:22:33:44:55:77');
Q
quanli 已提交
126
        await sleep(6000);
Q
quanli 已提交
127 128 129
        console.info('[bluetooth_js]a2dp connect result:' + JSON.stringify(conn));
        expect(conn).assertTrue();
        a2dpSrc.off('connectionStateChange', StateChangeParam);
J
jiyong_sd 已提交
130 131 132
        done();
    })

Q
quanli 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
   
    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0300
     * @tc.name test A2DP disconnect
     * @tc.desc Test disconnect api.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0300', 0, async function (done) {
        function StateChangeParam(data) {
            console.info("[bluetooth_js] a2dp state " + JSON.stringify(data) +
            'deviceId: ' + data.deviceId + 'state:'+ data.state);
            expect(true).assertEqual(data !=null);
        }
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        a2dpSrc.on('connectionStateChange', StateChangeParam);
        await sleep(6000);
        let conn = a2dpSrc.disconnect('11:22:33:44:55:66');
        console.info('[bluetooth_js]a2dp disconnect result:' + JSON.stringify(conn));
        expect(conn).assertFalse();
        a2dpSrc.off('connectionStateChange', StateChangeParam);
        done();
    })
J
jiyong_sd 已提交
159 160

    /**
Q
quanli 已提交
161 162 163 164 165
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0400
     * @tc.name test a invaild A2DP disconnect
     * @tc.desc Test disconnect api.
     * @tc.size MEDIUM
     * @ since 8
J
jiyong_sd 已提交
166
     * @tc.type Function
Q
quanli 已提交
167
     * @tc.level Level 3
J
jiyong_sd 已提交
168
     */
Q
quanli 已提交
169 170 171 172 173 174
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0400', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let conn = a2dpSrc.disconnect('test');
        console.info('[bluetooth_js]a2dp disconnect1 result:' + JSON.stringify(conn));
        expect(conn).assertFalse();
J
jiyong_sd 已提交
175 176 177
        done();
    })

Q
quanli 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
     /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0500
     * @tc.name test Get A2DP ConnectionState
     * @tc.desc Test getProfileConnState api.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
      it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0500', 0, async function (done) {
        let a2dpSrcConn = bluetooth.getProfileConnState(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]get a2dp result:' + JSON.stringify(a2dpSrcConn));
        expect(a2dpSrcConn).assertEqual(ProfileConnectionState.STATE_DISCONNECTED);
        done();
    })
    
    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0600
     * @tc.name test a invaild A2DP Connect
     * @tc.desc Test connect api.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0600', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        let conn = a2dpSrc.connect('test');
        console.info('[bluetooth_js]a2dp invaild connect:' + JSON.stringify(conn));
        expect(conn).assertFalse();
        done();
    })
J
jiyong_sd 已提交
210 211

    /**
Q
quanli 已提交
212 213 214 215 216
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0700
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
     * @tc.size MEDIUM
     * @ since 8
J
jiyong_sd 已提交
217
     * @tc.type Function
Q
quanli 已提交
218
     * @tc.level Level 3
J
jiyong_sd 已提交
219
     */
Q
quanli 已提交
220 221 222 223 224
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0700', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let ret = a2dpSrc.getDeviceState('11:22:33:44:55:66');
        expect(ret).assertEqual(ProfileConnectionState.STATE_DISCONNECTED);
J
jiyong_sd 已提交
225 226 227
        done();
    })

Q
quanli 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0800
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0800', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let ret = a2dpSrc.getDeviceState('test');
        expect(ret).assertEqual(ProfileConnectionState.STATE_DISCONNECTED);
        done();
    })
J
jiyong_sd 已提交
244

Q
quanli 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    /**
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0900
     * @tc.name test  get A2DP Playing State
     * @tc.desc Test getPlayingState api.
     * @tc.size MEDIUM
     * @ since 8
     * @tc.type Function
     * @tc.level Level 3
     */
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_0900', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let state = a2dpSrc.getPlayingState('11:22:33:44:55:66');
        console.info('[bluetooth_js]a2dp getPlayingState result:' + JSON.stringify(state));
        expect(state).assertEqual(PlayingState.STATE_NOT_PLAYING);
        expect(true).assertEqual(state!=PlayingState.STATE_PLAYING);
        done();
    })
J
jiyong_sd 已提交
263 264

    /**
Q
quanli 已提交
265 266 267 268 269
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_1000
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
     * @tc.size MEDIUM
     * @ since 8
J
jiyong_sd 已提交
270
     * @tc.type Function
Q
quanli 已提交
271
     * @tc.level Level 1
J
jiyong_sd 已提交
272
     */
Q
quanli 已提交
273 274 275 276 277
    it('SUB_COMMUNICATION_BLUETOOTH_BR_A2DP_Conn_1000', 0, async function (done) {
        let a2dpSrc = bluetooth.getProfile(ProfileId.PROFILE_A2DP_SOURCE);
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let retArray = a2dpSrc.getConnectionDevices();
        expect(true).assertEqual(retArray.length>=0);
J
jiyong_sd 已提交
278 279 280
        done();
    })

Q
quanli 已提交
281

J
jiyong_sd 已提交
282 283 284
})

}
Q
quanli 已提交
285