1 /*
2 * Copyright (c) 2022 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 "base_shape_pattern_test_ng.h"
17
18 #include "gtest/gtest.h"
19
20 #include "base/geometry/dimension.h"
21 #include "base/geometry/ng/size_t.h"
22 #include "base/memory/referenced.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/view_stack_processor.h"
25 #include "core/components_ng/layout/layout_property.h"
26 #include "core/components_ng/pattern/shape/shape_abstract_model_ng.h"
27 #include "core/components_ng/pattern/shape/shape_paint_property.h"
28 #include "core/components_ng/pattern/shape/shape_pattern.h"
29 #include "core/components_ng/property/layout_constraint.h"
30
31 namespace OHOS::Ace::NG {
32
CheckSize(bool hasValue)33 void BaseShapePatternTestNg::CheckSize(bool hasValue)
34 {
35 auto frameNode = CreadFrameNode();
36 EXPECT_EQ(frameNode == nullptr, false);
37 if (hasValue) {
38 auto shapeAbstactModel = ShapeAbstractModelNG();
39 SetSize(shapeAbstactModel);
40 }
41 ViewStackProcessor::GetInstance()->Pop();
42 auto layoutProperty = frameNode->GetLayoutProperty();
43 if (hasValue) {
44 auto mesureLayout = layoutProperty->GetCalcLayoutConstraint()->selfIdealSize;
45 EXPECT_EQ(mesureLayout.has_value(), true);
46 EXPECT_EQ(mesureLayout->Width().has_value(), true);
47 EXPECT_EQ(mesureLayout->Height().has_value(), true);
48 } else {
49 EXPECT_EQ(layoutProperty->GetCalcLayoutConstraint() == nullptr, true);
50 }
51 }
52
CheckFill(bool hasValue)53 void BaseShapePatternTestNg::CheckFill(bool hasValue)
54 {
55 auto frameNode = CreadFrameNode();
56 EXPECT_EQ(frameNode == nullptr, false);
57 auto shapeAbstactModel = ShapeAbstractModelNG();
58 SetSize(shapeAbstactModel);
59 if (hasValue) {
60 shapeAbstactModel.SetFill(Color::RED);
61 }
62 ViewStackProcessor::GetInstance()->Pop();
63 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
64 EXPECT_EQ(shapeProperty == nullptr, false);
65 if (hasValue) {
66 EXPECT_EQ(shapeProperty->HasFill(), true);
67 EXPECT_EQ(shapeProperty->GetFillValue(), Color::RED);
68 } else {
69 EXPECT_EQ(shapeProperty->HasFill(), false);
70 }
71 Draw(frameNode);
72 }
73
CheckFillOpacity(bool hasValue)74 void BaseShapePatternTestNg::CheckFillOpacity(bool hasValue)
75 {
76 auto frameNode = CreadFrameNode();
77 EXPECT_EQ(frameNode == nullptr, false);
78 auto shapeAbstactModel = ShapeAbstractModelNG();
79 SetSize(shapeAbstactModel);
80 if (hasValue) {
81 shapeAbstactModel.SetFillOpacity(OPACITY);
82 }
83 ViewStackProcessor::GetInstance()->Pop();
84 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
85 EXPECT_EQ(shapeProperty == nullptr, false);
86 if (hasValue) {
87 EXPECT_EQ(shapeProperty->HasFillOpacity(), true);
88 EXPECT_FLOAT_EQ(shapeProperty->GetFillOpacityValue(), OPACITY);
89 } else {
90 EXPECT_EQ(shapeProperty->HasFillOpacity(), false);
91 }
92 Draw(frameNode);
93 }
94
CheckStroke(bool hasValue)95 void BaseShapePatternTestNg::CheckStroke(bool hasValue)
96 {
97 auto frameNode = CreadFrameNode();
98 EXPECT_EQ(frameNode == nullptr, false);
99 auto shapeAbstactModel = ShapeAbstractModelNG();
100 SetSize(shapeAbstactModel);
101 if (hasValue) {
102 shapeAbstactModel.SetStroke(Color::RED);
103 }
104 ViewStackProcessor::GetInstance()->Pop();
105 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
106 EXPECT_EQ(shapeProperty == nullptr, false);
107 if (hasValue) {
108 EXPECT_EQ(shapeProperty->HasStroke(), true);
109 EXPECT_EQ(shapeProperty->GetStrokeValue(), Color::RED);
110 } else {
111 EXPECT_EQ(shapeProperty->HasStroke(), false);
112 }
113 Draw(frameNode);
114 }
115
CheckStrokeWidth(bool hasValue)116 void BaseShapePatternTestNg::CheckStrokeWidth(bool hasValue)
117 {
118 auto frameNode = CreadFrameNode();
119 EXPECT_EQ(frameNode == nullptr, false);
120 auto shapeAbstactModel = ShapeAbstractModelNG();
121 SetSize(shapeAbstactModel);
122 if (hasValue) {
123 shapeAbstactModel.SetStrokeWidth(Dimension(STROKE_WIDTH));
124 }
125 ViewStackProcessor::GetInstance()->Pop();
126 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
127 EXPECT_EQ(shapeProperty == nullptr, false);
128 if (hasValue) {
129 EXPECT_EQ(shapeProperty->HasStrokeWidth(), true);
130 EXPECT_FLOAT_EQ(shapeProperty->GetStrokeWidthValue().ConvertToPx(), STROKE_WIDTH);
131 } else {
132 EXPECT_EQ(shapeProperty->HasStrokeWidth(), false);
133 }
134 Draw(frameNode);
135 }
136
CheckStrokeOpacity(bool hasValue)137 void BaseShapePatternTestNg::CheckStrokeOpacity(bool hasValue)
138 {
139 auto frameNode = CreadFrameNode();
140 EXPECT_EQ(frameNode == nullptr, false);
141 auto shapeAbstactModel = ShapeAbstractModelNG();
142 SetSize(shapeAbstactModel);
143 if (hasValue) {
144 shapeAbstactModel.SetStrokeOpacity(OPACITY);
145 }
146 ViewStackProcessor::GetInstance()->Pop();
147 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
148 EXPECT_EQ(shapeProperty == nullptr, false);
149 if (hasValue) {
150 EXPECT_EQ(shapeProperty->HasStrokeOpacity(), true);
151 EXPECT_FLOAT_EQ(shapeProperty->GetStrokeOpacityValue(), OPACITY);
152 } else {
153 EXPECT_EQ(shapeProperty->HasStrokeOpacity(), false);
154 }
155 Draw(frameNode);
156 }
157
CheckStrokeDashArray(bool hasValue)158 void BaseShapePatternTestNg::CheckStrokeDashArray(bool hasValue)
159 {
160 auto frameNode = CreadFrameNode();
161 EXPECT_EQ(frameNode == nullptr, false);
162 auto shapeAbstactModel = ShapeAbstractModelNG();
163 SetSize(shapeAbstactModel);
164 if (hasValue) {
165 auto len = static_cast<int32_t>(STROKE_DASH_ARRAY.size());
166 std::vector<Dimension> strokeDashArray(len);
167 for (int32_t i = 0; i < len; i++) {
168 strokeDashArray[i] = Dimension(STROKE_DASH_ARRAY.at(i));
169 }
170 shapeAbstactModel.SetStrokeDashArray(strokeDashArray);
171 }
172 ViewStackProcessor::GetInstance()->Pop();
173 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
174 EXPECT_EQ(shapeProperty == nullptr, false);
175 if (hasValue) {
176 EXPECT_EQ(shapeProperty->HasStrokeDashArray(), true);
177 EXPECT_EQ(shapeProperty->GetStrokeDashArrayValue().size(), STROKE_DASH_ARRAY.size());
178 const std::vector<Dimension> propStrokeDashArray = shapeProperty->GetStrokeDashArrayValue();
179 auto propLen = static_cast<int32_t>(propStrokeDashArray.size());
180 for (int32_t i = 0; i < propLen; i++) {
181 EXPECT_FLOAT_EQ(propStrokeDashArray[i].ConvertToPx(), STROKE_DASH_ARRAY.at(i));
182 }
183 } else {
184 EXPECT_EQ(shapeProperty->HasStrokeDashArray(), false);
185 }
186 Draw(frameNode);
187 }
188
CheckStrokeDashOffset(bool hasValue)189 void BaseShapePatternTestNg::CheckStrokeDashOffset(bool hasValue)
190 {
191 auto frameNode = CreadFrameNode();
192 EXPECT_EQ(frameNode == nullptr, false);
193 auto shapeAbstactModel = ShapeAbstractModelNG();
194 SetSize(shapeAbstactModel);
195 if (hasValue) {
196 shapeAbstactModel.SetStrokeDashOffset(Dimension(DASHOFFSET));
197 }
198 ViewStackProcessor::GetInstance()->Pop();
199 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
200 EXPECT_EQ(shapeProperty == nullptr, false);
201 if (hasValue) {
202 EXPECT_EQ(shapeProperty->HasStrokeDashOffset(), true);
203 EXPECT_FLOAT_EQ(shapeProperty->GetStrokeDashOffset()->ConvertToPx(), DASHOFFSET);
204 } else {
205 EXPECT_EQ(shapeProperty->HasStrokeDashOffset(), false);
206 }
207 Draw(frameNode);
208 }
209
CheckStrokeLineCap(bool hasValue)210 void BaseShapePatternTestNg::CheckStrokeLineCap(bool hasValue)
211 {
212 auto frameNode = CreadFrameNode();
213 EXPECT_EQ(frameNode == nullptr, false);
214 auto shapeAbstactModel = ShapeAbstractModelNG();
215 SetSize(shapeAbstactModel);
216 if (hasValue) {
217 shapeAbstactModel.SetStrokeLineCap(LINE_CAP);
218 }
219 ViewStackProcessor::GetInstance()->Pop();
220 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
221 EXPECT_EQ(shapeProperty == nullptr, false);
222 if (hasValue) {
223 EXPECT_EQ(shapeProperty->HasStrokeLineCap(), true);
224 EXPECT_EQ(shapeProperty->GetStrokeLineCap(), LINE_CAP);
225 } else {
226 EXPECT_EQ(shapeProperty->HasStrokeLineCap(), false);
227 }
228 Draw(frameNode);
229 }
230
CheckStrokeLineJoin(bool hasValue)231 void BaseShapePatternTestNg::CheckStrokeLineJoin(bool hasValue)
232 {
233 auto frameNode = CreadFrameNode();
234 EXPECT_EQ(frameNode == nullptr, false);
235 auto shapeAbstactModel = ShapeAbstractModelNG();
236 SetSize(shapeAbstactModel);
237 if (hasValue) {
238 shapeAbstactModel.SetStrokeLineJoin(LINE_JOIN);
239 }
240 ViewStackProcessor::GetInstance()->Pop();
241 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
242 EXPECT_EQ(shapeProperty == nullptr, false);
243 if (hasValue) {
244 EXPECT_EQ(shapeProperty->HasStrokeLineJoin(), true);
245 EXPECT_EQ(shapeProperty->GetStrokeLineJoinValue(), LINE_JOIN);
246 } else {
247 EXPECT_EQ(shapeProperty->HasStrokeLineJoin(), false);
248 }
249 Draw(frameNode);
250 }
251
CheckStrokeMiterLimit(bool hasValue)252 void BaseShapePatternTestNg::CheckStrokeMiterLimit(bool hasValue)
253 {
254 auto frameNode = CreadFrameNode();
255 EXPECT_EQ(frameNode == nullptr, false);
256 auto shapeAbstactModel = ShapeAbstractModelNG();
257 SetSize(shapeAbstactModel);
258 if (hasValue) {
259 shapeAbstactModel.SetStrokeMiterLimit(STROKE_LIMIT);
260 }
261 ViewStackProcessor::GetInstance()->Pop();
262 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
263 EXPECT_EQ(shapeProperty == nullptr, false);
264 if (hasValue) {
265 EXPECT_EQ(shapeProperty->HasStrokeMiterLimit(), true);
266 EXPECT_FLOAT_EQ(static_cast<float>(shapeProperty->GetStrokeMiterLimitValue()), STROKE_LIMIT);
267 } else {
268 EXPECT_EQ(shapeProperty->HasStrokeMiterLimit(), false);
269 }
270 Draw(frameNode);
271 }
272
CheckAntiAlias(bool hasValue)273 void BaseShapePatternTestNg::CheckAntiAlias(bool hasValue)
274 {
275 auto frameNode = CreadFrameNode();
276 EXPECT_EQ(frameNode == nullptr, false);
277 auto shapeAbstactModel = ShapeAbstractModelNG();
278 SetSize(shapeAbstactModel);
279 if (hasValue) {
280 shapeAbstactModel.SetAntiAlias(ANTIALIAS);
281 }
282 ViewStackProcessor::GetInstance()->Pop();
283 auto shapeProperty = frameNode->GetPaintProperty<ShapePaintProperty>();
284 EXPECT_EQ(shapeProperty == nullptr, false);
285 if (hasValue) {
286 EXPECT_EQ(shapeProperty->HasAntiAlias(), true);
287 EXPECT_EQ(shapeProperty->GetAntiAliasValue(), ANTIALIAS);
288 } else {
289 EXPECT_EQ(shapeProperty->HasAntiAlias(), false);
290 }
291 Draw(frameNode);
292 }
293
SetSize(ShapeAbstractModelNG & shapeAbstractModelNG) const294 void BaseShapePatternTestNg::SetSize(ShapeAbstractModelNG& shapeAbstractModelNG) const
295 {
296 auto width = Dimension(WIDTH);
297 auto height = Dimension(HEIGHT);
298 shapeAbstractModelNG.SetWidth(width);
299 shapeAbstractModelNG.SetHeight(height);
300 }
301
302 } // namespace OHOS::Ace::NG