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"
32 #include <unordered_set>
33 #include <unordered_map>
35 using namespace Aurum;
36 using namespace AurumInternal;
38 std::once_flag UiDevice::mOnceFlag;
41 std::shared_ptr<ScreenAnalyzerWatcher> UiDevice::mSAWatcher;
44 UiDevice::UiDevice() : UiDevice(nullptr) {}
46 UiDevice::UiDevice(IDevice *impl)
47 : mDeviceImpl(impl), mWaiter(new Waiter{this})
49 LOGI("UiDevice constructor");
52 mSAWatcher = std::make_shared<ScreenAnalyzerWatcher>();
54 LOGI("UiDevice constructor finish");
63 std::shared_ptr<UiDevice> UiDevice::getInstance(IDevice *deviceImpl)
65 static std::shared_ptr<UiDevice> device{nullptr};
66 std::call_once(mOnceFlag, [deviceImpl] {
68 device.reset(new UiDevice(deviceImpl));
71 device.reset(new UiDevice(new TizenDeviceImpl()));
73 device.reset(new UiDevice(new MockDeviceImpl()));
81 std::vector<std::shared_ptr<AccessibleNode>> UiDevice::getWindowRoot() const
83 return mDeviceImpl->getWindowRoot();
86 bool UiDevice::hasObject(const std::shared_ptr<UiSelector> selector) const
88 auto rootNodes = getWindowRoot();
89 for (const auto &node : rootNodes) {
90 const std::shared_ptr<AccessibleNode> foundNode =
91 Comparer::findObject(getInstance(), selector, node);
92 if (foundNode) return true;
98 std::shared_ptr<UiObject> UiDevice::findObject(const std::shared_ptr<UiSelector> selector) const
100 auto rootNodes = getWindowRoot();
101 for (const auto &node : rootNodes) {
102 const std::shared_ptr<AccessibleNode> foundNode =
103 Comparer::findObject(getInstance(), selector, node);
105 return std::make_shared<UiObject>(getInstance(), selector, foundNode);
107 return std::shared_ptr<UiObject>{nullptr};
111 std::vector<std::shared_ptr<UiObject>> UiDevice::findObjects(
112 const std::shared_ptr<UiSelector> selector) const
114 std::vector<std::shared_ptr<UiObject>> ret{};
115 auto rootNodes = getWindowRoot();
116 for (const auto &window : rootNodes) {
117 std::vector<std::shared_ptr<AccessibleNode>> nodes{};
118 Comparer::findObjects(nodes, getInstance(), selector, window);
120 for (auto &node : nodes)
121 ret.push_back(std::make_shared<UiObject>(getInstance(), selector, node));
125 bool UiDevice::waitFor(
126 const std::function<bool(const ISearchable *)> condition) const
128 return mWaiter->waitFor(condition);
131 std::shared_ptr<UiObject> UiDevice::waitFor(
132 const std::function<std::shared_ptr<UiObject>(const ISearchable *)>
135 return mWaiter->waitFor(condition);
137 bool UiDevice::waitForIdle() const
139 std::this_thread::sleep_for(std::chrono::milliseconds{167});
143 bool UiDevice::waitForEvents(
144 const A11yEvent type, const int timeout) const
146 return executeAndWaitForEvents(NULL, type, timeout, std::string(), 0);
149 //FIXME: obj only need for idle event
150 bool UiDevice::executeAndWaitForEvents
151 (const Runnable *cmd, const A11yEvent type, const int timeout, const std::string packageName, const int count) const
153 //FIXME: Need to get top window
154 auto wins = this->getWindowRoot();
156 return AccessibleWatcher::getInstance()->executeAndWaitForEvents(cmd, type, timeout, packageName, wins[0], count);
159 bool UiDevice::sendKeyAndWaitForEvents(
160 const std::string keycode, const A11yEvent type, const int timeout) const
162 std::unique_ptr<SendKeyRunnable> cmd = std::make_unique<SendKeyRunnable>(keycode);
163 return executeAndWaitForEvents(cmd.get(), type, timeout, std::string(), 0);
166 bool UiDevice::click(const int x, const int y)
168 bool result = mDeviceImpl->click(x, y);
173 bool UiDevice::click(const int x, const int y, const unsigned int durationMs)
175 bool result = mDeviceImpl->click(x, y, durationMs);
180 bool UiDevice::drag(const int sx, const int sy, const int ex, const int ey,
181 const int steps, const int durationMs)
183 bool result = mDeviceImpl->drag(sx, sy, ex, ey, steps, durationMs);
188 int UiDevice::touchDown(const int x, const int y)
190 int seq = mDeviceImpl->touchDown(x, y);
194 bool UiDevice::touchMove(const int x, const int y, const int seq)
196 bool result = mDeviceImpl->touchMove(x, y, seq);
200 bool UiDevice::touchUp(const int x, const int y, const int seq)
202 bool result = mDeviceImpl->touchUp(x, y, seq);
207 bool UiDevice::wheelUp(int amount, const int durationMs)
209 bool result = mDeviceImpl->wheelUp(amount, durationMs);
214 bool UiDevice::wheelDown(int amount, const int durationMs)
216 bool result = mDeviceImpl->wheelDown(amount, durationMs);
221 bool UiDevice::pressBack(KeyRequestType type)
223 bool result = mDeviceImpl->pressBack(type);
228 bool UiDevice::pressHome(KeyRequestType type)
230 bool result = mDeviceImpl->pressHome(type);
235 bool UiDevice::pressMenu(KeyRequestType type)
237 bool result = mDeviceImpl->pressMenu(type);
242 bool UiDevice::pressVolUp(KeyRequestType type)
244 bool result = mDeviceImpl->pressVolUp(type);
249 bool UiDevice::pressVolDown(KeyRequestType type)
251 bool result = mDeviceImpl->pressVolDown(type);
256 bool UiDevice::pressPower(KeyRequestType type)
258 bool result = mDeviceImpl->pressPower(type);
263 bool UiDevice::pressKeyCode(std::string keycode, KeyRequestType type)
265 bool result = mDeviceImpl->pressKeyCode(keycode, type);
269 bool UiDevice::repeatKeyCode(std::string keycode, int intervalMs, int durationMs)
271 bool result = mDeviceImpl->repeatKeyCode(keycode, intervalMs, durationMs);
275 bool UiDevice::takeScreenshot(std::string path, bool asPixels, void **pixels)
277 return mDeviceImpl->takeScreenshot(path, asPixels, pixels);
280 long long UiDevice::getSystemTime(TimeRequestType type)
282 return mDeviceImpl->getSystemTime(type);
285 const Size2D<int> UiDevice::getScreenSize()
287 return mDeviceImpl->getScreenSize();
291 std::vector<std::shared_ptr<SaObject>> UiDevice::getSaObject()
293 return mSAWatcher->GetSaObjects();
296 std::shared_ptr<ScreenAnalyzerWatcher> UiDevice::getSAWatcher()
302 void UiDevice::RequestScreenAnalyze()
305 mSAWatcher->PublishData();
309 bool UiDevice::getExternalAppLaunched()
311 auto ret = this->getWindowRoot();
312 return (ret.size() > 0) ? false : true;
315 void UiDevice::setWithScreenAnalyzer(bool withScreenAnalyzer)
317 mIsWithSA = withScreenAnalyzer;
320 bool UiDevice::getWithScreenAnalyzer()
325 bool UiDevice::registerCallback(const A11yEvent type, EventHandler cb, void *data) const
327 return AccessibleWatcher::getInstance()->registerCallback(type, cb, data);