1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include <string>
18 #include <vector>
19 #include <nlohmann/json.hpp>
20 #include "print_service_converter.h"
21 #include "print_page_size.h"
22 #include "print_log.h"
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Print {
28
29 class PrintServiceConverterTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase(void)37 void PrintServiceConverterTest::SetUpTestCase(void) {}
38
TearDownTestCase(void)39 void PrintServiceConverterTest::TearDownTestCase(void) {}
40
SetUp(void)41 void PrintServiceConverterTest::SetUp(void)
42 {
43 static int32_t testNo = 0;
44 PRINT_HILOGI("PrintServiceConverterTest_%{public}d", ++testNo);
45 }
46
TearDown(void)47 void PrintServiceConverterTest::TearDown(void) {}
48
49 /**
50 * @tc.name: PrintServiceConverterTest_0001
51 * @tc.desc: reportPrintSuccess
52 * @tc.type: FUNC
53 * @tc.require:
54 */
55 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0001, TestSize.Level1)
56 {
57 int dpc = 120;
58 EXPECT_EQ(DpcToDpi(dpc), 300);
59 }
60
61 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0002, TestSize.Level1)
62 {
63 std::vector<std::string> list;
64 list.push_back("1111");
65 list.push_back("2222");
66 std::string value = "1111";
67 AddToUniqueList(list, value);
68 EXPECT_EQ(list.size(), 2);
69 }
70
71 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0003, TestSize.Level1)
72 {
73 std::vector<std::string> list;
74 list.push_back("1111");
75 list.push_back("2222");
76 std::string value = "3333";
77 AddToUniqueList(list, value);
78 EXPECT_EQ(list.size(), 3);
79 }
80
81 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0004, TestSize.Level1)
82 {
83 int code = 3;
84 EXPECT_EQ(GetQulityString(code), "3");
85 }
86
87 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0005, TestSize.Level1)
88 {
89 const char* src = nullptr;
90 ColorModeCode dst;
91 EXPECT_EQ(ConvertColorModeCode(src, dst), false);
92 }
93
94 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_006, TestSize.Level1)
95 {
96 PrintPageSize pageSize;
97 nlohmann::json jsonObject;
98 EXPECT_EQ(ConvertPageSizeToJson(pageSize, jsonObject), true);
99 }
100
101 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0007, TestSize.Level1)
102 {
103 const char* src = "color";
104 ColorModeCode dst;
105 bool re = ConvertColorModeCode(src, dst);
106 EXPECT_EQ(dst, 1);
107 EXPECT_EQ(re, true);
108 }
109
110 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0008, TestSize.Level1)
111 {
112 const char* src = "monochrome";
113 ColorModeCode dst;
114 bool re = ConvertColorModeCode(src, dst);
115 EXPECT_EQ(dst, 0);
116 EXPECT_EQ(re, true);
117 }
118
119 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0009, TestSize.Level1)
120 {
121 const char* src = "auto";
122 ColorModeCode dst;
123 bool re = ConvertColorModeCode(src, dst);
124 EXPECT_EQ(dst, 2);
125 EXPECT_EQ(re, true);
126 }
127
128 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0010, TestSize.Level1)
129 {
130 const char* src = "sss";
131 ColorModeCode dst;
132 EXPECT_EQ(ConvertColorModeCode(src, dst), false);
133 }
134
135 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0011, TestSize.Level1)
136 {
137 ColorModeCode code = ColorModeCode::COLOR_MODE_COLOR;
138 nlohmann::json jsonObject;
139 EXPECT_EQ(ConvertColorModeToJson(code, jsonObject), true);
140 }
141
142 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0012, TestSize.Level1)
143 {
144 DuplexModeCode code = DuplexModeCode::DUPLEX_MODE_ONE_SIDED;
145 nlohmann::json jsonObject;
146 EXPECT_EQ(ConvertDuplexModeToJson(code, jsonObject), true);
147 }
148
149 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0013, TestSize.Level1)
150 {
151 const char* src = nullptr;
152 std::string id = "";
153 EXPECT_EQ(ConvertPageSizeId(src, id), false);
154 }
155
156 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0014, TestSize.Level1)
157 {
158 const char* src = "iso_a4_210x297mm";
159 std::string id = "";
160 EXPECT_EQ(ConvertPageSizeId(src, id), true);
161 }
162
163 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0015, TestSize.Level1)
164 {
165 const char* src = nullptr;
166 PrintPageSize dst;
167 EXPECT_EQ(ConvertPrintPageSize(src, dst), false);
168 }
169
170 HWTEST_F(PrintServiceConverterTest, PrintServiceConverterTest_0016, TestSize.Level1)
171 {
172 const char* src = "iso_a4_210x297mm";
173 PrintPageSize dst;
174 EXPECT_EQ(ConvertPrintPageSize(src, dst), true);
175 }
176
177 }
178 }
179