libaurum: change delay time between key down and key up
[platform/core/uifw/aurum.git] / libaurum / src / UiDevice.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18 #include "Aurum.h"
19
20 #ifdef TIZEN
21 #include "TizenDeviceImpl.h"
22 #endif
23 #include "MockDeviceImpl.h"
24 #include "AtspiAccessibleWatcher.h"
25 #include <unistd.h>
26 #include <utility>
27 #include <vector>
28 #include <chrono>
29 #include <thread>
30 #include <algorithm>
31 #include <iostream>
32
33 using namespace Aurum;
34 using namespace AurumInternal;
35
36 UiDevice::UiDevice() : UiDevice(nullptr) {}
37
38 UiDevice::UiDevice(IDevice *impl)
39     : mDeviceImpl(impl), mWaiter(new Waiter{this})
40 {
41 }
42
43 UiDevice::~UiDevice()
44 {
45     delete mDeviceImpl;
46     delete mWaiter;
47 }
48
49 std::shared_ptr<UiDevice> UiDevice::getInstance(IDevice *deviceImpl)
50 {
51     static std::shared_ptr<UiDevice> device{nullptr};
52
53     if (deviceImpl) {
54         device.reset(new UiDevice(deviceImpl));
55     } else {
56         if (device) return device;
57         else {
58 #ifdef TIZEN
59             device.reset(new UiDevice(new TizenDeviceImpl()));
60 #else
61             device.reset(new UiDevice(new MockDeviceImpl()));
62 #endif
63         }
64     }
65
66     return device;
67 }
68
69 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
70 {
71     std::vector<std::shared_ptr<AccessibleNode>> ret{};
72
73     auto appsMap = AccessibleWatcher::getInstance()->getActiveAppMap();
74     LOGI("activeAppMap.size: %d" , (int)appsMap.size());
75     for (auto itr = appsMap.begin(); itr != appsMap.end(); itr++)
76     {
77         auto activeWindows = itr->second->getActiveWindows();
78         std::transform(activeWindows.begin(), activeWindows.end(), std::back_inserter(ret),
79             [&](std::shared_ptr<AccessibleWindow> window){
80                 LOGI("active pkg: %s, window: %s", window->getAccessibleNode()->getPkg().c_str(), window->getTitle().c_str());
81                 return window->getAccessibleNode();
82             }
83         );
84     }
85
86     return ret;
87 }
88
89 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
90 {
91     auto rootNodes = getWindowRoot();
92     for (const auto &node : rootNodes) {
93         const std::shared_ptr<AccessibleNode> foundNode =
94             Comparer::findObject(getInstance(), selector, node);
95         if (foundNode) return true;
96     }
97
98     return false;
99 }
100
101 std::shared_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
102 {
103     auto rootNodes = getWindowRoot();
104     for (const auto &node : rootNodes) {
105         const std::shared_ptr<AccessibleNode> foundNode =
106             Comparer::findObject(getInstance(), selector, node);
107         if (foundNode)
108             return std::make_shared<UiObject>(getInstance(), selector, foundNode);
109     }
110     return std::shared_ptr<UiObject>{nullptr};
111 }
112
113
114 std::vector<std::shared_ptr<UiObject>> UiDevice::findObjects(
115     const std::shared_ptr<UiSelector> selector) const
116 {
117     std::vector<std::shared_ptr<UiObject>> ret{};
118     auto rootNodes = getWindowRoot();
119     for (const auto &window : rootNodes) {
120         std::vector<std::shared_ptr<AccessibleNode>> nodes =
121             Comparer::findObjects(getInstance(), selector, window);
122         for (auto &node : nodes)
123             ret.push_back(std::make_shared<UiObject>(getInstance(), selector, node));
124     }
125     return ret;
126 }
127 bool UiDevice::waitFor(
128     const std::function<bool(const ISearchable *)> condition) const
129 {
130     return mWaiter->waitFor(condition);
131 }
132
133 std::shared_ptr<UiObject> UiDevice::waitFor(
134     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
135         condition) const
136 {
137     return mWaiter->waitFor(condition);
138 }
139 bool UiDevice::waitForIdle() const
140 {
141     std::this_thread::sleep_for(std::chrono::milliseconds{167});
142     return true;
143 }
144
145 bool UiDevice::waitForEvents(
146         const A11yEvent type, const int timeout) const
147 {
148     return executeAndWaitForEvents(NULL, type, timeout);
149 }
150
151 bool UiDevice::executeAndWaitForEvents(
152         const Runnable *cmd, const A11yEvent type, const int timeout) const
153 {
154     return AccessibleWatcher::getInstance()->executeAndWaitForEvents(cmd, type, timeout);
155 }
156
157 bool UiDevice::sendKeyAndWaitForEvents(
158         const std::string keycode, const A11yEvent type, const int timeout) const
159 {
160     std::unique_ptr<SendKeyRunnable> cmd = std::make_unique<SendKeyRunnable>(keycode);
161     return executeAndWaitForEvents(cmd.get(), type, timeout);
162 }
163
164 bool UiDevice::click(const int x, const int y)
165 {
166     bool result =  mDeviceImpl->click(x, y);
167     waitForIdle();
168     return result;
169 }
170
171 bool UiDevice::click(const int x, const int y, const unsigned int durationMs)
172 {
173     bool result = mDeviceImpl->click(x, y, durationMs);
174     waitForIdle();
175     return result;
176 }
177
178 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
179                     const int steps, const int durationMs)
180 {
181     bool result =  mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
182     waitForIdle();
183     return result;
184 }
185
186 int UiDevice::touchDown(const int x, const int y)
187 {
188     int seq =  mDeviceImpl->touchDown(x, y);
189     return seq;
190 }
191
192 bool UiDevice::touchMove(const int x, const int y, const int seq)
193 {
194     bool result =  mDeviceImpl->touchMove(x, y, seq);
195     return result;
196 }
197
198 bool UiDevice::touchUp(const int x, const int y, const int seq)
199 {
200     bool result =  mDeviceImpl->touchUp(x, y, seq);
201     waitForIdle();
202     return result;
203 }
204
205 bool UiDevice::wheelUp(int amount, const int durationMs)
206 {
207     bool result =  mDeviceImpl->wheelUp(amount, durationMs);
208     waitForIdle();
209     return result;
210 }
211
212 bool UiDevice::wheelDown(int amount, const int durationMs)
213 {
214     bool result =  mDeviceImpl->wheelDown(amount, durationMs);
215     waitForIdle();
216     return result;
217 }
218
219 bool UiDevice::pressBack(KeyRequestType type)
220 {
221     bool result =  mDeviceImpl->pressBack(type);
222     waitForIdle();
223     return result;
224 }
225
226 bool UiDevice::pressHome(KeyRequestType type)
227 {
228     bool result =  mDeviceImpl->pressHome(type);
229     waitForIdle();
230     return result;
231 }
232
233 bool UiDevice::pressMenu(KeyRequestType type)
234 {
235     bool result =  mDeviceImpl->pressMenu(type);
236     waitForIdle();
237     return result;
238 }
239
240 bool UiDevice::pressVolUp(KeyRequestType type)
241 {
242     bool result =  mDeviceImpl->pressVolUp(type);
243     waitForIdle();
244     return result;
245 }
246
247 bool UiDevice::pressVolDown(KeyRequestType type)
248 {
249     bool result =  mDeviceImpl->pressVolDown(type);
250     waitForIdle();
251     return result;
252 }
253
254 bool UiDevice::pressPower(KeyRequestType type)
255 {
256     bool result =  mDeviceImpl->pressPower(type);
257     waitForIdle();
258     return result;
259 }
260
261 bool UiDevice::pressKeyCode(std::string keycode, KeyRequestType type)
262 {
263     bool result =  mDeviceImpl->pressKeyCode(keycode, type);
264     return result;
265 }
266
267 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
268 {
269     return mDeviceImpl->takeScreenshot(path, scale, quality);
270 }
271
272 long long UiDevice::getSystemTime(TimeRequestType type)
273 {
274     return mDeviceImpl->getSystemTime(type);
275 }
276
277 const Size2D<int> UiDevice::getScreenSize()
278 {
279     return mDeviceImpl->getScreenSize();
280 }