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 btManagerPairTest() { 21describe('btManagerPairTest', 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 console.info('[bluetooth_js] clickRequestPermission start'); 42 let driver = Driver.create(); 43 await driver.delayMs(3000); 44 try { 45 let button = await driver.findComponent(ON.text("开启")); 46 await button.click(); 47 await driver.delayMs(3000); 48 console.info('[bluetooth_js] click 开启 end'); 49 } catch (err) { 50 console.info('[bluetooth_js] clickRequestPermission failed. ' + err); 51 } 52 try { 53 let button1 = await driver.findComponent(ON.text("允许")); 54 await button1.click(); 55 await driver.delayMs(3000); 56 console.info('[bluetooth_js] click 允许 end'); 57 } catch (err) { 58 console.info('[bluetooth_js] click 允许 failed. ' + err); 59 } 60 } 61 62 async function tryToEnableBt() { 63 let sta = bluetoothManager.getState(); 64 switch (sta) { 65 case 0: 66 bluetoothManager.enableBluetooth(); 67 await clickTheWindow(); 68 await sleep(10000); 69 let sta1 = bluetoothManager.getState(); 70 console.info('[bluetooth_js] bt turn off:' + JSON.stringify(sta1)); 71 break; 72 case 1: 73 console.info('[bluetooth_js] bt turning on:' + JSON.stringify(sta)); 74 await sleep(3000); 75 break; 76 case 2: 77 console.info('[bluetooth_js] bt turn on:' + JSON.stringify(sta)); 78 break; 79 case 3: 80 bluetoothManager.enableBluetooth(); 81 await clickTheWindow(); 82 await sleep(10000); 83 let sta2 = bluetoothManager.getState(); 84 console.info('[bluetooth_js] bt turning off:' + JSON.stringify(sta2)); 85 break; 86 default: 87 console.info('[bluetooth_js] enable success'); 88 } 89 } 90 beforeAll(async function (done) { 91 console.info('beforeAll called') 92 await openPhone(); 93 done(); 94 }) 95 beforeEach(async function (done) { 96 console.info('beforeEach called') 97 await tryToEnableBt() 98 done() 99 }) 100 afterEach(function () { 101 console.info('afterEach called') 102 }) 103 afterAll(function () { 104 console.info('afterAll called') 105 }) 106 107 /** 108 * @tc.number SUB_COMMUNICATION_BTMANAGER_PAIR_0100 109 * @tc.name testPinRequiredOn 110 * @tc.desc Test pinRequired off api 111 * @tc.size MediumTest 112 * @ since 8 113 * @tc.type Function 114 * @tc.level Level 3 115 */ 116 it('SUB_COMMUNICATION_BTMANAGER_PAIR_0100', 0, async function (done) { 117 function onReceiveEvent(data) { 118 console.info('pin required = '+ JSON.stringify(data)); 119 } 120 try { 121 bluetoothManager.on('pinRequired', onReceiveEvent); 122 bluetoothManager.off('pinRequired', onReceiveEvent); 123 } catch (error) { 124 console.error(`[bluetooth_js]PairDevice error, code is ${error.code}, 125 message is ${error.message}`); 126 expect(true).assertEqual(error != null); 127 } 128 done(); 129 }) 130}) 131}