2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 using namespace Aurum;
28 UiObject::UiObject() : UiObject(nullptr, nullptr, nullptr) {}
32 if (mWaiter) delete mWaiter;
35 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
36 const AccessibleNode *node)
39 mNode(std::shared_ptr<AccessibleNode>(const_cast<AccessibleNode *>(node))),
40 mWaiter(new Waiter{this, this})
44 UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
45 std::shared_ptr<AccessibleNode> node)
48 mNode(std::move(node)),
49 mWaiter(new Waiter{this, this})
53 UiObject::UiObject(UiObject &&src)
54 : mDevice(src.mDevice),
55 mSelector(std::move(src.mSelector)),
56 mNode(std::move(src.mNode)),
59 src.mDevice = nullptr;
60 src.mSelector = nullptr;
62 src.mWaiter = nullptr;
65 std::shared_ptr<UiSelector> UiObject::getSelector()
67 return this->mSelector;
70 bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
72 std::shared_ptr<AccessibleNode> node =
73 Comparer::findObject(mDevice, selector, getAccessibleNode());
74 if (node != nullptr) {
75 // todo : what is this node.recycle()
81 std::shared_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
83 std::shared_ptr<AccessibleNode> node =
84 Comparer::findObject(mDevice, selector, getAccessibleNode());
86 return std::make_shared<UiObject>(mDevice, selector, std::move(node));
88 return std::shared_ptr<UiObject>{nullptr};
91 std::vector<std::shared_ptr<UiObject>> UiObject::findObjects(
92 const std::shared_ptr<UiSelector> selector) const
94 std::vector<std::shared_ptr<UiObject>> result{};
95 auto nodes = Comparer::findObjects(mDevice, selector, getAccessibleNode());
96 for ( auto& node : nodes) {
98 LOGI("Skipped! (node == nullptr)");
101 result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
106 bool UiObject::waitFor(
107 const std::function<bool(const ISearchable *)> condition) const
109 return mWaiter->waitFor(condition);
112 std::shared_ptr<UiObject> UiObject::waitFor(
113 const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
116 return mWaiter->waitFor(condition);
119 bool UiObject::waitFor(
120 const std::function<bool(const UiObject *)> condition) const
122 return mWaiter->waitFor(condition);
125 UiObject *UiObject::getParent() const
127 std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
128 if (!node) return nullptr;
129 return new UiObject(mDevice, mSelector, std::move(node));
132 int UiObject::getChildCount() const
134 return getAccessibleNode()->getChildCount();
137 std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
138 auto childNode = getAccessibleNode()->getChildAt(index);
140 return std::make_shared<UiObject>(mDevice, mSelector, childNode);
145 std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
147 auto sel = Sel::depth(2);
148 return this->findObjects(sel);
151 std::shared_ptr<Node> UiObject::getDescendant()
153 std::vector<std::shared_ptr<Node>> nodeChildren{};
155 auto children = getChildren();
156 for (auto &&child : children) {
157 nodeChildren.push_back(child->getDescendant());
159 return std::make_shared<Node>(shared_from_this(), nodeChildren);
162 std::string UiObject::getApplicationPackage() const
164 return getAccessibleNode()->getPkg();
167 std::string UiObject::getId() const
169 return getAccessibleNode()->getId();
172 std::string UiObject::getAutomationId() const
174 return getAccessibleNode()->getAutomationId();
177 std::string UiObject::getType() const
179 return getAccessibleNode()->getType();
182 std::string UiObject::getElementStyle() const
184 return getAccessibleNode()->getStyle();
187 std::string UiObject::getText() const
189 return getAccessibleNode()->getText();
192 std::string UiObject::getRole() const
194 return getAccessibleNode()->getRole();
197 std::string UiObject::getXPath() const
199 return getAccessibleNode()->getXPath();
202 const double UiObject::getMinValue() const
204 return getAccessibleNode()->getMinValue();
207 const double UiObject::getMaxValue() const
209 return getAccessibleNode()->getMaxValue();
212 const double UiObject::getValue() const
214 return getAccessibleNode()->getValue();
217 const double UiObject::getIncrement() const
219 return getAccessibleNode()->getIncrement();
222 bool UiObject::setValue(double value)
224 return getAccessibleNode()->setValue(value);
227 bool UiObject::setText(std::string text)
229 return getAccessibleNode()->setValue(text);
232 std::string UiObject::getOcrText() const
234 return getAccessibleNode()->getOcrText();
237 std::string UiObject::getToolkitName() const
239 getAccessibleNode()->updateToolkitName();
240 return getAccessibleNode()->getToolkitName();
243 void UiObject::setOcrText(std::string text)
245 getAccessibleNode()->setOcrText(text);
248 bool UiObject::isCheckable() const
250 return getAccessibleNode()->isCheckable();
253 bool UiObject::isChecked() const
255 return getAccessibleNode()->isChecked();
258 bool UiObject::isClickable() const
260 return getAccessibleNode()->isClickable();
263 bool UiObject::isEnabled() const
265 return getAccessibleNode()->isEnabled();
268 bool UiObject::isFocusable() const
270 return getAccessibleNode()->isFocusable();
273 bool UiObject::isFocused() const
275 return getAccessibleNode()->isFocused();
278 bool UiObject::isLongClickable() const
280 return getAccessibleNode()->isLongClickable();
283 bool UiObject::isScrollable() const
285 return getAccessibleNode()->isScrollable();
288 bool UiObject::isSelectable() const
290 return getAccessibleNode()->isSelectable();
293 bool UiObject::isSelected() const
295 return getAccessibleNode()->isSelected();
298 bool UiObject::isVisible() const
300 return getAccessibleNode()->isVisible();
303 bool UiObject::isShowing() const
305 return getAccessibleNode()->isShowing();
308 bool UiObject::isActive() const
310 return getAccessibleNode()->isActive();
313 void UiObject::refresh() const
318 void UiObject::updateRoleName() const
320 mNode->updateRoleName();
323 void UiObject::updateUniqueId() const
325 mNode->updateUniqueId();
328 void UiObject::updateName() const
333 void UiObject::updateApplication() const
335 mNode->updateApplication();
338 void UiObject::updateAttributes() const
340 mNode->updateAttributes();
343 void UiObject::updateStates() const
345 mNode->updateStates();
348 void UiObject::updateExtents() const
350 mNode->updateExtents();
353 void UiObject::updateXPath() const
355 mNode->updateXPath();
358 void UiObject::updateValue() const
360 mNode->updateValue();
363 void UiObject::updatePid() const
368 void UiObject::updateToolkitName() const
370 mNode->updateToolkitName();
373 bool UiObject::setFocus() const
375 return mNode->setFocus();
378 bool UiObject::isValid() const
380 return mNode->isValid();
383 const Rect<int> UiObject::getScreenBoundingBox() const
385 return mNode->getScreenBoundingBox();
388 const Rect<int> UiObject::getWindowBoundingBox() const
390 return mNode->getWindowBoundingBox();
393 void UiObject::click() const
395 mNode->updateExtents();
396 const Rect<int> rect = mNode->getScreenBoundingBox();
397 const Point2D<int> midPoint = rect.midPoint();
398 mDevice->click(midPoint.x, midPoint.y);
401 void UiObject::longClick(const unsigned int durationMs) const
403 mNode->updateExtents();
404 const Rect<int> rect = mNode->getScreenBoundingBox();
405 const Point2D<int> midPoint = rect.midPoint();
406 mDevice->click(midPoint.x, midPoint.y, durationMs);
409 bool UiObject::DoAtspiActivate() const
411 return mNode->doAction("activate");
415 std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
417 if (mNode == nullptr) throw;
418 // TODO : wait for animation and refresh current node
419 // mDevice->waitForIdle();