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 /**Trigger the given key action. */ 49 void TriggerKey(const KeyAction &key, const UiOpArgs &opt, ApiCallErr &error); 50 51 /**Perform the given touch action.*/ 52 void PerformTouch(const TouchAction &touch, const UiOpArgs &opt, ApiCallErr &err); 53 54 /**Delay current thread for given duration.*/ 55 static void DelayMs(uint32_t ms); 56 57 /**Take screen capture, save to given file path as PNG.*/ 58 void TakeScreenCap(std::string_view savePath, ApiCallErr &err); 59 60 void DumpUiHierarchy(nlohmann::json &out, ApiCallErr &error); 61 62 /** Get current UI tree.*/ 63 const WidgetTree *GetWidgetTree() const; 64 65 /** Get current UI controller.*/ 66 const UiController *GetUiController(ApiCallErr &error); 67 GetFrontendClassDef()68 const FrontEndClassDef &GetFrontendClassDef() const override 69 { 70 return DRIVER_DEF; 71 } 72 73 private: 74 /**Update UI controller and UI objects.*/ 75 void UpdateUi(bool updateUiTree, ApiCallErr &error); 76 // UI objects that are needed to be updated before each interaction and used in the interaction 77 UiController *uiController_ = nullptr; 78 std::unique_ptr<WidgetTree> widgetTree_ = nullptr; 79 std::vector<Window> windows_; 80 }; 81 } // namespace OHOS::uitest 82 83 #endif