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 <gmock/gmock.h>
18
19 #include "shaper.h"
20 #include "typography_impl.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace TextEngine {
28 struct MockVars {
29 std::shared_ptr<TypographyStyle> typographyStyle_;
30 std::vector<VariantSpan> variantSpan_;
31 std::shared_ptr<FontProviders> providers_ = FontProviders::Create();
32 std::vector<LineMetrics> lineMetrics_;
33 std::shared_ptr<Boundary> boundary_;
34 } g_tiMockvars;
35
InitTiMockVars(struct MockVars && vars)36 void InitTiMockVars(struct MockVars &&vars)
37 {
38 g_tiMockvars = std::move(vars);
39 }
40
DoShape(std::vector<VariantSpan> spans,const TypographyStyle & ys,const std::shared_ptr<FontProviders> & fontProviders,const double widthLimit)41 std::vector<LineMetrics> Shaper::DoShape(std::vector<VariantSpan> spans, const TypographyStyle &ys,
42 const std::shared_ptr<FontProviders> &fontProviders, const double widthLimit)
43 {
44 return g_tiMockvars.lineMetrics_;
45 }
46
PaintShadow(TexgineCanvas & canvas,double offsetx,double offsety)47 void VariantSpan::PaintShadow(TexgineCanvas &canvas, double offsetx, double offsety) noexcept(false)
48 {
49 }
50
Paint(TexgineCanvas & canvas,double offsetx,double offsety)51 void VariantSpan::Paint(TexgineCanvas &canvas, double offsetx, double offsety) noexcept(false)
52 {
53 }
54
55 class TypographyImplTest : public testing::Test {
56 };
57
58 /**
59 * @tc.name:Layout
60 * @tc.desc: Verify the Layout
61 * @tc.type:FUNC
62 */
63 HWTEST_F(TypographyImplTest, Layout, TestSize.Level1)
64 {
65 InitTiMockVars({});
66 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
67 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
68 EXPECT_NO_THROW({
69 InitTiMockVars({});
70 ti->Layout(0.0);
71
72 LineMetrics lm;
73 std::vector<LineMetrics> lineMetrics;
74 lineMetrics.emplace_back(lm);
75 InitTiMockVars({.lineMetrics_ = lineMetrics});
76 ti->Layout(0.0);
77 });
78 }
79
80 /**
81 * @tc.name:Paint
82 * @tc.desc: Verify the Paint
83 * @tc.type:FUNC
84 */
85 HWTEST_F(TypographyImplTest, Paint, TestSize.Level1)
86 {
87 InitTiMockVars({});
88 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
89 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
90 EXPECT_NO_THROW({
91 std::shared_ptr<VariantSpan> vs = std::make_shared<VariantSpan>(TextSpan::MakeEmpty());
92 std::vector<VariantSpan> variantSpan;
93 variantSpan.emplace_back(*vs);
94 LineMetrics lm = {.lineSpans = variantSpan};
95 std::vector<LineMetrics> lineMetrics;
96 lineMetrics.emplace_back(lm);
97 InitTiMockVars({.lineMetrics_ = lineMetrics});
98 std::shared_ptr<TexgineCanvas> tc = std::make_shared<TexgineCanvas>();
99 ti->Layout(0.0);
100 ti->Paint(*tc, 0.0, 0.0);
101 });
102 }
103
104 /**
105 * @tc.name:GetTextRectsByBoundary
106 * @tc.desc: Verify the GetTextRectsByBoundary
107 * @tc.type:FUNC
108 */
109 HWTEST_F(TypographyImplTest, GetTextRectsByBoundary, TestSize.Level1)
110 {
111 InitTiMockVars({});
112 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
113 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
114 EXPECT_NO_THROW({
115 std::shared_ptr<VariantSpan> vs = std::make_shared<VariantSpan>(TextSpan::MakeEmpty());
116 std::vector<VariantSpan> variantSpan;
117 variantSpan.emplace_back(*vs);
118 LineMetrics lm = {.lineSpans = variantSpan};
119 std::vector<LineMetrics> lineMetrics;
120 lineMetrics.emplace_back(lm);
121 std::shared_ptr<Boundary> boundary1 = std::make_shared<Boundary>(1, 0);
122 std::shared_ptr<Boundary> boundary2 = std::make_shared<Boundary>(-1, 0);
123 std::shared_ptr<Boundary> boundary3 = std::make_shared<Boundary>(-2, -1);
124 std::shared_ptr<Boundary> boundary4 = std::make_shared<Boundary>(0, 1);
125 InitTiMockVars({.lineMetrics_ = lineMetrics, .boundary_ = boundary1});
126 ti->Layout(0.0);
127 ti->GetTextRectsByBoundary(*g_tiMockvars.boundary_, TextRectHeightStyle::TIGHT,
128 TextRectWidthStyle::MAX_WIDTH);
129 InitTiMockVars({.lineMetrics_ = lineMetrics, .boundary_ = boundary2});
130 ti->Layout(0.0);
131 ti->GetTextRectsByBoundary(*g_tiMockvars.boundary_, TextRectHeightStyle::TIGHT,
132 TextRectWidthStyle::MAX_WIDTH);
133 InitTiMockVars({.lineMetrics_ = lineMetrics, .boundary_ = boundary3});
134 ti->Layout(0.0);
135 ti->GetTextRectsByBoundary(*g_tiMockvars.boundary_, TextRectHeightStyle::TIGHT,
136 TextRectWidthStyle::MAX_WIDTH);
137 InitTiMockVars({.lineMetrics_ = lineMetrics, .boundary_ = boundary4});
138 ti->Layout(0.0);
139 ti->GetTextRectsByBoundary(*g_tiMockvars.boundary_, TextRectHeightStyle::TIGHT,
140 TextRectWidthStyle::MAX_WIDTH);
141 });
142 }
143
144 /**
145 * @tc.name:GetGlyphIndexByCoordinate
146 * @tc.desc: Verify the GetGlyphIndexByCoordinate
147 * @tc.type:FUNC
148 */
149 HWTEST_F(TypographyImplTest, GetGlyphIndexByCoordinate, TestSize.Level1)
150 {
151 InitTiMockVars({});
152 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
153 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
154 EXPECT_NO_THROW({
155 ti->Layout(0.0);
156 ti->GetGlyphIndexByCoordinate(0.0, 0.0);
157 ti->GetGlyphIndexByCoordinate(1.0, 0.0);
158 ti->GetGlyphIndexByCoordinate(0.0, 1.0);
159 ti->GetGlyphIndexByCoordinate(1.0, 1.0);
160 });
161 }
162
163 /**
164 * @tc.name:GetWordBoundaryByIndex
165 * @tc.desc: Verify the GetWordBoundaryByIndex
166 * @tc.type:FUNC
167 */
168 HWTEST_F(TypographyImplTest, GetWordBoundaryByIndex, TestSize.Level1)
169 {
170 InitTiMockVars({});
171 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
172 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
173 EXPECT_NO_THROW({
174 ti->GetWordBoundaryByIndex(0);
175 ti->GetWordBoundaryByIndex(1);
176 });
177 }
178
179 /**
180 * @tc.name:GetFunctions
181 * @tc.desc: Verify the GetFunction
182 * @tc.type:FUNC
183 */
184 HWTEST_F(TypographyImplTest, GetFunctions, TestSize.Level1)
185 {
186 InitTiMockVars({});
187 std::shared_ptr<TypographyImpl> ti = std::make_shared<TypographyImpl>(*g_tiMockvars.typographyStyle_,
188 g_tiMockvars.variantSpan_, g_tiMockvars.providers_);
189 EXPECT_NO_THROW({
190 ti->GetMaxWidth();
191 ti->GetHeight();
192 ti->GetActualWidth();
193 ti->GetMinIntrinsicWidth();
194 ti->GetMaxIntrinsicWidth();
195 ti->GetAlphabeticBaseline();
196 ti->GetIdeographicBaseline();
197 ti->GetLineCount();
198 });
199 }
200 } // namespace TextEngine
201 } // namespace Rosen
202 } // namespace OHOS
203