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 RetError 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 node information by window ID and set the result by callback.
63 * @param window: The window 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 mode PREFETCH_PREDECESSORS: Need to make the parent node info also.
67 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also.
68 * PREFETCH_CHILDREN: Need to make the child node info also.
69 * otherwise: Make the node information by elementId only.
70 */
71 void SearchDefaultFocusedByWindowId(const int32_t windowId, const int32_t requestId,
72 const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
73 bool isFilter = false) override;
74
75 /**
76 * @brief Make the child node information by accessibility ID and filtered by text and set the result by callback.
77 * @param elementId: The unique id of the component ID.
78 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
79 * @param callback To transfer the node info to ASAC and it defined by ASAC.
80 * @param text Filter for the child components to matched with the text
81 */
82 void SearchElementInfosByText(const int64_t elementId, const std::string& text, const int32_t requestId,
83 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
84
85 /**
86 * @brief Make the node information of the component focused by the focus type specified.
87 * @param elementId: The unique id of the component ID.
88 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
89 * @param callback To transfer the node info to ASAC and it defined by ASAC.
90 * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
91 * FOCUS_TYPE_INPUT: text input focus
92 */
93 void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
94 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
95
96 /**
97 * @brief Make the node info by current focus move direction.
98 * @param elementId: The unique id of the component ID.
99 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
100 * @param callback To transfer the node info to ASAC and it defined by ASAC.
101 * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
102 */
103 void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
104 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
105
106 /**
107 * @brief To return the result of perform action.
108 * @param elementId: The unique id of the component ID.
109 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
110 * @param callback To transfer the node info to ASAC and it defined by ASAC.
111 * @param action Refer to [AccessibilityElementInfo.ActionType]
112 * @param actionArguments The parameter for action type. such as:
113 * action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
114 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
115 * action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
116 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
117 * action: ACCESSIBILITY_ACTION_NEXT_TEXT,
118 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
119 * action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
120 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
121 * action: ACCESSIBILITY_ACTION_SET_SELECTION,
122 * actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
123 * {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
124 * action: ACCESSIBILITY_ACTION_SET_TEXT,
125 * actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
126 */
127 void ExecuteAction(const int64_t elementId, const int32_t action,
128 const std::map<std::string, std::string> &actionArguments, int32_t requestId,
129 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
130
131 /**
132 * @brief To return the result of cursor position.
133 * @param elementId: The unique id of the component ID.
134 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
135 * @param callback To transfer the node info to ASAC and it defined by ASAC.
136 */
137 void GetCursorPosition(const int64_t elementId, int32_t requestId,
138 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
139
140 /**
141 * @brief Search element information by specific property.
142 * @param elementId: The unique id of the component ID.
143 * @param param The specific property parameters.
144 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
145 * @param callback To transfer the node info to ASAC and it defined by ASAC.
146 */
147 void SearchElementInfoBySpecificProperty(const int64_t elementId,
148 const SpecificPropertyParam& param, const int32_t requestId,
149 const sptr<IAccessibilityElementOperatorCallback>& callback) override;
150
151 /**
152 * @brief The function is called while accessibility System check the id of window is not equal
153 * to the id of active window when sendAccessibility.
154 */
155
156 void ClearFocus() override;
157
158 /**
159 * @brief the low layer is notified by the function called while accessibility system execute
160 * the function of executeAction from AS to check the all low windows cared the outside event.
161 * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
162 */
163 void OutsideTouch() override;
164
165 /**
166 * @brief Get the window id related with operator object
167 * @param windowId: The unique id of the window related with operator object.
168 */
169 int32_t GetWindowId();
170
171 /**
172 * @brief Set the element information by accessibility id to AA.
173 * @param infos The element info searched by accessibility id.
174 * @param requestId The request id from AA, it is used to match with request and response.
175 */
176 void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
177 const int32_t requestId);
178
179 /**
180 * @brief Set the element information by window id to AA.
181 * @param infos The element info searched by window id.
182 * @param requestId The request id from AA, it is used to match with request and response.
183 */
184 void SetSearchDefaultFocusByWindowIdResult(const std::list<AccessibilityElementInfo> &infos,
185 const int32_t requestId);
186
187 /**
188 * @brief Set the element information matched with text to AA.
189 * @param infos The element information searched matched with text.
190 * @param requestId The request id from AA, it is used to match with request and response.
191 */
192 void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
193 const int32_t requestId);
194
195 /**
196 * @brief Set the element information matched with focus type to AA.
197 * @param info The element information searched matched with focus type.
198 * @param requestId The request id from AA, it is used to match with request and response.
199 */
200 void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
201
202 /**
203 * @brief Set the element information by focus direction to AA.
204 * @param info The element information searched by focus direction.
205 * @param requestId The request id from AA, it is used to match with request and response.
206 */
207 void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
208
209 /**
210 * @brief Set the result of action executed to AA.
211 * @param succeeded True: The action is executed successfully; otherwise is false.
212 * @param requestId The request id from AA, it is used to match with request and response.
213 */
214 void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
215
216 /**
217 * @brief Set the result of action executed to AA.
218 * @param cursorPosition Mark number for cursor position.
219 * @param requestId The request id from AA, it is used to match with request and response.
220 */
221 void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId);
222
223 /**
224 * @brief To return the result of elementId, treeId, childWindowId.
225 * @param elementId: The element Id.
226 * @param treeId: The tree Id.
227 * @param childWindowId The childwindow Id.
228 */
229 void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t treeId, const int32_t childWindowId);
230
231 /**
232 * @brief To return the result of treeId
233 * @param treeId: The tree Id.
234 */
235 void SetBelongTreeId(const int32_t treeId);
236
237 /**
238 * @brief To return the result of parentWindowId
239 * @param parentWindowId: The parentWindow Id.
240 */
241 void SetParentWindowId(const int32_t parentWindowId);
242
243 private:
244 int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
245
246 ffrt::mutex requestsMutex_;
247 int32_t windowId_ = 0;
248 AccessibilityElementOperatorCallback &operatorCallback_;
249 std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
250 std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
251 };
252
253 template <class T>
TranslateListToVector(const std::list<T> & originList)254 std::vector<T> TranslateListToVector(const std::list<T>& originList)
255 {
256 std::size_t len = originList.size();
257 std::vector<T> destVector(len);
258 std::copy(originList.begin(), originList.end(), destVector.begin());
259 return destVector;
260 }
261
262 template <class T>
TranslateVectorToList(const std::vector<T> & originVector)263 std::list<T> TranslateVectorToList(const std::vector<T>& originVector)
264 {
265 size_t len = originVector.length();
266 std::list<T> destList(len);
267 std::copy(originVector.begin(), originVector.end(), destList.begin());
268 return destList;
269 }
270 } // namespace Accessibility
271 } // namespace OHOS
272 #endif // MOCK_ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H