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_chart_pillar.h"
17 #include "common/screen.h"
18 
19 namespace OHOS {
20 namespace {
21     static int16_t g_blank = 20;
22 }
23 
SetUp()24 void UITestChartPillar::SetUp()
25 {
26     if (container_ == nullptr) {
27         container_ = new UIScrollView();
28         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
29         container_->SetThrowDrag(true);
30     }
31 
32     dataSerial_[0] = new UIChartDataSerial();
33     dataSerial_[0]->SetMaxDataCount(5); // 5: number of data points
34     Point pointArray[5] = {{0, 2478}, {1, 2600}, {2, 3000}, {3, 3200}, {4, 3500}};
35     dataSerial_[0]->AddPoints(pointArray, 5); // 5: number of data points
36     dataSerial_[0]->SetFillColor(Color::Red());
37 
38     dataSerial_[1] = new UIChartDataSerial();
39     dataSerial_[1]->SetMaxDataCount(5); // 5: number of data points
40     Point pointArray1[5] = {{0, 2000}, {1, 0}, {2, 800}, {3, 700}, {4, 433}};
41     dataSerial_[1]->AddPoints(pointArray1, 5); // 5: number of data points
42     dataSerial_[1]->SetFillColor(Color::Green());
43 
44     dataSerial_[2] = new UIChartDataSerial(); // 2 array index
45     dataSerial_[2]->SetMaxDataCount(5); // 2 array index, 5: number of data points
46     Point pointArray2[5] = {{0, 100}, {1, 200}, {2, 300}, {3, 400}, {4, 500}};
47     dataSerial_[2]->AddPoints(pointArray2, 5); // 2 array index, 5: number of data points
48     dataSerial_[2]->SetFillColor(Color::Blue()); // 2 array index
49     curDataIndex_ = 0;
50 }
51 
InnerDeleteChildren(UIView * view) const52 void UITestChartPillar::InnerDeleteChildren(UIView* view) const
53 {
54     if (view == nullptr) {
55         return;
56     }
57     while (view != nullptr) {
58         UIView* tempView = view;
59         view = view->GetNextSibling();
60         if (tempView->IsViewGroup()) {
61             InnerDeleteChildren(static_cast<UIViewGroup*>(tempView)->GetChildrenHead());
62         }
63         if (tempView->GetViewType() == UI_AXIS) {
64             return;
65         }
66         if (tempView->GetParent()) {
67             static_cast<UIViewGroup*>(tempView->GetParent())->Remove(tempView);
68         }
69         delete tempView;
70     }
71 }
72 
TearDown()73 void UITestChartPillar::TearDown()
74 {
75     chart_->ClearDataSerial();
76     for (uint8_t i = 0; i < DATA_NUM; i++) {
77         delete dataSerial_[i];
78         dataSerial_[i] = nullptr;
79     }
80     InnerDeleteChildren(container_);
81     container_ = nullptr;
82     lastX_ = 0;
83     lastY_ = 0;
84     positionX_ = 0;
85     positionY_ = 0;
86 }
87 
GetTestView()88 const UIView* UITestChartPillar::GetTestView()
89 {
90     UIKitChartPillarTestAddDataSerial001();
91     UIKitChartPillarTestEnableReverse002();
92     UIKitChartPillarTestSetAxisLineColor003();
93     UIKitChartPillarTestSetAxisLineVisible004();
94     return container_;
95 }
96 
UIKitChartPillarTestAddDataSerial001()97 void UITestChartPillar::UIKitChartPillarTestAddDataSerial001()
98 {
99     UILabel* label = GetTitleLabel("UIChartPillar效果 ");
100     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
101     container_->Add(label);
102 
103     chart_ = new UIChartPillar();
104     chart_->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE);
105     chart_->SetWidth(454); // 454: width
106     chart_->SetHeight(250); // 250: height
107 
108     UIXAxis& xAxis = chart_->GetXAxis();
109     UIYAxis& yAxis = chart_->GetYAxis();
110     xAxis.SetMarkNum(5); // 5: number of scales
111     yAxis.SetDataRange(0, 5000); // 0: minimum value, 5000: maximum value
112 
113     chart_->AddDataSerial(dataSerial_[0]);
114     curDataIndex_++;
115     container_->Add(chart_);
116     SetLastPos(chart_);
117 
118     addDataSerialBtn_ = new UILabelButton();
119     deleteDataSerialBtn_ = new UILabelButton();
120     clearDataSerialBtn_ = new UILabelButton();
121 
122     positionY_ = lastY_ + 10; // 10: increase y-coordinate
123     positionX_ = 48; // 48: x-coordinate
124     SetUpButton(addDataSerialBtn_, "添加数据 ", UI_TEST_ADD_DATA);
125 
126     positionX_ = addDataSerialBtn_->GetX() + addDataSerialBtn_->GetWidth() + g_blank;
127     positionY_ = addDataSerialBtn_->GetY();
128     SetUpButton(deleteDataSerialBtn_, "删除数据 ", UI_TEST_DELETE_DATA);
129 
130     positionX_ = deleteDataSerialBtn_->GetX() + deleteDataSerialBtn_->GetWidth() + g_blank;
131     positionY_ = deleteDataSerialBtn_->GetY();
132     SetUpButton(clearDataSerialBtn_, "清空数据 ", UI_TEST_CLEAR_DATA);
133 }
134 
UIKitChartPillarTestEnableReverse002()135 void UITestChartPillar::UIKitChartPillarTestEnableReverse002()
136 {
137     reverseBtn_ = new UILabelButton();
138     positionX_ = 48; // 48: x-coordinate
139     SetUpButton(reverseBtn_, "翻转 ", UI_TEST_FLIP);
140 }
141 
UIKitChartPillarTestSetAxisLineColor003()142 void UITestChartPillar::UIKitChartPillarTestSetAxisLineColor003()
143 {
144     setAxisColorBtn_ = new UILabelButton();
145     positionX_ = reverseBtn_->GetX() + reverseBtn_->GetWidth() + g_blank;
146     positionY_ = reverseBtn_->GetY();
147     SetUpButton(setAxisColorBtn_, "坐标轴颜色 ", UI_TEST_AXIS_COLOR);
148 }
149 
UIKitChartPillarTestSetAxisLineVisible004()150 void UITestChartPillar::UIKitChartPillarTestSetAxisLineVisible004()
151 {
152     setAxisVisibleBtn_ = new UILabelButton();
153     positionX_ = setAxisColorBtn_->GetX() + setAxisColorBtn_->GetWidth() + g_blank;
154     positionY_ = setAxisColorBtn_->GetY();
155     SetUpButton(setAxisVisibleBtn_, "坐标轴不可见", UI_TEST_AXIS_NOT_VISIBLE);
156 }
157 
OnClick(UIView & view,const ClickEvent & event)158 bool UITestChartPillar::OnClick(UIView& view, const ClickEvent& event)
159 {
160     if (&view == addDataSerialBtn_) {
161         if (curDataIndex_ >= DATA_NUM) {
162             return true;
163         }
164         chart_->AddDataSerial(dataSerial_[curDataIndex_]);
165         curDataIndex_++;
166         chart_->Invalidate();
167     } else if (&view == deleteDataSerialBtn_) {
168         if (curDataIndex_ == 0) {
169             return true;
170         }
171         chart_->DeleteDataSerial(dataSerial_[curDataIndex_ - 1]);
172         curDataIndex_--;
173         chart_->Invalidate();
174     } else if (&view == clearDataSerialBtn_) {
175         chart_->ClearDataSerial();
176         curDataIndex_ = 0;
177         chart_->Invalidate();
178     } else if (&view == reverseBtn_) {
179         chart_->EnableReverse(true);
180         chart_->Invalidate();
181     } else if (&view == setAxisColorBtn_) {
182         UIXAxis& xAxis = chart_->GetXAxis();
183         UIYAxis& yAxis = chart_->GetYAxis();
184         xAxis.SetLineColor(Color::Red());
185         yAxis.SetLineColor(Color::Red());
186         chart_->Invalidate();
187     } else if (&view == setAxisVisibleBtn_) {
188         UIXAxis& xAxis = chart_->GetXAxis();
189         UIYAxis& yAxis = chart_->GetYAxis();
190         xAxis.SetVisible(false);
191         yAxis.SetVisible(false);
192         chart_->Invalidate();
193     }
194     return true;
195 }
196 
SetUpButton(UILabelButton * btn,const char * title,const char * id)197 void UITestChartPillar::SetUpButton(UILabelButton* btn, const char* title, const char* id)
198 {
199     if (btn == nullptr || title == nullptr || id == nullptr) {
200         return;
201     }
202     container_->Add(btn);
203     btn->SetPosition(positionX_, positionY_, BUTTON_WIDHT2, BUTTON_HEIGHT2);
204     positionY_ += btn->GetHeight() + 10; // 10: increase height
205     btn->SetText(title);
206     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
207     btn->SetOnClickListener(this);
208     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
209     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
210     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
211     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
212     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
213     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
214     btn->SetViewId(id);
215     container_->Invalidate();
216 }
217 
SetLastPos(UIView * view)218 void UITestChartPillar::SetLastPos(UIView* view)
219 {
220     if (view == nullptr) {
221         return;
222     }
223     lastX_ = view->GetX();
224     lastY_ = view->GetY() + view->GetHeight();
225 }
226 } // namespace OHOS
227