test: Add InvocationSpy and ValueSpy helper classes
[quassel.git] / src / test / util / invocationspy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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     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  * Spy that allows to be notified with a value.
70  *
71  * Works like @a InvocationSpy, but takes a value when notified. After successful notification, the value
72  * can be accessed and used for test case expectations.
73  */
74 template<typename T>
75 class ValueSpy : public InvocationSpy
76 {
77 public:
78     using InvocationSpy::InvocationSpy;
79
80     /**
81      * Notifies the spy with the given value.
82      *
83      * @param value The notification value
84      */
85     void notify(const T& value)
86     {
87         _value = value;
88         InvocationSpy::notify();
89     }
90
91     /**
92      * Provides the value the spy was last notified with.
93      *
94      * @note The value is only valid if wait() returned with true.
95      * @returns The value given to notify(), or boost::none if the spy wasn't notified
96      */
97     T value() const { return *_value; }
98
99 private:
100     boost::optional<T> _value;
101 };
102
103 }  // namespace test