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