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 controller from '@ohos.nfc.controller'; 19import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, Level} from '@ohos/hypium' 20 21function sleep(delay) { 22 return new Promise(resovle => setTimeout(resovle, delay)) 23} 24 25let NfcState={ 26 STATE_OFF : 1, 27 STATE_TURNING_ON : 2, 28 STATE_ON : 3, 29 STATE_TURNING_OFF : 4, 30} 31 32export default function nfcControllerTest() { 33 describe('nfcControllerTest', function () { 34 beforeAll(function () { 35 console.info('rbeforeAll called') 36 }) 37 beforeEach(function() { 38 console.info('beforeEach called') 39 }) 40 afterEach(function () { 41 console.info('afterEach called') 42 }) 43 afterAll(function () { 44 console.info('afterAll called') 45 }) 46 47 /** 48 * @tc.number SUB_COMMUNICATION_NFC_Cont_0100 49 * @tc.name Test on_off_openNfcapi 50 * @tc.desc Register the NFC switch status event and enable the NFC switch. 51 * @tc.size since 7 52 * @tc.type Function 53 * @tc.level Level 0 54 */ 55 it('SUB_COMMUNICATION_NFC_Cont_0100', Level.LEVEL0, function () { 56 let NFC_STATE_NOTIFY = "nfcStateChange"; 57 let recvNfcStateNotifyFunc = result => { 58 console.info("[NFC_test] controller1 nfc state receive state ->" + result); 59 expect(result != null).assertTrue(); 60 } 61 controller.on(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc); 62 controller.off(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc); 63 }) 64 65 /** 66 * @tc.number SUB_COMMUNICATION_NFC_Cont_0200 67 * @tc.name Test isNfcAvailableapi 68 * @tc.desc Check whether the NFC function is enabled. 69 * @tc.size since 7 70 * @tc.type Function 71 * @tc.level Level 0 72 */ 73 it('SUB_COMMUNICATION_NFC_Cont_0200', Level.LEVEL0, function () { 74 let nfcisAvailable = controller.isNfcAvailable(); 75 console.info('[NFC_test] controller2 Nfc Available ->' + JSON.stringify(nfcisAvailable)); 76 expect(nfcisAvailable).assertTrue(); 77 }) 78 79 /** 80 * @tc.number SUB_COMMUNICATION_NFC_Cont_0300 81 * @tc.name Test isNfcOpenapi 82 * @tc.desc Check whether the NFC function is enabled. 83 * @tc.size since 7 84 * @tc.type Function 85 * @tc.level Level 0 86 */ 87 it('SUB_COMMUNICATION_NFC_Cont_0300', Level.LEVEL0, function () { 88 let nfcswitchis = controller.isNfcOpen(); 89 console.info('[NFC_test] controller3 Nfc isopen state is ->' + JSON.stringify(nfcswitchis)); 90 expect(nfcswitchis).assertTrue(); 91 }) 92 93 /** 94 * @tc.number SUB_COMMUNICATION_NFC_Cont_0400 95 * @tc.name Test isNfcAvailable_isNfcOpenapi 96 * @tc.desc Check whether the NFC function is enabled on the device. 97 * @tc.size since 7 98 * @tc.type Function 99 * @tc.level Level 0 100 */ 101 it('SUB_COMMUNICATION_NFC_Cont_0400', Level.LEVEL0, function () { 102 let nfcisAvailable1 = controller.isNfcAvailable(); 103 console.info('[NFC_test] controller4 NfcAvailable 1 ->' + JSON.stringify(nfcisAvailable1)); 104 expect(nfcisAvailable1).assertTrue(); 105 let nfcenable1 = controller.isNfcOpen(); 106 console.info('[NFC_test] controller4 Nfc isopen 1 state is ->' + JSON.stringify(nfcenable1)); 107 expect(nfcenable1).assertTrue(); 108 }) 109 110 /** 111 * @tc.number SUB_COMMUNICATION_NFC_Cont_0500 112 * @tc.name Test getNfcStateapi 113 * @tc.desc Querying the Status When NFC Is Enabled 114 * @tc.size since 7 115 * @tc.type Function 116 * @tc.level Level 0 117 */ 118 it('SUB_COMMUNICATION_NFC_Cont_0500', Level.LEVEL0, function () { 119 let checkopennfc = controller.getNfcState(); 120 console.info("[NFC_test] controller5 checkopen the state of nfc-> " + JSON.stringify(checkopennfc)); 121 expect(checkopennfc).assertEqual(NfcState.STATE_ON); 122 }) 123 124 console.info("*************[nfc_test] start nfc js unit test end*************"); 125 }) 126}