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_FALSE(ConvertStringToPrinterState("-1", state));
168 EXPECT_FALSE(ConvertStringToPrinterState("\"state\"", state));
169 EXPECT_FALSE(ConvertStringToPrinterState("{\"state\":2}", state));
170 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"0\"}", state));
171 EXPECT_EQ(state, PRINTER_IDLE);
172 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"1\",\"reason\":\"none\"}", state));
173 EXPECT_EQ(state, PRINTER_BUSY);
174 EXPECT_TRUE(ConvertStringToPrinterState("{\"state\":\"2\",\"reason\":\"shutdown\"}", state));
175 EXPECT_EQ(state, PRINTER_UNAVAILABLE);
176 }
177
178 HWTEST_F(VendorHelperTest, VendorHelperTest_0005, TestSize.Level1)
179 {
180 Print_DiscoveryItem discoveryItem = {0};
181 LogDiscoveryItem(nullptr);
182 LogDiscoveryItem(&discoveryItem);
183 EXPECT_EQ(discoveryItem.printerId, nullptr);
184 EXPECT_EQ(discoveryItem.printerName, nullptr);
185 EXPECT_EQ(discoveryItem.description, nullptr);
186 EXPECT_EQ(discoveryItem.location, nullptr);
187 EXPECT_EQ(discoveryItem.makeAndModel, nullptr);
188 EXPECT_EQ(discoveryItem.printerUri, nullptr);
189 EXPECT_EQ(discoveryItem.printerUuid, nullptr);
190 BuildDiscoveryItem(discoveryItem);
191 LogDiscoveryItem(&discoveryItem);
192 LogDiscoveryItem(&discoveryItem);
193 EXPECT_EQ(discoveryItem.printerId, "printerId");
194 EXPECT_EQ(discoveryItem.printerName, "printerName");
195 EXPECT_EQ(discoveryItem.description, "description");
196 EXPECT_EQ(discoveryItem.location, "location");
197 EXPECT_EQ(discoveryItem.makeAndModel, "makeAndModel");
198 EXPECT_EQ(discoveryItem.printerUri, "printerUri");
199 EXPECT_EQ(discoveryItem.printerUuid, "printerUuid");
200 }
201
202 HWTEST_F(VendorHelperTest, VendorHelperTest_0006, TestSize.Level1)
203 {
204 Print_PrinterCapability capability = {0};
205 LogPageCapability(nullptr);
206 LogOtherCapability(nullptr);
207 LogPageCapability(&capability);
208 LogOtherCapability(&capability);
209 EXPECT_EQ(capability.supportedPageSizes, nullptr);
210 EXPECT_EQ(capability.supportedMediaTypes, nullptr);
211 EXPECT_EQ(capability.supportedPaperSources, nullptr);
212
213 BuildCapability(capability);
214 LogPageCapability(&capability);
215 LogOtherCapability(&capability);
216 EXPECT_NE(capability.supportedPageSizes, nullptr);
217 EXPECT_EQ(capability.supportedMediaTypes, "{\"a\",\"b\",\"c\"}");
218 EXPECT_EQ(capability.supportedPaperSources, "{\"a\",\"b\",\"c\"}");
219 }
220
221 HWTEST_F(VendorHelperTest, VendorHelperTest_0007, TestSize.Level1)
222 {
223 LogDefaultValue(nullptr);
224 Print_DefaultValue defaultValue;
225 BuildDefaultValue(defaultValue);
226 LogDefaultValue(&defaultValue);
227 EXPECT_EQ(defaultValue.defaultMediaType, "a");
228 EXPECT_EQ(defaultValue.defaultPageSizeId, "ISO_A4");
229 EXPECT_EQ(defaultValue.defaultPaperSource, "a");
230 EXPECT_EQ(defaultValue.otherDefaultValues, "default");
231 }
232
233 HWTEST_F(VendorHelperTest, VendorHelperTest_0008, TestSize.Level1)
234 {
235 Print_PropertyList propertyList = {0};
236 LogProperties(nullptr);
237 LogProperties(&propertyList);
238 EXPECT_EQ(propertyList.list, nullptr);
239 }
240
241 HWTEST_F(VendorHelperTest, VendorHelperTest_0009, TestSize.Level1)
242 {
243 auto printerInfo = ConvertVendorCapabilityToPrinterInfo(nullptr, nullptr, nullptr);
244 EXPECT_EQ(printerInfo, nullptr);
245 Print_DiscoveryItem discoveryItem = {0};
246 printerInfo = ConvertVendorCapabilityToPrinterInfo(&discoveryItem, nullptr, nullptr);
247 EXPECT_EQ(printerInfo, nullptr);
248 BuildDiscoveryItem(discoveryItem);
249 Print_PrinterCapability capability = {0};
250 BuildCapability(capability);
251 Print_DefaultValue defaultValue;
252 BuildDefaultValue(defaultValue);
253 printerInfo = ConvertVendorCapabilityToPrinterInfo(&discoveryItem, &capability, &defaultValue);
254 EXPECT_NE(printerInfo, nullptr);
255 }
256
257 HWTEST_F(VendorHelperTest, VendorHelperTest_0010, TestSize.Level1)
258 {
259 std::vector<std::string> stringVector;
260 Print_StringList list = {0};
261 EXPECT_FALSE(ConvertStringVectorToStringList(stringVector, list));
262 stringVector.push_back("test");
263 EXPECT_TRUE(ConvertStringVectorToStringList(stringVector, list));
264 EXPECT_EQ(list.count, stringVector.size());
265 ReleaseStringList(list);
266 }
267
268 HWTEST_F(VendorHelperTest, VendorHelperTest_0011, TestSize.Level1)
269 {
270 PrinterCapability printerCap;
271 Print_PrinterCapability capability = {0};
272 BuildCapability(capability);
273 EXPECT_FALSE(UpdateResolutionDefaultValue(printerCap, nullptr));
274 EXPECT_FALSE(UpdateCopiesCapability(printerCap, nullptr, nullptr));
275 EXPECT_FALSE(UpdateCopiesCapability(printerCap, &capability, nullptr));
276 EXPECT_FALSE(UpdateOrientationCapability(printerCap, nullptr, nullptr));
277 EXPECT_FALSE(UpdateOrientationCapability(printerCap, &capability, nullptr));
278 EXPECT_FALSE(UpdateMediaCapability(printerCap, nullptr, nullptr));
279 EXPECT_FALSE(UpdateMediaCapability(printerCap, &capability, nullptr));
280 EXPECT_FALSE(UpdateMarginCapability(printerCap, nullptr));
281 }
282
283 HWTEST_F(VendorHelperTest, VendorHelperTest_0012, TestSize.Level1)
284 {
285 PrinterInfo printerInfo;
286 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, nullptr));
287 Print_DiscoveryItem discoveryItem;
288 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
289 discoveryItem.printerId = "printerId";
290 EXPECT_FALSE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
291 discoveryItem.printerName = "printerName";
292 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
293 discoveryItem.description = "description";
294 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
295 discoveryItem.printerUri = "printerUri";
296 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
297 discoveryItem.makeAndModel = "makeAndModel";
298 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
299 discoveryItem.detailInfo = "detailInfo";
300 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
301 discoveryItem.detailInfo ="{\"key\":\"value\"}";
302 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
303 discoveryItem.detailInfo = "{\"bsunidriver_support\": \"false\"}";
304 EXPECT_TRUE(UpdatePrinterInfoWithDiscovery(printerInfo, &discoveryItem));
305 }
306 } // namespace Print
307 } // namespace OHOS