1 /*
2  * Copyright (c) 2024 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 #include "print_cups_attribute.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 using json = nlohmann::json;
22 
23 namespace {
24 const char *const ATTR_TEST_ALL[] = {"all"};
25 const size_t ATTR_TEST_SIDES_COUNT = 3;
26 const char *const ATTR_TEST_SIDES_ARRAY[ATTR_TEST_SIDES_COUNT] = {
27     "one-sided", "two-sided-long-edge", "two-sided-short-edge"};
28 
29 const size_t ATTR_TEST_COLOR_MODE_COUNT = 2;
30 const char *const ATTR_TEST_COLOR_MODE_ARRAY[ATTR_TEST_COLOR_MODE_COUNT] = {"monochrome", "color"};
31 
32 const size_t ATTR_TEST_PAGE_SIZE_COUNT = 4;
33 const char *const ATTR_TEST_PAGE_SIZE_ARRAY[ATTR_TEST_PAGE_SIZE_COUNT] = {
34     "iso_b3_353x500mm", "iso_a4_210x297mm", "na_letter_8.5x11in", "om_card_54x86mm"};
35 
36 const size_t ATTR_TEST_QUALITY_COUNT = 3;
37 const int ATTR_TEST_QUALITY_ARRAY[ATTR_TEST_QUALITY_COUNT] = {IPP_QUALITY_DRAFT, IPP_QUALITY_NORMAL, IPP_QUALITY_HIGH};
38 const int ATTR_TEST_MAX_COPIES = 99;
39 const int ATTR_TEST_RESOLUTION_SMALL = 250;  // need change
40 const int ATTR_TEST_RESOLUTION_DEFAULT = 600;
41 const int ATTR_TEST_RESULUTION_SMALL_DPCM = 100;
42 
43 const int ATTR_TEST_PAPER_LEFT = 100;
44 const int ATTR_TEST_PAPER_RIGHT = 200;
45 const int ATTR_TEST_PAPER_TOP = 300;
46 const int ATTR_TEST_PAPER_BOTTOM = 400;
47 
48 const int ATTR_TEST_ORIENTATION_COUNT = 3;
49 const int ATTR_TEST_ORIENTATION_ARRAY[ATTR_TEST_ORIENTATION_COUNT] = {
50     IPP_ORIENT_PORTRAIT, IPP_ORIENT_LANDSCAPE, IPP_ORIENT_REVERSE_PORTRAIT};
51 
52 const int ATTR_TEST_SOURCE_COUNT = 2;
53 const char *const ATTR_TEST_SOURCE_ARRAY[ATTR_TEST_SOURCE_COUNT] = {"main source", "front slot"};
54 
55 const int ATTR_TEST_DOCUMENT_HANDLING_COUNT = 2;
56 const char *const ATTR_TEST_DOCUMENT_HANDLING_ARRAY[] = {"separate-uncollated", "multi-collated"};
57 
58 const int ATTR_TEST_MEDIA_TYPE_COUNT = 3;
59 const char *const ATTR_TEST_MEDIA_TYPE_ARRAY[ATTR_TEST_MEDIA_TYPE_COUNT] = {"envelope", "stationery", "transparency"};
60 
TestAttrCount(const std::string &jsonString, int count)61 void TestAttrCount(const std::string &jsonString, int count)
62 {
63     EXPECT_TRUE(json::accept(jsonString));
64     auto jsonObject = json::parse(jsonString);
65     EXPECT_TRUE(jsonObject.is_array());
66     EXPECT_EQ(jsonObject.size(), count);
67 }
68 }  // namespace
69 
70 namespace OHOS::Print {
71 using PreAttrTestFunc = std::function<void(ipp_t *)>;
72 using PostResponseTestFunc = std::function<void(ipp_t *)>;
73 using PostAttrTestFunc = std::function<void(PrinterCapability &)>;
74 class PrintCupsAttributeTest : public testing::Test {
75 public:
76     static void SetUpTestCase(void);
77     static void TearDownTestCase(void);
78     void SetUp();
79     void TearDown();
80     void DoTestResponse(PreAttrTestFunc preFunc, PostResponseTestFunc postFunc);
81     void DoTest(PreAttrTestFunc preFunc, PostAttrTestFunc postFunc);
82 };
83 
SetUpTestCase(void)84 void PrintCupsAttributeTest::SetUpTestCase(void)
85 {}
86 
TearDownTestCase(void)87 void PrintCupsAttributeTest::TearDownTestCase(void)
88 {}
89 
SetUp(void)90 void PrintCupsAttributeTest::SetUp(void)
91 {}
92 
TearDown(void)93 void PrintCupsAttributeTest::TearDown(void)
94 {}
95 
DoTestResponse(PreAttrTestFunc preFunc, PostResponseTestFunc postFunc)96 void PrintCupsAttributeTest::DoTestResponse(PreAttrTestFunc preFunc, PostResponseTestFunc postFunc)
97 {
98     if (preFunc == nullptr || postFunc == nullptr) {
99         return;
100     }
101     ipp_t *request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
102     if (request == nullptr) {
103         return;
104     }
105     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, "printer-uri");
106     ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, "user");
107     ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", 1, nullptr, ATTR_TEST_ALL);
108     ipp_t *response = ippNewResponse(request);
109     ippDelete(request);
110     if (response == nullptr) {
111         return;
112     }
113     preFunc(response);
114     postFunc(response);
115     ippDelete(response);
116 }
117 
DoTest(PreAttrTestFunc preFunc, PostAttrTestFunc postFunc)118 void PrintCupsAttributeTest::DoTest(PreAttrTestFunc preFunc, PostAttrTestFunc postFunc)
119 {
120     PostResponseTestFunc postResponseFunc = [this, postFunc](ipp_t *response) {
121         PrinterCapability printerCaps;
122         ParsePrinterAttributes(response, printerCaps);
123         if (postFunc != nullptr) {
124             postFunc(printerCaps);
125         }
126     };
127     DoTestResponse(preFunc, postResponseFunc);
128 }
129 
130 /**
131  * @tc.name: PrintCupsAttributeTest_0001
132  * @tc.desc: printer idle state test
133  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0001, TestSize.Level1)134 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0001, TestSize.Level1)
135 {
136     PreAttrTestFunc preFunc = [this](ipp_t *response) {
137         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PRINTER_IDLE);
138         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", nullptr, "Printer info test");
139         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", nullptr, "Printer location test");
140     };
141     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
142         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-state"), "idle");
143         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-location"), "Printer location test");
144     };
145     DoTest(preFunc, postFunc);
146 }
147 
148 /**
149  * @tc.name: PrintCupsAttributeTest_0002
150  * @tc.desc: printer processing state test
151  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0002, TestSize.Level1)152 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0002, TestSize.Level1)
153 {
154     PreAttrTestFunc preFunc = [this](ipp_t *response) {
155         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PRINTER_PROCESSING);
156         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", nullptr, "");
157         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", nullptr, "");
158     };
159     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
160         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-state"), "processing");
161         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-info"), "");
162         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-location"), "");
163     };
164     DoTest(preFunc, postFunc);
165 }
166 
167 /**
168  * @tc.name: PrintCupsAttributeTest_0003
169  * @tc.desc: printer stopped state test
170  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0003, TestSize.Level1)171 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0003, TestSize.Level1)
172 {
173     PreAttrTestFunc preFunc = [this](ipp_t *response) {
174         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PRINTER_STOPPED);
175     };
176     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
177         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-state"), "stopped");
178         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-info"), "");
179         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-location"), "");
180     };
181     DoTest(preFunc, postFunc);
182 }
183 
184 /**
185  * @tc.name: PrintCupsAttributeTest_0004
186  * @tc.desc: printer empty state test
187  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0004, TestSize.Level1)188 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0004, TestSize.Level1)
189 {
190     PreAttrTestFunc preFunc = [this](ipp_t *response) {};
191     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
192         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-state"), "");
193         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-info"), "");
194         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("printer-location"), "");
195     };
196     DoTest(preFunc, postFunc);
197 }
198 
199 /**
200  * @tc.name: PrintCupsAttributeTest_0005
201  * @tc.desc: printer sides test
202  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0005, TestSize.Level1)203 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0005, TestSize.Level1)
204 {
205     PreAttrTestFunc preFunc = [this](ipp_t *response) {
206         ippAddStrings(response,
207             IPP_TAG_PRINTER,
208             IPP_TAG_KEYWORD,
209             "sides-supported",
210             ATTR_TEST_SIDES_COUNT,
211             nullptr,
212             ATTR_TEST_SIDES_ARRAY);
213         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-default", nullptr, ATTR_TEST_SIDES_ARRAY[0]);
214     };
215     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
216         std::string sideString = printerCaps.GetPrinterAttrValue("sides-supported");
217         TestAttrCount(sideString, ATTR_TEST_SIDES_COUNT);
218         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("sides-default"), "0");
219     };
220     DoTest(preFunc, postFunc);
221 }
222 
223 /**
224  * @tc.name: PrintCupsAttributeTest_0006
225  * @tc.desc: printer sides test
226  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0006, TestSize.Level1)227 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0006, TestSize.Level1)
228 {
229     PreAttrTestFunc preFunc = [this](ipp_t *response) {
230         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-default", nullptr, ATTR_TEST_SIDES_ARRAY[1]);
231     };
232     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
233         std::string sideString = printerCaps.GetPrinterAttrValue("sides-supported");
234         TestAttrCount(sideString, 0);
235         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("sides-default"), "1");
236     };
237     DoTest(preFunc, postFunc);
238 }
239 
240 /**
241  * @tc.name: PrintCupsAttributeTest_0007
242  * @tc.desc: printer color mode test
243  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0007, TestSize.Level1)244 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0007, TestSize.Level1)
245 {
246     PreAttrTestFunc preFunc = [this](ipp_t *response) {
247         ippAddStrings(response,
248             IPP_TAG_PRINTER,
249             IPP_TAG_KEYWORD,
250             "print-color-mode-supported",
251             ATTR_TEST_COLOR_MODE_COUNT,
252             nullptr,
253             ATTR_TEST_COLOR_MODE_ARRAY);
254         ippAddString(response,
255             IPP_TAG_PRINTER,
256             IPP_TAG_KEYWORD,
257             "print-color-mode-default",
258             nullptr,
259             ATTR_TEST_COLOR_MODE_ARRAY[0]);
260     };
261     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
262         std::string colorModeString = printerCaps.GetPrinterAttrValue("print-color-mode-supported");
263         TestAttrCount(colorModeString, ATTR_TEST_COLOR_MODE_COUNT);
264         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("defaultColorMode"), "0");
265     };
266     DoTest(preFunc, postFunc);
267 }
268 
269 /**
270  * @tc.name: PrintCupsAttributeTest_0008
271  * @tc.desc: printer color mode test
272  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0008, TestSize.Level1)273 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0008, TestSize.Level1)
274 {
275     PreAttrTestFunc preFunc = [this](ipp_t *response) {
276         ippAddString(response,
277             IPP_TAG_PRINTER,
278             IPP_TAG_KEYWORD,
279             "print-color-mode-default",
280             nullptr,
281             ATTR_TEST_COLOR_MODE_ARRAY[1]);
282     };
283     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
284         std::string colorModeString = printerCaps.GetPrinterAttrValue("print-color-mode-supported");
285         TestAttrCount(colorModeString, 0);
286         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("defaultColorMode"), "1");
287     };
288     DoTest(preFunc, postFunc);
289 }
290 
291 /**
292  * @tc.name: PrintCupsAttributeTest_0009
293  * @tc.desc: printer page size test
294  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0009, TestSize.Level1)295 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0009, TestSize.Level1)
296 {
297     PreAttrTestFunc preFunc = [this](ipp_t *response) {
298         ippAddStrings(response,
299             IPP_TAG_PRINTER,
300             IPP_TAG_KEYWORD,
301             "media-supported",
302             ATTR_TEST_PAGE_SIZE_COUNT,
303             nullptr,
304             ATTR_TEST_PAGE_SIZE_ARRAY);
305         ippAddString(
306             response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default", nullptr, ATTR_TEST_PAGE_SIZE_ARRAY[0]);
307     };
308     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
309         std::string pageSizeString = printerCaps.GetPrinterAttrValue("supportedPageSizeArray");
310         TestAttrCount(pageSizeString, ATTR_TEST_PAGE_SIZE_COUNT);
311         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("defaultPageSizeId"), "ISO_B3");
312     };
313     DoTest(preFunc, postFunc);
314 }
315 
316 /**
317  * @tc.name: PrintCupsAttributeTest_0010
318  * @tc.desc: printer page size test
319  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0010, TestSize.Level1)320 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0010, TestSize.Level1)
321 {
322     PreAttrTestFunc preFunc = [this](ipp_t *response) {
323         ippAddString(response,
324             IPP_TAG_PRINTER,
325             IPP_TAG_KEYWORD,
326             "print-color-mode-default",
327             nullptr,
328             ATTR_TEST_COLOR_MODE_ARRAY[1]);
329     };
330     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
331         std::string pageSizeString = printerCaps.GetPrinterAttrValue("print-color-mode-supported");
332         TestAttrCount(pageSizeString, 0);
333         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("defaultPageSizeId"), "");
334     };
335     DoTest(preFunc, postFunc);
336 }
337 
338 /**
339  * @tc.name: PrintCupsAttributeTest_0011
340  * @tc.desc: printer quality / copies test
341  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0011, TestSize.Level1)342 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0011, TestSize.Level1)
343 {
344     PreAttrTestFunc preFunc = [this](ipp_t *response) {
345         ippAddIntegers(response,
346             IPP_TAG_PRINTER,
347             IPP_TAG_ENUM,
348             "print-quality-supported",
349             ATTR_TEST_QUALITY_COUNT,
350             ATTR_TEST_QUALITY_ARRAY);
351         ippAddRange(response, IPP_TAG_PRINTER, "copies-supported", 1, ATTR_TEST_MAX_COPIES);
352         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default", 1);
353     };
354     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
355         std::string qualityString = printerCaps.GetPrinterAttrValue("print-quality-supported");
356         TestAttrCount(qualityString, ATTR_TEST_QUALITY_COUNT);
357         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("copies-supported"), std::to_string(ATTR_TEST_MAX_COPIES).c_str());
358         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("copies-default"), "1");
359     };
360     DoTest(preFunc, postFunc);
361 }
362 
363 /**
364  * @tc.name: PrintCupsAttributeTest_0012
365  * @tc.desc: printer quality / copies test
366  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0012, TestSize.Level1)367 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0012, TestSize.Level1)
368 {
369     PreAttrTestFunc preFunc = [this](ipp_t *response) {};
370     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
371         std::string pageSizeString = printerCaps.GetPrinterAttrValue("print-quality-supported");
372         EXPECT_TRUE(pageSizeString.empty());
373         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("copies-supported"), "");
374         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("copies-default"), "");
375     };
376     DoTest(preFunc, postFunc);
377 }
378 
379 /**
380  * @tc.name: PrintCupsAttributeTest_0013
381  * @tc.desc: printer resolution test
382  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0013, TestSize.Level1)383 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0013, TestSize.Level1)
384 {
385     PreAttrTestFunc preFunc = [this](ipp_t *response) {
386         ippAddResolution(response,
387             IPP_TAG_PRINTER,
388             "printer-resolution-supported",
389             IPP_RES_PER_INCH,
390             ATTR_TEST_RESOLUTION_DEFAULT,
391             ATTR_TEST_RESOLUTION_DEFAULT);
392         ippAddResolution(response,
393             IPP_TAG_PRINTER,
394             "printer-resolution-default",
395             IPP_RES_PER_INCH,
396             ATTR_TEST_RESOLUTION_DEFAULT,
397             ATTR_TEST_RESOLUTION_DEFAULT);
398     };
399     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
400         std::string supportedResolutionString = printerCaps.GetPrinterAttrValue("printer-resolution-supported");
401         TestAttrCount(supportedResolutionString, 1);
402         std::string defaultResolutionString = printerCaps.GetPrinterAttrValue("printer-resolution-default");
403         EXPECT_TRUE(json::accept(defaultResolutionString));
404         auto defaultResolutionJson = json::parse(defaultResolutionString);
405         EXPECT_TRUE(defaultResolutionJson.contains("horizontalDpi"));
406         EXPECT_TRUE(defaultResolutionJson.contains("verticalDpi"));
407         EXPECT_TRUE(defaultResolutionJson["horizontalDpi"].is_number());
408         EXPECT_TRUE(defaultResolutionJson["verticalDpi"].is_number());
409         EXPECT_EQ(defaultResolutionJson["horizontalDpi"], ATTR_TEST_RESOLUTION_DEFAULT);
410         EXPECT_EQ(defaultResolutionJson["verticalDpi"], ATTR_TEST_RESOLUTION_DEFAULT);
411     };
412     DoTest(preFunc, postFunc);
413 }
414 
415 /**
416  * @tc.name: PrintCupsAttributeTest_0014
417  * @tc.desc: printer resolution test
418  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0014, TestSize.Level1)419 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0014, TestSize.Level1)
420 {
421     PreAttrTestFunc preFunc = [this](ipp_t *response) {
422         ippAddResolution(response,
423             IPP_TAG_PRINTER,
424             "printer-resolution-default",
425             IPP_RES_PER_CM,
426             ATTR_TEST_RESULUTION_SMALL_DPCM,
427             ATTR_TEST_RESULUTION_SMALL_DPCM);
428     };
429     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
430         std::string supportedResolutionString = printerCaps.GetPrinterAttrValue("printer-resolution-supported");
431         EXPECT_TRUE(supportedResolutionString.empty());
432         std::string defaultResolutionString = printerCaps.GetPrinterAttrValue("printer-resolution-default");
433         EXPECT_TRUE(json::accept(defaultResolutionString));
434         auto defaultResolutionJson = json::parse(defaultResolutionString);
435         EXPECT_TRUE(defaultResolutionJson.contains("horizontalDpi"));
436         EXPECT_TRUE(defaultResolutionJson.contains("verticalDpi"));
437         EXPECT_TRUE(defaultResolutionJson["horizontalDpi"].is_number());
438         EXPECT_TRUE(defaultResolutionJson["verticalDpi"].is_number());
439         EXPECT_EQ(defaultResolutionJson["horizontalDpi"], ATTR_TEST_RESOLUTION_SMALL);
440         EXPECT_EQ(defaultResolutionJson["verticalDpi"], ATTR_TEST_RESOLUTION_SMALL);
441     };
442     DoTest(preFunc, postFunc);
443 }
444 
445 /**
446  * @tc.name: PrintCupsAttributeTest_0015
447  * @tc.desc: printer media test
448  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0015, TestSize.Level1)449 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0015, TestSize.Level1)
450 {
451     PreAttrTestFunc preFunc = [this](ipp_t *response) {
452         auto mediaCol = ippNew();
453         if (mediaCol == nullptr) {
454             return;
455         }
456         ippAddInteger(mediaCol, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-bottom-margin", ATTR_TEST_PAPER_BOTTOM);
457         ippAddInteger(mediaCol, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-left-margin", ATTR_TEST_PAPER_LEFT);
458         ippAddInteger(mediaCol, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-right-margin", ATTR_TEST_PAPER_RIGHT);
459         ippAddInteger(mediaCol, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-top-margin", ATTR_TEST_PAPER_TOP);
460         ippAddBoolean(mediaCol, IPP_TAG_PRINTER, "duplex-supported", 1);
461         ippAddString(mediaCol, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-source", nullptr, "Front Input Slot");
462         ippAddString(mediaCol, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-type", nullptr, "stationery");
463         ippAddCollection(response, IPP_TAG_PRINTER, "media-col-default", mediaCol);
464         ippDelete(mediaCol);
465     };
466     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
467         EXPECT_STREQ(
468             printerCaps.GetPrinterAttrValue("media-top-margin-default"), std::to_string(ATTR_TEST_PAPER_TOP).c_str());
469         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-bottom-margin-default"),
470             std::to_string(ATTR_TEST_PAPER_BOTTOM).c_str());
471         EXPECT_STREQ(
472             printerCaps.GetPrinterAttrValue("media-left-margin-default"), std::to_string(ATTR_TEST_PAPER_LEFT).c_str());
473         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-right-margin-default"),
474             std::to_string(ATTR_TEST_PAPER_RIGHT).c_str());
475         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-source-default"), "Front Input Slot");
476         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-type-default"), "stationery");
477     };
478     DoTest(preFunc, postFunc);
479 }
480 
481 /**
482  * @tc.name: PrintCupsAttributeTest_0016
483  * @tc.desc: printer margin test
484  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0016, TestSize.Level1)485 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0016, TestSize.Level1)
486 {
487     PreAttrTestFunc preFunc = [this](ipp_t *response) {
488         ippAddInteger(
489             response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-bottom-margin-supported", ATTR_TEST_PAPER_BOTTOM);
490         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-left-margin-supported", ATTR_TEST_PAPER_LEFT);
491         ippAddInteger(
492             response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-right-margin-supported", ATTR_TEST_PAPER_RIGHT);
493         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "media-top-margin-supported", ATTR_TEST_PAPER_TOP);
494     };
495     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
496         EXPECT_STREQ(
497             printerCaps.GetPrinterAttrValue("media-top-margin-supported"), std::to_string(ATTR_TEST_PAPER_TOP).c_str());
498         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-bottom-margin-supported"),
499             std::to_string(ATTR_TEST_PAPER_BOTTOM).c_str());
500         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-left-margin-supported"),
501             std::to_string(ATTR_TEST_PAPER_LEFT).c_str());
502         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("media-right-margin-supported"),
503             std::to_string(ATTR_TEST_PAPER_RIGHT).c_str());
504     };
505     DoTest(preFunc, postFunc);
506 }
507 
508 /**
509  * @tc.name: PrintCupsAttributeTest_0017
510  * @tc.desc: printer orientation test
511  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0017, TestSize.Level1)512 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0017, TestSize.Level1)
513 {
514     PreAttrTestFunc preFunc = [this](ipp_t *response) {
515         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "orientation-requested-default", IPP_ORIENT_PORTRAIT);
516         ippAddIntegers(response,
517             IPP_TAG_PRINTER,
518             IPP_TAG_ENUM,
519             "orientation-requested-supported",
520             ATTR_TEST_ORIENTATION_COUNT,
521             ATTR_TEST_ORIENTATION_ARRAY);
522     };
523     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
524         EXPECT_STREQ(printerCaps.GetPrinterAttrValue("orientation-requested-default"), "3");
525         std::string orientationString = printerCaps.GetPrinterAttrValue("orientation-requested-supported");
526         TestAttrCount(orientationString, ATTR_TEST_ORIENTATION_COUNT);
527     };
528     DoTest(preFunc, postFunc);
529 }
530 
531 /**
532  * @tc.name: PrintCupsAttributeTest_0018
533  * @tc.desc: printer other attributes test
534  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0018, TestSize.Level1)535 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0018, TestSize.Level1)
536 {
537     PreAttrTestFunc preFunc = [this](ipp_t *response) {
538         ippAddStrings(response,
539             IPP_TAG_PRINTER,
540             IPP_CONST_TAG(IPP_TAG_KEYWORD),
541             "media-source-supported",
542             ATTR_TEST_SOURCE_COUNT,
543             nullptr,
544             ATTR_TEST_SOURCE_ARRAY);
545         ippAddStrings(response,
546             IPP_TAG_PRINTER,
547             IPP_CONST_TAG(IPP_TAG_KEYWORD),
548             "multiple-document-handling-supported",
549             ATTR_TEST_DOCUMENT_HANDLING_COUNT,
550             nullptr,
551             ATTR_TEST_DOCUMENT_HANDLING_ARRAY);
552     };
553     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
554         std::string sourceString = printerCaps.GetPrinterAttrValue("media-source-supported");
555         TestAttrCount(sourceString, ATTR_TEST_SOURCE_COUNT);
556         std::string documentHandlingString = printerCaps.GetPrinterAttrValue("multiple-document-handling-supported");
557         TestAttrCount(documentHandlingString, ATTR_TEST_DOCUMENT_HANDLING_COUNT);
558     };
559     DoTest(preFunc, postFunc);
560 }
561 
562 /**
563  * @tc.name: PrintCupsAttributeTest_0019
564  * @tc.desc: printer option test
565  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0019, TestSize.Level1)566 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0019, TestSize.Level1)
567 {
568     PreAttrTestFunc preFunc = [this](ipp_t *response) {
569         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-make-and-model", nullptr, "Test make and model");
570         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uuid", nullptr, "Test printer uuid");
571         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", nullptr, "Test printer name");
572         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location", nullptr, "Printer location test");
573         ippAddStrings(response,
574             IPP_TAG_PRINTER,
575             IPP_CONST_TAG(IPP_TAG_KEYWORD),
576             "media-type-supported",
577             ATTR_TEST_MEDIA_TYPE_COUNT,
578             nullptr,
579             ATTR_TEST_MEDIA_TYPE_ARRAY);
580     };
581     PostAttrTestFunc postFunc = [this](PrinterCapability &printerCaps) {
582         std::string mediaTypeString = printerCaps.GetPrinterAttrValue("media-type-supported");
583         TestAttrCount(mediaTypeString, ATTR_TEST_MEDIA_TYPE_COUNT);
584     };
585     DoTest(preFunc, postFunc);
586 }
587 
588 /**
589  * @tc.name: PrintCupsAttributeTest_0020
590  * @tc.desc: ParsePrinterStatusAttributes test
591  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0020, TestSize.Level1)592 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0020, TestSize.Level1)
593 {
594     PreAttrTestFunc preFunc = [this](ipp_t *response) {
595         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PSTATE_IDLE);
596     };
597     PostResponseTestFunc postFunc = [this](ipp_t *response) {
598         PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
599         EXPECT_TRUE(ParsePrinterStatusAttributes(response, status));
600         EXPECT_EQ(status, PRINTER_STATUS_IDLE);
601     };
602     DoTestResponse(preFunc, postFunc);
603 }
604 /**
605  * @tc.name: PrintCupsAttributeTest_0021
606  * @tc.desc: ParsePrinterStatusAttributes test
607  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0021, TestSize.Level1)608 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0021, TestSize.Level1)
609 {
610     PreAttrTestFunc preFunc = [this](ipp_t *response) {
611         ippAddString(response, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", nullptr, "Test printer name");
612     };
613     PostResponseTestFunc postFunc = [this](ipp_t *response) {
614         PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
615         EXPECT_FALSE(ParsePrinterStatusAttributes(response, status));
616     };
617     DoTestResponse(preFunc, postFunc);
618 }
619 
620 /**
621  * @tc.name: PrintCupsAttributeTest_0022
622  * @tc.desc: ParsePrinterStatusAttributes test
623  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0022, TestSize.Level1)624 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0022, TestSize.Level1)
625 {
626     PreAttrTestFunc preFunc = [this](ipp_t *response) {
627         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", 0);
628     };
629     PostResponseTestFunc postFunc = [this](ipp_t *response) {
630         PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
631         EXPECT_FALSE(ParsePrinterStatusAttributes(response, status));
632     };
633     DoTestResponse(preFunc, postFunc);
634 }
635 
636 /**
637  * @tc.name: PrintCupsAttributeTest_0023
638  * @tc.desc: ParsePrinterStatusAttributes test
639  */
HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0023, TestSize.Level1)640 HWTEST_F(PrintCupsAttributeTest, PrintCupsAttributeTest_0023, TestSize.Level1)
641 {
642     PreAttrTestFunc preFunc = [this](ipp_t *response) {
643         ippAddInteger(response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PSTATE_STOPPED + 1);
644     };
645     PostResponseTestFunc postFunc = [this](ipp_t *response) {
646         PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
647         EXPECT_FALSE(ParsePrinterStatusAttributes(response, status));
648     };
649     DoTestResponse(preFunc, postFunc);
650 }
651 }  // namespace OHOS::Print