• 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 abilityAccessCtrl from '@ohos.abilityAccessCtrl'
18import bundle from '@ohos.bundle'
19import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
20import { UiComponent, UiDriver, BY, Component, Driver, UiWindow, ON, MatchPattern, DisplayRotation, ResizeDirection, UiDirection, MouseButton, WindowMode, PointerMatrix, UIElementInfo, UIEventObserver } from '@ohos.UiTest'
21
22const PERMISSION_USER_SET = 1;
23const PERMISSION_USER_NAME1 = "ohos.permission.LOCATION";
24const PERMISSION_USER_NAME2 = 'ohos.permission.LOCATION_IN_BACKGROUND';
25let tokenID = undefined;
26async function grantPerm() {
27    console.info("====grant Permission start====");
28    let appInfo = await bundle.getApplicationInfo('ohos.acts.communication.bluetooth.bluetoothhost', 0, 100);
29    tokenID = appInfo.accessTokenId;
30    console.info("accessTokenId" + appInfo.accessTokenId + " bundleName:" + appInfo.bundleName);
31    let atManager = abilityAccessCtrl.createAtManager();
32    if (atManager != null) {
33        await atManager.grantUserGrantedPermission(tokenID, PERMISSION_USER_NAME1, PERMISSION_USER_SET).then((result) => {
34            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
35            console.info("tokenId" + tokenID + " result:" + result);
36        }).catch((err) => {
37            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
38        });
39        await atManager.grantUserGrantedPermission(tokenID, PERMISSION_USER_NAME2, PERMISSION_USER_SET).then((results) => {
40            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(results));
41            console.info("tokenId" + tokenID + " result:" + results);
42        }).catch((error) => {
43            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(error));
44        });
45    } else {
46        console.info('[permission] case apply permission failed, createAtManager failed');
47    }
48    console.info("====grant Permission end====");
49}
50
51export default function btDiscoveryTest() {
52describe('btDiscoveryTest', function() {
53    function sleep(delay) {
54        return new Promise(resovle => setTimeout(resovle, delay))
55    }
56
57    async function openPhone() {
58        try{
59            let drivers = Driver.create();
60            console.info('[bluetooth_js] bt driver create:'+ drivers);
61            await drivers.delayMs(1000);
62            await drivers.wakeUpDisplay();
63            await drivers.delayMs(5000);
64            await drivers.swipe(1500, 1000, 1500, 100);
65            await drivers.delayMs(10000);
66        } catch (error) {
67            console.info('[bluetooth_js] driver error info:'+ error);
68        }
69    }
70
71    async function clickTheWindow() {
72        try{
73            let driver = Driver.create();
74            console.info('[bluetooth_js] bt driver create:'+ driver);
75            await driver.delayMs(1000);
76            await driver.click(950, 2550);
77            await driver.delayMs(5000);
78            await driver.click(950, 2550);
79            await driver.delayMs(3000);
80        } catch (error) {
81            console.info('[bluetooth_js] driver error info:'+ error);
82        }
83    }
84
85    async function tryToEnableBt() {
86        let sta = bluetooth.getState();
87        switch(sta){
88            case 0:
89                bluetooth.enableBluetooth();
90                await clickTheWindow();
91                await sleep(10000);
92                let sta1 = bluetooth.getState();
93                console.info('[bluetooth_js] bt turn off:'+ JSON.stringify(sta1));
94                break;
95            case 1:
96                console.info('[bluetooth_js] bt turning on:'+ JSON.stringify(sta));
97                await sleep(3000);
98                break;
99            case 2:
100                console.info('[bluetooth_js] bt turn on:'+ JSON.stringify(sta));
101                break;
102            case 3:
103                bluetooth.enableBluetooth();
104                await clickTheWindow();
105                await sleep(10000);
106                let sta2 = bluetooth.getState();
107                console.info('[bluetooth_js] bt turning off:'+ JSON.stringify(sta2));
108                break;
109            default:
110                console.info('[bluetooth_js] enable success');
111        }
112    }
113
114    beforeAll(async function (done) {
115        console.info('beforeAll called')
116        await grantPerm();
117        await openPhone();
118        done()
119    })
120    beforeEach(async function(done) {
121        console.info('beforeEach called')
122        await tryToEnableBt()
123        done()
124    })
125    afterEach(function () {
126        console.info('afterEach called')
127    })
128    afterAll(function () {
129        console.info('afterAll called')
130    })
131
132    /**
133     * @tc.number SUB_COMMUNICATION_BLUETOOTH_DISCOVERY_0100
134     * @tc.name TEST DISCOVERY
135     * @tc.desc TEST DISCOVERY
136     * @tc.type Function
137     * @tc.level Level 0
138     */
139    it('SUB_COMMUNICATION_BLUETOOTH_DISCOVERY_0100', 0, async function (done) {
140        function onReceiveEvent(data){
141            console.info('[bluetooth_js] Device' + JSON.stringify(data)+
142                 'length' + data.length);
143            expect(true).assertTrue(data.length > 0);
144        }
145        bluetooth.on("bluetoothDeviceFind", onReceiveEvent);
146        let result = bluetooth.startBluetoothDiscovery();
147        await sleep(3000);
148        console.info('[bluetooth_js] startDiscovery'+result);
149        expect(result).assertTrue();
150        bluetooth.off('bluetoothDeviceFind', onReceiveEvent);
151        result = bluetooth.stopBluetoothDiscovery();
152        console.info('[bluetooth_js] stopDiscovery'+result);
153        expect(true).assertTrue();
154        done();
155    })
156
157    /**
158     * @tc.number SUB_COMMUNICATION_BLUETOOTH_DISCOVERY_0300
159     * @tc.name TEST DISCOVERY
160     * @tc.desc TEST DISCOVERY api 8.
161     * @tc.type Function
162     * @tc.level Level 1
163     */
164    it('SUB_COMMUNICATION_BLUETOOTH_DISCOVERY_0300', 0, async function (done) {
165        let result1 = bluetooth.setBluetoothScanMode(bluetooth.ScanMode.SCAN_MODE_CONNECTABLE,10);
166        expect(result1).assertTrue();
167        let getScanMode = bluetooth.getBluetoothScanMode();
168        console.info('[bluetooth_js] getScanMode = '+ JSON.stringify(getScanMode));
169        expect(true).assertEqual(getScanMode == bluetooth.ScanMode.SCAN_MODE_CONNECTABLE);
170        let result = bluetooth.startBluetoothDiscovery();
171        await sleep(2000);
172        console.info('[bluetooth_js] startDiscovery1'+result);
173        expect(result).assertTrue();
174        let result2 = bluetooth.startBluetoothDiscovery();
175        console.info('[bluetooth_js] startDiscovery2'+result2);
176        expect(result2).assertFalse();
177        done();
178    })
179})
180}
181
182
183