diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn index a22333895ca39cd07c2aa8f692e4d95f41ca7fae..b939cdf923c9f4ccc4dfea22abde9fb65f6250a0 100644 --- a/test/native/unittest/BUILD.gn +++ b/test/native/unittest/BUILD.gn @@ -12,9 +12,10 @@ # limitations under the License. import("//build/test.gni") -import("./../../../usbmgr.gni") group("usb_manager_unittest") { testonly = true - deps = [] + deps = [ + "js/usbsystemapi_js_test:UsbSystemApiJsTest", + ] } diff --git a/test/native/unittest/js/usb_system_api_js_test/BUILD.gn b/test/native/unittest/js/usb_system_api_js_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..6c4ec58244ec135081d362961615e4d129e3a066 --- /dev/null +++ b/test/native/unittest/js/usb_system_api_js_test/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2025 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/test.gni") + +module_output_path = "usb/usb_manager" +ohos_js_unittest("UsbSystemApiJsTest") { + module_out_path = module_output_path + hap_profile = "./config.json" + certificate_profile = "./openharmony_sx.p7b" +} diff --git a/test/native/unittest/js/usb_system_api_js_test/UsbSystemApiJsTest.js b/test/native/unittest/js/usb_system_api_js_test/UsbSystemApiJsTest.js new file mode 100644 index 0000000000000000000000000000000000000000..3164b8970a2849097c3002c5e22fb05bb4cb9fab --- /dev/null +++ b/test/native/unittest/js/usb_system_api_js_test/UsbSystemApiJsTest.js @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2025 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 usbManager from '@ohos.usbManager' +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' + +describe("UsbSystemApiJsTest", function () { + + beforeAll(function () { + console.info('***************USB Unit Test beforeAll called***************'); + try { + console.info('usbManager.getVersion() = ' + usbManager.getVersion()); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } + }) + + afterAll(function () { + console.info('AfterAll called'); + }) + + /* + * @tc.name:UsbSystemApi_test_test + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_test", 0, done => { + console.info('----------------------UsbSystemApi_test_test---------------------------'); + console.info('----------------------UsbSystemApi_test_start---------------------------'); + console.info('----------------------UsbSystemApi_test_finish---------------------------'); + expect(true).assertTrue(); + done(); + }); + + /* + * @tc.name:UsbSystemApi_test_addDeviceAccessRight + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_addDeviceAccessRight", 0, done => { + console.info('UsbSystemApi_test_addDeviceAccessRight start'); + try { + let bundleName = "ohos.usb.testBundleName"; + let deviceName = "ohos.usb.testDeviceName"; + var ret = usbManager.addDeviceAccessRight(bundleName, deviceName).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.addDeviceAccessRight ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_addDeviceAccessRight finish'); + }); + + /* + * @tc.name:UsbSystemApi_test_addDeviceAccessRight + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_addDeviceAccessRight", 0, done => { + console.info('UsbSystemApi_test_addDeviceAccessRight start'); + try { + let bundleName = "ohos.usb.testBundleName"; + let deviceName = "ohos.usb.testDeviceName"; + var ret = usbManager.addDeviceAccessRight(bundleName, deviceName).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.addDeviceAccessRight ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_addDeviceAccessRight finish'); + }); + + /* + * @tc.name:UsbSystemApi_test_usbFunctionsFromString + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_usbFunctionsFromString", 0, done => { + console.info('UsbSystemApi_test_usbFunctionsFromString start'); + try { + let funcs = "Permission denied. Call requestRight or requestAccessoryRight to get the " + + "permission or USBDevicePipe access right first."; + var ret = usbManager.usbFunctionsFromString(funcs).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.usbFunctionsFromString ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_usbFunctionsFromString finish'); + }); + + /* + * @tc.name:UsbSystemApi_test_getFunctionsFromString + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_getFunctionsFromString", 0, done => { + console.info('UsbSystemApi_test_getFunctionsFromString start'); + try { + let funcs = "Permission denied. Call requestRight or requestAccessoryRight to get the " + + "permission or USBDevicePipe access right first."; + var ret = usbManager.getFunctionsFromString(funcs).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.getFunctionsFromString ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_getFunctionsFromString finish'); + }); + + /* + * @tc.name:UsbSystemApi_test_usbFunctionsToString + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_usbFunctionsToString", 0, done => { + console.info('UsbSystemApi_test_usbFunctionsToString start'); + try { + let type = usbManager.FunctionType.HDC; + var ret = usbManager.usbFunctionsToString(type).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.usbFunctionsToString ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_usbFunctionsToString finish'); + }); + + /* + * @tc.name:UsbSystemApi_test_getStringFromFunctions + * @tc.desc:verify SystemApi of queryDeviceInfo + * @tc.type: FUNC + */ + it("UsbSystemApi_test_getStringFromFunctions", 0, done => { + console.info('UsbSystemApi_test_getStringFromFunctions start'); + try { + let type = usbManager.FunctionType.HDC; + var ret = usbManager.getStringFromFunctions(type).then(data => { + console.info('data = ' + JSON.stringify(data)); + }); + console.info('usbManager.getStringFromFunctions ret = ' + ret); + expect(ret).assertTrue(); + } catch (error) { + console.info('usbManager.getVersion() error.code = ' + error.code); + expect(error.code).assertEqual(SYSTEMAPI_DENIED_CODE); + } finally { + done(); + } + console.info('UsbSystemApi_test_getStringFromFunctions finish'); + }); +}); \ No newline at end of file diff --git a/test/native/unittest/js/usb_system_api_js_test/config.json b/test/native/unittest/js/usb_system_api_js_test/config.json new file mode 100644 index 0000000000000000000000000000000000000000..a5de79c27cab3160e88fefbb129f60c65cb06ab4 --- /dev/null +++ b/test/native/unittest/js/usb_system_api_js_test/config.json @@ -0,0 +1,69 @@ +{ + "app": { + "bundleName": "com.usb_manager.systemapitest", + "vendor": "example", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 12, + "target": 12 + } + }, + "deviceConfig": {}, + "module": { + "reqPermissions": [], + "package": "com.usb_manager.systemapitest", + "name": ".MyApplication", + "deviceType": [ + "default", + "tablet", + "2in1" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "abilities": [ + { + "visible": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "com.usb_manager.systemapitest.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "PermissionJsTest", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ], + "requestPermissions": [ + { + "name": "ohos.permission.MANAGE_USB_CONFIG" + } + ] + } + } + \ No newline at end of file diff --git a/test/native/unittest/js/usb_system_api_js_test/openharmony_sx.p7b b/test/native/unittest/js/usb_system_api_js_test/openharmony_sx.p7b new file mode 100644 index 0000000000000000000000000000000000000000..e97c990bab081afb63e67e0125aede7f68545b6a Binary files /dev/null and b/test/native/unittest/js/usb_system_api_js_test/openharmony_sx.p7b differ