Aurum: Improve performance of findElements command
[platform/core/uifw/aurum.git] / libaurum / src / UiObject.cc
index 85e349c..3bbecc0 100644 (file)
@@ -1,42 +1,59 @@
-#include "UiObject.h"
-#include "Comparer.h"
-#include "Sel.h"
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+#include "Aurum.h"
 
 #include <iostream>
 #include <utility>
 
-#include "loguru.hpp"
+#include <chrono>
+#include <thread>
 
-UiObject::UiObject() {}
+using namespace Aurum;
+
+UiObject::UiObject() : UiObject(nullptr, nullptr, nullptr) {}
 
 UiObject::~UiObject()
 {
     if (mWaiter) delete mWaiter;
 }
 
-UiObject::UiObject(const UiDevice *device, const UiSelector *selector,
+UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
                    const AccessibleNode *node)
     : mDevice(device),
       mSelector(selector),
-      mNode(node),
+      mNode(std::shared_ptr<AccessibleNode>(const_cast<AccessibleNode *>(node))),
       mWaiter(new Waiter{this, this})
 {
-    // tood interface to interact with input interface
-    // mInputImple = mDevice->getInputInterface();
 }
 
-UiObject::UiObject(const UiObject &src)
-    : mDevice(src.mDevice),
-      mSelector(src.mSelector),
-      mNode(src.mNode),
-      mWaiter{src.mWaiter}
+UiObject::UiObject(const std::shared_ptr<UiDevice> device, const std::shared_ptr<UiSelector> selector,
+                   std::shared_ptr<AccessibleNode> node)
+    : mDevice(device),
+      mSelector(selector),
+      mNode(std::move(node)),
+      mWaiter(new Waiter{this, this})
 {
 }
 
 UiObject::UiObject(UiObject &&src)
     : mDevice(src.mDevice),
-      mSelector(src.mSelector),
-      mNode(src.mNode),
+      mSelector(std::move(src.mSelector)),
+      mNode(std::move(src.mNode)),
       mWaiter{src.mWaiter}
 {
     src.mDevice = nullptr;
@@ -45,9 +62,14 @@ UiObject::UiObject(UiObject &&src)
     src.mWaiter = nullptr;
 }
 
