Introduce Intelligent Ui Automation thorough Screen Analyzer
[platform/core/uifw/aurum.git] / libaurum / src / UiObject.cc
1 /*
2  * Copyright (c) 2021 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 bool UiObject::setValue(double value)
223 {
224     return getAccessibleNode()->setValue(value);
225 }
226
227 bool UiObject::setText(std::string text)
228 {
229     return getAccessibleNode()->setValue(text);
230 }
231
232 std::string UiObject::getOcrText() const
233 {
234     return getAccessibleNode()->getOcrText();
235 }
236
237 std::string UiObject::getToolkitName() const
238 {
239     getAccessibleNode()->updateToolkitName();
240     return getAccessibleNode()->getToolkitName();
241 }
242
243 void UiObject::setOcrText(std::string text)
244 {
245     getAccessibleNode()->setOcrText(text);
246 }
247
248 bool UiObject::isCheckable() const
249 {
250     return getAccessibleNode()->isCheckable();
251 }
252
253 bool UiObject::isChecked() const
254 {
255     return getAccessibleNode()->isChecked();
256 }
257
258 bool UiObject::isClickable() const
259 {
260     return getAccessibleNode()->isClickable();
261 }
262
263 bool UiObject::isEnabled() const
264 {
265     return getAccessibleNode()->isEnabled();
266 }
267
268 bool UiObject::isFocusable() const
269 {
270     return getAccessibleNode()->isFocusable();
271 }
272
273 bool UiObject::isFocused() const
274 {
275     return getAccessibleNode()->isFocused();
276 }
277
278 bool UiObject::isLongClickable() const
279 {
280     return getAccessibleNode()->isLongClickable();
281 }
282
283 bool UiObject::isScrollable() const
284 {
285     return getAccessibleNode()->isScrollable();
286 }
287
288 bool UiObject::isSelectable() const
289 {
290     return getAccessibleNode()->isSelectable();
291 }
292
293 bool UiObject::isSelected() const
294 {
295     return getAccessibleNode()->isSelected();
296 }
297
298 bool UiObject::isVisible() const
299 {
300     return getAccessibleNode()->isVisible();
301 }
302
303 bool UiObject::isShowing() const
304 {
305     return getAccessibleNode()->isShowing();
306 }
307
308 bool UiObject::isActive() const
309 {
310     return getAccessibleNode()->isActive();
311 }
312
313 void UiObject::refresh() const
314 {
315     mNode->refresh();
316 }
317
318 void UiObject::updateRoleName() const
319 {
320     mNode->updateRoleName();
321 }
322
323 void UiObject::updateUniqueId() const
324 {
325     mNode->updateUniqueId();
326 }
327
328 void UiObject::updateName() const
329 {
330     mNode->updateName();
331 }
332
333 void UiObject::updateApplication() const
334 {
335     mNode->updateApplication();
336 }
337
338 void UiObject::updateAttributes() const
339 {
340     mNode->updateAttributes();
341 }
342
343 void UiObject::updateStates() const
344 {
345     mNode->updateStates();
346 }
347
348 void UiObject::updateExtents() const
349 {
350     mNode->updateExtents();
351 }
352
353 void UiObject::updateXPath() const
354 {
355     mNode->updateXPath();
356 }
357
358 void UiObject::updateValue() const
359 {
360     mNode->updateValue();
361 }
362
363 void UiObject::updatePid() const
364 {
365     mNode->updatePid();
366 }
367
368 void UiObject::updateToolkitName() const
369 {
370     mNode->updateToolkitName();
371 }
372
373 bool UiObject::setFocus() const
374 {
375     return mNode->setFocus();
376 }
377
378 bool UiObject::isValid() const
379 {
380     return mNode->isValid();
381 }
382
383 const Rect<int> UiObject::getScreenBoundingBox() const
384 {
385     return mNode->getScreenBoundingBox();
386 }
387
388 const Rect<int> UiObject::getWindowBoundingBox() const
389 {
390     return mNode->getWindowBoundingBox();
391 }
392
393 void UiObject::click() const
394 {
395     mNode->updateExtents();
396     const Rect<int> rect = mNode->getScreenBoundingBox();
397     const Point2D<int> midPoint = rect.midPoint();
398     mDevice->click(midPoint.x, midPoint.y);
399 }
400
401 void UiObject::longClick(const unsigned int durationMs) const
402 {
403     mNode->updateExtents();
404     const Rect<int> rect = mNode->getScreenBoundingBox();
405     const Point2D<int> midPoint = rect.midPoint();
406     mDevice->click(midPoint.x, midPoint.y, durationMs);
407 }
408
409 bool UiObject::DoAtspiActivate() const
410 {
411     return mNode->doAction("activate");
412 }
413
414
415 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
416 {
417     if (mNode == nullptr) throw;
418     // TODO : wait for animation and refresh current node
419     // mDevice->waitForIdle();
420     return mNode;
421 }