• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "drawing_font_collection.h"
17 #include "drawing_text_typography.h"
18 #include "gtest/gtest.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace {
25 const double DEFAULT_FONT_SIZE = 50;
26 const char* DEFAULT_TEXT = "text";
27 } // namespace
28 
29 class NdkTypographyAlignmentTest : public testing::Test {
30 public:
31     void SetUp() override;
32     void TearDown() override;
33     void PrepareWorkForAlignmentTest();
34 protected:
35     OH_Drawing_TypographyCreate* fHandler{nullptr};
36     OH_Drawing_Typography* fTypography{nullptr};
37     OH_Drawing_TypographyStyle* fTypoStyle{nullptr};
38     OH_Drawing_TextStyle* fTxtStyle{nullptr};
39     int fLayoutWidth{50};
40 };
41 
SetUp()42 void NdkTypographyAlignmentTest::SetUp()
43 {
44     fTypoStyle = OH_Drawing_CreateTypographyStyle();
45     ASSERT_NE(fTypoStyle, nullptr);
46     OH_Drawing_SetTypographyTextFontSize(fTypoStyle, DEFAULT_FONT_SIZE);
47 }
48 
TearDown()49 void NdkTypographyAlignmentTest::TearDown()
50 {
51     if (fHandler != nullptr) {
52         OH_Drawing_DestroyTypographyHandler(fHandler);
53         fHandler = nullptr;
54     }
55     if (fTypography != nullptr) {
56         OH_Drawing_DestroyTypography(fTypography);
57         fTypography = nullptr;
58     }
59     if (fTypoStyle != nullptr) {
60         OH_Drawing_DestroyTypographyStyle(fTypoStyle);
61         fTypoStyle = nullptr;
62     }
63     if (fTxtStyle != nullptr) {
64         OH_Drawing_DestroyTextStyle(fTxtStyle);
65         fTxtStyle = nullptr;
66     }
67 }
68 
PrepareWorkForAlignmentTest()69 void NdkTypographyAlignmentTest::PrepareWorkForAlignmentTest()
70 {
71     fHandler = OH_Drawing_CreateTypographyHandler(fTypoStyle, OH_Drawing_CreateFontCollection());
72     ASSERT_NE(fHandler, nullptr);
73     fTxtStyle = OH_Drawing_CreateTextStyle();
74     ASSERT_NE(fTxtStyle, nullptr);
75     OH_Drawing_SetTextStyleFontSize(fTxtStyle, DEFAULT_FONT_SIZE);
76     OH_Drawing_TypographyHandlerPushTextStyle(fHandler, fTxtStyle);
77     OH_Drawing_TypographyHandlerAddText(fHandler, DEFAULT_TEXT);
78     fTypography = OH_Drawing_CreateTypography(fHandler);
79     ASSERT_NE(fTypography, nullptr);
80     OH_Drawing_TypographyLayout(fTypography, fLayoutWidth);
81 }
82 
83 /*
84  * @tc.name: TypographyAlignmentTest001
85  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
86  * @tc.type: FUNC
87  */
88 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest001, TestSize.Level0)
89 {
90     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_START);
91     OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_LTR);
92     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_LEFT);
93     OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_RTL);
94     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_RIGHT);
95     PrepareWorkForAlignmentTest();
96     OH_Drawing_TextBox* boxes =
97         OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
98     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 23);
99 }
100 
101 /*
102  * @tc.name: TypographyAlignmentTest002
103  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
104  * @tc.type: FUNC
105  */
106 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest002, TestSize.Level0)
107 {
108     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_END);
109     OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_LTR);
110     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_RIGHT);
111     OH_Drawing_SetTypographyTextDirection(fTypoStyle, TEXT_DIRECTION_RTL);
112     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_LEFT);
113     PrepareWorkForAlignmentTest();
114     OH_Drawing_TextBox* boxes =
115     OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
116     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 18);
117 }
118 
119 /*
120  * @tc.name: TypographyAlignmentTest003
121  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
122  * @tc.type: FUNC
123  */
124 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest003, TestSize.Level0)
125 {
126     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_CENTER);
127     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_CENTER);
128     PrepareWorkForAlignmentTest();
129     OH_Drawing_TextBox* boxes =
130     OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
131     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 20);
132 }
133 
134 /*
135  * @tc.name: TypographyAlignmentTest004
136  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
137  * @tc.type: FUNC
138  */
139 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest004, TestSize.Level0)
140 {
141     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_JUSTIFY);
142     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_JUSTIFY);
143     PrepareWorkForAlignmentTest();
144     OH_Drawing_TextBox* boxes =
145     OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
146     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 18);
147 }
148 
149 /*
150  * @tc.name: TypographyAlignmentTest005
151  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
152  * @tc.type: FUNC
153  */
154 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest005, TestSize.Level0)
155 {
156     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_LEFT);
157     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_LEFT);
158     PrepareWorkForAlignmentTest();
159     OH_Drawing_TextBox* boxes =
160     OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
161     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 18);
162 }
163 
164 /*
165  * @tc.name: TypographyAlignmentTest006
166  * @tc.desc: test for gets the typoStyle alignment mode and whether to enable text prompts
167  * @tc.type: FUNC
168  */
169 HWTEST_F(NdkTypographyAlignmentTest, TypographyAlignmentTest006, TestSize.Level0)
170 {
171     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_RIGHT);
172     EXPECT_EQ(OH_Drawing_TypographyStyleGetEffectiveAlignment(fTypoStyle), TEXT_ALIGN_RIGHT);
173     PrepareWorkForAlignmentTest();
174     OH_Drawing_TextBox* boxes =
175     OH_Drawing_TypographyGetRectsForRange(fTypography, 1, 2, RECT_HEIGHT_STYLE_TIGHT, RECT_WIDTH_STYLE_TIGHT);
176     EXPECT_EQ(::round(OH_Drawing_GetLeftFromTextBox(boxes, 0)), 23);
177 }
178 
179 /*
180  * @tc.name: TypographyTrailingSpaceOptimizedTest001
181  * @tc.desc: test for setting the different value of trailingSpaceOptimized and improve code coverage
182  * @tc.type: FUNC
183  */
184 HWTEST_F(NdkTypographyAlignmentTest, TypographyTrailingSpaceOptimizedTest001, TestSize.Level0)
185 {
186     OH_Drawing_SetTypographyTextAlign(fTypoStyle, TEXT_ALIGN_RIGHT);
187     OH_Drawing_SetTypographyTextTrailingSpaceOptimized(nullptr, true);
188     OH_Drawing_SetTypographyTextTrailingSpaceOptimized(nullptr, false);
189     OH_Drawing_SetTypographyTextTrailingSpaceOptimized(fTypoStyle, true);
190     OH_Drawing_SetTypographyTextTrailingSpaceOptimized(fTypoStyle, false);
191     PrepareWorkForAlignmentTest();
192 }
193 } // namespace OHOS