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 #include "component_test/core/combination_isscrollable.h"
17
18 #include "core/components_ng/pattern/stepper/stepper_node.h"
19 #include "core/components_ng/pattern/stepper/stepper_pattern.h"
20 #include "core/components_ng/pattern/swiper/swiper_layout_property.h"
21 #include "core/components_ng/pattern/swiper/swiper_pattern.h"
22 #include "core/components_ng/pattern/tabs/tab_bar_layout_property.h"
23 #include "core/components_ng/pattern/tabs/tab_bar_pattern.h"
24
25 namespace OHOS::Ace {
CombinationIsScrollable(const RefPtr<NG::FrameNode> frameNode)26 CombinationIsScrollable::CombinationIsScrollable(const RefPtr<NG::FrameNode> frameNode)
27 {
28 frameNode_ = frameNode;
29 pattern_ = frameNode_->GetPattern();
30 }
31
IsComponentExist()32 bool CombinationIsScrollable::IsComponentExist()
33 {
34 CHECK_NULL_RETURN(pattern_, false);
35 if (AceType::InstanceOf<OHOS::Ace::NG::SwiperPattern>(pattern_)) {
36 isSwiper_ = true;
37 return true;
38 }
39 if (AceType::InstanceOf<OHOS::Ace::NG::StepperPattern>(pattern_)) {
40 isSteper_ = true;
41 return true;
42 }
43 if (AceType::InstanceOf<OHOS::Ace::NG::TabBarPattern>(pattern_)) {
44 isTabBar_ = false;
45 return true;
46 }
47 return false;
48 }
49
IsComponentScrollable() const50 bool CombinationIsScrollable::IsComponentScrollable() const
51 {
52 if (isSwiper_) {
53 auto swiperPattern = AceType::DynamicCast<OHOS::Ace::NG::SwiperPattern>(pattern_);
54 CHECK_NULL_RETURN(swiperPattern, false);
55 auto swiperLayoutProperty = frameNode_->GetLayoutProperty<OHOS::Ace::NG::SwiperLayoutProperty>();
56 CHECK_NULL_RETURN(swiperLayoutProperty, false);
57 bool isLoop = swiperLayoutProperty->GetLoop().value_or(true);
58 if (!isLoop && swiperPattern->TotalCount() <= 1) {
59 return false;
60 }
61 return true;
62 }
63 if (isSteper_) {
64 auto swiperFrameNode = AceType::DynamicCast<OHOS::Ace::NG::StepperNode>(frameNode_);
65 CHECK_NULL_RETURN(swiperFrameNode, false);
66 auto swiperNode = AceType::DynamicCast<NG::FrameNode>(
67 swiperFrameNode->GetChildAtIndex(swiperFrameNode->GetChildIndexById(swiperFrameNode->GetSwiperId())));
68 CHECK_NULL_RETURN(swiperNode, false);
69 auto swiperPattern = swiperNode->GetPattern<OHOS::Ace::NG::SwiperPattern>();
70 CHECK_NULL_RETURN(swiperPattern, false);
71 auto swiperLayoutProperty = swiperNode->GetLayoutProperty<OHOS::Ace::NG::SwiperLayoutProperty>();
72 CHECK_NULL_RETURN(swiperLayoutProperty, false);
73 bool isLoop = swiperLayoutProperty->GetLoop().value_or(true);
74 return (!isLoop && swiperPattern->TotalCount() <= 1) ? false : true;
75 }
76 auto tabBarPattern = frameNode_->GetPattern<OHOS::Ace::NG::TabBarPattern>();
77 auto swiperPattern = frameNode_->GetPattern<OHOS::Ace::NG::SwiperPattern>();
78 CHECK_NULL_RETURN(tabBarPattern, false);
79 auto tabBarLayoutProperty = tabBarPattern->GetLayoutProperty<OHOS::Ace::NG::TabBarLayoutProperty>();
80 CHECK_NULL_RETURN(tabBarLayoutProperty, false);
81 if (tabBarLayoutProperty->GetTabBarMode().value_or(TabBarMode::FIXED) == TabBarMode::SCROLLABLE &&
82 swiperPattern->TotalCount() > 1) {
83 return true;
84 }
85 return false;
86 }
87 } // namespace OHOS::Ace
88