64fd6763e1ab60fde2d69b7deb92fde6ba6ea77d
[platform/core/uifw/aurum.git] / libaurum / src / UiDevice.cc
1 #include "UiDevice.h"
2 #include "AccessibleWatcher.h"
3 #include "Comparer.h"
4 #include "DeviceImpl/TizenImpl.h"
5
6 #include <unistd.h>
7 #include <utility>
8 #include <vector>
9
10 UiDevice::UiDevice() : UiDevice(DeviceType::DEFAULT, nullptr) {}
11
12 UiDevice::UiDevice(DeviceType type, IDevice *impl)
13     : mType(type), mDeviceImpl(impl), mWaiter(new Waiter{this})
14 {
15 }
16
17 UiDevice::~UiDevice()
18 {
19     delete mDeviceImpl;
20     delete mWaiter;
21 }
22
23 UiDevice *UiDevice::getInstance(DeviceType type)
24 {
25     static UiDevice *device = nullptr;
26 #ifdef TIZEN
27     if (!device) device = new UiDevice(type, new TizenImpl());
28 #endif
29     return device;
30 }
31
32 std::vector<std::unique_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
33 {
34     return AccessibleWatcher::getInstance()->getTopNode();
35 }
36
37 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
38 {
39     auto root = getWindowRoot();
40     for (auto it = root.begin(); it != root.end(); ++it) {
41         std::unique_ptr<AccessibleNode> node =
42             Comparer::findObject(this, selector, (*it).get());
43         if (node != nullptr) return true;
44     }
45
46     return false;
47 }
48
49 std::unique_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
50 {
51     auto root = getWindowRoot();
52     for (auto it = root.begin(); it != root.end(); ++it) {
53         std::unique_ptr<AccessibleNode> node =
54             Comparer::findObject(this, selector, (*it).get());
55         if (node)
56             return std::make_unique<UiObject>(this, selector, std::move(node));
57     }
58     return std::unique_ptr<UiObject>{nullptr};
59 }
60
61 std::vector<std::unique_ptr<UiObject>> UiDevice::findObjects(
62     const std::shared_ptr<UiSelector> selector) const
63 {
64     std::vector<std::unique_ptr<UiObject>> ret{};
65     auto root = getWindowRoot();
66     for (auto it = root.begin(); it != root.end(); ++it) {
67         std::vector<std::unique_ptr<AccessibleNode>>          nodes =
68             Comparer::findObjects(this, selector, (*it).get());
69
70         for (auto &node : nodes)
71             ret.push_back(std::make_unique<UiObject>(this, selector, std::move(node)));
72     }
73     return ret;
74 }
75 bool UiDevice::waitFor(
76     const std::function<bool(const ISearchable *)> condition) const
77 {
78     return mWaiter->waitFor(condition);
79 }
80
81 std::unique_ptr<UiObject> UiDevice::waitFor(
82     const std::function<std::unique_ptr<UiObject>(const ISearchable *)>
83         condition) const
84 {
85     return mWaiter->waitFor(condition);
86 }
87
88 bool UiDevice::click(const int x, const int y)
89 {
90     return mDeviceImpl->click(x, y);
91 }
92
93 bool UiDevice::click(const int x, const int y, const unsigned int intv)
94 {
95     return mDeviceImpl->click(x, y, intv);
96 }
97
98 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
99                     const int steps)
100 {
101     return mDeviceImpl->drag(sx, sy, ex, ey, steps);
102 }
103
104 bool UiDevice::touchDown(const int x, const int y)
105 {
106     return mDeviceImpl->touchDown(x, y);
107 }
108
109 bool UiDevice::touchMove(const int x, const int y)
110 {
111     return mDeviceImpl->touchMove(x, y);
112 }
113
114 bool UiDevice::touchUp(const int x, const int y)
115 {
116     return mDeviceImpl->touchUp(x, y);
117 }
118
119 bool UiDevice::pressBack()
120 {
121     return mDeviceImpl->pressBack();
122 }
123
124 bool UiDevice::pressHome()
125 {
126     return mDeviceImpl->pressHome();
127 }
128
129 bool UiDevice::pressMenu()
130 {
131     return mDeviceImpl->pressMenu();
132 }
133
134 bool UiDevice::pressVolUp()
135 {
136     return mDeviceImpl->pressVolUp();
137 }
138
139 bool UiDevice::pressVolDown()
140 {
141     return mDeviceImpl->pressVolDown();
142 }
143
144 bool UiDevice::pressPower()
145 {
146     return mDeviceImpl->pressPower();
147 }
148
149 bool UiDevice::pressKeyCode(std::string keycode)
150 {
151     return mDeviceImpl->pressKeyCode(keycode);
152 }
153
154 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
155 {
156     return mDeviceImpl->takeScreenshot(path, scale, quality);
157 }
158
159 long long UiDevice::getSystemTime(TypeRequestType type)
160 {
161     return mDeviceImpl->getSystemTime(type);
162 }