aurum: Performance improvement
[platform/core/uifw/aurum.git] / libaurum / src / UiObject.cc
1 #include "Aurum.h"
2
3 #include <iostream>
4 #include <utility>
5
6 #include <chrono>
7 #include <thread>
8
9 UiObject::UiObject() : UiObject(nullptr, nullptr, nullptr) {}
10
11 UiObject::~UiObject()
12 {
13     if (mWaiter) delete mWaiter;
14 }
15
16 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
17                    const AccessibleNode *node)
18     : mDevice(device),
19       mSelector(selector),
20       mNode(std::shared_ptr<AccessibleNode>(const_cast<AccessibleNode *>(node))),
21       mWaiter(new Waiter{this, this})
22 {
23 }
24
25 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
26                    std::shared_ptr<AccessibleNode> node)
27     : mDevice(device),
28       mSelector(selector),
29       mNode(std::move(node)),
30       mWaiter(new Waiter{this, this})
31 {
32 }
33
34 // UiObject::UiObject(const UiObject &src)
35 //     : mDevice(src.mDevice),
36 //       mSelector(src.mSelector),
37 //       mNode(src.mNode),
38 //       mWaiter{src.mWaiter},
39 //       mNode_src(std::move(src.mNode_src))
40
41 // {
42 // }
43
44 UiObject::UiObject(UiObject &&src)
45     : mDevice(src.mDevice),
46       mSelector(std::move(src.mSelector)),
47       mNode(std::move(src.mNode)),
48       mWaiter{src.mWaiter}
49 {
50     src.mDevice = nullptr;
51     src.mSelector = nullptr;
52     src.mNode = nullptr;
53     src.mWaiter = nullptr;
54 }
55
56 std::shared_ptr<UiSelector> UiObject::getSelector()
57 {
58     return this->mSelector;
59 }
60
61 bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
62 {
63     std::shared_ptr<AccessibleNode> node =
64         Comparer::findObject(mDevice, selector, getAccessibleNode());
65     if (node != nullptr) {
66         // todo : what is this node.recycle()
67         return true;
68     }
69     return false;
70 }
71
72 std::shared_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
73 {
74     std::shared_ptr<AccessibleNode> node =
75         Comparer::findObject(mDevice, selector, getAccessibleNode());
76     if (node)
77         return std::make_shared<UiObject>(mDevice, selector, std::move(node));
78     else
79         return std::shared_ptr<UiObject>{nullptr};
80 }
81
82 std::vector<std::shared_ptr<UiObject>> UiObject::findObjects(
83     const std::shared_ptr<UiSelector> selector) const
84 {
85     std::vector<std::shared_ptr<UiObject>> result{};
86     auto nodes = Comparer::findObjects(mDevice, selector, getAccessibleNode());
87     for ( auto& node : nodes) {
88         if (!node) {
89             LOGI("Skipped! (node == nullptr)");
90             continue;
91         }
92         result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
93     }
94     return result;
95 }
96
97 bool UiObject::waitFor(
98     const std::function<bool(const ISearchable *)> condition) const
99 {
100     return mWaiter->waitFor(condition);
101 }
102
103 std::shared_ptr<UiObject> UiObject::waitFor(
104     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
105         condition) const
106 {
107     return mWaiter->waitFor(condition);
108 }
109
110 bool UiObject::waitFor(
111     const std::function<bool(const UiObject *)> condition) const
112 {
113     return mWaiter->waitFor(condition);
114 }
115
116 UiObject *UiObject::getParent() const
117 {
118     std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
119     if (!node) return nullptr;
120     return new UiObject(mDevice, mSelector, std::move(node));
121 }
122
123 int UiObject::getChildCount() const
124 {
125     return getAccessibleNode()->getChildCount();
126 }
127
128 std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
129     auto childNode = getAccessibleNode()->getChildAt(index);
130     if (childNode) {
131         return std::make_shared<UiObject>(mDevice, mSelector, childNode);
132     }
133     return nullptr;
134 }
135
136 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
137 {
138     auto sel = Sel::depth(1);
139     return this->findObjects(sel);
140 }
141
142 std::shared_ptr<Node> UiObject::getDescendant()
143 {
144     std::vector<std::shared_ptr<Node>> nodeChildren{};
145
146     auto children = getChildren();
147     for (auto &&child : children) {
148         nodeChildren.push_back(child->getDescendant());
149     }
150     return std::make_shared<Node>(shared_from_this(), nodeChildren);
151 }
152
153 std::string UiObject::getApplicationPackage() const
154 {
155     return getAccessibleNode()->getPkg();
156 }
157
158 std::string UiObject::getId() const
159 {
160     return getAccessibleNode()->getId();
161 }
162
163 std::string UiObject::getAutomationId() const
164 {
165     return getAccessibleNode()->getAutomationId();
166 }
167
168 std::string UiObject::getElementType() const
169 {
170     return getAccessibleNode()->getType();
171 }
172
173 std::string UiObject::getElementStyle() const
174 {
175     return getAccessibleNode()->getStyle();
176 }
177
178 std::string UiObject::getText() const
179 {
180     return getAccessibleNode()->getText();
181 }
182
183 std::string UiObject::getRole() const
184 {
185     return getAccessibleNode()->getRole();
186 }
187
188 void UiObject::setText(std::string text)
189 {
190     getAccessibleNode()->setValue(text);
191 }
192
193 bool UiObject::isCheckable() const
194 {
195     return getAccessibleNode()->isCheckable();
196 }
197
198 bool UiObject::isChecked() const
199 {
200     return getAccessibleNode()->isChecked();
201 }
202
203 bool UiObject::isClickable() const
204 {
205     return getAccessibleNode()->isClickable();
206 }
207
208 bool UiObject::isEnabled() const
209 {
210     return getAccessibleNode()->isEnabled();
211 }
212
213 bool UiObject::isFocusable() const
214 {
215     return getAccessibleNode()->isFocusable();
216 }
217
218 bool UiObject::isFocused() const
219 {
220     return getAccessibleNode()->isFocused();
221 }
222
223 bool UiObject::isLongClickable() const
224 {
225     return getAccessibleNode()->isLongClickable();
226 }
227
228 bool UiObject::isScrollable() const
229 {
230     return getAccessibleNode()->isScrollable();
231 }
232
233 bool UiObject::isSelectable() const
234 {
235     return getAccessibleNode()->isSelectable();
236 }
237
238 bool UiObject::isSelected() const
239 {
240     return getAccessibleNode()->isSelected();
241 }
242
243 bool UiObject::isVisible() const
244 {
245     return getAccessibleNode()->isVisible();
246 }
247
248 bool UiObject::isShowing() const
249 {
250     return getAccessibleNode()->isShowing();
251 }
252
253 bool UiObject::isActive() const
254 {
255     return getAccessibleNode()->isActive();
256 }
257
258 void UiObject::refresh() const
259 {
260     mNode->refresh();
261 }
262
263 void UiObject::updateRoleName() const
264 {
265     mNode->updateRoleName();
266 }
267
268 void UiObject::updateUniqueId() const
269 {
270     mNode->updateUniqueId();
271 }
272
273 void UiObject::updateName() const
274 {
275     mNode->updateName();
276 }
277
278 void UiObject::updateApplication() const
279 {
280     mNode->updateApplication();
281 }
282
283 void UiObject::updateAttributes() const
284 {
285     mNode->updateAttributes();
286 }
287
288 void UiObject::updateStates() const
289 {
290     mNode->updateStates();
291 }
292
293 void UiObject::updateExtents() const
294 {
295     mNode->updateExtents();
296 }
297
298 bool UiObject::isValid() const
299 {
300     return mNode->isValid();
301 }
302
303 const Rect<int> UiObject::getScreenBoundingBox() const
304 {
305     return mNode->getScreenBoundingBox();
306 }
307
308 const Rect<int> UiObject::getWindowBoundingBox() const
309 {
310     return mNode->getWindowBoundingBox();
311 }
312
313 void UiObject::click() const
314 {
315     mNode->updateExtents();
316     const Rect<int> rect = mNode->getScreenBoundingBox();
317     const Point2D<int> midPoint = rect.midPoint();
318     mDevice->click(midPoint.x, midPoint.y);
319 }
320
321 void UiObject::longClick(const unsigned int intv) const
322 {
323     mNode->updateExtents();
324     const Rect<int> rect = mNode->getScreenBoundingBox();
325     const Point2D<int> midPoint = rect.midPoint();
326     mDevice->click(midPoint.x, midPoint.y, intv);
327 }
328
329 bool UiObject::DoAtspiActivate() const
330 {
331     return mNode->doAction("activate");
332 }
333
334
335 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
336 {
337     if (mNode == nullptr) throw;
338     // TODO : wait for animation and refresh current node
339     // mDevice->waitForIdle();
340     return mNode;
341 }