1 /*
2  * Copyright (c) 2020-2021 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 "ui_test_ui_scroll_view.h"
17 #include "common/screen.h"
18 #include "components/ui_label_button.h"
19 
20 #define VISIBLE_XBAR(visible)                                               \
21     if (Screen::GetInstance().GetScreenShape() == ScreenShape::RECTANGLE) { \
22         scroll->SetXScrollBarVisible(visible);                              \
23     }                                                                       \
24 
25 namespace OHOS {
26 namespace {
27 static int16_t g_buttonH = 400;
28 static int16_t g_buttonW = 400;
29 static int16_t g_smallButtonH = 40;
30 static int16_t g_smallButtonW = 40;
31 static int16_t g_scrollH = 300;
32 static int16_t g_scrollW = 300;
33 static UILabel* g_scrollSateLabel = nullptr;
34 const int16_t DELTA_Y_COORDINATE = 19;
35 const int16_t DELTA_Y_COORDINATE_2 = 37;
36 } // namespace
37 
38 class TestOnScrollListener : public UIScrollView::OnScrollListener {
OnScrollStart()39     void OnScrollStart() override
40     {
41         g_scrollSateLabel->SetText("moving");
42         g_scrollSateLabel->Invalidate();
43     }
44 
OnScrollEnd()45     void OnScrollEnd() override
46     {
47         g_scrollSateLabel->SetText("stop");
48         g_scrollSateLabel->Invalidate();
49     }
50 };
51 
52 static TestOnScrollListener* g_listener = nullptr;
53 
SetUp()54 void UITestUIScrollView::SetUp()
55 {
56     if (container_ == nullptr) {
57         container_ = new UIScrollView();
58         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
59         container_->SetThrowDrag(true);
60     }
61     if (g_scrollSateLabel == nullptr) {
62         g_scrollSateLabel = GetTitleLabel("stop");
63         g_scrollSateLabel->SetPosition(0, 0, 100, TITLE_LABEL_DEFAULT_HEIGHT); // 100: width
64         g_scrollSateLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
65     }
66 
67     if (g_listener == nullptr) {
68         g_listener = new TestOnScrollListener();
69     }
70 }
71 
TearDown()72 void UITestUIScrollView::TearDown()
73 {
74     container_->Remove(g_scrollSateLabel);
75     DeleteChildren(container_);
76     container_ = nullptr;
77     if (g_scrollSateLabel != nullptr) {
78         delete g_scrollSateLabel;
79         g_scrollSateLabel = nullptr;
80     }
81     if (g_listener != nullptr) {
82         delete g_listener;
83         g_listener = nullptr;
84     }
85     lastX_ = 0;
86     lastY_ = 0;
87     UITest::TearDown();
88 }
89 
GetTestView()90 const UIView* UITestUIScrollView::GetTestView()
91 {
92     UIKitUIScrollViewTestbar001();
93     UIKitUIScrollViewTestbar002();
94     UIKitUIScrollViewTestbar003();
95     UIKitUIScrollViewTestbar004();
96     UIKitUIScrollViewTestScrollableSet001();
97     UIKitUIScrollViewTestScrollableSet002();
98     UIKitUIScrollViewTestScrollBlankSet001();
99     UIKitUIScrollViewTestScrolllistener001();
100     return container_;
101 }
102 
UIKitUIScrollViewTestbar001()103 void UITestUIScrollView::UIKitUIScrollViewTestbar001()
104 {
105     if (container_ == nullptr) {
106         return;
107     }
108     positionX_ = TEXT_DISTANCE_TO_LEFT_SIDE;
109     positionY_ = TEXT_DISTANCE_TO_TOP_SIDE;
110     UILabel* label = GetTitleLabel("UIScrollView设置游标可见 ");
111     label->SetPosition(positionX_, positionY_);
112     container_->Add(label);
113     positionY_ += DELTA_Y_COORDINATE_2;
114 
115     UIScrollView* scroll = new UIScrollView();
116     scroll->SetIntercept(true);
117     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
118     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
119     VISIBLE_XBAR(false);
120     scroll->SetYScrollBarVisible(true);
121     scroll->SetReboundSize(50); // 50: rebound size
122     container_->Add(scroll);
123     UILabelButton* button1 = new UILabelButton();
124     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
125     button1->SetText("button1");
126     scroll->Add(button1);
127     SetLastPos(scroll);
128     positionY_ += g_scrollH;
129 }
130 
UIKitUIScrollViewTestbar002()131 void UITestUIScrollView::UIKitUIScrollViewTestbar002()
132 {
133     if (container_ == nullptr) {
134         return;
135     }
136     UILabel* label = GetTitleLabel("UIScrollView设置游标可见 ");
137     container_->Add(label);
138     positionY_ += DELTA_Y_COORDINATE;
139     label->SetPosition(positionX_, positionY_);
140     positionY_ += DELTA_Y_COORDINATE_2;
141 
142     UIScrollView* scroll = new UIScrollView();
143     scroll->SetIntercept(true);
144     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
145     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
146     VISIBLE_XBAR(true);
147     scroll->SetYScrollBarVisible(true);
148     container_->Add(scroll);
149     UIButton* button1 = new UIButton();
150     button1->SetPosition(0, 0, g_smallButtonW, g_smallButtonH);
151     scroll->Add(button1);
152     SetLastPos(scroll);
153     positionY_ += g_scrollH;
154 }
155 
UIKitUIScrollViewTestbar003()156 void UITestUIScrollView::UIKitUIScrollViewTestbar003()
157 {
158     if (container_ == nullptr) {
159         return;
160     }
161     UILabel* label = GetTitleLabel("UIScrollView游标滑动状态 ");
162     container_->Add(label);
163     positionY_ += DELTA_Y_COORDINATE;
164     label->SetPosition(positionX_, positionY_);
165     positionY_ += DELTA_Y_COORDINATE_2;
166 
167     UIScrollView* scroll = new UIScrollView();
168     scroll->SetIntercept(true);
169     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
170     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
171     VISIBLE_XBAR(true);
172     scroll->SetYScrollBarVisible(true);
173     container_->Add(scroll);
174     UILabelButton* button1 = new UILabelButton();
175     button1->SetText("button1");
176     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
177     UILabelButton* button2 = new UILabelButton();
178     button2->SetText("button2");
179     button2->SetPosition(0, g_buttonH, g_buttonW, g_buttonH);
180     scroll->Add(button1);
181     scroll->Add(button2);
182     SetLastPos(scroll);
183     positionY_ += g_scrollH;
184 }
185 
UIKitUIScrollViewTestbar004()186 void UITestUIScrollView::UIKitUIScrollViewTestbar004()
187 {
188     if (container_ == nullptr) {
189         return;
190     }
191     UILabel* label = GetTitleLabel("UIScrollView设置游标宽度为20");
192     container_->Add(label);
193     positionY_ += DELTA_Y_COORDINATE;
194     label->SetPosition(positionX_, positionY_);
195     positionY_ += DELTA_Y_COORDINATE_2;
196 
197     UIScrollView* scroll = new UIScrollView();
198     scroll->SetIntercept(true);
199     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
200     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
201     VISIBLE_XBAR(true);
202     scroll->SetYScrollBarVisible(true);
203     container_->Add(scroll);
204     UIButton* button1 = new UIButton();
205     button1->SetPosition(0, 0, g_smallButtonW, g_smallButtonH);
206     scroll->Add(button1);
207     SetLastPos(scroll);
208     positionY_ += g_scrollH;
209 }
210 
UIKitUIScrollViewTestScrollableSet001()211 void UITestUIScrollView::UIKitUIScrollViewTestScrollableSet001()
212 {
213     if (container_ == nullptr) {
214         return;
215     }
216     positionX_ = Screen::GetInstance().GetWidth() / 2 + TEXT_DISTANCE_TO_LEFT_SIDE; // 2: half of screen width
217     positionY_ = TEXT_DISTANCE_TO_TOP_SIDE;
218     UILabel* label = GetTitleLabel("UIScrollView设置仅垂直方向可滑动 ");
219     container_->Add(label);
220     label->SetPosition(positionX_, positionY_);
221     positionY_ += DELTA_Y_COORDINATE_2;
222 
223     UIScrollView* scroll = new UIScrollView();
224     scroll->SetIntercept(true);
225     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
226     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
227     scroll->SetScrollBlankSize(20); // 20: means scroll blank size
228     scroll->SetHorizontalScrollState(false);
229     scroll->SetVerticalScrollState(true);
230     VISIBLE_XBAR(true);
231     scroll->SetYScrollBarVisible(true);
232     container_->Add(scroll);
233     UILabelButton* button1 = new UILabelButton();
234     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
235     button1->SetText("button1");
236     scroll->Add(button1);
237     SetLastPos(scroll);
238     positionY_ += g_scrollH;
239 }
240 
UIKitUIScrollViewTestScrollableSet002()241 void UITestUIScrollView::UIKitUIScrollViewTestScrollableSet002()
242 {
243     if (container_ == nullptr) {
244         return;
245     }
246     UILabel* label = GetTitleLabel("UIScrollView设置仅垂直方向不可滑动 ");
247     container_->Add(label);
248     positionY_ += DELTA_Y_COORDINATE;
249     label->SetPosition(positionX_, positionY_);
250     positionY_ += DELTA_Y_COORDINATE_2;
251 
252     UIScrollView* scroll = new UIScrollView();
253     scroll->SetIntercept(true);
254     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
255     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
256     scroll->SetHorizontalScrollState(true);
257     scroll->SetVerticalScrollState(false);
258     VISIBLE_XBAR(true);
259     scroll->SetYScrollBarVisible(true);
260     container_->Add(scroll);
261     UILabelButton* button1 = new UILabelButton();
262     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
263     button1->SetText("button1");
264     scroll->Add(button1);
265     SetLastPos(scroll);
266     positionY_ += g_scrollH;
267 }
268 
UIKitUIScrollViewTestScrollBlankSet001()269 void UITestUIScrollView::UIKitUIScrollViewTestScrollBlankSet001()
270 {
271     if (container_ == nullptr) {
272         return;
273     }
274     UILabel* label = GetTitleLabel("UIScrollView设置高为50的可滑动空白区域 ");
275     container_->Add(label);
276     positionY_ += DELTA_Y_COORDINATE;
277     label->SetPosition(positionX_, positionY_);
278     positionY_ += DELTA_Y_COORDINATE_2;
279 
280     UIScrollView* scroll = new UIScrollView();
281     scroll->SetIntercept(true);
282     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
283     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
284     scroll->SetScrollBlankSize(50); // (50): means scroll blank size
285     VISIBLE_XBAR(true);
286     scroll->SetYScrollBarVisible(true);
287     scroll->SetScrollBarSide(SCROLL_BAR_LEFT_SIDE);
288     container_->Add(scroll);
289     UILabelButton* button1 = new UILabelButton();
290     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
291     button1->SetText("button1");
292     scroll->Add(button1);
293     SetLastPos(scroll);
294     positionY_ += g_scrollH;
295 }
296 
UIKitUIScrollViewTestScrolllistener001()297 void UITestUIScrollView::UIKitUIScrollViewTestScrolllistener001()
298 {
299     if (container_ == nullptr) {
300         return;
301     }
302     UILabel* label = GetTitleLabel("UIScrollView设置滑动状态监听 ");
303     container_->Add(label);
304     positionY_ += DELTA_Y_COORDINATE;
305     label->SetPosition(positionX_, positionY_);
306     positionY_ += DELTA_Y_COORDINATE_2;
307 
308     UIScrollView* scroll = new UIScrollView();
309     scroll->SetIntercept(true);
310     scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
311     scroll->SetPosition(positionX_, positionY_, g_scrollW, g_scrollH);
312     container_->Add(scroll);
313     UILabelButton* button1 = new UILabelButton();
314     button1->SetPosition(0, 0, g_buttonW, g_buttonH);
315     button1->SetText("button1");
316     scroll->Add(button1);
317     scroll->RegisterScrollListener(g_listener);
318     g_scrollSateLabel->SetPosition(positionX_ + scroll->GetWidth() + 10, scroll->GetY()); // 10: increase width
319     container_->Add(g_scrollSateLabel);
320 
321     SetLastPos(scroll);
322 }
323 
SetLastPos(UIView * view)324 void UITestUIScrollView::SetLastPos(UIView* view)
325 {
326     if (view == nullptr) {
327         return;
328     }
329     lastX_ = view->GetX();
330     lastY_ = view->GetY() + view->GetHeight();
331 }
332 } // namespace OHOS
333