1 /*
2 * Copyright (c) 2021 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 #include <gtest/gtest.h>
17
18 #include "ohos/aafwk/content/intent.h"
19 #include "ohos/aafwk/content/intent_filter.h"
20
21 using namespace testing::ext;
22 using namespace OHOS::AAFwk;
23 using OHOS::Parcel;
24
25 static const int LARGE_STR_LEN = 65534;
26 static const int SET_COUNT = 20;
27 static const int LOOP_TEST = 1000;
28
29 class IntentFilterBaseTest : public testing::Test {
30 public:
IntentFilterBaseTest()31 IntentFilterBaseTest() : filter_(nullptr)
32 {}
~IntentFilterBaseTest()33 ~IntentFilterBaseTest()
34 {
35 filter_ = nullptr;
36 }
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41
42 IntentFilter *filter_;
43 void CompareFilter(IntentFilter &filter1, IntentFilter &filter2);
44 void SendParcelTest(IntentFilter &filter);
45 };
46
SetUpTestCase(void)47 void IntentFilterBaseTest::SetUpTestCase(void)
48 {}
49
TearDownTestCase(void)50 void IntentFilterBaseTest::TearDownTestCase(void)
51 {}
52
SetUp(void)53 void IntentFilterBaseTest::SetUp(void)
54 {
55 filter_ = new (std::nothrow) IntentFilter();
56 }
57
TearDown(void)58 void IntentFilterBaseTest::TearDown(void)
59 {
60 delete filter_;
61 filter_ = nullptr;
62 }
63
64 /*
65 * Feature: IntentFilter
66 * Function: SetEntity/GetEntity
67 * SubFunction: NA
68 * FunctionPoints: SetEntity/GetEntity
69 * EnvConditions: NA
70 * CaseDescription: Verify the function when the input string is empty
71 */
72 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_001, TestSize.Level1)
73 {
74 std::string setValue;
75 filter_->SetEntity(setValue);
76 EXPECT_EQ(setValue, filter_->GetEntity());
77 }
78
79 /*
80 * Feature: IntentFilter
81 * Function: SetEntity/GetEntity
82 * SubFunction: NA
83 * FunctionPoints: SetEntity/GetEntity
84 * EnvConditions: NA
85 * CaseDescription: Verify the function when the input string contains special characters
86 */
87 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_002, TestSize.Level1)
88 {
89 std::string setValue("@#¥#3243adsafdf_中文");
90 filter_->SetEntity(setValue);
91 EXPECT_EQ(setValue, filter_->GetEntity());
92 }
93
94 /*
95 * Feature: IntentFilter
96 * Function: SetEntity/GetEntity
97 * SubFunction: NA
98 * FunctionPoints: SetEntity/GetEntity
99 * EnvConditions: NA
100 * CaseDescription: Verify the function when the input string has a long size
101 */
102 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_003, TestSize.Level1)
103 {
104 std::string setValue(LARGE_STR_LEN, 's');
105 filter_->SetEntity(setValue);
106 EXPECT_EQ(setValue, filter_->GetEntity());
107 }
108
109 /*
110 * Feature: IntentFilter
111 * Function: SetEntity/GetEntity
112 * SubFunction: NA
113 * FunctionPoints: SetEntity/GetEntity
114 * EnvConditions: NA
115 * CaseDescription: Verify the function when the input string is overrode
116 */
117 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_004, TestSize.Level1)
118 {
119 std::string setValue1("1234");
120 filter_->SetEntity(setValue1);
121
122 std::string setValue2("abcd");
123 filter_->SetEntity(setValue2);
124
125 EXPECT_EQ(setValue2, filter_->GetEntity());
126 }
127
128 /*
129 * Feature: IntentFilter
130 * Function: SetEntity/GetEntity
131 * SubFunction: NA
132 * FunctionPoints: SetEntity/GetEntity
133 * EnvConditions: NA
134 * CaseDescription: Verify the function when the input string is set 20 times
135 */
136 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_005, TestSize.Level1)
137 {
138 std::string setValue("1234");
139 for (int i = 0; i < SET_COUNT; i++) {
140 filter_->SetEntity(setValue);
141 }
142 EXPECT_EQ(setValue, filter_->GetEntity());
143 }
144
145 /*
146 * Feature: IntentFilter
147 * Function: SetEntity/GetEntity
148 * SubFunction: NA
149 * FunctionPoints: SetEntity/GetEntity
150 * EnvConditions: NA
151 * CaseDescription: Verify the function when the input string is default
152 */
153 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_006, TestSize.Level1)
154 {
155 std::string setValue;
156 EXPECT_EQ(setValue, filter_->GetEntity());
157 }
158
159 /*
160 * Feature: IntentFilter
161 * Function: SetEntity/GetEntity
162 * SubFunction: NA
163 * FunctionPoints: SetEntity/GetEntity
164 * EnvConditions: NA
165 * CaseDescription: Verify the function when the input string contains special characters
166 */
167 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Entity_007, TestSize.Level1)
168 {
169 std::string setValue("@#¥#3243adsafdf_中文");
170 for (int i = 0; i < LOOP_TEST; i++) {
171 filter_->SetEntity(setValue);
172 EXPECT_EQ(setValue, filter_->GetEntity());
173 }
174 }
175
176 /*
177 * Feature: IntentFilter
178 * Function: action
179 * SubFunction: NA
180 * FunctionPoints: action
181 * EnvConditions: NA
182 * CaseDescription: Verify the function when action is not exist
183 */
184 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Action_001, TestSize.Level1)
185 {
186 std::string empty;
187 std::string action = "action.system.test";
188 EXPECT_EQ(0, filter_->CountAction());
189 EXPECT_EQ(false, filter_->HasAction(action));
190 EXPECT_EQ(empty, filter_->GetAction(0));
191
192 filter_->RemoveAction(action);
193 EXPECT_EQ(0, filter_->CountAction());
194 EXPECT_EQ(false, filter_->HasAction(action));
195 }
196
197 /*
198 * Feature: IntentFilter
199 * Function: action
200 * SubFunction: NA
201 * FunctionPoints: action
202 * EnvConditions: NA
203 * CaseDescription: Verify the function when actions are same
204 */
205 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Action_002, TestSize.Level1)
206 {
207 std::string action = "action.system.test";
208 int actionCount = 1;
209
210 for (int i = 0; i < SET_COUNT; i++) {
211 filter_->AddAction(action);
212 }
213
214 EXPECT_EQ(actionCount, filter_->CountAction());
215 EXPECT_EQ(true, filter_->HasAction(action));
216 EXPECT_EQ(action, filter_->GetAction(0));
217
218 filter_->RemoveAction(action);
219 EXPECT_EQ(0, filter_->CountAction());
220 EXPECT_EQ(false, filter_->HasAction(action));
221 }
222
223 /*
224 * Feature: IntentFilter
225 * Function: action
226 * SubFunction: NA
227 * FunctionPoints: action
228 * EnvConditions: NA
229 * CaseDescription: Verify the function when actions are different
230 */
231 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Action_003, TestSize.Level1)
232 {
233 std::string actionPrefix = "action.system.test";
234
235 for (int i = 0; i < SET_COUNT; i++) {
236 std::string action = actionPrefix + std::to_string(i);
237 filter_->AddAction(action);
238 }
239
240 EXPECT_EQ(SET_COUNT, filter_->CountAction());
241 for (int i = 0; i < SET_COUNT; i++) {
242 std::string action = actionPrefix + std::to_string(i);
243 EXPECT_EQ(true, filter_->HasAction(action));
244 EXPECT_EQ(action, filter_->GetAction(i));
245 }
246
247 int remove = SET_COUNT / 2;
248 for (int i = 0; i < remove; i++) {
249 std::string action = actionPrefix + std::to_string(i);
250 filter_->RemoveAction(action);
251 }
252
253 EXPECT_EQ(remove, filter_->CountAction());
254 for (int i = 0; i < remove; i++) {
255 std::string action = actionPrefix + std::to_string(i);
256 EXPECT_EQ(false, filter_->HasAction(action));
257 }
258
259 for (int i = remove; i < SET_COUNT; i++) {
260 std::string action = actionPrefix + std::to_string(i);
261 EXPECT_EQ(true, filter_->HasAction(action));
262 EXPECT_EQ(action, filter_->GetAction(i - remove));
263 }
264 }
265
266 /*
267 * Feature: IntentFilter
268 * Function: action
269 * SubFunction: NA
270 * FunctionPoints: action
271 * EnvConditions: NA
272 * CaseDescription: Verify the function when actions are same
273 */
274 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_Action_004, TestSize.Level1)
275 {
276 std::string action = "action.system.test";
277 int actionCount = 1;
278
279 for (int i = 0; i < LOOP_TEST; i++) {
280 filter_->AddAction(action);
281 EXPECT_EQ(actionCount, filter_->CountAction());
282 EXPECT_EQ(true, filter_->HasAction(action));
283 EXPECT_EQ(action, filter_->GetAction(0));
284
285 filter_->RemoveAction(action);
286 EXPECT_EQ(0, filter_->CountAction());
287 EXPECT_EQ(false, filter_->HasAction(action));
288 }
289 }
290
CompareFilter(IntentFilter & filter1,IntentFilter & filter2)291 void IntentFilterBaseTest::CompareFilter(IntentFilter &filter1, IntentFilter &filter2)
292 {
293 EXPECT_EQ(filter1.GetEntity(), filter2.GetEntity());
294 EXPECT_EQ(filter1.CountAction(), filter2.CountAction());
295
296 int count = filter1.CountAction();
297 for (int i = 0; i < count; i++) {
298 EXPECT_EQ(filter1.GetAction(i), filter2.GetAction(i));
299 }
300 }
301
SendParcelTest(IntentFilter & filter)302 void IntentFilterBaseTest::SendParcelTest(IntentFilter &filter)
303 {
304 Parcel data;
305 bool result;
306
307 result = data.WriteParcelable(&filter);
308 EXPECT_EQ(result, true);
309
310 IntentFilter *filterNew = nullptr;
311 filterNew = data.ReadParcelable<IntentFilter>();
312 EXPECT_NE(filterNew, nullptr);
313
314 if (filterNew) {
315 CompareFilter(filter, *filterNew);
316 delete filterNew;
317 }
318 }
319
320 /*
321 * Feature: IntentFilter
322 * Function: marshall and unmarshall
323 * SubFunction: NA
324 * FunctionPoints: marshall and unmarshall
325 * EnvConditions: NA
326 * CaseDescription: Verify marshall and unmarshall when filter is empty
327 */
328 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_parcelable_001, TestSize.Level0)
329 {
330 SendParcelTest(*filter_);
331 }
332
333 /*
334 * Feature: IntentFilter
335 * Function: marshall and unmarshall
336 * SubFunction: NA
337 * FunctionPoints: marshall and unmarshall
338 * EnvConditions: NA
339 * CaseDescription: Verify marshall and unmarshall when filter has action and entity
340 */
341 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_parcelable_002, TestSize.Level0)
342 {
343 filter_->SetEntity("entity.system.test");
344 filter_->AddAction("action.system.test1");
345 filter_->AddAction("action.system.test2");
346
347 SendParcelTest(*filter_);
348 }
349
350 /*
351 * Feature: IntentFilter
352 * Function: marshall and unmarshall
353 * SubFunction: NA
354 * FunctionPoints: marshall and unmarshall
355 * EnvConditions: NA
356 * CaseDescription: Verify marshall and unmarshall. Pressure test.
357 */
358 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_parcelable_003, TestSize.Level0)
359 {
360 filter_->SetEntity("entity.system.test");
361 filter_->AddAction("action.system.test1");
362 filter_->AddAction("action.system.test2");
363
364 for (int i = 0; i < LOOP_TEST; i++) {
365 SendParcelTest(*filter_);
366 }
367 }
368
369 /*
370 * Feature: IntentFilter
371 * Function: Match
372 * SubFunction: NA
373 * FunctionPoints: Match
374 * EnvConditions: NA
375 * CaseDescription: Verify the function. Pressure test.
376 */
377 HWTEST_F(IntentFilterBaseTest, AaFwk_IntentFilter_match_005, TestSize.Level1)
378 {
379 for (int i = 0; i < LOOP_TEST; i++) {
380 Intent intent;
381 intent.SetAction("action.system.action1");
382 intent.SetEntity("entity.system.entity1");
383
384 filter_->SetEntity("entity.system.entity1");
385 filter_->AddAction("action.system.action1");
386 filter_->AddAction("action.system.action2");
387
388 EXPECT_EQ(true, filter_->Match(intent));
389 }
390 }
391
392 using testFilterMatchType = std::tuple<std::string, std::string, bool>;
393 class IntentFilterMatchTest : public testing::TestWithParam<testFilterMatchType> {
394 public:
IntentFilterMatchTest()395 IntentFilterMatchTest() : filter_(nullptr)
396 {}
~IntentFilterMatchTest()397 ~IntentFilterMatchTest()
398 {
399 filter_ = nullptr;
400 }
401 static void SetUpTestCase(void);
402 static void TearDownTestCase(void);
403 void SetUp();
404 void TearDown();
405 IntentFilter *filter_;
406 };
407
SetUpTestCase(void)408 void IntentFilterMatchTest::SetUpTestCase(void)
409 {}
410
TearDownTestCase(void)411 void IntentFilterMatchTest::TearDownTestCase(void)
412 {}
413
SetUp(void)414 void IntentFilterMatchTest::SetUp(void)
415 {
416 filter_ = new (std::nothrow) IntentFilter();
417 }
418
TearDown(void)419 void IntentFilterMatchTest::TearDown(void)
420 {
421 delete filter_;
422 filter_ = nullptr;
423 }
424
425 /*
426 * Feature: IntentFilter
427 * Function: Match
428 * SubFunction: NA
429 * FunctionPoints: Match
430 * EnvConditions: NA
431 * CaseDescription: Verify whether parameter change.
432 * AaFwk_IntentFilter_match_001
433 * AaFwk_IntentFilter_match_002
434 * AaFwk_IntentFilter_match_003
435 * AaFwk_IntentFilter_match_004
436 */
437
438 HWTEST_P(IntentFilterMatchTest, AaFwk_IntentFilter_match, TestSize.Level0)
439 {
440 std::string filterEntity = "entity.system.entity1";
441 std::string filterAction1 = "action.system.action1";
442 std::string filterAction2 = "action.system.action2";
443 std::string intentEntity = std::get<0>(GetParam());
444 std::string intentAction = std::get<1>(GetParam());
445 bool result = std::get<2>(GetParam());
446
447 filter_->SetEntity(filterEntity);
448 filter_->AddAction(filterAction1);
449 filter_->AddAction(filterAction2);
450
451 Intent intent;
452 intent.SetEntity(intentEntity);
453 intent.SetAction(intentAction);
454
455 EXPECT_EQ(result, filter_->Match(intent));
456 }
457
458 INSTANTIATE_TEST_CASE_P(IntentFilterMatchTestP, IntentFilterMatchTest,
459 testing::Values(testFilterMatchType("entity.system.entityA", "action.system.actionA", false),
460 testFilterMatchType("entity.system.entity1", "action.system.actionA", false),
461 testFilterMatchType("entity.system.entityA", "action.system.action2", false),
462 testFilterMatchType("entity.system.entity1", "action.system.action1", true)));
463