BluetoothA2dp.test.js 9.7 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
/*
 * 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'


Q
quanli 已提交
20 21
export default function btA2dpConnTest() {
describe('btA2dpConnTest', function () {
J
jiyong_sd 已提交
22 23 24 25 26 27 28 29 30
    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 已提交
31
                await sleep(10000);
Q
quanli 已提交
32 33
                let sta1 = bluetooth.getState();
                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta1));
J
jiyong_sd 已提交
34 35 36 37 38 39 40 41 42 43
                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();
Q
quanli 已提交
44
                await sleep(10000);
Q
quanli 已提交
45 46
                let sta2 = bluetooth.getState();
                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta2));
J
jiyong_sd 已提交
47 48 49 50 51
                break;
            default:
                console.info('[bluetooth_js] enable success');
        }
    }
Q
quanli 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    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 已提交
66 67

    /**
Q
quanli 已提交
68
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0100
Q
quanli 已提交
69 70
     * @tc.name test bluetooth Profile ConnectionState
     * @tc.desc Test getBtConnectionState api.
J
jiyong_sd 已提交
71
     * @tc.type Function
Q
quanli 已提交
72
     * @tc.level Level 2
J
jiyong_sd 已提交
73
     */
Q
quanli 已提交
74
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0100', 0, async function (done) {
Q
quanli 已提交
75 76
        let connState = bluetooth.getBtConnectionState();
        console.info('[bluetooth_js] get bt connection state result' + JSON.stringify(connState));
Q
quanli 已提交
77 78 79 80
        expect(connState).assertEqual(bluetooth.ProfileConnectionState.STATE_DISCONNECTED);
        expect(true).assertTrue(bluetooth.ProfileConnectionState.STATE_CONNECTING!= connState );
        expect(true).assertTrue(bluetooth.ProfileConnectionState.STATE_CONNECTED!= connState );
        expect(true).assertTrue(bluetooth.ProfileConnectionState.STATE_DISCONNECTING!= connState );
J
jiyong_sd 已提交
81 82 83 84
        done();
    })

    /**
Q
quanli 已提交
85
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0200
Q
quanli 已提交
86 87
     * @tc.name test A2DP Connect
     * @tc.desc Test connect api.
J
jiyong_sd 已提交
88
     * @tc.type Function
Q
quanli 已提交
89
     * @tc.level Level 1
J
jiyong_sd 已提交
90
     */
Q
quanli 已提交
91
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0200', 0, async function (done) {
Q
quanli 已提交
92 93 94 95 96
        function StateChangeParam(data) {
            console.info("[bluetooth_js] a2dp state " + JSON.stringify(data) +
            'deviceId: ' + data.deviceId + 'state:'+ data.state);
            expect(true).assertEqual(data !=null);
        }
Q
quanli 已提交
97
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
98 99
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        a2dpSrc.on('connectionStateChange', StateChangeParam);
Q
quanli 已提交
100
        a2dpSrc.connect('11:22:33:44:55:77');
Q
quanli 已提交
101
        await sleep(6000);
Q
quanli 已提交
102
       
Q
quanli 已提交
103
        a2dpSrc.off('connectionStateChange', StateChangeParam);
J
jiyong_sd 已提交
104 105 106
        done();
    })

Q
quanli 已提交
107 108
   
    /**
Q
quanli 已提交
109
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0300
Q
quanli 已提交
110 111 112 113 114
     * @tc.name test A2DP disconnect
     * @tc.desc Test disconnect api.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
115
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0300', 0, async function (done) {
Q
quanli 已提交
116 117 118 119 120
        function StateChangeParam(data) {
            console.info("[bluetooth_js] a2dp state " + JSON.stringify(data) +
            'deviceId: ' + data.deviceId + 'state:'+ data.state);
            expect(true).assertEqual(data !=null);
        }
Q
quanli 已提交
121
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
122 123 124 125 126 127 128 129 130
        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 已提交
131 132

    /**
Q
quanli 已提交
133
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0400
Q
quanli 已提交
134 135
     * @tc.name test a invaild A2DP disconnect
     * @tc.desc Test disconnect api.
J
jiyong_sd 已提交
136
     * @tc.type Function
Q
quanli 已提交
137
     * @tc.level Level 3
J
jiyong_sd 已提交
138
     */
Q
quanli 已提交
139
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0400', 0, async function (done) {
Q
quanli 已提交
140
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
141 142 143 144
        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 已提交
145 146 147
        done();
    })

