fix potentional erros according to svace/coverity avalytics
[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 UiSelector *selector,
18                    const AccessibleNode *node)
19     : mDevice(device),
20       mSelector(selector),
21       mNode(node),
22       mWaiter(new Waiter{this, this})
23 {
24     // tood interface to interact with input interface
25     // mInputImple = mDevice->getInputInterface();
26 }
27
28 UiObject::UiObject(const UiObject &src)
29     : mDevice(src.mDevice),
30       mSelector(src.mSelector),
31       mNode(src.mNode),
32       mWaiter{src.mWaiter}
33 {
34 }
35
36 UiObject::UiObject(UiObject &&src)
37     : mDevice(src.mDevice),
38       mSelector(src.mSelector),
39       mNode(src.mNode),
40       mWaiter{src.mWaiter}
41 {
42     src.mDevice = nullptr;
43     src.mSelector = nullptr;
44     src.mNode = nullptr;
45     src.mWaiter = nullptr;
46 }
47
48 bool UiObject::hasObject(const UiSelector *selector) const
49 {
50     AccessibleNode *node =
51         Comparer::findObject(mDevice, selector, getAccessibleNode());
52     if (node != nullptr) {
53         // todo : what is this node.recycle()
54         return true;
55     }
56     return false;
57 }
58
59 std::unique_ptr<UiObject> UiObject::findObject(const UiSelector *selector) const
60 {
61     AccessibleNode *node =
62         Comparer::findObject(mDevice, selector, getAccessibleNode());
63     if (node)
64         return std::make_unique<UiObject>(mDevice, selector, node);
65     else
66         return std::unique_ptr<UiObject>{nullptr};
67 }
68
69 std::vector<std::unique_ptr<UiObject>> UiObject::findObjects(
70     const UiSelector *selector) const
71 {
72     return std::vector<std::unique_ptr<UiObject>>{};
73 }
74
75 bool UiObject::waitFor(
76     const std::function<bool(const ISearchable *)> condition) const
77 {
78     return mWaiter->waitFor(condition);
79 }
80
81 std::unique_ptr<UiObject> UiObject::waitFor(
82     const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
83         condition) const
84 {
85     return mWaiter->waitFor(condition);
86 }
87
88 bool UiObject::waitFor(
89     const std::function<bool(const UiObject *)> condition) const
90 {
91     LOG_F(INFO, "asdf");
92     return mWaiter->waitFor(condition);
93 }
94
95 UiObject *UiObject::getParent() const
96 {
97     AccessibleNode *node = getAccessibleNode()->getParent();
98     if (!node) return nullptr;
99     return new UiObject(mDevice, mSelector, node);
100 }
101
102 int UiObject::getChildCount() const
103 {
104     return getAccessibleNode()->getChildCount();
105 }
106
107 std::vector<std::unique_ptr<UiObject>> UiObject::getChildren() const
108 {
109     return findObjects(Sel::depth(1).get());
110 }
111
112 std::string UiObject::getContentDescription() const
113 {
114     return getAccessibleNode()->getDesc();
115 }
116
117 std::string UiObject::getApplicationPackage() const
118 {
119     return getAccessibleNode()->getPkg();
120 }
121
122 std::string UiObject::getResourceName() const
123 {
124     return getAccessibleNode()->getRes();
125 }
126
127 std::string UiObject::getText() const
128 {
129     return getAccessibleNode()->getText();
130 }
131
132 void UiObject::setText(std::string &text)
133 {
134     getAccessibleNode()->setValue(text);
135 }
136
137 bool UiObject::isCheckable() const
138 {
139     return getAccessibleNode()->isCheckable();
140 }
141
142 bool UiObject::isChecked() const
143 {
144     return getAccessibleNode()->isChecked();
145 }
146
147 bool UiObject::isClickable() const
148 {
149     return getAccessibleNode()->isClickable();
150 }
151
152 bool UiObject::isEnabled() const
153 {
154     return getAccessibleNode()->isEnabled();
155 }
156
157 bool UiObject::isFocusable() const
158 {
159     return getAccessibleNode()->isFocusable();
160 }
161
162 bool UiObject::isFocused() const
163 {
164     return getAccessibleNode()->isFocused();
165 }
166
167 bool UiObject::isLongClickable() const
168 {
169     return getAccessibleNode()->isLongClickable();
170 }
171
172 bool UiObject::isScrollable() const
173 {
174     return getAccessibleNode()->isScrollable();
175 }
176
177 bool UiObject::isSelectable() const
178 {
179     return getAccessibleNode()->isSelectable();
180 }
181
182 bool UiObject::isSelected() const
183 {
184     return getAccessibleNode()->isSelected();
185 }
186
187 void UiObject::refresh() const
188 {
189     mNode->refresh();
190 }
191
192 void UiObject::click() const
193 {
194     LOG_SCOPE_F(INFO, "click on obj %p", this);
195     mNode->refresh();
196     const Rect<int> rect = mNode->getBoundingBox();
197     std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
198     const Point2D<int> midPoint = rect.midPoint();
199     const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y);
200     // todo click implementation
201 }
202
203 const AccessibleNode *UiObject::getAccessibleNode() const
204 {
205     if (mNode == nullptr) throw;
206
207     // TODO : wait for animation and refresh current node
208     // mDevice->waitForIdle();
209     // mNode->refresh();
210
211     return mNode;
212 }