first commit

This commit is contained in:
2025-07-06 00:23:46 +02:00
commit 38f50c8819
1788 changed files with 112878 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
using Nitrox.Test.Client.Communication;
using NitroxModel.Packets;
namespace NitroxClient.Communication;
[TestClass]
public class DeferredPacketReceiverTest
{
[TestMethod]
public void NonActionPacket()
{
// Arrange
const ushort PLAYER_ID = 1;
TestNonActionPacket packet = new(PLAYER_ID);
PacketReceiver packetReceiver = new();
// Act
packetReceiver.Add(packet);
Packet storedPacket = packetReceiver.GetNextPacket();
// Assert
storedPacket.Should().NotBeNull();
packetReceiver.GetNextPacket().Should().BeNull();
storedPacket.Should().Be(packet);
packet.PlayerId.Should().Be(PLAYER_ID);
}
}

View File

@@ -0,0 +1,91 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NitroxClient.Communication.Abstract;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class SessionJoinedStateTests
{
[TestMethod]
public void NegotiateShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
SessionJoined connectionState = new SessionJoined();
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationExcepion()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
SessionJoined connectionState = new SessionJoined();
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionJoined connectionState = new SessionJoined();
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionJoined connectionState = new SessionJoined();
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionJoined connection = new SessionJoined();
// Act
connection.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NitroxClient.Communication.Abstract;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class SessionReservationRejectedStateTests
{
[TestMethod]
public void NegotiateShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
SessionReservationRejected connectionState = new SessionReservationRejected();
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationExcepion()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
SessionReservationRejected connectionState = new SessionReservationRejected();
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReservationRejected connectionState = new SessionReservationRejected();
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReservationRejected connectionState = new SessionReservationRejected();
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReservationRejected connection = new SessionReservationRejected();
// Act
connection.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,129 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.Packets;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class SessionReservedStateTests
{
[TestMethod]
public void NegotiateShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
SessionReserved connectionState = new SessionReserved();
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldSendPlayerJoiningMultiplayerSessionPacket()
{
// Arrange
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
TestConstants.TEST_CORRELATION_ID,
TestConstants.TEST_PLAYER_ID,
TestConstants.TEST_RESERVATION_KEY);
IClient client = Substitute.For<IClient>();
client.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns(successfulReservation);
connectionContext.Client.Returns(client);
SessionReserved connectionState = new SessionReserved();
// Act
connectionState.JoinSession(connectionContext);
// Assert
client.Received().Send(Arg.Any<PlayerJoiningMultiplayerSession>());
}
[TestMethod]
public void JoinSessionShouldTransitionToSessionJoinedState()
{
// Arrange
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
TestConstants.TEST_CORRELATION_ID,
TestConstants.TEST_PLAYER_ID,
TestConstants.TEST_RESERVATION_KEY);
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns(successfulReservation);
connectionContext.Client.Returns(serverClient);
SessionReserved connection = new SessionReserved();
// Act
connection.JoinSession(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<SessionJoined>());
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReserved connectionState = new SessionReserved();
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReserved connectionState = new SessionReserved();
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
SessionReserved connection = new SessionReserved();
// Act
connection.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,162 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class AwaitingReservationCredentialsStateTests
{
[TestMethod]
public void NegotiateShouldSendServerAuthorityReservationRequest()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
connectionContext.PlayerSettings.Returns(TestConstants.TEST_PLAYER_SETTINGS);
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
serverClient.Received().Send(Arg.Any<MultiplayerSessionReservationRequest>());
}
[TestMethod]
public void NegotiateShouldTransitionToAwaitingSessionReservationState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
connectionContext.PlayerSettings.Returns(TestConstants.TEST_PLAYER_SETTINGS);
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingSessionReservation>());
}
[TestMethod]
public void NegotiateShouldThrowInvalidOperationExceptionWhenPlayerSettingsIsNull()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.PlayerSettings.Returns((PlayerSettings)null);
connectionContext.AuthenticationContext.Returns(TestConstants.TEST_AUTHENTICATION_CONTEXT);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void NegotiateShouldThrowInvalidOperationExceptionWhenAuthenticationContextIsNull()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(true);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.SessionPolicy.Returns(TestConstants.TEST_SESSION_POLICY);
connectionContext.AuthenticationContext.Returns((AuthenticationContext)null);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials();
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,156 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.Packets;
using NitroxModel.Packets.Exceptions;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class AwaitingSessionReservationStateTests
{
[TestMethod]
public void NegotiateShouldTransitionToSessionRevervedAfterReceivingSuccessfulReservation()
{
// Arrange
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
TestConstants.TEST_CORRELATION_ID,
TestConstants.TEST_PLAYER_ID,
TestConstants.TEST_RESERVATION_KEY);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns(successfulReservation);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<SessionReserved>());
}
[TestMethod]
public void NegotiateShouldThrowUncorrelatedPacketExceptionWhenTheReservationHasTheWrongCorrelationId()
{
// Arrange
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
"wrong",
TestConstants.TEST_PLAYER_ID,
TestConstants.TEST_RESERVATION_KEY);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns(successfulReservation);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<UncorrelatedPacketException>();
}
[TestMethod]
public void NegotiateShouldTransitionToSessionReservationRejectedAfterReceivingRejectedReservation()
{
// Arrange
MultiplayerSessionReservation rejectedReservation = new MultiplayerSessionReservation(TestConstants.TEST_CORRELATION_ID, TestConstants.TEST_REJECTION_STATE);
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns(rejectedReservation);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<SessionReservationRejected>());
}
[TestMethod]
public void NegotiateShouldThrowInvalidOperationExceptionWhenTheReservationIsNull()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Reservation.Returns((MultiplayerSessionReservation)null);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,144 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.Packets;
using NitroxModel.Packets.Exceptions;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class EstablishingSessionPolicyStateTests
{
[TestMethod]
public void NegotiateShouldTransitionToAwaitingReservationCredentialsState()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy(TestConstants.TEST_CORRELATION_ID, false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, false));
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingReservationCredentials>());
}
[TestMethod]
public void NegotiateShouldTransitionToAwaitingReservationCredentialsStateWithPassword()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy(TestConstants.TEST_CORRELATION_ID, false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, true));
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<AwaitingReservationCredentials>());
}
[TestMethod]
public void NegotiateShouldThrowUncorrelatedPacketExceptionWhenThePolicyHasTheWrongCorrelationId()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.SessionPolicy.Returns(new MultiplayerSessionPolicy("wrong", false, TestConstants.TEST_MAX_PLAYER_CONNECTIONS, false));
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<UncorrelatedPacketException>();
}
[TestMethod]
public void NegotiateShouldThrowInvalidOperationExceptionIfTheSessionPolicyIsNull()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.SessionPolicy.Returns((MultiplayerSessionPolicy)null);
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.NegotiateReservationAsync(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldStopTheClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
serverClient.Received().Stop();
}
[TestMethod]
public void DisconnectShouldResetTheConnectionContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().ClearSessionState();
}
[TestMethod]
public void DisconnectShouldTransitionToDisconnectedState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
EstablishingSessionPolicy connectionState = new EstablishingSessionPolicy(TestConstants.TEST_CORRELATION_ID);
// Act
connectionState.Disconnect(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<Disconnected>());
}
}
}

