142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
342103316Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442103316Sopenharmony_ci * you may not use this file except in compliance with the License.
542103316Sopenharmony_ci * You may obtain a copy of the License at
642103316Sopenharmony_ci *
742103316Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842103316Sopenharmony_ci *
942103316Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042103316Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142103316Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242103316Sopenharmony_ci * See the License for the specific language governing permissions and
1342103316Sopenharmony_ci * limitations under the License.
1442103316Sopenharmony_ci */
1542103316Sopenharmony_ci
1642103316Sopenharmony_ciimport usb from '@ohos.usb';
1742103316Sopenharmony_ciimport CheckEmptyUtils from './CheckEmptyUtils.js';
1842103316Sopenharmony_ciimport { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'
1942103316Sopenharmony_ci
2042103316Sopenharmony_ci/* usb core functions test */
2142103316Sopenharmony_cidescribe('UsbFunctionsJsFunctionsTest', function () {
2242103316Sopenharmony_ci
2342103316Sopenharmony_ci  beforeAll(function () {
2442103316Sopenharmony_ci    console.log('*************Usb Unit UsbFunctionsJsFunctionsTest Begin*************');
2542103316Sopenharmony_ci    var Version = usb.getVersion()
2642103316Sopenharmony_ci    console.info('begin test getversion :' + Version)
2742103316Sopenharmony_ci    // version > 17  host currentMode = 2 device currentMode = 1
2842103316Sopenharmony_ci    var usbPortList = usb.getPorts()
2942103316Sopenharmony_ci    if (usbPortList.length > 0) {
3042103316Sopenharmony_ci      if (usbPortList[0].status.currentMode == 2) {
3142103316Sopenharmony_ci        usb.setPortRoles(usbPortList[0].id, usb.SINK, usb.DEVICE).then(data => {
3242103316Sopenharmony_ci          console.info('usb case setPortRoles return: ' + data);
3342103316Sopenharmony_ci        }).catch(error => {
3442103316Sopenharmony_ci          console.info('usb case setPortRoles error : ' + error);
3542103316Sopenharmony_ci        });
3642103316Sopenharmony_ci        console.log('*************Usb Unit switch to device Begin*************');
3742103316Sopenharmony_ci      }
3842103316Sopenharmony_ci    }
3942103316Sopenharmony_ci  })
4042103316Sopenharmony_ci  beforeEach(function () {
4142103316Sopenharmony_ci    console.info('beforeEach: *************Usb Unit  Test Case*************');
4242103316Sopenharmony_ci  })
4342103316Sopenharmony_ci  afterEach(function () {
4442103316Sopenharmony_ci    console.info('afterEach: *************Usb Unit  Test Case*************');
4542103316Sopenharmony_ci  })
4642103316Sopenharmony_ci  afterAll(function () {
4742103316Sopenharmony_ci    console.log('*************Usb Unit UsbFunctionsJsFunctionsTest End*************');
4842103316Sopenharmony_ci  })
4942103316Sopenharmony_ci
5042103316Sopenharmony_ci  /**
5142103316Sopenharmony_ci   * @tc.number    : SUB_USB_get_current_functions_test_01
5242103316Sopenharmony_ci   * @tc.name      : getCurrentFunctions
5342103316Sopenharmony_ci   * @tc.desc      : 获取当前设备模式 掩码与描述字符转换
5442103316Sopenharmony_ci   */
5542103316Sopenharmony_ci  it('SUB_USB_get_current_functions_test_01', 0, function () {
5642103316Sopenharmony_ci    console.info('usb get_current_functions_test_01 begin');
5742103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
5842103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
5942103316Sopenharmony_ci
6042103316Sopenharmony_ci    var strMaskCode = usb.usbFunctionsToString(maskCode)
6142103316Sopenharmony_ci    console.info('usb case usbFunctionsToString return str: ' + strMaskCode);
6242103316Sopenharmony_ci    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
6342103316Sopenharmony_ci    console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode);
6442103316Sopenharmony_ci    expect(nMaskCode).assertEqual(maskCode);
6542103316Sopenharmony_ci    console.info('usb get_current_functions_test_01 :  PASS');
6642103316Sopenharmony_ci    expect(true).assertTrue();
6742103316Sopenharmony_ci  })
6842103316Sopenharmony_ci
6942103316Sopenharmony_ci  /**
7042103316Sopenharmony_ci   * @tc.number    : SUB_USB_get_current_functions_test_02
7142103316Sopenharmony_ci   * @tc.name      : usbFunctionString
7242103316Sopenharmony_ci   * @tc.desc      : 反向测试 获取当前设备模式 掩码与描述字符转换
7342103316Sopenharmony_ci   */
7442103316Sopenharmony_ci  it('SUB_USB_get_current_functions_test_02', 0, function () {
7542103316Sopenharmony_ci    console.info('usb get_current_functions_test_02 begin');
7642103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
7742103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
7842103316Sopenharmony_ci
7942103316Sopenharmony_ci    var strMaskCode = usb.usbFunctionsToString(maskCode)
8042103316Sopenharmony_ci    console.info('usb case usbFunctionsToString return str: ' + strMaskCode);
8142103316Sopenharmony_ci    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
8242103316Sopenharmony_ci    console.info('usb case strMaskCode usbFunctionsFromString return int: ' + nMaskCode);
8342103316Sopenharmony_ci
8442103316Sopenharmony_ci    var errmaskCode = 0
8542103316Sopenharmony_ci    var strMaskCode = usb.usbFunctionsToString(errmaskCode)
8642103316Sopenharmony_ci
8742103316Sopenharmony_ci    console.info('usb case ' + errmaskCode + ' usbFunctionsToString return str: ' + strMaskCode);
8842103316Sopenharmony_ci
8942103316Sopenharmony_ci    var errStrMaskCode = 'none'
9042103316Sopenharmony_ci    var nMaskCode = usb.usbFunctionsFromString(errStrMaskCode)
9142103316Sopenharmony_ci    expect(nMaskCode).assertEqual(usb.NONE);
9242103316Sopenharmony_ci    console.info('usb case errStrMaskCode ' + errStrMaskCode +
9342103316Sopenharmony_ci      ' usbFunctionsFromString return int: ' + nMaskCode);
9442103316Sopenharmony_ci
9542103316Sopenharmony_ci    console.info('usb get_current_functions_test_02 :  PASS');
9642103316Sopenharmony_ci  })
9742103316Sopenharmony_ci
9842103316Sopenharmony_ci  function callSetCurFunction(caseName, iValue) {
9942103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
10042103316Sopenharmony_ci    console.info('usb case param case name:' + caseName);
10142103316Sopenharmony_ci    console.info('usb case param iValue:' + iValue);
10242103316Sopenharmony_ci    usb.setCurrentFunctions(iValue).then(data => {
10342103316Sopenharmony_ci      console.info('usb case SetCurFunction ret:' + data);
10442103316Sopenharmony_ci      expect(data).assertTrue();
10542103316Sopenharmony_ci      console.info('usb case ' + caseName + ': PASS');
10642103316Sopenharmony_ci    }).catch(error => {
10742103316Sopenharmony_ci      console.info('usb case ' + caseName + ' error : ' + error);
10842103316Sopenharmony_ci      expect(false).assertTrue();
10942103316Sopenharmony_ci    });
11042103316Sopenharmony_ci  }
11142103316Sopenharmony_ci
11242103316Sopenharmony_ci  /**
11342103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_02
11442103316Sopenharmony_ci   * @tc.name      : functions_test
11542103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 1 设置为ACM功能
11642103316Sopenharmony_ci   */
11742103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_02', 0, function () {
11842103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
11942103316Sopenharmony_ci    console.info('usb set_current_functions_test_02 set ACM begin');
12042103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
12142103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
12242103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
12342103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
12442103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_02 ACM 1', usb.ACM)
12542103316Sopenharmony_ci  })
12642103316Sopenharmony_ci
12742103316Sopenharmony_ci  /**
12842103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_03
12942103316Sopenharmony_ci   * @tc.name      : functions_test
13042103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 2 设置为ECM功能
13142103316Sopenharmony_ci   */
13242103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_03', 0, function () {
13342103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
13442103316Sopenharmony_ci    console.info('usb set_current_functions_test_03 set ECM begin');
13542103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
13642103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
13742103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
13842103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
13942103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_03 ECM 2', usb.ECM)
14042103316Sopenharmony_ci  })
14142103316Sopenharmony_ci
14242103316Sopenharmony_ci  /**
14342103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_04
14442103316Sopenharmony_ci   * @tc.name      : functions_test
14542103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 3 设置为ACM、ECM功能
14642103316Sopenharmony_ci   */
14742103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_04', 0, function () {
14842103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
14942103316Sopenharmony_ci    console.info('usb set_current_functions_test_04 set ACM ECM begin');
15042103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
15142103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
15242103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
15342103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
15442103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_04 ACM ECM 3', (usb.ACM | usb.ECM))
15542103316Sopenharmony_ci  })
15642103316Sopenharmony_ci
15742103316Sopenharmony_ci  /**
15842103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_05
15942103316Sopenharmony_ci   * @tc.name      : functions_test
16042103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 4 设置为HDC功能
16142103316Sopenharmony_ci   */
16242103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_05', 0, function () {
16342103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
16442103316Sopenharmony_ci    console.info('usb set_current_functions_test_05 set HDC begin');
16542103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
16642103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
16742103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
16842103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
16942103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_05 HDC 4', usb.HDC)
17042103316Sopenharmony_ci  })
17142103316Sopenharmony_ci
17242103316Sopenharmony_ci  /**
17342103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_06
17442103316Sopenharmony_ci   * @tc.name      : functions_test
17542103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 5 设置为ACM、HDC功能
17642103316Sopenharmony_ci   */
17742103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_06', 0, function () {
17842103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
17942103316Sopenharmony_ci    console.info('usb set_current_functions_test_06 set ACM HDC begin');
18042103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
18142103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
18242103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
18342103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
18442103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_06 ACM HDC 5', (usb.HDC | usb.ACM))
18542103316Sopenharmony_ci  })
18642103316Sopenharmony_ci
18742103316Sopenharmony_ci  /**
18842103316Sopenharmony_ci   * @tc.number    : SUB_USB_set_current_functions_test_07
18942103316Sopenharmony_ci   * @tc.name      : functions_test
19042103316Sopenharmony_ci   * @tc.desc      : 在设备模式下设置当前的USB功能列表 Set 6 设置为ECM、HDC功能
19142103316Sopenharmony_ci   */
19242103316Sopenharmony_ci  it('SUB_USB_set_current_functions_test_07', 0, function () {
19342103316Sopenharmony_ci    CheckEmptyUtils.sleep(3000)
19442103316Sopenharmony_ci    console.info('usb set_current_functions_test_07 set ECM HDC begin');
19542103316Sopenharmony_ci    var maskCode = usb.getCurrentFunctions();
19642103316Sopenharmony_ci    console.info('usb case getCurrentFunctions return: ' + maskCode);
19742103316Sopenharmony_ci    var funcString = usb.usbFunctionsToString(maskCode);
19842103316Sopenharmony_ci    console.info('usb case funcString:' + funcString);
19942103316Sopenharmony_ci    callSetCurFunction('set_current_functions_test_07 ECM HDC 6', (usb.HDC | usb.ECM))
20042103316Sopenharmony_ci  })
20142103316Sopenharmony_ci
20242103316Sopenharmony_ci})
203