1 /*
2  * Copyright (C) 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 MOCK_ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
17 #define MOCK_ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include "accessibility_element_operator.h"
22 #include "accessibility_element_operator_callback.h"
23 #include "accessibility_element_operator_stub.h"
24 #include "ffrt.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace Accessibility {
29 /*
30  * The class define the interface for UI to implement.
31  * It is triggered by ABMS when AA to request the accessibility information.
32  */
33 class MockAccessibilityElementOperatorImpl : public AccessibilityElementOperatorStub {
34 public:
35     /**
36      * @brief construct function
37      * @param object The object of IPC
38      */
39     explicit MockAccessibilityElementOperatorImpl(int32_t windowId,
40         const std::shared_ptr<AccessibilityElementOperator> &operation,
41         AccessibilityElementOperatorCallback &callback);
42 
43     /**
44      * @brief destruct function
45      */
46     ~MockAccessibilityElementOperatorImpl();
47 
48     /**
49      * @brief Make the node information by accessibility ID and set the result by callback.
50      * @param elementId: The unique id of the component ID.
51      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
52      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
53      * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also.
54      *              PREFETCH_SIBLINGS: Need to make the sister/brothers node info also.
55      *              PREFETCH_CHILDREN: Need to make the child node info also.
56      *              otherwise: Make the node information by elementId only.
57      */
58     void SearchElementInfoByAccessibilityId(const int64_t elementId, const int32_t requestId,
59         const sptr<IAccessibilityElementOperatorCallback>& callback, const int32_t mode, bool isFilter) override;
60 
61     /**
62      * @brief Make the child node information by accessibility ID and filtered by text and set the result by callback.
63      * @param elementId: The unique id of the component ID.
64      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
65      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
66      * @param text  Filter for the child components to matched with the text
67      */
68     void SearchElementInfosByText(const int64_t elementId, const std::string& text, const int32_t requestId,
69         const sptr<IAccessibilityElementOperatorCallback>& callback) override;
70 
71     /**
72      * @brief Make the node information of the component focused by the focus type specified.
73      * @param elementId: The unique id of the component ID.
74      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
75      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
76      * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
77      *                  FOCUS_TYPE_INPUT: text input focus
78      */
79     void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
80         const sptr<IAccessibilityElementOperatorCallback>& callback) override;
81 
82     /**
83      * @brief Make the node info by current focus move direction.
84      * @param elementId: The unique id of the component ID.
85      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
86      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
87      * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
88      */
89     void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
90         const sptr<IAccessibilityElementOperatorCallback>& callback) override;
91 
92     /**
93      * @brief To return the result of perform action.
94      * @param elementId: The unique id of the component ID.
95      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
96      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
97      * @param action Refer to [AccessibilityElementInfo.ActionType]
98      * @param actionArguments The parameter for action type. such as:
99      *      action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
100      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
101      *      action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
102      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
103      *      action: ACCESSIBILITY_ACTION_NEXT_TEXT,
104      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
105      *      action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
106      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
107      *      action: ACCESSIBILITY_ACTION_SET_SELECTION,
108      *                  actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
109      *                                  {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
110      *      action: ACCESSIBILITY_ACTION_SET_TEXT,
111      *                  actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
112      */
113     void ExecuteAction(const int64_t elementId, const int32_t action,
114         const std::map<std::string, std::string> &actionArguments, int32_t requestId,
115         const sptr<IAccessibilityElementOperatorCallback>& callback) override;
116 
117     /**
118      * @brief To return the result of cursor position.
119      * @param elementId: The unique id of the component ID.
120      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
121      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
122      */
123     void GetCursorPosition(const int64_t elementId, int32_t requestId,
124         const sptr<IAccessibilityElementOperatorCallback>& callback) override;
125 
126     /**
127      * @brief The function is called while accessibility System check the id of window is not equal
128      * to the id of active window when sendAccessibility.
129      */
130 
131     void ClearFocus() override;
132 
133     /**
134      * @brief the low layer is notified by the function called while accessibility system execute
135      * the function of executeAction from AS to check the all low windows cared the outside event.
136      * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
137      */
138     void OutsideTouch() override;
139 
140     /**
141      * @brief Get the window id related with operator object
142      * @param windowId: The unique id of the window related with operator object.
143      */
144     int32_t GetWindowId();
145 
146     /**
147      * @brief Set the element information by accessibility id to AA.
148      * @param infos The element info searched by accessibility id.
149      * @param requestId The request id from AA, it is used to match with request and response.
150      */
151     void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
152         const int32_t requestId);
153 
154     /**
155      * @brief Set the element information matched with text to AA.
156      * @param infos The element information searched matched with text.
157      * @param requestId The request id from AA, it is used to match with request and response.
158      */
159     void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
160         const int32_t requestId);
161 
162     /**
163      * @brief Set the element information matched with focus type to AA.
164      * @param info The element information searched matched with focus type.
165      * @param requestId The request id from AA, it is used to match with request and response.
166      */
167     void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
168 
169     /**
170      * @brief Set the element information by focus direction to AA.
171      * @param info The element information searched by focus direction.
172      * @param requestId The request id from AA, it is used to match with request and response.
173      */
174     void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
175 
176     /**
177      * @brief Set the result of action executed to AA.
178      * @param succeeded True: The action is executed successfully; otherwise is false.
179      * @param requestId The request id from AA, it is used to match with request and response.
180      */
181     void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
182 
183     /**
184     * @brief To return the result of elementId, treeId, childWindowId.
185     * @param elementId: The element Id.
186     * @param treeId: The tree Id.
187     * @param childWindowId The childwindow Id.
188     */
189     void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t treeId, const int32_t childWindowId);
190 
191     /**
192     * @brief To return the result of treeId
193     * @param treeId: The tree Id.
194     */
195     void SetBelongTreeId(const int32_t treeId);
196 
197     /**
198     * @brief To return the result of parentWindowId
199     * @param parentWindowId: The parentWindow Id.
200     */
201     void SetParentWindowId(const int32_t parentWindowId);
202 
203 private:
204     int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
205 
206     ffrt::mutex requestsMutex_;
207     int32_t windowId_ = 0;
208     AccessibilityElementOperatorCallback &operatorCallback_;
209     std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
210     std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
211 };
212 
213 template <class T>
TranslateListToVector(const std::list<T> & originList)214 std::vector<T> TranslateListToVector(const std::list<T>& originList)
215 {
216     std::size_t len = originList.size();
217     std::vector<T> destVector(len);
218     std::copy(originList.begin(), originList.end(), destVector.begin());
219     return destVector;
220 }
221 
222 template <class T>
TranslateVectorToList(const std::vector<T> & originVector)223 std::list<T> TranslateVectorToList(const std::vector<T>& originVector)
224 {
225     size_t len = originVector.length();
226     std::list<T> destList(len);
227     std::copy(originVector.begin(), originVector.end(), destList.begin());
228     return destList;
229 }
230 } // namespace Accessibility
231 } // namespace OHOS
232 #endif // MOCK_ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H