X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Ftest%2Futil%2Finvocationspy.h;fp=src%2Ftest%2Futil%2Finvocationspy.h;h=903fc386d270fc9b5578d509c1a25c7bda9d6741;hp=92dd8ec4e5002e33a5f1a4e85324ea2565bcbdaa;hb=12a6abcfe51ac66e8763b3caadaedcb47c2723f1;hpb=7dee6df30bc997b3045ec0e20cdefdf8a70ba93e diff --git a/src/test/util/invocationspy.h b/src/test/util/invocationspy.h index 92dd8ec4..903fc386 100644 --- a/src/test/util/invocationspy.h +++ b/src/test/util/invocationspy.h @@ -55,7 +55,7 @@ public: * @param timeout Timeout for waiting * @returns true if the spy was notified, and false if it timed out. */ - bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60}); + virtual bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60}); signals: /// Internally used signal @@ -65,6 +65,8 @@ private: QSignalSpy _internalSpy; }; +// ----------------------------------------------------------------------------------------------------------------------------------------- + /** * Spy that allows to be notified with a value. * @@ -100,4 +102,47 @@ private: boost::optional _value; }; +// ----------------------------------------------------------------------------------------------------------------------------------------- + +/** + * Spy that is notified by one (or multiple) signal(s). + * + * Unlike QSignalSpy, this class is not bound to a particular signal. Instead, SignalSpy::connect() must be called + * to define the signal prior to calling wait(). connect() can also be called more than once, in which case any of + * the connected signals will trigger the spy. + * + * Before wait() returns, it disconnects all existing signals, so the spy can be reused without having to explicitly + * reset it. + */ +class TEST_UTIL_EXPORT SignalSpy : private InvocationSpy +{ +public: + using InvocationSpy::InvocationSpy; + + /** + * Connects a signal to wait for. + * + * @param sender The signal's sender + * @param sig The signal + */ + template + void connect(const TSender* sender, TSignal sig) { + _connections.emplace_back(QObject::connect(sender, sig, this, &SignalSpy::notify)); + } + + /** + * Waits for the spy to be notified with any of the connected signals within the given timeout. + * + * Any connections will be cleared before this function returns. When reusing the spy, one needs + * to connect signals before calling wait() again. + * + * @param timeout Timeout for waiting + * @returns true if a signal was received, and false if it timed out. + */ + bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60}) override; + +private: + std::vector _connections; +}; + } // namespace test