summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2021-03-25 10:02:55 +0100
committerSona Kurazyan <sona.kurazyan@qt.io>2021-04-16 19:10:36 +0200
commita290ec98c2edf753f253d22d245190bb19f9822e (patch)
treebfc397e2726d9bc869aa07637162872543f1ff6c
parent13df88781d77e887712692906f6693fbbe0a8999 (diff)
Add a helper class for waiting for model changes in tests
The tests are using complicated logic when waiting for model changes. This patch introduces a new WaitHelper class, which uses a QPromise to indicate the start and finish of a task, and QFutureWatcher to wait for the associated future to finish. This class will be reused in other parts of code, where waiting is required. Task-number: QTBUG-90688 Pick-to: 5.15 Change-Id: I4a412fa555be342c168e526601f8fc56d5091872 Reviewed-by: Brett Stottlemyer <bstottle@ford.com>
-rw-r--r--tests/auto/shared/model_utilities.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/auto/shared/model_utilities.h b/tests/auto/shared/model_utilities.h
index 7e30382..b1044a5 100644
--- a/tests/auto/shared/model_utilities.h
+++ b/tests/auto/shared/model_utilities.h
@@ -29,6 +29,48 @@
#include <QtTest/QtTest>
#include <QModelIndex>
+// Helper class which can be used by tests for starting a task and
+// waiting for its completion. It takes care of running an event
+// loop while waiting, until finished() method is called (or the
+// timeout is reached).
+class WaitHelper : public QObject
+{
+ Q_OBJECT
+
+public:
+ WaitHelper() { m_promise.reportStarted(); }
+
+ ~WaitHelper()
+ {
+ if (m_promise.future().isRunning())
+ m_promise.reportFinished();
+ }
+
+ /*
+ Starts an event loop and waits until finish() method is called
+ or the timeout is reached.
+ */
+ bool wait(int timeout = 30000)
+ {
+ if (m_promise.future().isFinished())
+ return true;
+
+ QFutureWatcher<void> watcher;
+ QSignalSpy watcherSpy(&watcher, &QFutureWatcher<void>::finished);
+ watcher.setFuture(m_promise.future());
+ return watcherSpy.wait(timeout);
+ }
+
+protected:
+ /*
+ The derived classes need to call this method to stop waiting.
+ */
+ void finish() { m_promise.reportFinished(); }
+
+private:
+ QFutureInterface<void> m_promise;
+};
+
namespace {
inline bool compareIndices(const QModelIndex &lhs, const QModelIndex &rhs)