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 {"exec", [this] () { return WuKongShellCommand::RunTestCommand(); }},
72 {"special", [this] () { return WuKongShellCommand::RunTestCommand(); }},
73 {"focus", [this] () { return WuKongShellCommand::RunTestCommand(); }},
74 {"appinfo", [this] () { return WuKongShellCommand::ShowAllAppInfo(); }},
75 };
76 TRACK_LOG_END();
77 return OHOS::ERR_OK;
78 }
79
CreateMessageMap()80 ErrCode WuKongShellCommand::CreateMessageMap()
81 {
82 TRACK_LOG_STD();
83 messageMap_ = {
84 {
85 ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR,
86 "error: wukong internal error.",
87 },
88 };
89 TRACK_LOG_END();
90 return OHOS::ERR_OK;
91 }
92
GetWuKongVersion()93 ErrCode WuKongShellCommand::GetWuKongVersion()
94 {
95 resultReceiver_ = WUKONG_TOOL_VERSION;
96 return OHOS::ERR_OK;
97 }
98
RunStopCommand()99 ErrCode WuKongShellCommand::RunStopCommand()
100 {
101 WukongSemaphore sem(SEMPHORE_STOP_NAME, 1);
102 sem.Open();
103 if (sem.GetValue() == 0) {
104 sem.Post();
105 }
106 sem.Close();
107 return OHOS::ERR_OK;
108 }
109
RunTestCommand()110 ErrCode WuKongShellCommand::RunTestCommand()
111 {
112 TRACK_LOG_STD();
113 // get testFlow by cmd_ of ShellCommand
114 std::shared_ptr<TestFlow> testFlow = TestFlowFactory::GetTestFlow(*this, cmd_);
115 if (testFlow == nullptr) {
116 ERROR_LOG_STR("GetTestFlow TestFlow is null command (%s)", cmd_.c_str());
117 return OHOS::ERR_INVALID_VALUE;
118 }
119
120 auto cm = ComponentManager::GetInstance();
121 if (cm == nullptr) {
122 ERROR_LOG("cm is nullptr");
123 return OHOS::ERR_INVALID_VALUE;
124 }
125 uint32_t handle = cm->AddRegisterListener(testFlow);
126
127 // check the command arguments
128 // if argument is not ok, exit wukong command.
129 ErrCode res = testFlow->CheckVaildityCmd();
130 if (res != OHOS::ERR_OK) {
131 DEBUG_LOG("Command arguments is invalid and exit");
132 return res;
133 }
134
135 // connect to accessibility
136 if (!cm->Connect()) {
137 ERROR_LOG("ComponentManager Connect failed");
138 OHOS::WuKong::Report::GetInstance()->Finish();
139 return OHOS::ERR_INVALID_OPERATION;
140 }
141 DEBUG_LOG("connected successfully");
142
143 auto aacPtr = OHOS::Accessibility::AccessibilityUITestAbility::GetInstance();
144 OHOS::Accessibility::AccessibilityElementInfo root;
145 if (aacPtr->GetRoot(root) != Accessibility::RET_OK) {
146 system(ACE_ENABLE.c_str());
147 }
148 // run test flow.
149 res = testFlow->Run();
150 if (res != OHOS::ERR_OK) {
151 DEBUG_LOG("Test flow run failed");
152 }
153 cm->Disconnect();
154 cm->DeleteRegisterListener(handle);
155
156 TRACK_LOG_END();
157 return res;
158 }
159
RunAsHelpCommand()160 ErrCode WuKongShellCommand::RunAsHelpCommand()
161 {
162 TRACK_LOG_STD();
163 resultReceiver_.append(WUKONG_HELP_MSG);
164 TRACK_LOG_END();
165 return OHOS::ERR_OK;
166 }
167
GetArgv()168 char **WuKongShellCommand::GetArgv()
169 {
170 return argv_;
171 }
172
GetArgc()173 int WuKongShellCommand::GetArgc()
174 {
175 return argc_;
176 }
177
ResultReceiverAppend(const std::string receiver)178 void WuKongShellCommand::ResultReceiverAppend(const std::string receiver)
179 {
180 resultReceiver_.append(receiver);
181 }
182
ShowAllAppInfo()183 ErrCode WuKongShellCommand::ShowAllAppInfo()
184 {
185 TRACK_LOG_STD();
186 ErrCode result = WuKongUtil::GetInstance()->GetAllAppInfo();
187 if (result != OHOS::ERR_OK) {
188 return result;
189 }
190 DEBUG_LOG_STR("GetAllAppInfo result: (%u)", result);
191 std::vector<std::string> bundleList;
192 std::vector<std::string> abilityList;
193 std::string iconpath;
194 WuKongUtil::GetInstance()->GetIconPath(iconpath);
195 WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
196
197 std::stringstream appInfo;
198 DEBUG_LOG_STR("bundleList size: (%u)", bundleList.size());
199 for (unsigned index = 0; index < bundleList.size(); index++) {
200 DEBUG_LOG_STR("Bundle Name: (%s), Ability Name: (%s)", bundleList[index].c_str(), abilityList[index].c_str());
201 appInfo << "BundleName: " << bundleList[index] << std::endl;
202 appInfo << "AbilityName: " << abilityList[index] << std::endl;
203 DEBUG_LOG_STR("IconPath: %s", iconpath.c_str());
204 }
205 resultReceiver_.append(appInfo.str());
206 TRACK_LOG_END();
207 return result;
208 }
209 } // namespace WuKong
210 } // namespace OHOS
211