UnitTest1.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using GenericDisplayCommand;
  2. using GenericDisplayCommand.Controls;
  3. using GenericDisplayCommand.Controls.V1;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Xml;
  11. using System.Xml.Serialization;
  12. namespace GenericDisplayCommandTest
  13. {
  14. [TestClass]
  15. public class UnitTest1
  16. {
  17. private LinearLayout ConstuctCarPlateLabel(string carPlate, string hint, string hintColor)
  18. {
  19. var label = new LinearLayout()
  20. {
  21. Id = carPlate,
  22. Height = "match_parent",
  23. Width = "match_parent",
  24. Orientation = "horizontal",
  25. AllowActions = new List<AllowAction>() { AllowAction.Select, AllowAction.Drag, AllowAction.Drop },
  26. };
  27. label.Views.Add(new TextView()
  28. {
  29. Text = carPlate,
  30. Background = "White",
  31. AllowActions = new List<AllowAction>() { AllowAction.Select, AllowAction.Drag }
  32. });
  33. label.Views.Add(new TextView() { Text = hint, Background = hintColor, PaddingLeft = "2dp" });
  34. return label;
  35. }
  36. public class A
  37. {
  38. public LinearLayout Host { get; set; }
  39. }
  40. [TestMethod]
  41. public void TestMethod1()
  42. {
  43. LinearLayout advLinearLayout = new LinearLayout();
  44. advLinearLayout.Views.Add(new VideoView() { Src = "mp4.avi" });
  45. advLinearLayout.Orientation = "vertical";
  46. advLinearLayout.Height = "0dp";
  47. advLinearLayout.WeightSum = 4.ToString();
  48. advLinearLayout.Width = "match_parent";
  49. LinearLayout middleLinearLayout = new LinearLayout()
  50. {
  51. Orientation = "horizontal",
  52. Height = "0dp",
  53. Weight = 3.ToString(CultureInfo.InvariantCulture),
  54. Width = "match_parent",
  55. };
  56. #region middle_left
  57. LinearLayout middle_left_LinearLayout = new LinearLayout()
  58. {
  59. Height = "match_parent",
  60. Width = "0dp",
  61. Weight = (7.5f).ToString(CultureInfo.InvariantCulture),
  62. };
  63. var carPlatesGridView = new GridView() { NumColumns = 3 };
  64. List<Tuple<string, string>> carPlateLabels = new List<Tuple<string, string>>();
  65. carPlateLabels.Add(new Tuple<string, string>("ACD343", "车牌付"));
  66. carPlateLabels.Add(new Tuple<string, string>("BCDFSADF", "普通用户"));
  67. carPlateLabels.Add(new Tuple<string, string>("CDF23423DF", "车牌付"));
  68. carPlateLabels.Add(new Tuple<string, string>("D232f23DF", "普通用户"));
  69. carPlateLabels.Add(new Tuple<string, string>("Eadfvvhjk3DF", "车牌付"));
  70. for (int i = 0; i < carPlateLabels.Count; i++)
  71. {
  72. carPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(carPlateLabels[i].Item1,
  73. carPlateLabels[i].Item2, "Yellow"));
  74. }
  75. middle_left_LinearLayout.Views.Add(carPlatesGridView);
  76. #endregion
  77. #region middle right
  78. LinearLayout middle_right_LinearLayout = new LinearLayout()
  79. {
  80. Height = "match_parent",
  81. Width = "0dp",
  82. Weight = 2.5f.ToString(CultureInfo.InvariantCulture),
  83. };
  84. var errorCarPlatesGridView = new GridView() { NumColumns = 1 };
  85. List<Tuple<string, string>> errorCarPlateLabels = new List<Tuple<string, string>>();
  86. errorCarPlateLabels.Add(new Tuple<string, string>("QCD343", "交易出错"));
  87. errorCarPlateLabels.Add(new Tuple<string, string>("TCDFSADF", "交易超时"));
  88. errorCarPlateLabels.Add(new Tuple<string, string>("XDF23423DF", "交易被取消"));
  89. errorCarPlateLabels.Add(new Tuple<string, string>("Y232f23DF", "交易状态未知"));
  90. errorCarPlateLabels.Add(new Tuple<string, string>("Zadfvvhjk3DF", "交易出错"));
  91. for (int i = 0; i < errorCarPlateLabels.Count; i++)
  92. {
  93. errorCarPlatesGridView.Views.Add(this.ConstuctCarPlateLabel(errorCarPlateLabels[i].Item1,
  94. errorCarPlateLabels[i].Item2, "Red"));
  95. }
  96. middle_right_LinearLayout.Views.Add(errorCarPlatesGridView);
  97. #endregion
  98. LinearLayout bottomLinearLayout = new LinearLayout()
  99. {
  100. Orientation = "horizontal",
  101. Height = "0dp",
  102. Weight = 3.ToString(CultureInfo.InvariantCulture),
  103. WeightSum = 10.ToString(CultureInfo.InvariantCulture),
  104. Width = "match_parent",
  105. };
  106. #region firstNozzleLinearLayout
  107. LinearLayout firstNozzleLinearLayout = new LinearLayout()
  108. {
  109. Orientation = "vertical",
  110. Height = "match_parent",
  111. Weight = 2.5f.ToString(CultureInfo.InvariantCulture),
  112. Width = "0dp",
  113. };
  114. List<Tuple<string, string>> firstNozzleBoundCarPlateLabels = new List<Tuple<string, string>>();
  115. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("QCD343", "车牌付加油中"));
  116. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("TCDFSADF", "车牌付待付"));
  117. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("XDF23423DF", "车牌付已支付"));
  118. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Y232f23DF", "车牌付已支付"));
  119. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Zadfvvhjk3DF", "车牌付已支付"));
  120. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("T232f23DF", "车牌付已支付"));
  121. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Qadfvvhjk3DF", "车牌付已支付"));
  122. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("H232f23DF", "车牌付已支付"));
  123. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Madfvvhjk3DF", "车牌付已支付"));
  124. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("N232f23DF", "车牌付已支付"));
  125. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Vadfvvhjk3DF", "车牌付已支付"));
  126. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("K232f23DF", "车牌付已支付"));
  127. firstNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Ladfvvhjk3DF", "车牌付已支付"));
  128. for (int i = 0; i < firstNozzleBoundCarPlateLabels.Count; i++)
  129. {
  130. firstNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(firstNozzleBoundCarPlateLabels[i].Item1,
  131. firstNozzleBoundCarPlateLabels[i].Item2, "Blue"));
  132. }
  133. firstNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 1 });
  134. #endregion
  135. bottomLinearLayout.Views.Add(firstNozzleLinearLayout);
  136. #region second NozzleLinearLayout
  137. LinearLayout secondNozzleLinearLayout = new LinearLayout()
  138. {
  139. Orientation = "vertical",
  140. Height = "match_parent",
  141. Weight = 2.5f.ToString(CultureInfo.InvariantCulture),
  142. Width = "0dp",
  143. };
  144. List<Tuple<string, string>> secondNozzleBoundCarPlateLabels = new List<Tuple<string, string>>();
  145. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("QCD343", "车牌付加油中"));
  146. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("TCDFSADF", "车牌付待付"));
  147. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("XDF23423DF", "车牌付已支付"));
  148. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Y232f23DF", "车牌付已支付"));
  149. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Zadfvvhjk3DF", "车牌付已支付"));
  150. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("T232f23DF", "车牌付已支付"));
  151. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Qadfvvhjk3DF", "车牌付已支付"));
  152. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("H232f23DF", "车牌付已支付"));
  153. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Madfvvhjk3DF", "车牌付已支付"));
  154. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("N232f23DF", "车牌付已支付"));
  155. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Vadfvvhjk3DF", "车牌付已支付"));
  156. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("K232f23DF", "车牌付已支付"));
  157. secondNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Ladfvvhjk3DF", "车牌付已支付"));
  158. for (int i = 0; i < secondNozzleBoundCarPlateLabels.Count; i++)
  159. {
  160. secondNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(secondNozzleBoundCarPlateLabels[i].Item1,
  161. secondNozzleBoundCarPlateLabels[i].Item2, "Blue"));
  162. }
  163. secondNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 2 });
  164. #endregion
  165. bottomLinearLayout.Views.Add(secondNozzleLinearLayout);
  166. #region third NozzleLinearLayout
  167. LinearLayout thirdNozzleLinearLayout = new LinearLayout()
  168. {
  169. Orientation = "vertical",
  170. Height = "match_parent",
  171. Weight = 2.5f.ToString(CultureInfo.InvariantCulture),
  172. Width = "0dp",
  173. };
  174. List<Tuple<string, string>> thirdNozzleBoundCarPlateLabels = new List<Tuple<string, string>>();
  175. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("QCD343", "车牌付加油中"));
  176. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("TCDFSADF", "车牌付待付"));
  177. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("XDF23423DF", "车牌付已支付"));
  178. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Y232f23DF", "车牌付已支付"));
  179. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Zadfvvhjk3DF", "车牌付已支付"));
  180. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("T232f23DF", "车牌付已支付"));
  181. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Qadfvvhjk3DF", "车牌付已支付"));
  182. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("H232f23DF", "车牌付已支付"));
  183. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Madfvvhjk3DF", "车牌付已支付"));
  184. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("N232f23DF", "车牌付已支付"));
  185. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Vadfvvhjk3DF", "车牌付已支付"));
  186. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("K232f23DF", "车牌付已支付"));
  187. thirdNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Ladfvvhjk3DF", "车牌付已支付"));
  188. for (int i = 0; i < thirdNozzleBoundCarPlateLabels.Count; i++)
  189. {
  190. thirdNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(thirdNozzleBoundCarPlateLabels[i].Item1,
  191. thirdNozzleBoundCarPlateLabels[i].Item2, "Blue"));
  192. }
  193. thirdNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 3 });
  194. #endregion
  195. bottomLinearLayout.Views.Add(thirdNozzleLinearLayout);
  196. #region fourth NozzleLinearLayout
  197. LinearLayout fourthNozzleLinearLayout = new LinearLayout()
  198. {
  199. Orientation = "vertical",
  200. Height = "match_parent",
  201. Weight = 2.5f.ToString(CultureInfo.InvariantCulture),
  202. Width = "0dp",
  203. };
  204. List<Tuple<string, string>> fourthNozzleBoundCarPlateLabels = new List<Tuple<string, string>>();
  205. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("QCD343", "车牌付加油中"));
  206. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("TCDFSADF", "车牌付待付"));
  207. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("XDF23423DF", "车牌付已支付"));
  208. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Y232f23DF", "车牌付已支付"));
  209. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Zadfvvhjk3DF", "车牌付已支付"));
  210. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("T232f23DF", "车牌付已支付"));
  211. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Qadfvvhjk3DF", "车牌付已支付"));
  212. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("H232f23DF", "车牌付已支付"));
  213. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Madfvvhjk3DF", "车牌付已支付"));
  214. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("N232f23DF", "车牌付已支付"));
  215. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Vadfvvhjk3DF", "车牌付已支付"));
  216. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("K232f23DF", "车牌付已支付"));
  217. fourthNozzleBoundCarPlateLabels.Add(new Tuple<string, string>("Ladfvvhjk3DF", "车牌付已支付"));
  218. for (int i = 0; i < fourthNozzleBoundCarPlateLabels.Count; i++)
  219. {
  220. fourthNozzleLinearLayout.Views.Add(this.ConstuctCarPlateLabel(fourthNozzleBoundCarPlateLabels[i].Item1,
  221. fourthNozzleBoundCarPlateLabels[i].Item2, "Blue"));
  222. }
  223. fourthNozzleLinearLayout.Views.Add(new NozzleView() { PumpId = 3, NozzleId = 4 });
  224. #endregion
  225. bottomLinearLayout.Views.Add(fourthNozzleLinearLayout);
  226. LinearLayout root = new LinearLayout();
  227. root.Orientation = "vertical";
  228. root.Height = "match_parent";
  229. root.Width = "match_parent";
  230. root.WeightSum = 10.ToString(CultureInfo.InvariantCulture);
  231. root.Views.Add(advLinearLayout);
  232. root.Views.Add(middleLinearLayout);
  233. root.Views.Add(bottomLinearLayout);
  234. var genericDisplayCommand = new GenericDisplayCommandV1Wrapper() { Version = 1, Control = root };
  235. XmlSerializer xmlSerializer = new XmlSerializer(typeof(GenericDisplayCommandV1Wrapper),
  236. new Type[] { typeof(LinearLayout), typeof(VideoView), typeof(TextView), typeof(NozzleView) });
  237. //MemoryStream ms = new MemoryStream();
  238. //xmlSerializer.Serialize(ms, root);
  239. var message = "";
  240. using (var sww = new StringWriter())
  241. {
  242. using (XmlWriter writer = XmlWriter.Create(sww))
  243. {
  244. xmlSerializer.Serialize(writer, genericDisplayCommand);
  245. var xml = sww.ToString(); // Your XML
  246. message = xml;
  247. }
  248. }
  249. //string json = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.None,
  250. // new JsonSerializerSettings
  251. // {
  252. // NullValueHandling = NullValueHandling.Ignore
  253. // });
  254. }
  255. /// <summary>
  256. /// Test the deserialize method of message type GernericDisplayCommand
  257. /// </summary>
  258. [TestMethod]
  259. public void TestMethod2()
  260. {
  261. var rootLinearlayout = new LinearLayout();
  262. var plateLinearlayout = new LinearLayout() { Id = "978"};
  263. plateLinearlayout.AllowActions = new List<AllowAction>() { AllowAction.Drag };
  264. var pumpLinearlayout = new LinearLayout() { Id = "13", AllowActions = new List<AllowAction>(){AllowAction.Drop}};
  265. rootLinearlayout.Views.Add(plateLinearlayout);
  266. rootLinearlayout.Views.Add(pumpLinearlayout);
  267. var genericDisplayCommand = new GenericDisplayCommandV1Wrapper(){Control = rootLinearlayout,Version = 1};
  268. var head = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  269. var s= /*head +*/ @"<GenericDisplayCommand><LinearLayout><Height></Height><Id></Id><LinearLayout><AllowActions><AllowAction>Drag</AllowAction></AllowActions ><Height></Height><Id>978</Id><Orientation></Orientation><Weight>0</Weight><WeightSum>0</WeightSum><Width>0</Width></LinearLayout><LinearLayout><AllowActions><AllowAction>Drop</AllowAction></AllowActions><Height>0</Height><Id>13</Id><Orientation></Orientation><Weight>0</Weight><WeightSum>0</WeightSum><Width>0</Width></LinearLayout><Orientation></Orientation><Weight>0</Weight><WeightSum>0</WeightSum><Width>0</Width></LinearLayout><Version>1</Version></GenericDisplayCommand> ";
  270. XmlSerializer xmldeSerializer = new XmlSerializer(typeof(GenericDisplayCommandV1Wrapper),
  271. new Type[] { typeof(LinearLayout), typeof(VideoView), typeof(TextView), typeof(NozzleView) });
  272. s = @"<GenericDisplayCommand><LinearLayout><AllowActions><AllowAction>Submit</AllowAction></AllowActions><Height></Height><Id>436</Id><Orientation></Orientation><Weight></Weight><WeightSum></WeightSum><Width></Width></LinearLayout><Version>1</Version></GenericDisplayCommand>";
  273. var message = "";
  274. using (var sww = new StringWriter())
  275. {
  276. using (XmlWriter writer = XmlWriter.Create(sww))
  277. {
  278. xmldeSerializer.Serialize(writer, genericDisplayCommand);
  279. var xml = sww.ToString(); // Your XML
  280. message = xml;
  281. }
  282. }
  283. using (var sr = new StringReader(s))
  284. {
  285. var result = (GenericDisplayCommandV1Wrapper)xmldeSerializer.Deserialize(sr);
  286. }
  287. }
  288. }
  289. }