1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #define private public
18 #include "vendor_ppd_driver.h"
19 #undef private
20 #include "vendor_manager.h"
21 #include "print_constant.h"
22 #include "print_log.h"
23 #include "mock/mock_vendor_manager.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace {
29 const int WAIT_TIME_MS = 100;
30 const std::string PRINTER_TEST_IP = "192.168.2.222";
31 }
32
33 namespace OHOS {
34 namespace Print {
35 class VendorPpdDriverTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41 };
42
SetUpTestCase(void)43 void VendorPpdDriverTest::SetUpTestCase(void) {}
44
TearDownTestCase(void)45 void VendorPpdDriverTest::TearDownTestCase(void) {}
46
SetUp(void)47 void VendorPpdDriverTest::SetUp(void)
48 {
49 static int32_t testNo = 0;
50 PRINT_HILOGI("VendorPpdDriverTest_%{public}d", ++testNo);
51 }
52
TearDown(void)53 void VendorPpdDriverTest::TearDown(void) {}
54
55 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0001, TestSize.Level1)
56 {
57 std::string printerId = "test.printer.id";
58 std::string defaultPpd = "test.ppd.name";
59 std::vector<std::string> propertyKeys(1, "test.property.key");
60 MockVendorManager mock;
61 VendorPpdDriver vendorDriver;
62 EXPECT_TRUE(vendorDriver.Init(&mock));
63 EXPECT_CALL(mock, QueryPPDInformation(_, _)).WillOnce(Return(false)).WillRepeatedly(
64 DoAll(SetArgReferee<1>(std::vector<std::string>(1, defaultPpd)), Return(true)));
65 EXPECT_FALSE(vendorDriver.OnQueryProperties(printerId, propertyKeys));
66 EXPECT_TRUE(vendorDriver.OnQueryProperties(printerId, propertyKeys));
67 std::vector<std::string> emptyPropertyKeys;
68 EXPECT_FALSE(vendorDriver.OnQueryProperties(printerId, emptyPropertyKeys));
69 vendorDriver.UnInit();
70 }
71
72 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0002, TestSize.Level1)
73 {
74 std::string printerId = "test.printer.id";
75 std::string defaultPpd = "test.ppd.name";
76 int defaultTimeout = 0;
77 MockVendorManager mock;
78 VendorPpdDriver vendorDriver;
79 EXPECT_TRUE(vendorDriver.Init(&mock));
80 EXPECT_CALL(mock, QueryDiscoveredPrinterInfoById(_, _)).WillOnce(Return(nullptr))
81 .WillRepeatedly(Return(std::make_shared<PrinterInfo>()));
82 EXPECT_CALL(mock, AddPrinterToCupsWithPpd(_, _, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
83 EXPECT_CALL(mock, QueryPrinterInfoByPrinterId(_, _, _)).WillRepeatedly(Return(E_PRINT_NONE));
84 EXPECT_CALL(mock, UpdatePrinterToDiscovery(_, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
85 vendorDriver.privatePrinterPpdMap.insert(std::make_pair(printerId, std::vector(1, defaultPpd)));
86 EXPECT_FALSE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
87 EXPECT_TRUE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
88 }
89
90 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0003, TestSize.Level1)
91 {
92 std::string printerId = "test.printer.id";
93 std::string defaultPpd = "test.ppd.name";
94 int defaultTimeout = 0;
95 MockVendorManager mock;
96 VendorPpdDriver vendorDriver;
97 EXPECT_TRUE(vendorDriver.Init(&mock));
98 EXPECT_CALL(mock, QueryDiscoveredPrinterInfoById(_, _)).WillRepeatedly(Return(std::make_shared<PrinterInfo>()));
99 EXPECT_CALL(mock, AddPrinterToCupsWithPpd(_, _, _)).WillOnce(Return(EXTENSION_ERROR_INVALID_PRINTER))
100 .WillRepeatedly(Return(EXTENSION_ERROR_NONE));
101 EXPECT_CALL(mock, QueryPrinterInfoByPrinterId(_, _, _)).WillRepeatedly(Return(E_PRINT_NONE));
102 EXPECT_CALL(mock, UpdatePrinterToDiscovery(_, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
103 vendorDriver.privatePrinterPpdMap.insert(std::make_pair(printerId, std::vector(1, defaultPpd)));
104 EXPECT_FALSE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
105 EXPECT_TRUE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
106 }
107
108 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0004, TestSize.Level1)
109 {
110 std::string printerId = "test.printer.id";
111 std::string defaultPpd = "test.ppd.name";
112 int defaultTimeout = 0;
113 MockVendorManager mock;
114 VendorPpdDriver vendorDriver;
115 EXPECT_TRUE(vendorDriver.Init(&mock));
116 EXPECT_CALL(mock, QueryDiscoveredPrinterInfoById(_, _)).WillRepeatedly(Return(std::make_shared<PrinterInfo>()));
117 EXPECT_CALL(mock, AddPrinterToCupsWithPpd(_, _, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
118 EXPECT_CALL(mock, QueryPrinterInfoByPrinterId(_, _, _)).WillOnce(Return(E_PRINT_UNKNOWN))
119 .WillRepeatedly(Return(E_PRINT_NONE));
120 EXPECT_CALL(mock, UpdatePrinterToDiscovery(_, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
121 vendorDriver.privatePrinterPpdMap.insert(std::make_pair(printerId, std::vector(1, defaultPpd)));
122 EXPECT_FALSE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
123 EXPECT_TRUE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
124 }
125
126 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0005, TestSize.Level1)
127 {
128 std::string printerId = "test.printer.id";
129 std::string defaultPpd = "test.ppd.name";
130 int defaultTimeout = 0;
131 MockVendorManager mock;
132 VendorPpdDriver vendorDriver;
133 EXPECT_TRUE(vendorDriver.Init(&mock));
134 EXPECT_CALL(mock, QueryDiscoveredPrinterInfoById(_, _)).WillRepeatedly(Return(std::make_shared<PrinterInfo>()));
135 EXPECT_CALL(mock, AddPrinterToCupsWithPpd(_, _, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
136 EXPECT_CALL(mock, QueryPrinterInfoByPrinterId(_, _, _)).WillRepeatedly(Return(E_PRINT_NONE));
137 EXPECT_CALL(mock, UpdatePrinterToDiscovery(_, _)).WillOnce(Return(EXTENSION_ERROR_INVALID_PRINTER))
138 .WillRepeatedly(Return(EXTENSION_ERROR_NONE));
139 vendorDriver.privatePrinterPpdMap.insert(std::make_pair(printerId, std::vector(1, defaultPpd)));
140 EXPECT_FALSE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
141 EXPECT_TRUE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
142 }
143
144 HWTEST_F(VendorPpdDriverTest, VendorPpdDriverTest_0006, TestSize.Level2)
145 {
146 std::string printerId = "test.printer.id";
147 std::string defaultPpd = "test.ppd.name";
148 std::vector<std::string> propertyKeys(1, "test.property.key");
149 int defaultTimeout = 0;
150 MockVendorManager mock;
151 VendorPpdDriver vendorDriver;
152 EXPECT_TRUE(vendorDriver.Init(&mock));
153 EXPECT_CALL(mock, QueryPPDInformation(_, _)).WillRepeatedly(
154 DoAll(SetArgReferee<1>(std::vector<std::string>(1, defaultPpd)), Return(true)));
155 EXPECT_CALL(mock, QueryDiscoveredPrinterInfoById(_, _)).WillRepeatedly(Return(std::make_shared<PrinterInfo>()));
156 EXPECT_CALL(mock, AddPrinterToCupsWithPpd(_, _, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
157 EXPECT_CALL(mock, QueryPrinterInfoByPrinterId(_, _, _)).WillRepeatedly(Return(E_PRINT_NONE));
158 EXPECT_CALL(mock, UpdatePrinterToDiscovery(_, _)).WillRepeatedly(Return(EXTENSION_ERROR_NONE));
159 EXPECT_FALSE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
160
161 EXPECT_TRUE(vendorDriver.OnQueryProperties(printerId, propertyKeys));
162 EXPECT_TRUE(vendorDriver.OnQueryCapability(printerId, defaultTimeout));
163 }
164
165 } // namespace Print
166 } // namespace OHOS
167