NativeControlSerializationTest.cs 8.0 KB

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