Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
scoped_qcoreapplication.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <cassert>
7#include <chrono>
8#include <functional>
9#include <memory>
10
11#include <QCoreApplication>
12#include <QElapsedTimer>
13
14namespace zrythm::test_helpers
15{
23class ScopedQCoreApplication
24{
25public:
26 ScopedQCoreApplication ()
27 {
28 int argc = 0;
29 char ** argv = nullptr;
30 app_ = std::make_unique<QCoreApplication> (argc, argv);
31 }
32
39 const std::function<bool ()> &cond,
40 unsigned int max_calls = 100)
41 {
42 for (unsigned int i = 0; i < max_calls && !cond (); ++i)
43 {
44 QCoreApplication::processEvents (QEventLoop::AllEvents, 50);
45 }
46 if (!cond ())
47 {
48 throw std::runtime_error ("condition failed");
49 }
50 }
51
52 static void process_events_until_timeout (
53 std::chrono::milliseconds time = std::chrono::milliseconds (200))
54 {
55 QElapsedTimer timer;
56 timer.start ();
57 while (timer.durationElapsed () < time)
58 {
59 QCoreApplication::processEvents (QEventLoop::AllEvents, 50);
60 }
61 }
62
63private:
64 std::unique_ptr<QCoreApplication> app_;
65};
66}
static void process_events_until_true(const std::function< bool()> &cond, unsigned int max_calls=100)
Processes QCoreApplication events until cond returns true.