From a290ec98c2edf753f253d22d245190bb19f9822e Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 25 Mar 2021 10:02:55 +0100 Subject: 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 --- tests/auto/shared/model_utilities.h | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) 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 #include +// 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 watcher; + QSignalSpy watcherSpy(&watcher, &QFutureWatcher::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 m_promise; +}; + namespace { inline bool compareIndices(const QModelIndex &lhs, const QModelIndex &rhs) -- cgit v1.2.3