1/* 2 * Copyright (c) 2021 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 usb from '@ohos.usb'; 17import CheckEmptyUtils from './CheckEmptyUtils.js'; 18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' 19 20 21/* usb core functions test */ 22describe('UsbCoreJsFunctionsTest', function () { 23 24 beforeAll(function () { 25 console.log('*************Usb Unit UsbCoreJsFunctionsTest Begin*************'); 26 var Version = usb.getVersion() 27 console.info('begin test getversion :' + Version) 28 // version > 17 host currentMode = 2 device currentMode = 1 29 var usbPortList = usb.getPorts() 30 if (usbPortList.length > 0) { 31 if (usbPortList[0].status.currentMode == 1) { 32 usb.setPortRoles(usbPortList[0].id, usb.SOURCE, usb.HOST).then(data => { 33 console.info('usb case setPortRoles return: ' + data); 34 }).catch(error => { 35 console.info('usb case setPortRoles error : ' + error); 36 }); 37 CheckEmptyUtils.sleep(8000) 38 console.log('*************Usb Unit Begin switch to host*************'); 39 } 40 } 41 }) 42 afterEach(function () { 43 console.info('afterEach: *************Usb Unit Test Case*************'); 44 }) 45 beforeEach(function () { 46 console.info('beforeEach: *************Usb Unit Test Case*************'); 47 }) 48 afterAll(function () { 49 console.log('*************Usb Unit UsbCoreJsFunctionsTest End*************'); 50 }) 51 52 var gDeviceList 53 var gPipe; 54 55 /** 56 * @tc.number : SUB_USB_get_devices_test_01 57 * @tc.name : getDevices 58 * @tc.desc : 获取设备列表 59 */ 60 it('SUB_USB_get_devices_test_01', 0, function () { 61 console.info('usb get_device_01 begin'); 62 gDeviceList = usb.getDevices(); 63 if (gDeviceList.length == 0) { 64 console.info('usb case get_devices list is null') 65 expect(false).assertTrue(); 66 return 67 } 68 expect(gDeviceList.length).assertLarger(0); 69 console.info('usb case getDevices ret length: ' + gDeviceList.length); 70 console.info('usb get_devices_test_01: PASS'); 71 }) 72 73 74 /** 75 * @tc.number : SUB_USB_has_right_01 76 * @tc.name : hasRigt 77 * @tc.desc : 权限查询 连接设备 关闭设备 78 */ 79 it('SUB_USB_has_right_01', 0, function () { 80 console.info('usb has_right_01 begin'); 81 if (gDeviceList.length == 0) { 82 console.info('usb case get_device_list is null') 83 expect(false).assertTrue(); 84 return 85 } 86 87 for (var i = 0; i < gDeviceList.length; i++) { 88 var hasRight = usb.hasRight(gDeviceList[i].name) 89 console.info('usb has_right ret :' + hasRight); 90 } 91 92 console.info('usb has_right_01 : PASS'); 93 expect(true).assertTrue(); 94 }) 95 96 97 /** 98 * @tc.number : SUB_USB_request_right_01 99 * @tc.name : requestRight 100 * @tc.desc : 请求权限 101 */ 102 it('SUB_USB_request_right_01', 0, function () { 103 console.info('usb request_right_01 begin'); 104 if (gDeviceList.length == 0) { 105 console.info('usb case get_device_list is null') 106 expect(false).assertTrue(); 107 return 108 } 109 110 for (var i = 0; i < gDeviceList.length; i++) { 111 usb.requestRight(gDeviceList[i].name).then(hasRight => { 112 expect(hasRight).assertTrue(); 113 console.info('usb request_right ret :' + hasRight); 114 console.info('usb request_right_01 : PASS'); 115 }).catch(error => { 116 console.info('usb case device request right failed : ' + error + ' :' + gDeviceList[i].name); 117 expect(false).assertTrue(); 118 }); 119 } 120 }) 121 122 /** 123 * @tc.number : SUB_USB_connect_device_01 124 * @tc.name : connectDevice 125 * @tc.desc : 打开设备 126 */ 127 it('SUB_USB_connect_device_01', 0, function () { 128 console.info('usb connect_device_01 begin'); 129 if (gDeviceList.length == 0) { 130 console.info('usb case get_device_list is null') 131 expect(false).assertTrue(); 132 return 133 } 134 var isRight = usb.hasRight(gDeviceList[0].name); 135 if (!isRight) { 136 usb.requestRight(gDeviceList[0].name).then(hasRight => { 137 138 }).catch(error => { 139 console.info('usb 01 requestRight error:' + error); 140 }); 141 CheckEmptyUtils.sleep(3000) 142 } 143 144 gPipe = usb.connectDevice(gDeviceList[0]) 145 console.info('usb case connectDevice ret: ' + JSON.stringify(gPipe)); 146 expect(CheckEmptyUtils.isEmpty(gPipe)).assertFalse(); 147 148 console.info('usb connect_device_01 : PASS'); 149 }) 150 151 /** 152 * @tc.number : SUB_USB_close_pipe_02 153 * @tc.name : closePipe 154 * @tc.desc : 反向测试 关闭设备 错误 busNum 155 */ 156 it('SUB_USB_close_pipe_02', 0, function () { 157 console.info('usb close_pipe_02 begin'); 158 if (gDeviceList.length == 0) { 159 console.info('usb case get_device_list is null') 160 expect(false).assertTrue(); 161 return 162 } 163 164 var tmpPipe = JSON.parse(JSON.stringify(gPipe)); 165 tmpPipe.busNum = -23 166 var isPipClose = usb.closePipe(tmpPipe); 167 console.info('usb case closePipe ret: ' + isPipClose); 168 expect(isPipClose == 0).assertFalse(); 169 170 console.info('usb close_pipe_02 : PASS'); 171 }) 172 173 /** 174 * @tc.number : SUB_USB_close_pipe_03 175 * @tc.name : closePipe 176 * @tc.desc : 反向测试 关闭设备 错误 devAddress 177 */ 178 it('SUB_USB_close_pipe_03', 0, function () { 179 console.info('usb close_pipe_03 begin'); 180 if (gDeviceList.length == 0) { 181 console.info('usb case get_device_list is null') 182 expect(false).assertTrue(); 183 return 184 } 185 var tmpPipe = JSON.parse(JSON.stringify(gPipe)); 186 tmpPipe.devAddress = -23 187 var isPipClose = usb.closePipe(tmpPipe); 188 console.info('usb case closePipe ret: ' + isPipClose); 189 expect(isPipClose == 0).assertFalse(); 190 191 console.info('usb close_pipe_03 : PASS'); 192 }) 193 194 /** 195 * @tc.number : SUB_USB_close_pipe_04 196 * @tc.name : closePipe 197 * @tc.desc : 反向测试 关闭设备 错误 devAddress busNum 198 */ 199 it('SUB_USB_close_pipe_04', 0, function () { 200 console.info('usb close_pipe_04 begin'); 201 if (gDeviceList.length == 0) { 202 console.info('usb case get_device_list is null') 203 expect(false).assertTrue(); 204 return 205 } 206 var tmpPipe = JSON.parse(JSON.stringify(gPipe)); 207 tmpPipe.devAddress = -23 208 tmpPipe.busNum = -23 209 var isPipClose = usb.closePipe(tmpPipe); 210 console.info('usb case closePipe ret: ' + isPipClose); 211 expect(isPipClose == 0).assertFalse(); 212 213 console.info('usb close_pipe_04 : PASS'); 214 }) 215 216 /** 217 * @tc.number : SUB_USB_close_pipe_01 218 * @tc.name : closePipe 219 * @tc.desc : 关闭设备 220 */ 221 it('SUB_USB_close_pipe_01', 0, function () { 222 console.info('usb close_pipe_01 begin'); 223 if (gDeviceList.length == 0) { 224 console.info('usb case get_device_list is null') 225 expect(false).assertTrue(); 226 return 227 } 228 229 console.info('usb case closePipe param: ' + JSON.stringify(gPipe)); 230 var isPipClose = usb.closePipe(gPipe); 231 console.info('usb case closePipe ret: ' + isPipClose); 232 expect(isPipClose).assertEqual(0); 233 234 console.info('usb close_pipe_01 : PASS'); 235 }) 236 237 /** 238 * @tc.number : SUB_USB_get_raw_descriptor_01 239 * @tc.name : getRawDescriptor 240 * @tc.desc : 获取原始的USB描述符 241 */ 242 it('SUB_USB_get_raw_descriptor_01', 0, function () { 243 if (gDeviceList.length == 0) { 244 console.info('usb case get_device_list is null') 245 expect(false).assertTrue(); 246 return 247 } 248 249 gPipe = usb.connectDevice(gDeviceList[0]) 250 console.info('usb case getRawDescriptor param: ' + JSON.stringify(gPipe)); 251 var descriptor = usb.getRawDescriptor(gPipe); 252 console.info('usb case getRawDescriptor ret: ' + descriptor); 253 expect(true).assertTrue(); 254 var isPipClose = usb.closePipe(gPipe); 255 expect(isPipClose).assertEqual(0); 256 257 console.info('usb SUB_USB_get_raw_descriptor_01 : PASS'); 258 }) 259 260 /** 261 * @tc.number : SUB_USB_get_file_descriptor_01 262 * @tc.name : getFileDescriptor 263 * @tc.desc : 获取文件描述符 264 */ 265 it('SUB_USB_get_file_descriptor_01', 0, function () { 266 if (gDeviceList.length == 0) { 267 console.info('usb case get_device_list is null') 268 expect(false).assertTrue(); 269 return 270 } 271 gPipe = usb.connectDevice(gDeviceList[0]) 272 console.info('usb case getFileDescriptor param: ' + JSON.stringify(gPipe)); 273 var fileDescriptor = usb.getFileDescriptor(gPipe); 274 console.info('usb case getFileDescriptor ret: ' + fileDescriptor); 275 expect(true).assertTrue(); 276 var isPipClose = usb.closePipe(gPipe); 277 expect(isPipClose).assertEqual(0); 278 279 console.info('usb SUB_USB_get_file_descriptor_01 : PASS'); 280 }) 281 282 /** 283 * @tc.number : SUB_USB_get_file_descriptor_02 284 * @tc.name : getFileDescriptor 285 * @tc.desc : 获取文件描述符,反向测试,错误参数busNum=512 286 */ 287 it('SUB_USB_get_file_descriptor_02', 0, function () { 288 if (gDeviceList.length == 0) { 289 console.info('usb case get_device_list is null') 290 expect(false).assertTrue(); 291 return 292 } 293 gPipe = usb.connectDevice(gDeviceList[0]) 294 var tempPipe = {busNum : 255, devAddress : 255} 295 console.info('usb case getFileDescriptor param: ' + JSON.stringify(gPipe)); 296 var fileDescriptor = usb.getFileDescriptor(tempPipe); 297 expect(fileDescriptor < 0).assertTrue(); 298 console.info('usb case getFileDescriptor ret: ' + fileDescriptor); 299 var isPipClose = usb.closePipe(gPipe); 300 expect(isPipClose).assertEqual(0); 301 console.info('usb SUB_USB_get_file_descriptor_02 : PASS'); 302 }) 303 304 /** 305 * @tc.number : SUB_USB_get_file_descriptor_03 306 * @tc.name : getFileDescriptor 307 * @tc.desc : 获取文件描述符,反向测试,错误参数devAddress=512 308 */ 309 it('SUB_USB_get_file_descriptor_03', 0, function () { 310 if (gDeviceList.length == 0) { 311 console.info('usb case get_device_list is null') 312 expect(false).assertTrue(); 313 return 314 } 315 gPipe = usb.connectDevice(gDeviceList[0]) 316 var tempPipe = {busNum : 255, devAddress : gPipe.devAddress} 317 console.info('usb case getFileDescriptor param: ' + JSON.stringify(gPipe)); 318 var fileDescriptor = usb.getFileDescriptor(tempPipe); 319 expect(fileDescriptor < 0).assertTrue(); 320 console.info('usb case getFileDescriptor ret: ' + fileDescriptor); 321 var isPipClose = usb.closePipe(gPipe); 322 expect(isPipClose).assertEqual(0); 323 console.info('usb SUB_USB_get_file_descriptor_03 : PASS'); 324 }) 325 326 /** 327 * @tc.number : SUB_USB_get_file_descriptor_04 328 * @tc.name : getFileDescriptor 329 * @tc.desc : 获取文件描述符,反向测试,错误参数busNum=512,devAddress=512 330 */ 331 it('SUB_USB_get_file_descriptor_04', 0, function () { 332 if (gDeviceList.length == 0) { 333 console.info('usb case get_device_list is null') 334 expect(false).assertTrue(); 335 return 336 } 337 gPipe = usb.connectDevice(gDeviceList[0]) 338 var tempPipe = {busNum : gPipe.busNum, devAddress : 255} 339 console.info('usb case getFileDescriptor param: ' + JSON.stringify(gPipe)); 340 var fileDescriptor = usb.getFileDescriptor(tempPipe); 341 expect(fileDescriptor < 0).assertTrue(); 342 console.info('usb case getFileDescriptor ret: ' + fileDescriptor); 343 var isPipClose = usb.closePipe(gPipe); 344 expect(isPipClose).assertEqual(0); 345 console.info('usb SUB_USB_get_file_descriptor_04 : PASS'); 346 }) 347 348}) 349