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 "wukong_shell_command.h"
17
18 #include <cstdlib>
19
20 #include "ability_manager_client.h"
21 #include "accessibility_element_info.h"
22 #include "accessibility_ui_test_ability.h"
23 #include "appexecfwk_errors.h"
24 #include "bundle_mgr_proxy.h"
25 #include "component_manager.h"
26 #include "iservice_registry.h"
27 #include "launcher_service.h"
28 #include "report.h"
29 #include "random_test_flow.h"
30 #include "special_test_flow.h"
31 #include "focus_test_flow.h"
32 #include "system_ability_definition.h"
33 #include "test_flow_factory.h"
34
35 namespace OHOS {
36 namespace WuKong {
37 namespace {
38 const std::string WUKONG_TOOL_NAME = "wukong";
39
40 const std::string WUKONG_TOOL_VERSION = "version: 3.2.0.0\n";
41
42 const std::string ACE_ENABLE = "param set persist.ace.testmode.enabled 1";
43
44 const std::string WUKONG_HELP_MSG =
45 "usage: wukong <command> [<arguments>]\n"
46 "These are common wukong command list:\n"
47 " help wukong help information\n"
48 " -v/--version wukong version\n"
49 " exec run random test\n"
50 " special run special test\n"
51 " focus run focus test\n"
52 " appinfo show all app information\n";
53 } // namespace
54
WuKongShellCommand(int argc,char * argv[])55 WuKongShellCommand::WuKongShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, WUKONG_TOOL_NAME)
56 {
57 }
58
init()59 ErrCode WuKongShellCommand::init()
60 {
61 return OHOS::ERR_OK;
62 }
63
CreateCommandMap()64 ErrCode WuKongShellCommand::CreateCommandMap()
65 {
66 TRACK_LOG_STD();
67 commandMap_ = {
68 {"--version", [this] () { return WuKongShellCommand::GetWuKongVersion(); }},
69 {"-v", [this] () { return WuKongShellCommand::GetWuKongVersion(); }},
70 {"help", [this] () { return WuKongShellCommand::RunAsHelpCommand(); }},
71 {"stop", [this] () { return WuKongShellCommand::RunStopCommand(); }},
72 {"exec", [this] () { return WuKongShellCommand::RunTestCommand(); }},
73 {"special", [this] () { return WuKongShellCommand::RunTestCommand(); }},
74 {"focus", [this] () { return WuKongShellCommand::RunTestCommand(); }},
75 {"appinfo", [this] () { return WuKongShellCommand::ShowAllAppInfo(); }},
76 };
77 TRACK_LOG_END();
78 return OHOS::ERR_OK;
79 }
80
CreateMessageMap()81 ErrCode WuKongShellCommand::CreateMessageMap()
82 {
83 TRACK_LOG_STD();
84 messageMap_ = {
85 {
86 ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR,
87 "error: wukong internal error.",
88 },
89 };
90 TRACK_LOG_END();
91 return OHOS::ERR_OK;
92 }
93
GetWuKongVersion()94 ErrCode WuKongShellCommand::GetWuKongVersion()
95 {
96 resultReceiver_ = WUKONG_TOOL_VERSION;
97 return OHOS::ERR_OK;
98 }
99
RunStopCommand()100 ErrCode WuKongShellCommand::RunStopCommand()
101 {
102 WukongSemaphore sem(SEMPHORE_STOP_NAME, 1);
103 sem.Open();
104 if (sem.GetValue() == 0) {
105 sem.Post();
106 }
107 sem.Close();
108 return OHOS::ERR_OK;
109 }
110
RunTestCommand()111 ErrCode WuKongShellCommand::RunTestCommand()
112 {
113 TRACK_LOG_STD();
114 // get testFlow by cmd_ of ShellCommand
115 std::shared_ptr<TestFlow> testFlow = TestFlowFactory::GetTestFlow(*this, cmd_);
116 if (testFlow == nullptr) {
117 ERROR_LOG_STR("GetTestFlow TestFlow is null command (%s)", cmd_.c_str());
118 return OHOS::ERR_INVALID_VALUE;
119 }
120
121 auto cm = ComponentManager::GetInstance();
122 if (cm == nullptr) {
123 ERROR_LOG("cm is nullptr");
124 return OHOS::ERR_INVALID_VALUE;
125 }
126 uint32_t handle = cm->AddRegisterListener(testFlow);
127
128 // check the command arguments
129 // if argument is not ok, exit wukong command.
130 ErrCode res = testFlow->CheckVaildityCmd();
131 if (res != OHOS::ERR_OK) {
132 DEBUG_LOG("Command arguments is invalid and exit");
133 return res;
134 }
135
136 // connect to accessibility
137 if (!cm->Connect()) {
138 ERROR_LOG("ComponentManager Connect failed");
139 OHOS::WuKong::Report::GetInstance()->Finish();
140 return OHOS::ERR_INVALID_OPERATION;
141 }
142 DEBUG_LOG("connected successfully");
143
144 auto aacPtr = OHOS::Accessibility::AccessibilityUITestAbility::GetInstance();
145 OHOS::Accessibility::AccessibilityElementInfo root;
146 if (aacPtr->GetRoot(root) != Accessibility::RET_OK) {
147 system(ACE_ENABLE.c_str());
148 }
149 // run test flow.
150 res = testFlow->Run();
151 if (res != OHOS::ERR_OK) {
152 DEBUG_LOG("Test flow run failed");
153 }
154 cm->Disconnect();
155 cm->DeleteRegisterListener(handle);
156
157 TRACK_LOG_END();
158 return res;
159 }
160
RunAsHelpCommand()161 ErrCode WuKongShellCommand::RunAsHelpCommand()
162 {
163 TRACK_LOG_STD();
164 resultReceiver_.append(WUKONG_HELP_MSG);
165 TRACK_LOG_END();
166 return OHOS::ERR_OK;
167 }
168
GetArgv()169 char **WuKongShellCommand::GetArgv()
170 {
171 return argv_;
172 }
173
GetArgc()174 int WuKongShellCommand::GetArgc()
175 {
176 return argc_;
177 }
178
ResultReceiverAppend(const std::string receiver)179 void WuKongShellCommand::ResultReceiverAppend(const std::string receiver)
180 {
181 resultReceiver_.append(receiver);
182 }
183
ShowAllAppInfo()184 ErrCode WuKongShellCommand::ShowAllAppInfo()
185 {
186 TRACK_LOG_STD();
187 ErrCode result = WuKongUtil::GetInstance()->GetAllAppInfo();
188 if (result != OHOS::ERR_OK) {
189 return result;
190 }
191 DEBUG_LOG_STR("GetAllAppInfo result: (%u)", result);
192 std::vector<std::string> bundleList;
193 std::vector<std::string> abilityList;
194 std::string iconpath;
195 WuKongUtil::GetInstance()->GetIconPath(iconpath);
196 WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
197
198 std::stringstream appInfo;
199 DEBUG_LOG_STR("bundleList size: (%u)", bundleList.size());
200 for (unsigned index = 0; index < bundleList.size(); index++) {
201 DEBUG_LOG_STR("Bundle Name: (%s), Ability Name: (%s)", bundleList[index].c_str(), abilityList[index].c_str());
202 appInfo << "BundleName: " << bundleList[index] << std::endl;
203 appInfo << "AbilityName: " << abilityList[index] << std::endl;
204 DEBUG_LOG_STR("IconPath: %s", iconpath.c_str());
205 }
206 resultReceiver_.append(appInfo.str());
207 TRACK_LOG_END();
208 return result;
209 }
210 } // namespace WuKong
211 } // namespace OHOS
212