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 bluetoothManager from '@ohos.bluetoothManager'; 17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 18import { UiComponent, UiDriver, BY, Component, Driver, UiWindow, ON, MatchPattern, DisplayRotation, ResizeDirection, UiDirection, MouseButton, WindowMode, PointerMatrix, UIElementInfo, UIEventObserver } from '@ohos.UiTest' 19 20export default function btManagerDiscoveryTest() { 21describe('btManagerDiscoveryTest', function() { 22 function sleep(delay) { 23 return new Promise(resovle => setTimeout(resovle, delay)) 24 } 25 26 async function openPhone() { 27 try{ 28 let drivers = Driver.create(); 29 console.info('[bluetooth_js] bt driver create:'+ drivers); 30 await drivers.delayMs(1000); 31 await drivers.wakeUpDisplay(); 32 await drivers.delayMs(5000); 33 await drivers.swipe(1500, 1000, 1500, 100); 34 await drivers.delayMs(10000); 35 } catch (error) { 36 console.info('[bluetooth_js] driver error info:'+ error); 37 } 38 } 39 40 async function clickTheWindow() { 41 try{ 42 let driver = Driver.create(); 43 console.info('[bluetooth_js] bt driver create:'+ driver); 44 await driver.delayMs(1000); 45 await driver.click(950, 2550); 46 await driver.delayMs(5000); 47 await driver.click(950, 2550); 48 await driver.delayMs(3000); 49 } catch (error) { 50 console.info('[bluetooth_js] driver error info:'+ error); 51 } 52 } 53 54 async function tryToEnableBt() { 55 let sta = bluetoothManager.getState(); 56 switch (sta) { 57 case 0: 58 bluetoothManager.enableBluetooth(); 59 await clickTheWindow(); 60 await sleep(10000); 61 let sta1 = bluetoothManager.getState(); 62 console.info('[bluetooth_js] bt turn off:' + JSON.stringify(sta1)); 63 break; 64 case 1: 65 console.info('[bluetooth_js] bt turning on:' + JSON.stringify(sta)); 66 await sleep(3000); 67 break; 68 case 2: 69 console.info('[bluetooth_js] bt turn on:' + JSON.stringify(sta)); 70 break; 71 case 3: 72 bluetoothManager.enableBluetooth(); 73 await clickTheWindow(); 74 await sleep(10000); 75 let sta2 = bluetoothManager.getState(); 76 console.info('[bluetooth_js] bt turning off:' + JSON.stringify(sta2)); 77 break; 78 default: 79 console.info('[bluetooth_js] enable success'); 80 } 81 } 82 beforeAll(async function (done) { 83 console.info('beforeAll called') 84 await openPhone(); 85 done(); 86 }) 87 beforeEach(async function (done) { 88 console.info('beforeEach called') 89 await tryToEnableBt() 90 done() 91 }) 92 afterEach(function () { 93 console.info('afterEach called') 94 }) 95 afterAll(function () { 96 console.info('afterAll called') 97 }) 98 /** 99 * @tc.number SUB_COMMUNICATION_BTMANAGER_DISCOVERY_0100 100 * @tc.name TEST bluetoothDeviceFind on&off 101 * @tc.desc TEST bluetoothDeviceFind 102 * @tc.type Function 103 * @tc.level Level 0 104 */ 105 it('SUB_COMMUNICATION_BTMANAGER_DISCOVERY_0100', 0, async function (done) { 106 function onReceiveEvent(data) { 107 console.info('[bluetooth_js] Device' + JSON.stringify(data) + 108 'length' + data.length); 109 expect(true).assertTrue(data.length > 0); 110 } 111 try { 112 bluetoothManager.on("bluetoothDeviceFind", onReceiveEvent); 113 let result = bluetoothManager.startBluetoothDiscovery(); 114 await sleep(3000); 115 console.info('[bluetooth_js] startDiscovery' + result); 116 expect(result).assertTrue(); 117 bluetoothManager.off('bluetoothDeviceFind', onReceiveEvent); 118 result = bluetoothManager.stopBluetoothDiscovery(); 119 console.info('[bluetooth_js] stopDiscovery' + result); 120 expect(true).assertTrue(); 121 } catch (error) { 122 console.error(`[bluetooth_js]bluetoothDeviceFin error, code is ${error.code}, 123 message is ${error.message}`); 124 } 125 done(); 126 }) 127}) 128}