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