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 <dynamic_font_style_set.h>
19
20 #include "param_test_macros.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<TexgineTypeface> typeface = std::make_shared<TexgineTypeface>();
30 std::shared_ptr<TexgineFontStyle> fontStyle = std::make_shared<TexgineFontStyle>();
31 std::shared_ptr<TextEngine::DynamicFontStyleSet> normalSet = nullptr;
32 } g_dfssMockVars;
33
34
Init(struct Mockvars vars)35 auto Init(struct Mockvars vars)
36 {
37 return [vars = std::move(vars)] {
38 g_dfssMockVars = std::move(vars);
39 auto typeface = std::make_unique<Typeface>(g_dfssMockVars.typeface);
40 g_dfssMockVars.normalSet = std::make_shared<DynamicFontStyleSet>(std::move(typeface));
41 };
42 }
43
44 #ifndef USE_ROSEN_DRAWING
GetFontStyle() const45 std::shared_ptr<TexgineFontStyle> TexgineTypeface::GetFontStyle() const
46 {
47 return g_dfssMockVars.fontStyle;
48 }
49 #endif
50
Typeface(std::shared_ptr<TexgineTypeface> tf)51 Typeface::Typeface(std::shared_ptr<TexgineTypeface> tf)
52 {
53 typeface_ = g_dfssMockVars.typeface;
54 }
55
56 #define PARAMCLASS DynamicFontStyleSet
57 class DynamicFontStyleSetTest : public testing::Test {
58 public:
59 };
60
61 #define PARAMFUNC Count
62 HWTEST_F(DynamicFontStyleSetTest, Count, TestSize.Level1)
63 {
64 DEFINE_TESTINFO0();
65
66 DynamicFontStyleSet nullset(nullptr);
67 RUN_TESTINFO0(nullset, {.checkFunc = CreateChecker(0)});
68
69 DynamicFontStyleSet normset(std::make_unique<Typeface>(g_dfssMockVars.typeface));
70 RUN_TESTINFO0(normset, {.checkFunc = CreateChecker(1)});
71 }
72 #undef PARAMFUNC
73
74 #define PARAMFUNC GetStyle
CreateStyleChecker(std::shared_ptr<TexgineFontStyle> s)75 auto CreateStyleChecker(std::shared_ptr<TexgineFontStyle> s)
76 {
77 return [s] (int, std::shared_ptr<TexgineFontStyle> style, std::shared_ptr<TexgineString>, DynamicFontStyleSet &) {
78 return *s->GetFontStyle() == *style->GetFontStyle();
79 };
80 }
81
82 /**
83 * @tc.name: GetStyle
84 * @tc.desc: Verify the GetStyle
85 * @tc.type:FUNC
86 * @tc.require: issueI6V6JU
87 */
88 HWTEST_F(DynamicFontStyleSetTest, GetStyle, TestSize.Level1)
89 {
90 DEFINE_VOID_TESTINFO3(int, std::shared_ptr<TexgineFontStyle>, std::shared_ptr<TexgineString>);
91
92 TexgineFontStyle::Slant slant {};
93 // (0, 0, slant): (wegiht, width, slant)
94 auto s1 = std::make_shared<TexgineFontStyle>(0, 0, slant);
95 auto s2 = std::make_shared<TexgineFontStyle>(100, 100, slant);
96 DynamicFontStyleSet nullset(nullptr);
97
98 #define CORE_TEST(obj, mock, a1, expect) \
99 { \
100 std::shared_ptr<TexgineFontStyle> style = s1; \
101 RUN_VOID_TESTINFO3(obj, { \
102 .init = Init({.fontStyle = mock}), \
103 .arg1 = a1, .arg2 = style, \
104 .checkFunc = CreateStyleChecker(expect)}); \
105 }
106
107 RUN_VOID_TESTINFO3(nullset, {.arg2 = nullptr, .exception = ExceptionType::INVALID_ARGUMENT});
108
109 // -1 is the parameter of GetStyle, index
110 CORE_TEST(nullset, s2, -1, s1);
111 CORE_TEST(nullset, s2, 0, s1);
112 CORE_TEST(nullset, s2, 1, s1);
113 CORE_TEST(*g_dfssMockVars.normalSet, s2, -1, s1);
114 CORE_TEST(*g_dfssMockVars.normalSet, s2, 1, s1);
115 #undef CORE_TEST
116
117 auto style = s1;
118 auto initfunc = Init({.typeface = nullptr, .fontStyle = s2});
119 auto checkfunc = CreateStyleChecker(s1);
120 RUN_VOID_TESTINFO3(*g_dfssMockVars.normalSet, {.init = initfunc, .arg2 = style, .checkFunc = checkfunc});
121 }
122 #undef PARAMFUNC
123
124 #define PARAMFUNC CreateTypeface
125 /**
126 * @tc.name: CreateTypeface
127 * @tc.desc: Verify the CreateTypeface
128 * @tc.type:FUNC
129 * @tc.require: issueI6V6JU
130 */
131 HWTEST_F(DynamicFontStyleSetTest, CreateTypeface, TestSize.Level1)
132 {
133 DEFINE_TESTINFO1(int);
134 Init({})();
135 DynamicFontStyleSet nullset(nullptr);
136 DynamicFontStyleSet normset(std::make_unique<Typeface>(g_dfssMockVars.typeface));
137 // arg1 is the parameter of CreateTypeface, index
138 RUN_TESTINFO1(nullset, {.arg1 = -1, .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
139 RUN_TESTINFO1(nullset, {.arg1 = 0, .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
140 RUN_TESTINFO1(nullset, {.arg1 = 1, .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
141 RUN_TESTINFO1(normset, {.arg1 = -1, .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
142 RUN_TESTINFO1(normset, {.arg1 = 0,
143 .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(g_dfssMockVars.typeface)});
144 RUN_TESTINFO1(normset, {.arg1 = 1, .checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
145 }
146 #undef PARAMFUNC
147
148 #define PARAMFUNC MatchStyle
149 /**
150 * @tc.name: MatchStyle
151 * @tc.desc: Verify the MatchStyle
152 * @tc.type:FUNC
153 * @tc.require: issueI6V6JU
154 */
155 HWTEST_F(DynamicFontStyleSetTest, MatchStyle, TestSize.Level1)
156 {
157 DEFINE_TESTINFO1(std::shared_ptr<TexgineFontStyle>);
158 Init({})();
159 DynamicFontStyleSet nullset(nullptr);
160 DynamicFontStyleSet normset(std::make_unique<Typeface>(g_dfssMockVars.typeface));
161 RUN_TESTINFO1(nullset, {.checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(nullptr)});
162 RUN_TESTINFO1(normset, {.checkFunc = CreateChecker<std::shared_ptr<TexgineTypeface>>(g_dfssMockVars.typeface)});
163 }
164 #undef PARAMFUNC
165 } // namespace TextEngine
166 } // namespace Rosen
167 } // namespace OHOS
168