• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, std::vector<float> &arg4, 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_TESTINFO4(std::vector<struct ScoredSpan>, double, TypographyStyle, std::vector<float>);
192     LineBreaker breaker;
193     for (auto i = 0; i < static_cast<int>(ss1.size()); i++) {
194         ss1[i].span = ts11_;
195         ss2[i].span = ts11_;
196         ss3[i].span = ts11_;
197     }
198 
199     // arg1~3 is the parameters of DoBreakLines
200     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss1, .arg2 = 100, .arg3 = gStyle_, .arg4 = {},
201         .checkFunc = DoBreakLinesChecker(prevs012) });
202     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss1, .arg2 = 1e9, .arg3 = gStyle_, .arg4 = {},
203         .checkFunc = DoBreakLinesChecker(prevs000) });
204     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss1, .arg2 = 1e9, .arg3 = hStyle_, .arg4 = {},
205         .checkFunc = DoBreakLinesChecker(prevs000) });
206     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss2, .arg2 = 100, .arg3 = gStyle_, .arg4 = {},
207         .checkFunc = DoBreakLinesChecker(prevs002) });
208     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss2, .arg2 = 100, .arg3 = hStyle_, .arg4 = {},
209         .checkFunc = DoBreakLinesChecker(prevs001) });
210     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss3, .arg2 = 100, .arg3 = gStyle_, .arg4 = {},
211         .checkFunc = DoBreakLinesChecker(prevs002) });
212     RUN_VOID_TESTINFO4(breaker, { .arg1 = ss3, .arg2 = 100, .arg3 = hStyle_, .arg4 = {},
213         .checkFunc = DoBreakLinesChecker(prevs002) });
214 }
215 #undef PARAMFUNC
216 
217 /**
218  * @tc.name: GenerateBreaks
219  * @tc.desc: Verify the GenerateBreaks
220  * @tc.type:FUNC
221  */
222 DEFINE_PARAM_TEST2(LineBreaker, GenerateBreaks, std::vector<VariantSpan>, std::vector<ScoredSpan>, {
223     { .arg1 = {ts10_}, .arg2 = GenScoredSpansByPrevs({0, 2}), .exception = ExceptionType::ERROR_STATUS },
224     { .arg1 = {ts10_}, .arg2 = GenScoredSpansByPrevs({0, 3, 2}), .exception = ExceptionType::ERROR_STATUS },
225     { .arg1 = {ts10_}, .arg2 = GenScoredSpansByPrevs({0, 2, 5}), .exception = ExceptionType::ERROR_STATUS },
226     { .arg1 = {ts10_}, .arg2 = GenScoredSpansByPrevs({0, 0, 0, 2, 2, 4, 4, 4, 7}),
227       .checkFunc = GetResultChecker(std::vector<int>{2, 4, 7, 9}) },
228 });
229 
230 /**
231  * @tc.name: GenerateLineMetrics
232  * @tc.desc: Verify the GenerateLineMetrics
233  * @tc.type:FUNC
234  */
235 #define PARAMFUNC GenerateLineMetrics
236 HWTEST_F(LineBreakerTest, GenerateLineMetrics, TestSize.Level1)
237 {
238     DEFINE_TESTINFO4(double, std::vector<VariantSpan>, std::vector<int32_t>, std::vector<float>);
239     LineBreaker breaker;
240     RUN_TESTINFO4(breaker, { .arg1 = 100.0, .arg2 = {3, VariantSpan{}}, .arg3 = {2, 10}, .arg4 = {},
241         .exception = ExceptionType::OUT_OF_RANGE });
242 }
243 #undef PARAMFUNC
244 } // namespace TextEngine
245 } // namespace Rosen
246 } // namespace OHOS
247