using GenericDisplayCommand; using GenericDisplayCommand.Controls; using GenericDisplayCommand.Controls.V1; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Xml; using System.Xml.Serialization; namespace GenericDisplayCommandTest { [TestClass] public class UnitTest1 { 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; } public class A { public LinearLayout Host { get; set; } } [TestMethod] public void TestMethod1() { LinearLayout advLinearLayout = new LinearLayout(); advLinearLayout.Views.Add(new VideoView() { Src = "mp4.avi" }); advLinearLayout.Orientation = "vertical"; advLinearLayout.Height = "0dp"; advLinearLayout.WeightSum = 4.ToString(); 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", "车牌付")); carPlateLabels.Add(new Tuple("BCDFSADF", "普通用户")); carPlateLabels.Add(new Tuple("CDF23423DF", "车牌付")); carPlateLabels.Add(new Tuple("D232f23DF", "普通用户")); carPlateLabels.Add(new Tuple("Eadfvvhjk3DF", "车牌付")); 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", "交易出错")); errorCarPlateLabels.Add(new Tuple("TCDFSADF", "交易超时")); errorCarPlateLabels.Add(new Tuple("XDF23423DF", "交易被取消")); errorCarPlateLabels.Add(new Tuple("Y232f23DF", "交易状态未知")); errorCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "交易出错")); 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", "车牌付加油中")); firstNozzleBoundCarPlateLabels.Add(new Tuple("TCDFSADF", "车牌付待付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("XDF23423DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Y232f23DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("T232f23DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Qadfvvhjk3DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("H232f23DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Madfvvhjk3DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("N232f23DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Vadfvvhjk3DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("K232f23DF", "车牌付已支付")); firstNozzleBoundCarPlateLabels.Add(new Tuple("Ladfvvhjk3DF", "车牌付已支付")); 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", "车牌付加油中")); secondNozzleBoundCarPlateLabels.Add(new Tuple("TCDFSADF", "车牌付待付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("XDF23423DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Y232f23DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("T232f23DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Qadfvvhjk3DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("H232f23DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Madfvvhjk3DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("N232f23DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Vadfvvhjk3DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("K232f23DF", "车牌付已支付")); secondNozzleBoundCarPlateLabels.Add(new Tuple("Ladfvvhjk3DF", "车牌付已支付")); 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", "车牌付加油中")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("TCDFSADF", "车牌付待付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("XDF23423DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Y232f23DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("T232f23DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Qadfvvhjk3DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("H232f23DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Madfvvhjk3DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("N232f23DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Vadfvvhjk3DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("K232f23DF", "车牌付已支付")); thirdNozzleBoundCarPlateLabels.Add(new Tuple("Ladfvvhjk3DF", "车牌付已支付")); 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", "车牌付加油中")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("TCDFSADF", "车牌付待付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("XDF23423DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Y232f23DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Zadfvvhjk3DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("T232f23DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Qadfvvhjk3DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("H232f23DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Madfvvhjk3DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("N232f23DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Vadfvvhjk3DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("K232f23DF", "车牌付已支付")); fourthNozzleBoundCarPlateLabels.Add(new Tuple("Ladfvvhjk3DF", "车牌付已支付")); 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 }; XmlSerializer xmlSerializer = new XmlSerializer(typeof(GenericDisplayCommandV1Wrapper), new Type[] { typeof(LinearLayout), typeof(VideoView), typeof(TextView), typeof(NozzleView) }); //MemoryStream ms = new MemoryStream(); //xmlSerializer.Serialize(ms, root); var message = ""; using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xmlSerializer.Serialize(writer, genericDisplayCommand); var xml = sww.ToString(); // Your XML message = xml; } } //string json = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.None, // new JsonSerializerSettings // { // NullValueHandling = NullValueHandling.Ignore // }); } /// /// Test the deserialize method of message type GernericDisplayCommand /// [TestMethod] public void TestMethod2() { var rootLinearlayout = new LinearLayout(); var plateLinearlayout = new LinearLayout() { Id = "978"}; plateLinearlayout.AllowActions = new List() { AllowAction.Drag }; var pumpLinearlayout = new LinearLayout() { Id = "13", AllowActions = new List(){AllowAction.Drop}}; rootLinearlayout.Views.Add(plateLinearlayout); rootLinearlayout.Views.Add(pumpLinearlayout); var genericDisplayCommand = new GenericDisplayCommandV1Wrapper(){Control = rootLinearlayout,Version = 1}; var head = ""; var s= /*head +*/ @"Drag978000Drop0130000001 "; XmlSerializer xmldeSerializer = new XmlSerializer(typeof(GenericDisplayCommandV1Wrapper), new Type[] { typeof(LinearLayout), typeof(VideoView), typeof(TextView), typeof(NozzleView) }); s = @"Submit4361"; var message = ""; using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xmldeSerializer.Serialize(writer, genericDisplayCommand); var xml = sww.ToString(); // Your XML message = xml; } } using (var sr = new StringReader(s)) { var result = (GenericDisplayCommandV1Wrapper)xmldeSerializer.Deserialize(sr); } } } }