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 "nfc_service.h"
19 #include <unistd.h>
20 #include "app_data_parser.h"
21 #include "nfc_event_handler.h"
22 #include "nfc_controller.h"
23 #include "nfc_polling_params.h"
24 #include "nfc_sdk_common.h"
25 #include "nfc_watch_dog.h"
26 #include "want.h"
27 #include "nfc_preferences.h"
28 #include "tag_session.h"
29 #include "nci_native_selector.h"
30
31 namespace OHOS {
32 namespace NFC {
33 namespace TEST {
34 using namespace testing::ext;
35 using namespace OHOS::NFC;
36 class NfcPollingManagerTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42
43 std::shared_ptr<NfcService> nfcService_ {};
44 };
45
SetUpTestCase()46 void NfcPollingManagerTest::SetUpTestCase()
47 {
48 std::cout << " SetUpTestCase NfcPollingManagerTest." << std::endl;
49 }
50
TearDownTestCase()51 void NfcPollingManagerTest::TearDownTestCase()
52 {
53 std::cout << " TearDownTestCase NfcPollingManagerTest." << std::endl;
54 }
55
SetUp()56 void NfcPollingManagerTest::SetUp()
57 {
58 std::cout << " SetUp NfcPollingManagerTest." << std::endl;
59 }
60
TearDown()61 void NfcPollingManagerTest::TearDown()
62 {
63 std::cout << " TearDown NfcPollingManagerTest." << std::endl;
64 }
65
66 /**
67 * @tc.name: IsForegroundEnabled001
68 * @tc.desc: Test NfcPollingManager IsForegroundEnabled.
69 * @tc.type: FUNC
70 */
71 HWTEST_F(NfcPollingManagerTest, IsForegroundEnabled001, TestSize.Level1)
72 {
73 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
74 service->Initialize();
75 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
76 bool enable = nfcPollingManager.lock()->IsForegroundEnabled();
77 ASSERT_TRUE(enable == false);
78 }
79 /**
80 * @tc.name: DisableForegroundByDeathRcpt001
81 * @tc.desc: Test NfcPollingManager DisableForegroundByDeathRcpt.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(NfcPollingManagerTest, DisableForegroundByDeathRcpt001, TestSize.Level1)
85 {
86 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
87 service->Initialize();
88 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
89 bool disable = nfcPollingManager.lock()->DisableForegroundByDeathRcpt();
90 ASSERT_TRUE(disable == true);
91 }
92 /**
93 * @tc.name: DisableForegroundDispatch001
94 * @tc.desc: Test NfcPollingManager DisableForegroundDispatch.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(NfcPollingManagerTest, DisableForegroundDispatch001, TestSize.Level1)
98 {
99 AppExecFwk::ElementName element;
100 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
101 service->Initialize();
102 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
103 bool disable = nfcPollingManager.lock()->DisableForegroundDispatch(element);
104 ASSERT_TRUE(disable == true);
105 }
106 /**
107 * @tc.name: DisableForegroundDispatch002
108 * @tc.desc: Test NfcPollingManager DisableForegroundDispatch.
109 * @tc.type: FUNC
110 */
111 HWTEST_F(NfcPollingManagerTest, DisableForegroundDispatch002, TestSize.Level1)
112 {
113 AppExecFwk::ElementName element;
114 std::weak_ptr<NfcService> nfcService;
115 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
116 std::weak_ptr<NCI::INciTagInterface> nciTagProxy;
117 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
118 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
119 bool disable = nfcPollingManager->DisableForegroundDispatch(element);
120 ASSERT_TRUE(disable);
121 }
122 /**
123 * @tc.name: EnableForegroundDispatch001
124 * @tc.desc: Test NfcPollingManager EnableForegroundDispatch.
125 * @tc.type: FUNC
126 */
127 HWTEST_F(NfcPollingManagerTest, EnableForegroundDispatch001, TestSize.Level1)
128 {
129 AppExecFwk::ElementName element;
130 std::vector<uint32_t> discTech = {1, 2, 4, 5, 10};
131 const sptr<KITS::IForegroundCallback> callback = nullptr;
132 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
133 service->Initialize();
134 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
135 bool enable = nfcPollingManager.lock()->EnableForegroundDispatch(element, discTech, callback);
136 ASSERT_TRUE(enable == false);
137 }
138 /**
139 * @tc.name: EnableForegroundDispatch002
140 * @tc.desc: Test NfcPollingManager EnableForegroundDispatch.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(NfcPollingManagerTest, EnableForegroundDispatch002, TestSize.Level1)
144 {
145 AppExecFwk::ElementName element;
146 std::vector<uint32_t> discTech = {1, 2, 4, 5, 10};
147 const sptr<KITS::IForegroundCallback> callback = nullptr;
148 std::weak_ptr<NfcService> nfcService;
149 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
150 std::weak_ptr<NCI::INciTagInterface> nciTagProxy;
151 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
152 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
153 bool enable = nfcPollingManager->EnableForegroundDispatch(element, discTech, callback);
154 ASSERT_TRUE(enable == false);
155 }
156 /**
157 * @tc.name: EnableForegroundDispatch003
158 * @tc.desc: Test NfcPollingManager EnableForegroundDispatch.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(NfcPollingManagerTest, EnableForegroundDispatch003, TestSize.Level1)
162 {
163 AppExecFwk::ElementName element;
164 std::vector<uint32_t> discTech = {1, 2, 4, 5, 10};
165 const sptr<KITS::IForegroundCallback> callback = nullptr;
166 std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
167 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
168 std::weak_ptr<NCI::INciTagInterface> nciTagProxy;
169 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
170 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
171 bool enable = nfcPollingManager->EnableForegroundDispatch(element, discTech, callback);
172 ASSERT_TRUE(enable == false);
173 }
174 /**
175 * @tc.name: EnableForegroundDispatch004
176 * @tc.desc: Test NfcPollingManager EnableForegroundDispatch.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(NfcPollingManagerTest, EnableForegroundDispatch004, TestSize.Level1)
180 {
181 AppExecFwk::ElementName element;
182 std::vector<uint32_t> discTech = {1, 2, 4, 5, 10};
183 const sptr<KITS::IForegroundCallback> callback = nullptr;
184 std::shared_ptr<NfcService> nfcService = std::make_shared<NfcService>();
185 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
186 std::weak_ptr<NCI::INciTagInterface> nciTagProxy;
187 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
188 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
189 bool enable = nfcPollingManager->EnableForegroundDispatch(element, discTech, callback, true);
190 ASSERT_TRUE(enable == false);
191 }
192 /**
193 * @tc.name: GetForegroundData001
194 * @tc.desc: Test NfcPollingManager GetForegroundData.
195 * @tc.type: FUNC
196 */
197 HWTEST_F(NfcPollingManagerTest, GetForegroundData001, TestSize.Level1)
198 {
199 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
200 service->Initialize();
201 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
202 std::shared_ptr<NfcPollingManager::ForegroundRegistryData> data = nfcPollingManager.lock()->GetForegroundData();
203 ASSERT_TRUE(data != nullptr);
204 }
205
206 /**
207 * @tc.name: GetPollingParameters001
208 * @tc.desc: Test NfcPollingManager GetPollingParameters.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(NfcPollingManagerTest, GetPollingParameters001, TestSize.Level1)
212 {
213 int screenState = 0;
214 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
215 service->Initialize();
216 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
217 std::shared_ptr<NfcPollingParams> nfcPollingParams = nfcPollingManager.lock()->GetPollingParameters(screenState);
218 ASSERT_TRUE(nfcPollingParams != nullptr);
219 }
220
221 /**
222 * @tc.name: HandleScreenChanged001
223 * @tc.desc: Test NfcPollingManager HandleScreenChanged.
224 * @tc.type: FUNC
225 */
226 HWTEST_F(NfcPollingManagerTest, HandleScreenChanged001, TestSize.Level1)
227 {
228 int screenState = 1;
229 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
230 service->Initialize();
231 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
232 nfcPollingManager.lock()->HandleScreenChanged(screenState);
233 std::shared_ptr<NfcPollingParams> nfcPollingParams = nfcPollingManager.lock()->GetPollingParameters(screenState);
234 ASSERT_TRUE(nfcPollingParams != nullptr);
235 }
236
237 /**
238 * @tc.name: HandlePackageUpdated001
239 * @tc.desc: Test NfcPollingManager HandlePackageUpdated.
240 * @tc.type: FUNC
241 */
242 HWTEST_F(NfcPollingManagerTest, HandlePackageUpdated001, TestSize.Level1)
243 {
244 int screenState = 1;
245 std::shared_ptr<EventFwk::CommonEventData> data = std::make_shared<EventFwk::CommonEventData>();
246 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
247 service->Initialize();
248 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
249 nfcPollingManager.lock()->HandlePackageUpdated(data);
250 std::shared_ptr<NfcPollingParams> nfcPollingParams = nfcPollingManager.lock()->GetPollingParameters(screenState);
251 ASSERT_TRUE(nfcPollingParams != nullptr);
252 }
253
254 /**
255 * @tc.name: HandlePackageUpdated002
256 * @tc.desc: Test NfcPollingManager HandlePackageUpdated.
257 * @tc.type: FUNC
258 */
259 HWTEST_F(NfcPollingManagerTest, HandlePackageUpdated002, TestSize.Level1)
260 {
261 std::shared_ptr<EventFwk::CommonEventData> data = std::make_shared<EventFwk::CommonEventData>();
262 AAFwk::Want want;
263 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
264 data->SetWant(want);
265 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
266 service->Initialize();
267 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
268 bool res = nfcPollingManager.lock()->HandlePackageUpdated(data);
269 ASSERT_TRUE(!res);
270 }
271
272 /**
273 * @tc.name: HandlePackageUpdated003
274 * @tc.desc: Test NfcPollingManager HandlePackageUpdated.
275 * @tc.type: FUNC
276 */
277 HWTEST_F(NfcPollingManagerTest, HandlePackageUpdated003, TestSize.Level1)
278 {
279 std::shared_ptr<EventFwk::CommonEventData> data = std::make_shared<EventFwk::CommonEventData>();
280 AAFwk::Want want;
281 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
282 data->SetWant(want);
283 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
284 service->Initialize();
285 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
286 bool res = nfcPollingManager.lock()->HandlePackageUpdated(data);
287 ASSERT_TRUE(!res);
288 }
289
290 /**
291 * @tc.name: HandlePackageUpdated004
292 * @tc.desc: Test NfcPollingManager HandlePackageUpdated.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(NfcPollingManagerTest, HandlePackageUpdated004, TestSize.Level1)
296 {
297 std::shared_ptr<EventFwk::CommonEventData> data = std::make_shared<EventFwk::CommonEventData>();
298 AAFwk::Want want;
299 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
300 data->SetWant(want);
301 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
302 service->Initialize();
303 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
304 bool res = nfcPollingManager.lock()->HandlePackageUpdated(data);
305 ASSERT_TRUE(!res);
306 }
307
308 /**
309 * @tc.name: HandlePackageUpdated005
310 * @tc.desc: Test NfcPollingManager HandlePackageUpdated.
311 * @tc.type: FUNC
312 */
313 HWTEST_F(NfcPollingManagerTest, HandlePackageUpdated005, TestSize.Level1)
314 {
315 std::shared_ptr<EventFwk::CommonEventData> data = std::make_shared<EventFwk::CommonEventData>();
316 AAFwk::Want want;
317 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_PRESENT);
318 data->SetWant(want);
319 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
320 service->Initialize();
321 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
322 bool res = nfcPollingManager.lock()->HandlePackageUpdated(data);
323 ASSERT_TRUE(!res);
324 }
325
326 /**
327 * @tc.name: SendTagToForeground001
328 * @tc.desc: Test NfcPollingManager SendTagToForeground.
329 * @tc.type: FUNC
330 */
331 HWTEST_F(NfcPollingManagerTest, SendTagToForeground001, TestSize.Level1)
332 {
333 KITS::TagInfoParcelable* tagInfo = nullptr;
334 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
335 service->Initialize();
336 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
337 nfcPollingManager.lock()->SendTagToForeground(tagInfo);
338 ASSERT_TRUE(service != nullptr);
339 }
340
341 /**
342 * @tc.name: EnableReaderMode001
343 * @tc.desc: Test NfcPollingManager EnableReaderMode.
344 * @tc.type: FUNC
345 */
346 HWTEST_F(NfcPollingManagerTest, EnableReaderMode001, TestSize.Level1)
347 {
348 AppExecFwk::ElementName element;
349 std::vector<uint32_t> discTech;
350 sptr<KITS::IReaderModeCallback> callback = nullptr;
351 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
352 service->Initialize();
353 std::weak_ptr<NFC::NfcPollingManager> nfcPollingManager = service->GetNfcPollingManager();
354 bool res = nfcPollingManager.lock()->EnableReaderMode(element, discTech, callback);
355 ASSERT_TRUE(!res);
356 }
357 /**
358 * @tc.name: EnableReaderMode002
359 * @tc.desc: Test NfcPollingManager EnableReaderMode.
360 * @tc.type: FUNC
361 */
362 HWTEST_F(NfcPollingManagerTest, EnableReaderMode002, TestSize.Level1)
363 {
364 AppExecFwk::ElementName element;
365 std::vector<uint32_t> discTech;
366 sptr<KITS::IReaderModeCallback> callback = nullptr;
367 std::weak_ptr<NfcService> nfcService;
368 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
369 std::weak_ptr<NCI::INciTagInterface> nciTagProxy;
370 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
371 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
372 bool res = nfcPollingManager->EnableReaderMode(element, discTech, callback);
373 ASSERT_TRUE(!res);
374 }
375 /**
376 * @tc.name: EnableReaderMode003
377 * @tc.desc: Test NfcPollingManager EnableReaderMode.
378 * @tc.type: FUNC
379 */
380 HWTEST_F(NfcPollingManagerTest, EnableReaderMode003, TestSize.Level1)
381 {
382 AppExecFwk::ElementName element;
383 std::vector<uint32_t> discTech;
384 sptr<KITS::IReaderModeCallback> callback = nullptr;
385 std::weak_ptr<NfcService> nfcService;
386 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
387 std::shared_ptr<NCI::INciTagInterface> nciTagProxy = NCI::NciNativeSelector::GetInstance().GetNciTagInterface();
388 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
389 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
390 bool res = nfcPollingManager->EnableReaderMode(element, discTech, callback);
391 ASSERT_TRUE(!res);
392 }
393 /**
394 * @tc.name: EnableReaderMode004
395 * @tc.desc: Test NfcPollingManager EnableReaderMode.
396 * @tc.type: FUNC
397 */
398 HWTEST_F(NfcPollingManagerTest, EnableReaderMode004, TestSize.Level1)
399 {
400 AppExecFwk::ElementName element;
401 std::vector<uint32_t> discTech;
402 sptr<KITS::IReaderModeCallback> callback = nullptr;
403 std::weak_ptr<NfcService> nfcService;
404 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy;
405 std::shared_ptr<NCI::INciTagInterface> nciTagProxy = NCI::NciNativeSelector::GetInstance().GetNciTagInterface();
406 std::shared_ptr<NFC::NfcPollingManager> nfcPollingManager =
407 std::make_shared<NFC::NfcPollingManager>(nfcService, nciNfccProxy, nciTagProxy);
408 bool res = nfcPollingManager->EnableReaderMode(element, discTech, callback, true);
409 ASSERT_TRUE(!res);
410 }
411 }
412 }
413 }