1 /*
2  * Copyright (C) 2008-2020 The Communi Project
3  *
4  * This test is free, and not covered by the BSD license. There is no
5  * restriction applied to their modification, redistribution, using and so on.
6  * You can study them, modify them, use them in your own program - either
7  * completely or partially.
8  */
9 
10 #include "ircbuffer.h"
11 #include "ircbuffermodel.h"
12 #include "ircconnection.h"
13 #include "irccommand.h"
14 #include "ircmessage.h"
15 #include "ircfilter.h"
16 #include <QtTest/QtTest>
17 
18 class tst_IrcBuffer : public QObject
19 {
20     Q_OBJECT
21 
22 private slots:
23     void testDefaults();
24     void testTitleNamePrefix();
25     void testSticky();
26     void testPersistent();
27     void testReceive();
28     void testDebug();
29     void testUserData();
30     void testClose();
31     void testSendCommand();
32 };
33 
testDefaults()34 void tst_IrcBuffer::testDefaults()
35 {
36     IrcBuffer buffer;
37     QVERIFY(buffer.title().isEmpty());
38     QVERIFY(buffer.name().isEmpty());
39     QVERIFY(buffer.prefix().isEmpty());
40     QVERIFY(!buffer.isChannel());
41     QVERIFY(!buffer.toChannel());
42     QVERIFY(!buffer.connection());
43     QVERIFY(!buffer.network());
44     QVERIFY(!buffer.model());
45     QVERIFY(!buffer.isActive());
46     QVERIFY(!buffer.isSticky());
47     QVERIFY(!buffer.isPersistent());
48     QVERIFY(buffer.userData().isEmpty());
49 }
50 
testTitleNamePrefix()51 void tst_IrcBuffer::testTitleNamePrefix()
52 {
53     IrcBuffer buffer;
54 
55     QSignalSpy titleSpy(&buffer, SIGNAL(titleChanged(QString)));
56     QSignalSpy nameSpy(&buffer, SIGNAL(nameChanged(QString)));
57     QSignalSpy prefixSpy(&buffer, SIGNAL(prefixChanged(QString)));
58     QVERIFY(titleSpy.isValid());
59     QVERIFY(nameSpy.isValid());
60     QVERIFY(prefixSpy.isValid());
61 
62     buffer.setName("name");
63     QCOMPARE(buffer.title(), QString("name"));
64     QCOMPARE(buffer.name(), QString("name"));
65     QCOMPARE(buffer.prefix(), QString());
66     QCOMPARE(titleSpy.count(), 1);
67     QCOMPARE(titleSpy.last().first().toString(), QString("name"));
68     QCOMPARE(nameSpy.count(), 1);
69     QCOMPARE(nameSpy.last().first().toString(), QString("name"));
70     QCOMPARE(prefixSpy.count(), 0);
71 
72     buffer.setPrefix("prefix");
73     QCOMPARE(buffer.title(), QString("prefixname"));
74     QCOMPARE(buffer.name(), QString("name"));
75     QCOMPARE(buffer.prefix(), QString("prefix"));
76     QCOMPARE(titleSpy.count(), 2);
77     QCOMPARE(titleSpy.last().first().toString(), QString("prefixname"));
78     QCOMPARE(nameSpy.count(), 1);
79     QCOMPARE(prefixSpy.count(), 1);
80     QCOMPARE(prefixSpy.last().first().toString(), QString("prefix"));
81 }
82 
testSticky()83 void tst_IrcBuffer::testSticky()
84 {
85     IrcBuffer buffer;
86     QVERIFY(!buffer.isSticky());
87 
88     QSignalSpy spy(&buffer, SIGNAL(stickyChanged(bool)));
89     QVERIFY(spy.isValid());
90 
91     buffer.setSticky(true);
92     QVERIFY(buffer.isSticky());
93     QCOMPARE(spy.count(), 1);
94     QVERIFY(spy.last().last().toBool());
95 
96     buffer.setSticky(false);
97     QVERIFY(!buffer.isSticky());
98     QCOMPARE(spy.count(), 2);
99     QVERIFY(!spy.last().last().toBool());
100 }
101 
testPersistent()102 void tst_IrcBuffer::testPersistent()
103 {
104     IrcBuffer buffer;
105     QVERIFY(!buffer.isPersistent());
106 
107     QSignalSpy spy(&buffer, SIGNAL(persistentChanged(bool)));
108     QVERIFY(spy.isValid());
109 
110     buffer.setPersistent(true);
111     QVERIFY(buffer.isPersistent());
112     QCOMPARE(spy.count(), 1);
113     QVERIFY(spy.last().last().toBool());
114 
115     buffer.setPersistent(false);
116     QVERIFY(!buffer.isPersistent());
117     QCOMPARE(spy.count(), 2);
118     QVERIFY(!spy.last().last().toBool());
119 }
120 
testReceive()121 void tst_IrcBuffer::testReceive()
122 {
123     Irc::registerMetaTypes();
124 
125     IrcBuffer buffer;
126 
127     QSignalSpy spy(&buffer, SIGNAL(messageReceived(IrcMessage*)));
128     QVERIFY(spy.isValid());
129 
130     buffer.receiveMessage(nullptr);
131     QCOMPARE(spy.count(), 0);
132 
133     IrcMessage msg(nullptr);
134     buffer.receiveMessage(&msg);
135     QCOMPARE(spy.count(), 1);
136     QCOMPARE(spy.last().at(0).value<IrcMessage*>(), &msg);
137 }
138 
testDebug()139 void tst_IrcBuffer::testDebug()
140 {
141     QString str;
142     QDebug dbg(&str);
143 
144     dbg << static_cast<IrcBuffer*>(nullptr);
145     QCOMPARE(str.trimmed(), QString::fromLatin1("IrcBuffer(0x0)"));
146     str.clear();
147 
148     IrcBuffer buffer;
149     dbg << &buffer;
150     QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+\\) ").match(str).hasMatch());
151     str.clear();
152 
153     buffer.setObjectName("obj");
154     dbg << &buffer;
155     QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj\\) ").match(str).hasMatch());
156     str.clear();
157 
158     buffer.setName("buf");
159     dbg << &buffer;
160     QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj, title=buf\\) ").match(str).hasMatch());
161     str.clear();
162 }
163 
testUserData()164 void tst_IrcBuffer::testUserData()
165 {
166     QVariantMap ud;
167     ud.insert("foo", "bar");
168 
169     IrcBuffer buffer;
170     buffer.setUserData(ud);
171     QCOMPARE(buffer.userData(), ud);
172 
173     buffer.setUserData(QVariantMap());
174     QVERIFY(buffer.userData().isEmpty());
175 }
176 
testClose()177 void tst_IrcBuffer::testClose()
178 {
179     IrcBufferModel model;
180     QPointer<IrcBuffer> buffer = model.add("foo");
181     buffer->close();
182     QVERIFY(!model.contains("foo"));
183     QVERIFY(!buffer);
184 }
185 
186 class TestCommandFilter : public QObject, public IrcCommandFilter
187 {
188     Q_OBJECT
189     Q_INTERFACES(IrcCommandFilter)
190 
191 public:
TestCommandFilter(IrcConnection * connection)192     TestCommandFilter(IrcConnection* connection) : lastCommand(nullptr) { connection->installCommandFilter(this); }
commandFilter(IrcCommand * command)193     bool commandFilter(IrcCommand *command) override { lastCommand = command; return true; }
194     IrcCommand* lastCommand;
195 };
196 
testSendCommand()197 void tst_IrcBuffer::testSendCommand()
198 {
199     IrcConnection connection;
200     TestCommandFilter filter(&connection);
201 
202     IrcBufferModel model(&connection);
203     QCOMPARE(model.connection(), &connection);
204 
205     IrcBuffer* buffer = model.add("foo");
206     QCOMPARE(buffer->connection(), &connection);
207 
208     IrcCommand* cmd = IrcCommand::createAway();
209     QVERIFY(!buffer->sendCommand(cmd));
210     QCOMPARE(filter.lastCommand, cmd);
211 }
212 
213 QTEST_MAIN(tst_IrcBuffer)
214 
215 #include "tst_ircbuffer.moc"
216