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 "random_test_flow.h"
29 #include "special_test_flow.h"
30 #include "focus_test_flow.h"
31 #include "system_ability_definition.h"
32 #include "test_flow_factory.h"
33
34 namespace OHOS {
35 namespace WuKong {
36 namespace {
37 const std::string WUKONG_TOOL_NAME = "wukong";
38
39 const std::string WUKONG_TOOL_VERSION = "version: 3.2.0.0\n";
40
41 const std::string ACE_ENABLE = "param set persist.ace.testmode.enabled 1";
42
43 const std::string WUKONG_HELP_MSG =
44 "usage: wukong <command> [<arguments>]\n"
45 "These are common wukong command list:\n"
46 " help wukong help information\n"
47 " -v/--version wukong version\n"
48 " exec run random test\n"
49 " special run special test\n"
50 " focus run focus test\n"
51 " appinfo show all app information\n";
52 } // namespace
53
WuKongShellCommand(int argc,char * argv[])54 WuKongShellCommand::WuKongShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, WUKONG_TOOL_NAME)
55 {
56 }
57
init()58 ErrCode WuKongShellCommand::init()
59 {
60 return OHOS::ERR_OK;
61 }
62
CreateCommandMap()63 ErrCode WuKongShellCommand::CreateCommandMap()
64 {
65 TRACK_LOG_STD();
66 commandMap_ = {
67 {"--version", std::bind(&WuKongShellCommand::GetWuKongVersion, this)},
68 {"-v", std::bind(&WuKongShellCommand::GetWuKongVersion, this)},
69 {"help", std::bind(&WuKongShellCommand::RunAsHelpCommand, this)},
70 {"stop", std::bind(&WuKongShellCommand::RunStopCommand, this)},
71 {"exec", std::bind(&WuKongShellCommand::RunTestCommand, this)},
72 {"special", std::bind(&WuKongShellCommand::RunTestCommand, this)},
73 {"focus", std::bind(&WuKongShellCommand::RunTestCommand, this)},
74 {"appinfo", std::bind(&WuKongShellCommand::ShowAllAppInfo, this)},
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 return OHOS::ERR_INVALID_OPERATION;
139 }
140 DEBUG_LOG("connected successfully");
141
142 auto aacPtr = OHOS::Accessibility::AccessibilityUITestAbility::GetInstance();
143 OHOS::Accessibility::AccessibilityElementInfo root;
144 if (aacPtr->GetRoot(root) != Accessibility::RET_OK) {
145 system(ACE_ENABLE.c_str());
146 }
147 // run test flow.
148 res = testFlow->Run();
149 if (res != OHOS::ERR_OK) {
150 DEBUG_LOG("Test flow run failed");
151 }
152 cm->Disconnect();
153 cm->DeleteRegisterListener(handle);
154
155 TRACK_LOG_END();
156 return res;
157 }
158
RunAsHelpCommand()159 ErrCode WuKongShellCommand::RunAsHelpCommand()
160 {
161 TRACK_LOG_STD();
162 resultReceiver_.append(WUKONG_HELP_MSG);
163 TRACK_LOG_END();
164 return OHOS::ERR_OK;
165 }
166
GetArgv()167 char **WuKongShellCommand::GetArgv()
168 {
169 return argv_;
170 }
171
GetArgc()172 int WuKongShellCommand::GetArgc()
173 {
174 return argc_;
175 }
176
ResultReceiverAppend(const std::string receiver)177 void WuKongShellCommand::ResultReceiverAppend(const std::string receiver)
178 {
179 resultReceiver_.append(receiver);
180 }
181
ShowAllAppInfo()182 ErrCode WuKongShellCommand::ShowAllAppInfo()
183 {
184 TRACK_LOG_STD();
185 ErrCode result = WuKongUtil::GetInstance()->GetAllAppInfo();
186 if (result != OHOS::ERR_OK) {
187 return result;
188 }
189 DEBUG_LOG_STR("GetAllAppInfo result: (%u)", result);
190 std::vector<std::string> bundleList;
191 std::vector<std::string> abilityList;
192 std::string iconpath;
193 WuKongUtil::GetInstance()->GetIconPath(iconpath);
194 WuKongUtil::GetInstance()->GetBundleList(bundleList, abilityList);
195
196 std::stringstream appInfo;
197 DEBUG_LOG_STR("bundleList size: (%u)", bundleList.size());
198 for (unsigned index = 0; index < bundleList.size(); index++) {
199 DEBUG_LOG_STR("Bundle Name: (%s), Ability Name: (%s)", bundleList[index].c_str(), abilityList[index].c_str());
200 appInfo << "BundleName: " << bundleList[index] << std::endl;
201 appInfo << "AbilityName: " << abilityList[index] << std::endl;
202 DEBUG_LOG_STR("IconPath: %s", iconpath.c_str());
203 }
204 resultReceiver_.append(appInfo.str());
205 TRACK_LOG_END();
206 return result;
207 }
208 } // namespace WuKong
209 } // namespace OHOS
210