1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 bool wait(std::chrono::milliseconds timeout = std::chrono::seconds{60});
61 /// Internally used signal
65 QSignalSpy _internalSpy;
69 * Spy that allows to be notified with a value.
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.
75 class ValueSpy : public InvocationSpy
78 using InvocationSpy::InvocationSpy;
81 * Notifies the spy with the given value.
83 * @param value The notification value
85 void notify(const T& value)
88 InvocationSpy::notify();
92 * Provides the value the spy was last notified with.
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
97 T value() const { return *_value; }
100 boost::optional<T> _value;