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"
25 #include <boost/variant.hpp>
27 #include "internalpeer.h"
28 #include "testglobal.h"
32 /// Variant-based wrapper struct for supported protocol messages
33 struct ProtocolMessage;
35 TEST_UTIL_EXPORT void PrintTo(const ProtocolMessage&, std::ostream*);
38 * Mocked peer that can be used in test cases involving SignalProxy.
40 * This mock class is intended to be used for testing on the abstraction level of Quassel protocol messages; it does not support
41 * mocking e.g. serialization or network connections, or other lower-level implementation details of specific peer types.
43 * To set expectations, use EXPECT_CALL on the mocked Dispatches() method and the provided protocol message matchers like this:
46 * EXPECT_CALL(*peer, Dispatches(RpcCall(QByteArray{SIGNAL(sendData(int,QString))}, ElementsAre(42, "Hello"))));
49 * The matchers take matchers themselves for their individual arguments, so you can do things like
52 * EXPECT_CALL(*peer, Dispatches(RpcCall(_, Contains(42))));
55 * @note For now, MockedPeer only supports SignalProxy message types. It can be extended in the future to support auth messages,
58 class TEST_UTIL_EXPORT MockedPeer : public InternalPeer
63 MockedPeer(QObject* parent = nullptr);
64 ~MockedPeer() override;
66 /// Every message dispatched goes through this mocked method, which can be used with the protocol message machters to define expectations
67 MOCK_METHOD1(Dispatches, void(const ProtocolMessage&));
69 using InternalPeer::dispatch;
70 void dispatch(const Protocol::SyncMessage&) override;
71 void dispatch(const Protocol::RpcCall&) override;
72 void dispatch(const Protocol::InitRequest&) override;
73 void dispatch(const Protocol::InitData&) override;
76 void dispatchInternal(const ProtocolMessage&);
79 void realDispatch(const T&);
81 friend struct DispatchVisitor;
84 // ---- Matchers for defining protocol message expectations for Dispatches() ---------------------------------------------------------------
86 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> SyncMessage(
87 ::testing::Matcher<QByteArray> className,
88 ::testing::Matcher<QString> objectName,
89 ::testing::Matcher<QByteArray> slotName,
90 ::testing::Matcher<QVariantList> params);
92 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> RpcCall(
93 ::testing::Matcher<QByteArray> slotName,
94 ::testing::Matcher<QVariantList> params);
96 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> InitRequest(
97 ::testing::Matcher<QByteArray> className,
98 ::testing::Matcher<QString> objectName);
100 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> InitData(
101 ::testing::Matcher<QByteArray> className,
102 ::testing::Matcher<QString> objectName,
103 ::testing::Matcher<QVariantMap> initData);