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