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 "vendor_helper.h"
18 #include "print_constant.h"
19 #include "print_log.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace {
25 const int TEST_VALUE_SMALL = -1;
26 const int TEST_VALUE_LARGE = 10;
27 const uint32_t ISO_A3_WIDTH = 11690;
28 const uint32_t ISO_A3_HEIGHT = 16540;
29 const uint32_t ISO_A4_WIDTH = 8268;
30 const uint32_t ISO_A4_HEIGHT = 11692;
31 const uint32_t DPI_A = 300;
32 const uint32_t DPI_B = 600;
33 const uint32_t DEFAULT_COUNT = 2;
34 const uint32_t TEST_MAX_COPIES = 99;
35 }
36 namespace OHOS {
37 namespace Print {
38 class VendorHelperTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase(void)46 void VendorHelperTest::SetUpTestCase(void) {}
47
TearDownTestCase(void)48 void VendorHelperTest::TearDownTestCase(void) {}
49
SetUp(void)50 void VendorHelperTest::SetUp(void)
51 {
52 static int32_t testNo = 0;
53 PRINT_HILOGI("VendorHelperTest_%{public}d", ++testNo);
54 }
55
TearDown(void)56 void VendorHelperTest::TearDown(void) {}
57
BuildDiscoveryItem(Print_DiscoveryItem & discoveryItem)58 static void BuildDiscoveryItem(Print_DiscoveryItem &discoveryItem)
59 {
60 discoveryItem.printerId = "printerId";
61 discoveryItem.printerName = "printerName";
62 discoveryItem.description = "description";
63 discoveryItem.location = "location";
64 discoveryItem.makeAndModel = "makeAndModel";
65 discoveryItem.printerUri = "printerUri";
66 discoveryItem.printerUuid = "printerUuid";
67 }
68
BuildCapability(Print_PrinterCapability & capability)69 static void BuildCapability(Print_PrinterCapability &capability)
70 {
71 static Print_ColorMode colorModes[] = {COLOR_MODE_MONOCHROME, COLOR_MODE_COLOR};
72 static Print_DuplexMode duplexModes[] = {DUPLEX_MODE_ONE_SIDED, DUPLEX_MODE_TWO_SIDED_SHORT_EDGE};
73 static Print_PageSize pageSizes[] = {{"ISO_A4", "iso_a4_210x297mm", ISO_A4_WIDTH, ISO_A4_HEIGHT},
74 {"ISO_A3", "iso_a3", ISO_A3_WIDTH, ISO_A3_HEIGHT},
75 {"ISO_A4", "iso_a4", ISO_A4_WIDTH, ISO_A4_HEIGHT}};
76 static Print_Quality qualities[] = {Print_Quality::PRINT_QUALITY_DRAFT, Print_Quality::PRINT_QUALITY_HIGH};
77 static Print_Resolution resolutions[] = {{DPI_A, DPI_A}, {DPI_B, DPI_B}};
78 static Print_OrientationMode orientations[] = {ORIENTATION_MODE_PORTRAIT, ORIENTATION_MODE_NONE};
79 capability.supportedColorModesCount = DEFAULT_COUNT;
80 capability.supportedColorModes = colorModes;
81 capability.supportedDuplexModesCount = DEFAULT_COUNT;
82 capability.supportedDuplexModes = duplexModes;
83 capability.supportedPageSizesCount = DEFAULT_COUNT + 1;
84 capability.supportedPageSizes = pageSizes;
85 capability.supportedMediaTypes = "{\"a\",\"b\",\"c\"}";
86 capability.supportedQualitiesCount = DEFAULT_COUNT;
87 capability.supportedQualities = qualities;
88 capability.supportedPaperSources = "{\"a\",\"b\",\"c\"}";
89 capability.supportedCopies = TEST_MAX_COPIES;
90 capability.supportedResolutionsCount = DEFAULT_COUNT;
91 capability.supportedResolutions = resolutions;
92 capability.supportedOrientationsCount = DEFAULT_COUNT;
93 capability.supportedOrientations = orientations;
94 capability.advancedCapability = "advance info";
95 }
96
BuildDefaultValue(Print_DefaultValue & defaultValue)97 static void BuildDefaultValue(Print_DefaultValue &defaultValue)
98 {
99 defaultValue.defaultColorMode = COLOR_MODE_MONOCHROME;
100 defaultValue.defaultDuplexMode = DUPLEX_MODE_ONE_SIDED;
101 defaultValue.defaultMediaType = "a";
102 defaultValue.defaultPageSizeId = "ISO_A4";
103 defaultValue.defaultMargin = {1, 1, 1, 1};
104 defaultValue.defaultPaperSource = "a";
105 defaultValue.defaultPrintQuality = Print_Quality::PRINT_QUALITY_HIGH;
106 defaultValue.defaultCopies = 1;
107 defaultValue.defaultResolution = {DPI_A, DPI_A};
108 defaultValue.defaultOrientation = ORIENTATION_MODE_PORTRAIT;
109 defaultValue.otherDefaultValues = "default";
110 }
111
112 HWTEST_F(VendorHelperTest, VendorHelperTest_0001, TestSize.Level1)
113 {
114 std::string test = "test ";
115 char *result = CopyString("");
116 EXPECT_NE(result, nullptr);
117 EXPECT_STREQ(result, "");
118 delete[] result;
119 result = nullptr;
120 result = CopyString(test);
121 EXPECT_NE(result, nullptr);
122 EXPECT_STREQ(result, test.c_str());
123 delete[] result;
124 result = nullptr;
125 }
126
127 HWTEST_F(VendorHelperTest, VendorHelperTest_0002, TestSize.Level1)
128 {
129 long value = 0;
130 EXPECT_FALSE(ConvertStringToLong("", value));
131 EXPECT_FALSE(ConvertStringToLong("999999999999999999999999999", value));
132 EXPECT_FALSE(ConvertStringToLong("x", value));
133 EXPECT_TRUE(ConvertStringToLong(std::to_string(TEST_VALUE_LARGE).c_str(), value));
134 EXPECT_EQ(value, TEST_VALUE_LARGE);
135 EXPECT_TRUE(ConvertStringToLong("0", value));
136 EXPECT_EQ(value, 0);
137 }
138
139 HWTEST_F(VendorHelperTest, VendorHelperTest_0003, TestSize.Level1)
140 {
141 uint32_t value = 0;
142 EXPECT_FALSE(ConvertColorMode(static_cast<Print_ColorMode>(TEST_VALUE_SMALL), value));
143 EXPECT_FALSE(ConvertColorMode(static_cast<Print_ColorMode>(TEST_VALUE_LARGE), value));
144 EXPECT_TRUE(ConvertColorMode(COLOR_MODE_AUTO, value));
145 EXPECT_EQ(value, static_cast<uint32_t>(COLOR_MODE_AUTO));
146 EXPECT_TRUE(ConvertColorMode(COLOR_MODE_MONOCHROME, value));
147 EXPECT_EQ(value, static_cast<uint32_t>(COLOR_MODE_MONOCHROME));
148
149 EXPECT_FALSE(ConvertDuplexMode(static_cast<Print_DuplexMode>(TEST_VALUE_SMALL), value));
150 EXPECT_FALSE(ConvertDuplexMode(static_cast<Print_DuplexMode>(TEST_VALUE_LARGE), value));
151 EXPECT_TRUE(ConvertDuplexMode(DUPLEX_MODE_ONE_SIDED, value));
152 EXPECT_EQ(value, static_cast<uint32_t>(DUPLEX_MODE_ONE_SIDED));
153 EXPECT_TRUE(ConvertDuplexMode(DUPLEX_MODE_TWO_SIDED_SHORT_EDGE, value));
154 EXPECT_EQ(value, static_cast<uint32_t>(DUPLEX_MODE_TWO_SIDED_SHORT_EDGE));
155
156 EXPECT_FALSE(ConvertQuality(static_cast<Print_Quality>(TEST_VALUE_SMALL), value));
157 EXPECT_FALSE(ConvertQuality(static_cast<Print_Quality>(TEST_VALUE_LARGE), value));
158 EXPECT_TRUE(ConvertQuality(Print_Quality::PRINT_QUALITY_DRAFT, value));
159 EXPECT_EQ(value, static_cast<uint32_t>(Print_Quality::PRINT_QUALITY_DRAFT));
160 EXPECT_TRUE(ConvertQuality(Print_Quality::PRINT_QUALITY_HIGH, value));
161 EXPECT_EQ(value, static_cast<uint32_t>(Print_Quality::PRINT_QUALITY_HIGH));
162 }
163
164 HWTEST_F(VendorHelperTest, VendorHelperTest_0004, TestSize.Level1)
165 {
166 Print_PrinterState state = PRINTER_UNAVAILABLE;
167 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"0\"}", state));
168 EXPECT_EQ(state, PRINTER_IDLE);
169 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"1\",\"reason\":\"none\"}", state));
170 EXPECT_EQ(state, PRINTER_BUSY);
171 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"2\",\"reason\":\"shutdown\"}", state));
172 EXPECT_EQ(state, PRINTER_UNAVAILABLE);
173 }
174
175 HWTEST_F(VendorHelperTest, VendorHelperTest_0005, TestSize.Level1)
176 {
177 Print_DiscoveryItem discoveryItem = {0};
178 LogDiscoveryItem(nullptr);
179 LogDiscoveryItem(&discoveryItem);
180 EXPECT_EQ(discoveryItem.printerId, nullptr);
181 EXPECT_EQ(discoveryItem.printerName, nullptr);
182 EXPECT_EQ(discoveryItem.description, nullptr);
183 EXPECT_EQ(discoveryItem.location, nullptr);
184 EXPECT_EQ(discoveryItem.makeAndModel, nullptr);
185 EXPECT_EQ(discoveryItem.printerUri, nullptr);
186 EXPECT_EQ(discoveryItem.printerUuid, nullptr);
187 BuildDiscoveryItem(discoveryItem);
188 LogDiscoveryItem(&discoveryItem);
189 LogDiscoveryItem(&discoveryItem);
190 EXPECT_EQ(discoveryItem.printerId, "printerId");
191 EXPECT_EQ(discoveryItem.printerName, "printerName");
192 EXPECT_EQ(discoveryItem.description, "description");
193 EXPECT_EQ(discoveryItem.location, "location");
194 EXPECT_EQ(discoveryItem.makeAndModel, "makeAndModel");
195 EXPECT_EQ(discoveryItem.printerUri, "printerUri");
196 EXPECT_EQ(discoveryItem.printerUuid, "printerUuid");
197 }
198
199 HWTEST_F(VendorHelperTest, VendorHelperTest_0006, TestSize.Level1)
200 {
201 Print_PrinterCapability capability = {0};
202 LogPageCapability(nullptr);
203 LogOtherCapability(nullptr);
204 LogPageCapability(&capability);
205 LogOtherCapability(&capability);
206 EXPECT_EQ(capability.supportedPageSizes, nullptr);
207 EXPECT_EQ(capability.supportedMediaTypes, nullptr);
208 EXPECT_EQ(capability.supportedPaperSources, nullptr);
209
210 BuildCapability(capability);
211 LogPageCapability(&capability);
212 LogOtherCapability(&capability);
213 EXPECT_NE(capability.supportedPageSizes, nullptr);
214 EXPECT_EQ(capability.supportedMediaTypes, "{\"a\",\"b\",\"c\"}");
215 EXPECT_EQ(capability.supportedPaperSources, "{\"a\",\"b\",\"c\"}");
216 }
217
218 HWTEST_F(VendorHelperTest, VendorHelperTest_0007, TestSize.Level1)
219 {
220 LogDefaultValue(nullptr);
221 Print_DefaultValue defaultValue;
222 BuildDefaultValue(defaultValue);
223 LogDefaultValue(&defaultValue);
224 EXPECT_EQ(defaultValue.defaultMediaType, "a");
225 EXPECT_EQ(defaultValue.defaultPageSizeId, "ISO_A4");
226 EXPECT_EQ(defaultValue.defaultPaperSource, "a");
227 EXPECT_EQ(defaultValue.otherDefaultValues, "default");
228 }
229
230 HWTEST_F(VendorHelperTest, VendorHelperTest_0008, TestSize.Level1)
231 {
232 Print_PropertyList propertyList = {0};
233 LogProperties(nullptr);
234 LogProperties(&propertyList);
235 EXPECT_EQ(propertyList.list, nullptr);
236 }
237
238 HWTEST_F(VendorHelperTest, VendorHelperTest_0009, TestSize.Level1)
239 {
240 auto printerInfo = ConvertVendorCapabilityToPrinterInfo(nullptr, nullptr, nullptr);
241 EXPECT_EQ(printerInfo, nullptr);
242 Print_DiscoveryItem discoveryItem = {0};
243 printerInfo = ConvertVendorCapabilityToPrinterInfo(&discoveryItem, nullptr, nullptr);
244 EXPECT_EQ(printerInfo, nullptr);
245 BuildDiscoveryItem(discoveryItem);
246 Print_PrinterCapability capability = {0};
247 BuildCapability(capability);
248 Print_DefaultValue defaultValue;
249 BuildDefaultValue(defaultValue);
250 printerInfo = ConvertVendorCapabilityToPrinterInfo(&discoveryItem, &capability, &defaultValue);
251 EXPECT_NE(printerInfo, nullptr);
252 }
253
254 HWTEST_F(VendorHelperTest, VendorHelperTest_0010, TestSize.Level1)
255 {
256 std::vector<std::string> stringVector;
257 Print_StringList list = {0};
258 EXPECT_FALSE(ConvertStringVectorToStringList(stringVector, list));
259 stringVector.push_back("test");
260 EXPECT_TRUE(ConvertStringVectorToStringList(stringVector, list));
261 EXPECT_EQ(list.count, stringVector.size());
262 ReleaseStringList(list);
263 }
264
265 HWTEST_F(VendorHelperTest, VendorHelperTest_0011, TestSize.Level1)
266 {
267 PrinterCapability printerCap;
268 Print_PrinterCapability capability = {0};
269 BuildCapability(capability);
270 EXPECT_FALSE(UpdateResolutionDefaultValue(printerCap, nullptr));
271 EXPECT_FALSE(UpdateCopiesCapability(printerCap, nullptr, nullptr));
272 EXPECT_FALSE(UpdateCopiesCapability(printerCap, &capability, nullptr));
273 EXPECT_FALSE(UpdateOrientationCapability(printerCap, nullptr, nullptr));
274 EXPECT_FALSE(UpdateOrientationCapability(printerCap, &capability, nullptr));
275 EXPECT_FALSE(UpdateMediaCapability(printerCap, nullptr, nullptr));
276 EXPECT_FALSE(UpdateMediaCapability(printerCap, &capability, nullptr));
277 EXPECT_FALSE(UpdateMarginCapability(printerCap, nullptr));
278 }
279
280 HWTEST_F(VendorHelperTest, VendorHelperTest_0012, TestSize.Level1)
281 {
282 PrinterInfo printerInfo;
283 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, nullptr));
284 Print_DiscoveryItem discoveryItem;
285 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
286 discoveryItem.printerId = "printerId";
287 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
288 discoveryItem.printerName = "printerName";
289 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
290 discoveryItem.description = "description";
291 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
292 discoveryItem.printerUri = "printerUri";
293 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
294 discoveryItem.makeAndModel = "makeAndModel";
295 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
296 discoveryItem.detailInfo = "detailInfo";
297 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
298 discoveryItem.detailInfo ="{\"key\":\"value\"}";
299 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
300 discoveryItem.detailInfo = "{\"bsunidriver_support\": \"false\"}";
301 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
302 }
303 } // namespace Print
304 } // namespace OHOS