ui_auto_test.cpp 7.7 KB
Newer Older
Z
zhdengc 已提交
1
/*
Y
youbing 已提交
2
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
Z
zhdengc 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ui_auto_test.h"
17

Y
youbing 已提交
18
#include "auto_test_manager.h"
Z
zhdengc 已提交
19 20
#include "components/root_view.h"
#include "components/ui_view_group.h"
Z
zhdengc 已提交
21
#include "dfx/event_injector.h"
Z
zhdengc 已提交
22 23 24 25
#include "ui_test_app.h"
#include "ui_test_group.h"

namespace OHOS {
Y
youbing 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
UIAutoTest::UIAutoTest()
{
}

UIAutoTest::~UIAutoTest()
{
}

void UIAutoTest::Reset(std::string testID) const
{
    ResetMainMenu();
    ClickViewById(testID.c_str());
}

Z
zhdengc 已提交
40
void UIAutoTest::ResetMainMenu() const
Z
zhdengc 已提交
41 42 43 44 45 46
{
    while (RootView::GetInstance()->GetChildById(UI_TEST_MAIN_LIST_ID) == nullptr) {
        ClickViewById(UI_TEST_BACK_BUTTON_ID);
    }
}

Z
zhdengc 已提交
47
void UIAutoTest::EnterSubMenu(const char* id) const
Z
zhdengc 已提交
48 49 50 51
{
    if (id == nullptr) {
        return;
    }
Y
youbing 已提交
52

Z
zhdengc 已提交
53
    UIView* view = RootView::GetInstance()->GetChildById(id);
Z
zhdengc 已提交
54
    if (view == nullptr) {
Z
zhdengc 已提交
55
        UIView* view = RootView::GetInstance()->GetChildById(UI_TEST_MAIN_LIST_ID);
Z
zhdengc 已提交
56 57 58 59 60
        if (view == nullptr) {
            return;
        }
        ListNode<TestCaseInfo>* node = UITestGroup::GetTestCase().Begin();
        while (node != UITestGroup::GetTestCase().End()) {
Z
zhdengc 已提交
61
            if ((node->data_.sliceId != nullptr) && (strcmp(id, node->data_.sliceId) == 0)) {
Z
zhdengc 已提交
62 63 64 65 66 67 68 69 70
                UITestGroup::GetTestCase().PushFront(node->data_);
                UITestGroup::GetTestCase().Remove(node);
                break;
            }
            node = node->next_;
        }
        reinterpret_cast<UIList*>(view)->RefreshList();
        CompareTools::WaitSuspend();
    }
Y
youbing 已提交
71

Z
zhdengc 已提交
72 73 74
    ClickViewById(id);
}

Z
zhdengc 已提交
75
void UIAutoTest::ClickViewById(const char* id) const
Z
zhdengc 已提交
76 77 78 79
{
    if (id == nullptr) {
        return;
    }
Z
zhdengc 已提交
80
    UIView* view = RootView::GetInstance()->GetChildById(id);
Z
zhdengc 已提交
81 82 83 84
    if (view == nullptr) {
        return;
    }
    Point point;
Z
zhdengc 已提交
85 86
    point.x = view->GetOrigRect().GetX();
    point.y = view->GetOrigRect().GetY();
Z
zhdengc 已提交
87 88 89 90
    EventInjector::GetInstance()->SetClickEvent(point);
    CompareTools::WaitSuspend();
}

Z
zhdengc 已提交
91
void UIAutoTest::DragViewToHead(const char* id) const
Z
zhdengc 已提交
92 93 94 95
{
    if (id == nullptr) {
        return;
    }
Z
zhdengc 已提交
96
    UIView* view = RootView::GetInstance()->GetChildById(id);
Z
zhdengc 已提交
97 98 99 100
    if (view == nullptr) {
        return;
    }
    Point startPoint;
Z
zhdengc 已提交
101 102
    startPoint.x = view->GetOrigRect().GetX();
    startPoint.y = view->GetOrigRect().GetY();
Z
zhdengc 已提交
103 104

    Point endPoint;
Z
zhdengc 已提交
105 106 107
    endPoint.x = 100; // 100 :end point x position;
    endPoint.y = 100; // 100 :end point y position;
    EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 300); // 300: drag time
Z
zhdengc 已提交
108 109
    CompareTools::WaitSuspend();
}
Z
zhdengc 已提交
110

Y
youbing 已提交
111
void UIAutoTest::DrageToView(const char* id, int16_t x, int16_t y) const
Z
zhdengc 已提交
112
{
Y
youbing 已提交
113 114 115 116 117
    if (id == nullptr) {
        return;
    }
    UIView* view = RootView::GetInstance()->GetChildById(id);
    if (view == nullptr) {
Z
zhdengc 已提交
118 119
        return;
    }
Y
youbing 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    Point startPoint;
    startPoint.x = view->GetOrigRect().GetX();
    startPoint.y = view->GetOrigRect().GetY();

    Point endPoint;
    endPoint.x = x;
    endPoint.y = y;
    EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 300); // 300: drag time
    CompareTools::WaitSuspend();
}

bool UIAutoTest::CompareByBinary(const char* fileName) const
{
    if (fileName == nullptr) {
        return false;
    }
Z
zhdengc 已提交
136 137 138
    char filePath[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
    CompareTools::StrnCatPath(filePath, DEFAULT_FILE_NAME_MAX_LENGTH, fileName, strlen(fileName));
    if (CompareTools::CheckFileExist(filePath, sizeof(filePath))) {
Y
youbing 已提交
139
        return CompareTools::CompareFile(filePath, sizeof(filePath));
Z
zhdengc 已提交
140
    } else {
Y
youbing 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        return CompareTools::SaveFile(filePath, sizeof(filePath));
    }
}

void UIAutoTest::RunTest(std::vector<std::shared_ptr<TestMsgInfo>> msgInfo)
{
    printf("UIAutoTest::RunTest----testInfo.size=[%d]\n", msgInfo.size());
    fflush(stdout);
    for (auto it: msgInfo) {
        OnTest(it);
    }

    AutoTestManager::GetInstance()->SendMsg(C_S_MAIN_ID_TEST_FINISH_INFO);
}

void UIAutoTest::OnTest(std::shared_ptr<TestMsgInfo> info)
{
    ResetMainMenu();
    OnEnterPage(info->pageNav);
    OnTestBySteps(info->steps, info->className);
}

void UIAutoTest::OnTestBySteps(std::vector<TestSteps> steps, std::string className)
{
    if (steps.empty()) {
        return;
    }

    int stepIndex = 0;
    for (auto it: steps) {
        OnTestOneStep(it, className, stepIndex++);
Z
zhdengc 已提交
172 173
    }
}
174

Y
youbing 已提交
175
void UIAutoTest::OnTestOneStep(TestSteps step, std::string className, size_t stepIndex)
176
{
Y
youbing 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    if (step.eventID == TestEventID::TEST_CLICK_EVENT) {
        ClickViewById(step.viewID.c_str());
    } else if (step.eventID == TestEventID::TEST_MOVE_EVENT) {
        if (step.eventValue.size() < EVENT_VALUE_SIZE_TWO) {
            return;
        }

        int16_t x = static_cast<int16_t>(step.eventValue[0]);
        int16_t y = static_cast<int16_t>(step.eventValue[1]);
        DrageToView(step.viewID.c_str(), x, y);
    }

    if (step.saveCheckPoint) {
        OnSaveFile(className, step.viewID, stepIndex);
    }
}

void UIAutoTest::OnEnterPage(std::vector<std::string> pageNav)
{
    if (pageNav.empty()) {
        return;
    }

    for (auto it: pageNav) {
        EnterSubMenu(it.c_str());
    }
}

void UIAutoTest::OnSaveFile(std::string className, std::string viewID, size_t stepIndex)
{
    std::string fileName = className + "@" + viewID + "@" + std::to_string(stepIndex) + ".bmp";
    fileNames_.push_back(fileName);

    std::string filePath;
    std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
    if (config->testMode == TestMode::TEST_MODE_BASE) {
        filePath =  config->baseDir + fileName;
    } else if (config->testMode == TestMode::TEST_MODE_RUN) {
        filePath =  config->runDir + fileName;
    }

    printf("OnSaveFile, filePath = %s\n", filePath.c_str());
    fflush(stdout);
    CompareTools::SaveFile(filePath.c_str(), strlen(filePath.c_str()));
}

void UIAutoTest::TestComplete() const
{
    printf("UIAutoTest::TestComplete");
    fflush(stdout);
    std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
    if (!config) {
        return;
    }
    if (config->testMode != TestMode::TEST_MODE_RUN) {
        return;
    }

    config->logDir += OnGetSystemTime();
    config->logDir += ".txt";
    printf("UIAutoTest::OnCompareFile--logDir=[%s]\n", config->logDir.c_str());
    fflush(stdout);
    for (auto it: fileNames_) {
        OnCompareFile(it);
    }
}

void UIAutoTest::OnCompareFile(std::string fileName) const
{
    std::shared_ptr<TestConfigInfo> config = AutoTestManager::GetInstance()->GetConfigInfo();
    if (!config) {
        return;
    }

    std::string fileBasePath = config->baseDir + fileName;
    std::string fileRunPath = config->runDir + fileName;

    std::string log;
    if (!CompareTools::CompareFile(fileBasePath.c_str(), fileRunPath.c_str())) {
        printf("UIAutoTest::OnCompareFile----different\n");
        fflush(stdout);
        log = "[FAIL]:[" + fileName + "]\n";
    } else {
        log = "[SUCESS]:[" + fileName + "]\n";
    }

    CompareTools::SaveResultLog(config->logDir.c_str(), log.c_str(), strlen(log.c_str()));
}

std::string UIAutoTest::OnGetSystemTime() const
{
    time_t t = time(0);
    char tmp[32] = { 0 };
    strftime(tmp, sizeof(tmp), "%Y-%m-%d-%H-%M-%S", localtime(&t));

    std::string loctime = tmp;

    return loctime;
275
}
Z
zhdengc 已提交
276
} // namespace OHOS