View File

@@ -0,0 +1,147 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.Packets;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession.ConnectionState
{
[TestClass]
public class DisconnectedStateTests
{
[TestMethod]
public void NegotiateShouldStartTheClientOnTheContext()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(false);
serverClient
.When(client => client.StartAsync(Arg.Any<string>(), TestConstants.TEST_SERVER_PORT))
.Do(info => serverClient.IsConnected.Returns(true));
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
connectionContext.ServerPort.Returns(TestConstants.TEST_SERVER_PORT);
Disconnected connectionState = new Disconnected();
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
serverClient.IsConnected.Should().BeTrue();
}
[TestMethod]
public void NegotiateShouldSendMultiplayerSessionPolicyRequestPacketToClient()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(false);
serverClient
.When(client => client.StartAsync(Arg.Any<string>(), TestConstants.TEST_SERVER_PORT))
.Do(info => serverClient.IsConnected.Returns(true));
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
connectionContext.ServerPort.Returns(TestConstants.TEST_SERVER_PORT);
Disconnected connectionState = new Disconnected();
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
serverClient.Received().Send(Arg.Any<MultiplayerSessionPolicyRequest>());
}
[TestMethod]
public void NegotiateShouldTransitionToEstablishingSessionPolicyState()
{
// Arrange
IClient serverClient = Substitute.For<IClient>();
serverClient.IsConnected.Returns(false);
serverClient
.When(client => client.StartAsync(Arg.Any<string>(), TestConstants.TEST_SERVER_PORT))
.Do(info => serverClient.IsConnected.Returns(true));
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(serverClient);
connectionContext.ServerPort.Returns(TestConstants.TEST_SERVER_PORT);
Disconnected connectionState = new Disconnected();
// Act
connectionState.NegotiateReservationAsync(connectionContext);
// Assert
connectionContext.Received().UpdateConnectionState(Arg.Any<EstablishingSessionPolicy>());
}
[TestMethod]
public async Task NegotiateShouldThrowInvalidOperationExceptionWhenClientIsNull()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns((IClient)null);
connectionContext.IpAddress.Returns(TestConstants.TEST_IP_ADDRESS);
Disconnected connectionState = new Disconnected();
// Act
Func<Task> action = async () => await connectionState.NegotiateReservationAsync(connectionContext);
// Assert
await action.Should().ThrowAsync<InvalidOperationException>();
}
[TestMethod]
public async Task NegotiateShouldThrowInvalidOperationExceptionWhenIpAddressIsNull()
{
// Arrange
IClient client = Substitute.For<IClient>();
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
connectionContext.Client.Returns(client);
connectionContext.IpAddress.Returns((string)null);
Disconnected connectionState = new Disconnected();
// Act
Func<Task> action = async () => await connectionState.NegotiateReservationAsync(connectionContext);
// Assert
await action.Should().ThrowAsync<InvalidOperationException>();
}
[TestMethod]
public void JoinSessionShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
Disconnected connectionState = new Disconnected();
// Act
Action action = () => connectionState.JoinSession(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void DisconnectShouldThrowInvalidOperationException()
{
// Arrange
IMultiplayerSessionConnectionContext connectionContext = Substitute.For<IMultiplayerSessionConnectionContext>();
Disconnected connectionState = new Disconnected();
// Act
Action action = () => connectionState.Disconnect(connectionContext);
// Assert
action.Should().Throw<InvalidOperationException>();
}
}
}

