UsbFunctionsJsunitEx.test.js 12.4 KB
Newer Older
J
jiyong_sd 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2021-2022 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 '@ohos/hypium'

/* usb core functions test */
export default function UsbFunctionsJsFunctionsTestEx() {
describe('UsbFunctionsJsFunctionsTestEx', function () {

23 24
  var invalidCode = 0;

J
jiyong_sd 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
  beforeAll(function () {
    console.log('*************Usb Unit UsbFunctionsJsFunctionsTestEx Begin*************');
    var Version = usb.getVersion()
    console.info('begin test getversion :' + Version)
    //  17 version host currentMode = 2 device currentMode = 1
    var usbPortList = usb.getPorts()
    if (usbPortList.length > 0) {
      if (usbPortList[0].status.currentMode == 2) {
        usb.setPortRoles(usbPortList[0].id, usb.SINK, usb.DEVICE).then(data => {
          console.info('usb case setPortRoles return: ' + data);
        }).catch(error => {
          console.info('usb case setPortRoles error : ' + error);
        });
38
        CheckEmptyUtils.sleep(8000)
J
jiyong_sd 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        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*************');
  })

  /**
56 57 58
   * @tc.number: SUB_USB_JS_0930
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 0, return 'none'
J
jiyong_sd 已提交
59 60 61 62 63 64 65
   */
  it('SUB_USB_JS_0930', 0, function () {
    console.info('usb SUB_USB_JS_0930 begin');
    var maskCode = usb.NONE
    console.info('usb case maskCode : ' + usb.NONE);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
66
    expect(strMaskCode).assertEqual('none');
J
jiyong_sd 已提交
67 68 69 70
    console.info('usb SUB_USB_JS_0930 :  PASS');
  })

  /**
71 72 73
   * @tc.number: SUB_USB_JS_0940
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 1, return 'acm'
J
jiyong_sd 已提交
74 75 76 77 78 79 80
   */
  it('SUB_USB_JS_0940', 0, function () {
    console.info('usb SUB_USB_JS_0940 begin');
    var maskCode = usb.ACM
    console.info('usb case maskCode : ' + usb.ACM);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
81
    expect(strMaskCode).assertEqual('acm');
J
jiyong_sd 已提交
82 83 84 85
    console.info('usb SUB_USB_JS_0940 :  PASS');
  })

  /**
86 87 88
   * @tc.number: SUB_USB_JS_0950
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 2, return 'ecm'
J
jiyong_sd 已提交
89 90 91 92 93 94 95
   */
  it('SUB_USB_JS_0950', 0, function () {
    console.info('usb SUB_USB_JS_0950 begin');
    var maskCode = usb.ECM
    console.info('usb case maskCode : ' + maskCode);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
96
    expect(strMaskCode).assertEqual('ecm');
J
jiyong_sd 已提交
97 98 99 100
    console.info('usb SUB_USB_JS_0950 :  PASS');
  })

  /**
101 102 103
   * @tc.number: SUB_USB_JS_0960
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 3, return 'acm,ecm'
J
jiyong_sd 已提交
104 105 106 107 108 109 110
   */
  it('SUB_USB_JS_0960', 0, function () {
    console.info('usb SUB_USB_JS_0960 begin');
    var maskCode = usb.ACM | usb.ECM
    console.info('usb case maskCode : ' + maskCode);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
111
    expect(strMaskCode).assertEqual('acm,ecm');
J
jiyong_sd 已提交
112 113 114 115
    console.info('usb SUB_USB_JS_0960 :  PASS');
  })

  /**
116 117 118
   * @tc.number: SUB_USB_JS_0970
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 4, return 'hdc'
J
jiyong_sd 已提交
119 120 121 122 123 124 125
   */
  it('SUB_USB_JS_0970', 0, function () {
    console.info('usb SUB_USB_JS_0970 begin');
    var maskCode = usb.HDC
    console.info('usb case maskCode : ' + maskCode);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
126
    expect(strMaskCode).assertEqual('hdc');
J
jiyong_sd 已提交
127 128 129 130
    console.info('usb SUB_USB_JS_0970 :  PASS');
  })

  /**
131 132 133
   * @tc.number: SUB_USB_JS_0980
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 5, return 'acm,hdc'
J
jiyong_sd 已提交
134 135 136 137 138 139 140
   */
  it('SUB_USB_JS_0980', 0, function () {
    console.info('usb SUB_USB_JS_0980 begin');
    var maskCode = usb.ACM | usb.HDC
    console.info('usb case maskCode : ' + maskCode);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
141
    expect(strMaskCode).assertEqual('acm,hdc');
J
jiyong_sd 已提交
142 143 144 145
    console.info('usb SUB_USB_JS_0980 :  PASS');
  })

  /**
146 147 148
   * @tc.number: SUB_USB_JS_0990
   * @tc.name: usbFunctionsToString
   * @tc.desc: Positive test: input 6, return 'ecm,hdc'
J
jiyong_sd 已提交
149 150 151 152 153 154 155
   */
  it('SUB_USB_JS_0990', 0, function () {
    console.info('usb SUB_USB_JS_0990 begin');
    var maskCode = usb.ECM | usb.HDC
    console.info('usb case maskCode : ' + maskCode);
    var strMaskCode = usb.usbFunctionsToString(maskCode)
    console.info('usb case maskCode ' + maskCode + ' usbFunctionsToString return int: ' + strMaskCode);
156
    expect(strMaskCode).assertEqual('ecm,hdc');
J
jiyong_sd 已提交
157 158 159
    console.info('usb SUB_USB_JS_0990 :  PASS');
  })

160
  /**
161 162 163
   * @tc.number: SUB_USB_JS_1160
   * @tc.name: usbFunctionsToString
   * @tc.desc: Negative test: Mask converted to description character, parameter type error
164
   */
165
  it('SUB_USB_JS_1160', 0, function () {
166 167 168
    console.info('usb SUB_USB_JS_1160 begin');
    try {
      var maskCode = usb.usbFunctionsToString("invalid");
169
      console.info('usb 1160 case usbFunctionsToString return: ' + maskCode);
170 171
      expect(false).assertTrue();
    } catch (err) {
172
      console.info('usb 1160 catch err code: ' + err.code + ' message: ' + err.message);
173 174 175 176 177
      expect(err.code).assertEqual(401);
      console.info('usb SUB_USB_JS_1160 :  PASS');
    }
  })

178
  /**
179 180 181 182
   * @tc.number: SUB_USB_JS_1360
   * @tc.name: usbFunctionsToString
   * @tc.desc: Negative test: Mask converted to description character,
   * parameter number exception, necessary parameters not input
183
   */
184
  it('SUB_USB_JS_1360', 0, function () {
185 186 187 188 189 190 191 192 193 194 195 196
    console.info('usb SUB_USB_JS_1360 begin');
    try {
      var maskCode = usb.usbFunctionsToString();
      console.info('usb 1360 case usbFunctionsToString return: ' + maskCode);
      expect(false).assertTrue();
    } catch (err) {
      console.info('usb 1360 catch err code: ' + err.code + ' message: ' + err.message);
      expect(err.code).assertEqual(401);
      console.info('usb SUB_USB_JS_1360 :  PASS');
    }
  })

J
jiyong_sd 已提交
197
  /**
198 199 200
   * @tc.number: SUB_USB_JS_0860
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'none', return 0
J
jiyong_sd 已提交
201 202 203 204 205 206
   */
  it('SUB_USB_JS_0860', 0, function () {
    console.info('usb SUB_USB_JS_0860 begin');
    var strMaskCode = 'none'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
207
    expect(nMaskCode).assertEqual(usb.NONE);
J
jiyong_sd 已提交
208 209 210 211
    console.info('usb SUB_USB_JS_0860 :  PASS');
  })

  /**
212 213 214
   * @tc.number: SUB_USB_JS_0870
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'acm', return 1
J
jiyong_sd 已提交
215 216 217 218 219 220
   */
  it('SUB_USB_JS_0870', 0, function () {
    console.info('usb SUB_USB_JS_0870 begin');
    var strMaskCode = 'acm'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
221
    expect(nMaskCode).assertEqual(usb.ACM);
J
jiyong_sd 已提交
222 223 224 225
    console.info('usb SUB_USB_JS_0870 :  PASS');
  })

  /**
226 227 228
   * @tc.number: SUB_USB_JS_0880
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'ecm', return 2
J
jiyong_sd 已提交
229 230 231 232 233 234
   */
  it('SUB_USB_JS_0880', 0, function () {
    console.info('usb SUB_USB_JS_0880 begin');
    var strMaskCode = 'ecm'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
235
    expect(nMaskCode).assertEqual(usb.ECM);
J
jiyong_sd 已提交
236 237 238 239
    console.info('usb SUB_USB_JS_0880 :  PASS');
  })

  /**
240 241 242
   * @tc.number: SUB_USB_JS_0890
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'acm,ecm', return 3
J
jiyong_sd 已提交
243 244 245 246 247 248
   */
  it('SUB_USB_JS_0890', 0, function () {
    console.info('usb SUB_USB_JS_0890 begin');
    var strMaskCode = 'acm,ecm'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
249
    expect(nMaskCode).assertEqual(usb.ACM | usb.ECM);
J
jiyong_sd 已提交
250 251 252 253
    console.info('usb SUB_USB_JS_0890 :  PASS');
  })

  /**
254 255 256
   * @tc.number: SUB_USB_JS_0900
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'hdc', return 4
J
jiyong_sd 已提交
257 258 259 260 261 262
   */
  it('SUB_USB_JS_0900', 0, function () {
    console.info('usb SUB_USB_JS_0900 begin');
    var strMaskCode = 'hdc'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
263
    expect(nMaskCode).assertEqual(usb.HDC);
J
jiyong_sd 已提交
264 265 266 267
    console.info('usb SUB_USB_JS_0900 :  PASS');
  })

  /**
268 269 270
   * @tc.number: SUB_USB_JS_0910
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'acm,hdc', return 5
J
jiyong_sd 已提交
271 272 273 274 275 276
   */
  it('SUB_USB_JS_0910', 0, function () {
    console.info('usb SUB_USB_JS_0910 begin');
    var strMaskCode = 'acm,hdc'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
277
    expect(nMaskCode).assertEqual(usb.HDC | usb.ACM);
J
jiyong_sd 已提交
278 279 280 281
    console.info('usb SUB_USB_JS_0910 :  PASS');
  })

  /**
282 283 284
   * @tc.number: SUB_USB_JS_0920
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Positive test: input 'ecm,hdc', return 6
J
jiyong_sd 已提交
285 286 287 288 289 290
   */
  it('SUB_USB_JS_0920', 0, function () {
    console.info('usb SUB_USB_JS_0920 begin');
    var strMaskCode = 'ecm,hdc'
    var nMaskCode = usb.usbFunctionsFromString(strMaskCode)
    console.info('usb case strMaskCode ' + strMaskCode + ' usbFunctionsFromString return int: ' + nMaskCode);
291
    expect(nMaskCode).assertEqual(usb.HDC | usb.ECM);
J
jiyong_sd 已提交
292 293 294
    console.info('usb SUB_USB_JS_0920 :  PASS');
  })

295
  /**
296 297 298
   * @tc.number: SUB_USB_JS_1170
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Negative test: Description character converted to mask, parameter type error
299
   */
300
  it('SUB_USB_JS_1170', 0, function () {
301 302 303
    console.info('usb SUB_USB_JS_1170 begin');
    try {
      var maskCode = usb.usbFunctionsFromString(invalidCode);
304
      console.info('usb 1170 case usbFunctionsFromString return: ' + maskCode);
305 306
      expect(false).assertTrue();
    } catch (err) {
307
      console.info('usb 1170 catch err code: ' + err.code + ' message: ' + err.message);
308 309 310 311
      expect(err.code).assertEqual(401);
      console.info('usb SUB_USB_JS_1170 :  PASS');
    }
  })
312 313

  /**
314 315 316 317
   * @tc.number: SUB_USB_JS_1370
   * @tc.name: usbFunctionsFromString
   * @tc.desc: Negative test: Description character converted to mask,
   * parameter number exception, necessary parameters not input
318
   */
319
  it('SUB_USB_JS_1370', 0, function () {
320 321 322 323 324 325 326 327 328 329 330
    console.info('usb SUB_USB_JS_1370 begin');
    try {
      var maskCode = usb.usbFunctionsFromString();
      console.info('usb 1370 case usbFunctionsFromString return: ' + maskCode);
      expect(false).assertTrue();
    } catch (err) {
      console.info('usb 1370 catch err code: ' + err.code + ' message: ' + err.message);
      expect(err.code).assertEqual(401);
      console.info('usb SUB_USB_JS_1370 :  PASS');
    }
  })
J
jiyong_sd 已提交
331 332
})
}