• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include <optional>
17 
18 #include "gtest/gtest.h"
19 
20 #include "test/unittest/core/pattern/scrollable/mock_scrollable.h"
21 #include "core/components_ng/manager/scroll_adjust/scroll_adjust_manager.h"
22 #include "core/components_ng/pattern/scrollable/scrollable_properties.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 namespace OHOS::Ace::NG {
27 
28 class ScrollAdjustmanagerTestNg : public testing::Test {
29 public:
30     void SetUp() override;
31     void TearDown() override;
32 
33     RefPtr<ScrollAdjustmanager> scrollAdjustmanager_;
34 };
35 
SetUp()36 void ScrollAdjustmanagerTestNg::SetUp()
37 {
38     scrollAdjustmanager_ = Referenced::MakeRefPtr<ScrollAdjustmanager>();
39 }
40 
TearDown()41 void ScrollAdjustmanagerTestNg::TearDown()
42 {
43     scrollAdjustmanager_ = nullptr;
44 }
45 
46 /**
47  * @tc.name: ChangeScrollStateIfNeedTest
48  * @tc.desc: ChangeScrollStateIfNeedTest
49  * @tc.type: FUNC
50  */
51 HWTEST_F(ScrollAdjustmanagerTestNg, ChangeScrollStateIfNeedTest, TestSize.Level1)
52 {
53     /**
54      * @tc.steps: step1. if current state is ScrollState::SCROLL, get ScrollState is need change.
55      * @tc.expected: step1. current back is false.
56      */
57     bool testResult = true;
58     bool actual = scrollAdjustmanager_->ChangeScrollStateIfNeed(ScrollState::SCROLL);
59     EXPECT_EQ(actual, testResult);
60 
61     /**
62      * @tc.steps: step2. if current state is ScrollState::FLING, get ScrollState is need change.
63      * @tc.expected: step2. current back is false.
64      */
65     testResult = false;
66     actual = scrollAdjustmanager_->ChangeScrollStateIfNeed(ScrollState::FLING);
67     EXPECT_EQ(actual, testResult);
68 
69     /**
70      * @tc.steps: step3. if current state is ScrollState::IDLE, get ScrollState is need change.
71      * @tc.expected: step3. current back is false.
72      */
73     testResult = false;
74     actual = scrollAdjustmanager_->ChangeScrollStateIfNeed(ScrollState::IDLE);
75     EXPECT_EQ(actual, testResult);
76 }
77 
78 /**
79  * @tc.name: AdjustEndIndexTest
80  * @tc.desc: AdjustEndIndexTest
81  * @tc.type: FUNC
82  */
83 HWTEST_F(ScrollAdjustmanagerTestNg, AdjustEndIndexTest, TestSize.Level1)
84 {
85     /**
86      * @tc.steps: step1. adjust endindex, get adjust result.
87      * @tc.expected: step1. adjust result.
88      */
89     int32_t testResult = 1;
90     EXPECT_EQ(scrollAdjustmanager_->AdjustEndIndex(0), testResult);
91 
92     /**
93      * @tc.steps: step1. adjust endindex, get adjust result.
94      * @tc.expected: step1. adjust result.
95      */
96     testResult = 3;
97     EXPECT_EQ(scrollAdjustmanager_->AdjustEndIndex(2), testResult);
98 
99     /**
100      * @tc.steps: step1. adjust endindex, get adjust result.
101      * @tc.expected: step1. adjust result.
102      */
103     testResult = 0;
104     EXPECT_EQ(scrollAdjustmanager_->AdjustEndIndex(-1), testResult);
105 }
106 
107 /**
108  * @tc.name: AdjustCachedCountTest
109  * @tc.desc: AdjustCachedCountTest
110  * @tc.type: FUNC
111  */
112 HWTEST_F(ScrollAdjustmanagerTestNg, AdjustCachedCountTest, TestSize.Level1)
113 {
114     /**
115      * @tc.steps: step1. adjust cachecount, get adjust result.
116      * @tc.expected: step1. adjust result.
117      */
118     int32_t testResult = 1;
119     EXPECT_EQ(scrollAdjustmanager_->AdjustCachedCount(0), testResult);
120 
121     /**
122      * @tc.steps: step1. adjust cachecount, get adjust result.
123      * @tc.expected: step1. adjust result.
124      */
125     testResult = 3;
126     EXPECT_EQ(scrollAdjustmanager_->AdjustCachedCount(2), testResult);
127 
128     /**
129      * @tc.steps: step1. adjust cachecount, get adjust result.
130      * @tc.expected: step1. adjust result.
131      */
132     testResult = 0;
133     EXPECT_EQ(scrollAdjustmanager_->AdjustCachedCount(-1), testResult);
134 }
135 }
136