• 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_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     ctrl.GetTagServiceIface();
191     ErrorCode errorCode = ctrl.UnregListener(TEST_NFC_STATE_CHANGE);
192     ASSERT_TRUE(errorCode == ErrorCode::ERR_NONE);
193 }
194 
195 /**
196  * @tc.name: NfcControllerImpl001
197  * @tc.desc: Test NfcControllerImpl.
198  * @tc.type: FUNC
199  */
200 HWTEST_F(NfcControllerTest, NfcControllerImpl001, TestSize.Level1)
201 {
202     std::weak_ptr<NFC::NfcService> nfcService;
203     sptr<NFC::NfcControllerImpl> impl = new NFC::NfcControllerImpl(nfcService);
204     ASSERT_TRUE(impl->GetState() == ErrorCode::ERR_NFC_PARAMETERS);
205 
206     ASSERT_TRUE(impl->TurnOn() == ErrorCode::ERR_NFC_PARAMETERS);
207 
208     ASSERT_TRUE(impl->TurnOff() == ErrorCode::ERR_NFC_PARAMETERS);
209 
210     bool isOpen = false;
211     ASSERT_TRUE(impl->IsNfcOpen(isOpen) == ErrorCode::ERR_NFC_PARAMETERS);
212 
213     ASSERT_TRUE(impl->RegisterCallBack(nullptr, "", 0) == ErrorCode::ERR_NFC_PARAMETERS);
214 
215     ASSERT_TRUE(impl->UnRegisterCallBack("", 0) == ErrorCode::ERR_NFC_PARAMETERS);
216 
217     ASSERT_TRUE(impl->UnRegisterAllCallBack(0) == ErrorCode::ERR_NFC_PARAMETERS);
218 
219     ASSERT_TRUE(impl->GetTagServiceIface() == nullptr);
220 
221     std::vector<std::u16string> args;
222     ASSERT_TRUE(impl->Dump(0, args) == ErrorCode::ERR_NFC_PARAMETERS);
223     delete impl;
224 
225     // fd = 0 is invalid, so experted return is ERR_NFC_PARAMETERS
226     std::shared_ptr<NFC::NfcService> nfcService2 = std::make_shared<NFC::NfcService>();
227     sptr<NFC::NfcControllerImpl> impl2 = new NFC::NfcControllerImpl(nfcService2);
228     ASSERT_TRUE(impl2->Dump(0, args) == ErrorCode::ERR_NFC_PARAMETERS);
229     delete impl2;
230 }
231 
232 /**
233  * @tc.name: OnRemoteDied001
234  * @tc.desc: Test NfcController OnRemoteDied.
235  * @tc.type: FUNC
236  */
237 HWTEST_F(NfcControllerTest, OnRemoteDied001, TestSize.Level1)
238 {
239     wptr<IRemoteObject> remoteObject = nullptr;
240     NfcController ctrl = NfcController::GetInstance();
241     ctrl.OnRemoteDied(remoteObject);
242     ErrorCode errorCode = ctrl.RegNdefMsgCb(nullptr);
243     ASSERT_TRUE(errorCode == ERR_NONE);
244 }
245 
246 /**
247  * @tc.name: RegNdefMsgCb001
248  * @tc.desc: Test NfcController RegNdefMsgCb.
249  * @tc.type: FUNC
250  */
251 HWTEST_F(NfcControllerTest, RegNdefMsgCb001, TestSize.Level1)
252 {
253     sptr<INdefMsgCallback> callback = nullptr;
254     NfcController ctrl = NfcController::GetInstance();
255     ErrorCode errorCode = ctrl.RegNdefMsgCb(callback);
256     ASSERT_TRUE(errorCode == ERR_NONE);
257 }
258 
259 /**
260  * @tc.name: RegQueryApplicationCb001
261  * @tc.desc: Test NfcController RegQueryApplicationCb.
262  * @tc.type: FUNC
263  */
264 HWTEST_F(NfcControllerTest, RegQueryApplicationCb001, TestSize.Level1)
265 {
266     std::string type = "";
267     QueryApplicationByVendor tagCallback = nullptr;
268     QueryHceAppByVendor hceCallback = nullptr;
269     NfcController ctrl = NfcController::GetInstance();
270     ErrorCode errorCode = ctrl.RegQueryApplicationCb(type, tagCallback, hceCallback);
271     ASSERT_TRUE(errorCode == ERR_NONE);
272 }
273 
274 /**
275  * @tc.name: RegCardEmulationNotifyCb001
276  * @tc.desc: Test NfcController RegCardEmulationNotifyCb.
277  * @tc.type: FUNC
278  */
279 HWTEST_F(NfcControllerTest, RegCardEmulationNotifyCb001, TestSize.Level1)
280 {
281     OnCardEmulationNotifyCb callback = nullptr;
282     NfcController ctrl = NfcController::GetInstance();
283     ErrorCode errorCode = ctrl.RegCardEmulationNotifyCb(callback);
284     ASSERT_TRUE(errorCode == ERR_NONE);
285 }
286 
287 /**
288  * @tc.name: NotifyEventStatus001
289  * @tc.desc: Test NfcController NotifyEventStatus.
290  * @tc.type: FUNC
291  */
292 HWTEST_F(NfcControllerTest, NotifyEventStatus001, TestSize.Level1)
293 {
294     int eventType = 0;
295     int arg1 = 0;
296     std::string arg2 = "";
297     NfcController ctrl = NfcController::GetInstance();
298     ErrorCode errorCode = ctrl.NotifyEventStatus(eventType, arg1, arg2);
299     ASSERT_TRUE(errorCode == ERR_NONE);
300 }
301 
302 /**
303  * @tc.name: GetHceServiceIface001
304  * @tc.desc: Test NfcController GetHceServiceIface.
305  * @tc.type: FUNC
306  */
307 HWTEST_F(NfcControllerTest, GetHceServiceIface001, TestSize.Level1)
308 {
309     NfcController ctrl = NfcController::GetInstance();
310     ctrl.GetHceServiceIface();
311     ErrorCode errorCode = ctrl.RegNdefMsgCb(nullptr);
312     ASSERT_TRUE(errorCode == ERR_NONE);
313 }
314 }
315 }
316 }
317