Add a test that creates, deletes and creates a character (#2193)

* Add a test that creates, deletes and creates a character

* My prs conflict with my other prs

Bottom text

* Add checking for profile equality between client and server

* Fix randomizing outside of an assert and add equals/hashcode and fix equality check

* Fix colors being slightly off when received by the server
This commit is contained in:
DrSmugleaf
2020-10-12 18:26:17 +02:00
committed by GitHub
parent 4c72109cdf
commit 43a156bd3b
3 changed files with 156 additions and 13 deletions

View File

@@ -0,0 +1,125 @@
using System.Threading.Tasks;
using Content.Client;
using Content.Client.Interfaces;
using Content.Client.State;
using Content.Server.GameTicking;
using Content.Server.Interfaces;
using Content.Server.Interfaces.GameTicking;
using Content.Server.Preferences;
using Content.Shared;
using Content.Shared.Preferences;
using NUnit.Framework;
using Robust.Client.Interfaces.State;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.Interfaces.Network;
namespace Content.IntegrationTests.Tests.Lobby
{
[TestFixture]
[TestOf(typeof(ClientPreferencesManager))]
[TestOf(typeof(ServerPreferencesManager))]
public class CharacterCreationTest : ContentIntegrationTest
{
[Test]
public async Task CreateDeleteCreateTest()
{
var (client, server) = await StartConnectedServerClientPair();
var clientNetManager = client.ResolveDependency<IClientNetManager>();
var clientStateManager = client.ResolveDependency<IStateManager>();
var clientPrefManager = client.ResolveDependency<IClientPreferencesManager>();
var serverConfig = server.ResolveDependency<IConfigurationManager>();
var serverTicker = server.ResolveDependency<IGameTicker>();
var serverPrefManager = server.ResolveDependency<IServerPreferencesManager>();
await server.WaitIdleAsync();
await client.WaitIdleAsync();
await server.WaitAssertion(() =>
{
var lobbyCvar = CCVars.GameLobbyEnabled;
serverConfig.SetCVar(lobbyCvar.Name, true);
serverTicker.RestartRound();
});
Assert.That(serverTicker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
await WaitUntil(client, () => clientStateManager.CurrentState is LobbyState, maxTicks: 60);
Assert.NotNull(clientNetManager.ServerChannel);
var clientNetId = clientNetManager.ServerChannel.UserId;
HumanoidCharacterProfile profile = null;
await client.WaitAssertion(() =>
{
clientPrefManager.SelectCharacter(0);
var clientCharacters = clientPrefManager.Preferences.Characters;
Assert.That(clientCharacters.Count, Is.EqualTo(1));
Assert.That(clientStateManager.CurrentState, Is.TypeOf<LobbyState>());
profile = HumanoidCharacterProfile.Random();
clientPrefManager.CreateCharacter(profile);
clientCharacters = clientPrefManager.Preferences.Characters;
Assert.That(clientCharacters.Count, Is.EqualTo(2));
Assert.That(clientCharacters[1].MemberwiseEquals(profile));
});
await WaitUntil(server, () => serverPrefManager.GetPreferences(clientNetId).Characters.Count == 2, maxTicks: 60);
await server.WaitAssertion(() =>
{
var serverCharacters = serverPrefManager.GetPreferences(clientNetId).Characters;
Assert.That(serverCharacters.Count, Is.EqualTo(2));
Assert.That(serverCharacters[1].MemberwiseEquals(profile));
});
await client.WaitAssertion(() =>
{
clientPrefManager.DeleteCharacter(1);
var clientCharacters = clientPrefManager.Preferences.Characters.Count;
Assert.That(clientCharacters, Is.EqualTo(1));
});
await WaitUntil(server, () => serverPrefManager.GetPreferences(clientNetId).Characters.Count == 1, maxTicks: 60);
await server.WaitAssertion(() =>
{
var serverCharacters = serverPrefManager.GetPreferences(clientNetId).Characters.Count;
Assert.That(serverCharacters, Is.EqualTo(1));
});
await client.WaitIdleAsync();
await client.WaitAssertion(() =>
{
profile = HumanoidCharacterProfile.Random();
clientPrefManager.CreateCharacter(profile);
var clientCharacters = clientPrefManager.Preferences.Characters;
Assert.That(clientCharacters.Count, Is.EqualTo(2));
Assert.That(clientCharacters[1].MemberwiseEquals(profile));
});
await WaitUntil(server, () => serverPrefManager.GetPreferences(clientNetId).Characters.Count == 2, maxTicks: 60);
await server.WaitAssertion(() =>
{
var serverCharacters = serverPrefManager.GetPreferences(clientNetId).Characters;
Assert.That(serverCharacters.Count, Is.EqualTo(2));
Assert.That(serverCharacters[1].MemberwiseEquals(profile));
});
}
}
}

View File

@@ -20,11 +20,11 @@ namespace Content.Shared.Preferences
Color skinColor) Color skinColor)
{ {
HairStyleName = hairStyleName; HairStyleName = hairStyleName;
HairColor = hairColor; HairColor = ClampColor(hairColor);
FacialHairStyleName = facialHairStyleName; FacialHairStyleName = facialHairStyleName;
FacialHairColor = facialHairColor; FacialHairColor = ClampColor(facialHairColor);
EyeColor = eyeColor; EyeColor = ClampColor(eyeColor);
SkinColor = skinColor; SkinColor = ClampColor(skinColor);
} }
public string HairStyleName { get; } public string HairStyleName { get; }
@@ -102,6 +102,11 @@ namespace Content.Shared.Preferences
} }
} }
public static Color ClampColor(Color color)
{
return new Color(color.RByte, color.GByte, color.BByte);
}
public static HumanoidCharacterAppearance EnsureValid(HumanoidCharacterAppearance appearance) public static HumanoidCharacterAppearance EnsureValid(HumanoidCharacterAppearance appearance)
{ {
string hairStyleName; string hairStyleName;
@@ -136,22 +141,17 @@ namespace Content.Shared.Preferences
facialHairColor, facialHairColor,
eyeColor, eyeColor,
skinColor); skinColor);
static Color ClampColor(Color color)
{
return new Color(color.RByte, color.GByte, color.BByte);
}
} }
public bool MemberwiseEquals(ICharacterAppearance maybeOther) public bool MemberwiseEquals(ICharacterAppearance maybeOther)
{ {
if (!(maybeOther is HumanoidCharacterAppearance other)) return false; if (!(maybeOther is HumanoidCharacterAppearance other)) return false;
if (HairStyleName != other.HairStyleName) return false; if (HairStyleName != other.HairStyleName) return false;
if (HairColor != other.HairColor) return false; if (!HairColor.Equals(other.HairColor)) return false;
if (FacialHairStyleName != other.FacialHairStyleName) return false; if (FacialHairStyleName != other.FacialHairStyleName) return false;
if (FacialHairColor != other.FacialHairColor) return false; if (!FacialHairColor.Equals(other.FacialHairColor)) return false;
if (EyeColor != other.EyeColor) return false; if (!EyeColor.Equals(other.EyeColor)) return false;
if (SkinColor != other.SkinColor) return false; if (!SkinColor.Equals(other.SkinColor)) return false;
return true; return true;
} }
} }

View File

@@ -1,3 +1,4 @@
#nullable enable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -243,5 +244,22 @@ namespace Content.Shared.Preferences
if (!_antagPreferences.SequenceEqual(other._antagPreferences)) return false; if (!_antagPreferences.SequenceEqual(other._antagPreferences)) return false;
return Appearance.MemberwiseEquals(other.Appearance); return Appearance.MemberwiseEquals(other.Appearance);
} }
public override bool Equals(object? obj)
{
return obj is HumanoidCharacterProfile other && MemberwiseEquals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(
Name,
Age,
Sex,
PreferenceUnavailable,
_jobPriorities,
_antagPreferences,
Appearance);
}
} }
} }