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