1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "test-util-export.h"
30 #include <boost/optional.hpp>
35 * Waits while spinning the event loop until notified, or timed out.
37 * Based on QSignalSpy (hence the name), but provides an API that is much more useful
38 * for writing asynchronous test cases.
40 class TEST_UTIL_EXPORT InvocationSpy : public QObject
45 InvocationSpy(QObject* parent = nullptr);
48 * Notifies the spy, which will cause it to return from wait().
53 * Waits for the spy to be notified within the given timeout.
55 * @param timeout Timeout for waiting
56 * @returns true if the spy was notified, and false if it timed out.
58 virtual bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60});
61 /// Internally used signal
65 QSignalSpy _internalSpy;
68 // -----------------------------------------------------------------------------------------------------------------------------------------
71 * Spy that allows to be notified with a value.
73 * Works like @a InvocationSpy, but takes a value when notified. After successful notification, the value
74 * can be accessed and used for test case expectations.
77 class ValueSpy : public InvocationSpy
80 using InvocationSpy::InvocationSpy;
83 * Notifies the spy with the given value.
85 * @param value The notification value
87 void notify(const T& value)
90 InvocationSpy::notify();
94 * Provides the value the spy was last notified with.
96 * @note The value is only valid if wait() returned with true.
97 * @returns The value given to notify(), or boost::none if the spy wasn't notified
99 T value() const { return *_value; }
102 boost::optional<T> _value;
105 // -----------------------------------------------------------------------------------------------------------------------------------------
108 * Spy that is notified by one (or multiple) signal(s).
110 * Unlike QSignalSpy, this class is not bound to a particular signal. Instead, SignalSpy::connect() must be called
111 * to define the signal prior to calling wait(). connect() can also be called more than once, in which case any of
112 * the connected signals will trigger the spy.
114 * Before wait() returns, it disconnects all existing signals, so the spy can be reused without having to explicitly
117 class TEST_UTIL_EXPORT SignalSpy : private InvocationSpy
120 using InvocationSpy::InvocationSpy;
123 * Connects a signal to wait for.
125 * @param sender The signal's sender
126 * @param sig The signal
128 template<typename TSender, typename TSignal>
129 void connect(const TSender* sender, TSignal sig) {
130 _connections.emplace_back(QObject::connect(sender, sig, this, &SignalSpy::notify));
134 * Waits for the spy to be notified with any of the connected signals within the given timeout.
136 * Any connections will be cleared before this function returns. When reusing the spy, one needs
137 * to connect signals before calling wait() again.
139 * @param timeout Timeout for waiting
140 * @returns true if a signal was received, and false if it timed out.
142 bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60}) override;
145 std::vector<QMetaObject::Connection> _connections;