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