-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to combine AvaloniaResources with the same system path (#15302)
* Try to combine AvaloniaResources with the same system path * Fix test assert
- Loading branch information
Showing
4 changed files
with
152 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
tests/Avalonia.Base.UnitTests/Utilities/AvaloniaResourcesIndexTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Avalonia.Utilities; | ||
using Xunit; | ||
|
||
namespace Avalonia.Base.UnitTests; | ||
|
||
public class AvaloniaResourcesIndexTests | ||
{ | ||
[Fact] | ||
public void Should_Write_And_Read_The_Same_Resources() | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
|
||
var fooBytes = Encoding.UTF8.GetBytes("foo"); | ||
var booBytes = Encoding.UTF8.GetBytes("boo"); | ||
AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream, | ||
new[] | ||
{ | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "foo.xaml", Size = fooBytes.Length, Open = () => new MemoryStream(fooBytes) | ||
}, | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "boo.xaml", Size = booBytes.Length, Open = () => new MemoryStream(booBytes) | ||
} | ||
}); | ||
|
||
memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field. | ||
|
||
var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream); | ||
var resourcesBasePosition = memoryStream.Position; | ||
|
||
Span<byte> buffer = stackalloc byte[index[0].Size]; | ||
|
||
Assert.Equal("foo.xaml", index[0].Path); | ||
Assert.Equal(0, index[0].Offset); | ||
Assert.Equal(fooBytes.Length, index[0].Size); | ||
|
||
memoryStream.Seek(resourcesBasePosition + index[0].Offset, SeekOrigin.Begin); | ||
memoryStream.ReadExactly(buffer); | ||
Assert.Equal(fooBytes, buffer.ToArray()); | ||
|
||
Assert.Equal("boo.xaml", index[1].Path); | ||
Assert.Equal(fooBytes.Length, index[1].Offset); | ||
Assert.Equal(booBytes.Length, index[1].Size); | ||
|
||
memoryStream.Seek(resourcesBasePosition + index[1].Offset, SeekOrigin.Begin); | ||
memoryStream.ReadExactly(buffer); | ||
Assert.Equal(booBytes, buffer.ToArray()); | ||
} | ||
|
||
[Fact] | ||
public void Should_Combined_Same_Physical_Path_Resources() | ||
{ | ||
using var memoryStream = new MemoryStream(); | ||
|
||
var resourceBytes = Encoding.UTF8.GetBytes("resource-data"); | ||
AvaloniaResourcesIndexReaderWriter.WriteResources(memoryStream, new[] | ||
{ | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "app.xaml", | ||
SystemPath = "app.ico", | ||
Size = resourceBytes.Length, | ||
Open = () => new MemoryStream(resourceBytes) | ||
}, | ||
new AvaloniaResourcesEntry | ||
{ | ||
Path = "!__AvaloniaDefaultWindowIcon", | ||
SystemPath = "app.ico", | ||
Size = resourceBytes.Length, | ||
Open = () => new MemoryStream(resourceBytes) | ||
} | ||
}); | ||
|
||
memoryStream.Seek(4, SeekOrigin.Begin); // skip 4 bytes for "index size" field. | ||
|
||
var index = AvaloniaResourcesIndexReaderWriter.ReadIndex(memoryStream); | ||
|
||
Assert.Equal("app.xaml", index[0].Path); | ||
Assert.Equal(0, index[0].Offset); | ||
Assert.Equal(resourceBytes.Length, index[0].Size); | ||
|
||
Assert.Equal("!__AvaloniaDefaultWindowIcon", index[1].Path); | ||
Assert.Equal(0, index[1].Offset); | ||
Assert.Equal(resourceBytes.Length, index[1].Size); | ||
} | ||
} |