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