ADTF  3.18.3
qt_test_tools.h
Go to the documentation of this file.
1 
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>
19 #include <chrono>
20 
21 namespace adtf
22 {
23 namespace ui
24 {
25 namespace testing
26 {
27 namespace hollow
28 {
29 
39 template <typename T>
40 T* 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,
77  "No widget with name %s and type %s.",
78  strObjectName.toLocal8Bit().data(),
79  T::staticMetaObject.className());
80  }
81 
82  return pObject;
83 }
84 
92 QString dump_tree(const QTreeWidgetItem* pNode, size_t nColumnCount, QString strPrefix = "");
93 
100 QString dump_tree(const QTreeWidget* pTree, size_t nColumnCount);
101 
108 tResult compare_dump(const QString& strChecked, const QString& strExpected);
109 
117 
118 }
119 
120 namespace lucky
121 {
122 
127 void schedule_in_gui_thread(std::function<void()> fnFunction);
128 
136 tResult execute_in_gui_thread(std::function<tResult()> fnFunction,
138 
139 }
140 
141 namespace mega
142 {
143 
151 
159 QAction* find_action(QMenu* pMenu, const QString& strName, bool bThrow = true);
160 
166 void trigger_action(QAction* pAction, Qt::ConnectionType eConnectionType = Qt::BlockingQueuedConnection);
167 
173 void trigger_action(const QString& strActionObjectName, Qt::ConnectionType eConnectionType = Qt::BlockingQueuedConnection);
174 
175 }
176 
177 namespace nitro
178 {
187 QString dump_tree(const QAbstractItemModel* pTree, size_t nColumnCount, const QModelIndex oNode = QModelIndex(), QString strPrefix = "");
188 }
189 
191 using hollow::dump_tree;
196 using mega::wait_for_next_idle;
197 using mega::find_action;
198 using mega::trigger_action;
199 using nitro::dump_tree;
200 
201 }
202 }
203 }
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
QString dump_tree(const QTreeWidgetItem *pNode, size_t nColumnCount, QString strPrefix="")
Dumps the content of a QTreeWidgetItem.
T * find_qobject(const QString &strObjectName)
Utility that finds a widget/object by its object name.
Definition: qt_test_tools.h:40
tResult wait_for_next_ui_timer(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
Wait until at least one more OnTimer() call have been issued by the X system.
tResult compare_dump(const QString &strChecked, const QString &strExpected)
Compare to large strings.
void schedule_in_gui_thread(std::function< void()> fnFunction)
Schedules the given function to be executed from within the GUI thread some time in the future.
tResult execute_in_gui_thread(std::function< tResult()> fnFunction, std::chrono::milliseconds tmTimeout=std::chrono::milliseconds{0})
Execute the given function from within the GUI thread and wait until it has finished.
Namespace for entire ADTF SDK.
QAction * find_action(QMenu *pMenu, const QString &strName, bool bThrow=true)
Finds an action within a menu.
tResult wait_for_next_idle(std::chrono::nanoseconds tmTimeout=std::chrono::nanoseconds{-1})
Wait until at least one more OnIdle() calls have been issued by the X system.
void trigger_action(QAction *pAction, Qt::ConnectionType eConnectionType=Qt::BlockingQueuedConnection)
Trigger an action within the Qt GUI thread.
#define THROW_ERROR_DESC(_code,...)
throws a tResult exception