using GenericDisplayCommand; using GenericDisplayCommand.Controls; using GenericDisplayCommand.Controls.V1; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Xml.Serialization; using Wayne.FDCPOSInterface; using Wayne.FDCPOSLibrary; namespace FdcServerTest { [TestClass] public class FdcServerTcpHandler_GenericDisplayCommandV1Wrapper_Test { public static bool ValueEquals(IEnumerable array1, IEnumerable array2) { if (array1 == null && array2 == null) { return true; } if ((array1 == null) || (array2 == null)) { return false; } if (array1.Count() != array2.Count()) { return false; } if (array1.Equals(array2)) { return true; } else { for (int Index = 0; Index < array1.Count(); Index++) { if (!Equals(array1.ElementAt(Index), array2.ElementAt(Index))) { return false; } } } return true; } public class Message { public string Content { get; set; } } [TestMethod] public void UTF8_content_xml_Serialize_Test0() { string msgStr = "hell"; var msg_ASCII = new Message() { Content = msgStr }; var xmlMsgBodyMemStream = new MemoryStream(); XmlSerializer serializer = new XmlSerializer(typeof(Message)); //StreamWriter default use UTF-8 encoding serializer.Serialize(xmlMsgBodyMemStream, msg_ASCII); byte[] xmlMsgBodyBytes_ascII = null; xmlMsgBodyMemStream.Position = 0; using (StreamReader _ = new StreamReader(xmlMsgBodyMemStream, Encoding.UTF8)) xmlMsgBodyBytes_ascII = Encoding.UTF8.GetBytes(_.ReadToEnd()); //================== msgStr = "你好你好"; xmlMsgBodyMemStream = new MemoryStream(); var msg_UTF8 = new Message() { Content = msgStr }; serializer.Serialize(xmlMsgBodyMemStream, msg_UTF8); byte[] xmlMsgBodyBytes_utf8 = null; xmlMsgBodyMemStream.Position = 0; using (StreamReader _ = new StreamReader(xmlMsgBodyMemStream, Encoding.UTF8)) xmlMsgBodyBytes_utf8 = Encoding.UTF8.GetBytes(_.ReadToEnd()); var byteLengthDiff = Encoding.UTF8.GetBytes(msg_UTF8.Content).Length - Encoding.UTF8.GetBytes(msg_ASCII.Content).Length; Assert.AreEqual(true, xmlMsgBodyBytes_utf8.Length > xmlMsgBodyBytes_ascII.Length); Assert.AreEqual(true, (xmlMsgBodyBytes_utf8.Length - byteLengthDiff) == xmlMsgBodyBytes_ascII.Length); } [TestMethod] public void SerializeFdcMessageToNetworkBytes_Test0() { string workStationId = "abcdef"; string appSendId = "i'm doing a test"; var innerMsg = this.PrepareGenericDisplayCommandWithChineseCharacterIn(); MemoryStream ms = new MemoryStream(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(GenericDisplayCommandWrapper)); xmlSerializer.Serialize(ms, innerMsg); ms.Position = 0; var innerMsgStr = new StreamReader(ms).ReadToEnd(); var outerMsg = new FDCMessageGenericTypelessMessage() { WorkstationID = workStationId, ApplicationSender = appSendId, MessageID = "9999", FDCdata = new FDCMessageFDCdataGenericTypelessMessage[] { new FDCMessageFDCdataGenericTypelessMessage() { FDCTimeStamp=FDCConvert.ToString(DateTime.Now), Message = innerMsgStr } } }; var raw = FdcServerTcpHandler.SerializeFdcMessageToNetworkBytes(outerMsg, true); // skip 4 bytes len, 16 bytes hash, then get the xml content bytes. var actual = raw.Item1.Skip(4 + 16).ToArray(); ms = new MemoryStream(); xmlSerializer = new XmlSerializer(typeof(FDCMessageGenericTypelessMessage)); xmlSerializer.Serialize(ms, outerMsg); ms.Position = 0; var expect = Encoding.UTF8.GetBytes(new StreamReader(ms).ReadToEnd()); Assert.AreEqual(true, raw.Item2.Length != raw.Item1.Length, "Chinese character contained string should have its (char length) > its (bytes length)"); Assert.AreEqual(true, ValueEquals(actual, expect)); } [TestMethod] public void SerializeFdcMessageToNetworkBytes_Test1() { string workStationId = "abcdef"; string appSendId = "i'm doing a test"; var innerMsg = this.PrepareGenericDisplayCommandWithOnlyAscIICharacterIn_Simple(); MemoryStream ms = new MemoryStream(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(GenericDisplayCommandWrapper)); xmlSerializer.Serialize(ms, innerMsg); ms.Position = 0; var innerMsgStr = new StreamReader(ms).ReadToEnd(); var outerMsg = new FDCMessageGenericTypelessMessage() { WorkstationID = workStationId, ApplicationSender = appSendId, MessageID = "9999", FDCdata = new FDCMessageFDCdataGenericTypelessMessage[] { new FDCMessageFDCdataGenericTypelessMessage() { FDCTimeStamp=FDCConvert.ToString(DateTime.Now), Message = innerMsgStr } } }; var raw = FdcServerTcpHandler.SerializeFdcMessageToNetworkBytes(outerMsg, true); // skip 4 bytes len, 16 bytes hash, then get the xml content bytes. var actual = raw.Item1.Skip(4 + 16).ToArray(); ms = new MemoryStream(); xmlSerializer = new XmlSerializer(typeof(FDCMessageGenericTypelessMessage)); xmlSerializer.Serialize(ms, outerMsg); ms.Position = 0; var expect = Encoding.UTF8.GetBytes(new StreamReader(ms).ReadToEnd()); Assert.AreEqual(true, (raw.Item2.Length + 4 + 16) == raw.Item1.Length, "Full AscII character contained string should have its (char length) == its (bytes length)"); Assert.AreEqual(true, ValueEquals(actual, expect)); } [TestMethod] public void SerializeFdcMessageToNetworkBytes_Test2() { string workStationId = "abcdef"; string appSendId = "i'm doing a test"; var innerMsg = this.PrepareGenericDisplayCommandWithOnlyAscIICharacterIn(); MemoryStream ms = new MemoryStream(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(GenericDisplayCommandWrapper)); xmlSerializer.Serialize(ms, innerMsg); ms.Position = 0; var innerMsgStr = new StreamReader(ms).ReadToEnd(); var outerMsg = new FDCMessageGenericTypelessMessage() { WorkstationID = workStationId, ApplicationSender = appSendId, MessageID = "9999", FDCdata = new FDCMessageFDCdataGenericTypelessMessage[] { new FDCMessageFDCdataGenericTypelessMessage() { FDCTimeStamp=FDCConvert.ToString(DateTime.Now), Message = innerMsgStr } } }; var raw = FdcServerTcpHandler.SerializeFdcMessageToNetworkBytes(outerMsg, true); // skip 4 bytes len, 16 bytes hash, then get the xml content bytes. var actual = raw.Item1.Skip(4 + 16).ToArray(); ms = new MemoryStream(); xmlSerializer = new XmlSerializer(typeof(FDCMessageGenericTypelessMessage)); xmlSerializer.Serialize(ms, outerMsg); ms.Position = 0; var expect = Encoding.UTF8.GetBytes(new StreamReader(ms).ReadToEnd()); Assert.AreEqual(true, (raw.Item2.Length + 4 + 16) == raw.Item1.Length, "Full AscII character contained string should have its (char length) == its (bytes length)"); Assert.AreEqual(true, ValueEquals(actual, expect)); } private GenericDisplayCommandWrapper PrepareGenericDisplayCommandWithOnlyAscIICharacterIn_Simple() { LinearLayout root = new LinearLayout(); root.Orientation = "vertical"; root.Height = "match_parent"; root.Width = "match_parent"; root.WeightSum = 10.ToString(CultureInfo.InvariantCulture); var genericDisplayCommand = new GenericDisplayCommandV1Wrapper() { Version = 1, Control = root }; return genericDisplayCommand; } private GenericDisplayCommandWrapper PrepareGenericDisplayCommandWithOnlyAscIICharacterIn() { LinearLayout advLinearLayout = new LinearLayout(); advLinearLayout.Views.Add(new VideoView() { Src = "mp4.avi" }); advLinearLayout.Orientation = "vertical"; advLinearLayout.Height = "0dp"; advLinearLayout.WeightSum = 4.ToString(CultureInfo.InvariantCulture); advLinearLayout.Width = "match_parent"; LinearLayout middleLinearLayout = new LinearLayout() { Orientation = "horizontal", Height = "0dp", Weight = 3.ToString(CultureInfo.InvariantCulture), Width = "match_parent", }; #region middle_left LinearLayout middle_left_LinearLayout = new LinearLayout() { Height = "match_parent", Width = "0dp", Weight = 7.5f.ToString(CultureInfo.InvariantCulture), }; var carPlatesGridView = new GridView() { NumColumns = 3 }; List> carPlateLabels = new List>(); carPlateLabels.Add(new Tuple("ACD343", "11")); carPlateLabels.Add(new Tuple("BCDFSADF", "22")); carPlateLabels.Add(new Tuple("CDF23423DF", "33")); carPlateLabels.Add(new Tuple("D232f23DF", "44")); carPlateLabels.Add(new Tuple("Eadfvvhjk3DF", "55")); for (int i = 0; i < carPlateLabels.Count; i++) { carPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(carPlateLabels[i].Item1, carPlateLabels[i].Item2, "Yellow")); } middle_left_LinearLayout.Views.Add(carPlatesGridView); #endregion #region middle right LinearLayout middle_right_LinearLayout = new LinearLayout() { Height = "match_parent", Width = "0dp", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), }; var errorCarPlatesGridView = new GridView() { NumColumns = 1 }; List> errorCarPlateLabels = new List>(); errorCarPlateLabels.Add(new Tuple("QCD343", "66")); errorCarPlateLabels.Add(new Tuple("TCDFSADF", "77")); errorCarPlateLabels.Add(new Tuple("XDF23423DF", "88")); errorCarPlateLabels.Add(new Tuple("Y232f23DF", "99")); errorCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "00")); for (int i = 0; i < errorCarPlateLabels.Count; i++) { errorCarPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(errorCarPlateLabels[i].Item1, errorCarPlateLabels[i].Item2, "Red")); } middle_right_LinearLayout.Views.Add(errorCarPlatesGridView); #endregion LinearLayout bottomLinearLayout = new LinearLayout() { Orientation = "horizontal", Height = "0dp", Weight = 3.ToString(CultureInfo.InvariantCulture), WeightSum = 10.ToString(CultureInfo.InvariantCulture), Width = "match_parent", }; #region firstNozzleLinearLayout LinearLayout firstNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> firstNozzleBoundCarPlateLabels = new List>(); firstNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "11")); for (int i = 0; i < firstNozzleBoundCarPlateLabels.Count; i++) { firstNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(firstNozzleBoundCarPlateLabels[i].Item1, firstNozzleBoundCarPlateLabels[i].Item2, "Blue")); } firstNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 1 }); #endregion bottomLinearLayout.Views.Add(firstNozzleLinearLayout); #region second NozzleLinearLayout LinearLayout secondNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> secondNozzleBoundCarPlateLabels = new List>(); secondNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "aa")); for (int i = 0; i < secondNozzleBoundCarPlateLabels.Count; i++) { secondNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(secondNozzleBoundCarPlateLabels[i].Item1, secondNozzleBoundCarPlateLabels[i].Item2, "Blue")); } secondNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 2 }); #endregion bottomLinearLayout.Views.Add(secondNozzleLinearLayout); #region third NozzleLinearLayout LinearLayout thirdNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> thirdNozzleBoundCarPlateLabels = new List>(); thirdNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "cc")); for (int i = 0; i < thirdNozzleBoundCarPlateLabels.Count; i++) { thirdNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(thirdNozzleBoundCarPlateLabels[i].Item1, thirdNozzleBoundCarPlateLabels[i].Item2, "Blue")); } thirdNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 3 }); #endregion bottomLinearLayout.Views.Add(thirdNozzleLinearLayout); #region fourth NozzleLinearLayout LinearLayout fourthNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> fourthNozzleBoundCarPlateLabels = new List>(); fourthNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "1111")); for (int i = 0; i < fourthNozzleBoundCarPlateLabels.Count; i++) { fourthNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(fourthNozzleBoundCarPlateLabels[i].Item1, fourthNozzleBoundCarPlateLabels[i].Item2, "Blue")); } fourthNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 4 }); #endregion bottomLinearLayout.Views.Add(fourthNozzleLinearLayout); LinearLayout root = new LinearLayout(); root.Orientation = "vertical"; root.Height = "match_parent"; root.Width = "match_parent"; root.WeightSum = 10.ToString(CultureInfo.InvariantCulture); root.Views.Add(advLinearLayout); root.Views.Add(middleLinearLayout); root.Views.Add(bottomLinearLayout); var genericDisplayCommand = new GenericDisplayCommandV1Wrapper() { Version = 1, Control = root }; return genericDisplayCommand; } private GenericDisplayCommandWrapper PrepareGenericDisplayCommandWithChineseCharacterIn() { LinearLayout advLinearLayout = new LinearLayout(); advLinearLayout.Views.Add(new VideoView() { Src = "mp4.avi" }); advLinearLayout.Orientation = "vertical"; advLinearLayout.Height = "0dp"; advLinearLayout.WeightSum = 4.ToString(CultureInfo.InvariantCulture); advLinearLayout.Width = "match_parent"; LinearLayout middleLinearLayout = new LinearLayout() { Orientation = "horizontal", Height = "0dp", Weight = 3.ToString(CultureInfo.InvariantCulture), Width = "match_parent", }; #region middle_left LinearLayout middle_left_LinearLayout = new LinearLayout() { Height = "match_parent", Width = "0dp", Weight = 7.5f.ToString(CultureInfo.InvariantCulture), }; var carPlatesGridView = new GridView() { NumColumns = 3 }; List> carPlateLabels = new List>(); carPlateLabels.Add(new Tuple("ACD343", "你好世界你好世界你好世界你好世界")); for (int i = 0; i < carPlateLabels.Count; i++) { carPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(carPlateLabels[i].Item1, carPlateLabels[i].Item2, "Yellow")); } middle_left_LinearLayout.Views.Add(carPlatesGridView); #endregion #region middle right LinearLayout middle_right_LinearLayout = new LinearLayout() { Height = "match_parent", Width = "0dp", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), }; var errorCarPlatesGridView = new GridView() { NumColumns = 1 }; List> errorCarPlateLabels = new List>(); errorCarPlateLabels.Add(new Tuple("QCD343", "66")); errorCarPlateLabels.Add(new Tuple("TCDFSADF", "77")); errorCarPlateLabels.Add(new Tuple("XDF23423DF", "88")); errorCarPlateLabels.Add(new Tuple("Y232f23DF", "99")); errorCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "00")); for (int i = 0; i < errorCarPlateLabels.Count; i++) { errorCarPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(errorCarPlateLabels[i].Item1, errorCarPlateLabels[i].Item2, "Red")); } middle_right_LinearLayout.Views.Add(errorCarPlatesGridView); #endregion LinearLayout bottomLinearLayout = new LinearLayout() { Orientation = "horizontal", Height = "0dp", Weight = 3.ToString(CultureInfo.InvariantCulture), WeightSum = 10.ToString(CultureInfo.InvariantCulture), Width = "match_parent", }; #region firstNozzleLinearLayout LinearLayout firstNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> firstNozzleBoundCarPlateLabels = new List>(); firstNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "11")); for (int i = 0; i < firstNozzleBoundCarPlateLabels.Count; i++) { firstNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(firstNozzleBoundCarPlateLabels[i].Item1, firstNozzleBoundCarPlateLabels[i].Item2, "Blue")); } firstNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 1 }); #endregion bottomLinearLayout.Views.Add(firstNozzleLinearLayout); #region second NozzleLinearLayout LinearLayout secondNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> secondNozzleBoundCarPlateLabels = new List>(); secondNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "aa")); for (int i = 0; i < secondNozzleBoundCarPlateLabels.Count; i++) { secondNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(secondNozzleBoundCarPlateLabels[i].Item1, secondNozzleBoundCarPlateLabels[i].Item2, "Blue")); } secondNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 2 }); #endregion bottomLinearLayout.Views.Add(secondNozzleLinearLayout); #region third NozzleLinearLayout LinearLayout thirdNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> thirdNozzleBoundCarPlateLabels = new List>(); thirdNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "cc")); for (int i = 0; i < thirdNozzleBoundCarPlateLabels.Count; i++) { thirdNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(thirdNozzleBoundCarPlateLabels[i].Item1, thirdNozzleBoundCarPlateLabels[i].Item2, "Blue")); } thirdNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 3 }); #endregion bottomLinearLayout.Views.Add(thirdNozzleLinearLayout); #region fourth NozzleLinearLayout LinearLayout fourthNozzleLinearLayout = new LinearLayout() { Orientation = "vertical", Height = "match_parent", Weight = 2.5f.ToString(CultureInfo.InvariantCulture), Width = "0dp", }; List> fourthNozzleBoundCarPlateLabels = new List>(); fourthNozzleBoundCarPlateLabels.Add(new Tuple("QCD343", "1111")); for (int i = 0; i < fourthNozzleBoundCarPlateLabels.Count; i++) { fourthNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(fourthNozzleBoundCarPlateLabels[i].Item1, fourthNozzleBoundCarPlateLabels[i].Item2, "Blue")); } fourthNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 4 }); #endregion bottomLinearLayout.Views.Add(fourthNozzleLinearLayout); LinearLayout root = new LinearLayout(); root.Orientation = "vertical"; root.Height = "match_parent"; root.Width = "match_parent"; root.WeightSum = 10.ToString(CultureInfo.InvariantCulture); root.Views.Add(advLinearLayout); root.Views.Add(middleLinearLayout); root.Views.Add(bottomLinearLayout); var genericDisplayCommand = new GenericDisplayCommandV1Wrapper() { Version = 1, Control = root }; return genericDisplayCommand; } private LinearLayout ConstuctCarPlateLabel(string carPlate, string hint, string hintColor) { var label = new LinearLayout() { Id = carPlate, Height = "match_parent", Width = "match_parent", Orientation = "horizontal", AllowActions = new List() { AllowAction.Select, AllowAction.Drag, AllowAction.Drop }, }; label.Views.Add(new TextView() { Text = carPlate, Background = "White", AllowActions = new List() { AllowAction.Select, AllowAction.Drag } }); label.Views.Add(new TextView() { Text = hint, Background = hintColor, PaddingLeft = "2dp" }); return label; } } }