libaurum: add a logic to wait for idle
[platform/core/uifw/aurum.git] / libaurum / src / UiDevice.cc
1 #include "UiDevice.h"
2 #include "AccessibleWatcher.h"
3 #include "Comparer.h"
4 #include "DeviceImpl/TizenImpl.h"
5
6 #include <unistd.h>
7 #include <utility>
8 #include <vector>
9 #include <chrono>
10 #include <thread>
11
12 UiDevice::UiDevice() : UiDevice(DeviceType::DEFAULT, nullptr) {}
13
14 UiDevice::UiDevice(DeviceType type, IDevice *impl)
15     : mType(type), mDeviceImpl(impl), mWaiter(new Waiter{this})
16 {
17 }
18
19 UiDevice::~UiDevice()
20 {
21     delete mDeviceImpl;
22     delete mWaiter;
23 }
24
25 UiDevice *UiDevice::getInstance(DeviceType type)
26 {
27     static UiDevice *device = nullptr;
28 #ifdef TIZEN
29     if (!device) device = new UiDevice(type, new TizenImpl());
30 #endif
31     return device;
32 }
33
34 std::vector<std::unique_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
35 {
36     return AccessibleWatcher::getInstance()->getTopNode();
37 }
38
39 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
40 {
41     auto root = getWindowRoot();
42     for (auto it = root.begin(); it != root.end(); ++it) {
43         std::unique_ptr<AccessibleNode> node =
44             Comparer::findObject(this, selector, (*it).get());
45         if (node != nullptr) return true;
46     }
47
48     return false;
49 }
50
51 std::unique_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
52 {
53     auto root = getWindowRoot();
54     for (auto it = root.begin(); it != root.end(); ++it) {
55         std::unique_ptr<AccessibleNode> node =
56             Comparer::findObject(this, selector, (*it).get());
57         if (node)
58             return std::make_unique<UiObject>(this, selector, std::move(node));
59     }
60     return std::unique_ptr<UiObject>{nullptr};
61 }
62
63 std::vector<std::unique_ptr<UiObject>> UiDevice::findObjects(
64     const std::shared_ptr<UiSelector> selector) const
65 {
66     std::vector<std::unique_ptr<UiObject>> ret{};
67     auto root = getWindowRoot();
68     for (auto it = root.begin(); it != root.end(); ++it) {
69         std::vector<std::unique_ptr<AccessibleNode>>          nodes =
70             Comparer::findObjects(this, selector, (*it).get());
71
72         for (auto &node : nodes)
73             ret.push_back(std::make_unique<UiObject>(this, selector, std::move(node)));
74     }
75     return ret;
76 }
77 bool UiDevice::waitFor(
78     const std::function<bool(const ISearchable *)> condition) const
79 {
80     return mWaiter->waitFor(condition);
81 }
82
83 std::unique_ptr<UiObject> UiDevice::waitFor(
84     const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
85         condition) const
86 {
87     return mWaiter->waitFor(condition);
88 }
89 bool UiDevice::waitForIdle() const
90 {
91     std::this_thread::sleep_for(std::chrono::milliseconds{167});
92     return true;
93 }
94
95 bool UiDevice::click(const int x, const int y)
96 {
97     bool result =  mDeviceImpl->click(x, y);
98     waitForIdle();
99     return result;
100 }
101
102 bool UiDevice::click(const int x, const int y, const unsigned int intv)
103 {
104     bool result = mDeviceImpl->click(x, y, intv);
105     waitForIdle();
106     return result;
107 }
108
109 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
110                     const int steps, const int durationMs)
111 {
112     bool result =  mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
113     waitForIdle();
114     return result;
115 }
116
117 bool UiDevice::touchDown(const int x, const int y)
118 {
119     bool result =  mDeviceImpl->touchDown(x, y);
120
121     return result;
122 }
123
124 bool UiDevice::touchMove(const int x, const int y)
125 {
126     bool result =  mDeviceImpl->touchMove(x, y);
127     return result;
128 }
129
130 bool UiDevice::touchUp(const int x, const int y)
131 {
132     bool result =  mDeviceImpl->touchUp(x, y);
133     waitForIdle();
134     return result;
135 }
136
137 bool UiDevice::pressBack()
138 {
139     bool result =  mDeviceImpl->pressBack();
140     waitForIdle();
141     return result;
142 }
143
144 bool UiDevice::pressHome()
145 {
146     bool result =  mDeviceImpl->pressHome();
147     waitForIdle();
148     return result;
149 }
150
151 bool UiDevice::pressMenu()
152 {
153     bool result =  mDeviceImpl->pressMenu();
154     waitForIdle();
155     return result;
156 }
157
158 bool UiDevice::pressVolUp()
159 {
160     bool result =  mDeviceImpl->pressVolUp();
161     waitForIdle();
162     return result;
163 }
164
165 bool UiDevice::pressVolDown()
166 {
167     bool result =  mDeviceImpl->pressVolDown();
168     waitForIdle();
169     return result;
170 }
171
172 bool UiDevice::pressPower()
173 {
174     bool result =  mDeviceImpl->pressPower();
175     waitForIdle();
176     return result;
177 }
178
179 bool UiDevice::pressKeyCode(std::string keycode)
180 {
181     bool result =  mDeviceImpl->pressKeyCode(keycode);
182     waitForIdle();
183     return result;
184 }
185
186 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
187 {
188     return mDeviceImpl->takeScreenshot(path, scale, quality);
189 }
190
191 long long UiDevice::getSystemTime(TypeRequestType type)
192 {
193     return mDeviceImpl->getSystemTime(type);
194 }