• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <gtest/gtest.h>
16 #include <thread>
17 
18 #include "nfc_controller.h"
19 #include "nfc_sdk_common.h"
20 
21 namespace OHOS {
22 namespace NFC {
23 namespace TEST {
24 using namespace testing::ext;
25 using namespace OHOS::NFC::KITS;
26 class NfcControllerTest : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30     void SetUp();
31     void TearDown();
32 public:
33     static constexpr const auto TEST_NFC_STATE_CHANGE = "nfcStateChange";
34 };
35 
36 class INfcControllerCallbackImpl : public INfcControllerCallback {
37 public:
INfcControllerCallbackImpl()38     INfcControllerCallbackImpl() {}
39 
~INfcControllerCallbackImpl()40     virtual ~INfcControllerCallbackImpl() {}
41 
42 public:
OnNfcStateChanged(int nfcState)43     void OnNfcStateChanged(int nfcState) override
44     {
45     }
46 
AsObject()47     OHOS::sptr<OHOS::IRemoteObject> AsObject() override
48     {
49         return nullptr;
50     }
51 };
52 
SetUpTestCase()53 void NfcControllerTest::SetUpTestCase()
54 {
55     std::cout << " SetUpTestCase NfcControllerTest." << std::endl;
56 }
57 
TearDownTestCase()58 void NfcControllerTest::TearDownTestCase()
59 {
60     std::cout << " TearDownTestCase NfcControllerTest." << std::endl;
61 }
62 
SetUp()63 void NfcControllerTest::SetUp() {}
64 
TearDown()65 void NfcControllerTest::TearDown() {}
66 
67 /**
68  * @tc.name: GetNfcState001
69  * @tc.desc: Test NfcController GetNfcState.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(NfcControllerTest, GetNfcState001, TestSize.Level1)
73 {
74     NfcController ctrl = NfcController::GetInstance();
75     int state = ctrl.GetNfcState();
76     ASSERT_TRUE(state == NfcState::STATE_OFF ||
77         state == NfcState::STATE_ON ||
78         state == NfcState::STATE_TURNING_ON ||
79         state == NfcState::STATE_TURNING_OFF);
80 }
81 
82 /**
83  * @tc.name: TurnOn001
84  * @tc.desc: Test NfcController TurnOn.
85  * @tc.type: FUNC
86  */
87 HWTEST_F(NfcControllerTest, TurnOn001, TestSize.Level1)
88 {
89     NfcController ctrl = NfcController::GetInstance();
90     ctrl.TurnOn();
91 
92     // wait for turn on finished.
93     std::this_thread::sleep_for(std::chrono::seconds(3));
94     int state = ctrl.GetNfcState();
95     ASSERT_TRUE(state == NfcState::STATE_ON);
96 }
97 
98 /**
99  * @tc.name: TurnOff001
100  * @tc.desc: Test NfcController TurnOff.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(NfcControllerTest, TurnOff001, TestSize.Level1)
104 {
105     NfcController ctrl = NfcController::GetInstance();
106     ctrl.TurnOff();
107 
108     // wait for turn off finished.
109     std::this_thread::sleep_for(std::chrono::seconds(3));
110     int state = ctrl.GetNfcState();
111     ASSERT_TRUE(state == NfcState::STATE_OFF);
112 }
113 
114 /**
115  * @tc.name: IsNfcAvailable001
116  * @tc.desc: Test NfcController IsNfcAvailable.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(NfcControllerTest, IsNfcAvailable001, TestSize.Level1)
120 {
121     NfcController ctrl = NfcController::GetInstance();
122 
123     // IsNfcAvailable Fixed return true
124     ASSERT_TRUE(ctrl.IsNfcAvailable() == true);
125 }
126 
127 /**
128  * @tc.name: IsNfcOpen001
129  * @tc.desc: Test NfcController IsNfcOpen.
130  * @tc.type: FUNC
131  */
132 HWTEST_F(NfcControllerTest, IsNfcOpen001, TestSize.Level1)
133 {
134     NfcController ctrl = NfcController::GetInstance();
135 
136     // open nfc
137     ctrl.TurnOn();
138     std::this_thread::sleep_for(std::chrono::seconds(3));
139 
140     bool isOpen = false;
141     int statusCode = ctrl.IsNfcOpen(isOpen);
142     ASSERT_TRUE(statusCode == KITS::ErrorCode::ERR_NONE);
143     ASSERT_TRUE(isOpen == true);
144 
145     // close nfc
146     ctrl.TurnOff();
147     std::this_thread::sleep_for(std::chrono::seconds(3));
148     isOpen = true;
149     statusCode = ctrl.IsNfcOpen(isOpen);
150     ASSERT_TRUE(statusCode == KITS::ErrorCode::ERR_NONE);
151     ASSERT_TRUE(isOpen == false);
152 }
153 
154 /**
155  * @tc.name: RegListener001
156  * @tc.desc: Test NfcController RegListener.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(NfcControllerTest, RegListener001, TestSize.Level1)
160 {
161     NfcController ctrl = NfcController::GetInstance();
162     sptr<INfcControllerCallbackImpl> iNfcControllerCallbackImpl =
163     sptr<INfcControllerCallbackImpl>(new (std::nothrow) INfcControllerCallbackImpl());
164     ErrorCode errorCode = ctrl.RegListener(iNfcControllerCallbackImpl, TEST_NFC_STATE_CHANGE);
165     ASSERT_TRUE(errorCode == ErrorCode::ERR_NONE);
166 }
167 
168 /**
169  * @tc.name: UnregListener001
170  * @tc.desc: Test NfcController UnregListener.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(NfcControllerTest, UnregListener001, TestSize.Level1)
174 {
175     NfcController ctrl = NfcController::GetInstance();
176     ErrorCode errorCode = ctrl.UnregListener(TEST_NFC_STATE_CHANGE);
177     ASSERT_TRUE(errorCode == ErrorCode::ERR_NONE);
178 }
179 
180 /**
181  * @tc.name: GetTagServiceIface001
182  * @tc.desc: Test NfcController GetTagServiceIface.
183  * @tc.type: FUNC
184  */
185 HWTEST_F(NfcControllerTest, GetTagServiceIface001, TestSize.Level1)
186 {
187     NfcController ctrl = NfcController::GetInstance();
188     OHOS::sptr<IRemoteObject> objsPtr = ctrl.GetTagServiceIface();
189     ASSERT_TRUE(objsPtr != nullptr);
190 }
191 }
192 }
193 }
194