• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import bluetooth from '@ohos.bluetooth';
17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
18
19
20
21let ProfileId = {
22    PROFILE_A2DP_SINK : 0,
23    PROFILE_A2DP_SOURCE : 1,
24    PROFILE_AVRCP_CT : 2,
25    PROFILE_AVRCP_TG : 3,
26    PROFILE_HANDS_FREE_AUDIO_GATEWAY : 4,
27    PROFILE_HANDS_FREE_UNIT : 5,
28    PROFILE_HID_HOST : 6,
29    PROFILE_PAN_NETWORK : 7
30}
31
32let PlayingState = {
33        STATE_NOT_PLAYING : 0x0000 ,
34        STATE_PLAYING : 0x0001,
35    }
36
37let a2dpSourceProfile = bluetooth.getProfile(1);
38
39
40function on(ON_VALUE_TEST_ELEMENT) {
41    return new Promise((resolve, reject) => {
42        a2dpSourceProfile.on(ON_VALUE_TEST_ELEMENT, function (err, data) {
43            if (err != undefined) {
44                reject(err);
45            } else {
46                resolve(data);
47            }
48        })
49    });
50}
51
52function off(OFF_VALUE_TEST_ELEMENT) {
53    return new Promise((resolve, reject) => {
54        a2dpSourceProfile.off(OFF_VALUE_TEST_ELEMENT, function (err, data) {
55            if (err != undefined) {
56                reject(err);
57            } else {
58                resolve(data);
59            }
60        })
61    });
62}
63
64
65describe('bluetoothhostTest_host1', function () {
66    beforeAll(function () {
67        console.info('beforeAll called')
68    })
69    beforeEach(function () {
70        console.info('beforeEach called')
71    })
72    afterEach(function () {
73        console.info('afterEach called')
74    })
75    afterAll(function () {
76        console.info('afterAll called')
77    })
78
79    function sleep(delay) {
80        return new Promise(resovle => setTimeout(resovle, delay))
81    }
82
83    async function tryToEnableBt() {
84        let sta = bluetooth.getState();
85        switch(sta){
86            case 0:
87                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta));
88                bluetooth.enableBluetooth();
89                await sleep(3000);
90                break;
91            case 1:
92                console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta));
93                await sleep(3000);
94                break;
95            case 2:
96                console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta));
97                break;
98            case 3:
99                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta));
100                bluetooth.enableBluetooth();
101                await sleep(3000);
102                break;
103            default:
104                console.info('[bluetooth_js] enable success');
105        }
106    }
107
108
109    /**
110     * @tc.number SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_GET_PROFILE_0001
111     * @tc.name testgetprofile
112     * @tc.desc Test getProfile api.
113     * @tc.type Function
114     * @tc.level Level 0
115     */
116    it('SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_GET_PROFILE_0001', 0, async function (done) {
117        console.info('[bluetooth_js] a2dp get profile start');
118        await tryToEnableBt();
119        let proFile = bluetooth.getProfile(1);
120        console.info('[bluetooth_js] a2dp get profile result:' + JSON.stringify(proFile));
121        expect(proFile != null).assertEqual(true);
122        done();
123    })
124
125
126    /**
127     * @tc.number SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_SOURCCE_PROFILE_CONN_0001
128     * @tc.name testa2dpSourceProfileconnect
129     * @tc.desc Test a2dpSourceProfile connect api.
130     * @tc.type Function
131     * @tc.level Level 0
132     */
133    it('SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_SOURCCE_PROFILE_CONN_0001', 0, async function (done) {
134        console.info('[bluetooth_js] a2dpSourceProfile the connect start');
135        await tryToEnableBt();
136        let conn = a2dpSourceProfile.connect('00:00:00:00:00:02');
137        expect(conn).assertTrue();
138        await sleep(3000);
139        let disConn = a2dpSourceProfile.disconnect('00:00:00:00:00:02');
140        console.info('[bluetooth_js] a2dpSourceProfile disconnect:' + JSON.stringify(disConn));
141        expect(disConn).assertTrue();
142        done();
143    })
144
145
146
147    /**
148     * @tc.number SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_GET_PLAYING_STATE_0001
149     * @tc.name testgetPlayingState
150     * @tc.desc Test getPlayingState api.
151     * @tc.type Function
152     * @tc.level Level 0
153     */
154    it('SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_GET_PLAYING_STATE_0001', 0, async function (done) {
155        console.info('[bluetooth_js] a2dpSourceProfile getPlayingState start');
156        await tryToEnableBt();
157        let state = a2dpSourceProfile.getPlayingState('00:00:00:00:00:02');
158        console.info('[bluetooth_js] a2dpSourceProfile the disconnect result:' + state);
159        expect(state).assertEqual(0);
160        await sleep(3000);
161        done();
162    })
163
164
165
166
167
168    /**
169     * @tc.number SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_SOURCCE_PROFILE_ON_0001
170     * @tc.name testa2dpSourceProfileon
171     * @tc.desc Test a2dpSourceProfile on api.
172     * @tc.type Function
173     * @tc.level Level 0
174     */
175    it('SUB_COMMUNACATION_bluetooth_DEVICE_JS_A2DP_SOURCCE_PROFILE_ON_0001', 0, async function (done) {
176        try {
177            await tryToEnableBt();
178            console.info('[bluetooth_js] a2dpSourceProfile the on start');
179            on("connectionStateChange", function (data) {
180                console.info("[bluetooth_js] a2dpSource_on data " + JSON.stringify(data));
181                expect(true).assertEqual(data !=null);
182                console.info("[bluetooth_js] StateChangeParam deviceId" + data.deviceId +
183                "ProfileConnectionState" + data.state);
184            });
185        }catch(e) {
186            expect(null).assertFail();
187        }
188        try {
189            console.info('[bluetooth_js] a2dpSourceProfile the off test start');
190            off("connectionStateChange", function (data) {
191                console.info("[bluetooth_js] a2dpSource_off data-> " + JSON.stringify(data));
192                expect(true).assertEqual(data ==null);
193            });
194        }catch(e) {
195            expect(null).assertFail();
196        }
197        done();
198    })
199
200})
201
202