View File

@@ -0,0 +1,119 @@
using FluentAssertions;
using FluentAssertions.Events;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxClient.Communication.Abstract;
using NitroxModel.Packets;
using NSubstitute;
namespace NitroxClient.Communication.MultiplayerSession
{
[TestClass]
public class MultiplayerSessionMangerTests
{
[TestMethod]
public void ManagerShouldInitializeInDisconnectedStage()
{
// Arrange
IClient client = Substitute.For<IClient>();
// Act
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client);
// Assert
multiplayerSession.CurrentState.CurrentStage.Should().Be(MultiplayerSessionConnectionStage.DISCONNECTED);
}
[TestMethod]
public void ManagerShouldInitializeWithClient()
{
// Arrange
IClient client = Substitute.For<IClient>();
// Act
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client);
// Assert
multiplayerSession.Client.Should().Be(client);
}
[TestMethod]
public void ConnectShouldSetIpAddress()
{
// Arrange
IClient client = Substitute.For<IClient>();
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client, TestConstants.TEST_CONNECTION_STATE);
// Act
multiplayerSession.ConnectAsync(TestConstants.TEST_IP_ADDRESS, TestConstants.TEST_SERVER_PORT);
// Assert
multiplayerSession.IpAddress.Should().Be(TestConstants.TEST_IP_ADDRESS);
multiplayerSession.ServerPort.Should().Be(TestConstants.TEST_SERVER_PORT);
}
[TestMethod]
public void ProcessSessionPolicyShouldSetThePolicy()
{
// Arrange
IClient client = Substitute.For<IClient>();
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client, TestConstants.TEST_CONNECTION_STATE);
// Act
multiplayerSession.ProcessSessionPolicy(TestConstants.TEST_SESSION_POLICY);
// Assert
multiplayerSession.SessionPolicy.Should().Be(TestConstants.TEST_SESSION_POLICY);
}
[TestMethod]
public void RequestSessionReservationShouldSetSettingsAndAuthContext()
{
// Arrange
IClient client = Substitute.For<IClient>();
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client, TestConstants.TEST_CONNECTION_STATE);
// Act
multiplayerSession.RequestSessionReservation(TestConstants.TEST_PLAYER_SETTINGS, TestConstants.TEST_AUTHENTICATION_CONTEXT);
// Assert
multiplayerSession.PlayerSettings.Should().Be(TestConstants.TEST_PLAYER_SETTINGS);
multiplayerSession.AuthenticationContext.Should().Be(TestConstants.TEST_AUTHENTICATION_CONTEXT);
}
[TestMethod]
public void ProcessReservationResponsePacketShouldSetTheReservation()
{
// Arrange
MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation(
TestConstants.TEST_CORRELATION_ID,
TestConstants.TEST_PLAYER_ID,
TestConstants.TEST_RESERVATION_KEY);
IClient client = Substitute.For<IClient>();
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client, TestConstants.TEST_CONNECTION_STATE);
// Act
multiplayerSession.ProcessReservationResponsePacket(successfulReservation);
// Assert
multiplayerSession.Reservation.Should().Be(successfulReservation);
}
[TestMethod]
public void UpdateStateShouldRaiseEvent()
{
// Arrange
IClient client = Substitute.For<IClient>();
IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client);
IMultiplayerSessionConnectionContext connectionContext = (IMultiplayerSessionConnectionContext)multiplayerSession;
IMonitor<IMultiplayerSession> monitor = multiplayerSession.Monitor();
// Act
connectionContext.UpdateConnectionState(TestConstants.TEST_CONNECTION_STATE);
// Assert
monitor.Should().Raise("ConnectionStateChanged");
}
}
}

