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 "gtest/gtest.h"
17 #include "rosen_text/text_style.h"
18 #include "convert.h"
19 #include "symbol_engine/text_animation_config.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS::Rosen {
25 class OHTextAnimationConfigTest : public testing::Test {
26 public:
27 static bool SetSymbolAnimation(const std::shared_ptr<TextEngine::SymbolAnimationConfig>& symbolAnimationConfig);
28 };
29
SetSymbolAnimation(const std::shared_ptr<TextEngine::SymbolAnimationConfig> & symbolAnimationConfig)30 bool OHTextAnimationConfigTest::SetSymbolAnimation(
31 const std::shared_ptr<TextEngine::SymbolAnimationConfig>& symbolAnimationConfig)
32 {
33 if (symbolAnimationConfig == nullptr) {
34 return false;
35 }
36 if (symbolAnimationConfig->parameters.size() < 2) {
37 return false;
38 }
39 return true;
40 }
41
42 /*
43 * @tc.name: DrawTextEffect001
44 * @tc.desc: test for DrawTextEffect witch canvas is nullptr and no parametor
45 * @tc.type: FUNC
46 */
47 HWTEST_F(OHTextAnimationConfigTest, DrawTextEffect001, TestSize.Level0)
48 {
49 // init data
50 std::shared_ptr<RSCanvas> rsCanvas = std::make_shared<RSCanvas>();
51 Drawing::Point offset = {100, 100}; // 100, 100 is the offset
52 RSPath path;
53 path.AddCircle(100, 100, 40); // 100 x, 100, 40 radius
54 path.AddCircle(100, 100, 30, Drawing::PathDirection::CCW_DIRECTION); // 100 x, 100, 30 radius
55 TextEngine::TextEffectElement effectElement;
56 effectElement.path = path;
57 effectElement.offset = offset;
58 SPText::TextAnimationConfig textEffect;
59 textEffect.SetUniqueId(19); // set UniqueId as 19
60 std::vector<TextEngine::TextEffectElement> effectElements;
61
62 // test DrawTestEffect: canvas: nullptr, effectElements: empty
63 bool flag1 = textEffect.DrawTextEffect(nullptr, effectElements);
64 EXPECT_FALSE(flag1);
65
66 // test DrawTestEffect: canvas: not nullptr, effectElements: empty
67 bool flag2 = textEffect.DrawTextEffect(rsCanvas.get(), effectElements);
68 EXPECT_FALSE(flag2);
69
70 // test DrawTestEffect: canvas: not nullptr, effectElements: size > 0
71 effectElements.push_back(effectElement);
72 bool flag3 = textEffect.DrawTextEffect(rsCanvas.get(), effectElements);
73 EXPECT_FALSE(flag3);
74
75 // test DrawTestEffect: set animationStart as true
76 textEffect.SetAnimationStart(true);
77 bool flag4 = textEffect.DrawTextEffect(rsCanvas.get(), effectElements);
78 EXPECT_FALSE(flag4);
79
80 // test DrawTestEffect: set SetAnimation
81 textEffect.SetAnimation(&SetSymbolAnimation);
82 bool flag5 = textEffect.DrawTextEffect(rsCanvas.get(), effectElements);
83 EXPECT_FALSE(flag5);
84 }
85
86 /*
87 * @tc.name: DrawTextEffect002
88 * @tc.desc: Test for DrawTextEffect by animation config
89 * @tc.type: FUNC
90 */
91 HWTEST_F(OHTextAnimationConfigTest, DrawTextEffect002, TestSize.Level0)
92 {
93 // init data
94 std::shared_ptr<RSCanvas> rsCanvas = std::make_shared<RSCanvas>();
95 Drawing::Point offset = {100, 100}; // 100, 100 is the offset
96 RSPath path;
97 path.AddCircle(100, 100, 40); // 100 x, 100, 40 radius
98 path.AddCircle(100, 100, 30, Drawing::PathDirection::CCW_DIRECTION); // 100 x, 100, 30 radius
99 TextEngine::TextEffectElement effectElement;
100 effectElement.path = path;
101 effectElement.offset = offset;
102 SPText::TextAnimationConfig textEffect;
103 std::vector<TextEngine::TextEffectElement> effectElements;
104 effectElements.push_back(effectElement);
105 std::vector<std::vector<Drawing::DrawingPiecewiseParameter>> parameters;
106 Drawing::DrawingPiecewiseParameter flipParameter1 = {
107 OHOS::Rosen::Drawing::DrawingCurveType::LINEAR, // animation curve type
108 {},
109 100, 0, // 100 is animation duration, 0 is animation delay
110 {{"alpha", {1.0, 0.0}}} // alpha is from 1 to 0
111 };
112 Drawing::DrawingPiecewiseParameter flipParameter2 = {
113 OHOS::Rosen::Drawing::DrawingCurveType::LINEAR, // animation curve type
114 {},
115 150, 0, // 100 is animation duration, 0 is animation delay
116 {{"alpha", {0, 1.0}}} // alpha is from 1 to 0
117 };
118 parameters.push_back({flipParameter1});
119 parameters.push_back({flipParameter2});
120
121 // set the parameters required for the text effect
122 textEffect.SetAnimationStart(true);
123 textEffect.SetAnimation(&SetSymbolAnimation);
124 textEffect.SetEffectStrategy(Drawing::DrawingEffectStrategy::TEXT_FLIP);
125 textEffect.SetEffectConfig(parameters);
126 bool flag = textEffect.DrawTextEffect(rsCanvas.get(), effectElements);
127 EXPECT_TRUE(flag);
128 }
129
130 /*
131 * @tc.name: SetAnimation
132 * @tc.desc: Test SetAnimation input nullptr
133 * @tc.type: FUNC
134 */
135 HWTEST_F(OHTextAnimationConfigTest, SetAnimation, TestSize.Level1)
136 {
137 // test input nullptr
138 SPText::TextAnimationConfig textEffect;
139 textEffect.SetAnimation(nullptr);
140 EXPECT_EQ(textEffect.animationFunc_, nullptr);
141 }
142
143 /*
144 * @tc.name: SetColor
145 * @tc.desc: Test whether the color was set successfully
146 * @tc.type: FUNC
147 */
148 HWTEST_F(OHTextAnimationConfigTest, SetColor, TestSize.Level0)
149 {
150 // init data
151 SPText::TextAnimationConfig textEffect;
152 RSColor color = RSColor(255, 0, 255, 255); // set color r 255, g 0, b 255, a 255
153
154 // test setColor
155 textEffect.SetColor(color);
156 EXPECT_TRUE(color == textEffect.color_);
157 }
158
159 /*
160 * @tc.name: ClearAllTextAnimation
161 * @tc.desc: Test ClearAllTextAnimation with invalid and valid animationFunc_
162 * @tc.type: FUNC
163 */
164 HWTEST_F(OHTextAnimationConfigTest, ClearAllTextAnimation, TestSize.Level1)
165 {
166 // test animationFunc_ is nullptr
167 SPText::TextAnimationConfig textEffect;
168 textEffect.ClearAllTextAnimation();
169 EXPECT_EQ(textEffect.animationFunc_, nullptr);
170
171 // test animationFunc_ not is nullptr
172 textEffect.SetAnimation(&SetSymbolAnimation);
173 textEffect.ClearAllTextAnimation();
174 EXPECT_NE(textEffect.animationFunc_, nullptr);
175 }
176
177 } // namespace OHOS::Rosen