Q
quanli 已提交
148
     /**
Q
quanli 已提交
149
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0500
Q
quanli 已提交
150 151 152 153 154
     * @tc.name test Get A2DP ConnectionState
     * @tc.desc Test getProfileConnState api.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
155
      it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0500', 0, async function (done) {
Q
quanli 已提交
156
        let a2dpSrcConn = bluetooth.getProfileConnState(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
157
        console.info('[bluetooth_js]get a2dp result:' + JSON.stringify(a2dpSrcConn));
Q
quanli 已提交
158
        expect(a2dpSrcConn).assertEqual(bluetooth.ProfileConnectionState.STATE_DISCONNECTED);
Q
quanli 已提交
159 160 161 162
        done();
    })
    
    /**
Q
quanli 已提交
163
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0600
Q
quanli 已提交
164 165 166 167 168
     * @tc.name test a invaild A2DP Connect
     * @tc.desc Test connect api.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
169
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0600', 0, async function (done) {
Q
quanli 已提交
170
        let a2dpSrc = bluetooth.getProfileInst(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
171 172 173 174 175
        let conn = a2dpSrc.connect('test');
        console.info('[bluetooth_js]a2dp invaild connect:' + JSON.stringify(conn));
        expect(conn).assertFalse();
        done();
    })
J
jiyong_sd 已提交
176 177

    /**
Q
quanli 已提交
178
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0700
Q
quanli 已提交
179 180
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
J
jiyong_sd 已提交
181
     * @tc.type Function
Q
quanli 已提交
182
     * @tc.level Level 3
J
jiyong_sd 已提交
183
     */
Q
quanli 已提交
184
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0700', 0, async function (done) {
Q
quanli 已提交
185
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
186 187
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let ret = a2dpSrc.getDeviceState('11:22:33:44:55:66');
Q
quanli 已提交
188
        expect(ret).assertEqual(bluetooth.ProfileConnectionState.STATE_DISCONNECTED);
J
jiyong_sd 已提交
189 190 191
        done();
    })

Q
quanli 已提交
192
    /**
Q
quanli 已提交
193
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0800
Q
quanli 已提交
194 195 196 197 198
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
199
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0800', 0, async function (done) {
Q
quanli 已提交
200
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
201 202
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let ret = a2dpSrc.getDeviceState('test');
Q
quanli 已提交
203
        expect(ret).assertEqual(bluetooth.ProfileConnectionState.STATE_DISCONNECTED);
Q
quanli 已提交
204 205
        done();
    })
J
jiyong_sd 已提交
206

Q
quanli 已提交
207
    /**
Q
quanli 已提交
208
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0900
Q
quanli 已提交
209 210 211 212 213
     * @tc.name test  get A2DP Playing State
     * @tc.desc Test getPlayingState api.
     * @tc.type Function
     * @tc.level Level 3
     */
Q
quanli 已提交
214
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_0900', 0, async function (done) {
Q
quanli 已提交
215
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
216 217 218
        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));
Q
quanli 已提交
219 220
        expect(state).assertEqual(bluetooth.PlayingState.STATE_NOT_PLAYING);
        expect(true).assertEqual(state!=bluetooth.PlayingState.STATE_PLAYING);
Q
quanli 已提交
221 222
        done();
    })
J
jiyong_sd 已提交
223 224

    /**
Q
quanli 已提交
225
     * @tc.number SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_1000
Q
quanli 已提交
226 227
     * @tc.name test getDevice A2DP State.
     * @tc.desc Test getDeviceState api.
J
jiyong_sd 已提交
228
     * @tc.type Function
Q
quanli 已提交
229
     * @tc.level Level 1
J
jiyong_sd 已提交
230
     */
Q
quanli 已提交
231
    it('SUB_COMMUNICATION_BLUETOOTH_A2DPCONN_1000', 0, async function (done) {
Q
quanli 已提交
232
        let a2dpSrc = bluetooth.getProfile(bluetooth.ProfileId.PROFILE_A2DP_SOURCE);
Q
quanli 已提交
233 234 235
        console.info('[bluetooth_js]a2dp get profile result:' + JSON.stringify(a2dpSrc));
        let retArray = a2dpSrc.getConnectionDevices();
        expect(true).assertEqual(retArray.length>=0);
J
jiyong_sd 已提交
236 237 238
        done();
    })

Q
quanli 已提交
239

J
jiyong_sd 已提交
240 241
})

Q
quanli 已提交
242
}