Aurum: Improve performance of findElements command
[platform/core/uifw/aurum.git] / libaurum / src / UiObject.cc
1 /*
2  * Copyright (c) 2022 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 #include <iostream>
21 #include <utility>
22
23 #include <chrono>
24 #include <thread>
25
26 using namespace Aurum;
27
28 UiObject::UiObject() : UiObject(nullptr, nullptr, nullptr) {}
29
30 UiObject::~UiObject()
31 {
32     if (mWaiter) delete mWaiter;
33 }
34
35 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
36                    const AccessibleNode *node)
37     : mDevice(device),
38       mSelector(selector),
39       mNode(std::shared_ptr<AccessibleNode>(const_cast<AccessibleNode *>(node))),
40       mWaiter(new Waiter{this, this})
41 {
42 }
43
44 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
45                    std::shared_ptr<AccessibleNode> node)
46     : mDevice(device),
47       mSelector(selector),
48       mNode(std::move(node)),
49       mWaiter(new Waiter{this, this})
50 {
51 }
52
53 UiObject::UiObject(UiObject &&src)
54     : mDevice(src.mDevice),
55       mSelector(std::move(src.mSelector)),
56       mNode(std::move(src.mNode)),
57       mWaiter{src.mWaiter}
58 {
59     src.mDevice = nullptr;
60     src.mSelector = nullptr;
61     src.mNode = nullptr;
62     src.mWaiter = nullptr;
63 }
64
65 std::shared_ptr<UiSelector> UiObject::getSelector()
66 {
67     return this->mSelector;
68 }
69
70 bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
71 {
72     std::shared_ptr<AccessibleNode> node =
73         Comparer::findObject(mDevice, selector, getAccessibleNode());
74     if (node != nullptr) {
75         // todo : what is this node.recycle()
76         return true;
77     }
78     return false;
79 }
80
81 std::shared_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
82 {
83     std::shared_ptr<AccessibleNode> node =
84         Comparer::findObject(mDevice, selector, getAccessibleNode());
85     if (node)
86         return std::make_shared<UiObject>(mDevice, selector, std::move(node));
87     else
88         return std::shared_ptr<UiObject>{nullptr};
89 }
90
91 std::vector<std::shared_ptr<UiObject>> UiObject::findObjects(
92     const std::shared_ptr<UiSelector> selector) const
93 {
94     std::vector<std::shared_ptr<UiObject>> result{};
95
96     std::vector<std::shared_ptr<AccessibleNode>> nodes{};
97     Comparer::findObjects(nodes, mDevice, selector, getAccessibleNode());
98     for ( auto& node : nodes) {
99         if (!node) {
100             LOGI("Skipped! (node == nullptr)");
101             continue;
102         }
103         result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
104     }
105     return result;
106 }
107
108 bool UiObject::waitFor(
109     const std::function<bool(const ISearchable *)> condition) const
110 {
111     return mWaiter->waitFor(condition);
112 }
113
114 std::shared_ptr<UiObject> UiObject::waitFor(
115     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
116         condition) const
117 {
118     return mWaiter->waitFor(condition);
119 }
120
121 bool UiObject::waitFor(
122     const std::function<bool(const UiObject *)> condition) const
123 {
124     return mWaiter->waitFor(condition);
125 }
126
127 UiObject *UiObject::getParent() const
128 {
129     std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
130     if (!node) return nullptr;
131     return new UiObject(mDevice, mSelector, std::move(node));
132 }
133
134 int UiObject::getChildCount() const
135 {
136     return getAccessibleNode()->getChildCount();
137 }
138
139 std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
140     auto childNode = getAccessibleNode()->getChildAt(index);
141     if (childNode) {
142         return std::make_shared<UiObject>(mDevice, mSelector, childNode);
143     }
144     return nullptr;
145 }
146
147 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
148 {
149     std::vector<std::shared_ptr<UiObject>> ret{};
150
151     auto children = getAccessibleNode()->getChildren();
152     for (auto &child : children) {
153         ret.push_back(std::make_shared<UiObject>(mDevice, mSelector, child));
154     }
155
156     return ret;
157 }
158
159 std::shared_ptr<Node> UiObject::getDescendant()
160 {
161     std::vector<std::shared_ptr<Node>> nodeChildren{};
162
163     auto children = getChildren();
164     for (auto &&child : children) {
165         nodeChildren.push_back(child->getDescendant());
166     }
167     return std::make_shared<Node>(shared_from_this(), nodeChildren);
168 }
169
170 std::string UiObject::getApplicationPackage() const
171 {
172     return getAccessibleNode()->getPkg();
173 }
174
175 std::string UiObject::getId() const
176 {
177     return getAccessibleNode()->getId();
178 }
179
180 std::string UiObject::getAutomationId() const
181 {
182     return getAccessibleNode()->getAutomationId();
183 }
184
185 std::string UiObject::getType() const
186 {
187     return getAccessibleNode()->getType();
188 }
189
190 std::string UiObject::getElementStyle() const
191 {
192     return getAccessibleNode()->getStyle();
193 }
194
195 std::string UiObject::getText() const
196 {
197     return getAccessibleNode()->getText();
198 }
199
200 std::string UiObject::getRole() const
201 {
202     return getAccessibleNode()->getRole();
203 }
204
205 std::string UiObject::getXPath() const
206 {
207     return getAccessibleNode()->getXPath();
208 }
209
210 const double UiObject::getMinValue() const
211 {
212     return getAccessibleNode()->getMinValue();
213 }
214
215 const double UiObject::getMaxValue() const
216 {
217     return getAccessibleNode()->getMaxValue();
218 }
219
220 const double UiObject::getValue() const
221 {
222     return getAccessibleNode()->getValue();
223 }
224
225 const double UiObject::getIncrement() const
226 {
227     return getAccessibleNode()->getIncrement();
228 }
229
230 const Rect<int> UiObject::getTextMinBoundingRect() const
231 {
232     return getAccessibleNode()->getTextMinBoundingRect();
233 }
234
235 bool UiObject::setValue(double value)
236 {
237     return getAccessibleNode()->setValue(value);
238 }
239
240 bool UiObject::setText(std::string text)
241 {
242     return getAccessibleNode()->setValue(text);
243 }
244
245 std::string UiObject::getOcrText() const
246 {
247     return getAccessibleNode()->getOcrText();
248 }
249
250 std::string UiObject::getToolkitName() const
251 {
252     getAccessibleNode()->updateToolkitName();
253     return getAccessibleNode()->getToolkitName();
254 }
255
256 void UiObject::setOcrText(std::string text)
257 {
258     getAccessibleNode()->setOcrText(text);
259 }
260
261 bool UiObject::isCheckable() const
262 {
263     return getAccessibleNode()->isCheckable();
264 }
265
266 bool UiObject::isChecked() const
267 {
268     return getAccessibleNode()->isChecked();
269 }
270
271 bool UiObject::isClickable() const
272 {
273     return getAccessibleNode()->isClickable();
274 }
275
276 bool UiObject::isEnabled() const
277 {
278     return getAccessibleNode()->isEnabled();
279 }
280
281 bool UiObject::isFocusable() const
282 {
283     return getAccessibleNode()->isFocusable();
284 }
285
286 bool UiObject::isFocused() const
287 {
288     return getAccessibleNode()->isFocused();
289 }
290
291 bool UiObject::isLongClickable() const
292 {
293     return getAccessibleNode()->isLongClickable();
294 }
295
296 bool UiObject::isScrollable() const
297 {
298     return getAccessibleNode()->isScrollable();
299 }
300
301 bool UiObject::isSelectable() const
302 {
303     return getAccessibleNode()->isSelectable();
304 }
305
306 bool UiObject::isSelected() const
307 {
308     return getAccessibleNode()->isSelected();
309 }
310
311 bool UiObject::isVisible() const
312 {
313     return getAccessibleNode()->isVisible();
314 }
315
316 bool UiObject::isShowing() const
317 {
318     return getAccessibleNode()->isShowing();
319 }
320
321 bool UiObject::isActive() const
322 {
323     return getAccessibleNode()->isActive();
324 }
325
326 bool UiObject::isHighlightable() const
327 {
328     return getAccessibleNode()->isHighlightable();
329 }
330
331 void UiObject::refresh() const
332 {
333     mNode->refresh();
334 }
335
336 void UiObject::updateRoleName() const
337 {
338     mNode->updateRoleName();
339 }
340
341 void UiObject::updateUniqueId() const
342 {
343     mNode->updateUniqueId();
344 }
345
346 void UiObject::updateName() const
347 {
348     mNode->updateName();
349 }
350
351 void UiObject::updateApplication() const
352 {
353     mNode->updateApplication();
354 }
355
356 void UiObject::updateAttributes() const
357 {
358     mNode->updateAttributes();
359 }
360
361 void UiObject::updateStates() const
362 {
363     mNode->updateStates();
364 }
365
366 void UiObject::updateExtents() const
367 {
368     mNode->updateExtents();
369 }
370
371 void UiObject::updateXPath() const
372 {
373     mNode->updateXPath();
374 }
375
376 void UiObject::updateValue() const
377 {
378     mNode->updateValue();
379 }
380
381 void UiObject::updatePid() const
382 {
383     mNode->updatePid();
384 }
385
386 void UiObject::updateToolkitName() const
387 {
388     mNode->updateToolkitName();
389 }
390
391 void UiObject::updateTextMinBoundingRect() const
392 {
393     mNode->updateTextMinBoundingRect();
394 }
395
396 bool UiObject::setFocus() const
397 {
398     return mNode->setFocus();
399 }
400
401 bool UiObject::isValid() const
402 {
403     return mNode->isValid();
404 }
405
406 const Rect<int> UiObject::getScreenBoundingBox() const
407 {
408     return mNode->getScreenBoundingBox();
409 }
410
411 const Rect<int> UiObject::getWindowBoundingBox() const
412 {
413     return mNode->getWindowBoundingBox();
414 }
415
416 void UiObject::click() const
417 {
418     mNode->updateExtents();
419     const Rect<int> rect = mNode->getScreenBoundingBox();
420     const Point2D<int> midPoint = rect.midPoint();
421     mDevice->click(midPoint.x, midPoint.y);
422 }
423
424 void UiObject::longClick(const unsigned int durationMs) const
425 {
426     mNode->updateExtents();
427     const Rect<int> rect = mNode->getScreenBoundingBox();
428     const Point2D<int> midPoint = rect.midPoint();
429     mDevice->click(midPoint.x, midPoint.y, durationMs);
430 }
431
432 bool UiObject::DoAtspiActivate() const
433 {
434     return mNode->doAction("activate");
435 }
436
437
438 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
439 {
440     if (mNode == nullptr) throw;
441     // TODO : wait for animation and refresh current node
442     // mDevice->waitForIdle();
443     return mNode;
444 }