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