libaurum: apply smart pointer wider and extract impl out
[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::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
134 {
135     auto sel = std::make_shared<UiSelector>();
136     sel->depth(1);
137     sel->isShowing(true);
138     return this->findObjects(sel);
139 }
140
141 std::string UiObject::getApplicationPackage() const
142 {
143     return getAccessibleNode()->getPkg();
144 }
145
146 std::string UiObject::getResourceName() const
147 {
148     return getAccessibleNode()->getRes();
149 }
150
151 std::string UiObject::getElementType() const
152 {
153     return getAccessibleNode()->getType();
154 }
155
156 std::string UiObject::getElementStyle() const
157 {
158     return getAccessibleNode()->getStyle();
159 }
160
161 std::string UiObject::getText() const
162 {
163     return getAccessibleNode()->getText();
164 }
165
166 std::string UiObject::getRole() const
167 {
168     return getAccessibleNode()->getRole();
169 }
170
171 void UiObject::setText(std::string text)
172 {
173     getAccessibleNode()->setValue(text);
174 }
175
176 bool UiObject::isCheckable() const
177 {
178     return getAccessibleNode()->isCheckable();
179 }
180
181 bool UiObject::isChecked() const
182 {
183     return getAccessibleNode()->isChecked();
184 }
185
186 bool UiObject::isClickable() const
187 {
188     return getAccessibleNode()->isClickable();
189 }
190
191 bool UiObject::isEnabled() const
192 {
193     return getAccessibleNode()->isEnabled();
194 }
195
196 bool UiObject::isFocusable() const
197 {
198     return getAccessibleNode()->isFocusable();
199 }
200
201 bool UiObject::isFocused() const
202 {
203     return getAccessibleNode()->isFocused();
204 }
205
206 bool UiObject::isLongClickable() const
207 {
208     return getAccessibleNode()->isLongClickable();
209 }
210
211 bool UiObject::isScrollable() const
212 {
213     return getAccessibleNode()->isScrollable();
214 }
215
216 bool UiObject::isSelectable() const
217 {
218     return getAccessibleNode()->isSelectable();
219 }
220
221 bool UiObject::isSelected() const
222 {
223     return getAccessibleNode()->isSelected();
224 }
225
226 bool UiObject::isVisible() const
227 {
228     return getAccessibleNode()->isVisible();
229 }
230
231 bool UiObject::isShowing() const
232 {
233     return getAccessibleNode()->isShowing();
234 }
235
236 bool UiObject::isActive() const
237 {
238     return getAccessibleNode()->isActive();
239 }
240
241 void UiObject::refresh() const
242 {
243     mNode->refresh();
244 }
245
246 const Rect<int> UiObject::getBoundingBox() const
247 {
248     mNode->refresh();
249     return mNode->getBoundingBox();
250 }
251
252 void UiObject::click() const
253 {
254     LOG_SCOPE_F(INFO, "click on obj %p", this);
255     mNode->refresh();
256     const Rect<int> rect = mNode->getBoundingBox();
257     const Point2D<int> midPoint = rect.midPoint();
258     mDevice->click(midPoint.x, midPoint.y);
259 }
260
261 void UiObject::longClick(const unsigned int intv) const
262 {
263     LOG_SCOPE_F(INFO, "click on obj %p", this);
264     mNode->refresh();
265     const Rect<int> rect = mNode->getBoundingBox();
266     const Point2D<int> midPoint = rect.midPoint();
267     mDevice->click(midPoint.x, midPoint.y, intv);
268 }
269
270 bool UiObject::DoAtspiActivate() const
271 {
272     return mNode->doAction("activate");
273 }
274
275
276 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
277 {
278     if (mNode == nullptr) throw;
279     // TODO : wait for animation and refresh current node
280     // mDevice->waitForIdle();
281     mNode->refresh();
282     return mNode;
283 }