1 /*
2  * Copyright (C) 2021 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 ACCESSIBILITY_UT_HELPER_H
17 #define ACCESSIBILITY_UT_HELPER_H
18 
19 #include "accessibility_event_info.h"
20 #include "ffrt.h"
21 #include "hilog/log.h"
22 #include "iremote_object.h"
23 
24 namespace OHOS {
25 namespace Accessibility {
26 class AccessibilityAbilityHelper {
27 public:
GetInstance()28     static AccessibilityAbilityHelper& GetInstance()
29     {
30         static AccessibilityAbilityHelper helper;
31         return helper;
32     }
33 
GetTouchEventActionVector()34     std::vector<int32_t> GetTouchEventActionVector()
35     {
36         std::lock_guard<ffrt::mutex> lock(mtx_);
37         return touchAction_;
38     }
39 
GetTouchEventActionOfTargetIndex(int32_t index)40     int32_t GetTouchEventActionOfTargetIndex(int32_t index)
41     {
42         std::lock_guard<ffrt::mutex> lock(mtx_);
43         int32_t size = static_cast<int32_t>(touchAction_.size());
44         if (size > index) {
45             return touchAction_[index];
46         }
47         return -1;
48     }
49 
ClearTouchEventActionVector()50     void ClearTouchEventActionVector()
51     {
52         std::lock_guard<ffrt::mutex> lock(mtx_);
53         touchAction_.clear();
54     }
55 
SetTouchEventActionVectors(int32_t touchAction)56     void SetTouchEventActionVectors(int32_t touchAction)
57     {
58         std::lock_guard<ffrt::mutex> lock(mtx_);
59         touchAction_.push_back(touchAction);
60     }
61 
GetKeyCode()62     int32_t GetKeyCode() const
63     {
64         return keyCode_;
65     }
66 
ClearKeyCode()67     void ClearKeyCode()
68     {
69         keyCode_ = 0;
70     }
71 
SetKeyCode(int32_t keyCode)72     void SetKeyCode(int32_t keyCode)
73     {
74         keyCode_ = keyCode;
75     }
76 
GetDestroyState()77     bool GetDestroyState()
78     {
79         return isDestroyEvents_;
80     }
81 
SetDestroyState()82     void SetDestroyState()
83     {
84         isDestroyEvents_ = true;
85     }
86 
GetTestElementId()87     int GetTestElementId()
88     {
89         return testElementId_;
90     }
91 
SetTestElementId(int ElementId)92     void SetTestElementId(int ElementId)
93     {
94         testElementId_ = ElementId;
95     }
96 
GetTestChannelElementId()97     int GetTestChannelElementId()
98     {
99         return testChannelElementId_;
100     }
101 
SetTestChannelElementId(int ElementId)102     void SetTestChannelElementId(int ElementId)
103     {
104         testChannelElementId_ = ElementId;
105     }
106 
GetTestChannelRequestId()107     int GetTestChannelRequestId()
108     {
109         return testChannelrequestId_;
110     }
111 
SetTestChannelRequestId(int ElementId)112     void SetTestChannelRequestId(int ElementId)
113     {
114         testChannelrequestId_ = ElementId;
115     }
116 
GetTestChannelMode()117     int GetTestChannelMode()
118     {
119         return testChannelmode_;
120     }
121 
SetTestChannelMode(int ElementId)122     void SetTestChannelMode(int ElementId)
123     {
124         testChannelmode_ = ElementId;
125     }
126 
GetTestClearFocus()127     int GetTestClearFocus()
128     {
129         return testClearFocus_;
130     }
131 
SetTestClearFocus(int ElementId)132     void SetTestClearFocus(int ElementId)
133     {
134         testClearFocus_ = ElementId;
135     }
136 
GetTestOutsideTouch()137     int GetTestOutsideTouch()
138     {
139         return testOutsideTouch_;
140     }
141 
SetTestOutsideTouch(int ElementId)142     void SetTestOutsideTouch(int ElementId)
143     {
144         testOutsideTouch_ = ElementId;
145     }
146 
GetGestureId()147     int GetGestureId()
148     {
149         return uTgestureId_;
150     }
151 
SetGestureId(int gestureId)152     void SetGestureId(int gestureId)
153     {
154         uTgestureId_ = gestureId;
155     }
156 
GetEventTypeVector()157     std::vector<EventType> GetEventTypeVector()
158     {
159         std::lock_guard<ffrt::mutex> lock(mtx_);
160         return eventType_;
161     }
162 
GetEventTypeOfTargetIndex(int32_t index)163     EventType GetEventTypeOfTargetIndex(int32_t index)
164     {
165         std::lock_guard<ffrt::mutex> lock(mtx_);
166         int32_t size = static_cast<int32_t>(eventType_.size());
167         if (size > index) {
168             return eventType_[index];
169         }
170         return TYPE_VIEW_INVALID;
171     }
172 
ClearEventTypeActionVector()173     void ClearEventTypeActionVector()
174     {
175         std::lock_guard<ffrt::mutex> lock(mtx_);
176         eventType_.clear();
177     }
178 
SetEventTypeVector(EventType eventType)179     void SetEventTypeVector(EventType eventType)
180     {
181         std::lock_guard<ffrt::mutex> lock(mtx_);
182         eventType_.push_back(eventType);
183     }
184 
GetTestChannelId()185     int GetTestChannelId()
186     {
187         return testChannelId_;
188     }
189 
SetTestChannelId(int ChannalId)190     void SetTestChannelId(int ChannalId)
191     {
192         testChannelId_ = ChannalId;
193     }
194 
GetTestEventType()195     int GetTestEventType()
196     {
197         return testEventType_;
198     }
199 
SetTestEventType(int EventType)200     void SetTestEventType(int EventType)
201     {
202         testEventType_ = EventType;
203     }
204 
GetTestGesture()205     int GetTestGesture()
206     {
207         return testGesture_;
208     }
209 
SetTestGesture(int Gesture)210     void SetTestGesture(int Gesture)
211     {
212         testGesture_ = Gesture;
213     }
214 
GetTestKeyPressEvent()215     int GetTestKeyPressEvent()
216     {
217         return testKeyPressEvent_;
218     }
219 
SetTestKeyPressEvent(int KeyPressEvent)220     void SetTestKeyPressEvent(int KeyPressEvent)
221     {
222         testKeyPressEvent_ = KeyPressEvent;
223     }
224 
GetTestDisplayId()225     int GetTestDisplayId()
226     {
227         return testDisplayId_;
228     }
229 
SetTestDisplayId(int DisplayId)230     void SetTestDisplayId(int DisplayId)
231     {
232         testDisplayId_ = DisplayId;
233     }
234 
GetTestStateType()235     int GetTestStateType()
236     {
237         return testStateType_;
238     }
239 
SetTestStateType(int StateType)240     void SetTestStateType(int StateType)
241     {
242         testStateType_ = StateType;
243     }
244 
GetTestKeyEvent()245     bool GetTestKeyEvent()
246     {
247         return testKeyEvent_;
248     }
249 
SetTestKeyEvent(bool KeyEvent)250     void SetTestKeyEvent(bool KeyEvent)
251     {
252         testKeyEvent_ = true;
253     }
254 
GetSendEventTimes()255     int GetSendEventTimes()
256     {
257         return sendEventTimes_;
258     }
259 
AddSendEventTimes()260     void AddSendEventTimes()
261     {
262         sendEventTimes_++;
263     }
264 
ClearSendEventTimes()265     void ClearSendEventTimes()
266     {
267         sendEventTimes_ = 0;
268     }
269 
GetTestSequence()270     uint32_t GetTestSequence() const
271     {
272         return testSequence_;
273     }
274 
SetTestSequence(uint32_t sequenceNum)275     void SetTestSequence(uint32_t sequenceNum)
276     {
277         testSequence_ = sequenceNum;
278     }
279 
SetShortKeyTargetAbilityState(bool state)280     void SetShortKeyTargetAbilityState(bool state)
281     {
282         enableShortKeyTargetAbility_ = state;
283     }
284 
GetShortKeyTargetAbilityState()285     bool GetShortKeyTargetAbilityState() const
286     {
287         return enableShortKeyTargetAbility_;
288     }
289 
SetZoomState(bool state)290     void SetZoomState(bool state)
291     {
292         zoomState_ = state;
293     }
294 
GetZoomState()295     bool GetZoomState() const
296     {
297         return zoomState_;
298     }
299 
SetScrollState(bool state)300     void SetScrollState(bool state)
301     {
302         scrollState_ = state;
303     }
304 
GetScrollState()305     bool GetScrollState() const
306     {
307         return scrollState_;
308     }
309 
AddUserId(int32_t id)310     void AddUserId(int32_t id)
311     {
312         userIds_.emplace_back(id);
313     }
314 
RemoveUserId(int32_t id)315     void RemoveUserId(int32_t id)
316     {
317         for (auto iter = userIds_.begin(); iter != userIds_.end();) {
318             if (*iter == id) {
319                 iter = userIds_.erase(iter);
320             } else {
321                 iter++;
322             }
323         }
324     }
325 
GetUserIds(std::vector<int32_t> & userIds)326     void GetUserIds(std::vector<int32_t> &userIds) const
327     {
328         for (auto& id : userIds_) {
329             userIds.emplace_back(id);
330         }
331     }
332 
AddPackage(const std::string & name)333     void AddPackage(const std::string &name)
334     {
335         packages_.emplace_back(name);
336     }
337 
RemovePackage(const std::string & name)338     void RemovePackage(const std::string &name)
339     {
340         for (auto iter = packages_.begin(); iter != packages_.end();) {
341             if (*iter == name) {
342                 iter = packages_.erase(iter);
343             } else {
344                 iter++;
345             }
346         }
347     }
348 
GetPackages(std::vector<std::string> & packages)349     void GetPackages(std::vector<std::string> &packages) const
350     {
351         for (auto& name : packages_) {
352             packages.emplace_back(name);
353         }
354     }
355 
SetCurrentUserId(int32_t id)356     void SetCurrentUserId(int32_t id)
357     {
358         currentUserId_ = id;
359     }
360 
GetCurrentUserId()361     int32_t GetCurrentUserId() const
362     {
363         return currentUserId_;
364     }
365 
ChangePackage(bool isChanged)366     void ChangePackage(bool isChanged)
367     {
368         isPackageChanged_ = isChanged;
369     }
370 
GetChangePackageFlag()371     bool GetChangePackageFlag() const
372     {
373         return isPackageChanged_;
374     }
375 
SetNeedAccountDataNullFlag(bool flag)376     void SetNeedAccountDataNullFlag(bool flag)
377     {
378         needAccountDataNull_ = flag;
379     }
380 
GetNeedAccountDataNullFlag()381     bool GetNeedAccountDataNullFlag() const
382     {
383         return needAccountDataNull_;
384     }
385 
SetEventWindowId(int32_t windowId)386     void SetEventWindowId(int32_t windowId)
387     {
388         eventWindowId_ = windowId;
389     }
390 
GetEventWindowId()391     int32_t GetEventWindowId() const
392     {
393         return eventWindowId_;
394     }
395 
SetEventWindowChangeType(WindowUpdateType type)396     void SetEventWindowChangeType(WindowUpdateType type)
397     {
398         eventWindowChangeType_ = type;
399     }
400 
GetEventWindowChangeType()401     WindowUpdateType GetEventWindowChangeType() const
402     {
403         return eventWindowChangeType_;
404     }
405 
SetRealId(int32_t realId)406     void SetRealId(int32_t realId)
407     {
408         realId_ = realId;
409     }
410 
GetRealId()411     int32_t GetRealId() const
412     {
413         return realId_;
414     }
415 
SetExecuteAction(int32_t action)416     void SetExecuteAction(int32_t action)
417     {
418         executeAction_ = action;
419     }
420 
GetExecuteAction()421     int32_t GetExecuteAction() const
422     {
423         return executeAction_;
424     }
425 public:
426     static const int32_t accountId_ = 100;
427 
428 private:
429     std::vector<int32_t> touchAction_;
430     bool isDestroyEvents_ = false;
431     int testElementId_ = -1;
432     int testClearFocus_ = -1;
433     int testOutsideTouch_ = -1;
434     int testChannelElementId_ = -1;
435     int testChannelrequestId_ = -1;
436     int testChannelmode_ = -1;
437     int uTgestureId_;
438     std::vector<EventType> eventType_;
439     int testChannelId_ = -1;
440     int testEventType_ = -1;
441     int testGesture_ = -1;
442     int testKeyPressEvent_ = -1;
443     int testDisplayId_ = -1;
444     int testStateType_ = -1;
445     bool testKeyEvent_ = false;
446     std::atomic<int> sendEventTimes_ = 0;
447     uint32_t testSequence_ = 0;
448     int32_t keyCode_ = 0;
449     bool enableShortKeyTargetAbility_ = false;
450     bool zoomState_ = false;
451     bool scrollState_ = false;
452     ffrt::mutex mtx_;
453     std::vector<int32_t> userIds_;
454     std::vector<std::string> packages_;
455     int32_t currentUserId_ = -1;
456     bool isPackageChanged_ = false;
457     bool needAccountDataNull_ = false;
458     int32_t eventWindowId_ = -1;
459     WindowUpdateType eventWindowChangeType_ = WINDOW_UPDATE_INVALID;
460     int32_t realId_ = 0;
461     int32_t executeAction_ = ACCESSIBILITY_ACTION_INVALID;
462 };
463 } // namespace Accessibility
464 } // namespace OHOS
465 #endif // ACCESSIBILITY_UT_HELPER_H