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_controller_impl.h"
20 #include "nfc_sdk_common.h"
21 #include "nfc_service.h"
22 #include "loghelper.h"
23
24 namespace OHOS {
25 namespace NFC {
26 namespace TEST {
27 using namespace testing::ext;
28 using namespace OHOS::NFC::KITS;
29 class NfcControllerTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 public:
36 static constexpr const auto TEST_NFC_STATE_CHANGE = "nfcStateChange";
37 };
38
39 class INfcControllerCallbackImpl : public INfcControllerCallback {
40 public:
INfcControllerCallbackImpl()41 INfcControllerCallbackImpl() {}
42
~INfcControllerCallbackImpl()43 virtual ~INfcControllerCallbackImpl() {}
44
45 public:
OnNfcStateChanged(int nfcState)46 void OnNfcStateChanged(int nfcState) override
47 {
48 }
49
AsObject()50 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
51 {
52 return nullptr;
53 }
54 };
55
SetUpTestCase()56 void NfcControllerTest::SetUpTestCase()
57 {
58 std::cout << " SetUpTestCase NfcControllerTest." << std::endl;
59 }
60
TearDownTestCase()61 void NfcControllerTest::TearDownTestCase()
62 {
63 std::cout << " TearDownTestCase NfcControllerTest." << std::endl;
64 }
65
SetUp()66 void NfcControllerTest::SetUp() {}
67
TearDown()68 void NfcControllerTest::TearDown() {}
69
70 /**
71 * @tc.name: GetNfcState001
72 * @tc.desc: Test NfcController GetNfcState.
73 * @tc.type: FUNC
74 */
75 HWTEST_F(NfcControllerTest, GetNfcState001, TestSize.Level1)
76 {
77 NfcController ctrl = NfcController::GetInstance();
78 int state = ctrl.GetNfcState();
79 ASSERT_TRUE(state == NfcState::STATE_OFF ||
80 state == NfcState::STATE_ON ||
81 state == NfcState::STATE_TURNING_ON ||
82 state == NfcState::STATE_TURNING_OFF);
83 }
84
85 /**
86 * @tc.name: TurnOn001
87 * @tc.desc: Test NfcController TurnOn.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(NfcControllerTest, TurnOn001, TestSize.Level1)
91 {
92 NfcController ctrl = NfcController::GetInstance();
93 ctrl.TurnOn();
94
95 // wait for turn on finished.
96 std::this_thread::sleep_for(std::chrono::seconds(3));
97 int state = ctrl.GetNfcState();
98 ASSERT_TRUE(state == NfcState::STATE_ON);
99 }
100
101 /**
102 * @tc.name: TurnOff001
103 * @tc.desc: Test NfcController TurnOff.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(NfcControllerTest, TurnOff001, TestSize.Level1)
107 {
108 NfcController ctrl = NfcController::GetInstance();
109 ctrl.TurnOff();
110
111 // wait for turn off finished.
112 std::this_thread::sleep_for(std::chrono::seconds(3));
113 int state = ctrl.GetNfcState();
114 ASSERT_TRUE(state == NfcState::STATE_OFF);
115 }
116
117 /**
118 * @tc.name: IsNfcAvailable001
119 * @tc.desc: Test NfcController IsNfcAvailable.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(NfcControllerTest, IsNfcAvailable001, TestSize.Level1)
123 {
124 NfcController ctrl = NfcController::GetInstance();
125
126 // IsNfcAvailable Fixed return true
127 ASSERT_TRUE(ctrl.IsNfcAvailable() == true);
128 }
129
130 /**
131 * @tc.name: IsNfcOpen001
132 * @tc.desc: Test NfcController IsNfcOpen.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(NfcControllerTest, IsNfcOpen001, TestSize.Level1)
136 {
137 NfcController ctrl = NfcController::GetInstance();
138
139 // open nfc
140 ctrl.TurnOn();
141 std::this_thread::sleep_for(std::chrono::seconds(3));
142
143 bool isOpen = false;
144 int statusCode = ctrl.IsNfcOpen(isOpen);
145 ASSERT_TRUE(statusCode == KITS::ErrorCode::ERR_NONE);
146 ASSERT_TRUE(isOpen == true);
147
148 // close nfc
149 ctrl.TurnOff();
150 std::this_thread::sleep_for(std::chrono::seconds(3));
151 isOpen = true;
152 statusCode = ctrl.IsNfcOpen(isOpen);
153 ASSERT_TRUE(statusCode == KITS::ErrorCode::ERR_NONE);
154 ASSERT_TRUE(isOpen == false);
155 }
156
157 /**
158 * @tc.name: RegListener001
159 * @tc.desc: Test NfcController RegListener.
160 * @tc.type: FUNC
161 */
162 HWTEST_F(NfcControllerTest, RegListener001, TestSize.Level1)
163 {
164 NfcController ctrl = NfcController::GetInstance();
165 const sptr<NfcControllerCallBackStub> g_nfcControllerCallbackStub =
166 sptr<NfcControllerCallBackStub>(new (std::nothrow) NfcControllerCallBackStub());
167 ErrorCode errorCode = ctrl.RegListener(g_nfcControllerCallbackStub, TEST_NFC_STATE_CHANGE);
168 ASSERT_TRUE(errorCode == ErrorCode::ERR_NONE);
169 }
170
171 /**
172 * @tc.name: UnregListener001
173 * @tc.desc: Test NfcController UnregListener.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(NfcControllerTest, UnregListener001, TestSize.Level1)
177 {
178 NfcController ctrl = NfcController::GetInstance();
179 ErrorCode errorCode = ctrl.UnregListener(TEST_NFC_STATE_CHANGE);
180 ASSERT_TRUE(errorCode == ErrorCode::ERR_NONE);
181 }
182
183 /**
184 * @tc.name: GetTagServiceIface001
185 * @tc.desc: Test NfcController GetTagServiceIface.
186 * @tc.type: FUNC
187 */
188 HWTEST_F(NfcControllerTest, GetTagServiceIface001, TestSize.Level1)
189 {
190 NfcController ctrl = NfcController::GetInstance();
191 ctrl.GetTagServiceIface();
192 ErrorCode errorCode = ctrl.UnregListener(TEST_NFC_STATE_CHANGE);
193 InfoLog("GetTagServiceIface001, errorCode = %{public}d", errorCode);
194 ASSERT_TRUE(errorCode == ErrorCode::ERR_NFC_PARAMETERS);
195 }
196
197 /**
198 * @tc.name: NfcControllerImpl001
199 * @tc.desc: Test NfcControllerImpl.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(NfcControllerTest, NfcControllerImpl001, TestSize.Level1)
203 {
204 std::weak_ptr<NFC::NfcService> nfcService;
205 sptr<NFC::NfcControllerImpl> impl = new NFC::NfcControllerImpl(nfcService);
206 int nfcState = 1;
207 ASSERT_TRUE(impl->GetState(nfcState) == ErrorCode::ERR_NFC_PARAMETERS);
208
209 ASSERT_TRUE(impl->TurnOn() == ErrorCode::ERR_NFC_PARAMETERS);
210
211 ASSERT_TRUE(impl->TurnOff() == ErrorCode::ERR_NFC_PARAMETERS);
212
213 ASSERT_TRUE(impl->RegisterNfcStatusCallBack(nullptr, "") == ErrorCode::ERR_NFC_PARAMETERS);
214
215 ASSERT_TRUE(impl->UnregisterNfcStatusCallBack("") == ErrorCode::ERR_NFC_PARAMETERS);
216
217 ASSERT_TRUE(impl->UnRegisterAllCallBack(0) == ErrorCode::ERR_NFC_PARAMETERS);
218
219 sptr<IRemoteObject> remoteObject = nullptr;
220 impl->GetTagServiceIface(remoteObject);
221 ASSERT_TRUE(remoteObject == nullptr);
222 delete impl;
223 }
224
225 /**
226 * @tc.name: OnRemoteDied001
227 * @tc.desc: Test NfcController OnRemoteDied.
228 * @tc.type: FUNC
229 */
230 HWTEST_F(NfcControllerTest, OnRemoteDied001, TestSize.Level1)
231 {
232 wptr<IRemoteObject> remoteObject = nullptr;
233 NfcController ctrl = NfcController::GetInstance();
234 ctrl.OnRemoteDied(remoteObject);
235 ErrorCode errorCode = ctrl.RegNdefMsgCb(nullptr);
236 ASSERT_TRUE(errorCode == ERR_NONE);
237 }
238
239 /**
240 * @tc.name: RegNdefMsgCb001
241 * @tc.desc: Test NfcController RegNdefMsgCb.
242 * @tc.type: FUNC
243 */
244 HWTEST_F(NfcControllerTest, RegNdefMsgCb001, TestSize.Level1)
245 {
246 sptr<INdefMsgCallback> callback = nullptr;
247 NfcController ctrl = NfcController::GetInstance();
248 ErrorCode errorCode = ctrl.RegNdefMsgCb(callback);
249 ASSERT_TRUE(errorCode == ERR_NONE);
250 }
251
252 /**
253 * @tc.name: RegQueryApplicationCb001
254 * @tc.desc: Test NfcController RegQueryApplicationCb.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(NfcControllerTest, RegQueryApplicationCb001, TestSize.Level1)
258 {
259 std::string type = "";
260 QueryApplicationByVendor tagCallback = nullptr;
261 QueryHceAppByVendor hceCallback = nullptr;
262 NfcController ctrl = NfcController::GetInstance();
263 ErrorCode errorCode = ctrl.RegQueryApplicationCb(type, tagCallback, hceCallback);
264 ASSERT_TRUE(errorCode == ERR_NONE);
265 }
266
267 /**
268 * @tc.name: RegCardEmulationNotifyCb001
269 * @tc.desc: Test NfcController RegCardEmulationNotifyCb.
270 * @tc.type: FUNC
271 */
272 HWTEST_F(NfcControllerTest, RegCardEmulationNotifyCb001, TestSize.Level1)
273 {
274 OnCardEmulationNotifyCb callback = nullptr;
275 NfcController ctrl = NfcController::GetInstance();
276 ErrorCode errorCode = ctrl.RegCardEmulationNotifyCb(callback);
277 ASSERT_TRUE(errorCode == ERR_NONE);
278 }
279
280 /**
281 * @tc.name: NotifyEventStatus001
282 * @tc.desc: Test NfcController NotifyEventStatus.
283 * @tc.type: FUNC
284 */
285 HWTEST_F(NfcControllerTest, NotifyEventStatus001, TestSize.Level1)
286 {
287 int eventType = 0;
288 int arg1 = 0;
289 std::string arg2 = "";
290 NfcController ctrl = NfcController::GetInstance();
291 ErrorCode errorCode = ctrl.NotifyEventStatus(eventType, arg1, arg2);
292 ASSERT_TRUE(errorCode == ERR_NONE);
293 }
294
295 /**
296 * @tc.name: GetHceServiceIface001
297 * @tc.desc: Test NfcController GetHceServiceIface.
298 * @tc.type: FUNC
299 */
300 HWTEST_F(NfcControllerTest, GetHceServiceIface001, TestSize.Level1)
301 {
302 NfcController ctrl = NfcController::GetInstance();
303 int32_t res = 0;
304 ctrl.GetHceServiceIface(res);
305 ErrorCode errorCode = ctrl.RegNdefMsgCb(nullptr);
306 ASSERT_TRUE(errorCode == ERR_NONE);
307 }
308 }
309 }
310 }
311