1/* 2 * Copyright (C) 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 16 17import tag from '@ohos.nfc.tag'; 18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium' 19import cardEmulation from '@ohos.nfc.cardEmulation'; 20import controller from '@ohos.nfc.controller'; 21 22function sleep(delay) { 23 return new Promise(resovle => setTimeout(resovle, delay)) 24} 25 26let isoDepTaginfo = { 27 "uid": [0x01, 0x02, 0x03, 0x04], 28 "technology": [1, 3], 29 "extrasData": [ 30 { 31 "Sak": 0x08, "Atqa": "B000", 32 }, 33 { 34 "HistoricalBytes": "4D54000500308693695B", "HiLayerResponse": "", 35 }, 36 ], 37 "tagRfDiscId": 1, 38}; 39 40let CardType = { 41 PAYMENT : "payment", 42 OTHER : "other", 43}; 44 45let IsoDepTag ; 46 47export default function nfcIsoDepTagTest() { 48 describe('nfcIsoDepTagTest', function () { 49 beforeAll(function () { 50 console.info('rbeforeAll called') 51 try { 52 IsoDepTag = tag.getIsoDep(isoDepTaginfo); 53 } catch (error) { 54 console.info('getIsoDep is ->' + error) 55 } 56 }) 57 beforeEach(function() { 58 console.info('beforeEach called') 59 }) 60 afterEach(function () { 61 console.info('afterEach called') 62 }) 63 afterAll(function () { 64 console.info('afterAll called') 65 }) 66 67 /** 68 * @tc.number SUB_Communication_NFC_nfcIsoDep_js_0100 69 * @tc.name Test getHistoricalBytes IsoDep 70 * @tc.desc Obtains the history bytes of a label. 71 * @tc.size since 9 72 * @tc.type Function 73 * @tc.level Level 2 74 */ 75 it('SUB_Communication_NFC_nfcIsoDep_js_0100', 0, function () { 76 if (IsoDepTag != null && IsoDepTag != undefined) { 77 let ResponseFlags = IsoDepTag.getHistoricalBytes(); 78 expect(ResponseFlags).assertInstanceOf('Array') 79 console.info('[NFC_test] Isodep1 test ResponseFlags data>:' + ResponseFlags); 80 } else { 81 console.info("[NFC_test]IsoDepTag1 = null & = undefined: "); 82 expect().assertFail(); 83 } 84 }) 85 86 /** 87 * @tc.number SUB_Communication_NFC_nfcIsoDep_js_0200 88 * @tc.name Test getHiLayerResponse IsoDep 89 * @tc.desc HiLayer response byte for obtaining the tag. 90 * @tc.size since 9 91 * @tc.type Function 92 * @tc.level Level 2 93 */ 94 it('SUB_Communication_NFC_nfcIsoDep_js_0200', 0, function () { 95 if (IsoDepTag != null && IsoDepTag != undefined) { 96 let HiLayerResponse = IsoDepTag.getHiLayerResponse(); 97 expect(HiLayerResponse).assertInstanceOf('Array') 98 console.info('[NFC_test] Isodep2 test ResponseFlags data2>:' + HiLayerResponse); 99 } else { 100 console.info("[NFC_test]IsoDepTag2 = null & = undefined: "); 101 expect().assertFail(); 102 } 103 }) 104 105 /** 106 * @tc.number SUB_Communication_NFC_nfcIsoDep_js_0300 107 * @tc.name Test isExtendedApduSupported IsoDep 108 * @tc.desc Check whether extended APDUs are supported.Promise 109 * @tc.size since 9 110 * @tc.type Function 111 * @tc.level Level 2 112 */ 113 it('SUB_Communication_NFC_nfcIsoDep_js_0300', 0, async function (done) { 114 if (IsoDepTag != null && IsoDepTag != undefined) { 115 await IsoDepTag.isExtendedApduSupported().then((data) => { 116 console.info("[NFC_test] Isodep3 isExtendedApduSupported data: " + data); 117 expect(true).assertEqual(data); 118 done(); 119 }).catch((err)=> { 120 console.info("[NFC_test] Isodep3 isExtendedApduSupported err: " + err); 121 expect().assertFail(); 122 done(); 123 }); 124 } else { 125 console.info("[NFC_test]IsoDepTag3 = null & = undefined: "); 126 expect().assertFail(); 127 } 128 }) 129 130 /** 131 * @tc.number SUB_Communication_NFC_nfcIsoDep_js_0400 132 * @tc.name Test isExtendedApduSupported IsoDep 133 * @tc.desc Check whether extended APDUs are supported.callback 134 * @tc.size since 9 135 * @tc.type Function 136 * @tc.level Level 2 137 */ 138 it('SUB_Communication_NFC_nfcIsoDep_js_0400', 0, async function (done) { 139 if (IsoDepTag != null && IsoDepTag != undefined) { 140 IsoDepTag.isExtendedApduSupported((err, data)=> { 141 if (err) { 142 console.info("[NFC_test] Isodep4 isExtendedApduSupported err: " + err); 143 expect().assertFail(); 144 done(); 145 } else { 146 console.info("[NFC_test] Isodep4 isExtendedApduSupported data: " + data); 147 expect(true).assertEqual(data); 148 done(); 149 } 150 }); 151 } else { 152 console.info("[NFC_test]IsoDepTag4 = null & = undefined: "); 153 expect().assertFail(); 154 } 155 }) 156 157 /** 158 * @tc.number SUB_Communication_NFC_nfccardEmulationnfc_js_0500 159 * @tc.name Test cardEmulationnfc 160 * @tc.desc Whether to support a certain type of card emulation. 161 * @tc.size since 9 162 * @tc.type Function 163 * @tc.level Level 2 164 */ 165 it('SUB_Communication_NFC_nfccardEmulationnfc_js_0500', 0, function () { 166 if (IsoDepTag != null && IsoDepTag != undefined) { 167 let cardEmulationnfc ; 168 try { 169 cardEmulationnfc = cardEmulation.isSupported(cardEmulation.FeatureType.ESE); 170 console.info('[NFC_test] cardEmulationnfc5 type ->' + cardEmulationnfc ) 171 expect(cardEmulationnfc).assertFalse(); 172 } catch (error) { 173 console.info('[NFC_test] cardEmulationnfc5 error' + error) 174 expect().assertFail(); 175 } 176 } else { 177 console.info("[NFC_test]IsoDepTag5 = null & = undefined: "); 178 expect().assertFail(); 179 } 180 }) 181 182 /** 183 * @tc.number SUB_Communication_NFC_nfccardEmulationnfc_js_0600 184 * @tc.name Test cardEmulationnfc 185 * @tc.desc hasHceCapability to support a certain type of card emulation. 186 * @tc.size since 9 187 * @tc.type Function 188 * @tc.level Level 2 189 */ 190 it('SUB_Communication_NFC_nfccardEmulationnfc_js_0600', 0, function () { 191 let cardEmulationnfc ; 192 try { 193 cardEmulationnfc = cardEmulation.hasHceCapability(); 194 console.info('[NFC_test] cardEmulationnfc6 hasHceCapability type ->' + cardEmulationnfc ) 195 expect(cardEmulationnfc).assertTrue(); 196 } catch (error) { 197 console.info('[NFC_test] cardEmulationnfc6 error' + error) 198 expect().assertFail(); 199 } 200 }) 201 202 /** 203 * @tc.number SUB_Communication_NFC_nfccardEmulationnfc_js_0700 204 * @tc.name Test cardEmulationnfc 205 * @tc.desc Whether to support a certain type of card isDefaultService. 206 * @tc.size since 9 207 * @tc.type Function 208 * @tc.level Level 2 209 */ 210 it('SUB_Communication_NFC_nfccardEmulationnfc_js_0700', 0, function () { 211 let cardEmulationnfc ; 212 let elementName = { 213 "bundleName": "com.test.cardemulation", 214 "abilityName": "com.test.cardemulation.MainAbility", 215 }; 216 try { 217 cardEmulationnfc = cardEmulation.isDefaultService(elementName, CardType.PAYMENT); 218 console.info('[NFC_test] cardEmulationnfc7 isDefaultService type ->' + cardEmulationnfc ) 219 expect(cardEmulationnfc).assertFalse(); 220 } catch (error) { 221 console.info('[NFC_test] cardEmulationnfc7 error' + error) 222 expect().assertFail(); 223 } 224 }) 225 226 /** 227 * @tc.number SUB_Communication_NFC_Enumerated_js_0800 228 * @tc.name Test Enumerated 229 * @tc.desc Enumeration return value test 230 * @tc.size since 9 231 * @tc.type Function 232 * @tc.level Level 2 233 */ 234 it('SUB_Communication_NFC_Enumerated_js_0800', 0, function () { 235 console.info('[nfc_test]1 PAYMENT Enumerated value test pass:' + cardEmulation.CardType.PAYMENT) 236 expect(cardEmulation.CardType.PAYMENT).assertEqual("payment"); 237 console.info('[nfc_test]2 OTHER Enumerated value test pass:' + cardEmulation.CardType.OTHER) 238 expect(cardEmulation.CardType.OTHER).assertEqual("other"); 239 console.info('[nfc_test]3 NFC_FORUM_TYPE_1 Enumerated value test pass:' + tag.NfcForumType.NFC_FORUM_TYPE_1) 240 expect(tag.NfcForumType.NFC_FORUM_TYPE_1).assertEqual(1); 241 console.info('[nfc_test]4 NFC_FORUM_TYPE_2 Enumerated value test pass:' + tag.NfcForumType.NFC_FORUM_TYPE_2) 242 expect(tag.NfcForumType.NFC_FORUM_TYPE_2).assertEqual(2); 243 console.info('[nfc_test]5 NFC_FORUM_TYPE_3 Enumerated value test pass:' + tag.NfcForumType.NFC_FORUM_TYPE_3) 244 expect(tag.NfcForumType.NFC_FORUM_TYPE_3).assertEqual(3); 245 console.info('[nfc_test]6 NFC_FORUM_TYPE_4 Enumerated value test pass:' + tag.NfcForumType.NFC_FORUM_TYPE_4) 246 expect(tag.NfcForumType.NFC_FORUM_TYPE_4).assertEqual(4); 247 console.info('[nfc_test]7 MIFARE_CLASSIC Enumerated value test pass:' + tag.NfcForumType.MIFARE_CLASSIC) 248 expect(tag.NfcForumType.MIFARE_CLASSIC).assertEqual(101); 249 console.info('[nfc_test]8 HCE Enumerated value test pass:' + cardEmulation.FeatureType.HCE) 250 expect(cardEmulation.FeatureType.HCE).assertEqual(0); 251 console.info('[nfc_test]9 UICC Enumerated value test pass:' + cardEmulation.FeatureType.UICC) 252 expect(cardEmulation.FeatureType.UICC).assertEqual(1); 253 console.info('[nfc_test]10 ESE Enumerated value test pass:' + cardEmulation.FeatureType.ESE) 254 expect(cardEmulation.FeatureType.ESE).assertEqual(2); 255 console.info('[nfc_test]11 TYPE_UNKNOWN Enumerated value test pass:' + tag.MifareClassicType.TYPE_UNKNOWN) 256 expect(tag.MifareClassicType.TYPE_UNKNOWN).assertEqual(0); 257 console.info('[nfc_test]12 TYPE_CLASSIC Enumerated value test pass:' + tag.MifareClassicType.TYPE_CLASSIC) 258 expect(tag.MifareClassicType.TYPE_CLASSIC).assertEqual(1); 259 console.info('[nfc_test]13 TYPE_PLUS Enumerated value test pass:' + tag.MifareClassicType.TYPE_PLUS) 260 expect(tag.MifareClassicType.TYPE_PLUS).assertEqual(2); 261 console.info('[nfc_test]14 TYPE_PRO Enumerated value test pass:' + tag.MifareClassicType.TYPE_PRO) 262 expect(tag.MifareClassicType.TYPE_PRO).assertEqual(3); 263 console.info('[nfc_test]15 MC_SIZE_MINI Enumerated value test pass:' + tag.MifareClassicSize.MC_SIZE_MINI) 264 expect(tag.MifareClassicSize.MC_SIZE_MINI).assertEqual(320); 265 console.info('[nfc_test]16 MC_SIZE_1K Enumerated value test pass:' + tag.MifareClassicSize.MC_SIZE_1K) 266 expect(tag.MifareClassicSize.MC_SIZE_1K).assertEqual(1024); 267 console.info('[nfc_test]17 MC_SIZE_2K Enumerated value test pass:' + tag.MifareClassicSize.MC_SIZE_2K) 268 expect(tag.MifareClassicSize.MC_SIZE_2K).assertEqual(2048); 269 console.info('[nfc_test]18 MC_SIZE_4K Enumerated value test pass:' + tag.MifareClassicSize.MC_SIZE_4K) 270 expect(tag.MifareClassicSize.MC_SIZE_4K).assertEqual(4096); 271 console.info('[nfc_test]19 TYPE_UNKNOWN Enumerated value test pass:' + tag.MifareUltralightType.TYPE_UNKNOWN) 272 expect(tag.MifareUltralightType.TYPE_UNKNOWN).assertEqual(0); 273 console.info('[nfc_test]20 TYPE_ULTRALIGHT Enumerated value pass:' + tag.MifareUltralightType.TYPE_ULTRALIGHT) 274 expect(tag.MifareUltralightType.TYPE_ULTRALIGHT).assertEqual(1); 275 console.info('[nfc_test]21 TYPE_ULTRALIGHT_C Enumerated value pass:' + tag.MifareUltralightType.TYPE_ULTRALIGHT_C) 276 expect(tag.MifareUltralightType.TYPE_ULTRALIGHT_C).assertEqual(2); 277 console.info('[nfc_test]22 TNF_EMPTY Enumerated value test pass:' + tag.TnfType.TNF_EMPTY) 278 expect(tag.TnfType.TNF_EMPTY).assertEqual(0x0); 279 console.info('[nfc_test]23 TNF_WELL_KNOWN Enumerated value test pass:' + tag.TnfType.TNF_WELL_KNOWN) 280 expect(tag.TnfType.TNF_WELL_KNOWN).assertEqual(0x01); 281 console.info('[nfc_test]24 TNF_MEDIA Enumerated value test pass:' + tag.TnfType.TNF_MEDIA) 282 expect(tag.TnfType.TNF_MEDIA).assertEqual(0x02); 283 console.info('[nfc_test]25 TNF_ABSOLUTE_URI Enumerated value test pass:' + tag.TnfType.TNF_ABSOLUTE_URI) 284 expect(tag.TnfType.TNF_ABSOLUTE_URI).assertEqual(0x03); 285 console.info('[nfc_test]26 TNF_EXT_APP Enumerated value test pass:' + tag.TnfType.TNF_EXT_APP) 286 expect(tag.TnfType.TNF_EXT_APP).assertEqual(0x04); 287 console.info('[nfc_test]27 TNF_UNKNOWN Enumerated value test pass:' + tag.TnfType.TNF_UNKNOWN) 288 expect(tag.TnfType.TNF_UNKNOWN).assertEqual(0x05); 289 console.info('[nfc_test]28 TNF_UNCHANGED Enumerated value test pass:' + tag.TnfType.TNF_UNCHANGED) 290 expect(tag.TnfType.TNF_UNCHANGED).assertEqual(0x06); 291 console.info('[nfc_test]29 RTD_TEXT Enumerated value pass:' + String(tag.RtdType.RTD_TEXT) + '--' + String(0x54)) 292 expect(String(tag.RtdType.RTD_TEXT)).assertEqual(String(0x54)); 293 console.info('[nfc_test]30 RTD_URI Enumerated value pass:' + String(tag.RtdType.RTD_URI) + '--' + String(0x55)) 294 expect(String(tag.RtdType.RTD_URI)).assertEqual(String(0x55)); 295 console.info('[nfc_test]31 STATE_OFF Enumerated value test pass:' + controller.NfcState.STATE_OFF) 296 expect(controller.NfcState.STATE_OFF).assertEqual(1); 297 console.info('[nfc_test]32 STATE_TURNING_ON Enumerated value test pass:' + controller.NfcState.STATE_TURNING_ON) 298 expect(controller.NfcState.STATE_TURNING_ON).assertEqual(2); 299 console.info('[nfc_test]33 STATE_ON Enumerated value test pass:' + controller.NfcState.STATE_ON) 300 expect(controller.NfcState.STATE_ON).assertEqual(3); 301 console.info('[nfc_test]34 STATE_TURNING_OFF Enumerated value test pass:' + controller.NfcState.STATE_TURNING_OFF) 302 expect(controller.NfcState.STATE_TURNING_OFF).assertEqual(4); 303 console.info('[nfc_test]35 NFC_A Enumerated value test pass:' + tag.NFC_A) 304 expect(tag.NFC_A).assertEqual(1); 305 console.info('[nfc_test]36 NFC_B Enumerated value test pass:' + tag.NFC_B) 306 expect(tag.NFC_B).assertEqual(2); 307 console.info('[nfc_test]37 ISO_DEP Enumerated value test pass:' + tag.ISO_DEP) 308 expect(tag.ISO_DEP).assertEqual(3); 309 console.info('[nfc_test]38 NFC_F Enumerated value test pass:' + tag.NFC_F) 310 expect(tag.NFC_F).assertEqual(4); 311 console.info('[nfc_test]39 NFC_V Enumerated value test pass:' + tag.NFC_V) 312 expect(tag.NFC_V).assertEqual(5); 313 console.info('[nfc_test]40 NDEF Enumerated value test pass:' + tag.NDEF) 314 expect(tag.NDEF).assertEqual(6); 315 console.info('[nfc_test]41 MIFARE_CLASSIC Enumerated value test pass:' + tag.MIFARE_CLASSIC) 316 expect(tag.MIFARE_CLASSIC).assertEqual(8); 317 console.info('[nfc_test]42 MIFARE_ULTRALIGHT Enumerated value test pass:' + tag.MIFARE_ULTRALIGHT) 318 expect(tag.MIFARE_ULTRALIGHT).assertEqual(9); 319 console.info('[nfc_test]43 NDEF_FORMATABLE Enumerated value test pass:' + tag.NDEF_FORMATABLE) 320 expect(tag.NDEF_FORMATABLE).assertEqual(7); 321 }) 322 323 console.info("*************[nfc_test] start nfc js unit test end*************"); 324 }) 325}