• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test_observer.h"
17 
18 #include <cinttypes>
19 #include <iostream>
20 #include <unistd.h>
21 
22 #include "hilog_wrapper.h"
23 #include "shell_command_config_loader.h"
24 #include "shell_command_executor.h"
25 #include "system_time.h"
26 
27 using namespace std::chrono_literals;
28 
29 namespace OHOS {
30 namespace AAFwk {
31 namespace {
32     constexpr const char* AA_TOOL_COMMAND_CONFIG = "/system/etc/shell_command_executor_config.json";
33 }
34 
TestObserver()35 TestObserver::TestObserver() : isFinished_(false)
36 {}
37 
~TestObserver()38 TestObserver::~TestObserver()
39 {}
40 
TestStatus(const std::string & msg,const int64_t & resultCode)41 void TestObserver::TestStatus(const std::string& msg, const int64_t& resultCode)
42 {
43     HILOG_INFO("enter, msg : %{public}s, code : %{public}" PRId64, msg.data(), resultCode);
44     printf("%s\n", msg.data());
45     fflush(stdout);
46 }
47 
TestFinished(const std::string & msg,const int64_t & resultCode)48 void TestObserver::TestFinished(const std::string& msg, const int64_t& resultCode)
49 {
50     HILOG_INFO("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 
ExecuteShellCommand(const std::string & cmd,const int64_t timeoutSec)56 ShellCommandResult TestObserver::ExecuteShellCommand(const std::string& cmd, const int64_t timeoutSec)
57 {
58     HILOG_INFO("enter, cmd : \"%{public}s\", timeoutSec : %{public}" PRId64, cmd.data(), timeoutSec);
59 
60     auto cmdExecutor = std::make_shared<ShellCommandExecutor>(cmd, timeoutSec);
61     if (!cmdExecutor) {
62         HILOG_ERROR("Failed to create ShellCommandExecutor intance");
63         return {};
64     }
65 
66     if(!std::make_shared<ShellCommandConfigLoder>()->ReadConfig(AA_TOOL_COMMAND_CONFIG)) {
67         HILOG_ERROR("Failed to read config");
68         return {};
69     }
70 
71     return cmdExecutor->WaitWorkDone();
72 }
73 
WaitForFinish(const int64_t & timeoutMs)74 bool TestObserver::WaitForFinish(const int64_t& timeoutMs)
75 {
76     HILOG_INFO("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     HILOG_INFO("User test finished");
89     return true;
90 }
91 }  // namespace AAFwk
92 }  // namespace OHOS
93