• 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 #include "component_manager.h"
17 
18 #include "accessibility_ui_test_ability.h"
19 #include "multimode_manager.h"
20 
21 namespace OHOS {
22 namespace WuKong {
23 namespace {
24 const std::string permissionBundleName = "com.ohos.permissionmanager";
25 const int DIV = 2;
26 const int DOWNTIME = 10;
27 const int ONESECOND = 1000000;
28 const int TWOSECONDS = 2000000;
29 const int OFFSET = 10;
30 }  // namespace
31 
32 class ComponentEventMonitor : public Accessibility::AccessibleAbilityListener {
33 public:
34     virtual ~ComponentEventMonitor() = default;
35 
36     void OnAbilityConnected() override;
37 
38     void OnAbilityDisconnected() override;
39 
40     void OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& eventInfo) override;
41 
42     void SetOnAbilityConnectCallback(std::function<void()> onConnectCb);
43 
44     void SetOnAbilityDisConnectCallback(std::function<void()> onDisConnectCb);
45 
OnKeyPressEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)46     bool OnKeyPressEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) override
47     {
48         return false;
49     }
50 
51     uint64_t GetLastEventMillis();
52 
53     bool WaitEventIdle(uint32_t idleThresholdMs, uint32_t timeoutMs);
54 
55 private:
56     std::function<void()> onConnectCallback_ = nullptr;
57     std::function<void()> onDisConnectCallback_ = nullptr;
58     std::atomic<uint64_t> lastEventMillis_ = 0;
59 };
60 
SetOnAbilityConnectCallback(std::function<void ()> onConnectCb)61 void ComponentEventMonitor::SetOnAbilityConnectCallback(std::function<void()> onConnectCb)
62 {
63     onConnectCallback_ = std::move(onConnectCb);
64 }
65 
SetOnAbilityDisConnectCallback(std::function<void ()> onDisConnectCb)66 void ComponentEventMonitor::SetOnAbilityDisConnectCallback(std::function<void()> onDisConnectCb)
67 {
68     onDisConnectCallback_ = std::move(onDisConnectCb);
69 }
70 
OnAbilityConnected()71 void ComponentEventMonitor::OnAbilityConnected()
72 {
73     if (onConnectCallback_ != nullptr) {
74         onConnectCallback_();
75     }
76 }
77 
OnAbilityDisconnected()78 void ComponentEventMonitor::OnAbilityDisconnected()
79 {
80     if (onDisConnectCallback_ != nullptr) {
81         onDisConnectCallback_();
82     }
83 }
84 
OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo & eventInfo)85 void ComponentEventMonitor::OnAccessibilityEvent(const Accessibility::AccessibilityEventInfo& eventInfo)
86 {
87     TRACK_LOG_STR("OnAccessibilityEvent Start %u", eventInfo.GetEventType());
88     TRACK_LOG_STR("current bundle: %s", eventInfo.GetBundleName().c_str());
89     if (eventInfo.GetBundleName() == permissionBundleName) {
90         auto listenerlist = ComponentManager::GetInstance()->GetListenerList();
91         for (auto it : listenerlist) {
92             it->OnPermissionScreenShown();
93         }
94     }
95 }
96 
GetLastEventMillis()97 uint64_t ComponentEventMonitor::GetLastEventMillis()
98 {
99     return 0;
100 }
101 
WaitEventIdle(uint32_t idleThresholdMs,uint32_t timeoutMs)102 bool ComponentEventMonitor::WaitEventIdle(uint32_t idleThresholdMs, uint32_t timeoutMs)
103 {
104     return true;
105 }
106 
ComponentManager()107 ComponentManager::ComponentManager()
108 {
109     componentMap_ = {
110         {Accessibility::ACCESSIBILITY_ACTION_CLICK,
111          std::bind(&ComponentManager::ComponentTouchInput, this, std::placeholders::_1)},
112         {Accessibility::ACCESSIBILITY_ACTION_SCROLL_FORWARD,
113          std::bind(&ComponentManager::ComponentUpSwapInput, this, std::placeholders::_1)},
114 
115         {Accessibility::ACCESSIBILITY_ACTION_SCROLL_BACKWARD,
116          std::bind(&ComponentManager::ComponentDownSwapInput, this, std::placeholders::_1)},
117         {Accessibility::ACCESSIBILITY_ACTION_SET_TEXT,
118          std::bind(&ComponentManager::ComponentMultikeyInput, this, std::placeholders::_1)},
119         {COMPONENT_LEFT_SWAP, std::bind(&ComponentManager::ComponentLeftSwapInput, this, std::placeholders::_1)},
120     };
121 }
~ComponentManager()122 ComponentManager::~ComponentManager()
123 {
124 }
125 
Connect()126 bool ComponentManager::Connect()
127 {
128     if (connected_ == true) {
129         return true;
130     }
131     std::mutex mtx;
132     std::unique_lock<std::mutex> uLock(mtx);
133     std::shared_ptr<ComponentEventMonitor> g_monitorInstance_ = std::make_shared<ComponentEventMonitor>();
134     std::condition_variable condition;
135     auto onConnectCallback = [&condition]() {
136         std::cout << "Success connect to AAMS" << std::endl;
137         condition.notify_all();
138     };
139 
140     g_monitorInstance_->SetOnAbilityConnectCallback(onConnectCallback);
141     auto ability = Accessibility::AccessibilityUITestAbility::GetInstance();
142     if (ability->RegisterAbilityListener(g_monitorInstance_) != Accessibility::RET_OK) {
143         std::cout << "Failed to register ComponentEventMonitor" << std::endl;
144         return false;
145     }
146     std::cout << "Start connect to AAMS" << std::endl;
147     if (ability->Connect() != Accessibility::RET_OK) {
148         std::cout << "Failed to connect to AAMS" << std::endl;
149         return false;
150     }
151     const auto timeout = std::chrono::milliseconds(1000);
152     if (condition.wait_for(uLock, timeout) == std::cv_status::timeout) {
153         std::cout << "Wait connection to AAMS timed out" << std::endl;
154         return false;
155     }
156     connected_ = true;
157     return true;
158 }
159 
Disconnect()160 void ComponentManager::Disconnect()
161 {
162     auto auita = Accessibility::AccessibilityUITestAbility::GetInstance();
163     if (auita != nullptr) {
164         auita->Disconnect();
165     }
166     connected_ = false;
167 }
AddRegisterListener(std::shared_ptr<ComponentManagerListener> listener)168 uint32_t ComponentManager::AddRegisterListener(std::shared_ptr<ComponentManagerListener> listener)
169 {
170     TRACK_LOG_STD();
171     listenerList_.push_back(listener);
172     TRACK_LOG_STR("Add linstener count (%d)", listenerList_.size());
173     return listenerList_.size() - 1;
174 }
175 
DeleteRegisterListener(const uint32_t handle)176 void ComponentManager::DeleteRegisterListener(const uint32_t handle)
177 {
178     if (listenerList_.size() > handle) {
179         listenerList_.erase(listenerList_.begin() + handle);
180     }
181 }
182 
GetListenerList()183 std::vector<std::shared_ptr<ComponentManagerListener>> ComponentManager::GetListenerList()
184 {
185     return listenerList_;
186 }
187 
GetReportInfo(std::string & info)188 ErrCode ComponentManager::GetReportInfo(std::string& info)
189 {
190     ErrCode result = OHOS::ERR_OK;
191     return result;
192 }
193 
PermoissionInput()194 ErrCode ComponentManager::PermoissionInput()
195 {
196     DEBUG_LOG("handle permission window");
197     return OHOS::ERR_OK;
198 }
199 
CreateEventInputMap()200 ErrCode ComponentManager::CreateEventInputMap()
201 {
202     return OHOS::ERR_OK;
203 }
204 
ComponentEventInput(OHOS::Accessibility::AccessibilityElementInfo & elementInfo,const int actionType)205 ErrCode ComponentManager::ComponentEventInput(OHOS::Accessibility::AccessibilityElementInfo& elementInfo,
206                                               const int actionType)
207 {
208     CreateEventInputMap();
209     // get position of current component
210     GetComponentPosition(elementInfo);
211     auto componentRespond = componentMap_[actionType];
212     if (componentRespond == nullptr) {
213         componentRespond = componentMap_[Accessibility::ACCESSIBILITY_ACTION_CLICK];
214     }
215     ErrCode result = componentRespond(elementInfo);
216     return result;
217 }
218 
ComponentTouchInput(Accessibility::AccessibilityElementInfo & elementInfo)219 ErrCode ComponentManager::ComponentTouchInput(Accessibility::AccessibilityElementInfo& elementInfo)
220 {
221     ErrCode result;
222     auto touchInput = MultimodeManager::GetInstance();
223     // Calculate touch position
224     int32_t elementTouchX = startX_ + (endX_ - startX_) / DIV;
225     int32_t elementTouchY = startY_ + (endY_ - startY_) / DIV;
226     std::string type = elementInfo.GetContent();
227     INFO_LOG_STR("component Content: Touch Position: (%d, %d)", elementTouchX, elementTouchY);
228     DEBUG_LOG_STR("component Content: (%s), Touch Position: (%d, %d)", type.c_str(), elementTouchX, elementTouchY);
229     result = touchInput->PointerInput(elementTouchX, elementTouchY, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN,
230                                       MMI::PointerEvent::POINTER_ACTION_DOWN);
231     if (result == OHOS::ERR_OK) {
232         result = touchInput->PointerInput(elementTouchX, elementTouchY, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN,
233                                           MMI::PointerEvent::POINTER_ACTION_UP);
234     }
235     return result;
236 }
237 
BackToPrePage()238 ErrCode ComponentManager::BackToPrePage()
239 {
240     ErrCode result = ERR_OK;
241     int backKeyCode = OHOS::MMI::KeyEvent::KEYCODE_BACK;
242     result = MultimodeManager::GetInstance()->SingleKeyCodeInput(backKeyCode, DOWNTIME);
243     return result;
244 }
245 
ComponentUpSwapInput(Accessibility::AccessibilityElementInfo & elementInfo)246 ErrCode ComponentManager::ComponentUpSwapInput(Accessibility::AccessibilityElementInfo& elementInfo)
247 {
248     // Calculate swap position
249     int32_t componentUpSwapStartX = startX_ + (endX_ - startX_) / DIV;
250     int32_t componentUpSwapStartY = endY_ - OFFSET;
251     int32_t componentUpSwapEndX = componentUpSwapStartX;
252     int32_t componentUpSwapEndY = startY_ + OFFSET;
253     INFO_LOG_STR("Component Up Swap: (%d, %d) -> (%d, %d)", componentUpSwapStartX, componentUpSwapStartY,
254                  componentUpSwapEndX, componentUpSwapEndY);
255     ErrCode result = MultimodeManager::GetInstance()->IntervalSwap(componentUpSwapStartX, componentUpSwapStartY,
256                                                                    componentUpSwapEndX, componentUpSwapEndY);
257     usleep(TWOSECONDS);
258     return result;
259 }
260 
ComponentDownSwapInput(Accessibility::AccessibilityElementInfo & elementInfo)261 ErrCode ComponentManager::ComponentDownSwapInput(Accessibility::AccessibilityElementInfo& elementInfo)
262 {
263     // Calculate swap position
264     int32_t componentDownSwapStartX = startX_ + (endX_ - startX_) / DIV;
265     int32_t componentDownSwapStartY = startY_ + OFFSET;
266     int32_t componentDownSwapEndX = componentDownSwapStartX;
267     int32_t componentDownSwapEndY = endY_ - OFFSET;
268     INFO_LOG_STR("Component Down Swap: (%d, %d) -> (%d, %d)", componentDownSwapStartX, componentDownSwapStartY,
269                  componentDownSwapEndX, componentDownSwapEndY);
270     ErrCode result = MultimodeManager::GetInstance()->IntervalSwap(componentDownSwapStartX, componentDownSwapStartY,
271                                                                    componentDownSwapEndX, componentDownSwapEndY);
272     usleep(TWOSECONDS);
273     return result;
274 }
275 
ComponentMultikeyInput(Accessibility::AccessibilityElementInfo & elementInfo)276 ErrCode ComponentManager::ComponentMultikeyInput(Accessibility::AccessibilityElementInfo& elementInfo)
277 {
278     ErrCode result = ComponentTouchInput(elementInfo);
279     if (result != OHOS::ERR_OK) {
280         return result;
281     }
282     usleep(ONESECOND);
283     result = MultimodeManager::GetInstance()->MultiKeyCodeInput(DOWNTIME);
284     return result;
285 }
286 
ComponentLeftSwapInput(Accessibility::AccessibilityElementInfo & elementInfo)287 ErrCode ComponentManager::ComponentLeftSwapInput(Accessibility::AccessibilityElementInfo& elementInfo)
288 {
289     ErrCode result;
290     // Calculate swap position
291     int32_t leftSwapStartX = startX_ + OFFSET;
292     int32_t leftSwapEndX = endX_ + OFFSET;
293     int32_t leftSwapStartY = startY_ + (endY_ - startY_) / DIV;
294     int32_t leftSwapEndY = leftSwapStartY;
295     INFO_LOG_STR("Component Left Swap: (%d, %d) -> (%d, %d)", leftSwapStartX, leftSwapStartY, leftSwapEndX,
296                  leftSwapEndY);
297     result = MultimodeManager::GetInstance()->IntervalSwap(leftSwapStartX, leftSwapStartY, leftSwapEndX, leftSwapEndY);
298     return result;
299 }
300 
GetComponentPosition(Accessibility::AccessibilityElementInfo & elementInfo)301 void ComponentManager::GetComponentPosition(Accessibility::AccessibilityElementInfo& elementInfo)
302 {
303     Accessibility::Rect componentBounds = elementInfo.GetRectInScreen();
304     startX_ = componentBounds.GetLeftTopXScreenPostion();
305     startY_ = componentBounds.GetLeftTopYScreenPostion();
306     endX_ = componentBounds.GetRightBottomXScreenPostion();
307     endY_ = componentBounds.GetRightBottomYScreenPostion();
308 }
309 }  // namespace WuKong
310 }  // namespace OHOS
311