Add a flag to enable Qt deprecation warnings on Qt < 5.13
[quassel.git] / src / test / util / invocationspy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "test-util-export.h"
24
25 #include <chrono>
26
27 #include <QObject>
28 #include <QSignalSpy>
29
30 #include <boost/optional.hpp>
31
32 namespace test {
33
34 /**
35  * Waits while spinning the event loop until notified, or timed out.
36  *
37  * Based on QSignalSpy (hence the name), but provides an API that is much more useful
38  * for writing asynchronous test cases.
39  */
40 class TEST_UTIL_EXPORT InvocationSpy : public QObject
41 {
42     Q_OBJECT
43
44 public:
45     InvocationSpy(QObject* parent = nullptr);
46
47     /**
48      * Notifies the spy, which will cause it to return from wait().
49      */
50     void notify();
51
52     /**
53      * Waits for the spy to be notified within the given timeout.
54      *
55      * @param timeout Timeout for waiting
56      * @returns true if the spy was notified, and false if it timed out.
57      */
58     virtual bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60});
59
60 signals:
61     /// Internally used signal
62     void notified();
63
64 private:
65     QSignalSpy _internalSpy;
66 };
67
68 // -----------------------------------------------------------------------------------------------------------------------------------------
69
70 /**
71  * Spy that allows to be notified with a value.
72  *
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.
75  */
76 template<typename T>
77 class ValueSpy : public InvocationSpy
78 {
79 public:
80     using InvocationSpy::InvocationSpy;
81
82     /**
83      * Notifies the spy with the given value.
84      *
85      * @param value The notification value
86      */
87     void notify(const T& value)
88     {
89         _value = value;
90         InvocationSpy::notify();
91     }
92
93     /**
94      * Provides the value the spy was last notified with.
95      *
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
98      */
99     T value() const { return *_value; }
100
101 private:
102     boost::optional<T> _value;
103 };
104
105 // -----------------------------------------------------------------------------------------------------------------------------------------
106
107 /**
108  * Spy that is notified by one (or multiple) signal(s).
109  *
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.
113  *
114  * Before wait() returns, it disconnects all existing signals, so the spy can be reused without having to explicitly
115  * reset it.
116  */
117 class TEST_UTIL_EXPORT SignalSpy : private InvocationSpy
118 {
119 public:
120     using InvocationSpy::InvocationSpy;
121
122     /**
123      * Connects a signal to wait for.
124      *
125      * @param sender The signal's sender
126      * @param sig The signal
127      */
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));
131     }
132
133     /**
134      * Waits for the spy to be notified with any of the connected signals within the given timeout.
135      *
136      * Any connections will be cleared before this function returns. When reusing the spy, one needs
137      * to connect signals before calling wait() again.
138      *
139      * @param timeout Timeout for waiting
140      * @returns true if a signal was received, and false if it timed out.
141      */
142     bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60}) override;
143
144 private:
145     std::vector<QMetaObject::Connection> _connections;
146 };
147
148 }  // namespace test