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
16 #define private public
17 #define protected public
18
19 #include <gtest/gtest.h>
20 #include <thread>
21 #include "app_data_parser.h"
22 #include "nfc_sdk_common.h"
23 #include "nfc_notification_publisher.h"
24 #include "nfc_param_util.h"
25 #include "nfc_data_share_impl.h"
26
27 namespace OHOS {
28 namespace NFC {
29 namespace TEST {
30 using namespace testing::ext;
31 using namespace OHOS::NFC;
32 using namespace OHOS::NFC::TAG;
33 class AppDataParserTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 public:
40 static constexpr const auto TECH_MASK = 4;
41 };
42
SetUpTestCase()43 void AppDataParserTest::SetUpTestCase()
44 {
45 std::cout << " SetUpTestCase AppDataParserTest." << std::endl;
46 }
47
TearDownTestCase()48 void AppDataParserTest::TearDownTestCase()
49 {
50 std::cout << " TearDownTestCase AppDataParserTest." << std::endl;
51 }
52
SetUp()53 void AppDataParserTest::SetUp()
54 {
55 std::cout << " SetUp AppDataParserTest." << std::endl;
56 }
57
TearDown()58 void AppDataParserTest::TearDown()
59 {
60 std::cout << " TearDown AppDataParserTest." << std::endl;
61 }
62
63 /**
64 * @tc.name: GetTechMask001
65 * @tc.desc: Test AppDataParser GetTechMask.
66 * @tc.type: FUNC
67 */
68 HWTEST_F(AppDataParserTest, GetTechMask001, TestSize.Level1)
69 {
70 AAFwk::Want want;
71 want.SetAction(KITS::ACTION_TAG_FOUND);
72 EventFwk::CommonEventData data;
73 data.SetWant(want);
74 const std::shared_ptr<EventFwk::CommonEventData> mdata =
75 std::make_shared<EventFwk::CommonEventData>(data);
76 AppDataParser parser = AppDataParser::GetInstance();
77 parser.HandleAppAddOrChangedEvent(nullptr);
78 parser.HandleAppAddOrChangedEvent(mdata);
79
80 parser.HandleAppRemovedEvent(nullptr);
81 parser.HandleAppRemovedEvent(mdata);
82
83 parser.InitAppList();
84
85 // no given tag technologies
86 std::vector<int> discTechList;
87 ASSERT_TRUE(parser.GetDispatchTagAppsByTech(discTechList).size() == 0);
88 }
89 /**
90 * @tc.name: GetTechMask002
91 * @tc.desc: Test AppDataParser GetTechMask.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(AppDataParserTest, GetTechMask002, TestSize.Level1)
95 {
96 AppExecFwk::ElementName element;
97 element.SetBundleName(KITS::ACTION_TAG_FOUND);
98 AAFwk::Want want;
99 want.SetElement(element);
100 EventFwk::CommonEventData data;
101 data.SetWant(want);
102 const std::shared_ptr<EventFwk::CommonEventData> mdata =
103 std::make_shared<EventFwk::CommonEventData>(data);
104 AppDataParser parser = AppDataParser::GetInstance();
105 parser.HandleAppAddOrChangedEvent(mdata);
106
107 parser.HandleAppRemovedEvent(mdata);
108
109 std::vector<int> discTechList;
110 // no app installed, or has app installed to matched with the given tag technologies.
111 discTechList.push_back(static_cast<int>(KITS::TagTechnology::NFC_A_TECH));
112 discTechList.push_back(static_cast<int>(KITS::TagTechnology::NFC_ISODEP_TECH));
113 ASSERT_TRUE(parser.GetDispatchTagAppsByTech(discTechList).size() >= 0);
114 }
115 /**
116 * @tc.name: IsBundleInstalled001
117 * @tc.desc: Test AppDataParser IsBundleInstalled.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(AppDataParserTest, IsBundleInstalled001, TestSize.Level1)
121 {
122 std::string bundleName = "test";
123 AppDataParser parser = AppDataParser::GetInstance();
124 bool ret = parser.IsBundleInstalled(bundleName);
125 ASSERT_TRUE(!ret);
126 }
127
128 /**
129 * @tc.name: GetBundleInfo001
130 * @tc.desc: Test AppDataParser GetBundleInfo.
131 * @tc.type: FUNC
132 */
133 HWTEST_F(AppDataParserTest, GetBundleInfo001, TestSize.Level1)
134 {
135 AppExecFwk::BundleInfo bundleInfo;
136 std::string bundleName = "";
137 AppDataParser parser = AppDataParser::GetInstance();
138 bool ret = parser.GetBundleInfo(bundleInfo, bundleName);
139 ASSERT_TRUE(!ret);
140 }
141
142 /**
143 * @tc.name: GetBundleInfo002
144 * @tc.desc: Test AppDataParser GetBundleInfo.
145 * @tc.type: FUNC
146 */
147 HWTEST_F(AppDataParserTest, GetBundleInfo002, TestSize.Level1)
148 {
149 AppExecFwk::BundleInfo bundleInfo;
150 std::string bundleName = "test";
151 AppDataParser parser = AppDataParser::GetInstance();
152 bool ret = parser.GetBundleInfo(bundleInfo, bundleName);
153 ASSERT_TRUE(!ret);
154 }
155
156 /**
157 * @tc.name: IsHceApp001
158 * @tc.desc: Test AppDataParser IsHceApp.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(AppDataParserTest, IsHceApp001, TestSize.Level1)
162 {
163 ElementName elementName;
164 AppDataParser parser = AppDataParser::GetInstance();
165 bool ret = parser.IsHceApp(elementName);
166 ASSERT_TRUE(!ret);
167 }
168
169 /**
170 * @tc.name: GetBundleNameByUid001
171 * @tc.desc: Test AppDataParser GetBundleNameByUid.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(AppDataParserTest, GetBundleNameByUid001, TestSize.Level1)
175 {
176 uint32_t uid = 0;
177 AppDataParser parser = AppDataParser::GetInstance();
178 std::string ret = parser.GetBundleNameByUid(uid);
179 ASSERT_TRUE(ret == "");
180 }
181
182 /**
183 * @tc.name: RegNotificationCallback001
184 * @tc.desc: Test AppDataParser RegNotificationCallback.
185 * @tc.type: FUNC
186 */
187 HWTEST_F(AppDataParserTest, RegNotificationCallback001, TestSize.Level1)
188 {
189 std::weak_ptr<NfcService> service;
190 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
191 uint32_t uid = 0;
192 AppDataParser parser = AppDataParser::GetInstance();
193 std::string ret = parser.GetBundleNameByUid(uid);
194 ASSERT_TRUE(ret == "");
195 }
196
197 /**
198 * @tc.name: RegNotificationCallback002
199 * @tc.desc: Test AppDataParser RegNotificationCallback.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(AppDataParserTest, RegNotificationCallback002, TestSize.Level1)
203 {
204 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
205 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
206 uint32_t uid = 0;
207 AppDataParser parser = AppDataParser::GetInstance();
208 std::string ret = parser.GetBundleNameByUid(uid);
209 ASSERT_TRUE(ret == "");
210 }
211
212 /**
213 * @tc.name: RegNotificationCallback003
214 * @tc.desc: Test AppDataParser RegNotificationCallback.
215 * @tc.type: FUNC
216 */
217 HWTEST_F(AppDataParserTest, RegNotificationCallback003, TestSize.Level1)
218 {
219 std::weak_ptr<NfcService> service;
220 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
221 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
222 uint32_t uid = 0;
223 AppDataParser parser = AppDataParser::GetInstance();
224 std::string ret = parser.GetBundleNameByUid(uid);
225 ASSERT_TRUE(ret == "");
226 }
227
228 /**
229 * @tc.name: RegNotificationCallback004
230 * @tc.desc: Test AppDataParser RegNotificationCallback.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(AppDataParserTest, RegNotificationCallback004, TestSize.Level1)
234 {
235 std::shared_ptr<NfcService> service = std::make_shared<NfcService>();
236 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
237 NfcNotificationPublisher::GetInstance().RegNotificationCallback(service);
238 uint32_t uid = 0;
239 AppDataParser parser = AppDataParser::GetInstance();
240 std::string ret = parser.GetBundleNameByUid(uid);
241 ASSERT_TRUE(ret == "");
242 }
243
244 /**
245 * @tc.name: IsNfcNtfDisabled001
246 * @tc.desc: Test IsNfcNtfDisabled.
247 * @tc.type: FUNC
248 */
249 HWTEST_F(AppDataParserTest, IsNfcNtfDisabled001, TestSize.Level1)
250 {
251 constexpr const char* NFC_NOT_DISTURB_KEYWORD = "settings.nfc.not_disturb";
252 int INVALID_VALUE = -1;
253 const std::string NFC_NOT_DISTURB_SUFFIX =
254 "/com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=settings.nfc.not_disturb";
255 const std::string NFC_NOT_DISTURB_PREFIX = "datashare://";
256 const std::string NFC_DATA_URI_NOT_DISTURB = NFC_NOT_DISTURB_PREFIX + NFC_NOT_DISTURB_SUFFIX;
257 Uri nfcNotDisturb(NFC_DATA_URI_NOT_DISTURB);
258 auto dataShare = NfcDataShareImpl::GetInstance();
259 ASSERT_TRUE(dataShare != nullptr);
260 dataShare->SetValue(nfcNotDisturb, NFC_NOT_DISTURB_KEYWORD, INVALID_VALUE);
261 ASSERT_TRUE(!NfcNotificationPublisher::GetInstance().IsNfcNtfDisabled());
262 }
263
264 /**
265 * @tc.name: UpdateNfcStateToParam001
266 * @tc.desc: Test AppDataParser UpdateNfcStateToParam.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(AppDataParserTest, UpdateNfcStateToParam001, TestSize.Level1)
270 {
271 int newState = 0;
272 std::shared_ptr<NfcParamUtil> nfcParamUtil = std::make_shared<NfcParamUtil>();
273 nfcParamUtil->UpdateNfcStateToParam(newState);
274 uint32_t uid = 0;
275 AppDataParser parser = AppDataParser::GetInstance();
276 std::string ret = parser.GetBundleNameByUid(uid);
277 ASSERT_TRUE(ret == "");
278 }
279 }
280 }
281 }