ADTF Qt5
Loading...
Searching...
No Matches
qt_test_tools.h
Go to the documentation of this file.
1
7
8/*
9 * This file depends on Qt which is licensed under LGPLv3.
10 * See ADTF_DIR/3rdparty/qt5 and doc/license for detailed information.
11 */
12#pragma once
13#include <QString>
14#include <QApplication>
15#include <QTreeWidget>
16#include <QThread>
17#include <adtf_utils.h>
18#include <adtfbase/exception_handling.h>
19#include <chrono>
20
21namespace adtf
22{
23namespace ui
24{
25namespace testing
26{
27namespace hollow
28{
29
39template<typename T>
40T* find_qobject(const QString& strObjectName)
41{
42 auto fnFindObject = [&]() -> T*
43 {
44 for (auto pWidget : qApp->topLevelWidgets())
45 {
46 if (auto pFound = qobject_cast<T*>(pWidget))
47 {
48 if (strObjectName.isEmpty() || strObjectName == pFound->objectName())
49 {
50 return pFound;
51 }
52 }
53
54 if (auto pFound = pWidget->findChild<T*>(strObjectName))
55 {
56 return pFound;
57 }
58 }
59
60 return nullptr;
61 };
62
63 T* pObject = nullptr;
64
65 if (QThread::currentThread() == qApp->thread())
66 {
67 pObject = fnFindObject();
68 }
69 else
70 {
71 QMetaObject::invokeMethod(qApp, fnFindObject, Qt::BlockingQueuedConnection, &pObject);
72 }
73
74 if (!pObject)
75 {
76 THROW_ERROR_DESC(ERR_NOT_FOUND, "No widget with name %s and type %s.", strObjectName.toLocal8Bit().data(),
77 T::staticMetaObject.className());
78 }
79
80 return pObject;
81}
82
90QString dump_tree(const QTreeWidgetItem* pNode, size_t nColumnCount, QString strPrefix = "");
91
98QString dump_tree(const QTreeWidget* pTree, size_t nColumnCount);
99
106tResult compare_dump(const QString& strChecked, const QString& strExpected);
107
114tResult wait_for_next_ui_timer(std::chrono::nanoseconds tmTimeout = std::chrono::nanoseconds{-1});
115
116} // namespace hollow
117
118namespace lucky
119{
120
125void schedule_in_gui_thread(std::function<void()> fnFunction);
126
134tResult execute_in_gui_thread(std::function<tResult()> fnFunction,
135 std::chrono::milliseconds tmTimeout = std::chrono::milliseconds{0});
136
137} // namespace lucky
138
139namespace mega
140{
141
148tResult wait_for_next_idle(std::chrono::nanoseconds tmTimeout = std::chrono::nanoseconds{-1});
149
157QAction* find_action(QMenu* pMenu, const QString& strName, bool bThrow = true);
158
164void trigger_action(QAction* pAction, Qt::ConnectionType eConnectionType = Qt::BlockingQueuedConnection);
165
171void trigger_action(const QString& strActionObjectName,
172 Qt::ConnectionType eConnectionType = Qt::BlockingQueuedConnection);
173
174} // namespace mega
175
176namespace nitro
177{
186QString dump_tree(const QAbstractItemModel* pTree,
187 size_t nColumnCount,
188 const QModelIndex oNode = QModelIndex(),
189 QString strPrefix = "");
190} // namespace nitro
191
192namespace v0
193{
194
204using nitro::dump_tree;
205
206} // namespace v0
207
208namespace v2
209{
219 {
220 private:
226 std::unique_ptr<QApplication> m_pApp;
227
228 // Member variables must stay alive as long as m_pApp exists.
229 int m_argc;
230 char* m_argv[2];
231
232 public:
239 : m_argc(1)
240 {
241 if (qApp)
242 {
243 throw std::runtime_error("QApplication already exists!");
244 }
245 // Static storage to prevent 'use-after-free' as QApplication
246 // keeps a reference to these variables.
247
248 m_argv[0] = const_cast<char*>("adtf_test");
249 m_argv[1] = nullptr;
250
251 m_pApp = std::make_unique<QApplication>(m_argc, m_argv);
252 }
253
257 cInitQt(const cInitQt&) = delete;
258
262 cInitQt& operator=(const cInitQt&) = delete;
263 };
264}
265
266using v0::find_qobject;
267using v0::dump_tree;
268using v0::compare_dump;
273using v0::find_action;
275using v0::dump_tree;
276
277using v2::cInitQt;
278
279} // namespace testing
280} // namespace ui
281} // namespace adtf
cInitQt()
Constructor that checks for and initializes the Qt environment.
Definition qt_test_tools.h:238
A RAII utility class for automatic Qt Application initialization.
Definition qt_test_tools.h:219
cInitQt(const cInitQt &)=delete
Deleted copy constructor to prevent multiple ownership of the QApplication.
cInitQt()
Constructor that checks for and initializes the Qt environment.
Definition qt_test_tools.h:238
cInitQt & operator=(const cInitQt &)=delete
Deleted assignment operator.
Legacy version namespace for all functionality taken over from ADTF v3.7.
Definition qt_test_tools.h:28
QString dump_tree(const QTreeWidgetItem *pNode, size_t nColumnCount, QString strPrefix="")
tResult wait_for_next_ui_timer(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
T * find_qobject(const QString &strObjectName)
Definition qt_test_tools.h:40
tResult compare_dump(const QString &strChecked, const QString &strExpected)
Legacy version namespace for all functionality taken over from ADTF v3.11.
Definition qt_test_tools.h:119
void schedule_in_gui_thread(std::function< void()> fnFunction)
tResult execute_in_gui_thread(std::function< tResult()> fnFunction, std::chrono::milliseconds tmTimeout=std::chrono::milliseconds{0})
Legacy version namespace for all functionality taken over from ADTF v3.12.
Definition qt_test_tools.h:140
QAction * find_action(QMenu *pMenu, const QString &strName, bool bThrow=true)
tResult wait_for_next_idle(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
void trigger_action(QAction *pAction, Qt::ConnectionType eConnectionType=Qt::BlockingQueuedConnection)
Legacy version namespace for all functionality taken over from ADTF v3.13.
Definition qt_test_tools.h:177
QString dump_tree(const QAbstractItemModel *pTree, size_t nColumnCount, const QModelIndex oNode=QModelIndex(), QString strPrefix="")
Namespace for all functionality provided since v1.0.
Definition qt_test_tools.h:193
void schedule_in_gui_thread(std::function< void()> fnFunction)
QAction * find_action(QMenu *pMenu, const QString &strName, bool bThrow=true)
QString dump_tree(const QTreeWidgetItem *pNode, size_t nColumnCount, QString strPrefix="")
tResult execute_in_gui_thread(std::function< tResult()> fnFunction, std::chrono::milliseconds tmTimeout=std::chrono::milliseconds{0})
tResult wait_for_next_ui_timer(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
T * find_qobject(const QString &strObjectName)
Definition qt_test_tools.h:40
tResult wait_for_next_idle(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
tResult compare_dump(const QString &strChecked, const QString &strExpected)
void trigger_action(QAction *pAction, Qt::ConnectionType eConnectionType=Qt::BlockingQueuedConnection)
Namespace for all functionality provided since v1.2.
Definition qt_test_tools.h:209
Namespace for all provided testing functionality helper.
Definition qt_test_tools.h:26
Namespace for the ADTF UI SDK.
Definition adtfui_pkg.h:13
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtfui_pkg.h:13