NativeControlSerializationTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Text;
  2. using PixiEditor.Extensions.CommonApi.LayoutBuilding;
  3. using PixiEditor.Extensions.Wasm.Api.LayoutBuilding;
  4. namespace PixiEditor.Extensions.Wasm.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 controlId = ByteMap.ControlMap["Layout"];
  13. byte[] controlIdBytes = BitConverter.GetBytes(controlId);
  14. int propertiesCount = 1;
  15. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  16. int stringLen = "Title".Length;
  17. byte[] stringLenBytes = BitConverter.GetBytes(stringLen);
  18. byte[] titleBytes = Encoding.UTF8.GetBytes("Title");
  19. int childCount = 0;
  20. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  21. List<byte> expectedBytes = new();
  22. expectedBytes.AddRange(controlIdBytes);
  23. expectedBytes.AddRange(propertiesCountBytes);
  24. expectedBytes.Add(ByteMap.GetTypeByteId(typeof(string)));
  25. expectedBytes.AddRange(stringLenBytes);
  26. expectedBytes.AddRange(titleBytes);
  27. expectedBytes.AddRange(childCountBytes);
  28. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  29. }
  30. [Fact]
  31. public void TestThatChildLayoutSerializesCorrectBytes()
  32. {
  33. CompiledControl layout = new CompiledControl(0, "Layout");
  34. layout.AddChild(new CompiledControl(0, "Center"));
  35. int controlId = ByteMap.ControlMap["Layout"];
  36. byte[] controlIdBytes = BitConverter.GetBytes(controlId);
  37. int propertiesCount = 0;
  38. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  39. int childCount = 1;
  40. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  41. int childControlId = ByteMap.ControlMap["Center"];
  42. byte[] childControlIdBytes = BitConverter.GetBytes(childControlId);
  43. int childPropertiesCount = 0;
  44. byte[] childPropertiesCountBytes = BitConverter.GetBytes(childPropertiesCount);
  45. int childChildCount = 0;
  46. byte[] childChildCountBytes = BitConverter.GetBytes(childChildCount);
  47. List<byte> expectedBytes = new();
  48. expectedBytes.AddRange(controlIdBytes);
  49. expectedBytes.AddRange(propertiesCountBytes);
  50. expectedBytes.AddRange(childCountBytes);
  51. expectedBytes.AddRange(childControlIdBytes);
  52. expectedBytes.AddRange(childPropertiesCountBytes);
  53. expectedBytes.AddRange(childChildCountBytes);
  54. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  55. }
  56. [Fact]
  57. public void TestThatChildNestedLayoutSerializesCorrectBytes()
  58. {
  59. CompiledControl layout = new CompiledControl(0, "Layout");
  60. CompiledControl center = new CompiledControl(1, "Center");
  61. CompiledControl text = new CompiledControl(2, "Text");
  62. text.AddProperty("Hello world");
  63. center.AddChild(text);
  64. layout.AddChild(center);
  65. int controlId = ByteMap.ControlMap["Layout"];
  66. byte[] controlIdBytes = BitConverter.GetBytes(controlId);
  67. int propertiesCount = 0;
  68. byte[] propertiesCountBytes = BitConverter.GetBytes(propertiesCount);
  69. int childCount = 1;
  70. byte[] childCountBytes = BitConverter.GetBytes(childCount);
  71. int childControlId = ByteMap.ControlMap["Center"];
  72. byte[] childControlIdBytes = BitConverter.GetBytes(childControlId);
  73. int childPropertiesCount = 0;
  74. byte[] childPropertiesCountBytes = BitConverter.GetBytes(childPropertiesCount);
  75. int childChildCount = 1;
  76. byte[] childChildCountBytes = BitConverter.GetBytes(childChildCount);
  77. int textControlId = ByteMap.ControlMap["Text"];
  78. byte[] textControlIdBytes = BitConverter.GetBytes(textControlId);
  79. int textPropertiesCount = 1;
  80. byte[] textPropertiesCountBytes = BitConverter.GetBytes(textPropertiesCount);
  81. int textStringLen = "Hello world".Length;
  82. byte[] textStringLenBytes = BitConverter.GetBytes(textStringLen);
  83. byte[] textTitleBytes = Encoding.UTF8.GetBytes("Hello world");
  84. int textChildCount = 0;
  85. byte[] textChildCountBytes = BitConverter.GetBytes(textChildCount);
  86. List<byte> expectedBytes = new();
  87. expectedBytes.AddRange(controlIdBytes);
  88. expectedBytes.AddRange(propertiesCountBytes);
  89. expectedBytes.AddRange(childCountBytes);
  90. expectedBytes.AddRange(childControlIdBytes);
  91. expectedBytes.AddRange(childPropertiesCountBytes);
  92. expectedBytes.AddRange(childChildCountBytes);
  93. expectedBytes.AddRange(textControlIdBytes);
  94. expectedBytes.AddRange(textPropertiesCountBytes);
  95. expectedBytes.Add(ByteMap.GetTypeByteId(typeof(string)));
  96. expectedBytes.AddRange(textStringLenBytes);
  97. expectedBytes.AddRange(textTitleBytes);
  98. expectedBytes.AddRange(textChildCountBytes);
  99. Assert.Equal(expectedBytes.ToArray(), layout.Serialize().ToArray());
  100. }
  101. [Fact]
  102. public void TestThatLayoutBuilderProperlyConvertsToNativeControls()
  103. {
  104. Layout layout = new Layout(
  105. new Center(
  106. child: new Text("hello sexy.")));
  107. CompiledControl compiledControl = layout.Build();
  108. Assert.Equal("Layout", compiledControl.ControlTypeId);
  109. Assert.Empty(compiledControl.Properties);
  110. Assert.Single(compiledControl.Children);
  111. Assert.Equal("Center", compiledControl.Children[0].ControlTypeId);
  112. Assert.Empty(compiledControl.Children[0].Properties);
  113. Assert.Equal("Text", compiledControl.Children[0].Children[0].ControlTypeId);
  114. Assert.Single(compiledControl.Children[0].Children[0].Properties);
  115. Assert.Equal("hello sexy.", compiledControl.Children[0].Children[0].Properties[0]);
  116. }
  117. [Fact]
  118. public void TestThatBuildButtonQueuesEvents()
  119. {
  120. Button button = new Button(
  121. child: new Text("hello sexy."),
  122. onClick: _ => { });
  123. button.Build();
  124. Assert.Contains(button.BuildQueuedEvents, x => x == "Click");
  125. }
  126. }