View File

@@ -0,0 +1,24 @@
using NitroxClient.Communication.Abstract;
using NitroxModel.DataStructures.Util;
using NitroxModel.MultiplayerSession;
using NitroxModel.Packets;
using NSubstitute;
namespace Nitrox.Test.Client.Communication.MultiplayerSession
{
internal static class TestConstants
{
public const string TEST_IP_ADDRESS = "#.#.#.#";
public const int TEST_SERVER_PORT = 11000;
public const ushort TEST_PLAYER_ID = 1;
public const string TEST_PLAYER_NAME = "TEST";
public const string TEST_RESERVATION_KEY = "@#*(&";
public const string TEST_CORRELATION_ID = "CORRELATED";
public const int TEST_MAX_PLAYER_CONNECTIONS = 100;
public const MultiplayerSessionReservationState TEST_REJECTION_STATE = MultiplayerSessionReservationState.REJECTED | MultiplayerSessionReservationState.UNIQUE_PLAYER_NAME_CONSTRAINT_VIOLATED;
public static readonly AuthenticationContext TEST_AUTHENTICATION_CONTEXT = new AuthenticationContext(TEST_PLAYER_NAME, Optional.Empty);
public static readonly MultiplayerSessionPolicy TEST_SESSION_POLICY = new MultiplayerSessionPolicy(TEST_CORRELATION_ID, false, TEST_MAX_PLAYER_CONNECTIONS, false);
public static readonly PlayerSettings TEST_PLAYER_SETTINGS = new PlayerSettings(RandomColorGenerator.GenerateColor());
public static readonly IMultiplayerSessionConnectionState TEST_CONNECTION_STATE = Substitute.For<IMultiplayerSessionConnectionState>();
}
}

View File

