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 '@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 btPairTest() { 21describe('btPairTest', 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 = bluetooth.getState(); 64 switch(sta){ 65 case 0: 66 bluetooth.enableBluetooth(); 67 await clickTheWindow(); 68 await sleep(10000); 69 let sta1 = bluetooth.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 bluetooth.enableBluetooth(); 81 await clickTheWindow(); 82 await sleep(10000); 83 let sta2 = bluetooth.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_BLUETOOTH_PAIR_0100 109 * @tc.name testPairDevice 110 * @tc.desc Test pairDevice api 111 * @tc.type Function 112 * @tc.size MediumTest 113 * @tc.level Level 2 114 */ 115 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0100', 0, async function (done) { 116 try{ 117 bluetooth.pairDevice("11:22:55:66:33:44"); 118 await sleep(3000); 119 } catch(err) { 120 console.error("pairDevice errCode:" + err.code + ",errMessage:" + err.message); 121 expect(Number(err.code)).assertEqual(2900099); 122 } 123 done(); 124 }) 125 126 /** 127 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0200 128 * @tc.name testPairDevice 129 * @tc.desc Test pairDevice of use vailded address. 130 * @tc.type Function 131 * @tc.size MediumTest 132 * @tc.level Level 0 133 */ 134 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0200', 0, async function (done) { 135 function PinRequiredParam(data) { 136 console.info("[bluetooth_js] pinRequired on:" + 137 JSON.stringify(data.deviceId)+JSON.stringify(data.pinCode)); 138 bluetooth.setDevicePairingConfirmation(data.deviceId,false); 139 } 140 bluetooth.BLE.on('pinRequired', PinRequiredParam); 141 let result = bluetooth.pairDevice("SSS"); 142 console.info("[bluetooth_js] onStartpair -> " + JSON.stringify(result)); 143 expect(result).assertFalse(); 144 bluetooth.BLE.off('pinRequired', PinRequiredParam); 145 done(); 146 }) 147 148 /** 149 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0300 150 * @tc.name testGetRemoteDeviceName 151 * @tc.desc Test get RemoteDeviceName 152 * @tc.type Function 153 * @tc.size MediumTest 154 * @tc.level Level 3 155 */ 156 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0300', 0, async function (done) { 157 let ret = bluetooth.getRemoteDeviceName("00:00:00:00:00:00"); 158 console.info('[bluetooth_js] getRemoteDeviceName ret2:' + JSON.stringify(ret)); 159 expect(ret.length).assertEqual(0); 160 done(); 161 }) 162 163 /** 164 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0400 165 * @tc.name testGetRemoteDeviceClass 166 * @tc.desc Test get RemoteDeviceClass 167 * @tc.type Function 168 * @tc.size MediumTest 169 * @tc.level Level 1 170 */ 171 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0400', 0, async function (done) { 172 let MajorMinorClass = { 173 COMPUTER_UNCATEGORIZED : 0x0100,COMPUTER_DESKTOP : 0x0104, 174 COMPUTER_SERVER : 0x0108,COMPUTER_LAPTOP : 0x010C, 175 COMPUTER_HANDHELD_PC_PDA : 0x0110,COMPUTER_PALM_SIZE_PC_PDA : 0x0114, 176 COMPUTER_WEARABLE : 0x0118,COMPUTER_TABLET : 0x011C, 177 PHONE_UNCATEGORIZED : 0x0200,PHONE_CELLULAR : 0x0204, 178 PHONE_CORDLESS : 0x0208,PHONE_SMART : 0x020C, 179 PHONE_MODEM_OR_GATEWAY : 0x0210,PHONE_ISDN : 0x0214, 180 NETWORK_FULLY_AVAILABLE : 0x0300,NETWORK_1_TO_17_UTILIZED : 0x0320, 181 NETWORK_17_TO_33_UTILIZED : 0x0340,NETWORK_33_TO_50_UTILIZED : 0x0360, 182 NETWORK_60_TO_67_UTILIZED : 0x0380,NETWORK_67_TO_83_UTILIZED : 0x03A0, 183 NETWORK_83_TO_99_UTILIZED : 0x03C0,NETWORK_NO_SERVICE : 0x03E0, 184 AUDIO_VIDEO_UNCATEGORIZED : 0x0400,AUDIO_VIDEO_WEARABLE_HEADSET: 0x0404, 185 AUDIO_VIDEO_HANDSFREE : 0x0408,AUDIO_VIDEO_MICROPHONE : 0x0410, 186 AUDIO_VIDEO_LOUDSPEAKER : 0x0414,AUDIO_VIDEO_HEADPHONES : 0x0418, 187 AUDIO_VIDEO_PORTABLE_AUDIO : 0x041C,AUDIO_VIDEO_CAR_AUDIO : 0x0420, 188 AUDIO_VIDEO_SET_TOP_BOX : 0x0424,AUDIO_VIDEO_HIFI_AUDIO : 0x0428, 189 AUDIO_VIDEO_VCR : 0x042C,AUDIO_VIDEO_VIDEO_CAMERA : 0x0430, 190 AUDIO_VIDEO_CAMCORDER : 0x0434,AUDIO_VIDEO_VIDEO_MONITOR : 0x0438, 191 AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER : 0x043C, 192 AUDIO_VIDEO_VIDEO_CONFERENCING : 0x0440,AUDIO_VIDEO_VIDEO_GAMING_TOY: 0x0448, 193 PERIPHERAL_NON_KEYBOARD_NON_POINTING : 0x0500, 194 PERIPHERAL_KEYBOARD : 0x0540,PERIPHERAL_POINTING_DEVICE : 0x0580, 195 PERIPHERAL_KEYBOARD_POINTING : 0x05C0,PERIPHERAL_UNCATEGORIZED : 0x0500, 196 PERIPHERAL_JOYSTICK : 0x0504,PERIPHERAL_GAMEPAD : 0x0508, 197 PERIPHERAL_REMOTE_CONTROL : 0x05C0,PERIPHERAL_SENSING_DEVICE : 0x0510, 198 PERIPHERAL_DIGITIZER_TABLET : 0x0514, 199 PERIPHERAL_CARD_READER : 0x0518,PERIPHERAL_DIGITAL_PEN : 0x051C, 200 PERIPHERAL_SCANNER_RFID : 0x0520,PERIPHERAL_GESTURAL_INPUT : 0x0522, 201 IMAGING_UNCATEGORIZED : 0x0600,IMAGING_DISPLAY : 0x0610, 202 IMAGING_CAMERA : 0x0620,IMAGING_SCANNER : 0x0640, 203 IMAGING_PRINTER : 0x0680,WEARABLE_UNCATEGORIZED : 0x0700, 204 WEARABLE_WRIST_WATCH : 0x0704,WEARABLE_PAGER : 0x0708, 205 WEARABLE_JACKET : 0x070C,WEARABLE_HELMET : 0x0710, 206 WEARABLE_GLASSES : 0x0714,TOY_UNCATEGORIZED : 0x0800, 207 TOY_ROBOT : 0x0804,TOY_VEHICLE : 0x0808, 208 TOY_DOLL_ACTION_FIGURE : 0x080C,TOY_CONTROLLER : 0x0810, 209 TOY_GAME : 0x0814,HEALTH_UNCATEGORIZED : 0x0900, 210 HEALTH_BLOOD_PRESSURE : 0x0904,HEALTH_THERMOMETER : 0x0908, 211 HEALTH_WEIGHING : 0x090C,HEALTH_GLUCOSE : 0x0910, 212 HEALTH_PULSE_OXIMETER : 0x0914,HEALTH_PULSE_RATE : 0x0918, 213 HEALTH_DATA_DISPLAY : 0x091C,HEALTH_STEP_COUNTER : 0x0920, 214 HEALTH_BODY_COMPOSITION_ANALYZER : 0x0924, 215 HEALTH_PEAK_FLOW_MOITOR : 0x0928,HEALTH_MEDICATION_MONITOR : 0x092C, 216 HEALTH_KNEE_PROSTHESIS : 0x0930,HEALTH_ANKLE_PROSTHESIS : 0x0934, 217 HEALTH_GENERIC_HEALTH_MANAGER : 0x0938, 218 HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C, 219 HEALTH_PERSONAL_MOBILITY_DEVICE : 0x093C 220 }; 221 let MajorClass = { 222 MAJOR_MISC : 0x0000,MAJOR_COMPUTER : 0x0100, 223 MAJOR_PHONE : 0x0200,MAJOR_NETWORKING : 0x0300, 224 MAJOR_AUDIO_VIDEO: 0x0400,MAJOR_PERIPHERAL : 0x0500, 225 MAJOR_IMAGING : 0x0600,MAJOR_WEARABLE : 0x0700, 226 MAJOR_TOY : 0x0800,MAJOR_HEALTH : 0x0900, 227 MAJOR_UNCATEGORIZED : 0x1F00 228 }; 229 let DeviceClass = bluetooth.getRemoteDeviceClass("00:00:00:00:00:00"); 230 console.info('[bluetooth_js] getRemoteDeviceClass ret2 :' + JSON.stringify(DeviceClass) 231 + 'majorClass:' +DeviceClass.majorClass + 'majorMinorClass:'+ DeviceClass.majorMinorClass 232 + 'classOfDevice:' + DeviceClass.classOfDevice); 233 expect(DeviceClass.majorClass).assertEqual(7936); 234 done(); 235 }) 236 237 /** 238 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0500 239 * @tc.name testGetRemoteDeviceName 240 * @tc.desc Test getRemoteDeviceName api 241 * @tc.type Function 242 * @tc.size MediumTest 243 * @tc.level Level 3 244 */ 245 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0500', 0, async function (done) { 246 let ret = bluetooth.getRemoteDeviceName("1125445"); 247 console.info('[bluetooth_js] getRemoteDeviceName ret2:' + JSON.stringify(ret)); 248 expect(ret.length).assertEqual(0); 249 done(); 250 }) 251 252 /** 253 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0600 254 * @tc.name testGetPairedDevices 255 * @tc.desc Test getPairedDevices api 256 * @tc.type Function 257 * @tc.size MediumTest 258 * @tc.level Level 3 259 */ 260 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0600', 0, async function (done) { 261 let ret = bluetooth.getPairedDevices(); 262 console.info('[bluetooth_js] getPairedDevices ret2:' + JSON.stringify(ret)); 263 expect(true).assertEqual(ret.length>=0); 264 done(); 265 }) 266 267 /** 268 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0700 269 * @tc.name testPairDevice 270 * @tc.desc Test pinRequired ON api 271 * @tc.type Function 272 * @tc.size MediumTest 273 * @tc.level Level 3 274 */ 275 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0700', 0, async function (done) { 276 function PinRequiredParam(data) { 277 console.info("[bluetooth_js] pinRequired on:" + JSON.stringify(data)); 278 let ret = bluetooth.setDevicePairingConfirmation(data.deviceId, false); 279 expect(ret).assertFalse(); 280 } 281 try { 282 bluetooth.BLE.on('pinRequired', PinRequiredParam); 283 bluetooth.pairDevice("99:55:22:88:66:11"); 284 await sleep(2000); 285 bluetooth.BLE.off('pinRequired', PinRequiredParam); 286 } catch(err) { 287 console.error("errCode:" + err.code + ",errMessage:" + err.message); 288 expect(Number(err.code)).assertEqual(2900099); 289 } 290 done(); 291 }) 292 293 /** 294 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0800 295 * @tc.name testPairDevice 296 * @tc.desc Test pinRequired off api 297 * @tc.type Function 298 * @tc.size MediumTest 299 * @tc.level Level 3 300 */ 301 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0800', 0, async function (done) { 302 function PinRequiredParam(data) { 303 console.info("[bluetooth_js] pinRequired on:" + JSON.stringify(data)); 304 let ret = bluetooth.setDevicePairingConfirmation(data.deviceId, true); 305 expect(ret).assertTrue(); 306 } 307 try { 308 bluetooth.BLE.on('pinRequired', PinRequiredParam); 309 bluetooth.pairDevice("66:88:33:55:22:99"); 310 await sleep(1000); 311 bluetooth.BLE.off('pinRequired', PinRequiredParam); 312 } catch(err) { 313 console.error("errCode:" + err.code + ",errMessage:" + err.message); 314 expect(Number(err.code)).assertEqual(2900099); 315 } 316 done(); 317 }) 318 319 /** 320 * @tc.number SUB_COMMUNICATION_BLUETOOTH_PAIR_0900 321 * @tc.name testPairDevice 322 * @tc.desc Test bondStateChange ON api 323 * @tc.type Function 324 * @tc.size MediumTest 325 * @tc.level Level 3 326 */ 327 it('SUB_COMMUNICATION_BLUETOOTH_PAIR_0900', 0, async function (done) { 328 function BondStateParam(data) { 329 console.info("[bluetooth_js] bondStateChange on:" + JSON.stringify(data) 330 +'bondStateChange deviceId:' + data.deviceId + 'bondStateChange state:' + data.state); 331 } 332 bluetooth.BLE.on('bondStateChange', BondStateParam); 333 bluetooth.pairDevice("58:62:22:23:69:54"); 334 expect(bluetooth.BondState.BOND_STATE_INVALID == 0).assertTrue(); 335 expect(bluetooth.BondState.BOND_STATE_BONDING == 1).assertTrue(); 336 expect(bluetooth.BondState.BOND_STATE_BONDED == 2).assertTrue(); 337 bluetooth.BLE.off('bondStateChange', BondStateParam); 338 done(); 339 }) 340 341}) 342}