1 /*
2  * Copyright (c) 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_TEST_UNITTEST_CORE_PATTERN_LIST_LIST_TEST_NG_H
17 #define FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_LIST_LIST_TEST_NG_H
18 
19 #include "test/unittest/core/pattern/test_ng.h"
20 #include "test/mock/core/animation/mock_animation_manager.h"
21 #define private public
22 #define protected public
23 #include "core/components_ng/pattern/list/list_item_group_model_ng.h"
24 #include "core/components_ng/pattern/list/list_item_model_ng.h"
25 #include "core/components_ng/pattern/list/list_model_ng.h"
26 #include "core/components_ng/pattern/list/list_pattern.h"
27 
28 namespace OHOS::Ace::NG {
29 using namespace testing;
30 using namespace testing::ext;
31 constexpr float LIST_WIDTH = 240.f;
32 constexpr float LIST_HEIGHT = 400.f;
33 constexpr float ITEM_WIDTH = 60.f;
34 constexpr float ITEM_HEIGHT = 100.f;
35 constexpr int32_t TOTAL_ITEM_NUMBER = 6;
36 constexpr int32_t VIEW_ITEM_NUMBER = 4;
37 constexpr int32_t GROUP_NUMBER = 4;
38 constexpr int32_t GROUP_ITEM_NUMBER = 2;
39 constexpr float GROUP_HEADER_LEN = 50.f;
40 constexpr float START_NODE_LEN = 80.f;
41 constexpr float END_NODE_LEN = 100.f;
42 constexpr float DELETE_AREA_DISTANCE = 50.f;
43 constexpr Dimension GROUP_MARGIN = 12.0_vp;
44 constexpr float DEFAULT_STIFFNESS = 228;
45 constexpr float DEFAULT_DAMPING = 30;
46 constexpr float SWIPER_SPEED_TH = 1500.f;
47 constexpr float SWIPER_TH = 0.25f;
48 constexpr int32_t DEFAULT_LANES = 1;
49 constexpr int32_t DEFAULT_SPACE = 0;
50 constexpr float DEFAULT_STARTOFFSET = 0.f;
51 constexpr float SPACE = 10.f;
52 constexpr float STROKE_WIDTH = 5.f;
53 const V2::ItemDivider ITEM_DIVIDER = { Dimension(STROKE_WIDTH), Dimension(10), Dimension(20), Color(0x000000) };
54 constexpr int32_t HEADER_INDEX = 0;
55 constexpr int32_t FOOTER_INDEX = 1;
56 const Color ITEMDEFAULT_COLOR = Color::WHITE;
57 const Color HOVER_COLOR = Color::RED;
58 const Color PRESS_COLOR = Color::BLACK;
59 constexpr double DISABLED_ALPHA = 0.4;
60 
61 class ListTestNg : public TestNG {
62 public:
63     static void SetUpTestSuite();
64     static void TearDownTestSuite();
65     static RefPtr<FrameNode> CreateCustomNode(const std::string& tag);
66     void SetUp() override;
67     void TearDown() override;
68     void GetList();
69     ListModelNG CreateList();
70     ListItemModelNG CreateListItem(V2::ListItemStyle listItemStyle = V2::ListItemStyle::NONE);
71     void CreateListItems(int32_t itemNumber, V2::ListItemStyle listItemStyle = V2::ListItemStyle::NONE);
72     ListItemGroupModelNG CreateListItemGroup(V2::ListItemGroupStyle listItemGroupStyle = V2::ListItemGroupStyle::NONE);
73     void CreateListItemGroups(
74         int32_t groupNumber, V2::ListItemGroupStyle listItemGroupStyle = V2::ListItemGroupStyle::NONE);
75 
76     void CreateItemWithSize(int32_t itemNumber, SizeT<Dimension> itemSize);
77     void CreateGroupChildrenMainSize(int32_t groupNumber);
78     void CreateGroupWithItem(int32_t groupNumber, Axis axis = Axis::VERTICAL);
79     void CreateGroupWithSetting(
80         int32_t groupNumber, V2::ListItemGroupStyle listItemGroupStyle, int32_t itemNumber = GROUP_ITEM_NUMBER);
81     void CreateGroupWithSettingWithComponentContent(
82         int32_t groupNumber, V2::ListItemGroupStyle listItemGroupStyle, int32_t itemNumber = GROUP_ITEM_NUMBER);
83     void CreateGroupWithSettingChildrenMainSize(int32_t groupNumber);
84     void CreateSwipeItems(std::function<void()> startAction, std::function<void()> endAction,
85         V2::SwipeEdgeEffect effect, int32_t itemNumber = TOTAL_ITEM_NUMBER);
86     void CreateRepeatVirtualScrollNode(int32_t itemNumber, const std::function<void(uint32_t)>& createFunc);
87     std::function<void()> GetRowOrColBuilder(float crossSize, float mainSize);
88     std::function<void()> GetRowOrColBuilder(Dimension crossSize, Dimension mainSize);
89 
90     void UpdateCurrentOffset(float offset, int32_t source = SCROLL_FROM_UPDATE);
91     void ScrollToEdge(ScrollEdgeType scrollEdgeType);
92     void ScrollToIndex(int32_t index, bool smooth, ScrollAlign align, std::optional<float> extraOffset = std::nullopt);
93     void ScrollToItemInGroup(int32_t index, int32_t indexInGroup, bool smooth, ScrollAlign align);
94     void DragSwiperItem(int32_t index, float mainDelta, float mainVelocity = SWIPER_SPEED_TH);
95     void HandleDragStart(int32_t index);
96     void HandleDragUpdate(int32_t index, float mainDelta);
97     void HandleDragEnd(int32_t index, float mainVelocity = SWIPER_SPEED_TH);
98     void ScrollSnap(double offset, double velocity);
99     void FlushIdleTask(const RefPtr<ListPattern>& listPattern);
100 
101     AssertionResult ScrollToIndex(int32_t index, bool smooth, ScrollAlign align, float expectOffset);
102     AssertionResult JumpToItemInGroup(
103         int32_t index, int32_t indexInGroup, bool smooth, ScrollAlign align, float expectOffset);
104     std::vector<RefPtr<FrameNode>> GetALLItem();
105     int32_t findFocusNodeIndex(RefPtr<FocusHub>& focusNode);
106     void ScrollTo(float position);
107 
108     RefPtr<FrameNode> frameNode_;
109     RefPtr<ListPattern> pattern_;
110     RefPtr<ListEventHub> eventHub_;
111     RefPtr<ListLayoutProperty> layoutProperty_;
112     RefPtr<ScrollablePaintProperty> paintProperty_;
113     RefPtr<ListAccessibilityProperty> accessibilityProperty_;
114 };
115 } // namespace OHOS::Ace::NG
116 
117 #endif // FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_LIST_LIST_TEST_NG_H