aurum: implement an atspi action for 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 UiObject::UiObject() : UiObject(nullptr, nullptr, nullptr) {}
11
12 UiObject::~UiObject()
13 {
14     if (mWaiter) delete mWaiter;
15 }
16
17 UiObject::UiObject(const UiDevice *device, const std::shared_ptr<UiSelector> selector,
18                    const AccessibleNode *node)
19     : mDevice(device),
20       mSelector(selector),
21       mNode(std::unique_ptr<AccessibleNode>(const_cast<AccessibleNode*>(node))),
22       mWaiter(new Waiter{this, this})
23 {
24 }
25
26 UiObject::UiObject(const UiDevice *device, const std::shared_ptr<UiSelector> selector,
27                    std::unique_ptr<AccessibleNode> node)
28     : mDevice(device),
29       mSelector(selector),
30       mNode(std::move(node)),
31       mWaiter(new Waiter{this, this})
32 {
33 }
34
35 // UiObject::UiObject(const UiObject &src)
36 //     : mDevice(src.mDevice),
37 //       mSelector(src.mSelector),
38 //       mNode(src.mNode),
39 //       mWaiter{src.mWaiter},
40 //       mNode_src(std::move(src.mNode_src))
41
42 // {
43 // }
44
45 UiObject::UiObject(UiObject &&src)
46     : mDevice(src.mDevice),
47       mSelector(std::move(src.mSelector)),
48       mNode(std::move(src.mNode)),
49       mWaiter{src.mWaiter}
50 {
51     src.mDevice = nullptr;
52     src.mSelector = nullptr;
53     src.mNode = nullptr;
54     src.mWaiter = nullptr;
55 }
56
57 std::shared_ptr<UiSelector> UiObject::getSelector()
58 {
59     return this->mSelector;
60 }
61
62 bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
63 {
64     std::unique_ptr<AccessibleNode> node =
65         Comparer::findObject(mDevice, selector, getAccessibleNode());
66     if (node != nullptr) {
67         // todo : what is this node.recycle()
68         return true;
69     }
70     return false;
71 }
72
73 std::unique_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
74 {
75     std::unique_ptr<AccessibleNode> node =
76         Comparer::findObject(mDevice, selector, getAccessibleNode());
77     if (node)
78         return std::make_unique<UiObject>(mDevice, selector, std::move(node));
79     else
80         return std::unique_ptr<UiObject>{nullptr};
81 }
82
83 std::vector<std::unique_ptr<UiObject>> UiObject::findObjects(
84     const std::shared_ptr<UiSelector> selector) const
85 {
86     return std::vector<std::unique_ptr<UiObject>>{};
87 }
88
89 bool UiObject::waitFor(
90     const std::function<bool(const ISearchable *)> condition) const
91 {
92     return mWaiter->waitFor(condition);
93 }
94
95 std::unique_ptr<UiObject> UiObject::waitFor(
96     const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
97         condition) const
98 {
99     return mWaiter->waitFor(condition);
100 }
101
102 bool UiObject::waitFor(
103     const std::function<bool(const UiObject *)> condition) const
104 {
105     LOG_F(INFO, "asdf");
106     return mWaiter->waitFor(condition);
107 }
108
109 UiObject *UiObject::getParent() const
110 {
111     std::unique_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
112     if (!node) return nullptr;
113     return new UiObject(mDevice, mSelector, std::move(node));
114 }
115
116 int UiObject::getChildCount() const
117 {
118     return getAccessibleNode()->getChildCount();
119 }
120
121 std::vector<std::unique_ptr<UiObject>> UiObject::getChildren() const
122 {
123     return findObjects(Sel::depth(1));
124 }
125
126 std::string UiObject::getContentDescription() const
127 {
128     return getAccessibleNode()->getDesc();
129 }
130
131 std::string UiObject::getApplicationPackage() const
132 {
133     return getAccessibleNode()->getPkg();
134 }
135
136 std::string UiObject::getResourceName() const
137 {
138     return getAccessibleNode()->getRes();
139 }
140
141 std::string UiObject::getText() const
142 {
143     return getAccessibleNode()->getText();
144 }
145
146 void UiObject::setText(std::string text)
147 {
148     getAccessibleNode()->setValue(text);
149 }
150
151 bool UiObject::isCheckable() const
152 {
153     return getAccessibleNode()->isCheckable();
154 }
155
156 bool UiObject::isChecked() const
157 {
158     return getAccessibleNode()->isChecked();
159 }
160
161 bool UiObject::isClickable() const
162 {
163     return getAccessibleNode()->isClickable();
164 }
165
166 bool UiObject::isEnabled() const
167 {
168     return getAccessibleNode()->isEnabled();
169 }
170
171 bool UiObject::isFocusable() const
172 {
173     return getAccessibleNode()->isFocusable();
174 }
175
176 bool UiObject::isFocused() const
177 {
178     return getAccessibleNode()->isFocused();
179 }
180
181 bool UiObject::isLongClickable() const
182 {
183     return getAccessibleNode()->isLongClickable();
184 }
185
186 bool UiObject::isScrollable() const
187 {
188     return getAccessibleNode()->isScrollable();
189 }
190
191 bool UiObject::isSelectable() const
192 {
193     return getAccessibleNode()->isSelectable();
194 }
195
196 bool UiObject::isSelected() const
197 {
198     return getAccessibleNode()->isSelected();
199 }
200
201 bool UiObject::isVisible() const
202 {
203     return getAccessibleNode()->isVisible();
204 }
205
206 bool UiObject::isShowing() const
207 {
208     return getAccessibleNode()->isShowing();
209 }
210
211 bool UiObject::isActive() const
212 {
213     return getAccessibleNode()->isActive();
214 }
215
216 void UiObject::refresh() const
217 {
218     mNode->refresh();
219 }
220
221 const Rect<int> UiObject::getBoundingBox() const
222 {
223     mNode->refresh();
224     return mNode->getBoundingBox();
225 }
226
227 void UiObject::click() const
228 {
229     LOG_SCOPE_F(INFO, "click on obj %p", this);
230     mNode->refresh();
231     const Rect<int> rect = mNode->getBoundingBox();
232     std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
233     const Point2D<int> midPoint = rect.midPoint();
234     const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y);
235 }
236
237 void UiObject::longClick(const unsigned int intv) const
238 {
239     LOG_SCOPE_F(INFO, "click on obj %p", this);
240     mNode->refresh();
241     const Rect<int> rect = mNode->getBoundingBox();
242     std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
243     const Point2D<int> midPoint = rect.midPoint();
244     const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y, intv);
245 }
246
247 bool UiObject::DoAtspiActivate() const
248 {
249     return mNode->doAction("activate");
250 }
251
252
253 const AccessibleNode *UiObject::getAccessibleNode() const
254 {
255     if (mNode == nullptr) throw;
256
257     // TODO : wait for animation and refresh current node
258     // mDevice->waitForIdle();
259     // mNode->refresh();
260
261     return mNode.get();
262 }