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