1 /*
2  * Copyright (c) 2020-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 /**
17  * @addtogroup UI_DFX
18  * @{
19  *
20  * @brief Provides test and analysis capabilities, such as stimulating input events and viewing information about a
21  *        Document Object Model (DOM) tree.
22  *
23  * @since 1.0
24  * @version 1.0
25  */
26 
27 /**
28  * @file event_injector.h
29  *
30  * @brief Declares functions for simulating input events.
31  *
32  * @since 1.0
33  * @version 1.0
34  */
35 #ifndef GRAPHIC_LITE_EVENT_INJECTOR_H
36 #define GRAPHIC_LITE_EVENT_INJECTOR_H
37 
38 #include "graphic_config.h"
39 #if ENABLE_DEBUG
40 #include "gfx_utils/heap_base.h"
41 #include "gfx_utils/input_event_info.h"
42 namespace OHOS {
43 /**
44  * @ Enumerates the event types.
45  */
46 enum class EventDataType {
47     /** Point event */
48     POINT_TYPE,
49     /** Key event */
50     KEY_TYPE,
51     /** Other events */
52     OTHERS
53 };
54 class PointEventInjector;
55 class KeyEventInjector;
56 
57 /**
58  * @brief Manages all types of simulated input events, registers and unregisters event injectors, and simulates
59  * input events.
60  *
61  * @since 1.0
62  * @version 1.0
63  */
64 class EventInjector : public HeapBase {
65 public:
66     /**
67      * @brief Obtains a singleton <b>EventInjector</b> instance.
68      *
69      * @return Returns the <b>EventInjector</b> instance.
70      * @since 1.0
71      * @version 1.0
72      */
73     static EventInjector* GetInstance();
74 
75     /**
76      * @brief Registers an event injector of a specified type.
77      *
78      * @param type Indicates the event type. For details, see {@link EventDataType}.
79      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
80      * @since 1.0
81      * @version 1.0
82      */
83     bool RegisterEventInjector(EventDataType type);
84 
85     /**
86      * @brief Unregisters an event injector of a specified type.
87      *
88      * @param type Indicates the event type. For details, see {@link EventDataType}.
89      * @since 1.0
90      * @version 1.0
91      */
92     void UnregisterEventInjector(EventDataType type);
93 
94     /**
95      * @brief Checks whether the event injector of a specified type is registered.
96      *
97      * @param type Indicates the event type. For details, see {@link EventDataType}.
98      * @return Returns <b>true</b> if the device is registered; returns <b>false</b> otherwise.
99      * @since 1.0
100      * @version 1.0
101      */
102     bool IsEventInjectorRegistered(EventDataType type) const;
103 
104     /**
105      * @brief Uses a data array of a specified length to simulate an input event of a specified type.
106      *
107      * @param dataArray Indicates the pointer to the data array used for simulating the event.
108      * @param arrayLength Indicates the length of the data array.
109      * @param type Indicates the event type. For details, see {@link EventDataType}.
110      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
111      * @since 1.0
112      * @version 1.0
113      */
114     bool SetInjectEvent(const DeviceData* dataArray, uint16_t arrayLength, EventDataType type);
115 
116     /**
117      * @brief Stimulates a click event.
118      *
119      * @param clickPoint Indicates the coordinates of the click point.
120      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
121      * @since 3.0
122      * @version 5.0
123      */
124     bool SetClickEvent(const Point& clickPoint);
125 
126     /**
127      * @brief Stimulates a long press event.
128      *
129      * @param longPressPoint Indicates the coordinates of the long press point.
130      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
131      * @since 3.0
132      * @version 5.0
133      */
134     bool SetLongPressEvent(const Point& longPressPoint);
135 
136     /**
137      * @brief Stimulates a drag event that occurs between two points.
138      *
139      * @param startPoint Indicates the coordinates of the start point.
140      * @param endPoint Indicates the coordinates of the end point.
141      * @param dragTime Indicates the duration of dragging from the start point to the end point, in milliseconds.
142      *                 The value range is [2, 499] x {@link INDEV_READ_PERIOD}.
143      *                 The shorter the duration is, the faster the sliding is.
144      *
145      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
146      * @since 3.0
147      * @version 5.0
148      */
149     bool SetDragEvent(const Point& startPoint, const Point& endPoint, uint32_t dragTime);
150 
151     /**
152      * @brief Simulates a key event.
153      *
154      * @param keyId Indicates the key ID.
155      * @param state Indicates the key state. For details, see {@link InputDevice}.
156      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> otherwise.
157      * @since 3.0
158      * @version 5.0
159      */
160     bool SetKeyEvent(uint16_t keyId, uint16_t state);
161 
162 #if ENABLE_WINDOW
163     /**
164      * @brief Set id of mock window.
165      *
166      * @param windowId id of mock window
167      *
168      * @since 3.0
169      * @version 5.0
170      */
171     void SetWindowId(uint8_t windowId);
172 #endif
173 
174 private:
EventInjector()175     EventInjector() : pointEventInjector_(nullptr), keyEventInjector_(nullptr) {}
176     virtual ~EventInjector();
177 
178     EventInjector(const EventInjector&) = delete;
179     EventInjector& operator=(const EventInjector&) = delete;
180     EventInjector(EventInjector&&) = delete;
181     EventInjector& operator=(EventInjector&&) = delete;
182 
183     PointEventInjector* pointEventInjector_;
184     KeyEventInjector* keyEventInjector_;
185 };
186 } // namespace OHOS
187 #endif // ENABLE_DEBUG
188 #endif // GRAPHIC_LITE_EVENT_INJECTOR_H
189