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 #ifndef UI_DRIVER_H 17 #define UI_DRIVER_H 18 19 #include "ui_controller.h" 20 #include "ui_model.h" 21 #include "widget_selector.h" 22 23 namespace OHOS::uitest { 24 class UiDriver : public BackendClass { 25 public: UiDriver()26 UiDriver() {} 27 ~UiDriver()28 ~UiDriver() override {} 29 30 /**Find widgets with the given selector. Results are arranged in the receiver in <b>DFS</b> order. 31 * @returns the widget object. 32 **/ 33 void FindWidgets(const WidgetSelector &select, std::vector<std::unique_ptr<Widget>> &rev, 34 ApiCallErr &err, bool updateUi = true); 35 36 /**Wait for the matching widget appear in the given timeout.*/ 37 std::unique_ptr<Widget> WaitForWidget(const WidgetSelector &select, const UiOpArgs &opt, ApiCallErr &err); 38 39 /**Find window matching the given matcher.*/ 40 std::unique_ptr<Window> FindWindow(std::function<bool(const Window &)> matcher, ApiCallErr &err); 41 42 /**Retrieve widget from updated UI.*/ 43 const Widget *RetrieveWidget(const Widget &widget, ApiCallErr &err, bool updateUi = true); 44 45 /**Retrieve window from updated UI.*/ 46 const Window *RetrieveWindow(const Window &window, ApiCallErr &err); 47 48 string GetHostApp(const Widget &widget); 49 50 /**Trigger the given key action. */ 51 void TriggerKey(const KeyAction &key, const UiOpArgs &opt, ApiCallErr &error); 52 53 /**Perform the given touch action.*/ 54 void PerformTouch(const TouchAction &touch, const UiOpArgs &opt, ApiCallErr &err); 55 56 /**Delay current thread for given duration.*/ 57 static void DelayMs(uint32_t ms); 58 59 /**Take screen capture, save to given file path as PNG.*/ 60 void TakeScreenCap(int32_t fd, ApiCallErr &err, Rect rect); 61 62 void DumpUiHierarchy(nlohmann::json &out, bool listWindows, ApiCallErr &error); 63 GetFrontendClassDef()64 const FrontEndClassDef &GetFrontendClassDef() const override 65 { 66 return DRIVER_DEF; 67 } 68 69 void SetDisplayRotation(DisplayRotation rotation, ApiCallErr &error); 70 71 DisplayRotation GetDisplayRotation(ApiCallErr &error); 72 73 void SetDisplayRotationEnabled(bool enabled, ApiCallErr &error); 74 75 bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec, ApiCallErr &error); 76 77 void WakeUpDisplay(ApiCallErr &error); 78 79 Point GetDisplaySize(ApiCallErr &error); 80 81 Point GetDisplayDensity(ApiCallErr &error); 82 83 bool GetCharKeyCode(char ch, int32_t &code, int32_t &ctrlCode, ApiCallErr &error); 84 85 void DfsTraverseTree(WidgetVisitor &visitor, const Widget *widget = nullptr); 86 87 void InjectMouseAction(MouseOpArgs mouseOpArgs, ApiCallErr &error); 88 89 static void RegisterController(std::unique_ptr<UiController> controller); 90 91 bool CheckStatus(bool isConnected, ApiCallErr &error); 92 93 static void RegisterUiEventListener(std::shared_ptr<UiEventListener> listener); 94 95 void GetLayoutJson(nlohmann::json &dom); 96 97 private: 98 /**Update UI controller and UI objects.*/ 99 void UpdateUi(bool updateUiTree, ApiCallErr &error, bool getWidgetNodes, string targetWin = ""); 100 // UI objects that are needed to be updated before each interaction and used in the interaction 101 static std::unique_ptr<UiController> uiController_; 102 std::unique_ptr<WidgetTree> widgetTree_ = nullptr; 103 std::vector<Window> windows_; 104 }; 105 } // namespace OHOS::uitest 106 107 #endif