aurum-service: app_context destroy after use
[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(1);
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::getElementType() 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 bool UiObject::isCheckable() const
233 {
234     return getAccessibleNode()->isCheckable();
235 }
236
237 bool UiObject::isChecked() const
238 {
239     return getAccessibleNode()->isChecked();
240 }
241
242 bool UiObject::isClickable() const
243 {
244     return getAccessibleNode()->isClickable();
245 }
246
247 bool UiObject::isEnabled() const
248 {
249     return getAccessibleNode()->isEnabled();
250 }
251
252 bool UiObject::isFocusable() const
253 {
254     return getAccessibleNode()->isFocusable();
255 }
256
257 bool UiObject::isFocused() const
258 {
259     return getAccessibleNode()->isFocused();
260 }
261
262 bool UiObject::isLongClickable() const
263 {
264     return getAccessibleNode()->isLongClickable();
265 }
266
267 bool UiObject::isScrollable() const
268 {
269     return getAccessibleNode()->isScrollable();
270 }
271
272 bool UiObject::isSelectable() const
273 {
274     return getAccessibleNode()->isSelectable();
275 }
276
277 bool UiObject::isSelected() const
278 {
279     return getAccessibleNode()->isSelected();
280 }
281
282 bool UiObject::isVisible() const
283 {
284     return getAccessibleNode()->isVisible();
285 }
286
287 bool UiObject::isShowing() const
288 {
289     return getAccessibleNode()->isShowing();
290 }
291
292 bool UiObject::isActive() const
293 {
294     return getAccessibleNode()->isActive();
295 }
296
297 void UiObject::refresh() const
298 {
299     mNode->refresh();
300 }
301
302 void UiObject::updateRoleName() const
303 {
304     mNode->updateRoleName();
305 }
306
307 void UiObject::updateUniqueId() const
308 {
309     mNode->updateUniqueId();
310 }
311
312 void UiObject::updateName() const
313 {
314     mNode->updateName();
315 }
316
317 void UiObject::updateApplication() const
318 {
319     mNode->updateApplication();
320 }
321
322 void UiObject::updateAttributes() const
323 {
324     mNode->updateAttributes();
325 }
326
327 void UiObject::updateStates() const
328 {
329     mNode->updateStates();
330 }
331
332 void UiObject::updateExtents() const
333 {
334     mNode->updateExtents();
335 }
336
337 void UiObject::updateXPath() const
338 {
339     mNode->updateXPath();
340 }
341
342 void UiObject::updateValue() const
343 {
344     mNode->updateValue();
345 }
346
347 bool UiObject::setFocus() const
348 {
349     return mNode->setFocus();
350 }
351
352 bool UiObject::isValid() const
353 {
354     return mNode->isValid();
355 }
356
357 const Rect<int> UiObject::getScreenBoundingBox() const
358 {
359     return mNode->getScreenBoundingBox();
360 }
361
362 const Rect<int> UiObject::getWindowBoundingBox() const
363 {
364     return mNode->getWindowBoundingBox();
365 }
366
367 void UiObject::click() const
368 {
369     mNode->updateExtents();
370     const Rect<int> rect = mNode->getScreenBoundingBox();
371     const Point2D<int> midPoint = rect.midPoint();
372     mDevice->click(midPoint.x, midPoint.y);
373 }
374
375 void UiObject::longClick(const unsigned int durationMs) const
376 {
377     mNode->updateExtents();
378     const Rect<int> rect = mNode->getScreenBoundingBox();
379     const Point2D<int> midPoint = rect.midPoint();
380     mDevice->click(midPoint.x, midPoint.y, durationMs);
381 }
382
383 bool UiObject::DoAtspiActivate() const
384 {
385     return mNode->doAction("activate");
386 }
387
388
389 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
390 {
391     if (mNode == nullptr) throw;
392     // TODO : wait for animation and refresh current node
393     // mDevice->waitForIdle();
394     return mNode;
395 }