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('UsbFunctionsJsFunctionsTest', function () { 22 23 beforeAll(function () { 24 console.log('*************Usb Unit UsbFunctionsJsFunctionsTest Begin*************'); 25 var Version = usb.getVersion() 26 console.info('begin test getversion :' + Version) 27 // version > 17 host currentMode = 2 device currentMode = 1 28 var usbPortList = usb.getPorts() 29 if (usbPortList.length > 0) { 30 if (usbPortList[0].status.currentMode == 2) { 31 usb.setPortRoles(usbPortList[0].id, usb.SINK, usb.DEVICE).then(data => { 32 console.info('usb case setPortRoles return: ' + data); 33 }).catch(error => { 34 console.info('usb case setPortRoles error : ' + error); 35 }); 36 console.log('*************Usb Unit switch to device Begin*************'); 37 } 38 } 39 }) 40 beforeEach(function () { 41 console.info('beforeEach: *************Usb Unit Test Case*************'); 42 }) 43 afterEach(function () { 44 console.info('afterEach: *************Usb Unit Test Case*************'); 45 }) 46 afterAll(function () { 47 console.log('*************Usb Unit UsbFunctionsJsFunctionsTest End*************'); 48 }) 49 50 /** 51 * @tc.number : SUB_USB_get_current_functions_test_01 52 * @tc.name : getCurrentFunctions 53 * @tc.desc : 获取当前设备模式 掩码与描述字符转换 54 */ 55 it('SUB_USB_get_current_functions_test_01', 0, function () { 56 console.info('usb get_current_functions_test_01 begin'); 57 var maskCode = usb.getCurrentFunctions(); 58 console.info('usb case getCurrentFunctions return: ' + maskCode); 59 60 var strMaskCode = usb.usbFunctionsToString(maskCode) 61 console.info('usb case usbFunctionsToString return str: ' + strMaskCode); 62 var nMaskCode = usb.usbFunctionsFromString(strMaskCode) 63 console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode); 64 expect(nMaskCode).assertEqual(maskCode); 65 console.info('usb get_current_functions_test_01 : PASS'); 66 expect(true).assertTrue(); 67 }) 68 69 /** 70 * @tc.number : SUB_USB_get_current_functions_test_02 71 * @tc.name : usbFunctionString 72 * @tc.desc : 反向测试 获取当前设备模式 掩码与描述字符转换 73 */ 74 it('SUB_USB_get_current_functions_test_02', 0, function () { 75 console.info('usb get_current_functions_test_02 begin'); 76 var maskCode = usb.getCurrentFunctions(); 77 console.info('usb case getCurrentFunctions return: ' + maskCode); 78 79 var strMaskCode = usb.usbFunctionsToString(maskCode) 80 console.info('usb case usbFunctionsToString return str: ' + strMaskCode); 81 var nMaskCode = usb.usbFunctionsFromString(strMaskCode) 82 console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode); 83 84 var errmaskCode = 0 85 var strMaskCode = usb.usbFunctionsToString(errmaskCode) 86 87 console.info('usb case ' + errmaskCode + ' usbFunctionsToString return str: ' + strMaskCode); 88 89 var errStrMaskCode = 'none' 90 var nMaskCode = usb.usbFunctionsFromString(errStrMaskCode) 91 expect(nMaskCode).assertEqual(usb.NONE); 92 console.info('usb case errStrMaskCode ' + errStrMaskCode + 93 ' usbFunctionsFromString return int: ' + nMaskCode); 94 95 console.info('usb get_current_functions_test_02 : PASS'); 96 }) 97 98 function callSetCurFunction(caseName, iValue) { 99 CheckEmptyUtils.sleep(3000) 100 console.info('usb case param case name:' + caseName); 101 console.info('usb case param iValue:' + iValue); 102 usb.setCurrentFunctions(iValue).then(data => { 103 console.info('usb case SetCurFunction ret:' + data); 104 expect(data).assertTrue(); 105 console.info('usb case ' + caseName + ': PASS'); 106 }).catch(error => { 107 console.info('usb case ' + caseName + ' error : ' + error); 108 expect(false).assertTrue(); 109 }); 110 } 111 112 /** 113 * @tc.number : SUB_USB_set_current_functions_test_02 114 * @tc.name : functions_test 115 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 1 设置为ACM功能 116 */ 117 it('SUB_USB_set_current_functions_test_02', 0, function () { 118 CheckEmptyUtils.sleep(3000) 119 console.info('usb set_current_functions_test_02 set ACM begin'); 120 var maskCode = usb.getCurrentFunctions(); 121 console.info('usb case getCurrentFunctions return: ' + maskCode); 122 var funcString = usb.usbFunctionsToString(maskCode); 123 console.info('usb case funcString:' + funcString); 124 callSetCurFunction('set_current_functions_test_02 ACM 1', usb.ACM) 125 }) 126 127 /** 128 * @tc.number : SUB_USB_set_current_functions_test_03 129 * @tc.name : functions_test 130 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 2 设置为ECM功能 131 */ 132 it('SUB_USB_set_current_functions_test_03', 0, function () { 133 CheckEmptyUtils.sleep(3000) 134 console.info('usb set_current_functions_test_03 set ECM begin'); 135 var maskCode = usb.getCurrentFunctions(); 136 console.info('usb case getCurrentFunctions return: ' + maskCode); 137 var funcString = usb.usbFunctionsToString(maskCode); 138 console.info('usb case funcString:' + funcString); 139 callSetCurFunction('set_current_functions_test_03 ECM 2', usb.ECM) 140 }) 141 142 /** 143 * @tc.number : SUB_USB_set_current_functions_test_04 144 * @tc.name : functions_test 145 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 3 设置为ACM、ECM功能 146 */ 147 it('SUB_USB_set_current_functions_test_04', 0, function () { 148 CheckEmptyUtils.sleep(3000) 149 console.info('usb set_current_functions_test_04 set ACM ECM begin'); 150 var maskCode = usb.getCurrentFunctions(); 151 console.info('usb case getCurrentFunctions return: ' + maskCode); 152 var funcString = usb.usbFunctionsToString(maskCode); 153 console.info('usb case funcString:' + funcString); 154 callSetCurFunction('set_current_functions_test_04 ACM ECM 3', (usb.ACM | usb.ECM)) 155 }) 156 157 /** 158 * @tc.number : SUB_USB_set_current_functions_test_05 159 * @tc.name : functions_test 160 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 4 设置为HDC功能 161 */ 162 it('SUB_USB_set_current_functions_test_05', 0, function () { 163 CheckEmptyUtils.sleep(3000) 164 console.info('usb set_current_functions_test_05 set HDC begin'); 165 var maskCode = usb.getCurrentFunctions(); 166 console.info('usb case getCurrentFunctions return: ' + maskCode); 167 var funcString = usb.usbFunctionsToString(maskCode); 168 console.info('usb case funcString:' + funcString); 169 callSetCurFunction('set_current_functions_test_05 HDC 4', usb.HDC) 170 }) 171 172 /** 173 * @tc.number : SUB_USB_set_current_functions_test_06 174 * @tc.name : functions_test 175 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 5 设置为ACM、HDC功能 176 */ 177 it('SUB_USB_set_current_functions_test_06', 0, function () { 178 CheckEmptyUtils.sleep(3000) 179 console.info('usb set_current_functions_test_06 set ACM HDC begin'); 180 var maskCode = usb.getCurrentFunctions(); 181 console.info('usb case getCurrentFunctions return: ' + maskCode); 182 var funcString = usb.usbFunctionsToString(maskCode); 183 console.info('usb case funcString:' + funcString); 184 callSetCurFunction('set_current_functions_test_06 ACM HDC 5', (usb.HDC | usb.ACM)) 185 }) 186 187 /** 188 * @tc.number : SUB_USB_set_current_functions_test_07 189 * @tc.name : functions_test 190 * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 6 设置为ECM、HDC功能 191 */ 192 it('SUB_USB_set_current_functions_test_07', 0, function () { 193 CheckEmptyUtils.sleep(3000) 194 console.info('usb set_current_functions_test_07 set ECM HDC begin'); 195 var maskCode = usb.getCurrentFunctions(); 196 console.info('usb case getCurrentFunctions return: ' + maskCode); 197 var funcString = usb.usbFunctionsToString(maskCode); 198 console.info('usb case funcString:' + funcString); 199 callSetCurFunction('set_current_functions_test_07 ECM HDC 6', (usb.HDC | usb.ECM)) 200 }) 201 202}) 203