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 }
33
34 namespace OHOS {
35 namespace Print {
36 MockBsuniDriver *g_mockDriver = nullptr;
OnCreateTest(const Print_ServiceAbility * context)37 int32_t OnCreateTest(const Print_ServiceAbility *context)
38 {
39 if (g_mockDriver != nullptr) {
40 return g_mockDriver->OnCreate(context);
41 }
42 return 0;
43 }
OnDestroyTest()44 int32_t OnDestroyTest()
45 {
46 if (g_mockDriver != nullptr) {
47 return g_mockDriver->OnDestroy();
48 }
49 return 0;
50 }
OnStartDiscoveryTest()51 int32_t OnStartDiscoveryTest()
52 {
53 if (g_mockDriver != nullptr) {
54 return g_mockDriver->OnStartDiscovery();
55 }
56 return 0;
57 }
OnStopDiscoveryTest()58 int32_t OnStopDiscoveryTest()
59 {
60 if (g_mockDriver != nullptr) {
61 return g_mockDriver->OnStopDiscovery();
62 }
63 return 0;
64 }
OnConnectPrinterTest(const char * printerId)65 int32_t OnConnectPrinterTest(const char *printerId)
66 {
67 if (g_mockDriver != nullptr) {
68 return g_mockDriver->OnConnectPrinter(printerId);
69 }
70 return 0;
71 }
OnDisconnectPrinterTest(const char * printerId)72 int32_t OnDisconnectPrinterTest(const char *printerId)
73 {
74 if (g_mockDriver != nullptr) {
75 return g_mockDriver->OnDisconnectPrinter(printerId);
76 }
77 return 0;
78 }
OnQueryCapabilityTest(const char * printerId)79 int32_t OnQueryCapabilityTest(const char *printerId)
80 {
81 if (g_mockDriver != nullptr) {
82 return g_mockDriver->OnQueryCapability(printerId);
83 }
84 return 0;
85 }
OnQueryCapabilityByIpTest(const char * printerIp,const char * protocol)86 int32_t OnQueryCapabilityByIpTest(const char *printerIp, const char *protocol)
87 {
88 if (g_mockDriver != nullptr) {
89 return g_mockDriver->OnQueryCapabilityByIp(printerIp, protocol);
90 }
91 return 0;
92 }
OnQueryPropertiesTest(const char * printerId,const Print_StringList * propertyKeyList)93 int32_t OnQueryPropertiesTest(const char *printerId, const Print_StringList *propertyKeyList)
94 {
95 if (g_mockDriver != nullptr) {
96 return g_mockDriver->OnQueryProperties(printerId, propertyKeyList);
97 }
98 return 0;
99 }
100 using MockTestFunc =
101 std::function<void(VendorBsuniDriver &vendorDriver, MockBsuniDriver &mockDriver, MockVendorManager &mockManager)>;
102 class VendorBsuniDriverTest : public testing::Test {
103 public:
104 static void SetUpTestCase(void);
105 static void TearDownTestCase(void);
106 void SetUp();
107 void TearDown();
108 void DoMockTest(MockTestFunc func);
109 };
110
SetUpTestCase(void)111 void VendorBsuniDriverTest::SetUpTestCase(void) {}
112
TearDownTestCase(void)113 void VendorBsuniDriverTest::TearDownTestCase(void) {}
114
SetUp(void)115 void VendorBsuniDriverTest::SetUp(void)
116 {
117 static int32_t testNo = 0;
118 PRINT_HILOGI("VendorBsuniDriverTest_%{public}d", ++testNo);
119 }
120
TearDown(void)121 void VendorBsuniDriverTest::TearDown(void) {}
122
DoMockTest(MockTestFunc func)123 void VendorBsuniDriverTest::DoMockTest(MockTestFunc func)
124 {
125 if (func == nullptr) {
126 PRINT_HILOGE("test func is null");
127 return;
128 }
129 MockBsuniDriver mockDriver;
130 g_mockDriver = &mockDriver;
131 MockVendorManager mockManager;
132 VendorBsuniDriver vendorDriver;
133 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
134 .onDestroy = OnDestroyTest,
135 .onStartDiscovery = OnStartDiscoveryTest,
136 .onStopDiscovery = OnStopDiscoveryTest,
137 .onConnectPrinter = OnConnectPrinterTest,
138 .onDisconnectPrinter = OnDisconnectPrinterTest,
139 .onQueryCapability = OnQueryCapabilityTest,
140 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
141 .onQueryProperties = OnQueryPropertiesTest };
142 vendorDriver.vendorExtension = &vendorExtension;
143 vendorDriver.vendorManager = &mockManager;
144 EXPECT_CALL(mockDriver, OnCreate(_)).Times(1).WillOnce(Return(0));
145 EXPECT_CALL(mockDriver, OnDestroy()).Times(1).WillOnce(Return(0));
146 vendorDriver.OnCreate();
147 func(vendorDriver, mockDriver, mockManager);
148 vendorDriver.OnDestroy();
149 vendorDriver.vendorExtension = nullptr;
150 g_mockDriver = nullptr;
151 }
152
153 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0001, TestSize.Level1)
154 {
155 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(nullptr));
156 g_mockDriver = nullptr;
157 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
158 .onDestroy = OnDestroyTest,
159 .onStartDiscovery = OnStartDiscoveryTest,
160 .onStopDiscovery = OnStopDiscoveryTest,
161 .onConnectPrinter = OnConnectPrinterTest,
162 .onDisconnectPrinter = OnDisconnectPrinterTest,
163 .onQueryCapability = OnQueryCapabilityTest,
164 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
165 .onQueryProperties = OnQueryPropertiesTest };
166 EXPECT_TRUE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
167 vendorExtension.onQueryProperties = nullptr;
168 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
169 vendorExtension.onQueryCapabilityByIp = nullptr;
170 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
171 vendorExtension.onQueryCapability = nullptr;
172 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
173 vendorExtension.onDisconnectPrinter = nullptr;
174 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
175 vendorExtension.onConnectPrinter = nullptr;
176 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
177 vendorExtension.onStopDiscovery = nullptr;
178 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
179 vendorExtension.onStartDiscovery = nullptr;
180 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
181 vendorExtension.onDestroy = nullptr;
182 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
183 vendorExtension.onCreate = nullptr;
184 EXPECT_FALSE(VendorBsuniDriver::CheckVendorExtension(&vendorExtension));
185 }
186
187 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0002, TestSize.Level2)
188 {
189 MockVendorManager mockManager;
190 VendorBsuniDriver vendorDriver;
191 vendorDriver.Init(&mockManager);
192 vendorDriver.UnInit();
193 }
194
195 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0003, TestSize.Level2)
196 {
197 std::vector<std::string> propertyKeys;
198 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_STATE);
199 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_SUPPLIES);
200 VendorBsuniDriver vendorDriver;
201 vendorDriver.OnCreate();
202 vendorDriver.OnDestroy();
203 vendorDriver.OnStartDiscovery();
204 vendorDriver.OnStopDiscovery();
205 EXPECT_EQ(vendorDriver.OnQueryCapability("", 0), false);
206 EXPECT_EQ(vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp"), false);
207 EXPECT_EQ(vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys), false);
208 Print_VendorExtension vendorExtension = { 0 };
209 vendorDriver.vendorExtension = &vendorExtension;
210 vendorDriver.OnCreate();
211 vendorDriver.OnDestroy();
212 vendorDriver.OnStartDiscovery();
213 vendorDriver.OnStopDiscovery();
214 EXPECT_EQ(vendorDriver.OnQueryCapability("", 0), false);
215 EXPECT_EQ(vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp"), false);
216 EXPECT_EQ(vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys), false);
217 }
218
219 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0004, TestSize.Level1)
220 {
221 std::vector<std::string> propertyKeys;
222 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_STATE);
223 propertyKeys.push_back(PRINTER_PROPERTY_KEY_DEVICE_SUPPLIES);
224 propertyKeys.push_back(PRINTER_PROPERTY_KEY_CUPS_PPD_FILE);
225 std::string printerId = PRINTER_TEST_IP;
226 VendorBsuniDriver vendorDriver;
227 EXPECT_EQ(vendorDriver.OnPrinterPropertiesQueried(printerId, nullptr), EXTENSION_ERROR_CALLBACK_NULL);
228 EXPECT_EQ(vendorDriver.OnPrinterCapabilityQueried(nullptr, nullptr, nullptr), EXTENSION_ERROR_CALLBACK_NULL);
229 MockVendorManager mockManager;
230 vendorDriver.vendorManager = &mockManager;
231 EXPECT_EQ(vendorDriver.OnPrinterPropertiesQueried(printerId, nullptr), EXTENSION_ERROR_NONE);
232 EXPECT_EQ(vendorDriver.OnPrinterCapabilityQueried(nullptr, nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
233 }
234
235 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0005, TestSize.Level2)
236 {
237 VendorBsuniDriver::SetDriverWrapper(nullptr);
238 EXPECT_EQ(VendorBsuniDriver::AddPrinterToDiscovery(nullptr), EXTENSION_INVALID_PARAMETER);
239 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(nullptr), EXTENSION_INVALID_PARAMETER);
240 EXPECT_EQ(VendorBsuniDriver::AddPrinterToCups(nullptr, nullptr, nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
241 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(nullptr), EXTENSION_INVALID_PARAMETER);
242 EXPECT_EQ(VendorBsuniDriver::OnCapabilityQueried(nullptr, nullptr, nullptr), EXTENSION_ERROR_CALLBACK_NULL);
243 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(nullptr, nullptr), EXTENSION_INVALID_PARAMETER);
244 EXPECT_EQ(VendorBsuniDriver::OnPropertiesQueried(PRINTER_TEST_IP.c_str(), nullptr), EXTENSION_ERROR_CALLBACK_NULL);
245 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromDiscovery(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_NULL);
246 EXPECT_EQ(VendorBsuniDriver::RemovePrinterFromCups(PRINTER_TEST_IP.c_str()), EXTENSION_ERROR_CALLBACK_NULL);
247 }
248
249 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0006, TestSize.Level2)
250 {
251 VendorBsuniDriver vendorDriver;
252 vendorDriver.OnCreate();
253 vendorDriver.OnDestroy();
254 vendorDriver.OnStartDiscovery();
255 vendorDriver.OnQueryCapability(PRINTER_TEST_IP, 0);
256 vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp");
257 std::vector<std::string> propertyKeys;
258 vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys);
259 }
260
261 HWTEST_F(VendorBsuniDriverTest, VendorBsuniDriverTest_0007, TestSize.Level2)
262 {
263 VendorBsuniDriver vendorDriver;
264 g_mockDriver = nullptr;
265 Print_VendorExtension vendorExtension = { .onCreate = OnCreateTest,
266 .onDestroy = OnDestroyTest,
267 .onStartDiscovery = OnStartDiscoveryTest,
268 .onStopDiscovery = OnStopDiscoveryTest,
269 .onConnectPrinter = OnConnectPrinterTest,
270 .onDisconnectPrinter = OnDisconnectPrinterTest,
271 .onQueryCapability = OnQueryCapabilityTest,
272 .onQueryCapabilityByIp = OnQueryCapabilityByIpTest,
273 .onQueryProperties = OnQueryPropertiesTest };
274 vendorDriver.vendorExtension = &vendorExtension;
275 vendorDriver.OnCreate();
276 vendorDriver.OnDestroy();
277 vendorDriver.OnStartDiscovery();
278 vendorDriver.OnQueryCapability(PRINTER_TEST_IP, 0);
279 vendorDriver.OnQueryCapabilityByIp(PRINTER_TEST_IP, "ipp");
280 std::vector<std::string> propertyKeys;
281 vendorDriver.OnQueryProperties(PRINTER_TEST_IP, propertyKeys);
282 }
283
BuildDefaultValue(Print_DefaultValue & defaultValue)284 static void BuildDefaultValue(Print_DefaultValue &defaultValue)
285 {
286 defaultValue.defaultColorMode = COLOR_MODE_MONOCHROME;
287 defaultValue.defaultDuplexMode = DUPLEX_MODE_ONE_SIDED;
288 defaultValue.defaultMediaType = "a";
289 defaultValue.defaultPageSizeId = "ISO_A4";
290 defaultValue.defaultMargin = {1, 1, 1, 1};
291 defaultValue.defaultPaperSource = "a";
292 defaultValue.defaultPrintQuality = Print_Quality::PRINT_QUALITY_HIGH;
293 defaultValue.defaultCopies = 1;
294 defaultValue.defaultResolution = {1, 1};
295 defaultValue.defaultOrientation = ORIENTATION_MODE_PORTRAIT;
296 defaultValue.otherDefaultValues = "default";
297 }
298
299 } // namespace Print
300 } // namespace OHOS