• 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 #include <gtest/gtest.h>
16 #include <thread>
17 
18 #include "ndef_msg_callback_stub.h"
19 #include "nfc_controller_callback_stub.h"
20 #include "nfc_controller_impl.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;
29 class NfcControllerImplTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
37 class INfcControllerCallbackImpl : public INfcControllerCallback {
38 public:
INfcControllerCallbackImpl()39     INfcControllerCallbackImpl() {}
40 
~INfcControllerCallbackImpl()41     virtual ~INfcControllerCallbackImpl() {}
42 
43 public:
OnNfcStateChanged(int nfcState)44     void OnNfcStateChanged(int nfcState) override
45     {
46     }
47 
AsObject()48     OHOS::sptr<OHOS::IRemoteObject> AsObject() override
49     {
50         return nullptr;
51     }
52 };
53 
SetUpTestCase()54 void NfcControllerImplTest::SetUpTestCase()
55 {
56     std::cout << " SetUpTestCase NfcControllerImplTest." << std::endl;
57 }
58 
TearDownTestCase()59 void NfcControllerImplTest::TearDownTestCase()
60 {
61     std::cout << " TearDownTestCase NfcControllerImplTest." << std::endl;
62 }
63 
SetUp()64 void NfcControllerImplTest::SetUp()
65 {
66     std::cout << " SetUp NfcControllerImplTest." << std::endl;
67 }
68 
TearDown()69 void NfcControllerImplTest::TearDown()
70 {
71     std::cout << " TearDown NfcControllerImplTest." << std::endl;
72 }
73 
74 /**
75  * @tc.name: GetState001
76  * @tc.desc: Test NfcControllerImplTest GetState.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(NfcControllerImplTest, GetState001, TestSize.Level1)
80 {
81     std::shared_ptr<NfcService> nfcService = nullptr;
82     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
83     int nfcState = 1;
84     ErrCode errCode = nfcControllerImpl->GetState(nfcState);
85     ASSERT_TRUE(errCode == KITS::ERR_NFC_PARAMETERS);
86 }
87 
88 /**
89  * @tc.name: GetState002
90  * @tc.desc: Test NfcControllerImplTest GetState.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(NfcControllerImplTest, GetState002, TestSize.Level1)
94 {
95     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
96     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
97     int nfcState = 1;
98     ErrCode errCode = nfcControllerImpl->GetState(nfcState);
99     ASSERT_TRUE(errCode == KITS::ERR_NONE);
100 }
101 
102 /**
103  * @tc.name: TurnOn001
104  * @tc.desc: Test NfcControllerImplTest TurnOn.
105  * @tc.type: FUNC
106  */
107 HWTEST_F(NfcControllerImplTest, TurnOn001, TestSize.Level1)
108 {
109     std::shared_ptr<NfcService> nfcService = nullptr;
110     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
111     ErrCode turnOn = nfcControllerImpl->TurnOn();
112     ASSERT_TRUE(turnOn == KITS::ERR_NFC_PARAMETERS);
113 }
114 
115 /**
116  * @tc.name: TurnOn002
117  * @tc.desc: Test NfcControllerImplTest TurnOn.
118  * @tc.type: FUNC
119  */
120 HWTEST_F(NfcControllerImplTest, TurnOn002, TestSize.Level1)
121 {
122     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
123     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
124     ErrCode turnOn = nfcControllerImpl->TurnOn();
125     InfoLog("TurnOn002, turnOn = %{public}d", turnOn);
126     ASSERT_TRUE(turnOn == KITS::ERR_NFC_STATE_INVALID);
127 }
128 
129 /**
130  * @tc.name: TurnOff001
131  * @tc.desc: Test NfcControllerImplTest TurnOff.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(NfcControllerImplTest, TurnOff001, TestSize.Level1)
135 {
136     std::shared_ptr<NfcService> nfcService = nullptr;
137     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
138     ErrCode turnOff = nfcControllerImpl->TurnOff();
139     ASSERT_TRUE(turnOff == KITS::ERR_NFC_PARAMETERS);
140 }
141 
142 /**
143  * @tc.name: TurnOff002
144  * @tc.desc: Test NfcControllerImplTest TurnOff.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(NfcControllerImplTest, TurnOff002, TestSize.Level1)
148 {
149     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
150     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
151     ErrCode turnOff = nfcControllerImpl->TurnOff();
152     ASSERT_TRUE(turnOff == KITS::ERR_NFC_STATE_INVALID);
153 }
154 
155 /**
156  * @tc.name: RegisterCallBack001
157  * @tc.desc: Test NfcControllerImplTest RegisterCallBack.
158  * @tc.type: FUNC
159  */
160 HWTEST_F(NfcControllerImplTest, RegisterCallBack001, TestSize.Level1)
161 {
162     sptr<INfcControllerCallback> callback = nullptr;
163     std::string type = "";
164     std::shared_ptr<NfcService> nfcService = nullptr;
165     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
166     nfcControllerImpl->CallbackEnter(101);
167     nfcControllerImpl->CallbackExit(101, 0);
168     ErrCode error = nfcControllerImpl->RegisterNfcStatusCallBack(callback, type);
169     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
170 }
171 
172 /**
173  * @tc.name: RegisterCallBack002
174  * @tc.desc: Test NfcControllerImplTest RegisterCallBack.
175  * @tc.type: FUNC
176  */
177 HWTEST_F(NfcControllerImplTest, RegisterCallBack002, TestSize.Level1)
178 {
179     sptr<NfcControllerCallBackStub> callback =
180         sptr<NfcControllerCallBackStub>(new NfcControllerCallBackStub());
181     std::string type = "";
182     std::shared_ptr<NfcService> nfcService = nullptr;
183     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
184     ErrCode error = nfcControllerImpl->RegisterNfcStatusCallBack(callback, type);
185     sleep(1);
186     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
187 }
188 
189 /**
190  * @tc.name: UnRegisterCallBack001
191  * @tc.desc: Test NfcControllerImplTest UnRegisterCallBack.
192  * @tc.type: FUNC
193  */
194 HWTEST_F(NfcControllerImplTest, UnRegisterCallBack001, TestSize.Level1)
195 {
196     std::string type = "";
197     std::shared_ptr<NfcService> nfcService = nullptr;
198     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
199     ErrCode error = nfcControllerImpl->UnregisterNfcStatusCallBack(type);
200     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
201 }
202 
203 /**
204  * @tc.name: UnRegisterCallBack002
205  * @tc.desc: Test NfcControllerImplTest UnRegisterCallBack.
206  * @tc.type: FUNC
207  */
208 HWTEST_F(NfcControllerImplTest, UnRegisterCallBack002, TestSize.Level1)
209 {
210     std::string type = "";
211     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
212     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
213     ErrCode error = nfcControllerImpl->UnregisterNfcStatusCallBack(type);
214     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
215 }
216 
217 /**
218  * @tc.name: UnRegisterAllCallBack001
219  * @tc.desc: Test NfcControllerImplTest UnRegisterAllCallBack.
220  * @tc.type: FUNC
221  */
222 HWTEST_F(NfcControllerImplTest, UnRegisterAllCallBack001, TestSize.Level1)
223 {
224     Security::AccessToken::AccessTokenID callerToken = 0;
225     std::shared_ptr<NfcService> nfcService = nullptr;
226     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
227     ErrCode error = nfcControllerImpl->UnRegisterAllCallBack(callerToken);
228     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
229 }
230 
231 /**
232  * @tc.name: UnRegisterAllCallBack002
233  * @tc.desc: Test NfcControllerImplTest UnRegisterAllCallBack.
234  * @tc.type: FUNC
235  */
236 HWTEST_F(NfcControllerImplTest, UnRegisterAllCallBack002, TestSize.Level1)
237 {
238     Security::AccessToken::AccessTokenID callerToken = 0;
239     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
240     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
241     ErrCode error = nfcControllerImpl->UnRegisterAllCallBack(callerToken);
242     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
243 }
244 
245 /**
246  * @tc.name: GetTagServiceIface001
247  * @tc.desc: Test NfcControllerImplTest GetTagServiceIface.
248  * @tc.type: FUNC
249  */
250 HWTEST_F(NfcControllerImplTest, GetTagServiceIface001, TestSize.Level1)
251 {
252     std::shared_ptr<NfcService> nfcService = nullptr;
253     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
254     sptr<IRemoteObject> iRemoteObject = nullptr;
255     nfcControllerImpl->GetTagServiceIface(iRemoteObject);
256     ASSERT_TRUE(iRemoteObject == nullptr);
257 }
258 
259 /**
260  * @tc.name: GetTagServiceIface002
261  * @tc.desc: Test NfcControllerImplTest GetTagServiceIface.
262  * @tc.type: FUNC
263  */
264 HWTEST_F(NfcControllerImplTest, GetTagServiceIface002, TestSize.Level1)
265 {
266     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
267     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
268     sptr<IRemoteObject> iRemoteObject = nullptr;
269     nfcControllerImpl->GetTagServiceIface(iRemoteObject);
270     ASSERT_TRUE(iRemoteObject == nullptr);
271 }
272 
273 /**
274  * @tc.name: RegNdefMsgCallback001
275  * @tc.desc: Test NfcControllerImplTest RegNdefMsgCallback.
276  * @tc.type: FUNC
277  */
278 HWTEST_F(NfcControllerImplTest, RegNdefMsgCallback001, TestSize.Level1)
279 {
280     sptr<INdefMsgCallback> callback = nullptr;
281     std::shared_ptr<NfcService> nfcService = nullptr;
282     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
283     ErrCode error = nfcControllerImpl->RegNdefMsgCb(callback);
284     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
285 }
286 
287 /**
288  * @tc.name: RegNdefMsgCallback002
289  * @tc.desc: Test NfcControllerImplTest RegNdefMsgCallback.
290  * @tc.type: FUNC
291  */
292 HWTEST_F(NfcControllerImplTest, RegNdefMsgCallback002, TestSize.Level1)
293 {
294     sptr<NdefMsgCallbackStub> callback = sptr<NdefMsgCallbackStub>(new NdefMsgCallbackStub());
295     std::shared_ptr<NfcService> nfcService = nullptr;
296     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
297     ErrCode error = nfcControllerImpl->RegNdefMsgCb(callback);
298     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
299 }
300 
301 /**
302  * @tc.name: RegNdefMsgCallback003
303  * @tc.desc: Test NfcControllerImplTest RegNdefMsgCallback.
304  * @tc.type: FUNC
305  */
306 HWTEST_F(NfcControllerImplTest, RegNdefMsgCallback003, TestSize.Level1)
307 {
308     sptr<NdefMsgCallbackStub> callback = sptr<NdefMsgCallbackStub>(new NdefMsgCallbackStub());
309     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
310     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
311     ErrCode error = nfcControllerImpl->RegNdefMsgCb(callback);
312     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
313 }
314 
315 #ifdef VENDOR_APPLICATIONS_ENABLED
316 /**
317  * @tc.name: RegQueryApplicationCb001
318  * @tc.desc: Test NfcControllerImplTest RegQueryApplicationCb.
319  * @tc.type: FUNC
320  */
321 HWTEST_F(NfcControllerImplTest, RegQueryApplicationCb001, TestSize.Level1)
322 {
323     sptr<IQueryAppInfoCallback> callback = nullptr;
324     std::shared_ptr<NfcService> nfcService = nullptr;
325     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
326     ErrCode error = nfcControllerImpl->RegQueryApplicationCb(callback);
327     ASSERT_TRUE(error == KITS::ERR_NONE);
328 }
329 
330 /**
331  * @tc.name: RegCardEmulationNotifyCb001
332  * @tc.desc: Test NfcControllerImplTest RegCardEmulationNotifyCb.
333  * @tc.type: FUNC
334  */
335 HWTEST_F(NfcControllerImplTest, RegCardEmulationNotifyCb001, TestSize.Level1)
336 {
337     sptr<IOnCardEmulationNotifyCb> callback = nullptr;
338     std::shared_ptr<NfcService> nfcService = nullptr;
339     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
340     ErrCode error = nfcControllerImpl->RegCardEmulationNotifyCb(callback);
341     ASSERT_TRUE(error == KITS::ERR_NONE);
342 }
343 
344 /**
345  * @tc.name: NotifyEventStatus001
346  * @tc.desc: Test NfcControllerImplTest NotifyEventStatus.
347  * @tc.type: FUNC
348  */
349 HWTEST_F(NfcControllerImplTest, NotifyEventStatus001, TestSize.Level1)
350 {
351     std::shared_ptr<NfcService> nfcService = nullptr;
352     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
353     ErrCode error = nfcControllerImpl->NotifyEventStatus(0, 0, "");
354     ASSERT_TRUE(error == KITS::ERR_NFC_PARAMETERS);
355 }
356 #endif
357 
358 /**
359  * @tc.name: GetHceServiceIface001
360  * @tc.desc: Test NfcControllerImplTest GetHceServiceIface.
361  * @tc.type: FUNC
362  */
363 HWTEST_F(NfcControllerImplTest, GetHceServiceIface001, TestSize.Level1)
364 {
365     std::shared_ptr<NfcService> nfcService = nullptr;
366     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
367     sptr<IRemoteObject> iRemoteObject = nullptr;
368     nfcControllerImpl->GetHceServiceIface(iRemoteObject);
369     ASSERT_TRUE(iRemoteObject == nullptr);
370 }
371 
372 /**
373  * @tc.name: GetHceServiceIface002
374  * @tc.desc: Test NfcControllerImplTest GetHceServiceIface.
375  * @tc.type: FUNC
376  */
377 HWTEST_F(NfcControllerImplTest, GetHceServiceIface002, TestSize.Level1)
378 {
379     std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
380     std::shared_ptr<NfcControllerImpl> nfcControllerImpl = std::make_shared<NfcControllerImpl>(nfcService);
381     sptr<IRemoteObject> iRemoteObject = nullptr;
382     nfcControllerImpl->GetHceServiceIface(iRemoteObject);
383     ASSERT_TRUE(iRemoteObject == nullptr);
384 }
385 }
386 }
387 }