libaurum: clear dirty logs up
[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     std::vector<std::shared_ptr<UiObject>> result{};
89     auto nodes = Comparer::findObjects(mDevice, selector, getAccessibleNode());
90     for ( auto& node : nodes) {
91         if (!node) {
92             LOG_F(INFO, "Skipped! (node == nullptr)");
93             continue;
94         }
95         result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
96     }
97     return result;
98 }
99
100 bool UiObject::waitFor(
101     const std::function<bool(const ISearchable *)> condition) const
102 {
103     return mWaiter->waitFor(condition);
104 }
105
106 std::shared_ptr<UiObject> UiObject::waitFor(
107     const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
108         condition) const
109 {
110     return mWaiter->waitFor(condition);
111 }
112
113 bool UiObject::waitFor(
114     const std::function<bool(const UiObject *)> condition) const
115 {
116     return mWaiter->waitFor(condition);
117 }
118
119 UiObject *UiObject::getParent() const
120 {
121     std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
122     if (!node) return nullptr;
123     return new UiObject(mDevice, mSelector, std::move(node));
124 }
125
126 int UiObject::getChildCount() const
127 {
128     return getAccessibleNode()->getChildCount();
129 }
130
131 std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
132     auto childNode = getAccessibleNode()->getChildAt(index);
133     if (childNode) {
134         return std::make_shared<UiObject>(mDevice, mSelector, childNode);
135     }
136     return nullptr;
137 }
138
139 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
140 {
141     auto sel = Sel::depth(1);
142     return this->findObjects(sel);
143 }
144
145 std::shared_ptr<Node> UiObject::getDescendant()
146 {
147     std::vector<std::shared_ptr<Node>> nodeChildren{};
148
149     auto children = getChildren();
150     for (auto &&child : children) {
151         nodeChildren.push_back(child->getDescendant());
152     }
153     return std::make_shared<Node>(shared_from_this(), nodeChildren);
154 }
155
156 std::string UiObject::getApplicationPackage() const
157 {
158     return getAccessibleNode()->getPkg();
159 }
160
161 std::string UiObject::getId() const
162 {
163     return getAccessibleNode()->getId();
164 }
165
166 std::string UiObject::getAutomationId() const
167 {
168     return getAccessibleNode()->getAutomationId();
169 }
170
171 std::string UiObject::getElementType() const
172 {
173     return getAccessibleNode()->getType();
174 }
175
176 std::string UiObject::getElementStyle() const
177 {
178     return getAccessibleNode()->getStyle();
179 }
180
181 std::string UiObject::getText() const
182 {
183     return getAccessibleNode()->getText();
184 }
185
186 std::string UiObject::getRole() const
187 {
188     return getAccessibleNode()->getRole();
189 }
190
191 void UiObject::setText(std::string text)
192 {
193     getAccessibleNode()->setValue(text);
194 }
195
196 bool UiObject::isCheckable() const
197 {
198     return getAccessibleNode()->isCheckable();
199 }
200
201 bool UiObject::isChecked() const
202 {
203     return getAccessibleNode()->isChecked();
204 }
205
206 bool UiObject::isClickable() const
207 {
208     return getAccessibleNode()->isClickable();
209 }
210
211 bool UiObject::isEnabled() const
212 {
213     return getAccessibleNode()->isEnabled();
214 }
215
216 bool UiObject::isFocusable() const
217 {
218     return getAccessibleNode()->isFocusable();
219 }
220
221 bool UiObject::isFocused() const
222 {
223     return getAccessibleNode()->isFocused();
224 }
225
226 bool UiObject::isLongClickable() const
227 {
228     return getAccessibleNode()->isLongClickable();
229 }
230
231 bool UiObject::isScrollable() const
232 {
233     return getAccessibleNode()->isScrollable();
234 }
235
236 bool UiObject::isSelectable() const
237 {
238     return getAccessibleNode()->isSelectable();
239 }
240
241 bool UiObject::isSelected() const
242 {
243     return getAccessibleNode()->isSelected();
244 }
245
246 bool UiObject::isVisible() const
247 {
248     return getAccessibleNode()->isVisible();
249 }
250
251 bool UiObject::isShowing() const
252 {
253     return getAccessibleNode()->isShowing();
254 }
255
256 bool UiObject::isActive() const
257 {
258     return getAccessibleNode()->isActive();
259 }
260
261 void UiObject::refresh() const
262 {
263     mNode->refresh();
264 }
265
266 const Rect<int> UiObject::getBoundingBox() const
267 {
268     mNode->refresh();
269     return mNode->getBoundingBox();
270 }
271
272 void UiObject::click() const
273 {
274     mNode->refresh();
275     const Rect<int> rect = mNode->getBoundingBox();
276     const Point2D<int> midPoint = rect.midPoint();
277     mDevice->click(midPoint.x, midPoint.y);
278 }
279
280 void UiObject::longClick(const unsigned int intv) const
281 {
282     mNode->refresh();
283     const Rect<int> rect = mNode->getBoundingBox();
284     const Point2D<int> midPoint = rect.midPoint();
285     mDevice->click(midPoint.x, midPoint.y, intv);
286 }
287
288 bool UiObject::DoAtspiActivate() const
289 {
290     return mNode->doAction("activate");
291 }
292
293
294 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
295 {
296     if (mNode == nullptr) throw;
297     // TODO : wait for animation and refresh current node
298     // mDevice->waitForIdle();
299     mNode->refresh();
300     return mNode;
301 }