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_action.h" 21 #include "widget_selector.h" 22 23 namespace OHOS::uitest { 24 struct WindowCacheModel { WindowCacheModelWindowCacheModel25 explicit WindowCacheModel(const Window &win) : window_(win), widgetIterator_(nullptr) {} 26 Window window_; 27 std::unique_ptr<ElementNodeIterator> widgetIterator_; 28 }; 29 class UiDriver : public BackendClass { 30 public: UiDriver()31 UiDriver() {} 32 ~UiDriver()33 ~UiDriver() override {} 34 35 /**Find widgets with the given selector. Results are arranged in the receiver in <b>DFS</b> order. 36 * @returns the widget object. 37 **/ 38 void FindWidgets(const WidgetSelector &select, vector<unique_ptr<Widget>> &rev, 39 ApiCallErr &err, bool updateUi = true); 40 41 /**Wait for the matching widget appear in the given timeout.*/ 42 std::unique_ptr<Widget> WaitForWidget(const WidgetSelector &select, const UiOpArgs &opt, ApiCallErr &err); 43 /**Find window matching the given matcher.*/ 44 std::unique_ptr<Window> FindWindow(std::function<bool(const Window &)> matcher, ApiCallErr &err); 45 46 /**Retrieve widget from updated UI.*/ 47 const Widget *RetrieveWidget(const Widget &widget, ApiCallErr &err, bool updateUi = true); 48 49 /**Retrieve window from updated UI.*/ 50 const Window *RetrieveWindow(const Window &window, ApiCallErr &err); 51 52 string GetHostApp(const Widget &widget); 53 54 /**Trigger the given key action. */ 55 void TriggerKey(const KeyAction &key, const UiOpArgs &opt, ApiCallErr &error); 56 57 /**Perform the given touch action.*/ 58 void PerformTouch(const TouchAction &touch, const UiOpArgs &opt, ApiCallErr &err); 59 60 void PerformMouseAction(const MouseAction &touch, const UiOpArgs &opt, ApiCallErr &err); 61 62 /**Delay current thread for given duration.*/ 63 static void DelayMs(uint32_t ms); 64 65 /**Take screen capture, save to given file path as PNG.*/ 66 void TakeScreenCap(int32_t fd, ApiCallErr &err, Rect rect); 67 68 void DumpUiHierarchy(nlohmann::json &out, bool listWindows, bool addExternAttr, ApiCallErr &error); 69 GetFrontendClassDef()70 const FrontEndClassDef &GetFrontendClassDef() const override 71 { 72 return DRIVER_DEF; 73 } 74 75 void SetDisplayRotation(DisplayRotation rotation, ApiCallErr &error); 76 77 DisplayRotation GetDisplayRotation(ApiCallErr &error); 78 79 void SetDisplayRotationEnabled(bool enabled, ApiCallErr &error); 80 81 bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec, ApiCallErr &error); 82 83 void WakeUpDisplay(ApiCallErr &error); 84 85 Point GetDisplaySize(ApiCallErr &error); 86 87 Point GetDisplayDensity(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 InputText(string_view text, ApiCallErr &error); 96 97 void GetMergeWindowBounds(Rect& mergeRect); 98 99 private: 100 bool TextToKeyEvents(string_view text, std::vector<KeyEvent> &events, ApiCallErr &error); 101 // UI objects that are needed to be updated before each interaction and used in the interaction 102 void UpdateUIWindows(ApiCallErr &error); 103 void DumpWindowsInfo(bool listWindows, Rect& mergeBounds, nlohmann::json& childDom); 104 static std::unique_ptr<UiController> uiController_; 105 // CacheModel: 106 std::vector<WindowCacheModel> windowCacheVec_; 107 // unique widget object save 108 std::vector<Widget> visitWidgets_; 109 std::vector<int> targetWidgetsIndex_; 110 }; 111 } // namespace OHOS::uitest 112 113 #endif