@@ -0,0 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NitroxModel.Packets;
namespace NitroxClient.Communication;
[TestClass]
public class PacketSuppressorTest
{
[TestMethod]
public void SingleSuppress()
{
Assert.IsFalse(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
using (PacketSuppressor<BedEnter>.Suppress())
{
Assert.IsTrue(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
}
Assert.IsFalse(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
}
[TestMethod]
public void MultipleSuppress()
{
Assert.IsFalse(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODEventInstancePacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODCustomEmitterPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODCustomLoopingEmitterPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODStudioEmitterPacket>.IsSuppressed);
using (PacketSuppressor<FMODAssetPacket, FMODEventInstancePacket, FMODCustomEmitterPacket, FMODCustomLoopingEmitterPacket, FMODStudioEmitterPacket>.Suppress())
{
Assert.IsFalse(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsTrue(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
Assert.IsTrue(PacketSuppressor<FMODEventInstancePacket>.IsSuppressed);
Assert.IsTrue(PacketSuppressor<FMODCustomEmitterPacket>.IsSuppressed);
Assert.IsTrue(PacketSuppressor<FMODCustomLoopingEmitterPacket>.IsSuppressed);
Assert.IsTrue(PacketSuppressor<FMODStudioEmitterPacket>.IsSuppressed);
}
Assert.IsFalse(PacketSuppressor<BedEnter>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODAssetPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODEventInstancePacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODCustomEmitterPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODCustomLoopingEmitterPacket>.IsSuppressed);
Assert.IsFalse(PacketSuppressor<FMODStudioEmitterPacket>.IsSuppressed);
}
}

View File

@@ -0,0 +1,16 @@
using System;
using NitroxModel.Packets;
namespace Nitrox.Test.Client.Communication
{
[Serializable]
public class TestNonActionPacket : Packet
{
public ushort PlayerId { get; }
public TestNonActionPacket(ushort playerId)
{
PlayerId = playerId;
}
}
}

View File

@@ -0,0 +1,85 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NitroxModel.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NitroxClient.GameLogic.Helper;
[TestClass]
public class BaseSerializationHelperTest
{
[TestMethod]
public void TestBytesRestoring()
{
List<int> lengths = new() { 10000, 100000, 300000, 500000 };
foreach (int length in lengths)
{
TestSerialization(GenerateRealisticBytes(length));
}
}
[TestMethod]
public void TestAllZeroBytes()
{
List<int> lengths = new() { 10000, 100000, 300000, 500000 };
foreach (int length in lengths)
{
byte[] data = new byte[length];
TestSerialization(data);
}
}
[TestMethod]
public void TestAllMaxBytes()
{
List<int> lengths = new() { 10000, 100000, 300000, 500000 };
foreach (int length in lengths)
{
byte[] data = new byte[length];
data.AsSpan().Fill(byte.MaxValue);
TestSerialization(data);
}
}
/// <summary>
/// Generates bytes to emulate Base data arrays
/// </summary>
private static byte[] GenerateRealisticBytes(int length)
{
byte[] generated = new byte[length];
byte[] randomBytes = new byte[length];
int randomIndex = 0;
Random random = new();
random.NextBytes(randomBytes);
for (int i = 0; i < length; i++)
{
if (random.Next(100) < 3)
{
generated[i] = randomBytes[randomIndex++];
}
else
{
generated[i] = 0;
}
}
return generated;
}
private static void TestSerialization(byte[] original)
{
byte[] compressed = BaseSerializationHelper.CompressBytes(original);
byte[] decompressed = BaseSerializationHelper.DecompressBytes(compressed, original.Length);
Log.Info($"Size: [Original: {original.Length}, Compressed: {compressed.Length}, Decompressed: {decompressed.Length}]");
Log.Info($"Original: {string.Join(", ", original.Take(100))}");
Log.Info($"Compressed: {string.Join(", ", compressed.Take(100))}");
Log.Info($"Decompressed: {string.Join(", ", decompressed.Take(100))}\n");
Assert.AreEqual(original.Length, decompressed.Length);
for (int i = 0; i < original.Length; i++)
{
Assert.AreEqual(original[i], decompressed[i]);
}
}
}

View File

@@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nitrox.Test.Client.Communication.MultiplayerSession;
using NitroxModel.MultiplayerSession;
using NitroxModel_Subnautica.DataStructures;
using NSubstitute;
using UnityEngine;
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
[TestClass]
public class PlayerPreferenceManagerTests
{
[TestMethod]
public void ShouldBeAbleToRetrieveANewPreferenceEntry()
{
//Given
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
PlayerPreference playerPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
Color preferredColor = playerPreference.PreferredColor();
//When
playerPreferenceManager.SetPreference(TestConstants.TEST_IP_ADDRESS, playerPreference);
PlayerPreference result = playerPreferenceManager.GetPreference(TestConstants.TEST_IP_ADDRESS);
//Then
result.PlayerName.Should().Be(TestConstants.TEST_PLAYER_NAME);
result.RedAdditive.Should().Be(preferredColor.r);
result.GreenAdditive.Should().Be(preferredColor.g);
result.BlueAdditive.Should().Be(preferredColor.b);
}
[TestMethod]
public void ShouldBeAbleToRetrieveUpdatedPreferencesForAnExistingIpAddress()
{
//Given
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
PlayerPreference playerPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
Color newColor = RandomColorGenerator.GenerateColor().ToUnity();
PlayerPreference newPlayerPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, newColor);
//When
playerPreferenceManager.SetPreference(TestConstants.TEST_IP_ADDRESS, playerPreference);
playerPreferenceManager.SetPreference(TestConstants.TEST_IP_ADDRESS, newPlayerPreference);
PlayerPreference result = playerPreferenceManager.GetPreference(TestConstants.TEST_IP_ADDRESS);
//Then
result.RedAdditive.Should().Be(newColor.r);
result.GreenAdditive.Should().Be(newColor.g);
result.BlueAdditive.Should().Be(newColor.b);
}
[TestMethod]
public void SetPreferenceShouldThrowExceptionWhenGivenANullIpAddress()
{
//Arrange
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
PlayerPreference playerPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
//Act
Action action = () => playerPreferenceManager.SetPreference(null, playerPreference);
//Assert
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void SetPreferenceShouldThrowExceptionWhenGivenANullJoinServerSettingsReference()
{
//Arrange
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
//Act
Action action = () => playerPreferenceManager.SetPreference(TestConstants.TEST_IP_ADDRESS, null);
//Assert
action.Should().Throw<ArgumentNullException>();
}
[TestMethod]
public void ShouldGetTheLastSetPlayerPreferenceWhenJoiningANewServer()
{
//Given
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
PlayerPreference firstPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
string firstIpAddress = "127.0.0.1";
playerPreferenceManager.SetPreference(firstIpAddress, firstPreference);
PlayerPreference secondPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
string secondIpAddress = "123.456.789.321";
playerPreferenceManager.SetPreference(secondIpAddress, secondPreference);
PlayerPreference thirdPreference = new PlayerPreference(TestConstants.TEST_PLAYER_NAME, RandomColorGenerator.GenerateColor().ToUnity());
Color expectedColor = thirdPreference.PreferredColor();
string thirdIpAddress = "000.000.000.000";
playerPreferenceManager.SetPreference(thirdIpAddress, thirdPreference);
//When
PlayerPreference result = playerPreferenceManager.GetPreference(TestConstants.TEST_IP_ADDRESS);
//Then
result.PlayerName.Should().Be(thirdPreference.PlayerName);
result.RedAdditive.Should().Be(expectedColor.r);
result.GreenAdditive.Should().Be(expectedColor.g);
result.BlueAdditive.Should().Be(expectedColor.b);
}
[TestMethod]
public void ShouldBeAbleToRetrieveADefaultPreferenceWhenThePlayerHasNone()
{
//Given
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
//When
PlayerPreference result = playerPreferenceManager.GetPreference(TestConstants.TEST_IP_ADDRESS);
//Then
result.Should().NotBeNull();
result.PlayerName.Should().BeNullOrEmpty();
}
[TestMethod]
public void GetPreferenceShouldThrowExceptionWhenGivenNullIpAddress()
{
//Arrange
PlayerPreferenceState playerPreferenceState = new PlayerPreferenceState();
playerPreferenceState.Preferences = new Dictionary<string, PlayerPreference>();
IPreferenceStateProvider stateProvider = Substitute.For<IPreferenceStateProvider>();
stateProvider.GetPreferenceState().Returns(playerPreferenceState);
PlayerPreferenceManager playerPreferenceManager = new PlayerPreferenceManager(stateProvider);
//Act
Action action = () => playerPreferenceManager.GetPreference(null);
//Assert
action.Should().Throw<ArgumentNullException>();
}
}
}