1 /*
2 * Copyright (c) 2023 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_service_mock_permission.h"
18 #include "mock_remote_object.h"
19 #define private public
20 #define protected public
21 #include "print_service_ability.h"
22 #include "print_bms_helper.h"
23 #undef protected
24 #undef private
25 #ifdef CUPS_ENABLE
26 #include "print_cups_client.h"
27 #endif // CUPS_ENABLE
28 #include "accesstoken_kit.h"
29 #include "array_wrapper.h"
30 #include "int_wrapper.h"
31 #include "ipc_skeleton.h"
32 #include "iservice_registry.h"
33 #include "print_constant.h"
34 #include "print_log.h"
35 #include "printer_info.h"
36 #include "print_utils.h"
37 #include "string_wrapper.h"
38 #include "system_ability_definition.h"
39 #include "want_params_wrapper.h"
40 #include "print_security_guard_manager.h"
41 #include "hisys_event_util.h"
42 #include <json/json.h>
43 #include "mock_print_callback_proxy.h"
44 #include "mock_print_extension_callback_proxy.h"
45
46
47 using namespace testing;
48 using namespace testing::ext;
49
50 namespace OHOS::Print {
51 using namespace std;
52 using namespace OHOS::HiviewDFX;
53 using namespace Security::AccessToken;
54 static constexpr const char *DEFAULT_EXTENSION_ID = "com.example.ext";
55 static constexpr const char *UNLOAD_EXTENSION_ID = "com.example.ext.unload";
56 static constexpr const char *NONEXIST_EXTENSION_ID = "com.example.ext.nonexist";
57 static constexpr const char *DEFAULT_EXT_PRINTER_ID = "https://10.10.10.10/FE8083DCD35F";
58 static constexpr const char *DEFAULT_EXT_PRINTER_ID2 = "https://10.10.10.10/0FDA6E208473";
59 static constexpr const char *DEFAULT_PRINT_FILE_A = "file://data/print/a.png";
60 static constexpr const char *DEFAULT_PRINT_FILE_B = "file://data/print/b.png";
61 static constexpr const char *DEFAULT_PRINT_FILE_C = "file://data/print/c.png";
62 static const std::string PRINT_GET_FILE_EVENT_TYPE = "getPrintFileCallback_adapter";
63 static const std::string PRINTER_EVENT_TYPE = "printerStateChange";
64 static const std::string PRINTJOB_EVENT_TYPE = "jobStateChange";
65 static const std::string EXTINFO_EVENT_TYPE = "extInfoChange";
66 static const std::string PRINT_ADAPTER_EVENT_TYPE = "printCallback_adapter";
67 static const std::string EVENT_CANCEL = "cancel";
68 const uint32_t MAX_JOBQUEUE_NUM = 512;
69
70 enum EXTENSION_ID_TYPE {
71 TYPE_DEFAULT,
72 TYPE_UNLOAD,
73 TYPE_NON_EXIST,
74 };
75 REGISTER_SYSTEM_ABILITY_BY_ID(PrintServiceAbility, PRINT_SERVICE_ID, true);
76 class PrintServiceAbilityTest : public testing::Test {
77 public:
78 static void SetUpTestCase(void);
79 static void TearDownTestCase(void);
80 void SetUp();
81 void TearDown();
82 std::string GetExtensionId(EXTENSION_ID_TYPE type);
83 std::string GetDefaultTaskId();
84 std::string GetDefaultJobId();
85 std::string GetDefaultPrinterId();
86 std::string GetInvalidPrinterId();
87 void InitExtPrinterList(std::vector<PrinterInfo> &printerList, size_t count);
88 void InitPrinterList(std::vector<PrinterInfo> &printerList, size_t count);
89 void InitExtPrinterIdList(std::vector<std::string> &idList, size_t count);
90 void InitFileList(std::vector<std::string> &fileList);
91 void InitExtensionList(std::vector<AppExecFwk::ExtensionAbilityInfo>& extList);
92 };
93
SetUpTestCase(void)94 void PrintServiceAbilityTest::SetUpTestCase(void) {}
95
TearDownTestCase(void)96 void PrintServiceAbilityTest::TearDownTestCase(void) {}
97
SetUp(void)98 void PrintServiceAbilityTest::SetUp(void)
99 {
100 static int32_t testNo = 0;
101 PRINT_HILOGE("PrintServiceAbilityTest_%{public}d", ++testNo);
102 }
103
TearDown(void)104 void PrintServiceAbilityTest::TearDown(void) {}
105
GetExtensionId(EXTENSION_ID_TYPE type)106 std::string PrintServiceAbilityTest::GetExtensionId(EXTENSION_ID_TYPE type)
107 {
108 switch (type) {
109 case TYPE_DEFAULT:
110 return DEFAULT_EXTENSION_ID;
111
112 case TYPE_UNLOAD:
113 return UNLOAD_EXTENSION_ID;
114
115 case TYPE_NON_EXIST:
116 return NONEXIST_EXTENSION_ID;
117
118 default:
119 break;
120 }
121 return DEFAULT_EXTENSION_ID;
122 }
123
GetDefaultTaskId()124 std::string PrintServiceAbilityTest::GetDefaultTaskId()
125 {
126 return std::to_string(0);
127 }
128
GetDefaultJobId()129 std::string PrintServiceAbilityTest::GetDefaultJobId()
130 {
131 return std::to_string(0);
132 }
133
GetDefaultPrinterId()134 std::string PrintServiceAbilityTest::GetDefaultPrinterId()
135 {
136 return PrintUtils::GetGlobalId(DEFAULT_EXTENSION_ID, DEFAULT_EXT_PRINTER_ID);
137 }
138
GetInvalidPrinterId()139 std::string PrintServiceAbilityTest::GetInvalidPrinterId()
140 {
141 return PrintUtils::GetGlobalId(DEFAULT_EXTENSION_ID, "wrong printer id");
142 }
143
InitExtPrinterList(std::vector<PrinterInfo> & printerList,size_t count)144 void PrintServiceAbilityTest::InitExtPrinterList(std::vector<PrinterInfo> &printerList, size_t count)
145 {
146 printerList.clear();
147 PrinterInfo info;
148 info.SetPrinterId(DEFAULT_EXT_PRINTER_ID);
149 printerList.emplace_back(info);
150 if (printerList.size() < count) {
151 info.SetPrinterId(DEFAULT_EXT_PRINTER_ID2);
152 printerList.emplace_back(info);
153 }
154 }
155
InitPrinterList(std::vector<PrinterInfo> & printerList,size_t count)156 void PrintServiceAbilityTest::InitPrinterList(std::vector<PrinterInfo> &printerList, size_t count)
157 {
158 printerList.clear();
159 PrinterInfo info;
160 info.SetPrinterId(PrintUtils::GetGlobalId(DEFAULT_EXTENSION_ID, DEFAULT_EXT_PRINTER_ID));
161 printerList.emplace_back(info);
162 if (printerList.size() < count) {
163 info.SetPrinterId(PrintUtils::GetGlobalId(DEFAULT_EXTENSION_ID, DEFAULT_EXT_PRINTER_ID2));
164 printerList.emplace_back(info);
165 }
166 }
167
InitExtPrinterIdList(std::vector<std::string> & idList,size_t count)168 void PrintServiceAbilityTest::InitExtPrinterIdList(std::vector<std::string> &idList, size_t count)
169 {
170 idList.clear();
171 idList.emplace_back(DEFAULT_EXT_PRINTER_ID);
172 if (idList.size() < count) {
173 idList.emplace_back(DEFAULT_EXT_PRINTER_ID2);
174 }
175 }
176
InitFileList(std::vector<std::string> & fileList)177 void PrintServiceAbilityTest::InitFileList(std::vector<std::string> &fileList)
178 {
179 fileList.emplace_back(DEFAULT_PRINT_FILE_A);
180 fileList.emplace_back(DEFAULT_PRINT_FILE_B);
181 fileList.emplace_back(DEFAULT_PRINT_FILE_C);
182 }
183
InitExtensionList(std::vector<AppExecFwk::ExtensionAbilityInfo> & extList)184 void PrintServiceAbilityTest::InitExtensionList(std::vector<AppExecFwk::ExtensionAbilityInfo>& extList)
185 {
186 std::vector<std::string> nameList = {DEFAULT_EXTENSION_ID, UNLOAD_EXTENSION_ID};
187 AppExecFwk::ExtensionAbilityInfo loadInfo;
188 for (size_t index = 0; index < nameList.size(); index++) {
189 loadInfo.bundleName = nameList[index];
190 loadInfo.moduleName = nameList[index];
191 loadInfo.name = nameList[index];
192 extList.emplace_back(loadInfo);
193 }
194 }
195 /**
196 * @tc.name: PrintServiceAbilityTest_0001
197 * @tc.desc: PrintServiceAbility ctor/dtor
198 * @tc.type: FUNC ~PrintServiceAbility()
199 * @tc.require:
200 */
201 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0001, TestSize.Level1)
202 {
203 PrintServiceAbility* print_service = new PrintServiceAbility(PRINT_SERVICE_ID, true);
204 if (print_service != nullptr) {
205 delete print_service;
206 print_service = nullptr;
207 }
208 PrintServiceAbility* new_print_service = new PrintServiceAbility(PRINT_SERVICE_ID, false);
209 if (new_print_service != nullptr) {
210 delete new_print_service;
211 new_print_service = nullptr;
212 }
213 EXPECT_EQ(PrintServiceAbility::GetInstance()->Init(), E_PRINT_SERVER_FAILURE);
214 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
215 std::shared_ptr<PrintServiceHelper> helper = nullptr;
216 service->SetHelper(helper);
217 EXPECT_EQ(PrintServiceAbility::GetInstance()->Init(), E_PRINT_SERVER_FAILURE);
218 }
219 /**
220 * @tc.name: PrintServiceAbilityTest_0002
221 * @tc.desc: PrintServiceAbility ctor/dtor
222 * @tc.type: FUNC ManualStart()
223 * @tc.require:
224 */
225 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0002, TestSize.Level1)
226 {
227 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
228 int state = static_cast<int>(service->state_);
229 service->ManualStart();
230 EXPECT_EQ(state, 0);
231 }
232 /**
233 * @tc.name: PrintServiceAbilityTest_0003
234 * @tc.desc: PrintServiceAbility ctor/dtor
235 * @tc.type: FUNC GetPrintJobOrderId()
236 * @tc.require:
237 */
238 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0003, TestSize.Level1)
239 {
240 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
241 service->GetPrintJobOrderId();
242 EXPECT_NE(service->currentJobOrderId_, 0);
243 }
244 /**
245 * @tc.name: PrintServiceAbilityTest_0004
246 * @tc.desc: PrintServiceAbility ctor/dtor
247 * @tc.type: FUNC
248 * @tc.require: return E_PRINT_NO_PERMISSION
249 */
250 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0004, TestSize.Level1)
251 {
252 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
253 EXPECT_EQ(service->StartService(), E_PRINT_NO_PERMISSION);
254 std::vector<std::string> fileList = {};
255 std::vector<uint32_t> fdList = {};
256 std::string taskId = "";
257 std::shared_ptr<AdapterParam> adapterParam = std::make_shared<AdapterParam>();
258 EXPECT_EQ(service->StartPrint(fileList, fdList, taskId), E_PRINT_NO_PERMISSION);
259 EXPECT_EQ(service->CallSpooler(fileList, fdList, taskId), E_PRINT_NO_PERMISSION);
260 EXPECT_EQ(service->StopPrint(taskId), E_PRINT_NO_PERMISSION);
261 std::string printerId = "1234";
262 EXPECT_EQ(service->ConnectPrinter(printerId), E_PRINT_NO_PERMISSION);
263 EXPECT_EQ(service->DisconnectPrinter(printerId), E_PRINT_NO_PERMISSION);
264 std::vector<std::string> extensionIds;
265 EXPECT_EQ(service->StartDiscoverPrinter(extensionIds), E_PRINT_NO_PERMISSION);
266 EXPECT_EQ(service->StopDiscoverPrinter(), E_PRINT_NO_PERMISSION);
267 std::vector<PrintExtensionInfo> extensionInfos;
268 EXPECT_EQ(service->QueryAllExtension(extensionInfos), E_PRINT_NO_PERMISSION);
269 std::vector<PrintJob> printJobs;
270 EXPECT_EQ(service->QueryAllPrintJob(printJobs), E_PRINT_NO_PERMISSION);
271 std::vector<std::string> printerList;
272 EXPECT_EQ(service->QueryAddedPrinter(printerList), E_PRINT_NO_PERMISSION);
273 PrinterInfo info;
274 EXPECT_EQ(service->QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_NO_PERMISSION);
275 std::vector<std::string> keyList;
276 std::vector<std::string> valueList;
277 EXPECT_EQ(service->QueryPrinterProperties(printerId, keyList, valueList), E_PRINT_NO_PERMISSION);
278 std::string printJobId = "1";
279 PrintJob printJob;
280 EXPECT_EQ(service->QueryPrintJobById(printJobId, printJob), E_PRINT_NO_PERMISSION);
281 std::string printerUri = "111.222.333";
282 std::string printerName = "pixlab_0759";
283 std::string printerMake = "pixlab b5";
284 EXPECT_EQ(service->AddPrinterToCups(printerUri, printerName, printerMake), E_PRINT_NO_PERMISSION);
285 PrinterCapability printerCaps;
286 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NO_PERMISSION);
287 EXPECT_EQ(service->StartNativePrintJob(printJob), E_PRINT_NO_PERMISSION);
288 EXPECT_EQ(service->StartPrintJob(printJob), E_PRINT_NO_PERMISSION);
289 EXPECT_EQ(service->CancelPrintJob(printJobId), E_PRINT_NO_PERMISSION);
290 std::vector<PrinterInfo> printerInfos;
291 EXPECT_EQ(service->AddPrinters(printerInfos), E_PRINT_NO_PERMISSION);
292 std::vector<std::string> printerIds;
293 EXPECT_EQ(service->RemovePrinters(printerIds), E_PRINT_NO_PERMISSION);
294 EXPECT_EQ(service->UpdatePrinters(printerInfos), E_PRINT_NO_PERMISSION);
295 uint32_t state = PrinterState::PRINTER_ADDED;
296 EXPECT_EQ(service->UpdatePrinterState(printerId, state), E_PRINT_NO_PERMISSION);
297 uint32_t subState = 0;
298 EXPECT_EQ(service->UpdatePrintJobStateOnlyForSystemApp(printJobId, state, subState), E_PRINT_NO_PERMISSION);
299 EXPECT_EQ(service->AdapterGetFileCallBack(printJobId, state, subState), E_PRINT_NONE);
300 state = PrintJobState::PRINT_JOB_CREATE_FILE_COMPLETED;
301 EXPECT_EQ(service->AdapterGetFileCallBack(printJobId, state, subState), E_PRINT_NO_PERMISSION);
302 std::string extInfo = "";
303 EXPECT_EQ(service->UpdateExtensionInfo(extInfo), E_PRINT_NO_PERMISSION);
304 std::string previewResult = "";
305 EXPECT_EQ(service->RequestPreview(printJob, previewResult), E_PRINT_NO_PERMISSION);
306 EXPECT_EQ(service->QueryPrinterCapability(printerId), E_PRINT_NO_PERMISSION);
307 std::string type = "";
308 sptr<IPrintCallback> listener = nullptr;
309 EXPECT_EQ(service->RegisterPrinterCallback(type, listener), E_PRINT_NO_PERMISSION);
310 EXPECT_EQ(service->UnregisterPrinterCallback(type), E_PRINT_NO_PERMISSION);
311 std::string extensionCID = "";
312 sptr<IPrintExtensionCallback> listenerCB = nullptr;
313 EXPECT_EQ(service->RegisterExtCallback(extensionCID, listenerCB), E_PRINT_NO_PERMISSION);
314 EXPECT_EQ(service->UnregisterAllExtCallback(extensionCID), E_PRINT_NO_PERMISSION);
315 EXPECT_EQ(service->LoadExtSuccess(extensionCID), E_PRINT_NO_PERMISSION);
316 std::string jobName = "a.jpeg";
317 PrintAttributes printAttributes;
318 EXPECT_EQ(service->PrintByAdapter(jobName, printAttributes, taskId), E_PRINT_NO_PERMISSION);
319 uint32_t fd = 46;
320 EXPECT_EQ(service->StartGetPrintFile(printJobId, printAttributes, fd), E_PRINT_NO_PERMISSION);
321 EXPECT_EQ(service->NotifyPrintService(printJobId, type), E_PRINT_NO_PERMISSION);
322 EXPECT_EQ(service->CallStatusBar(), E_PRINT_NO_PERMISSION);
323 uint32_t event = 0;
324 std::string jobId = GetDefaultJobId();
325 EXPECT_EQ(service->NotifyPrintServiceEvent(jobId, event), E_PRINT_NO_PERMISSION);
326 EXPECT_EQ(service->DestroyExtension(), E_PRINT_NO_PERMISSION);
327 PrinterPreferences printerPreference;
328 EXPECT_EQ(service->SetPrinterPreference(printerId, printerPreference), E_PRINT_NO_PERMISSION);
329 EXPECT_EQ(service->On(taskId, type, listener), E_PRINT_NO_PERMISSION);
330 EXPECT_EQ(service->Off(taskId, type), E_PRINT_NO_PERMISSION);
331 EXPECT_EQ(service->SetDefaultPrinter(printerId, 0), E_PRINT_NO_PERMISSION);
332 EXPECT_EQ(service->DeletePrinterFromCups(printerName), E_PRINT_NO_PERMISSION);
333 std::vector<PrinterInfo> printers;
334 EXPECT_EQ(service->DiscoverUsbPrinters(printers), E_PRINT_NO_PERMISSION);
335 EXPECT_EQ(service->UpdatePrinterInSystem(info), E_PRINT_NO_PERMISSION);
336
337 PrintServiceMockPermission::MockPermission();
338 EXPECT_EQ(service->StartService(), E_PRINT_NONE);
339 EXPECT_EQ(service->StartPrint(fileList, fdList, taskId), E_PRINT_INVALID_PARAMETER);
340
341 EXPECT_EQ(service->StopDiscoverPrinter(), E_PRINT_NONE);
342 EXPECT_EQ(service->StopPrint(taskId), E_PRINT_NONE);
343 }
344 /**
345 * @tc.name: PrintServiceAbilityTest_0006
346 * @tc.desc: PrintServiceAbility ctor/dtor
347 * @tc.type: FUNC CallSpooler
348 * @tc.require: has token
349 */
350 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0006, TestSize.Level1)
351 {
352 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
353 std::vector<std::string> fileList = {};
354 std::vector<uint32_t> fdList = {};
355 std::string jobId = "";
356 EXPECT_EQ(service->CallSpooler(fileList, fdList, jobId), E_PRINT_INVALID_PARAMETER);
357 jobId = "jobId";
358 EXPECT_EQ(service->CallSpooler(fileList, fdList, jobId), E_PRINT_NONE);
359 }
360
361 /**
362 * @tc.name: PrintServiceAbilityTest_0007
363 * @tc.desc: PrintServiceAbility ctor/dtor
364 * @tc.type: FUNC SetHelper
365 * @tc.require: has token
366 */
367 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0007, TestSize.Level1)
368 {
369 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
370 std::shared_ptr<PrintServiceHelper> helper = nullptr;
371 service->SetHelper(helper);
372 EXPECT_EQ(service->helper_, helper);
373 helper = make_shared<PrintServiceHelper>();
374 if (helper != nullptr) {
375 service->SetHelper(helper);
376 EXPECT_EQ(service->helper_, helper);
377 }
378 }
379
380 /**
381 * @tc.name: PrintServiceAbilityTest_0008
382 * @tc.desc: PrintServiceAbility ctor/dtor
383 * @tc.type: FUNC ConnectPrinter
384 * @tc.require:
385 */
386 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0008, TestSize.Level1)
387 {
388 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
389 std::string printerId ="1234";
390 EXPECT_EQ(service->ConnectPrinter(printerId), E_PRINT_INVALID_PRINTER);
391 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
392 service->printSystemData_.discoveredPrinterInfoList_[printerId] = info;
393 service->ConnectPrinter(printerId);
394 std::string extensionId = PrintUtils::GetExtensionId(printerId);
395 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_CONNECT_PRINTER);
396 sptr<IPrintExtensionCallback> extCb = nullptr;
397 service->extCallbackMap_[cid] = extCb;
398 EXPECT_EQ(service->ConnectPrinter(printerId), E_PRINT_NONE);
399 extCb = new MockPrintExtensionCallbackProxy();
400 EXPECT_NE(extCb, nullptr);
401 if (extCb != nullptr) {
402 service->extCallbackMap_.clear();
403 service->extCallbackMap_[cid] = extCb;
404 }
405 service->ConnectPrinter(printerId);
406 }
407
408 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0009, TestSize.Level1)
409 {
410 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
411 std::string printerId ="1234";
412 EXPECT_EQ(service->DisconnectPrinter(printerId), E_PRINT_INVALID_PRINTER);
413 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
414 service->printSystemData_.discoveredPrinterInfoList_[printerId] = info;
415 service->DisconnectPrinter(printerId);
416 std::string extensionId = PrintUtils::GetExtensionId(printerId);
417 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_CONNECT_PRINTER);
418 sptr<IPrintExtensionCallback> extCb = nullptr;
419 service->extCallbackMap_[cid] = extCb;
420 EXPECT_EQ(service->DisconnectPrinter(printerId), E_PRINT_SERVER_FAILURE);
421 extCb = new MockPrintExtensionCallbackProxy();
422 EXPECT_NE(extCb, nullptr);
423 if (extCb != nullptr) {
424 service->extCallbackMap_.clear();
425 service->extCallbackMap_[cid] = extCb;
426 }
427 service->DisconnectPrinter(printerId);
428 }
429
430 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0010, TestSize.Level1)
431 {
432 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
433 std::vector<std::string> extensionIds;
434 EXPECT_EQ(service->StartDiscoverPrinter(extensionIds), E_PRINT_INVALID_EXTENSION);
435 }
436
437 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0011, TestSize.Level1)
438 {
439 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
440 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
441 service->helper_ = helper;
442 std::string extensionCid = "";
443 sptr<IPrintExtensionCallback> listener = nullptr;
444 EXPECT_EQ(service->RegisterExtCallback(extensionCid, listener), E_PRINT_INVALID_PARAMETER);
445 extensionCid = PrintUtils::EncodeExtensionCid(GetExtensionId(TYPE_NON_EXIST), PRINT_EXTCB_START_DISCOVERY);
446 EXPECT_EQ(service->RegisterExtCallback(extensionCid, listener), E_PRINT_INVALID_EXTENSION);
447 extensionCid = PrintUtils::EncodeExtensionCid(GetExtensionId(TYPE_UNLOAD), PRINT_EXTCB_START_DISCOVERY);
448 EXPECT_EQ(service->RegisterExtCallback(extensionCid, listener), E_PRINT_INVALID_EXTENSION);
449 extensionCid = PrintUtils::EncodeExtensionCid(GetExtensionId(TYPE_DEFAULT), PRINT_EXTCB_MAX);
450 EXPECT_EQ(service->RegisterExtCallback(extensionCid, listener), E_PRINT_INVALID_EXTENSION);
451 EXPECT_EQ(service->LoadExtSuccess(GetExtensionId(TYPE_NON_EXIST)), E_PRINT_INVALID_EXTENSION);
452 EXPECT_EQ(service->LoadExtSuccess(GetExtensionId(TYPE_UNLOAD)), E_PRINT_INVALID_EXTENSION);
453 }
454
455 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0012, TestSize.Level1)
456 {
457 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
458 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
459 service->helper_ = helper;
460 std::string taskId = "";
461 std::string type = "";
462 sptr<IPrintCallback> listener = nullptr;
463 EXPECT_EQ(service->On(taskId, type, listener), E_PRINT_INVALID_PARAMETER);
464 EXPECT_EQ(service->Off(taskId, type), E_PRINT_INVALID_PARAMETER);
465 taskId = "1";
466 type = "printCallback_adapter";
467 service->On(taskId, type, listener);
468 EXPECT_EQ(service->On(taskId, type, listener), E_PRINT_INVALID_PARAMETER);
469 EXPECT_EQ(service->Off(taskId, type), E_PRINT_NONE);
470 listener = new MockPrintCallbackProxy();
471 if (listener != nullptr) {
472 service->On(taskId, type, listener);
473 }
474 service->registeredListeners_[type] = listener;
475 taskId = "";
476 EXPECT_EQ(service->On(taskId, type, listener), E_PRINT_NONE);
477 EXPECT_EQ(service->Off(taskId, type), E_PRINT_NONE);
478 }
479
480 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0013, TestSize.Level1)
481 {
482 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
483 std::vector<PrinterInfo> printerInfos;
484 InitExtPrinterList(printerInfos, 1);
485 EXPECT_EQ(service->AddPrinters(printerInfos), E_PRINT_NONE);
486 EXPECT_EQ(service->UpdatePrinters(printerInfos), E_PRINT_NONE);
487 std::vector<std::string> printerIds;
488 InitExtPrinterIdList(printerIds, 1);
489 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
490 info->SetPrinterId(printerIds[0]);
491 std::string printerId = printerIds[0];
492 service->printSystemData_.discoveredPrinterInfoList_[printerId] = info;
493 EXPECT_EQ(service->RemovePrinters(printerIds), E_PRINT_NONE);
494 std::vector<std::string> newPrinterIds;
495 newPrinterIds.push_back("1234");
496 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
497 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
498 service->RemovePrinters(newPrinterIds);
499 printerInfos.clear();
500 EXPECT_EQ(service->UpdatePrinters(printerInfos), E_PRINT_NONE);
501 }
502
503 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0014, TestSize.Level1)
504 {
505 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
506 PrintJob testJob;
507 std::string jobId = GetDefaultJobId();
508 testJob.SetJobId(jobId);
509 testJob.SetPrinterId(GetDefaultPrinterId());
510 EXPECT_EQ(service->StartPrintJob(testJob), E_PRINT_INVALID_PRINTJOB);
511 std::shared_ptr<PrintJob> job = std::make_shared<PrintJob>();
512 service->printJobList_["1"] = job;
513 EXPECT_EQ(service->StartPrintJob(testJob), E_PRINT_INVALID_PRINTJOB);
514 service->printJobList_["0"] = job;
515 std::string extensionId = PrintUtils::GetExtensionId(GetDefaultPrinterId());
516 sptr<IPrintExtensionCallback> listener = nullptr;
517 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_START_PRINT);
518 service->extCallbackMap_[cid] = listener;
519 service->StartPrintJob(testJob);
520 }
521
522 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0018, TestSize.Level1)
523 {
524 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
525 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
526 service->helper_ = helper;
527 std::string jobId ="1";
528 uint32_t state = PRINT_JOB_PREPARED;
529 uint32_t subState = PRINT_JOB_COMPLETED_SUCCESS;
530 service->notifyAdapterJobChanged(jobId, state, subState);
531 auto attrIt = service->printAttributesList_.find(jobId);
532 EXPECT_EQ(attrIt, service->printAttributesList_.end());
533 PrintAttributes attr;
534 service->printAttributesList_[jobId] = attr;
535 service->notifyAdapterJobChanged(jobId, state, subState);
536 attrIt = service->printAttributesList_.find(jobId);
537 EXPECT_NE(attrIt, service->printAttributesList_.end());
538 sptr<IPrintCallback> listener = nullptr;
539 service->adapterListenersByJobId_[jobId] = listener;
540 service->notifyAdapterJobChanged(jobId, state, subState);
541 attrIt = service->printAttributesList_.find(jobId);
542 EXPECT_NE(attrIt, service->printAttributesList_.end());
543 }
544
545 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0019, TestSize.Level1)
546 {
547 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
548 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
549 service->helper_ = helper;
550 std::string jobId ="1";
551 uint32_t state = PRINT_JOB_PREPARED;
552 uint32_t subState = PRINT_JOB_COMPLETED_SUCCESS;
553
554 state = PRINT_JOB_BLOCKED;
555 service->notifyAdapterJobChanged(jobId, state, subState);
556 auto attrIt = service->printAttributesList_.find(jobId);
557 EXPECT_EQ(attrIt, service->printAttributesList_.end());
558 }
559
560 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0020, TestSize.Level1)
561 {
562 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
563 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
564 service->helper_ = helper;
565 std::string jobName = "a.jpeg";
566 PrintAttributes printAttributes;
567 std::string taskId ="1";
568 EXPECT_EQ(service->PrintByAdapter(jobName, printAttributes, taskId), E_PRINT_NONE);
569 }
570
571 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0021, TestSize.Level1)
572 {
573 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
574 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
575 service->helper_ = helper;
576 std::string jobId = "1";
577 PrintAttributes printAttributes;
578 uint32_t fd = 46;
579 sptr<IPrintCallback> listener = nullptr;
580 service->adapterListenersByJobId_[jobId] = listener;
581 EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE);
582 service->adapterListenersByJobId_.clear();
583
584 EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE);
585 service->printAttributesList_[jobId] = printAttributes;
586 EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE);
587 std::string newJobId = "2";
588 EXPECT_EQ(service->StartGetPrintFile(newJobId, printAttributes, fd), E_PRINT_NONE);
589 }
590
591 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0022, TestSize.Level1)
592 {
593 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
594 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
595 service->helper_ = helper;
596 std::string printerId = "1234";
597 std::shared_ptr<PrinterInfo> info =std::make_shared<PrinterInfo>();
598 service->printSystemData_.discoveredPrinterInfoList_[printerId] = info;
599 EXPECT_EQ(service->printSystemData_.QueryDiscoveredPrinterInfoById(printerId), info);
600 }
601
602 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0024, TestSize.Level1)
603 {
604 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
605 PrintJob jobInfo;
606 service->CheckJobQueueBlocked(jobInfo);
607 EXPECT_EQ(service->isJobQueueBlocked_, false);
608 }
609
610 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0025, TestSize.Level1)
611 {
612 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
613 std::string extensionId = "com.ohos.spooler:0";
614 EXPECT_EQ(service->DelayStartDiscovery(extensionId), false);
615 service->extensionStateList_[extensionId] = PRINT_EXTENSION_UNLOAD;
616 EXPECT_EQ(service->DelayStartDiscovery(extensionId), false);
617 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_STOP_DISCOVERY);
618 sptr<IPrintExtensionCallback> listener = nullptr;
619 service->extCallbackMap_[cid] = listener;
620 EXPECT_EQ(service->DelayStartDiscovery(extensionId), false);
621 cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_START_DISCOVERY);
622 service->extCallbackMap_.clear();
623 service->extCallbackMap_[cid] = listener;
624 EXPECT_EQ(service->DelayStartDiscovery(extensionId), false);
625 }
626
627 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0026, TestSize.Level1)
628 {
629 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
630 service->state_ = ServiceRunningState::STATE_NOT_START;
631 service->OnStop();
632 EXPECT_EQ(service->state_, ServiceRunningState::STATE_NOT_START);
633 service->state_ = ServiceRunningState::STATE_RUNNING;
634 service->OnStop();
635 EXPECT_EQ(service->state_, ServiceRunningState::STATE_NOT_START);
636 }
637
638 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0028, TestSize.Level1)
639 {
640 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
641 std::string extensionId = "com.ohos.spooler:0";
642 service->extensionStateList_[extensionId] = PRINT_EXTENSION_UNLOAD;
643 EXPECT_EQ(service->StopDiscoverPrinter(), E_PRINT_NONE);
644 service->extensionStateList_[extensionId] = PRINT_EXTENSION_LOADED;
645 EXPECT_EQ(service->StopDiscoverPrinter(), E_PRINT_NONE);
646 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_STOP_DISCOVERY);
647 sptr<IPrintExtensionCallback> listener = nullptr;
648 service->extCallbackMap_[cid] = listener;
649 EXPECT_EQ(service->StopDiscoverPrinter(), E_PRINT_NONE);
650 }
651
652 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0029, TestSize.Level1)
653 {
654 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
655 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
656 service->helper_ = helper;
657 std::vector<PrintJob> printJobs;
658 int32_t userId = 100;
659 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
660 service->printUserMap_.insert(std::make_pair(userId, userData));
661 EXPECT_EQ(service->QueryAllPrintJob(printJobs), E_PRINT_NONE);
662 }
663
664 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0030, TestSize.Level1)
665 {
666 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
667 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
668 service->helper_ = helper;
669 std::vector<PrintJob> printJobs;
670 service->QueryAllPrintJob(printJobs);
671 EXPECT_EQ(service->QueryAllPrintJob(printJobs), E_PRINT_NONE);
672 int32_t userId = 100;
673 service->printUserMap_.insert(std::make_pair(userId, nullptr));
674 service->QueryAllPrintJob(printJobs);
675 EXPECT_EQ(service->QueryAllPrintJob(printJobs), E_PRINT_NONE);
676 }
677
678 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0031, TestSize.Level1)
679 {
680 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
681 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
682 service->helper_ = helper;
683 std::vector<std::string> printerList;
684 EXPECT_EQ(service->QueryAddedPrinter(printerList), E_PRINT_NONE);
685 }
686
687 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0032, TestSize.Level1)
688 {
689 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
690 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
691 service->helper_ = helper;
692 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
693 std::vector<std::string> keyList;
694 std::vector<std::string> valueList;
695 EXPECT_EQ(service->QueryPrinterProperties(printerId, keyList, valueList), E_PRINT_INVALID_PRINTER);
696 auto printerInfo = std::make_shared<PrinterInfo>();
697 service->printSystemData_.addedPrinterInfoList_[printerId] = printerInfo;
698 keyList.push_back("printerId");
699 keyList.push_back("printerName");
700 EXPECT_EQ(service->QueryPrinterProperties(printerId, keyList, valueList), E_PRINT_INVALID_PRINTER);
701 }
702
703 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0033, TestSize.Level1)
704 {
705 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
706 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
707 service->helper_ = helper;
708 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
709 PrinterInfo info;
710 EXPECT_EQ(service->QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_INVALID_PRINTER);
711 auto printerInfo = std::make_shared<CupsPrinterInfo>();
712 printerInfo->name = "testName";
713 printerInfo->uri = "testUri";
714 printerInfo->maker = "testMaker";
715 printerInfo->alias = "testAlias";
716 service->printSystemData_.addedPrinterMap_.Insert(printerId, printerInfo);
717 EXPECT_EQ(service->QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_NONE);
718 }
719
720 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0034, TestSize.Level1)
721 {
722 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
723 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
724 service->helper_ = helper;
725 std::string printJobId = "1";
726 PrintJob printJob;
727 EXPECT_EQ(service->QueryPrintJobById(printJobId, printJob), E_PRINT_INVALID_PRINTJOB);
728 int32_t userId = 100;
729 service->printUserMap_.insert(std::make_pair(userId, nullptr));
730 EXPECT_EQ(service->QueryPrintJobById(printJobId, printJob), E_PRINT_INVALID_PRINTJOB);
731 }
732
733 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0035, TestSize.Level1)
734 {
735 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
736 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
737 service->helper_ = helper;
738 std::string printerUri = "testPrinterUri";
739 std::string printerName = "testPrinterName";
740 std::string printerMake = "testPrinterMake";
741 EXPECT_EQ(service->AddPrinterToCups(printerUri, printerName, printerMake), E_PRINT_SERVER_FAILURE);
742 }
743
744 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0036, TestSize.Level1)
745 {
746 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
747 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
748 service->helper_ = helper;
749 PrintJob printJob1;
750 printJob1.SetOption("test");
751 EXPECT_EQ(service->UpdatePrintJobOptionByPrinterId(printJob1), false);
752
753 PrintJob printJob2;
754 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
755 printJob2.SetPrinterId(printerId);
756 auto printerInfo = std::make_shared<CupsPrinterInfo>();
757 printerInfo->name = "testName";
758 printerInfo->uri = "testUri";
759 printerInfo->maker = "testMaker";
760 service->printSystemData_.addedPrinterMap_.Insert(printerId, printerInfo);
761 EXPECT_EQ(service->UpdatePrintJobOptionByPrinterId(printJob2), false);
762
763 Json::Value infoJson;
764 infoJson["printerName"] = "testPrinterName";
765 printJob2.SetOption(PrintJsonUtil::WriteString(infoJson));
766 EXPECT_EQ(service->UpdatePrintJobOptionByPrinterId(printJob2), true);
767 service->StartNativePrintJob(printJob2);
768 std::string extensionId = PrintUtils::GetExtensionId(printerId);
769 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_START_PRINT);
770 sptr<IPrintExtensionCallback> extCb = nullptr;
771 service->extCallbackMap_[cid] = extCb;
772 service->StartNativePrintJob(printJob2);
773 }
774
775 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0037, TestSize.Level1)
776 {
777 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
778 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
779 service->helper_ = helper;
780 PrintJob printJob;
781 std::string jobId = GetDefaultJobId();
782 auto nativePrintJob = std::make_shared<PrintJob>();
783 nativePrintJob->UpdateParams(printJob);
784 nativePrintJob->Dump();
785 service->AddToPrintJobList(jobId, nativePrintJob);
786 EXPECT_NE(service->AddNativePrintJob(jobId, printJob), nativePrintJob);
787 }
788
789 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0038, TestSize.Level1)
790 {
791 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
792 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
793 service->helper_ = helper;
794 PrintJob printJob;
795 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
796 printJob.SetPrinterId(printerId);
797 auto printerInfo = std::make_shared<CupsPrinterInfo>();
798 printerInfo->name = "testName";
799 printerInfo->uri = "testUri";
800 printerInfo->maker = "testMaker";
801 service->printSystemData_.addedPrinterMap_.Insert(printerId, printerInfo);
802 printJob.SetOption("test");
803 EXPECT_EQ(service->StartNativePrintJob(printJob), E_PRINT_INVALID_PRINTER);
804 }
805
806 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0040, TestSize.Level1)
807 {
808 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
809 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
810 service->helper_ = helper;
811 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
812 std::string jobId = GetDefaultJobId();
813 service->UpdateQueuedJobList(jobId, printJob);
814 service->UpdateQueuedJobList("0", printJob);
815 for (int i = 0; i < MAX_JOBQUEUE_NUM + 1; i++) {
816 service->queuedJobList_.insert(std::make_pair(std::to_string(i), printJob));
817 }
818 service->UpdateQueuedJobList("515", printJob);
819 int32_t userId = 100;
820 service->printUserMap_.insert(std::make_pair(userId, nullptr));
821 service->UpdateQueuedJobList("515", printJob);
822 service->printAppCount_ = 3;
823 std::string type ="0";
824 EXPECT_EQ(service->NotifyPrintService(jobId, type), E_PRINT_NONE);
825 type = "spooler_closed_for_started";
826 EXPECT_EQ(service->NotifyPrintService(jobId, type), E_PRINT_NONE);
827 type = "spooler_closed_for_cancelled";
828 EXPECT_EQ(service->NotifyPrintService(jobId, type), E_PRINT_NONE);
829 type = "";
830 EXPECT_EQ(service->NotifyPrintService(jobId, type), E_PRINT_INVALID_PARAMETER);
831 }
832
833 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0041, TestSize.Level1)
834 {
835 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
836 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
837 service->helper_ = helper;
838 std::string jobId = GetDefaultJobId();
839 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_INVALID_USERID);
840 int32_t userId = 100;
841 service->printUserMap_.insert(std::make_pair(userId, nullptr));
842 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_INVALID_USERID);
843 }
844
845 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0042, TestSize.Level1)
846 {
847 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
848 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
849 service->helper_ = helper;
850 PrintJob printJob;
851 printJob.SetJobId(GetDefaultJobId());
852 service->SetPrintJobCanceled(printJob);
853 auto testPrintJob = std::make_shared<PrintJob>(printJob);
854 std::string testJobId = testPrintJob->GetJobId();
855 auto attrIt = service->printAttributesList_.find(testJobId);
856 EXPECT_EQ(attrIt, service->printAttributesList_.end());
857 std::string jobId = "123";
858 printJob.SetJobId(jobId);
859 int32_t userId = 100;
860 service->userJobMap_[jobId] = userId;
861 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
862 service->printUserMap_[userId] = userData;
863 service->SetPrintJobCanceled(printJob);
864 testPrintJob = std::make_shared<PrintJob>(printJob);
865 testJobId = testPrintJob->GetJobId();
866 attrIt = service->printAttributesList_.find(testJobId);
867 EXPECT_EQ(attrIt, service->printAttributesList_.end());
868 }
869
870 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0043, TestSize.Level1)
871 {
872 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
873 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
874 service->helper_ = helper;
875 int32_t userId = 100;
876 service->CancelUserPrintJobs(userId);
877 auto userIt = service->printUserMap_.find(userId);
878 EXPECT_EQ(userIt, service->printUserMap_.end());
879
880 service->printUserMap_.insert(std::make_pair(userId, nullptr));
881 service->CancelUserPrintJobs(userId);
882 userIt = service->printUserMap_.find(userId);
883 EXPECT_NE(userIt, service->printUserMap_.end());
884
885 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
886 service->printUserMap_[userId] = userData;
887 service->CancelUserPrintJobs(userId);
888 userIt = service->printUserMap_.find(userId);
889 EXPECT_EQ(userIt, service->printUserMap_.end());
890 }
891
892 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0044, TestSize.Level1)
893 {
894 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
895 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
896 service->helper_ = helper;
897 PrintJob printJob;
898 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
899 EXPECT_EQ(service->SendQueuePrintJob(printerId), false);
900
901 std::string jobId = GetDefaultJobId();
902 service->printerJobMap_[printerId].insert(std::make_pair(jobId, true));
903 EXPECT_EQ(service->SendQueuePrintJob(printerId), false);
904 int32_t userId = 100;
905 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
906 service->printUserMap_[userId] = userData;
907 EXPECT_EQ(service->SendQueuePrintJob(printerId), false);
908 }
909
910 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0045, TestSize.Level1)
911 {
912 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
913 std::string printerUri = "111.222.333";
914 std::string printerId = "pixlab_0759";
915 PrinterCapability printerCaps;
916 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NONE);
917 }
918
919 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0046, TestSize.Level1)
920 {
921 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
922 std::string jobId = "1";
923 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
924 service->StartPrintJobCB(jobId, printJob);
925 EXPECT_EQ(printJob->jobState_, PRINT_JOB_QUEUED);
926 }
927
928 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0048, TestSize.Level1)
929 {
930 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
931 uint32_t state = PRINTER_UNKNOWN + 1;
932 std::string printerId ="1234";
933 EXPECT_EQ(service->UpdatePrinterState(printerId, state), E_PRINT_INVALID_PARAMETER);
934 state = PrinterState::PRINTER_ADDED;
935 EXPECT_EQ(service->UpdatePrinterState(printerId, state), E_PRINT_INVALID_PRINTER);
936 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
937 service->printSystemData_.discoveredPrinterInfoList_[printerId] = info;
938 EXPECT_EQ(service->UpdatePrinterState(printerId, state), E_PRINT_INVALID_PRINTER);
939 }
940
941 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0049, TestSize.Level1)
942 {
943 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
944 std::string jobId = "1";
945 uint32_t state = PRINTER_UNKNOWN + 1;
946 uint32_t subState = 0;
947 EXPECT_EQ(service->checkJobState(state, subState), true);
948 service->UpdatePrintJobState(jobId, state, subState);
949 state = PRINT_JOB_BLOCKED;
950 subState = PRINT_JOB_BLOCKED_OFFLINE - 1;
951 EXPECT_EQ(service->checkJobState(state, subState), false);
952 EXPECT_EQ(service->UpdatePrintJobState(jobId, state, subState), E_PRINT_INVALID_PARAMETER);
953 subState = PRINT_JOB_BLOCKED_UNKNOWN + 1;
954 EXPECT_EQ(service->checkJobState(state, subState), true);
955 EXPECT_EQ(service->UpdatePrintJobState(jobId, state, subState), E_PRINT_INVALID_USERID);
956 state = PRINT_JOB_COMPLETED;
957 subState = PRINT_JOB_COMPLETED_FILE_CORRUPT + 1;
958 EXPECT_EQ(service->checkJobState(state, subState), false);
959 EXPECT_EQ(service->UpdatePrintJobState(jobId, state, subState), E_PRINT_INVALID_PARAMETER);
960 }
961
962 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0050, TestSize.Level1)
963 {
964 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
965 std::string jobId = "1";
966 uint32_t state = PRINT_JOB_CREATE_FILE_COMPLETED;
967 uint32_t subState = 0;
968 EXPECT_EQ(service->UpdatePrintJobState(jobId, state, subState), false);
969 }
970
971 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0051, TestSize.Level1)
972 {
973 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
974 std::string jobId = "1";
975 uint32_t state = PRINTER_UNKNOWN;
976 uint32_t subState = 0;
977 EXPECT_EQ(service->AdapterGetFileCallBack(jobId, state, subState), E_PRINT_NONE);
978 std::string type = PRINT_GET_FILE_EVENT_TYPE;
979 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
980 service->registeredListeners_[type] = listener;
981 service->AdapterGetFileCallBack(jobId, state, subState);
982 subState = PRINT_JOB_CREATE_FILE_COMPLETED_SUCCESS;
983 service->AdapterGetFileCallBack(jobId, state, subState);
984 subState = PRINT_JOB_CREATE_FILE_COMPLETED_FAILED;
985 service->AdapterGetFileCallBack(jobId, state, subState);
986 }
987
988 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0052, TestSize.Level1)
989 {
990 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
991 std::string jobId = "1";
992 int32_t userId = 100;
993 service->userJobMap_[jobId] = userId;
994 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
995 service->printUserMap_[userId] = userData;
996 uint32_t state = PRINTER_UNKNOWN;
997 uint32_t subState = 0;
998 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_INVALID_PRINTJOB);
999 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1000 userData->printJobList_[jobId] = printJob;
1001 state = PRINT_JOB_BLOCKED;
1002 std::string printerId = "1234";
1003 printJob->SetPrinterId(printerId);
1004 service->printerJobMap_[printerId].insert(std::make_pair(jobId, true));
1005 auto printerInfo = std::make_shared<PrinterInfo>();
1006 service->printSystemData_.addedPrinterInfoList_[printerId] = printerInfo;
1007 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printerInfo;
1008 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_NONE);
1009 userData->queuedJobList_[jobId] = printJob;
1010 state = PRINT_JOB_COMPLETED;
1011 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_NONE);
1012 }
1013
1014 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0053, TestSize.Level1)
1015 {
1016 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1017 std::string jobId = "1";
1018 EXPECT_EQ(service->IsQueuedJobListEmpty(jobId), false);
1019 int32_t userId = 100;
1020 service->userJobMap_[jobId] = userId;
1021 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1022 auto printJob = std::make_shared<PrintJob>();
1023 userData->queuedJobList_[jobId] = printJob;
1024 service->printUserMap_[userId] = userData;
1025 EXPECT_EQ(service->IsQueuedJobListEmpty(jobId), false);
1026 userData->queuedJobList_.clear();
1027 service->currentUserId_ = 0;
1028 EXPECT_EQ(service->IsQueuedJobListEmpty(jobId), false);
1029 service->currentUserId_ = 100;
1030 EXPECT_EQ(service->IsQueuedJobListEmpty(jobId), true);
1031 }
1032
1033 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0055, TestSize.Level1)
1034 {
1035 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1036 uint32_t event = -1;
1037 std::string jobId = GetDefaultJobId();
1038 EXPECT_EQ(service->NotifyPrintServiceEvent(jobId, event), E_PRINT_INVALID_PARAMETER);
1039 event = 3;
1040 EXPECT_EQ(service->NotifyPrintServiceEvent(jobId, event), E_PRINT_INVALID_PARAMETER);
1041 event = 0;
1042 service->NotifyPrintServiceEvent(jobId, event);
1043 auto printJob = std::make_shared<PrintJob>();
1044 service->printJobList_[jobId] = printJob;
1045 service->NotifyPrintServiceEvent(jobId, event);
1046 event = 1;
1047 service->NotifyPrintServiceEvent(jobId, event);
1048 event = 2;
1049 service->NotifyPrintServiceEvent(jobId, event);
1050 event = 3;
1051 service->NotifyPrintServiceEvent(jobId, event);
1052 }
1053
1054 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0056, TestSize.Level1)
1055 {
1056 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1057 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1058 service->helper_ = helper;
1059 std::string type = "";
1060 sptr<IPrintCallback> listener = nullptr;
1061 EXPECT_EQ(service->RegisterPrinterCallback(type, listener), E_PRINT_INVALID_PARAMETER);
1062 EXPECT_EQ(service->UnregisterPrinterCallback(type), E_PRINT_INVALID_TOKEN);
1063 sptr<IPrintCallback> listener2 = new MockPrintCallbackProxy();
1064 service->RegisterPrinterCallback(type, listener2);
1065 service->UnregisterPrinterCallback(type);
1066 }
1067
1068 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0057, TestSize.Level1)
1069 {
1070 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1071 int event = 0;
1072 PrinterInfo info;
1073 EXPECT_EQ(service->SendPrinterChangeEvent(event, info), 0);
1074 }
1075
1076 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0058, TestSize.Level1)
1077 {
1078 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1079 std::shared_ptr<PrintJob> jobInfo = std::make_shared<PrintJob>();
1080 std::string printerId = "1234";
1081 uint32_t subState = 0;
1082 EXPECT_NE(service->ReportHisysEvent(jobInfo, printerId, subState), 0);
1083 printerId = "ePrintID";
1084 EXPECT_NE(service->ReportHisysEvent(jobInfo, printerId, subState), 0);
1085 std::shared_ptr<PrinterInfo> printInfo = std::make_shared<PrinterInfo>();
1086 printInfo->SetPrinterName("Direct0759");
1087 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printInfo;
1088 EXPECT_NE(service->ReportHisysEvent(jobInfo, printerId, subState), 0);
1089 Json::Value infoJson;
1090 infoJson["printerUri"] = "ipp123";
1091 jobInfo->SetOption(PrintJsonUtil::WriteString(infoJson));
1092 EXPECT_NE(service->ReportHisysEvent(jobInfo, printerId, subState), 0);
1093 infoJson["jobDescription"] = "testPrinter";
1094 jobInfo->SetOption(PrintJsonUtil::WriteString(infoJson));
1095 EXPECT_NE(service->ReportHisysEvent(jobInfo, printerId, subState), 0);
1096 }
1097
1098 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0059, TestSize.Level1)
1099 {
1100 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1101 PrintJob jobInfo;
1102 std::string previewResult = "";
1103 service->RequestPreview(jobInfo, previewResult);
1104
1105 std::string extensionId = "DEFAULT_EXTENSION_ID";
1106 std::string extInfo = "spooler";
1107 service->SendExtensionEvent(extensionId, extInfo);
1108 EXPECT_EQ(service->UnregisterAllExtCallback(extensionId), E_PRINT_NONE);
1109 }
1110
1111 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0060, TestSize.Level1)
1112 {
1113 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1114 AppExecFwk::ExtensionAbilityInfo extInfo;
1115 extInfo.bundleName = "spooler";
1116
1117 PrintExtensionInfo newExtInfo;
1118 PrintExtensionInfo judgeExtInfo;
1119 judgeExtInfo.SetExtensionId(extInfo.bundleName);
1120 judgeExtInfo.SetVendorId(extInfo.bundleName);
1121 judgeExtInfo.SetVendorName(extInfo.bundleName);
1122 judgeExtInfo.SetVendorIcon(0);
1123 judgeExtInfo.SetVersion("1.0.0");
1124 newExtInfo = service->ConvertToPrintExtensionInfo(extInfo);
1125 EXPECT_EQ(newExtInfo.extensionId_, judgeExtInfo.extensionId_);
1126 EXPECT_EQ(newExtInfo.vendorId_, judgeExtInfo.vendorId_);
1127 EXPECT_EQ(newExtInfo.vendorName_, judgeExtInfo.vendorName_);
1128 EXPECT_EQ(newExtInfo.vendorIcon_, judgeExtInfo.vendorIcon_);
1129 EXPECT_EQ(newExtInfo.version_, judgeExtInfo.version_);
1130 }
1131
1132 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0061, TestSize.Level1)
1133 {
1134 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1135 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1136 service->helper_ = helper;
1137 std::string jobId = "123";
1138 int32_t userId = 100;
1139 service->userJobMap_[jobId] = userId;
1140 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1141 service->printUserMap_[userId] = userData;
1142 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_INVALID_PRINTJOB);
1143 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1144 userData->queuedJobList_[jobId] = printJob;
1145 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_NONE);
1146 printJob->SetJobState(PRINT_JOB_RUNNING);
1147 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_NONE);
1148 printJob->SetPrinterId("1234");
1149 std::string extensionId = PrintUtils::GetExtensionId("1234");
1150 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_CANCEL_PRINT);
1151 sptr<IPrintExtensionCallback> extCb = nullptr;
1152 service->extCallbackMap_[cid] = extCb;
1153 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_NONE);
1154 printJob->SetPrinterId("com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620");
1155 std::string extensionId2 = PrintUtils::GetExtensionId("com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620");
1156 std::string cid2 = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_CANCEL_PRINT);
1157 service->extCallbackMap_[cid2] = extCb;
1158 EXPECT_EQ(service->CancelPrintJob(jobId), E_PRINT_NONE);
1159 }
1160
1161 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0062, TestSize.Level1)
1162 {
1163 DelayedSingleton<PrintBMSHelper>::GetInstance()->hasBms = false;
1164 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1165 std::vector<std::string> extensionIds;
1166 std::string extensionId = "com.ohos.spooler";
1167 extensionIds.push_back(extensionId);
1168 EXPECT_EQ(service->StartDiscoverPrinter(extensionIds), E_PRINT_INVALID_EXTENSION);
1169 std::vector<PrintExtensionInfo> extensionInfos;
1170 service->QueryAllExtension(extensionInfos);
1171 service->StartDiscoverPrinter(extensionIds);
1172 }
1173
1174 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0063, TestSize.Level1)
1175 {
1176 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1177 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1178 service->helper_ = helper;
1179 std::string extensionId = "com.ohos.spooler:0";
1180 service->extensionStateList_[extensionId] = PRINT_EXTENSION_UNLOAD;
1181 EXPECT_EQ(service->DestroyExtension(), E_PRINT_NONE);
1182 service->extensionStateList_[extensionId] = PRINT_EXTENSION_LOADED;
1183 EXPECT_EQ(service->DestroyExtension(), E_PRINT_NONE);
1184 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_DESTROY_EXTENSION);
1185 sptr<IPrintExtensionCallback> listener = nullptr;
1186 service->extCallbackMap_[cid] = listener;
1187 EXPECT_EQ(service->DestroyExtension(), E_PRINT_NONE);
1188 }
1189
1190 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0064, TestSize.Level1)
1191 {
1192 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1193 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1194 service->helper_ = helper;
1195 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1196 PrinterInfo info;
1197 EXPECT_EQ(service->QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_INVALID_PRINTER);
1198 auto printerInfo = std::make_shared<CupsPrinterInfo>();
1199 printerInfo->name = "testName";
1200 printerInfo->uri = "testUri";
1201 printerInfo->maker = "testMaker";
1202 PrinterCapability caps;
1203 Json::Value opsJson;
1204 opsJson["key"] = "value";
1205 caps.SetOption(PrintJsonUtil::WriteString(opsJson));
1206 std::vector<PrintPageSize> pageSizeList;
1207 PrintPageSize pageSize;
1208 pageSizeList.push_back(pageSize);
1209 caps.SetSupportedPageSize(pageSizeList);
1210 printerInfo->printerCapability = caps;
1211 service->printSystemData_.addedPrinterMap_.Insert(printerId, printerInfo);
1212 EXPECT_EQ(service->QueryPrinterInfoByPrinterId(printerId, info), E_PRINT_NONE);
1213 }
1214
1215 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0065, TestSize.Level1)
1216 {
1217 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1218 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1219 auto info = std::make_shared<PrinterInfo>();
1220 info->SetPrinterId(printerId);
1221 service->printSystemData_.addedPrinterInfoList_[printerId] = info;
1222 std::vector<std::string> keyList;
1223 keyList.emplace_back("pagesizeId");
1224 keyList.emplace_back("orientation");
1225 keyList.emplace_back("duplex");
1226 keyList.emplace_back("quality");
1227 keyList.emplace_back("test");
1228 std::vector<std::string> valueList;
1229 EXPECT_EQ(service->QueryPrinterProperties(printerId, keyList, valueList), E_PRINT_INVALID_PRINTER);
1230 }
1231
1232 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0066, TestSize.Level1)
1233 {
1234 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1235 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1236 auto info = std::make_shared<PrinterInfo>();
1237 info->SetPrinterId(printerId);
1238 service->printSystemData_.addedPrinterInfoList_[printerId] = info;
1239 std::string savePrinterPreference = "test";
1240 std::vector<std::string> keyList;
1241 keyList.emplace_back("pagesizeId");
1242 keyList.emplace_back("orientation");
1243 keyList.emplace_back("duplex");
1244 keyList.emplace_back("quality");
1245 keyList.emplace_back("test");
1246 std::vector<std::string> valueList;
1247 EXPECT_EQ(service->QueryPrinterProperties(printerId, keyList, valueList), E_PRINT_INVALID_PRINTER);
1248 }
1249
1250 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0067, TestSize.Level1)
1251 {
1252 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1253 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1254 std::string printerExtId = PrintUtils::GetGlobalId("", printerId);
1255 PrinterCapability printerCaps;
1256 printerCaps.SetOption("test");
1257 std::string printerUri = "ipp://192.168.186.1:631/ipp/print";
1258 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NONE);
1259 }
1260
1261 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0068, TestSize.Level1)
1262 {
1263 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1264 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1265 std::string printerExtId = PrintUtils::GetGlobalId("", printerId);
1266 PrinterCapability printerCaps;
1267 printerCaps.SetOption("test");
1268 std::string printerUri = "ipp://192.168.186.1:631/ipp/print";
1269 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NONE);
1270 }
1271
1272 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0069, TestSize.Level1)
1273 {
1274 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1275 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1276 PrinterCapability printerCaps;
1277 std::string printerUri = "ipp://192.168.186.1:631/ipp/print";
1278 service->printSystemData_.discoveredPrinterInfoList_["123"] = nullptr;
1279 auto printerInfo = std::make_shared<PrinterInfo>();
1280 service->printSystemData_.discoveredPrinterInfoList_["456"] = printerInfo;
1281 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printerInfo;
1282 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NONE);
1283 }
1284
1285 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0079, TestSize.Level1)
1286 {
1287 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1288 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1289 service->helper_ = helper;
1290 std::string printerId = "123";
1291 PrintJob printJob;
1292 Json::Value opsJson;
1293 opsJson["key"] = "value";
1294 printJob.SetPrinterId(printerId);
1295 printJob.SetOption(PrintJsonUtil::WriteString(opsJson));
1296 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
1297 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
1298 auto ret = service->StartNativePrintJob(printJob);
1299 EXPECT_EQ(ret, E_PRINT_INVALID_PRINTER);
1300 auto extensionId = PrintUtils::GetExtensionId(printerId);
1301 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_START_PRINT);
1302 sptr<IPrintExtensionCallback> extCb = nullptr;
1303 service->extCallbackMap_[cid] = extCb;
1304 ret = service->StartNativePrintJob(printJob);
1305 EXPECT_EQ(ret, E_PRINT_INVALID_PRINTER);
1306 }
1307
1308 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0080, TestSize.Level1)
1309 {
1310 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1311 std::string printerId = "123";
1312 service->SetLastUsedPrinter(printerId);
1313 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
1314 auto ret = service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
1315 service->SetLastUsedPrinter(printerId);
1316 EXPECT_EQ(ret, 1);
1317 }
1318
1319 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0081, TestSize.Level1)
1320 {
1321 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1322 uint32_t state = PRINT_JOB_UNKNOWN + 1;
1323 uint32_t subState = PRINT_JOB_CREATE_FILE_COMPLETED_SUCCESS;
1324 service->checkJobState(state, subState);
1325 std::string jobId = "123";
1326 auto ret = service->UpdatePrintJobStateOnlyForSystemApp(jobId, state, subState);
1327 EXPECT_EQ(ret, E_PRINT_NONE);
1328 }
1329
1330 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0082, TestSize.Level1)
1331 {
1332 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1333 uint32_t state = PRINT_JOB_CREATE_FILE_COMPLETED;
1334 uint32_t subState = PRINT_JOB_CREATE_FILE_COMPLETED_SUCCESS;
1335 std::string jobId = "123";
1336 std::string type = PRINT_GET_FILE_EVENT_TYPE;
1337 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1338 service->registeredListeners_[type] = listener;
1339 auto ret = service->AdapterGetFileCallBack(jobId, state, subState);
1340 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1341 subState = PRINT_JOB_CREATE_FILE_COMPLETED_FAILED;
1342 ret = service->AdapterGetFileCallBack(jobId, state, subState);
1343 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1344 subState = PRINT_JOB_BLOCKED_UNKNOWN;
1345 ret = service->AdapterGetFileCallBack(jobId, state, subState);
1346 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1347 }
1348
1349 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0083, TestSize.Level1)
1350 {
1351 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1352 std::string jobId = "1";
1353 int32_t userId = 100;
1354 service->userJobMap_[jobId] = userId;
1355 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1356 service->printUserMap_[userId] = userData;
1357 uint32_t state = PRINT_JOB_COMPLETED;
1358 uint32_t subState = 0;
1359 std::string printerId = "1234";
1360 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1361 printJob->SetPrinterId(printerId);
1362 userData->printJobList_[jobId] = printJob;
1363 service->printerJobMap_[printerId].insert(std::make_pair(jobId, true));
1364 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_NONE);
1365 }
1366
1367 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0084, TestSize.Level1)
1368 {
1369 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1370 std::string jobId = "1";
1371 int32_t userId = 100;
1372 service->userJobMap_[jobId] = userId;
1373 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1374 service->printUserMap_[userId] = userData;
1375 uint32_t state = PRINT_JOB_COMPLETED;
1376 uint32_t subState = 0;
1377 std::string printerId = "1234";
1378 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1379 printJob->SetPrinterId(printerId);
1380 userData->printJobList_[jobId] = printJob;
1381 service->printerJobMap_[printerId].insert(std::make_pair(jobId, true));
1382 auto printerInfo = std::make_shared<PrinterInfo>();
1383 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printerInfo;
1384 service->printSystemData_.addedPrinterInfoList_[printerId] = printerInfo;
1385 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_NONE);
1386 }
1387
1388 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0085, TestSize.Level1)
1389 {
1390 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1391 std::string printerId = "1234";
1392 service->printAppCount_ = 1;
1393 service->ReportCompletedPrint(printerId);
1394 std::string jobId = "1";
1395 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1396 service->queuedJobList_[jobId] = printJob;
1397 service->printAppCount_ = 0;
1398 service->ReportCompletedPrint(printerId);
1399 EXPECT_EQ(service->unloadCount_, 0);
1400 }
1401
1402 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0086, TestSize.Level1)
1403 {
1404 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1405 std::string printerId = "";
1406 EXPECT_EQ(service->isEprint(printerId), false);
1407 }
1408
1409 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0087, TestSize.Level1)
1410 {
1411 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1412 std::string printerId = "1234";
1413 auto ret = service->QueryPrinterCapability(printerId);
1414 EXPECT_EQ(ret, E_PRINT_INVALID_PRINTER);
1415 auto printerInfo = std::make_shared<PrinterInfo>();
1416 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printerInfo;
1417 ret = service->QueryPrinterCapability(printerId);
1418 EXPECT_EQ(ret, E_PRINT_SERVER_FAILURE);
1419 std::string extensionId = PrintUtils::GetExtensionId(printerId);
1420 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_REQUEST_CAP);
1421 sptr<IPrintExtensionCallback> extCb = nullptr;
1422 service->extCallbackMap_[cid] = extCb;
1423 ret = service->QueryPrinterCapability(printerId);
1424 EXPECT_EQ(ret, E_PRINT_NONE);
1425 }
1426
1427 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0088, TestSize.Level1)
1428 {
1429 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1430 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1431 service->helper_ = helper;
1432 std::string extensionCid = "";
1433 sptr<IPrintExtensionCallback> listener = nullptr;
1434 auto ret = service->RegisterExtCallback(extensionCid, listener);
1435 EXPECT_EQ(ret, E_PRINT_INVALID_PARAMETER);
1436 std::string extensionCid2 = "123:20";
1437 ret = service->RegisterExtCallback(extensionCid2, listener);
1438 EXPECT_EQ(ret, E_PRINT_INVALID_EXTENSION);
1439 std::string extensionId = "123";
1440 service->extensionStateList_[extensionId] = PRINT_EXTENSION_UNLOAD;
1441 ret = service->RegisterExtCallback(extensionCid2, listener);
1442 EXPECT_EQ(ret, E_PRINT_INVALID_EXTENSION);
1443 service->extensionStateList_[extensionId] = PRINT_EXTENSION_LOADING;
1444 ret = service->RegisterExtCallback(extensionCid2, listener);
1445 EXPECT_EQ(ret, E_PRINT_INVALID_PARAMETER);
1446 std::string extensionCid3 = "123:2";
1447 ret = service->RegisterExtCallback(extensionCid2, listener);
1448 EXPECT_EQ(ret, E_PRINT_INVALID_PARAMETER);
1449 }
1450
1451 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0089, TestSize.Level1)
1452 {
1453 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1454 uint32_t callbackId = 0;
1455 std::string extensionId = "123";
1456 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, callbackId);
1457 sptr<IPrintExtensionCallback> extCb = nullptr;
1458 service->extCallbackMap_[cid] = extCb;
1459 auto ret = service->UnregisterAllExtCallback(extensionId);
1460 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1461 }
1462
1463 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0090, TestSize.Level1)
1464 {
1465 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1466 std::string extensionId = "123";
1467 service->extensionStateList_[extensionId] = PRINT_EXTENSION_UNLOAD;
1468 auto ret = service->LoadExtSuccess(extensionId);
1469 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1470 service->extensionStateList_[extensionId] = PRINT_EXTENSION_LOADING;
1471 ret = service->LoadExtSuccess(extensionId);
1472 EXPECT_EQ(ret, E_PRINT_NO_PERMISSION);
1473 }
1474
1475 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0091, TestSize.Level1)
1476 {
1477 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1478 std::string jobId = "123";
1479 std::string type = PRINT_CALLBACK_ADAPTER;
1480 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1481 EXPECT_NE(listener, nullptr);
1482 service->On(jobId, type, listener);
1483 type = PRINTER_CHANGE_EVENT_TYPE;
1484 service->On(jobId, type, listener);
1485 }
1486
1487 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0092, TestSize.Level1)
1488 {
1489 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1490 service->helper_ = nullptr;
1491 AAFwk::Want want;
1492 auto ret = service->StartAbility(want);
1493 EXPECT_EQ(ret, E_PRINT_NONE);
1494 }
1495
1496 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0093, TestSize.Level1)
1497 {
1498 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1499 service->printUserDataMap_[100] = nullptr;
1500 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1501 service->printUserDataMap_[101] = userData;
1502 int event = 0;
1503 PrinterInfo info;
1504 EXPECT_NE(service->SendPrinterDiscoverEvent(event, info), 0);
1505 }
1506
1507 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0094, TestSize.Level1)
1508 {
1509 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1510 service->printUserDataMap_[100] = nullptr;
1511 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1512 service->printUserDataMap_[101] = userData;
1513 int event = 0;
1514 PrinterInfo info;
1515 EXPECT_NE(service->SendPrinterChangeEvent(event, info), 0);
1516 }
1517
1518 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0095, TestSize.Level1)
1519 {
1520 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1521 PrinterInfo info;
1522 service->SendPrinterEvent(info);
1523 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1524 service->registeredListeners_[PRINTER_EVENT_TYPE] = listener;
1525 service->SendPrinterEvent(info);
1526 EXPECT_EQ(info.GetPrinterState(), PRINTER_UNKNOWN);
1527 }
1528
1529 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0096, TestSize.Level1)
1530 {
1531 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1532 PrinterInfo info;
1533 PrinterEvent printerEvent = PRINTER_EVENT_STATE_CHANGED;
1534 service->registeredListeners_["test"] = nullptr;
1535 EXPECT_EQ(service->SendPrinterEventChangeEvent(printerEvent, info), 0);
1536
1537 std::string eventType = "123" + PRINTER_CHANGE_EVENT_TYPE;
1538 service->registeredListeners_[eventType] = nullptr;
1539 EXPECT_EQ(service->SendPrinterEventChangeEvent(printerEvent, info), 0);
1540
1541 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1542 service->registeredListeners_[eventType] = listener;
1543 EXPECT_EQ(service->SendPrinterEventChangeEvent(printerEvent, info), 0);
1544 }
1545
1546 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0097, TestSize.Level1)
1547 {
1548 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1549 PrintJob jobInfo;
1550 jobInfo.SetJobId("1");
1551 service->SendPrintJobEvent(jobInfo);
1552 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1553 service->registeredListeners_[PRINTJOB_EVENT_TYPE] = listener;
1554 service->SendPrintJobEvent(jobInfo);
1555 jobInfo.SetJobState(PRINT_JOB_COMPLETED);
1556 service->SendPrintJobEvent(jobInfo);
1557 jobInfo.SetJobState(PRINT_JOB_BLOCKED);
1558 service->SendPrintJobEvent(jobInfo);
1559 jobInfo.SetJobState(PRINT_JOB_COMPLETED);
1560 jobInfo.SetSubState(PRINT_JOB_COMPLETED_SUCCESS);
1561 service->SendPrintJobEvent(jobInfo);
1562 jobInfo.SetSubState(PRINT_JOB_COMPLETED_FAILED);
1563 service->SendPrintJobEvent(jobInfo);
1564 jobInfo.SetSubState(PRINT_JOB_COMPLETED_CANCELLED);
1565 std::string taskEvent = PrintUtils::GetTaskEventId(jobInfo.GetJobId(), EVENT_CANCEL);
1566 service->registeredListeners_[taskEvent] = listener;
1567 service->SendPrintJobEvent(jobInfo);
1568 }
1569
1570 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0098, TestSize.Level1)
1571 {
1572 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1573 std::string extensionId = "123";
1574 std::string extInfo = "123";
1575 EXPECT_EQ(service->SendExtensionEvent(extensionId, extInfo), 0);
1576 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1577 service->registeredListeners_[EXTINFO_EVENT_TYPE] = listener;
1578 EXPECT_NE(service->SendExtensionEvent(extensionId, extInfo), 0);
1579 }
1580
1581 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0099, TestSize.Level1)
1582 {
1583 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1584 PrintJob jobInfo;
1585 std::string jobId = "123";
1586 jobInfo.SetJobId(jobId);
1587 service->isJobQueueBlocked_ = true;
1588 jobInfo.SetJobState(PRINT_JOB_COMPLETED);
1589 service->CheckJobQueueBlocked(jobInfo);
1590 EXPECT_EQ(service->isJobQueueBlocked_, true);
1591 service->isJobQueueBlocked_ = false;
1592 service->CheckJobQueueBlocked(jobInfo);
1593 EXPECT_EQ(service->isJobQueueBlocked_, false);
1594 jobInfo.SetJobState(PRINT_JOB_BLOCKED);
1595 service->CheckJobQueueBlocked(jobInfo);
1596 EXPECT_EQ(service->isJobQueueBlocked_, true);
1597 int32_t userId = 100;
1598 service->userJobMap_[jobId] = userId;
1599 service->currentUserId_ = userId;
1600 service->CheckJobQueueBlocked(jobInfo);
1601 EXPECT_EQ(service->isJobQueueBlocked_, true);
1602 service->isJobQueueBlocked_ = true;
1603 jobInfo.SetJobState(PRINT_JOB_COMPLETED);
1604 service->CheckJobQueueBlocked(jobInfo);
1605 EXPECT_EQ(service->isJobQueueBlocked_, true);
1606 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1607 service->printUserMap_[userId] = userData;
1608 auto job = std::make_shared<PrintJob>();
1609 job->SetJobState(PRINT_JOB_COMPLETED);
1610 auto job2 = std::make_shared<PrintJob>();
1611 job2->SetJobState(PRINT_JOB_BLOCKED);
1612 userData->queuedJobList_["1"] = job;
1613 userData->queuedJobList_["2"] = job2;
1614 service->CheckJobQueueBlocked(jobInfo);
1615 EXPECT_EQ(service->isJobQueueBlocked_, true);
1616 userData->queuedJobList_.clear();
1617 userData->queuedJobList_["1"] = job;
1618 service->CheckJobQueueBlocked(jobInfo);
1619 EXPECT_EQ(service->isJobQueueBlocked_, false);
1620 service->userJobMap_.clear();
1621 service->CheckJobQueueBlocked(jobInfo);
1622 EXPECT_EQ(service->isJobQueueBlocked_, false);
1623 }
1624
1625 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0100, TestSize.Level1)
1626 {
1627 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1628 std::string jobName = "";
1629 PrintAttributes printAttributes;
1630 std::string taskId = "";
1631 auto ret = service->PrintByAdapter(jobName, printAttributes, taskId);
1632 EXPECT_EQ(ret, E_PRINT_INVALID_PARAMETER);
1633 }
1634
1635 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0102, TestSize.Level1)
1636 {
1637 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1638 std::string jobId = "123";
1639 uint32_t state = PRINT_JOB_RUNNING;
1640 uint32_t subState = PRINT_JOB_SPOOLER_CLOSED_FOR_CANCELED;
1641 service->notifyAdapterJobChanged(jobId, state, subState);
1642 auto attrIt = service->printAttributesList_.find(jobId);
1643 EXPECT_EQ(attrIt, service->printAttributesList_.end());
1644 state = PRINT_JOB_SPOOLER_CLOSED;
1645 service->notifyAdapterJobChanged(jobId, state, subState);
1646 attrIt = service->printAttributesList_.find(jobId);
1647 EXPECT_EQ(attrIt, service->printAttributesList_.end());
1648 state = PRINT_JOB_COMPLETED;
1649 PrintAttributes printAttributes;
1650 service->printAttributesList_[jobId] = printAttributes;
1651 service->notifyAdapterJobChanged(jobId, state, subState);
1652 attrIt = service->printAttributesList_.find(jobId);
1653 EXPECT_EQ(attrIt, service->printAttributesList_.end());
1654 }
1655
1656 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0103, TestSize.Level1)
1657 {
1658 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1659 service->helper_ = nullptr;
1660 AAFwk::Want want;
1661 EXPECT_FALSE(service->StartPluginPrintIconExtAbility(want));
1662 service->helper_ = std::make_shared<PrintServiceHelper>();
1663 service->StartPluginPrintIconExtAbility(want);
1664 }
1665
1666 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0104, TestSize.Level1)
1667 {
1668 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1669 std::string jobId = "123";
1670 EXPECT_EQ(service->GetUserDataByJobId(jobId), nullptr);
1671 int32_t userId = 100;
1672 service->userJobMap_[jobId] = userId;
1673 EXPECT_EQ(service->GetUserDataByJobId(jobId), nullptr);
1674 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1675 service->printUserMap_[userId] = userData;
1676 EXPECT_NE(service->GetUserDataByJobId(jobId), nullptr);
1677 }
1678
1679 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0105, TestSize.Level1)
1680 {
1681 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1682 std::string jobId = "123";
1683 service->RegisterAdapterListener(jobId);
1684 auto lisIt = service->adapterListenersByJobId_.find(jobId);
1685 EXPECT_EQ(lisIt, service->adapterListenersByJobId_.end());
1686 sptr<IPrintCallback> listener = new MockPrintCallbackProxy();
1687 service->registeredListeners_[PRINT_ADAPTER_EVENT_TYPE] = listener;
1688 service->RegisterAdapterListener(jobId);
1689 lisIt = service->adapterListenersByJobId_.find(jobId);
1690 EXPECT_NE(lisIt, service->adapterListenersByJobId_.end());
1691 }
1692
1693 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0106, TestSize.Level1)
1694 {
1695 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1696 std::string printerId = "1234";
1697 auto ret = service->SetDefaultPrinter(printerId, 0);
1698 EXPECT_EQ(ret, E_PRINT_NONE);
1699 }
1700
1701 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0107, TestSize.Level1)
1702 {
1703 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1704 std::string printerId = "1234";
1705 EXPECT_EQ(service->CheckIsDefaultPrinter(printerId), false);
1706 }
1707
1708 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0108, TestSize.Level1)
1709 {
1710 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1711 std::string printerId = "1234";
1712 EXPECT_EQ(service->CheckIsLastUsedPrinter(printerId), false);
1713 }
1714
1715 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0109, TestSize.Level1)
1716 {
1717 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1718 std::string printerName = "pixlab_0759";
1719 EXPECT_EQ(service->DeletePrinterFromCups(printerName), E_PRINT_NONE);
1720 }
1721
1722 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0111, TestSize.Level1)
1723 {
1724 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1725 std::string printerId = "printerId";
1726 int32_t userId1 = 100;
1727 int32_t userId2 = 101;
1728 std::shared_ptr<PrintUserData> userData1 = std::make_shared<PrintUserData>();
1729 auto ret = userData1->SetDefaultPrinter("test", 0);
1730 EXPECT_EQ(ret, E_PRINT_NONE);
1731 std::shared_ptr<PrintUserData> userData2 = std::make_shared<PrintUserData>();
1732 ret = userData1->SetDefaultPrinter(printerId, 0);
1733 EXPECT_EQ(ret, E_PRINT_NONE);
1734 service->printUserMap_[userId1] = userData1;
1735 service->printUserMap_[userId2] = userData2;
1736 service->DeletePrinterFromUserData(printerId);
1737 }
1738
1739 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0112, TestSize.Level1)
1740 {
1741 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1742 std::string jobId = "1";
1743 int32_t userId = 100;
1744 service->userJobMap_[jobId] = userId;
1745 std::shared_ptr<PrintUserData> userData = std::make_shared<PrintUserData>();
1746 service->printUserMap_[userId] = userData;
1747 uint32_t state = PRINT_JOB_COMPLETED;
1748 uint32_t subState = 0;
1749 std::string printerId = "1234";
1750 std::shared_ptr<PrintJob> printJob = std::make_shared<PrintJob>();
1751 printJob->SetPrinterId(printerId);
1752 userData->queuedJobList_[jobId] = printJob;
1753 service->printerJobMap_[printerId].insert(std::make_pair(jobId, true));
1754 auto printerInfo = std::make_shared<PrinterInfo>();
1755 service->printSystemData_.discoveredPrinterInfoList_[printerId] = printerInfo;
1756 service->printSystemData_.addedPrinterInfoList_[printerId] = printerInfo;
1757 service->currentUserId_ = 0;
1758 EXPECT_EQ(service->CheckAndSendQueuePrintJob(jobId, state, subState), E_PRINT_NONE);
1759 }
1760
1761 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0113, TestSize.Level1)
1762 {
1763 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1764 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1765 service->helper_ = helper;
1766 std::string taskId = "";
1767 std::string type = PRINTER_CHANGE_EVENT_TYPE;
1768 auto ret = service->Off(taskId, type);
1769 EXPECT_EQ(ret, E_PRINT_NONE);
1770 }
1771
1772 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0120, TestSize.Level1)
1773 {
1774 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1775 EXPECT_NE(service, nullptr);
1776 std::map<std::string, std::shared_ptr<PrintJob>> jobList;
1777 auto job = std::make_shared<PrintJob>();
1778 EXPECT_NE(job, nullptr);
1779 job->SetJobState(PRINT_JOB_BLOCKED);
1780 jobList.insert(std::make_pair("test", job));
1781 EXPECT_EQ(service->DetermineUserJobStatus(jobList), PRINT_JOB_BLOCKED);
1782 }
1783
1784 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0121, TestSize.Level1)
1785 {
1786 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1787 EXPECT_NE(service, nullptr);
1788 std::map<std::string, std::shared_ptr<PrintJob>> jobList;
1789 auto job = std::make_shared<PrintJob>();
1790 EXPECT_NE(job, nullptr);
1791 job->SetJobState(PRINT_JOB_COMPLETED);
1792 jobList.insert(std::make_pair("test", job));
1793 EXPECT_EQ(service->DetermineUserJobStatus(jobList), PRINT_JOB_COMPLETED);
1794 }
1795
1796 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0122, TestSize.Level1)
1797 {
1798 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1799 EXPECT_NE(service, nullptr);
1800 std::map<std::string, std::shared_ptr<PrintJob>> jobList;
1801 auto job = std::make_shared<PrintJob>();
1802 EXPECT_NE(job, nullptr);
1803 job->SetJobState(PRINT_JOB_RUNNING);
1804 jobList.insert(std::make_pair("test", job));
1805 EXPECT_EQ(service->DetermineUserJobStatus(jobList), PRINT_JOB_RUNNING);
1806 }
1807
1808 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0123, TestSize.Level1)
1809 {
1810 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1811 int32_t userId = 100;
1812 auto userData1 = service->GetUserDataByUserId(100);
1813 EXPECT_NE(userData1, nullptr);
1814 auto userData2 = service->GetUserDataByUserId(100);
1815 EXPECT_EQ(userData1, userData2);
1816 auto job = std::make_shared<PrintJob>();
1817 EXPECT_NE(job, nullptr);
1818 job->SetJobState(PRINT_JOB_BLOCKED);
1819 userData1->queuedJobList_.insert(std::make_pair("test1", job));
1820 service->NotifyCurrentUserChanged(userId);
1821 auto job2 = std::make_shared<PrintJob>();
1822 EXPECT_NE(job2, nullptr);
1823 job2->SetJobState(PRINT_JOB_RUNNING);
1824 userData1->queuedJobList_.insert(std::make_pair("test2", job2));
1825 service->NotifyCurrentUserChanged(userId);
1826 }
1827
1828 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0124, TestSize.Level1)
1829 {
1830 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1831 int32_t userId = 100;
1832 auto userData1 = service->GetUserDataByUserId(100);
1833 EXPECT_NE(userData1, nullptr);
1834 auto userData2 = service->GetUserDataByUserId(100);
1835 EXPECT_EQ(userData1, userData2);
1836 auto job = std::make_shared<PrintJob>();
1837 EXPECT_NE(job, nullptr);
1838 job->SetJobState(PRINT_JOB_COMPLETED);
1839 userData1->queuedJobList_.insert(std::make_pair("test1", job));
1840 service->NotifyCurrentUserChanged(userId);
1841 auto job2 = std::make_shared<PrintJob>();
1842 EXPECT_NE(job2, nullptr);
1843 job2->SetJobState(PRINT_JOB_RUNNING);
1844 userData1->queuedJobList_.insert(std::make_pair("test2", job2));
1845 service->NotifyCurrentUserChanged(userId);
1846 }
1847
1848 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0125, TestSize.Level1)
1849 {
1850 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1851 EXPECT_NE(service, nullptr);
1852 uint32_t subState = PRINT_JOB_SPOOLER_CLOSED_FOR_CANCELED;
1853 EXPECT_EQ(service->GetListeningState(subState), PREVIEW_ABILITY_DESTROY_FOR_CANCELED);
1854 subState = PRINT_JOB_SPOOLER_CLOSED_FOR_STARTED;
1855 EXPECT_EQ(service->GetListeningState(subState), PREVIEW_ABILITY_DESTROY_FOR_STARTED);
1856 subState = PRINT_JOB_CREATE_FILE_COMPLETED_SUCCESS;
1857 EXPECT_EQ(service->GetListeningState(subState), PREVIEW_ABILITY_DESTROY);
1858 uint32_t state1 = service->GetListeningState(PRINT_JOB_SPOOLER_CLOSED, subState);
1859 uint32_t state2 = service->GetListeningState(subState);
1860 EXPECT_EQ(state1, state2);
1861 state1 = service->GetListeningState(PRINT_JOB_BLOCKED, subState);
1862 EXPECT_EQ(state1, PRINT_TASK_BLOCK);
1863 state1 = service->GetListeningState(PRINT_JOB_QUEUED, PRINT_JOB_COMPLETED_SUCCESS);
1864 EXPECT_EQ(state1, PRINT_TASK_SUCCEED);
1865 state1 = service->GetListeningState(PRINT_JOB_QUEUED, PRINT_JOB_COMPLETED_FAILED);
1866 EXPECT_EQ(state1, PRINT_TASK_FAIL);
1867 state1 = service->GetListeningState(PRINT_JOB_QUEUED, PRINT_JOB_COMPLETED_CANCELLED);
1868 EXPECT_EQ(state1, PRINT_TASK_CANCEL);
1869 state1 = service->GetListeningState(PRINT_JOB_QUEUED, PRINT_JOB_BLOCKED_OUT_OF_INK);
1870 EXPECT_EQ(state1, PRINT_TASK_FAIL);
1871 }
1872
1873 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0126, TestSize.Level1)
1874 {
1875 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1876 std::string printerName = "testPrinterName";
1877 EXPECT_EQ(service->printSystemData_.QueryPrinterIdByStandardizeName(printerName), "");
1878 }
1879
1880 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0128, TestSize.Level1)
1881 {
1882 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1883 std::string printerId = "Pixlab_0759";
1884 PrinterInfo info;
1885 EXPECT_EQ(service->UpdatePrinterCapability(printerId, info), true);
1886 }
1887
1888 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0129, TestSize.Level1)
1889 {
1890 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1891 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
1892 service->helper_ = helper;
1893 std::vector<PrinterInfo> printers;
1894 service->DiscoverUsbPrinters(printers);
1895 EXPECT_EQ(printers.size(), 0);
1896 }
1897
1898 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0130, TestSize.Level1)
1899 {
1900 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1901 PrinterInfo info;
1902 info.SetPrinterId(DEFAULT_EXT_PRINTER_ID);
1903 EXPECT_EQ(service->AddPrinterToDiscovery(info), E_PRINT_NONE);
1904 EXPECT_EQ(service->UpdatePrinterInDiscovery(info), E_PRINT_NONE);
1905 info.SetPrinterId("1234");
1906 EXPECT_EQ(service->UpdatePrinterInDiscovery(info), E_PRINT_NONE);
1907
1908 std::shared_ptr<PrinterInfo> info1 = std::make_shared<PrinterInfo>();
1909 info1->SetPrinterId(DEFAULT_EXT_PRINTER_ID);
1910 service->printSystemData_.discoveredPrinterInfoList_[DEFAULT_EXT_PRINTER_ID] = info1;
1911 EXPECT_EQ(service->RemovePrinterFromDiscovery(DEFAULT_EXT_PRINTER_ID), E_PRINT_NONE);
1912 EXPECT_EQ(service->RemovePrinterFromDiscovery(DEFAULT_EXT_PRINTER_ID), E_PRINT_INVALID_PRINTER);
1913 }
1914
1915 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0131, TestSize.Level1)
1916 {
1917 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1918 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
1919 std::string printerExtId = PrintUtils::GetGlobalId("", printerId);
1920 PrinterCapability printerCaps;
1921 printerCaps.SetOption("test");
1922 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
1923 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_INVALID_PRINTER);
1924 }
1925
1926 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0132, TestSize.Level1)
1927 {
1928 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1929 std::string vendorName = "fwk.driver";
1930 std::string printerId = "testprinter";
1931 PrinterInfo info;
1932 info.SetPrinterId(printerId);
1933 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1934 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1935 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
1936 EXPECT_TRUE(service->RemoveVendorPrinterFromDiscovery(vendorName, printerId));
1937 }
1938
1939 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0133, TestSize.Level1)
1940 {
1941 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1942 std::string vendorName = "fwk.driver";
1943 std::string printerId = "testprinter";
1944 PrinterInfo info;
1945 PrinterInfo info2;
1946 info.SetPrinterId(printerId);
1947 std::string globalId = VendorManager::GetGlobalPrinterId(vendorName, printerId);
1948 EXPECT_EQ(service->QueryVendorPrinterInfo(globalId, info2), E_PRINT_INVALID_PRINTER);
1949 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
1950 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1951 EXPECT_EQ(service->QueryVendorPrinterInfo(globalId, info2), E_PRINT_INVALID_PRINTER);
1952 EXPECT_TRUE(service->RemoveVendorPrinterFromDiscovery(vendorName, printerId));
1953 PrinterCapability cap;
1954 info.SetCapability(cap);
1955 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1956 EXPECT_EQ(service->QueryVendorPrinterInfo(globalId, info2), E_PRINT_NONE);
1957 }
1958
1959 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0134, TestSize.Level1)
1960 {
1961 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1962 std::string vendorName = "fwk.driver";
1963 std::string printerId = "testprinter";
1964 std::string ppdData;
1965 PrinterInfo info;
1966 info.SetPrinterId(printerId);
1967 EXPECT_FALSE(service->RemoveVendorPrinterFromCups(vendorName, printerId));
1968 EXPECT_FALSE(service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData));
1969 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1970 EXPECT_FALSE(service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData));
1971 PrinterCapability cap;
1972 info.SetCapability(cap);
1973 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
1974 EXPECT_FALSE(service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData));
1975 info.SetUri("uri");
1976 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
1977 EXPECT_FALSE(service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData));
1978 info.SetPrinterMake("maker");
1979 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
1980 service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData);
1981 ppdData = "ppd";
1982 service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData);
1983 service->RemoveVendorPrinterFromCups(vendorName, printerId);
1984 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
1985 }
1986
1987 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0135, TestSize.Level1)
1988 {
1989 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
1990 EXPECT_EQ(service->TryConnectPrinterByIp(""), E_PRINT_INVALID_PRINTER);
1991 std::string param = "{\"protocol\":\"ipp\"}";
1992 EXPECT_EQ(service->TryConnectPrinterByIp(param), E_PRINT_INVALID_PRINTER);
1993 param = "{\"protocol\":\"ipp\",\"ip\":\"a.b.c.d\"}";
1994 EXPECT_EQ(service->TryConnectPrinterByIp(param), E_PRINT_SERVER_FAILURE);
1995 }
1996
1997 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0136, TestSize.Level1)
1998 {
1999 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2000 std::string vendorName = "fwk.driver";
2001 std::string printerId = "testprinter";
2002 std::string ppdData;
2003 PrinterInfo info;
2004 info.SetPrinterName(vendorName);
2005 info.SetPrinterId(printerId);
2006 PrinterCapability cap;
2007 info.SetCapability(cap);
2008 info.SetUri("uri");
2009 info.SetPrinterMake("maker");
2010 ppdData = "ppd";
2011 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
2012 service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData);
2013
2014 std::string vendorName1 = "fwk.driver";
2015 std::string printerId1 = "testprinter1";
2016 PrinterInfo info1;
2017 info1.SetPrinterName(vendorName1);
2018 info1.SetPrinterId(printerId1);
2019 PrinterCapability cap1;
2020 info1.SetCapability(cap1);
2021 info1.SetUri("uri1");
2022 info1.SetPrinterMake("maker1");
2023 EXPECT_TRUE(service->RenamePrinterWhenAdded(info1) == "fwk.driver 1");
2024 service->RemoveVendorPrinterFromCups(vendorName, printerId);
2025 }
2026
2027 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0137, TestSize.Level1)
2028 {
2029 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2030 PrinterInfo info;
2031 info.SetPrinterId(DEFAULT_EXT_PRINTER_ID);
2032 EXPECT_EQ(service->UpdatePrinterInSystem(info), E_PRINT_INVALID_PRINTER);
2033 }
2034
2035 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0138, TestSize.Level1)
2036 {
2037 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2038 PrinterInfo info;
2039 info.SetPrinterId(DEFAULT_EXT_PRINTER_ID);
2040 std::string type = "testType";
2041 EXPECT_EQ(service->CheckUserIdInEventType(type), false);
2042 type = PRINTER_CHANGE_EVENT_TYPE;
2043 EXPECT_EQ(service->CheckUserIdInEventType(type), false);
2044 }
2045
2046 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0139, TestSize.Level1)
2047 {
2048 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2049 std::string vendorName = "fwk.driver";
2050 std::string printerId = "testprinter";
2051 std::string ppdData;
2052 PrinterInfo info;
2053 info.SetPrinterId(printerId);
2054 EXPECT_FALSE(service->RemoveVendorPrinterFromCups(vendorName, printerId));
2055 EXPECT_FALSE(service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData));
2056 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
2057 EXPECT_FALSE(service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData));
2058 PrinterCapability cap;
2059 info.SetCapability(cap);
2060 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
2061 EXPECT_FALSE(service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData));
2062 info.SetUri("uri");
2063 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
2064 EXPECT_FALSE(service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData));
2065 info.SetPrinterMake("maker");
2066 EXPECT_TRUE(service->UpdateVendorPrinterToDiscovery(vendorName, info));
2067 service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData);
2068 ppdData = "ppd";
2069 service->AddVendorPrinterToCupsWithSpecificPpd(vendorName, printerId, ppdData);
2070 service->RemoveVendorPrinterFromCups(vendorName, printerId);
2071 EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
2072 }
2073
2074 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0140, TestSize.Level1)
2075 {
2076 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2077 std::string printerId = "testPrinterId";
2078 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
2079 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
2080 PrinterInfo info;
2081 EXPECT_EQ(service->UpdatePrinterCapability(printerId, info), true);
2082 }
2083
2084 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0141, TestSize.Level1)
2085 {
2086 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2087 std::string printerId = "com.ohos.spooler:testPrinterId";
2088 PrinterInfo info;
2089 info.SetPrinterId(printerId);
2090 auto infoPtr = std::make_shared<PrinterInfo>(info);
2091 service->printSystemData_.AddPrinterToDiscovery(infoPtr);
2092 std::string extensionId = "com.ohos.spooler";
2093 EXPECT_EQ(service->AddSinglePrinterInfo(info, extensionId), E_PRINT_NONE);
2094 }
2095
2096 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0143, TestSize.Level1)
2097 {
2098 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2099 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2100 service->helper_ = helper;
2101 std::string printerId = "123";
2102 PrinterPreferences preferences;
2103 preferences.SetBorderless(true);
2104 EXPECT_EQ(service->SetPrinterPreference(printerId, preferences), E_PRINT_NONE);
2105 }
2106
2107 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0144, TestSize.Level1)
2108 {
2109 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2110 std::string printerId = "com.ohos.spooler:testPrinterId";
2111 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
2112 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
2113 PrinterInfo info;
2114 EXPECT_EQ(service->UpdatePrinterCapability(printerId, info), true);
2115 }
2116
2117 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0145, TestSize.Level1)
2118 {
2119 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2120 EXPECT_EQ(service->StartPrintJobInternal(nullptr), E_PRINT_SERVER_FAILURE);
2121 }
2122
2123 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0146, TestSize.Level1)
2124 {
2125 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2126 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2127 service->helper_ = helper;
2128 std::string extensionId = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
2129 std::string printerId = "USB-PixLab_V1-1620";
2130 std::string standardizeId = PrintUtils::GetGlobalId(extensionId, printerId);
2131 PrinterCapability printerCaps;
2132 printerCaps.SetOption("test");
2133 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
2134 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2135 info->SetOption("test");
2136 service->printSystemData_.discoveredPrinterInfoList_[standardizeId] = info;
2137 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, standardizeId, printerCaps), E_PRINT_NONE);
2138 }
2139
2140 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0147, TestSize.Level1)
2141 {
2142 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2143 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2144 service->helper_ = helper;
2145 std::string extensionId = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
2146 std::string printerId = "USB-PixLab_V1-1620";
2147 std::string standardizeId = PrintUtils::GetGlobalId(extensionId, printerId);
2148 PrinterCapability printerCaps;
2149 printerCaps.SetOption("test");
2150 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
2151 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2152 Json::Value opsJson;
2153 opsJson["key"] = "value";
2154 info->SetOption(PrintJsonUtil::WriteString(opsJson));
2155 service->printSystemData_.discoveredPrinterInfoList_[standardizeId] = info;
2156 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, standardizeId, printerCaps), E_PRINT_INVALID_PRINTER);
2157 }
2158
2159 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0148, TestSize.Level1)
2160 {
2161 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2162 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2163 service->helper_ = helper;
2164 std::string extensionId = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
2165 std::string printerId = "USB-PixLab_V1-1620";
2166 std::string standardizeId = PrintUtils::GetGlobalId(extensionId, printerId);
2167 PrinterCapability printerCaps;
2168 printerCaps.SetOption("test");
2169 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
2170 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2171 Json::Value opsJson;
2172 opsJson["printerMake"] = 123;
2173 info->SetOption(PrintJsonUtil::WriteString(opsJson));
2174 service->printSystemData_.discoveredPrinterInfoList_[standardizeId] = info;
2175 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, standardizeId, printerCaps), E_PRINT_INVALID_PRINTER);
2176 }
2177
2178 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0149, TestSize.Level1)
2179 {
2180 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2181 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2182 service->helper_ = helper;
2183 std::vector<std::string> printerList;
2184 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
2185 cupsPrinter->name = "testPrinterName";
2186 std::string printerId = "testPrinterId";
2187 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
2188 EXPECT_EQ(service->QueryAddedPrinter(printerList), E_PRINT_NONE);
2189 }
2190
2191 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0150, TestSize.Level1)
2192 {
2193 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2194 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2195 service->helper_ = helper;
2196 std::vector<std::string> printerList;
2197 auto cupsPrinter = std::make_shared<CupsPrinterInfo>();
2198 cupsPrinter->name = "testPrinterName";
2199 std::string printerId = "";
2200 service->printSystemData_.addedPrinterMap_.Insert(printerId, cupsPrinter);
2201 EXPECT_EQ(service->QueryAddedPrinter(printerList), E_PRINT_NONE);
2202 }
2203
2204 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0153, TestSize.Level1)
2205 {
2206 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2207 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2208 service->helper_ = helper;
2209 std::string extensionId = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
2210 std::string printerId = "USB-PixLab_V1-1620";
2211 std::string standardizeId = PrintUtils::GetGlobalId(extensionId, printerId);
2212 PrinterCapability printerCaps;
2213 printerCaps.SetOption("test");
2214 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
2215 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2216 service->printSystemData_.discoveredPrinterInfoList_[standardizeId] = info;
2217 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, standardizeId, printerCaps), E_PRINT_NONE);
2218 }
2219
2220 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0155, TestSize.Level1)
2221 {
2222 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2223 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2224 service->helper_ = helper;
2225 PrintJob printJob;
2226 std::string jobId = "123";
2227 printJob.SetJobId(jobId);
2228 std::string printerId = "com.ohos.spooler:p2p://DIRECT-PixLab_V1-1620";
2229 printJob.SetPrinterId(printerId);
2230 auto printerInfo = std::make_shared<CupsPrinterInfo>();
2231 printerInfo->name = "testName";
2232 printerInfo->uri = "testUri";
2233 printerInfo->maker = "testMaker";
2234 Json::Value infoJson;
2235 infoJson["printerName"] = "testPrinterName";
2236 printJob.SetOption(PrintJsonUtil::WriteString(infoJson));
2237 service->printSystemData_.addedPrinterMap_.Insert(printerId, printerInfo);
2238 std::string extensionId = PrintUtils::GetExtensionId(printerId);
2239 std::string cid = PrintUtils::EncodeExtensionCid(extensionId, PRINT_EXTCB_START_PRINT);
2240 sptr<IPrintExtensionCallback> extCb = nullptr;
2241 service->extCallbackMap_[cid] = extCb;
2242 EXPECT_EQ(service->CheckPrintJob(printJob), false);
2243 auto printJobPtr = std::make_shared<PrintJob>(printJob);
2244 service->printJobList_[jobId] = printJobPtr;
2245 EXPECT_EQ(service->CheckPrintJob(printJob), true);
2246 }
2247
2248 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0156, TestSize.Level1)
2249 {
2250 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2251 std::shared_ptr<PrintServiceHelper> helper = std::make_shared<PrintServiceHelper>();
2252 service->helper_ = helper;
2253 std::string extensionId = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
2254 std::string printerId = "USB-PixLab_V1-1620";
2255 std::string standardizeId = PrintUtils::GetGlobalId(extensionId, printerId);
2256 PrinterCapability printerCaps;
2257 printerCaps.SetOption("test");
2258 std::string printerUri = "usb:ipp://192.168.186.1:631/ipp/print";
2259 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2260 Json::Value opsJson;
2261 opsJson["printerMake"] = "123";
2262 info->SetOption(PrintJsonUtil::WriteString(opsJson));
2263 service->printSystemData_.discoveredPrinterInfoList_[standardizeId] = info;
2264 EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, standardizeId, printerCaps), E_PRINT_SERVER_FAILURE);
2265 }
2266
2267 /**
2268 * @tc.name: PrintServiceAbilityTest_0157
2269 * @tc.desc: PrintServiceAbility ctor/dtor
2270 * @tc.type: FUNC ConnectPrinter
2271 * @tc.require: use old version printerId
2272 */
2273 HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0157, TestSize.Level1)
2274 {
2275 auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
2276 std::shared_ptr<PrinterInfo> info = std::make_shared<PrinterInfo>();
2277 info->SetPrinterId("1234");
2278 EXPECT_EQ(service->ConnectPrinter(info->GetPrinterId()), E_PRINT_INVALID_PRINTER);
2279 info->SetPrinterId("com.ohos.spooler:mdns://testId");
2280 EXPECT_EQ(service->ConnectPrinter(info->GetPrinterId()), E_PRINT_INVALID_PRINTER);
2281 CupsPrinterInfo cupsInfo;
2282 service->printSystemData_.InsertCupsPrinter(info->GetPrinterId(), cupsInfo);
2283 EXPECT_EQ(service->ConnectPrinter(info->GetPrinterId()), E_PRINT_NONE);
2284 }
2285 } // namespace OHOS::Print
2286