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