1 /*
2  * Copyright (c) 2020-2022 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_app.h"
17 
18 #include "test_resource_config.h"
19 #include "ui_test.h"
20 #include "ui_test_group.h"
21 
22 namespace OHOS {
GetInstance()23 UITestApp* UITestApp::GetInstance()
24 {
25     static UITestApp instance;
26     return &instance;
27 }
28 
Start()29 void UITestApp::Start()
30 {
31     if (rootView_ == nullptr) {
32         rootView_ = RootView::GetInstance();
33         rootView_->SetPosition(0, 0);
34         rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
35         if (mainMenu_ == nullptr) {
36             mainMenu_ = new UIViewGroup();
37             mainMenu_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
38             rootView_->Add(mainMenu_);
39             rootView_->Invalidate();
40         }
41     }
42     Init();
43 }
44 
Init()45 void UITestApp::Init()
46 {
47     InitBackBtn();
48     InitTestLabel();
49     InitMainMenu();
50 }
51 
InitMainMenu()52 void UITestApp::InitMainMenu()
53 {
54     if (mainMenu_ != nullptr) {
55         if (testLabel_ == nullptr) {
56             testLabel_ = new UILabel();
57             testLabel_->Resize(300, BACK_BUTTON_HEIGHT); // 300: test Label width;
58             testLabel_->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
59             testLabel_->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 0);
60             testLabel_->SetText("Test Demo");
61             testLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: means font size
62             mainMenu_->Add(testLabel_);
63         }
64         InitAutoTestBtn();
65         if ((mainList_ == nullptr) && (adapter_ == nullptr)) {
66             uint8_t deltaHeight = 60; // 60: UIList height(64) - first button border width(4)
67             constexpr uint8_t margin = 24; // 24: x-coordinate
68             mainList_ = new UIList(UIList::VERTICAL);
69             mainList_->SetPosition(margin, deltaHeight);
70             mainList_->Resize(Screen::GetInstance().GetWidth() - margin,
71                               Screen::GetInstance().GetHeight() - deltaHeight);
72             mainList_->SetThrowDrag(true);
73             mainList_->SetReboundSize(50); // 50: rebound size
74             mainList_->SetViewId(UI_TEST_MAIN_LIST_ID);
75             mainList_->SetYScrollBarVisible(true);
76             if (backBtn_ == nullptr) {
77                 InitBackBtn();
78             }
79             if (testCaseLabel_ == nullptr) {
80                 InitTestLabel();
81             }
82             adapter_ = new TestCaseListAdapter(mainMenu_, backBtn_, testCaseLabel_);
83             UITestGroup::SetUpTestCase();
84             mainList_->SetAdapter(adapter_);
85             mainMenu_->Add(mainList_);
86         }
87     }
88 }
89 
InitAutoTestBtn()90 void UITestApp::InitAutoTestBtn()
91 {
92     if (autoTestBtn_ == nullptr) {
93         autoTestBtn_ = new UILabelButton();
94         autoTestBtn_->Resize(163, 64); // 163: button width; 64: button height
95         autoTestBtn_->SetPosition(Screen::GetInstance().GetWidth() - autoTestBtn_->GetWidth(), 0);
96         autoTestBtn_->SetText("自动测试");
97         auto listern = new UIView::OnClickListener();
98         autoTestBtn_->SetOnClickListener(listern);
99         autoTestBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
100         autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
101         autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
102         autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
103         autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
104         autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
105         autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
106         autoTestBtn_->SetVisible(false);
107         mainMenu_->Add(autoTestBtn_);
108     }
109 }
110 
InitBackBtn()111 void UITestApp::InitBackBtn()
112 {
113     if (backBtn_ == nullptr) {
114         backBtn_ = new UILabelButton();
115         backBtn_->SetPosition(0, 0);
116         backBtn_->Resize(163, 64); // 163: button width; 64: button height
117         backBtn_->SetText("Back");
118         backBtn_->SetViewId(UI_TEST_BACK_BUTTON_ID);
119         backBtn_->SetLabelPosition(72, 0);                   // 72: button label x-coordinate
120         backBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
121         backBtn_->SetImageSrc(TEST_BACK_LEFT_ARROW, TEST_BACK_LEFT_ARROW);
122         // 27: button Image x-coordinate; 18: half px of image height
123         backBtn_->SetImagePosition(27, BACK_BUTTON_HEIGHT / 2 - 18);
124         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
125         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
126         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
127         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
128         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
129         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
130     }
131 }
132 
InitTestLabel()133 void UITestApp::InitTestLabel()
134 {
135     if (testCaseLabel_ == nullptr) {
136         testCaseLabel_ = new UILabel();
137         testCaseLabel_->Resize(Screen::GetInstance().GetWidth(), BACK_BUTTON_HEIGHT);
138         testCaseLabel_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
139         testCaseLabel_->SetText("Test Case Name");
140         testCaseLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: means font size
141     }
142 }
143 
~UITestApp()144 UITestApp::~UITestApp()
145 {
146     if (adapter_ != nullptr) {
147         delete adapter_;
148         adapter_ = nullptr;
149     }
150     if (mainList_ != nullptr) {
151         delete mainList_;
152         mainList_ = nullptr;
153     }
154     if (backBtn_ != nullptr) {
155         delete backBtn_;
156         backBtn_ = nullptr;
157     }
158     rootView_ = nullptr;
159     if (autoTestBtn_ != nullptr) {
160         auto listener = autoTestBtn_->GetOnClickListener();
161         if (listener != nullptr) {
162             delete listener;
163             autoTestBtn_->SetOnClickListener(nullptr);
164         }
165         delete autoTestBtn_;
166         autoTestBtn_ = nullptr;
167     }
168     if (mainMenu_ != nullptr) {
169         delete mainMenu_;
170         mainMenu_ = nullptr;
171     }
172 }
173 } // namespace OHOS
174