Compare commits
10 Commits
feature/te
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
302536575c | ||
|
|
53c0cd515d | ||
|
|
3963e07568 | ||
|
|
b13493537e | ||
|
|
9199265eb6 | ||
|
|
9292919f26 | ||
|
|
8018c9c6af | ||
|
|
880d4b4424 | ||
|
|
7601db667c | ||
|
|
b1b03e136d |
161
Eindproject/Tests/JSONConvertCanvasMessages.cs
Normal file
161
Eindproject/Tests/JSONConvertCanvasMessages.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertCanvasMessages
|
||||
{
|
||||
//Helper method for the tests
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(payload));
|
||||
return json;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructCanvasDataSend()
|
||||
{
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = { 10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
Color color = Color.FromRgb(0, 0, 0);
|
||||
|
||||
byte[] message = JSONConvert.ConstructCanvasDataSend(type, coordinateInfo, color);
|
||||
byte[] payload = GetPayload(message);
|
||||
dynamic json = GetDynamic(payload);
|
||||
int ID = json.canvasType;
|
||||
JArray coorArray = json.coords;
|
||||
double[][] coordinates = coorArray.ToObject<double[][]>();
|
||||
Color colorResult = json.color;
|
||||
|
||||
Assert.AreEqual(0x04, message[4]);
|
||||
Assert.AreEqual(type, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData");
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructDrawingCanvasData()
|
||||
{
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = {10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
Color color = Color.FromRgb(0, 0, 0);
|
||||
|
||||
byte[] message = JSONConvert.ConstructDrawingCanvasData(coordinateInfo, color);
|
||||
byte[] payload = GetPayload(message);
|
||||
dynamic json = GetDynamic(payload);
|
||||
int ID = json.canvasType;
|
||||
JArray coorArray = json.coords;
|
||||
double[][] coordinates = coorArray.ToObject<double[][]>();
|
||||
Color colorResult = json.color;
|
||||
|
||||
Assert.AreEqual(0x04, message[4]);
|
||||
Assert.AreEqual(JSONConvert.CANVAS_WRITING, ID, "The canvas type message is not correct on the ConstructDrawingCanvasData");
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
Assert.AreEqual(color, colorResult, "color is not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructCanvasReset()
|
||||
{
|
||||
byte[] canvasReset = JSONConvert.ConstructCanvasReset();
|
||||
dynamic json = GetDynamic(GetPayload(canvasReset));
|
||||
int ID = json.canvasType;
|
||||
|
||||
Assert.AreEqual(JSONConvert.CANVAS_RESET, ID, $"Canvas type should be reset(1)! Not, {ID}");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetCanvasMessageType()
|
||||
{
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
int resultID = JSONConvert.GetCanvasMessageType(payload);
|
||||
|
||||
Assert.AreEqual(type, resultID, $"Canvas type should be {IDsend}! Not, {resultID}");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestgetCoordinates()
|
||||
{
|
||||
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
double[][] coordinateInfo = new double[2][];
|
||||
double[] coordinatesOne = { 10.0, 10.0, 3.0, 3.0 };
|
||||
double[] coordinatesTwo = { 10.0, 10.0, 3.0, 3.0 };
|
||||
coordinateInfo[0] = coordinatesOne;
|
||||
coordinateInfo[1] = coordinatesTwo;
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type,
|
||||
coords = coordinateInfo
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
|
||||
double[][] coordinates = JSONConvert.getCoordinates(payload);
|
||||
|
||||
for (int i = 0; i < coordinateInfo.Length; i++)
|
||||
{
|
||||
CollectionAssert.AreEqual(coordinateInfo[i], coordinates[i], "Coordinates are not correct on the ConstructDrawingCanvasData");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetCanvasDrawingColor()
|
||||
{
|
||||
|
||||
int type = JSONConvert.CANVAS_WRITING;
|
||||
byte IDsend = JSONConvert.CANVAS;
|
||||
Color colorSend = Color.FromRgb(0, 0, 0);
|
||||
dynamic payloadSend = new
|
||||
{
|
||||
canvasType = type,
|
||||
color = colorSend
|
||||
};
|
||||
byte[] message = JSONConvert.GetMessageToSend(IDsend, payloadSend);
|
||||
byte[] payload = GetPayload(message);
|
||||
|
||||
Color colorResult = JSONConvert.getCanvasDrawingColor(payload);
|
||||
|
||||
Assert.AreEqual(colorSend, colorResult, "Colors are not equal!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
100
Eindproject/Tests/JSONConvertGameCommand.cs
Normal file
100
Eindproject/Tests/JSONConvertGameCommand.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertGameCommand
|
||||
{
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] payload)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(payload)));
|
||||
}
|
||||
|
||||
public byte[] GameCommandMessage()
|
||||
{
|
||||
byte identifier = 0x05;
|
||||
dynamic payload = new
|
||||
{
|
||||
command = JSONConvert.GameCommand.NEXT_ROUND
|
||||
};
|
||||
|
||||
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public byte[] GameLobbyID()
|
||||
{
|
||||
byte identifier = 0x05;
|
||||
dynamic payload = new
|
||||
{
|
||||
lobbyToStart = 1
|
||||
};
|
||||
|
||||
byte[] res = JSONConvert.GetMessageToSend(identifier, payload);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructGameStartData()
|
||||
{
|
||||
byte[] lobbyData = JSONConvert.ConstructGameStartData(1);
|
||||
dynamic payload = GetDynamic(lobbyData);
|
||||
|
||||
int lobbyid = 1;
|
||||
JSONConvert.GameCommand gameCommand = payload.command;
|
||||
|
||||
Assert.AreEqual(0x05, lobbyData[4]);
|
||||
Assert.AreEqual(JSONConvert.GameCommand.START_GAME, gameCommand);
|
||||
Assert.AreEqual(lobbyid, (int)payload.lobbyToStart);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestConstructGameTimerElapsedMessage()
|
||||
{
|
||||
byte[] lobbyData = JSONConvert.ConstructGameTimerElapsedMessage(1);
|
||||
dynamic payload = GetDynamic(lobbyData);
|
||||
|
||||
int lobbyid = 1;
|
||||
JSONConvert.GameCommand gameCommand = payload.command;
|
||||
|
||||
Assert.AreEqual(0x05, lobbyData[4]);
|
||||
Assert.AreEqual(JSONConvert.GameCommand.TIMER_ELAPSED, gameCommand);
|
||||
Assert.AreEqual(lobbyid, (int)payload.id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetGameCommand()
|
||||
{
|
||||
byte[] data = GetPayload(GameCommandMessage());
|
||||
JSONConvert.GameCommand gameCommand = JSONConvert.GetGameCommand(data);
|
||||
|
||||
Assert.AreEqual(JSONConvert.GameCommand.NEXT_ROUND, gameCommand);
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetStartGameLobbyID()
|
||||
{
|
||||
byte[] data = GetPayload(GameLobbyID());
|
||||
int lobbyID = JSONConvert.GetStartGameLobbyID(data);
|
||||
|
||||
int expected = 1;
|
||||
|
||||
Assert.AreEqual(expected, lobbyID);
|
||||
}
|
||||
}
|
||||
}
|
||||
203
Eindproject/Tests/JSONConvertLobbyMessagesTests.cs
Normal file
203
Eindproject/Tests/JSONConvertLobbyMessagesTests.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
using Client;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SharedClientServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
[TestClass]
|
||||
public class JSONConvertLobbyMessagesTests
|
||||
{
|
||||
public byte[] GetPayload(byte[] message)
|
||||
{
|
||||
byte[] payload = new byte[message.Length - 5];
|
||||
Array.Copy(message, 5, payload, 0, message.Length - 5);
|
||||
return payload;
|
||||
}
|
||||
|
||||
public dynamic GetDynamic(byte[] res)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(GetPayload(res)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyHostMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyHostMessage();
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyHostCreatedMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyHostCreatedMessage(3);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.HOST, identifier);
|
||||
Assert.AreEqual(3, id);
|
||||
}
|
||||
[TestMethod]
|
||||
public void TestLobbyRequestMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyRequestMessage();
|
||||
dynamic payload = GetDynamic(res);
|
||||
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, identifier);
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyListMessage()
|
||||
{
|
||||
|
||||
Lobby[] lobbies = new Lobby[] { new Lobby(3, 0, 9), new Lobby(1, 0, 3), new Lobby(1, 0, 1) };
|
||||
byte[] res = JSONConvert.ConstructLobbyListMessage(lobbies);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
JArray lobbiesJArray = payload.lobbies;
|
||||
Lobby[] lobbiesFromDynamic = lobbiesJArray.ToObject<Lobby[]>();
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.LIST, identifier);
|
||||
for (int i = 0; i < lobbies.Length; i++)
|
||||
{
|
||||
Lobby l1 = lobbies[i];
|
||||
Lobby l2 = lobbiesFromDynamic[i];
|
||||
Assert.AreEqual(l1.ID, l2.ID);
|
||||
Assert.AreEqual(l1.PlayersIn, l2.PlayersIn);
|
||||
Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyJoinMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyJoinMessage(8);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN, identifier);
|
||||
Assert.AreEqual(8, id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyLeaveMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyLeaveMessage(4);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
int id = payload.id;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.LEAVE, identifier);
|
||||
Assert.AreEqual(4, id);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbyIdentifier()
|
||||
{
|
||||
dynamic res = new
|
||||
{
|
||||
identifier = JSONConvert.LobbyIdentifier.REQUEST
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
|
||||
JSONConvert.LobbyIdentifier lobbyIdentifier = JSONConvert.GetLobbyIdentifier(arr);
|
||||
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.REQUEST, lobbyIdentifier);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestLobbyJoinSuccessMessage()
|
||||
{
|
||||
byte[] res = JSONConvert.ConstructLobbyJoinSuccessMessage(true);
|
||||
|
||||
dynamic payload = GetDynamic(res);
|
||||
JSONConvert.LobbyIdentifier identifier = payload.identifier;
|
||||
bool host = payload.host;
|
||||
|
||||
Assert.AreEqual(0x03, res[4]);
|
||||
Assert.AreEqual(JSONConvert.LobbyIdentifier.JOIN_SUCCESS, identifier);
|
||||
Assert.IsTrue(host);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbiesFromMessage()
|
||||
{
|
||||
Lobby[] lobbiesArray = new Lobby[] { new Lobby(7, 0, 8), new Lobby(3, 0, 5), new Lobby(5, 0, 10) };
|
||||
dynamic res = new
|
||||
{
|
||||
lobbies = lobbiesArray
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
Lobby[] testLobbies = JSONConvert.GetLobbiesFromMessage(arr);
|
||||
|
||||
for (int i = 0; i < lobbiesArray.Length; i++)
|
||||
{
|
||||
Lobby l1 = lobbiesArray[i];
|
||||
Lobby l2 = testLobbies[i];
|
||||
Assert.AreEqual(l1.ID, l2.ID);
|
||||
Assert.AreEqual(l1.PlayersIn, l2.PlayersIn);
|
||||
Assert.AreEqual(l1.MaxPlayers, l2.MaxPlayers);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobbyID()
|
||||
{
|
||||
dynamic res = new
|
||||
{
|
||||
id = 5
|
||||
};
|
||||
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
int testID = JSONConvert.GetLobbyID(arr);
|
||||
|
||||
Assert.AreEqual(5, testID);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetLobby()
|
||||
{
|
||||
Lobby l = new Lobby(6, 0, 9);
|
||||
dynamic res = new
|
||||
{
|
||||
lobby = l
|
||||
};
|
||||
byte[] arr = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
|
||||
|
||||
Lobby testLobby = JSONConvert.GetLobby(arr);
|
||||
|
||||
Assert.AreEqual(l.ID, testLobby.ID);
|
||||
Assert.AreEqual(l.MaxPlayers, testLobby.MaxPlayers);
|
||||
Assert.AreEqual(l.PlayersIn, testLobby.PlayersIn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user