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