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
18 #include "font_styles.h"
19 #include "param_test_macros.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace TextEngine {
27 class FontStylesTest : public testing::Test {
28 };
29
GetFontStyles(int weight,int style)30 FontStyles GetFontStyles(int weight, int style)
31 {
32 FontStyles fs(static_cast<FontWeight>(weight),
33 static_cast<FontStyle>(style));
34 return fs;
35 }
36
GetFontStyles(int weight,int width,int slant)37 FontStyles GetFontStyles(int weight, int width, int slant)
38 {
39 FontStyles fs(static_cast<FontStyles::Weight>(weight),
40 static_cast<FontStyles::Width>(width),
41 static_cast<FontStyles::Slant>(slant));
42 return fs;
43 }
44
45 /**
46 * @tc.name: FontStyles1
47 * @tc.desc: Verify the FontStyles
48 * @tc.type:FUNC
49 * @tc.require: issueI6V6I4
50 */
51 HWTEST_F(FontStylesTest, FontStyles1, TestSize.Level1)
52 {
53 constexpr auto excption = ExceptionType::INVALID_ARGUMENT;
54 constexpr auto weight = 0;
55 constexpr auto style = 0;
56
57 // FontWeight: -100 < -1 < 0 <= 3 <= 8 < 9 < 100
58 ASSERT_EXCEPTION(excption, GetFontStyles(-100, style));
59 ASSERT_EXCEPTION(excption, GetFontStyles(-1, style));
60 EXPECT_NO_THROW(GetFontStyles(0, style));
61 EXPECT_NO_THROW(GetFontStyles(3, style));
62 EXPECT_NO_THROW(GetFontStyles(8, style));
63 ASSERT_EXCEPTION(excption, GetFontStyles(9, style));
64 ASSERT_EXCEPTION(excption, GetFontStyles(100, style));
65
66 // FontStyle: -100 < -1 < 0 <= 1 < 2 < 100
67 ASSERT_EXCEPTION(excption, GetFontStyles(weight, -100));
68 ASSERT_EXCEPTION(excption, GetFontStyles(weight, -1));
69 EXPECT_NO_THROW(GetFontStyles(weight, 0));
70 EXPECT_NO_THROW(GetFontStyles(weight, 1));
71 ASSERT_EXCEPTION(excption, GetFontStyles(weight, 2));
72 ASSERT_EXCEPTION(excption, GetFontStyles(weight, 100));
73 }
74
75 /**
76 * @tc.name: FontStyles2
77 * @tc.desc: Verify the FontStyles
78 * @tc.type:FUNC
79 * @tc.require: issueI6V6I4
80 */
81 HWTEST_F(FontStylesTest, FontStyles2, TestSize.Level1)
82 {
83 constexpr auto excption = ExceptionType::INVALID_ARGUMENT;
84 constexpr auto weight = 0;
85 constexpr auto width = 0;
86 constexpr auto slant = 0;
87
88 // Weight: -100 < -1 < 0 <= 4 <= 10 < 11 < 100
89 ASSERT_EXCEPTION(excption, GetFontStyles(-100, width, slant));
90 ASSERT_EXCEPTION(excption, GetFontStyles(-1, width, slant));
91 EXPECT_NO_THROW(GetFontStyles(0, width, slant));
92 EXPECT_NO_THROW(GetFontStyles(4, width, slant));
93 EXPECT_NO_THROW(GetFontStyles(10, width, slant));
94 ASSERT_EXCEPTION(excption, GetFontStyles(11, width, slant));
95 ASSERT_EXCEPTION(excption, GetFontStyles(100, width, slant));
96
97 // Width: -100 < -1 < 0 <= 4 <= 8 < 9 < 100
98 ASSERT_EXCEPTION(excption, GetFontStyles(weight, -100, slant));
99 ASSERT_EXCEPTION(excption, GetFontStyles(weight, -1, slant));
100 EXPECT_NO_THROW(GetFontStyles(weight, 0, slant));
101 EXPECT_NO_THROW(GetFontStyles(weight, 4, slant));
102 EXPECT_NO_THROW(GetFontStyles(weight, 8, slant));
103 ASSERT_EXCEPTION(excption, GetFontStyles(weight, 9, slant));
104 ASSERT_EXCEPTION(excption, GetFontStyles(weight, 100, slant));
105
106 // Slant: -100 < -1 < 0 <= 1 <= 2 < 3 < 100
107 ASSERT_EXCEPTION(excption, GetFontStyles(weight, width, -100));
108 ASSERT_EXCEPTION(excption, GetFontStyles(weight, width, -1));
109 EXPECT_NO_THROW(GetFontStyles(weight, width, 0));
110 EXPECT_NO_THROW(GetFontStyles(weight, width, 1));
111 EXPECT_NO_THROW(GetFontStyles(weight, width, 2));
112 ASSERT_EXCEPTION(excption, GetFontStyles(weight, width, 3));
113 ASSERT_EXCEPTION(excption, GetFontStyles(weight, width, 100));
114 }
115
116 /**
117 * @tc.name: ToTexgineFontStyle
118 * @tc.desc: Verify the ToTexgineFontStyle
119 * @tc.type:FUNC
120 * @tc.require: issueI6V6I4
121 */
122 HWTEST_F(FontStylesTest, ToTexgineFontStyle, TestSize.Level1)
123 {
124 #ifndef USE_ROSEN_DRAWING
125 // (0, 0) is (FontWeight, FontStyle)
126 EXPECT_EQ(GetFontStyles(0, 0).ToTexgineFontStyle().GetFontStyle()->weight(), 100);
127 EXPECT_EQ(GetFontStyles(0, 0).ToTexgineFontStyle().GetFontStyle()->slant(), 0);
128
129 // (1, 0, 0) is (FontStyles::Weight, FontStyles::Width, FontStyles::Slant)
130 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->weight(), 100);
131 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->width(), 1);
132 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->slant(), 0);
133 #else
134 // (0, 0) is (FontWeight, FontStyle)
135 EXPECT_EQ(GetFontStyles(0, 0).ToTexgineFontStyle().GetFontStyle()->GetWeight(), 100);
136 EXPECT_EQ(GetFontStyles(0, 0).ToTexgineFontStyle().GetFontStyle()->GetSlant(), 0);
137
138 // (1, 0, 0) is (FontStyles::Weight, FontStyles::Width, FontStyles::Slant)
139 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->GetWeight(), 100);
140 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->GetWidth(), 1);
141 EXPECT_EQ(GetFontStyles(1, 0, 0).ToTexgineFontStyle().GetFontStyle()->GetSlant(), 0);
142 #endif
143 }
144
145 /**
146 * @tc.name: OperatorEqual
147 * @tc.desc: Verify the OperatorEqual
148 * @tc.type:FUNC
149 * @tc.require: issueI6V6I4
150 */
151 HWTEST_F(FontStylesTest, OperatorEqual, TestSize.Level1)
152 {
153 auto fontStyles = GetFontStyles(1, 1, 1);
154 EXPECT_NE(GetFontStyles(0, 1, 1), fontStyles);
155 EXPECT_NE(GetFontStyles(1, 0, 1), fontStyles);
156 EXPECT_NE(GetFontStyles(1, 1, 0), fontStyles);
157 EXPECT_EQ(GetFontStyles(1, 1, 1), fontStyles);
158 }
159
160 /**
161 * @tc.name: OperatorLessThan
162 * @tc.desc: Verify the OperatorLessThan
163 * @tc.type:FUNC
164 * @tc.require: issueI6V6I4
165 */
166 HWTEST_F(FontStylesTest, OperatorLessThan, TestSize.Level1)
167 {
168 auto fontStyles = GetFontStyles(1, 1, 1);
169 EXPECT_TRUE(GetFontStyles(0, 1, 1) < fontStyles);
170 EXPECT_TRUE(GetFontStyles(1, 0, 1) < fontStyles);
171 EXPECT_TRUE(GetFontStyles(1, 1, 0) < fontStyles);
172 EXPECT_FALSE(GetFontStyles(1, 1, 1) < fontStyles);
173 }
174 } // namespace TextEngine
175 } // namespace Rosen
176 } // namespace OHOS
177