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 #define private public
18 #define protected public
19 #include "vendor_bsuni_driver.h"
20 #undef private
21 #undef protected
22 #include "print_constant.h"
23 #include "print_log.h"
24 #include "mock/mock_bsuni_driver.h"
25 #include "mock/mock_vendor_manager.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace {
31 const std::string PRINTER_TEST_IP = "192.168.2.222";
32 const uint32_t ISO_A3_WIDTH = 11690;
33 const uint32_t ISO_A3_HEIGHT = 16540;
34 const uint32_t ISO_A4_WIDTH = 8268;
35 const uint32_t ISO_A4_HEIGHT = 11692;
36 const uint32_t DPI_A = 300;
37 const uint32_t DPI_B = 600;
38 const uint32_t DEFAULT_COUNT = 2;
39 const uint32_t TEST_MAX_COPIES = 99;
40 const int WAIT_TIME_MS = 100;
BuildDiscoveryItem(Print_DiscoveryItem & discoveryItem)41 static void BuildDiscoveryItem(Print_DiscoveryItem &discoveryItem)
42 {
43 discoveryItem.printerId = "printerId";
44 discoveryItem.printerName = "printerName";
45 discoveryItem.description = "description";
46 discoveryItem.location = "location";
47 discoveryItem.makeAndModel = "makeAndModel";
48 discoveryItem.printerUri = "printerUri";
49 discoveryItem.printerUuid = "printerUuid";
50 }
51
BuildCapability(Print_PrinterCapability & capability)52 static void BuildCapability(Print_PrinterCapability &capability)
53 {
54 static Print_ColorMode colorModes[] = {COLOR_MODE_MONOCHROME, COLOR_MODE_COLOR};
55 static Print_DuplexMode duplexModes[] = {DUPLEX_MODE_ONE_SIDED, DUPLEX_MODE_TWO_SIDED_SHORT_EDGE};
56 static Print_PageSize pageSizes[] = {{"ISO_A4", "iso_a4_210x297mm", ISO_A4_WIDTH, ISO_A4_HEIGHT},
57 {"ISO_A3", "iso_a3", ISO_A3_WIDTH, ISO_A3_HEIGHT},
58 {"ISO_A4", "iso_a4", ISO_A4_WIDTH, ISO_A4_HEIGHT}};
59 static Print_Quality qualities[] = {Print_Quality::PRINT_QUALITY_DRAFT, Print_Quality::PRINT_QUALITY_HIGH};
60 static Print_Resolution resolutions[] = {{DPI_A, DPI_A}, {DPI_B, DPI_B}};
61 static Print_OrientationMode orientations[] = {ORIENTATION_MODE_PORTRAIT, ORIENTATION_MODE_NONE};
62 capability.supportedColorModesCount = DEFAULT_COUNT;
63 capability.supportedColorModes = colorModes;
64 capability.supportedDuplexModesCount = DEFAULT_COUNT;
65 capability.supportedDuplexModes = duplexModes;
66 capability.supportedPageSizesCount = DEFAULT_COUNT + 1;
67 capability.supportedPageSizes = pageSizes;
68 capability.supportedMediaTypes = "{\"a\",\"b\",\"c\"}";
69 capability.supportedQualitiesCount = DEFAULT_COUNT;
70 capability.supportedQualities = qualities;
71 capability.supportedPaperSources = "{\"a\",\"b\",\"c\"}";
72 capability.supportedCopies = TEST_MAX_COPIES;
73 capability.supportedResolutionsCount = DEFAULT_COUNT;
74 capability.supportedResolutions = resolutions;
75 capability.supportedOrientationsCount = DEFAULT_COUNT;
76 capability.supportedOrientations = orientations;
77 capability.advancedCapability = "advance info";
78 }
79
BuildDefaultValue(Print_DefaultValue & defaultValue)80 static void BuildDefaultValue(Print_DefaultValue &defaultValue)
81 {
82 defaultValue.defaultColorMode = COLOR_MODE_MONOCHROME;
83 defaultValue.defaultDuplexMode = DUPLEX_MODE_ONE_SIDED;
84 defaultValue.defaultMediaType = "a";
85 defaultValue.defaultPageSizeId = "ISO_A4";
86 defaultValue.defaultMargin = {1, 1, 1, 1};
87 defaultValue.defaultPaperSource = "a";
88 defaultValue.defaultPrintQuality = Print_Quality::PRINT_QUALITY_HIGH;
89 defaultValue.defaultCopies = 1;
90 defaultValue.defaultResolution = {DPI_A, DPI_A};
91 defaultValue.defaultOrientation = ORIENTATION_MODE_PORTRAIT;
92 defaultValue.otherDefaultValues = "default";
93 }
94 }
95
96 namespace OHOS {
97 namespace Print {
98 MockBsuniDriver *g_mockDriver = nullptr;
OnCreateTest(const Print_ServiceAbility * context)99 int32_t OnCreateTest(const Print_ServiceAbility *context)
100 {
101 if (g_mockDriver != nullptr) {
102 return g_mockDriver->OnCreate(context);
103 }
104 return 0;
105 }
OnDestroyTest()106 int32_t OnDestroyTest()
107 {
108 if (g_mockDriver != nullptr) {
109 return g_mockDriver->OnDestroy();
110 }
111 return 0;
112 }
OnStartDiscoveryTest()113 int32_t OnStartDiscoveryTest()
114 {
115 if (g_mockDriver != nullptr) {
116 return g_mockDriver->OnStartDiscovery();
117 }
118 return 0;
119 }
OnStopDiscoveryTest()120 int32_t OnStopDiscoveryTest()
121 {
122 if (g_mockDriver != nullptr) {
123 return g_mockDriver->OnStopDiscovery();
124 }
125 return 0;
126 }
OnConnectPrinterTest(const char * printerId)127 int32_t OnConnectPrinterTest(const char *printerId)
128 {
129 if (g_mockDriver != nullptr) {
130 return g_mockDriver->OnConnectPrinter(printerId);
131 }
132 return 0;
133 }
OnDisconnectPrinterTest(const char * printerId)134 int32_t OnDisconnectPrinterTest(const char *printerId)
135 {
136 if (g_mockDriver != nullptr) {
137 return g_mockDriver->OnDisconnectPrinter(printerId);
138 }
139 return 0;
140 }
OnQueryCapabilityTest(const char * printerId)141 int32_t OnQueryCapabilityTest(const char *printerId)
142 {
143 if (g_mockDriver != nullptr) {
144 return g_mockDriver->OnQueryCapability(printerId);
145 }
146 return 0;
147 }
OnQueryCapabilityByIpTest(const char * printerIp,const char * protocol)148 int32_t OnQueryCapabilityByIpTest(const char *printerIp, const char *protocol)
149 {
150 if (g_mockDriver != nullptr) {
151 return g_mockDriver->OnQueryCapabilityByIp(printerIp, protocol);
152 }
153 return 0;
154 }
OnQueryPropertiesTest(const char * printerId,const Print_StringList * propertyKeyList)155 int32_t OnQueryPropertiesTest(const char *printerId, const Print_StringList *propertyKeyList)
156 {
157 if (g_mockDriver != nullptr) {
158 return g_mockDriver->OnQueryProperties(printerId, propertyKeyList);
159 }
160 return 0;
161 }
162 using MockTestFunc =
163 std::function<void(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager)>;
164 class VendorBsuniDriverTest : public testing::Test {
165 public:
166 static void SetUpTestCase(void);
167 static void TearDownTestCase(void);
168 void SetUp();
169 void TearDown();
170 void DoMockTest(MockTestFunc func);
171 };
172
SetUpTestCase(void)173 void VendorBsuniDriverTest::SetUpTestCase(void) {}
174
TearDownTestCase(void)175 void VendorBsuniDriverTest::TearDownTestCase(void) {}
176
SetUp(void)177 void VendorBsuniDriverTest::SetUp(void)
178 {
179 static int32_t testNo = 0;
180 PRINT_HILOGI("VendorBsuniDriverTest_%{public}d", ++testNo);
181 }
182
TearDown(void)183 void VendorBsuniDriverTest::TearDown(void) {}
184
DoMockTest(MockTestFunc func)185 void VendorBsuniDriverTest::DoMockTest(MockTestFunc func)
186 {
187 if (func == nullptr) {
188 PRINT_HILOGE("test func is null");
189 return;
190 }
191 MockBsuniDriver mockDriver;
192 g_mockDriver = &mockDriver;
193 MockVendorManager mockManager;
194 VendorBsuniDriver vendorDriver;
195 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
196 .onDestroy = OnDestroyTest,
197 .onStartDiscovery = OnStartDiscoveryTest,
198 .onStopDiscovery = OnStopDiscoveryTest,
199 .onConnectPrinter = OnConnectPrinterTest,
200 .onDisconnectPrinter = OnDisconnectPrinterTest,
201 .onQueryCapability = OnQueryCapabilityTest,
202 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
203 .onQueryProperties = OnQueryPropertiesTest };
204 vendorDriver.vendorExtension = &vendorExtension;
205 vendorDriver.vendorManager = &mockManager;
206 EXPECT_CALL(mockDriver, OnCreate(_)).Times(1).WillOnce(Return(0));
207 EXPECT_CALL(mockDriver, OnDestroy()).Times(1).WillOnce(Return(0));
208 vendorDriver.OnCreate();
209 func(vendorDriver, mockDriver, mockManager);
210 ThreadSyncWait syncWait;
211 syncWait.Wait(WAIT_TIME_MS);
212 vendorDriver.OnDestroy();
213 vendorDriver.vendorExtension = nullptr;
214 g_mockDriver = nullptr;
215 }
216
217 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0001, TestSize.Level1)
218 {
219 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(nullptr));
220 g_mockDriver = nullptr;
221 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
222 .onDestroy = OnDestroyTest,
223 .onStartDiscovery = OnStartDiscoveryTest,
224 .onStopDiscovery = OnStopDiscoveryTest,
225 .onConnectPrinter = OnConnectPrinterTest,
226 .onDisconnectPrinter = OnDisconnectPrinterTest,
227 .onQueryCapability = OnQueryCapabilityTest,
228 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
229 .onQueryProperties = OnQueryPropertiesTest };
230 EXPECT_TRUE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
231 vendorExtension.onQueryProperties = nullptr;
232 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
233 vendorExtension.onQueryCapabilityByIp = nullptr;
234 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
235 vendorExtension.onQueryCapability = nullptr;
236 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
237 vendorExtension.onDisconnectPrinter = nullptr;
238 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
239 vendorExtension.onConnectPrinter = nullptr;
240 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
241 vendorExtension.onStopDiscovery = nullptr;
242 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
243 vendorExtension.onStartDiscovery = nullptr;
244 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
245 vendorExtension.onDestroy = nullptr;
246 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
247 vendorExtension.onCreate = nullptr;
248 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
249 }
250
251 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0002, TestSize.Level2)
252 {
253 MockVendorManager mockManager;
254 VendorBsuniDriver vendorDriver;
255 EXPECT_EQ(vendorDriver.Init(&mockManager), false);
256 vendorDriver.UnInit();
257 }
258
259 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0003, TestSize.Level2)
260 {
261 std::vector<std::string> propertyKeys;
262 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_STATE);
263 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_SUPPLIES);
264 VendorBsuniDriver vendorDriver;
265 vendorDriver.OnCreate();
266 vendorDriver.OnDestroy();
267 vendorDriver.OnStartDiscovery();
268 vendorDriver.OnStopDiscovery();
269 EXPECT_EQ(vendorDriver.OnQueryCapability("", 0), false);
270 EXPECT_EQ(vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp"), false);
271 EXPECT_EQ(vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys), false);
272 Print_VendorExtension vendorExtension = { 0 };
273 vendorDriver.vendorExtension = &vendorExtension;
274 vendorDriver.OnCreate();
275 vendorDriver.OnDestroy();
276 vendorDriver.OnStartDiscovery();
277 vendorDriver.OnStopDiscovery();
278 EXPECT_EQ(vendorDriver.OnQueryCapability("", 0), false);
279 EXPECT_EQ(vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp"), false);
280 EXPECT_EQ(vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys), false);
281 }
282
283 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0004, TestSize.Level2)
284 {
285 VendorBsuniDriver vendorDriver;
286 vendorDriver.OnCreate();
287 vendorDriver.OnDestroy();
288 vendorDriver.OnStartDiscovery();
289 EXPECT_EQ(vendorDriver.OnQueryCapability(PRINTER_TEST_IP, 0), false);
290 vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp");
291 std::vector<std::string> propertyKeys;
292 vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys);
293 }
294
295 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0005, TestSize.Level2)
296 {
297 VendorBsuniDriver vendorDriver;
298 g_mockDriver = nullptr;
299 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
300 .onDestroy = OnDestroyTest,
301 .onStartDiscovery = OnStartDiscoveryTest,
302 .onStopDiscovery = OnStopDiscoveryTest,
303 .onConnectPrinter = OnConnectPrinterTest,
304 .onDisconnectPrinter = OnDisconnectPrinterTest,
305 .onQueryCapability = OnQueryCapabilityTest,
306 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
307 .onQueryProperties = OnQueryPropertiesTest };
308 vendorDriver.vendorExtension = &vendorExtension;
309 vendorDriver.OnCreate();
310 vendorDriver.OnDestroy();
311 vendorDriver.OnStartDiscovery();
312 EXPECT_EQ(vendorDriver.OnQueryCapability(PRINTER_TEST_IP, 0), true);
313 vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp");
314 std::vector<std::string> propertyKeys;
315 vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys);
316 }
317
318 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0006, TestSize.Level2)
319 {
320 MockTestFunc testFunc = [this](VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver,
__anon71fabd580202(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager) 321 MockVendorManager &mockManager) {
322 EXPECT_CALL(mockDriver, OnStartDiscovery()).Times(1).WillOnce(Return(0));
323 EXPECT_CALL(mockDriver, OnStopDiscovery()).Times(1).WillOnce(Return(0));
324 vendorDriver.OnStartDiscovery();
325 vendorDriver.OnStopDiscovery();
326 };
327 DoMockTest(testFunc);
328 }
329
330 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0007, TestSize.Level2)
331 {
332 MockTestFunc testFunc = [this](VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver,
__anon71fabd580302(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager) 333 MockVendorManager &mockManager) {
334 EXPECT_CALL(mockDriver, OnQueryCapability(_)).Times(2).WillOnce(Return(1)).WillRepeatedly(Return(0));
335 std::string printerId = PRINTER_TEST_IP;
336 EXPECT_EQ(vendorDriver.OnQueryCapability(printerId, 0), false);
337 EXPECT_EQ(vendorDriver.OnQueryCapability(printerId, 0), true);
338 };
339 DoMockTest(testFunc);
340 }
341
342 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0008, TestSize.Level2)
343 {
344 MockTestFunc testFunc = [this](VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver,
__anon71fabd580402(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager) 345 MockVendorManager &mockManager) {
346 EXPECT_CALL(mockDriver, OnQueryCapabilityByIp(_, _)).Times(2).WillOnce(Return(1)).WillRepeatedly(Return(0));
347 std::string printerIp = PRINTER_TEST_IP;
348 vendorDriver.OnQueryCapabilityByIp(printerIp, "ipp");
349 vendorDriver.OnQueryCapabilityByIp(printerIp, "ipp");
350 };
351 DoMockTest(testFunc);
352 }
353
354 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0009, TestSize.Level2)
355 {
356 MockTestFunc testFunc = [this](VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver,
__anon71fabd580502(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager) 357 MockVendorManager &mockManager) {
358 EXPECT_CALL(mockManager, RemovePrinterFromCups(_, _)).WillRepeatedly(Return(0));
359 EXPECT_CALL(mockManager, RemovePrinterFromDiscovery(_, _)).WillRepeatedly(Return(0));
360 std::string printerIp = PRINTER_TEST_IP;
361 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
362 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(nullptr, nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
363 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(nullptr), EXTENSION_INVALID_PARAMETER);
364 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(printerIp.c_str(), nullptr), EXTENSION_INVALID_PARAMETER);
365 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(printerIp.c_str()), EXTENSION_ERROR_NONE);
366 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(nullptr), EXTENSION_INVALID_PARAMETER);
367 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(printerIp.c_str()), EXTENSION_ERROR_NONE);
368 };
369 DoMockTest(testFunc);
370 }
371
372 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_00010, TestSize.Level2)
373 {
374 VendorBsuniDriver::SetDriverWrapper(nullptr);
375 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(nullptr), EXTENSION_INVALID_PARAMETER);
376 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(nullptr), EXTENSION_INVALID_PARAMETER);
377 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(nullptr, nullptr, nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
378 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(nullptr), EXTENSION_INVALID_PARAMETER);
379 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(nullptr, nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
380 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
381 }
382
383 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0011, TestSize.Level2)
384 {
385 VendorBsuniDriver::SetDriverWrapper(nullptr);
386 Print_DiscoveryItem discoveryItem = {0};
387 Print_PrinterCapability capability = {0};
388 Print_DefaultValue defaultValue;
389 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(&discoveryItem), EXTENSION_INVALID_PARAMETER);
390 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, nullptr, nullptr, nullptr),
391 EXTENSION_INVALID_PARAMETER);
392 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), nullptr), EXTENSION_INVALID_PARAMETER);
393 }
394
395 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0012, TestSize.Level2)
396 {
397 VendorBsuniDriver::SetDriverWrapper(nullptr);
398 Print_DiscoveryItem discoveryItem = {0};
399 BuildDiscoveryItem(discoveryItem);
400 Print_PrinterCapability capability = {0};
401 BuildCapability(capability);
402 Print_DefaultValue defaultValue;
403 BuildDefaultValue(defaultValue);
404 Print_PropertyList propertyList = {0};
405 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(&discoveryItem), EXTENSION_ERROR_CALLBACK_NULL);
406 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_NULL);
407 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, nullptr, nullptr, nullptr),
408 EXTENSION_INVALID_PARAMETER);
409 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, nullptr, nullptr),
410 EXTENSION_INVALID_PARAMETER);
411 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
412 EXTENSION_ERROR_CALLBACK_NULL);
413 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_NULL);
414 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(&discoveryItem, &capability, &defaultValue),
415 EXTENSION_ERROR_CALLBACK_NULL);
416 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
417 EXTENSION_ERROR_CALLBACK_NULL);
418 }
419
420 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0013, TestSize.Level2)
421 {
422 VendorBsuniDriver vendorDriver;
423 VendorBsuniDriver::SetDriverWrapper(&vendorDriver);
424 Print_DiscoveryItem discoveryItem = {0};
425 BuildDiscoveryItem(discoveryItem);
426 Print_PrinterCapability capability = {0};
427 BuildCapability(capability);
428 Print_DefaultValue defaultValue;
429 BuildDefaultValue(defaultValue);
430 Print_PropertyList propertyList = {0};
431 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(&discoveryItem), EXTENSION_ERROR_CALLBACK_FAIL);
432 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_FAIL);
433 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
434 EXTENSION_ERROR_CALLBACK_FAIL);
435 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
436 EXTENSION_ERROR_CALLBACK_FAIL);
437 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_FAIL);
438 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(&discoveryItem, &capability, &defaultValue),
439 EXTENSION_ERROR_CALLBACK_FAIL);
440 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
441 EXTENSION_ERROR_NONE);
442 propertyList.count = 1;
443 propertyList.list = new Print_Property[1];
444 propertyList.list[0].key = PRINTER_PROPERTY_KEY_CUPS_PPD_FILE;
445 propertyList.list[0].value = "ppd";
446 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
447 EXTENSION_ERROR_CALLBACK_FAIL);
448 delete[] propertyList.list;
449 propertyList.list = nullptr;
450 }
451
452 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0014, TestSize.Level2)
453 {
454 VendorBsuniDriver vendorDriver;
455 vendorDriver.opQueue.Run();
456 VendorBsuniDriver::SetDriverWrapper(&vendorDriver);
457 Print_DiscoveryItem discoveryItem = {0};
458 BuildDiscoveryItem(discoveryItem);
459 Print_PrinterCapability capability = {0};
460 BuildCapability(capability);
461 Print_DefaultValue defaultValue;
462 BuildDefaultValue(defaultValue);
463 Print_PropertyList propertyList = {0};
464 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(&discoveryItem), EXTENSION_ERROR_NONE);
465 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_NONE);
466 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
467 EXTENSION_ERROR_NONE);
468 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, "test_ppd_data"),
469 EXTENSION_ERROR_NONE);
470 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_NONE);
471 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(&discoveryItem, &capability, &defaultValue),
472 EXTENSION_ERROR_NONE);
473 propertyList.count = DEFAULT_COUNT;
474 propertyList.list = new Print_Property[DEFAULT_COUNT];
475 propertyList.list[0].key = PRINTER_PROPERTY_KEY_CUPS_PPD_FILE;
476 propertyList.list[0].value = "ppd";
477 propertyList.list[1].key = PRINTER_PROPERTY_KEY_DEVICE_STATE;
478 propertyList.list[1].value = "{\"state\":\"1\",\"reason\":\"none\"}";
479 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
480 EXTENSION_ERROR_NONE);
481 delete[] propertyList.list;
482 propertyList.list = nullptr;
483 vendorDriver.opQueue.Stop();
484 }
485
486 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0015, TestSize.Level2)
487 {
488 VendorBsuniDriver vendorDriver;
489 MockVendorManager mockManager;
490 vendorDriver.vendorManager = &mockManager;
491 vendorDriver.opQueue.Run();
492 VendorBsuniDriver::SetDriverWrapper(&vendorDriver);
493 Print_DiscoveryItem discoveryItem = {0};
494 BuildDiscoveryItem(discoveryItem);
495 Print_PrinterCapability capability = {0};
496 BuildCapability(capability);
497 Print_DefaultValue defaultValue;
498 BuildDefaultValue(defaultValue);
499 Print_PropertyList propertyList = {0};
500 EXPECT_CALL(mockManager, AddPrinterToDiscovery(_, _)).WillRepeatedly(Return(0));
501 EXPECT_CALL(mockManager, RemovePrinterFromDiscovery(_, _)).WillRepeatedly(Return(0));
502 EXPECT_CALL(mockManager, UpdatePrinterToDiscovery(_, _)).WillOnce(Return(1)).WillRepeatedly(Return(0));
503 EXPECT_CALL(mockManager, AddPrinterToCupsWithPpd(_, _, _, _)).WillOnce(Return(1)).WillRepeatedly(Return(0));
504 EXPECT_CALL(mockManager, RemovePrinterFromCups(_, _)).WillRepeatedly(Return(0));
505 EXPECT_CALL(mockManager, OnPrinterPpdQueried(_, _, _, _)).WillOnce(Return(false)).WillRepeatedly(Return(true));
506 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(&discoveryItem), EXTENSION_ERROR_NONE);
507 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_NONE);
508 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
509 EXTENSION_ERROR_NONE);
510 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, nullptr),
511 EXTENSION_ERROR_NONE);
512 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(&discoveryItem, &capability, &defaultValue, "test_ppd_data"),
513 EXTENSION_ERROR_NONE);
514 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_NONE);
515 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(&discoveryItem, &capability, &defaultValue),
516 EXTENSION_ERROR_NONE);
517 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(&discoveryItem, &capability, &defaultValue),
518 EXTENSION_ERROR_NONE);
519 propertyList.count = DEFAULT_COUNT;
520 propertyList.list = new Print_Property[DEFAULT_COUNT];
521 propertyList.list[0].key = PRINTER_PROPERTY_KEY_CUPS_PPD_FILE;
522 propertyList.list[0].value = "ppd";
523 propertyList.list[1].key = PRINTER_PROPERTY_KEY_DEVICE_STATE;
524 propertyList.list[1].value = "{\"state\":\"1\",\"reason\":\"none\"}";
525 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
526 EXTENSION_ERROR_NONE);
527 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), &propertyList),
528 EXTENSION_ERROR_NONE);
529 delete[] propertyList.list;
530 propertyList.list = nullptr;
531 ThreadSyncWait syncWait;
532 syncWait.Wait(WAIT_TIME_MS);
533 vendorDriver.opQueue.Stop();
534 }
535 } // namespace Print
536 } // namespace OHOS
537