• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 The function is called while accessibility System check the id of window is not equal
142      * to the id of active window when sendAccessibility.
143      */
144 
145     void ClearFocus() override;
146 
147     /**
148      * @brief the low layer is notified by the function called while accessibility system execute
149      * the function of executeAction from AS to check the all low windows cared the outside event.
150      * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
151      */
152     void OutsideTouch() override;
153 
154     /**
155      * @brief Get the window id related with operator object
156      * @param windowId: The unique id of the window related with operator object.
157      */
158     int32_t GetWindowId();
159 
160     /**
161      * @brief Set the element information by accessibility id to AA.
162      * @param infos The element info searched by accessibility id.
163      * @param requestId The request id from AA, it is used to match with request and response.
164      */
165     void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
166         const int32_t requestId);
167 
168     /**
169      * @brief Set the element information by window id to AA.
170      * @param infos The element info searched by window id.
171      * @param requestId The request id from AA, it is used to match with request and response.
172      */
173     void SetSearchDefaultFocusByWindowIdResult(const std::list<AccessibilityElementInfo> &infos,
174         const int32_t requestId);
175 
176     /**
177      * @brief Set the element information matched with text to AA.
178      * @param infos The element information searched matched with text.
179      * @param requestId The request id from AA, it is used to match with request and response.
180      */
181     void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
182         const int32_t requestId);
183 
184     /**
185      * @brief Set the element information matched with focus type to AA.
186      * @param info The element information searched matched with focus type.
187      * @param requestId The request id from AA, it is used to match with request and response.
188      */
189     void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
190 
191     /**
192      * @brief Set the element information by focus direction to AA.
193      * @param info The element information searched by focus direction.
194      * @param requestId The request id from AA, it is used to match with request and response.
195      */
196     void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
197 
198     /**
199      * @brief Set the result of action executed to AA.
200      * @param succeeded True: The action is executed successfully; otherwise is false.
201      * @param requestId The request id from AA, it is used to match with request and response.
202      */
203     void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
204 
205     /**
206      * @brief Set the result of action executed to AA.
207      * @param cursorPosition Mark number for cursor position.
208      * @param requestId The request id from AA, it is used to match with request and response.
209      */
210     void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId);
211 
212     /**
213     * @brief To return the result of elementId, treeId, childWindowId.
214     * @param elementId: The element Id.
215     * @param treeId: The tree Id.
216     * @param childWindowId The childwindow Id.
217     */
218     void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t treeId, const int32_t childWindowId);
219 
220     /**
221     * @brief To return the result of treeId
222     * @param treeId: The tree Id.
223     */
224     void SetBelongTreeId(const int32_t treeId);
225 
226     /**
227     * @brief To return the result of parentWindowId
228     * @param parentWindowId: The parentWindow Id.
229     */
230     void SetParentWindowId(const int32_t parentWindowId);
231 
232 private:
233     int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
234 
235     ffrt::mutex requestsMutex_;
236     int32_t windowId_ = 0;
237     AccessibilityElementOperatorCallback &operatorCallback_;
238     std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
239     std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
240 };
241 
242 template <class T>
TranslateListToVector(const std::list<T> & originList)243 std::vector<T> TranslateListToVector(const std::list<T>& originList)
244 {
245     std::size_t len = originList.size();
246     std::vector<T> destVector(len);
247     std::copy(originList.begin(), originList.end(), destVector.begin());
248     return destVector;
249 }
250 
251 template <class T>
TranslateVectorToList(const std::vector<T> & originVector)252 std::list<T> TranslateVectorToList(const std::vector<T>& originVector)
253 {
254     size_t len = originVector.length();
255     std::list<T> destList(len);
256     std::copy(originVector.begin(), originVector.end(), destList.begin());
257     return destList;
258 }
259 } // namespace Accessibility
260 } // namespace OHOS
261 #endif // MOCK_ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H