1 /* 2 * Copyright (c) 2021-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 <chrono> 17 #include <unistd.h> 18 #include <memory> 19 #include <iostream> 20 #include "extern_api.h" 21 #include "ipc_transactors_impl.h" 22 #include "system_ui_controller.h" 23 24 using namespace std; 25 using namespace std::chrono; 26 27 namespace OHOS::uitest { 28 /**Print to the console of this shell process.*/ PrintToConsole(string_view message)29 static inline void PrintToConsole(string_view message) 30 { 31 std::cout << message << std::endl; 32 } 33 DumpLayout()34 static int32_t DumpLayout() 35 { 36 auto controller = SysUiController("sys_ui_controller", ""); 37 if (!controller.ConnectToSysAbility()) { 38 PrintToConsole("Dump layout failed, cannot connect to AAMS"); 39 return EXIT_FAILURE; 40 } 41 auto data = nlohmann::json(); 42 controller.GetCurrentUiDom(data); 43 PrintToConsole(data.dump()); 44 return EXIT_SUCCESS; 45 } 46 StartDaemon(string_view token)47 static int32_t StartDaemon(string_view token) 48 { 49 if (token.empty()) { 50 LOG_E("Empty transaction token"); 51 return EXIT_FAILURE; 52 } 53 if (daemon(0, 0) != 0) { 54 LOG_E("Failed to daemonize current process"); 55 return EXIT_FAILURE; 56 } 57 LOG_I("Server starting up"); 58 TransactionServerImpl server(token); 59 if (!server.Initialize()) { 60 LOG_E("Failed to initialize server"); 61 return EXIT_FAILURE; 62 } 63 // set actual api handlerFunc provided by uitest_core 64 server.SetCallFunction(ApiTransact); 65 // set delayed UiController 66 auto controllerProvider = [](string_view device, list<unique_ptr<UiController>> &receiver) { 67 auto controller = make_unique<SysUiController>("sys_ui_controller", device); 68 if (controller->ConnectToSysAbility()) { 69 receiver.push_back(move(controller)); 70 } 71 }; 72 UiController::RegisterControllerProvider(controllerProvider); 73 LOG_I("UiTest-daemon running, pid=%{public}d", getpid()); 74 auto exitCode = server.RunLoop(); 75 LOG_I("Server exited with code: %{public}d", exitCode); 76 server.Finalize(); 77 return exitCode == EXIT_SUCCESS; 78 } 79 main(int32_t argc,char * argv[])80 extern "C" int32_t main(int32_t argc, char *argv[]) 81 { 82 static constexpr string_view usage = "USAGE: uitestkit <screenCap|dumpLayout>"; 83 if (argc < INDEX_TWO) { 84 PrintToConsole("Missing argument"); 85 PrintToConsole(usage); 86 exit(EXIT_FAILURE); 87 } 88 string command(argv[1]); 89 if (command == "dumpLayout") { 90 exit(DumpLayout()); 91 } else if (command == "start-daemon") { 92 string_view token = argc < 3 ? "" : argv[2]; 93 exit(StartDaemon(token)); 94 } else { 95 PrintToConsole("Illegal argument: " + command); 96 PrintToConsole(usage); 97 exit(EXIT_FAILURE); 98 } 99 } 100 }