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_app.h"
17
18 #include "auto_test_app.h"
19 #include "compare_tools.h"
20 #include "test_resource_config.h"
21 #include "ui_auto_test.h"
22 #include "ui_test.h"
23 #include "ui_test_group.h"
24
25 namespace OHOS {
26 #ifdef _WIN32
AutoTestThread(LPVOID)27 DWORD AutoTestThread(LPVOID)
28 #elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
29 void* AutoTestThread(void*)
30 #endif // _WIN32
31 {
32 #ifdef _WIN32
33 const char logPath[] = ".\\auto_test_log.txt";
34 CompareTools::SetLogPath(logPath, sizeof(logPath));
35 #else
36 const char logPath[] = "./auto_test_log.txt";
37 CompareTools::SetLogPath(logPath, sizeof(logPath));
38 #endif
39 // 装载用例
40 UIAutoTest::SetUpTestCase();
41 AutoTestApp::GetInstance()->Start();
42 AutoTestCaseGroup::TearDownTestCase();
43 CompareTools::UnsetLogPath();
44 }
45
GetInstance()46 UITestApp* UITestApp::GetInstance()
47 {
48 static UITestApp instance;
49 return &instance;
50 }
51
Start()52 void UITestApp::Start()
53 {
54 if (rootView_ == nullptr) {
55 rootView_ = RootView::GetInstance();
56 rootView_->SetPosition(0, 0);
57 rootView_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
58 if (mainMenu_ == nullptr) {
59 mainMenu_ = new UIViewGroup();
60 mainMenu_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
61 rootView_->Add(mainMenu_);
62 rootView_->Invalidate();
63 }
64 }
65 Init();
66 }
67
68 class BtnOnClickOnAutoTestListener : public UIView::OnClickListener {
69 public:
BtnOnClickOnAutoTestListener()70 BtnOnClickOnAutoTestListener() {}
~BtnOnClickOnAutoTestListener()71 virtual ~BtnOnClickOnAutoTestListener() {}
72
OnClick(UIView & view,const ClickEvent & event)73 bool OnClick(UIView& view, const ClickEvent& event) override
74 {
75 ThreadCreate(AutoTestThread, nullptr, nullptr);
76 }
77 };
78
Init()79 void UITestApp::Init()
80 {
81 InitBackBtn();
82 InitTestLabel();
83 InitMainMenu();
84 }
85
InitMainMenu()86 void UITestApp::InitMainMenu()
87 {
88 if (mainMenu_ != nullptr) {
89 if (testLabel_ == nullptr) {
90 testLabel_ = new UILabel();
91 testLabel_->Resize(300, BACK_BUTTON_HEIGHT); // 300: test Label width;
92 testLabel_->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
93 testLabel_->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 0);
94 testLabel_->SetText("Test Demo");
95 testLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: means font size
96 mainMenu_->Add(testLabel_);
97 }
98 if (autoTestBtn_ == nullptr) {
99 autoTestBtn_ = new UILabelButton();
100 autoTestBtn_->Resize(163, 64); // 163: button width; 64: button height
101 autoTestBtn_->SetPosition(Screen::GetInstance().GetWidth() - autoTestBtn_->GetWidth(), 0);
102 autoTestBtn_->SetText("自动测试");
103 auto listern = new BtnOnClickOnAutoTestListener();
104 autoTestBtn_->SetOnClickListener(listern);
105 autoTestBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
106 autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
107 autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
108 autoTestBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
109 autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
110 autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
111 autoTestBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
112 mainMenu_->Add(autoTestBtn_);
113 }
114 if ((mainList_ == nullptr) && (adapter_ == nullptr)) {
115 uint8_t deltaHeight = 60; // 60: UIList height(64) - first button border width(4)
116 constexpr uint8_t margin = 24; // 24: x-coordinate
117 mainList_ = new UIList(UIList::VERTICAL);
118 mainList_->SetPosition(margin, deltaHeight);
119 mainList_->Resize(Screen::GetInstance().GetWidth() - margin,
120 Screen::GetInstance().GetHeight() - deltaHeight);
121 mainList_->SetThrowDrag(true);
122 mainList_->SetReboundSize(50); // 50: rebound size
123 mainList_->SetViewId(UI_TEST_MAIN_LIST_ID);
124 mainList_->SetYScrollBarVisible(true);
125 if (backBtn_ == nullptr) {
126 InitBackBtn();
127 }
128 if (testCaseLabel_ == nullptr) {
129 InitTestLabel();
130 }
131 adapter_ = new TestCaseListAdapter(mainMenu_, backBtn_, testCaseLabel_);
132 UITestGroup::SetUpTestCase();
133 mainList_->SetAdapter(adapter_);
134 mainMenu_->Add(mainList_);
135 }
136 }
137 }
138
InitBackBtn()139 void UITestApp::InitBackBtn()
140 {
141 if (backBtn_ == nullptr) {
142 backBtn_ = new UILabelButton();
143 backBtn_->SetPosition(0, 0);
144 backBtn_->Resize(163, 64); // 163: button width; 64: button height
145 backBtn_->SetText("Back");
146 backBtn_->SetViewId(UI_TEST_BACK_BUTTON_ID);
147 backBtn_->SetLabelPosition(72, 0); // 72: button label x-coordinate
148 backBtn_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24); // 24: means font size
149 backBtn_->SetImageSrc(TEST_BACK_LEFT_ARROW, TEST_BACK_LEFT_ARROW);
150 // 27: button Image x-coordinate; 18: half px of image height
151 backBtn_->SetImagePosition(27, BACK_BUTTON_HEIGHT / 2 - 18);
152 backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::RELEASED);
153 backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::PRESSED);
154 backBtn_->SetStyleForState(STYLE_BORDER_RADIUS, 0, UIButton::INACTIVE);
155 backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::RELEASED);
156 backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::PRESSED);
157 backBtn_->SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
158 }
159 }
160
InitTestLabel()161 void UITestApp::InitTestLabel()
162 {
163 if (testCaseLabel_ == nullptr) {
164 testCaseLabel_ = new UILabel();
165 testCaseLabel_->Resize(Screen::GetInstance().GetWidth(), BACK_BUTTON_HEIGHT);
166 testCaseLabel_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
167 testCaseLabel_->SetText("Test Case Name");
168 testCaseLabel_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: means font size
169 }
170 }
171
~UITestApp()172 UITestApp::~UITestApp()
173 {
174 if (adapter_ != nullptr) {
175 delete adapter_;
176 adapter_ = nullptr;
177 }
178 if (mainList_ != nullptr) {
179 delete mainList_;
180 mainList_ = nullptr;
181 }
182 if (backBtn_ != nullptr) {
183 delete backBtn_;
184 backBtn_ = nullptr;
185 }
186 if (rootView_ != nullptr) {
187 rootView_ = nullptr;
188 }
189 if (autoTestBtn_ != nullptr) {
190 auto listener = autoTestBtn_->GetOnClickListener();
191 if (listener != nullptr) {
192 delete listener;
193 autoTestBtn_->SetOnClickListener(nullptr);
194 }
195 delete autoTestBtn_;
196 autoTestBtn_ = nullptr;
197 }
198 if (mainMenu_ != nullptr) {
199 delete mainMenu_;
200 mainMenu_ = nullptr;
201 }
202 }
203 } // namespace OHOS
204