-bool UiObject::hasObject(const UiSelector *selector) const
+std::shared_ptr<UiSelector> UiObject::getSelector()
+{
+    return this->mSelector;
+}
+
+bool UiObject::hasObject(const std::shared_ptr<UiSelector> selector) const
 {
-    AccessibleNode *node =
+    std::shared_ptr<AccessibleNode> node =
         Comparer::findObject(mDevice, selector, getAccessibleNode());
     if (node != nullptr) {
         // todo : what is this node.recycle()
@@ -56,20 +78,31 @@ bool UiObject::hasObject(const UiSelector *selector) const
     return false;
 }
 
-std::unique_ptr<UiObject> UiObject::findObject(const UiSelector *selector) const
+std::shared_ptr<UiObject> UiObject::findObject(const std::shared_ptr<UiSelector> selector) const
 {
-    AccessibleNode *node =
+    std::shared_ptr<AccessibleNode> node =
         Comparer::findObject(mDevice, selector, getAccessibleNode());
     if (node)
-        return std::make_unique<UiObject>(mDevice, selector, node);
+        return std::make_shared<UiObject>(mDevice, selector, std::move(node));
     else
-        return std::unique_ptr<UiObject>{nullptr};
+        return std::shared_ptr<UiObject>{nullptr};
 }
 
-std::vector<std::unique_ptr<UiObject>> UiObject::findObjects(
-    const UiSelector *selector) const
+std::vector<std::shared_ptr<UiObject>> UiObject::findObjects(
+    const std::shared_ptr<UiSelector> selector) const
 {
-    return std::vector<std::unique_ptr<UiObject>>{};
+    std::vector<std::shared_ptr<UiObject>> result{};
+
+    std::vector<std::shared_ptr<AccessibleNode>> nodes{};
+    Comparer::findObjects(nodes, mDevice, selector, getAccessibleNode());
+    for ( auto& node : nodes) {
+        if (!node) {
+            LOGI("Skipped! (node == nullptr)");
+            continue;
+        }
+        result.push_back(std::make_shared<UiObject>(mDevice, selector, std::move(node)));
+    }
+    return result;
 }
 
 bool UiObject::waitFor(
@@ -78,8 +111,8 @@ bool UiObject::waitFor(
     return mWaiter->waitFor(condition);
 }
 
-std::unique_ptr<UiObject> UiObject::waitFor(
-    const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
+std::shared_ptr<UiObject> UiObject::waitFor(
+    const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
         condition) const
 {
     return mWaiter->waitFor(condition);
@@ -88,15 +121,14 @@ std::unique_ptr<UiObject> UiObject::waitFor(
 bool UiObject::waitFor(
     const std::function<bool(const UiObject *)> condition) const
 {
-    LOG_F(INFO, "asdf");
     return mWaiter->waitFor(condition);
 }
 
 UiObject *UiObject::getParent() const
 {
-    AccessibleNode *node = getAccessibleNode()->getParent();
+    std::shared_ptr<AccessibleNode> node = getAccessibleNode()->getParent();
     if (!node) return nullptr;
-    return new UiObject(mDevice, mSelector, node);
+    return new UiObject(mDevice, mSelector, std::move(node));
 }
 
 int UiObject::getChildCount() const
@@ -104,14 +136,35 @@ int UiObject::getChildCount() const
     return getAccessibleNode()->getChildCount();
 }
 
-std::vector<std::unique_ptr<UiObject>> UiObject::getChildren() const
+std::shared_ptr<UiObject> UiObject::getChildAt(int index) const {
+    auto childNode = getAccessibleNode()->getChildAt(index);
+    if (childNode) {
+        return std::make_shared<UiObject>(mDevice, mSelector, childNode);
+    }
+    return nullptr;
+}
+
+std::vector<std::shared_ptr<UiObject>> UiObject::getChildren() const
 {
-    return findObjects(Sel::depth(1).get());
+    std::vector<std::shared_ptr<UiObject>> ret{};
+
+    auto children = getAccessibleNode()->getChildren();
+    for (auto &child : children) {
+        ret.push_back(std::make_shared<UiObject>(mDevice, mSelector, child));
+    }
+
+    return ret;
 }
 
-std::string UiObject::getContentDescription() const
+std::shared_ptr<Node> UiObject::getDescendant()
 {
-    return getAccessibleNode()->getDesc();
+    std::vector<std::shared_ptr<Node>> nodeChildren{};
+
+    auto children = getChildren();
+    for (auto &&child : children) {
+        nodeChildren.push_back(child->getDescendant());
+    }
+    return std::make_shared<Node>(shared_from_this(), nodeChildren);
 }
 
 std::string UiObject::getApplicationPackage() const
@@ -119,9 +172,24 @@ std::string UiObject::getApplicationPackage() const
     return getAccessibleNode()->getPkg();
 }
 
-std::string UiObject::getResourceName() const
+std::string UiObject::getId() const
+{
+    return getAccessibleNode()->getId();
+}
+
+std::string UiObject::getAutomationId() const
+{
+    return getAccessibleNode()->getAutomationId();
+}
+
+std::string UiObject::getType() const
+{
+    return getAccessibleNode()->getType();
+}
+
+std::string UiObject::getElementStyle() const
 {
-    return getAccessibleNode()->getRes();
+    return getAccessibleNode()->getStyle();
 }
 
 std::string UiObject::getText() const
@@ -129,9 +197,65 @@ std::string UiObject::getText() const
     return getAccessibleNode()->getText();
 }
 
-void UiObject::setText(std::string &text)
+std::string UiObject::getRole() const
+{
+    return getAccessibleNode()->getRole();
+}
+
+std::string UiObject::getXPath() const
+{
+    return getAccessibleNode()->getXPath();
+}
+
+const double UiObject::getMinValue() const
 {
-    getAccessibleNode()->setValue(text);
+    return getAccessibleNode()->getMinValue();
+}
+
+const double UiObject::getMaxValue() const
+{
+    return getAccessibleNode()->getMaxValue();
+}
+
+const double UiObject::getValue() const
+{
+    return getAccessibleNode()->getValue();
+}
+
+const double UiObject::getIncrement() const
+{
+    return getAccessibleNode()->getIncrement();
+}
+
+const Rect<int> UiObject::getTextMinBoundingRect() const
+{
+    return getAccessibleNode()->getTextMinBoundingRect();
+}
+
+bool UiObject::setValue(double value)
+{
+    return getAccessibleNode()->setValue(value);
+}
+
+bool UiObject::setText(std::string text)
+{
+    return getAccessibleNode()->setValue(text);
+}
+
+std::string UiObject::getOcrText() const
+{
+    return getAccessibleNode()->getOcrText();
+}
+
+std::string UiObject::getToolkitName() const
+{
+    getAccessibleNode()->updateToolkitName();
+    return getAccessibleNode()->getToolkitName();
+}
+
+void UiObject::setOcrText(std::string text)
+{
+    getAccessibleNode()->setOcrText(text);
 }
 
 bool UiObject::isCheckable() const
@@ -184,29 +308,137 @@ bool UiObject::isSelected() const
     return getAccessibleNode()->isSelected();
 }
 
+bool UiObject::isVisible() const
+{
+    return getAccessibleNode()->isVisible();
+}
+
+bool UiObject::isShowing() const
+{
+    return getAccessibleNode()->isShowing();
+}
+
+bool UiObject::isActive() const
+{
+    return getAccessibleNode()->isActive();
+}
+
+bool UiObject::isHighlightable() const
+{
+    return getAccessibleNode()->isHighlightable();
+}
+
 void UiObject::refresh() const
 {
     mNode->refresh();
 }
 
+void UiObject::updateRoleName() const
+{
+    mNode->updateRoleName();
+}
+
+void UiObject::updateUniqueId() const
+{
+    mNode->updateUniqueId();
+}
+
+void UiObject::updateName() const
+{
+    mNode->updateName();
+}
+
+void UiObject::updateApplication() const
+{
+    mNode->updateApplication();
+}
+
+void UiObject::updateAttributes() const
+{
+    mNode->updateAttributes();
+}
+
+void UiObject::updateStates() const
+{
+    mNode->updateStates();
+}
+
+void UiObject::updateExtents() const
+{
+    mNode->updateExtents();
+}
+
+void UiObject::updateXPath() const
+{
+    mNode->updateXPath();
+}
+
+void UiObject::updateValue() const
+{
+    mNode->updateValue();
+}
+
+void UiObject::updatePid() const
+{
+    mNode->updatePid();
+}
+
+void UiObject::updateToolkitName() const
+{
+    mNode->updateToolkitName();
+}
+
+void UiObject::updateTextMinBoundingRect() const
+{
+    mNode->updateTextMinBoundingRect();
+}
+
+bool UiObject::setFocus() const
+{
+    return mNode->setFocus();
+}
+
+bool UiObject::isValid() const
+{
+    return mNode->isValid();
+}
+
+const Rect<int> UiObject::getScreenBoundingBox() const
+{
+    return mNode->getScreenBoundingBox();
+}
+
+const Rect<int> UiObject::getWindowBoundingBox() const
+{
+    return mNode->getWindowBoundingBox();
+}
+
 void UiObject::click() const
 {
-    LOG_SCOPE_F(INFO, "click on obj %p", this);
-    mNode->refresh();
-    const Rect<int> rect = mNode->getBoundingBox();
-    std::cout << rect.mTopLeft.x << ", " << rect.mTopLeft.y << std::endl;
+    mNode->updateExtents();
+    const Rect<int> rect = mNode->getScreenBoundingBox();
     const Point2D<int> midPoint = rect.midPoint();
-    const_cast<UiDevice *>(mDevice)->click(midPoint.x, midPoint.y);
-    // todo click implementation
+    mDevice->click(midPoint.x, midPoint.y);
 }
 
-const AccessibleNode *UiObject::getAccessibleNode() const
+void UiObject::longClick(const unsigned int durationMs) const
 {
-    if (mNode == nullptr) throw;
+    mNode->updateExtents();
+    const Rect<int> rect = mNode->getScreenBoundingBox();
+    const Point2D<int> midPoint = rect.midPoint();
+    mDevice->click(midPoint.x, midPoint.y, durationMs);
+}
 
+bool UiObject::DoAtspiActivate() const
+{
+    return mNode->doAction("activate");
+}
+
+
+std::shared_ptr<AccessibleNode> UiObject::getAccessibleNode() const
+{
+    if (mNode == nullptr) throw;
     // TODO : wait for animation and refresh current node
     // mDevice->waitForIdle();
-    // mNode->refresh();
-
     return mNode;
-}
\ No newline at end of file
+}