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.
21 #include "TizenDeviceImpl.h"
23 #include "MockDeviceImpl.h"
24 #include "AtspiAccessibleWatcher.h"
33 using namespace Aurum;
34 using namespace AurumInternal;
36 UiDevice::UiDevice() : UiDevice(nullptr) {}
38 UiDevice::UiDevice(IDevice *impl)
39 : mDeviceImpl(impl), mWaiter(new Waiter{this})
49 std::shared_ptr<UiDevice> UiDevice::getInstance(IDevice *deviceImpl)
51 static std::shared_ptr<UiDevice> device{nullptr};
54 device.reset(new UiDevice(deviceImpl));
56 if (device) return device;
59 device.reset(new UiDevice(new TizenDeviceImpl()));
61 device.reset(new UiDevice(new MockDeviceImpl()));
69 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
71 std::vector<std::shared_ptr<AccessibleNode>> ret{};
73 auto appsMap = AccessibleWatcher::getInstance()->getActiveAppMap();
74 LOGI("activeAppMap.size: %d" , (int)appsMap.size());
75 for (auto itr = appsMap.begin(); itr != appsMap.end(); itr++)
77 auto activeWindows = itr->second->getActiveWindows();
78 std::transform(activeWindows.begin(), activeWindows.end(), std::back_inserter(ret),
79 [&](std::shared_ptr<AccessibleWindow> window){
80 LOGI("active pkg: %s, window: %s", window->getAccessibleNode()->getPkg().c_str(), window->getTitle().c_str());
81 return window->getAccessibleNode();
89 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
91 auto rootNodes = getWindowRoot();
92 for (const auto &node : rootNodes) {
93 const std::shared_ptr<AccessibleNode> foundNode =
94 Comparer::findObject(getInstance(), selector, node);
95 if (foundNode) return true;
101 std::shared_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
103 auto rootNodes = getWindowRoot();
104 for (const auto &node : rootNodes) {
105 const std::shared_ptr<AccessibleNode> foundNode =
106 Comparer::findObject(getInstance(), selector, node);
108 return std::make_shared<UiObject>(getInstance(), selector, foundNode);
110 return std::shared_ptr<UiObject>{nullptr};
114 std::vector<std::shared_ptr<UiObject>> UiDevice::findObjects(
115 const std::shared_ptr<UiSelector> selector) const
117 std::vector<std::shared_ptr<UiObject>> ret{};
118 auto rootNodes = getWindowRoot();
119 for (const auto &window : rootNodes) {
120 std::vector<std::shared_ptr<AccessibleNode>> nodes =
121 Comparer::findObjects(getInstance(), selector, window);
122 for (auto &node : nodes)
123 ret.push_back(std::make_shared<UiObject>(getInstance(), selector, node));
127 bool UiDevice::waitFor(
128 const std::function<bool(const ISearchable *)> condition) const
130 return mWaiter->waitFor(condition);
133 std::shared_ptr<UiObject> UiDevice::waitFor(
134 const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
137 return mWaiter->waitFor(condition);
139 bool UiDevice::waitForIdle() const
141 std::this_thread::sleep_for(std::chrono::milliseconds{167});
145 bool UiDevice::waitForEvents(
146 const A11yEvent type, const int timeout) const
148 return executeAndWaitForEvents(NULL, type, timeout);
151 bool UiDevice::executeAndWaitForEvents(
152 const Runnable *cmd, const A11yEvent type, const int timeout) const
154 return AccessibleWatcher::getInstance()->executeAndWaitForEvents(cmd, type, timeout);
157 bool UiDevice::sendKeyAndWaitForEvents(
158 const std::string keycode, const A11yEvent type, const int timeout) const
160 std::unique_ptr<SendKeyRunnable> cmd = std::make_unique<SendKeyRunnable>(keycode);
161 return executeAndWaitForEvents(cmd.get(), type, timeout);
164 bool UiDevice::click(const int x, const int y)
166 bool result = mDeviceImpl->click(x, y);
171 bool UiDevice::click(const int x, const int y, const unsigned int durationMs)
173 bool result = mDeviceImpl->click(x, y, durationMs);
178 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
179 const int steps, const int durationMs)
181 bool result = mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
186 int UiDevice::touchDown(const int x, const int y)
188 int seq = mDeviceImpl->touchDown(x, y);
192 bool UiDevice::touchMove(const int x, const int y, const int seq)
194 bool result = mDeviceImpl->touchMove(x, y, seq);
198 bool UiDevice::touchUp(const int x, const int y, const int seq)
200 bool result = mDeviceImpl->touchUp(x, y, seq);
205 bool UiDevice::wheelUp(int amount, const int durationMs)
207 bool result = mDeviceImpl->wheelUp(amount, durationMs);
212 bool UiDevice::wheelDown(int amount, const int durationMs)
214 bool result = mDeviceImpl->wheelDown(amount, durationMs);
219 bool UiDevice::pressBack(KeyRequestType type)
221 bool result = mDeviceImpl->pressBack(type);
226 bool UiDevice::pressHome(KeyRequestType type)
228 bool result = mDeviceImpl->pressHome(type);
233 bool UiDevice::pressMenu(KeyRequestType type)
235 bool result = mDeviceImpl->pressMenu(type);
240 bool UiDevice::pressVolUp(KeyRequestType type)
242 bool result = mDeviceImpl->pressVolUp(type);
247 bool UiDevice::pressVolDown(KeyRequestType type)
249 bool result = mDeviceImpl->pressVolDown(type);
254 bool UiDevice::pressPower(KeyRequestType type)
256 bool result = mDeviceImpl->pressPower(type);
261 bool UiDevice::pressKeyCode(std::string keycode, KeyRequestType type)
263 bool result = mDeviceImpl->pressKeyCode(keycode, type);
267 bool UiDevice::takeScreenshot(std::string path, float scale, int quality)
269 return mDeviceImpl->takeScreenshot(path, scale, quality);
272 long long UiDevice::getSystemTime(TimeRequestType type)
274 return mDeviceImpl->getSystemTime(type);
277 const Size2D<int> UiDevice::getScreenSize()
279 return mDeviceImpl->getScreenSize();