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=92dd8ec4e5002e33a5f1a4e85324ea2565bcbdaa;hp=0000000000000000000000000000000000000000;hb=34e58425ffb5475b210abb484970e674ddb98744;hpb=cf64023910d5b42477f6158f4cee853cc7f7f3f8 diff --git a/src/test/util/invocationspy.h b/src/test/util/invocationspy.h new file mode 100644 index 00000000..92dd8ec4 --- /dev/null +++ b/src/test/util/invocationspy.h @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (C) 2005-2018 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#pragma once + +#include "test-util-export.h" + +#include + +#include +#include + +#include + +namespace test { + +/** + * Waits while spinning the event loop until notified, or timed out. + * + * Based on QSignalSpy (hence the name), but provides an API that is much more useful + * for writing asynchronous test cases. + */ +class TEST_UTIL_EXPORT InvocationSpy : public QObject +{ + Q_OBJECT + +public: + InvocationSpy(QObject* parent = nullptr); + + /** + * Notifies the spy, which will cause it to return from wait(). + */ + void notify(); + + /** + * Waits for the spy to be notified within the given timeout. + * + * @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}); + +signals: + /// Internally used signal + void notified(); + +private: + QSignalSpy _internalSpy; +}; + +/** + * Spy that allows to be notified with a value. + * + * Works like @a InvocationSpy, but takes a value when notified. After successful notification, the value + * can be accessed and used for test case expectations. + */ +template +class ValueSpy : public InvocationSpy +{ +public: + using InvocationSpy::InvocationSpy; + + /** + * Notifies the spy with the given value. + * + * @param value The notification value + */ + void notify(const T& value) + { + _value = value; + InvocationSpy::notify(); + } + + /** + * Provides the value the spy was last notified with. + * + * @note The value is only valid if wait() returned with true. + * @returns The value given to notify(), or boost::none if the spy wasn't notified + */ + T value() const { return *_value; } + +private: + boost::optional _value; +}; + +} // namespace test