• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::vector<Window> & out)31         void GetUiWindows(std::vector<Window> &out) override
32         {
33             for (auto iter = testIn.cbegin(); iter != testIn.cend(); ++iter) {
34                 out.emplace_back(iter->second);
35             }
36         }
37 
GetWidgetsInWindow(const Window & winInfo,std::unique_ptr<ElementNodeIterator> & elementNodeIterator)38         bool GetWidgetsInWindow(const Window &winInfo,
39                                 std::unique_ptr<ElementNodeIterator> &elementNodeIterator) override
40         {
41             // copy ele
42             auto eleCopy = windowNodeMap.at(winInfo.id_);
43             elementNodeIterator = std::make_unique<MockElementNodeIterator>(eleCopy);
44             return true;
45         }
46 
IsWorkable()47         bool IsWorkable() const override
48         {
49             return true;
50         }
51 
InjectTouchEventSequence(const PointerMatrix & events)52         void InjectTouchEventSequence(const PointerMatrix &events) const override
53         {
54             touch_event_records_ = std::make_unique<PointerMatrix>(events.GetFingers(), events.GetSteps());
55             for (int step = 0; step < events.GetSteps(); step++) {
56                 for (int finger = 0; finger < events.GetFingers(); finger++) {
57                     touch_event_records_->PushAction(events.At(finger, step));
58                 }
59             }
60         }
61 
AddWindowsAndNode(Window in,std::vector<MockAccessibilityElementInfo> eles)62         void AddWindowsAndNode(Window in, std::vector<MockAccessibilityElementInfo> eles)
63         {
64             testIn.emplace(in.id_, in);
65             windowNodeMap.emplace(in.id_, eles);
66         }
RemoveWindowsAndNode(Window in)67         void RemoveWindowsAndNode(Window in)
68         {
69             testIn.erase(in.id_);
70             windowNodeMap.erase(in.id_);
71         }
72 
73     private:
74         std::map<int, Window> testIn;
75         std::map<int, std::vector<MockAccessibilityElementInfo>> windowNodeMap;
76     };
77 } // namespace OHOS::uitest
78 #endif