1 /*
2 * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 #define protected public
19 #define private public
20 #include "test/mock/core/pipeline/mock_pipeline_context.h"
21
22 #include "core/components/common/layout/grid_system_manager.h"
23 #include "core/components_ng/pattern/grid_container/grid_container_layout_property.h"
24 #include "core/components_ng/property/grid_property.h"
25
26 #undef private
27 #undef protected
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS::Ace::NG {
33 namespace {
34 constexpr int32_t SPANONE = 10;
35 constexpr int32_t SPANTWO = -10;
36 constexpr int32_t OFFSETONE = 5;
37 constexpr int32_t INDEX = 1;
38
39 const Dimension TESTWIDTH { 1.0, DimensionUnit::VP };
40 const Dimension TESTLEFT { 2.0, DimensionUnit::VP };
41 const Dimension TESTRIGHT { 3.0, DimensionUnit::VP };
42 const Dimension TEST_VALUE = Dimension(0.0);
43
44 const GridTypedProperty TEST_TYPED0 { GridSizeType::XS, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
45 const GridTypedProperty TEST_TYPED1 { GridSizeType::SM, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
46 const GridTypedProperty TEST_TYPED2 { GridSizeType::MD, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
47 const GridTypedProperty TEST_TYPED3 { GridSizeType::LG, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
48 const GridTypedProperty TEST_TYPED4 { GridSizeType::XL, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
49 const GridTypedProperty TEST_TYPED5 { GridSizeType::UNDEFINED, DEFAULT_GRID_SPAN, DEFAULT_GRID_OFFSET };
50
MakeProperty(RefPtr<GridProperty> gridProperty)51 void MakeProperty(RefPtr<GridProperty> gridProperty)
52 {
53 gridProperty->typedPropertySet_.push_back(TEST_TYPED0);
54 gridProperty->typedPropertySet_.push_back(TEST_TYPED1);
55 gridProperty->typedPropertySet_.push_back(TEST_TYPED2);
56 gridProperty->typedPropertySet_.push_back(TEST_TYPED3);
57 gridProperty->typedPropertySet_.push_back(TEST_TYPED4);
58 gridProperty->typedPropertySet_.push_back(TEST_TYPED5);
59 };
60 } // namespace
61
62 class GridPropertyTestNg : public testing::Test {
63 public:
64 static void SetUpTestSuite();
65 static void TearDownTestSuite();
66 };
67
SetUpTestSuite()68 void GridPropertyTestNg::SetUpTestSuite()
69 {
70 MockPipelineContext::SetUp();
71 }
72
TearDownTestSuite()73 void GridPropertyTestNg::TearDownTestSuite()
74 {
75 MockPipelineContext::TearDown();
76 }
77
78 /**
79 * @tc.name: UpdateSpan_Test
80 * @tc.desc: Test cast to GridProperty
81 * @tc.type: FUNC
82 */
83 HWTEST_F(GridPropertyTestNg, UpdateSpan_Test, TestSize.Level1)
84 {
85 /**
86 * @tc.steps1: create a object gridProperty.
87 */
88 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
89 auto type = static_cast<GridSizeType>(INDEX);
90
91 /**
92 * @tc.steps2: call UpdateSpan.push span is < 0.
93 * @tc.expected: Return result is false.
94 */
95 bool result = gridProperty->UpdateSpan(SPANTWO, type);
96 EXPECT_FALSE(result);
97
98 /**
99 * @tc.steps3: call UpdateSpan.push span >= 0, container_ is null.
100 * @tc.expected: Return result is true.
101 */
102 bool result_Span = gridProperty->UpdateSpan(SPANONE, type);
103 EXPECT_TRUE(result_Span);
104
105 /**
106 * @tc.steps4: call UpdateOffset.push Offset >= 0, container_ is null.
107 * @tc.expected: Return result is true.
108 */
109 bool result_Off = gridProperty->UpdateOffset(OFFSETONE, type);
110 EXPECT_TRUE(result_Off);
111 }
112
113 /**
114 * @tc.name: UpdateSpan_Test002
115 * @tc.desc: Test cast to GridProperty
116 * @tc.type: FUNC
117 */
118 HWTEST_F(GridPropertyTestNg, UpdateSpan_Test002, TestSize.Level1)
119 {
120 /**
121 * @tc.steps1: create a object gridProperty.
122 */
123 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
124 auto type = static_cast<GridSizeType>(INDEX);
125
126 /**
127 * @tc.steps2: create a container_.
128 */
129 gridProperty->container_ = AceType::MakeRefPtr<GridContainerLayoutProperty>();
130
131 /**
132 * @tc.steps2: call UpdateSpan.push span is < 0.
133 * @tc.expected: Return result is false.
134 */
135 bool result = gridProperty->UpdateSpan(SPANONE, type);
136 EXPECT_FALSE(result);
137 }
138
139 /**
140 * @tc.name: UpdateOffset_Test
141 * @tc.desc: Test cast to GridProperty
142 * @tc.type: FUNC
143 */
144 HWTEST_F(GridPropertyTestNg, UpdateOffset_Test, TestSize.Level1)
145 {
146 /**
147 * @tc.steps1: create a object gridProperty.
148 */
149 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
150 auto type = static_cast<GridSizeType>(INDEX);
151
152 /**
153 * @tc.steps2: create a container_.
154 */
155 gridProperty->container_ = AceType::MakeRefPtr<GridContainerLayoutProperty>();
156
157 /**
158 * @tc.steps2: call UpdateSpan.push span is < 0.
159 * @tc.expected: Return result is false.
160 */
161 bool result = gridProperty->UpdateOffset(OFFSETONE, type);
162 EXPECT_FALSE(result);
163 }
164
165 /**
166 * @tc.name: SetSpan_Test
167 * @tc.desc: Test cast to GridProperty
168 * @tc.type: FUNC
169 */
170 HWTEST_F(GridPropertyTestNg, SetSpan_Test, TestSize.Level1)
171 {
172 /**
173 * @tc.steps1: create a object gridProperty.
174 */
175 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
176
177 /**
178 * @tc.steps2: call MakeProperty.set typedPropertySet_ is not empty
179 * @tc.steps2: call SetSpan, push span_ == span.
180 * @tc.expected: Return result is false.
181 */
182 MakeProperty(gridProperty);
183 auto type = static_cast<GridSizeType>(INDEX);
184 gridProperty->UpdateSpan(SPANONE, type);
185 bool result = gridProperty->SetSpan(type, SPANONE);
186 EXPECT_FALSE(result);
187
188 /**
189 * @tc.steps3: call SetSpan, push span_ != span.
190 * @tc.expected: Return result_Span is true.
191 */
192 bool result_Span = gridProperty->SetSpan(type, DEFAULT_GRID_SPAN);
193 EXPECT_TRUE(result_Span);
194 }
195
196 /**
197 * @tc.name: SetOffset_Test
198 * @tc.desc: Test cast to GridProperty
199 * @tc.type: FUNC
200 */
201 HWTEST_F(GridPropertyTestNg, SetOffset_Test, TestSize.Level1)
202 {
203 /**
204 * @tc.steps1: create a object gridProperty.
205 */
206 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
207
208 /**
209 * @tc.steps2: call SetOffset, push offset_ != offset.
210 * @tc.expected: Return result is true.
211 */
212 auto type = static_cast<GridSizeType>(INDEX);
213 bool result = gridProperty->SetOffset(type, OFFSETONE);
214 EXPECT_TRUE(result);
215
216 /**
217 * @tc.steps3: call MakeProperty.
218 * @tc.steps3: call SetOffset, push offset_ == offset.
219 * @tc.expected: Return result_Off is false.
220 */
221 MakeProperty(gridProperty);
222 gridProperty->UpdateOffset(OFFSETONE, type);
223 bool result_Off = gridProperty->SetOffset(type, OFFSETONE);
224 EXPECT_FALSE(result_Off);
225 }
226
227 /**
228 * @tc.name: ToJsonValue_Test01
229 * @tc.desc: Test cast to GridProperty
230 * @tc.type: FUNC
231 */
232 HWTEST_F(GridPropertyTestNg, ToJsonValue_Test01, TestSize.Level1)
233 {
234 /**
235 * @tc.steps1: create a object gridProperty.
236 */
237 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
238
239 /**
240 * @tc.steps2: callback ToJsonValue.push gridInfo_ is null.
241 * @tc.expected: Return expected results.
242 */
243 auto json = JsonUtil::Create(true);
244 gridProperty->ToJsonValue(json);
245 EXPECT_EQ(json->GetString("gridSpan"), "");
246 EXPECT_EQ(json->GetString("gridOffset"), "");
247
248 /**
249 * @tc.steps3: callback GetWidth.
250 * @tc.expected: expected Return result is default value.
251 */
252 auto result_Width = gridProperty->GetWidth();
253 EXPECT_EQ(result_Width, TEST_VALUE);
254
255 /**
256 * @tc.steps4: callback GetOffset.
257 * @tc.expected: expected Return result is default value.
258 */
259 auto result_Offset = gridProperty->GetOffset();
260 EXPECT_EQ(result_Offset, TEST_VALUE);
261 }
262
263 /**
264 * @tc.name: GetOffset_Test
265 * @tc.desc: Test cast to GridProperty
266 * @tc.type: FUNC
267 */
268 HWTEST_F(GridPropertyTestNg, GetOffset_Test, TestSize.Level1)
269 {
270 /**
271 * @tc.steps1: create a object gridProperty.
272 * @tc.steps1: create a columnBuilder and builder.
273 */
274 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
275 GridColumnInfo::Builder columnBuilder;
276 GridContainerInfo::Builder builder;
277 builder.SetColumns(INDEX);
278 builder.SetGutterWidth(TESTWIDTH);
279 builder.SetMarginLeft(TESTLEFT);
280 builder.SetMarginRight(TESTRIGHT);
281 builder.SetSizeType(GridSizeType::XL);
282 builder.SetColumnType(GridColumnType::NONE);
283 builder.SetGridTemplateType(GridTemplateType::NORMAL);
284
285 /**
286 * @tc.steps2: callback GetOffset.push hasColumnOffset_ is false.
287 * @tc.expected: Return expected results.
288 */
289 gridProperty->gridInfo_ = columnBuilder.Build();
290 gridProperty->gridInfo_->parent_ = builder.Build();
291 gridProperty->gridInfo_->dimOffsets_ = { TESTWIDTH };
292 auto result_One = gridProperty->GetOffset();
293 EXPECT_EQ(result_One, TESTRIGHT);
294
295 /**
296 * @tc.steps3: callback GetOffset.push hasColumnOffset_ is true.
297 * @tc.expected: Return expected results.
298 */
299 gridProperty->gridInfo_->hasColumnOffset_ = true;
300 auto result_Two = gridProperty->GetOffset();
301 EXPECT_EQ(result_Two, UNDEFINED_DIMENSION);
302 }
303
304 /**
305 * @tc.name: GetContainerPositionTest01
306 * @tc.desc: Test cast to GridProperty
307 * @tc.type: FUNC
308 */
309 HWTEST_F(GridPropertyTestNg, GetContainerPositionTest01, TestSize.Level1)
310 {
311 /**
312 * @tc.steps1: create a object gridProperty.
313 */
314 RefPtr<GridProperty> gridProperty = AceType::MakeRefPtr<NG::GridProperty>();
315
316 gridProperty->container_ = AceType::MakeRefPtr<GridContainerLayoutProperty>();
317 auto columnFrameNode1 = AceType::MakeRefPtr<FrameNode>("test1", 1, AceType::MakeRefPtr<Pattern>());
318 auto temp = AceType::MakeRefPtr<GridContainerLayoutProperty>();
319 temp->host_ = columnFrameNode1;
320 gridProperty->container_ = temp;
321
322 /**
323 * @tc.steps2: callback GetContainerPosition.push container_ is not null.
324 * @tc.expected: Return expected results.
325 */
326 auto result = gridProperty->GetContainerPosition();
327 EXPECT_EQ(result, OffsetF());
328 }
329 } // namespace OHOS::Ace::NG
330