AvaloniaResourcesIndexTests.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Avalonia.Utilities;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests;
  7. public class AvaloniaResourcesIndexTests
  8. {
  9. [Fact]
  10. public void Should_Write_And_Read_The_Same_Resources()
  11. {
  12. using var memoryStream = new MemoryStream();
  13. var fooBytes = Encoding.UTF8.GetBytes("foo");
  14. var booBytes = Encoding.UTF8.GetBytes("boo");
  15. AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream,
  16. new[]
  17. {
  18. new AvaloniaResourcesEntry
  19. {
  20. Path = "foo.xaml", Size = fooBytes.Length, Open = () => new MemoryStream(fooBytes)
  21. },
  22. new AvaloniaResourcesEntry
  23. {
  24. Path = "boo.xaml", Size = booBytes.Length, Open = () => new MemoryStream(booBytes)
  25. }
  26. });
  27. memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field.
  28. var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream);
  29. var resourcesBasePosition = memoryStream.Position;
  30. Span<byte> buffer = stackalloc byte[index[0].Size];
  31. Assert.Equal("foo.xaml", index[0].Path);
  32. Assert.Equal(0, index[0].Offset);
  33. Assert.Equal(fooBytes.Length, index[0].Size);
  34. memoryStream.Seek(resourcesBasePosition + index[0].Offset, SeekOrigin.Begin);
  35. memoryStream.ReadExactly(buffer);
  36. Assert.Equal(fooBytes, buffer.ToArray());
  37. Assert.Equal("boo.xaml", index[1].Path);
  38. Assert.Equal(fooBytes.Length, index[1].Offset);
  39. Assert.Equal(booBytes.Length, index[1].Size);
  40. memoryStream.Seek(resourcesBasePosition + index[1].Offset, SeekOrigin.Begin);
  41. memoryStream.ReadExactly(buffer);
  42. Assert.Equal(booBytes, buffer.ToArray());
  43. }
  44. [Fact]
  45. public void Should_Combined_Same_Physical_Path_Resources()
  46. {
  47. using var memoryStream = new MemoryStream();
  48. var resourceBytes = Encoding.UTF8.GetBytes("resource-data");
  49. AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream, new[]
  50. {
  51. new AvaloniaResourcesEntry
  52. {
  53. Path = "app.xaml",
  54. SystemPath = "app.ico",
  55. Size = resourceBytes.Length,
  56. Open = () => new MemoryStream(resourceBytes)
  57. },
  58. new AvaloniaResourcesEntry
  59. {
  60. Path = "!__AvaloniaDefaultWindowIcon",
  61. SystemPath = "app.ico",
  62. Size = resourceBytes.Length,
  63. Open = () => new MemoryStream(resourceBytes)
  64. }
  65. });
  66. memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field.
  67. var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream);
  68. Assert.Equal("app.xaml", index[0].Path);
  69. Assert.Equal(0, index[0].Offset);
  70. Assert.Equal(resourceBytes.Length, index[0].Size);
  71. Assert.Equal("!__AvaloniaDefaultWindowIcon", index[1].Path);
  72. Assert.Equal(0, index[1].Offset);
  73. Assert.Equal(resourceBytes.Length, index[1].Size);
  74. }
  75. }