• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import usb from '@ohos.usb';
17import CheckEmptyUtils from './CheckEmptyUtils.js';
18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
19
20/* usb core functions test */
21export default function UsbPortAndFunctionsJsFunctionsTest() {
22describe('UsbPortAndFunctionsJsFunctionsTest', function () {
23
24  beforeAll(function () {
25    console.log('*************Usb Unit UsbPortAndFunctionsJsFunctionsTest Begin*************');
26    var Version = usb.getVersion()
27    console.info('begin test getversion :' + Version)
28    // version > 17  host currentMode = 2 device currentMode = 1
29    var usbPortList = usb.getPorts()
30    if (usbPortList.length > 0) {
31      if (usbPortList[0].status.currentMode == 2) {
32        usb.setPortRoles(usbPortList[0].id, usb.SINK, usb.DEVICE).then(data => {
33          console.info('usb case setPortRoles return: ' + data);
34        }).catch(error => {
35          console.info('usb case setPortRoles error : ' + error);
36        });
37        CheckEmptyUtils.sleep(8000)
38        console.log('*************Usb Unit switch to device Begin*************');
39      }
40    }
41  })
42  beforeEach(function () {
43    console.info('beforeEach: *************Usb Unit  Test Case*************');
44  })
45  afterEach(function () {
46    console.info('afterEach: *************Usb Unit  Test Case*************');
47  })
48  afterAll(function () {
49    console.log('*************Usb Unit UsbPortAndFunctionsJsFunctionsTest End*************');
50  })
51
52  function callSetCurFunction(caseName, iValue) {
53    console.info('usb case param case name:' + caseName);
54    console.info('usb case param iValue:' + iValue);
55    usb.setCurrentFunctions(iValue).then(data => {
56      console.info('usb case SetCurFunction ret:' + data);
57      expect(data).assertTrue();
58      console.info('usb case ' + caseName + ': PASS');
59    }).catch(error => {
60      console.info('usb case ' + caseName + ' error : ' + error);
61      expect(false).assertTrue();
62    });
63    CheckEmptyUtils.sleep(6000)
64  }
65
66  /**
67   * @tc.number: SUB_USB_JS_0350
68   * @tc.name: functions_test
69   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 1, Set as ACM function
70   */
71  it('SUB_USB_JS_0350', 0, function () {
72    CheckEmptyUtils.sleep(3000)
73    console.info('usb SUB_USB_JS_0350 set ACM begin');
74    var maskCode = usb.getCurrentFunctions();
75    console.info('usb case getCurrentFunctions return: ' + maskCode);
76    var funcString = usb.usbFunctionsToString(maskCode);
77    console.info('usb case funcString:' + funcString);
78    callSetCurFunction('SUB_USB_JS_0350 ACM 1', usb.ACM)
79  })
80
81  /**
82   * @tc.number: SUB_USB_JS_0360
83   * @tc.name: functions_test
84   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 2, Set as ECM function
85   */
86  it('SUB_USB_JS_0360', 0, function () {
87    CheckEmptyUtils.sleep(3000)
88    console.info('usb SUB_USB_JS_0360 set ECM begin');
89    var maskCode = usb.getCurrentFunctions();
90    console.info('usb case getCurrentFunctions return: ' + maskCode);
91    var funcString = usb.usbFunctionsToString(maskCode);
92    console.info('usb case funcString:' + funcString);
93    callSetCurFunction('SUB_USB_JS_0360 ECM 2', usb.ECM)
94  })
95
96  /**
97   * @tc.number: SUB_USB_JS_0370
98   * @tc.name: functions_test
99   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 3, Set as ACM、ECM function
100   */
101  it('SUB_USB_JS_0370', 0, function () {
102    CheckEmptyUtils.sleep(3000)
103    console.info('usb SUB_USB_JS_0370 set ACM ECM begin');
104    var maskCode = usb.getCurrentFunctions();
105    console.info('usb case getCurrentFunctions return: ' + maskCode);
106    var funcString = usb.usbFunctionsToString(maskCode);
107    console.info('usb case funcString:' + funcString);
108    callSetCurFunction('SUB_USB_JS_0370 ACM ECM 3', (usb.ACM | usb.ECM))
109  })
110
111  /**
112   * @tc.number: SUB_USB_JS_0380
113   * @tc.name: functions_test
114   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 4, Set as HDC function
115   */
116  it('SUB_USB_JS_0380', 0, function () {
117    CheckEmptyUtils.sleep(3000)
118    console.info('usb SUB_USB_JS_0380 set HDC begin');
119    var maskCode = usb.getCurrentFunctions();
120    console.info('usb case getCurrentFunctions return: ' + maskCode);
121    var funcString = usb.usbFunctionsToString(maskCode);
122    console.info('usb case funcString:' + funcString);
123    callSetCurFunction('SUB_USB_JS_0380 HDC 4', usb.HDC)
124  })
125
126  /**
127   * @tc.number: SUB_USB_JS_0390
128   * @tc.name: functions_test
129   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 5, Set as ACM、HDC function
130   */
131  it('SUB_USB_JS_0390', 0, function () {
132    CheckEmptyUtils.sleep(3000)
133    console.info('usb SUB_USB_JS_0390 set ACM HDC begin');
134    var maskCode = usb.getCurrentFunctions();
135    console.info('usb case getCurrentFunctions return: ' + maskCode);
136    var funcString = usb.usbFunctionsToString(maskCode);
137    console.info('usb case funcString:' + funcString);
138    callSetCurFunction('SUB_USB_JS_0390 ACM HDC 5', (usb.HDC | usb.ACM))
139  })
140
141  /**
142   * @tc.number: SUB_USB_JS_0400
143   * @tc.name: functions_test
144   * @tc.desc: Positive test: Set the current USB function list in device mode, Set 6, Set as ECM、HDC function
145   */
146  it('SUB_USB_JS_0400', 0, function () {
147    CheckEmptyUtils.sleep(3000)
148    console.info('usb SUB_USB_JS_0400 set ECM HDC begin');
149    var maskCode = usb.getCurrentFunctions();
150    console.info('usb case getCurrentFunctions return: ' + maskCode);
151    var funcString = usb.usbFunctionsToString(maskCode);
152    console.info('usb case funcString:' + funcString);
153    callSetCurFunction('SUB_USB_JS_0400 ECM HDC 6', (usb.HDC | usb.ECM))
154  })
155
156    /**
157   * @tc.number: SUB_USB_JS_0010
158   * @tc.name: setPortRoles
159   * @tc.desc: Positive test: Switch to Device, set powerRole 2 DataRole 2
160   */
161  it('SUB_USB_JS_0010', 0, function () {
162    console.info('usb SUB_USB_JS_0010 device 2 2 begin');
163    var usbPortList = usb.getPorts()
164    if (usbPortList.length == 0) {
165      console.info('usb SUB_USB_JS_0010 device 2 2 usbPortList is null');
166      expect(false).assertTrue();
167      return
168    }
169
170    for (var i = 0; i < usbPortList.length; i++) {
171      console.info('usb 0010 case set data role 2, data role 2');
172      usb.setPortRoles(usbPortList[i].id, usb.SINK, usb.DEVICE).then(data => {
173        console.info('usb 0010 case setPortRoles return: ' + data);
174        expect(data).assertTrue();
175      }).catch(error => {
176        console.info('usb 0010 case setPortRoles error : ' + error);
177        expect(false).assertTrue();
178      });
179      CheckEmptyUtils.sleep(8000)
180    }
181
182    console.info('usb SUB_USB_JS_0010 device 2 2:  PASS');
183  })
184
185  /**
186   * @tc.number: SUB_USB_JS_0020
187   * @tc.name: setPortRoles
188   * @tc.desc: Positive test: Switch to host, set powerRole 1 DataRole 1
189   */
190  it('SUB_USB_JS_0020', 0, function () {
191    console.info('usb SUB_USB_JS_0020 host 1 1 begin');
192    var usbPortList = usb.getPorts()
193    if (usbPortList.length == 0) {
194      console.info('usb SUB_USB_JS_0020 host 1 1 usbPortList is null');
195      expect(false).assertTrue();
196      return
197    }
198
199    for (var i = 0; i < usbPortList.length; i++) {
200      console.info('usb 0020 case set data role 1, data role 1');
201      usb.setPortRoles(usbPortList[i].id, usb.SOURCE, usb.HOST).then(data => {
202        console.info('usb 0020 case setPortRoles return: ' + data);
203        expect(data).assertTrue();
204      }).catch(error => {
205        console.info('usb 0020 case setPortRoles error : ' + error);
206        expect(false).assertTrue();
207      });
208      CheckEmptyUtils.sleep(8000)
209    }
210
211    console.info('usb SUB_USB_JS_0020 host 1 1:  PASS');
212  })
213})
214}
215