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