1 /********************************************************************
3 * Copyright (C) 2008 Davide Pesavento
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
25 #include "ClientSocketTest.h"
27 void ClientSocketTest::initTestCase()
29 qRegisterMetaType
< QList
<Move
> >();
31 _listener
= new QTcpServer(this);
32 QVERIFY(_listener
->listen(QHostAddress::LocalHost
, 42000));
33 QVERIFY(!_listener
->hasPendingConnections());
36 void ClientSocketTest::cleanupTestCase()
42 void ClientSocketTest::init()
45 QTcpSocket
*s
= new QTcpSocket();
46 _testedObject
= new ClientSocket(s
);
47 s
->connectToHost(QHostAddress::LocalHost
, 42000);
48 QVERIFY(_listener
->waitForNewConnection(10000));
49 QVERIFY(_listener
->hasPendingConnections());
50 _serverStub
= _listener
->nextPendingConnection();
51 _joinAcceptedSpy
= new QSignalSpy(_testedObject
, SIGNAL(joinAccepted(int)));
52 _joinRefusedSpy
= new QSignalSpy(_testedObject
, SIGNAL(joinRefused(QString
)));
53 _receivedGameSettingsSpy
= new QSignalSpy(_testedObject
, SIGNAL(receivedGameSettings(int, int, int, int, bool)));
54 _receivedHistorySpy
= new QSignalSpy(_testedObject
, SIGNAL(receivedHistory(QList
<Move
>)));
57 QCOMPARE(_listener
->hasPendingConnections(), false);
58 QCOMPARE(_serverStub
->state(), QAbstractSocket::ConnectedState
);
59 QCOMPARE(_testedObject
->_socket
, s
);
60 QCOMPARE(_testedObject
->_buffer
.isEmpty(), true);
61 QCOMPARE(_joinAcceptedSpy
->isValid(), true);
62 QCOMPARE(_joinAcceptedSpy
->count(), 0);
63 QCOMPARE(_joinRefusedSpy
->isValid(), true);
64 QCOMPARE(_joinRefusedSpy
->count(), 0);
65 QCOMPARE(_receivedGameSettingsSpy
->isValid(), true);
66 QCOMPARE(_receivedGameSettingsSpy
->count(), 0);
67 QCOMPARE(_receivedHistorySpy
->isValid(), true);
68 QCOMPARE(_receivedHistorySpy
->count(), 0);
71 void ClientSocketTest::cleanup()
73 delete _joinAcceptedSpy
;
74 delete _joinRefusedSpy
;
75 delete _receivedGameSettingsSpy
;
76 delete _receivedHistorySpy
;
81 void ClientSocketTest::joinGame()
83 QFETCH(QString
, mode
);
84 QFETCH(QString
, name
);
85 QFETCH(QString
, output
);
88 _testedObject
->changeState(StreamSocket::Idle
);
89 _testedObject
->joinGame(mode
, name
);
90 _testedObject
->_socket
->waitForBytesWritten(10000);
93 QCOMPARE(_serverStub
->waitForReadyRead(10000), true);
94 QCOMPARE(QString(_serverStub
->read(output
.size())), output
);
97 void ClientSocketTest::joinGame_data()
99 QTest::addColumn
<QString
>("mode");
100 QTest::addColumn
<QString
>("name");
101 QTest::addColumn
<QString
>("output");
103 QTest::newRow("Join1") << "player" << "Pippo" << "<joinRequest gameMode=\"player\">Pippo</joinRequest>";
104 QTest::newRow("Join2") << "spectator" << "Pluto" << "<joinRequest gameMode=\"spectator\">Pluto</joinRequest>";
107 void ClientSocketTest::cancelJoin()
110 QString
output("<playerLeft>-1</playerLeft>");
111 _testedObject
->changeState(StreamSocket::AwaitingGameStart
);
112 _testedObject
->cancelJoin();
113 _testedObject
->_socket
->waitForBytesWritten(10000);
116 QCOMPARE(_serverStub
->waitForReadyRead(10000), true);
117 QCOMPARE(QString(_serverStub
->read(output
.size())), output
);
120 void ClientSocketTest::joinAccepted()
123 QByteArray
input("<joinACK>1</joinACK>");
125 _testedObject
->changeState(StreamSocket::AwaitingJoinAnswer
);
126 _serverStub
->write(input
);
130 QCOMPARE(_testedObject
->state(), StreamSocket::AwaitingGameStart
);
131 QCOMPARE(_joinAcceptedSpy
->count(), 1);
132 QCOMPARE(_joinAcceptedSpy
->at(0).at(0).type(), QVariant::Int
);
133 QCOMPARE(_joinAcceptedSpy
->at(0).at(0).toInt(), id
);
136 void ClientSocketTest::joinRefused()
139 QByteArray
input("<joinNAK>2</joinNAK>");
140 QString
errorMessage("The name you have chosen is already\nbeing used by someone else.");
141 _testedObject
->changeState(StreamSocket::AwaitingJoinAnswer
);
142 _serverStub
->write(input
);
146 QCOMPARE(_testedObject
->state(), StreamSocket::Idle
);
147 QCOMPARE(_joinRefusedSpy
->count(), 1);
148 QCOMPARE(_joinRefusedSpy
->at(0).at(0).type(), QVariant::String
);
149 QCOMPARE(_joinRefusedSpy
->at(0).at(0).toString(), errorMessage
);
152 void ClientSocketTest::receivedGameSettings()
155 QByteArray
input("<settings playing=\"false\"><difficultyOne>5</difficultyOne><difficultyTwo>2</difficultyTwo><numberOfPlayers>3</numberOfPlayers><timerDuration>60</timerDuration></settings>");
156 _testedObject
->changeState(StreamSocket::FullyOpened
);
157 _serverStub
->write(input
);
161 QCOMPARE(_testedObject
->state(), StreamSocket::Idle
);
162 QCOMPARE(_receivedGameSettingsSpy
->count(), 1);
163 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(0).type(), QVariant::Int
);
164 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(0).toInt(), 5);
165 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(1).type(), QVariant::Int
);
166 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(1).toInt(), 2);
167 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(2).type(), QVariant::Int
);
168 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(2).toInt(), 3);
169 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(3).type(), QVariant::Int
);
170 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(3).toInt(), 60);
171 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(4).type(), QVariant::Bool
);
172 QCOMPARE(_receivedGameSettingsSpy
->at(0).at(4).toBool(), false);
175 void ClientSocketTest::receivedHistory()
178 QByteArray
input("<history><move><player>0</player><point><x>2</x><y>3</y><z>6</z></point></move><move><player>1</player><point><x>0</x><y>5</y><z>1</z></point></move></history>");
179 Move
m0(0, Point(2, 3, 6));
180 Move
m1(1, Point(0, 5, 1));
181 _testedObject
->changeState(StreamSocket::AwaitingGameStart
);
182 _serverStub
->write(input
);
186 QCOMPARE(_receivedHistorySpy
->count(), 1);
187 QList
<Move
> moves
= _receivedHistorySpy
->at(0).at(0).value
< QList
<Move
> >();
188 QCOMPARE(moves
.at(0), m0
);
189 QCOMPARE(moves
.at(1), m1
);
192 QTEST_MAIN(ClientSocketTest
)