• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_TEST_PATTERN_TEST_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_TEST_PATTERN_TEST_NG_H
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <type_traits>
22 
23 #define private public
24 #define protected public
25 #include "core/components/theme/theme_constants.h"
26 #include "core/components_ng/base/frame_node.h"
27 #include "core/components_ng/pattern/linear_layout/column_model_ng.h"
28 #include "core/components_ng/pattern/linear_layout/row_model_ng.h"
29 #include "core/components_ng/pattern/scrollable/scrollable.h"
30 #include "core/components_ng/pattern/stack/stack_model_ng.h"
31 #include "core/components_ng/pattern/text/text_model_ng.h"
32 #include "core/components_ng/pattern/scrollable/scrollable_animation_consts.h"
33 
34 namespace OHOS::Ace::NG {
35 using namespace testing;
36 using namespace testing::ext;
37 constexpr Dimension FILL_LENGTH = Dimension(1.0, DimensionUnit::PERCENT);
38 constexpr int32_t NULL_VALUE = -1;
39 constexpr double DEFAULT_FRICTION = 0.6;
40 constexpr double NEW_DEFAULT_FRICTION = 0.7;
41 constexpr float DRAG_VELOCITY = 200.f;
42 
43 class TestNG : public testing::Test {
44 public:
45     static void SetUpTestSuite();
46     static void TearDownTestSuite();
47     void FlushUITasks();
48     void FlushUITasks(const RefPtr<FrameNode>& frameNode);
49     void CreateDone();
50     RefPtr<FrameNode> GetStageNode();
51     void MountToStageNode(const RefPtr<FrameNode>& frameNode);
52     void RemoveFromStageNode();
53     uint64_t GetActions(const RefPtr<AccessibilityProperty>& accessibilityProperty);
54     TouchEventInfo CreateTouchEventInfo(TouchType touchType, Offset location);
55     static RefPtr<ThemeConstants> CreateThemeConstants(const std::string& patternName);
56     void FlushExpandSafeAreaTask();
57     void CreateLayoutTask(const RefPtr<FrameNode>& frameNode);
58     RefPtr<FrameNode> CreateText(const std::u16string& content, const std::function<void(TextModelNG)>& callback);
59     RefPtr<FrameNode> CreateRow(const std::function<void(RowModelNG)>& callback);
60     RefPtr<FrameNode> CreateColumn(const std::function<void(ColumnModelNG)>& callback);
61     RefPtr<FrameNode> CreateStack(const std::function<void(StackModelNG)>& callback);
62     void SetSize(std::optional<Axis> axis, const CalcLength& crossSize, const CalcLength& mainSize);
63     AssertionResult IsExist(const RefPtr<FrameNode>& frameNode, int32_t index);
64     AssertionResult IsExistAndActive(const RefPtr<FrameNode>& frameNode, int32_t index);
65     AssertionResult IsExistAndInActive(const RefPtr<FrameNode>& frameNode, int32_t index);
66 
IsEqual(const double & actual,const double & expected)67     AssertionResult IsEqual(const double& actual, const double& expected)
68     {
69         if (NearEqual(actual, expected)) {
70             return AssertionSuccess();
71         }
72         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
73     }
74 
IsEqual(const float & actual,const double & expected)75     AssertionResult IsEqual(const float& actual, const double& expected)
76     {
77         if (NearEqual(actual, expected)) {
78             return AssertionSuccess();
79         }
80         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
81     }
82 
IsEqual(const double & actual,const float & expected)83     AssertionResult IsEqual(const double& actual, const float& expected)
84     {
85         if (NearEqual(actual, expected)) {
86             return AssertionSuccess();
87         }
88         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
89     }
90 
IsEqual(const float & actual,const float & expected)91     AssertionResult IsEqual(const float& actual, const float& expected)
92     {
93         if (NearEqual(actual, expected)) {
94             return AssertionSuccess();
95         }
96         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
97     }
98 
IsEqual(const int32_t & actual,const int32_t & expected)99     AssertionResult IsEqual(const int32_t& actual, const int32_t& expected)
100     {
101         if (NearEqual(actual, expected)) {
102             return AssertionSuccess();
103         }
104         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
105     }
106 
IsEqual(const std::string & actual,const std::string & expected)107     AssertionResult IsEqual(const std::string& actual, const std::string& expected)
108     {
109         if (NearEqual(actual, expected)) {
110             return AssertionSuccess();
111         }
112         return AssertionFailure() << "Actual: " << actual << " Expected: " << expected;
113     }
114 
115     template<typename T>
IsEqual(const T & actual,const T & expected)116     AssertionResult IsEqual(const T& actual, const T& expected)
117     {
118         if (NearEqual(actual, expected)) {
119             return AssertionSuccess();
120         }
121         return AssertionFailure() << "Actual: " << actual.ToString() << " Expected: " << expected.ToString();
122     }
123 
IsEqual(const NG::OverScrollOffset & actual,const NG::OverScrollOffset & expected)124     AssertionResult IsEqual(const NG::OverScrollOffset& actual, const NG::OverScrollOffset& expected)
125     {
126         if (NearEqual(actual.start, expected.start) && NearEqual(actual.end, expected.end)) {
127             return AssertionSuccess();
128         }
129         return AssertionFailure() << "Actual: " << "{ " << actual.start << " , " << actual.end << " }"
130                                   << " Expected: " << "{ " << expected.start << " , " << expected.end << " }";
131     }
132 
IsEqual(const ListItemIndex & actual,const ListItemIndex & expected)133     AssertionResult IsEqual(const ListItemIndex& actual, const ListItemIndex& expected)
134     {
135         if (actual.index == expected.index && actual.area == expected.area &&
136             actual.indexInGroup == expected.indexInGroup) {
137             return AssertionSuccess();
138         }
139         return AssertionFailure() << "Actual: " << "{ " << actual.index << " , " << actual.area << " , "
140                                   << actual.indexInGroup << " }" << " Expected: " << "{ " << expected.index << " , "
141                                   << expected.area << " , " << expected.indexInGroup << " }";
142     }
143 
GetChildFrameNode(const RefPtr<FrameNode> & frameNode,int32_t index)144     RefPtr<FrameNode> GetChildFrameNode(const RefPtr<FrameNode>& frameNode, int32_t index)
145     {
146         return AceType::DynamicCast<FrameNode>(frameNode->GetChildByIndex(index));
147     }
148 
GetChildFocusHub(const RefPtr<FrameNode> & frameNode,int32_t index)149     RefPtr<FocusHub> GetChildFocusHub(const RefPtr<FrameNode>& frameNode, int32_t index)
150     {
151         return GetChildFrameNode(frameNode, index)->GetOrCreateFocusHub();
152     }
153 
154     template<typename T>
GetChildPattern(const RefPtr<FrameNode> & frameNode,int32_t index)155     RefPtr<T> GetChildPattern(const RefPtr<FrameNode>& frameNode, int32_t index)
156     {
157         RefPtr<Pattern> pattern = GetChildFrameNode(frameNode, index)->GetPattern();
158         return AceType::DynamicCast<T>(pattern);
159     }
160 
161     template<typename T>
GetChildLayoutProperty(const RefPtr<FrameNode> & frameNode,int32_t index)162     RefPtr<T> GetChildLayoutProperty(const RefPtr<FrameNode>& frameNode, int32_t index)
163     {
164         RefPtr<LayoutProperty> layoutProperty = GetChildFrameNode(frameNode, index)->GetLayoutProperty();
165         return AceType::DynamicCast<T>(layoutProperty);
166     }
167 
168     template<typename T>
GetChildAccessibilityProperty(const RefPtr<FrameNode> & frameNode,int32_t index)169     RefPtr<T> GetChildAccessibilityProperty(const RefPtr<FrameNode>& frameNode, int32_t index)
170     {
171         return GetChildFrameNode(frameNode, index)->GetAccessibilityProperty<T>();
172     }
173 
174     template<typename T>
GetChildEventHub(const RefPtr<FrameNode> & frameNode,int32_t index)175     RefPtr<T> GetChildEventHub(const RefPtr<FrameNode>& frameNode, int32_t index)
176     {
177         return GetChildFrameNode(frameNode, index)->GetOrCreateEventHub<T>();
178     }
179 
GetChildRect(const RefPtr<FrameNode> & frameNode,int32_t index)180     RectF GetChildRect(const RefPtr<FrameNode>& frameNode, int32_t index)
181     {
182         return GetChildFrameNode(frameNode, index)->GetGeometryNode()->GetFrameRect();
183     }
184 
GetChildSize(const RefPtr<FrameNode> & frameNode,int32_t index)185     SizeF GetChildSize(const RefPtr<FrameNode>& frameNode, int32_t index)
186     {
187         return GetChildFrameNode(frameNode, index)->GetGeometryNode()->GetFrameSize();
188     }
189 
GetChildOffset(const RefPtr<FrameNode> & frameNode,int32_t index)190     OffsetF GetChildOffset(const RefPtr<FrameNode>& frameNode, int32_t index)
191     {
192         return GetChildFrameNode(frameNode, index)->GetGeometryNode()->GetFrameOffset();
193     }
194 
GetChildX(const RefPtr<FrameNode> & frameNode,int32_t index)195     float GetChildX(const RefPtr<FrameNode>& frameNode, int32_t index)
196     {
197         return GetChildRect(frameNode, index).GetX();
198     }
199 
GetChildY(const RefPtr<FrameNode> & frameNode,int32_t index)200     float GetChildY(const RefPtr<FrameNode>& frameNode, int32_t index)
201     {
202         return GetChildRect(frameNode, index).GetY();
203     }
204 
GetChildWidth(const RefPtr<FrameNode> & frameNode,int32_t index)205     float GetChildWidth(const RefPtr<FrameNode>& frameNode, int32_t index)
206     {
207         return GetChildRect(frameNode, index).Width();
208     }
209 
GetChildHeight(const RefPtr<FrameNode> & frameNode,int32_t index)210     float GetChildHeight(const RefPtr<FrameNode>& frameNode, int32_t index)
211     {
212         return GetChildRect(frameNode, index).Height();
213     }
214 
ClearOldNodes()215     void ClearOldNodes()
216     {
217         ElementRegister::GetInstance()->Clear();
218     }
219 
GetElmtId()220     ElementIdType GetElmtId()
221     {
222         elmtId_++;
223         return elmtId_;
224     }
225 
ResetElmtId()226     void ResetElmtId()
227     {
228         elmtId_ = 10000;
229     }
230 
231     ElementIdType elmtId_ = 10000;
232 };
233 } // namespace OHOS::Ace::NG
234 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SCROLL_SCROLL_PATTERN_H
235