libaurum: fix leaking memory by using smart pointer
[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 bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
58 {
59     std::unique_ptr<AccessibleNode> node =
60         Comparer::findObject(mDevice, selector, getAccessibleNode());
61     if (node != nullptr) {
62         // todo : what is this node.recycle()
63         return true;
64     }
65     return false;
66 }
67
68 std::unique_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
69 {
70     std::unique_ptr<AccessibleNode> node =
71         Comparer::findObject(mDevice, selector, getAccessibleNode());
72     if (node)
73         return std::make_unique<UiObject>(mDevice, selector, std::move(node));
74     else
75         return std::unique_ptr<UiObject>{nullptr};
76 }
77
78 std::vector<std::unique_ptr<UiObject>> UiObject::findObjects(
79     const std::shared_ptr<UiSelector> selector) const
80 {
81     return std::vector<std::unique_ptr<UiObject>>{};
82 }
83
84 bool UiObject::waitFor(
85     const std::function<bool(const ISearchable *)> condition) const
86 {
87     return mWaiter->waitFor(condition);
88 }
89
90 std::unique_ptr<UiObject> UiObject::waitFor(
91     const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
92         condition) const
93 {
94     return mWaiter->waitFor(condition);
95 }
96
97 bool UiObject::waitFor(
98     const std::function<bool(const UiObject *)> condition) const
99 {
100     LOG_F(INFO, "asdf");
101     return mWaiter->waitFor(condition);
102 }
103
104 UiObject *UiObject::getParent() const
105 {
106     std::unique_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
107     if (!node) return nullptr;
108     return new UiObject(mDevice, mSelector, std::move(node));
109 }
110
111 int UiObject::getChildCount() const
112 {
113     return getAccessibleNode()->getChildCount();
114 }
115
116 std::vector<std::unique_ptr<UiObject>> UiObject::getChildren() const
117 {
118     return findObjects(Sel::depth(1));
119 }
120
121 std::string UiObject::getContentDescription() const
122 {
123     return getAccessibleNode()->getDesc();
124 }
125
126 std::string UiObject::getApplicationPackage() const
127 {
128     return getAccessibleNode()->getPkg();
129 }
130
131 std::string UiObject::getResourceName() const
132 {
133     return getAccessibleNode()->getRes();
134 }
135
136 std::string UiObject::getText() const
137 {
138     return getAccessibleNode()->getText();
139 }
140
141 void UiObject::setText(std::string &text)
142 {
143     getAccessibleNode()->setValue(text);
144 }
145
146 bool UiObject::isCheckable() const
147 {
148     return getAccessibleNode()->isCheckable();
149 }
150
151 bool UiObject::isChecked() const
152 {
153     return getAccessibleNode()->isChecked();
154 }
155
156 bool UiObject::isClickable() const
157 {
158     return getAccessibleNode()->isClickable();
159 }
160
161 bool UiObject::isEnabled() const
162 {
163     return getAccessibleNode()->isEnabled();
164 }
165
166 bool UiObject::isFocusable() const
167 {
168     return getAccessibleNode()->isFocusable();
169 }
170
171 bool UiObject::isFocused() const
172 {
173     return getAccessibleNode()->isFocused();
174 }
175
176 bool UiObject::isLongClickable() const
177 {
178     return getAccessibleNode()->isLongClickable();
179 }
180
181 bool UiObject::isScrollable() const
182 {
183     return getAccessibleNode()->isScrollable();
184 }
185
186 bool UiObject::isSelectable() const
187 {
188     return getAccessibleNode()->isSelectable();
189 }
190
191 bool UiObject::isSelected() const
192 {
193     return getAccessibleNode()->isSelected();
194 }
195
196 bool UiObject::isVisible() const
197 {
198     return getAccessibleNode()->isVisible();
199 }
200
201 void UiObject::refresh() const
202 {
203     mNode->refresh();
204 }
205
206 const Rect<int> UiObject::getBoundingBox() const
207 {
208     mNode->refresh();
209     return mNode->getBoundingBox();
210 }
211
212 void UiObject::click() const
213 {
214     LOG_SCOPE_F(INFO, "click on obj %p", this);
215     mNode->refresh();
216     const Rect<int> rect = mNode->getBoundingBox();
217     std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
218     const Point2D<int> midPoint = rect.midPoint();
219     const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y);
220 }
221
222 void UiObject::longClick(const unsigned int intv) const
223 {
224     LOG_SCOPE_F(INFO, "click on obj %p", this);
225     mNode->refresh();
226     const Rect<int> rect = mNode->getBoundingBox();
227     std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
228     const Point2D<int> midPoint = rect.midPoint();
229     const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y, intv);
230 }
231
232 const AccessibleNode *UiObject::getAccessibleNode() const
233 {
234     if (mNode == nullptr) throw;
235
236     // TODO : wait for animation and refresh current node
237     // mDevice->waitForIdle();
238     // mNode->refresh();
239
240     return mNode.get();
241 }