diff --git a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp index ca10ba52f60..505ee6d3a42 100644 --- a/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp +++ b/tests/auto/corelib/thread/qpromise/tst_qpromise.cpp @@ -105,6 +105,31 @@ struct ThreadWrapper }; #endif +struct FutureWatcher +{ + bool thenCalled = false; + bool onFailedCalled = false; + bool onCanceledCalled = false; + + FutureWatcher() = default; + Q_DISABLE_COPY_MOVE(FutureWatcher) + + template + explicit FutureWatcher(QFuture &f) { setFuture(f); } + + template + void setFuture(QFuture &f) + { + f.onFailed([&]{ + onFailedCalled = true; + if constexpr (!std::is_void_v) + return T{}; + }) + .then([&](auto&&...) { thenCalled = true; }) + .onCanceled([&]{ onCanceledCalled = true; }); + } +}; + void tst_QPromise::promise() { const auto testCanCreatePromise = [] (auto promise) { @@ -540,20 +565,15 @@ template static inline void testCancelWhenDestroyedRunsContinuations() { QFuture future; - bool onCanceledCalled = false; - bool thenCalled = false; + FutureWatcher r; { QPromise promise; future = promise.future(); - future.then([&] (auto&&) { - thenCalled = true; - }).onCanceled([&] () { - onCanceledCalled = true; - }); + r.setFuture(future); } QVERIFY(future.isFinished()); - QVERIFY(!thenCalled); - QVERIFY(onCanceledCalled); + QVERIFY(!r.thenCalled); + QVERIFY(r.onCanceledCalled); } void tst_QPromise::cancelWhenDestroyedRunsContinuations() @@ -568,24 +588,15 @@ template static inline void testCancelWhenDestroyedWithFailureHandler() { QFuture future; - bool onFailedCalled = false; - bool thenCalled = false; + FutureWatcher r; { QPromise promise; future = promise.future(); - future - .onFailed([&] () { - onFailedCalled = true; - if constexpr (!std::is_same_v) - return T{}; - }) - .then([&] (auto&&) { - thenCalled = true; - }); + r.setFuture(future); } QVERIFY(future.isFinished()); - QVERIFY(!onFailedCalled); - QVERIFY(!thenCalled); + QVERIFY(!r.onFailedCalled); + QVERIFY(!r.thenCalled); } void tst_QPromise::cancelWhenDestroyedWithFailureHandler() @@ -606,10 +617,7 @@ static inline void testContinuationsRunWhenFinished() QPromise promise; QFuture future = promise.future(); - bool thenCalled = false; - future.then([&] (auto&&) { - thenCalled = true; - }); + FutureWatcher r(future); promise.start(); if constexpr (!std::is_void_v) { @@ -617,7 +625,7 @@ static inline void testContinuationsRunWhenFinished() } promise.finish(); - QVERIFY(thenCalled); + QVERIFY(r.thenCalled); } void tst_QPromise::continuationsRunWhenFinished()