2 #include "AccessibleWatcher.h"
6 #include "TizenDeviceImpl.h"
8 #include "MockDeviceImpl.h"
18 UiDevice::UiDevice() : UiDevice(nullptr) {}
20 UiDevice::UiDevice(IDevice *impl)
21 : mDeviceImpl(impl), mWaiter(new Waiter{this})
31 std::shared_ptr<UiDevice> UiDevice::getInstance(IDevice *deviceImpl)
33 static std::shared_ptr<UiDevice> device{nullptr};
36 device.reset(new UiDevice(deviceImpl));
38 if (device) return device;
41 device.reset(new UiDevice(new TizenDeviceImpl()));
43 device.reset(new UiDevice(new MockDeviceImpl()));
51 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
53 std::vector<std::shared_ptr<AccessibleNode>> ret{};
55 auto apps = AccessibleWatcher::getInstance()->getActiveApplications();
56 for (auto &app : apps){
57 auto activeWindows = app->getActiveWindows();
58 std::transform(activeWindows.begin(), activeWindows.end(), std::back_inserter(ret),
59 [&](std::shared_ptr<AccessibleWindow> window){
60 return window->getNode();
67 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
69 auto rootNodes = getWindowRoot();
70 for (const auto &node : rootNodes) {
71 const std::shared_ptr<AccessibleNode> foundNode =
72 Comparer::findObject(getInstance(), selector, node);
73 if (foundNode) return true;
79 std::shared_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
81 auto rootNodes = getWindowRoot();
82 for (const auto &node : rootNodes) {
83 const std::shared_ptr<AccessibleNode> foundNode =
84 Comparer::findObject(getInstance(), selector, node);
86 return std::make_shared<UiObject>(getInstance(), selector, foundNode);
88 return std::shared_ptr<UiObject>{nullptr};
92 std::vector<std::shared_ptr<UiObject>> UiDevice::findObjects(
93 const std::shared_ptr<UiSelector> selector) const
95 std::vector<std::shared_ptr<UiObject>> ret{};
96 auto rootNodes = getWindowRoot();
97 for (const auto &window : rootNodes) {
98 std::vector<std::shared_ptr<AccessibleNode>> nodes =
99 Comparer::findObjects(getInstance(), selector, window);
100 for (auto &node : nodes)
101 ret.push_back(std::make_shared<UiObject>(getInstance(), selector, node));
105 bool UiDevice::waitFor(
106 const std::function<bool(const ISearchable *)> condition) const
108 return mWaiter->waitFor(condition);
111 std::shared_ptr<UiObject> UiDevice::waitFor(
112 const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
115 return mWaiter->waitFor(condition);
117 bool UiDevice::waitForIdle() const
119 std::this_thread::sleep_for(std::chrono::milliseconds{167});
123 bool UiDevice::click(const int x, const int y)
125 bool result = mDeviceImpl->click(x, y);
130 bool UiDevice::click(const int x, const int y, const unsigned int intv)
132 bool result = mDeviceImpl->click(x, y, intv);
137 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
138 const int steps, const int durationMs)
140 bool result = mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
145 int UiDevice::touchDown(const int x, const int y)
147 int seq = mDeviceImpl->touchDown(x, y);
151 bool UiDevice::touchMove(const int x, const int y, const int seq)
153 bool result = mDeviceImpl->touchMove(x, y, seq);
157 bool UiDevice::touchUp(const int x, const int y, const int seq)
159 bool result = mDeviceImpl->touchUp(x, y, seq);
164 bool UiDevice::wheelUp(int amount, const int durationMs)
166 bool result = mDeviceImpl->wheelUp(amount, durationMs);
171 bool UiDevice::wheelDown(int amount, const int durationMs)
173 bool result = mDeviceImpl->wheelDown(amount, durationMs);
178 bool UiDevice::pressBack(KeyRequestType type)
180 bool result = mDeviceImpl->pressBack(type);
185 bool UiDevice::pressHome(KeyRequestType type)
187 bool result = mDeviceImpl->pressHome(type);
192 bool UiDevice::pressMenu(KeyRequestType type)
194 bool result = mDeviceImpl->pressMenu(type);
199 bool UiDevice::pressVolUp(KeyRequestType type)
201 bool result = mDeviceImpl->pressVolUp(type);
206 bool UiDevice::pressVolDown(KeyRequestType type)
208 bool result = mDeviceImpl->pressVolDown(type);
213 bool UiDevice::pressPower(KeyRequestType type)
215 bool result = mDeviceImpl->pressPower(type);
220 bool UiDevice::pressKeyCode(std::string keycode, KeyRequestType type)
222 bool result = mDeviceImpl->pressKeyCode(keycode, type);
227 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
229 return mDeviceImpl->takeScreenshot(path, scale, quality);
232 long long UiDevice::getSystemTime(TimeRequestType type)
234 return mDeviceImpl->getSystemTime(type);