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_auto_test.h"
17
18 #include <sys/time.h>
19 #include "auto_test_manager.h"
20 #include "components/root_view.h"
21 #include "components/ui_view_group.h"
22 #include "dfx/event_injector.h"
23 #include "ui_test_app.h"
24 #include "ui_test_group.h"
25
26 namespace OHOS {
UIAutoTest()27 UIAutoTest::UIAutoTest()
28 {
29 }
30
~UIAutoTest()31 UIAutoTest::~UIAutoTest()
32 {
33 }
34
Reset(std::string testID) const35 void UIAutoTest::Reset(std::string testID) const
36 {
37 ResetMainMenu();
38 ClickViewById(testID.c_str());
39 }
40
ResetMainMenu() const41 void UIAutoTest::ResetMainMenu() const
42 {
43 while (RootView::GetInstance()->GetChildById(UI_TEST_MAIN_LIST_ID) == nullptr) {
44 ClickViewById(UI_TEST_BACK_BUTTON_ID);
45 }
46 }
47
EnterSubMenu(const char * id) const48 void UIAutoTest::EnterSubMenu(const char* id) const
49 {
50 if (id == nullptr) {
51 return;
52 }
53
54 UIView* view = RootView::GetInstance()->GetChildById(id);
55 if (view == nullptr) {
56 UIView* listView = RootView::GetInstance()->GetChildById(UI_TEST_MAIN_LIST_ID);
57 if (listView == nullptr) {
58 return;
59 }
60 ListNode<TestCaseInfo>* node = UITestGroup::GetTestCase().Begin();
61 while (node != UITestGroup::GetTestCase().End()) {
62 if ((node->data_.sliceId != nullptr) && (strcmp(id, node->data_.sliceId) == 0)) {
63 UITestGroup::GetTestCase().PushFront(node->data_);
64 UITestGroup::GetTestCase().Remove(node);
65 break;
66 }
67 node = node->next_;
68 }
69 reinterpret_cast<UIList*>(listView)->RefreshList();
70 CompareTools::WaitSuspend();
71 }
72
73 ClickViewById(id);
74 }
75
ClickViewById(const char * id) const76 void UIAutoTest::ClickViewById(const char* id) const
77 {
78 if (id == nullptr) {
79 return;
80 }
81 UIView* view = RootView::GetInstance()->GetChildById(id);
82 if (view == nullptr) {
83 return;
84 }
85 Point point;
86 point.x = view->GetOrigRect().GetX();
87 point.y = view->GetOrigRect().GetY();
88 EventInjector::GetInstance()->SetClickEvent(point);
89 CompareTools::WaitSuspend();
90 }
91
DragViewToHead(const char * id) const92 void UIAutoTest::DragViewToHead(const char* id) const
93 {
94 if (id == nullptr) {
95 return;
96 }
97 UIView* view = RootView::GetInstance()->GetChildById(id);
98 if (view == nullptr) {
99 return;
100 }
101 Point startPoint;
102 startPoint.x = view->GetOrigRect().GetX();
103 startPoint.y = view->GetOrigRect().GetY();
104
105 Point endPoint;
106 endPoint.x = 100; // 100 :end point x position;
107 endPoint.y = 100; // 100 :end point y position;
108 EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 100); // 100: drag time
109 CompareTools::WaitSuspend();
110 }
111
DrageToView(const char * id,int16_t x,int16_t y) const112 void UIAutoTest::DrageToView(const char* id, int16_t x, int16_t y) const
113 {
114 if (id == nullptr) {
115 return;
116 }
117 UIView* view = RootView::GetInstance()->GetChildById(id);
118 if (view == nullptr) {
119 return;
120 }
121 Point startPoint;
122 startPoint.x = view->GetOrigRect().GetX();
123 startPoint.y = view->GetOrigRect().GetY();
124
125 Point endPoint;
126 endPoint.x = x;
127 endPoint.y = y;
128 EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 100); // 100: drag time
129
130 uint16_t waitTime = std::abs((std::abs(x) > std::abs(y) ? x : y) * WAIT_TIME_MUBLITE) + DRAGE_DEFAULT_WAIT_TIME;
131 CompareTools::WaitSuspend(waitTime);
132 }
133
CompareByBinary(const char * fileName) const134 bool UIAutoTest::CompareByBinary(const char* fileName) const
135 {
136 if (fileName == nullptr) {
137 return false;
138 }
139 char filePath[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
140 CompareTools::StrnCatPath(filePath, DEFAULT_FILE_NAME_MAX_LENGTH, fileName, strlen(fileName));
141 if (CompareTools::CheckFileExist(filePath, sizeof(filePath))) {
142 return CompareTools::CompareFile(filePath, sizeof(filePath));
143 } else {
144 return CompareTools::SaveFile(filePath, sizeof(filePath));
145 }
146 }
147
RunTest(std::vector<std::shared_ptr<TestMsgInfo>> msgInfo)148 void UIAutoTest::RunTest(std::vector<std::shared_ptr<TestMsgInfo>> msgInfo)
149 {
150 printf("UIAutoTest::RunTest----testInfo.size=[%d]\n", msgInfo.size());
151 fflush(stdout);
152 for (auto it: msgInfo) {
153 OnTest(it);
154 }
155
156 AutoTestManager::GetInstance()->SendMsg(C_S_MAIN_ID_TEST_FINISH_INFO);
157 }
158
OnTest(std::shared_ptr<TestMsgInfo> info)159 void UIAutoTest::OnTest(std::shared_ptr<TestMsgInfo> info)
160 {
161 ResetMainMenu();
162 OnEnterPage(info->pageNav);
163 OnTestBySteps(info->steps, info->className);
164 }
165
OnTestBySteps(std::vector<TestSteps> steps,std::string className)166 void UIAutoTest::OnTestBySteps(std::vector<TestSteps> steps, std::string className)
167 {
168 if (steps.empty()) {
169 return;
170 }
171
172 int stepIndex = 0;
173 for (auto it: steps) {
174 OnTestOneStep(it, className, stepIndex++);
175 }
176 }
177
OnTestOneStep(TestSteps step,std::string className,size_t stepIndex)178 void UIAutoTest::OnTestOneStep(TestSteps step, std::string className, size_t stepIndex)
179 {
180 if (step.eventID == TestEventID::TEST_CLICK_EVENT) {
181 ClickViewById(step.viewID.c_str());
182 } else if (step.eventID == TestEventID::TEST_MOVE_EVENT) {
183 if (step.eventValue.size() < EVENT_VALUE_SIZE_TWO) {
184 return;
185 }
186
187 int16_t x = static_cast<int16_t>(step.eventValue[0]);
188 int16_t y = static_cast<int16_t>(step.eventValue[1]);
189 DrageToView(step.viewID.c_str(), x, y);
190 }
191
192 if (step.saveCheckPoint) {
193 OnSaveFile(className, step.viewID, stepIndex);
194 }
195 }
196
OnEnterPage(std::vector<std::string> pageNav)197 void UIAutoTest::OnEnterPage(std::vector<std::string> pageNav)
198 {
199 if (pageNav.empty()) {
200 return;
201 }
202
203 for (auto it: pageNav) {
204 EnterSubMenu(it.c_str());
205 }
206 }
207
OnSaveFile(std::string className,std::string viewID,size_t stepIndex)208 void UIAutoTest::OnSaveFile(std::string className, std::string viewID, size_t stepIndex)
209 {
210 std::string fileName = className + "@" + viewID + "@" + std::to_string(stepIndex) + ".bmp";
211 fileNames_.push_back(fileName);
212
213 std::string filePath;
214 std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
215 if (config->testMode == TestMode::TEST_MODE_BASE) {
216 filePath = config->baseDir + fileName;
217 } else if (config->testMode == TestMode::TEST_MODE_RUN) {
218 filePath = config->runDir + fileName;
219 }
220
221 printf("OnSaveFile, filePath = %s\n", filePath.c_str());
222 fflush(stdout);
223 CompareTools::SaveFile(filePath.c_str(), strlen(filePath.c_str()));
224 }
225
TestComplete() const226 void UIAutoTest::TestComplete() const
227 {
228 printf("UIAutoTest::TestComplete");
229 fflush(stdout);
230 std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
231 if (!config) {
232 return;
233 }
234 if (config->testMode != TestMode::TEST_MODE_RUN) {
235 return;
236 }
237
238 config->logDir += OnGetSystemTime();
239 config->logDir += ".txt";
240 printf("UIAutoTest::OnCompareFile--logDir=[%s]\n", config->logDir.c_str());
241 fflush(stdout);
242 for (auto it: fileNames_) {
243 OnCompareFile(it);
244 }
245 }
246
OnCompareFile(std::string fileName) const247 void UIAutoTest::OnCompareFile(std::string fileName) const
248 {
249 std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
250 if (!config) {
251 return;
252 }
253
254 std::string fileBasePath = config->baseDir + fileName;
255 std::string fileRunPath = config->runDir + fileName;
256
257 std::string log;
258 if (!CompareTools::CompareFile(fileBasePath.c_str(), fileRunPath.c_str())) {
259 printf("UIAutoTest::OnCompareFile----different\n");
260 fflush(stdout);
261 log = "[FAIL]:[" + fileName + "]\n";
262 } else {
263 log = "[SUCESS]:[" + fileName + "]\n";
264 }
265
266 CompareTools::SaveResultLog(config->logDir.c_str(), log.c_str(), strlen(log.c_str()));
267 }
268
OnGetSystemTime() const269 std::string UIAutoTest::OnGetSystemTime() const
270 {
271 time_t t = time(0);
272 char tmp[32] = { 0 };
273 strftime(tmp, sizeof(tmp), "%Y-%m-%d-%H-%M-%S", localtime(&t));
274
275 std::string loctime = tmp;
276
277 return loctime;
278 }
279 } // namespace OHOS
280