1/*
2 * Copyright (c) 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 "auto_test_manager.h"
17
18#include <iostream>
19#include <memory>
20
21#include "dfx/event_injector.h"
22
23namespace OHOS {
24AutoTestManager::AutoTestManager()
25{
26    SetUpTestCase();
27}
28
29AutoTestManager::~AutoTestManager()
30{
31    TearDownTestCase();
32}
33
34AutoTestManager* AutoTestManager::GetInstance()
35{
36    static AutoTestManager instance;
37    return &instance;
38}
39
40void AutoTestManager::SetUpTestCase()
41{
42    autoTest_ = std::make_shared<UIAutoTest>();
43
44#ifdef _WIN32
45    const char logPath[] = ".\\auto_test_log.txt";
46    CompareTools::SetLogPath(logPath, sizeof(logPath));
47#else
48    const char logPath[] = "./auto_test_log.txt";
49    CompareTools::SetLogPath(logPath, sizeof(logPath));
50#endif
51    EventInjector::GetInstance()->RegisterEventInjector(EventDataType::POINT_TYPE);
52    EventInjector::GetInstance()->RegisterEventInjector(EventDataType::KEY_TYPE);
53#if defined(ENABLE_WINDOW) && ENABLE_WINDOW
54    Window* window = RootView::GetInstance()->GetBoundWindow();
55    if (window != nullptr) {
56        EventInjector::GetInstance()->SetWindowId(window->GetWindowId());
57    }
58#endif
59}
60
61void AutoTestManager::TearDownTestCase()
62{
63    if (EventInjector::GetInstance()->IsEventInjectorRegistered(EventDataType::POINT_TYPE)) {
64        EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::POINT_TYPE);
65    }
66    if (EventInjector::GetInstance()->IsEventInjectorRegistered(EventDataType::KEY_TYPE)) {
67        EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::KEY_TYPE);
68    }
69}
70
71void AutoTestManager::SendMsg(size_t mainID)
72{
73    sendMsgFunc_(mainID);
74}
75
76void AutoTestManager::SetSendMsgFuncCallBack(SendMsgFunc sendMsgFunc)
77{
78    sendMsgFunc_ = sendMsgFunc;
79}
80
81void AutoTestManager::StartTest(std::vector<std::shared_ptr<TestMsgInfo>> msgInfo)
82{
83    printf("AutoTestManager::StartTest----msgInfo.size=[%d]\n", msgInfo.size());
84    fflush(stdout);
85    if (!autoTest_) {
86        return;
87    }
88
89    autoTest_->RunTest(msgInfo);
90}
91
92void AutoTestManager::TestComplete()
93{
94    if (!autoTest_) {
95        return;
96    }
97
98    autoTest_->TestComplete();
99}
100
101void AutoTestManager::SetConfigInfo(const std::shared_ptr<TestConfigInfo> configInfo)
102{
103    configInfo_ = configInfo;
104    printf("AutoTestManager::SetConfigInfo--testMode=[%zu], baseDir=[%s], runDir=[%s], logDir=[%s]\n",
105        configInfo_->testMode, configInfo_->baseDir.c_str(), configInfo_->runDir.c_str(),
106        configInfo_->logDir.c_str());
107    fflush(stdout);
108    SendMsg(C_S_MAIN_ID_REQUEST_TEST_INFO);
109}
110
111std::shared_ptr<TestConfigInfo> AutoTestManager::GetConfigInfo()
112{
113    return configInfo_;
114}
115}
116