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 applipcable 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 #include "iservice_registry.h"
18 #define private public
19 #include "print_utils.h"
20 #undef private
21 #include "print_constant.h"
22 #include "print_extension_callback_stub.h"
23 #include "print_log.h"
24 #include "print_sync_load_callback.h"
25 #include "system_ability_definition.h"
26 #include "print_json_util.h"
27
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Print {
32 class PrintUtilsTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase(void)40 void PrintUtilsTest::SetUpTestCase(void) {}
41
TearDownTestCase(void)42 void PrintUtilsTest::TearDownTestCase(void) {}
43
SetUp(void)44 void PrintUtilsTest::SetUp(void) {}
45
TearDown(void)46 void PrintUtilsTest::TearDown(void) {}
47
48 /**
49 * @tc.name: PrintUtilsTest_0001
50 * @tc.desc: Verify the toLower function.
51 * @tc.type: FUNC
52 * @tc.require:
53 */
54 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0001_NeedRename, TestSize.Level2)
55 {
56 std::string testResult = "test";
57 std::string result = PrintUtils::ToLower("TEST");
58 EXPECT_EQ(testResult, result);
59 }
60
61 /**
62 * @tc.name: PrintUtilsTest_0002
63 * @tc.desc: Verify the getExtensionId function.
64 * @tc.type: FUNC
65 * @tc.require:
66 */
67 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0002_NeedRename, TestSize.Level2)
68 {
69 std::string gid = "com.sample.ext";
70 std::string testResult = "";
71 std::string result = PrintUtils::GetExtensionId(gid);
72 EXPECT_EQ(testResult, result);
73 }
74
75 /**
76 * @tc.name: PrintUtilsTest_0003
77 * @tc.desc: Verify the getExtensionId function.
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0003_NeedRename, TestSize.Level2)
82 {
83 std::string gid = "com.sample.ext:1234";
84 std::string testResult = "com.sample.ext";
85 std::string result = PrintUtils::GetExtensionId(gid);
86 EXPECT_EQ(testResult, result);
87 }
88
89 /**
90 * @tc.name: PrintUtilsTest_0004
91 * @tc.desc: Verify the getGlobalId function.
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(PrintUtilsTest, GetGlobalId_NotIncludeExtensionId_ReturnWithExtensionId, TestSize.Level2)
96 {
97 std::string id = "com.sample.ext";
98 std::string localId = "1234";
99 std::string testResult = "com.sample.ext:1234";
100 std::string result = PrintUtils::GetGlobalId(id, localId);
101 EXPECT_EQ(testResult, result);
102 }
103
104 /**
105 * @tc.name: PrintUtilsTest_0005
106 * @tc.desc: Verify the getLocalId function.
107 * @tc.type: FUNC
108 * @tc.require:
109 */
110 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0005_NeedRename, TestSize.Level2)
111 {
112 std::string gid = "com.sample.ext";
113 std::string id = "com.sample.ext";
114 std::string testResult = "";
115 std::string result = PrintUtils::GetLocalId(gid, id);
116 EXPECT_EQ(testResult, result);
117 }
118
119 /**
120 * @tc.name: PrintUtilsTest_0006
121 * @tc.desc: Verify the getLocalId function.
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0006_NeedRename, TestSize.Level2)
126 {
127 std::string gid = "com.sample.ext:1234";
128 std::string id = "com.sample.extt";
129 std::string testResult = "";
130 std::string result = PrintUtils::GetLocalId(gid, id);
131 EXPECT_EQ(testResult, result);
132 }
133
134 /**
135 * @tc.name: PrintUtilsTest_0007
136 * @tc.desc: Verify the getLocalId function.
137 * @tc.type: FUNC
138 * @tc.require:
139 */
140 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0007_NeedRename, TestSize.Level2)
141 {
142 std::string gid = "com.sample.ext:1234";
143 std::string id = "com.sample.ext4";
144 std::string testResult = "1234";
145 std::string result = PrintUtils::GetLocalId(gid, id);
146 EXPECT_NE(testResult, result);
147 }
148
149 /**
150 * @tc.name: PrintUtilsTest_0008
151 * @tc.desc: Verify the encodeExtensionCid function.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0008_NeedRename, TestSize.Level2)
156 {
157 std::string gid = "com.sample.ext";
158 uint32_t cid = 1;
159 std::string testResult = "com.sample.ext:1";
160 std::string result = PrintUtils::EncodeExtensionCid(gid, cid);
161 EXPECT_EQ(testResult, result);
162 }
163
164 /**
165 * @tc.name: PrintUtilsTest_0009
166 * @tc.desc: Verify the decodeExtensionCid function.
167 * @tc.type: FUNC
168 * @tc.require:
169 */
170 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0009_NeedRename, TestSize.Level2)
171 {
172 std::string gid = "com.sample.ext";
173 std::string extensionId = "com.sample.ext";
174 uint32_t callbackId;
175 EXPECT_FALSE(PrintUtils::DecodeExtensionCid(gid, extensionId, callbackId));
176 }
177
178 /**
179 * @tc.name: PrintUtilsTest_0010
180 * @tc.desc: Verify the decodeExtensionCid function.
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0010_NeedRename, TestSize.Level2)
185 {
186 std::string gid = "com.sample.ext:1";
187 std::string extensionId = "com.sample.ext";
188 uint32_t testCid = 1;
189 uint32_t callbackId = 0;
190 EXPECT_TRUE(PrintUtils::DecodeExtensionCid(gid, extensionId, callbackId));
191 EXPECT_EQ(testCid, callbackId);
192 }
193
194 /**
195 * @tc.name: PrintUtilsTest_0011
196 * @tc.desc: Verify the getTaskEventId function.
197 * @tc.type: FUNC
198 * @tc.require:
199 */
200 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0011_NeedRename, TestSize.Level2)
201 {
202 std::string taskId = "1234";
203 std::string type = "block";
204 std::string testResult = "block-1234";
205 std::string result = PrintUtils::GetTaskEventId(taskId, type);
206 EXPECT_EQ(testResult, result);
207 }
208
209 /**
210 * @tc.name: PrintUtilsTest_0012
211 * @tc.desc: Verify the openFile function.
212 * @tc.type: FUNC
213 * @tc.require:
214 */
215 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0012_NeedRename, TestSize.Level2)
216 {
217 int32_t result = PrintUtils::OpenFile("/error");
218 EXPECT_EQ(PRINT_INVALID_ID, result);
219 }
220
221 /**
222 * @tc.name: PrintUtilsTest_0013
223 * @tc.desc: Verify the openFile function.
224 * @tc.type: FUNC
225 * @tc.require:
226 */
227 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0013_NeedRename, TestSize.Level2)
228 {
229 int32_t result = PrintUtils::OpenFile("/data/e11");
230 EXPECT_EQ(PRINT_INVALID_ID, result);
231 }
232
233 /**
234 * @tc.name: PrintUtilsTest_0014
235 * @tc.desc: Verify the isPathValid function.
236 * @tc.type: FUNC
237 * @tc.require:
238 */
239 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0014_NeedRename, TestSize.Level2)
240 {
241 int32_t result = PrintUtils::IsPathValid("/data/service/e11/data/service/ \
242 e11/data/service/e11/data/service/e11/data/service/e11/data/service/e11/data/service/ \
243 e11/data/service/e11/data/service/e11/data/service/e11/data/service/e11");
244 EXPECT_EQ(E_PRINT_NONE, result);
245 }
246
247 /**
248 * @tc.name: PrintUtilsTest_0015
249 * @tc.desc: Verify the isPathValid function.
250 * @tc.type: FUNC
251 * @tc.require:
252 */
253 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0015_NeedRename, TestSize.Level2)
254 {
255 int32_t result = PrintUtils::IsPathValid("/data/service/e11//data/service/e11");
256 EXPECT_EQ(E_PRINT_NONE, result);
257 }
258
259 /**
260 * @tc.name: PrintUtilsTest_0016
261 * @tc.desc: Verify the isPathValid function.
262 * @tc.type: FUNC
263 * @tc.require:
264 */
265 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0016_NeedRename, TestSize.Level2)
266 {
267 int32_t result = PrintUtils::IsPathValid("/data/service/e11");
268 EXPECT_NE(PRINT_INVALID_ID, result);
269 }
270
271 /**
272 * @tc.name: PrintUtilsTest_0017
273 * @tc.desc: Verify the getIdFromFdPath function.
274 * @tc.type: FUNC
275 * @tc.require:
276 */
277 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0017_NeedRename, TestSize.Level2)
278 {
279 int32_t result = PrintUtils::GetIdFromFdPath("/data/service");
280 EXPECT_EQ(E_PRINT_NONE, result);
281 }
282
283 /**
284 * @tc.name: PrintUtilsTest_0018
285 * @tc.desc: Verify the getIdFromFdPath function.
286 * @tc.type: FUNC
287 * @tc.require:
288 */
289 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0018_NeedRename, TestSize.Level2)
290 {
291 int32_t result = PrintUtils::GetIdFromFdPath("/data/service/e11");
292 EXPECT_NE(PRINT_INVALID_ID, result);
293 }
294
295 /**
296 * @tc.name: PrintUtilsTest_0019
297 * @tc.desc: Verify the getJobStateChar function.
298 * @tc.type: FUNC
299 * @tc.require:
300 */
301 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0019_NeedRename, TestSize.Level2)
302 {
303 std::string testResult = "PRINT_JOB_PREPARED";
304 std::string result = PrintUtils::GetJobStateChar(PRINT_JOB_PREPARED);
305 EXPECT_EQ(testResult, result);
306 }
307
308 /**
309 * @tc.name: PrintUtilsTest_0020
310 * @tc.desc: Verify the getJobStateChar function.
311 * @tc.type: FUNC
312 * @tc.require:
313 */
314 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0020_NeedRename, TestSize.Level2)
315 {
316 std::string testResult = "PRINT_JOB_UNKNOWN";
317 std::string result = PrintUtils::GetJobStateChar(PRINT_JOB_PREPARED + 100);
318 EXPECT_EQ(testResult, result);
319 }
320
321 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0021_NeedRename, TestSize.Level2)
322 {
323 PrintUtils printUtils;
324 std::shared_ptr<AdapterParam> adapterParam = std::make_shared<AdapterParam>();
325 AAFwk::Want want;
326 printUtils.BuildPrintAttributesParam(adapterParam, want);
327 adapterParam->printAttributes.SetCopyNumber(1);
328 printUtils.BuildPrintAttributesParam(adapterParam, want);
329 EXPECT_EQ(adapterParam->printAttributes.hasCopyNumber_, true);
330 EXPECT_EQ(adapterParam->printAttributes.copyNumber_, 1);
331
332 adapterParam->printAttributes.SetIsSequential(true);
333 printUtils.BuildPrintAttributesParam(adapterParam, want);
334 EXPECT_EQ(adapterParam->printAttributes.hasSequential_, true);
335 EXPECT_EQ(adapterParam->printAttributes.isSequential_, true);
336
337 adapterParam->printAttributes.SetIsLandscape(false);
338 printUtils.BuildPrintAttributesParam(adapterParam, want);
339 EXPECT_EQ(adapterParam->printAttributes.hasLandscape_, true);
340 EXPECT_EQ(adapterParam->printAttributes.isLandscape_, false);
341
342 adapterParam->printAttributes.SetDirectionMode(0);
343 printUtils.BuildPrintAttributesParam(adapterParam, want);
344 EXPECT_EQ(adapterParam->printAttributes.hasDirectionMode_, true);
345 EXPECT_EQ(adapterParam->printAttributes.directionMode_, 0);
346
347 adapterParam->printAttributes.SetColorMode(0);
348 printUtils.BuildPrintAttributesParam(adapterParam, want);
349 EXPECT_EQ(adapterParam->printAttributes.hasColorMode_, true);
350 EXPECT_EQ(adapterParam->printAttributes.colorMode_, 0);
351
352 adapterParam->printAttributes.SetDuplexMode(0);
353 printUtils.BuildPrintAttributesParam(adapterParam, want);
354 EXPECT_EQ(adapterParam->printAttributes.hasDuplexMode_, true);
355 EXPECT_EQ(adapterParam->printAttributes.duplexMode_, 0);
356
357 adapterParam->printAttributes.SetOption("123");
358 printUtils.BuildPrintAttributesParam(adapterParam, want);
359 EXPECT_EQ(adapterParam->printAttributes.hasOption_, true);
360 EXPECT_EQ(adapterParam->printAttributes.option_, "123");
361 }
362
363 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0022_NeedRename, TestSize.Level2)
364 {
365 PrintUtils printUtils;
366 PrintAttributes attrParam;
367 Json::Value attrJson;
368 printUtils.ParseAttributesObjectParamForJson(attrParam, attrJson);
369 EXPECT_EQ(PrintJsonUtil::WriteString(attrJson), "null");
370
371 PrintRange range;
372 range.SetStartPage(1);
373 range.SetEndPage(1);
374 std::vector<uint32_t> pages;
375 pages.push_back(1);
376 range.SetPages(pages);
377 attrParam.SetPageRange(range);
378 printUtils.ParseAttributesObjectParamForJson(attrParam, attrJson);
379 EXPECT_EQ(PrintJsonUtil::WriteString(attrJson), "{\"pageRange\":{\"endPage\":1,\"pages\":[1],\"startPage\":1}}");
380
381 PrintPageSize pageSize("11", "123", 400, 600);
382 attrParam.SetPageSize(pageSize);
383 printUtils.ParseAttributesObjectParamForJson(attrParam, attrJson);
384 EXPECT_EQ(PrintJsonUtil::WriteString(attrJson), "{\"pageRange\":{\"endPage\":1,\"pages\":[1],\"startPage\":1},"
385 "\"pageSize\":{\"height\":600,\"id\":\"11\",\"name\":\"123\","
386 "\"width\":400}}");
387
388 PrintMargin margin;
389 margin.SetTop(100);
390 margin.SetBottom(100);
391 margin.SetLeft(100);
392 margin.SetRight(100);
393 attrParam.SetMargin(margin);
394 printUtils.ParseAttributesObjectParamForJson(attrParam, attrJson);
395 EXPECT_EQ(PrintJsonUtil::WriteString(attrJson), "{\"margin\":{\"bottom\":100,\"left\":100,\"right\":100,"
396 "\"top\":100},\"pageRange\":{\"endPage\":1,\"pages\":[1],\"startPage\":1},"
397 "\"pageSize\":{\"height\":600,\"id\":\"11\",\"name\":\"123\",\"width\":400}}");
398 }
399
400 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0023_NeedRename, TestSize.Level2)
401 {
402 PrintUtils printUtils;
403 std::shared_ptr<AdapterParam> adapterParam = std::make_shared<AdapterParam>();
404 AAFwk::Want want;
405 printUtils.BuildAdapterParam(adapterParam, want);
406 AAFwk::Want preWant = want;
407 adapterParam->isCheckFdList = false;
408 printUtils.BuildAdapterParam(adapterParam, want);
409 EXPECT_EQ(want.IsEquals(preWant), true);
410 }
411
412 /**
413 * @tc.name: PrintUtilsTest_0024
414 * @tc.desc: Verify the getLocalId function.
415 * @tc.type: FUNC
416 * @tc.require:
417 */
418 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0024_NeedRename, TestSize.Level2)
419 {
420 std::string gid = "com.sample.ext:1234";
421 std::string id = "com.sample.ext";
422 std::string testResult = "1234";
423 std::string result = PrintUtils::GetLocalId(gid, id);
424 EXPECT_EQ(testResult, result);
425 }
426
427 /**
428 * @tc.name: PrintUtilsTest_0025
429 * @tc.desc: Verify the getLocalId function.
430 * @tc.type: FUNC
431 * @tc.require:
432 */
433 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0025_NeedRename, TestSize.Level2)
434 {
435 int32_t result = PrintUtils::OpenFile("content://test/test1");
436 EXPECT_EQ(99, result);
437 }
438
439 /**
440 * @tc.name: PrintUtilsTest_0026
441 * @tc.desc: Verify the isPathValid function.
442 * @tc.type: FUNC
443 * @tc.require:
444 */
445 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0026_NeedRename, TestSize.Level2)
446 {
447 string testString(2048, '1');
448 int32_t result = PrintUtils::IsPathValid("testString");
449 EXPECT_EQ(E_PRINT_NONE, result);
450 }
451
452 /**
453 * @tc.name: PrintUtilsTest_0027
454 * @tc.desc: Verify the isPathValid function.
455 * @tc.type: FUNC
456 * @tc.require:
457 */
458 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0027_NeedRename, TestSize.Level2)
459 {
460 std::string result = PrintUtils::GetBundleNameForUid(123);
461 EXPECT_EQ("", result);
462 }
463
464 /**
465 * @tc.name: PrintUtilsTest_0028
466 * @tc.desc: Verify the CheckUserIdInEventType function.
467 * @tc.type: FUNC
468 * @tc.require:
469 */
470 HWTEST_F(PrintUtilsTest, PrintUtilsTest_0028_NeedRename, TestSize.Level2)
471 {
472 std::string type = "test";
473 int32_t callerUserId = 100; // defaute test user id
474 EXPECT_FALSE(PrintUtils::CheckUserIdInEventType(type, callerUserId));
475 type = "test:";
476 EXPECT_FALSE(PrintUtils::CheckUserIdInEventType(type, callerUserId));
477 type = "999:test";
478 EXPECT_FALSE(PrintUtils::CheckUserIdInEventType(type, callerUserId));
479 type = "100:test";
480 EXPECT_TRUE(PrintUtils::CheckUserIdInEventType(type, callerUserId));
481 }
482
483 /**
484 * @tc.name: PrintUtilsTest_0029
485 * @tc.desc: Verify the getGlobalId function.
486 * @tc.type: FUNC
487 * @tc.require:
488 */
489 HWTEST_F(PrintUtilsTest, GetGlobalId_IncludeExtensionId_ReturnLocalId, TestSize.Level2)
490 {
491 std::string id = "com.sample.ext";
492 std::string localId = "com.sample.ext:1234";
493 std::string testResult = "com.sample.ext:1234";
494 std::string result = PrintUtils::GetGlobalId(id, localId);
495 EXPECT_EQ(testResult, result);
496 }
497
498 /**
499 * @tc.name: PrintUtilsTest_0030
500 * @tc.desc: Verify the IsUsbPrinter function.
501 * @tc.type: FUNC
502 * @tc.require:
503 */
504 HWTEST_F(PrintUtilsTest, IsUsbPrinter_NotIncludeUSBString_Returnfalse, TestSize.Level2)
505 {
506 std::string printerId = "com.sample.ext:1234";
507 bool result = PrintUtils::IsUsbPrinter(printerId);
508 EXPECT_EQ(result, false);
509 }
510
511 /**
512 * @tc.name: PrintUtilsTest_0031
513 * @tc.desc: Verify the IsUsbPrinter function.
514 * @tc.type: FUNC
515 * @tc.require:
516 */
517 HWTEST_F(PrintUtilsTest, IsUsbPrinter_IncludeUSBString_ReturnTrue, TestSize.Level2)
518 {
519 std::string printerId = "com.sample.ext:USB-printer-1234";
520 bool result = PrintUtils::IsUsbPrinter(printerId);
521 EXPECT_EQ(result, true);
522 }
523 } // namespace Print
524 } // namespace OHOS
525