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 { WindowCacheModelOHOS::uitest::WindowCacheModel25 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 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 bool updateTitle = false); 46 47 /**Retrieve widget from updated UI.*/ 48 const Widget *RetrieveWidget(const Widget &widget, ApiCallErr &err, bool updateUi = true); 49 50 /**Retrieve window from updated UI.*/ 51 const Window *RetrieveWindow(const Window &window, ApiCallErr &err, bool updateTitle = false); 52 53 string GetHostApp(const Widget &widget); 54 55 /**Trigger the given key action. */ 56 void TriggerKey(const KeyAction &key, const UiOpArgs &opt, ApiCallErr &error); 57 58 /**Perform the given touch action.*/ 59 void PerformTouch(const TouchAction &touch, const UiOpArgs &opt, ApiCallErr &err); 60 61 void PerformMouseAction(const MouseAction &touch, const UiOpArgs &opt, ApiCallErr &err); 62 63 /**Delay current thread for given duration.*/ 64 static void DelayMs(uint32_t ms); 65 66 /**Take screen capture, save to given file path as PNG.*/ 67 void TakeScreenCap(int32_t fd, ApiCallErr &err, Rect rect); 68 69 void DumpUiHierarchy(nlohmann::json &out, bool listWindows, bool addExternAttr, ApiCallErr &error); 70 71 const FrontEndClassDef &GetFrontendClassDef() const override 72 { 73 return DRIVER_DEF; 74 } 75 76 void SetDisplayRotation(DisplayRotation rotation, ApiCallErr &error); 77 78 DisplayRotation GetDisplayRotation(ApiCallErr &error); 79 80 void SetDisplayRotationEnabled(bool enabled, ApiCallErr &error); 81 82 bool WaitForUiSteady(uint32_t idleThresholdMs, uint32_t timeoutSec, ApiCallErr &error); 83 84 void WakeUpDisplay(ApiCallErr &error); 85 86 Point GetDisplaySize(ApiCallErr &error); 87 88 Point GetDisplayDensity(ApiCallErr &error); 89 90 static void RegisterController(std::unique_ptr<UiController> controller); 91 92 bool CheckStatus(bool isConnected, ApiCallErr &error); 93 94 static void RegisterUiEventListener(std::shared_ptr<UiEventListener> listener); 95 96 void InputText(string_view text, ApiCallErr &error); 97 98 void GetMergeWindowBounds(Rect& mergeRect); 99 100 private: 101 bool TextToKeyEvents(string_view text, std::vector<KeyEvent> &events, ApiCallErr &error); 102 // UI objects that are needed to be updated before each interaction and used in the interaction 103 void UpdateUIWindows(ApiCallErr &error, bool updateTitle = false); 104 void DumpWindowsInfo(bool listWindows, Rect& mergeBounds, nlohmann::json& childDom); 105 static std::unique_ptr<UiController> uiController_; 106 // CacheModel: 107 std::vector<WindowCacheModel> windowCacheVec_; 108 // unique widget object save 109 std::vector<Widget> visitWidgets_; 110 std::vector<int> targetWidgetsIndex_; 111 }; 112 } // namespace OHOS::uitest 113 114 #endif