diff --git a/usb/BUILD.gn b/usb/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..6e37eabef7c52dedfe058058c0d2ca624e6cd144 --- /dev/null +++ b/usb/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos_var.gni") + +group("usb") { + testonly = true + if (is_standard_system) { + deps = [ "usb_standard:usb_js_test" ] + } +} diff --git a/usb/usb_standard/BUILD.gn b/usb/usb_standard/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..ed66ed5e7fbb3b33e9ef074b552fa8eb063e6786 --- /dev/null +++ b/usb/usb_standard/BUILD.gn @@ -0,0 +1,31 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//test/xts/tools/build/suite.gni") + +ohos_js_hap_suite("usb_js_test") { + hap_profile = "./src/main/config.json" + deps = [ + ":usb_js_assets", + ":usb_js_resources", + ] + certificate_profile = "./signature/openharmony_sx.p7b" + hap_name = "ActsUsbJSApiTest" +} +ohos_js_assets("usb_js_assets") { + source_dir = "./src/main/js/default" +} +ohos_resources("usb_js_resources") { + sources = [ "./src/main/resources" ] + hap_profile = "./src/main/config.json" +} diff --git a/usb/usb_standard/Test.json b/usb/usb_standard/Test.json new file mode 100755 index 0000000000000000000000000000000000000000..cd7ef814da181eed0960f3513e55467031ee16b2 --- /dev/null +++ b/usb/usb_standard/Test.json @@ -0,0 +1,18 @@ +{ + "description": "Configuration for usb js api Tests", + "driver": { + "type": "JSUnitTest", + "test-timeout": "160000", + "package": "ohos.acts.usb.usb.function", + "shell-timeout": "60000" + }, + "kits": [ + { + "test-file-name": [ + "ActsUsbJSApiTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} diff --git a/usb/usb_standard/signature/openharmony_sx.p7b b/usb/usb_standard/signature/openharmony_sx.p7b new file mode 100755 index 0000000000000000000000000000000000000000..9be1e98fa4c0c28ca997ed660112fa16b194f0f5 Binary files /dev/null and b/usb/usb_standard/signature/openharmony_sx.p7b differ diff --git a/usb/usb_standard/src/main/config.json b/usb/usb_standard/src/main/config.json new file mode 100755 index 0000000000000000000000000000000000000000..4a05ad4d02dabfd0b326c8c4c82faf7443a03bf1 --- /dev/null +++ b/usb/usb_standard/src/main/config.json @@ -0,0 +1,60 @@ +{ + "app": { + "bundleName": "ohos.acts.usb.usb.function", + "vendor": "example", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 4, + "target": 5 + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.acts.usb.usb.function", + "name": ".MyApplication", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "visible": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "ohos.acts.usb.usb.function.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} diff --git a/usb/usb_standard/src/main/js/default/app.js b/usb/usb_standard/src/main/js/default/app.js new file mode 100755 index 0000000000000000000000000000000000000000..e423f4bce4698ec1d7dc86c3eea3990a5e7b1085 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/app.js @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('AceApplication onCreate'); + }, + onDestroy() { + console.info('AceApplication onDestroy'); + } +}; diff --git a/usb/usb_standard/src/main/js/default/i18n/en-US.json b/usb/usb_standard/src/main/js/default/i18n/en-US.json new file mode 100755 index 0000000000000000000000000000000000000000..e63c70d978a3a53be988388c87182f81785e170c --- /dev/null +++ b/usb/usb_standard/src/main/js/default/i18n/en-US.json @@ -0,0 +1,6 @@ +{ + "strings": { + "hello": "Hello", + "world": "World" + } +} \ No newline at end of file diff --git a/usb/usb_standard/src/main/js/default/i18n/zh-CN.json b/usb/usb_standard/src/main/js/default/i18n/zh-CN.json new file mode 100755 index 0000000000000000000000000000000000000000..de6ee5748322f44942c1b003319d8e66c837675f --- /dev/null +++ b/usb/usb_standard/src/main/js/default/i18n/zh-CN.json @@ -0,0 +1,6 @@ +{ + "strings": { + "hello": "您好", + "world": "世界" + } +} \ No newline at end of file diff --git a/usb/usb_standard/src/main/js/default/pages/index/index.css b/usb/usb_standard/src/main/js/default/pages/index/index.css new file mode 100755 index 0000000000000000000000000000000000000000..6fda792753f2e15f22b529c7b90a82185b2770bf --- /dev/null +++ b/usb/usb_standard/src/main/js/default/pages/index/index.css @@ -0,0 +1,9 @@ +.container { + flex-direction: column; + justify-content: center; + align-items: center; +} + +.title { + font-size: 100px; +} diff --git a/usb/usb_standard/src/main/js/default/pages/index/index.hml b/usb/usb_standard/src/main/js/default/pages/index/index.hml new file mode 100755 index 0000000000000000000000000000000000000000..f64b040a5ae394dbaa5e185e1ecd4f4556b92184 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/pages/index/index.hml @@ -0,0 +1,5 @@ +
+ + {{ $t('strings.hello') }} {{ title }} + +
diff --git a/usb/usb_standard/src/main/js/default/pages/index/index.js b/usb/usb_standard/src/main/js/default/pages/index/index.js new file mode 100755 index 0000000000000000000000000000000000000000..3cdd779e2442eb4d78feec5f58ce90bc1f61074d --- /dev/null +++ b/usb/usb_standard/src/main/js/default/pages/index/index.js @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Core, ExpectExtend} from 'deccjsunit/index' + +const injectRef = Object.getPrototypeOf(global) || global +injectRef.regeneratorRuntime = require('@babel/runtime/regenerator') + +export default { + data: { + title: "" + }, + onInit() { + this.title = this.$t('strings.world'); + }, + onShow() { + console.info('onShow finish') + const core = Core.getInstance() + const expectExtend = new ExpectExtend({ + 'id': 'extend' + }) + core.addService('expect', expectExtend) + core.init() + + const configService = core.getDefaultService('config') + this.timeout = 5000 + configService.setConfig(this) + + require('../../test/ListUsb.test') + core.execute() + }, + onReady() { + }, +} diff --git a/usb/usb_standard/src/main/js/default/test/CheckEmptyUtils.js b/usb/usb_standard/src/main/js/default/test/CheckEmptyUtils.js new file mode 100755 index 0000000000000000000000000000000000000000..a2fa91880328249e09b61f779b8e0739ff2625a5 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/CheckEmptyUtils.js @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + + /** + * Check obj is empty. + * + * @param {object} obj + * @return {boolean} true(empty) + */ + isEmpty(obj) { + return (typeof obj == 'undefined' || obj == null || + JSON.stringify(obj) == '{}' || JSON.stringify(obj) == 'undefined'); + }, + + /** + * Check str is empty. + * + * @param {string} str + * @return {boolean} true(empty) + */ + checkStrIsEmpty(str) { + return str.trim().length == 0; + }, + + /** + * Check array is empty. + * + * @param {Array}arr + * @return {boolean} true(empty) + */ + isEmptyArr(arr) { + return arr.length == 0; + }, + + // ArrayBuffer转为字符串,参数为ArrayBuffer对象 + ab2str(buf) { + return String.fromCharCode.apply(null, new Uint8Array(buf)); + }, + + // 字符串转为ArrayBuffer对象,参数为字符串 + str2ab(str) { + var arr = []; + for (var i = 0, j = str.length; i < j; ++i) { + arr.push(str.charCodeAt(i)); + } + var tmpUint8Array = new Uint8Array(arr); + return tmpUint8Array; + }, + + // 简单阻塞的粗暴实现 + sleep(delay) { + for (var t = Date.now(); Date.now() - t <= delay;); + } +} diff --git a/usb/usb_standard/src/main/js/default/test/DateAndTimeUtil.js b/usb/usb_standard/src/main/js/default/test/DateAndTimeUtil.js new file mode 100755 index 0000000000000000000000000000000000000000..22ba24b4cbeb96bc42adcaf86d6a48c8f76282e2 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/DateAndTimeUtil.js @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const twelve = 12; + +/* + * date package tool class + */ +export default class DateAndTimeUtil { + constructor() { + }; + + /** + * + * Get the current time + */ + now() { + const datetime = new Date(); + const hours = datetime.getHours(); + const minutes = datetime.getMinutes(); + return this.concatTime(hours, minutes); + }; + + /** + * + * Get the current time + */ + nowWithSeconds() { + const datetime = new Date(); + const hours = datetime.getHours(); + const minutes = datetime.getMinutes(); + const seconds = datetime.getMilliseconds() + console.info('now: ' + hours + ':' + minutes + ':' + seconds) + var now = this.concatTimeWithSeconds(hours, minutes, seconds); + console.info('now concat: ' + now); + return now; + }; + + /** + * format + * @param value + * @return + */ + fill(value) { + return (value > 9 ? '' : '0') + value; + }; + + /** + * concat date + * @param year m d + * @return + */ + concatDate(year, month, date) { + return year + this.year + month + this.month + date + this.day; + }; + + concatTime(hours, minutes) { + return `${this.fill(hours)}:${this.fill(minutes)}`; + }; + + concatTimeWithSeconds(hours, minutes, milliseconds) { + return `${this.fill(hours)}:${this.fill(minutes)}:${this.fill(milliseconds)}`; + }; + + /** + * Turn to 24-hour clock + * @param str + * @return + */ + transform24(str) { + const timeFlag = str.substr(0, 2); + if (timeFlag == this.morning) { + const h = str.substr(2).split(':')[0]; + if (h == twelve) { + const time = '0' + ':' + str.substr(2).split(':')[1]; + return time; + } else { + return h + ':' + str.substr(2).split(':')[1]; + } + } else { + const h = str.substr(2).split(':')[0]; + const h1 = parseInt(h) + twelve; + if (h != twelve) { + const time = h1 + ':' + str.substr(2).split(':')[1]; + return time; + } + } + }; + + /** + * Turn to 12-hour clock + * @param str + * @return + */ + transform12(str) { + const hours = str.substring(0, str.indexOf(':')); + const minutes = str.split(':')[1]; + if (hours < twelve) { + return this.morning.concat(`${hours}:${minutes}`); + } + if (hours == twelve) { + return this.afternoon.concat(`${hours}:${minutes}`); + } else { + const reduceHours = parseInt(hours) - twelve; + return this.afternoon.concat(`${reduceHours}:${minutes}`); + } + }; +} diff --git a/usb/usb_standard/src/main/js/default/test/EventConstants.js b/usb/usb_standard/src/main/js/default/test/EventConstants.js new file mode 100755 index 0000000000000000000000000000000000000000..b07e489e6e10b811689644aec2cb022ad3f71cec --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/EventConstants.js @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Constants of events that will be registered to system. + */ +const EventConstants = { + + /** + * Bitmask used for extracting the USBEndpoint type from it's address + */ + USB_ENDPOINT_XFERTYPE_MASK: 0x03, + + /** + * Control USBEndpoint type + */ + USB_ENDPOINT_XFER_CONTROL: 0, + + /** + * Isochronous USBEndpoint type + */ + USB_ENDPOINT_XFER_ISOC: 1, + + /** + * Bulk USBEndpoint type + */ + USB_ENDPOINT_XFER_BULK: 2, + + /** + * Interrupt USBEndpoint type + */ + USB_ENDPOINT_XFER_INT: 3, + + /** + * Bitmask used for extracting the USBEndpoint number from it's address + */ + USB_ENDPOINT_NUMBER_MASK: 0x0f, + + /** + * Bitmask used for extracting the USBEndpoint direction from it's address + */ + USB_ENDPOINT_DIR_MASK: 0x80, + + /** + * Used to signify direction of data for USBEndpoint is OUT, host to device + */ + USB_ENDPOINT_DIR_OUT: 0, + + /** + * Used to signify direction of data for USBEndpoint is IN, device to host + */ + USB_ENDPOINT_DIR_IN: 0x80 +} + +export default EventConstants; \ No newline at end of file diff --git a/usb/usb_standard/src/main/js/default/test/ListUsb.test.js b/usb/usb_standard/src/main/js/default/test/ListUsb.test.js new file mode 100755 index 0000000000000000000000000000000000000000..cd298d4f751197d22b33475d16146910e5916f8d --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/ListUsb.test.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +/* host test case */ +require('./UsbCoreJsunit.test.js') +require('./UsbCoreJsunitEx.test.js') +require('./UsbDevicePipeJsunit.test.js') +require('./UsbDevicePipeJsunitEx.test.js') + +/* device test case */ +require('./UsbFunctionsJsunit.test.js') +require('./UsbFunctionsJsunitEx.test.js') + +/* device and host test case */ +require('./UsbPortJsunit.test.js') +require('./UsbPortJsunitEx.test.js') diff --git a/usb/usb_standard/src/main/js/default/test/UsbCoreJsunit.test.js b/usb/usb_standard/src/main/js/default/test/UsbCoreJsunit.test.js new file mode 100755 index 0000000000000000000000000000000000000000..39fb1431c2a911ddc72c1126699c6f9db2ec44d4 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbCoreJsunit.test.js @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + + +/* usb core functions test */ +describe('UsbCoreJsFunctionsTest', function () { + + beforeAll(function () { + console.log('*************Usb Unit UsbCoreJsFunctionsTest Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + // 切换为host + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 1) { + usb.setPortRoles(usbPortList[0].id, 1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + }); + console.log('*************Usb Unit Begin switch to host*************'); + } + } + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + afterAll(function () { + console.log('*************Usb Unit UsbCoreJsFunctionsTest End*************'); + }) + + var gDeviceList + var gPipe; + + /** + * @tc.number : get_devices_test_01 + * @tc.name : getDevices + * @tc.desc : 获取设备列表 + */ + it('SUB_USB_get_devices_test_01', 0, function () { + console.info('usb get_device_01 begin'); + gDeviceList = usb.getDevices(); + if (gDeviceList.length == 0) { + console.info('usb case get_devices list is null') + expect(false).assertTrue(); + return + } + expect(gDeviceList.length).assertLarger(0); + console.info('usb case getDevices ret length: ' + gDeviceList.length); + console.info('usb get_devices_test_01: PASS'); + }) + + + /** + * @tc.number : has_right_1 + * @tc.name : hasRigt + * @tc.desc : 权限查询 连接设备 关闭设备 + */ + it('SUB_USB_has_right_01', 0, function () { + console.info('usb has_right_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + var hasRight = usb.hasRight(gDeviceList[i].name) + console.info('usb has_right ret :' + hasRight); + } + + console.info('usb has_right_01 : PASS'); + expect(true).assertTrue(); + }) + + + /** + * @tc.number : request_right_01 + * @tc.name : requestRight + * @tc.desc : 请求权限 + */ + it('SUB_USB_request_right_01', 0, function () { + console.info('usb request_right_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + usb.requestRight(gDeviceList[i].name).then(hasRight => { + expect(hasRight).assertTrue(); + console.info('usb request_right ret :' + hasRight); + console.info('usb request_right_01 : PASS'); + }).catch(error => { + console.info('usb case device request right failed : ' + error + ' :' + gDeviceList[i].name); + expect(false).assertTrue(); + }); + } + }) + + /** + * @tc.number : connect_device_01 + * @tc.name : connectDevice + * @tc.desc : 打开设备 + */ + it('SUB_USB_connect_device_01', 0, function () { + console.info('usb connect_device_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + + }).catch(error => { + console.info('usb 01 requestRight error:' + error); + }); + CheckEmptyUtils.sleep(3000) + } + + gPipe = usb.connectDevice(gDeviceList[0]) + console.info('usb case connectDevice ret: ' + JSON.stringify(gPipe)); + expect(CheckEmptyUtils.isEmpty(gPipe)).assertFalse(); + + console.info('usb connect_device_01 : PASS'); + }) + + /** + * @tc.number : close_pipe_02 + * @tc.name : closePipe + * @tc.desc : 反向测试 关闭设备 错误 busNum + */ + it('SUB_USB_close_pipe_02', 0, function () { + console.info('usb close_pipe_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + var tmpPipe = JSON.parse(JSON.stringify(gPipe)); + tmpPipe.busNum = -23 + var isPipClose = usb.closePipe(tmpPipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose == 0).assertFalse(); + + console.info('usb close_pipe_02 : PASS'); + }) + + /** + * @tc.number : close_pipe_03 + * @tc.name : closePipe + * @tc.desc : 反向测试 关闭设备 错误 devAddress + */ + it('SUB_USB_close_pipe_03', 0, function () { + console.info('usb close_pipe_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var tmpPipe = JSON.parse(JSON.stringify(gPipe)); + tmpPipe.devAddress = -23 + var isPipClose = usb.closePipe(tmpPipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose == 0).assertFalse(); + + console.info('usb close_pipe_03 : PASS'); + }) + + /** + * @tc.number : close_pipe_04 + * @tc.name : closePipe + * @tc.desc : 反向测试 关闭设备 错误 devAddress busNum + */ + it('SUB_USB_close_pipe_04', 0, function () { + console.info('usb close_pipe_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var tmpPipe = JSON.parse(JSON.stringify(gPipe)); + tmpPipe.devAddress = -23 + tmpPipe.busNum = -23 + var isPipClose = usb.closePipe(tmpPipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose == 0).assertFalse(); + + console.info('usb close_pipe_04 : PASS'); + }) + + /** + * @tc.number : close_pipe_01 + * @tc.name : closePipe + * @tc.desc : 关闭设备 + */ + it('SUB_USB_close_pipe_01', 0, function () { + console.info('usb close_pipe_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + console.info('usb case closePipe param: ' + JSON.stringify(gPipe)); + var isPipClose = usb.closePipe(gPipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + + console.info('usb close_pipe_01 : PASS'); + }) + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbCoreJsunitEx.test.js b/usb/usb_standard/src/main/js/default/test/UsbCoreJsunitEx.test.js new file mode 100755 index 0000000000000000000000000000000000000000..1556c4ac498b0aaecdf8d731e67a9cd791097a5c --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbCoreJsunitEx.test.js @@ -0,0 +1,615 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + + +/* usb core functions test */ +describe('UsbCoreJsFunctionsTestEx', function () { + var gDeviceList + beforeAll(function () { + console.log('*************Usb Unit UsbCoreJsFunctionsTestEx Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + // 切换为host + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 1) { + usb.setPortRoles(usbPortList[0].id, 1, 1).then(data => { + console.info('usb case setPortRolesEx return: ' + data); + }).catch(error => { + console.info('usb case setPortRolesEx error : ' + error); + }); + console.log('*************Usb Unit Begin switch to host*************'); + } + } + gDeviceList = usb.getDevices(); + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test CaseEx*************'); + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test CaseEx*************'); + }) + afterAll(function () { + console.log('*************Usb Unit UsbCoreJsFunctionsTestEx End*************'); + }) + + + /** + * @tc.number : connect_device_02 + * @tc.name : connectDevice + * @tc.desc : 反向测试 错误参数 devAddress 打开设备 + */ + it('SUB_USB_connect_device_02', 0, function () { + console.info('usb connect_device_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 02 requestRight error:' + error); + }); + console.info('usb 02 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.devAddress = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' devAddress ' + device.devAddress); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertTrue(); + + console.info('usb connect_device_02 : PASS'); + }) + + /** + * @tc.number : connect_device_03 + * @tc.name : connectDevice + * @tc.desc : 反向测试 busNum错误参数 打开设备 + */ + it('SUB_USB_connect_device_03', 0, function () { + console.info('usb connect_device_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 03 requestRight error:' + error); + }); + console.info('usb 03 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.busNum = 2 + 1000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' busNum ' + device.busNum); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertTrue(); + console.info('usb connect_device_03 : PASS'); + }) + + /** + * @tc.number : connect_device_04 + * @tc.name : connectDevice + * @tc.desc : 反向测试 serial错误参数 打开设备 + */ + it('SUB_USB_connect_device_04', 0, function () { + console.info('usb connect_device_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 04 requestRight error:' + error); + }); + console.info('usb 04 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.serial = 'asdfsd' + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' serial ' + device.serial); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_04 : PASS'); + }) + + /** + * @tc.number : connect_device_05 + * @tc.name : connectDevice + * @tc.desc : 反向测试 name错误参数 打开设备 + */ + it('SUB_USB_connect_device_05', 0, function () { + console.info('usb connect_device_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 05 requestRight error:' + error); + }); + console.info('usb 05 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.name = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + ' name ' + device.name); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_05 : PASS'); + }) + + /** + * @tc.number : connect_device_06 + * @tc.name : connectDevice + * @tc.desc : 反向测试 manufacturerName错误参数 打开设备 + */ + it('SUB_USB_connect_device_06', 0, function () { + console.info('usb connect_device_06 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 06 requestRight error:' + error); + }); + console.info('usb 06 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.manufacturerName = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' manufacturerName ' + device.manufacturerName); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_06 : PASS'); + }) + + /** + * @tc.number : connect_device_07 + * @tc.name : connectDevice + * @tc.desc : 反向测试 productName错误参数 打开设备 + */ + it('SUB_USB_connect_device_07', 0, function () { + console.info('usb connect_device_07 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 07 requestRight error:' + error); + }); + console.info('usb 07 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.productName = 'sdfsdfe' + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' productName ' + device.productName); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_07 : PASS'); + }) + + /** + * @tc.number : connect_device_08 + * @tc.name : connectDevice + * @tc.desc : 反向测试 version错误参数 打开设备 + */ + it('SUB_USB_connect_device_08', 0, function () { + console.info('usb connect_device_08 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 08 requestRight error:' + error); + }); + console.info('usb 08 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.version = 'gwefsdf' + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' version ' + device.version); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_08 : PASS'); + }) + + /** + * @tc.number : connect_device_09 + * @tc.name : connectDevice + * @tc.desc : 反向测试 vendorId错误参数 打开设备 + */ + it('SUB_USB_connect_device_09', 0, function () { + console.info('usb connect_device_09 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 09 requestRight error:' + error); + }); + console.info('usb 09 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.vendorId = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' vendorId ' + device.vendorId); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_09 : PASS'); + }) + + /** + * @tc.number : connect_device_10 + * @tc.name : connectDevice + * @tc.desc : 反向测试 productId错误参数 打开设备 + */ + it('SUB_USB_connect_device_10', 0, function () { + console.info('usb connect_device_10 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 10 requestRight error:' + error); + }); + console.info('usb 10 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.productId = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + + ' productId ' + device.productId); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_10 : PASS'); + }) + + /** + * @tc.number : connect_device_11 + * @tc.name : connectDevice + * @tc.desc : 反向测试 clazz错误参数 打开设备 + */ + it('SUB_USB_connect_device_11', 0, function () { + console.info('usb connect_device_11 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 11 requestRight error:' + error); + }); + console.info('usb 11 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.clazz = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + ' clazz ' + device.clazz); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_11 : PASS'); + }) + + /** + * @tc.number : connect_device_12 + * @tc.name : connectDevice + * @tc.desc : 反向测试 subclass错误参数 打开设备 + */ + it('SUB_USB_connect_device_12', 0, function () { + console.info('usb connect_device_12 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 12 requestRight error:' + error); + }); + console.info('usb 12 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.subclass = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + ' subclass ' + device.subclass); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_12 : PASS'); + }) + + /** + * @tc.number : connect_device_13 + * @tc.name : connectDevice + * @tc.desc : 反向测试 protocol错误参数 打开设备 + */ + it('SUB_USB_connect_device_13', 0, function () { + console.info('usb connect_device_13 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + var isRight = usb.hasRight(gDeviceList[0].name); + if (!isRight) { + usb.requestRight(gDeviceList[0].name).then(hasRight => { + }).catch(error => { + console.info('usb 13 requestRight error:' + error); + }); + console.info('usb 13 requestRight end:'); + CheckEmptyUtils.sleep(3000) + } + + var device = JSON.parse(JSON.stringify(gDeviceList[0])); + device.protocol = 2 + 10000 + try { + var usbDevicePipe = usb.connectDevice(device) + } catch (err) { + expect(true).assertTrue(); + console.info('usb connectDevice fail:' + err); + } + console.info('usb case connectDevice ret: ' + JSON.stringify(usbDevicePipe) + ' protocol ' + device.protocol); + expect(CheckEmptyUtils.isEmpty(usbDevicePipe)).assertFalse(); + var isPipClose = usb.closePipe(usbDevicePipe); + console.info('usb case closePipe ret: ' + isPipClose); + expect(isPipClose).assertEqual(0); + console.info('usb connect_device_13 : PASS'); + }) + + /** + * @tc.number : has_right_02 + * @tc.name : hasRight + * @tc.desc : 反向测试 给错误设备名字 + */ + it('SUB_USB_has_right_02', 0, function () { + console.info('usb has_right_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + var deviceName = gDeviceList[i].name + deviceName = deviceName + '$#' + var hasRight = usb.hasRight(deviceName) + console.info('usb has_right ret :' + hasRight); + expect(hasRight == false).assertTrue(); + } + + console.info('usb has_right_02 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : has_right_03 + * @tc.name : hasRight + * @tc.desc : 反向测试 给设备名字为字母 + */ + it('SUB_USB_has_right_03', 0, function () { + console.info('usb has_right_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + var deviceName = gDeviceList[i].name + deviceName = deviceName + 'abcdg' + var hasRight = usb.hasRight(deviceName) + console.info('usb hasRight ret :' + hasRight); + expect(hasRight == false).assertTrue(); + } + + console.info('usb has_right_03 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : request_right_02 + * @tc.name : requestRight + * @tc.desc : 反向测试 错误设备名字 请求权限 + */ + it('SUB_USB_request_right_02', 0, function () { + console.info('usb request_right_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + var diviceName = gDeviceList[i].name + diviceName = diviceName + '@#' + usb.requestRight(diviceName).then(hasRight => { + console.info('usb request_right ret :' + hasRight); + expect(hasRight).assertFalse(); + console.info('usb request_right_02 : PASS'); + }).catch(error => { + expect(error).assertFalse(); + console.info('usb request_right_02 error: ' + error); + }); + } + }) + + /** + * @tc.number : request_right_03 + * @tc.name : requestRight + * @tc.desc : 反向测试 设备名字为数字 请求权限 + */ + it('SUB_USB_request_right_03', 0, function () { + console.info('usb request_right_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var i = 0; i < gDeviceList.length; i++) { + var diviceName = gDeviceList[i].name + diviceName = diviceName + '123' + usb.requestRight(diviceName).then(hasRight => { + console.info('usb request_right ret :' + hasRight); + expect(hasRight).assertFalse(); + console.info('usb request_right_03 : PASS'); + }).catch(error => { + expect(error).assertFalse(); + console.info('usb request_right_03 error: ' + error); + }); + } + }) +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunit.test.js b/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunit.test.js new file mode 100755 index 0000000000000000000000000000000000000000..a0106077fda4f827f30e628f3feead3a4e272540 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunit.test.js @@ -0,0 +1,465 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import EventConstants from './EventConstants.js'; +import parameter from '@ohos.systemparameter'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb device pipe test */ +describe('UsbDevicePipeJsFunctionsTest', function () { + var gDeviceList + var gPipe + + beforeAll(function () { + console.log('*************Usb Unit UsbDevicePipeJsFunctionsTest Begin*************'); + var Version = usb.getVersion() + console.info('usb unit begin test getversion :' + Version) + + // 切换为host + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 1) { + usb.setPortRoles(usbPortList[0].id, 1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + }); + console.log('*************Usb Unit switch to host Begin*************'); + } + } + + gDeviceList = usb.getDevices(); + gPipe = usb.connectDevice(gDeviceList[0]) + console.info('usb unit connectDevice gPipe ret : ' + JSON.stringify(gPipe)); + }) + + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + + afterAll(function () { + var isPipClose = usb.closePipe(gPipe) + console.info('usb unit close gPipe ret : ' + isPipClose); + console.log('*************Usb Unit UsbDevicePipeJsFunctionsTest End*************'); + }) + + function findInitPoint(testParam, j) { + var bfind = false + for (var k = 0; k < testParam.config.interfaces[j].endpoints.length; k++) { + var endpoint = testParam.config.interfaces[j].endpoints[k]; + if (endpoint.type == EventConstants.USB_ENDPOINT_XFER_BULK) { + bfind = true + if (endpoint.direction == EventConstants.USB_ENDPOINT_DIR_OUT) { + testParam.maxOutSize = endpoint.maxPacketSize; + testParam.outEndpoint = endpoint; + } else if (endpoint.direction == EventConstants.USB_ENDPOINT_DIR_IN) { + testParam.maxInSize = endpoint.maxPacketSize; + testParam.inEndpoint = endpoint + } + } + } + if (bfind) { + testParam.interface = testParam.config.interfaces[j] + return bfind + } + return false + } + + function getFlag(testParam, j) { + if (testParam.config.interfaces[j].endpoints.length == 0) { + return false + } + + if (testParam.config.interfaces[j].clazz != 10 || + testParam.config.interfaces[j].subclass != 0 || + testParam.config.interfaces[j].protocol != 2) { + return false + } + return true + } + + function initPoint(testParam) { + for (var j = 0; j < testParam.config.interfaces.length; j++) { + if (getFlag(testParam, j) == true) { + if (findInitPoint(testParam, j) == true) { + break + } + } + } + } + + // 预制传输相关参数 + function getTransferTestParam() { + var testParam = { + config: null, + device: null, + pip: null, + inEndpoint: null, + interface: null, + outEndpoint: null, + usbRequest: null, + sendData: '', + isClaimed: 0, + maxInSize: 1024, + maxOutSize: 1024 + } + + console.info('usb case gDeviceList.length: ' + gDeviceList.length); + for (var i = 0; i < gDeviceList.length; i++) { + testParam.device = gDeviceList[i] + testParam.config = testParam.device.configs[0] + testParam.pip = gPipe + initPoint(testParam) + } + return testParam + } + + /** + * @tc.number : bulk_transfer_test_01 + * @tc.name : bulkTransfer + * @tc.desc : 批量传输 收数据 + */ + it('SUB_USB_bulk_transfer_test_01', 0, function () { + console.info('usb bulk_transfer_test_01 begin'); + var testParam = getTransferTestParam() + if (testParam.interface == null || testParam.inEndpoint == null) { + expect(false).assertTrue(); + return + } + + testParam.isClaimed = usb.releaseInterface(testParam.pip, testParam.interface) + expect(testParam.isClaimed).assertEqual(0); + testParam.isClaimed = usb.claimInterface(testParam.pip, testParam.interface, true); + expect(testParam.isClaimed).assertEqual(0); + + console.info('usb case readData begin'); + var tmpUint8Array = new Uint8Array(testParam.maxInSize); + usb.bulkTransfer(testParam.pip, testParam.inEndpoint, tmpUint8Array, 5000).then(data => { + console.info('usb case readData tmpUint8Array buffer : ' + CheckEmptyUtils.ab2str(tmpUint8Array)); + console.info('usb case readData ret: ' + data); + console.info('usb case bulk_transfer_test_01 : PASS'); + expect(data >= 0).assertTrue(); + }).catch(error => { + console.info('usb case readData error : ' + JSON.stringify(error)); + expect(false).assertTrue(); + }); + }) + + /** + * @tc.number : bulk_transfer_test_02 + * @tc.name : bulkTransfer + * @tc.desc : 批量传输 发数据 + */ + it('SUB_USB_bulk_transfer_test_02', 0, function () { + console.info('usb bulk_transfer_test_02 begin'); + var testParam = getTransferTestParam() + if (testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + testParam.isClaimed = usb.releaseInterface(testParam.pip, testParam.interface) + expect(testParam.isClaimed).assertEqual(0); + testParam.isClaimed = usb.claimInterface(testParam.pip, testParam.interface, true); + expect(testParam.isClaimed).assertEqual(0); + + testParam.sendData = 'send default'; + try { + testParam.sendData = parameter.getSync('test_usb', 'default'); + console.log('usb parameter ' + JSON.stringify(testParam.sendData)); + } catch (e) { + console.log('usb parameter getSync unexpected error: ' + e); + } + + var tmpUint8Array = CheckEmptyUtils.str2ab(testParam.sendData); + usb.bulkTransfer(testParam.pip, testParam.outEndpoint, tmpUint8Array, 5000).then(data => { + console.info('usb case bulk_transfer_test_02 ret: ' + data); + console.info('usb case bulk_transfer_test_02 send data: ' + testParam.sendData); + console.info('usb case bulk_transfer_test_02 : PASS'); + expect(true).assertTrue(); + }).catch(error => { + console.info('usb write error : ' + JSON.stringify(error)); + expect(false).assertTrue(); + }); + + }) + + /** + * @tc.number : claim_interface_test_01 + * @tc.name : claimInterface + * @tc.desc : 获取接口 并释放 + */ + it('SUB_USB_claim_interface_test_01', 0, function () { + console.info('usb claim_interface_test_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb 01 case get_device_list is null') + expect(gDeviceList.length).assertEqual(-1); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 01 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[k], true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertEqual(0); + if (isClaim == 0) { + isClaim = usb.releaseInterface(gPipe, gDeviceList[0].configs[j].interfaces[k]) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertEqual(0); + } + } + } + + console.info('usb claim_interface_test_01 : PASS'); + expect(true).assertTrue(); + }) + + function getTransferParam(iCmd, iReqType, iValue, iIndex) { + var tmpUint8Array = new Uint8Array(512); + var requestCmd = iCmd + var requestType = iReqType + var value = iValue; + var index = iIndex; + var controlParam = { + request: requestCmd, + reqType: requestType, + value: value, + index: index, + data: tmpUint8Array + } + return controlParam + } + + /** + * @tc.number : set_configuration_test_01 + * @tc.name : setConfiguration + * @tc.desc : 设置设备接口 + */ + it('SUB_USB_set_configuration_test_01', 0, function () { + console.info('usb set_configuration_test_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var ret = usb.setConfiguration(gPipe, gDeviceList[0].configs[j]) + console.info('usb case setConfiguration return : ' + ret); + expect(ret).assertEqual(0); + } + + console.info('usb set_configuration_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_configuration_test_02 + * @tc.name : setConfiguration + * @tc.desc : 反向测试 设置设备接口 + */ + it('SUB_USB_set_configuration_test_02', 0, function () { + console.info('usb set_configuration_test_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var config = JSON.parse(JSON.stringify(gDeviceList[0].configs[j])); + config.id = 255 + var ret = usb.setConfiguration(gPipe, config) + console.info('usb case setConfiguration return : ' + ret); + expect(ret).assertLess(0); + } + + console.info('usb set_configuration_test_02 : PASS'); + }) + + /** + * @tc.number : set_interface_test_01 + * @tc.name : setInterface + * @tc.desc : 设置设备接口 + */ + it('SUB_USB_set_interface_test_01', 0, function () { + console.info('usb set_interface_test_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true) + expect(isClaim).assertEqual(0) + if (isClaim == 0) { + var ret = usb.setInterface(gPipe, gDeviceList[0].configs[j].interfaces[0]) + expect(ret).assertEqual(0); + console.info('usb case setInterface return : ' + ret); + } + } + + console.info('usb set_interface_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_interface_test_02 + * @tc.name : setInterface + * @tc.desc : 反向测试 设置设备接口 + */ + it('SUB_USB_set_interface_test_02', 0, function () { + console.info('usb set_interface_test_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue() + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true) + expect(isClaim).assertEqual(0) + if (isClaim == 0) { + var tmpInterface = JSON.parse(JSON.stringify(gDeviceList[0].configs[j].interfaces[0])); + tmpInterface.id = 234 + var ret = usb.setInterface(gPipe, tmpInterface) + expect(ret).assertLess(0) + console.info('usb case setInterface return : ' + ret) + } + } + + console.info('usb set_interface_test_02 : PASS'); + expect(true).assertTrue(); + }) + + function callControlTransfer(pip, controlParam, timeout, caseName) { + usb.controlTransfer(pip, controlParam, timeout).then(data => { + console.info('usb controlTransfer ret data : ' + data + ' ' + caseName); + console.info('usb controlTransfer controlParam.data buffer : ' + controlParam.data + ' ' + caseName); + console.info('usb' + caseName + ': PASS'); + expect(true).assertTrue(); + }).catch(error => { + console.info('usb controlTransfer error : ' + JSON.stringify(error)); + console.info('usb' + caseName + ': PASS'); + expect(false).assertTrue(); + }); + } + + /** + * @tc.number : control_transfer_test_01 + * @tc.name : controlTransfer + * @tc.desc : 控制传输 GetDescriptor: cmd 6 reqType 128 value 512 index 0 + */ + it('SUB_USB_control_transfer_test_01', 0, function () { + console.info('usb control_transfer_test_01 begin'); + var testParam = getTransferTestParam() + if (testParam.inEndpoint == null || testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + var timeout = 5000; + var controlParam = getTransferParam(6, ((1 << 7) | (0 << 5) | (0 & 0x1f)), (2 << 8), 0) + callControlTransfer(testParam.pip, controlParam, timeout, 'control_transfer_test_01 GetDescriptor') + }) + + /** + * @tc.number : control_transfer_test_02 + * @tc.name : controlTransfer + * @tc.desc : 控制传输 GetStatus: cmd 0 reqType 128 value 0 index 0 + */ + it('SUB_USB_control_transfer_test_02', 0, function () { + console.info('usb control_transfer_test_02 begin'); + var testParam = getTransferTestParam() + if (testParam.inEndpoint == null || testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + var timeout = 5000; + var controlParam = getTransferParam(0, 128, 0, 0) + callControlTransfer(testParam.pip, controlParam, timeout, 'control_transfer_test_02 GetStatus') + }) + + /** + * @tc.number : control_transfer_test_03 + * @tc.name : controlTransfer + * @tc.desc : 控制传输 GetConfiguration: cmd 8 reqType 128 value 0 index 0 + */ + it('SUB_USB_control_transfer_test_03', 0, function () { + console.info('usb control_transfer_test_03 begin'); + var testParam = getTransferTestParam() + if (testParam.inEndpoint == null || testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + var timeout = 5000; + var controlParam = getTransferParam(8, 128, 0, 0) + callControlTransfer(testParam.pip, controlParam, timeout, 'control_transfer_test_03 GetConfiguration') + }) + + /** + * @tc.number : control_transfer_test_04 + * @tc.name : controlTransfer + * @tc.desc : 控制传输 GetInterface: cmd 10 reqType 129 value 0 index 1 + */ + it('SUB_USB_control_transfer_test_04', 0, function () { + console.info('usb control_transfer_test_04 begin'); + var testParam = getTransferTestParam() + if (testParam.inEndpoint == null || testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + var timeout = 5000; + var controlParam = getTransferParam(10, 129, 0, 1) + callControlTransfer(testParam.pip, controlParam, timeout, 'control_transfer_test_04 GetInterface') + }) + + /** + * @tc.number : control_transfer_test_05 + * @tc.name : controlTransfer + * @tc.desc : 控制传输 ClearFeature: cmd 1 reqType 0 value 0 index 0 + */ + it('SUB_USB_control_transfer_test_05', 0, function () { + console.info('usb control_transfer_test_05 begin'); + var testParam = getTransferTestParam() + if (testParam.inEndpoint == null || testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + var timeout = 5000; + var controlParam = getTransferParam(1, 0, 0, 0) + callControlTransfer(testParam.pip, controlParam, timeout, 'control_transfer_test_05 ClearFeature') + }) + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunitEx.test.js b/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunitEx.test.js new file mode 100755 index 0000000000000000000000000000000000000000..42f715712d9fc2cf86705c869fac7c9d20324ee4 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbDevicePipeJsunitEx.test.js @@ -0,0 +1,778 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import EventConstants from './EventConstants.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb device pipe test */ +describe('UsbDevicePipeJsFunctionsTestEx', function () { + var gDeviceList + var gPipe + + beforeAll(function () { + console.log('*************Usb Unit UsbDevicePipeJsFunctionsTestEx Begin*************'); + var Version = usb.getVersion() + console.info('usb unit begin test getversion :' + Version) + + // 切换为host + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 1) { + usb.setPortRoles(usbPortList[0].id, 1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + }); + + console.log('*************Usb Unit switch to host Ex Begin*************'); + CheckEmptyUtils.sleep(3000) + } + } + + gDeviceList = usb.getDevices(); + gPipe = usb.connectDevice(gDeviceList[0]) + console.info('usb unit connectDevice gPipe ret : ' + JSON.stringify(gPipe)); + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Ex Case*************'); + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Ex Case*************'); + }) + afterAll(function () { + var isPipClose = usb.closePipe(gPipe) + console.info('usb unit close gPipe ret : ' + isPipClose); + console.log('*************Usb Unit UsbDevicePipeJsFunctionsTestEx End*************'); + }) + + function findInitPoint(testParam, j) { + var bfind = false + for (var k = 0; k < testParam.config.interfaces[j].endpoints.length; k++) { + var endpoint = testParam.config.interfaces[j].endpoints[k]; + if (endpoint.type == EventConstants.USB_ENDPOINT_XFER_BULK) { + + bfind = true + if (endpoint.direction == EventConstants.USB_ENDPOINT_DIR_OUT) { + testParam.outEndpoint = endpoint; + testParam.maxOutSize = endpoint.maxPacketSize; + } else if (endpoint.direction == EventConstants.USB_ENDPOINT_DIR_IN) { + testParam.inEndpoint = endpoint + testParam.maxInSize = endpoint.maxPacketSize; + } + } + } + if (bfind) { + testParam.interface = testParam.config.interfaces[j] + return true + } + return false + } + + function getFlag(testParam, j) { + if (testParam.config.interfaces[j].clazz != 10 || + testParam.config.interfaces[j].subclass != 0 || + testParam.config.interfaces[j].protocol != 2) { + return false + } + + if (testParam.config.interfaces[j].endpoints.length == 0) { + return false + } + + return true + } + + function initPoint(testParam) { + for (var j = 0; j < testParam.config.interfaces.length; j++) { + if (getFlag(testParam, j)) { + if (findInitPoint(testParam, j)) { + break + } + } + } + } + + // 预制传输相关参数 + function getTransferTestParam() { + var testParam = { + device: null, + config: null, + pip: null, + inEndpoint: null, + outEndpoint: null, + interface: null, + usbRequest: null, + sendData: '', + isClaimed: 0, + maxInSize: 1024, + maxOutSize: 1024 + } + + console.info('usb case gDeviceList.length: ' + gDeviceList.length); + for (var i = 0; i < gDeviceList.length; i++) { + testParam.device = gDeviceList[i] + testParam.config = testParam.device.configs[0] + testParam.pip = gPipe + initPoint(testParam) + } + return testParam + } + + /** + * @tc.number : bulk_transfer_test_03 + * @tc.name : bulkTransfer + * @tc.desc : 反向测试 批量传输 发数据 修改 outEndpoint.address = 123 + */ + it('SUB_USB_bulk_transfer_test_03', 0, function () { + console.info('usb bulk_transfer_test_03 begin'); + var testParam = getTransferTestParam() + if (testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + testParam.isClaimed = usb.releaseInterface(testParam.pip, testParam.interface) + expect(testParam.isClaimed).assertEqual(0); + testParam.isClaimed = usb.claimInterface(testParam.pip, testParam.interface, true); + expect(testParam.isClaimed).assertEqual(0); + + testParam.sendData = 'send time 13213213 wzy 03'; + var tmpUint8Array = CheckEmptyUtils.str2ab(testParam.sendData); + var TmpTestParam = testParam + TmpTestParam.outEndpoint.address = 123 + usb.bulkTransfer(TmpTestParam.pip, TmpTestParam.outEndpoint, tmpUint8Array, 5000).then(data => { + console.info('usb case bulk_transfer_test_03 ret: ' + data); + console.info('usb case bulk_transfer_test_03 : PASS'); + expect(data).assertEqual(-1); + }).catch(error => { + console.info('usb 03 write error : ' + JSON.stringify(error)); + console.info('usb case bulk_transfer_test_03 : PASS'); + expect(false).assertFalse(); + }); + }) + + /** + * @tc.number : bulk_transfer_test_04 + * @tc.name : bulkTransfer + * @tc.desc : 反向测试 批量传输 发数据 修改 outEndpoint.number = 123 + */ + it('SUB_USB_bulk_transfer_test_04', 0, function () { + console.info('usb bulk_transfer_test_04 begin'); + var testParam = getTransferTestParam() + if (testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + testParam.isClaimed = usb.releaseInterface(testParam.pip, testParam.interface) + expect(testParam.isClaimed).assertEqual(0); + testParam.isClaimed = usb.claimInterface(testParam.pip, testParam.interface, true); + expect(testParam.isClaimed).assertEqual(0); + + testParam.sendData = 'send time 13213213 wzy 04'; + var tmpUint8Array = CheckEmptyUtils.str2ab(testParam.sendData); + var TmpTestParam = testParam + TmpTestParam.outEndpoint.number = 123 + usb.bulkTransfer(TmpTestParam.pip, TmpTestParam.outEndpoint, tmpUint8Array, 5000).then(data => { + console.info('usb case bulk_transfer_test_04 ret: ' + data); + console.info('usb case bulk_transfer_test_04 : PASS'); + expect(data).assertEqual(-1); + }).catch(error => { + console.info('usb 04 write error : ' + JSON.stringify(error)); + console.info('usb case bulk_transfer_test_04 : PASS'); + expect(false).assertFalse(); + }); + }) + + /** + * @tc.number : bulk_transfer_test_05 + * @tc.name : bulkTransfer + * @tc.desc : 反向测试 批量传输 发数据 修改 outEndpoint.type = 123 + */ + it('SUB_USB_bulk_transfer_test_05', 0, function () { + console.info('usb bulk_transfer_test_05 begin'); + var testParam = getTransferTestParam() + if (testParam.interface == null || testParam.outEndpoint == null) { + expect(false).assertTrue(); + return + } + + testParam.isClaimed = usb.releaseInterface(testParam.pip, testParam.interface) + expect(testParam.isClaimed).assertEqual(0); + testParam.isClaimed = usb.claimInterface(testParam.pip, testParam.interface, true); + expect(testParam.isClaimed).assertEqual(0); + + testParam.sendData = 'send time 13213213 wzy 05'; + var tmpUint8Array = CheckEmptyUtils.str2ab(testParam.sendData); + var TmpTestParam = testParam + TmpTestParam.outEndpoint.type = 123 + usb.bulkTransfer(TmpTestParam.pip, TmpTestParam.outEndpoint, tmpUint8Array, 5000).then(data => { + console.info('usb case bulk_transfer_test_05 ret: ' + data); + console.info('usb case bulk_transfer_test_05 : PASS'); + expect(data).assertEqual(-1); + }).catch(error => { + console.info('usb 05 write error : ' + JSON.stringify(error)); + console.info('usb case bulk_transfer_test_05 : PASS'); + expect(false).assertFalse(); + }); + }) + + /** + * @tc.number : claim_interface_test_02 + * @tc.name : claimInterface + * @tc.desc : 反向测试 USBInterface传入异常id 获取接口 失败 + */ + it('SUB_USB_claim_interface_test_02', 0, function () { + console.info('usb claim_interface_test_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb case 02 current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case 02 current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var TmpInterface = gDeviceList[0].configs[j].interfaces[k] + TmpInterface.id = 123 + var isClaim = usb.claimInterface(gPipe, TmpInterface, true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb claim_interface_test_02 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : claim_interface_test_03 + * @tc.name : claimInterface + * @tc.desc : 反向测试 USBInterface传入异常protocol 获取接口 失败 + */ + it('SUB_USB_claim_interface_test_03', 0, function () { + console.info('usb claim_interface_test_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb case 03 current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case 03 current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var TmpInterface = gDeviceList[0].configs[j].interfaces[k] + TmpInterface.protocol = 120 + var isClaim = usb.claimInterface(gPipe, TmpInterface, true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb claim_interface_test_03 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : claim_interface_test_04 + * @tc.name : claimInterface + * @tc.desc : 反向测试 USBInterface传入异常clazz 获取接口 失败 + */ + it('SUB_USB_claim_interface_test_04', 0, function () { + console.info('usb claim_interface_test_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb case 04 current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case 04 current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var TmpInterface = gDeviceList[0].configs[j].interfaces[k] + TmpInterface.clazz = 784 + var isClaim = usb.claimInterface(gPipe, TmpInterface, true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb claim_interface_test_04 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : claim_interface_test_05 + * @tc.name : claimInterface + * @tc.desc : 反向测试 USBInterface传入异常name 获取接口 失败 + */ + it('SUB_USB_claim_interface_test_05', 0, function () { + console.info('usb claim_interface_test_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb case 05 current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case 05 current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var TmpInterface = gDeviceList[0].configs[j].interfaces[k] + TmpInterface.name = '123sdf' + var isClaim = usb.claimInterface(gPipe, TmpInterface, true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb claim_interface_test_05 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : claim_interface_test_06 + * @tc.name : claimInterface + * @tc.desc : 反向测试 USBInterface传入异常subClass 获取接口 失败 + */ + it('SUB_USB_claim_interface_test_06', 0, function () { + console.info('usb claim_interface_test_06 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb case 06 current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb case 06 current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var TmpInterface = gDeviceList[0].configs[j].interfaces[k] + TmpInterface.subClass = 1210 + var isClaim = usb.claimInterface(gPipe, TmpInterface, true) + console.info('usb case claimInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb claim_interface_test_06 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : release_interface_test_01 + * @tc.name : releaseInterface + * @tc.desc : 反向测试 获取接口 release传入Interface的错误ID 并释放 + */ + it('SUB_USB_release_interface_test_01', 0, function () { + console.info('usb release_interface_test_01 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 01 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb 01 case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var tmpInterface = gDeviceList[0].configs[j].interfaces[k] + tmpInterface.id = 134 + var isClaim = usb.releaseInterface(gPipe, tmpInterface) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb release_interface_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : release_interface_test_02 + * @tc.name : releaseInterface + * @tc.desc : 反向测试 获取接口 release传入Interface的错误name 并释放 + */ + it('SUB_USB_release_interface_test_02', 0, function () { + console.info('usb release_interface_test_02 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 02 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb 02 case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var tmpInterface = gDeviceList[0].configs[j].interfaces[k] + tmpInterface.name = '134wer' + var isClaim = usb.releaseInterface(gPipe, tmpInterface) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb release_interface_test_02 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : release_interface_test_03 + * @tc.name : releaseInterface + * @tc.desc : 反向测试 获取接口 release传入Interface的错误clazz 并释放 + */ + it('SUB_USB_release_interface_test_03', 0, function () { + console.info('usb release_interface_test_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 03 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb 03 case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var tmpInterface = gDeviceList[0].configs[j].interfaces[k] + tmpInterface.clazz = 78 + var isClaim = usb.releaseInterface(gPipe, tmpInterface) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb release_interface_test_03 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : release_interface_test_04 + * @tc.name : releaseInterface + * @tc.desc : 反向测试 获取接口 release传入Interface的错误protocol 并释放 + */ + it('SUB_USB_release_interface_test_04', 0, function () { + console.info('usb release_interface_test_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 04 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb 04 case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var tmpInterface = gDeviceList[0].configs[j].interfaces[k] + tmpInterface.protocol = 124 + var isClaim = usb.releaseInterface(gPipe, tmpInterface) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb release_interface_test_04 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : release_interface_test_05 + * @tc.name : releaseInterface + * @tc.desc : 反向测试 获取接口 release传入Interface的错误subClass 并释放 + */ + it('SUB_USB_release_interface_test_05', 0, function () { + console.info('usb release_interface_test_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + if (gDeviceList[0].configs.length == 0) { + console.info('usb 05 case current device.configs.length = 0'); + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + if (gDeviceList[0].configs[j].interfaces.length == 0) { + console.info('usb 05 case current device.configs.interfaces.length = 0'); + } + for (var k = 0; k < gDeviceList[0].configs[j].interfaces.length; k++) { + var tmpInterface = gDeviceList[0].configs[j].interfaces[k] + tmpInterface.subClass = 784 + var isClaim = usb.releaseInterface(gPipe, tmpInterface) + console.info('usb case releaseInterface function return: ' + isClaim); + expect(isClaim).assertLess(0); + } + } + + console.info('usb release_interface_test_05 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_interface_test_03 + * @tc.name : setInterface + * @tc.desc : 反向测试 Interface的protocol 设置设备接口 + */ + it('SUB_USB_set_interface_test_03', 0, function () { + console.info('usb set_interface_test_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue() + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaimed = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true); + + var tmpInterface = gDeviceList[0].configs[j].interfaces[0] + tmpInterface.protocol = 482 + var ret = usb.setInterface(gPipe, tmpInterface) + expect(ret).assertLess(0) + console.info('usb case setInterface return : ' + ret) + } + + console.info('usb set_interface_test_03 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_interface_test_04 + * @tc.name : setInterface + * @tc.desc : 反向测试 Interface的clazz 设置设备接口 + */ + it('SUB_USB_set_interface_test_04', 0, function () { + console.info('usb set_interface_test_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue() + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true) + + var tmpInterface = gDeviceList[0].configs[j].interfaces[0] + tmpInterface.clazz = 482 + var ret = usb.setInterface(gPipe, tmpInterface) + expect(ret).assertLess(0) + console.info('usb case setInterface return : ' + ret) + + } + + console.info('usb set_interface_test_04 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_interface_test_05 + * @tc.name : setInterface + * @tc.desc : 反向测试 Interface的subClass 设置设备接口 + */ + it('SUB_USB_set_interface_test_05', 0, function () { + console.info('usb set_interface_test_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue() + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true) + + var tmpInterface = gDeviceList[0].configs[j].interfaces[0] + tmpInterface.subClass = 482 + var ret = usb.setInterface(gPipe, tmpInterface) + expect(ret).assertLess(0) + console.info('usb case setInterface return : ' + ret) + + } + + console.info('usb set_interface_test_05 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_interface_test_06 + * @tc.name : setInterface + * @tc.desc : 反向测试 Interface的name 设置设备接口 + */ + it('SUB_USB_set_interface_test_06', 0, function () { + console.info('usb set_interface_test_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue() + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var isClaim = usb.claimInterface(gPipe, gDeviceList[0].configs[j].interfaces[0], true) + + var tmpInterface = gDeviceList[0].configs[j].interfaces[0] + tmpInterface.name = 'wer32' + var ret = usb.setInterface(gPipe, tmpInterface) + expect(ret).assertLess(0) + console.info('usb case setInterface return : ' + ret) + } + + console.info('usb set_interface_test_06 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_configuration_test_03 + * @tc.name : setConfiguration + * @tc.desc : 反向测试 USBConfig的name 赋值错误 设置设备接口 + */ + it('SUB_USB_set_configuration_test_03', 0, function () { + console.info('usb set_configuration_test_03 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var config = gDeviceList[0].configs[j] + config.name = 'asdfsd' + var ret = usb.setConfiguration(gPipe, config) + console.info('usb case setConfiguration return : ' + ret); + expect(ret).assertEqual(0); + } + + console.info('usb set_configuration_test_03 : PASS'); + }) + + /** + * @tc.number : set_configuration_test_04 + * @tc.name : setConfiguration + * @tc.desc : 反向测试 USBConfig的name 赋值错误 设置设备接口 + */ + it('SUB_USB_set_configuration_test_04', 0, function () { + console.info('usb set_configuration_test_04 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var config = gDeviceList[0].configs[j] + config.name = 'asdfsd' + config.id = 154 + var ret = usb.setConfiguration(gPipe, config) + console.info('usb case setConfiguration return : ' + ret); + expect(ret).assertLess(0); + } + + console.info('usb set_configuration_test_04 : PASS'); + }) + + /** + * @tc.number : set_configuration_test_05 + * @tc.name : setConfiguration + * @tc.desc : 反向测试 USBConfig的attributes 赋值错误 设置设备接口 + */ + it('SUB_USB_set_configuration_test_05', 0, function () { + console.info('usb set_configuration_test_05 begin'); + if (gDeviceList.length == 0) { + console.info('usb case get_device_list is null') + expect(false).assertTrue(); + return + } + + for (var j = 0; j < gDeviceList[0].configs.length; j++) { + var config = gDeviceList[0].configs[j] + config.attributes = 154 + var ret = usb.setConfiguration(gPipe, config) + console.info('usb case setConfiguration return : ' + ret); + expect(ret).assertLess(0); + } + + console.info('usb set_configuration_test_05 : PASS'); + }) + + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunit.test.js b/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunit.test.js new file mode 100755 index 0000000000000000000000000000000000000000..73320625f4efe6311a4eb645492f9ec2d990198f --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunit.test.js @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb core functions test */ +describe('UsbFunctionsJsFunctionsTest', function () { + + beforeAll(function () { + console.log('*************Usb Unit UsbFunctionsJsFunctionsTest Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + // 切换到 device + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 2) { + console.info('usb case set data role 1, data role 1'); + usb.setPortRoles(usbPortList[0].id, 2, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + }); + console.log('*************Usb Unit switch to device Begin*************'); + } + } + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + afterAll(function () { + console.log('*************Usb Unit UsbFunctionsJsFunctionsTest End*************'); + }) + + /** + * @tc.number : get_current_functions_test_01 + * @tc.name : getCurrentFunctions + * @tc.desc : 获取当前设备模式 掩码与描述字符转换 + */ + it('SUB_USB_get_current_functions_test_01', 0, function () { + console.info('usb get_current_functions_test_01 begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + + var strMaskCode = usb.usbFunctionsToString(maskCode) + console.info('usb case usbFunctionsToString return str: ' + strMaskCode); + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode); + expect(nMaskCode).assertEqual(maskCode); + console.info('usb get_current_functions_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : get_current_functions_test_02 + * @tc.name : usbFunctionString + * @tc.desc : 反向测试 获取当前设备模式 掩码与描述字符转换 + */ + it('SUB_USB_get_current_functions_test_02', 0, function () { + console.info('usb get_current_functions_test_02 begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + + var strMaskCode = usb.usbFunctionsToString(maskCode) + console.info('usb case usbFunctionsToString return str: ' + strMaskCode); + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode); + + var errmaskCode = 0 + var strMaskCode = usb.usbFunctionsToString(errmaskCode) + + console.info('usb case ' + errmaskCode + ' usbFunctionsToString return str: ' + strMaskCode); + + var errStrMaskCode = 'none' + var nMaskCode = usb.usbFunctionsFromString(errStrMaskCode) + expect(nMaskCode).assertEqual(0); + console.info('usb case errStrMaskCode ' + errStrMaskCode + + ' usbFunctionsFromString return int: ' + nMaskCode); + + console.info('usb get_current_functions_test_02 : PASS'); + }) + + function callSetCurFunction(caseName, iValue) { + CheckEmptyUtils.sleep(3000) + console.info('usb case param case name:' + caseName); + console.info('usb case param iValue:' + iValue); + usb.setCurrentFunctions(iValue).then(data => { + console.info('usb case SetCurFunction ret:' + data); + expect(data).assertTrue(); + console.info('usb case ' + caseName + ': PASS'); + }).catch(error => { + console.info('usb case ' + caseName + ' error : ' + error); + expect(false).assertTrue(); + }); + } + + /** + * @tc.number : set_current_functions_test_02 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 1 设置为ACM功能 + */ + it('SUB_USB_set_current_functions_test_02', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_02 set ACM begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_02 ACM 1', 1) + }) + + /** + * @tc.number : set_current_functions_test_03 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 2 设置为ECM功能 + */ + it('SUB_USB_set_current_functions_test_03', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_03 set ECM begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_03 ECM 2', 2) + }) + + /** + * @tc.number : set_current_functions_test_04 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 3 设置为ACM、ECM功能 + */ + it('SUB_USB_set_current_functions_test_04', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_04 set ACM ECM begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_04 ACM ECM 3', 3) + }) + + /** + * @tc.number : set_current_functions_test_05 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 4 设置为HDC功能 + */ + it('SUB_USB_set_current_functions_test_05', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_05 set HDC begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_05 HDC 4', 4) + }) + + /** + * @tc.number : set_current_functions_test_06 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 5 设置为ACM、HDC功能 + */ + it('SUB_USB_set_current_functions_test_06', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_06 set ACM HDC begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_06 ACM HDC 5', 5) + }) + + /** + * @tc.number : set_current_functions_test_07 + * @tc.name : functions_test + * @tc.desc : 在设备模式下设置当前的USB功能列表 Set 6 设置为ECM、HDC功能 + */ + it('SUB_USB_set_current_functions_test_07', 0, function () { + CheckEmptyUtils.sleep(3000) + console.info('usb set_current_functions_test_07 set ECM HDC begin'); + var maskCode = usb.getCurrentFunctions(); + console.info('usb case getCurrentFunctions return: ' + maskCode); + var funcString = usb.usbFunctionsToString(maskCode); + console.info('usb case funcString:' + funcString); + callSetCurFunction('set_current_functions_test_07 ECM HDC 6', 6) + }) + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunitEx.test.js b/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunitEx.test.js new file mode 100755 index 0000000000000000000000000000000000000000..f339e8f638098946259dd94835eb431a202aa48c --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbFunctionsJsunitEx.test.js @@ -0,0 +1,250 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb core functions test */ +describe('UsbFunctionsJsFunctionsTestEx', function () { + + beforeAll(function () { + console.log('*************Usb Unit UsbFunctionsJsFunctionsTestEx Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + + // 切换到 device + var usbPortList = usb.getPorts() + if (usbPortList.length > 0) { + if (usbPortList[0].status.currentMode == 2) { + console.info('usb case set data role 1, data role 1'); + usb.setPortRoles(usbPortList[0].id, 2, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + }); + + console.log('*************Usb Unit switch to device Begin*************'); + } + } + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + + afterAll(function () { + console.log('*************Usb Unit UsbFunctionsJsFunctionsTest End*************'); + }) + + /** + * @tc.number : usb_function_to_string_test_01 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 0 返回 'none' + */ + it('SUB_USB_usb_function_to_string_test_01', 0, function () { + console.info('usb usb_function_to_string_test_01 begin'); + var maskCode = 0 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('none'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_01 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_02 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 1 返回 'acm' + */ + it('SUB_USB_usb_function_to_string_test_02', 0, function () { + console.info('usb usb_function_to_string_test_02 begin'); + var maskCode = 1 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('acm'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_02 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_03 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 2 返回 'ecm' + */ + it('SUB_USB_usb_function_to_string_test_03', 0, function () { + console.info('usb usb_function_to_string_test_02 begin'); + var maskCode = 2 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('ecm'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_02 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_04 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 3 返回 'acm,ecm' + */ + it('SUB_USB_usb_function_to_string_test_04', 0, function () { + console.info('usb usb_function_to_string_test_02 begin'); + var maskCode = 3 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('acm,ecm'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_02 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_05 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 4 返回 'hdc' + */ + it('SUB_USB_usb_function_to_string_test_05', 0, function () { + console.info('usb usb_function_to_string_test_05 begin'); + var maskCode = 4 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('hdc'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_05 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_06 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 5 返回 'acm,hdc' + */ + it('SUB_USB_usb_function_to_string_test_06', 0, function () { + console.info('usb usb_function_to_string_test_06 begin'); + var maskCode = 5 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('hdc,acm'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_06 : PASS'); + }) + + /** + * @tc.number : usb_function_to_string_test_07 + * @tc.name : usbFunctionsToString + * @tc.desc : 正向测试 6 返回 'ecm,hdc' + */ + it('SUB_USB_usb_function_to_string_test_07', 0, function () { + console.info('usb usb_function_to_string_test_07 begin'); + var maskCode = 6 + var strMaskCode = usb.usbFunctionsToString(maskCode) + expect(strMaskCode).assertEqual('hdc,ecm'); + console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode); + console.info('usb usb_function_to_string_test_07 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_01 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'none' 返回 0 + */ + it('SUB_USB_usb_function_from_string_test_01', 0, function () { + console.info('usb usb_function_from_string_test_01 begin'); + var strMaskCode = 'none' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(0); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_to_string_test_01 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_02 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'acm' 返回 1 + */ + it('SUB_USB_usb_function_from_string_test_02', 0, function () { + console.info('usb usb_function_from_string_test_02 begin'); + var strMaskCode = 'acm' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(1); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_02 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_03 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'ecm' 返回 2 + */ + it('SUB_USB_usb_function_from_string_test_03', 0, function () { + console.info('usb usb_function_from_string_test_03 begin'); + var strMaskCode = 'ecm' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(2); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_03 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_04 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'acm,ecm' 返回 3 + */ + it('SUB_USB_usb_function_from_string_test_04', 0, function () { + console.info('usb usb_function_from_string_test_04 begin'); + var strMaskCode = 'acm,ecm' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(3); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_04 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_05 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'hdc' 返回 4 + */ + it('SUB_USB_usb_function_from_string_test_05', 0, function () { + console.info('usb usb_function_from_string_test_05 begin'); + var strMaskCode = 'hdc' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(4); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_04 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_06 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'acm,hdc' 返回 5 + */ + it('SUB_USB_usb_function_from_string_test_06', 0, function () { + console.info('usb usb_function_from_string_test_06 begin'); + var strMaskCode = 'acm,hdc' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(5); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_06 : PASS'); + }) + + /** + * @tc.number : usb_function_from_string_test_07 + * @tc.name : usbFunctionsFromString + * @tc.desc : 正向测试 'ecm,hdc' 返回 6 + */ + it('SUB_USB_usb_function_from_string_test_07', 0, function () { + console.info('usb usb_function_from_string_test_07 begin'); + var strMaskCode = 'ecm,hdc' + var nMaskCode = usb.usbFunctionsFromString(strMaskCode) + expect(nMaskCode).assertEqual(6); + console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode); + console.info('usb usb_function_from_string_test_07 : PASS'); + }) + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbPortJsunit.test.js b/usb/usb_standard/src/main/js/default/test/UsbPortJsunit.test.js new file mode 100755 index 0000000000000000000000000000000000000000..86be317426afd667c565ff4d4a7fbd916feba341 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbPortJsunit.test.js @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb core functions test */ +describe('UsbPortJsFunctionsTest', function () { + + beforeAll(function () { + console.log('*************Usb Unit UsbPortJsFunctionsTest Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + afterAll(function () { + console.log('*************Usb Unit UsbPortJsFunctionsTest End*************'); + }) + + /** + * @tc.number : get_ports_test_01 + * @tc.name : getPorts + * @tc.desc : 获取USB端口描述信息列表 + */ + it('SUB_USB_get_ports_test_01', 0, function () { + console.info('usb get_ports_test_01 begin'); + var usbPortList = usb.getPorts() + if (usbPortList.length == 0) { + console.info('usb get_ports_test_01 usbPortList is null'); + expect(false).assertTrue(); + return + } + expect(usbPortList.length > 0).assertTrue(); + console.info('usb get_ports_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : get_supported_modes_test_01 + * @tc.name : getSupportedModes + * @tc.desc : 获取指定的端口支持的模式列表的组合掩码 + */ + it('SUB_USB_get_supported_modes_test_01', 0, function () { + console.info('usb get_supported_modes_test_01 begin'); + var usbPortList = usb.getPorts() + if (usbPortList.length == 0) { + console.info('usb get_supported_modes_test_01 usbPortList is null'); + expect(false).assertTrue(); + return + } + + expect(usbPortList.length > 0).assertTrue(); + for (var i = 0; i < usbPortList.length; i++) { + var maskCode = usb.getSupportedModes(usbPortList[i].id) + expect(maskCode).assertEqual(usbPortList[i].supportedModes); + } + + console.info('usb get_supported_modes_test_01 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_01 + * @tc.name : setPortRoles + * @tc.desc : 切换为Device set powerRole 2 DataRole 2 + */ + it('SUB_USB_set_port_roles_test_01', 0, function () { + console.info('usb set_port_roles_test_01 device 2 2 begin'); + var usbPortList = usb.getPorts() + if (usbPortList.length == 0) { + console.info('usb set_port_roles_test_01 device 2 2 usbPortList is null'); + expect(false).assertTrue(); + return + } + + for (var i = 0; i < usbPortList.length; i++) { + console.info('usb case set data role 2, data role 2'); + usb.setPortRoles(usbPortList[i].id, 2, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(false).assertTrue(); + }); + } + + console.info('usb set_port_roles_test_01 device 2 2: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_02 + * @tc.name : setPortRoles + * @tc.desc : 切换为host set powerRole 1 DataRole 1 + */ + it('SUB_USB_set_port_roles_test_02', 0, function () { + console.info('usb set_port_roles_test_02 host 1 1 begin'); + var usbPortList = usb.getPorts() + if (usbPortList.length == 0) { + console.info('usb set_port_roles_test_02 host 1 1 usbPortList is null'); + expect(false).assertTrue(); + return + } + + for (var i = 0; i < usbPortList.length; i++) { + console.info('usb case set data role 1, data role 1'); + CheckEmptyUtils.sleep(5000) + usb.setPortRoles(usbPortList[i].id, 1, 1).then(data => { + expect(data).assertTrue(); + console.info('usb case setPortRoles return: ' + data); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(false).assertTrue(); + }); + } + + console.info('usb set_port_roles_test_02 host 1 1: PASS'); + expect(true).assertTrue(); + }) + +}) diff --git a/usb/usb_standard/src/main/js/default/test/UsbPortJsunitEx.test.js b/usb/usb_standard/src/main/js/default/test/UsbPortJsunitEx.test.js new file mode 100755 index 0000000000000000000000000000000000000000..dfb5088003fc4b5adab243f270097a5a465b8640 --- /dev/null +++ b/usb/usb_standard/src/main/js/default/test/UsbPortJsunitEx.test.js @@ -0,0 +1,387 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import usb from '@ohos.usb'; +import CheckEmptyUtils from './CheckEmptyUtils.js'; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +/* usb core functions test */ +describe('UsbPortJsFunctionsTestEx', function () { + + beforeAll(function () { + console.log('*************Usb Unit UsbPortJsFunctionsTestEx Begin*************'); + var Version = usb.getVersion() + console.info('begin test getversion :' + Version) + }) + beforeEach(function () { + console.info('beforeEach: *************Usb Unit Test Case*************'); + }) + + afterEach(function () { + console.info('afterEach: *************Usb Unit Test Case*************'); + }) + + afterAll(function () { + console.log('*************Usb Unit UsbPortJsFunctionsTest End*************'); + }) + + /** + * @tc.number : get_supported_modes_test_02 + * @tc.name : getSupportedModes + * @tc.desc : 反向测试 改变id 获取指定的端口支持的模式列表的组合掩码 + */ + it('SUB_USB_get_supported_modes_test_02', 0, function () { + console.info('usb get_supported_modes_test_02 begin'); + var usbPortList = usb.getPorts() + if (usbPortList.length == 0) { + console.info('usb get_supported_modes_test_01 usbPortList is null'); + expect(false).assertTrue(); + return + } + + expect(usbPortList.length > 0).assertTrue(); + + var portID = usbPortList[0].id + 10 + var maskCode = usb.getSupportedModes(portID) + expect(maskCode).assertEqual(0); + + console.info('usb get_supported_modes_test_02 : PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_03 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_03', 0, function () { + var portId = 2; + var powerRole = 2; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(2, 2, -1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_03: PASS'); + }) + + console.info('set_port_roles_test_03: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_04 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,portId,powerRole参数错误 + */ + it('SUB_USB_set_port_roles_test_04', 0, function () { + var portId = -1; + var powerRole = -1; + var dataRole = 2; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, -1, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_04: PASS'); + }) + + console.info('set_port_roles_test_04: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_05 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,portId,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_05', 0, function () { + var portId = -1; + var powerRole = 2; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, 2, -1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_05: PASS'); + }) + + console.info('set_port_roles_test_05: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_06 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,powerRole,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_06', 0, function () { + var portId = 2; + var powerRole = -1; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(2, -1, -1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_06: PASS'); + }) + + console.info('set_port_roles_test_06: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_07 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,portId,portId,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_07', 0, function () { + var portId = -1; + var dataRole = -1; + var powerRole = -1; + + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, -1, -1).then(data => { + console.info('usb case setPortRoles 07 return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles 07 error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_07: PASS'); + }) + + console.info('set_port_roles_test_07: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_08 + * @tc.name : setPortRoles + * @tc.desc : host模式下,portId参数错误 + */ + it('SUB_USB_set_port_roles_test_08', 0, function () { + var portId = -1; + var powerRole = 1; + var dataRole = 1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, 1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_08: PASS'); + }) + + console.info('set_port_roles_test_08: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_09 + * @tc.name : setPortRoles + * @tc.desc : host模式下,powerRole参数错误 + */ + it('SUB_USB_set_port_roles_test_09', 0, function () { + var portId = 1; + var powerRole = -1; + var dataRole = 1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(1, -1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_09: PASS'); + }) + + console.info('set_port_roles_test_09: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_10 + * @tc.name : setPortRoles + * @tc.desc : host模式下,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_10', 0, function () { + var portId = 1; + var powerRole = 1; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(1, -1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_10: PASS'); + }) + + console.info('set_port_roles_test_10: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_11 + * @tc.name : setPortRoles + * @tc.desc : host模式下,portId,powerRole参数错误 + */ + it('SUB_USB_set_port_roles_test_11', 0, function () { + var portId = -1; + var powerRole = -1; + var dataRole = 1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, -1, 1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_11: PASS'); + }) + + console.info('set_port_roles_test_11: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_12 + * @tc.name : setPortRoles + * @tc.desc : host模式下,portId,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_12', 0, function () { + var portId = -1; + var powerRole = 1; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, 1, -1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_12: PASS'); + }) + + console.info('set_port_roles_test_12: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_13 + * @tc.name : setPortRoles + * @tc.desc : host模式下,powerRole,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_13', 0, function () { + var portId = 1; + var powerRole = -1; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(1, -1, -1).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_13: PASS'); + }) + + console.info('set_port_roles_test_13: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_14 + * @tc.name : setPortRoles + * @tc.desc : host模式下,portId,powerRole,dataRole参数错误 + */ + it('SUB_USB_set_port_roles_test_14', 0, function () { + var portId = -1; + var powerRole = -1; + var dataRole = -1; + CheckEmptyUtils.sleep(2000) + + usb.setPortRoles(-1, -1, -1).then(data => { + console.info('usb case setPortRoles 14 return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles 14 error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_14: PASS'); + }) + + console.info('set_port_roles_test_14: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_15 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,portId参数错误 + */ + it('SUB_USB_set_port_roles_test_15', 0, function () { + var portId = -1; + var powerRole = 2; + var dataRole = 2; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(-1, 2, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_15: PASS'); + }) + + console.info('set_port_roles_test_15: PASS'); + expect(true).assertTrue(); + }) + + /** + * @tc.number : set_port_roles_test_16 + * @tc.name : setPortRoles + * @tc.desc : Device模式下,powerRole参数错误 + */ + it('SUB_USB_set_port_roles_test_16', 0, function () { + var portId = 2; + var powerRole = -1; + var dataRole = 2; + CheckEmptyUtils.sleep(2000) + usb.setPortRoles(2, -1, 2).then(data => { + console.info('usb case setPortRoles return: ' + data); + expect(data).assertTrue(); + }).catch(error => { + console.info('usb case setPortRoles error : ' + error); + expect(error).assertFalse(); + console.info('set_port_roles_test_16: PASS'); + }) + + console.info('set_port_roles_test_16: PASS'); + expect(true).assertTrue(); + }) +}) diff --git a/usb/usb_standard/src/main/resources/base/element/string.json b/usb/usb_standard/src/main/resources/base/element/string.json new file mode 100755 index 0000000000000000000000000000000000000000..9cc0314b698d82fe05d06e24bf9f020bbaba05bb --- /dev/null +++ b/usb/usb_standard/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "UsbJSApiTest" + }, + { + "name": "mainability_description", + "value": "JS_Phone_Empty Feature Ability" + } + ] +} \ No newline at end of file diff --git a/usb/usb_standard/src/main/resources/base/media/icon.png b/usb/usb_standard/src/main/resources/base/media/icon.png new file mode 100755 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/usb/usb_standard/src/main/resources/base/media/icon.png differ