libaurum: apply smart pointer wider and extract impl out
[platform/core/uifw/aurum.git] / libaurum / src / UiDevice.cc
1 #include "UiDevice.h"
2 #include "AccessibleWatcher.h"
3 #include "Comparer.h"
4
5 #ifdef TIZEN
6 #include "TizenDeviceImpl.h"
7 #endif
8 #include "MockDeviceImpl.h"
9
10 #include <unistd.h>
11 #include <utility>
12 #include <vector>
13 #include <chrono>
14 #include <thread>
15 #include <algorithm>
16 #include <iostream>
17
18 UiDevice::UiDevice() : UiDevice(nullptr) {}
19
20 UiDevice::UiDevice(IDevice *impl)
21     : mDeviceImpl(impl), mWaiter(new Waiter{this})
22 {
23 }
24
25 UiDevice::~UiDevice()
26 {
27     delete mDeviceImpl;
28     delete mWaiter;
29 }
30
31 std::shared_ptr<UiDevice> UiDevice::getInstance(IDevice *deviceImpl)
32 {
33     static std::shared_ptr<UiDevice> device{nullptr};
34
35     if (deviceImpl) {
36         device.reset(new UiDevice(deviceImpl));
37     } else {
38         if (device) return device;
39         else {
40 #ifdef TIZEN
41             device.reset(new UiDevice(new TizenDeviceImpl()));
42 #else
43             device.reset(new UiDevice(new MockDeviceImpl()));
44 #endif
45         }
46     }
47
48     return device;
49 }
50
51 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
52 {
53     std::vector<std::shared_ptr<AccessibleNode>> ret{};
54
55     auto apps = AccessibleWatcher::getInstance()->getActiveApplications();
56     for (auto &app : apps){
57         auto activeWindows = app->getActiveWindows();
58         std::transform(activeWindows.begin(), activeWindows.end(), std::back_inserter(ret),
59             [&](std::shared_ptr<AccessibleWindow> window){
60                 return window->getNode();
61             }
62         );
63     }
64     return ret;
65 }
66
67 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
68 {
69     auto rootNodes = getWindowRoot();
70     for (const auto &node : rootNodes) {
71         const std::shared_ptr<AccessibleNode> foundNode =
72             Comparer::findObject(getInstance(), selector, node);
73         if (foundNode) return true;
74     }
75
76     return false;
77 }
78
79 std::shared_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
80 {
81     auto rootNodes = getWindowRoot();
82     for (const auto &node : rootNodes) {
83         const std::shared_ptr<AccessibleNode> foundNode =
84             Comparer::findObject(getInstance(), selector, node);
85         if (foundNode)
86             return std::make_shared<UiObject>(getInstance(), selector, foundNode);
87     }
88     return std::shared_ptr<UiObject>{nullptr};
89 }
90
91
92 std::vector<std::shared_ptr<UiObject>> UiDevice::findObjects(
93     const std::shared_ptr<UiSelector> selector) const
94 {
95     std::vector<std::shared_ptr<UiObject>> ret{};
96     auto rootNodes = getWindowRoot();
97     for (const auto &window : rootNodes) {
98         std::vector<std::shared_ptr<AccessibleNode>> nodes =
99             Comparer::findObjects(getInstance(), selector, window);
100         for (auto &node : nodes)
101             ret.push_back(std::make_shared<UiObject>(getInstance(), selector, node));
102     }
103     return ret;
104 }
105 bool UiDevice::waitFor(
106     const std::function<bool(const ISearchable *)> condition) const
107 {
108     return mWaiter->waitFor(condition);
109 }
110
111 std::shared_ptr<UiObject> UiDevice::waitFor(
112     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
113         condition) const
114 {
115     return mWaiter->waitFor(condition);
116 }
117 bool UiDevice::waitForIdle() const
118 {
119     std::this_thread::sleep_for(std::chrono::milliseconds{167});
120     return true;
121 }
122
123 bool UiDevice::click(const int x, const int y)
124 {
125     bool result =  mDeviceImpl->click(x, y);
126     waitForIdle();
127     return result;
128 }
129
130 bool UiDevice::click(const int x, const int y, const unsigned int intv)
131 {
132     bool result = mDeviceImpl->click(x, y, intv);
133     waitForIdle();
134     return result;
135 }
136
137 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
138                     const int steps, const int durationMs)
139 {
140     bool result =  mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
141     waitForIdle();
142     return result;
143 }
144
145 int UiDevice::touchDown(const int x, const int y)
146 {
147     int seq =  mDeviceImpl->touchDown(x, y);
148     return seq;
149 }
150
151 bool UiDevice::touchMove(const int x, const int y, const int seq)
152 {
153     bool result =  mDeviceImpl->touchMove(x, y, seq);
154     return result;
155 }
156
157 bool UiDevice::touchUp(const int x, const int y, const int seq)
158 {
159     bool result =  mDeviceImpl->touchUp(x, y, seq);
160     waitForIdle();
161     return result;
162 }
163
164 bool UiDevice::wheelUp(int amount, const int durationMs)
165 {
166     bool result =  mDeviceImpl->wheelUp(amount, durationMs);
167     waitForIdle();
168     return result;
169 }
170
171 bool UiDevice::wheelDown(int amount, const int durationMs)
172 {
173     bool result =  mDeviceImpl->wheelDown(amount, durationMs);
174     waitForIdle();
175     return result;
176 }
177
178 bool UiDevice::pressBack(KeyRequestType type)
179 {
180     bool result =  mDeviceImpl->pressBack(type);
181     waitForIdle();
182     return result;
183 }
184
185 bool UiDevice::pressHome(KeyRequestType type)
186 {
187     bool result =  mDeviceImpl->pressHome(type);
188     waitForIdle();
189     return result;
190 }
191
192 bool UiDevice::pressMenu(KeyRequestType type)
193 {
194     bool result =  mDeviceImpl->pressMenu(type);
195     waitForIdle();
196     return result;
197 }
198
199 bool UiDevice::pressVolUp(KeyRequestType type)
200 {
201     bool result =  mDeviceImpl->pressVolUp(type);
202     waitForIdle();
203     return result;
204 }
205
206 bool UiDevice::pressVolDown(KeyRequestType type)
207 {
208     bool result =  mDeviceImpl->pressVolDown(type);
209     waitForIdle();
210     return result;
211 }
212
213 bool UiDevice::pressPower(KeyRequestType type)
214 {
215     bool result =  mDeviceImpl->pressPower(type);
216     waitForIdle();
217     return result;
218 }
219
220 bool UiDevice::pressKeyCode(std::string keycode, KeyRequestType type)
221 {
222     bool result =  mDeviceImpl->pressKeyCode(keycode, type);
223     waitForIdle();
224     return result;
225 }
226
227 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
228 {
229     return mDeviceImpl->takeScreenshot(path, scale, quality);
230 }
231
232 long long UiDevice::getSystemTime(TimeRequestType type)
233 {
234     return mDeviceImpl->getSystemTime(type);
235 }