1 /*
2  * Copyright (c) 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_scroll_bar.h"
17 #include "components/ui_label_button.h"
18 #include "engines/gfx/gfx_engine_manager.h"
19 #include "test_resource_config.h"
20 
21 namespace {
22 constexpr int16_t LABEL_WIDTH = 150;
23 constexpr int16_t LABEL_HEIGHT = 74;
24 constexpr int16_t LABEL_INTERVAL = 50;
25 constexpr int16_t LABEL_LEFT_INTERVAL = 125;
26 constexpr int16_t VIEW_WIDTH = 400;
27 constexpr int16_t VIEW_HEIGHT = 400;
28 }
29 namespace OHOS {
SetUp()30 void UITestScrollBar::SetUp()
31 {
32     BaseGfxEngine::GetInstance()->SetScreenShape(ScreenShape::CIRCLE);
33 
34     if (container_ == nullptr) {
35         container_ = new UIViewGroup();
36     }
37     if (scrollView_ == nullptr) {
38         scrollView_ = new UIScrollView();
39     }
40     if (list_ == nullptr) {
41         list_ = new UIList();
42     }
43     if (foreImg1_ == nullptr) {
44         foreImg1_ = new UIImageView();
45     }
46     if (foreImg2_ == nullptr) {
47         foreImg2_ = new UIImageView();
48     }
49     if (adapter_ == nullptr) {
50         adapter_ = new TextAdapter();
51     }
52     SetAdapterData();
53 
54     container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
55 
56     container_->Add(scrollView_);
57     scrollView_->SetPosition(0, 0, VIEW_WIDTH, VIEW_HEIGHT);
58     scrollView_->SetYScrollBarVisible(true);
59     scrollView_->SetHorizontalScrollState(false);
60     scrollView_->SetReboundSize(50); // 50: rebound size
61     scrollView_->SetScrollBarCenter({200, 200}); // 200: x, y of center
62 
63     container_->Add(list_);
64     list_->SetPosition(450, 0, VIEW_WIDTH, VIEW_HEIGHT); // 450: x
65     list_->SetYScrollBarVisible(true);
66     list_->SetScrollBarSide(SCROLL_BAR_LEFT_SIDE);
67     list_->SetReboundSize(50); // 50: rebound size
68 }
69 
SetAdapterData()70 void UITestScrollBar::SetAdapterData()
71 {
72     if (adapterData_ == nullptr) {
73         adapterData_ = new List<const char*>();
74         adapterData_->PushBack("abcd0");
75         adapterData_->PushBack("abcd1");
76         adapterData_->PushBack("abcd2");
77         adapterData_->PushBack("abcd3");
78         adapterData_->PushBack("abcd4");
79         adapterData_->PushBack("abcd5");
80         adapterData_->PushBack("abcd6");
81         adapterData_->PushBack("abcd7");
82         adapterData_->PushBack("abcd8");
83         adapterData_->PushBack("abcd9");
84         adapterData_->PushBack("abcd10");
85         adapterData_->PushBack("abcd11");
86         adapterData_->PushBack("abcd12");
87         adapterData_->PushBack("abcd13");
88         adapterData_->PushBack("abcd14");
89         adapterData_->PushBack("abcd15");
90     }
91 }
92 
TearDown()93 void UITestScrollBar::TearDown()
94 {
95     DeleteChildren(container_);
96     container_ = nullptr;
97     scrollView_ = nullptr;
98     list_ = nullptr;
99     foreImg1_ = nullptr;
100     foreImg2_ = nullptr;
101     if (adapterData_ != nullptr) {
102         adapterData_->Clear();
103         delete adapterData_;
104         adapterData_ = nullptr;
105     }
106     if (adapter_ != nullptr) {
107         delete adapter_;
108         adapter_ = nullptr;
109     }
110 
111     BaseGfxEngine::GetInstance()->SetScreenShape(ScreenShape::RECTANGLE);
112 }
113 
GetTestView()114 const UIView* UITestScrollBar::GetTestView()
115 {
116     UIKitScrollBarTESTScrollview001();
117     UIKitScrollBarTESTList001();
118     return container_;
119 }
120 
UIKitScrollBarTESTScrollview001()121 void UITestScrollBar::UIKitScrollBarTESTScrollview001()
122 {
123     UILabelButton* label1 = new UILabelButton();
124     scrollView_->Add(label1);
125     label1->LayoutTopOfParent(LABEL_INTERVAL);
126     label1->LayoutLeftOfParent(LABEL_LEFT_INTERVAL);
127     label1->SetWidth(LABEL_WIDTH);
128     label1->SetHeight(LABEL_HEIGHT);
129     label1->SetViewId("label1");
130     label1->SetText("label1");
131 
132     UILabelButton* label2 = new UILabelButton();
133     scrollView_->Add(label2);
134     label2->LayoutBottomToSibling("label1", LABEL_INTERVAL);
135     label2->LayoutLeftOfParent(LABEL_LEFT_INTERVAL);
136     label2->SetWidth(LABEL_WIDTH);
137     label2->SetHeight(LABEL_HEIGHT);
138     label2->SetViewId("label2");
139     label2->SetText("label2");
140 
141     UILabelButton* label3 = new UILabelButton();
142     scrollView_->Add(label3);
143     label3->LayoutBottomToSibling("label2", LABEL_INTERVAL);
144     label3->LayoutLeftOfParent(LABEL_LEFT_INTERVAL);
145     label3->SetWidth(LABEL_WIDTH);
146     label3->SetHeight(LABEL_HEIGHT);
147     label3->SetViewId("label3");
148     label3->SetText("label3");
149 
150     UILabelButton* label4 = new UILabelButton();
151     scrollView_->Add(label4);
152     label4->LayoutBottomToSibling("label3", LABEL_INTERVAL);
153     label4->LayoutLeftOfParent(LABEL_LEFT_INTERVAL);
154     label4->SetWidth(LABEL_WIDTH);
155     label4->SetHeight(LABEL_HEIGHT);
156     label4->SetViewId("label4");
157     label4->SetText("label4");
158 
159     UILabelButton* label5 = new UILabelButton();
160     scrollView_->Add(label5);
161     label5->LayoutBottomToSibling("label4", LABEL_INTERVAL);
162     label5->LayoutLeftOfParent(LABEL_LEFT_INTERVAL);
163     label5->SetWidth(LABEL_WIDTH);
164     label5->SetHeight(LABEL_HEIGHT);
165     label5->SetViewId("label5");
166     label5->SetText("label5");
167 
168     foreImg1_->SetSrc(TEST_CIRCLE_FORE_IMAGE);
169     container_->Add(foreImg1_);
170     foreImg1_->SetPosition(0, 0);
171 }
172 
UIKitScrollBarTESTList001()173 void UITestScrollBar::UIKitScrollBarTESTList001()
174 {
175     adapter_->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
176     adapter_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
177     adapter_->SetHeight(LABEL_HEIGHT);
178     adapter_->SetWidth(VIEW_WIDTH);
179     adapter_->SetData(adapterData_);
180 
181     list_->SetAdapter(adapter_);
182     list_->SetIntercept(true);
183     list_->SetYScrollBarVisible(true);
184 
185     foreImg2_->SetSrc(TEST_CIRCLE_FORE_IMAGE);
186     container_->Add(foreImg2_);
187     foreImg2_->SetPosition(450, 0); // 450: x
188 }
189 } // namespace OHOS
190