1/* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import usb from '@ohos.usbManager' 17import deviceManager from '@ohos.driver.deviceManager' 18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index' 19 20describe("SystemApiJsTest", function () { 21 const TAG = "[SystemApiJsTest]"; 22 const SYSTEMAPI_DENIED_CODE = 202; 23 const TEST_DEVICE_ID = 0; 24 const TEST_DRIVER_UID = 'testDriverUid' 25 const USB_CLASS_HUB = 9; 26 const TEST_FUNCTION = () => { 27 console.info("Test function is called"); 28 }; 29 30 let deviceNum = 0; 31 const isDeviceConnected = done => { 32 if (deviceNum > 0) { 33 console.info("Test USB device is connected"); 34 return true; 35 } 36 console.info("Test USB device is not connected"); 37 expect(true).assertTrue(); 38 if (typeof(done) === 'function') { 39 done(); 40 } 41 return false; 42 } 43 44 beforeAll(function () { 45 console.info('beforeAll called'); 46 try { 47 let devicesList = usb.getDevices(); 48 devicesList = devicesList.filter(device => device.clazz != USB_CLASS_HUB); 49 if (Array.isArray(devicesList)) { 50 deviceNum = devicesList.length; 51 } 52 } catch (err) { 53 console.error(TAG, `getDevices failed, message is ${err.message}`); 54 } 55 }) 56 57 afterAll(function () { 58 console.info('AfterAll called'); 59 }) 60 61 /* 62 * @tc.name:SystemApi_queryDeviceInfo_001 63 * @tc.desc:verify SystemApi of queryDeviceInfo 64 * @tc.type: FUNC 65 */ 66 it("SystemApi_queryDeviceInfo_001", 0, done => { 67 console.info('----------------------SystemApi_queryDeviceInfo_001---------------------------'); 68 if (!isDeviceConnected(done)) { 69 return; 70 } 71 try { 72 deviceManager.queryDeviceInfo(TEST_DEVICE_ID); 73 expect(false).assertTrue(); 74 done(); 75 } catch (err) { 76 expect(err.code).assertEqual(SYSTEMAPI_DENIED_CODE); 77 done(); 78 } 79 }); 80 81 /* 82 * @tc.name:SystemApi_queryDriverInfo_001 83 * @tc.desc:verify SystemApi of queryDriverInfo 84 * @tc.type: FUNC 85 */ 86 it("SystemApi_queryDriverInfo_001", 0, done => { 87 console.info('----------------------SystemApi_queryDriverInfo_001---------------------------'); 88 if (!isDeviceConnected(done)) { 89 return; 90 } 91 try { 92 deviceManager.queryDriverInfo(TEST_DRIVER_UID); 93 expect(false).assertTrue(); 94 done(); 95 } catch (err) { 96 expect(err.code).assertEqual(SYSTEMAPI_DENIED_CODE); 97 done(); 98 } 99 }); 100});