1/*
2 * Copyright (c) 2022-2024 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 "test_observer.h"
17
18#include <cinttypes>
19#include <iostream>
20#include <unistd.h>
21
22#include "hilog_tag_wrapper.h"
23#include "shell_command_config_loader.h"
24#include "shell_command_executor.h"
25#include "system_time.h"
26
27using namespace std::chrono_literals;
28
29namespace OHOS {
30namespace AAFwk {
31namespace {
32    constexpr const char* AA_TOOL_COMMAND_CONFIG = "/system/etc/shell_command_executor_config.json";
33}
34
35TestObserver::TestObserver() : isFinished_(false)
36{}
37
38TestObserver::~TestObserver()
39{}
40
41void TestObserver::TestStatus(const std::string& msg, const int64_t& resultCode)
42{
43    TAG_LOGI(AAFwkTag::AA_TOOL, "enter, msg : %{public}s, code : %{public}" PRId64, msg.data(), resultCode);
44    printf("%s\n", msg.data());
45    fflush(stdout);
46}
47
48void TestObserver::TestFinished(const std::string& msg, const int64_t& resultCode)
49{
50    TAG_LOGI(AAFwkTag::AA_TOOL, "enter, msg : %{public}s, code : %{public}" PRId64, msg.data(), resultCode);
51    std::cout << "TestFinished-ResultCode: " + std::to_string(resultCode) << std::endl;
52    std::cout << "TestFinished-ResultMsg: " + msg << std::endl;
53    isFinished_ = true;
54}
55
56ShellCommandResult TestObserver::ExecuteShellCommand(const std::string& cmd, const int64_t timeoutSec)
57{
58    TAG_LOGI(AAFwkTag::AA_TOOL, "enter, cmd : \"%{public}s\", timeoutSec : %{public}" PRId64, cmd.data(), timeoutSec);
59
60    auto cmdExecutor = std::make_shared<ShellCommandExecutor>(cmd, timeoutSec);
61    if (!cmdExecutor) {
62        TAG_LOGE(AAFwkTag::AA_TOOL, "instance create failed");
63        return {};
64    }
65
66    if (!std::make_shared<ShellCommandConfigLoader>()->ReadConfig(AA_TOOL_COMMAND_CONFIG)) {
67        TAG_LOGE(AAFwkTag::AA_TOOL, "Read config failed");
68        return {};
69    }
70
71    return cmdExecutor->WaitWorkDone();
72}
73
74bool TestObserver::WaitForFinish(const int64_t& timeoutMs)
75{
76    TAG_LOGD(AAFwkTag::AA_TOOL, "enter");
77
78    auto realTime = timeoutMs > 0 ? timeoutMs : 0;
79    int64_t startTime = SystemTime::GetNowSysTime();
80    while (!isFinished_) {
81        int64_t nowSysTime = SystemTime::GetNowSysTime();
82        if (realTime && (nowSysTime - startTime > realTime)) {
83            return false;
84        }
85        sleep(1);
86    }
87
88    TAG_LOGI(AAFwkTag::AA_TOOL, "User test finished");
89    return true;
90}
91}  // namespace AAFwk
92}  // namespace OHOS
93