2
0

NativeControlSerializationTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Text;
  2. using PixiEditor.Extensions.CommonApi.FlyUI;
  3. using PixiEditor.Extensions.Sdk.Api.FlyUI;
  4. namespace PixiEditor.Extensions.Sdk.Tests;
  5. public class NativeControlSerializationTest
  6. {
  7. [Fact]
  8. public void TestThatNoChildLayoutSerializesCorrectBytes()
  9. {
  10. CompiledControl layout = new CompiledControl(0, "Layout");
  11. layout.AddProperty("Title");
  12. int uniqueId = 0;
  13. byte[] uniqueIdBytes = BitConverter.GetBytes(uniqueId);
  14. string controlId = "Layout";
  15. byte[] controlIdBytes = Encoding.UTF8.GetBytes(controlId);
  16. int propertiesCount = 1;
  17. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  18. int stringLen = "Title".Length;
  19. byte[] stringLenBytes = BitConverter.GetBytes(stringLen);
  20. byte[] titleBytes = Encoding.UTF8.GetBytes("Title");
  21. int childCount = 0;
  22. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  23. List<byte> expectedBytes = new();
  24. expectedBytes.AddRange(uniqueIdBytes);
  25. expectedBytes.AddRange(BitConverter.GetBytes(controlId.Length));
  26. expectedBytes.AddRange(controlIdBytes);
  27. expectedBytes.AddRange(propertiesCountBytes);
  28. expectedBytes.Add(ByteMap.GetTypeByteId(typeof(string)));
  29. expectedBytes.AddRange(stringLenBytes);
  30. expectedBytes.AddRange(titleBytes);
  31. expectedBytes.AddRange(childCountBytes);
  32. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  33. }
  34. [Fact]
  35. public void TestThatChildLayoutSerializesCorrectBytes()
  36. {
  37. CompiledControl layout = new CompiledControl(0, "Layout");
  38. layout.AddChild(new CompiledControl(1, "Center"));
  39. int uniqueId = 0;
  40. byte[] uniqueIdBytes = BitConverter.GetBytes(uniqueId);
  41. string controlId = "Layout";
  42. byte[] controlIdBytes = Encoding.UTF8.GetBytes(controlId);
  43. int propertiesCount = 0;
  44. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  45. int childCount = 1;
  46. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  47. int childUniqueId = 1;
  48. byte[] childUniqueIdBytes = BitConverter.GetBytes(childUniqueId);
  49. string childControlId = "Center";
  50. byte[] childControlIdBytes = Encoding.UTF8.GetBytes(childControlId);
  51. int childPropertiesCount = 0;
  52. byte[] childPropertiesCountBytes = BitConverter.GetBytes(childPropertiesCount);
  53. int childChildCount = 0;
  54. byte[] childChildCountBytes = BitConverter.GetBytes(childChildCount);
  55. List<byte> expectedBytes = new();
  56. expectedBytes.AddRange(uniqueIdBytes);
  57. expectedBytes.AddRange(BitConverter.GetBytes(controlId.Length));
  58. expectedBytes.AddRange(controlIdBytes);
  59. expectedBytes.AddRange(propertiesCountBytes);
  60. expectedBytes.AddRange(childCountBytes);
  61. expectedBytes.AddRange(childUniqueIdBytes);
  62. expectedBytes.AddRange(BitConverter.GetBytes(childControlId.Length));
  63. expectedBytes.AddRange(childControlIdBytes);
  64. expectedBytes.AddRange(childPropertiesCountBytes);
  65. expectedBytes.AddRange(childChildCountBytes);
  66. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  67. }
  68. [Fact]
  69. public void TestThatChildNestedLayoutSerializesCorrectBytes()
  70. {
  71. CompiledControl layout = new CompiledControl(0, "Layout");
  72. CompiledControl center = new CompiledControl(1, "Center");
  73. CompiledControl text = new CompiledControl(2, "Text");
  74. text.AddProperty("Hello world");
  75. center.AddChild(text);
  76. layout.AddChild(center);
  77. int uniqueId = 0;
  78. byte[] uniqueIdBytes = BitConverter.GetBytes(uniqueId);
  79. string controlId = "Layout";
  80. byte[] controlIdBytes = Encoding.UTF8.GetBytes(controlId);
  81. int propertiesCount = 0;
  82. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  83. int childCount = 1;
  84. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  85. int childUniqueId = 1;
  86. byte[] childUniqueIdBytes = BitConverter.GetBytes(childUniqueId);
  87. string childControlId = "Center";
  88. byte[] childControlIdBytes = Encoding.UTF8.GetBytes(childControlId);
  89. int childPropertiesCount = 0;
  90. byte[] childPropertiesCountBytes = BitConverter.GetBytes(childPropertiesCount);
  91. int childChildCount = 1;
  92. byte[] childChildCountBytes = BitConverter.GetBytes(childChildCount);
  93. int textUniqueId = 2;
  94. byte[] textUniqueIdBytes = BitConverter.GetBytes(textUniqueId);
  95. string textControlId = "Text";
  96. byte[] textControlIdBytes = Encoding.UTF8.GetBytes(textControlId);
  97. int textPropertiesCount = 1;
  98. byte[] textPropertiesCountBytes = BitConverter.GetBytes(textPropertiesCount);
  99. int textStringLen = "Hello world".Length;
  100. byte[] textStringLenBytes = BitConverter.GetBytes(textStringLen);
  101. byte[] textTitleBytes = Encoding.UTF8.GetBytes("Hello world");
  102. int textChildCount = 0;
  103. byte[] textChildCountBytes = BitConverter.GetBytes(textChildCount);
  104. List<byte> expectedBytes = new();
  105. expectedBytes.AddRange(uniqueIdBytes);
  106. expectedBytes.AddRange(BitConverter.GetBytes(controlId.Length));
  107. expectedBytes.AddRange(controlIdBytes);
  108. expectedBytes.AddRange(propertiesCountBytes);
  109. expectedBytes.AddRange(childCountBytes);
  110. expectedBytes.AddRange(childUniqueIdBytes);
  111. expectedBytes.AddRange(BitConverter.GetBytes(childControlId.Length));
  112. expectedBytes.AddRange(childControlIdBytes);
  113. expectedBytes.AddRange(childPropertiesCountBytes);
  114. expectedBytes.AddRange(childChildCountBytes);
  115. expectedBytes.AddRange(textUniqueIdBytes);
  116. expectedBytes.AddRange(BitConverter.GetBytes(textControlId.Length));
  117. expectedBytes.AddRange(textControlIdBytes);
  118. expectedBytes.AddRange(textPropertiesCountBytes);
  119. expectedBytes.Add(ByteMap.GetTypeByteId(typeof(string)));
  120. expectedBytes.AddRange(textStringLenBytes);
  121. expectedBytes.AddRange(textTitleBytes);
  122. expectedBytes.AddRange(textChildCountBytes);
  123. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  124. }
  125. [Fact]
  126. public void TestThatLayoutBuilderProperlyConvertsToNativeControls()
  127. {
  128. Layout layout = new Layout(
  129. new Center(
  130. child: new Text("hello sexy.")));
  131. CompiledControl compiledControl = layout.BuildNative();
  132. Assert.Equal("Layout", compiledControl.ControlTypeId);
  133. Assert.Empty(compiledControl.Properties);
  134. Assert.Single(compiledControl.Children);
  135. Assert.Equal("Center", compiledControl.Children[0].ControlTypeId);
  136. Assert.Empty(compiledControl.Children[0].Properties);
  137. Assert.Equal("Text", compiledControl.Children[0].Children[0].ControlTypeId);
  138. Assert.True(compiledControl.Children[0].Children[0].Properties.Count > 0);
  139. Assert.Equal("hello sexy.", compiledControl.Children[0].Children[0].Properties[0].value);
  140. }
  141. [Fact]
  142. public void TestThatBuildButtonQueuesEvents()
  143. {
  144. Button button = new Button(
  145. child: new Text("hello sexy."),
  146. onClick: _ => { });
  147. button.BuildNative();
  148. Assert.Contains(button.BuildQueuedEvents, x => x == "Click");
  149. }
  150. }