aurum: add searching highlightable object
[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     auto nodes = Comparer::findObjects(mDevice, selector, getAccessibleNode());
96     for ( auto& node : nodes) {
97         if (!node) {
98             LOGI("Skipped! (node == nullptr)");
99             continue;
100         }
101         result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
102     }
103     return result;
104 }
105
106 bool UiObject::waitFor(
107     const std::function<bool(const ISearchable *)> condition) const
108 {
109     return mWaiter->waitFor(condition);
110 }
111
112 std::shared_ptr<UiObject> UiObject::waitFor(
113     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
114         condition) const
115 {
116     return mWaiter->waitFor(condition);
117 }
118
119 bool UiObject::waitFor(
120     const std::function<bool(const UiObject *)> condition) const
121 {
122     return mWaiter->waitFor(condition);
123 }
124
125 UiObject *UiObject::getParent() const
126 {
127     std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
128     if (!node) return nullptr;
129     return new UiObject(mDevice, mSelector, std::move(node));
130 }
131
132 int UiObject::getChildCount() const
133 {
134     return getAccessibleNode()->getChildCount();
135 }
136
137 std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
138     auto childNode = getAccessibleNode()->getChildAt(index);
139     if (childNode) {
140         return std::make_shared<UiObject>(mDevice, mSelector, childNode);
141     }
142     return nullptr;
143 }
144
145 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
146 {
147     auto sel = Sel::depth(2);
148     return this->findObjects(sel);
149 }
150
151 std::shared_ptr<Node> UiObject::getDescendant()
152 {
153     std::vector<std::shared_ptr<Node>> nodeChildren{};
154
155     auto children = getChildren();
156     for (auto &&child : children) {
157         nodeChildren.push_back(child->getDescendant());
158     }
159     return std::make_shared<Node>(shared_from_this(), nodeChildren);
160 }
161
162 std::string UiObject::getApplicationPackage() const
163 {
164     return getAccessibleNode()->getPkg();
165 }
166
167 std::string UiObject::getId() const
168 {
169     return getAccessibleNode()->getId();
170 }
171
172 std::string UiObject::getAutomationId() const
173 {
174     return getAccessibleNode()->getAutomationId();
175 }
176
177 std::string UiObject::getType() const
178 {
179     return getAccessibleNode()->getType();
180 }
181
182 std::string UiObject::getElementStyle() const
183 {
184     return getAccessibleNode()->getStyle();
185 }
186
187 std::string UiObject::getText() const
188 {
189     return getAccessibleNode()->getText();
190 }
191
192 std::string UiObject::getRole() const
193 {
194     return getAccessibleNode()->getRole();
195 }
196
197 std::string UiObject::getXPath() const
198 {
199     return getAccessibleNode()->getXPath();
200 }
201
202 const double UiObject::getMinValue() const
203 {
204     return getAccessibleNode()->getMinValue();
205 }
206
207 const double UiObject::getMaxValue() const
208 {
209     return getAccessibleNode()->getMaxValue();
210 }
211
212 const double UiObject::getValue() const
213 {
214     return getAccessibleNode()->getValue();
215 }
216
217 const double UiObject::getIncrement() const
218 {
219     return getAccessibleNode()->getIncrement();
220 }
221
222 const Rect<int> UiObject::getTextMinBoundingRect() const
223 {
224     return getAccessibleNode()->getTextMinBoundingRect();
225 }
226
227 bool UiObject::setValue(double value)
228 {
229     return getAccessibleNode()->setValue(value);
230 }
231
232 bool UiObject::setText(std::string text)
233 {
234     return getAccessibleNode()->setValue(text);
235 }
236
237 std::string UiObject::getOcrText() const
238 {
239     return getAccessibleNode()->getOcrText();
240 }
241
242 std::string UiObject::getToolkitName() const
243 {
244     getAccessibleNode()->updateToolkitName();
245     return getAccessibleNode()->getToolkitName();
246 }
247
248 void UiObject::setOcrText(std::string text)
249 {
250     getAccessibleNode()->setOcrText(text);
251 }
252
253 bool UiObject::isCheckable() const
254 {
255     return getAccessibleNode()->isCheckable();
256 }
257
258 bool UiObject::isChecked() const
259 {
260     return getAccessibleNode()->isChecked();
261 }
262
263 bool UiObject::isClickable() const
264 {
265     return getAccessibleNode()->isClickable();
266 }
267
268 bool UiObject::isEnabled() const
269 {
270     return getAccessibleNode()->isEnabled();
271 }
272
273 bool UiObject::isFocusable() const
274 {
275     return getAccessibleNode()->isFocusable();
276 }
277
278 bool UiObject::isFocused() const
279 {
280     return getAccessibleNode()->isFocused();
281 }
282
283 bool UiObject::isLongClickable() const
284 {
285     return getAccessibleNode()->isLongClickable();
286 }
287
288 bool UiObject::isScrollable() const
289 {
290     return getAccessibleNode()->isScrollable();
291 }
292
293 bool UiObject::isSelectable() const
294 {
295     return getAccessibleNode()->isSelectable();
296 }
297
298 bool UiObject::isSelected() const
299 {
300     return getAccessibleNode()->isSelected();
301 }
302
303 bool UiObject::isVisible() const
304 {
305     return getAccessibleNode()->isVisible();
306 }
307
308 bool UiObject::isShowing() const
309 {
310     return getAccessibleNode()->isShowing();
311 }
312
313 bool UiObject::isActive() const
314 {
315     return getAccessibleNode()->isActive();
316 }
317
318 bool UiObject::isHighlightable() const
319 {
320     return getAccessibleNode()->isHighlightable();
321 }
322
323 void UiObject::refresh() const
324 {
325     mNode->refresh();
326 }
327
328 void UiObject::updateRoleName() const
329 {
330     mNode->updateRoleName();
331 }
332
333 void UiObject::updateUniqueId() const
334 {
335     mNode->updateUniqueId();
336 }
337
338 void UiObject::updateName() const
339 {
340     mNode->updateName();
341 }
342
343 void UiObject::updateApplication() const
344 {
345     mNode->updateApplication();
346 }
347
348 void UiObject::updateAttributes() const
349 {
350     mNode->updateAttributes();
351 }
352
353 void UiObject::updateStates() const
354 {
355     mNode->updateStates();
356 }
357
358 void UiObject::updateExtents() const
359 {
360     mNode->updateExtents();
361 }
362
363 void UiObject::updateXPath() const
364 {
365     mNode->updateXPath();
366 }
367
368 void UiObject::updateValue() const
369 {
370     mNode->updateValue();
371 }
372
373 void UiObject::updatePid() const
374 {
375     mNode->updatePid();
376 }
377
378 void UiObject::updateToolkitName() const
379 {
380     mNode->updateToolkitName();
381 }
382
383 void UiObject::updateTextMinBoundingRect() const
384 {
385     mNode->updateTextMinBoundingRect();
386 }
387
388 bool UiObject::setFocus() const
389 {
390     return mNode->setFocus();
391 }
392
393 bool UiObject::isValid() const
394 {
395     return mNode->isValid();
396 }
397
398 const Rect<int> UiObject::getScreenBoundingBox() const
399 {
400     return mNode->getScreenBoundingBox();
401 }
402
403 const Rect<int> UiObject::getWindowBoundingBox() const
404 {
405     return mNode->getWindowBoundingBox();
406 }
407
408 void UiObject::click() const
409 {
410     mNode->updateExtents();
411     const Rect<int> rect = mNode->getScreenBoundingBox();
412     const Point2D<int> midPoint = rect.midPoint();
413     mDevice->click(midPoint.x, midPoint.y);
414 }
415
416 void UiObject::longClick(const unsigned int durationMs) 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, durationMs);
422 }
423
424 bool UiObject::DoAtspiActivate() const
425 {
426     return mNode->doAction("activate");
427 }
428
429
430 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
431 {
432     if (mNode == nullptr) throw;
433     // TODO : wait for animation and refresh current node
434     // mDevice->waitForIdle();
435     return mNode;
436 }