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/* usb core functions test */ 21describe('UsbPortJsFunctionsTest', function () { 22 23 beforeAll(function () { 24 console.log('*************Usb Unit UsbPortJsFunctionsTest Begin*************'); 25 var Version = usb.getVersion() 26 console.info('begin test getversion :' + Version) 27 }) 28 beforeEach(function () { 29 console.info('beforeEach: *************Usb Unit Test Case*************'); 30 }) 31 afterEach(function () { 32 console.info('afterEach: *************Usb Unit Test Case*************'); 33 }) 34 afterAll(function () { 35 console.log('*************Usb Unit UsbPortJsFunctionsTest End*************'); 36 }) 37 38 /** 39 * @tc.number : SUB_USB_get_ports_test_01 40 * @tc.name : getPorts 41 * @tc.desc : 获取USB端口描述信息列表 42 */ 43 it('SUB_USB_get_ports_test_01', 0, function () { 44 console.info('usb get_ports_test_01 begin'); 45 var usbPortList = usb.getPorts() 46 if (usbPortList.length == 0) { 47 console.info('usb get_ports_test_01 usbPortList is null'); 48 expect(false).assertTrue(); 49 return 50 } 51 expect(usbPortList.length > 0).assertTrue(); 52 console.info('usb get_ports_test_01 : PASS'); 53 expect(true).assertTrue(); 54 }) 55 56 /** 57 * @tc.number : SUB_USB_get_supported_modes_test_01 58 * @tc.name : getSupportedModes 59 * @tc.desc : 获取指定的端口支持的模式列表的组合掩码 60 */ 61 it('SUB_USB_get_supported_modes_test_01', 0, function () { 62 console.info('usb get_supported_modes_test_01 begin'); 63 var usbPortList = usb.getPorts() 64 if (usbPortList.length == 0) { 65 console.info('usb get_supported_modes_test_01 usbPortList is null'); 66 expect(false).assertTrue(); 67 return 68 } 69 70 expect(usbPortList.length > 0).assertTrue(); 71 for (var i = 0; i < usbPortList.length; i++) { 72 var maskCode = usb.getSupportedModes(usbPortList[i].id) 73 expect(maskCode).assertEqual(usbPortList[i].supportedModes); 74 } 75 76 console.info('usb get_supported_modes_test_01 : PASS'); 77 expect(true).assertTrue(); 78 }) 79 80 /** 81 * @tc.number : SUB_USB_set_port_roles_test_01 82 * @tc.name : setPortRoles 83 * @tc.desc : 切换为Device set powerRole 2 DataRole 2 84 */ 85 it('SUB_USB_set_port_roles_test_01', 0, function () { 86 console.info('usb set_port_roles_test_01 device 2 2 begin'); 87 var usbPortList = usb.getPorts() 88 if (usbPortList.length == 0) { 89 console.info('usb set_port_roles_test_01 device 2 2 usbPortList is null'); 90 expect(false).assertTrue(); 91 return 92 } 93 94 for (var i = 0; i < usbPortList.length; i++) { 95 console.info('usb case set data role 2, data role 2'); 96 usb.setPortRoles(usbPortList[i].id, usb.SINK, usb.DEVICE).then(data => { 97 console.info('usb case setPortRoles return: ' + data); 98 expect(data).assertTrue(); 99 }).catch(error => { 100 console.info('usb case setPortRoles error : ' + error); 101 expect(false).assertTrue(); 102 }); 103 } 104 105 console.info('usb set_port_roles_test_01 device 2 2: PASS'); 106 expect(true).assertTrue(); 107 }) 108 109 /** 110 * @tc.number : SUB_USB_set_port_roles_test_02 111 * @tc.name : setPortRoles 112 * @tc.desc : 切换为host set powerRole 1 DataRole 1 113 */ 114 it('SUB_USB_set_port_roles_test_02', 0, function () { 115 console.info('usb set_port_roles_test_02 host 1 1 begin'); 116 var usbPortList = usb.getPorts() 117 if (usbPortList.length == 0) { 118 console.info('usb set_port_roles_test_02 host 1 1 usbPortList is null'); 119 expect(false).assertTrue(); 120 return 121 } 122 123 for (var i = 0; i < usbPortList.length; i++) { 124 console.info('usb case set data role 1, data role 1'); 125 CheckEmptyUtils.sleep(5000) 126 usb.setPortRoles(usbPortList[i].id, usb.SOURCE, usb.HOST).then(data => { 127 expect(data).assertTrue(); 128 console.info('usb case setPortRoles return: ' + data); 129 }).catch(error => { 130 console.info('usb case setPortRoles error : ' + error); 131 expect(false).assertTrue(); 132 }); 133 } 134 135 console.info('usb set_port_roles_test_02 host 1 1: PASS'); 136 expect(true).assertTrue(); 137 }) 138 139}) 140