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 "test_case_list_adapter.h"
17 #include "common/screen.h"
18 #include "components/ui_button.h"
19 #include "components/ui_label_button.h"
20 #include "gfx_utils/list.h"
21 #include "test_resource_config.h"
22 #include "ui_test.h"
23 #include "ui_test_group.h"
24
25 namespace OHOS {
26 namespace {
27 const uint16_t TESTCASE_BUTTON_HEIGHT = 64;
28 const uint16_t STYLE_BORDER_WIDTH_VALUE = 4;
29 const uint16_t STYLE_BORDER_RADIUS_VALUE = 12;
30 const char* g_uiTestId = "arkui_ui_lite_test_case_id";
31 } // namespace
32
GetCount()33 uint16_t TestCaseListAdapter::GetCount()
34 {
35 return UITestGroup::GetTestCase().Size();
36 }
37
38 class BtnOnClickBackListener : public UIView::OnClickListener {
39 public:
BtnOnClickBackListener(UIViewGroup * uiView,UIViewGroup * mainMenu,UITest * uiTest,UILabel * testCaseLabel)40 BtnOnClickBackListener(UIViewGroup* uiView,
41 UIViewGroup* mainMenu,
42 UITest* uiTest,
43 UILabel* testCaseLabel)
44 : rootView_(uiView), mainMenu_(mainMenu), uiTest_(uiTest), testCaseLabel_(testCaseLabel)
45 {
46 }
47
~BtnOnClickBackListener()48 ~BtnOnClickBackListener() {}
49
OnClick(UIView & view,const ClickEvent & event)50 bool OnClick(UIView& view, const ClickEvent& event) override
51 {
52 if ((rootView_ == nullptr) || (mainMenu_ == nullptr) || (uiTest_ == nullptr) || (testCaseLabel_ == nullptr)) {
53 return false;
54 }
55
56 rootView_->Remove(testCaseLabel_);
57 rootView_->Remove(&view);
58
59 UIView* tempView = rootView_->GetChildById(g_uiTestId);
60 if (tempView != nullptr) {
61 rootView_->Remove(tempView);
62 uiTest_->TearDown();
63 }
64
65 rootView_->Add(mainMenu_);
66 rootView_->Invalidate();
67 return true;
68 }
69
70 private:
71 UIViewGroup* rootView_;
72 UIViewGroup* mainMenu_;
73 UITest* uiTest_;
74 UILabel* testCaseLabel_;
75 };
76
77 class BtnOnClickUiTestListener : public UIView::OnClickListener {
78 public:
BtnOnClickUiTestListener(UIViewGroup * uiView,UIViewGroup * mainMenu_,UILabelButton * backBtn,TestCaseInfo * uiTestInfo,UILabel * testCaseLabel)79 BtnOnClickUiTestListener(UIViewGroup* uiView,
80 UIViewGroup* mainMenu_,
81 UILabelButton* backBtn,
82 TestCaseInfo* uiTestInfo,
83 UILabel* testCaseLabel)
84 : rootView_(uiView),
85 mainMenu_(mainMenu_),
86 backBtn_(backBtn),
87 testCaseLabel_(testCaseLabel),
88 uiTest_(nullptr),
89 sliceId_(nullptr)
90 {
91 if (uiTestInfo != nullptr) {
92 uiTest_ = uiTestInfo->testObj;
93 sliceId_ = uiTestInfo->sliceId;
94 }
95 }
~BtnOnClickUiTestListener()96 ~BtnOnClickUiTestListener() {}
OnClick(UIView & view,const ClickEvent & event)97 bool OnClick(UIView& view, const ClickEvent& event) override
98 {
99 if ((rootView_ == nullptr) || (mainMenu_ == nullptr) || (backBtn_ == nullptr) || (testCaseLabel_ == nullptr) ||
100 (uiTest_ == nullptr) || (sliceId_ == nullptr)) {
101 return false;
102 }
103 rootView_->Remove(mainMenu_);
104
105 UIView::OnClickListener* click = backBtn_->GetOnClickListener();
106 if (click != nullptr) {
107 delete click;
108 click = nullptr;
109 }
110 click = new BtnOnClickBackListener(rootView_, mainMenu_, uiTest_, testCaseLabel_);
111 backBtn_->SetOnClickListener(click);
112 rootView_->Add(backBtn_);
113 if (testCaseLabel_ != nullptr) {
114 testCaseLabel_->SetText(sliceId_);
115 }
116 rootView_->Add(testCaseLabel_);
117
118 uiTest_->SetUp();
119 UIView* tempView = const_cast<UIView*>(uiTest_->GetTestView());
120 if (tempView != nullptr) {
121 tempView->SetViewId(g_uiTestId);
122 tempView->SetPosition(tempView->GetX(), tempView->GetY() + backBtn_->GetHeight());
123 rootView_->Add(tempView);
124 }
125 rootView_->Invalidate();
126 return true;
127 }
128
129 private:
130 UIViewGroup* rootView_;
131 UIViewGroup* mainMenu_;
132 UILabelButton* backBtn_;
133 UILabel* testCaseLabel_;
134 UITest* uiTest_;
135 const char* sliceId_;
136 };
137
GetView(UIView * inView,int16_t index)138 UIView* TestCaseListAdapter::GetView(UIView* inView, int16_t index)
139 {
140 List<TestCaseInfo> testCaseList = UITestGroup::GetTestCase();
141 if (testCaseList.IsEmpty()) {
142 return nullptr;
143 }
144 if ((index > testCaseList.Size() - 1) || (index < 0)) {
145 return nullptr;
146 }
147 UILabelButton* item = nullptr;
148 if (inView == nullptr) {
149 item = new UILabelButton();
150 item->SetPosition(0, 0);
151 item->SetStyleForState(STYLE_BORDER_WIDTH, STYLE_BORDER_WIDTH_VALUE, UIButton::RELEASED);
152 item->SetStyleForState(STYLE_BORDER_WIDTH, STYLE_BORDER_WIDTH_VALUE, UIButton::PRESSED);
153 item->SetStyleForState(STYLE_BORDER_WIDTH, STYLE_BORDER_WIDTH_VALUE, UIButton::INACTIVE);
154 item->SetStyleForState(STYLE_BORDER_OPA, 0, UIButton::RELEASED);
155 item->SetStyleForState(STYLE_BORDER_OPA, 0, UIButton::PRESSED);
156 item->SetStyleForState(STYLE_BORDER_OPA, 0, UIButton::INACTIVE);
157 item->Resize(Screen::GetInstance().GetWidth() - TEXT_DISTANCE_TO_LEFT_SIDE, TESTCASE_BUTTON_HEIGHT);
158 } else {
159 item = static_cast<UILabelButton*>(inView);
160 }
161
162 UIView::OnClickListener* listener = item->GetOnClickListener();
163 if (listener != nullptr) {
164 delete listener;
165 listener = nullptr;
166 }
167 ListNode<TestCaseInfo>* node = testCaseList.Begin();
168 for (uint16_t i = 0; i < index; i++) {
169 node = node->next_;
170 }
171 listener = new BtnOnClickUiTestListener(rootView_, mainMenu_, backBtn_, &node->data_, testCaseLabel_);
172 item->SetOnClickListener(listener);
173 item->SetText(node->data_.sliceId);
174 item->SetViewId(node->data_.sliceId);
175 item->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
176 item->SetViewIndex(index);
177 item->SetAlign(TEXT_ALIGNMENT_LEFT);
178 item->SetLabelPosition(24, 0); // 24: lable x-coordinate
179 item->SetImageSrc(TEST_RIGHT_ARROW, TEST_RIGHT_ARROW);
180 // 2: half of button height; 18: half px of image height
181 item->SetImagePosition(item->GetWidth() - TEXT_DISTANCE_TO_LEFT_SIDE, TESTCASE_BUTTON_HEIGHT / 2 - 18);
182 item->SetStyleForState(STYLE_BORDER_RADIUS, STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
183 item->SetStyleForState(STYLE_BORDER_RADIUS, STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
184 item->SetStyleForState(STYLE_BORDER_RADIUS, STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
185 item->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
186 item->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
187 item->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
188 return item;
189 }
190
GetItemWidthWithMargin(int16_t index)191 int16_t TestCaseListAdapter::GetItemWidthWithMargin(int16_t index)
192 {
193 // 2: two borders on both sides
194 return Screen::GetInstance().GetWidth() - TEXT_DISTANCE_TO_LEFT_SIDE + STYLE_BORDER_WIDTH_VALUE * 2;
195 }
196
GetItemHeightWithMargin(int16_t index)197 int16_t TestCaseListAdapter::GetItemHeightWithMargin(int16_t index)
198 {
199 return TESTCASE_BUTTON_HEIGHT + STYLE_BORDER_WIDTH_VALUE * 2; // 2: two borders on both sides
200 }
201 } // namespace OHOS
202