• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (autoTestBtn_ == nullptr) {
65             autoTestBtn_ = new UILabelButton();
66             autoTestBtn_->Resize(163, 64); // 163: button width; 64: button height
67             autoTestBtn_->SetPosition(Screen::GetInstance().GetWidth() - autoTestBtn_->GetWidth(), 0);
68             autoTestBtn_->SetText("自动测试");
69             auto listern = new UIView::OnClickListener();
70             autoTestBtn_->SetOnClickListener(listern);
71             autoTestBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
72             autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
73             autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
74             autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
75             autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
76             autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
77             autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
78             autoTestBtn_->SetVisible(false);
79             mainMenu_->Add(autoTestBtn_);
80         }
81         if ((mainList_ == nullptr) && (adapter_ == nullptr)) {
82             uint8_t deltaHeight = 60; // 60: UIList height(64) - first button border width(4)
83             constexpr uint8_t margin = 24; // 24: x-coordinate
84             mainList_ = new UIList(UIList::VERTICAL);
85             mainList_->SetPosition(margin, deltaHeight);
86             mainList_->Resize(Screen::GetInstance().GetWidth() - margin,
87                 Screen::GetInstance().GetHeight() - deltaHeight);
88             mainList_->SetThrowDrag(true);
89             mainList_->SetReboundSize(50); // 50: rebound size
90             mainList_->SetViewId(UI_TEST_MAIN_LIST_ID);
91             mainList_->SetYScrollBarVisible(true);
92             if (backBtn_ == nullptr) {
93                 InitBackBtn();
94             }
95             if (testCaseLabel_ == nullptr) {
96                 InitTestLabel();
97             }
98             adapter_ = new TestCaseListAdapter(mainMenu_, backBtn_, testCaseLabel_);
99             UITestGroup::SetUpTestCase();
100             mainList_->SetAdapter(adapter_);
101             mainMenu_->Add(mainList_);
102         }
103     }
104 }
105 
InitBackBtn()106 void UITestApp::InitBackBtn()
107 {
108     if (backBtn_ == nullptr) {
109         backBtn_ = new UILabelButton();
110         backBtn_->SetPosition(0, 0);
111         backBtn_->Resize(163, 64); // 163: button width; 64: button height
112         backBtn_->SetText("Back");
113         backBtn_->SetViewId(UI_TEST_BACK_BUTTON_ID);
114         backBtn_->SetLabelPosition(72, 0);                   // 72: button label x-coordinate
115         backBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
116         backBtn_->SetImageSrc(TEST_BACK_LEFT_ARROW, TEST_BACK_LEFT_ARROW);
117         // 27: button Image x-coordinate; 18: half px of image height
118         backBtn_->SetImagePosition(27, BACK_BUTTON_HEIGHT / 2 - 18);
119         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
120         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
121         backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
122         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
123         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
124         backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
125     }
126 }
127 
InitTestLabel()128 void UITestApp::InitTestLabel()
129 {
130     if (testCaseLabel_ == nullptr) {
131         testCaseLabel_ = new UILabel();
132         testCaseLabel_->Resize(Screen::GetInstance().GetWidth(), BACK_BUTTON_HEIGHT);
133         testCaseLabel_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
134         testCaseLabel_->SetText("Test Case Name");
135         testCaseLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: means font size
136     }
137 }
138 
~UITestApp()139 UITestApp::~UITestApp()
140 {
141     if (adapter_ != nullptr) {
142         delete adapter_;
143         adapter_ = nullptr;
144     }
145     if (mainList_ != nullptr) {
146         delete mainList_;
147         mainList_ = nullptr;
148     }
149     if (backBtn_ != nullptr) {
150         delete backBtn_;
151         backBtn_ = nullptr;
152     }
153     rootView_ = nullptr;
154     if (autoTestBtn_ != nullptr) {
155         auto listener = autoTestBtn_->GetOnClickListener();
156         if (listener != nullptr) {
157             delete listener;
158             autoTestBtn_->SetOnClickListener(nullptr);
159         }
160         delete autoTestBtn_;
161         autoTestBtn_ = nullptr;
162     }
163     if (mainMenu_ != nullptr) {
164         delete mainMenu_;
165         mainMenu_ = nullptr;
166     }
167 }
168 } // namespace OHOS
169