1 /* 2 * Copyright (c) 2022-2023 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 MOCK_CONTROLLER_H 17 #define MOCK_CONTROLLER_H 18 19 #include "ui_controller.h" 20 #include "ui_model.h" 21 #include "element_node_iterator.h" 22 23 namespace OHOS::uitest { 24 class MockController : public UiController { 25 public: 26 static std::unique_ptr<PointerMatrix> touch_event_records_; MockController()27 MockController() : UiController() {} 28 29 ~MockController() = default; 30 GetUiWindows(std::map<int32_t,vector<Window>> & out,int32_t targetDisplay)31 void GetUiWindows(std::map<int32_t, vector<Window>> &out, int32_t targetDisplay) override 32 { 33 vector<Window> winInfos; 34 for (auto iter = testIn.cbegin(); iter != testIn.cend(); ++iter) { 35 winInfos.emplace_back(iter->second); 36 } 37 out.insert(make_pair(0, move(winInfos))); 38 } 39 GetWidgetsInWindow(const Window & winInfo,std::unique_ptr<ElementNodeIterator> & elementNodeIterator,AamsWorkMode mode)40 bool GetWidgetsInWindow(const Window &winInfo, 41 std::unique_ptr<ElementNodeIterator> &elementNodeIterator, 42 AamsWorkMode mode) override 43 { 44 // copy ele 45 auto eleCopy = windowNodeMap.at(winInfo.id_); 46 elementNodeIterator = std::make_unique<MockElementNodeIterator>(eleCopy); 47 return true; 48 } 49 IsWorkable()50 bool IsWorkable() const override 51 { 52 return true; 53 } 54 InjectTouchEventSequence(const PointerMatrix & events)55 void InjectTouchEventSequence(const PointerMatrix &events) const override 56 { 57 touch_event_records_ = std::make_unique<PointerMatrix>(events.GetFingers(), events.GetSteps()); 58 for (int step = 0; step < events.GetSteps(); step++) { 59 for (int finger = 0; finger < events.GetFingers(); finger++) { 60 touch_event_records_->PushAction(events.At(finger, step)); 61 } 62 } 63 } 64 AddWindowsAndNode(Window in,std::vector<MockAccessibilityElementInfo> eles)65 void AddWindowsAndNode(Window in, std::vector<MockAccessibilityElementInfo> eles) 66 { 67 testIn.emplace(in.id_, in); 68 windowNodeMap.emplace(in.id_, eles); 69 } RemoveWindowsAndNode(Window in)70 void RemoveWindowsAndNode(Window in) 71 { 72 testIn.erase(in.id_); 73 windowNodeMap.erase(in.id_); 74 } 75 76 private: 77 std::map<int, Window> testIn; 78 std::map<int, std::vector<MockAccessibilityElementInfo>> windowNodeMap; 79 }; 80 } // namespace OHOS::uitest 81 #endif