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 <utility>
17
18 #include <gtest/gtest.h>
19
20 #include "line_breaker.h"
21 #include "mock/mock_any_span.h"
22 #include "my_any_span.h"
23 #include "param_test_macros.h"
24 #include "text_converter.h"
25 #include "text_span.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace TextEngine {
GenScoredSpansByBreaks(const std::vector<std::pair<double,double>> & breaks)33 std::vector<ScoredSpan> GenScoredSpansByBreaks(const std::vector<std::pair<double, double>> &breaks)
34 {
35 std::vector<ScoredSpan> sss;
36 for (const auto &breakPair : breaks) {
37 sss.push_back({.preBreak = breakPair.first, .postBreak = breakPair.second});
38 }
39 return sss;
40 }
41
42 struct ScoredSpanCheck {
43 double preBreak = 0;
44 double postBreak = 0;
45 bool isAnySpan = false;
46 };
47
ScoredSpansChecker(const std::vector<struct ScoredSpanCheck> & cls)48 auto ScoredSpansChecker(const std::vector<struct ScoredSpanCheck> &cls)
49 {
50 return [cls](std::vector<struct ScoredSpan> sss) {
51 ASSERT_EQ(cls.size(), sss.size());
52 for (auto i = 0u; i < sss.size(); i++) {
53 ASSERT_EQ(sss[i].preBreak, cls[i].preBreak);
54 ASSERT_EQ(sss[i].postBreak, cls[i].postBreak);
55 if (cls[i].isAnySpan) {
56 ASSERT_NE(sss[i].span.TryToAnySpan(), nullptr);
57 } else {
58 ASSERT_NE(sss[i].span.TryToTextSpan(), nullptr);
59 }
60 }
61 };
62 }
63
DoBreakLinesChecker(const std::vector<int> & arg)64 auto DoBreakLinesChecker(const std::vector<int> &arg)
65 {
66 return [arg](std::vector<struct ScoredSpan> &arg1,
67 double &arg2, TypographyStyle &arg3, LineBreaker &object) {
68 if (arg.size() != arg1.size()) {
69 return false;
70 }
71 for (auto i = 0u; i < arg1.size(); i++) {
72 if (arg1[i].prev != arg[i]) {
73 return false;
74 }
75 }
76 return true;
77 };
78 }
79
80 // Setting the flag of break lines
GenScoredSpansByPrevs(const std::vector<int> & prevs)81 std::vector<ScoredSpan> GenScoredSpansByPrevs(const std::vector<int> &prevs)
82 {
83 std::vector<ScoredSpan> sss;
84 for (const auto &prev : prevs) {
85 sss.push_back({.prev = prev});
86 }
87 return sss;
88 }
89
LineMetricsSizesChecker(const std::vector<size_t> & sizes)90 auto LineMetricsSizesChecker(const std::vector<size_t> &sizes)
91 {
92 return [sizes](std::vector<LineMetrics> &&lineMetrics) {
93 if (lineMetrics.size() != sizes.size()) {
94 return false;
95 }
96 for (auto i = 0u; i < lineMetrics.size(); i++) {
97 if (lineMetrics[i].lineSpans.size() != sizes[i]) {
98 return false;
99 }
100 }
101 return true;
102 };
103 }
104
105 class ControllerForTest {
106 public:
107 struct TextSpanInfo {
108 double preBreak = 0;
109 double postBreak = 0;
110 CharGroups cgs_ = {};
111 };
112
GenerateTestSpan(const TextSpanInfo & info)113 static std::shared_ptr<TextSpan> GenerateTestSpan(const TextSpanInfo &info)
114 {
115 auto ts = std::make_shared<TextSpan>();
116 ts->preBreak_ = info.preBreak;
117 ts->postBreak_ = info.postBreak;
118 ts->cgs_ = info.cgs_;
119 return ts;
120 }
121 };
122
123 #define PARAMCLASS LineBreaker
124 class LineBreakerTest : public testing::Test {
125 public:
SetUpTestCase()126 static void SetUpTestCase()
127 {
128 CharGroups cgs1 = CharGroups::CreateEmpty();
129 // {0x013B, 13.664}: {glyph codepont, davanceX}
130 cgs1.PushBack({.chars = TextConverter::ToUTF16("m"), .glyphs = {{0x013B, 13.664}}});
131 cgs1.PushBack({.chars = TextConverter::ToUTF16("o"), .glyphs = {{0x0145, 9.456}}});
132 cgs1.PushBack({.chars = TextConverter::ToUTF16("s"), .glyphs = {{0x0166, 7.28}}});
133 cgs1.PushBack({.chars = TextConverter::ToUTF16("t"), .glyphs = {{0x016E, 5.88}}});
134 ts10_ = ControllerForTest::GenerateTestSpan({.preBreak = 0, .postBreak = 10, .cgs_ = cgs1});
135
136 CharGroups cgs2 = CharGroups::CreateEmpty();
137 cgs2.PushBack({.chars = TextConverter::ToUTF16("m"), .glyphs = {{0x013B, 13.664}}});
138 ts20_ = ControllerForTest::GenerateTestSpan({.preBreak = 20, .postBreak = 20, .cgs_ = cgs2});
139
140 ts11_ = ControllerForTest::GenerateTestSpan({.preBreak = 10, .postBreak = 10, .cgs_ = cgs1.GetSub(0, 1)});
141 ts12_ = ControllerForTest::GenerateTestSpan({.preBreak = 10, .postBreak = 20, .cgs_ = cgs1.GetSub(1, 2)});
142
143 gStyle_.breakStrategy = BreakStrategy::GREEDY;
144 hStyle_.breakStrategy = BreakStrategy::HIGH_QUALITY;
145 }
146
147 static inline std::shared_ptr<TextSpan> ts10_ = nullptr;
148 static inline std::shared_ptr<TextSpan> ts20_ = nullptr;
149 static inline std::shared_ptr<TextSpan> ts11_ = nullptr;
150 static inline std::shared_ptr<TextSpan> ts12_ = nullptr;
151 static inline TypographyStyle gStyle_;
152 static inline TypographyStyle hStyle_;
153 };
154
155 //(10, 0): (width, height)
156 auto as10 = std::make_shared<MyAnySpan>(10, 0);
157 auto as20 = std::make_shared<MyAnySpan>(20, 0);
158 std::shared_ptr<AnySpan> asNullptr = nullptr;
159 std::shared_ptr<TextSpan> tsNullptr = nullptr;
160 // {0, 10}: {preBreak, postBreak}
161 auto longChecker1 = ScoredSpansChecker({{0, 10, false}, {20, 20, true}, {40, 40, false}, {60, 60, true}});
162 auto longChecker2 = ScoredSpansChecker(
163 {{0, 10, false}, {20, 20, true}, {30, 30, false}, {30, 40, false}, {60, 60, true}});
164 DEFINE_PARAM_TEST1(LineBreaker, GenerateScoreSpans, std::vector<VariantSpan>, {
165 { .arg1 = {tsNullptr}, .exception = ExceptionType::INVALID_ARGUMENT },
166 { .arg1 = {asNullptr}, .exception = ExceptionType::INVALID_ARGUMENT },
167 { .arg1 = {ts10_}, .checkFunc = ScoredSpansChecker({{0, 10, false}}) },
168 { .arg1 = {as10}, .checkFunc = ScoredSpansChecker({{10, 10, true}}) },
169 { .arg1 = {ts10_, as10, ts20_, as20}, .checkFunc = longChecker1 },
170 { .arg1 = {ts10_, as10, ts11_, ts12_, as20}, .checkFunc = longChecker2 },
171 });
172
173 // {100, 100}: {preBreak, postBreak}
174 auto ss1 = GenScoredSpansByBreaks({{100, 100}, {200, 200}, {300, 300}});
175 auto ss2 = GenScoredSpansByBreaks({{90, 90}, {100, 100}, {180, 180}});
176 auto ss3 = GenScoredSpansByBreaks({{90, 90}, {100, 1100}, {1180, 1180}});
177
178 // These vectors is the flag of break lines
179 std::vector<int> prevs000 = {0, 0, 0};
180 std::vector<int> prevs001 = {0, 0, 1};
181 std::vector<int> prevs002 = {0, 0, 2};
182 std::vector<int> prevs012 = {0, 1, 2};
183
184 /**
185 * @tc.name: DoBreakLines
186 * @tc.desc: Verify the DoBreakLines
187 * @tc.type:FUNC
188 */
189 #define PARAMFUNC DoBreakLines
190 HWTEST_F(LineBreakerTest, DoBreakLines, TestSize.Level1) {
191 DEFINE_VOID_TESTINFO3(std::vector<struct ScoredSpan>, double, TypographyStyle);
192 LineBreaker breaker;
193 // arg1~3 is the parameters of DoBreakLines
194 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss1, .arg2 = 100, .arg3 = gStyle_,
195 .checkFunc = DoBreakLinesChecker(prevs012) });
196 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss1, .arg2 = 1e9, .arg3 = gStyle_,
197 .checkFunc = DoBreakLinesChecker(prevs000) });
198 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss1, .arg2 = 1e9, .arg3 = hStyle_,
199 .checkFunc = DoBreakLinesChecker(prevs000) });
200 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss2, .arg2 = 100, .arg3 = gStyle_,
201 .checkFunc = DoBreakLinesChecker(prevs002) });
202 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss2, .arg2 = 100, .arg3 = hStyle_,
203 .checkFunc = DoBreakLinesChecker(prevs001) });
204 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss3, .arg2 = 100, .arg3 = gStyle_,
205 .checkFunc = DoBreakLinesChecker(prevs002) });
206 RUN_VOID_TESTINFO3(breaker, { .arg1 = ss3, .arg2 = 100, .arg3 = hStyle_,
207 .checkFunc = DoBreakLinesChecker(prevs002) });
208 }
209 #undef PARAMFUNC
210
211 /**
212 * @tc.name: GenerateBreaks
213 * @tc.desc: Verify the GenerateBreaks
214 * @tc.type:FUNC
215 */
216 DEFINE_PARAM_TEST1(LineBreaker, GenerateBreaks, std::vector<ScoredSpan>, {
217 { .arg1 = GenScoredSpansByPrevs({0, 2}), .exception = ExceptionType::ERROR_STATUS },
218 { .arg1 = GenScoredSpansByPrevs({0, 3, 2}), .exception = ExceptionType::ERROR_STATUS },
219 { .arg1 = GenScoredSpansByPrevs({0, 2, 5}), .exception = ExceptionType::ERROR_STATUS },
220 { .arg1 = GenScoredSpansByPrevs({0, 0, 0, 2, 2, 4, 4, 4, 7}),
221 .checkFunc = GetResultChecker(std::vector<int>{2, 4, 7, 9}) },
222 });
223
224 /**
225 * @tc.name: GenerateLineMetrics
226 * @tc.desc: Verify the GenerateLineMetrics
227 * @tc.type:FUNC
228 */
229 #define PARAMFUNC GenerateLineMetrics
230 HWTEST_F(LineBreakerTest, GenerateLineMetrics, TestSize.Level1)
231 {
232 DEFINE_TESTINFO2(std::vector<VariantSpan>, std::vector<int32_t>);
233 LineBreaker breaker;
234 RUN_TESTINFO2(breaker, { .arg1 = {3, VariantSpan{}}, .arg2 = {2, 10},
235 .exception = ExceptionType::OUT_OF_RANGE });
236 RUN_TESTINFO2(breaker, { .arg1 = {3, VariantSpan{}}, .arg2 = {3, 1},
237 .exception = ExceptionType::ERROR_STATUS });
238 RUN_TESTINFO2(breaker, { .arg1 = {9, VariantSpan{}}, .arg2 = {2, 4, 7, 9},
239 .checkFunc = LineMetricsSizesChecker({2, 2, 3, 2}) });
240 }
241 #undef PARAMFUNC
242 } // namespace TextEngine
243 } // namespace Rosen
244 } // namespace OHOS
245